// THE ARCHIVE SOURCES (release 37 lane F): the registry every caller asks — the // page import, the lookup across sources, the channel page — and the process's // clients. One entry per source, in the order a lookup lists them. ADDING A // SOURCE is one module (its client + its `ArchiveSource`) and one entry here: // see plans/release-37/archive-sources.md, "Adding an archive source". // // Server-only (the clients). What a URL says without a request is // lib/archives/mementos.ts, client-safe. import { getSettings } from "../settings"; import { getPaths } from "../paths"; import { ArchiveOrgRequestError } from "../archiveOrgClient"; import { isWaybackExcluded, WAYBACK_EXCLUDED_PHRASE, WaybackCaptureRefusal } from "../waybackClient"; import { archiveSourceLabel, parseArchiveTodayUrl } from "./mementos"; import { waybackSource } from "./wayback"; import { ARCHIVE_TODAY_QUEUE, ArchiveTodayClient, ArchiveTodayRequestError, archiveTodayTestOrigin, processArchiveTodayFetch, } from "./archiveTodayClient"; import { archiveSessionBrowser } from "./archiveTodaySession"; import { noteArchiveTodayOutcome } from "../serviceCookies-server"; import { ArchiveRefusal, type ArchiveCaptureRow, type ArchiveRefusalCode, type ArchiveSource, type ArchiveSourceId } from "./types"; export * from "./types"; const archivesSettings = () => { try { return getSettings().archives; } catch { return undefined; } }; // THE process's archive.today client: one chain, one gap, one cool-down, the // host and pace read from settings per request. Under the e2e fake // (E2E_ARCHIVE_TODAY_ORIGIN_FILE) no browser is ever opened. export const archiveTodayClient = new ArchiveTodayClient( { fetch: processArchiveTodayFetch, host: () => archivesSettings()?.archiveTodayHost ?? "archive.ph", openBrowser: () => archiveSessionBrowser(getPaths(), { onLog: (l) => console.log(`[archive-session] ${l}`) })(), browserEnabled: () => archivesSettings()?.archiveTodayBrowser !== false && archiveTodayTestOrigin() === null, // The cookie refresh alert for archive.today (lib/serviceCookies.ts). observe: (o) => noteArchiveTodayOutcome(getPaths(), o), }, { minGapMs: () => (archivesSettings()?.archiveTodayGapSeconds ?? 8) * 1000, cooldownMs: () => (archivesSettings()?.archiveTodayCooldownMinutes ?? 30) * 60_000, }, ); export function archiveTodaySource(client: ArchiveTodayClient = archiveTodayClient): ArchiveSource { return { id: "archive.today", label: "archive.today", queueKey: ARCHIVE_TODAY_QUEUE, matchesUrl: (url) => parseArchiveTodayUrl(url) !== null, parseMementoUrl: (url) => parseArchiveTodayUrl(url, client.host), listCaptures: (url, o) => client.listCaptures(url, o), closestCapture: (url, at, o) => client.closestCapture(url, at, o), getCapture: (memento, o) => client.getCapture(memento, o), }; } export const ARCHIVE_SOURCES: readonly ArchiveSource[] = [waybackSource(), archiveTodaySource()]; export function archiveSource(id: string | undefined, sources: readonly ArchiveSource[] = ARCHIVE_SOURCES): ArchiveSource | null { return sources.find((s) => s.id === (id ?? "wayback")) ?? null; } // The source a COPY's URL is on, or null (a listing, an original page). export function sourceForUrl(url: string, sources: readonly ArchiveSource[] = ARCHIVE_SOURCES): ArchiveSource | null { return sources.find((s) => s.matchesUrl(url)) ?? null; } // An error as an archive refusal (its code, source and sentence), or null // when it is not one: ArchiveRefusal, WaybackCaptureRefusal, and the Wayback // Machine's exclusion (a CDX 403). export function archiveRefusalOf(e: unknown): { source: ArchiveSourceId; code: ArchiveRefusalCode; message: string } | null { if (e instanceof ArchiveRefusal) return { source: e.source, code: e.code, message: e.message }; if (isWaybackExcluded(e)) return { source: "wayback", code: "excluded", message: (e as Error).message }; if (e instanceof WaybackCaptureRefusal) return { source: "wayback", code: e.code, message: e.message }; return null; } export type ArchiveLookupEntry = { source: ArchiveSourceId; label: string; ok: boolean; captures: ArchiveCaptureRow[]; // The refusal's code ("excluded", "challenge", "rate-limited", …), when // the source refused. refusal?: ArchiveRefusalCode; error?: string; }; export type ArchiveLookup = { url: string; found: number; sources: ArchiveLookupEntry[]; }; // THE COPIES OF A PAGE across sources (`archive-lookup`): each source asked // in turn (each through its own paced client), its copies oldest first, at // most `limit` per source. A source that refuses or fails is its own entry's // sentence; the others still answer. Read only. export async function archiveLookup( url: string, o: { source?: ArchiveSourceId; sources?: readonly ArchiveSource[]; limit?: number; from?: string; to?: string; signal?: AbortSignal; } = {}, ): Promise { const all = o.sources ?? ARCHIVE_SOURCES; const chosen = o.source ? all.filter((s) => s.id === o.source) : all; const entries: ArchiveLookupEntry[] = []; for (const s of chosen) { try { const captures = await s.listCaptures(url, { ...(o.limit ? { limit: o.limit } : {}), ...(o.from ? { from: o.from } : {}), ...(o.to ? { to: o.to } : {}), ...(o.signal ? { signal: o.signal } : {}), }); entries.push({ source: s.id, label: s.label, ok: true, captures }); } catch (e) { if (o.signal?.aborted) throw e; const refusal = archiveRefusalOf(e); if (refusal) { entries.push({ source: s.id, label: s.label, ok: false, captures: [], refusal: refusal.code, error: refusal.message }); } else if (e instanceof ArchiveOrgRequestError || e instanceof ArchiveTodayRequestError) { entries.push({ source: s.id, label: s.label, ok: false, captures: [], error: e.message }); } else { throw e; } } } return { url, found: entries.reduce((n, e) => n + e.captures.length, 0), sources: entries }; } // THE COPY NEAREST A TIME, across sources (lane E's find-copy page mode): each // source asked in turn (the Wayback Machine first), the first that holds a // copy answers. A source that EXCLUDES the page (a Wayback 403) or holds none // passes to the next; any other refusal or error is thrown, so the caller's // stop rules still apply. export async function nearestAcrossSources( url: string, at: string, o: { sources?: readonly ArchiveSource[]; signal?: AbortSignal } = {}, ): Promise { for (const s of o.sources ?? ARCHIVE_SOURCES) { try { const near = await s.closestCapture(url, at, o.signal ? { signal: o.signal } : {}); if (near) return near; } catch (e) { if (o.signal?.aborted) throw e; if (archiveRefusalOf(e)?.code === "excluded") continue; throw e; } } return null; } export { archiveSourceLabel, WAYBACK_EXCLUDED_PHRASE };