Static site hosting via tangled
1const PDS_SERVICE_ID = "#atproto_pds";
2
3async function getServiceEndpointFromDidDoc(didDoc) {
4 const service = didDoc.service.find((s) => s.id === PDS_SERVICE_ID);
5 if (!service) {
6 throw new Error(
7 `No PDS service found in DID doc ${JSON.stringify(didDoc)}`
8 );
9 }
10 return service.serviceEndpoint;
11}
12
13async function resolveDid(did) {
14 if (did.startsWith("did:plc:")) {
15 const res = await fetch(`https://plc.directory/${encodeURIComponent(did)}`);
16 const didDoc = await res.json();
17 return didDoc;
18 } else if (did.startsWith("did:web:")) {
19 const website = did.split(":")[2];
20 const res = await fetch(`https://${website}/.well-known/did.json`);
21 const didDoc = await res.json();
22 return didDoc;
23 } else {
24 throw new Error(`Unsupported DID: ${did}`);
25 }
26}
27
28async function getServiceEndpointForDid(did) {
29 const didDoc = await resolveDid(did);
30 return getServiceEndpointFromDidDoc(didDoc);
31}
32
33export async function listRecords({ did, collection }) {
34 const serviceEndpoint = await getServiceEndpointForDid(did);
35 let cursor = "";
36 const records = [];
37 do {
38 const res = await fetch(
39 `${serviceEndpoint}/xrpc/com.atproto.repo.listRecords?repo=${did}&collection=${collection}&limit=100&cursor=${cursor}`
40 );
41 const data = await res.json();
42 const recordsWithAuthor = data.records.map((record) => {
43 return {
44 ...record,
45 author: did,
46 };
47 });
48 records.push(...recordsWithAuthor);
49 cursor = data.cursor;
50 } while (cursor);
51 return records;
52}