-
Notifications
You must be signed in to change notification settings - Fork 774
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
fix: allow URL object to be passed to local fetch()
calls
#1769
Conversation
Due to the local fetch check, URL objects are wrongly not accepted by `fetch()` calls, amend the `checkedFetch` function in order to allow such scenario (which anyways works after deployment). resolves cloudflare#1588
🦋 Changeset detectedLatest commit: 088bb51 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Hi there 🙂 , I'm just trying to do this small bug fix, I hope my change makes sense 🙂 Anyways the type of the request is still |
Also I couldn't fine unit tests regarding this |
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.
Thanks for putting this together @dario-piotrowicz.
From looking at the MDN docs for fetch()
, it says:
The fetch() method's parameters are identical to those of the Request() constructor.
So I would propose a simpler fix which is to always pass the fetch params to a Request()
constructor and get the URL that way. E.g.
checkedFetch(request, init) {
const url = new URL((new Request(request, init)).url;
...
}
WDYT?
And sorry that I never made any tests for this 😱 I don't know why...
It is a little awkward mocking out the real fetch()
but we do it elsewhere.
Sounds good thanks 😃 About the tests if you want I can have a try in adding some 😄 by the way, what about the types then? how can I get the param types updated? |
Regarding the typings, I think we probably could start by creating an issue here: https://github.com/cloudflare/workers-types. |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2996103266/npm-package-wrangler-1769 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/1769/npm-package-wrangler-1769 Or you can use npx https://prerelease-registry.developers.workers.dev/runs/2996103266/npm-package-wrangler-1769 dev path/to/script.js |
simplify solution
2179852
to
634761b
Compare
Codecov Report
@@ Coverage Diff @@
## main #1769 +/- ##
==========================================
- Coverage 80.19% 74.13% -6.06%
==========================================
Files 92 97 +5
Lines 6226 7068 +842
Branches 1603 1850 +247
==========================================
+ Hits 4993 5240 +247
- Misses 1233 1828 +595
|
Yep, looks like the runtime code only accepts a |
I see, thanks @petebacondarwin and @mrbbot 🙂 , shall I then proceed and create an issue in https://github.com/cloudflare/workers-types? |
Yes please! |
Done 🙂 |
* fix: allow URL object to be passed to local `fetch()` calls Due to the local fetch check, URL objects are wrongly not accepted by `fetch()` calls, amend the `checkedFetch` function in order to allow such scenario (which anyways works after deployment). resolves #1588
const url = new URL( | ||
(typeof request === "string" ? new Request(request, init) : request).url | ||
); | ||
const url = new URL(new Request(request, init).url); |
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.
Ah, I think I got bitten by this change just the other day. Something to do with the request body being consumed twice. I'll look into it, but this should maybe be reverted.
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.
Yes let's revert this. I can see this error using the reproduction in the linked issue.
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.
Here: #2597
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.
sorry about the bug 😢 (thanks for the fix Pete 🙇)
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 never thought to test it on POST requests coming out of a Worker.
The reproduction required a fetch()
call to a Request
that has a body (but GET requests cannot have bodies).
This reverts and fixes the changes in cloudflare#1769 which does not support creating requests from requests whose bodies have already been consumed. Fixes cloudflare#2562
This reverts and fixes the changes in cloudflare#1769 which does not support creating requests from requests whose bodies have already been consumed. Fixes cloudflare#2562
Due to the local fetch check, URL objects are wrongly not accepted by
fetch()
calls,amend the
checkedFetch
function in order to allow such scenario (whichanyways works after deployment).
resolves #1588