// Release 37 S1: WaybackClient.getCapture and closestCapture on the fake // Wayback (common/testing/web/fakeWayback.mjs) — the raw `id_` body, the 2 MB // cap, the mimetype refusal, redirects followed inside the archive only (the // FINAL capture recorded), and the polite client's gap between requests. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackClient.pages.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { startFakeWayback, PAGE_ARTICLE_LATER_TS, PAGE_ARTICLE_TS, PAGE_ARTICLE_URL, PAGE_BIG_TS, PAGE_BIG_URL, PAGE_IMAGE_TS, PAGE_IMAGE_URL, PAGE_JUMP_TS, PAGE_JUMP_URL, PAGE_PLAIN_TS, PAGE_PLAIN_URL, } from "../testing/web/fakeWayback.mjs"; import { WaybackCaptureRefusal, WaybackClient, waybackCdxUrl, waybackTestOrigin, WAYBACK_PAGE_MAX_BYTES } from "./waybackClient"; const refused = (code: string) => (e: unknown) => e instanceof WaybackCaptureRefusal && e.code === code; test("getCapture: the raw id_ body of a page, its mimetype, size and sha256", async () => { const wb = await startFakeWayback(); try { const c = new WaybackClient({ fetch: wb.fetch }, { minGapMs: 0 }); const cap = await c.getCapture(PAGE_ARTICLE_TS, PAGE_ARTICLE_URL, { raw: true }); assert.equal(cap.mimetype, "text/html"); assert.match(cap.contentType, /charset=utf-8/); assert.equal(cap.finalTs, PAGE_ARTICLE_TS); assert.equal(cap.originalUrl, PAGE_ARTICLE_URL); assert.ok(cap.body.toString("utf8").includes("A Post About Bread")); assert.equal(cap.bytes, cap.body.byteLength); assert.equal(cap.sha256, createHash("sha256").update(cap.body).digest("hex")); assert.deepEqual(wb.requests.map((r) => r.path), [`/web/${PAGE_ARTICLE_TS}id_/${PAGE_ARTICLE_URL}`]); const plain = await c.getCapture(PAGE_PLAIN_TS, PAGE_PLAIN_URL); assert.equal(plain.mimetype, "text/plain"); } finally { await wb.close(); } }); test("getCapture: over the cap is refused unread past it; an image is refused by its mimetype", async () => { const wb = await startFakeWayback(); try { const c = new WaybackClient({ fetch: wb.fetch }, { minGapMs: 0 }); assert.equal(WAYBACK_PAGE_MAX_BYTES, 2 * 1024 * 1024); await assert.rejects(c.getCapture(PAGE_BIG_TS, PAGE_BIG_URL), refused("too-large")); // A smaller cap of the caller's own. await assert.rejects(c.getCapture(PAGE_ARTICLE_TS, PAGE_ARTICLE_URL, { maxBytes: 100 }), refused("too-large")); await assert.rejects(c.getCapture(PAGE_IMAGE_TS, PAGE_IMAGE_URL), (e: unknown) => refused("mimetype")(e) && (e as WaybackCaptureRefusal).mimetype === "image/jpeg" && /image\/jpeg, not a page/.test((e as Error).message), ); await assert.rejects(c.getCapture("20200101000000", "https://never.example/"), refused("not-archived")); } finally { await wb.close(); } }); test("getCapture: a redirect to another capture is followed (the final timestamp is the capture's); one off the archive is refused", async () => { const wb = await startFakeWayback(); try { const c = new WaybackClient({ fetch: wb.fetch }, { minGapMs: 0 }); const near = await c.getCapture("20140301000000", PAGE_ARTICLE_URL); assert.equal(near.finalTs, PAGE_ARTICLE_TS); assert.deepEqual(wb.requests.map((r) => r.path), [ `/web/20140301000000id_/${PAGE_ARTICLE_URL}`, `/web/${PAGE_ARTICLE_TS}id_/${PAGE_ARTICLE_URL}`, ]); await assert.rejects(c.getCapture(PAGE_JUMP_TS, PAGE_JUMP_URL), (e: unknown) => refused("offsite-redirect")(e) && /elsewhere\.example/.test((e as Error).message), ); } finally { await wb.close(); } }); test("closestCapture: the nearest 200 capture to a date, through one CDX request; none for a page never captured", async () => { const wb = await startFakeWayback(); try { const c = new WaybackClient({ fetch: wb.fetch }, { minGapMs: 0 }); assert.equal((await c.closestCapture(PAGE_ARTICLE_URL, "20150601"))?.timestamp, PAGE_ARTICLE_LATER_TS); assert.equal((await c.closestCapture(PAGE_ARTICLE_URL, "2014"))?.timestamp, PAGE_ARTICLE_TS); assert.equal(await c.closestCapture("https://never.example/", "2014"), null); assert.equal(wb.requests.length, 3); assert.match(wb.requests[0].path, /closest=20150601/); assert.match(wb.requests[0].path, /filter=statuscode%3A200/); assert.equal( waybackCdxUrl("https://a.example/", { closest: "2014", limit: 5 }), "https://web.archive.org/cdx/search/cdx?url=https%3A%2F%2Fa.example%2F&output=json&limit=5&closest=2014&sort=closest", ); } finally { await wb.close(); } }); test("polite: every request of a capture (its redirects too) waits out the client's gap", async () => { const wb = await startFakeWayback(); try { const gap = 150; const c = new WaybackClient({ fetch: wb.fetch }, { minGapMs: gap }); await c.closestCapture(PAGE_ARTICLE_URL, "2014"); await c.getCapture("20140301000000", PAGE_ARTICLE_URL); const at = wb.requests.map((r) => r.at); assert.equal(at.length, 3); for (let i = 1; i < at.length; i++) assert.ok(at[i] - at[i - 1] >= gap - 5, `request ${i} came ${at[i] - at[i - 1]} ms after the last`); } finally { await wb.close(); } }); test("the e2e origin file: a loopback origin is used, anything else ignored", async () => { const dir = await mkdtemp(path.join(tmpdir(), "wb-origin-")); try { const file = path.join(dir, "origin"); assert.equal(waybackTestOrigin({}), null); assert.equal(waybackTestOrigin({ E2E_WAYBACK_ORIGIN_FILE: file }), null); await writeFile(file, "http://127.0.0.1:4567/\n"); assert.equal(waybackTestOrigin({ E2E_WAYBACK_ORIGIN_FILE: file }), "http://127.0.0.1:4567"); await writeFile(file, "https://web.archive.org"); assert.equal(waybackTestOrigin({ E2E_WAYBACK_ORIGIN_FILE: file }), null); } finally { await rm(dir, { recursive: true, force: true }); } });