Skip to content

Commit

Permalink
stream: do not emit readable if the stream ended
Browse files Browse the repository at this point in the history
Fixes a regression introduced by the once-per-microtick 'readable'
event emission.

See: nodejs#17979
PR-URL: nodejs#18372
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
mafintosh authored and MayaLekova committed May 8, 2018
1 parent b0fd262 commit 613978c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
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());

0 comments on commit 613978c

Please sign in to comment.