Skip to content
Open
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
11 changes: 9 additions & 2 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const kFlowing = 1 << 24;
const kHasPaused = 1 << 25;
const kPaused = 1 << 26;
const kDataListening = 1 << 27;
const kEndScheduled = 1 << 28;

// TODO(benjamingr) it is likely slower to do it this way than with free functions
function makeBitMapDescriptor(bit) {
Expand Down Expand Up @@ -1757,15 +1758,21 @@ function endReadable(stream) {
const state = stream._readableState;

debug('endReadable');
if ((state[kState] & kEndEmitted) === 0) {
state[kState] |= kEnded;
if ((state[kState] & (kEndEmitted | kEndScheduled)) === 0) {
state[kState] |= kEnded | kEndScheduled;
process.nextTick(endReadableNT, state, stream);
}
}

function endReadableNT(state, stream) {
debug('endReadableNT');

// The scheduled tick is running; allow endReadable() to schedule again.
// This matters both when the 'end' emission is skipped below (e.g. after
// an unshift()) and when the stream is later reset for reuse
// (see undestroy()), which clears kEndEmitted but not this flag.
state[kState] &= ~kEndScheduled;

// Check that we didn't get one last unshift.
if ((state[kState] & (kErrored | kCloseEmitted | kEndEmitted)) === 0 && state.length === 0) {
state[kState] |= kEndEmitted;
Expand Down
Loading