/
navigator.js
118 lines (105 loc) Β· 2.93 KB
/
navigator.js
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
'use strict';
const {
ObjectDefineProperties,
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 nodeVersion = process.version;
/**
* @param {object} process
* @param {string} process.platform
* @param {string} process.arch
* @returns {string}
*/
function getNavigatorPlatform(process) {
if (process.platform === 'darwin') {
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
return 'MacIntel';
} else if (process.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 (process.platform === 'linux') {
if (process.arch === 'ia32') {
return 'Linux i686';
} else if (process.arch === 'x64') {
return 'Linux x86_64';
}
return `Linux ${process.arch}`;
} else if (process.platform === 'freebsd') {
if (process.arch === 'ia32') {
return 'FreeBSD i386';
} else if (process.arch === 'x64') {
return 'FreeBSD amd64';
}
return `FreeBSD ${process.arch}`;
} else if (process.platform === 'openbsd') {
if (process.arch === 'ia32') {
return 'OpenBSD i386';
} else if (process.arch === 'x64') {
return 'OpenBSD amd64';
}
return `OpenBSD ${process.arch}`;
} else if (process.platform === 'sunos') {
if (process.arch === 'ia32') {
return 'SunOS i86pc';
}
return `SunOS ${process.arch}`;
} else if (process.platform === 'aix') {
return 'AIX';
}
return `${StringPrototypeToUpperCase(process.platform[0])}${StringPrototypeSlice(process.platform, 1)} ${process.arch}`;
}
class Navigator {
// Private properties are used to avoid brand validations.
#availableParallelism;
#userAgent = `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
#platform = getNavigatorPlatform(process);
constructor() {
if (arguments[0] === kInitialize) {
return;
}
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
/**
* @return {number}
*/
get hardwareConcurrency() {
this.#availableParallelism ??= getAvailableParallelism();
return this.#availableParallelism;
}
/**
* @return {string}
*/
get userAgent() {
return this.#userAgent;
}
/**
* @return {string}
*/
get platform() {
return this.#platform;
}
}
ObjectDefineProperties(Navigator.prototype, {
hardwareConcurrency: kEnumerableProperty,
userAgent: kEnumerableProperty,
platform: kEnumerableProperty,
});
module.exports = {
getNavigatorPlatform,
navigator: new Navigator(kInitialize),
Navigator,
};