Skip to content

Commit fa90968

Browse files
committed
lib: make navigator not runtime-lookup process.version/arch/platform
Preserves nodejs#53649.
1 parent 9791836 commit fa90968

File tree

4 files changed

+91
-50
lines changed

4 files changed

+91
-50
lines changed

lib/internal/navigator.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ const {
77
StringPrototypeSlice,
88
StringPrototypeToUpperCase,
99
Symbol,
10-
globalThis,
1110
} = primordials;
1211

13-
const {
14-
Intl,
15-
} = globalThis;
16-
1712
const {
1813
ERR_ILLEGAL_CONSTRUCTOR,
1914
} = require('internal/errors').codes;
@@ -27,60 +22,68 @@ const {
2722
} = internalBinding('os');
2823

2924
const kInitialize = Symbol('kInitialize');
30-
const nodeVersion = process.version;
25+
26+
const {
27+
platform,
28+
arch,
29+
version: nodeVersion,
30+
} = require('internal/process/per_thread');
31+
32+
const {
33+
language,
34+
} = internalBinding('config');
3135

3236
/**
33-
* @param {object} process
34-
* @param {string} process.platform
35-
* @param {string} process.arch
37+
* @param {string} arch
38+
* @param {string} platform
3639
* @returns {string}
3740
*/
38-
function getNavigatorPlatform(process) {
39-
if (process.platform === 'darwin') {
41+
function getNavigatorPlatform(arch, platform) {
42+
if (platform === 'darwin') {
4043
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
4144
return 'MacIntel';
42-
} else if (process.platform === 'win32') {
45+
} else if (platform === 'win32') {
4346
// On Windows, modern browsers return 'Win32' even if running on a 64-bit version of Windows.
4447
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#usage_notes
4548
return 'Win32';
46-
} else if (process.platform === 'linux') {
47-
if (process.arch === 'ia32') {
49+
} else if (platform === 'linux') {
50+
if (arch === 'ia32') {
4851
return 'Linux i686';
49-
} else if (process.arch === 'x64') {
52+
} else if (arch === 'x64') {
5053
return 'Linux x86_64';
5154
}
52-
return `Linux ${process.arch}`;
53-
} else if (process.platform === 'freebsd') {
54-
if (process.arch === 'ia32') {
55+
return `Linux ${arch}`;
56+
} else if (platform === 'freebsd') {
57+
if (arch === 'ia32') {
5558
return 'FreeBSD i386';
56-
} else if (process.arch === 'x64') {
59+
} else if (arch === 'x64') {
5760
return 'FreeBSD amd64';
5861
}
59-
return `FreeBSD ${process.arch}`;
60-
} else if (process.platform === 'openbsd') {
61-
if (process.arch === 'ia32') {
62+
return `FreeBSD ${arch}`;
63+
} else if (platform === 'openbsd') {
64+
if (arch === 'ia32') {
6265
return 'OpenBSD i386';
63-
} else if (process.arch === 'x64') {
66+
} else if (arch === 'x64') {
6467
return 'OpenBSD amd64';
6568
}
66-
return `OpenBSD ${process.arch}`;
67-
} else if (process.platform === 'sunos') {
68-
if (process.arch === 'ia32') {
69+
return `OpenBSD ${arch}`;
70+
} else if (platform === 'sunos') {
71+
if (arch === 'ia32') {
6972
return 'SunOS i86pc';
7073
}
71-
return `SunOS ${process.arch}`;
72-
} else if (process.platform === 'aix') {
74+
return `SunOS ${arch}`;
75+
} else if (platform === 'aix') {
7376
return 'AIX';
7477
}
75-
return `${StringPrototypeToUpperCase(process.platform[0])}${StringPrototypeSlice(process.platform, 1)} ${process.arch}`;
78+
return `${StringPrototypeToUpperCase(platform[0])}${StringPrototypeSlice(platform, 1)} ${arch}`;
7679
}
7780

7881
class Navigator {
7982
// Private properties are used to avoid brand validations.
8083
#availableParallelism;
8184
#userAgent;
8285
#platform;
83-
#language;
86+
#language = language;
8487
#languages;
8588

8689
constructor() {
@@ -102,7 +105,6 @@ class Navigator {
102105
* @return {string}
103106
*/
104107
get language() {
105-
this.#language ??= Intl?.Collator().resolvedOptions().locale || 'en-US';
106108
return this.#language;
107109
}
108110

@@ -126,7 +128,7 @@ class Navigator {
126128
* @return {string}
127129
*/
128130
get platform() {
129-
this.#platform ??= getNavigatorPlatform(process);
131+
this.#platform ??= getNavigatorPlatform(arch, platform);
130132
return this.#platform;
131133
}
132134
}

lib/internal/process/per_thread.js

+10
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ const {
2828
StringPrototypeStartsWith,
2929
Symbol,
3030
SymbolIterator,
31+
globalThis,
3132
} = primordials;
3233

34+
const {
35+
Intl,
36+
} = globalThis;
37+
3338
const {
3439
ErrnoException,
3540
codes: {
@@ -423,11 +428,16 @@ function toggleTraceCategoryState(asyncHooksEnabled) {
423428
}
424429
}
425430

431+
const { arch, platform, version } = process;
432+
426433
module.exports = {
427434
toggleTraceCategoryState,
428435
assert,
429436
buildAllowedFlags,
430437
wrapProcessMethods,
431438
hrtime,
432439
hrtimeBigInt,
440+
arch,
441+
platform,
442+
version,
433443
};

lib/internal/process/pre_execution.js

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ function setupNavigator() {
327327

328328
// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
329329
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
330+
internalBinding('config').language = Intl?.Collator().resolvedOptions().locale || 'en-US';
330331
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
331332
}
332333

test/parallel/test-navigator.js

+46-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,36 @@
22

33
'use strict';
44

5-
const common = require('../common');
5+
/* eslint node-core/require-common-first: 0 */
6+
67
const assert = require('assert');
8+
9+
{
10+
11+
// Ensures `navigator` has not been evaluated yet
12+
assert.strictEqual(require.resolve('../common') in require.cache, false);
13+
14+
const { version, platform, arch } = process;
15+
try {
16+
let called = false;
17+
Object.defineProperty(process, 'arch', { get() { called += 'arch|'; return arch; } });
18+
Object.defineProperty(process, 'platform', { get() { called = 'platform|'; return platform; } });
19+
Object.defineProperty(process, 'version', { get() { called = 'version|'; return version; } });
20+
21+
navigator; // eslint-disable-line no-unused-expressions
22+
23+
assert.strictEqual(
24+
called,
25+
false
26+
);
27+
} finally {
28+
Object.defineProperty(process, 'arch', { value: arch });
29+
Object.defineProperty(process, 'platform', { value: platform });
30+
Object.defineProperty(process, 'version', { value: version });
31+
}
32+
}
33+
34+
const common = require('../common');
735
const { getNavigatorPlatform } = require('internal/navigator');
836
const { execFile } = require('child_process');
937

@@ -57,23 +85,23 @@ if (process.platform === 'darwin') {
5785
assert.strictEqual(navigator.platform, `${process.platform[0].toUpperCase()}${process.platform.slice(1)} ${process.arch}`);
5886
}
5987

60-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'darwin' }), 'MacIntel');
61-
assert.strictEqual(getNavigatorPlatform({ arch: 'arm64', platform: 'darwin' }), 'MacIntel');
62-
assert.strictEqual(getNavigatorPlatform({ arch: 'ia32', platform: 'linux' }), 'Linux i686');
63-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'linux' }), 'Linux x86_64');
64-
assert.strictEqual(getNavigatorPlatform({ arch: 'arm64', platform: 'linux' }), 'Linux arm64');
65-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'win32' }), 'Win32');
66-
assert.strictEqual(getNavigatorPlatform({ arch: 'arm64', platform: 'win32' }), 'Win32');
67-
assert.strictEqual(getNavigatorPlatform({ arch: 'ia32', platform: 'freebsd' }), 'FreeBSD i386');
68-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'freebsd' }), 'FreeBSD amd64');
69-
assert.strictEqual(getNavigatorPlatform({ arch: 'arm64', platform: 'freebsd' }), 'FreeBSD arm64');
70-
assert.strictEqual(getNavigatorPlatform({ arch: 'ia32', platform: 'openbsd' }), 'OpenBSD i386');
71-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'openbsd' }), 'OpenBSD amd64');
72-
assert.strictEqual(getNavigatorPlatform({ arch: 'arm64', platform: 'openbsd' }), 'OpenBSD arm64');
73-
assert.strictEqual(getNavigatorPlatform({ arch: 'ia32', platform: 'sunos' }), 'SunOS i86pc');
74-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'sunos' }), 'SunOS x64');
75-
assert.strictEqual(getNavigatorPlatform({ arch: 'ppc', platform: 'aix' }), 'AIX');
76-
assert.strictEqual(getNavigatorPlatform({ arch: 'x64', platform: 'reactos' }), 'Reactos x64');
88+
assert.strictEqual(getNavigatorPlatform('x64', 'darwin'), 'MacIntel');
89+
assert.strictEqual(getNavigatorPlatform('arm64', 'darwin'), 'MacIntel');
90+
assert.strictEqual(getNavigatorPlatform('ia32', 'linux'), 'Linux i686');
91+
assert.strictEqual(getNavigatorPlatform('x64', 'linux'), 'Linux x86_64');
92+
assert.strictEqual(getNavigatorPlatform('arm64', 'linux'), 'Linux arm64');
93+
assert.strictEqual(getNavigatorPlatform('x64', 'win32'), 'Win32');
94+
assert.strictEqual(getNavigatorPlatform('arm64', 'win32'), 'Win32');
95+
assert.strictEqual(getNavigatorPlatform('ia32', 'freebsd'), 'FreeBSD i386');
96+
assert.strictEqual(getNavigatorPlatform('x64', 'freebsd'), 'FreeBSD amd64');
97+
assert.strictEqual(getNavigatorPlatform('arm64', 'freebsd'), 'FreeBSD arm64');
98+
assert.strictEqual(getNavigatorPlatform('ia32', 'openbsd'), 'OpenBSD i386');
99+
assert.strictEqual(getNavigatorPlatform('x64', 'openbsd'), 'OpenBSD amd64');
100+
assert.strictEqual(getNavigatorPlatform('arm64', 'openbsd'), 'OpenBSD arm64');
101+
assert.strictEqual(getNavigatorPlatform('ia32', 'sunos'), 'SunOS i86pc');
102+
assert.strictEqual(getNavigatorPlatform('x64', 'sunos'), 'SunOS x64');
103+
assert.strictEqual(getNavigatorPlatform('ppc', 'aix'), 'AIX');
104+
assert.strictEqual(getNavigatorPlatform('x64', 'reactos'), 'Reactos x64');
77105

78106
assert.strictEqual(typeof navigator.language, 'string');
79107
assert.strictEqual(navigator.language.length !== 0, true);

0 commit comments

Comments
 (0)