-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathindex.ts
60 lines (53 loc) · 1.53 KB
/
index.ts
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
52
53
54
55
56
57
58
59
60
import dns from 'dns/promises'
import { HandleResolverOpts } from '../types'
const SUBDOMAIN = '_atproto'
const PREFIX = 'did='
export class HandleResolver {
public timeout: number
constructor(opts: HandleResolverOpts = {}) {
this.timeout = opts.timeout ?? 3000
}
async resolve(handle: string): Promise<string | undefined> {
const dnsPromise = this.resolveDns(handle)
const httpAbort = new AbortController()
const httpPromise = this.resolveHttp(handle, httpAbort.signal).catch(
() => undefined,
)
const dnsRes = await dnsPromise
if (dnsRes) {
httpAbort.abort()
return dnsRes
}
return httpPromise
}
async resolveDns(handle: string): Promise<string | undefined> {
let chunkedResults: string[][]
try {
chunkedResults = await dns.resolveTxt(`${SUBDOMAIN}.${handle}`)
} catch (err) {
return undefined
}
const results = chunkedResults.map((chunks) => chunks.join(''))
const found = results.filter((i) => i.startsWith(PREFIX))
if (found.length !== 1) {
return undefined
}
return found[0].slice(PREFIX.length)
}
async resolveHttp(
handle: string,
signal?: AbortSignal,
): Promise<string | undefined> {
const url = new URL('/.well-known/atproto-did', `https://${handle}`)
try {
const res = await fetch(url, { signal })
const did = await res.text()
if (typeof did === 'string' && did.startsWith('did:')) {
return did
}
return undefined
} catch (err) {
return undefined
}
}
}