mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-01 07:33:12 +08:00
A Console instance over process stdio passed a patched write the line and an error handler, and discarded what the write threw, whatever its ignoreErrors setting. Node's kWriteToConsole does that only with ignoreErrors (the default, and the global console). With ignoreErrors false, from the options or the third constructor argument, it calls stream.write(line) alone and lets a throw propagate out of the console method. The console write path now takes the instance's ignoreErrors, so such an instance passes only the line and keeps the write's exception pending. Instances bound to other streams already followed the setting.
44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
// What a replaced process.stdout.write receives from a Console instance over
|
|
// process.stdout: with ignoreErrors (the default) the line and an error
|
|
// handler, and a throw from the write is discarded; with ignoreErrors false,
|
|
// as an option or the third constructor argument, only the line, and the
|
|
// throw propagates out of the console method.
|
|
const { Console } = require("node:console");
|
|
const original = process.stdout.write;
|
|
const seen = [];
|
|
let fail = false;
|
|
process.stdout.write = function (chunk, encodingOrCallback, callback) {
|
|
seen.push(`${typeof chunk}:${JSON.stringify(String(chunk))}:${typeof encodingOrCallback}:${typeof callback}`);
|
|
if (fail) throw new Error("write failed");
|
|
if (typeof encodingOrCallback === "function") encodingOrCallback(null);
|
|
return true;
|
|
};
|
|
const strict = new Console({ stdout: process.stdout, ignoreErrors: false });
|
|
const lenient = new Console({ stdout: process.stdout });
|
|
const positional = new Console(process.stdout, process.stderr, false);
|
|
strict.log("strict", 1);
|
|
lenient.log("lenient");
|
|
positional.info("positional");
|
|
fail = true;
|
|
try {
|
|
strict.log("throws");
|
|
seen.push("strict: no throw");
|
|
} catch (e) {
|
|
seen.push(`strict threw ${e.message}`);
|
|
}
|
|
try {
|
|
lenient.log("swallowed");
|
|
seen.push("lenient: no throw");
|
|
} catch (e) {
|
|
seen.push(`lenient threw ${e.message}`);
|
|
}
|
|
try {
|
|
console.log("global");
|
|
seen.push("global: no throw");
|
|
} catch (e) {
|
|
seen.push(`global threw ${e.message}`);
|
|
}
|
|
fail = false;
|
|
process.stdout.write = original;
|
|
console.log(seen.join("\n"));
|