// demo epic D1: a published archive served by the stub editor's own port, // under /archive-stub (the synthetic draft corpus — common/testing/draft — as // a site serves it), and the editor's `archives` action (POST // /api/ops/archives): `connect` reads the origin's corpus.json and, unless // `dryRun`, writes settings.remoteArchives into E2E_UMTOOL_SETTINGS_FILE (the // fixture's copy, which umtool's source picker reads); `remove` drops one. // Nothing leaves loopback. import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import path from "node:path"; import { writeDraftCorpus } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; export const ARCHIVE_PREFIX = "/archive-stub"; /** Write the stub archive's files under `dir` (once, at the stub's start). */ export async function prepareArchiveStub(dir) { mkdirSync(dir, { recursive: true }); await writeDraftCorpus(dir); const corpus = JSON.parse(readFileSync(path.join(dir, "corpus.json"), "utf8")); writeFileSync( path.join(dir, "corpus.json"), JSON.stringify({ ...corpus, spec: 6, generatedAt: new Date().toISOString(), site: { id: "stubalyzer", title: "Stubalyzer" } }), ); } /** * GET /archive-stub/: the archive's static files, written on the first * ask (make-fixture.mjs empties the fixture root while the stub starts, so * nothing is written before a spec asks). True when answered. */ export function archiveStubRoute(req, res, url, { dir }) { if (req.method !== "GET" || !url.pathname.startsWith(`${ARCHIVE_PREFIX}/`)) return false; const rel = decodeURIComponent(url.pathname.slice(ARCHIVE_PREFIX.length)); const file = path.normalize(path.join(dir, rel)); if (!file.startsWith(dir)) { res.writeHead(403).end(); return true; } const ready = existsSync(path.join(dir, "corpus.json")) ? Promise.resolve() : prepareArchiveStub(dir); ready.then( () => { try { const body = readFileSync(file); res.writeHead(200, { "content-type": "application/json" }); res.end(body); } catch { res.writeHead(404).end("not found"); } }, (e) => res.writeHead(500).end(String(e?.message ?? e)), ); return true; } const settingsOf = (file) => { try { return JSON.parse(readFileSync(file, "utf8")); } catch { return {}; } }; /** POST /api/ops/archives. True when answered. */ export function archivesRoute(req, res, url, { json, readBody, settingsFile }) { if (url.pathname !== "/api/ops/archives") return false; if (req.method === "GET") { json(res, 200, { ok: true, archives: settingsOf(settingsFile).remoteArchives ?? [] }); return true; } if (req.method !== "POST") return false; readBody(req, async (body) => { const settings = settingsOf(settingsFile); const list = Array.isArray(settings.remoteArchives) ? settings.remoteArchives : []; if (typeof body?.remove === "string") { const next = list.filter((a) => a.id !== body.remove && a.origin !== body.remove); writeFileSync(settingsFile, JSON.stringify({ ...settings, remoteArchives: next }, null, 2)); return json(res, 200, { ok: true, removed: list.find((a) => !next.includes(a)) ?? null }); } const origin = String(body?.connect ?? "").trim().replace(/\/+$/, ""); if (!/^https?:\/\//.test(origin)) return json(res, 400, { ok: false, error: `"${origin}" is not an archive's address` }); let corpus; try { const r = await fetch(`${origin}/corpus.json`); if (!r.ok) return json(res, 502, { ok: false, error: `${origin}/corpus.json answered HTTP ${r.status}` }); corpus = await r.json(); } catch (e) { return json(res, 502, { ok: false, error: `${origin}/corpus.json did not answer: ${e.message}` }); } const channels = (corpus.channels ?? []).map((c) => ({ slug: c.slug, name: c.name ?? c.slug, videoCount: c.videoCount ?? null })); const probe = { origin, siteId: corpus.site?.id ?? null, title: corpus.site?.title ?? origin, description: "", spec: corpus.spec ?? null, generatedAt: corpus.generatedAt ?? null, totals: { channels: channels.length, videos: channels.reduce((n, c) => n + (c.videoCount ?? 0), 0) }, channels, }; const have = list.find((a) => a.origin === origin); const archive = have ?? { id: probe.siteId ?? "archive", origin, title: probe.title, addedAt: new Date().toISOString() }; if (!have && body?.dryRun !== true) { mkdirSync(path.dirname(settingsFile), { recursive: true }); writeFileSync(settingsFile, JSON.stringify({ ...settings, remoteArchives: [...list, archive] }, null, 2)); } return json(res, 200, { ok: true, probe, archive, already: have ? true : body?.dryRun === true ? null : false }); }); return true; }