Uh oh!
There was an error while loading. Please reload this page.
lib: refactor lazy loading of undici for fetch method - #52275
Conversation
nodejs-github-bot
commented
Mar 30, 2024
Review requested:
|
There was a problem hiding this comment.
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.
MoLow
commented
Mar 31, 2024
see #52015 |
1 similar comment
MoLow
commented
Mar 31, 2024
see #52015 |
mcollina
commented
Mar 31, 2024
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 |
There was a problem hiding this comment.
do we really need to lazily load it every time?
Can't we have something around the lines of:
letimpl;value: functionfetch(input,init=undefined){impl??=require('internal/deps/undici/undici');returnimpl(input,init)},There was a problem hiding this comment.
Technically require would cache it anyway, right?
There was a problem hiding this comment.
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)
mcollina
commented
Apr 5, 2024
What @atlowChemi wrote in #52275 (comment) |
There was a problem hiding this comment.
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
9ee922eCompare
atlowChemi
left a comment
There was a problem hiding this comment.
Other than lint errors, LGTM
Uh oh!
There was an error while loading. Please reload this page.
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
5c6a889CompareMoLow
commented
Apr 9, 2024
@YCChenVictor can you add a test that checks #52015 won't regress? |
b42ee5b to
2eddedaCompare
Ethan-Arrowood
left a comment
There was a problem hiding this comment.
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
7937b69Comparenodejs-github-bot
commented
Apr 11, 2024
nodejs-github-bot
commented
Apr 11, 2024
YCChenVictor
commented
Apr 12, 2024
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! |
nodejs-github-bot
commented
Apr 12, 2024
nodejs-github-bot
commented
Apr 12, 2024
nodejs-github-bot
commented
Apr 12, 2024
nodejs-github-bot
commented
Apr 12, 2024
joyeecheung
commented
Apr 12, 2024
The failures are most likely flakes. |
nodejs-github-bot
commented
Apr 12, 2024
nodejs-github-bot
commented
Apr 12, 2024
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 |
nodejs-github-bot
commented
Apr 12, 2024
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 <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
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 <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
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 <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
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