Skip to content

Commit 49bf3e8

Browse files
committed
Document-extract: address codex review (Windows trailing slash, cache sentinel, test name)
1 parent 4779c38 commit 49bf3e8

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

extensions/document-extract/document-extractor.standard-fonts.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("PDF document extractor with standard fonts", () => {
3939
}
4040
});
4141

42-
it("reuses the cached standard-font path across calls", async () => {
42+
it("extracts the same text on repeated calls without leaking state", async () => {
4343
const extractor = createPdfDocumentExtractor();
4444
const first = await extractor.extract({
4545
buffer: helloPdfBuffer,

extensions/document-extract/document-extractor.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createRequire } from "node:module";
2-
import { dirname, join, sep } from "node:path";
2+
import { dirname, join } from "node:path";
33
import type {
44
DocumentExtractedImage,
55
DocumentExtractionRequest,
@@ -56,24 +56,29 @@ const MAX_RENDER_DIMENSION = 10_000;
5656

5757
let canvasModulePromise: Promise<CanvasModule> | null = null;
5858
let pdfJsModulePromise: Promise<PdfJsModule> | null = null;
59-
let standardFontDataUrlCache: string | null = null;
60-
61-
// pdf.js requires a filesystem path (with trailing separator) to resolve the
62-
// fonts shipped under `pdfjs-dist/standard_fonts/`. Without it, any PDF that
63-
// references the 14 standard fonts (Helvetica, Times, Courier, Symbol,
64-
// ZapfDingbats) yields empty text plus a noisy `UnknownErrorException`.
65-
// pdf.js silently rejects `file://` URLs on Node, so we pass a plain path.
59+
// `null` = not resolved yet, `false` = resolution failed once, `string` = resolved path.
60+
let standardFontDataUrlCache: string | false | null = null;
61+
62+
// pdf.js requires a filesystem path that ends in `/` (it throws
63+
// `Invalid factory url ... must include trailing slash` from
64+
// `getFactoryUrlProp`). Without it, any PDF that references the 14 standard
65+
// fonts (Helvetica, Times, Courier, Symbol, ZapfDingbats) yields empty text
66+
// plus a noisy `UnknownErrorException`. pdf.js silently rejects `file://`
67+
// URLs on Node, so we pass a plain path.
6668
function resolveStandardFontDataUrl(): string | undefined {
69+
if (standardFontDataUrlCache === false) {
70+
return undefined;
71+
}
6772
if (standardFontDataUrlCache !== null) {
68-
return standardFontDataUrlCache || undefined;
73+
return standardFontDataUrlCache;
6974
}
7075
try {
7176
const requireFn = createRequire(import.meta.url);
7277
const pkgPath = requireFn.resolve("pdfjs-dist/package.json");
73-
standardFontDataUrlCache = join(dirname(pkgPath), "standard_fonts") + sep;
78+
standardFontDataUrlCache = `${join(dirname(pkgPath), "standard_fonts")}/`;
7479
return standardFontDataUrlCache;
7580
} catch {
76-
standardFontDataUrlCache = "";
81+
standardFontDataUrlCache = false;
7782
return undefined;
7883
}
7984
}

0 commit comments

Comments
 (0)