Files
scriptc/tests/corpus/process-write-dynamic-chunks.ts
T
Malte UblandClaude Opus 5.5 7be1a990c7 Write run-time-typed chunks to the process streams (W2)
`process.stdout.write(data)`/`process.stderr.write(data)` with a chunk
whose type is only known at run time (`any`, or a `string | Uint8Array`
union) was refused. Lower the one-argument form to the new
`process.stdioWriteDyn` libCall, and factor the existing stdio write
method's dispatch into `scr_stdio_write_call` so both share Node's
Writable validation: a string or a Uint8Array/Buffer writes its bytes,
null throws ERR_STREAM_NULL_VALUES, and any other value throws
ERR_INVALID_ARG_TYPE, all before anything is written.

`scr_stdio_write_call` keeps its exact statements, so it is only the
call sites that change; programs that contain a JS stdio value and link
the events unit may differ (their write method's bytes).

Corpus: `tests/corpus/process-write-dynamic-chunks.ts` covers strings,
Buffer, Uint8Array, a string | Uint8Array union, a JSON field, and the
four invalid chunks with their codes and messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 05:37:58 -07:00

35 lines
1.2 KiB
TypeScript

// process.stdout/stderr.write of chunks whose type is only known at run
// time: `any` values (a child's 'data' chunk, a JSON field) and
// string | Uint8Array unions. Strings and Uint8Array/Buffer values write
// their bytes; null throws ERR_STREAM_NULL_VALUES and any other value
// throws ERR_INVALID_ARG_TYPE, both TypeErrors raised synchronously before
// anything is written.
function forward(data: any): boolean {
return process.stdout.write(data);
}
function pick(useBytes: boolean): string | Uint8Array {
return useBytes ? new Uint8Array([98, 121, 116, 101, 115, 10]) : "text\n";
}
console.log("string", forward("any string\n"));
console.log("buffer", forward(Buffer.from("any buffer\n")));
console.log("u8", forward(new TextEncoder().encode("any u8\n")));
process.stdout.write(pick(false));
process.stdout.write(pick(true));
process.stderr.write(pick(false));
const parsed: any = JSON.parse('{"chunk":"from json\\n","n":42}');
forward(parsed.chunk);
for (const bad of [parsed.n, null, undefined, { a: 1 }]) {
try {
forward(bad);
console.log("no throw");
} catch (error) {
if (error instanceof TypeError) {
console.log((error as NodeJS.ErrnoException).code, error.message);
}
}
}