// ONE ORIGINAL URL'S STATE (release 37 S5): the request side of // ./liveness.ts. Through the process's web pacer (lib/web/pacer.ts: one // request at a time, 3 s apart, 429/503 backed off) — never the Wayback // Machine, never a burst — and behind the web fetch's fence (http(s) only, // no private or loopback host, every redirect hop checked the same way). // // HEAD, following redirects by hand (at most WEB_FETCH_MAX_REDIRECTS); // a GET instead when the HEAD is refused (405, 403, 501) or when the page's // platform has a "gone" page that answers 200 (its words decide), its body // read up to LIVENESS_MAX_BODY_BYTES. // // Answers an OriginalCheck; never throws for what the site answered (a // cancelled run's abort is rethrown). import { fetchRefusal, WEB_FETCH_MAX_REDIRECTS, type WebFetchOptions } from "../web/fetchPage"; import { webPacer, WebRequestError, type WebPacer } from "../web/pacer"; import { classifyOriginalAnswer, hasGoneMarkers, type OriginalCheck } from "./liveness"; // A "gone" page's words are near its top: the body is read no further. export const LIVENESS_MAX_BODY_BYTES = 256 * 1024; // A HEAD these statuses refuse is asked again as a GET. const HEAD_REFUSED = new Set([403, 405, 501]); export type CheckOriginalOptions = Pick & { pacer?: WebPacer; signal?: AbortSignal; }; async function readCapped(res: Response, maxBytes: number): Promise { if (!res.body) return ""; const reader = res.body.getReader(); const chunks: Uint8Array[] = []; let n = 0; for (;;) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); n += value.byteLength; if (n >= maxBytes) { await reader.cancel().catch(() => {}); break; } } return new TextDecoder("utf-8", { fatal: false }).decode(Buffer.concat(chunks).subarray(0, maxBytes)); } type Hop = { redirect: string } | { status: number; body?: string }; // One method's walk: redirects followed by hand, each hop fenced. async function walk( url: string, method: "HEAD" | "GET", o: CheckOriginalOptions, ): Promise<{ finalUrl: string; status: number; body?: string } | { refusal: string; status: number | null; finalUrl: string }> { const pacer = o.pacer ?? webPacer; let at = url; for (let hop = 0; hop <= WEB_FETCH_MAX_REDIRECTS; hop++) { const refusal = await fetchRefusal(at, o); if (refusal) return { refusal, status: null, finalUrl: at }; const step: Hop = await pacer.request( at, { method, redirect: "manual", headers: { accept: "text/html,application/xhtml+xml,text/plain;q=0.9,*/*;q=0.5" }, ...(o.signal ? { signal: o.signal } : {}), }, async (res): Promise => { const loc = res.headers.get("location"); if (res.status >= 300 && res.status < 400 && loc) { await res.body?.cancel().catch(() => {}); return { redirect: new URL(loc, at).toString() }; } if (method === "HEAD") { await res.body?.cancel().catch(() => {}); return { status: res.status }; } return { status: res.status, body: await readCapped(res, LIVENESS_MAX_BODY_BYTES) }; }, ); if ("redirect" in step) { at = step.redirect; continue; } return { finalUrl: at, status: step.status, ...(step.body !== undefined ? { body: step.body } : {}) }; } return { refusal: `more than ${WEB_FETCH_MAX_REDIRECTS} redirects`, status: null, finalUrl: at }; } export async function checkOriginalUrl(url: string, o: CheckOriginalOptions = {}): Promise { try { let answer = await walk(url, "HEAD", o); const headRefused = !("refusal" in answer) && HEAD_REFUSED.has(answer.status); const needsBody = !("refusal" in answer) && answer.status >= 200 && answer.status < 300 && hasGoneMarkers(answer.finalUrl); if (headRefused || needsBody) answer = await walk(url, "GET", o); if ("refusal" in answer) { return { url, availability: "error", status: answer.status, ...(answer.finalUrl !== url ? { finalUrl: answer.finalUrl } : {}), reason: answer.refusal }; } return classifyOriginalAnswer({ url, finalUrl: answer.finalUrl, status: answer.status, ...(answer.body !== undefined ? { body: answer.body } : {}) }); } catch (e) { if (o.signal?.aborted) throw e; const status = e instanceof WebRequestError ? e.status : null; return { url, availability: "error", status, reason: (e as Error).message }; } }