Skip to content

Open source CapCut alternative.

TypeScript

fix(worker): build the bundle for production and refuse one that points at localhost

David Le9d0d2c0b1c70
diff --git a/site/package.json b/site/package.json
index a6fe52db..94e98240 100644
--- a/site/package.json
+++ b/site/package.json
@@ -19,7 +19,7 @@
"prisma:prod": "dotenv -e .env -- prisma db push",
"engine:dev": "bun run src/cut/engine/main.ts",
"engine:build": "bash scripts/build-cut-engine.sh",
- "worker:build": "bun build --target=node --minify --external=pg-native --external=skia-canvas --external=mediabunny --external=@mediabunny/server --external=node-web-audio-api --loader .css:text src/cut/worker/main.ts --outfile dist/cut-worker/main.js",
+ "worker:build": "NODE_ENV=production bun build --target=node --minify --external=pg-native --external=skia-canvas --external=mediabunny --external=@mediabunny/server --external=node-web-audio-api --loader .css:text src/cut/worker/main.ts --outfile dist/cut-worker/main.js && bun scripts/check-worker-bundle.ts",
"bench:watch": "bun run scripts/bench-watch.ts",
"eval:cut-chat": "bun run scripts/eval-cut-chat.ts",
"eval:cut-queue": "bun run scripts/eval-cut-queue.ts",
diff --git a/site/scripts/check-worker-bundle.ts b/site/scripts/check-worker-bundle.ts
new file mode 100644
index 00000000..26958ea3
--- /dev/null
+++ b/site/scripts/check-worker-bundle.ts
@@ -0,0 +1,11 @@
+// The worker bundle is built once and runs in production, so the bundler
+// folds process.env.NODE_ENV at build time. A build that folded it to
+// "development" points every hosted call at localhost, and the container's
+// only symptom is "fetch failed" on each job. Refuse to ship that bundle.
+import { readFileSync } from "node:fs";
+
+const bundle = readFileSync("dist/cut-worker/main.js", "utf8");
+if (bundle.includes("http://localhost:3000")) {
+ console.error("worker bundle points hosted calls at localhost: CUT_HOSTED_ORIGIN (src/cut/lib/hosts.ts) folded with NODE_ENV unset at build time.");
+ process.exit(1);
+}
diff --git a/site/src/cut/lib/headless/http.ts b/site/src/cut/lib/headless/http.ts
index 4dbbe9bd..c7f52a32 100644
--- a/site/src/cut/lib/headless/http.ts
+++ b/site/src/cut/lib/headless/http.ts
@@ -54,6 +54,11 @@ export async function fetchWithRetry(
}
await wait(BACKOFF_BASE_MS * 2 ** (attempt - 1));
}
- const detail = last instanceof Error ? last.message : String(last);
+ // Node's fetch says only "fetch failed"; the network error underneath
+ // (DNS, refused, reset, certificate) rides on `cause`, with its code.
+ const inner = last instanceof Error && last.cause instanceof Error ? last.cause : null;
+ const code = (inner as { code?: string } | null)?.code;
+ const cause = inner ? `: ${inner.message}${code && !inner.message.includes(code) ? ` [${code}]` : ""}` : "";
+ const detail = last instanceof Error ? `${last.message}${cause}` : String(last);
throw new Error(`Could not ${what} — the service did not answer (${detail}).`);
}
diff --git a/site/src/cut/worker/wrangler.jsonc b/site/src/cut/worker/wrangler.jsonc
index cebc5039..85a9e189 100644
--- a/site/src/cut/worker/wrangler.jsonc
+++ b/site/src/cut/worker/wrangler.jsonc
@@ -28,7 +28,11 @@
// 12 GiB for every second a replica is up, which is why a wake starts
// only the replicas the queue needs. Cloudflare validation: >= 3 GiB
// memory per vCPU, disk <= 2x memory.
- "instance_type": { "vcpu": 4, "memory_mib": 12288, "disk_mb": 20000 }
+ "instance_type": { "vcpu": 4, "memory_mib": 12288, "disk_mb": 20000 },
+ // Every replica exits within a minute of an empty queue, so a deploy
+ // replaces them all at once; a staged rollout only keeps old code
+ // claiming jobs for longer.
+ "rollout_step_percentage": 100
}
],
"durable_objects": {