-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: use primordials for navigator.userAgent
PR-URL: #50467 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Ethan Arrowood <[email protected]>
- Loading branch information
1 parent
1bd6537
commit 48dbde7
Showing
1 changed file
with
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectDefineProperties, | ||
StringPrototypeIndexOf, | ||
StringPrototypeSlice, | ||
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; | ||
|
||
class Navigator { | ||
// Private properties are used to avoid brand validations. | ||
#availableParallelism; | ||
#userAgent = `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`; | ||
|
||
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; | ||
} | ||
} | ||
|
||
ObjectDefineProperties(Navigator.prototype, { | ||
hardwareConcurrency: kEnumerableProperty, | ||
userAgent: kEnumerableProperty, | ||
}); | ||
|
||
module.exports = { | ||
navigator: new Navigator(kInitialize), | ||
Navigator, | ||
}; |