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'); 25 26// This test verifies that: 27// 1. unshift() does not cause colliding _read() calls. 28// 2. unshift() after the 'end' event is an error, but after the EOF 29// signalling null, it is ok, and just creates a new readable chunk. 30// 3. push() after the EOF signaling null is an error. 31// 4. _read() is not called after pushing the EOF null chunk. 32 33const stream = require('stream'); 34const hwm = 10; 35const r = stream.Readable({ highWaterMark: hwm }); 36const chunks = 10; 37 38const data = Buffer.allocUnsafe(chunks * hwm + Math.ceil(hwm / 2)); 39for (let i = 0; i < data.length; i++) { 40 const c = 'asdf'.charCodeAt(i % 4); 41 data[i] = c; 42} 43 44let pos = 0; 45let pushedNull = false; 46r._read = function(n) { 47 assert(!pushedNull, '_read after null push'); 48 49 // Every third chunk is fast 50 push(!(chunks % 3)); 51 52 function push(fast) { 53 assert(!pushedNull, 'push() after null push'); 54 const c = pos >= data.length ? null : data.slice(pos, pos + n); 55 pushedNull = c === null; 56 if (fast) { 57 pos += n; 58 r.push(c); 59 if (c === null) pushError(); 60 } else { 61 setTimeout(function() { 62 pos += n; 63 r.push(c); 64 if (c === null) pushError(); 65 }, 1); 66 } 67 } 68}; 69 70function pushError() { 71 assert.throws(() => { 72 r.push(Buffer.allocUnsafe(1)); 73 }, { 74 code: 'ERR_STREAM_PUSH_AFTER_EOF', 75 name: 'Error', 76 message: 'stream.push() after EOF' 77 }); 78} 79 80const w = stream.Writable(); 81const written = []; 82w._write = function(chunk, encoding, cb) { 83 written.push(chunk.toString()); 84 cb(); 85}; 86 87r.on('end', common.mustCall(function() { 88 assert.throws(function() { 89 r.unshift(Buffer.allocUnsafe(1)); 90 }, { 91 code: 'ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 92 name: 'Error', 93 message: 'stream.unshift() after end event' 94 }); 95 w.end(); 96})); 97 98r.on('readable', function() { 99 let chunk; 100 while (null !== (chunk = r.read(10))) { 101 w.write(chunk); 102 if (chunk.length > 4) 103 r.unshift(Buffer.from('1234')); 104 } 105}); 106 107w.on('finish', common.mustCall(function() { 108 // Each chunk should start with 1234, and then be asfdasdfasdf... 109 // The first got pulled out before the first unshift('1234'), so it's 110 // lacking that piece. 111 assert.strictEqual(written[0], 'asdfasdfas'); 112 let asdf = 'd'; 113 console.error(`0: ${written[0]}`); 114 for (let i = 1; i < written.length; i++) { 115 console.error(`${i.toString(32)}: ${written[i]}`); 116 assert.strictEqual(written[i].slice(0, 4), '1234'); 117 for (let j = 4; j < written[i].length; j++) { 118 const c = written[i].charAt(j); 119 assert.strictEqual(c, asdf); 120 switch (asdf) { 121 case 'a': asdf = 's'; break; 122 case 's': asdf = 'd'; break; 123 case 'd': asdf = 'f'; break; 124 case 'f': asdf = 'a'; break; 125 } 126 } 127 } 128})); 129 130process.on('exit', function() { 131 assert.strictEqual(written.length, 18); 132 console.log('ok'); 133}); 134