Skip to content

Commit 4944ad0

Browse files
authored
lib: enable fetch by default
PR-URL: #41811 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent df0e665 commit 4944ad0

File tree

10 files changed

+67
-38
lines changed

10 files changed

+67
-38
lines changed

doc/api/cli.md

+9-9
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: v17.5.0
287-
-->
288-
289-
Enable experimental support for the [Fetch API][].
290-
291283
### `--experimental-global-webcrypto`
292284

293285
<!-- YAML
@@ -333,6 +325,14 @@ added: v11.8.0
333325

334326
Use the specified file as a security policy.
335327

328+
### `--no-experimental-fetch`
329+
330+
<!-- YAML
331+
added: REPLACEME
332+
-->
333+
334+
Disable experimental support for the [Fetch API][].
335+
336336
### `--no-experimental-repl-await`
337337

338338
<!-- YAML
@@ -1587,7 +1587,6 @@ Node.js options that are allowed are:
15871587
* `--enable-fips`
15881588
* `--enable-source-maps`
15891589
* `--experimental-abortcontroller`
1590-
* `--experimental-fetch`
15911590
* `--experimental-global-webcrypto`
15921591
* `--experimental-import-meta-resolve`
15931592
* `--experimental-json-modules`
@@ -1617,6 +1616,7 @@ Node.js options that are allowed are:
16171616
* `--napi-modules`
16181617
* `--no-addons`
16191618
* `--no-deprecation`
1619+
* `--no-experimental-fetch`
16201620
* `--no-experimental-repl-await`
16211621
* `--no-extra-info-on-fatal-exception`
16221622
* `--no-force-async-hooks-checks`

doc/api/globals.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ This variable may appear to be global but is not. See [`exports`][].
384384
added: v17.5.0
385385
-->
386386

387-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
387+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
388388
> CLI flag.
389389
390390
A browser-compatible implementation of the [`fetch()`][] function.
@@ -395,7 +395,7 @@ A browser-compatible implementation of the [`fetch()`][] function.
395395
added: REPLACEME
396396
-->
397397

398-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
398+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
399399
> CLI flag.
400400
401401
A browser-compatible implementation of {FormData}.
@@ -421,7 +421,7 @@ Node.js this is different. The top-level scope is not the global scope;
421421
added: v17.5.0
422422
-->
423423

424-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
424+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
425425
> CLI flag.
426426
427427
A browser-compatible implementation of {Headers}.
@@ -526,7 +526,7 @@ This variable may appear to be global but is not. See [`require()`][].
526526
added: v17.5.0
527527
-->
528528

529-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
529+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
530530
> CLI flag.
531531
532532
A browser-compatible implementation of {Response}.
@@ -537,7 +537,7 @@ A browser-compatible implementation of {Response}.
537537
added: v17.5.0
538538
-->
539539

540-
> Stability: 1 - Experimental. Enable this API with the [`--experimental-fetch`][]
540+
> Stability: 1 - Experimental. Disable this API with the [`--no-experimental-fetch`][]
541541
> CLI flag.
542542
543543
A browser-compatible implementation of {Request}.
@@ -660,8 +660,8 @@ The object that acts as the namespace for all W3C
660660
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
661661

662662
[Web Crypto API]: webcrypto.md
663-
[`--experimental-fetch`]: cli.md#--experimental-fetch
664663
[`--experimental-global-webcrypto`]: cli.md#--experimental-global-webcrypto
664+
[`--no-experimental-fetch`]: cli.md#--no-experimental-fetch
665665
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
666666
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
667667
[`EventTarget` and `Event` API]: events.md#eventtarget-and-event-api

doc/node.1

+3-3
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-global-webcrypto
146143
Expose the Web Crypto API on the global scope.
147144
.
@@ -159,6 +156,9 @@ Enable experimental support for loading modules using `import` over `https:`.
159156
.It Fl -experimental-policy
160157
Use the specified file as a security policy.
161158
.
159+
.It Fl -no-experimental-fetch
160+
Disable experimental support for the Fetch API.
161+
.
162162
.It Fl -no-experimental-repl-await
163163
Disable top-level await keyword support in REPL.
164164
.

