// ARCHIVE SOURCES (release 37 lane F, plans/release-37/archive-sources.md): a // service that holds copies of web pages — the Wayback Machine, archive.today, // and whichever comes next — read the same way. A source answers four // questions: is this URL one of my copies (a "memento"), which copies do I // hold of a page, which one is nearest a time, and what are a copy's bytes. // Each source is ONE module (lib/archives/.ts) registered in // lib/archives/index.ts; the page import, the lookup and the citation code ask // the registry, never a source by name. // // Types and the refusal only; client-safe (no node imports). // The sources this build knows. A record's `page.source` is one of these // (absent = "wayback", the records made before sources existed). export const ARCHIVE_SOURCE_IDS = ["wayback", "archive.today"] as const; export type ArchiveSourceId = (typeof ARCHIVE_SOURCE_IDS)[number]; export function isArchiveSourceId(v: unknown): v is ArchiveSourceId { return typeof v === "string" && (ARCHIVE_SOURCE_IDS as readonly string[]).includes(v); } // What a memento URL says before anything is fetched. archive.today's short // form (`archive.ph/AbCdE`) names neither the page nor the time: those are // learned from the copy itself. export type ArchiveMementoRef = { source: ArchiveSourceId; // The copy's URL, on the source's canonical host. mementoUrl: string; // The page it is a copy of, when the URL names it. originalUrl?: string; // The capture time (4–14 digits, a Wayback timestamp's form), when the URL // names it. A short one is "the copy nearest this". captureTs?: string; // The bytes as captured, when the source has such a form (Wayback's `id_`). rawUrl?: string; // archive.today: the short id (`AbCdE`). id?: string; // archive.today: `newest` / `oldest` — the URL names a page, and the source // picks the copy. pick?: "newest" | "oldest"; }; // One copy a source lists (a lookup's row). export type ArchiveCaptureRow = { source: ArchiveSourceId; // YYYYMMDDhhmmss captureTs: string; originalUrl: string; // The copy as a page a reader opens. mementoUrl: string; // The status the capture recorded, when the source says ("200", "302"). status?: string; mimetype?: string; }; // One copy's bytes (getCapture). `body` is what the source served — what the // page import stores — and `page` the same copy as a page the extractors read // (`unwrapped`): Wayback's `id_` body as it is, archive.today's snapshot with // its own header taken off and its rewritten links put back. export type ArchiveRawCapture = { source: ArchiveSourceId; body: Buffer; // The bare mimetype ("text/html") and the Content-Type as served. mimetype: string; contentType: string; // The copy the answer is (after any redirect). finalTs: string; originalUrl: string; mementoUrl: string; bytes: number; sha256: string; unwrapped: true; page: string; // archive.today: the copy was read through the headless browser (its check // stood in the way of a plain request). viaBrowser?: boolean; }; export type ArchiveRefusalCode = // The source answered, and holds nothing of this page for anyone: a Wayback // CDX 403 ("excluded from the Wayback Machine (cause unknown)"). | "excluded" // The source put a check (a CAPTCHA, a "one more step" page) in front of // the copy. Never solved by code: Connect archive session, then again. | "challenge" // The source said "too many requests" (HTTP 429): the run stops, and the // client leaves the source alone for a while. | "rate-limited" // No such copy. | "not-archived" // A body that is not a page, or over the cap. | "mimetype" | "too-large" // Redirects that leave the source or go round. | "offsite-redirect" | "redirect-loop" // An answer that names no capture time or page. | "unreadable"; // A copy (or a listing) a source will not hand back, with the sentence a // caller shows. Thrown by the archive.today client and by the registry's // lookups; Wayback's own refusals (WaybackCaptureRefusal, WaybackExcludedError) // are read as these by `archiveRefusalOf` (lib/archives/index.ts). export class ArchiveRefusal extends Error { readonly code: ArchiveRefusalCode; readonly source: ArchiveSourceId; // rate-limited: when the client will ask again (epoch ms). readonly retryAt?: number; constructor(source: ArchiveSourceId, code: ArchiveRefusalCode, message: string, o: { retryAt?: number } = {}) { super(message); this.name = "ArchiveRefusal"; this.source = source; this.code = code; if (o.retryAt !== undefined) this.retryAt = o.retryAt; } } // THE SOURCE. Every request goes through the source's own paced client; a job // that reads a source runs on its `queueKey`. export type ArchiveSource = { id: ArchiveSourceId; // "Wayback Machine", "archive.today". label: string; // The job queue its requests run on ("platform:archive.org", // "platform:archive.today"). queueKey: string; // Is this a memento URL of mine (a copy, not a listing)? matchesUrl(url: string): boolean; parseMementoUrl(url: string): ArchiveMementoRef | null; // The copies held of `originalUrl`, oldest first; `from`/`to` are // capture-time prefixes (inclusive), `limit` at most this many. Paced. listCaptures( originalUrl: string, o?: { from?: string; to?: string; limit?: number; signal?: AbortSignal }, ): Promise; // The copy nearest `at` (4–14 digits), or null when there is none. closestCapture(url: string, at: string, o?: { signal?: AbortSignal }): Promise; // One copy's bytes, at most `maxBytes`. Throws ArchiveRefusal (or the // source's own typed refusal) for what it will not hand back. getCapture(memento: string | ArchiveMementoRef, o?: { maxBytes?: number; signal?: AbortSignal }): Promise; };