11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst stream = require('stream'); 61cb0ef41Sopenharmony_ciconst { inspect } = require('util'); 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci{ 91cb0ef41Sopenharmony_ci // This test ensures that the stream implementation correctly handles values 101cb0ef41Sopenharmony_ci // for highWaterMark which exceed the range of signed 32 bit integers and 111cb0ef41Sopenharmony_ci // rejects invalid values. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci // This number exceeds the range of 32 bit integer arithmetic but should still 141cb0ef41Sopenharmony_ci // be handled correctly. 151cb0ef41Sopenharmony_ci const ovfl = Number.MAX_SAFE_INTEGER; 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci const readable = stream.Readable({ highWaterMark: ovfl }); 181cb0ef41Sopenharmony_ci assert.strictEqual(readable._readableState.highWaterMark, ovfl); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci const writable = stream.Writable({ highWaterMark: ovfl }); 211cb0ef41Sopenharmony_ci assert.strictEqual(writable._writableState.highWaterMark, ovfl); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci for (const invalidHwm of [true, false, '5', {}, -5, NaN]) { 241cb0ef41Sopenharmony_ci for (const type of [stream.Readable, stream.Writable]) { 251cb0ef41Sopenharmony_ci assert.throws(() => { 261cb0ef41Sopenharmony_ci type({ highWaterMark: invalidHwm }); 271cb0ef41Sopenharmony_ci }, { 281cb0ef41Sopenharmony_ci name: 'TypeError', 291cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 301cb0ef41Sopenharmony_ci message: "The property 'options.highWaterMark' is invalid. " + 311cb0ef41Sopenharmony_ci `Received ${inspect(invalidHwm)}` 321cb0ef41Sopenharmony_ci }); 331cb0ef41Sopenharmony_ci } 341cb0ef41Sopenharmony_ci } 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci{ 381cb0ef41Sopenharmony_ci // This test ensures that the push method's implementation 391cb0ef41Sopenharmony_ci // correctly handles the edge case where the highWaterMark and 401cb0ef41Sopenharmony_ci // the state.length are both zero 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci const readable = stream.Readable({ highWaterMark: 0 }); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci for (let i = 0; i < 3; i++) { 451cb0ef41Sopenharmony_ci const needMoreData = readable.push(); 461cb0ef41Sopenharmony_ci assert.strictEqual(needMoreData, true); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci} 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci{ 511cb0ef41Sopenharmony_ci // This test ensures that the read(n) method's implementation 521cb0ef41Sopenharmony_ci // correctly handles the edge case where the highWaterMark, state.length 531cb0ef41Sopenharmony_ci // and n are all zero 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci const readable = stream.Readable({ highWaterMark: 0 }); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci readable._read = common.mustCall(); 581cb0ef41Sopenharmony_ci readable.read(0); 591cb0ef41Sopenharmony_ci} 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci{ 621cb0ef41Sopenharmony_ci // Parse size as decimal integer 631cb0ef41Sopenharmony_ci ['1', '1.0', 1].forEach((size) => { 641cb0ef41Sopenharmony_ci const readable = new stream.Readable({ 651cb0ef41Sopenharmony_ci read: common.mustCall(), 661cb0ef41Sopenharmony_ci highWaterMark: 0, 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci readable.read(size); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci assert.strictEqual(readable._readableState.highWaterMark, Number(size)); 711cb0ef41Sopenharmony_ci }); 721cb0ef41Sopenharmony_ci} 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci{ 751cb0ef41Sopenharmony_ci // Test highwatermark limit 761cb0ef41Sopenharmony_ci const hwm = 0x40000000 + 1; 771cb0ef41Sopenharmony_ci const readable = stream.Readable({ 781cb0ef41Sopenharmony_ci read() {}, 791cb0ef41Sopenharmony_ci }); 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci assert.throws(() => readable.read(hwm), common.expectsError({ 821cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 831cb0ef41Sopenharmony_ci message: 'The value of "size" is out of range.' + 841cb0ef41Sopenharmony_ci ' It must be <= 1GiB. Received ' + 851cb0ef41Sopenharmony_ci hwm, 861cb0ef41Sopenharmony_ci })); 871cb0ef41Sopenharmony_ci} 88