Skip to content

Commit b92833e

Browse files
committed
lib: add navigator.userAgent
1 parent f0e720a commit b92833e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-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 the version.
642+
643+
```js
644+
console.log(`The user-agent is ${navigator.userAgent}`);
645+
```
646+
632647
## `PerformanceEntry`
633648

634649
<!-- YAML

lib/internal/navigator.js

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const kInitialize = Symbol('kInitialize');
2222
class Navigator {
2323
// Private properties are used to avoid brand validations.
2424
#availableParallelism;
25+
#userAgent = `Node.js/${process.versions.node}`;
2526

2627
constructor() {
2728
if (arguments[0] === kInitialize) {
@@ -37,10 +38,18 @@ class Navigator {
3738
this.#availableParallelism ??= getAvailableParallelism();
3839
return this.#availableParallelism;
3940
}
41+
42+
/**
43+
* @return {string}
44+
*/
45+
get userAgent() {
46+
return this.#userAgent;
47+
}
4048
}
4149

4250
ObjectDefineProperties(Navigator.prototype, {
4351
hardwareConcurrency: kEnumerableProperty,
52+
userAgent: kEnumerableProperty,
4453
});
4554

4655
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+\.\d+\.\d+(-pre)?$/);

0 commit comments

Comments
 (0)