Skip to content

Commit 48dbde7

Browse files
UzlopakUlisesGascon
authored andcommitted
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]>
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‎

Lines changed: 61 additions & 0 deletions
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+
constkInitialize=Symbol('kInitialize');
23+
constnodeVersion=process.version;
24+
25+
classNavigator{
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+
thrownewERR_ILLEGAL_CONSTRUCTOR();
35+
}
36+
37+
/**
38+
* @return{number}
39+
*/
40+
gethardwareConcurrency(){
41+
this.#availableParallelism ??=getAvailableParallelism();
42+
returnthis.#availableParallelism;
43+
}
44+
45+
/**
46+
* @return{string}
47+
*/
48+
getuserAgent(){
49+
returnthis.#userAgent;
50+
}
51+
}
52+
53+
ObjectDefineProperties(Navigator.prototype,{
54+
hardwareConcurrency: kEnumerableProperty,
55+
userAgent: kEnumerableProperty,
56+
});
57+
58+
module.exports={
59+
navigator: newNavigator(kInitialize),
60+
Navigator,
61+
};

0 commit comments

Comments
(0)