-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add warning to
fetch()
calls that will change the requested port
In Workers published to the Edge (rather than previews) there is a bug where a custom port on a downstream fetch request is ignored, defaulting to the standard port. For example, `https://my.example.com:668` will actually send the request to `https://my.example.com:443`. This does not happen when using `wrangler dev` (both in remote and local mode), but to ensure that developers are aware of it this change displays a runtime warning in the console when the bug is hit. Closes #1320
- Loading branch information
1 parent
2d93604
commit 65ad2fc
Showing
5 changed files
with
41 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
fix: add warning to `fetch()` calls that will change the requested port | ||
|
||
In Workers published to the Edge (rather than previews) there is a bug where a custom port on a downstream fetch request is ignored, defaulting to the standard port. | ||
For example, `https://my.example.com:668` will actually send the request to `https://my.example.com:443`. | ||
|
||
This does not happen when using `wrangler dev` (both in remote and local mode), but to ensure that developers are aware of it this change displays a runtime warning in the console when the bug is hit. | ||
|
||
Closes #1320 |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
globalThis.fetch = (() => { | ||
const originalFetch = globalThis.fetch; | ||
return (request, init) => { | ||
const url = new URL( | ||
(typeof request === "string" ? new Request(request, init) : request).url | ||
); | ||
if ( | ||
url.port !== undefined && | ||
url.protocol === "https" && | ||
url.port != "443" | ||
) { | ||
const actualUrl = new URL(url.toString()); | ||
actualUrl.port = "443"; | ||
console.warn( | ||
`WARNING: known issue with \`fetch()\` requests to custom HTTPS ports in published Workers:\n` + | ||
`The custom port of the URL being fetched "${url.toString()}" will be ignored.\n` + | ||
`Instead it will use the "default port" (${actualUrl}) when the Worker is published to Cloudflare.` | ||
); | ||
} | ||
return originalFetch(request, init); | ||
}; | ||
})(); |