diff options
Diffstat (limited to 'lib/_stream_readable.js')
-rw-r--r-- | lib/_stream_readable.js | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 968c7b258c..fe77c44186 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -512,12 +512,24 @@ function onEofChunk(stream, state) { } } state.ended = true; - state.needReadable = false; - // We are not protecting if emittedReadable = true, - // so 'readable' gets scheduled anyway. - state.emittedReadable = true; - process.nextTick(emitReadable_, stream); + if (state.sync) { + // If we are sync, wait until next tick to emit the data. + // Otherwise we risk emitting data in the flow() + // the readable code triggers during a read() call + emitReadable(stream); + } else { + // Emit 'readable' now to make sure it gets picked up. + state.needReadable = false; + state.emittedReadable = true; + // We have to emit readable now that we are EOF. Modules + // in the ecosystem (e.g. dicer) rely on this event being sync. + if (state.ended) { + emitReadable_(stream); + } else { + process.nextTick(emitReadable_, stream); + } + } } // Don't emit readable right away in sync mode, because this can trigger |