Skip to content

Commit 5a52c51

Browse files
anonrigtargos
authored andcommitted
lib: add navigator.userAgent
PR-URL: #50200 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent a362c27 commit 5a52c51

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/api/globals.md

+15
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,21 @@ logical processors available to the current Node.js instance.
629629
console.log(`This process is running on ${navigator.hardwareConcurrency}`);
630630
```
631631

632+
### `navigator.userAgent`
633+
634+
<!-- YAML
635+
added: REPLACEME
636+
-->
637+
638+
* {string}
639+
640+
The `navigator.userAgent` read-only property returns user agent
641+
consisting of the runtime name and major version number.
642+
643+
```js
644+
console.log(`The user-agent is ${navigator.userAgent}`); // Prints "Node.js/21"
645+
```
646+
632647
## `PerformanceEntry`
633648

634649
<!-- YAML

lib/internal/navigator.js

+10
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ const {
1818
} = internalBinding('os');
1919

2020
const kInitialize = Symbol('kInitialize');
21+
const nodeVersion = process.version;
2122

2223
class Navigator {
2324
// Private properties are used to avoid brand validations.
2425
#availableParallelism;
26+
#userAgent = `Node.js/${nodeVersion.slice(1, nodeVersion.indexOf('.'))}`;
2527

2628
constructor() {
2729
if (arguments[0] === kInitialize) {
@@ -37,10 +39,18 @@ class Navigator {
3739
this.#availableParallelism ??= getAvailableParallelism();
3840
return this.#availableParallelism;
3941
}
42+
43+
/**
44+
* @return {string}
45+
*/
46+
get userAgent() {
47+
return this.#userAgent;
48+
}
4049
}
4150

4251
ObjectDefineProperties(Navigator.prototype, {
4352
hardwareConcurrency: kEnumerableProperty,
53+
userAgent: kEnumerableProperty,
4454
});
4555

4656
module.exports = {

test/parallel/test-navigator.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ const is = {
1313
is.number(+navigator.hardwareConcurrency, 'hardwareConcurrency');
1414
is.number(navigator.hardwareConcurrency, 'hardwareConcurrency');
1515
assert.ok(navigator.hardwareConcurrency > 0);
16+
assert.strictEqual(typeof navigator.userAgent, 'string');
17+
assert.match(navigator.userAgent, /^Node\.js\/\d+$/);

0 commit comments

Comments
 (0)