mirror of
https://github.com/aljazceru/ditto.git
synced 2025-12-18 05:54:23 +01:00
Add fetchWorker for fetching off the main thread
This commit is contained in:
19
src/workers/fetch.ts
Normal file
19
src/workers/fetch.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user