Skip to content

Commit faeee11

Browse files
陈刚mcollina
陈刚
authored andcommitted
stream: readable continues to read when push('')
PR-URL: #18211 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent baf8495 commit faeee11

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/_stream_readable.js

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
250250
}
251251
} else if (!addToFront) {
252252
state.reading = false;
253+
maybeReadMore(stream, state);
253254
}
254255
}
255256

test/parallel/test-stream-readable-event.js

+30
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,33 @@ const Readable = require('stream').Readable;
8383
r.on('readable', common.mustCall());
8484
}, 1);
8585
}
86+
87+
{
88+
// pushing a empty string in non-objectMode should
89+
// trigger next `read()`.
90+
const underlyingData = ['', 'x', 'y', '', 'z'];
91+
const expected = underlyingData.filter((data) => data);
92+
const result = [];
93+
94+
const r = new Readable({
95+
encoding: 'utf8',
96+
});
97+
r._read = function() {
98+
process.nextTick(() => {
99+
if (!underlyingData.length) {
100+
this.push(null);
101+
} else {
102+
this.push(underlyingData.shift());
103+
}
104+
});
105+
};
106+
107+
r.on('readable', () => {
108+
const data = r.read();
109+
if (data !== null) result.push(data);
110+
});
111+
112+
r.on('end', common.mustCall(() => {
113+
assert.deepStrictEqual(result, expected);
114+
}));
115+
}

0 commit comments

Comments
 (0)