|
| 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()); |
0 commit comments