|
| 1 | +import dns from 'node:dns/promises'; |
| 2 | + |
| 3 | +import { isAtprotoDid, type AtprotoDid, type Handle } from '@atcute/identity'; |
| 4 | +import { |
| 5 | + AmbiguousHandleError, |
| 6 | + DidNotFoundError, |
| 7 | + FailedHandleResolutionError, |
| 8 | + InvalidResolvedHandleError, |
| 9 | + type HandleResolver, |
| 10 | + type ResolveHandleOptions, |
| 11 | +} from '@atcute/identity-resolver'; |
| 12 | + |
| 13 | +const SUBDOMAIN = '_atproto'; |
| 14 | +const PREFIX = 'did='; |
| 15 | + |
| 16 | +export interface NodeDnsHandleResolverOptions { |
| 17 | + nameservers?: string[]; |
| 18 | +} |
| 19 | + |
| 20 | +export class NodeDnsHandleResolver implements HandleResolver { |
| 21 | + #resolver: dns.Resolver | null = null; |
| 22 | + |
| 23 | + get nameservers(): string[] | undefined { |
| 24 | + return this.#resolver?.getServers(); |
| 25 | + } |
| 26 | + |
| 27 | + constructor({ nameservers }: NodeDnsHandleResolverOptions = {}) { |
| 28 | + if (nameservers) { |
| 29 | + this.#resolver = new dns.Resolver(); |
| 30 | + this.#resolver.setServers(nameservers); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + async resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid> { |
| 35 | + let results: string[][]; |
| 36 | + |
| 37 | + try { |
| 38 | + const signal = options?.signal; |
| 39 | + const resolver = this.#resolver ?? dns; |
| 40 | + |
| 41 | + results = await resolver.resolveTxt(`${SUBDOMAIN}.${handle}`); |
| 42 | + signal?.throwIfAborted(); |
| 43 | + } catch (cause) { |
| 44 | + if (cause instanceof Error && 'code' in cause && cause.code === 'ENOTFOUND') { |
| 45 | + throw new DidNotFoundError(handle); |
| 46 | + } |
| 47 | + |
| 48 | + throw new FailedHandleResolutionError(handle, { cause }); |
| 49 | + } |
| 50 | + |
| 51 | + const records = results.map((record) => record.join('').replace(/^"|"$/g, '').replace(/\\"/g, '"')); |
| 52 | + for (let i = 0, il = records.length; i < il; i++) { |
| 53 | + const data = records[i]; |
| 54 | + |
| 55 | + if (!data.startsWith(PREFIX)) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + for (let j = i + 1; j < il; j++) { |
| 60 | + const data = records[j]; |
| 61 | + if (data.startsWith(PREFIX)) { |
| 62 | + throw new AmbiguousHandleError(handle); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + const did = data.slice(PREFIX.length); |
| 67 | + if (!isAtprotoDid(did)) { |
| 68 | + throw new InvalidResolvedHandleError(handle, did); |
| 69 | + } |
| 70 | + |
| 71 | + return did; |
| 72 | + } |
| 73 | + |
| 74 | + // theoretically this shouldn't happen, it should've returned ENOTFOUND |
| 75 | + throw new DidNotFoundError(handle); |
| 76 | + } |
| 77 | +} |
0 commit comments