Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lazy-load undici
  • Loading branch information
targos committed Feb 19, 2022
commit 7d96d08c47657cf799c5c639fb6395e0740b8de9
38 changes: 31 additions & 7 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
NumberParseInt,
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
SafeMap,
Expand Down Expand Up @@ -156,18 +157,41 @@ function setupFetch() {
return;
}

const undici = require('internal/deps/undici/undici');
let undici;
function lazyUndici() {
if (undici) {
return undici;
}

emitExperimentalWarning('The Fetch API');
undici = require('internal/deps/undici/undici');
Comment thread
targos marked this conversation as resolved.
Resolved
}

async function fetch(input, init = undefined) {
emitExperimentalWarning('Fetch');
return undici.fetch(input, init);
return lazyUndici().fetch(input, init);
}

defineOperation(globalThis, 'fetch', fetch);
exposeInterface(globalThis, 'FormData', undici.FormData);
exposeInterface(globalThis, 'Headers', undici.Headers);
exposeInterface(globalThis, 'Request', undici.Request);
exposeInterface(globalThis, 'Response', undici.Response);

function lazyInterface(name) {
return {
configurable: true,
enumerable: false,
get() {
return lazyUndici()[name];
},
set(value) {
exposeInterface(globalThis, name, value);
}
};
}

ObjectDefineProperties(globalThis, {
FormData: lazyInterface('FormData'),
Comment thread
targos marked this conversation as resolved.
Resolved
Headers: lazyInterface('Headers'),
Request: lazyInterface('Request'),
Response: lazyInterface('Response'),
});
}

// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ assert.strictEqual(typeof globalThis.Response, 'function');

common.expectWarning(
'ExperimentalWarning',
'Fetch is an experimental feature. This feature could change at any time'
'The Fetch API is an experimental feature. This feature could change at any time'
);

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