Skip to content

Commit

Permalink
fix: do not crash in wrangler dev when passing a request object to fe…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
petebacondarwin authored Jan 23, 2023
1 parent 89d78c0 commit 416babf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .changeset/perfect-grapes-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": patch
---

fix: do not crash in wrangler dev when passing a request object to fetch

This reverts and fixes the changes in https://github.com/cloudflare/wrangler2/pull/1769
which does not support creating requests from requests whose bodies have already been consumed.

Fixes #2562
7 changes: 6 additions & 1 deletion fixtures/worker-app/src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { now } from "./dep";
export default {
fetch(request) {
async fetch(request) {
console.log(
request.method,
request.url,
new Map([...request.headers]),
request.cf
);

await fetch(new URL("https://example.com"));
await fetch(
new Request("https://example.com", { method: "POST", body: "foo" })
);

return new Response(`${request.url} ${now()}`);
},

Expand Down
10 changes: 9 additions & 1 deletion packages/wrangler/templates/checked-fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const urls = new Set();

export function checkedFetch(request, init) {
const url = new URL(new Request(request, init).url);
const url =
request instanceof URL
? request
: new URL(
(typeof request === "string"
? new Request(request, init)
: request
).url
);
if (url.port && url.port !== "443" && url.protocol === "https:") {
if (!urls.has(url.toString())) {
urls.add(url.toString());
Expand Down

0 comments on commit 416babf

Please sign in to comment.