Skip to content

Commit 263e61d

Browse files
committed
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‎

Lines changed: 4 additions & 0 deletions
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‎

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

2929
constkInitialize=Symbol('kInitialize');
30-
constnodeVersion=process.version;
30+
constnodeVersion=require('internal/process/version');
31+
constplatform=require('internal/process/platform');
32+
constarch=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-
functiongetNavigatorPlatform(process){
39-
if(process.platform==='darwin'){
39+
functiongetNavigatorPlatform(arch,platform){
40+
if(platform==='darwin'){
4041
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
4142
return'MacIntel';
42-
}elseif(process.platform==='win32'){
43+
}elseif(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-
}elseif(process.platform==='linux'){
47-
if(process.arch==='ia32'){
47+
}elseif(platform==='linux'){
48+
if(arch==='ia32'){
4849
return'Linux i686';
49-
}elseif(process.arch==='x64'){
50+
}elseif(arch==='x64'){
5051
return'Linux x86_64';
5152
}
52-
return`Linux ${process.arch}`;
53-
}elseif(process.platform==='freebsd'){
54-
if(process.arch==='ia32'){
53+
return`Linux ${arch}`;
54+
}elseif(platform==='freebsd'){
55+
if(arch==='ia32'){
5556
return'FreeBSD i386';
56-
}elseif(process.arch==='x64'){
57+
}elseif(arch==='x64'){
5758
return'FreeBSD amd64';
5859
}
59-
return`FreeBSD ${process.arch}`;
60-
}elseif(process.platform==='openbsd'){
61-
if(process.arch==='ia32'){
60+
return`FreeBSD ${arch}`;
61+
}elseif(platform==='openbsd'){
62+
if(arch==='ia32'){
6263
return'OpenBSD i386';
63-
}elseif(process.arch==='x64'){
64+
}elseif(arch==='x64'){
6465
return'OpenBSD amd64';
6566
}
66-
return`OpenBSD ${process.arch}`;
67-
}elseif(process.platform==='sunos'){
68-
if(process.arch==='ia32'){
67+
return`OpenBSD ${arch}`;
68+
}elseif(platform==='sunos'){
69+
if(arch==='ia32'){
6970
return'SunOS i86pc';
7071
}
71-
return`SunOS ${process.arch}`;
72-
}elseif(process.platform==='aix'){
72+
return`SunOS ${arch}`;
73+
}elseif(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
classNavigator{
@@ -126,7 +127,7 @@ class Navigator{
126127
* @return{string}
127128
*/
128129
getplatform(){
129-
this.#platform ??=getNavigatorPlatform(process);
130+
this.#platform ??=getNavigatorPlatform(arch,platform);
130131
returnthis.#platform;
131132
}
132133
}

‎lib/internal/process/arch.js‎

Lines changed: 3 additions & 0 deletions
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‎

Lines changed: 3 additions & 0 deletions
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‎

Lines changed: 3 additions & 0 deletions
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‎

Lines changed: 18 additions & 14 deletions
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‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,36 @@
22

33
'use strict';
44

5-
constcommon=require('../common');
5+
/* eslint node-core/require-common-first: 0 */
6+
67
constassert=require('assert');
8+
9+
{
10+
11+
// Ensures `navigator` has not been evaluated yet
12+
assert.strictEqual(require.resolve('../common')inrequire.cache,false);
13+
14+
const{ version, platform, arch }=process;
15+
try{
16+
letcalled=false;
17+
Object.defineProperty(process,'arch',{get(){called+='arch|';returnarch;}});
18+
Object.defineProperty(process,'platform',{get(){called='platform|';returnplatform;}});
19+
Object.defineProperty(process,'version',{get(){called='version|';returnversion;}});
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+
constcommon=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(typeofnavigator.language,'string');
79107
assert.strictEqual(navigator.language.length!==0,true);

0 commit comments

Comments
(0)