diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index d1a17fd066076d..3e73fb2d62221c 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -570,7 +570,8 @@ function maybeReadMore(stream, state) { function maybeReadMore_(stream, state) { var len = state.length; while (!state.reading && !state.ended && - state.length < state.highWaterMark) { + (state.length < state.highWaterMark || + state.flowing && state.length === 0)) { debug('maybeReadMore read 0'); stream.read(0); if (len === state.length) diff --git a/test/parallel/test-stream-readable-hwm-0-async.js b/test/parallel/test-stream-readable-hwm-0-async.js new file mode 100644 index 00000000000000..ac64524feb33d9 --- /dev/null +++ b/test/parallel/test-stream-readable-hwm-0-async.js @@ -0,0 +1,27 @@ +'use strict'; + +const common = require('../common'); + +// This test ensures that Readable stream will call _read() for streams +// with highWaterMark === 0 upon .read(0) instead of just trying to +// emit 'readable' event. + +const { Readable } = require('stream'); + +let count = 5; + +const r = new Readable({ + // Called 6 times: First 5 return data, last one signals end of stream. + read: common.mustCall(() => { + process.nextTick(common.mustCall(() => { + if (count--) + r.push('a'); + else + r.push(null); + })); + }, 6), + highWaterMark: 0, +}); + +r.on('end', common.mustCall()); +r.on('data', common.mustCall(5));