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'); 26 27let len = 0; 28const chunks = new Array(10); 29for (let i = 1; i <= 10; i++) { 30 chunks[i - 1] = Buffer.allocUnsafe(i); 31 len += i; 32} 33 34const test = new Readable(); 35let n = 0; 36test._read = function(size) { 37 const chunk = chunks[n++]; 38 setTimeout(function() { 39 test.push(chunk === undefined ? null : chunk); 40 }, 1); 41}; 42 43test.on('end', thrower); 44function thrower() { 45 throw new Error('this should not happen!'); 46} 47 48let bytesread = 0; 49test.on('readable', function() { 50 const b = len - bytesread - 1; 51 const res = test.read(b); 52 if (res) { 53 bytesread += res.length; 54 console.error(`br=${bytesread} len=${len}`); 55 setTimeout(next, 1); 56 } 57 test.read(0); 58}); 59test.read(0); 60 61function next() { 62 // Now let's make 'end' happen 63 test.removeListener('end', thrower); 64 test.on('end', common.mustCall()); 65 66 // One to get the last byte 67 let r = test.read(); 68 assert(r); 69 assert.strictEqual(r.length, 1); 70 r = test.read(); 71 assert.strictEqual(r, null); 72} 73