Files
scriptc/tests/corpus/stream-dynamic-write-callback/main.ts
T
Chris Tate d8f3565ac4 Call untyped write completion callbacks with the error argument
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.
2026-10-29 18:55:44 -05:00

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);