forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigator.js
More file actions
164 lines (146 loc) Β· 3.86 KB
/
Copy pathnavigator.js
File metadata and controls
164 lines (146 loc) Β· 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
const {
ObjectDefineProperties,
ObjectFreeze,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeToUpperCase,
Symbol,
} = primordials;
const {
ERR_ILLEGAL_CONSTRUCTOR,
} = require('internal/errors').codes;
const {
kEnumerableProperty,
} = require('internal/util');
const {
getAvailableParallelism,
} = internalBinding('os');
const kInitialize = Symbol('kInitialize');
const {
platform,
arch,
version: nodeVersion,
} = require('internal/process/per_thread');
const {
getDefaultLocale,
} = internalBinding('config');
/**
* @param {string} arch
* @param {string} platform
* @returns {string}
*/
function getNavigatorPlatform(arch, platform) {
if (platform === 'darwin') {
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
return 'MacIntel';
} else if (platform === 'win32') {
// On Windows, modern browsers return 'Win32' even if running on a 64-bit version of Windows.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#usage_notes
return 'Win32';
} else if (platform === 'linux') {
if (arch === 'ia32') {
return 'Linux i686';
} else if (arch === 'x64') {
return 'Linux x86_64';
}
return `Linux ${arch}`;
} else if (platform === 'freebsd') {
if (arch === 'ia32') {
return 'FreeBSD i386';
} else if (arch === 'x64') {
return 'FreeBSD amd64';
}
return `FreeBSD ${arch}`;
} else if (platform === 'openbsd') {
if (arch === 'ia32') {
return 'OpenBSD i386';
} else if (arch === 'x64') {
return 'OpenBSD amd64';
}
return `OpenBSD ${arch}`;
} else if (platform === 'sunos') {
if (arch === 'ia32') {
return 'SunOS i86pc';
}
return `SunOS ${arch}`;
} else if (platform === 'aix') {
return 'AIX';
}
return `${StringPrototypeToUpperCase(platform[0])}${StringPrototypeSlice(platform, 1)} ${arch}`;
}
class Navigator {
// Private properties are used to avoid brand validations: reading a private
// field from a non-Navigator receiver throws a TypeError on its own.
#availableParallelism;
#locks;
#userAgent;
#platform;
#languages;
constructor() {
if (arguments[0] === kInitialize) {
return;
}
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
/**
* @returns {number}
*/
get hardwareConcurrency() {
this.#availableParallelism ??= getAvailableParallelism();
return this.#availableParallelism;
}
/**
* @returns {LockManager}
*/
get locks() {
this.#locks ??= require('internal/locks').locks;
return this.#locks;
}
/**
* @returns {string}
*/
get language() {
// `language` does not read a private field, so brand-check explicitly
// to keep parity with the other getters when called on a non-Navigator
// receiver (e.g. `Navigator.prototype.language`).
this.#languages; // eslint-disable-line no-unused-expressions
// The default locale might be changed dynamically, so always invoke the
// binding.
return getDefaultLocale() || 'en-US';
}
/**
* @returns {Array<string>}
*/
get languages() {
this.#languages ??= ObjectFreeze([this.language]);
return this.#languages;
}
/**
* @returns {string}
*/
get userAgent() {
this.#userAgent ??= `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
return this.#userAgent;
}
/**
* @returns {string}
*/
get platform() {
this.#platform ??= getNavigatorPlatform(arch, platform);
return this.#platform;
}
}
ObjectDefineProperties(Navigator.prototype, {
hardwareConcurrency: kEnumerableProperty,
language: kEnumerableProperty,
languages: kEnumerableProperty,
userAgent: kEnumerableProperty,
platform: kEnumerableProperty,
locks: kEnumerableProperty,
});
module.exports = {
getNavigatorPlatform,
navigator: new Navigator(kInitialize),
Navigator,
};