Skip to content

Commit 309c71a

Browse files
authored
lib: add --no-experimental-global-navigator CLI flag
PR-URL: #50562 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Ethan Arrowood <[email protected]>
1 parent 3607b92 commit 309c71a

File tree

6 files changed

+37
-14
lines changed

6 files changed

+37
-14
lines changed

doc/api/cli.md

Lines changed: 12 additions & 0 deletions
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
@@ -2359,6 +2369,7 @@ Node.js options that are allowed are:
23592369
* `--no-deprecation`
23602370
* `--no-experimental-fetch`
23612371
* `--no-experimental-global-customevent`
2372+
* `--no-experimental-global-navigator`
23622373
* `--no-experimental-global-webcrypto`
23632374
* `--no-experimental-repl-await`
23642375
* `--no-extra-info-on-fatal-exception`
@@ -2774,6 +2785,7 @@ done
27742785
[Module customization hooks]: module.md#customization-hooks
27752786
[Module customization hooks: enabling]: module.md#enabling
27762787
[Modules loaders]: packages.md#modules-loaders
2788+
[Navigator API]: globals.md#navigator
27772789
[Node.js issue tracker]: https://github.com/nodejs/node/issues
27782790
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
27792791
[Permission Model]: permissions.md#permission-model

doc/api/globals.md

Lines changed: 5 additions & 10 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ 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,
398+
true);
394399
AddOption("--experimental-global-webcrypto",
395400
"expose experimental Web Crypto API on the global scope",
396401
&EnvironmentOptions::experimental_global_web_crypto,

src/node_options.h

Lines changed: 1 addition & 0 deletions
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)