Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add navigator.userAgent #50200

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
anonrig marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: REPLACEME
-->

* {string}

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

```js
console.log(`The user-agent is ${navigator.userAgent}`); // Prints "Node.js/21"
```
anonrig marked this conversation as resolved.
Show resolved Hide resolved

## `PerformanceEntry`

<!-- YAML
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const {
} = internalBinding('os');

const kInitialize = Symbol('kInitialize');
const nodeVersion = process.version;

class Navigator {
// Private properties are used to avoid brand validations.
#availableParallelism;
#userAgent = `Node.js/${nodeVersion.slice(1, nodeVersion.indexOf('.'))}`;
anonrig marked this conversation as resolved.
Show resolved Hide resolved

constructor() {
if (arguments[0] === kInitialize) {
Expand All @@ -37,10 +39,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
2 changes: 2 additions & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ const is = {
is.number(+navigator.hardwareConcurrency, 'hardwareConcurrency');
is.number(navigator.hardwareConcurrency, 'hardwareConcurrency');
assert.ok(navigator.hardwareConcurrency > 0);
assert.strictEqual(typeof navigator.userAgent, 'string');
anonrig marked this conversation as resolved.
Show resolved Hide resolved
assert.match(navigator.userAgent, /^Node\.js\/\d+$/);