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 0667117
Show file tree
Hide file tree
Showing 3 changed files with 60 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
32 changes: 32 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,40 @@ class Navigator {
get userAgent() {
return this.#userAgent;
}

/**
* @return {string}
*/
get platform() {

Check failure on line 56 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected to return a value in getter 'platform'
if (this.#platform === undefined) {
this.#platform = process.platform;
if (process.platform === "darwin") {

Check failure on line 59 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
// On macOS, modern browsers return "MacIntel" even if running on Apple Silicon.
this.#platform = "MacIntel";

Check failure on line 61 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
} else if (process.platform === "win32") {

Check failure on line 62 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
// 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";

Check failure on line 65 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
} else if (process.platform === "linux") {

Check failure on line 66 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
if (this.arch === "ia32") {

Check failure on line 67 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
this.#platform = "Linux i686";

Check failure on line 68 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
} else if (process.arch === 'x64') {
this.#platform = "Linux x86_64";

Check failure on line 70 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Strings must use singlequote
} else {
this.#platform = `Linux ${process.arch}`;
}
} else {
// freebsd, openbsd, sunos, aix etc.

Check failure on line 75 in lib/internal/navigator.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Comments should not begin with a lowercase character
this.#platform = `${StringPrototypeToUpperCase(process.platform[0])}${StringPrototypeSlice(process.platform, 1)} ${process.arch}`;
}
}
}
}

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 0667117

Please sign in to comment.