diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 56a3e9dac3..c598c91306 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -73,6 +73,7 @@ function ReadableState(options, stream) { // whenever we return null, then we set a flag to say // that we're awaiting a 'readable' event emission. this.needReadable = false; + this.emittedReadable = false; this.decoder = null; if (options.encoding) { @@ -125,6 +126,9 @@ Readable.prototype.read = function(n) { var state = this._readableState; var nOrig = n; + if (typeof n !== 'number' || n > 0) + state.emittedReadable = false; + n = howMuchToRead(n, state); // if we've ended, and we're now clear, then finish it up. @@ -224,9 +228,13 @@ function onread(er, chunk) { // if we've ended and we have some data left, then emit // 'readable' now to make sure it gets picked up. if (!sync) { - if (state.length > 0) - this.emit('readable'); - else + if (state.length > 0) { + state.needReadable = false; + if (!state.emittedReadable) { + state.emittedReadable = true; + this.emit('readable'); + } + } else endReadable(this); } return; @@ -254,7 +262,10 @@ function onread(er, chunk) { if (state.needReadable && !sync) { state.needReadable = false; - this.emit('readable'); + if (!state.emittedReadable) { + state.emittedReadable = true; + this.emit('readable'); + } } }