Skip to content

Commit

Permalink
lib: add navigator.platform
Browse files Browse the repository at this point in the history
Co-authored-by: Mestery <mestery@protonmail.com>
Co-authored-by: Voltrex <mohammadkeyvanzade94@gmail.com>
  • Loading branch information
3 people committed Apr 28, 2023
1 parent b31d587 commit eb7379b
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ module.exports = {
DecompressionStream: 'readable',
fetch: 'readable',
FormData: 'readable',
navigator: 'readable',
ReadableStream: 'readable',
ReadableStreamDefaultReader: 'readable',
ReadableStreamBYOBReader: 'readable',
Expand Down
29 changes: 29 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,33 @@ The `MessagePort` class. See [`MessagePort`][] for more details.

This variable may appear to be global but is not. See [`module`][].

## `navigator`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
>
> An implementation of the [Navigator API][]. Similar to [`window.navigator`][]
> in browsers.
### `navigator.platform`

<!-- YAML
added: REPLACEME
-->

* {string}

A string identifying the operating system platform on which the Node.js process
is running. For example, it returns 'Linux' on Linux, 'Darwin' on macOS, and
'Win32' on Windows.

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

## `PerformanceEntry`

<!-- YAML
Expand Down Expand Up @@ -980,6 +1007,7 @@ added: v18.0.0
A browser-compatible implementation of [`WritableStreamDefaultWriter`][].

[Navigator API]: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
[Web Crypto API]: webcrypto.md
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
[`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent
Expand Down Expand Up @@ -1037,6 +1065,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
[`setInterval`]: timers.md#setintervalcallback-delay-args
[`setTimeout`]: timers.md#settimeoutcallback-delay-args
[`structuredClone`]: https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
[`window.navigator`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator
[buffer section]: buffer.md
[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
[module system documentation]: modules.md
Expand Down
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ rules:
message: Use `const { MessageEvent } = require('internal/worker/io');` instead of the global.
- name: MessagePort
message: Use `const { MessagePort } = require('internal/worker/io');` instead of the global.
- name: Navigator
message: Use `const navigator = require('internal/navigator');` instead of the global.
- name: PerformanceEntry
message: Use `const { PerformanceEntry } = require('perf_hooks');` instead of the global.
- name: PerformanceMark
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ defineLazyProperties(
['structuredClone'],
);

// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
ObjectDefineProperty(globalThis, 'navigator', {
__proto__: null,
enumerable: true,
configurable: true,
writable: false,
value: require('internal/navigator'),
});

// Set the per-Environment callback that will be called
// when the TrackingTraceStateObserver updates trace state.
// Note that when NODE_USE_V8_PLATFORM is true, the observer is
Expand Down
36 changes: 36 additions & 0 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const {
ObjectDefineProperties,
} = primordials;

const {
kEnumerableProperty,
} = require('internal/util');

const {
getOSInformation,
} = internalBinding('os');

class Navigator {
/**
* Chromium: https://github.com/chromium/chromium/blob/main/ui/webui/resources/js/platform.ts
* @return {string}
*/
get platform() {
switch (process.platform) {
case 'win32': return 'Win32';
case 'android': return 'Android';
case 'darwin':
// It should return MacIntel for both M1 and Intel mac devices.
return 'MacIntel';
default: return getOSInformation()[0];
}
}
}

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

module.exports = new Navigator();
4 changes: 4 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ if (global.gc) {
knownGlobals.push(global.gc);
}

if (global.navigator) {
knownGlobals.push(global.navigator);
}

if (global.Performance) {
knownGlobals.push(global.Performance);
}
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const expectedModules = new Set([
'NativeModule async_hooks',
'NativeModule internal/process/task_queues',
'NativeModule timers',
'Internal Binding os',
'NativeModule internal/navigator',
'Internal Binding trace_events',
'NativeModule internal/constants',
'NativeModule path',
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ builtinModules.forEach((moduleName) => {
'clearImmediate',
'clearInterval',
'clearTimeout',
'navigator',
'atob',
'btoa',
'performance',
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

require('../common');
const assert = require('assert');
const { platform } = require('process');

assert.strictEqual(navigator.platform, {
aix: 'AIX',
android: 'Android',
darwin: 'MacIntel',
freebsd: 'FreeBSD',
linux: 'Linux',
openbsd: 'OpenBSD',
sunos: 'SunOS',
win32: 'Win32',
}[platform]);

0 comments on commit eb7379b

Please sign in to comment.