The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Split soundtrack: the mix export skips the proxy and reads at most a part
Claudeda6964bb9692
diff --git a/next.config.ts b/next.config.tsindex 077fc063..7b148572 100644--- a/next.config.ts+++ b/next.config.ts@@ -8,9 +8,10 @@ const nextConfig: NextConfig = {experimental: {// The sign-in proxy holds every request body it sees in memory, up to this size, even one it refuses: a small limit// keeps many open requests from filling the server's memory. The routes that take a file in a form (uploads, a Soul- // finish, a Layers render, a skill, an Assist source, an explainer document) skip the proxy and check the asker- // themselves (src/proxy.ts matcher). Everything else sends JSON: Evolution's 2 MB frame is the largest. A body over- // the limit reaches the route cut short, with no error (POST /api/generations then says it is too large).+ // finish, a Layers render, a skill, an Assist source, an explainer document) and Split soundtrack's mix export (16 MB+ // parts) skip the proxy and check the asker themselves (src/proxy.ts matcher). Everything else sends JSON: Evolution's+ // 2 MB frame is the largest. A body over the limit reaches the route cut short, with no error (POST /api/generations+ // then says it is too large).proxyClientMaxBodySize: "4mb",},async headers() {diff --git a/src/app/api/audio-tools/export/route.ts b/src/app/api/audio-tools/export/route.tsindex 6ea5a9b3..fafd615c 100644--- a/src/app/api/audio-tools/export/route.ts+++ b/src/app/api/audio-tools/export/route.ts@@ -1,3 +1,4 @@+import { gateAsProxy, readBodyWithin } from "@/server/direct-gate";import { MixExportError, PART_MAX, receiveMixPart } from "@/server/mix-export";/**@@ -5,11 +6,16 @@ import { MixExportError, PART_MAX, receiveMixPart } from "@/server/mix-export";* the first part's answer, `&last=1&name=` on the last part, which answers with the Library asset (201).*/export async function POST(request: Request) {+ // The proxy doesn't run on this route (a part is bigger than the 4 MB of a body it holds): its checks run here.+ const denied = gateAsProxy(request);+ if (denied) return denied;const q = new URL(request.url).searchParams;const offset = Number(q.get("offset") ?? "0");if (!Number.isSafeInteger(offset) || offset < 0) return Response.json({ error: "Say where the part starts (offset)" }, { status: 400 });- if (Number(request.headers.get("content-length")) > PART_MAX) return Response.json({ error: "That part is too large" }, { status: 413 });- const data = Buffer.from(await request.arrayBuffer());+ // Never more than a part, with or without a Content-Length (a chunked body has none).+ const data = await readBodyWithin(request, PART_MAX);+ if (data === "too-big") return Response.json({ error: "That part is too large" }, { status: 413 });+ if (data === null) return Response.json({ error: "That part didn't arrive whole — export the mix again" }, { status: 400 });try {const r = receiveMixPart({ id: q.get("id"), offset, data, last: q.get("last") === "1", name: q.get("name") });return Response.json(r, { status: "relPath" in r ? 201 : 200 });diff --git a/src/proxy.ts b/src/proxy.tsindex 3a4cd71d..8ccee68b 100644--- a/src/proxy.ts+++ b/src/proxy.ts@@ -257,9 +257,10 @@ export const config = {// Everything except build assets and the public folder's own files: the favicon and app icons,// the demo samples, the preset covers, vendored scripts (the 3D viewer's meshopt decoder) and the// SVGs at the root. Generated SVGs under /api/files stay behind sign-in. The routes that take a file in a form- // (/api/uploads, a Soul finish, a Layers render, a skill upload, an Assist source, an explainer document) make the- // same checks themselves (src/server/direct-gate.ts): the proxy would hold the whole file in memory first.+ // (/api/uploads, a Soul finish, a Layers render, a skill upload, an Assist source, an explainer document), and Split+ // soundtrack's mix export (a WAV in parts of 16 MB), make the same checks themselves (src/server/direct-gate.ts): the+ // proxy would hold the whole file in memory first.matcher: [- "/((?!_next/static|_next/image|favicon\\.ico$|apple-icon\\.png$|samples/|previews/|vendor/|api/uploads$|api/generations/[^/]+/finish$|api/layers/renders$|api/assist/skills/upload$|api/assist/projects/[^/]+/sources$|api/explainer/document$|[^/]+\\.svg$).*)",+ "/((?!_next/static|_next/image|favicon\\.ico$|apple-icon\\.png$|samples/|previews/|vendor/|api/uploads$|api/generations/[^/]+/finish$|api/layers/renders$|api/assist/skills/upload$|api/assist/projects/[^/]+/sources$|api/explainer/document$|api/audio-tools/export$|[^/]+\\.svg$).*)",],};diff --git a/src/server/mix-export.ts b/src/server/mix-export.tsindex 8d15997a..f8d5b91b 100644--- a/src/server/mix-export.ts+++ b/src/server/mix-export.ts@@ -7,10 +7,11 @@ import { adoptFile, scratchDir, type Asset } from "./storage";/*** Split soundtrack's Export mix (and "Put back on the video", which exports first): the browser writes the mix as a- * 16-bit WAV and sends it here in parts of MIX_PART_BYTES, which the sign-in proxy passes whole (it buffers at most- * 210 MB of a body) and the server appends to a scratch file without holding the mix in memory. The first part starts- * with the WAV header, which says how big the file will be (at most MIX_MAX_BYTES); the last one turns the file into an- * upload in the Library. A 5.1 film clip's mix outgrows the 200 MB upload cap after about six minutes; this doesn't.+ * 16-bit WAV and sends it here in parts of MIX_PART_BYTES, which skip the sign-in proxy (it holds at most 4 MB of a+ * body; the route runs its checks and reads no more than PART_MAX) and the server appends to a scratch file without+ * holding the mix in memory. The first part starts with the WAV header, which says how big the file will be (at most+ * MIX_MAX_BYTES); the last one turns the file into an upload in the Library. A 5.1 film clip's mix outgrows the 200 MB+ * upload cap after about six minutes; this doesn't.*/export class MixExportError extends Error {diff --git a/tests/request-body-limits.test.ts b/tests/request-body-limits.test.tsindex 0b3e058f..4690c1fd 100644--- a/tests/request-body-limits.test.ts+++ b/tests/request-body-limits.test.ts@@ -20,6 +20,7 @@ const skillsUpload = await import("@/app/api/assist/skills/upload/route");const sources = await import("@/app/api/assist/projects/[projectId]/sources/route");const explainerDoc = await import("@/app/api/explainer/document/route");const generations = await import("@/app/api/generations/route");+const mixExport = await import("@/app/api/audio-tools/export/route");let owner = "";let account = "";@@ -49,6 +50,8 @@ const BIG = [{ name: "skill upload", path: "/api/assist/skills/upload", call: (r: Request) => skillsUpload.POST(r) },{ name: "Assist sources", path: "/api/assist/projects/p1/sources", call: (r: Request) => sources.POST(r, { params: Promise.resolve({ projectId: "p1" }) } as never) },{ name: "explainer document", path: "/api/explainer/document", call: (r: Request) => explainerDoc.POST(r) },+ // PR #15: Split soundtrack's mix export takes its WAV as the body, in parts of 16 MB (a library change: the owner's).+ { name: "mix export", path: "/api/audio-tools/export", call: (r: Request) => mixExport.POST(r) },];function form(pathname: string, init: { cookie?: string; headers?: Record<string, string> } = {}) {@@ -67,7 +70,7 @@ describe("M-3: the proxy holds at most a few MB of any request body", () => {it("leaves out only the routes that take a file in a form", () => {const matches = (p: string) => new RegExp(`^${config.matcher[0]}$`).test(p);for (const r of BIG) expect(matches(r.path), r.path).toBe(false);- for (const p of ["/api/generations", "/api/generations/g1", "/api/generations/g1/cancel", "/api/layers", "/api/layers/abc", "/api/assist/skills", "/api/assist/projects/p1", "/api/assist/projects/p1/sources/s1", "/api/explainer"]) {+ for (const p of ["/api/generations", "/api/generations/g1", "/api/generations/g1/cancel", "/api/layers", "/api/layers/abc", "/api/assist/skills", "/api/assist/projects/p1", "/api/assist/projects/p1/sources/s1", "/api/explainer", "/api/audio-tools"]) {expect(matches(p), p).toBe(true);}});diff --git a/tests/split-soundtrack.test.ts b/tests/split-soundtrack.test.tsindex db02a9d3..b21ef8d4 100644--- a/tests/split-soundtrack.test.ts+++ b/tests/split-soundtrack.test.ts@@ -21,6 +21,7 @@ import {installedDbs,missingDbs,MIX_MAX_BYTES,+ MIX_PART_BYTES,mixerBudget,mixerBytes,mixerProblem,@@ -63,6 +64,7 @@ const { getAsset, saveFile, scratchDir, storedMedia } = await import("@/server/sconst gens = await import("@/server/generations");const route = await import("@/app/api/audio-tools/route");const exportRoute = await import("@/app/api/audio-tools/export/route");+const { PART_MAX } = await import("@/server/mix-export");beforeAll(() => setSetting("demoMode", "auto"));afterAll(() => fs.rmSync(dir, { recursive: true, force: true }));@@ -584,4 +586,34 @@ describe("POST /api/audio-tools/export (a mix sent in parts)", () => {new DataView(huge.buffer).setUint32(4, MIX_MAX_BYTES, true);expect((await send("offset=0", huge)).status).toBe(413);});++ it("takes a whole 16 MB part, sent without a length, and never reads past a part", async () => {+ // The sign-in proxy holds 4 MB of a body at most (next.config.ts): this route skips it and reads the part itself.+ const plan = mixPlan([{ channels: [new Float32Array(MIX_PART_BYTES / 2)], gain: 1 }], 48000);+ const part = mixPart(plan, 0, plan.frames);+ expect(part.length).toBe(MIX_PART_BYTES + 44);+ const stream = (bytes: Uint8Array, chunk = 1024 ** 2) => {+ let at = 0;+ let pulled = 0;+ const body = new ReadableStream<Uint8Array>({+ pull(c) {+ if (at >= bytes.length) return c.close();+ const next = bytes.subarray(at, at + chunk);+ at += next.length;+ pulled += next.length;+ c.enqueue(next);+ },+ });+ return { body, pulled: () => pulled };+ };+ const whole = stream(part);+ const req = (q: string, body: ReadableStream<Uint8Array>) => new Request(`http://x/api/audio-tools/export?${q}`, { method: "POST", body, duplex: "half" } as RequestInit);+ const res = await exportRoute.POST(req("offset=0&last=1&name=Big", whole.body));+ expect(res.status).toBe(201);+ expect(fs.statSync(path.join(dir, "files", getAsset((await res.json()).id)!.relPath)).size).toBe(part.length);+ // A body past a part's limit stops being read soon after it.+ const over = stream(new Uint8Array(PART_MAX * 3));+ expect((await exportRoute.POST(req("offset=0", over.body))).status).toBe(413);+ expect(over.pulled()).toBeLessThanOrEqual(PART_MAX + 2 * 1024 ** 2);+ });});