|
| 1 | +/** |
| 2 | + * Build-time helpers for standard.site document verification. |
| 3 | + * |
| 4 | + * Episodes are published to ATProto as `site.standard.document` records by |
| 5 | + * `scripts/publish-episodes.ts`. Each record stores its `path` (matching an |
| 6 | + * episode's slug) but gets a random TID record key, so the only way to emit the |
| 7 | + * per-page `<link rel="site.standard.document">` verification tag is to look up |
| 8 | + * the published records and map their `path` back to their record key. |
| 9 | + * |
| 10 | + * This module resolves the configured DID's PDS, lists its document records |
| 11 | + * (unauthenticated — listRecords is public), and returns a `path -> rkey` map. |
| 12 | + * The result is memoized so the lookup runs once per build despite the page |
| 13 | + * being rendered for many episodes. |
| 14 | + */ |
| 15 | + |
| 16 | +const DOCUMENT_COLLECTION = 'site.standard.document'; |
| 17 | + |
| 18 | +interface PlcService { |
| 19 | + id?: string; |
| 20 | + type?: string; |
| 21 | + serviceEndpoint?: string; |
| 22 | +} |
| 23 | + |
| 24 | +async function resolvePds(did: string): Promise<string> { |
| 25 | + if (did.startsWith('did:plc:')) { |
| 26 | + const res = await fetch(`https://plc.directory/${did}`); |
| 27 | + if (!res.ok) throw new Error(`Failed to resolve DID: ${did}`); |
| 28 | + const doc = (await res.json()) as { service?: PlcService[] }; |
| 29 | + const pds = doc.service?.find( |
| 30 | + (s) => s.type === 'AtprotoPersonalDataServer' || s.id === '#atproto_pds' |
| 31 | + ); |
| 32 | + if (!pds?.serviceEndpoint) { |
| 33 | + throw new Error(`No PDS found in DID document for ${did}`); |
| 34 | + } |
| 35 | + return pds.serviceEndpoint; |
| 36 | + } |
| 37 | + |
| 38 | + if (did.startsWith('did:web:')) { |
| 39 | + const domain = did.replace('did:web:', ''); |
| 40 | + const res = await fetch(`https://${domain}/.well-known/did.json`); |
| 41 | + if (!res.ok) throw new Error(`Failed to resolve DID: ${did}`); |
| 42 | + const doc = (await res.json()) as { service?: PlcService[] }; |
| 43 | + const pds = doc.service?.find( |
| 44 | + (s) => s.type === 'AtprotoPersonalDataServer' || s.id === '#atproto_pds' |
| 45 | + ); |
| 46 | + if (!pds?.serviceEndpoint) { |
| 47 | + throw new Error(`No PDS found in DID document for ${did}`); |
| 48 | + } |
| 49 | + return pds.serviceEndpoint; |
| 50 | + } |
| 51 | + |
| 52 | + throw new Error(`Unsupported DID method: ${did}`); |
| 53 | +} |
| 54 | + |
| 55 | +async function buildDocumentRkeyMap(did: string): Promise<Map<string, string>> { |
| 56 | + const pds = await resolvePds(did); |
| 57 | + const map = new Map<string, string>(); |
| 58 | + let cursor: string | undefined; |
| 59 | + |
| 60 | + do { |
| 61 | + const url = new URL(`${pds}/xrpc/com.atproto.repo.listRecords`); |
| 62 | + url.searchParams.set('repo', did); |
| 63 | + url.searchParams.set('collection', DOCUMENT_COLLECTION); |
| 64 | + url.searchParams.set('limit', '100'); |
| 65 | + if (cursor) url.searchParams.set('cursor', cursor); |
| 66 | + |
| 67 | + const res = await fetch(url); |
| 68 | + if (!res.ok) { |
| 69 | + throw new Error(`Failed to list standard.site documents: ${res.status}`); |
| 70 | + } |
| 71 | + |
| 72 | + const data = (await res.json()) as { |
| 73 | + records: { uri: string; value: { path?: string } }[]; |
| 74 | + cursor?: string; |
| 75 | + }; |
| 76 | + |
| 77 | + for (const record of data.records) { |
| 78 | + const path = record.value.path; |
| 79 | + const rkey = record.uri.split('/').pop(); |
| 80 | + if (path && rkey) map.set(path, rkey); |
| 81 | + } |
| 82 | + |
| 83 | + cursor = data.cursor; |
| 84 | + } while (cursor); |
| 85 | + |
| 86 | + return map; |
| 87 | +} |
| 88 | + |
| 89 | +let cache: Promise<Map<string, string>> | null = null; |
| 90 | + |
| 91 | +/** |
| 92 | + * Returns a `path -> rkey` map of the published standard.site documents for the |
| 93 | + * configured DID, or an empty map if standard.site is not configured (or the |
| 94 | + * lookup fails). Memoized for the lifetime of the build. |
| 95 | + */ |
| 96 | +export function getDocumentRkeys(): Promise<Map<string, string>> { |
| 97 | + if (cache) return cache; |
| 98 | + |
| 99 | + const did = import.meta.env.STANDARD_SITE_DID; |
| 100 | + if (!did) { |
| 101 | + cache = Promise.resolve(new Map()); |
| 102 | + return cache; |
| 103 | + } |
| 104 | + |
| 105 | + cache = buildDocumentRkeyMap(did).catch((err) => { |
| 106 | + console.warn( |
| 107 | + `[standard.site] Could not load document records for verification: ${ |
| 108 | + err instanceof Error ? err.message : String(err) |
| 109 | + }` |
| 110 | + ); |
| 111 | + return new Map<string, string>(); |
| 112 | + }); |
| 113 | + |
| 114 | + return cache; |
| 115 | +} |
0 commit comments