forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {LRU} from './direct-fetch-record'
2
3const serviceCache = new LRU<`did:${string}`, string>()
4
5export async function resolvePdsServiceUrl(did: `did:${string}`) {
6 return await serviceCache.getOrTryInsertWith(did, async () => {
7 const docUrl = did.startsWith('did:plc:')
8 ? `https://plc.directory/${did}`
9 : `https://${did.substring(8)}/.well-known/did.json`
10
11 // TODO: validate!
12 const doc: {
13 service: {
14 serviceEndpoint: string
15 type: string
16 }[]
17 } = await (await fetch(docUrl)).json()
18 const service = doc.service.find(
19 s => s.type === 'AtprotoPersonalDataServer',
20 )?.serviceEndpoint
21
22 if (service === undefined)
23 throw new Error(`could not find a service for ${did}`)
24 return service
25 })
26}