// The editor's side of a research case's verify, leads and evidence stages // (release 38 lane C), stubbed: // // POST /api/ops/transcribe {path, start, end, whilePaused?, words?, out?} // job "cs-", done after 0.6 s, its log ending with the editor's // `@@transcribe-result {…}` -- the text the fixture's HEARD; with `out`, the // result written there, and with `words` its words: `.heard.json` // beside the file when a fixture put one there (a word zoom's take), else // HEARD's words, a third of a second each // POST /api/ops/wayback-lookup {url} // 403 {refusal: "excluded"} for the fixture's EXCLUDED_URL, else two captures // POST /api/ops/fetch-posts {slug, …} // job, done after 0.6 s // GET /api/ops/job/cs-?tail=N // // (create-channel and import-page are editor-stub-import-page.mjs's; // capture-posts is editor-stub-intake.mjs's, shared with U4's Add post.) Never a // worker, the Wayback Machine or a forum. Every request is recorded by the // main stub's `record`. import { existsSync, readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { EXCLUDED_URL, HEARD } from "./case-stages-fixture.mjs"; const jobs = new Map(); let next = 1; function job(kind, log, onDone, ms = 600) { const id = `cs-${next++}`; const j = { id, kind, status: "queued", log: [...log] }; jobs.set(id, j); setTimeout(() => (j.status = "running"), 150); setTimeout(() => { try { onDone?.(j); } catch { /* the fixture was rebuilt under the job */ } if (j.status !== "failed") j.status = "done"; }, ms); return id; } /** Handles the routes above; answers true when it did. `ctx`: {channels, json, readBody, push(record)}. */ export function caseStagesRoute(req, res, url, ctx) { const { json, readBody } = ctx; const post = (p, fn) => { if (req.method !== "POST" || url.pathname !== p) return false; readBody(req, (body) => { ctx.push({ path: url.pathname, body }); fn(body ?? {}); }); return true; }; if ( post("/api/ops/transcribe", (body) => { if (typeof body.path !== "string" || !path.isAbsolute(body.path)) return json(res, 400, { ok: false, error: '"path" must be an absolute file path' }); const result = { version: 1, path: body.path, window: { start: body.start, end: body.end }, worker: { id: "w1", name: "stub parakeet", appId: "parakeet", model: "stub", device: "cpu" }, transcriptFormat: "parakeet-json", durationMs: 420, waitedMs: 0, cues: [{ start: body.start ?? 0, end: body.end ?? 0, text: HEARD }], text: HEARD, }; const id = job("transcribe-file", [`Transcribe ${path.basename(body.path)} [${body.start}, ${body.end})${body.whilePaused ? " while paused" : ""}`], (j) => { j.log.push(`@@transcribe-result ${JSON.stringify(result)}`); if (typeof body.out === "string" && path.isAbsolute(body.out)) { const sidecar = `${body.path}.heard.json`; const words = body.words !== true ? undefined : existsSync(sidecar) ? JSON.parse(readFileSync(sidecar, "utf8")) : HEARD.split(/\s+/).map((w, i) => ({ w, start: i / 3, end: i / 3 + 0.3 })); writeFileSync(body.out, JSON.stringify({ ...result, ...(words ? { words } : {}) })); } }); json(res, 200, { ok: true, jobId: id }); }) ) return true; if ( post("/api/ops/wayback-lookup", (body) => { if (body.url === EXCLUDED_URL) { return json(res, 403, { ok: false, error: `${body.url} is excluded from the Wayback Machine (cause unknown): the Wayback Machine answered HTTP 403 for it`, refusal: "excluded" }); } const captures = ["20140220093000", "20140304120000"].map((timestamp) => ({ timestamp, status: "200", replayUrl: `https://web.archive.org/web/${timestamp}/${body.url}` })); json(res, 200, { ok: true, url: body.url, found: captures.length, captures }); }) ) return true; if ( post("/api/ops/fetch-posts", (body) => { if (!body.slug) return json(res, 400, { ok: false, error: "name a channel" }); json(res, 200, { ok: true, jobId: job("fetch-posts", [`Fetch ${body.older ? "older " : ""}posts of ${body.slug}`]) }); }) ) return true; const m = /^\/api\/ops\/job\/(cs-\d+)$/.exec(url.pathname); if (req.method === "GET" && m) { const j = jobs.get(m[1]); if (!j) json(res, 404, { ok: false, error: "no such job" }); else json(res, 200, { ok: true, job: { id: j.id, kind: j.kind, status: j.status }, tail: j.log }); return true; } return false; }