// IS THE ORIGINAL STILL UP? (release 37 S5, plans/release-37/wayback-pages.md // "S5") — the pure half of a page record's availability: what one answer from // the ORIGINAL URL (never the Wayback Machine) says about it. // // available a 2xx, after any redirects that stayed on the same // site, whose body carries no platform's "gone" page // deleted 404 or 410, or a platform's "this does not exist" // page (PAGE_GONE_MARKERS) — the Wayback copy is then // the only copy // account_unavailable a platform's "this account is suspended / this // document is not shared" page: the page is not served, // and that may reverse // error anything else (a 403, a 5xx, a redirect to another // site, no answer), with the status — never a deletion // reported from ignorance // // Pure and client-safe: the request side is ./liveness-server.ts. import type { PostAvailability } from "../posts"; // One platform's "gone" page: the hosts it applies to and the words it shows. export type PageGoneMarker = { // A short name for logs and tests. id: string; // The page's host (no "www."), tested against each pattern. hosts: readonly RegExp[]; // Matched against the page's text (tags dropped, entities and curly quotes // folded, whitespace collapsed, lower case). text: RegExp; availability: Extract; // What the log and the sidecar say. reason: string; }; const X_HOSTS = [/^(?:mobile\.)?(?:x|twitter)\.com$/]; const TUMBLR_HOSTS = [/(?:^|\.)tumblr\.com$/]; const GOOGLE_DOCS_HOSTS = [/^(?:docs|drive)\.google\.com$/]; const YOUTUBE_HOSTS = [/^(?:m\.)?youtube\.com$/]; // THE TABLE (unit-tested in liveness.test.ts). A platform that serves its // "nothing here" page with a 200 is only told apart by its words; one that // answers 404 needs no entry (the status says it). export const PAGE_GONE_MARKERS: readonly PageGoneMarker[] = [ { id: "x-no-account", hosts: X_HOSTS, text: /this account doesn't exist/, availability: "deleted", reason: "X says the account doesn't exist", }, { id: "x-no-page", hosts: X_HOSTS, text: /(?:hmm\.\.\.\s*)?this page doesn't exist|sorry, that page doesn't exist/, availability: "deleted", reason: "X says the page doesn't exist", }, { id: "x-suspended", hosts: X_HOSTS, text: /account suspended/, availability: "account_unavailable", reason: "X says the account is suspended", }, { id: "tumblr-nothing-here", hosts: TUMBLR_HOSTS, text: /there's nothing here\.?|whatever you were looking for doesn't currently exist at this address/, availability: "deleted", reason: "Tumblr says there's nothing here", }, { id: "gdocs-no-file", hosts: GOOGLE_DOCS_HOSTS, text: /sorry, the file you have requested does not exist/, availability: "deleted", reason: "Google Docs says the file does not exist", }, { id: "gdocs-permission", hosts: GOOGLE_DOCS_HOSTS, text: /you need (?:access|permission)/, availability: "account_unavailable", reason: "Google Docs says the file is not shared", }, { id: "youtube-no-channel", hosts: YOUTUBE_HOSTS, text: /this channel does not exist/, availability: "deleted", reason: "YouTube says the channel does not exist", }, { id: "youtube-terminated", hosts: YOUTUBE_HOSTS, text: /this account has been terminated/, availability: "account_unavailable", reason: "YouTube says the account has been terminated", }, ]; // The host without "www.", lower case; "" for no URL. export function bareHost(url: string): string { try { return new URL(url).hostname.toLowerCase().replace(/^www\./, ""); } catch { return ""; } } // Whether a platform has a "gone" page worth reading the body for. export function hasGoneMarkers(url: string): boolean { const host = bareHost(url); return !!host && PAGE_GONE_MARKERS.some((m) => m.hosts.some((h) => h.test(host))); } // The page's words, folded for matching: tags and scripts dropped, a few // entities and curly quotes folded, whitespace collapsed, lower case. export function foldPageText(html: string): string { return html .replace(/<(script|style|noscript)\b[^>]*>[\s\S]*?<\/\1>/gi, " ") .replace(/<[^>]+>/g, " ") .replace(/�*39;|�*27;|'|’|‘|’|’/gi, "'") .replace(/ | /gi, " ") .replace(/&/gi, "&") .replace(/[‘’ʼ]/g, "'") .replace(/\s+/g, " ") .trim() .toLowerCase(); } // The platform's "gone" page this body is, or null. export function pageGoneMarker(url: string, html: string): PageGoneMarker | null { const host = bareHost(url); if (!host || !html) return null; const text = foldPageText(html); for (const m of PAGE_GONE_MARKERS) { if (!m.hosts.some((h) => h.test(host))) continue; if (m.text.test(text)) return m; } return null; } // Two-label public suffixes the "same site" test must not stop at. const SECOND_LEVEL = /^(?:co|com|net|org|gov|ac|edu|ne|or)\.[a-z]{2}$/; // A host's registrable part, roughly: its last two labels, or three under a // two-label suffix (co.uk, com.au). // Sites that moved house: a redirect between them stays on the site. const SITE_ALIASES: Readonly> = { "twitter.com": "x.com", "youtu.be": "youtube.com" }; export function siteOf(host: string): string { const labels = host.toLowerCase().replace(/^www\./, "").split(".").filter(Boolean); const last2 = labels.slice(-2).join("."); const site = labels.length > 2 && SECOND_LEVEL.test(last2) ? labels.slice(-3).join(".") : last2; return SITE_ALIASES[site] ?? site; } // Whether a redirect from `a` to `b` stayed on the same site (http→https, // www, a subdomain of the same registrable domain: tumblr's blog → www). export function sameSite(a: string, b: string): boolean { const ha = bareHost(a); const hb = bareHost(b); if (!ha || !hb) return false; return ha === hb || siteOf(ha) === siteOf(hb); } // One check's verdict on an original URL. export type OriginalCheck = { url: string; availability: PostAvailability; // The final answer's HTTP status (null: no answer). status: number | null; // Where the redirects (if any) ended. finalUrl?: string; // Why, in a phrase (the marker's reason, the status, the redirect, the // network error). reason: string; }; // THE VERDICT on one final answer. `body` is the page's text when it was // read (a GET), else undefined (a HEAD). export function classifyOriginalAnswer(a: { url: string; finalUrl: string; status: number; body?: string; }): OriginalCheck { const base = { url: a.url, status: a.status, ...(a.finalUrl !== a.url ? { finalUrl: a.finalUrl } : {}) }; if (a.status === 404 || a.status === 410) { return { ...base, availability: "deleted", reason: `HTTP ${a.status}` }; } if (a.status >= 200 && a.status < 300) { if (!sameSite(a.url, a.finalUrl)) { return { ...base, availability: "error", reason: `redirects to another site (${bareHost(a.finalUrl) || a.finalUrl})` }; } const marker = a.body !== undefined ? pageGoneMarker(a.finalUrl, a.body) : null; if (marker) return { ...base, availability: marker.availability, reason: marker.reason }; return { ...base, availability: "available", reason: `HTTP ${a.status}` }; } // A platform's gone page can come with another status (a 403 wall). const marker = a.body !== undefined ? pageGoneMarker(a.finalUrl, a.body) : null; if (marker) return { ...base, availability: marker.availability, reason: `${marker.reason} (HTTP ${a.status})` }; return { ...base, availability: "error", reason: `HTTP ${a.status}` }; } // A webpage record's ids grouped by the original URL they were captured // from: one request per distinct URL, whatever number of records a capture // split into. Order: the order of `records` (first appearance). export function groupByOriginalUrl( records: ReadonlyArray<{ id: string; page?: { originalUrl: string } }>, ): Map { const out = new Map(); for (const r of records) { const url = r.page?.originalUrl; if (!url) continue; const list = out.get(url); if (list) list.push(r.id); else out.set(url, [r.id]); } return out; } // What a reader is told about a page record whose original is gone (the // source-gone label's page sentence, lib/sourceGone.ts SOURCE_GONE_LABELS // for everything else). export const PAGE_ORIGINAL_GONE_LABEL = "Original gone — the Wayback Machine copy is the only copy";