Skip to content

Commit 3860002

Browse files
committed
lib: add --no-experimental-global-navigator CLI flag
1 parent 77b0595 commit 3860002

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

doc/api/cli.md

+12
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,16 @@ added: v19.0.0
11961196

11971197
Disable exposition of [CustomEvent Web API][] on the global scope.
11981198

1199+
### `--no-experimental-global-navigator`
1200+
1201+
<!-- YAML
1202+
added: REPLACEME
1203+
-->
1204+
1205+
> Stability: 1 - Experimental
1206+
1207+
Disable exposition of [Navigator API][] on the global scope.
1208+
11991209
### `--no-experimental-global-webcrypto`
12001210

12011211
<!-- YAML
@@ -2315,6 +2325,7 @@ Node.js options that are allowed are:
23152325
* `--experimental-abortcontroller`
23162326
* `--experimental-default-type`
23172327
* `--experimental-detect-module`
2328+
* `--experimental-global-navigator`
23182329
* `--experimental-import-meta-resolve`
23192330
* `--experimental-json-modules`
23202331
* `--experimental-loader`
@@ -2765,6 +2776,7 @@ done
27652776
[Module customization hooks]: module.md#customization-hooks
27662777
[Module customization hooks: enabling]: module.md#enabling
27672778
[Modules loaders]: packages.md#modules-loaders
2779+
[Navigator API]: globals.md#navigator
27682780
[Node.js issue tracker]: https://github.com/nodejs/node/issues
27692781
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
27702782
[Permission Model]: permissions.md#permission-model

doc/api/globals.md

+5-10
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,8 @@ This variable may appear to be global but is not. See [`module`][].
600600
added: v21.0.0
601601
-->
602602

603-
> Stability: 1.1 - Active development
603+
> Stability: 1.1 - Active development. Disable this API with the
604+
> [`--no-experimental-global-navigator`][] CLI flag.
604605
605606
A partial implementation of the [Navigator API][].
606607

@@ -610,18 +611,11 @@ A partial implementation of the [Navigator API][].
610611
added: v21.0.0
611612
-->
612613

613-
> Stability: 1.1 - Active development
614+
> Stability: 1.1 - Active development. Disable this API with the
615+
> [`--no-experimental-global-navigator`][] CLI flag.
614616
615617
A partial implementation of [`window.navigator`][].
616618

617-
If your app or a dependency uses a check for `navigator` to determine whether it
618-
is running in a browser, the following can be used to delete the `navigator`
619-
global before app code runs:
620-
621-
```bash
622-
node --import 'data:text/javascript,delete globalThis.navigator' app.js
623-
```
624-
625619
### `navigator.hardwareConcurrency`
626620

627621
<!-- YAML
@@ -1145,6 +1139,7 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
11451139
[Web Crypto API]: webcrypto.md
11461140
[`--experimental-websocket`]: cli.md#--experimental-websocket
11471141
[`--no-experimental-global-customevent`]: cli.md#--no-experimental-global-customevent
1142+
[`--no-experimental-global-navigator`]: cli.md#--no-experimental-global-navigator
11481143
[`--no-experimental-global-webcrypto`]: cli.md#--no-experimental-global-webcrypto
11491144
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
11501145
[`ByteLengthQueuingStrategy`]: webstreams.md#class-bytelengthqueuingstrategy

lib/internal/bootstrap/web/exposed-window-or-worker.js

-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ exposeLazyInterfaces(globalThis, 'perf_hooks', [
5252

5353
defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
5454

55-
// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
56-
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
57-
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
58-
5955
// https://w3c.github.io/FileAPI/#creating-revoking
6056
const { installObjectURLMethods } = require('internal/url');
6157
installObjectURLMethods();

lib/internal/process/pre_execution.js

+14
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function prepareExecution(options) {
7777
const mainEntry = patchProcessObject(expandArgv1);
7878
setupTraceCategoryState();
7979
setupInspectorHooks();
80+
setupNavigator();
8081
setupWarningHandler();
8182
setupUndici();
8283
setupWebCrypto();
@@ -336,6 +337,19 @@ function setupUndici() {
336337
}
337338
}
338339

340+
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
341+
// removed.
342+
function setupNavigator() {
343+
if (getEmbedderOptions().noBrowserGlobals ||
344+
getOptionValue('--no-experimental-global-navigator')) {
345+
return;
346+
}
347+
348+
// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
349+
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
350+
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
351+
}
352+
339353
// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
340354
// removed.
341355
function setupWebCrypto() {

src/node_options.cc

+4
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
391391
&EnvironmentOptions::experimental_global_customevent,
392392
kAllowedInEnvvar,
393393
true);
394+
AddOption("--experimental-global-navigator",
395+
"expose experimental Navigator API on the global scope",
396+
&EnvironmentOptions::experimental_global_navigator,
397+
kAllowedInEnvvar);
394398
AddOption("--experimental-global-webcrypto",
395399
"expose experimental Web Crypto API on the global scope",
396400
&EnvironmentOptions::experimental_global_web_crypto,

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class EnvironmentOptions : public Options {
110110
bool experimental_fetch = true;
111111
bool experimental_websocket = false;
112112
bool experimental_global_customevent = true;
113+
bool experimental_global_navigator = true;
113114
bool experimental_global_web_crypto = true;
114115
bool experimental_https_modules = false;
115116
bool experimental_wasm_modules = false;

0 commit comments

Comments
 (0)