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'; 23const common = require('../common'); 24const assert = require('assert'); 25const Readable = require('_stream_readable'); 26const Writable = require('_stream_writable'); 27const EE = require('events').EventEmitter; 28 29function runTest(highWaterMark, objectMode, produce) { 30 31 const old = new EE(); 32 const r = new Readable({ highWaterMark, objectMode }); 33 assert.strictEqual(r, r.wrap(old)); 34 35 r.on('end', common.mustCall()); 36 37 old.pause = function() { 38 old.emit('pause'); 39 flowing = false; 40 }; 41 42 old.resume = function() { 43 old.emit('resume'); 44 flow(); 45 }; 46 47 let flowing; 48 let chunks = 10; 49 let oldEnded = false; 50 const expected = []; 51 function flow() { 52 flowing = true; 53 while (flowing && chunks-- > 0) { 54 const item = produce(); 55 expected.push(item); 56 old.emit('data', item); 57 } 58 if (chunks <= 0) { 59 oldEnded = true; 60 old.emit('end'); 61 } 62 } 63 64 const w = new Writable({ highWaterMark: highWaterMark * 2, 65 objectMode }); 66 const written = []; 67 w._write = function(chunk, encoding, cb) { 68 written.push(chunk); 69 setTimeout(cb, 1); 70 }; 71 72 w.on('finish', common.mustCall(function() { 73 performAsserts(); 74 })); 75 76 r.pipe(w); 77 78 flow(); 79 80 function performAsserts() { 81 assert(oldEnded); 82 assert.deepStrictEqual(written, expected); 83 } 84} 85 86runTest(100, false, function() { return Buffer.allocUnsafe(100); }); 87runTest(10, false, function() { return Buffer.from('xxxxxxxxxx'); }); 88runTest(1, true, function() { return { foo: 'bar' }; }); 89 90const objectChunks = [ 5, 'a', false, 0, '', 'xyz', { x: 4 }, 7, [], 555 ]; 91runTest(1, true, function() { return objectChunks.shift(); }); 92