diff options
| author | Yagiz Nizipli <yagiz@nizipli.com> | 2023-04-28 12:21:01 -0400 |
|---|---|---|
| committer | Yagiz Nizipli <yagiz@nizipli.com> | 2023-04-28 14:59:39 -0400 |
| commit | eb7379b62e356d973c91a2c1d433d8d2770cee18 (patch) | |
| tree | b763546dfc65fc860b7fb5dbfe2ffaa73328c264 | |
| parent | b31d587dc8c1b0f459243d2b96a17a7175c996cf (diff) | |
| download | node-new-navigator-platform.tar.gz | |
lib: add `navigator.platform`navigator-platform
Co-authored-by: Mestery <mestery@protonmail.com>
Co-authored-by: Voltrex <mohammadkeyvanzade94@gmail.com>
| -rw-r--r-- | .eslintrc.js | 1 | ||||
| -rw-r--r-- | doc/api/globals.md | 29 | ||||
| -rw-r--r-- | lib/.eslintrc.yaml | 2 | ||||
| -rw-r--r-- | lib/internal/bootstrap/node.js | 9 | ||||
| -rw-r--r-- | lib/internal/navigator.js | 36 | ||||
| -rw-r--r-- | test/common/index.js | 4 | ||||
| -rw-r--r-- | test/parallel/test-bootstrap-modules.js | 2 | ||||
| -rw-r--r-- | test/parallel/test-global.js | 1 | ||||
| -rw-r--r-- | test/parallel/test-navigator.js | 16 |
9 files changed, 100 insertions, 0 deletions
diff --git a/.eslintrc.js b/.eslintrc.js index e1961de42d..feb1f83683 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -327,6 +327,7 @@ module.exports = { DecompressionStream: 'readable', fetch: 'readable', FormData: 'readable', + navigator: 'readable', ReadableStream: 'readable', ReadableStreamDefaultReader: 'readable', ReadableStreamBYOBReader: 'readable', diff --git a/doc/api/globals.md b/doc/api/globals.md index 306d800dbd..0d5b6a106b 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -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 @@ -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 @@ -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 diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index cc4fa19750..538dbbd323 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -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 diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 41a833885f..e25e0405c7 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -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 diff --git a/lib/internal/navigator.js b/lib/internal/navigator.js new file mode 100644 index 0000000000..7bf0779b90 --- /dev/null +++ b/lib/internal/navigator.js @@ -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(); diff --git a/test/common/index.js b/test/common/index.js index f3caa9d1d4..7a38c44322 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -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); } diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 0a671eb95e..32f90905b6 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -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', diff --git a/test/parallel/test-global.js b/test/parallel/test-global.js index 9ac9b4f728..3fd9a3f6f5 100644 --- a/test/parallel/test-global.js +++ b/test/parallel/test-global.js @@ -49,6 +49,7 @@ builtinModules.forEach((moduleName) => { 'clearImmediate', 'clearInterval', 'clearTimeout', + 'navigator', 'atob', 'btoa', 'performance', diff --git a/test/parallel/test-navigator.js b/test/parallel/test-navigator.js new file mode 100644 index 0000000000..ba760846bb --- /dev/null +++ b/test/parallel/test-navigator.js @@ -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]); |
