diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 1830eea66decc9..1a111ed0f47141 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -383,17 +383,9 @@ function howMuchToRead(n, state) { else return state.length; } - // If we're asking for more than the current hwm, then raise the hwm. - if (n > state.highWaterMark) - state.highWaterMark = computeNewHighWaterMark(n); if (n <= state.length) return n; - // Don't have enough - if (!state.ended) { - state.needReadable = true; - return 0; - } - return state.length; + return state.ended ? state.length : 0; } // You can override either this method, or the async _read(n) below. @@ -409,6 +401,10 @@ Readable.prototype.read = function(n) { const state = this._readableState; const nOrig = n; + // If we're asking for more than the current hwm, then raise the hwm. + if (n > state.highWaterMark) + state.highWaterMark = computeNewHighWaterMark(n); + if (n !== 0) state.emittedReadable = false;