Skip to content

Commit

Permalink
lib: add navigator.userAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 16, 2023
1 parent f0e720a commit 1704a23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ logical processors available to the current Node.js instance.
console.log(`This process is running on ${navigator.hardwareConcurrency}`);
```

### `navigator.userAgent`

<!-- YAML
added: REPLACEME
-->

* {string}

The `navigator.userAgent` read-only property returns user agent
consisting of the runtime name and the version.

```js
console.log(`The user-agent is ${navigator.userAgent}`);
```

## `PerformanceEntry`

<!-- YAML
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const kInitialize = Symbol('kInitialize');
class Navigator {
// Private properties are used to avoid brand validations.
#availableParallelism;
#userAgent = `Node.js/${process.version}`;

constructor() {
if (arguments[0] === kInitialize) {
Expand All @@ -37,10 +38,18 @@ class Navigator {
this.#availableParallelism ??= getAvailableParallelism();
return this.#availableParallelism;
}

/**
* @return {string}
*/
get userAgent() {
return this.#userAgent;
}
}

ObjectDefineProperties(Navigator.prototype, {
hardwareConcurrency: kEnumerableProperty,
userAgent: kEnumerableProperty,
});

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ const is = {
is.number(+navigator.hardwareConcurrency, 'hardwareConcurrency');
is.number(navigator.hardwareConcurrency, 'hardwareConcurrency');
assert.ok(navigator.hardwareConcurrency > 0);
assert.strictEqual(typeof navigator.userAgent, 'string');

0 comments on commit 1704a23

Please sign in to comment.