-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
'use strict'; | ||
|
||
const { | ||
StringPrototypeToUpperCase, | ||
StringPrototypeSlice, | ||
ObjectDefineProperties, | ||
Symbol, | ||
} = primordials; | ||
|
@@ -20,10 +22,61 @@ const { | |
const kInitialize = Symbol('kInitialize'); | ||
const nodeVersion = process.version; | ||
|
||
/** | ||
* @param {Object} process | ||
Check warning on line 26 in lib/internal/navigator.js GitHub Actions / lint-js-and-md
|
||
* @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'; | ||
} else { | ||
return `Linux ${process.arch}`; | ||
} | ||
} else if (process.platform === 'freebsd') { | ||
if (process.arch === 'ia32') { | ||
return 'FreeBSD i386'; | ||
} else if (process.arch === 'x64') { | ||
return 'FreeBSD amd64'; | ||
} else { | ||
return `FreeBSD ${process.arch}`; | ||
} | ||
} else if (process.platform === 'openbsd') { | ||
if (process.arch === 'ia32') { | ||
return 'OpenBSD i386'; | ||
} else if (process.arch === 'x64') { | ||
return 'OpenBSD amd64'; | ||
} else { | ||
return `OpenBSD ${process.arch}`; | ||
} | ||
} else if (process.platform === 'sunos') { | ||
if (process.arch === 'ia32') { | ||
return 'SunOS i86pc'; | ||
} else { | ||
return `SunOS ${process.arch}`; | ||
} | ||
} else { | ||
// For cases like freebsd, openbsd, sunos, aix etc. | ||
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/${nodeVersion.slice(1, nodeVersion.indexOf('.'))}`; | ||
#platform = getNavigatorPlatform(process); | ||
|
||
constructor() { | ||
if (arguments[0] === kInitialize) { | ||
|
@@ -46,14 +99,23 @@ class Navigator { | |
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters