fix(docker): polyfill DOMMatrix/ImageData/Path2D for pdfjs-dist in linux/amd64 container via NODE_OPTIONS --require

This commit is contained in:
Chandler Copeland
2026-04-03 18:02:39 -06:00
parent f15e538f5c
commit 2d2a43a3c9
5 changed files with 322 additions and 41 deletions

View File

@@ -0,0 +1,23 @@
/**
* Next.js instrumentation hook — runs once before any route handler.
* Polyfills browser globals required by pdfjs-dist at module load time.
* @napi-rs/canvas normally provides these but may be absent in some server
* environments (e.g. linux/amd64 Docker image on ARM build host).
* Text extraction never actually calls these — pdfjs needs the classes defined.
*/
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
if (typeof globalThis.DOMMatrix === 'undefined') {
// @ts-expect-error minimal stub
globalThis.DOMMatrix = class DOMMatrix { constructor() { return this; } };
}
if (typeof globalThis.ImageData === 'undefined') {
// @ts-expect-error minimal stub
globalThis.ImageData = class ImageData { constructor() { return this; } };
}
if (typeof globalThis.Path2D === 'undefined') {
// @ts-expect-error minimal stub
globalThis.Path2D = class Path2D { constructor() { return this; } };
}
}
}