From 156cfe5c90a18d838979d143046698d0ac3f1072 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Fri, 23 Feb 2024 19:06:17 +0530 Subject: [PATCH] fix(ext/node): init arch, pid, platform at startup (#22561) --- ext/node/polyfills/process.ts | 16 ++++----------- tests/unit_node/process_test.ts | 35 +++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts index a8da35f70d..5187184707 100644 --- a/ext/node/polyfills/process.ts +++ b/ext/node/polyfills/process.ts @@ -48,13 +48,10 @@ import { Command } from "ext:runtime/40_process.js"; let argv0Getter = () => ""; export let argv0 = "deno"; -// TODO(kt3k): This should be set at start up time export let arch = ""; -// TODO(kt3k): This should be set at start up time export let platform = ""; -// TODO(kt3k): This should be set at start up time export let pid = 0; let stdin, stdout, stderr; @@ -368,9 +365,6 @@ class Process extends EventEmitter { /** https://nodejs.org/api/process.html#process_process_arch */ get arch() { - if (!arch) { - arch = arch_(); - } return arch; } @@ -561,9 +555,6 @@ class Process extends EventEmitter { /** https://nodejs.org/api/process.html#process_process_pid */ get pid() { - if (!pid) { - pid = Deno.pid; - } return pid; } @@ -574,9 +565,6 @@ class Process extends EventEmitter { /** https://nodejs.org/api/process.html#process_process_platform */ get platform() { - if (!platform) { - platform = isWindows ? "win32" : Deno.build.os; - } return platform; } @@ -939,6 +927,10 @@ internals.__bootstrapNodeProcess = function ( process.setStartTime(Date.now()); + 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; diff --git a/tests/unit_node/process_test.ts b/tests/unit_node/process_test.ts index bff3c66418..24ef6a3161 100644 --- a/tests/unit_node/process_test.ts +++ b/tests/unit_node/process_test.ts @@ -1,7 +1,14 @@ // deno-lint-ignore-file no-undef // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import process, { argv, env } from "node:process"; +import process, { + arch as importedArch, + argv, + env, + pid as importedPid, + platform as importedPlatform, +} from "node:process"; + import { Readable } from "node:stream"; import { once } from "node:events"; import { @@ -83,7 +90,11 @@ Deno.test({ Deno.test({ name: "process.platform", fn() { + const expectedOs = Deno.build.os == "windows" ? "win32" : Deno.build.os; assertEquals(typeof process.platform, "string"); + assertEquals(process.platform, expectedOs); + assertEquals(typeof importedPlatform, "string"); + assertEquals(importedPlatform, expectedOs); }, }); @@ -102,14 +113,20 @@ Deno.test({ Deno.test({ name: "process.arch", fn() { - assertEquals(typeof process.arch, "string"); - if (Deno.build.arch == "x86_64") { - assertEquals(process.arch, "x64"); - } else if (Deno.build.arch == "aarch64") { - assertEquals(process.arch, "arm64"); - } else { - throw new Error("unreachable"); + function testValue(arch: string) { + if (Deno.build.arch == "x86_64") { + assertEquals(arch, "x64"); + } else if (Deno.build.arch == "aarch64") { + assertEquals(arch, "arm64"); + } else { + throw new Error("unreachable"); + } } + + assertEquals(typeof process.arch, "string"); + testValue(process.arch); + assertEquals(typeof importedArch, "string"); + testValue(importedArch); }, }); @@ -118,6 +135,8 @@ Deno.test({ fn() { assertEquals(typeof process.pid, "number"); assertEquals(process.pid, Deno.pid); + assertEquals(typeof importedPid, "number"); + assertEquals(importedPid, Deno.pid); }, });