11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciconst Readable = require('stream').Readable; 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci{ 291cb0ef41Sopenharmony_ci // First test, not reading when the readable is added. 301cb0ef41Sopenharmony_ci // make sure that on('readable', ...) triggers a readable event. 311cb0ef41Sopenharmony_ci const r = new Readable({ 321cb0ef41Sopenharmony_ci highWaterMark: 3 331cb0ef41Sopenharmony_ci }); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci r._read = common.mustNotCall(); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci // This triggers a 'readable' event, which is lost. 381cb0ef41Sopenharmony_ci r.push(Buffer.from('blerg')); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci setTimeout(function() { 411cb0ef41Sopenharmony_ci // We're testing what we think we are 421cb0ef41Sopenharmony_ci assert(!r._readableState.reading); 431cb0ef41Sopenharmony_ci r.on('readable', common.mustCall()); 441cb0ef41Sopenharmony_ci }, 1); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci{ 481cb0ef41Sopenharmony_ci // Second test, make sure that readable is re-emitted if there's 491cb0ef41Sopenharmony_ci // already a length, while it IS reading. 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci const r = new Readable({ 521cb0ef41Sopenharmony_ci highWaterMark: 3 531cb0ef41Sopenharmony_ci }); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci r._read = common.mustCall(); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci // This triggers a 'readable' event, which is lost. 581cb0ef41Sopenharmony_ci r.push(Buffer.from('bl')); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci setTimeout(function() { 611cb0ef41Sopenharmony_ci // Assert we're testing what we think we are 621cb0ef41Sopenharmony_ci assert(r._readableState.reading); 631cb0ef41Sopenharmony_ci r.on('readable', common.mustCall()); 641cb0ef41Sopenharmony_ci }, 1); 651cb0ef41Sopenharmony_ci} 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci{ 681cb0ef41Sopenharmony_ci // Third test, not reading when the stream has not passed 691cb0ef41Sopenharmony_ci // the highWaterMark but *has* reached EOF. 701cb0ef41Sopenharmony_ci const r = new Readable({ 711cb0ef41Sopenharmony_ci highWaterMark: 30 721cb0ef41Sopenharmony_ci }); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci r._read = common.mustNotCall(); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci // This triggers a 'readable' event, which is lost. 771cb0ef41Sopenharmony_ci r.push(Buffer.from('blerg')); 781cb0ef41Sopenharmony_ci r.push(null); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci setTimeout(function() { 811cb0ef41Sopenharmony_ci // Assert we're testing what we think we are 821cb0ef41Sopenharmony_ci assert(!r._readableState.reading); 831cb0ef41Sopenharmony_ci r.on('readable', common.mustCall()); 841cb0ef41Sopenharmony_ci }, 1); 851cb0ef41Sopenharmony_ci} 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci{ 881cb0ef41Sopenharmony_ci // Pushing an empty string in non-objectMode should 891cb0ef41Sopenharmony_ci // trigger next `read()`. 901cb0ef41Sopenharmony_ci const underlyingData = ['', 'x', 'y', '', 'z']; 911cb0ef41Sopenharmony_ci const expected = underlyingData.filter((data) => data); 921cb0ef41Sopenharmony_ci const result = []; 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci const r = new Readable({ 951cb0ef41Sopenharmony_ci encoding: 'utf8', 961cb0ef41Sopenharmony_ci }); 971cb0ef41Sopenharmony_ci r._read = function() { 981cb0ef41Sopenharmony_ci process.nextTick(() => { 991cb0ef41Sopenharmony_ci if (!underlyingData.length) { 1001cb0ef41Sopenharmony_ci this.push(null); 1011cb0ef41Sopenharmony_ci } else { 1021cb0ef41Sopenharmony_ci this.push(underlyingData.shift()); 1031cb0ef41Sopenharmony_ci } 1041cb0ef41Sopenharmony_ci }); 1051cb0ef41Sopenharmony_ci }; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci r.on('readable', () => { 1081cb0ef41Sopenharmony_ci const data = r.read(); 1091cb0ef41Sopenharmony_ci if (data !== null) result.push(data); 1101cb0ef41Sopenharmony_ci }); 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci r.on('end', common.mustCall(() => { 1131cb0ef41Sopenharmony_ci assert.deepStrictEqual(result, expected); 1141cb0ef41Sopenharmony_ci })); 1151cb0ef41Sopenharmony_ci} 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci{ 1181cb0ef41Sopenharmony_ci // #20923 1191cb0ef41Sopenharmony_ci const r = new Readable(); 1201cb0ef41Sopenharmony_ci r._read = function() { 1211cb0ef41Sopenharmony_ci // Actually doing thing here 1221cb0ef41Sopenharmony_ci }; 1231cb0ef41Sopenharmony_ci r.on('data', function() {}); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci r.removeAllListeners(); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci assert.strictEqual(r.eventNames().length, 0); 1281cb0ef41Sopenharmony_ci} 129