Skip to content

Commit a83008a

Browse files
committed
stream: defer readable and flow when sync
1 parent 591a1c5 commit a83008a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lib/_stream_readable.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,18 @@ function onEofChunk(stream, state) {
486486
}
487487
state.ended = true;
488488

489-
// emit 'readable' now to make sure it gets picked up.
490-
state.needReadable = false;
491-
if (!state.emittedReadable) {
492-
state.emittedReadable = true;
493-
emitReadable_(stream);
489+
if (state.sync && state.length) {
490+
// if we are sync and have data in the buffer, wait until next tick
491+
// to emit the data. otherwise we risk emitting data in the flow()
492+
// the readable code triggers during a read() call
493+
emitReadable(stream);
494+
} else {
495+
// emit 'readable' now to make sure it gets picked up.
496+
state.needReadable = false;
497+
if (!state.emittedReadable) {
498+
state.emittedReadable = true;
499+
emitReadable_(stream);
500+
}
494501
}
495502
}
496503

0 commit comments

Comments
 (0)