From faeee11c1fafdf8252d0e13046810ac81d315fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=88=9A?= Date: Thu, 18 Jan 2018 02:42:38 +0800 Subject: [PATCH] stream: readable continues to read when `push('')` PR-URL: https://github.com/nodejs/node/pull/18211 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater --- lib/_stream_readable.js | 1 + test/parallel/test-stream-readable-event.js | 30 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 46afe5f33dee91..9678d77f6c6434 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -250,6 +250,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { } } else if (!addToFront) { state.reading = false; + maybeReadMore(stream, state); } } diff --git a/test/parallel/test-stream-readable-event.js b/test/parallel/test-stream-readable-event.js index 3e7a42ccee2698..33b912e71ec98f 100644 --- a/test/parallel/test-stream-readable-event.js +++ b/test/parallel/test-stream-readable-event.js @@ -83,3 +83,33 @@ const Readable = require('stream').Readable; r.on('readable', common.mustCall()); }, 1); } + +{ + // pushing a empty string in non-objectMode should + // trigger next `read()`. + const underlyingData = ['', 'x', 'y', '', 'z']; + const expected = underlyingData.filter((data) => data); + const result = []; + + const r = new Readable({ + encoding: 'utf8', + }); + r._read = function() { + process.nextTick(() => { + if (!underlyingData.length) { + this.push(null); + } else { + this.push(underlyingData.shift()); + } + }); + }; + + r.on('readable', () => { + const data = r.read(); + if (data !== null) result.push(data); + }); + + r.on('end', common.mustCall(() => { + assert.deepStrictEqual(result, expected); + })); +}