1'use strict'; 2const common = require('../common'); 3 4const assert = require('assert'); 5const stream = require('stream'); 6const { inspect } = require('util'); 7 8{ 9 // This test ensures that the stream implementation correctly handles values 10 // for highWaterMark which exceed the range of signed 32 bit integers and 11 // rejects invalid values. 12 13 // This number exceeds the range of 32 bit integer arithmetic but should still 14 // be handled correctly. 15 const ovfl = Number.MAX_SAFE_INTEGER; 16 17 const readable = stream.Readable({ highWaterMark: ovfl }); 18 assert.strictEqual(readable._readableState.highWaterMark, ovfl); 19 20 const writable = stream.Writable({ highWaterMark: ovfl }); 21 assert.strictEqual(writable._writableState.highWaterMark, ovfl); 22 23 for (const invalidHwm of [true, false, '5', {}, -5, NaN]) { 24 const expected = typeof invalidHwm === 'string' ? 25 invalidHwm : inspect(invalidHwm); 26 for (const type of [stream.Readable, stream.Writable]) { 27 assert.throws(() => { 28 type({ highWaterMark: invalidHwm }); 29 }, { 30 name: 'TypeError', 31 code: 'ERR_INVALID_OPT_VALUE', 32 message: 33 `The value "${expected}" is invalid for option "highWaterMark"` 34 }); 35 } 36 } 37} 38 39{ 40 // This test ensures that the push method's implementation 41 // correctly handles the edge case where the highWaterMark and 42 // the state.length are both zero 43 44 const readable = stream.Readable({ highWaterMark: 0 }); 45 46 for (let i = 0; i < 3; i++) { 47 const needMoreData = readable.push(); 48 assert.strictEqual(needMoreData, true); 49 } 50} 51 52{ 53 // This test ensures that the read(n) method's implementation 54 // correctly handles the edge case where the highWaterMark, state.length 55 // and n are all zero 56 57 const readable = stream.Readable({ highWaterMark: 0 }); 58 59 readable._read = common.mustCall(); 60 readable.read(0); 61} 62 63{ 64 // Parse size as decimal integer 65 ['1', '1.0', 1].forEach((size) => { 66 const readable = new stream.Readable({ 67 read: common.mustCall(), 68 highWaterMark: 0, 69 }); 70 readable.read(size); 71 72 assert.strictEqual(readable._readableState.highWaterMark, Number(size)); 73 }); 74} 75