Skip to content

Commit cd302d7

Browse files
committed
stream: make _read() be called indefinitely if the user wants so
Fixes: #26097 PR-URL: #26135 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 0d64a56 commit cd302d7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/_stream_readable.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ Readable.prototype.read = function(n) {
495495
};
496496

497497
function onEofChunk(stream, state) {
498+
debug('onEofChunk');
498499
if (state.ended) return;
499500
if (state.decoder) {
500501
var chunk = state.decoder.end();
@@ -525,6 +526,7 @@ function onEofChunk(stream, state) {
525526
// a nextTick recursion warning, but that's not so bad.
526527
function emitReadable(stream) {
527528
var state = stream._readableState;
529+
debug('emitReadable', state.needReadable, state.emittedReadable);
528530
state.needReadable = false;
529531
if (!state.emittedReadable) {
530532
debug('emitReadable', state.flowing);
@@ -538,6 +540,7 @@ function emitReadable_(stream) {
538540
debug('emitReadable_', state.destroyed, state.length, state.ended);
539541
if (!state.destroyed && (state.length || state.ended)) {
540542
stream.emit('readable');
543+
state.emittedReadable = false;
541544
}
542545

543546
// The stream needs another readable event if
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { Readable } = require('stream');
6+
7+
const buf = Buffer.alloc(8192);
8+
9+
const readable = new Readable({
10+
read: common.mustCall(function() {
11+
this.push(buf);
12+
}, 31)
13+
});
14+
15+
let i = 0;
16+
17+
readable.on('readable', common.mustCall(function() {
18+
if (i++ === 10) {
19+
// We will just terminate now.
20+
process.removeAllListeners('readable');
21+
return;
22+
}
23+
24+
const data = readable.read();
25+
// TODO(mcollina): there is something odd in the highWaterMark logic
26+
// investigate.
27+
if (i === 1) {
28+
assert.strictEqual(data.length, 8192 * 2);
29+
} else {
30+
assert.strictEqual(data.length, 8192 * 3);
31+
}
32+
}, 11));

0 commit comments

Comments
 (0)