// A WAYBACK PAGES CHANNEL'S SCOPE (release 37 S2): `config.wayback` — the // targets whose captures the channel is about, and whether the download lane // imports new ones by itself. Pure and client-safe: no I/O. // // config.wayback = { // targets: [{url, match: "exact"|"prefix"|"host"|"domain", from?, to?, // mime?: ["text/html"], include?, exclude?}], // collapse?: "digest", // auto?: boolean, // } // // The reader is the schema's (lib/waybackPages/config.ts coerceWaybackPagesConfig): // a target without a usable `url` or `match` is dropped, a date that is not // 4–14 digits is dropped; here also a regex that does not compile. A channel // with no usable target is not a Wayback pages channel. import { coerceWaybackPagesConfig, WAYBACK_TARGET_MATCHES, type WaybackTarget, type WaybackTargetMatch } from "./config"; // The schema's own target (lib/waybackPages/config.ts, `config.wayback`). export type { WaybackTarget }; export const WAYBACK_MATCH_TYPES = WAYBACK_TARGET_MATCHES; export type WaybackMatchType = WaybackTargetMatch; // The mimetypes a target asks for when it names none. export const DEFAULT_WAYBACK_MIMES: readonly string[] = ["text/html"]; // What a scan needs of `config.wayback`: the targets (the schema's), and // whether the download lane imports new rows. export type WaybackChannelConfig = { targets: WaybackTarget[]; collapse: "digest"; // The download lane imports new scan rows for this channel (default off: no // unattended crawling of a host unless the channel opts in). auto: boolean; }; function regexOk(src: string | undefined): string | undefined { if (!src) return undefined; try { new RegExp(src); return src; } catch { return undefined; } } // One target through the schema's coercion, its include/exclude kept only // when they compile (a regex that does not is no filter at all). export function parseWaybackTarget(raw: unknown): WaybackTarget | null { const c = coerceWaybackPagesConfig({ targets: [raw] }); const t = c?.targets[0]; if (!t) return null; const { include, exclude, ...rest } = t; const inc = regexOk(include); const exc = regexOk(exclude); return { ...rest, ...(inc ? { include: inc } : {}), ...(exc ? { exclude: exc } : {}) }; } // `config.wayback` of a channel config (or a raw config.json object), or null // when the channel names no usable target. export function waybackConfigOf(config: unknown): WaybackChannelConfig | null { if (!config || typeof config !== "object") return null; const w = (config as { wayback?: unknown }).wayback; const c = coerceWaybackPagesConfig(w); if (!c) return null; const targets = c.targets.map(parseWaybackTarget).filter((t): t is WaybackTarget => t !== null); if (targets.length === 0) return null; return { targets, collapse: "digest", auto: c.auto === true }; } export function isWaybackPagesConfig(config: unknown): boolean { return waybackConfigOf(config) !== null; } // The identity of a target in the scan file: what it ASKS the CDX API. A // target whose url, match, bounds or mimes change is a new target (scanned from // its start); include/exclude only filter what is kept. export function waybackTargetKey(t: WaybackTarget): string { return JSON.stringify([t.match, t.url, t.from ?? "", t.to ?? "", (t.mime ?? DEFAULT_WAYBACK_MIMES).join(",")]); } // The `url` the CDX API takes for a target: a host or a domain without its // scheme or path. export function cdxUrlParam(t: WaybackTarget): string { if (t.match === "host" || t.match === "domain") { try { return new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(t.url) ? t.url : `http://${t.url}`).hostname; } catch { return t.url; } } return t.url; } function escapeRegex(s: string): string { return s.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&"); } // The CDX `filter=mimetype:` regex for a target's mimes. export function cdxMimeFilter(t: WaybackTarget): string { return `mimetype:${(t.mime ?? DEFAULT_WAYBACK_MIMES).map(escapeRegex).join("|")}`; } // Obvious non-page assets, by the path's extension: dropped from a scan unless // the target's `include` names them. const ASSET_EXT_RE = /\.(?:css|js|mjs|map|png|jpe?g|gif|webp|avif|svg|ico|bmp|tiff?|woff2?|ttf|otf|eot|mp4|m4v|webm|mov|mp3|m4a|ogg|wav|flac|zip|gz|tgz|rar|7z|pdf|swf|flv|exe|dmg|iso)$/i; export function isAssetUrl(originalUrl: string): boolean { let p = originalUrl; try { p = new URL(originalUrl).pathname; } catch { p = originalUrl.split(/[?#]/)[0] ?? originalUrl; } return ASSET_EXT_RE.test(p); } // Whether a capture of `originalUrl` belongs in the target's scan. export function keepCaptureUrl(t: WaybackTarget, originalUrl: string): boolean { if (t.exclude && new RegExp(t.exclude).test(originalUrl)) return false; if (t.include) return new RegExp(t.include).test(originalUrl); return !isAssetUrl(originalUrl); }