mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-03 07:54:40 +08:00
A write completion callback that a JavaScript module returned reaches the stream lowering as an untyped value. Since the error-argument form of write callbacks, the lowering refused it with SC2020, so tests/corpus/8532-stream-saved-callback-receiver failed to compile. Such a callback is now converted to a function of the error argument. The stream calls it unbound with null after a successful write or with the write's error, and ignores what it returns, as Node does.
29 lines
1.0 KiB
TypeScript
29 lines
1.0 KiB
TypeScript
// Write completion callbacks from a JavaScript module run unbound with
|
|
// Node's arguments: null after a successful write, the _write error after a
|
|
// failed one, and the write-after-end error. Their return value is ignored.
|
|
import { Writable } from "node:stream";
|
|
import { makeReporter } from "./report.js";
|
|
|
|
const lines: string[] = [];
|
|
const sink = new Writable({
|
|
write(chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
|
|
callback(chunk.toString() === "bad" ? new Error("rejected") : null);
|
|
},
|
|
});
|
|
sink.on("error", (error) => lines.push(`error event ${error.message}`));
|
|
sink.write("good", makeReporter(lines, "good"));
|
|
sink.write("bad", "utf8", makeReporter(lines, "bad"));
|
|
|
|
const ended = new Writable({
|
|
write(_chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
|
|
callback();
|
|
},
|
|
});
|
|
ended.on("error", () => lines.push("ended error event"));
|
|
ended.end();
|
|
ended.write("late", makeReporter(lines, "late"));
|
|
|
|
setTimeout(() => {
|
|
for (const line of lines) console.log(line);
|
|
}, 5);
|