mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-02 07:43:42 +08:00
6490 covers events.once with an AbortSignal: abort, an aborted signal, an undefined signal, abort-listener order, 'removeListener' events, a throwing 'newListener' listener and the settlement turns. 6491 covers stream receivers (file streams, Readable.from, a destroyed PassThrough, a Writable) and a NodeJS.ReadableStream part raced against its destination. 6492 covers races over events.once results with entries that only reject and entries of different value types. 6493 covers stream subclasses whose completion callbacks are typed `Function`. 6494 covers events.once over checked-dynamic stream slots. The node-types fixture runs the race shapes under @types/node, where events.once returns Promise<any[]>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
// Stream subclasses that declare their completion callbacks with the broad
|
|
// `Function` type: the runtime's callable arrives and calls through it
|
|
// complete the chunk, pass data, or fail the stream.
|
|
import { Transform, Writable } from "node:stream";
|
|
|
|
class Prefixer extends Transform {
|
|
_s: string;
|
|
constructor() {
|
|
super();
|
|
this._s = "";
|
|
}
|
|
_transform(c: Buffer, _: string, cb: Function) {
|
|
const text = c.toString();
|
|
const lines = text.split("\n").map((line, index, all) => {
|
|
if (index === 0) return this._s === "" || this._s.endsWith("\n") ? "> " + line : line;
|
|
if (index === all.length - 1) return line !== "" ? "> " + line : line;
|
|
return "> " + line;
|
|
});
|
|
this._s += text;
|
|
this.push(lines.join("\n"));
|
|
cb();
|
|
}
|
|
}
|
|
|
|
class Upper extends Transform {
|
|
_transform(chunk: Buffer, _encoding: string, cb: Function) {
|
|
if (chunk.toString() === "bad") {
|
|
cb(new Error("bad chunk"));
|
|
return;
|
|
}
|
|
cb(null, chunk.toString().toUpperCase());
|
|
}
|
|
_flush(cb: Function) {
|
|
this.push("!");
|
|
cb();
|
|
}
|
|
}
|
|
|
|
class Sink extends Writable {
|
|
seen: string[] = [];
|
|
_write(chunk: Buffer, _encoding: string, cb: Function) {
|
|
this.seen.push(chunk.toString());
|
|
cb();
|
|
}
|
|
_final(cb: Function) {
|
|
this.seen.push("<final>");
|
|
cb();
|
|
}
|
|
}
|
|
|
|
const prefixer = new Prefixer();
|
|
prefixer.pipe(process.stdout);
|
|
prefixer.write("a\nb");
|
|
prefixer.write("c\n");
|
|
prefixer.end("d\n");
|
|
|
|
const sink = new Sink();
|
|
const upper = new Upper();
|
|
upper.pipe(sink);
|
|
sink.on("finish", () => console.log("sink", JSON.stringify(sink.seen)));
|
|
upper.write("a");
|
|
upper.end("b");
|
|
|
|
const bad = new Upper();
|
|
bad.on("error", (err: Error) => console.log("error", err.message));
|
|
bad.write("bad");
|