Skip to content

Commit 0667117

Browse files
committed
lib: add navigator.platform
1 parent 4f5db8b commit 0667117

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

doc/api/globals.md

+15
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,21 @@ logical processors available to the current Node.js instance.
637637
console.log(`This process is running on ${navigator.hardwareConcurrency} logical processors`);
638638
```
639639

640+
### `navigator.platform`
641+
642+
<!-- YAML
643+
added:REPLACEME
644+
-->
645+
646+
* {string}
647+
648+
The `navigator.platform` read-only property returns a string identifying the
649+
platform on which the Node.js instance is running.
650+
651+
```js
652+
console.log(`This process is running on ${navigator.platform}`);
653+
```
654+
640655
### `navigator.userAgent`
641656

642657
<!-- YAML

lib/internal/navigator.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const {
4+
StringPrototypeToUpperCase,
5+
StringPrototypeSlice,
46
ObjectDefineProperties,
57
Symbol,
68
} = primordials;
@@ -24,6 +26,7 @@ class Navigator {
2426
// Private properties are used to avoid brand validations.
2527
#availableParallelism;
2628
#userAgent = `Node.js/${nodeVersion.slice(1, nodeVersion.indexOf('.'))}`;
29+
#platform;
2730

2831
constructor() {
2932
if (arguments[0] === kInitialize) {
@@ -46,11 +49,40 @@ class Navigator {
4649
get userAgent() {
4750
return this.#userAgent;
4851
}
52+
53+
/**
54+
* @return {string}
55+
*/
56+
get platform() {
57+
if (this.#platform === undefined) {
58+
this.#platform = process.platform;
59+
if (process.platform === "darwin") {
60+
// On macOS, modern browsers return "MacIntel" even if running on Apple Silicon.
61+
this.#platform = "MacIntel";
62+
} else if (process.platform === "win32") {
63+
// On Windows, modern browsers return "Win32" even if running on a 64-bit version of Windows.
64+
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform#usage_notes
65+
this.#platform = "Win32";
66+
} else if (process.platform === "linux") {
67+
if (this.arch === "ia32") {
68+
this.#platform = "Linux i686";
69+
} else if (process.arch === 'x64') {
70+
this.#platform = "Linux x86_64";
71+
} else {
72+
this.#platform = `Linux ${process.arch}`;
73+
}
74+
} else {
75+
// freebsd, openbsd, sunos, aix etc.
76+
this.#platform = `${StringPrototypeToUpperCase(process.platform[0])}${StringPrototypeSlice(process.platform, 1)} ${process.arch}`;
77+
}
78+
}
79+
}
4980
}
5081

5182
ObjectDefineProperties(Navigator.prototype, {
5283
hardwareConcurrency: kEnumerableProperty,
5384
userAgent: kEnumerableProperty,
85+
platform: kEnumerableProperty,
5486
});
5587

5688
module.exports = {

test/parallel/test-navigator.js

+13
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ is.number(navigator.hardwareConcurrency, 'hardwareConcurrency');
1515
assert.ok(navigator.hardwareConcurrency > 0);
1616
assert.strictEqual(typeof navigator.userAgent, 'string');
1717
assert.match(navigator.userAgent, /^Node\.js\/\d+$/);
18+
19+
assert.strictEqual(typeof navigator.platform, 'string');
20+
if (process.platform === 'darwin') {
21+
assert.strictEqual(navigator.platform, 'MacIntel');
22+
} else if (process.platform === 'win32') {
23+
assert.strictEqual(navigator.platform, 'Win32');
24+
} else if (process.platform === 'linux' && process.arch === 'ia32') {
25+
assert.strictEqual(navigator.platform, 'Linux i686');
26+
} else if (process.platform === 'linux' && process.arch === 'x64') {
27+
assert.strictEqual(navigator.platform, 'Linux x86_64');
28+
} else {
29+
assert.strictEqual(navigator.platform, `${process.platform[0].toUpperCase()}${process.platform.slice(1)} ${process.arch}`);
30+
}

0 commit comments

Comments
 (0)