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: fix readable being emitted when nothing to do #18372

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
4 changes: 3 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ function emitReadable(stream) {
function emitReadable_(stream) {
var state = stream._readableState;
debug('emit readable');
stream.emit('readable');
if (!state.destroyed && (state.length || state.ended)) {
stream.emit('readable');
}
state.needReadable = !state.flowing && !state.ended;
flow(stream);
}
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-stream-readable-no-unneeded-readable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');
const { Readable, PassThrough } = require('stream');

const source = new Readable({
read: () => {}
});

source.push('foo');
source.push('bar');
source.push(null);

const pt = source.pipe(new PassThrough());

const wrapper = new Readable({
read: () => {
let data = pt.read();

if (data) {
wrapper.push(data);
return;
}

pt.once('readable', function() {
data = pt.read();
if (data) {
wrapper.push(data);
}
// else the end event should fire
});
}
});

pt.once('end', function() {
wrapper.push(null);
});

wrapper.resume();
wrapper.once('end', common.mustCall());