Skip to content

Commit 07a80cb

Browse files
anonrigMesteeryVoltrexKeyva
committed
lib: add navigator.platform
Co-authored-by: Mestery <[email protected]> Co-authored-by: Voltrex <[email protected]>
1 parent b31d587 commit 07a80cb

File tree

9 files changed

+93
-0
lines changed

9 files changed

+93
-0
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ module.exports = {
327327
DecompressionStream: 'readable',
328328
fetch: 'readable',
329329
FormData: 'readable',
330+
navigator: 'readable',
330331
ReadableStream: 'readable',
331332
ReadableStreamDefaultReader: 'readable',
332333
ReadableStreamBYOBReader: 'readable',

doc/api/globals.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,33 @@ The `MessagePort` class. See [`MessagePort`][] for more details.
567567

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

570+
## `navigator`
571+
572+
<!-- YAML
573+
added: REPLACEME
574+
-->
575+
576+
> Stability: 1 - Experimental
577+
>
578+
> An implementation of the [Navigator API][]. Similar to [`window.navigator`][]
579+
> in browsers.
580+
581+
### `navigator.platform`
582+
583+
<!-- YAML
584+
added: REPLACEME
585+
-->
586+
587+
* {string}
588+
589+
A string identifying the operating system platform on which the Node.js process
590+
is running. For example, it returns 'Linux' on Linux, 'Darwin' on macOS, and
591+
'Win32' on Windows.
592+
593+
```js
594+
console.log(`This process is running on ${navigator.platform}`);
595+
```
596+
570597
## `PerformanceEntry`
571598

572599
<!-- YAML
@@ -980,6 +1007,7 @@ added: v18.0.0
9801007
9811008
A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
9821009

1010+
[Navigator API]: https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
9831011
[Web Crypto API]: webcrypto.md
9841012
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
9851013
[`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent
@@ -1037,6 +1065,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
10371065
[`setInterval`]: timers.md#setintervalcallback-delay-args
10381066
[`setTimeout`]: timers.md#settimeoutcallback-delay-args
10391067
[`structuredClone`]: https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
1068+
[`window.navigator`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/navigator
10401069
[buffer section]: buffer.md
10411070
[built-in objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
10421071
[module system documentation]: modules.md

lib/.eslintrc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ rules:
7575
message: Use `const { MessageEvent } = require('internal/worker/io');` instead of the global.
7676
- name: MessagePort
7777
message: Use `const { MessagePort } = require('internal/worker/io');` instead of the global.
78+
- name: Navigator
79+
message: Use `const navigator = require('internal/navigator');` instead of the global.
7880
- name: PerformanceEntry
7981
message: Use `const { PerformanceEntry } = require('perf_hooks');` instead of the global.
8082
- name: PerformanceMark

lib/internal/bootstrap/node.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ defineLazyProperties(
224224
['structuredClone'],
225225
);
226226

227+
// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
228+
ObjectDefineProperty(globalThis, 'navigator', {
229+
__proto__: null,
230+
enumerable: true,
231+
configurable: true,
232+
writable: false,
233+
value: require('internal/navigator'),
234+
});
235+
227236
// Set the per-Environment callback that will be called
228237
// when the TrackingTraceStateObserver updates trace state.
229238
// Note that when NODE_USE_V8_PLATFORM is true, the observer is

lib/internal/navigator.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const {
4+
ObjectDefineProperties,
5+
} = primordials;
6+
7+
const {
8+
kEnumerableProperty,
9+
} = require('internal/util');
10+
11+
const {
12+
getOSInformation,
13+
} = internalBinding('os');
14+
15+
class Navigator {
16+
get platform() {
17+
switch (process.platform) {
18+
case 'win32': return 'Win32';
19+
case 'android': return 'Android';
20+
default: return getOSInformation()[0];
21+
}
22+
}
23+
}
24+
25+
ObjectDefineProperties(Navigator.prototype, {
26+
platform: kEnumerableProperty,
27+
});
28+
29+
module.exports = new Navigator();

test/common/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ if (global.gc) {
308308
knownGlobals.push(global.gc);
309309
}
310310

311+
if (global.navigator) {
312+
knownGlobals.push(global.navigator);
313+
}
314+
311315
if (global.Performance) {
312316
knownGlobals.push(global.Performance);
313317
}

test/parallel/test-bootstrap-modules.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const expectedModules = new Set([
4646
'NativeModule async_hooks',
4747
'NativeModule internal/process/task_queues',
4848
'NativeModule timers',
49+
'Internal Binding os',
50+
'NativeModule internal/navigator',
4951
'Internal Binding trace_events',
5052
'NativeModule internal/constants',
5153
'NativeModule path',

test/parallel/test-global.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ builtinModules.forEach((moduleName) => {
4949
'clearImmediate',
5050
'clearInterval',
5151
'clearTimeout',
52+
'navigator',
5253
'atob',
5354
'btoa',
5455
'performance',

test/parallel/test-navigator.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { platform } = require('process');
6+
7+
assert.strictEqual(navigator.platform, {
8+
aix: 'AIX',
9+
android: 'Android',
10+
darwin: 'Darwin',
11+
freebsd: 'FreeBSD',
12+
linux: 'Linux',
13+
openbsd: 'OpenBSD',
14+
sunos: 'SunOS',
15+
win32: 'Win32',
16+
}[platform]);

0 commit comments

Comments
 (0)