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.

refactor: installGlobals workaround for vite

Brad Barbinbd166de7b2d4
diff --git a/apps/carbon/app/entry.server.tsx b/apps/carbon/app/entry.server.tsx
index a439464668..6bc243546a 100644
--- a/apps/carbon/app/entry.server.tsx
+++ b/apps/carbon/app/entry.server.tsx
@@ -8,6 +8,7 @@ import { PassThrough } from "stream";
import logger from "~/lib/logger";
import type { OperatingSystemPlatform } from "./components/OperatingSystem";
import { OperatingSystemContextProvider } from "./components/OperatingSystem";
+import { installAndLockGlobals } from "./utils/installGlobals";
const ABORT_DELAY = 30000;
@@ -15,6 +16,9 @@ const ABORT_DELAY = 30000;
// in a way that doesn't interfere with the vite compiler.
export * from "~/jobs.server";
+// Workaround for vite single fetch mode
+installAndLockGlobals();
+
export default function handleRequest(
request: Request,
responseStatusCode: number,
diff --git a/apps/carbon/app/utils/installGlobals.ts b/apps/carbon/app/utils/installGlobals.ts
new file mode 100644
index 0000000000..20581f6725
--- /dev/null
+++ b/apps/carbon/app/utils/installGlobals.ts
@@ -0,0 +1,114 @@
+import { installGlobals } from "@remix-run/node";
+import { IncomingMessage } from "node:http";
+
+export function installAndLockGlobals() {
+ // @ts-ignore
+ if (global.__installedAndLocked) {
+ return;
+ }
+
+ installGlobals({ nativeFetch: true });
+
+ const File = global.File;
+ const Headers = global.Headers;
+ const Request = global.Request;
+ const Response = global.Response;
+ const fetch = global.fetch;
+ const FormData = global.FormData;
+
+ /* @vercel/remix expects the non-native Headers API
+ see: https://github.com/vercel/remix/blob/325c03d4c395b3c48aee88b1574c4688ee59251e/packages/vercel-remix/server.ts#L105
+ This adds a minimal compatibility layer, though the result is different
+ when multiple values for the same header are added.
+ {
+ "header": [
+ "value1, value2"
+ ]
+ }
+ vs
+ {
+ "header": [
+ "value1",
+ "value2"
+ ]
+ }
+ This should be fine, but hasn't been extensively tested.
+ Need to patch the prototype here since undici holds a reference.
+ */
+ if (!Object.hasOwn(Headers.prototype, "raw")) {
+ Object.defineProperties(Headers.prototype, {
+ raw: {
+ value: function raw() {
+ const entries: Iterable<[string, string]> = this.entries();
+ return Object.fromEntries(
+ Array.from(entries).map(([k, v]) => [k, [v]])
+ );
+ },
+ configurable: true,
+ },
+ });
+ }
+
+ /* @vercel/remix doesn't set duplex when initializing a steaming body
+ see: https://github.com/vercel/remix/blob/325c03d4c395b3c48aee88b1574c4688ee59251e/packages/vercel-remix/server.ts#L83-L94
+ */
+ class PatchedRequest extends Request {
+ constructor(input: RequestInfo | URL, init: RequestInit = {}) {
+ let initPatched = init;
+ if (
+ init.body &&
+ init.body instanceof IncomingMessage &&
+ init.duplex === undefined
+ ) {
+ initPatched = {
+ ...init,
+ duplex: "half",
+ };
+ }
+ super(input, initPatched);
+ }
+ }
+
+ // Ignore future changes to these properties
+ Object.defineProperties(global, {
+ File: {
+ get() {
+ return File;
+ },
+ set() {},
+ },
+ Headers: {
+ get() {
+ return Headers;
+ },
+ set() {},
+ },
+ Request: {
+ get() {
+ return PatchedRequest;
+ },
+ set() {},
+ },
+ Response: {
+ get() {
+ return Response;
+ },
+ set() {},
+ },
+ fetch: {
+ get() {
+ return fetch;
+ },
+ set() {},
+ },
+ FormData: {
+ get() {
+ return FormData;
+ },
+ set() {},
+ },
+ });
+
+ // @ts-ignore
+ global.__installedAndLocked = true;
+}
diff --git a/apps/carbon/vite.config.ts b/apps/carbon/vite.config.ts
index 6a35052f71..7e52128293 100644
--- a/apps/carbon/vite.config.ts
+++ b/apps/carbon/vite.config.ts
@@ -1,15 +1,10 @@
import { vitePlugin as remix } from "@remix-run/dev";
-import { installGlobals } from "@remix-run/node";
import { vercelPreset } from "@vercel/remix/vite";
import path from "node:path";
import { flatRoutes } from "remix-flat-routes";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
-installGlobals({
- nativeFetch: true,
-});
-
export default defineConfig({
ssr: {
noExternal: [