Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
51 lines (45 loc) · 1.51 KB
/
Copy pathmain.ts
File metadata and controls
51 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { AutoRouter, cors, error } from "itty-router";
import { AtprotoHandleResolver } from "@atproto-labs/handle-resolver";
const atprotoResolver = new AtprotoHandleResolver({
async resolveTxt(hostname) {
try {
return (await Deno.resolveDns(hostname, "TXT")).flat();
} catch (_) {
return [];
}
},
fetch,
});
const { preflight, corsify } = cors();
const router = AutoRouter({
before: [preflight],
finally: [corsify],
});
router.get("/xrpc/com.atproto.identity.resolveHandle", async ({ query }) => {
const { handle } = query;
if (typeof handle !== "string" || !handle)
return error(400, "handle query parameter required");
const did: string | null = await atprotoResolver.resolve(handle);
if (!did) return error(404, `Could not resolve handle to DID: ${handle}`);
return { did };
});
router.get("/xrpc/town.muni.leaf.resolveHandle", async ({ query }) => {
const { handle } = query;
if (typeof handle !== "string" || !handle)
return error(400, "handle query parameter required");
const txtRecords = (await Deno.resolveDns(`_leaf.${handle}`, "TXT"))
.flat()
.flatMap((x) => {
const split = x.split("=");
if (split.length == 2) {
const [key, value] = split;
return [{ key, value }];
}
return [];
});
const did: string | undefined = txtRecords.find((x) => x.key == "did")?.value;
if (!did || !did.startsWith("did:"))
return error(404, `Could not resolve handle to DID: ${handle}`);
return { did };
});
Deno.serve(router.fetch);