Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

perf(workers): lazily load PDF and OCR dependencies

Mohamed Bassem82ca073f1cb8
diff --git a/apps/workers/workers/assetPreprocessingWorker.ts b/apps/workers/workers/assetPreprocessingWorker.ts
index 83ae403b..8182d372 100644
--- a/apps/workers/workers/assetPreprocessingWorker.ts
+++ b/apps/workers/workers/assetPreprocessingWorker.ts
@@ -1,9 +1,6 @@
import os from "os";
import { and, eq } from "drizzle-orm";
import { workerStatsCounter } from "metrics";
-import PDFParser from "pdf2json";
-import { fromBuffer } from "pdf2pic";
-import { createWorker } from "tesseract.js";
import { withWorkerEventLog, withWorkerTracing } from "workerTracing";
import type { AssetPreprocessingRequest } from "@karakeep/shared-server";
@@ -123,6 +120,7 @@ async function readImageText(buffer: Buffer) {
if (serverConfig.ocr.langs.length == 1 && serverConfig.ocr.langs[0] == "") {
return null;
}
+ const { createWorker } = await import("tesseract.js");
const worker = await createWorker(serverConfig.ocr.langs, undefined, {
cachePath: serverConfig.ocr.cacheDir ?? os.tmpdir(),
});
@@ -173,6 +171,7 @@ async function readPDFText(buffer: Buffer): Promise<{
text: string;
metadata: Record<string, object>;
}> {
+ const { default: PDFParser } = await import("pdf2json");
return new Promise((resolve, reject) => {
const pdfParser = new PDFParser(null, true);
pdfParser.on("pdfParser_dataError", reject);
@@ -208,6 +207,7 @@ export async function extractAndSavePDFScreenshot(
`[assetPreprocessing][${jobId}] Attempting to generate PDF screenshot for bookmarkId: ${bookmark.id}`,
);
try {
+ const { fromBuffer } = await import("pdf2pic");
/**
* If you encountered any issues with this library, make sure you have ghostscript and graphicsmagick installed following this URL
* https://github.com/yakovmeister/pdf2image/blob/HEAD/docs/gm-installation.md