pdsOf

tcmts.atproto.pdsOf · #z7rm3pab · term · runs on the server

uses tcmts.atproto.localNetwork, which uses process

A DID document's PDS (#atproto_pds), as an origin to put paths after: https (http on a local network), and nothing but a scheme and a host (no path, query or credentials, which would let a document choose where tcm's requests go on a host). Throws if there's none, or it's anything else.

import { localNetwork, Refused } from "#codebase/tcmts/atproto";
import type { DidDocument } from "#codebase/tcmts/atproto";

export function pdsOf(doc: DidDocument, did: string): string {
  const endpoint = doc.service?.find(
    (s) => s.id.endsWith("#atproto_pds") && s.type === "AtprotoPersonalDataServer",
  )?.serviceEndpoint;
  if (!endpoint) throw new Refused(`${did} has no PDS in its DID document`);
  let u: URL;
  try {
    u = new URL(endpoint);
  } catch {
    throw new Refused(`${did}'s PDS isn't a URL: ${endpoint}`);
  }
  if (u.protocol !== "https:" && !(localNetwork() && u.protocol === "http:"))
    throw new Refused(`${did}'s PDS must be https: ${endpoint}`);
  if (
    u.username ||
    u.password ||
    /[?#]/.test(endpoint) ||
    (u.pathname !== "/" && u.pathname !== "")
  )
    throw new Refused(`${did}'s PDS must be a server's address alone: ${endpoint}`);
  return u.origin;
}

uses

used by