Add semaphore to nip05 script

This commit is contained in:
Alex Gleason 2025-02-07 18:17:50 -06:00
parent b902abc7cc
commit 7780507a15
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,3 +1,4 @@
import { Semaphore } from '@lambdalisue/async';
import { NSchema as n } from '@nostrify/nostrify'; import { NSchema as n } from '@nostrify/nostrify';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
@ -5,6 +6,7 @@ import { faviconCache } from '@/utils/favicon.ts';
import { nip05Cache } from '@/utils/nip05.ts'; import { nip05Cache } from '@/utils/nip05.ts';
const kysely = await Storages.kysely(); const kysely = await Storages.kysely();
const sem = new Semaphore(5);
const query = kysely const query = kysely
.selectFrom('nostr_events') .selectFrom('nostr_events')
@ -12,31 +14,37 @@ const query = kysely
.where('kind', '=', 0); .where('kind', '=', 0);
for await (const { content } of query.stream(100)) { for await (const { content } of query.stream(100)) {
const signal = AbortSignal.timeout(30_000); // generous timeout while (sem.locked) {
await new Promise((resolve) => setTimeout(resolve, 0));
// Parse metadata.
const metadata = n.json().pipe(n.metadata()).catch({}).safeParse(content);
if (!metadata.success) continue;
// Update nip05.
const { nip05 } = metadata.data;
if (nip05) {
try {
await nip05Cache.fetch(nip05, { signal });
} catch {
// Ignore.
}
} }
// Update favicon. sem.lock(async () => {
const domain = nip05?.split('@')[1].toLowerCase(); const signal = AbortSignal.timeout(30_000); // generous timeout
if (domain) {
try { // Parse metadata.
await faviconCache.fetch(domain, { signal }); const metadata = n.json().pipe(n.metadata()).catch({}).safeParse(content);
} catch { if (!metadata.success) return;
// Ignore.
// Update nip05.
const { nip05 } = metadata.data;
if (nip05) {
try {
await nip05Cache.fetch(nip05, { signal });
} catch {
// Ignore.
}
} }
}
// Update favicon.
const domain = nip05?.split('@')[1].toLowerCase();
if (domain) {
try {
await faviconCache.fetch(domain, { signal });
} catch {
// Ignore.
}
}
});
} }
Deno.exit(); Deno.exit();