Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 263e61d

Browse files
committedJul 8, 2024··
lib: make navigator not runtime-lookup process.version/arch/platform
Preserves most of #53649, except for `#language` due to the lack of Intl primordials.
1 parent b9289a6 commit 263e61d

File tree

7 files changed

+103
-57
lines changed

7 files changed

+103
-57
lines changed
 

‎lib/internal/bootstrap/node.js

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ const {
8585
},
8686
} = internalBinding('util');
8787

88+
require('internal/process/arch'); // Ensure the process arch is cached
89+
require('internal/process/platform'); // Ensure the process platform is cached
90+
require('internal/process/version'); // Etnsure the process version is cached
91+
8892
setupProcessObject();
8993

9094
setupGlobalProxy();

‎lib/internal/navigator.js

+26-25
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,53 @@ const {
2727
} = internalBinding('os');
2828

2929
const kInitialize = Symbol('kInitialize');
30-
const nodeVersion = process.version;
30+
const nodeVersion = require('internal/process/version');
31+
const platform = require('internal/process/platform');
32+
const arch = require('internal/process/arch');
3133

3234
/**
33-
* @param {object} process
34-
* @param {string} process.platform
35-
* @param {string} process.arch
35+
* @param {string} arch
36+
* @param {string} platform
3637
* @returns {string}
3738
*/
38-
function getNavigatorPlatform(process) {
39-
if (process.platform === 'darwin') {
39+
function getNavigatorPlatform(arch, platform) {
40+
if (platform === 'darwin') {
4041
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
4142
return 'MacIntel';
42-
} else if (process.platform === 'win32') {
43+
} else if (platform === 'win32') {
4344
// On Windows, modern browsers return 'Win32' even if running on a 64-bit version of Windows.
4445
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#usage_notes
4546
return 'Win32';
46-
} else if (process.platform === 'linux') {
47-
if (process.arch === 'ia32') {
47+
} else if (platform === 'linux') {
48+
if (arch === 'ia32') {
4849
return 'Linux i686';
49-
} else if (process.arch === 'x64') {
50+
} else if (arch === 'x64') {
5051
return 'Linux x86_64';
5152
}
52-
return `Linux ${process.arch}`;
53-
} else if (process.platform === 'freebsd') {
54-
if (process.arch === 'ia32') {
53+
return `Linux ${arch}`;
54+
} else if (platform === 'freebsd') {
55+
if (arch === 'ia32') {
5556
return 'FreeBSD i386';
56-
} else if (process.arch === 'x64') {
57+
} else if (arch === 'x64') {
5758
return 'FreeBSD amd64';
5859
}
59-
return `FreeBSD ${process.arch}`;
60-
} else if (process.platform === 'openbsd') {
61-
if (process.arch === 'ia32') {
60+
return `FreeBSD ${arch}`;
61+
} else if (platform === 'openbsd') {
62+
if (arch === 'ia32') {
6263
return 'OpenBSD i386';
63-
} else if (process.arch === 'x64') {
64+
} else if (arch === 'x64') {
6465
return 'OpenBSD amd64';
6566
}
66-
return `OpenBSD ${process.arch}`;
67-
} else if (process.platform === 'sunos') {
68-
if (process.arch === 'ia32') {
67+
return `OpenBSD ${arch}`;
68+
} else if (platform === 'sunos') {
69+
if (arch === 'ia32') {
6970
return 'SunOS i86pc';
7071
}
71-
return `SunOS ${process.arch}`;
72-
} else if (process.platform === 'aix') {
72+
return `SunOS ${arch}`;
73+
} else if (platform === 'aix') {
7374
return 'AIX';
7475
}
75-
return `${StringPrototypeToUpperCase(process.platform[0])}${StringPrototypeSlice(process.platform, 1)} ${process.arch}`;
76+
return `${StringPrototypeToUpperCase(platform[0])}${StringPrototypeSlice(platform, 1)} ${arch}`;
7677
}
7778

7879
class Navigator {
@@ -126,7 +127,7 @@ class Navigator {
126127
* @return {string}
127128
*/
128129
get platform() {
129-
this.#platform ??= getNavigatorPlatform(process);
130+
this.#platform ??= getNavigatorPlatform(arch, platform);
130131
return this.#platform;
131132
}
132133
}

‎lib/internal/process/arch.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('process').arch;

‎lib/internal/process/platform.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('process').platform;

‎lib/internal/process/version.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('process').version;

‎test/parallel/test-bootstrap-modules.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,32 @@ expected.beforePreExec = new Set([
5858
'NativeModule internal/process/promises',
5959
'NativeModule internal/fixed_queue',
6060
'NativeModule async_hooks',
61-
'NativeModule internal/process/task_queues',
62-
'NativeModule timers',
61+
'Internal Binding blob',
62+
'Internal Binding mksnapshot',
63+
'Internal Binding performance',
64+
'Internal Binding permission',
6365
'Internal Binding trace_events',
66+
'Internal Binding url',
67+
'NativeModule internal/console/constructor',
68+
'NativeModule internal/console/global',
6469
'NativeModule internal/constants',
65-
'NativeModule path',
70+
'NativeModule internal/event_target',
71+
'NativeModule internal/perf/utils',
72+
'NativeModule internal/process/arch',
6673
'NativeModule internal/process/execution',
6774
'NativeModule internal/process/permission',
75+
'NativeModule internal/process/platform',
76+
'NativeModule internal/process/task_queues',
77+
'NativeModule internal/process/version',
6878
'NativeModule internal/process/warning',
69-
'NativeModule internal/console/constructor',
70-
'NativeModule internal/console/global',
7179
'NativeModule internal/querystring',
72-
'NativeModule querystring',
73-
'Internal Binding url',
74-
'Internal Binding blob',
7580
'NativeModule internal/url',
76-
'NativeModule util',
7781
'NativeModule internal/webidl',
78-
'Internal Binding performance',
79-
'Internal Binding permission',
80-
'NativeModule internal/perf/utils',
81-
'NativeModule internal/event_target',
82-
'Internal Binding mksnapshot',
82+
'NativeModule path',
83+
'NativeModule process',
84+
'NativeModule querystring',
85+
'NativeModule timers',
86+
'NativeModule util',
8387
'NativeModule internal/v8/startup_snapshot',
8488
'NativeModule internal/process/signal',
8589
'Internal Binding fs',

‎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)
Please sign in to comment.