import { fetch, Agent, setGlobalDispatcher } from "undici"; import dns from "dns/promises"; export async function byDNS(domain: string): Promise { const txt = `_atproto.${domain}`; let records: string[][]; try { records = await dns.resolveTxt(txt); } catch (e) { return null; } for (const chunks of records) { const record = chunks.join(""); const trimmed = record.trim(); if (trimmed.toLowerCase().startsWith("did=")) { const candidate = trimmed.slice(4).trim(); if (candidate) return candidate; } } return null; } export async function byHTTP(domain: string, secure: boolean = true): Promise { const agent = new Agent({ keepAliveTimeout: 10000 }); setGlobalDispatcher(agent); let url; if (secure) { url = `https://${domain}/.well-known/atproto-did`; } else { url = `http://${domain}/.well-known/atproto-did`; } try { const response = await fetch(url); const data = await response.text(); if (!data) { return null; } else { return data.trim(); } } catch (e) { return null; } } export async function getPLC(did: string, endpoint: string = "https://plc.directory"): Promise { const agent = new Agent({ keepAliveTimeout: 10000 }); setGlobalDispatcher(agent); try { const response = await fetch(`${endpoint}/${did}`); const data = await response.json(); if (!data) { return null; } else { return data; } } catch (e) { return null; } } export async function byPLC(did: string) { const didDoc = await getPLC(did); return didDoc.id; } export async function getPDS(did: string) { const didDoc = await getPLC(did); return didDoc.service[0].serviceEndpoint; }