Skip to content
Merged
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
lib: implement lazy loading for fetch function
Instead of importing undici upfront, the module is now conditionally
required using require only when the fetch function is called for the
first time and the undici implementation is not already available. This
lazy loading approach improves resource usage and test reliability by
loading undici only when needed.
  • Loading branch information
YCChenVictor committed Apr 9, 2024
commit 5c6a8893db5541d53cb9c05fbc2324fb9ec1e5ca
12 changes: 7 additions & 5 deletions lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
const { installObjectURLMethods } = require('internal/url');
installObjectURLMethods();

let fetchImpl;
// https://fetch.spec.whatwg.org/#fetch-method
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
configurable: true,
enumerable: true,
writable: true,
value: function fetch(input, init = undefined) { // eslint-disable-line func-name-matching
// Loading undici alone lead to promises which breaks lots of tests so we
// have to load it really lazily for now.
const { fetch: impl } = require('internal/deps/undici/undici');
return impl(input, init);
value: function value(input, init = undefined) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't init = undefined redundant here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's to specify that the init argument is optional.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but you're defaulting it to undefined -.which would be the case if it wasn't defined, it's redundant

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for https://github.com/nodejs/node/blob/main/deps/undici/undici.js#L12497

It makes no sense to default an argument value to undefined ever

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It affects the .length of the function, which is observable. Parameters with default values don't count towards the length. And since the correct length is 1, not 2, this is the only way to do that without either explicitly setting the length attribute or referencing arguments.

if (!fetchImpl) { // Implement lazy loading of undici module for fetch function
const undiciModule = require('internal/deps/undici/undici');
fetchImpl = undiciModule.fetch;
}
return fetchImpl(input, init);
},
});

Expand Down