Skip to content

Commit 5b2004a

Browse files
committed
stream: use validateNumber for options.min
Signed-off-by: Taeuk Ha <tomcat0519@naver.com>
1 parent 404b0cf commit 5b2004a

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

lib/internal/webstreams/readablestream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const {
6262
const {
6363
validateAbortSignal,
6464
validateBuffer,
65+
validateNumber,
6566
validateObject,
6667
kValidateObjectAllowObjects,
6768
kValidateObjectAllowObjectsAndNull,
@@ -1095,8 +1096,7 @@ class ReadableStreamBYOBReader {
10951096
// detached, but there's no API available to use to check that.
10961097

10971098
const min = options?.min ?? 1;
1098-
if (typeof min !== 'number')
1099-
throw new ERR_INVALID_ARG_TYPE('options.min', 'number', min);
1099+
validateNumber(min, 'options.min');
11001100
if (!NumberIsInteger(min))
11011101
throw new ERR_INVALID_ARG_VALUE('options.min', min, 'must be an integer');
11021102
if (min <= 0)

test/parallel/test-whatwg-readablebytestream-bad-buffers-and-views.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,38 @@ let pass = 0;
8585
.then(common.mustCall());
8686
}
8787

88+
{
89+
// options.min must be a number.
90+
const stream = new ReadableStream({
91+
start(c) {
92+
c.enqueue(new Uint8Array([1, 2, 3]));
93+
},
94+
type: 'bytes',
95+
});
96+
const reader = stream.getReader({ mode: 'byob' });
97+
98+
assert
99+
.rejects(reader.read(new Uint8Array(3), { min: 'not a number' }), {
100+
code: 'ERR_INVALID_ARG_TYPE',
101+
name: 'TypeError',
102+
})
103+
.then(common.mustCall());
104+
}
105+
106+
{
107+
// A valid numeric options.min still works as expected.
108+
const stream = new ReadableStream({
109+
start(c) {
110+
c.enqueue(new Uint8Array([1, 2, 3]));
111+
},
112+
type: 'bytes',
113+
});
114+
const reader = stream.getReader({ mode: 'byob' });
115+
116+
reader.read(new Uint8Array(3), { min: 1 }).then(common.mustCall(({ value, done }) => {
117+
assert.strictEqual(done, false);
118+
assert.deepStrictEqual([...value], [1, 2, 3]);
119+
}));
120+
}
121+
88122
process.on('exit', () => assert.strictEqual(pass, 2));

0 commit comments

Comments
 (0)