Merge branch 'fetchworker-init' into 'main'

fetchWorker: support init options, support POST

See merge request soapbox-pub/ditto!64
This commit is contained in:
Alex Gleason 2023-11-29 20:02:45 +00:00
commit 4ca3f1768b
4 changed files with 68 additions and 13 deletions

View File

@ -1,5 +1,6 @@
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { z } from '@/deps.ts'; import { z } from '@/deps.ts';
import { fetchWorker } from '@/workers/fetch.ts';
import type { Uploader } from './types.ts'; import type { Uploader } from './types.ts';
@ -22,7 +23,7 @@ const ipfsUploader: Uploader = {
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
const response = await fetch(url, { const response = await fetchWorker(url, {
method: 'POST', method: 'POST',
body: formData, body: formData,
}); });
@ -41,7 +42,7 @@ const ipfsUploader: Uploader = {
url.search = query.toString(); url.search = query.toString();
await fetch(url, { await fetchWorker(url, {
method: 'POST', method: 'POST',
}); });
}, },

View File

@ -4,10 +4,14 @@ import { fetchWorker } from './fetch.ts';
await sleep(2000); await sleep(2000);
Deno.test('fetchWorker', async () => { Deno.test({
const response = await fetchWorker('https://example.com'); name: 'fetchWorker',
const text = await response.text(); async fn() {
assert(text.includes('Example Domain')); const response = await fetchWorker('https://example.com');
const text = await response.text();
assert(text.includes('Example Domain'));
},
sanitizeResources: false,
}); });
Deno.test({ Deno.test({

View File

@ -11,11 +11,61 @@ const _worker = Comlink.wrap<typeof FetchWorker>(
), ),
); );
const fetchWorker: typeof fetch = async (input, init) => { /**
const { signal, ...rest } = init || {}; * Fetch implementation with a Web Worker.
const url = input instanceof Request ? input.url : input.toString(); * Calling this performs the fetch in a separate CPU thread so it doesn't block the main thread.
const args = await _worker.fetch(url, rest, signal); */
return new Response(...args); const fetchWorker: typeof fetch = async (...args) => {
const [url, init] = serializeFetchArgs(args);
const { body, signal, ...rest } = init;
const result = await _worker.fetch(url, { ...rest, body: await prepareBodyForWorker(body) }, signal);
return new Response(...result);
}; };
/** Take arguments to `fetch`, and turn them into something we can send over Comlink. */
function serializeFetchArgs(args: Parameters<typeof fetch>): [string, RequestInit] {
const request = normalizeRequest(args);
const init = requestToInit(request);
return [request.url, init];
}
/** Get a `Request` object from arguments to `fetch`. */
function normalizeRequest(args: Parameters<typeof fetch>): Request {
return new Request(...args);
}
/** Get the body as a type we can transfer over Web Workers. */
async function prepareBodyForWorker(
body: BodyInit | undefined | null,
): Promise<ArrayBuffer | Blob | string | undefined | null> {
if (!body || typeof body === 'string' || body instanceof ArrayBuffer || body instanceof Blob) {
return body;
} else {
const response = new Response(body);
return await response.arrayBuffer();
}
}
/**
* Convert a `Request` object into its serialized `RequestInit` format.
* `RequestInit` is a subset of `Request`, just lacking helper methods like `json()`,
* making it easier to serialize (exceptions: `body` and `signal`).
*/
function requestToInit(request: Request): RequestInit {
return {
method: request.method,
headers: [...request.headers.entries()],
body: request.body,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
mode: request.mode,
credentials: request.credentials,
cache: request.cache,
redirect: request.redirect,
integrity: request.integrity,
keepalive: request.keepalive,
signal: request.signal,
};
}
export { fetchWorker }; export { fetchWorker };

View File

@ -10,11 +10,11 @@ export const FetchWorker = {
): Promise<[BodyInit, ResponseInit]> { ): Promise<[BodyInit, ResponseInit]> {
const response = await fetch(url, { ...init, signal }); const response = await fetch(url, { ...init, signal });
return [ return [
await response.text(), await response.arrayBuffer(),
{ {
status: response.status, status: response.status,
statusText: response.statusText, statusText: response.statusText,
headers: Array.from(response.headers.entries()), headers: [...response.headers.entries()],
}, },
]; ];
}, },