1'use strict'; 2require('../common'); 3const assert = require('assert'); 4 5const { Transform, Readable, Writable } = require('stream'); 6 7const DEFAULT = 16 * 1024; 8 9function testTransform(expectedReadableHwm, expectedWritableHwm, options) { 10 const t = new Transform(options); 11 assert.strictEqual(t._readableState.highWaterMark, expectedReadableHwm); 12 assert.strictEqual(t._writableState.highWaterMark, expectedWritableHwm); 13} 14 15// Test overriding defaultHwm 16testTransform(666, DEFAULT, { readableHighWaterMark: 666 }); 17testTransform(DEFAULT, 777, { writableHighWaterMark: 777 }); 18testTransform(666, 777, { 19 readableHighWaterMark: 666, 20 writableHighWaterMark: 777, 21}); 22 23// test 0 overriding defaultHwm 24testTransform(0, DEFAULT, { readableHighWaterMark: 0 }); 25testTransform(DEFAULT, 0, { writableHighWaterMark: 0 }); 26 27// Test highWaterMark overriding 28testTransform(555, 555, { 29 highWaterMark: 555, 30 readableHighWaterMark: 666, 31}); 32testTransform(555, 555, { 33 highWaterMark: 555, 34 writableHighWaterMark: 777, 35}); 36testTransform(555, 555, { 37 highWaterMark: 555, 38 readableHighWaterMark: 666, 39 writableHighWaterMark: 777, 40}); 41 42// Test highWaterMark = 0 overriding 43testTransform(0, 0, { 44 highWaterMark: 0, 45 readableHighWaterMark: 666, 46}); 47testTransform(0, 0, { 48 highWaterMark: 0, 49 writableHighWaterMark: 777, 50}); 51testTransform(0, 0, { 52 highWaterMark: 0, 53 readableHighWaterMark: 666, 54 writableHighWaterMark: 777, 55}); 56 57// Test undefined, null 58[undefined, null].forEach((v) => { 59 testTransform(DEFAULT, DEFAULT, { readableHighWaterMark: v }); 60 testTransform(DEFAULT, DEFAULT, { writableHighWaterMark: v }); 61 testTransform(666, DEFAULT, { highWaterMark: v, readableHighWaterMark: 666 }); 62 testTransform(DEFAULT, 777, { highWaterMark: v, writableHighWaterMark: 777 }); 63}); 64 65// test NaN 66{ 67 assert.throws(() => { 68 new Transform({ readableHighWaterMark: NaN }); 69 }, { 70 name: 'TypeError', 71 code: 'ERR_INVALID_OPT_VALUE', 72 message: 'The value "NaN" is invalid for option "readableHighWaterMark"' 73 }); 74 75 assert.throws(() => { 76 new Transform({ writableHighWaterMark: NaN }); 77 }, { 78 name: 'TypeError', 79 code: 'ERR_INVALID_OPT_VALUE', 80 message: 'The value "NaN" is invalid for option "writableHighWaterMark"' 81 }); 82} 83 84// Test non Duplex streams ignore the options 85{ 86 const r = new Readable({ readableHighWaterMark: 666 }); 87 assert.strictEqual(r._readableState.highWaterMark, DEFAULT); 88 const w = new Writable({ writableHighWaterMark: 777 }); 89 assert.strictEqual(w._writableState.highWaterMark, DEFAULT); 90} 91