lib: add navigator.userAgent
PR-URL: https://github.com/nodejs/node/pull/50200 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
This commit is contained in:
parent
cd6b86bb5a
commit
05a7810a1d
@ -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 major version number.
|
||||
|
||||
```js
|
||||
console.log(`The user-agent is ${navigator.userAgent}`); // Prints "Node.js/21"
|
||||
```
|
||||
|
||||
## `PerformanceEntry`
|
||||
|
||||
<!-- YAML
|
||||
|
@ -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('.'))}`;
|
||||
|
||||
constructor() {
|
||||
if (arguments[0] === kInitialize) {
|
||||
@ -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 = {
|
||||
|
@ -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');
|
||||
assert.match(navigator.userAgent, /^Node\.js\/\d+$/);
|
||||
|
Loading…
x
Reference in New Issue
Block a user