Version
v28.0.0-pre (current main)
Platform
macOS
Subsystem
stream
What steps will reproduce the bug?
Calling unshift() from a 'data' listener can lose data when a byte-mode Readable produces chunks synchronously. The stream then emits 'end' without reporting an error.
The example below consumes A from the first chunk and puts B back so it can be read before CD:
const { Readable } = require('node:stream');
const chunks = [Buffer.from('AB'), Buffer.from('CD')];
const stream = new Readable({
read() {
this.push(chunks.shift() ?? null);
},
});
const received = [];
let firstChunk = true;
stream.on('data', (chunk) => {
if (firstChunk) {
firstChunk = false;
// Consume A and put B back for the next read.
received.push(chunk.subarray(0, 1));
stream.unshift(chunk.subarray(1));
} else {
received.push(chunk);
}
});
stream.on('end', () => {
console.log(Buffer.concat(received).toString());
});
How often does it reproduce? Is there a required condition?
every time, no condition required
What is the expected behavior? Why is that the expected behavior?
Expected output:
ABCD
What do you see instead?
Actual output: