GeezerVison is showing github.com — lib: add `navigator.userAgent` · nodejs/node@5a52c51
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/globals.md +15Lines changed: 15 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -629,6 +629,21 @@ logical processors available to the current Node.js instance. |
629 | 629 | console.log(`This process is running on ${navigator.hardwareConcurrency}`); |
630 | 630 | ``` |
631 | 631 |
|
| 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 | + |
632 | 647 | ## `PerformanceEntry` |
633 | 648 |
|
634 | 649 | <!-- YAML |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/internal/navigator.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,10 +18,12 @@ const { |
18 | 18 | } = internalBinding('os'); |
19 | 19 |
|
20 | 20 | const kInitialize = Symbol('kInitialize'); |
| 21 | +const nodeVersion = process.version; |
21 | 22 |
|
22 | 23 | class Navigator { |
23 | 24 | // Private properties are used to avoid brand validations. |
24 | 25 | #availableParallelism; |
| 26 | + #userAgent = `Node.js/${nodeVersion.slice(1, nodeVersion.indexOf('.'))}`; |
25 | 27 |
|
26 | 28 | constructor() { |
27 | 29 | if (arguments[0] === kInitialize) { |
@@ -37,10 +39,18 @@ class Navigator { |
37 | 39 | this.#availableParallelism ??= getAvailableParallelism(); |
38 | 40 | return this.#availableParallelism; |
39 | 41 | } |
| 42 | + |
| 43 | + /** |
| 44 | + * @return {string} |
| 45 | + */ |
| 46 | + get userAgent() { |
| 47 | + return this.#userAgent; |
| 48 | + } |
40 | 49 | } |
41 | 50 |
|
42 | 51 | ObjectDefineProperties(Navigator.prototype, { |
43 | 52 | hardwareConcurrency: kEnumerableProperty, |
| 53 | + userAgent: kEnumerableProperty, |
44 | 54 | }); |
45 | 55 |
|
46 | 56 | module.exports = { |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-navigator.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,3 +13,5 @@ const is = { |
13 | 13 | is.number(+navigator.hardwareConcurrency, 'hardwareConcurrency'); |
14 | 14 | is.number(navigator.hardwareConcurrency, 'hardwareConcurrency'); |
15 | 15 | assert.ok(navigator.hardwareConcurrency > 0); |
| 16 | +assert.strictEqual(typeof navigator.userAgent, 'string'); |
| 17 | +assert.match(navigator.userAgent, /^Node\.js\/\d+$/); |