lib/internal/bootstrap/pre_execution.js

+37-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
NumberParseInt,
5+
ObjectDefineProperties,
56
ObjectDefineProperty,
67
ObjectGetOwnPropertyDescriptor,
78
SafeMap,
@@ -152,18 +153,46 @@ function setupWarningHandler() {
152153
// https://fetch.spec.whatwg.org/
153154
function setupFetch() {
154155
if (process.config.variables.node_no_browser_globals ||
155-
!getOptionValue('--experimental-fetch')) {
156+
getOptionValue('--no-experimental-fetch')) {
156157
return;
157158
}
158159

159-
emitExperimentalWarning('Fetch');
160+
let undici;
161+
function lazyUndici() {
162+
if (undici) {
163+
return undici;
164+
}
165+
166+
emitExperimentalWarning('The Fetch API');
167+
undici = require('internal/deps/undici/undici');
168+
return undici;
169+
}
170+
171+
async function fetch(input, init = undefined) {
172+
return lazyUndici().fetch(input, init);
173+
}
174+
175+
defineOperation(globalThis, 'fetch', fetch);
160176

161-
const undici = require('internal/deps/undici/undici');
162-
defineOperation(globalThis, 'fetch', undici.fetch);
163-
exposeInterface(globalThis, 'FormData', undici.FormData);
164-
exposeInterface(globalThis, 'Headers', undici.Headers);
165-
exposeInterface(globalThis, 'Request', undici.Request);
166-
exposeInterface(globalThis, 'Response', undici.Response);
177+
function lazyInterface(name) {
178+
return {
179+
configurable: true,
180+
enumerable: false,
181+
get() {
182+
return lazyUndici()[name];
183+
},
184+
set(value) {
185+
exposeInterface(globalThis, name, value);
186+
}
187+
};
188+
}
189+
190+
ObjectDefineProperties(globalThis, {
191+
FormData: lazyInterface('FormData'),
192+
Headers: lazyInterface('Headers'),
193+
Request: lazyInterface('Request'),
194+
Response: lazyInterface('Response'),
195+
});
167196
}
168197

169198
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is

src/node_options.cc

+2-1
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-global-webcrypto",
323324
"expose experimental Web Crypto API on the global scope",
324325
&EnvironmentOptions::experimental_global_web_crypto,

src/node_options.h

+1-1
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
bool experimental_global_web_crypto = false;
112112
bool experimental_https_modules = false;
113113
std::string experimental_specifier_resolution;

test/common/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,7 @@ if (global.structuredClone) {
301301
}
302302

303303
if (global.fetch) {
304-
knownGlobals.push(
305-
global.fetch,
306-
global.FormData,
307-
global.Request,
308-
global.Response,
309-
global.Headers,
310-
);
304+
knownGlobals.push(fetch);
311305
}
312306
if (hasCrypto && global.crypto) {
313307
knownGlobals.push(global.crypto);

test/parallel/test-fetch-disabled.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Flags: --no-experimental-fetch
12
import '../common/index.mjs';
23

34
import assert from 'assert';

test/parallel/test-fetch.mjs

+6-3
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';
@@ -12,6 +10,11 @@ assert.strictEqual(typeof globalThis.Headers, 'function');
1210
assert.strictEqual(typeof globalThis.Request, 'function');
1311
assert.strictEqual(typeof globalThis.Response, 'function');
1412

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

test/parallel/test-global.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ builtinModules.forEach((moduleName) => {
5454
'setInterval',
5555
'setTimeout',
5656
'structuredClone',
57+
'fetch',
5758
];
5859
assert.deepStrictEqual(new Set(Object.keys(global)), new Set(expected));
5960
}

0 commit comments

Comments
 (0)