Skip to content

Commit

Permalink
Refactor checkFetch from a separate method to a proxy around fetch (#…
Browse files Browse the repository at this point in the history
…3497)

* refactor checkFetch from a separate method to a proxy around fetch

* Add changeset

* prettier with the right options
  • Loading branch information
evanderkoogh authored and lrapoport-cf committed Aug 11, 2023
1 parent 85f2536 commit e8b409e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-files-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Refactor dev-only checkedFetch check from a method substitution to a JavaScript Proxy to be able to support Proxied global fetch function.
1 change: 0 additions & 1 deletion packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ export async function bundleWorker(
// when we do a build of wrangler. (re: https://github.com/cloudflare/workers-sdk/issues/1477)
"process.env.NODE_ENV": `"${process.env["NODE_ENV" + ""]}"`,
...(legacyNodeCompat ? { global: "globalThis" } : {}),
...(checkFetch ? { fetch: "checkedFetch" } : {}),
...options.define,
},
}),
Expand Down
11 changes: 9 additions & 2 deletions packages/wrangler/templates/checked-fetch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const urls = new Set();

export function checkedFetch(request, init) {
function checkURL(request, init) {
const url =
request instanceof URL
? request
Expand All @@ -19,5 +19,12 @@ export function checkedFetch(request, init) {
);
}
}
return globalThis.fetch(request, init);
}

globalThis.fetch = new Proxy(globalThis.fetch, {
apply(target, thisArg, argArray) {
const [request, init] = argArray;
checkURL(request, init);
return Reflect.apply(target, thisArg, argArray);
},
});

0 comments on commit e8b409e

Please sign in to comment.