this repo has no description
atproto
bluesky
typescript
express
1import { fetch, Agent, setGlobalDispatcher } from "undici";
2import dns from "dns/promises";
3
4export async function byDNS(domain: string): Promise<string | any> {
5 const txt = `_atproto.${domain}`;
6 let records: string[][];
7
8 try {
9 records = await dns.resolveTxt(txt);
10 } catch (e) {
11 return null;
12 }
13
14 for (const chunks of records) {
15 const record = chunks.join("");
16 const trimmed = record.trim();
17
18 if (trimmed.toLowerCase().startsWith("did=")) {
19 const candidate = trimmed.slice(4).trim();
20
21 if (candidate) return candidate;
22 }
23 }
24
25 return null;
26}
27
28export async function byHTTP(domain: string, secure: boolean = true): Promise<string | any> {
29 const agent = new Agent({ keepAliveTimeout: 10000 });
30 setGlobalDispatcher(agent);
31
32 let url;
33
34 if (secure) {
35 url = `https://${domain}/.well-known/atproto-did`;
36 } else {
37 url = `http://${domain}/.well-known/atproto-did`;
38 }
39
40 try {
41 const response = await fetch(url);
42 const data = await response.text();
43
44 if (!data) {
45 return null;
46 } else {
47 return data.trim();
48 }
49 } catch (e) {
50 return null;
51 }
52}
53
54export async function getPLC(did: string, endpoint: string = "https://plc.directory"): Promise<any> {
55 const agent = new Agent({ keepAliveTimeout: 10000 });
56 setGlobalDispatcher(agent);
57
58 try {
59 const response = await fetch(`${endpoint}/${did}`);
60 const data = await response.json();
61
62 if (!data) {
63 return null;
64 } else {
65 return data;
66 }
67 } catch (e) {
68 return null;
69 }
70}
71
72export async function byPLC(did: string) {
73 const didDoc = await getPLC(did);
74 return didDoc.id;
75}
76
77export async function getPDS(did: string) {
78 const didDoc = await getPLC(did);
79 return didDoc.service[0].serviceEndpoint;
80}