SPB Git forge
38commits 1branches 0releases
338.7 MBsize
maindefault branch
5 h agolast push
HTML 53.9% TypeScript 44.5% JavaScript 0.6% SQL 0.5%
2.1 KB · 40 lines typescript
Raw Blame History
1/**2 * Proxies to the worker's internal HTTP server (DCI_WORKER_URL, default http://127.0.0.1:8320; compose:3 * http://worker-maint:8320): GET /data-gaps and GET /trace/<documentId> (extraction debugger). 10 s timeout, 502 on failure.4 */5import type { FastifyInstance } from "fastify";6import { z } from "zod";7import { getEnv } from "../../env.js";8import { envelope, HttpError, parseQuery } from "../../lib/http.js";9import { strParam } from "../../lib/params.js";1011async function workerGet(path: string): Promise<unknown> {12  const base = getEnv().workerUrl.replace(/\/+$/, "");13  const url = `${base}${path}`;14  let host = base;15  try { host = new URL(base).host; } catch { /* keep raw */ }16  try {17    const res = await fetch(url, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(10_000) });18    const text = await res.text();19    let body: unknown = text;20    try { body = JSON.parse(text); } catch { /* keep text */ }21    if (!res.ok) throw new HttpError(res.status === 404 ? 404 : 502, `worker answered HTTP ${res.status}`, { worker: host, path, body });22    return body;23  } catch (e) {24    if (e instanceof HttpError) throw e;25    const msg = (e as Error).name === "TimeoutError" ? "timeout after 10 s" : (e as Error).message;26    throw new HttpError(502, `worker unreachable: ${msg}`, { worker: host, path });27  }28}2930export async function proxyAdminRoutes(app: FastifyInstance): Promise<void> {31  app.get("/data-gaps", { schema: { summary: "Data gaps counters (proxied from the worker GET /data-gaps; 502 when the worker is unreachable)" } }, async () => envelope(await workerGet("/data-gaps"), { source: getEnv().workerUrl }));3233  app.get("/documents/:id/trace", { schema: { summary: "Extraction debugger trace for a document (proxied from the worker GET /trace/<id>?live=1 re-fetches the page)" } }, async (req) => {34    const { id } = req.params as { id: string };35    const q = parseQuery(z.object({ live: strParam }), req.query);36    const live = q.live === "1" || q.live === "true" ? "1" : "";37    return envelope(await workerGet(`/trace/${encodeURIComponent(id)}${live ? "?live=1" : ""}`), { source: getEnv().workerUrl, live: live === "1" });38  });39}40