serviceAudiences

tcmts.atproto.serviceAudiences · #ebr474sk · function · runs on the server

uses process

Who the app's server is to browsers signing in to it: the did:webs of $ATPROTO_SERVICE_URL (its addresses, space-separated, as configure's serviceUrls may say instead); with none set, the localhost address this request came to, as the browser asked it (for an app on this machine: tcm dev, or behind a proxy there).

import { ATPROTO_CONFIG, localNetwork, serviceDid } from "#codebase/tcmts/atproto";
import { request } from "#codebase/tcmts/atproto/lib/server";

export function serviceAudiences(): string[] {
  const urls =
    ATPROTO_CONFIG.serviceUrls ??
    (process.env.ATPROTO_SERVICE_URL ?? "").split(/[\s,]+/).filter(Boolean);
  if (urls.length) return urls.map(serviceDid);
  // (as the browser asked: behind a proxy, the host it names; taken only for this machine's own addresses)
  const req = request();
  // (a server function's request has only a path: its host is the Host it was asked with)
  const url = new URL(req.url, `http://${req.headers.get("host") ?? "localhost"}`);
  const host = req.headers.get("x-forwarded-host")?.split(",")[0]!.trim() || url.host;
  const here = new URL(`${url.protocol}//${host}`);
  return ["localhost", "127.0.0.1", "[::1]"].includes(here.hostname) || localNetwork()
    ? [serviceDid(here.origin)]
    : [];
}

uses

used by