GeezerVison is showing github.com — stream: use validateNumber for BYOB reader options.min · nodejs/node@4f6f2cf
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/webstreams/readablestream.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -62,6 +62,7 @@ const { | 62 | 62 | const { | 63 | 63 | validateAbortSignal, | 64 | 64 | validateBuffer, | | 65 | + validateNumber, | 65 | 66 | validateObject, | 66 | 67 | kValidateObjectAllowObjects, | 67 | 68 | kValidateObjectAllowObjectsAndNull, | @@ -1086,8 +1087,7 @@ class ReadableStreamBYOBReader { | 1086 | 1087 | // detached, but there's no API available to use to check that. | 1087 | 1088 |
| 1088 | 1089 | const min = options?.min ?? 1; | 1089 | | - if (typeof min !== 'number') | 1090 | | - throw new ERR_INVALID_ARG_TYPE('options.min', 'number', min); | | 1090 | + validateNumber(min, 'options.min'); | 1091 | 1091 | if (!NumberIsInteger(min)) | 1092 | 1092 | throw new ERR_INVALID_ARG_VALUE('options.min', min, 'must be an integer'); | 1093 | 1093 | if (min <= 0) | |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const assert = require('node:assert'); | | 4 | + | | 5 | +const { | | 6 | + ReadableStream, | | 7 | +} = require('node:stream/web'); | | 8 | + | | 9 | +// Validation of the options.min argument of ReadableStreamBYOBReader.read() | | 10 | +// must reject with the same errors regardless of how the checks are implemented internally. | | 11 | + | | 12 | +const reader = new ReadableStream({ type: 'bytes' }) | | 13 | + .getReader({ mode: 'byob' }); | | 14 | + | | 15 | +(async () => { | | 16 | + // A null min is not covered here: `options?.min ?? 1` turns it into | | 17 | + // the default before validation, so it never reaches the type check. | | 18 | + for (const min of ['1', true, {}, [], 1n]) { | | 19 | + await assert.rejects( | | 20 | + reader.read(new Uint8Array(8), { min }), | | 21 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 22 | + ); | | 23 | + } | | 24 | + | | 25 | + for (const min of [NaN, 1.5, 0, -1]) { | | 26 | + await assert.rejects( | | 27 | + reader.read(new Uint8Array(8), { min }), | | 28 | + { code: 'ERR_INVALID_ARG_VALUE' }, | | 29 | + ); | | 30 | + } | | 31 | + | | 32 | + await assert.rejects( | | 33 | + reader.read(new Uint8Array(8), { min: 9 }), | | 34 | + { code: 'ERR_OUT_OF_RANGE' }, | | 35 | + ); | | 36 | + | | 37 | + await assert.rejects( | | 38 | + reader.read(new DataView(new ArrayBuffer(8)), { min: 9 }), | | 39 | + { code: 'ERR_OUT_OF_RANGE' }, | | 40 | + ); | | 41 | +})().then(common.mustCall()); |
|