Skip to content
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

Merged
merged 3 commits into from
Jan 12, 2023

Conversation

dario-piotrowicz
Copy link
Member

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

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-bot
Copy link

changeset-bot bot commented Sep 4, 2022

🦋 Changeset detected

Latest commit: 088bb51

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

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

@dario-piotrowicz
Copy link
Member Author

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 Request | string, so although this works fine typescript is not happy 😢 I had a look into workers-types but if I understood correctly the fetch param types there are all auto-generated?
do the fetch param types need to be updated in the workers runtime? (PS: I had a look just to be sure and according to MDN fetch is indeed supposed to accept a URL object)

@dario-piotrowicz
Copy link
Member Author

Also I couldn't fine unit tests regarding this fetch patching (that's why I didn't add any), is it not being tested? or am I just missing the tests?

Copy link
Contributor

@petebacondarwin petebacondarwin left a 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.

@dario-piotrowicz
Copy link
Member Author

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 scream 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?

@dario-piotrowicz dario-piotrowicz requested review from petebacondarwin and removed request for threepointone September 5, 2022 21:19
@petebacondarwin
Copy link
Contributor

Regarding the typings, I think we probably could start by creating an issue here: https://github.com/cloudflare/workers-types.
From the docs, the request can be anything with a "stringifier" which doesn't seem very easy to define in pure TS.
@mrbbot could confirm but I think the fix would be in the Workers Runtime code internally at Cloudflare.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2022

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 with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/2996103266/npm-package-wrangler-1769 dev path/to/script.js

@codecov
Copy link

codecov bot commented Sep 5, 2022

Codecov Report

Merging #1769 (634761b) into main (75f3ae8) will decrease coverage by 6.05%.
The diff coverage is n/a.

❗ Current head 634761b differs from pull request most recent head 088bb51. Consider uploading reports for the commit 088bb51 to get more accurate results

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
packages/wrangler/src/metrics/send-event.ts 100.00% <0.00%> (ø)
packages/wrangler/src/inspect.ts 5.62% <0.00%> (ø)
packages/wrangler/src/proxy.ts 18.47% <0.00%> (ø)
packages/wrangler/src/dev/local.tsx 29.51% <0.00%> (ø)
packages/wrangler/src/dev/start-server.ts 69.49% <0.00%> (ø)
packages/wrangler/src/dev-registry.tsx 14.28% <0.00%> (ø)
packages/wrangler/src/bundle.ts 71.42% <0.00%> (+1.78%) ⬆️
packages/wrangler/src/dev.tsx 84.65% <0.00%> (+3.36%) ⬆️
...ackages/wrangler/src/__tests__/helpers/mock-bin.ts 100.00% <0.00%> (+5.26%) ⬆️
packages/wrangler/src/api/dev.ts 58.33% <0.00%> (+42.94%) ⬆️

@mrbbot
Copy link
Contributor

mrbbot commented Sep 5, 2022

@mrbbot could confirm but I think the fix would be in the Workers Runtime code internally at Cloudflare.

Yep, looks like the runtime code only accepts a Request and string, but I'm guessing there's some hidden magic to coerce values to strings. We could update the types generation script to account for this, but I think in this case the best solution would be to define an override for fetch accepting Request | string | URL.

@dario-piotrowicz
Copy link
Member Author

I see, thanks @petebacondarwin and @mrbbot 🙂 , shall I then proceed and create an issue in https://github.com/cloudflare/workers-types?

@petebacondarwin
Copy link
Contributor

Yes please!

@dario-piotrowicz
Copy link
Member Author

Yes please!

Done 🙂
cloudflare/workers-types#277

@petebacondarwin petebacondarwin merged commit 6a67efe into cloudflare:main Jan 12, 2023
@github-actions github-actions bot mentioned this pull request Jan 12, 2023
GregBrimble pushed a commit that referenced this pull request Jan 13, 2023
* 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);
Copy link
Contributor

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.

Copy link
Contributor

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.

#2562 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here: #2597

Copy link
Member Author

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 🙇)

Copy link
Contributor

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).

petebacondarwin added a commit to petebacondarwin/wrangler2 that referenced this pull request Jan 23, 2023
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
petebacondarwin added a commit to petebacondarwin/wrangler2 that referenced this pull request Jan 23, 2023
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
@dario-piotrowicz dario-piotrowicz deleted the 1588 branch January 23, 2023 22:46
petebacondarwin added a commit that referenced this pull request Jan 23, 2023
…tch (#2597)

This reverts and fixes the changes in #1769
which does not support creating requests from requests whose bodies have already been consumed.

Fixes #2562
@github-actions github-actions bot mentioned this pull request Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: fetch() does not accept URL objects when testing locally but does when deployed
4 participants