This commit is contained in:
2026-07-15 19:34:13 -05:00
parent 84b7ee0a47
commit 8e0253821f
8 changed files with 663 additions and 751 deletions

70
main.ts
View File

@@ -19,12 +19,6 @@ import STYLES_CSS from "./web/styles.css" with { type: "text" };
import SAMPLE_DOCUMENTS from "./sample-data/buyers_vision_anonymous.json" with {
type: "json",
};
import PDFJS_LIB_TEXT from "pdfjs-dist/legacy/build/pdf.min.mjs" with {
type: "text",
};
import PDFJS_WORKER_TEXT from "pdfjs-dist/legacy/build/pdf.worker.min.mjs" with {
type: "text",
};
const PREFIX = "[BizMatch QC]";
@@ -354,6 +348,30 @@ Deno.serve(async (request) => {
}
}
// ---- Client-side log forwarding ----
if (url.pathname === "/api/client-log" && request.method === "POST") {
try {
const body = await request.json() as {
level?: string;
message?: string;
data?: unknown;
};
const level = body.level || "info";
const prefix = `${PREFIX} Client`;
if (level === "error") {
console.error(
`${prefix} [ERROR] ${body.message || ""}`,
body.data || "",
);
} else if (level === "warn") {
console.warn(`${prefix} [WARN] ${body.message || ""}`, body.data || "");
} else {
console.log(`${prefix} ${body.message || ""}`, body.data || "");
}
} catch { /* ignore */ }
return new Response(null, { status: 204 });
}
// ---- PDF prepare endpoint ----
if (url.pathname === "/api/pdf/prepare" && request.method === "POST") {
try {
@@ -374,9 +392,29 @@ Deno.serve(async (request) => {
const startTime = performance.now();
const cacheResult = await cacheOrGetPdf(sourcePath);
// Validate cached file
let fileSize: number;
try {
fileSize = (await Deno.stat(cacheResult.path)).size;
const f = await Deno.open(cacheResult.path, { read: true });
const head = new Uint8Array(5);
let n = 0;
while (n < 5) {
const r = await f.read(head.subarray(n));
if (r === null) break;
n += r;
}
f.close();
const header = new TextDecoder().decode(head.slice(0, n));
if (header !== "%PDF-") {
console.warn(
`${PREFIX} Cached PDF invalid header: "${header}", re-caching`,
);
await Deno.remove(cacheResult.path).catch(() => {});
const fresh = await cacheOrGetPdf(sourcePath);
fileSize = (await Deno.stat(fresh.path)).size;
}
} catch {
throw new Error("Cached file is not readable.");
}
@@ -452,25 +490,6 @@ Deno.serve(async (request) => {
});
}
// ---- PDF.js library assets ----
if (url.pathname === "/pdfjs/legacy/pdf.min.mjs") {
return new Response(PDFJS_LIB_TEXT, {
headers: {
"content-type": "text/javascript; charset=utf-8",
"cache-control": "public, max-age=86400",
},
});
}
if (url.pathname === "/pdfjs/legacy/pdf.worker.min.mjs") {
return new Response(PDFJS_WORKER_TEXT, {
headers: {
"content-type": "text/javascript; charset=utf-8",
"cache-control": "public, max-age=86400",
},
});
}
// ---- Static files ----
const staticFiles: Record<string, { body: string; type: string }> = {
"/": { body: INDEX_HTML, type: "text/html; charset=utf-8" },
@@ -503,7 +522,6 @@ console.log(
}`,
);
console.log(`${PREFIX} Documents loaded: ${documents.length}`);
console.log(`${PREFIX} PDF.js version: 6.1.200`);
if (loadError) console.error(`${PREFIX} ${loadError}`);
// ------- Window management -------