Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: hide navigator behind a flag #50412

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,16 @@ CommonJS. This includes `import` and `export` statements and `import.meta`
references. It does _not_ include `import()` expressions, which are valid in
CommonJS.

### `--experimental-global-navigator`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

Enable exposition of [Navigator API][] on the global scope.

### `--experimental-import-meta-resolve`

<!-- YAML
Expand Down Expand Up @@ -2313,6 +2323,7 @@ Node.js options that are allowed are:
* `--experimental-abortcontroller`
* `--experimental-default-type`
* `--experimental-detect-module`
* `--experimental-global-navigator`
* `--experimental-import-meta-resolve`
* `--experimental-json-modules`
* `--experimental-loader`
Expand Down Expand Up @@ -2763,6 +2774,7 @@ done
[Module customization hooks]: module.md#customization-hooks
[Module customization hooks: enabling]: module.md#enabling
[Modules loaders]: packages.md#modules-loaders
[Navigator API]: globals.md#navigator
[Node.js issue tracker]: https://github.com/nodejs/node/issues
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
[Permission Model]: permissions.md#permission-model
Expand Down
22 changes: 12 additions & 10 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,30 +598,32 @@ This variable may appear to be global but is not. See [`module`][].

<!-- YAML
added: v21.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/50412
description: The API is behind the CLI flag `--experimental-global-navigator`.
-->

> Stability: 1.1 - Active development
> Stability: 1.1 - Active development. Use `--experimental-global-navigator` to
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
> enable this feature.

A partial implementation of the [Navigator API][].

## `navigator`

<!-- YAML
added: v21.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/50412
description: The API is behind the CLI flag `--experimental-global-navigator`.
-->

> Stability: 1.1 - Active development
> Stability: 1.1 - Active development. Use `--experimental-global-navigator` to
> enable this feature.

A partial implementation of [`window.navigator`][].

If your app or a dependency uses a check for `navigator` to determine whether it
is running in a browser, the following can be used to delete the `navigator`
global before app code runs:

```bash
node --import 'data:text/javascript,delete globalThis.navigator' app.js
```

### `navigator.hardwareConcurrency`

<!-- YAML
Expand Down
4 changes: 0 additions & 4 deletions lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ exposeLazyInterfaces(globalThis, 'perf_hooks', [

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

// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);

// https://w3c.github.io/FileAPI/#creating-revoking
const { installObjectURLMethods } = require('internal/url');
installObjectURLMethods();
14 changes: 14 additions & 0 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function prepareExecution(options) {
const mainEntry = patchProcessObject(expandArgv1);
setupTraceCategoryState();
setupInspectorHooks();
setupNavigator();
setupWarningHandler();
setupUndici();
setupWebCrypto();
Expand Down Expand Up @@ -336,6 +337,19 @@ function setupUndici() {
}
}

// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
// removed.
function setupNavigator() {
if (getEmbedderOptions().noBrowserGlobals ||
getOptionValue('--no-experimental-global-navigator')) {
return;
}

// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
}

// TODO(aduh95): move this to internal/bootstrap/web/* when the CLI flag is
// removed.
function setupWebCrypto() {
Expand Down
4 changes: 4 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::experimental_global_customevent,
kAllowedInEnvvar,
true);
AddOption("--experimental-global-navigator",
"expose experimental Navigator API on the global scope",
&EnvironmentOptions::experimental_global_navigator,
kAllowedInEnvvar);
AddOption("--experimental-global-webcrypto",
"expose experimental Web Crypto API on the global scope",
&EnvironmentOptions::experimental_global_web_crypto,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class EnvironmentOptions : public Options {
bool experimental_fetch = true;
bool experimental_websocket = false;
bool experimental_global_customevent = true;
bool experimental_global_navigator = false;
bool experimental_global_web_crypto = true;
bool experimental_https_modules = false;
bool experimental_wasm_modules = false;
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ builtinModules.forEach((moduleName) => {
'structuredClone',
'fetch',
'crypto',
'navigator',
];
assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected));
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-navigator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --experimental-global-navigator
'use strict';

require('../common');
Expand Down
Loading