// THE CDX LISTING A WAYBACK PAGES SCAN ASKS FOR (release 37 S2): one target's // captures, paged with the API's resume key, one row per distinct content // (`collapse=digest`), only those that recorded a 200, only the target's // mimetypes, inside its date bounds. Pure: the URL and the parse; the request // is the Wayback client's (lib/waybackClient.ts `cdxPage`), paced. // // The CDX server's JSON answer with `showResumeKey=true`: a header row, the // rows, and — when more remain — an empty row then `[]`. import { WAYBACK_ORIGIN } from "../waybackClient"; import { cdxMimeFilter, cdxUrlParam, type WaybackTarget } from "./targets"; // Rows asked for per request. export const CDX_PAGE_ROWS = 5_000; // Rows read per run, all targets together, unless the run asks for fewer. export const DEFAULT_SCAN_MAX_ROWS = 50_000; // The hard ceiling on a run's `maxRows`. export const SCAN_MAX_ROWS_LIMIT = 200_000; // The columns asked for, in this order. export const CDX_FIELDS = ["original", "timestamp", "digest", "mimetype", "statuscode"] as const; export type CdxQuery = { target: WaybackTarget; limit: number; resumeKey?: string; // A later pass's lower bound (YYYYMMDDhhmmss), when later than the target's // own `from`. from?: string; }; export function scanCdxUrl(q: CdxQuery, origin: string = WAYBACK_ORIGIN): string { const t = q.target; const p = new URLSearchParams(); p.set("url", cdxUrlParam(t)); p.set("matchType", t.match); p.set("output", "json"); p.set("fl", CDX_FIELDS.join(",")); p.append("filter", "statuscode:200"); p.append("filter", cdxMimeFilter(t)); p.set("collapse", "digest"); const from = laterTs(t.from, q.from); if (from) p.set("from", from); if (t.to) p.set("to", t.to); p.set("limit", String(q.limit)); p.set("showResumeKey", "true"); if (q.resumeKey) p.set("resumeKey", q.resumeKey); return `${origin}/cdx/search/cdx?${p.toString()}`; } // The later of two CDX bounds (prefix timestamps compare by padding). function laterTs(a: string | undefined, b: string | undefined): string | undefined { if (!a) return b; if (!b) return a; return a.padEnd(14, "0") >= b.padEnd(14, "0") ? a : b; } export type CdxScanRow = { originalUrl: string; timestamp: string; digest: string; mimetype: string; status: number; }; export type CdxScanPage = { rows: CdxScanRow[]; // Present when the listing continues. resumeKey?: string; }; // One page of the answer. An empty body or `[]` is no rows; anything that is // not the JSON list shape is an error (the API changed, or answered a page). export function parseScanCdxPage(body: string): CdxScanPage { const text = body.trim(); if (!text) return { rows: [] }; let raw: unknown; try { raw = JSON.parse(text); } catch { throw new Error(`the CDX API did not answer JSON (${text.slice(0, 80)})`); } if (!Array.isArray(raw)) throw new Error("the CDX API's answer is not a list"); if (raw.length === 0) return { rows: [] }; const list = raw as unknown[]; let resumeKey: string | undefined; // The tail: [] then [key]. const last = list[list.length - 1]; const beforeLast = list[list.length - 2]; let end = list.length; if (Array.isArray(last) && last.length === 1 && typeof last[0] === "string" && Array.isArray(beforeLast) && beforeLast.length === 0) { resumeKey = last[0] || undefined; end -= 2; } const header = list[0]; if (!Array.isArray(header)) throw new Error("the CDX API's answer has no header row"); const col = (name: string) => (header as unknown[]).indexOf(name); const ci = { original: col("original"), timestamp: col("timestamp"), digest: col("digest"), mimetype: col("mimetype"), statuscode: col("statuscode") }; if (ci.original < 0 || ci.timestamp < 0) throw new Error("the CDX API's header names no timestamp or original"); const rows: CdxScanRow[] = []; for (const row of list.slice(1, end)) { if (!Array.isArray(row) || row.length === 0) continue; const timestamp = String(row[ci.timestamp] ?? ""); const originalUrl = String(row[ci.original] ?? ""); if (!/^\d{14}$/.test(timestamp) || !originalUrl) continue; const digest = ci.digest >= 0 ? String(row[ci.digest] ?? "") : ""; const status = ci.statuscode >= 0 ? Number(row[ci.statuscode]) : 200; rows.push({ originalUrl, timestamp, digest: digest || `ts-${timestamp}`, mimetype: ci.mimetype >= 0 ? String(row[ci.mimetype] ?? "") : "", status: Number.isFinite(status) ? status : 0, }); } return { rows, ...(resumeKey ? { resumeKey } : {}) }; }