Merge branch 'fetch-worker' into 'main'
Add fetchWorker for fetching off the main thread Closes #77 See merge request soapbox-pub/ditto!62
This commit is contained in:
commit
5a954a304d
|
@ -1 +1 @@
|
|||
deno 1.37.1
|
||||
deno 1.38.3
|
||||
|
|
|
@ -76,5 +76,6 @@ export { default as uuid62 } from 'npm:uuid62@^1.0.2';
|
|||
export { Machina } from 'https://gitlab.com/soapbox-pub/nostr-machina/-/raw/08a157d39f2741c9a3a4364cb97db36e71d8c03a/mod.ts';
|
||||
export * as Sentry from 'https://deno.land/x/sentry@7.78.0/index.js';
|
||||
export { sentry as sentryMiddleware } from 'npm:@hono/sentry@^1.0.0';
|
||||
export * as Comlink from 'npm:comlink@^4.4.1';
|
||||
|
||||
export type * as TypeFest from 'npm:type-fest@^4.3.0';
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { TTLCache, z } from '@/deps.ts';
|
||||
import { Time } from '@/utils/time.ts';
|
||||
import { fetchWorker } from '@/workers/fetch.ts';
|
||||
|
||||
const nip05Cache = new TTLCache<string, Promise<string | null>>({ ttl: Time.hours(1), max: 5000 });
|
||||
|
||||
|
@ -19,7 +20,7 @@ async function lookup(value: string, opts: LookupOpts = {}): Promise<string | nu
|
|||
const [_, name = '_', domain] = match;
|
||||
|
||||
try {
|
||||
const res = await fetch(`https://${domain}/.well-known/nostr.json?name=${name}`, {
|
||||
const res = await fetchWorker(`https://${domain}/.well-known/nostr.json?name=${name}`, {
|
||||
signal: AbortSignal.timeout(timeout),
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { TTLCache, unfurl } from '@/deps.ts';
|
||||
import { Time } from '@/utils/time.ts';
|
||||
import { fetchWorker } from '@/workers/fetch.ts';
|
||||
|
||||
interface PreviewCard {
|
||||
url: string;
|
||||
|
@ -22,7 +23,7 @@ async function unfurlCard(url: string, signal: AbortSignal): Promise<PreviewCard
|
|||
console.log(`Unfurling ${url}...`);
|
||||
try {
|
||||
const result = await unfurl(url, {
|
||||
fetch: (url) => fetch(url, { signal }),
|
||||
fetch: (url) => fetchWorker(url, { signal }),
|
||||
});
|
||||
|
||||
return {
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
import { assert } from '@/deps-test.ts';
|
||||
|
||||
import { fetchWorker } from './fetch.ts';
|
||||
|
||||
Deno.test('fetchWorker', async () => {
|
||||
await sleep(2000);
|
||||
const response = await fetchWorker('https://example.com');
|
||||
const text = await response.text();
|
||||
assert(text.includes('Example Domain'));
|
||||
});
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
|
@ -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 };
|
|
@ -0,0 +1,17 @@
|
|||
import { Comlink } from '@/deps.ts';
|
||||
|
||||
export const FetchWorker = {
|
||||
async fetch(url: string): Promise<[BodyInit, ResponseInit]> {
|
||||
const response = await fetch(url);
|
||||
return [
|
||||
await response.text(),
|
||||
{
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: Array.from(response.headers.entries()),
|
||||
},
|
||||
];
|
||||
},
|
||||
};
|
||||
|
||||
Comlink.expose(FetchWorker);
|
Loading…
Reference in New Issue