2023-07-04 22:55:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const {
|
|
|
|
ObjectDefineProperties,
|
2024-04-21 16:53:08 +00:00
|
|
|
ObjectFreeze,
|
2023-11-02 13:31:50 +00:00
|
|
|
StringPrototypeIndexOf,
|
|
|
|
StringPrototypeSlice,
|
2023-11-02 20:21:21 +00:00
|
|
|
StringPrototypeToUpperCase,
|
2023-07-04 22:55:34 +00:00
|
|
|
Symbol,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
const {
|
|
|
|
ERR_ILLEGAL_CONSTRUCTOR,
|
|
|
|
} = require('internal/errors').codes;
|
|
|
|
|
|
|
|
const {
|
|
|
|
kEnumerableProperty,
|
|
|
|
} = require('internal/util');
|
|
|
|
|
|
|
|
const {
|
|
|
|
getAvailableParallelism,
|
|
|
|
} = internalBinding('os');
|
|
|
|
|
|
|
|
const kInitialize = Symbol('kInitialize');
|
2024-07-15 23:09:54 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
platform,
|
|
|
|
arch,
|
|
|
|
version: nodeVersion,
|
|
|
|
} = require('internal/process/per_thread');
|
|
|
|
|
|
|
|
const {
|
2024-08-09 00:52:02 +00:00
|
|
|
getDefaultLocale,
|
2024-07-15 23:09:54 +00:00
|
|
|
} = internalBinding('config');
|
2023-07-04 22:55:34 +00:00
|
|
|
|
2023-11-02 20:21:21 +00:00
|
|
|
/**
|
2024-07-15 23:09:54 +00:00
|
|
|
* @param {string} arch
|
|
|
|
* @param {string} platform
|
2023-11-02 20:21:21 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2024-07-15 23:09:54 +00:00
|
|
|
function getNavigatorPlatform(arch, platform) {
|
|
|
|
if (platform === 'darwin') {
|
2023-11-02 20:21:21 +00:00
|
|
|
// On macOS, modern browsers return 'MacIntel' even if running on Apple Silicon.
|
|
|
|
return 'MacIntel';
|
2024-07-15 23:09:54 +00:00
|
|
|
} else if (platform === 'win32') {
|
2023-11-02 20:21:21 +00:00
|
|
|
// 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';
|
2024-07-15 23:09:54 +00:00
|
|
|
} else if (platform === 'linux') {
|
|
|
|
if (arch === 'ia32') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'Linux i686';
|
2024-07-15 23:09:54 +00:00
|
|
|
} else if (arch === 'x64') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'Linux x86_64';
|
|
|
|
}
|
2024-07-15 23:09:54 +00:00
|
|
|
return `Linux ${arch}`;
|
|
|
|
} else if (platform === 'freebsd') {
|
|
|
|
if (arch === 'ia32') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'FreeBSD i386';
|
2024-07-15 23:09:54 +00:00
|
|
|
} else if (arch === 'x64') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'FreeBSD amd64';
|
|
|
|
}
|
2024-07-15 23:09:54 +00:00
|
|
|
return `FreeBSD ${arch}`;
|
|
|
|
} else if (platform === 'openbsd') {
|
|
|
|
if (arch === 'ia32') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'OpenBSD i386';
|
2024-07-15 23:09:54 +00:00
|
|
|
} else if (arch === 'x64') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'OpenBSD amd64';
|
|
|
|
}
|
2024-07-15 23:09:54 +00:00
|
|
|
return `OpenBSD ${arch}`;
|
|
|
|
} else if (platform === 'sunos') {
|
|
|
|
if (arch === 'ia32') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'SunOS i86pc';
|
|
|
|
}
|
2024-07-15 23:09:54 +00:00
|
|
|
return `SunOS ${arch}`;
|
|
|
|
} else if (platform === 'aix') {
|
2023-11-02 20:21:21 +00:00
|
|
|
return 'AIX';
|
|
|
|
}
|
2024-07-15 23:09:54 +00:00
|
|
|
return `${StringPrototypeToUpperCase(platform[0])}${StringPrototypeSlice(platform, 1)} ${arch}`;
|
2023-11-02 20:21:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 22:55:34 +00:00
|
|
|
class Navigator {
|
|
|
|
// Private properties are used to avoid brand validations.
|
|
|
|
#availableParallelism;
|
2024-07-04 14:35:28 +00:00
|
|
|
#userAgent;
|
|
|
|
#platform;
|
|
|
|
#languages;
|
2023-07-04 22:55:34 +00:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
if (arguments[0] === kInitialize) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-28 09:57:38 +00:00
|
|
|
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
2023-07-04 22:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
get hardwareConcurrency() {
|
|
|
|
this.#availableParallelism ??= getAvailableParallelism();
|
|
|
|
return this.#availableParallelism;
|
|
|
|
}
|
2023-10-20 20:22:48 +00:00
|
|
|
|
2023-11-04 18:07:42 +00:00
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
get language() {
|
2024-08-09 00:52:02 +00:00
|
|
|
// The default locale might be changed dynamically, so always invoke the
|
|
|
|
// binding.
|
|
|
|
return getDefaultLocale() || 'en-US';
|
2023-11-04 18:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {Array<string>}
|
|
|
|
*/
|
|
|
|
get languages() {
|
2024-07-04 14:35:28 +00:00
|
|
|
this.#languages ??= ObjectFreeze([this.language]);
|
2023-11-04 18:07:42 +00:00
|
|
|
return this.#languages;
|
|
|
|
}
|
|
|
|
|
2023-10-20 20:22:48 +00:00
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
get userAgent() {
|
2024-07-04 14:35:28 +00:00
|
|
|
this.#userAgent ??= `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`;
|
2023-10-20 20:22:48 +00:00
|
|
|
return this.#userAgent;
|
|
|
|
}
|
2023-11-02 20:21:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
get platform() {
|
2024-07-15 23:09:54 +00:00
|
|
|
this.#platform ??= getNavigatorPlatform(arch, platform);
|
2023-11-02 20:21:21 +00:00
|
|
|
return this.#platform;
|
|
|
|
}
|
2023-07-04 22:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectDefineProperties(Navigator.prototype, {
|
|
|
|
hardwareConcurrency: kEnumerableProperty,
|
2023-11-04 18:07:42 +00:00
|
|
|
language: kEnumerableProperty,
|
|
|
|
languages: kEnumerableProperty,
|
2023-10-20 20:22:48 +00:00
|
|
|
userAgent: kEnumerableProperty,
|
2023-11-02 20:21:21 +00:00
|
|
|
platform: kEnumerableProperty,
|
2023-07-04 22:55:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
2023-11-02 20:21:21 +00:00
|
|
|
getNavigatorPlatform,
|
2023-07-04 22:55:34 +00:00
|
|
|
navigator: new Navigator(kInitialize),
|
|
|
|
Navigator,
|
|
|
|
};
|