Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: refactor lazy loading of undici for fetch method #52275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 15 additions & 26 deletions lib/internal/bootstrap/web/exposed-window-or-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,21 @@ defineReplaceableLazyAttribute(globalThis, 'perf_hooks', ['performance']);
const { installObjectURLMethods } = require('internal/url');
installObjectURLMethods();

{
// https://fetch.spec.whatwg.org/#fetch-method
function set(value) {
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
writable: true,
value,
});
}
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
configurable: true,
enumerable: true,
set,
get() {
function fetch(input, init = undefined) {
// 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);
}
set(fetch);
return fetch;
},
});
}
let fetchImpl;
// https://fetch.spec.whatwg.org/#fetch-method
ObjectDefineProperty(globalThis, 'fetch', {
__proto__: null,
configurable: true,
enumerable: true,
writable: true,
value: function value(input, init = undefined) {
Copy link

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
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

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

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

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);
},
});

// https://xhr.spec.whatwg.org/#interface-formdata
// https://fetch.spec.whatwg.org/#headers-class
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fetch-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
require('../common');
const { mock, test } = require('node:test');
const assert = require('node:assert');

test('should correctly stub globalThis.fetch', async () => {
const customFetch = async (url) => {
return {
text: async () => 'foo',
};
};

mock.method(globalThis, 'fetch', customFetch);

const response = await globalThis.fetch('some-url');
const text = await response.text();

assert.strictEqual(text, 'foo');
MoLow marked this conversation as resolved.
Show resolved Hide resolved
mock.restoreAll();
});