mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-23 06:10:02 +08:00
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.
22 lines
583 B
TypeScript
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(",")));
|