Files
scriptc/tests/corpus/stdout-patch-buffer-from-encoded-data/patch.mjs
T
Chris Tate b7bcbc68d8 Pass a Buffer to a patched stdio write from an encoded data listener
A byte write to a replaced process.stdout.write or process.stderr.write
was boxed with the 'data' chunk helper, which answers a string while a
listener of a socket or HTTP response with setEncoding("utf8") runs. A
`process.stdout.write(buffer)` from such a listener reached the
replacement as a string. Node passes the Buffer it was given.

The stdio hook now boxes a byte write as a Buffer whatever listener is
running.
2026-10-29 02:58:50 -05:00

9 lines
380 B
JavaScript

// Replaces process.stdout.write with a recorder; an exit handler prints what it saw.
const seen = [];
process.stdout.write = function (chunk) {
const kind = typeof chunk === "string" ? "string" : Buffer.isBuffer(chunk) ? "Buffer" : typeof chunk;
seen.push(`${kind}:${String(chunk)}`);
return true;
};
process.on("exit", () => process.stderr.write(`${seen.join("\n")}\n`));