Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: make _read() be called indefinitely if the user wants so #26135

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ Readable.prototype.read = function(n) {
};

function onEofChunk(stream, state) {
debug('onEofChunk');
if (state.ended) return;
if (state.decoder) {
var chunk = state.decoder.end();
Expand Down Expand Up @@ -525,6 +526,7 @@ function onEofChunk(stream, state) {
// a nextTick recursion warning, but that's not so bad.
function emitReadable(stream) {
var state = stream._readableState;
debug('emitReadable', state.needReadable, state.emittedReadable);
state.needReadable = false;
if (!state.emittedReadable) {
debug('emitReadable', state.flowing);
Expand All @@ -538,6 +540,7 @@ function emitReadable_(stream) {
debug('emitReadable_', state.destroyed, state.length, state.ended);
if (!state.destroyed && (state.length || state.ended)) {
stream.emit('readable');
state.emittedReadable = false;
}

// The stream needs another readable event if
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-stream-readable-infinite-read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { Readable } = require('stream');

const buf = Buffer.alloc(8192);

const readable = new Readable({
read: common.mustCall(function() {
this.push(buf);
}, 31)
});

let i = 0;

readable.on('readable', common.mustCall(function() {
if (i++ === 10) {
// We will just terminate now.
process.removeAllListeners('readable');
return;
}

const data = readable.read();
// TODO(mcollina): there is something odd in the highWaterMark logic
// investigate.
if (i === 1) {
assert.strictEqual(data.length, 8192 * 2);
} else {
assert.strictEqual(data.length, 8192 * 3);
}
}, 11));