Skip to content

Commit 7612574

Browse files
mcollinaaddaleax
authored andcommitted
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 8584068 commit 7612574

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/_stream_readable.js

+3
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ Readable.prototype.read = function(n) {
494494
};
495495

496496
function onEofChunk(stream, state) {
497+
debug('onEofChunk');
497498
if (state.ended) return;
498499
if (state.decoder) {
499500
var chunk = state.decoder.end();
@@ -524,6 +525,7 @@ function onEofChunk(stream, state) {
524525
// a nextTick recursion warning, but that's not so bad.
525526
function emitReadable(stream) {
526527
var state = stream._readableState;
528+
debug('emitReadable', state.needReadable, state.emittedReadable);
527529
state.needReadable = false;
528530
if (!state.emittedReadable) {
529531
debug('emitReadable', state.flowing);
@@ -537,6 +539,7 @@ function emitReadable_(stream) {
537539
debug('emitReadable_', state.destroyed, state.length, state.ended);
538540
if (!state.destroyed && (state.length || state.ended)) {
539541
stream.emit('readable');
542+
state.emittedReadable = false;
540543
}
541544

542545
// The stream needs another readable event if
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)