Skip to content

Commit 357b012

Browse files
joyeecheungrdw-msft
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: nodejs#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 7dfcdde commit 357b012

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

Lines changed: 42 additions & 0 deletions
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

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

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

test/parallel/test-bootstrap-modules.js

Lines changed: 1 addition & 1 deletion
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)