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: enable fetch by default #41811

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 9 additions & 9 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,6 @@ effort to report stack traces relative to the original source file.
Overriding `Error.prepareStackTrace` prevents `--enable-source-maps` from
modifying the stack trace.

### `--experimental-fetch`

<!-- YAML
added: v17.5.0
-->

Enable experimental support for the [Fetch API][].

### `--experimental-global-webcrypto`

<!-- YAML
Expand Down Expand Up @@ -333,6 +325,14 @@ added: v11.8.0

Use the specified file as a security policy.

### `--no-experimental-fetch`
targos marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: REPLACEME
-->

Disable experimental support for the [Fetch API][].

### `--no-experimental-repl-await`

<!-- YAML
Expand Down Expand Up @@ -1587,7 +1587,6 @@ Node.js options that are allowed are:
* `--enable-fips`
* `--enable-source-maps`
* `--experimental-abortcontroller`
* `--experimental-fetch`
* `--experimental-global-webcrypto`
* `--experimental-import-meta-resolve`
* `--experimental-json-modules`
Expand Down Expand Up @@ -1617,6 +1616,7 @@ Node.js options that are allowed are:
* `--napi-modules`
* `--no-addons`
* `--no-deprecation`
* `--no-experimental-fetch`
* `--no-experimental-repl-await`
* `--no-extra-info-on-fatal-exception`
* `--no-force-async-hooks-checks`
Expand Down
12 changes: 6 additions & 6 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ This variable may appear to be global but is not. See [`exports`][].
added: v17.5.0
-->

> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
> CLI flag.

A browser-compatible implementation of the [`fetch()`][] function.
Expand All @@ -395,7 +395,7 @@ A browser-compatible implementation of the [`fetch()`][] function.
added: REPLACEME
-->

> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
> CLI flag.

A browser-compatible implementation of {FormData}.
Expand All @@ -421,7 +421,7 @@ Node.js this is different. The top-level scope is not the global scope;
added: v17.5.0
-->

> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
> CLI flag.

A browser-compatible implementation of {Headers}.
Expand Down Expand Up @@ -526,7 +526,7 @@ This variable may appear to be global but is not. See [`require()`][].
added: v17.5.0
-->

> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
> CLI flag.

A browser-compatible implementation of {Response}.
Expand All @@ -537,7 +537,7 @@ A browser-compatible implementation of {Response}.
added: v17.5.0
-->

> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
> CLI flag.

A browser-compatible implementation of {Request}.
Expand Down Expand Up @@ -660,8 +660,8 @@ The object that acts as the namespace for all W3C
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.

[Web Crypto API]: webcrypto.md
[`--experimental-fetch`]: cli.md#--experimental-fetch
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api
Expand Down
6 changes: 3 additions & 3 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ Requires Node.js to be built with
.It Fl -enable-source-maps
Enable Source Map V3 support for stack traces.
.
.It Fl -experimental-fetch
Enable experimental support for the Fetch API.
.
.It Fl -experimental-global-webcrypto
Expose the Web Crypto API on the global scope.
.
Expand All @@ -159,6 +156,9 @@ Enable experimental support for loading modules using `import` over `https:`.
.It Fl -experimental-policy
Use the specified file as a security policy.
.
.It Fl -no-experimental-fetch
Disable experimental support for the Fetch API.
.
.It Fl -no-experimental-repl-await
Disable top-level await keyword support in REPL.
.
Expand Down
44 changes: 36 additions & 8 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
NumberParseInt,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
SafeMap,
Expand Down Expand Up @@ -152,18 +153,45 @@ function setupWarningHandler() {
// https://fetch.spec.whatwg.org/
function setupFetch() {
if (process.config.variables.node_no_browser_globals ||
!getOptionValue('--experimental-fetch')) {
getOptionValue('--no-experimental-fetch')) {
return;
}

emitExperimentalWarning('Fetch');
targos marked this conversation as resolved.
Show resolved Hide resolved
let undici;
function lazyUndici() {
if (undici) {
return undici;
}

emitExperimentalWarning('The Fetch API');
undici = require('internal/deps/undici/undici');
targos marked this conversation as resolved.
Show resolved Hide resolved
}

async function fetch(input, init = undefined) {
return lazyUndici().fetch(input, init);
}

defineOperation(globalThis, 'fetch', fetch);

const undici = require('internal/deps/undici/undici');
defineOperation(globalThis, 'fetch', undici.fetch);
exposeInterface(globalThis, 'FormData', undici.FormData);
exposeInterface(globalThis, 'Headers', undici.Headers);
exposeInterface(globalThis, 'Request', undici.Request);
exposeInterface(globalThis, 'Response', undici.Response);
function lazyInterface(name) {
return {
configurable: true,
enumerable: false,
get() {
return lazyUndici()[name];
},
set(value) {
exposeInterface(globalThis, name, value);
}
};
}

ObjectDefineProperties(globalThis, {
FormData: lazyInterface('FormData'),
targos marked this conversation as resolved.
Show resolved Hide resolved
Headers: lazyInterface('Headers'),
Request: lazyInterface('Request'),
Response: lazyInterface('Response'),
});
}

// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
Expand Down
3 changes: 2 additions & 1 deletion src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
AddOption("--experimental-fetch",
"experimental Fetch API",
&EnvironmentOptions::experimental_fetch,
kAllowedInEnvironment);
kAllowedInEnvironment,
true);
AddOption("--experimental-global-webcrypto",
"expose experimental Web Crypto API on the global scope",
&EnvironmentOptions::experimental_global_web_crypto,
Expand Down
2 changes: 1 addition & 1 deletion src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class EnvironmentOptions : public Options {
std::vector<std::string> conditions;
std::string dns_result_order;
bool enable_source_maps = false;
bool experimental_fetch = false;
bool experimental_fetch = true;
bool experimental_global_web_crypto = false;
bool experimental_https_modules = false;
std::string experimental_specifier_resolution;
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fetch-disabled.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --no-experimental-fetch
import '../common/index.mjs';

import assert from 'assert';
Expand Down
9 changes: 6 additions & 3 deletions test/parallel/test-fetch.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// Flags: --experimental-fetch --no-warnings
targos marked this conversation as resolved.
Show resolved Hide resolved

import '../common/index.mjs';
import * as common from '../common/index.mjs';

import assert from 'assert';
import events from 'events';
Expand All @@ -12,6 +10,11 @@ assert.strictEqual(typeof globalThis.Headers, 'function');
assert.strictEqual(typeof globalThis.Request, 'function');
assert.strictEqual(typeof globalThis.Response, 'function');

common.expectWarning(
'ExperimentalWarning',
'The Fetch API is an experimental feature. This feature could change at any time'
);

const server = http.createServer((req, res) => {
// TODO: Remove this once keep-alive behavior can be disabled from the client
// side.
Expand Down