Skip to content

Commit

Permalink
lib: add navigator.platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Oct 25, 2023
1 parent 4f5db8b commit 42b169f
Show file tree
Hide file tree
Showing 3 changed files with 61 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 @@ -637,6 +637,21 @@ logical processors available to the current Node.js instance.
console.log(`This process is running on ${navigator.hardwareConcurrency} logical processors`);
```

### `navigator.platform`

<!-- YAML
added: REPLACEME
-->

* {string}

The `navigator.platform` read-only property returns a string identifying the
platform on which the Node.js instance is running.

```js
console.log(`This process is running on ${navigator.platform}`);
```

### `navigator.userAgent`

<!-- YAML
Expand Down
33 changes: 33 additions & 0 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const {
StringPrototypeToUpperCase,
StringPrototypeSlice,
ObjectDefineProperties,
Symbol,
} = primordials;
Expand All @@ -24,6 +26,7 @@ class Navigator {
// Private properties are used to avoid brand validations.
#availableParallelism;
#userAgent = `Node.js/${nodeVersion.slice(1, nodeVersion.indexOf('.'))}`;
#platform;

constructor() {
if (arguments[0] === kInitialize) {
Expand All @@ -46,11 +49,41 @@ class Navigator {
get userAgent() {
return this.#userAgent;
}

/**
* @return {string}
*/
get platform() {
if (this.#platform === undefined) {
this.#platform = process.platform;
if (process.platform === 'darwin') {
// On macOS, modern browsers return 'MacIntel'even if running on Apple Silicon.
this.#platform = 'MacIntel';
} else if (process.platform === 'win32') {
// On Windows, modern browsers return "Win32" even if running on a 64-bit version of Windows.
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#usage_notes
this.#platform = 'Win32';
} else if (process.platform === 'linux') {
if (this.arch === 'ia32') {
this.#platform = 'Linux i686';
} else if (process.arch === 'x64') {
this.#platform = 'Linux x86_64';
} else {
this.#platform = `Linux ${process.arch}`;
}
} else {
// For cases like freebsd, openbsd, sunos, aix etc.
this.#platform = `${StringPrototypeToUpperCase(process.platform[0])}${StringPrototypeSlice(process.platform, 1)} ${process.arch}`;
}
}
return this.#platform;
}
}

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

module.exports = {
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ is.number(navigator.hardwareConcurrency, 'hardwareConcurrency');
assert.ok(navigator.hardwareConcurrency > 0);
assert.strictEqual(typeof navigator.userAgent, 'string');
assert.match(navigator.userAgent, /^Node\.js\/\d+$/);

assert.strictEqual(typeof navigator.platform, 'string');
if (process.platform === 'darwin') {
assert.strictEqual(navigator.platform, 'MacIntel');
} else if (process.platform === 'win32') {
assert.strictEqual(navigator.platform, 'Win32');
} else if (process.platform === 'linux' && process.arch === 'ia32') {
assert.strictEqual(navigator.platform, 'Linux i686');
} else if (process.platform === 'linux' && process.arch === 'x64') {
assert.strictEqual(navigator.platform, 'Linux x86_64');
} else {
assert.strictEqual(navigator.platform, `${process.platform[0].toUpperCase()}${process.platform.slice(1)} ${process.arch}`);
}

0 comments on commit 42b169f

Please sign in to comment.