Replace all timeouts with AbortSignal

This commit is contained in:
Alex Gleason
2023-12-22 10:38:48 -06:00
parent 6d6e3bcecc
commit ad0aaf97dd
10 changed files with 35 additions and 40 deletions

View File

@@ -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());

View File

@@ -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;
}