// IS EACH PAGE RECORD'S ORIGINAL STILL UP? (release 37 S5) — the Wayback // pages channel's branch of check-post-availability // (./checkPostAvailability.ts). A page record's availability is the state of // the ORIGINAL URL it was captured from, never the Wayback Machine's: // lib/waybackPages/liveness-server.ts checkOriginalUrl, through the process's // web pacer (one request at a time, 3 s apart, 429/503 backed off). // // ONE REQUEST PER DISTINCT URL: a capture split into many records (an X // profile's tweets, a Tumblr index's posts) and the versions of one page share // an original URL, and each of them gets the URL's verdict. `limit` counts // URLs. The verdicts land in the same sidecar every post channel uses // (posts-availability.json, mergePostAvailability: history appended only when // a verdict changes), with the answer's status and reason; the index merges // them in, so a record whose original is gone is `isDeleted` — the Wayback // copy is then the only copy. import path from "node:path"; import type { Paths } from "../lib/paths"; import { mergePostAvailability, readAllPosts, readPostAvailability, writePostAvailability } from "../lib/posts-server"; import type { PostAvailability, PostAvailabilityMap } from "../lib/posts"; import { groupByOriginalUrl, type OriginalCheck } from "../lib/waybackPages/liveness"; import { checkOriginalUrl, type CheckOriginalOptions } from "../lib/waybackPages/liveness-server"; import type { CheckPostAvailabilityMode, CheckPostAvailabilityResult } from "./checkPostAvailability"; // Which original URLs to ask this run: `stale` (the default) the // never-checked first, then the least recently checked (by the oldest check // among the URL's records); `unchecked` only URLs with a record never // checked; `all` every URL. At most `limit`. export function selectUrlsToCheck( groups: ReadonlyMap, availability: PostAvailabilityMap, mode: CheckPostAvailabilityMode, limit: number, ): string[] { const oldest = (ids: readonly string[]) => ids.reduce((min, id) => { const at = availability[id]?.checkedAt ?? ""; return at < min ? at : min; }, "￿"); let urls = [...groups.keys()]; if (mode === "unchecked") urls = urls.filter((u) => groups.get(u)!.some((id) => !availability[id])); if (mode === "stale") { const at = new Map(urls.map((u) => [u, oldest(groups.get(u)!)])); urls.sort((a, b) => at.get(a)!.localeCompare(at.get(b)!)); } return urls.slice(0, Math.max(0, limit)); } export async function checkPageAvailability(opts: { paths: Paths; slug: string; mode: CheckPostAvailabilityMode; limit: number; onLog?: (line: string) => void; signal?: AbortSignal; // Tests: the pacer, the fence's resolver, loopback allowed. check?: CheckOriginalOptions; now?: () => Date; }): Promise { const log = (line: string) => opts.onLog?.(line); const channelRoot = path.join(opts.paths.channelsDir, opts.slug); const empty = { checked: 0, deleted: 0, newlyDeleted: [] as string[], changed: 0, remaining: 0 }; const records = (await readAllPosts(channelRoot)).filter((p) => p.page); if (records.length === 0) return { ok: true, ...empty }; const availability = await readPostAvailability(channelRoot); const groups = groupByOriginalUrl(records); const batch = selectUrlsToCheck(groups, availability, opts.mode, opts.limit); if (batch.length === 0) { log(`Nothing to check for ${opts.slug} (mode: ${opts.mode}).`); return { ok: true, ...empty }; } log( `Checking ${batch.length} of ${groups.size} original URL(s) (${records.length} page record(s)) of ${opts.slug} ` + `at their own sites, not the Wayback Machine (mode: ${opts.mode}).`, ); const observations = new Map(); const details = new Map(); const urls: OriginalCheck[] = []; for (const url of batch) { if (opts.signal?.aborted) break; const verdict = await checkOriginalUrl(url, { ...opts.check, ...(opts.signal ? { signal: opts.signal } : {}) }); urls.push(verdict); const ids = groups.get(url)!; for (const id of ids) { observations.set(id, verdict.availability); details.set(id, { status: verdict.status, reason: verdict.reason }); } log(` ${url}: ${verdict.availability} — ${verdict.reason}${ids.length > 1 ? ` (${ids.length} records)` : ""}`); } const checkedAt = (opts.now?.() ?? new Date()).toISOString(); const { map, newlyDeleted, changed } = mergePostAvailability(availability, observations, checkedAt, details); await writePostAvailability(channelRoot, map); const deleted = Object.values(map).filter((r) => r.availability === "deleted").length; const remaining = records.filter((r) => !map[r.id]).length; if (newlyDeleted.length > 0) { log(`${newlyDeleted.length} page record(s) whose original is newly GONE (the Wayback copy is the only copy): ${newlyDeleted.slice(0, 10).join(", ")}${newlyDeleted.length > 10 ? ` (+${newlyDeleted.length - 10} more)` : ""}`); } log(`Done: ${urls.length} URL(s) checked (${observations.size} record(s)), ${changed} changed, ${deleted} gone overall, ${remaining} never checked.`); if (opts.signal?.aborted) log("Cancelled; the rest are left for a later run."); return { ok: true, checked: observations.size, deleted, newlyDeleted, changed, remaining, urls }; }