// archive.today's ANSWERS, read (release 37 lane F). Pure: no network, no disk, // no clock — the client (archiveTodayClient.ts) fetches, this reads. // // - its TIMEMAP (the Memento protocol's link format) as capture rows; // - a copy's FACTS from its answer's headers (Memento-Datetime, Link // rel="original") or, when a copy was read through a browser and has no // headers, from the copy's own header bar; // - its CHECK PAGE (a CAPTCHA, "one more step", a browser check) told apart // from a copy, so a check is a typed refusal and never a parsed record; // - a copy UNWRAPPED: archive.today serves its snapshot rewritten — its own // header bar on top, the page inside `#CONTENT`, links out of it as // `/o//`, images as its own cached copies (the original kept as // `old-src` where it keeps it). Unwrapping takes the bar off, puts the // original hrefs (and image srcs) back, and keeps the title and the page's // own s, so the extractors (generic, and the site ones) read it the // way they read a raw Wayback body. The images a record names afterwards // are archive.today's cached copies (`mapArchiveTodayMedia`). // // The layout is archive.today's as known on 2026-10-20 (no live copy was read // to write this: synthetic fixtures only). The stored body is the copy AS // SERVED, so a better unwrap re-derives every record offline (reextract-pages). import { isArchiveTodayHost, timestampOfDate, unwrapArchiveTodayOutlink } from "./mementos"; import { parseWaybackUrl } from "../wayback"; import type { PostMedia } from "../posts"; import type { ArchiveCaptureRow } from "./types"; // ─── The timemap ─── export type LinkEntry = { url: string; params: Record }; // RFC 6690 link format: `; rel="…"; datetime="…", ; …`. URLs may // hold commas, so entries are read by their `<…>` brackets, not split. export function parseLinkFormat(text: string): LinkEntry[] { const out: LinkEntry[] = []; const re = /<([^>]*)>((?:\s*;\s*[A-Za-z][\w-]*\s*=\s*(?:"[^"]*"|[^;,<]*))*)/g; for (const m of text.matchAll(re)) { const params: Record = {}; for (const p of m[2].matchAll(/;\s*([A-Za-z][\w-]*)\s*=\s*(?:"([^"]*)"|([^;,<]*))/g)) { params[p[1].toLowerCase()] = (p[2] ?? p[3] ?? "").trim(); } out.push({ url: m[1].trim(), params }); } return out; } const relsOf = (e: LinkEntry) => (e.params.rel ?? "").toLowerCase().split(/\s+/).filter(Boolean); // A timemap's copies, oldest first. The original is the entry rel="original" // (else `fallbackOriginal`). export function parseArchiveTodayTimemap(text: string, fallbackOriginal: string): ArchiveCaptureRow[] { const entries = parseLinkFormat(text); const original = entries.find((e) => relsOf(e).includes("original"))?.url || fallbackOriginal; const rows: ArchiveCaptureRow[] = []; const seen = new Set(); for (const e of entries) { if (!relsOf(e).includes("memento")) continue; const ts = timestampOfDate(e.params.datetime); if (!ts || seen.has(e.url)) continue; seen.add(e.url); rows.push({ source: "archive.today", captureTs: ts, originalUrl: original, mementoUrl: e.url, status: "200", mimetype: "text/html" }); } return rows.sort((a, b) => a.captureTs.localeCompare(b.captureTs) || a.mementoUrl.localeCompare(b.mementoUrl)); } // The rows within capture-time prefixes `from`/`to` (inclusive), like CDX's. export function withinCaptureRange(ts: string, from?: string, to?: string): boolean { if (from && ts.slice(0, from.length) < from) return false; if (to && ts.slice(0, to.length) > to) return false; return true; } // A 4–14 digit timestamp as epoch ms (the missing units the start of the // period); NaN for one that is not a time. function tsMs(ts: string): number { const p = ts.padEnd(14, "0"); return Date.UTC(+p.slice(0, 4), Math.max(0, +p.slice(4, 6) - 1), Math.max(1, +p.slice(6, 8)), +p.slice(8, 10), +p.slice(10, 12), +p.slice(12, 14)); } // The row nearest `at` (4–14 digits) IN TIME — not by the digits, where // 20191201 is "nearer" 20190512 than 20200101 — the earlier of two as near. export function nearestCapture(rows: readonly T[], at: string): T | null { const want = tsMs(at); let best: T | null = null; let bestD = Infinity; for (const r of rows) { const d = Math.abs(tsMs(r.captureTs) - want); if (d < bestD || (d === bestD && best && r.captureTs < best.captureTs)) { best = r; bestD = d; } } return best; } // ─── A copy's facts ─── export type CopyFacts = { captureTs?: string; originalUrl?: string }; // From the answer's headers: Memento-Datetime, and the Link header's // rel="original". export function copyFactsFromHeaders(headers: { get(name: string): string | null }): CopyFacts { const out: CopyFacts = {}; const ts = timestampOfDate(headers.get("memento-datetime")); if (ts) out.captureTs = ts; const link = headers.get("link"); if (link) { const orig = parseLinkFormat(link).find((e) => relsOf(e).includes("original"))?.url; if (orig && /^https?:\/\//i.test(orig)) out.originalUrl = orig; } return out; } const attr = (tag: string, name: string): string | undefined => { const m = new RegExp(`\\s${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, "i").exec(tag); return m ? decodeEntities(m[1] ?? m[2] ?? m[3] ?? "") : undefined; }; function decodeEntities(s: string): string { return s .replace(/"/g, '"') .replace(/'|'/gi, "'") .replace(/</g, "<") .replace(/>/g, ">") .replace(/&/g, "&"); } // From the copy's own header bar (a copy read through a browser has no // headers): the "saved from" URL box and the capture's