open source is social v-it.org
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 sol pbc
3
4export async function resolveHandle(did, env) {
5 try {
6 const cached = await env.DB.prepare(
7 "SELECT handle, fetched_at FROM handles WHERE did = ? AND fetched_at > datetime('now', '-24 hours')",
8 )
9 .bind(did)
10 .first();
11
12 if (cached?.handle) {
13 return cached.handle;
14 }
15
16 const res = await fetch(`https://plc.directory/${did}`);
17 if (!res.ok) {
18 return null;
19 }
20
21 const data = await res.json();
22 const handle = Array.isArray(data?.alsoKnownAs)
23 ? data.alsoKnownAs.find(value => typeof value === 'string' && value.startsWith('at://'))?.slice(5) ?? null
24 : null;
25
26 if (!handle) {
27 return null;
28 }
29
30 await env.DB.prepare(
31 `INSERT INTO handles (did, handle, fetched_at)
32 VALUES (?, ?, datetime('now'))
33 ON CONFLICT(did) DO UPDATE SET
34 handle = excluded.handle,
35 fetched_at = excluded.fetched_at`,
36 )
37 .bind(did, handle)
38 .run();
39
40 return handle;
41 } catch {
42 return null;
43 }
44}
45
46export async function resolveHandles(dids, env) {
47 const handles = new Map();
48
49 for (const did of dids) {
50 const handle = await resolveHandle(did, env);
51 if (handle) {
52 handles.set(did, handle);
53 }
54 }
55
56 return handles;
57}