Files
scriptc/tests/corpus/6821-stream-write-nullable-chunks.cjs
T
Chris Tate 23ec584deb Read nullable union chunks of push and write by their representation
Readable.push and Writable.write with a union chunk (Buffer, string and
null arms) read the chunk's tag and payload from a tagged union box. A
union with one reference arm and null, such as `string | null` or
`Buffer | null`, is the arm pointer itself, so `push(line)` with
`line: string | null` and `push(done ? null : text)` read past the
string and crashed. Both now read the tag and payload with unionTag and
unionPeek, as the other union consumers do. unshift and end refuse
union chunks and are unchanged.
2026-10-21 13:24:10 -05:00

24 lines
854 B
JavaScript

// Writes and pushes of a chunk that is a string or null: a null write throws
// ERR_STREAM_NULL_VALUES, and a null push ends the readable.
const { Writable, Readable } = require("node:stream");
const written = [];
const sink = new Writable({
write(chunk, _encoding, callback) {
written.push(chunk.toString());
callback();
},
});
for (let i = 0; i < 3; i++) {
const chunk = i === 1 ? null : `c${i}`;
try {
console.log("write", JSON.stringify(chunk), sink.write(chunk));
} catch (error) {
console.log("write threw", error.name, error.code, error.message);
}
}
const r = new Readable({ read() {} });
for (let i = 0; i < 3; i++) r.push(i === 2 ? null : `r${i}`);
r.on("data", (d) => console.log("data", String(d)));
r.on("end", () => console.log("end"));
sink.end(() => console.log("written", JSON.stringify(written.join(""))));