GeezerVison is showing github.com — lib: use primordials for navigator.userAgent · nodejs/node@48dbde7
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboard +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ObjectDefineProperties, |
| 5 | + StringPrototypeIndexOf, |
| 6 | + StringPrototypeSlice, |
| 7 | + Symbol, |
| 8 | +} = primordials; |
| 9 | + |
| 10 | +const { |
| 11 | + ERR_ILLEGAL_CONSTRUCTOR, |
| 12 | +} = require('internal/errors').codes; |
| 13 | + |
| 14 | +const { |
| 15 | + kEnumerableProperty, |
| 16 | +} = require('internal/util'); |
| 17 | + |
| 18 | +const { |
| 19 | + getAvailableParallelism, |
| 20 | +} = internalBinding('os'); |
| 21 | + |
| 22 | +const kInitialize = Symbol('kInitialize'); |
| 23 | +const nodeVersion = process.version; |
| 24 | + |
| 25 | +class Navigator { |
| 26 | + // Private properties are used to avoid brand validations. |
| 27 | + #availableParallelism; |
| 28 | + #userAgent = `Node.js/${StringPrototypeSlice(nodeVersion, 1, StringPrototypeIndexOf(nodeVersion, '.'))}`; |
| 29 | + |
| 30 | + constructor() { |
| 31 | + if (arguments[0] === kInitialize) { |
| 32 | + return; |
| 33 | + } |
| 34 | + throw new ERR_ILLEGAL_CONSTRUCTOR(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return {number} |
| 39 | + */ |
| 40 | + get hardwareConcurrency() { |
| 41 | + this.#availableParallelism ??= getAvailableParallelism(); |
| 42 | + return this.#availableParallelism; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return {string} |
| 47 | + */ |
| 48 | + get userAgent() { |
| 49 | + return this.#userAgent; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +ObjectDefineProperties(Navigator.prototype, { |
| 54 | + hardwareConcurrency: kEnumerableProperty, |
| 55 | + userAgent: kEnumerableProperty, |
| 56 | +}); |
| 57 | + |
| 58 | +module.exports = { |
| 59 | + navigator: new Navigator(kInitialize), |
| 60 | + Navigator, |
| 61 | +}; |