Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): land tab exports through the request reader [rebuild]

David Le664fa5581023
diff --git a/site/src/cut/server/jobs.test.ts b/site/src/cut/server/jobs.test.ts
index 5ec498dd..7c78a9a9 100644
--- a/site/src/cut/server/jobs.test.ts
+++ b/site/src/cut/server/jobs.test.ts
@@ -229,3 +229,36 @@ describe("preview publication", () => {
});
}
});
+
+/** The slice of Bun's server the hand-in test starts; the site's types carry no Bun globals. */
+declare const Bun: {
+ serve(options: { port: number; fetch(req: Request): Promise<Response> }): { url: URL; stop(force: boolean): void };
+};
+
+describe("a tab render handed in over HTTP", () => {
+ // The tab PUTs its file to the engine's Bun server, whose request body has
+ // to survive the awaits that open the file it lands in.
+ test("lands the file and its stems from a real request body", async () => {
+ const projectId = (await projects.createProject("Wire Render")).id;
+ const job = await jobs.createClientJob(projectId, "mp4", undefined, "fileAndStems");
+ const server = Bun.serve({
+ port: 0,
+ fetch: async (req) => {
+ const settled = new URL(req.url).pathname === "/stems"
+ ? await jobs.completeClientStems(job.id, req.body)
+ : await jobs.completeClientJob(job.id, req.body);
+ return Response.json({ status: settled?.status ?? null, error: settled?.error ?? null });
+ },
+ });
+ try {
+ const put = (where: string, text: string) =>
+ fetch(new URL(where, server.url), { method: "PUT", body: text }).then((r) => r.json());
+ expect(await put("/stems", "zip bytes")).toEqual({ status: "running", error: null });
+ expect(await put("/file", "mp4 bytes")).toEqual({ status: "done", error: null });
+ const file = path.join(projects.exportsDir(projectId), "Wire Render.mp4");
+ expect(await readFile(file, "utf8")).toBe("mp4 bytes");
+ } finally {
+ server.stop(true);
+ }
+ });
+});
diff --git a/site/src/cut/server/jobs.ts b/site/src/cut/server/jobs.ts
index baee6975..e85f7ee1 100644
--- a/site/src/cut/server/jobs.ts
+++ b/site/src/cut/server/jobs.ts
@@ -400,6 +400,28 @@ export function progressClientJob(id: string, ratio: number): void {
if (Number.isFinite(ratio)) job.progress = Math.max(job.progress, Math.min(1, Math.max(0, ratio)));
}
+/** Stream a request body into `file`, chunk by chunk. A body whose connection
+ * drops rejects the read, so the caller's catch runs. It reads through the
+ * stream's own reader: under Bun a request body's async iterator throws
+ * "undefined is not a function" once an await has left the tick, and the Node
+ * stream adapter hangs on the same body and leaks the rejection. */
+async function writeBody(body: ReadableStream<Uint8Array>, file: string): Promise<void> {
+ const reader = body.getReader();
+ const out = await open(file, "w");
+ try {
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) {
+ return;
+ }
+ await out.write(value);
+ }
+ } finally {
+ reader.releaseLock();
+ await out.close();
+ }
+}
+
/**
* The finished file, streamed into exports/ under the claimed name. Null when
* the job is no longer a running tab render — canceled from another dock, or
@@ -415,15 +437,7 @@ export async function completeClientJob(
const partial = `${job.outPath}.part`;
try {
if (!body) throw new Error("The export arrived without a file.");
- // Read chunk by chunk into a file handle. A body whose connection drops
- // rejects the loop, so the catch below runs; under Bun the Node stream
- // adapter hangs on the same body and leaks the rejection.
- const file = await open(partial, "w");
- try {
- for await (const chunk of body) await file.write(chunk);
- } finally {
- await file.close();
- }
+ await writeBody(body, partial);
await rename(partial, job.outPath);
job.status = "done";
job.progress = 1;
@@ -452,12 +466,7 @@ export async function completeClientStems(
const partial = `${job.stemsPath}.part`;
try {
if (!body) throw new Error("The stems arrived without a file.");
- const file = await open(partial, "w");
- try {
- for await (const chunk of body) await file.write(chunk);
- } finally {
- await file.close();
- }
+ await writeBody(body, partial);
await rename(partial, job.stemsPath);
} catch (err) {
await rm(partial, { force: true });