import { createHash } from "node:crypto"; import { Lexicons, lexiconDoc } from "@atproto/lexicon"; import { CID } from "multiformats/cid"; import episodeLexicon from "../lexicons/place/pod/episode.json"; import showLexicon from "../lexicons/place/pod/show.json"; import { HttpError } from "./errors"; import type { EpisodeRecord, RecordEnvelope, ShowRecord } from "./types"; const lexicons = new Lexicons([episodeLexicon, showLexicon].map((schema) => lexiconDoc.parse(schema))); const DID = /^(?:did:plc:[a-z2-7]{24}|did:web:[A-Za-z0-9._:%-]+)$/; const ID = /^[A-Za-z0-9_-]{43}$/; export function parseRecordUri(uri: unknown, collection?: "place.pod.show" | "place.pod.episode") { if (typeof uri !== "string" || uri.length > 2048) throw new HttpError(400, "invalid_uri", "Expected a DID-based podcast AT record URI"); const match = /^at:\/\/([^/]+)\/(place\.pod\.(?:show|episode))\/([A-Za-z0-9._~:-]{1,512})$/.exec(uri); if (!match || !DID.test(match[1]) || match[3] === "." || match[3] === ".." || (collection && match[2] !== collection)) { throw new HttpError(400, "invalid_uri", "Expected a DID-based podcast AT record URI"); } return { did: match[1], collection: match[2], rkey: match[3] }; } export function isValidCid(value: unknown): value is string { if (typeof value !== "string" || value.length > 128) return false; try { const cid = CID.parse(value); return cid.version === 1 && cid.code === 0x71 && cid.toString() === value; } catch { return false; } } export function isResolverId(value: string): boolean { return ID.test(value); } export function showId(showUri: string): string { return createHash("sha256").update(showUri).digest("base64url"); } export function resolverId(showUri: string, guid: string): string { return createHash("sha256").update(JSON.stringify([showUri, guid])).digest("base64url"); } export function resolverUrl(baseUrl: string, id: string): string { return `${baseUrl}/e/${id}`; } export function parseResolverPath(pathname: string): string | null { const match = /^\/e\/([A-Za-z0-9_-]{43})$/.exec(pathname); return match?.[1] ?? null; } function validateRecord(envelope: RecordEnvelope, type: string): void { if (!isValidCid(envelope.cid)) throw new HttpError(502, "invalid_record_response", "Record source returned an invalid CID"); try { lexicons.assertValidRecord(type, envelope.value); } catch { throw new HttpError(422, "invalid_record", "AT record does not satisfy the podcast lexicon"); } if ((envelope.value as Record).$type !== type) { throw new HttpError(422, "invalid_record", "Unexpected AT record type"); } } export function validateShow(envelope: RecordEnvelope): ShowRecord { parseRecordUri(envelope.uri, "place.pod.show"); validateRecord(envelope, "place.pod.show"); return envelope.value as ShowRecord; } export function validateEpisode(envelope: RecordEnvelope, baseUrl: string): EpisodeRecord { const episode = parseRecordUri(envelope.uri, "place.pod.episode"); validateRecord(envelope, "place.pod.episode"); const record = envelope.value as EpisodeRecord; const show = parseRecordUri(record.showUri, "place.pod.show"); if (show.did !== episode.did) throw new HttpError(422, "invalid_owner", "Pilot episodes must be stored in the publisher's show repository"); if (!record.guid || !record.media) throw new HttpError(422, "missing_media", "Resolver registration requires guid and media.primary"); const media = record.media; const url = resolverUrl(baseUrl, resolverId(record.showUri, record.guid)); if (record.audioUrl !== url || media.url !== url || record.audioMimeType !== media.type || record.audioSizeBytes !== media.length || media.primary.type !== media.type || media.primary.length !== media.length || !Number.isSafeInteger(media.length) || !/^audio\/[a-z0-9!#$&^_.+-]+$/i.test(media.type)) { throw new HttpError(422, "invalid_enclosure", "Resolver identity and enclosure metadata must agree"); } if (media.source && (!Number.isSafeInteger(media.source.length) || !/^audio\/[a-z0-9!#$&^_.+-]+$/i.test(media.source.type))) { throw new HttpError(422, "invalid_source", "Source media requires a safe byte length and audio MIME type"); } const hash = media.source?.contentHash; if (hash && !/^[a-f0-9]{64}$/i.test(hash.value)) { throw new HttpError(422, "invalid_source_hash", "Source contentHash must be a hexadecimal SHA-256 digest"); } return record; }