// A WAYBACK PAGES CHANNEL (release 37, plans/release-37/wayback-pages.md, "What // a channel is"): a named scope of Wayback Machine captures — a channel on // platform "webpage", `sourceKind: "social"`, whose records are page records // (Post.page). `config.wayback` says which captures are in scope: one or more // targets, each a URL and how it matches (one page, a path prefix, a host, a // whole domain), optionally bounded by capture date. // // Client-safe and a LEAF (no imports): lib/channelConfig.ts composes the // coercion below into its one set of coercions, and the channel form imports // both. export const WAYBACK_TARGET_MATCHES = ["exact", "prefix", "host", "domain"] as const; export type WaybackTargetMatch = (typeof WAYBACK_TARGET_MATCHES)[number]; export type WaybackTarget = { // The page, path prefix, host or domain, as the CDX API takes it (a scheme // is optional; for "host" and "domain" only the host counts). url: string; match: WaybackTargetMatch; // Capture-time bounds, a Wayback timestamp prefix (4–14 digits: // yyyy[MM[dd[hh[mm[ss]]]]]), inclusive. from?: string; to?: string; // Mimetypes kept (default: text/html). mime?: string[]; // Regex sources over a capture's original URL: kept when it matches // `include` (if set) and not `exclude`. include?: string; exclude?: string; // Release 37 lane F: the archive sources that hold this target's copies // ("wayback", "archive.today"; lib/archives/). Absent = ["wayback"]. sources?: WaybackTargetSource[]; }; // The archive sources a target may name (lib/archives/types.ts // ARCHIVE_SOURCE_IDS; repeated here because this file is a leaf). export const WAYBACK_TARGET_SOURCES = ["wayback", "archive.today"] as const; export type WaybackTargetSource = (typeof WAYBACK_TARGET_SOURCES)[number]; // The sources a target's copies are looked for on: its own, else the // Wayback Machine alone. export function targetSources(target: Pick): WaybackTargetSource[] { return target.sources?.length ? [...target.sources] : ["wayback"]; } export type WaybackPagesConfig = { targets: WaybackTarget[]; // How a scan folds repeated captures: "digest" = one row per distinct body. collapse?: "digest"; // The download lane imports the captures a later scan adds (release 37 S2, // controller/laneWaybackAuto.ts). Off unless true. auto?: boolean; }; const TS_PREFIX_RE = /^\d{4,14}$/; const trimmed = (v: unknown): string | undefined => typeof v === "string" && v.trim() ? v.trim() : undefined; export function coerceWaybackTarget(v: unknown): WaybackTarget | undefined { if (!v || typeof v !== "object" || Array.isArray(v)) return undefined; const r = v as Record; const url = trimmed(r.url); if (!url || /\s/.test(url)) return undefined; const match = (WAYBACK_TARGET_MATCHES as readonly string[]).includes(r.match as string) ? (r.match as WaybackTargetMatch) : undefined; if (!match) return undefined; const out: WaybackTarget = { url, match }; const from = trimmed(r.from); if (from && TS_PREFIX_RE.test(from)) out.from = from; const to = trimmed(r.to); if (to && TS_PREFIX_RE.test(to)) out.to = to; if (Array.isArray(r.mime)) { const mime = r.mime .map((m) => (typeof m === "string" ? m.trim().toLowerCase() : "")) .filter((m) => /^[a-z0-9.+-]+\/[a-z0-9.+*-]+$/.test(m)); if (mime.length > 0) out.mime = [...new Set(mime)]; } const include = trimmed(r.include); if (include) out.include = include; const exclude = trimmed(r.exclude); if (exclude) out.exclude = exclude; if (Array.isArray(r.sources)) { const sources = [...new Set(r.sources.filter((x): x is WaybackTargetSource => (WAYBACK_TARGET_SOURCES as readonly unknown[]).includes(x)))]; // ["wayback"] alone is the default: not written. if (sources.length > 0 && !(sources.length === 1 && sources[0] === "wayback")) out.sources = sources; } return out; } // `config.wayback`: an object with at least one valid target, else omitted. // Invalid targets are dropped one by one, never repaired. export function coerceWaybackPagesConfig(v: unknown): WaybackPagesConfig | undefined { if (!v || typeof v !== "object" || Array.isArray(v)) return undefined; const r = v as Record; if (!Array.isArray(r.targets)) return undefined; const targets = r.targets.map(coerceWaybackTarget).filter((t): t is WaybackTarget => !!t); if (targets.length === 0) return undefined; const out: WaybackPagesConfig = { targets }; if (r.collapse === "digest") out.collapse = "digest"; if (r.auto === true) out.auto = true; return out; } // A Wayback pages channel: a posts channel on platform "webpage". Structural, // so this leaf needs no import of the channel config's type. export function isWaybackPagesChannel( config: { sourceKind?: string; platform?: string } | null | undefined, ): boolean { return config?.sourceKind === "social" && config.platform === "webpage"; } const WAYBACK_HOST_RE = /^(?:web|wayback)\.archive\.org$/i; // The Wayback Machine's own LISTING forms name a scope, not one capture: // web.archive.org/web/*/example.com/blog* → prefix example.com/blog // web.archive.org/web/*/*.example.com* → the domain example.com // web.archive.org/web/*/example.com/about → exactly that page // web.archive.org/web/2014*/example.com* → a prefix, captures of 2014 // and a capture URL (web.archive.org/web//) names one page, // exactly. A URL that is not on the Wayback Machine is taken as the target // itself: a trailing `*` makes it a prefix, a leading `*.` a domain. // Null for anything that names no target. // Release 37 lane F: an archive.today copy that names its page // (archive.ph//, /newest/, /oldest/) is that page, // exactly, on archive.today; a short id (archive.ph/) names no page. const ARCHIVE_TODAY_COPY_RE = /^https?:\/\/(?:www\.)?archive\.(?:ph|today|is|li|vn|fo|md)\/(?:newest|oldest|\d{4,14}|\d{4}\.\d{2}\.\d{2}(?:-\d{6})?)\/(.+)$/i; export function waybackTargetFromUrl(input: string): WaybackTarget | null { const raw = input.trim(); if (!raw) return null; const at = ARCHIVE_TODAY_COPY_RE.exec(raw); if (at) { const inner = waybackTargetFromUrl(at[1].replace(/^(https?):\/(?!\/)/i, "$1://")); return inner ? { url: inner.url, match: "exact", sources: ["archive.today"] } : null; } let rest = raw; let from: string | undefined; let to: string | undefined; let u: URL | null = null; try { u = new URL(raw); } catch { u = null; } if (u && WAYBACK_HOST_RE.test(u.hostname)) { const m = /^\/(?:web\/)?(\d{0,14})(\*?)(?:[a-z]{2}_)?\/(.+)$/s.exec(u.pathname + u.search); if (!m) return null; const [, digits, star, original] = m; rest = original; if (star && digits) { from = digits; to = digits; } // A capture URL (digits, no star) is one page at one time; its target is // the page. } rest = rest.replace(/^(https?):\/(?!\/)/i, "$1://"); let match: WaybackTargetMatch = "exact"; if (rest.endsWith("*")) { match = "prefix"; rest = rest.replace(/\*+$/, ""); } const bare = rest.replace(/^https?:\/\//i, ""); if (bare.startsWith("*.")) { const host = bare.slice(2).split(/[/?#]/)[0]; if (!host || !host.includes(".")) return null; return { url: host.toLowerCase(), match: "domain", ...(from ? { from } : {}), ...(to ? { to } : {}) }; } const host = bare.split(/[/?#]/)[0]; if (!host || !host.includes(".") || /\s/.test(rest)) return null; return { url: rest, match, ...(from ? { from } : {}), ...(to ? { to } : {}) }; } // The host a target names (no "www."), for a channel's handle. export function waybackTargetHost(target: Pick): string { const bare = target.url.replace(/^https?:\/\//i, ""); return bare.split(/[/?#:]/)[0].toLowerCase().replace(/^www\./, ""); }