cloudflare / cloudflare/workerd

🐛 `fetch` rejects any custom Fetcher instance

Open
#1,686 1 comment 3 reactions 0 assignees View on GitHub
feature request
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

Given `fetch` has a `fetcher` option, it would've been very easy to enter my own Fetcher instance and have the whole Fetch API in my hands for my fetcher, but unfortunately, the fetcher input is validated against the Fetcher class.

> Incorrect type for the 'fetcher' field on 'RequestInitializerDict': the provided value is not of type 'Fetcher'.

Frames that leads up to this error:
.wrangler/tmp/bundle-Xfh9jL/checked-fetch.js
```js
globalThis.fetch = new Proxy(globalThis.fetch, {
apply(target, thisArg, argArray) {
const [request, init] = argArray;
checkURL(request, init); // next frame starts here
return Reflect.apply(target, thisArg, argArray);
},
});
```

.wrangler/tmp/bundle-Xfh9jL/checked-fetch.js
```js
const url =
request instanceof URL
? request
: new URL(
(typeof request === "string"
? new Request(request, init) // error is thrown here
: request
).url
);
```

This is produced by the following worker:
```ts
class MyFetcherWrapper implements Fetcher {
connect(address: string | SocketAddress, options?: SocketOptions | undefined): Socket {
throw new Error("Not implemented.");
}

fetch(input: RequestInfo, init?: RequestInit> | undefined): Promise {
return Promise.resolve(new Response()); // for the purpose of a minimal reproducible example
}
}

export default {
async fetch(request: Request, env: never, ctx: ExecutionContext): Promise {
return fetch("https://google.com", {
fetcher: new MyFetcherWrapper()
});
},
};
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.