Skip to content

Commit 48dbde7

Browse files
UzlopakUlisesGascon
authored andcommitted
lib: use primordials for navigator.userAgent
PR-URL: #50467 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent 1bd6537 commit 48dbde7

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

lib/internal/navigator.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
};

0 commit comments

Comments
 (0)