summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/process.ts
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-03-22 13:49:07 -0700
committerGitHub <noreply@github.com>2024-03-22 20:49:07 +0000
commit08ec6e5831478f6d15188e91d9a8e3c00d80c1ed (patch)
tree13a830c1ab1ee2ce5ebcffe5c0fcecd77146c8bc /ext/node/polyfills/process.ts
parent9c2f9f14e749e4dd63ad02e3ca8af5fc8bd112ee (diff)
perf: warm expensive init code at snapshot time (#22714)
Slightly different approach to similar changes in #22386 Note that this doesn't use a warmup script -- we are actually just doing more work at snapshot time.
Diffstat (limited to 'ext/node/polyfills/process.ts')
-rw-r--r--ext/node/polyfills/process.ts120
1 files changed, 71 insertions, 49 deletions
diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts
index 44207e2f4..70c516c96 100644
--- a/ext/node/polyfills/process.ts
+++ b/ext/node/polyfills/process.ts
@@ -68,7 +68,7 @@ const notImplementedEvents = [
"worker",
];
-export const argv: string[] = [];
+export const argv: string[] = ["", ""];
let globalProcessExitCode: number | undefined = undefined;
/** https://nodejs.org/api/process.html#process_process_exit_code */
@@ -868,69 +868,91 @@ function synchronizeListeners() {
}
}
+// Overwrites the 1st and 2nd items with getters.
+Object.defineProperty(argv, "0", { get: () => argv0 });
+Object.defineProperty(argv, "1", {
+ get: () => {
+ if (Deno.mainModule?.startsWith("file:")) {
+ return pathFromURL(new URL(Deno.mainModule));
+ } else {
+ return join(Deno.cwd(), "$deno$node.js");
+ }
+ },
+});
+
// Should be called only once, in `runtime/js/99_main.js` when the runtime is
// bootstrapped.
internals.__bootstrapNodeProcess = function (
argv0Val: string | undefined,
args: string[],
denoVersions: Record<string, string>,
+ warmup = false,
) {
- // Overwrites the 1st item with getter.
- if (typeof argv0Val === "string") {
- argv0 = argv0Val;
- Object.defineProperty(argv, "0", {
- get: () => {
- return argv0Val;
- },
- });
- } else {
- Object.defineProperty(argv, "0", { get: () => argv0 });
- }
+ if (!warmup) {
+ argv0 = argv0Val || "";
+ // Manually concatenate these arrays to avoid triggering the getter
+ for (let i = 0; i < args.length; i++) {
+ argv[i + 2] = args[i];
+ }
- // Overwrites the 2st item with getter.
- Object.defineProperty(argv, "1", {
- get: () => {
- if (Deno.mainModule?.startsWith("file:")) {
- return pathFromURL(new URL(Deno.mainModule));
- } else {
- return join(Deno.cwd(), "$deno$node.js");
- }
- },
- });
- for (let i = 0; i < args.length; i++) {
- argv[i + 2] = args[i];
- }
+ for (const [key, value] of Object.entries(denoVersions)) {
+ versions[key] = value;
+ }
- for (const [key, value] of Object.entries(denoVersions)) {
- versions[key] = value;
- }
+ core.setNextTickCallback(processTicksAndRejections);
+ core.setMacrotaskCallback(runNextTicks);
+ enableNextTick();
- core.setNextTickCallback(processTicksAndRejections);
- core.setMacrotaskCallback(runNextTicks);
- enableNextTick();
+ // Replace stdin if it is not a terminal
+ const newStdin = initStdin();
+ if (newStdin) {
+ stdin = process.stdin = newStdin;
+ }
- stdin = process.stdin = initStdin();
- /** https://nodejs.org/api/process.html#process_process_stdout */
- stdout = process.stdout = createWritableStdioStream(
- io.stdout,
- "stdout",
- );
+ // Replace stdout/stderr if they are not terminals
+ if (!io.stdout.isTerminal()) {
+ /** https://nodejs.org/api/process.html#process_process_stdout */
+ stdout = process.stdout = createWritableStdioStream(
+ io.stdout,
+ "stdout",
+ );
+ }
- /** https://nodejs.org/api/process.html#process_process_stderr */
- stderr = process.stderr = createWritableStdioStream(
- io.stderr,
- "stderr",
- );
+ if (!io.stderr.isTerminal()) {
+ /** https://nodejs.org/api/process.html#process_process_stderr */
+ stderr = process.stderr = createWritableStdioStream(
+ io.stderr,
+ "stderr",
+ );
+ }
- process.setStartTime(Date.now());
+ process.setStartTime(Date.now());
- arch = arch_();
- platform = isWindows ? "win32" : Deno.build.os;
- pid = Deno.pid;
+ arch = arch_();
+ platform = isWindows ? "win32" : Deno.build.os;
+ pid = Deno.pid;
- // @ts-ignore Remove setStartTime and #startTime is not modifiable
- delete process.setStartTime;
- delete internals.__bootstrapNodeProcess;
+ // @ts-ignore Remove setStartTime and #startTime is not modifiable
+ delete process.setStartTime;
+ delete internals.__bootstrapNodeProcess;
+ } else {
+ // Warmup, assuming stdin/stdout/stderr are all terminals
+ stdin = process.stdin = initStdin(true);
+
+ /** https://nodejs.org/api/process.html#process_process_stdout */
+ stdout = process.stdout = createWritableStdioStream(
+ io.stdout,
+ "stdout",
+ true,
+ );
+
+ /** https://nodejs.org/api/process.html#process_process_stderr */
+ stderr = process.stderr = createWritableStdioStream(
+ io.stderr,
+ "stderr",
+ true,
+ );
+ }
};
export default process;