Skip to content

Build your personal knowledge base with Trilium Notes

build(server): bundle the Tesseract.js worker instead of copying the package

Elian Doran5d792e148149
diff --git a/apps/desktop/scripts/build.ts b/apps/desktop/scripts/build.ts
index 6d259fc0b4..2d1e0b98ae 100644
--- a/apps/desktop/scripts/build.ts
+++ b/apps/desktop/scripts/build.ts
@@ -20,6 +20,7 @@ async function main() {
// The image compression worker, which lives in the server it embeds. Built here too so the
// desktop app can compress off-thread rather than falling back to doing it in the main process.
await build.buildBackend([ "../server/src/services/image_worker.ts" ]);
+ await build.buildTesseractWorker("../server/src/services/ocr/tesseract_worker.ts");
// Copy assets.
build.copy("src/assets", "assets/");
diff --git a/apps/server/scripts/build.ts b/apps/server/scripts/build.ts
index dd9cddb3b5..298cf0ae3d 100644
--- a/apps/server/scripts/build.ts
+++ b/apps/server/scripts/build.ts
@@ -9,6 +9,7 @@ async function main() {
// Its own call so it lands beside the bundle rather than under a `services/` path: the pool
// looks for it next to whatever is running, and desktop builds it the same way.
await build.buildBackend([ "src/services/image_worker.ts" ]);
+ await build.buildTesseractWorker("src/services/ocr/tesseract_worker.ts");
// Copy assets
build.copy("src/assets", "assets/");
diff --git a/apps/server/src/services/ocr/tesseract_recognizer.spec.ts b/apps/server/src/services/ocr/tesseract_recognizer.spec.ts
index 74bd334216..8325f347f8 100644
--- a/apps/server/src/services/ocr/tesseract_recognizer.spec.ts
+++ b/apps/server/src/services/ocr/tesseract_recognizer.spec.ts
@@ -15,10 +15,13 @@ vi.mock("tesseract.js", () => ({
}));
// Avoid touching the real filesystem for the worker cache directory.
+const mockFs = {
+ mkdirSync: vi.fn(),
+ existsSync: vi.fn()
+};
+
vi.mock("fs", () => ({
- default: {
- mkdirSync: vi.fn()
- }
+ default: mockFs
}));
vi.mock("../data_dir.js", () => ({
@@ -104,6 +107,23 @@ describe("TesseractRecognizer", () => {
);
});
+ it("spawns the bundled worker when the build placed one beside the bundle", async () => {
+ mockWorker.recognize.mockResolvedValue({ data: pageWithoutBlocks("text", 90) });
+
+ mockFs.existsSync.mockReturnValue(true);
+ await recognizer.recognize(image, "eng");
+ const bundledConfig = mockTesseract.createWorker.mock.calls[0][2];
+ expect(mockFs.existsSync).toHaveBeenCalledWith(expect.stringMatching(/tesseract_worker\.cjs$/));
+ expect(bundledConfig.workerPath).toMatch(/tesseract_worker\.cjs$/);
+
+ // From source there is no bundle, and an explicit `workerPath: undefined` would override
+ // the default tesseract.js resolves inside its own package.
+ mockFs.existsSync.mockReturnValue(false);
+ await recognizer.recognize(image, "ron");
+ const sourceConfig = mockTesseract.createWorker.mock.calls[1][2];
+ expect(sourceConfig).not.toHaveProperty("workerPath");
+ });
+
it("asks tesseract for the per-word breakdown the confidence filter needs", async () => {
mockWorker.recognize.mockResolvedValue({ data: pageWithLines(80, [ [ [ "x", 80 ] ] ]) });
diff --git a/apps/server/src/services/ocr/tesseract_recognizer.ts b/apps/server/src/services/ocr/tesseract_recognizer.ts
index 5f654b0197..c36673c70f 100644
--- a/apps/server/src/services/ocr/tesseract_recognizer.ts
+++ b/apps/server/src/services/ocr/tesseract_recognizer.ts
@@ -1,9 +1,16 @@
import { getLog, options } from "@triliumnext/core";
import fs from "fs";
+import { join } from "path";
import Tesseract from "tesseract.js";
import dataDirs from "../data_dir.js";
+/**
+ * The worker the build bundles from `tesseract_worker.ts` into the bundle root. Running from
+ * source, it does not exist and tesseract.js spawns the worker script inside its own package.
+ */
+const BUNDLED_WORKER_PATH = join(__dirname, "tesseract_worker.cjs");
+
export interface RecognitionResult {
/** Recognized text after per-word confidence filtering. */
text: string;
@@ -65,6 +72,8 @@ class TesseractRecognizer {
getLog().info(`Initializing Tesseract worker for language(s): ${language}`);
const worker = await Tesseract.createWorker(language, 1, {
cachePath: dataDirs.OCR_CACHE_DIR,
+ // Spread rather than passed as `undefined`, which would override the default path.
+ ...(fs.existsSync(BUNDLED_WORKER_PATH) && { workerPath: BUNDLED_WORKER_PATH }),
// Without an errorHandler, tesseract.js rethrows job failures (e.g. undecodable
// images) from its worker message handler as uncaught exceptions — in the desktop
// app that surfaces as Electron's blocking "JavaScript error" dialog (#9754).
diff --git a/apps/server/src/services/ocr/tesseract_worker.ts b/apps/server/src/services/ocr/tesseract_worker.ts
new file mode 100644
index 0000000000..f5e7d325b1
--- /dev/null
+++ b/apps/server/src/services/ocr/tesseract_worker.ts
@@ -0,0 +1,7 @@
+/**
+ * Entry point of the Tesseract.js worker bundle, which `tesseract_recognizer.ts` spawns by path.
+ *
+ * The engine loaders it bundles read their `.wasm` from the directory the bundle is in, so
+ * `BuildHelper.buildTesseractWorker()` copies the engine builds next to it.
+ */
+import "tesseract.js/src/worker-script/node/index.js";
diff --git a/scripts/build-utils.ts b/scripts/build-utils.ts
index e14cce3044..14b51bcd2a 100644
--- a/scripts/build-utils.ts
+++ b/scripts/build-utils.ts
@@ -69,7 +69,10 @@ export default class BuildHelper {
"pdfjs-dist",
"./xhr-sync-worker.js",
"vite",
- "tesseract.js",
+ // tesseract.js requires it only when `global.fetch` is missing, which no
+ // supported Node or Electron version is. Bundling it would pull in
+ // `encoding` and a second iconv-lite.
+ "node-fetch",
// Test fixtures referenced via require.resolve from
// integration-test-only code paths in apps/server. These
// paths are gated at runtime by TRILIUM_INTEGRATION_TEST and
@@ -136,14 +139,23 @@ export default class BuildHelper {
minify: true
});
writeFileSync(join(this.outDir, "meta.json"), JSON.stringify(result.metafile));
+ }
- // Tesseract.js is marked as external above because its worker runs in
- // a separate worker_thread. Copy the worker source, WASM core and all
- // transitive runtime deps so they are available in dist/node_modules.
- this.copyNodeModules([
- "tesseract.js", "tesseract.js-core", "wasm-feature-detect",
- "regenerator-runtime", "is-url", "bmp-js"
- ]);
+ /**
+ * Bundles the Tesseract.js OCR worker into the bundle root and copies the engine builds beside
+ * it, where their loaders read them from, one per CPU feature level.
+ *
+ * Only the full engine builds are copied, not the `-lstm` ones. The Node worker's `getCore()`
+ * compares its `lstmOnly` boolean against OEM numbers, so it never selects an `-lstm` build.
+ *
+ * @param entryPoint the path of `tesseract_worker.ts`, relative to the project.
+ */
+ async buildTesseractWorker(entryPoint: string) {
+ await this.buildBackend([ entryPoint ]);
+ for (const variant of [ "", "-simd", "-relaxedsimd" ]) {
+ const file = `tesseract-core${variant}.wasm`;
+ this.copy(`/node_modules/tesseract.js-core/${file}`, file);
+ }
}
buildFrontend() {