Live video on the AT Protocol
1import type { IdResolver } from "@atproto/identity";
2
3import axios from "axios";
4
5import type { TestPdsServer } from "./pds.js";
6
7export const mockNetworkUtilities = (pds: TestPdsServer) => {
8 mockResolvers(pds.ctx.idResolver, pds);
9};
10
11export const mockResolvers = (idResolver: IdResolver, pds: TestPdsServer) => {
12 // Map pds public url to its local url when resolving from plc
13 const origResolveDid = idResolver.did.resolveNoCache;
14 idResolver.did.resolveNoCache = async (did: string) => {
15 const result = await (origResolveDid.call(
16 idResolver.did,
17 did,
18 ) as ReturnType<typeof origResolveDid>);
19 const service = result?.service?.find((svc) => svc.id === "#atproto_pds");
20
21 if (typeof service?.serviceEndpoint === "string") {
22 service.serviceEndpoint = service.serviceEndpoint.replace(
23 pds.ctx.cfg.service.publicUrl,
24 `http://localhost:${pds.port}`,
25 );
26 }
27
28 return result;
29 };
30
31 const origResolveHandleDns = idResolver.handle.resolveDns;
32 idResolver.handle.resolve = async (handle: string) => {
33 const isPdsHandle = pds.ctx.cfg.identity.serviceHandleDomains.some(
34 (domain) => handle.endsWith(domain),
35 );
36
37 if (!isPdsHandle) {
38 return origResolveHandleDns.call(idResolver.handle, handle);
39 }
40
41 const url = `${pds.url}/.well-known/atproto-did`;
42 try {
43 const res = await axios.get(url, { headers: { host: handle } });
44 return res.data;
45 } catch (err) {
46 return undefined;
47 }
48 };
49};