// The editor's side of Import this capture (release 37 S4), stubbed: // // POST /api/ops/create-channel {fields: {name, handling, url}} // a Wayback pages channel's config.json written into the fixture corpus // (slug from the name, as the form derives it) → {ok, slug, jobIds: []} // POST /api/ops/import-page {slug, url} | {slug, url: , at: YYYYMMDD} // a job "ip-": running after 0.5 s, done after 1.5 s, when it writes // ONE page record into //posts/ — its text the // fixture's synthetic page (it holds the cited quote) — and ends its log // with the editor's `@@import-page {…}` result line // GET /api/ops/job/ip-?tail=N // that job's state and log // // Never the Wayback Machine, never a real editor. Every request is recorded by // the main stub's `record`. import { appendFileSync, mkdirSync, writeFileSync } from "node:fs"; import path from "node:path"; export const IMPORT_PAGE_TEXT = "The first loaf came out flat, and the second, and the third, which is how it goes when the starter is young."; const jobs = new Map(); let next = 1; function slugOf(name) { return String(name).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60) || "pages"; } function parseCapture(url) { const m = /^https?:\/\/web\.archive\.org\/web\/(\d{4,14})[a-z]{0,2}_?\/(.+)$/.exec(String(url)); return m ? { ts: m[1], original: m[2] } : null; } /** * Handles the three routes; answers true when it did. `ctx`: {channels, json, * readBody, push(record)}. */ export function importPageRoute(req, res, url, ctx) { const { json, readBody } = ctx; if (req.method === "POST" && url.pathname === "/api/ops/create-channel") { readBody(req, (body) => { const fields = body?.fields ?? {}; const slug = body?.slug ?? slugOf(fields.name); ctx.push({ path: url.pathname, body }); const dir = path.join(ctx.channels, slug); mkdirSync(dir, { recursive: true }); // Release 38 lane C: an archived lead's channel is X, YouTube or a page, by its URL. const u = String(fields.url ?? ""); const platform = /^https?:\/\/(?:www\.)?(?:x|twitter)\.com\//.test(u) ? "twitter" : /youtube\.com\//.test(u) ? "youtube" : "webpage"; writeFileSync( path.join(dir, "config.json"), JSON.stringify( platform === "youtube" ? { name: fields.name, handling: fields.handling ?? "youtube", url: u } : { name: fields.name, handling: fields.handling ?? "transcribe", sourceKind: "social", platform, url: u }, null, 2, ), ); const jobIds = body?.fetchPostsNow || body?.fetchPlaylist ? [`ip-start-${slug}`] : []; json(res, 200, { ok: true, slug, jobIds, ...(jobIds.length === 1 ? { jobId: jobIds[0] } : {}) }); }); return true; } if (req.method === "POST" && url.pathname === "/api/ops/import-page") { readBody(req, (body) => { ctx.push({ path: url.pathname, body }); // Release 38 lane C: the page's own URL and `at` -- the capture nearest that date (here, noon on it). const cap = parseCapture(body?.url) ?? (body?.at && /^https?:\/\//.test(String(body?.url)) ? { ts: `${String(body.at).slice(0, 8)}120000`, original: String(body.url) } : null); if (!body?.slug || !cap) return json(res, 400, { ok: false, error: "not one capture" }); const id = `ip-${next++}`; const job = { id, status: "queued", log: [`Import ${body.url} into ${body.slug}`] }; jobs.set(id, job); setTimeout(() => (job.status = "running"), 500); setTimeout(() => { const recordId = `${cap.ts}-0123456789ab`; const post = { id: recordId, slug: `${body.slug}/${recordId}`, channelSlug: body.slug, author: new URL(cap.original).hostname, createdAt: "2014-03-04T18:30:00.000Z", uploadDate: "20140304", text: IMPORT_PAGE_TEXT, url: cap.original, platform: "webpage", isReply: false, isRepost: false, links: [], threadId: "page-0123456789ab", page: { originalUrl: cap.original, captureTs: cap.ts, waybackUrl: `https://web.archive.org/web/${cap.ts}/${cap.original}`, rawUrl: `https://web.archive.org/web/${cap.ts}id_/${cap.original}`, kind: "generic", title: "A Post About Bread", extractor: "generic", extractorVersion: 1, }, }; try { const dir = path.join(ctx.channels, body.slug, "posts"); mkdirSync(dir, { recursive: true }); appendFileSync(path.join(dir, "2014-03.jsonl"), JSON.stringify(post) + "\n"); appendFileSync(path.join(ctx.channels, body.slug, "posts-archive"), `webpage ${recordId}\n`); } catch { /* the fixture was rebuilt under the job */ } job.log.push(`@@import-page ${JSON.stringify({ ok: true, slug: body.slug, url: body.url, captureTs: cap.ts, records: [recordId], written: 1, updated: 0, unchanged: 0 })}`); job.status = "done"; }, 1500); json(res, 200, { ok: true, jobId: id }); }); return true; } const m = /^\/api\/ops\/job\/(ip-\d+)$/.exec(url.pathname); if (req.method === "GET" && m) { const job = jobs.get(m[1]); if (!job) { json(res, 404, { ok: false, error: "no such job" }); return true; } json(res, 200, { ok: true, job: { id: job.id, kind: "import-page", status: job.status }, tail: job.log }); return true; } return false; }