fix(labs): serve the in-browser OCR engine from this origin

Local lab OCR reads a report photo on the device with tesseract.js, whose
defaults load the worker, the WebAssembly core and the language data
from cdn.jsdelivr.net. The production CSP allows workers and fetches only
from this origin, so the engine could not load; where it did load (dev),
the browser of anyone scanning a report contacted a third-party CDN.

The worker, the three LSTM core builds and the deu and eng best_int
language data (about 17 MB) are now copied from pinned packages into
public/tesseract/ at prebuild and predev, and createWorker is pointed at
them with the worker started from its URL rather than a blob. A test
holds the options against the staged file list.
This commit is contained in:
Marc-André Bombeck committed 2026-09-26 15:37:13 +02:00
1 parent 94ac863bdf
commit 2c88acc5f7
6 files changed
+193 -3

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { beforeEach, describe, expect, it, vi } from "vitest";
/**
* The in-browser OCR engine loads from this origin, never from a CDN.
*
* tesseract.js defaults its worker, WebAssembly core and language data to
* cdn.jsdelivr.net. That sends the browser of anyone scanning a lab report
* to a third party, and the production CSP refuses it, so the feature could
* not load. These tests pin the options `createWorker` receives and hold them
* against the files `scripts/stage-tesseract-assets.mjs` actually stages.
*/
const recognize = vi.fn();
const terminate = vi.fn().mockResolvedValue(undefined);
const createWorker = vi.fn();
vi.mock("tesseract.js", () => ({
createWorker: (...args: unknown[]) => createWorker(...args),
}));
import { LOCAL_OCR_ASSET_OPTIONS, ocrImageToText } from "../local-ocr";
beforeEach(() => {
recognize.mockReset();
createWorker.mockReset();
createWorker.mockResolvedValue({ recognize, terminate });
});
describe("local OCR engine assets", () => {
it("starts the worker from same-origin paths, not a CDN or a blob", async () => {
recognize.mockResolvedValue({ data: { text: " Hb 14.2 g/dl " } });
const file = new File(["x"], "scan.png", { type: "image/png" });
await expect(ocrImageToText(file)).resolves.toBe("Hb 14.2 g/dl");
expect(createWorker).toHaveBeenCalledTimes(1);
const [langs, oem, options] = createWorker.mock.calls[0] as [
string,
number,
Record<string, unknown>,
];
expect(langs).toBe("deu+eng");
expect(oem).toBe(1);
expect(options.workerBlobURL).toBe(false);
for (const key of ["workerPath", "corePath", "langPath"]) {
const value = String(options[key]);
expect(value.startsWith("/tesseract/"), `${key}: ${value}`).toBe(true);
}
expect(JSON.stringify(options)).not.toMatch(/https?:|jsdelivr/);
});
it("points at exactly the files the staging script copies", () => {
const script = readFileSync(
join(process.cwd(), "scripts/stage-tesseract-assets.mjs"),
"utf8",
);
// Worker, the three LSTM core builds, and gzipped best_int data for
// both languages. The worker resolves `${corePath}/<build>.wasm.js` and
// `${langPath}/<lang>.traineddata.gz`.
expect(LOCAL_OCR_ASSET_OPTIONS.workerPath).toBe("/tesseract/worker.min.js");
expect(script).toContain('"worker.min.js"');
expect(LOCAL_OCR_ASSET_OPTIONS.corePath).toBe("/tesseract/core");
expect(script).toContain('join("core", file)');
for (const build of [
"tesseract-core-lstm.wasm.js",
"tesseract-core-simd-lstm.wasm.js",
"tesseract-core-relaxedsimd-lstm.wasm.js",
]) {
expect(script).toContain(build);
}
expect(LOCAL_OCR_ASSET_OPTIONS.langPath).toBe("/tesseract/lang");
expect(LOCAL_OCR_ASSET_OPTIONS.gzip).toBe(true);
expect(script).toContain('join("lang", `${lang}.traineddata.gz`)');
expect(script).toContain('"4.0.0_best_int"');
});
});