Skip to content

Commit 672967a

Browse files
committed
lib: enable fetch by default
1 parent f069793 commit 672967a

File tree

8 files changed

+43
-26
lines changed

8 files changed

+43
-26
lines changed

doc/api/cli.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,6 @@ effort to report stack traces relative to the original source file.
280280
Overriding `Error.prepareStackTrace` prevents `--enable-source-maps` from
281281
modifying the stack trace.
282282

283-
### `--experimental-fetch`
284-
285-
<!-- YAML
286-
added: REPLACEME
287-
-->
288-
289-
Enable experimental support for the [Fetch API][].
290-
291283
### `--experimental-import-meta-resolve`
292284

293285
<!-- YAML
@@ -315,6 +307,14 @@ added: v11.8.0
315307

316308
Use the specified file as a security policy.
317309

310+
### `--no-experimental-fetch`
311+
312+
<!-- YAML
313+
added: REPLACEME
314+
-->
315+
316+
Disable experimental support for the [Fetch API][].
317+
318318
### `--no-experimental-repl-await`
319319

320320
<!-- YAML
@@ -1569,7 +1569,6 @@ Node.js options that are allowed are:
15691569
* `--enable-fips`
15701570
* `--enable-source-maps`
15711571
* `--experimental-abortcontroller`
1572-
* `--experimental-fetch`
15731572
* `--experimental-import-meta-resolve`
15741573
* `--experimental-json-modules`
15751574
* `--experimental-loader`
@@ -1597,6 +1596,7 @@ Node.js options that are allowed are:
15971596
* `--napi-modules`
15981597
* `--no-addons`
15991598
* `--no-deprecation`
1599+
* `--no-experimental-fetch`
16001600
* `--no-experimental-repl-await`
16011601
* `--no-extra-info-on-fatal-exception`
16021602
* `--no-force-async-hooks-checks`

doc/api/globals.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ This variable may appear to be global but is not. See [`exports`][].
339339
added: REPLACEME
340340
-->
341341

342-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
342+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
343343
> CLI flag.
344344
345345
A browser-compatible implementation of the [`fetch()`][] function.
@@ -365,7 +365,7 @@ Node.js this is different. The top-level scope is not the global scope;
365365
added: REPLACEME
366366
-->
367367

368-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
368+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
369369
> CLI flag.
370370
371371
A browser-compatible implementation of {Headers}.
@@ -470,7 +470,7 @@ This variable may appear to be global but is not. See [`require()`][].
470470
added: REPLACEME
471471
-->
472472

473-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
473+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
474474
> CLI flag.
475475
476476
A browser-compatible implementation of {Response}.
@@ -481,7 +481,7 @@ A browser-compatible implementation of {Response}.
481481
added: REPLACEME
482482
-->
483483

484-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
484+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
485485
> CLI flag.
486486
487487
A browser-compatible implementation of {Request}.
@@ -590,7 +590,7 @@ The object that acts as the namespace for all W3C
590590
[WebAssembly][webassembly-org] related functionality. See the
591591
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
592592

593-
[`--experimental-fetch`]: cli.md#--experimental-fetch
593+
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
594594
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
595595
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
596596
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api

doc/node.1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ Requires Node.js to be built with
139139
.It Fl -enable-source-maps
140140
Enable Source Map V3 support for stack traces.
141141
.
142-
.It Fl -experimental-fetch
143-
Enable experimental support for the Fetch API.
144-
.
145142
.It Fl -experimental-import-meta-resolve
146143
Enable experimental ES modules support for import.meta.resolve().
147144
.
@@ -153,6 +150,9 @@ to use as a custom module loader.
153150
.It Fl -experimental-policy
154151
Use the specified file as a security policy.
155152
.
153+
.It Fl -no-experimental-fetch
154+
Disable experimental support for the Fetch API.
155+
.
156156
.It Fl -no-experimental-repl-await
157157
Disable top-level await keyword support in REPL.
158158
.

lib/internal/bootstrap/pre_execution.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,18 @@ function setupWarningHandler() {
147147

148148
// https://fetch.spec.whatwg.org/
149149
function setupFetch() {
150-
if (!getOptionValue('--experimental-fetch')) {
150+
if (getOptionValue('--no-experimental-fetch')) {
151151
return;
152152
}
153153

154-
emitExperimentalWarning('Fetch');
155-
156154
const undici = require('internal/deps/undici/undici');
157-
defineOperation(globalThis, 'fetch', undici.fetch);
155+
156+
async function fetch(input, init = undefined) {
157+
emitExperimentalWarning('Fetch');
158+
return undici.fetch(input, init);
159+
}
160+
161+
defineOperation(globalThis, 'fetch', fetch);
158162
exposeInterface(globalThis, 'Headers', undici.Headers);
159163
exposeInterface(globalThis, 'Request', undici.Request);
160164
exposeInterface(globalThis, 'Response', undici.Response);

src/node_options.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
318318
AddOption("--experimental-fetch",
319319
"experimental Fetch API",
320320
&EnvironmentOptions::experimental_fetch,
321-
kAllowedInEnvironment);
321+
kAllowedInEnvironment,
322+
true);
322323
AddOption("--experimental-json-modules", "", NoOp{}, kAllowedInEnvironment);
323324
AddOption("--experimental-loader",
324325
"use the specified module as a custom loader",

src/node_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class EnvironmentOptions : public Options {
107107
std::vector<std::string> conditions;
108108
std::string dns_result_order;
109109
bool enable_source_maps = false;
110-
bool experimental_fetch = false;
110+
bool experimental_fetch = true;
111111
std::string experimental_specifier_resolution;
112112
bool experimental_wasm_modules = false;
113113
bool experimental_import_meta_resolve = false;

test/parallel/test-fetch-disabled.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Flags: --no-experimental-fetch
2+
import '../common/index.mjs';
3+
4+
import assert from 'assert';
5+
6+
assert.strictEqual(typeof globalThis.fetch, 'undefined');
7+
assert.strictEqual(typeof globalThis.Headers, 'undefined');
8+
assert.strictEqual(typeof globalThis.Request, 'undefined');
9+
assert.strictEqual(typeof globalThis.Response, 'undefined');

test/parallel/test-fetch.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Flags: --experimental-fetch --no-warnings
2-
3-
import '../common/index.mjs';
1+
import * as common from '../common/index.mjs';
42

53
import assert from 'assert';
64
import events from 'events';
@@ -11,6 +9,11 @@ assert.strictEqual(typeof globalThis.Headers, 'function');
119
assert.strictEqual(typeof globalThis.Request, 'function');
1210
assert.strictEqual(typeof globalThis.Response, 'function');
1311

12+
common.expectWarning(
13+
'ExperimentalWarning',
14+
'Fetch is an experimental feature. This feature could change at any time'
15+
);
16+
1417
const server = http.createServer((req, res) => {
1518
// TODO: Remove this once keep-alive behavior can be disabled from the client
1619
// side.

0 commit comments

Comments
 (0)