-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
lib: refactor lazy loading of undici for fetch method #52275
Conversation
Review requested:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this setup slows down every invocation of fetch()
by adding an additional function call and the require logic. The previous logic added lazy loading that should be kept in.
Could you add a test? I don't understand what would not be possible in the previous setup.
see #52015 |
1 similar comment
see #52015 |
The test should be added and the lazyloading kept in. |
I think this configuration is faster on the first call (there's no need to replace console.log("BEFORE");
console.log(Object.getOwnPropertyDescriptor(globalThis, "fetch"));
console.log(Object.getOwnPropertyDescriptor(globalThis, "fetch").set.toString());
console.log(Object.getOwnPropertyDescriptor(globalThis, "fetch").get.toString());
fetch;
console.log("AFTER");
console.log(Object.getOwnPropertyDescriptor(globalThis, "fetch"));
console.log(Object.getOwnPropertyDescriptor(globalThis, "fetch").value.toString()); When I run the above code in Node.js v21.7.0:
With this pull request, the Lazyloading is kept, as undici is only imported when |
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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really need to lazily load it every time?
Can't we have something around the lines of:
let impl;
value: function fetch(input, init = undefined) {
impl ??= require('internal/deps/undici/undici');
return impl(input, init)
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically require would cache it anyway, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, technically it will, but the cost for calling require would still be higher (I don't have benchmarks, but it has more overhead than holding a local variable in memory)
What @atlowChemi wrote in #52275 (comment) |
@@ -59,27 +59,16 @@ installObjectURLMethods(); | |||
|
|||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the curly bracket is no longer necessary. It was probably used to avoid creating the set
function in the root scope.
Object.defineProperty is updated to lazily load the undici dependency for the fetch method. This change allows for simpler and more reliable mocking of the fetch method for testing purposes, resolving issues encountered with premature method invocation during testing. Fixes: nodejs#52015
796a413
to
9ee922e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than lint errors, LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
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.
9ee922e
to
5c6a889
Compare
@YCChenVictor can you add a test that checks #52015 won't regress? |
b42ee5b
to
2eddeda
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this work with the following lines (just below your contribution?):
exposeLazyInterfaces(globalThis, 'internal/deps/undici/undici', [
'FormData', 'Headers', 'Request', 'Response',
]);
If I use any of those interfaces, should fetch
be loaded as well?
2eddeda
to
7937b69
Compare
I'm uncertain why the checks failed. Perhaps I should rebase this branch on the latest node commit? Any guidance would be greatly appreciated, and I'm sincerely grateful for any corrections or suggestions. Thank you! |
The failures are most likely flakes. |
Commit Queue failed- Loading data for nodejs/node/pull/52275 ✔ Done loading data for nodejs/node/pull/52275 ----------------------------------- PR info ------------------------------------ Title lib: refactor lazy loading of undici for fetch method (#52275) Author Victor Chen (@YCChenVictor, first-time contributor) Branch YCChenVictor:feature/lazy-fetch-undici-loading -> nodejs:main Labels lib / src, needs-ci, commit-queue-squash Commits 3 - lib: refactor lazy loading of undici for fetch method - lib: implement lazy loading for fetch function - lib: test for fetch mock without prior invocation Committers 1 - YCChen PR-URL: https://github.com/nodejs/node/pull/52275 Fixes: https://github.com/nodejs/node/issues/52015 Reviewed-By: Moshe Atlow Reviewed-By: Matteo Collina Reviewed-By: Chemi Atlow Reviewed-By: James M Snell ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/52275 Fixes: https://github.com/nodejs/node/issues/52015 Reviewed-By: Moshe Atlow Reviewed-By: Matteo Collina Reviewed-By: Chemi Atlow Reviewed-By: James M Snell -------------------------------------------------------------------------------- ⚠ Commits were pushed since the last approving review: ⚠ - lib: test for fetch mock without prior invocation ℹ This PR was created on Sat, 30 Mar 2024 14:26:41 GMT ✔ Approvals: 4 ✔ - Moshe Atlow (@MoLow) (TSC): https://github.com/nodejs/node/pull/52275#pullrequestreview-1990083304 ✔ - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/52275#pullrequestreview-1985970496 ✔ - Chemi Atlow (@atlowChemi): https://github.com/nodejs/node/pull/52275#pullrequestreview-1988005649 ✔ - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/52275#pullrequestreview-1992479891 ✔ Last GitHub CI successful ℹ Last Full PR CI on 2024-04-12T14:12:32Z: https://ci.nodejs.org/job/node-test-pull-request/58323/ - Querying data for job/node-test-pull-request/58323/ ✔ Last Jenkins CI successful -------------------------------------------------------------------------------- ✔ Aborted `git node land` session in /home/runner/work/node/node/.ncuhttps://github.com/nodejs/node/actions/runs/8665947485 |
Landed in 2cd3073 |
Object.defineProperty is updated to lazily load the undici dependency for the fetch method. This change allows for simpler and more reliable mocking of the fetch method for testing purposes, resolving issues encountered with premature method invocation during testing. Fixes: #52015 PR-URL: #52275 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Object.defineProperty is updated to lazily load the undici dependency for the fetch method. This change allows for simpler and more reliable mocking of the fetch method for testing purposes, resolving issues encountered with premature method invocation during testing. Fixes: #52015 PR-URL: #52275 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Object.defineProperty is updated to lazily load the undici dependency for the fetch method. This change allows for simpler and more reliable mocking of the fetch method for testing purposes, resolving issues encountered with premature method invocation during testing. Fixes: #52015 PR-URL: #52275 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Object.defineProperty is updated to lazily load the undici dependency for the fetch method. This change allows for simpler and more reliable mocking of the fetch method for testing purposes, resolving issues encountered with premature method invocation during testing.
Fixes: #52015