summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYagiz Nizipli <yagiz@nizipli.com>2023-04-28 12:21:01 -0400
committerYagiz Nizipli <yagiz@nizipli.com>2023-04-28 14:59:39 -0400
commiteb7379b62e356d973c91a2c1d433d8d2770cee18 (patch)
treeb763546dfc65fc860b7fb5dbfe2ffaa73328c264 /lib
parentb31d587dc8c1b0f459243d2b96a17a7175c996cf (diff)
downloadnode-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>
Diffstat (limited to 'lib')
-rw-r--r--lib/.eslintrc.yaml2
-rw-r--r--lib/internal/bootstrap/node.js9
-rw-r--r--lib/internal/navigator.js36
3 files changed, 47 insertions, 0 deletions
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();