Skip to content

Commit 7f698f0

Browse files
joyeecheungtargos
authored andcommitted
lib: define FormData and fetch etc. in the built-in snapshot
Now that --experimental-fetch is true by default, define the dependent interfaces in the built-in snapshot and only delete them at run time when --no-experimental-fetch is set. PR-URL: #51598 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 8f63f6f commit 7f698f0

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

lib/internal/bootstrap/web/exposed-window-or-worker.js

+42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
const {
1212
globalThis,
13+
ObjectDefineProperty,
1314
} = primordials;
1415

1516
const {
@@ -55,3 +56,44 @@ defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
5556
// https://w3c.github.io/FileAPI/#creating-revoking
5657
const { installObjectURLMethods } = require('internal/url');
5758
installObjectURLMethods();
59+
60+
{
61+
// https://fetch.spec.whatwg.org/#fetch-method
62+
function set(value) {
63+
ObjectDefineProperty(globalThis, 'fetch', {
64+
__proto__: null,
65+
writable: true,
66+
value,
67+
});
68+
}
69+
ObjectDefineProperty(globalThis, 'fetch', {
70+
__proto__: null,
71+
configurable: true,
72+
enumerable: true,
73+
set,
74+
get() {
75+
function fetch(input, init = undefined) {
76+
// Loading undici alone lead to promises which breaks lots of tests so we
77+
// have to load it really lazily for now.
78+
const { fetch: impl } = require('internal/deps/undici/undici');
79+
return impl(input, init);
80+
}
81+
set(fetch);
82+
return fetch;
83+
},
84+
});
85+
}
86+
87+
// https://xhr.spec.whatwg.org/#interface-formdata
88+
// https://fetch.spec.whatwg.org/#headers-class
89+
// https://fetch.spec.whatwg.org/#request-class
90+
// https://fetch.spec.whatwg.org/#response-class
91+
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
92+
'FormData', 'Headers', 'Request', 'Response',
93+
]);
94+
95+
// The WebAssembly Web API which relies on Response.
96+
// https:// webassembly.github.io/spec/web-api/#streaming-modules
97+
internalBinding('wasm_web_api').setImplementation((streamState, source) => {
98+
require('internal/wasm_web_api').wasmStreamingCallback(streamState, source);
99+
});

lib/internal/process/pre_execution.js

+8-52
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
DatePrototypeGetMonth,
1111
DatePrototypeGetSeconds,
1212
NumberParseInt,
13-
ObjectDefineProperties,
1413
ObjectDefineProperty,
1514
ObjectFreeze,
1615
ObjectGetOwnPropertyDescriptor,
@@ -30,7 +29,6 @@ const {
3029
} = require('internal/options');
3130
const { reconnectZeroFillToggle } = require('internal/buffer');
3231
const {
33-
defineOperation,
3432
exposeInterface,
3533
exposeLazyInterfaces,
3634
defineReplaceableLazyAttribute,
@@ -302,58 +300,16 @@ function setupWarningHandler() {
302300
// https://fetch.spec.whatwg.org/
303301
// https://websockets.spec.whatwg.org/
304302
function setupUndici() {
305-
if (getEmbedderOptions().noBrowserGlobals) {
306-
return;
307-
}
308-
309-
let undici;
310-
function lazyUndici() {
311-
if (undici) {
312-
return undici;
313-
}
314-
315-
undici = require('internal/deps/undici/undici');
316-
return undici;
317-
}
318-
319-
function lazyInterface(name) {
320-
return {
321-
configurable: true,
322-
enumerable: false,
323-
get() {
324-
return lazyUndici()[name];
325-
},
326-
set(value) {
327-
exposeInterface(globalThis, name, value);
328-
},
329-
};
303+
if (getOptionValue('--no-experimental-fetch')) {
304+
delete globalThis.fetch;
305+
delete globalThis.FormData;
306+
delete globalThis.Headers;
307+
delete globalThis.Request;
308+
delete globalThis.Response;
330309
}
331310

332-
if (!getOptionValue('--no-experimental-fetch')) {
333-
// Fetch is meant to return a Promise, but not be async.
334-
function fetch(input, init = undefined) {
335-
return lazyUndici().fetch(input, init);
336-
}
337-
338-
defineOperation(globalThis, 'fetch', fetch);
339-
340-
ObjectDefineProperties(globalThis, {
341-
FormData: lazyInterface('FormData'),
342-
Headers: lazyInterface('Headers'),
343-
Request: lazyInterface('Request'),
344-
Response: lazyInterface('Response'),
345-
});
346-
347-
// The WebAssembly Web API: https://webassembly.github.io/spec/web-api
348-
internalBinding('wasm_web_api').setImplementation((streamState, source) => {
349-
require('internal/wasm_web_api').wasmStreamingCallback(streamState, source);
350-
});
351-
}
352-
353-
if (getOptionValue('--experimental-websocket')) {
354-
ObjectDefineProperties(globalThis, {
355-
WebSocket: lazyInterface('WebSocket'),
356-
});
311+
if (!getEmbedderOptions().noBrowserGlobals && getOptionValue('--experimental-websocket')) {
312+
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', ['WebSocket']);
357313
}
358314
}
359315

test/parallel/test-bootstrap-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ expected.beforePreExec = new Set([
9898
'NativeModule internal/modules/package_json_reader',
9999
'Internal Binding module_wrap',
100100
'NativeModule internal/modules/cjs/loader',
101+
'Internal Binding wasm_web_api',
101102
]);
102103

103104
expected.atRunTime = new Set([
104-
'Internal Binding wasm_web_api',
105105
'Internal Binding worker',
106106
'NativeModule internal/modules/run_main',
107107
'NativeModule internal/net',

0 commit comments

Comments
 (0)