Skip to content

Commit 613b68e

Browse files
committed
stream: fix readable being emitted when nothing to do
1 parent 8803b69 commit 613b68e

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/_stream_readable.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,9 @@ function emitReadable(stream) {
520520
function emitReadable_(stream) {
521521
var state = stream._readableState;
522522
debug('emit readable');
523-
stream.emit('readable');
523+
if (!state.destroyed && (state.length || state.ended)) {
524+
stream.emit('readable');
525+
}
524526
state.needReadable = !state.flowing && !state.ended;
525527
flow(stream);
526528
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Readable, PassThrough } = require('stream');
4+
5+
const source = new Readable({
6+
read: () => {}
7+
});
8+
9+
source.push('foo');
10+
source.push('bar');
11+
source.push(null);
12+
13+
const pt = source.pipe(new PassThrough());
14+
15+
const wrapper = new Readable({
16+
read: () => {
17+
let data = pt.read();
18+
19+
if (data) {
20+
wrapper.push(data);
21+
return;
22+
}
23+
24+
pt.once('readable', function() {
25+
data = pt.read();
26+
if (data) {
27+
wrapper.push(data);
28+
}
29+
// else the end event should fire
30+
});
31+
}
32+
});
33+
34+
pt.once('end', function() {
35+
wrapper.push(null);
36+
});
37+
38+
wrapper.resume();
39+
wrapper.once('end', common.mustCall());

0 commit comments

Comments
 (0)