Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): init arch, pid, platform at startup #22561

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions ext/node/polyfills/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
35 changes: 27 additions & 8 deletions tests/unit_node/process_test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
},
});

Expand All @@ -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);
},
});

Expand All @@ -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);
},
});

Expand Down