1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25const stream = require('stream'); 26 27let passed = false; 28 29class TestStream extends stream.Transform { 30 _transform(chunk, encoding, done) { 31 if (!passed) { 32 // Char 'a' only exists in the last write 33 passed = chunk.toString().includes('a'); 34 } 35 done(); 36 } 37} 38 39const s1 = new stream.PassThrough(); 40const s2 = new stream.PassThrough(); 41const s3 = new TestStream(); 42s1.pipe(s3); 43// Don't let s2 auto close which may close s3 44s2.pipe(s3, { end: false }); 45 46// We must write a buffer larger than highWaterMark 47const big = Buffer.alloc(s1.writableHighWaterMark + 1, 'x'); 48 49// Since big is larger than highWaterMark, it will be buffered internally. 50assert(!s1.write(big)); 51// 'tiny' is small enough to pass through internal buffer. 52assert(s2.write('tiny')); 53 54// Write some small data in next IO loop, which will never be written to s3 55// Because 'drain' event is not emitted from s1 and s1 is still paused 56setImmediate(s1.write.bind(s1), 'later'); 57 58// Assert after two IO loops when all operations have been done. 59process.on('exit', function() { 60 assert(passed, 'Large buffer is not handled properly by Writable Stream'); 61}); 62