mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-21 22:34:24 +01:00
Replace all timeouts with AbortSignal
This commit is contained in:
@@ -7,12 +7,12 @@ const nip05Cache = new TTLCache<string, Promise<string | null>>({ ttl: Time.hour
|
||||
const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w.-]+)$/;
|
||||
|
||||
interface LookupOpts {
|
||||
timeout?: number;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
/** Get pubkey from NIP-05. */
|
||||
async function lookup(value: string, opts: LookupOpts = {}): Promise<string | null> {
|
||||
const { timeout = 2000 } = opts;
|
||||
const { signal = AbortSignal.timeout(2000) } = opts;
|
||||
|
||||
const match = value.match(NIP05_REGEX);
|
||||
if (!match) return null;
|
||||
@@ -21,7 +21,7 @@ async function lookup(value: string, opts: LookupOpts = {}): Promise<string | nu
|
||||
|
||||
try {
|
||||
const res = await fetchWorker(`https://${domain}/.well-known/nostr.json?name=${name}`, {
|
||||
signal: AbortSignal.timeout(timeout),
|
||||
signal,
|
||||
});
|
||||
|
||||
const { names } = nostrJsonSchema.parse(await res.json());
|
||||
|
||||
@@ -60,12 +60,12 @@ const previewCardCache = new TTLCache<string, Promise<PreviewCard | null>>({
|
||||
});
|
||||
|
||||
/** Unfurl card from cache if available, otherwise fetch it. */
|
||||
function unfurlCardCached(url: string, timeout = Time.seconds(1)): Promise<PreviewCard | null> {
|
||||
function unfurlCardCached(url: string, signal = AbortSignal.timeout(1000)): Promise<PreviewCard | null> {
|
||||
const cached = previewCardCache.get(url);
|
||||
if (cached !== undefined) {
|
||||
return cached;
|
||||
} else {
|
||||
const card = unfurlCard(url, AbortSignal.timeout(timeout));
|
||||
const card = unfurlCard(url, signal);
|
||||
previewCardCache.set(url, card);
|
||||
return card;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user