// WHICH SCAN ROWS ARE ALREADY IMPORTED (release 37 S2). Derived, never stored // twice: a row of wayback-scan.json is imported when the channel holds a // webpage record (a Post with `page`) of the same original URL whose capture is // that content — the same digest, or, for a record that names no digest, a // capture time inside the row's first..last seen. Pure, client-safe. import { normalHost, type WaybackScanRow } from "./scanFile"; // The `page` facts this needs (PagePostInfo's provenance half + digest). export type ImportedPageFacts = { originalUrl: string; captureTs: string; digest?: string; waybackUrl?: string; }; export type ImportedRecordRef = { id: string; captureTs: string; waybackUrl?: string; }; export type ImportedIndex = Map>; // One spelling per page the Wayback Machine treats as one: no scheme, the host // lowercased without www. or a default port, path and query as captured. export function pageUrlKey(url: string): string { try { const u = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(url) ? url : `http://${url}`); return `${normalHost(u.host)}${u.pathname}${u.search}`; } catch { return url; } } export function buildImportedIndex( posts: ReadonlyArray<{ id: string; page?: ImportedPageFacts | null }>, ): ImportedIndex { const index: ImportedIndex = new Map(); for (const p of posts) { const page = p.page; if (!page || typeof page.originalUrl !== "string" || typeof page.captureTs !== "string") continue; const key = pageUrlKey(page.originalUrl); const list = index.get(key) ?? []; list.push({ id: p.id, captureTs: page.captureTs, ...(page.digest ? { digest: page.digest } : {}), ...(page.waybackUrl ? { waybackUrl: page.waybackUrl } : {}), }); index.set(key, list); } for (const list of index.values()) list.sort((a, b) => a.captureTs.localeCompare(b.captureTs) || a.id.localeCompare(b.id)); return index; } // The first record that holds this row's content, or null. export function importedRecordOf( row: Pick, index: ImportedIndex, ): ImportedRecordRef | null { const list = index.get(pageUrlKey(row.originalUrl)); if (!list) return null; for (const r of list) { const hit = r.digest ? r.digest === row.digest : r.captureTs >= row.firstTs && r.captureTs <= row.lastTs; if (hit) return { id: r.id, captureTs: r.captureTs, ...(r.waybackUrl ? { waybackUrl: r.waybackUrl } : {}) }; } return null; }