Files
scriptc/tests/corpus/5424-stream-write-buffer-encoding.ts
T
Chris Tate ae5d9bf81f Add Readable.from over generators and match Node 24.22 stream timing
Readable.from accepts sync and async generators of strings and Buffers,
following from.js: values are pushed while push() accepts, an async
generator's next() promise is awaited once per value, and destroy runs
the generator's throw() and return() before the destroy callback.

for await over a stream follows createAsyncIterator: the loop registers
its 'readable' listener, reads through read(), and a parked loop wakes
through the 'readable' tick. push(null) before the first read ends the
stream after the next read, finished() answers an already settled stream
with its result on a nextTick, read(0) on an object-mode stream reads
nothing, and write(string, 'buffer') throws ERR_UNKNOWN_ENCODING.
2026-10-15 18:07:26 -05:00

22 lines
583 B
TypeScript

import { Writable } from "node:stream";
const seen: string[] = [];
const w = new Writable({
write(chunk: Buffer, _enc, cb) {
seen.push(chunk.toString());
cb();
},
});
console.log("buffer chunk", w.write(Buffer.from("ab"), "buffer"));
try {
w.write("cd", "buffer");
} catch (err) {
console.log((err as Error).name, (err as NodeJS.ErrnoException).code, (err as Error).message);
}
try {
w.end("ef", "buffer");
} catch (err) {
console.log("end", (err as NodeJS.ErrnoException).code);
}
w.end(Buffer.from("gh"), "buffer", () => console.log("ended", seen.join(",")));