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

🐛 fetch rejects any custom Fetcher instance #1686

Open
nora-soderlund opened this issue Feb 18, 2024 · 0 comments
Open

🐛 fetch rejects any custom Fetcher instance #1686

nora-soderlund opened this issue Feb 18, 2024 · 0 comments
Labels
feature request Request for Workers team to add a feature

Comments

@nora-soderlund
Copy link

nora-soderlund commented Feb 18, 2024

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()
		});
	},
};
@jasnell jasnell added the feature request Request for Workers team to add a feature label Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for Workers team to add a feature
Projects
None yet
Development

No branches or pull requests

2 participants