stream: use validateNumber for BYOB reader options.min

The error thrown for a non-number min is unchanged.

Signed-off-by: greenhead <shren0812@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65014
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
greenhead authored and Antoine du Hamel committed 2026-08-27 14:17:05 +02:00
1 parent 39ecedbbd2
commit 3a174ce16b
2 files changed
+43 -2

No files matched your search

+2 -2
View File
@@ -62,6 +62,7 @@ const {
const {
validateAbortSignal,
validateBuffer,
validateNumber,
validateObject,
kValidateObjectAllowObjects,
kValidateObjectAllowObjectsAndNull,
@@ -1086,8 +1087,7 @@ class ReadableStreamBYOBReader {
// detached, but there's no API available to use to check that.
const min = options?.min ?? 1;
if (typeof min !== 'number')
throw new ERR_INVALID_ARG_TYPE('options.min', 'number', min);
validateNumber(min, 'options.min');
if (!NumberIsInteger(min))
throw new ERR_INVALID_ARG_VALUE('options.min', min, 'must be an integer');
if (min <= 0)
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common');
const assert = require('node:assert');
const {
ReadableStream,
} = require('node:stream/web');
// Validation of the options.min argument of ReadableStreamBYOBReader.read()
// must reject with the same errors regardless of how the checks are implemented internally.
const reader = new ReadableStream({ type: 'bytes' })
.getReader({ mode: 'byob' });
(async () => {
// A null min is not covered here: `options?.min ?? 1` turns it into
// the default before validation, so it never reaches the type check.
for (const min of ['1', true, {}, [], 1n]) {
await assert.rejects(
reader.read(new Uint8Array(8), { min }),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
}
for (const min of [NaN, 1.5, 0, -1]) {
await assert.rejects(
reader.read(new Uint8Array(8), { min }),
{ code: 'ERR_INVALID_ARG_VALUE' },
);
}
await assert.rejects(
reader.read(new Uint8Array(8), { min: 9 }),
{ code: 'ERR_OUT_OF_RANGE' },
);
await assert.rejects(
reader.read(new DataView(new ArrayBuffer(8)), { min: 9 }),
{ code: 'ERR_OUT_OF_RANGE' },
);
})().then(common.mustCall());