cloudflare / workerd

The JavaScript / Wasm runtime that powers Cloudflare Workers

Home Page:https://blog.cloudflare.com/workerd-open-source-workers-runtime/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🐛 `fetch` rejects any custom Fetcher instance

nora-soderlund opened this issue · comments

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

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

	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:

class MyFetcherWrapper implements Fetcher {
	connect(address: string | SocketAddress, options?: SocketOptions | undefined): Socket {
		throw new Error("Not implemented.");
	}

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

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