Add fetchWorker for fetching off the main thread

This commit is contained in:
Alex Gleason
2023-11-28 18:44:23 -06:00
parent 81971df7fd
commit 3a85e3f8bf
7 changed files with 56 additions and 3 deletions

19
src/workers/fetch.ts Normal file
View File

@@ -0,0 +1,19 @@
import { Comlink } from '@/deps.ts';
import type { FetchWorker } from './fetch.worker.ts';
const _worker = Comlink.wrap<typeof FetchWorker>(
new Worker(
new URL('./fetch.worker.ts', import.meta.url),
{ type: 'module' },
),
);
const fetchWorker: typeof fetch = async (input) => {
const url = input instanceof Request ? input.url : input.toString();
const args = await _worker.fetch(url);
return new Response(...args);
};
export { fetchWorker };