Skip to content

Commit 7d96d08

Browse files
committed
lazy-load undici
1 parent 92594d6 commit 7d96d08

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

lib/internal/bootstrap/pre_execution.js

+31-7
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,
@@ -156,18 +157,41 @@ function setupFetch() {
156157
return;
157158
}
158159

159-
const undici = require('internal/deps/undici/undici');
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+
}
160169

161170
async function fetch(input, init = undefined) {
162-
emitExperimentalWarning('Fetch');
163-
return undici.fetch(input, init);
171+
return lazyUndici().fetch(input, init);
164172
}
165173

166174
defineOperation(globalThis, 'fetch', fetch);
167-
exposeInterface(globalThis, 'FormData', undici.FormData);
168-
exposeInterface(globalThis, 'Headers', undici.Headers);
169-
exposeInterface(globalThis, 'Request', undici.Request);
170-
exposeInterface(globalThis, 'Response', undici.Response);
175+
176+
function lazyInterface(name) {
177+
return {
178+
configurable: true,
179+
enumerable: false,
180+
get() {
181+
return lazyUndici()[name];
182+
},
183+
set(value) {
184+
exposeInterface(globalThis, name, value);
185+
}
186+
};
187+
}
188+
189+
ObjectDefineProperties(globalThis, {
190+
FormData: lazyInterface('FormData'),
191+
Headers: lazyInterface('Headers'),
192+
Request: lazyInterface('Request'),
193+
Response: lazyInterface('Response'),
194+
});
171195
}
172196

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

test/parallel/test-fetch.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ assert.strictEqual(typeof globalThis.Response, 'function');
1212

1313
common.expectWarning(
1414
'ExperimentalWarning',
15-
'Fetch is an experimental feature. This feature could change at any time'
15+
'The Fetch API is an experimental feature. This feature could change at any time'
1616
);
1717

1818
const server = http.createServer((req, res) => {

0 commit comments

Comments
 (0)