Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(jobs): shim DOMMatrix for pdfjs in Node runtimes

Sidwebworks61aae674139c
diff --git a/apps/erp/app/polyfill.ts b/apps/erp/app/polyfill.ts
deleted file mode 100644
index 77e4db524b..0000000000
--- a/apps/erp/app/polyfill.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-if (typeof DOMMatrix === "undefined") {
- // @ts-expect-error -- stub for SSR; d3-interpolate uses DOMMatrix for SVG transforms
- globalThis.DOMMatrix = class DOMMatrix {
- constructor() {
- return { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
- }
- };
-}
-
-if (typeof Promise.withResolvers === "undefined") {
- Promise.withResolvers = <T>() => {
- let resolve!: (value: T | PromiseLike<T>) => void;
- let reject!: (reason?: unknown) => void;
- const promise = new Promise<T>((res, rej) => {
- resolve = res;
- reject = rej;
- });
- return { promise, resolve, reject };
- };
-}
diff --git a/apps/erp/app/root.tsx b/apps/erp/app/root.tsx
index d81704fdaf..49a070e2d2 100644
--- a/apps/erp/app/root.tsx
+++ b/apps/erp/app/root.tsx
@@ -44,8 +44,8 @@ import { getMode, setMode } from "~/services/mode.server";
import Background from "~/styles/background.css?url";
import NProgress from "~/styles/nprogress.css?url";
import Tailwind from "~/styles/tailwind.css?url";
+import "@carbon/lib/shims";
import type { Route } from "./+types/root";
-import "./polyfill";
import { getTheme } from "./services/theme.server";
export const middleware = [requestIdMiddleware, flashMiddleware];
diff --git a/apps/erp/app/ssr-shims/canvas-stub.cjs b/apps/erp/app/ssr-shims/canvas-stub.cjs
index ec941e6557..da00975689 100644
--- a/apps/erp/app/ssr-shims/canvas-stub.cjs
+++ b/apps/erp/app/ssr-shims/canvas-stub.cjs
@@ -45,17 +45,14 @@ function createCanvas() {
};
}
-const DOMMatrixStub =
- typeof globalThis.DOMMatrix !== "undefined"
- ? globalThis.DOMMatrix
- : class DOMMatrixStubInner {
- constructor() {}
- };
-
const api = {
createCanvas,
Image: ImageStub,
- DOMMatrix: DOMMatrixStub
+ // Provided by `@carbon/lib/shims`, imported at the app root before Konva's SSR
+ // graph loads. Getter defers resolution past this module's eval order.
+ get DOMMatrix() {
+ return globalThis.DOMMatrix;
+ }
};
module.exports = api;
diff --git a/apps/mes/app/root.tsx b/apps/mes/app/root.tsx
index 96463391da..546ec0e681 100644
--- a/apps/mes/app/root.tsx
+++ b/apps/mes/app/root.tsx
@@ -42,6 +42,7 @@ import { getMode, setMode } from "~/services/mode.server";
import Background from "~/styles/background.css?url";
import NProgress from "~/styles/nprogress.css?url";
import Tailwind from "~/styles/tailwind.css?url";
+import "@carbon/lib/shims";
import type { Route } from "./+types/root";
import { getTheme } from "./services/theme.server";
diff --git a/apps/mes/app/ssr-shims/canvas-stub.cjs b/apps/mes/app/ssr-shims/canvas-stub.cjs
index ec941e6557..da00975689 100644
--- a/apps/mes/app/ssr-shims/canvas-stub.cjs
+++ b/apps/mes/app/ssr-shims/canvas-stub.cjs
@@ -45,17 +45,14 @@ function createCanvas() {
};
}
-const DOMMatrixStub =
- typeof globalThis.DOMMatrix !== "undefined"
- ? globalThis.DOMMatrix
- : class DOMMatrixStubInner {
- constructor() {}
- };
-
const api = {
createCanvas,
Image: ImageStub,
- DOMMatrix: DOMMatrixStub
+ // Provided by `@carbon/lib/shims`, imported at the app root before Konva's SSR
+ // graph loads. Getter defers resolution past this module's eval order.
+ get DOMMatrix() {
+ return globalThis.DOMMatrix;
+ }
};
module.exports = api;
diff --git a/packages/jobs/src/inngest/index.ts b/packages/jobs/src/inngest/index.ts
index af649fc13b..beb97a43ea 100644
--- a/packages/jobs/src/inngest/index.ts
+++ b/packages/jobs/src/inngest/index.ts
@@ -1,3 +1,7 @@
+// Must load before any function module pulls in pdfjs (extract-document), whose
+// init runs `new DOMMatrix()` — undefined in the Node worker without this shim.
+import "@carbon/lib/shims";
+
// Re-export the inngest client and helpers
export { inngest } from "./client.ts";
diff --git a/packages/lib/package.json b/packages/lib/package.json
index 3f26e3968c..118c3e2dec 100644
--- a/packages/lib/package.json
+++ b/packages/lib/package.json
@@ -27,6 +27,7 @@
"./events": "./src/events.ts",
"./inngest": "./src/inngest/client.ts",
"./resend.server": "./src/resend.server.ts",
+ "./shims": "./src/shims.ts",
"./slack.server": "./src/slack.server.ts",
"./trigger": "./src/trigger.ts",
"./twenty.server": "./src/twenty.server.ts"
@@ -38,6 +39,8 @@
"lint": "biome lint --write ",
"typecheck": "tsgo --noEmit"
},
- "sideEffects": false,
+ "sideEffects": [
+ "./src/shims.ts"
+ ],
"version": "0.0.0"
}
diff --git a/packages/lib/src/shims.ts b/packages/lib/src/shims.ts
new file mode 100644
index 0000000000..5dcc792a4b
--- /dev/null
+++ b/packages/lib/src/shims.ts
@@ -0,0 +1,30 @@
+// Runtime shims for environments without browser globals (Node SSR, the Inngest
+// jobs worker). Import for side effects — `import "@carbon/lib/shims"` — at each
+// runtime entry, before anything that touches these globals (notably pdfjs-dist,
+// whose module init runs `new DOMMatrix()`, and d3-interpolate's SSR transforms).
+
+// Returns a plain identity matrix instead of a real instance: consumers only read
+// the a–f components, and this keeps the class body dependency-free.
+export class DOMMatrixShim {
+ constructor() {
+ return { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
+ }
+}
+
+const g = globalThis as any;
+
+if (typeof g.DOMMatrix === "undefined") {
+ g.DOMMatrix = DOMMatrixShim;
+}
+
+if (typeof (Promise as any).withResolvers === "undefined") {
+ (Promise as any).withResolvers = function withResolvers() {
+ let resolve: (value: unknown) => void;
+ let reject: (reason?: unknown) => void;
+ const promise = new Promise((res, rej) => {
+ resolve = res;
+ reject = rej;
+ });
+ return { promise, resolve: resolve!, reject: reject! };
+ };
+}