Skip to content

Open source CapCut alternative.

TypeScript

feat(site): compile the Cut engine to a standalone Mac binary

David Le4deb8fac9b62
diff --git a/site/.gitignore b/site/.gitignore
index 30f2cc01..7c8e197a 100644
--- a/site/.gitignore
+++ b/site/.gitignore
@@ -46,3 +46,6 @@ next-env.d.ts
# Cut (video editor) local runtime data — per-project media/exports + shared library
/projects/
/library/
+
+# Cut engine build output
+/dist/
diff --git a/site/package-lock.json b/site/package-lock.json
index b883a270..c2b42e20 100644
--- a/site/package-lock.json
+++ b/site/package-lock.json
@@ -55,6 +55,7 @@
"@types/pg": "^8.20.0",
"@types/react": "^19",
"@types/react-dom": "^19",
+ "bun": "^1.3.14",
"dotenv": "^17.4.2",
"dotenv-cli": "^11.0.0",
"eslint": "^9",
@@ -2610,6 +2611,230 @@
"node": ">=14"
}
},
+ "node_modules/@oven/bun-darwin-aarch64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-darwin-aarch64/-/bun-darwin-aarch64-1.3.14.tgz",
+ "integrity": "sha512-Omj20SuiHBOUjUBIyqtkNjSUIjOtEOJwmbix/ZyFH4BaQ6OZTaaRWIR4TjHVz0yadHgli6lLTiAh1uarnvD49A==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ]
+ },
+ "node_modules/@oven/bun-darwin-x64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-darwin-x64/-/bun-darwin-x64-1.3.14.tgz",
+ "integrity": "sha512-FFj3QdU/OhlDyZOJ8CWfN5eWLpRlT4qjZg7lMQi7jA6GuoY5ajlO1zWLP/MuHYRSbXQUvV52RejNi8DVnAp13w==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ]
+ },
+ "node_modules/@oven/bun-darwin-x64-baseline": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-darwin-x64-baseline/-/bun-darwin-x64-baseline-1.3.14.tgz",
+ "integrity": "sha512-OSfsTZstc898HHElhU4NccaBGOSSDn5VfahiVTnidZ9B/+wb7WTyfZJaBeJcfjwJ9H2W9uTh2TGtl3UfcXgV9g==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "darwin"
+ ]
+ },
+ "node_modules/@oven/bun-freebsd-aarch64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-freebsd-aarch64/-/bun-freebsd-aarch64-1.3.14.tgz",
+ "integrity": "sha512-LIKrXaFxAHybVO5Pf+9XP2FHUj/5APvXTUKk9dqHm5iFz4oH+W24cmhjkJirNujh9hKeTyrpWSe3no9JZKowIw==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "freebsd"
+ ]
+ },
+ "node_modules/@oven/bun-freebsd-x64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-freebsd-x64/-/bun-freebsd-x64-1.3.14.tgz",
+ "integrity": "sha512-uwD+fGUH1ADpIF3B1U2jWzzb20QwRLZfj5QZ28GUCGrAJ/nTmWrD6YYGsblCY1wuhldRez3lU40AyuvSCyLYmw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "freebsd"
+ ]
+ },
+ "node_modules/@oven/bun-linux-aarch64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-aarch64/-/bun-linux-aarch64-1.3.14.tgz",
+ "integrity": "sha512-X5SsPZHs+iYO8R/efIcRtc7gT2Q2DgPfliCxEkx4cXBumwkw0c/EsHMNwH3EgGpCDaZ7IYVPhpCG/xBOQHEwZw==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@oven/bun-linux-aarch64-android": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-aarch64-android/-/bun-linux-aarch64-android-1.3.14.tgz",
+ "integrity": "sha512-y4kq5b85lsrmFb9Xvi4w9mA5IEFJkLMrSmYn06q24KjL9rUWDWO3VFZEtteZxUN5+ec3Zm5S8OnJw1umaCbVjA==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ]
+ },
+ "node_modules/@oven/bun-linux-aarch64-musl": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-aarch64-musl/-/bun-linux-aarch64-musl-1.3.14.tgz",
+ "integrity": "sha512-jmqOA92Cd1NL/1XBd4bFkJLxQ86K0RW7ohxS2qzzAvuitO4JiIxjjTeCspoU44zCozH72HpfZfUE2On31OjnWA==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@oven/bun-linux-x64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-x64/-/bun-linux-x64-1.3.14.tgz",
+ "integrity": "sha512-7OVTAKvwfPmSbIV1HpdOoVVx5VRc427GuPPne93N6vk4eQBPId9nXmZDh9/zGaKPdbVjVtQSZafWQoUjx38Utw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@oven/bun-linux-x64-android": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-x64-android/-/bun-linux-x64-android-1.3.14.tgz",
+ "integrity": "sha512-qe9e1d+3VAEU7nAA2ol9Jvmy/o99PVMSgZhHn7Q/9O3YcDrfEqyQ8zm4zoe5qTEo8HZH0dN03Le0Ys2eQPs7eg==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "android"
+ ]
+ },
+ "node_modules/@oven/bun-linux-x64-baseline": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-x64-baseline/-/bun-linux-x64-baseline-1.3.14.tgz",
+ "integrity": "sha512-q/8EdOC0yUE8FPeoOVq8/Pw5I9/tJaYmUfO/uDUAREx8IUnOJH1RJ5A3BjFqre8pvJoiZA9AovPJq5FnNNjSxA==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@oven/bun-linux-x64-musl": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-x64-musl/-/bun-linux-x64-musl-1.3.14.tgz",
+ "integrity": "sha512-GBCB/k/sIqcr06eTNgg7g46qiUv35Jasx4XiccJ/n7RGqrE4RWUD/XJBbWFprVPjvqd59+QtSnS99XGqvftHfg==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@oven/bun-linux-x64-musl-baseline": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-linux-x64-musl-baseline/-/bun-linux-x64-musl-baseline-1.3.14.tgz",
+ "integrity": "sha512-n6iE71G4lQE4XkrZhQQcL5YUlxDbnq6nqV7zeQi33PMsLT/0kYE+RvHOtBWZ3w0wMdXZfINmp63hIb9ijUBGtw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "linux"
+ ]
+ },
+ "node_modules/@oven/bun-windows-aarch64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-windows-aarch64/-/bun-windows-aarch64-1.3.14.tgz",
+ "integrity": "sha512-T7s3x/BsVKQObGU6QDkZeI6wKynzqGbBH1yI77jrrj5siElclxr3DQrDIk8CV4G5/SJq2HHq4kpLyYY2DKCSmA==",
+ "cpu": [
+ "arm64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ]
+ },
+ "node_modules/@oven/bun-windows-x64": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-windows-x64/-/bun-windows-x64-1.3.14.tgz",
+ "integrity": "sha512-mUFWL3BoYkNpjd8e9PqROiFF/1Xeotq20mABJsiQH62jM1g5zqWh4khw1RZ6bX8Q8fWvlPaxG1PjofkmjUi3vg==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ]
+ },
+ "node_modules/@oven/bun-windows-x64-baseline": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/@oven/bun-windows-x64-baseline/-/bun-windows-x64-baseline-1.3.14.tgz",
+ "integrity": "sha512-uIjLUC1S9DWgICzuoMba7vurBJnBruE4S5CxnvmZkdqWVXRzx1Rgu636HoH+k0qeaQCFh3jeG3JQ1y6fRHv0sw==",
+ "cpu": [
+ "x64"
+ ],
+ "dev": true,
+ "license": "MIT",
+ "optional": true,
+ "os": [
+ "win32"
+ ]
+ },
"node_modules/@prisma/adapter-pg": {
"version": "7.8.0",
"resolved": "https://registry.npmjs.org/@prisma/adapter-pg/-/adapter-pg-7.8.0.tgz",
@@ -5072,6 +5297,47 @@
"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
"license": "BSD-3-Clause"
},
+ "node_modules/bun": {
+ "version": "1.3.14",
+ "resolved": "https://registry.npmjs.org/bun/-/bun-1.3.14.tgz",
+ "integrity": "sha512-aB6GVd42x1Y5ie1K16SF+oLGtgSkwX9hgoDdIW88pjvfTccU8F1vfpoOt34QLv0dZ1v3XimtaxPlZUG81Gx9Zg==",
+ "cpu": [
+ "arm64",
+ "x64"
+ ],
+ "dev": true,
+ "hasInstallScript": true,
+ "license": "MIT",
+ "os": [
+ "darwin",
+ "linux",
+ "android",
+ "freebsd",
+ "win32"
+ ],
+ "bin": {
+ "bun": "bin/bun.exe",
+ "bunx": "bin/bunx.exe"
+ },
+ "optionalDependencies": {
+ "@oven/bun-darwin-aarch64": "1.3.14",
+ "@oven/bun-darwin-x64": "1.3.14",
+ "@oven/bun-darwin-x64-baseline": "1.3.14",
+ "@oven/bun-freebsd-aarch64": "1.3.14",
+ "@oven/bun-freebsd-x64": "1.3.14",
+ "@oven/bun-linux-aarch64": "1.3.14",
+ "@oven/bun-linux-aarch64-android": "1.3.14",
+ "@oven/bun-linux-aarch64-musl": "1.3.14",
+ "@oven/bun-linux-x64": "1.3.14",
+ "@oven/bun-linux-x64-android": "1.3.14",
+ "@oven/bun-linux-x64-baseline": "1.3.14",
+ "@oven/bun-linux-x64-musl": "1.3.14",
+ "@oven/bun-linux-x64-musl-baseline": "1.3.14",
+ "@oven/bun-windows-aarch64": "1.3.14",
+ "@oven/bun-windows-x64": "1.3.14",
+ "@oven/bun-windows-x64-baseline": "1.3.14"
+ }
+ },
"node_modules/bundle-name": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/bundle-name/-/bundle-name-4.1.0.tgz",
diff --git a/site/package.json b/site/package.json
index 3381507e..fc945b01 100644
--- a/site/package.json
+++ b/site/package.json
@@ -13,7 +13,9 @@
"db:generate": "prisma generate",
"db:pull": "prisma db pull",
"prisma:dev": "dotenv -e .env.development -- npx prisma migrate dev",
- "prisma:prod": "dotenv -e .env -- prisma db push"
+ "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"
},
"dependencies": {
"@ai-sdk/react": "^4.0.15",
@@ -61,6 +63,7 @@
"@types/pg": "^8.20.0",
"@types/react": "^19",
"@types/react-dom": "^19",
+ "bun": "^1.3.14",
"dotenv": "^17.4.2",
"dotenv-cli": "^11.0.0",
"eslint": "^9",
diff --git a/site/scripts/build-cut-engine.sh b/site/scripts/build-cut-engine.sh
new file mode 100755
index 00000000..307e6206
--- /dev/null
+++ b/site/scripts/build-cut-engine.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+# Compile the Donkey Cut engine into single-file Mac binaries.
+#
+# scripts/build-cut-engine.sh # arm64 (default)
+# ENGINE_ARCHES="arm64 x64" scripts/... # both architectures
+#
+# Output: dist/cut-engine/donkey-cut-engine-<arch>. The Donkey app bundles
+# the binary and spawns it with DONKEY_CUT_ENGINE=1 (see plans/cut-engine.md).
+set -euo pipefail
+cd "$(dirname "$0")/.."
+
+read -ra arches <<< "${ENGINE_ARCHES:-arm64}"
+mkdir -p dist/cut-engine
+
+for arch in "${arches[@]}"; do
+ out="dist/cut-engine/donkey-cut-engine-$arch"
+ echo "==> bun build --compile ($arch)"
+ npx bun build --compile --minify \
+ --target="bun-darwin-$arch" \
+ src/cut/engine/main.ts \
+ --outfile "$out"
+ echo "==> built $out ($(du -h "$out" | cut -f1))"
+done
diff --git a/site/src/cut/engine/config.ts b/site/src/cut/engine/config.ts
new file mode 100644
index 00000000..921466a8
--- /dev/null
+++ b/site/src/cut/engine/config.ts
@@ -0,0 +1,19 @@
+import { DEFAULT_ENGINE_PORT } from "../lib/ports";
+
+/**
+ * The port the engine binds, from DONKEY_CUT_PORT when set. A bad value throws
+ * rather than silently binding a random OS-assigned port that the client's
+ * fixed probe list would never find.
+ */
+export function enginePort(): number {
+ const raw = process.env.DONKEY_CUT_PORT;
+ if (raw === undefined || raw === "") return DEFAULT_ENGINE_PORT;
+ const n = Number(raw);
+ if (!Number.isInteger(n) || n < 1 || n > 65535) {
+ throw new Error(`DONKEY_CUT_PORT must be an integer 1–65535, got "${raw}".`);
+ }
+ return n;
+}
+
+/** The engine's own loopback base URL (honors DONKEY_CUT_PORT). */
+export const engineBaseUrl = () => `http://127.0.0.1:${enginePort()}`;
diff --git a/site/src/cut/engine/main.ts b/site/src/cut/engine/main.ts
new file mode 100644
index 00000000..04e153a4
--- /dev/null
+++ b/site/src/cut/engine/main.ts
@@ -0,0 +1,23 @@
+/**
+ * Donkey Cut engine entry — the binary the Donkey Mac app spawns.
+ *
+ * Primes the engine environment BEFORE any server module loads (data roots
+ * are computed at module load time), then dispatches:
+ *
+ * donkey-cut-engine serve the Cut API on 127.0.0.1
+ * donkey-cut-engine mcp-proxy <base> <key> stdio MCP proxy (spawned by the
+ * engine itself for AI chats)
+ */
+process.env.DONKEY_CUT_ENGINE ??= "1";
+
+const [, , cmd, ...rest] = process.argv;
+
+void (async () => {
+ if (cmd === "mcp-proxy") {
+ const { engineBaseUrl } = await import("./config");
+ const { runMcpProxy } = await import("../server/ai/mcp-proxy-core.mjs");
+ runMcpProxy(rest[0] ?? engineBaseUrl(), rest[1] ?? "");
+ return;
+ }
+ await import("./serve");
+})();
diff --git a/site/src/cut/engine/paths.ts b/site/src/cut/engine/paths.ts
new file mode 100644
index 00000000..1b446b36
--- /dev/null
+++ b/site/src/cut/engine/paths.ts
@@ -0,0 +1,55 @@
+import { execFile } from "node:child_process";
+import os from "node:os";
+import path from "node:path";
+
+import { findOnPath } from "../server/util";
+
+/** Directories a GUI-spawned process misses but user CLIs commonly live in. */
+const COMMON_BIN_DIRS = [
+ "/opt/homebrew/bin",
+ "/usr/local/bin",
+ path.join(os.homedir(), ".local", "bin"),
+ path.join(os.homedir(), ".bun", "bin"),
+ path.join(os.homedir(), ".npm-global", "bin"),
+ "/usr/bin",
+ "/bin",
+ "/usr/sbin",
+ "/sbin",
+];
+
+function loginShellPath(): Promise<string | null> {
+ return new Promise((resolve) => {
+ // Login (non-interactive) shell: runs .zprofile, where PATH is usually
+ // set, without the hang risk of interactive rc files.
+ execFile(
+ "/bin/zsh",
+ ["-lc", 'printf %s "$PATH"'],
+ { timeout: 4000 },
+ (err, stdout) => resolve(err ? null : stdout.trim() || null)
+ );
+ });
+}
+
+/**
+ * The engine is spawned by an app, not a terminal, so it inherits a bare
+ * PATH. Rebuild it: the app's bundled tools first (bundled tool always wins),
+ * then the user's login-shell PATH, common install dirs, and whatever was
+ * already there.
+ */
+export async function widenPath(): Promise<void> {
+ const parts: string[] = [];
+ const push = (p?: string | null) => {
+ if (!p) return;
+ for (const dir of p.split(":")) {
+ if (dir && !parts.includes(dir)) parts.push(dir);
+ }
+ };
+ push(process.env.DONKEY_CUT_TOOLS_DIR);
+ push(await loginShellPath());
+ for (const dir of COMMON_BIN_DIRS) push(dir);
+ push(process.env.PATH);
+ process.env.PATH = parts.join(":");
+}
+
+/** First executable named `name` on the (widened) PATH, or null. */
+export const resolveOnPath = findOnPath;
diff --git a/site/src/cut/engine/serve.ts b/site/src/cut/engine/serve.ts
new file mode 100644
index 00000000..5a71c4be
--- /dev/null
+++ b/site/src/cut/engine/serve.ts
@@ -0,0 +1,137 @@
+import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
+import { Readable } from "node:stream";
+
+import { allowedOrigin, corsHeaders, preflightHeaders } from "../server/cors";
+import { matchCutRoute } from "../server/http/routes";
+import { enginePort } from "./config";
+import { resolveOnPath, widenPath } from "./paths";
+
+// Throws (and exits with a clear message) on a bad DONKEY_CUT_PORT rather than
+// binding a random port the client would never find.
+const PORT = enginePort();
+
+function toWebRequest(req: IncomingMessage, signal: AbortSignal): Request {
+ const headers = new Headers();
+ for (const [k, v] of Object.entries(req.headers)) {
+ if (Array.isArray(v)) for (const x of v) headers.append(k, x);
+ else if (typeof v === "string") headers.set(k, v);
+ }
+ const method = req.method ?? "GET";
+ const hasBody = method !== "GET" && method !== "HEAD";
+ const init: RequestInit = {
+ method,
+ headers,
+ signal,
+ ...(hasBody ? { body: Readable.toWeb(req) as unknown as BodyInit, duplex: "half" } : {}),
+ } as RequestInit;
+ return new Request(`http://127.0.0.1:${PORT}${req.url ?? "/"}`, init);
+}
+
+async function writeResponse(
+ res: Response,
+ out: ServerResponse,
+ cors: string | null,
+ headOnly: boolean
+) {
+ if (cors) for (const [k, v] of Object.entries(corsHeaders(cors))) out.setHeader(k, v);
+ out.statusCode = res.status;
+ res.headers.forEach((v, k) => out.setHeader(k, v));
+ // A HEAD reply carries the GET headers but no body.
+ if (headOnly || !res.body) {
+ void res.body?.cancel();
+ out.end();
+ return;
+ }
+ await new Promise<void>((resolve) => {
+ const stream = Readable.fromWeb(res.body as never);
+ stream.on("error", () => out.destroy());
+ out.on("close", () => {
+ // Client hung up mid-response: tear down the source so its file
+ // descriptor / handle is released instead of reading on to nowhere.
+ if (!out.writableFinished) stream.destroy();
+ resolve();
+ });
+ stream.pipe(out);
+ });
+}
+
+async function start() {
+ await widenPath();
+
+ // The Agent SDK can't resolve its built-in CLI from inside a compiled
+ // binary; point it at the user's own Claude Code install (their login is
+ // the whole point). Missing is fine — the models probe reports it.
+ if (!process.env.DONKEY_CUT_CLAUDE) {
+ const claude = await resolveOnPath("claude");
+ if (claude) process.env.DONKEY_CUT_CLAUDE = claude;
+ }
+
+ const server = createServer((req, res) => {
+ const origin = req.headers.origin ?? "";
+ const cors = allowedOrigin(origin);
+
+ // A present-but-unlisted Origin is a foreign site's browser tab; refuse it
+ // before any handler runs so a malicious page can't drive the local engine.
+ // No Origin header means a same-machine / non-browser caller — allowed.
+ if (origin && !cors) {
+ res.writeHead(403);
+ res.end("Cross-origin request refused.");
+ return;
+ }
+
+ if (req.method === "OPTIONS") {
+ if (cors) {
+ const requested = req.headers["access-control-request-headers"];
+ res.writeHead(204, preflightHeaders(cors, typeof requested === "string" ? requested : null));
+ } else {
+ res.writeHead(204);
+ }
+ res.end();
+ return;
+ }
+
+ const method = req.method ?? "GET";
+ const pathname = (req.url ?? "/").split("?")[0];
+ const match = matchCutRoute(method, pathname);
+ if (!match) {
+ res.writeHead(404, cors ? corsHeaders(cors) : {});
+ res.end("Not found.");
+ return;
+ }
+ if ("methodNotAllowed" in match) {
+ res.writeHead(405, { Allow: match.methodNotAllowed.join(", "), ...(cors ? corsHeaders(cors) : {}) });
+ res.end("Method not allowed.");
+ return;
+ }
+
+ // Abort only on a real client disconnect. On node:http, req 'close' fires
+ // once the request body is fully read — not on hang-up — so keying the
+ // abort off req would cancel every turn the instant its body arrived.
+ const aborter = new AbortController();
+ res.on("close", () => {
+ if (!res.writableFinished) aborter.abort();
+ });
+
+ void (async () => {
+ try {
+ const webReq = toWebRequest(req, aborter.signal);
+ const webRes = await match.handler(webReq, match.params);
+ await writeResponse(webRes, res, cors, match.head);
+ } catch (err) {
+ if (!res.headersSent) {
+ res.writeHead(500, {
+ "Content-Type": "application/json",
+ ...(cors ? corsHeaders(cors) : {}),
+ });
+ }
+ res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
+ }
+ })();
+ });
+
+ server.listen(PORT, "127.0.0.1", () => {
+ console.log(`donkey-cut-engine listening on http://127.0.0.1:${PORT}`);
+ });
+}
+
+void start();