// WHAT A MEMENTO URL SAYS (release 37 lane F), for every archive source, with // no request: which source a URL is a copy on, the page and time it names, and // how a record links its copy. Client-safe (no node imports): the channel form // and the channel page read it too. // // archive.today (one service under several names: archive.ph, archive.today, // archive.is, archive.li, archive.vn, archive.fo, archive.md) names a copy as // https://archive.ph/ a copy, by its short id (AbCdE) // https://archive.ph// the copy of nearest // (: YYYYMMDDhhmmss, or YYYY.MM.DD-hhmmss) // https://archive.ph/newest/ its newest copy // https://archive.ph/oldest/ its oldest copy // and lists them at /timemap/ (the Memento protocol's link format). // `/o//` is a link OUT of a copy (to ), and a bare // `/` or `/search/` a listing: neither is a copy. import { parseWaybackUrl, waybackPageUrl } from "../wayback"; import type { ArchiveMementoRef, ArchiveSourceId } from "./types"; export const ARCHIVE_TODAY_HOSTS = [ "archive.ph", "archive.today", "archive.is", "archive.li", "archive.vn", "archive.fo", "archive.md", ] as const; export const DEFAULT_ARCHIVE_TODAY_HOST = "archive.ph"; export const ARCHIVE_SOURCE_LABELS: Record = { wayback: "Wayback Machine", "archive.today": "archive.today", }; export function archiveSourceLabel(id: string | undefined): string { return ARCHIVE_SOURCE_LABELS[(id ?? "wayback") as ArchiveSourceId] ?? id ?? "Wayback Machine"; } export function isArchiveTodayHost(host: string): boolean { const h = host.toLowerCase().replace(/^www\./, ""); return (ARCHIVE_TODAY_HOSTS as readonly string[]).includes(h); } // The canonical host a setting names, or the default: one of the known names, // nothing else (a typo never sends requests somewhere new). export function archiveTodayHostOf(setting: unknown): string { const h = typeof setting === "string" ? setting.trim().toLowerCase().replace(/^www\./, "") : ""; return isArchiveTodayHost(h) ? h : DEFAULT_ARCHIVE_TODAY_HOST; } const SHORT_ID_RE = /^[A-Za-z0-9]{4,6}$/; // YYYYMMDDhhmmss (or a whole-unit prefix), or archive.today's dotted // YYYY.MM.DD-hhmmss (or YYYY.MM.DD). const TS_SEG_RE = /^(?:\d{4}(?:\d{2}){0,5}|\d{4}\.\d{2}\.\d{2}(?:-\d{6})?)$/; const RESERVED = new Set(["o", "timemap", "timegate", "search", "submit", "wip", "faq", "newest", "oldest", "about", "donate"]); // "2019.01.02-030405" → "20190102030405"; a digit timestamp as it is. export function archiveTodayTs(seg: string): string { return seg.replace(/[.-]/g, ""); } // An HTTP date (Memento-Datetime: "Tue, 01 Jan 2019 12:34:56 GMT") or an // ISO-8601 instant as a 14-digit UTC timestamp, or null. export function timestampOfDate(value: string | null | undefined): string | null { if (!value) return null; const ms = Date.parse(value.trim()); if (!Number.isFinite(ms)) return null; return new Date(ms).toISOString().replace(/[-:T]/g, "").slice(0, 14); } // A 14-digit timestamp as an HTTP date (what a timegate's Accept-Datetime takes). export function httpDateOfTimestamp(ts: string): string { const p = ts.padEnd(14, "0"); const ms = Date.UTC(+p.slice(0, 4), +p.slice(4, 6) - 1 || 0, +p.slice(6, 8) || 1, +p.slice(8, 10), +p.slice(10, 12), +p.slice(12, 14)); return new Date(ms).toUTCString(); } function originalOf(rest: string, search: string): string | null { let r = rest; if (/^https?%3a/i.test(r)) { try { r = decodeURIComponent(r); } catch { return null; } } r = r.replace(/^(https?):\/(?!\/)/i, "$1://"); if (!/^https?:\/\//i.test(r)) r = `http://${r}`; const out = `${r}${search}`; try { const u = new URL(out); if (!u.hostname.includes(".")) return null; if (isArchiveTodayHost(u.hostname)) return null; } catch { return null; } return out; } // An archive.today COPY's parts (on the canonical `host`), or null for // anything else: another host, a listing, a search, a link out of a copy. export function parseArchiveTodayUrl(url: string | null | undefined, host: string = DEFAULT_ARCHIVE_TODAY_HOST): ArchiveMementoRef | null { if (!url) return null; let u: URL; try { u = new URL(url.trim()); } catch { return null; } if (u.protocol !== "http:" && u.protocol !== "https:") return null; if (!isArchiveTodayHost(u.hostname)) return null; const canon = archiveTodayHostOf(host); const path = u.pathname.replace(/^\/+/, ""); const segs = path.split("/"); const first = segs[0] ?? ""; if (!first) return null; if (first === "newest" || first === "oldest") { const original = originalOf(segs.slice(1).join("/"), u.search); if (!original) return null; return { source: "archive.today", mementoUrl: `https://${canon}/${first}/${original}`, originalUrl: original, pick: first }; } if (TS_SEG_RE.test(first) && segs.length > 1) { const original = originalOf(segs.slice(1).join("/"), u.search); if (!original) return null; const ts = archiveTodayTs(first); return { source: "archive.today", mementoUrl: `https://${canon}/${ts}/${original}`, originalUrl: original, captureTs: ts }; } if (RESERVED.has(first.toLowerCase())) return null; // `/`, `//`, and the copy's own sub-pages (`//image`) name the copy. if (SHORT_ID_RE.test(first) && (segs.length === 1 || segs.slice(1).every((s) => s === "" || /^[a-z]+$/.test(s)))) { return { source: "archive.today", mementoUrl: `https://${canon}/${first}`, id: first }; } return null; } // Any URL on an archive.today host (a copy, a listing, a search). export function isArchiveTodayUrl(url: string): boolean { try { return isArchiveTodayHost(new URL(url.trim()).hostname); } catch { return false; } } // `/o//` (a link out of an archive.today copy) as ; any other // URL as given. export function unwrapArchiveTodayOutlink(href: string): string { const m = /^(?:https?:)?\/\/(?:www\.)?(?:archive\.(?:ph|today|is|li|vn|fo|md))\/o\/[A-Za-z0-9]+\/(.+)$/i.exec(href.trim()) ?? /^\/o\/[A-Za-z0-9]+\/(https?:\/\/.+)$/i.exec(href.trim()); if (!m) return href; return m[1].replace(/^(https?):\/(?!\/)/i, "$1://"); } // Which source a URL is a COPY on, or null (a listing, an original page). export function mementoSourceOf(url: string): ArchiveSourceId | null { if (parseWaybackUrl(url)) return "wayback"; if (parseArchiveTodayUrl(url)) return "archive.today"; return null; } // THE LINK A RECORD'S COPY IS CITED BY (lane D: shots, the moment page, // citation labels, the MCP): `page.archiveUrl` for a record from a source that // is not the Wayback Machine, else its Wayback replay URL — and the source's // name for the label. export function pageArchiveLink(page: { source?: string; archiveUrl?: string; waybackUrl?: string; captureTs: string; originalUrl: string; }): { source: ArchiveSourceId; label: string; url: string } { const source = (page.source ?? "wayback") as ArchiveSourceId; if (source !== "wayback" && page.archiveUrl) { return { source, label: archiveSourceLabel(source), url: page.archiveUrl }; } return { source: "wayback", label: ARCHIVE_SOURCE_LABELS.wayback, url: page.waybackUrl || waybackPageUrl({ captureTs: page.captureTs, originalUrl: page.originalUrl }), }; }