Something went wrong. Try again.
Stable episode delivery and analytics on pod.place
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import { HttpError, RecordNotFound } from "./errors";import { isValidCid, parseRecordUri } from "./validation";import type { RecordEnvelope, RecordReader } from "./types";
type Fetcher = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
// Slingshot is the configured trusted read layer. This client checks the response// identity/shape; it does not independently verify AT repository commit signatures.export class SlingshotReader implements RecordReader { constructor( private readonly endpoint = "https://slingshot.microcosm.blue", private readonly fetcher: Fetcher = fetch, ) { const url = new URL(endpoint); if (url.protocol !== "https:" || url.username || url.password || url.pathname !== "/" || url.search || url.hash) { throw new Error("SLINGSHOT_URL must be a trusted HTTPS origin"); } }
async get(uri: string): Promise<RecordEnvelope> { const record = parseRecordUri(uri); const url = new URL("/xrpc/com.atproto.repo.getRecord", this.endpoint); url.searchParams.set("repo", record.did); url.searchParams.set("collection", record.collection); url.searchParams.set("rkey", record.rkey); let response: Response; try { response = await this.fetcher(url, { redirect: "error", signal: AbortSignal.timeout(10_000) }); } catch { throw new HttpError(503, "record_source_unavailable", "Trusted record source unavailable"); } if (response.status === 404) { await response.body?.cancel(); throw new RecordNotFound(); } if (!response.ok && response.status !== 400) { await response.body?.cancel(); throw new HttpError(503, "record_source_unavailable", "Trusted record source unavailable"); } if (Number(response.headers.get("content-length") ?? 0) > 1_048_576) { await response.body?.cancel(); throw new HttpError(502, "invalid_record_response", "Record response exceeds size limit"); } const chunks: Uint8Array<ArrayBuffer>[] = []; let size = 0; const reader = response.body?.getReader(); if (!reader) throw new HttpError(502, "invalid_record_response", "Missing record response"); try { while (true) { const { value, done } = await reader.read(); if (done) break; size += value.byteLength; if (size > 1_048_576) { await reader.cancel(); throw new HttpError(502, "invalid_record_response", "Record response exceeds size limit"); } chunks.push(new Uint8Array(value)); } } catch (error) { if (error instanceof HttpError) throw error; throw new HttpError(503, "record_source_unavailable", "Trusted record response interrupted"); } let body: RecordEnvelope; try { body = JSON.parse(await new Blob(chunks).text()); } catch { throw new HttpError(502, "invalid_record_response", "Invalid record response"); } if (!response.ok) { // AT XRPC uses HTTP 400 with RecordNotFound, not necessarily HTTP 404. if ((body as unknown as { error?: string })?.error === "RecordNotFound") throw new RecordNotFound(); throw new HttpError(503, "record_source_unavailable", "Trusted record source unavailable"); } if (!body || body.uri !== uri || !isValidCid(body.cid) || !body.value || typeof body.value !== "object") { throw new HttpError(502, "invalid_record_response", "Record source returned a mismatched or invalid record"); } return body; }}