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 <[email protected]> Reviewed-By: Matthew Aitken <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
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‎

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+
functionset(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+
functionfetch(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+
returnimpl(input,init);
80+
}
81+
set(fetch);
82+
returnfetch;
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
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
functionsetupUndici(){
305-
if(getEmbedderOptions().noBrowserGlobals){
306-
return;
307-
}
308-
309-
letundici;
310-
functionlazyUndici(){
311-
if(undici){
312-
returnundici;
313-
}
314-
315-
undici=require('internal/deps/undici/undici');
316-
returnundici;
317-
}
318-
319-
functionlazyInterface(name){
320-
return{
321-
configurable: true,
322-
enumerable: false,
323-
get(){
324-
returnlazyUndici()[name];
325-
},
326-
set(value){
327-
exposeInterface(globalThis,name,value);
328-
},
329-
};
303+
if(getOptionValue('--no-experimental-fetch')){
304+
deleteglobalThis.fetch;
305+
deleteglobalThis.FormData;
306+
deleteglobalThis.Headers;
307+
deleteglobalThis.Request;
308+
deleteglobalThis.Response;
330309
}
331310

332-
if(!getOptionValue('--no-experimental-fetch')){
333-
// Fetch is meant to return a Promise, but not be async.
334-
functionfetch(input,init=undefined){
335-
returnlazyUndici().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‎

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=newSet([
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)