-
Notifications
You must be signed in to change notification settings - Fork 27.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
feat(experimental): option to polyfill fetch
using undici
in Node.js <18
#40318
Merged
ijjk
merged 61 commits into
vercel:canary
from
Ethan-Arrowood:feature/polyfill-undici-fetch
Sep 27, 2022
Merged
Changes from all commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
66408d8
polyfill undici
28a09b8
move polyfill to inside !global.fetch
0daf850
remove nodejs type update
1c5c027
update types and compile undici
f4169d7
try using global.__NEXT_USE_UNDICI and add tests
0b2475a
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
07be524
remove auto-generated files
32d3677
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
4cbc65f
Add ?. to useUndici
eb44e6b
Fix global config resolution
7fd115a
Fix global properties and add more tests
2a962a4
Update function name
6593733
remove unused code
a11d117
require fetch implementation conditionally
balazsorban44 ebc33d7
update lock file
balazsorban44 f623f65
add `keepAlive` tests for `undici`
balazsorban44 502db7f
handle `keepAlive` with `undici`
balazsorban44 88c65cd
Merge branch 'canary' into feature/polyfill-undici-fetch
balazsorban44 1651916
update compiled
balazsorban44 8393aaf
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
balazsorban44 76d7b6c
Merge branch 'canary' into feature/polyfill-undici-fetch
367862f
add back `mini-css-extract-plugin` compiled
balazsorban44 3866700
revert compiled
balazsorban44 58ecb4c
Merge branch 'canary' into feature/polyfill-undici-fetch
balazsorban44 454d917
revert webpack compiled changes
balazsorban44 1b26d54
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
balazsorban44 0de1fb4
revert
balazsorban44 ec21630
revert
balazsorban44 e8550fa
revert?
balazsorban44 7e6b1f9
more revert
balazsorban44 4919546
one more
balazsorban44 ef8b44f
update taskfile
balazsorban44 789b43b
update compiled
balazsorban44 245ff7b
Add version guard for <16.8.0
60d99d7
fix lock file
0de3c26
Update packages/next/server/config.ts
Ethan-Arrowood 989f9c8
fix semver usage
ed03b2b
useUndici -> enableUndici
421891b
Merge branch 'canary' into feature/polyfill-undici-fetch
acf39cb
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
203c3e7
Merge branch 'feature/polyfill-undici-fetch' of github.com:Ethan-Arro…
bbc2ac5
fix lock file
cbafb7f
Merge branch 'canary' into feature/polyfill-undici-fetch
d2087ce
Merge branch 'canary' into feature/polyfill-undici-fetch
ijjk f770bd5
update lock
ijjk 31beccc
update compiled
ijjk 7722ea5
Update packages/next/server/node-polyfill-fetch.js
Ethan-Arrowood 424531a
add enableUndici to worker exportPage
7da8b81
Merge branch 'canary' of github.com:vercel/next.js into feature/polyf…
72c90fe
add missing argument in exportPage and revert compiled file changes
e5f3c60
remove compiled sass-loader file
fbc3e4d
Merge branch 'canary' into feature/polyfill-undici-fetch
fcfa989
update lock file
balazsorban44 08fefca
Merge branch 'canary' into feature/polyfill-undici-fetch
balazsorban44 b101ec6
update compiled
balazsorban44 82a0961
fix test
balazsorban44 ef18f28
tweak `enableUndici` warnings
balazsorban44 05ca18f
pass `enableUndici` to worker
balazsorban44 519c717
tweak test
balazsorban44 6002dba
Merge branch 'canary' into feature/polyfill-undici-fetch
ijjk 8ed03ee
Merge branch 'canary' into feature/polyfill-undici-fetch
ijjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Matteo Collina and Undici contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"undici","main":"index.js","author":"Matteo Collina <hello@matteocollina.com>","license":"MIT"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,56 @@ | ||
import fetch, { | ||
Headers, | ||
Request, | ||
Response, | ||
} from 'next/dist/compiled/node-fetch' | ||
|
||
// Polyfill fetch() in the Node.js environment | ||
|
||
if (!global.fetch) { | ||
const agent = ({ protocol }) => | ||
protocol === 'http:' ? global.__NEXT_HTTP_AGENT : global.__NEXT_HTTPS_AGENT | ||
const fetchWithAgent = (url, opts, ...rest) => { | ||
if (!opts) { | ||
opts = { agent } | ||
} else if (!opts.agent) { | ||
opts.agent = agent | ||
function getFetchImpl() { | ||
return global.__NEXT_USE_UNDICI | ||
? require('next/dist/compiled/undici') | ||
: require('next/dist/compiled/node-fetch') | ||
} | ||
// Due to limitation of global configuration, we have to do this resolution at runtime | ||
global.fetch = (...args) => { | ||
const fetchImpl = getFetchImpl() | ||
|
||
if (global.__NEXT_USE_UNDICI) { | ||
// Undici does not support the `keepAlive` option, | ||
// instead we have to pass a custom dispatcher | ||
if ( | ||
!global.__NEXT_HTTP_AGENT_OPTIONS?.keepAlive && | ||
!global.__NEXT_UNDICI_AGENT_SET | ||
) { | ||
global.__NEXT_UNDICI_AGENT_SET = true | ||
fetchImpl.setGlobalDispatcher(new fetchImpl.Agent({ pipelining: 0 })) | ||
} | ||
return fetchImpl.fetch(...args) | ||
} | ||
return fetch(url, opts, ...rest) | ||
const agent = ({ protocol }) => | ||
protocol === 'http:' | ||
? global.__NEXT_HTTP_AGENT | ||
: global.__NEXT_HTTPS_AGENT | ||
|
||
if (!args[1]) { | ||
args[1] = { agent } | ||
} else if (!args[1].agent) { | ||
args[1].agent = agent | ||
} | ||
|
||
return fetchImpl(...args) | ||
} | ||
global.fetch = fetchWithAgent | ||
global.Headers = Headers | ||
global.Request = Request | ||
global.Response = Response | ||
|
||
Object.defineProperties(global, { | ||
Headers: { | ||
get() { | ||
return getFetchImpl().Headers | ||
}, | ||
}, | ||
Request: { | ||
get() { | ||
return getFetchImpl().Request | ||
}, | ||
}, | ||
Response: { | ||
get() { | ||
return getFetchImpl().Response | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This could be a problem because the agent is created for every
fetch()
. It should be reused, 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.
No, the Agent is created and assigned to
global.__NEXT_HTTP_AGENT
orglobal.__NEXT_HTTPS_AGENT
. This function just returns the correct agent every time fetch is called.But since we can't do that for the Undici agent we have to have all that
!global.__NEXT_UNDICI_AGENT_SET
logic.