Skip to content

Commit bd2a3c1

Browse files
joyeecheungrichardlau
authored andcommittedMar 25, 2024
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 <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent 0fb079b commit bd2a3c1

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,
@@ -301,58 +299,16 @@ function setupWarningHandler() {
301299
// https://fetch.spec.whatwg.org/
302300
// https://websockets.spec.whatwg.org/
303301
function setupUndici() {
304-
if (getEmbedderOptions().noBrowserGlobals) {
305-
return;
306-
}
307-
308-
let undici;
309-
function lazyUndici() {
310-
if (undici) {
311-
return undici;
312-
}
313-
314-
undici = require('internal/deps/undici/undici');
315-
return undici;
316-
}
317-
318-
function lazyInterface(name) {
319-
return {
320-
configurable: true,
321-
enumerable: false,
322-
get() {
323-
return lazyUndici()[name];
324-
},
325-
set(value) {
326-
exposeInterface(globalThis, name, value);
327-
},
328-
};
302+
if (getOptionValue('--no-experimental-fetch')) {
303+
delete globalThis.fetch;
304+
delete globalThis.FormData;
305+
delete globalThis.Headers;
306+
delete globalThis.Request;
307+
delete globalThis.Response;
329308
}
330309

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

‎test/parallel/test-bootstrap-modules.js

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

102103
expected.atRunTime = new Set([
103-
'Internal Binding wasm_web_api',
104104
'Internal Binding worker',
105105
'NativeModule internal/modules/run_main',
106106
'NativeModule internal/net',

0 commit comments

Comments
 (0)
Please sign in to comment.