diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index cd8669830a5368..3a593ac05422af 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -530,7 +530,7 @@ function maybeReadMore(stream, state) { function maybeReadMore_(stream, state) { var len = state.length; - while (!state.reading && !state.flowing && !state.ended && + while (!state.reading && !state.ended && state.length < state.highWaterMark) { debug('maybeReadMore read 0'); stream.read(0); diff --git a/test/parallel/test-fs-read-stream-events.js b/test/parallel/test-fs-read-stream-events.js new file mode 100644 index 00000000000000..8282fb4846a747 --- /dev/null +++ b/test/parallel/test-fs-read-stream-events.js @@ -0,0 +1,35 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); + +const encodings = ['utf8', 'base64', 'ascii', 'binary', 'hex', 'utf16le']; + +function test(encoding) { + + const file = path.join(tmpdir.path, `read-stream-events-${encoding}.txt`); + + const data = '1234'; + fs.writeFileSync(file, data); + + const rs = fs.createReadStream(file, { + encoding, + highWaterMark: data.length - 1 + }); + + let chunks = ''; + + rs.on('data', common.mustCall(function(data) { + chunks += Buffer.from(data, encoding); + }, 2)); + + rs.on('end', common.mustCall(function() { + assert.strictEqual(chunks, data); + })); +} + +for (const encoding of encodings) + test(encoding);