stream: readable continues to read when push('')

PR-URL: https://github.com/nodejs/node/pull/18211
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
陈刚 2018-01-18 02:42:38 +08:00 committed by Matteo Collina
parent baf8495078
commit faeee11c1f
2 changed files with 31 additions and 0 deletions

View File

@ -250,6 +250,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
}
} else if (!addToFront) {
state.reading = false;
maybeReadMore(stream, state);
}
}

View File

@ -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);
}));
}