Open source CapCut alternative.
TypeScript
fix(site): let a cloud save follow its own lost save [rebuild]
David Le5370ec2e4c83
diff --git a/site/prisma/Cut.prisma b/site/prisma/Cut.prismaindex 8fa4abf7..b86d3e64 100644--- a/site/prisma/Cut.prisma+++ b/site/prisma/Cut.prisma@@ -11,6 +11,10 @@ model CutProject {// Optimistic concurrency for autosave: PUT sends the version it loaded and// conflicts 409 instead of clobbering a newer save from another device.version Int @default(1)+ // The transport that saved the current version and that save's place in+ // its sequence, so a save following its own unacknowledged one applies.+ writer String?+ writerSeq Int?previewKey String?createdAt DateTime @default(now())updatedAt DateTime @updatedAtdiff --git a/site/prisma/migrations/20261023113015/migration.sql b/site/prisma/migrations/20261023113015/migration.sqlnew file mode 100644index 00000000..3a6ffaa1--- /dev/null+++ b/site/prisma/migrations/20261023113015/migration.sql@@ -0,0 +1,3 @@+-- AlterTable+ALTER TABLE "CutProject" ADD COLUMN "writer" TEXT,+ADD COLUMN "writerSeq" INTEGER;diff --git a/site/src/cut/lib/backend/cloud.ts b/site/src/cut/lib/backend/cloud.tsindex 280fb2e4..29b6f21a 100644--- a/site/src/cut/lib/backend/cloud.ts+++ b/site/src/cut/lib/backend/cloud.ts@@ -4,7 +4,7 @@// Media bytes ride presigned R2 URLs minted by those routes, not this// transport.import { observeOperationResponse, operationFailure } from "../operationFailure";-import type { CutBackend, CutRequestInit } from "./types";+import { DOC_SEQ_HEADER, DOC_WRITER_HEADER, HTTP_CONFLICT, type CutBackend, type CutRequestInit } from "./types";const cloudPath = (path: string) => path.replace(/^\/api\/cut\//, "/api/cut-cloud/");@@ -14,6 +14,7 @@ const cloudPath = (path: string) => path.replace(/^\/api\/cut\//, "/api/cut-cloulet session: { base: string; headers: Record<string, string> } | null = null;export function bindCloudSession(base: string, headers: Record<string, string>) {session = { base: base.replace(/\/$/, ""), headers };+ sessionWriter = newWriter();}/** A request on the hosted site as this process's user: same-origin in the@@ -33,9 +34,10 @@ export function cloudRequest(path: string, init?: RequestInit): Promise<Response/** Capture transport credentials for work that outlives the active project. */export function captureCloudBackend(): CutBackend {const bound = session ? { base: session.base, headers: { ...session.headers } } : null;+ const writer = newWriter();return {...cloudBackend,- fetch: (path, init) => cloudFetch(path, init, (url, options) => requestWithSession(bound, url, options)),+ fetch: (path, init) => cloudFetch(path, init, (url, options) => requestWithSession(bound, url, options), writer),url: (path) => (bound?.base ?? "") + cloudPath(path),};}@@ -75,6 +77,15 @@ export function pinDocBase(projectId: string, version: string | null) {else basePins.set(projectId, version);}+// A writer is one transport's identity on the server: the page load, a bound+// headless session, or a captured backend. Each save it sends carries the next+// number in its sequence, so a save that follows its own unacknowledged one+// (the response was lost, the save retried on the old version) applies, and a+// delayed save that its own later one overtook answers 409.+type DocWriter = { id: string; seq: number };+const newWriter = (): DocWriter => ({ id: crypto.randomUUID(), seq: 0 });+let sessionWriter = newWriter();+// One doc PUT on the wire per project: each waits for the one before it and// reads the version at dispatch. Two saves in flight would carry the same ?v=// and the later one would 409 against its own session's write.@@ -87,7 +98,12 @@ const docGets = new Map<string, Promise<void>>();// /projects/:id only — /projects/folders is the folder collection, not a doc.const PROJECT_DOC = /^\/api\/cut\/projects\/(?!folders$)([^/?]+)$/;-async function cloudFetch(path: string, options?: CutRequestInit, request = cloudRequest): Promise<Response> {+async function cloudFetch(+ path: string,+ options?: CutRequestInit,+ request = cloudRequest,+ writer = sessionWriter+): Promise<Response> {const { observe, ...init } = options ?? {};const doc = PROJECT_DOC.exec(path);const method = (init?.method ?? "GET").toUpperCase();@@ -110,20 +126,29 @@ async function cloudFetch(path: string, options?: CutRequestInit, request = clou}const prev = putChains.get(projectId) ?? Promise.resolve();- const run = prev.then(() => putDoc(projectId, path, init, request));+ const run = prev.then(() => putDoc(projectId, path, writer, init, request));// The chain survives a failed link; the failure is the caller's to handle.putChains.set(projectId, run.catch(() => undefined));return run;}-async function putDoc(projectId: string, path: string, init?: RequestInit, request = cloudRequest): Promise<Response> {+async function putDoc(+ projectId: string,+ path: string,+ writer: DocWriter,+ init?: RequestInit,+ request = cloudRequest+): Promise<Response> {await docGets.get(projectId);// A pinned base outranks the map; with neither, an unversioned PUT is the// first save and succeeds unconditionally.const v = basePins.get(projectId) ?? docVersions.get(projectId);- const res = await request(cloudPath(path) + (v ? `?v=${encodeURIComponent(v)}` : ""), init);- if (res.ok || res.status === 409) basePins.delete(projectId);- if (res.status === 409) {+ const headers = new Headers(init?.headers);+ headers.set(DOC_WRITER_HEADER, writer.id);+ headers.set(DOC_SEQ_HEADER, String(++writer.seq));+ const res = await request(cloudPath(path) + (v ? `?v=${encodeURIComponent(v)}` : ""), { ...init, headers });+ if (res.ok || res.status === HTTP_CONFLICT) basePins.delete(projectId);+ if (res.status === HTTP_CONFLICT) {const body = (await res.clone().json()diff --git a/site/src/cut/lib/backend/cloudCapture.test.ts b/site/src/cut/lib/backend/cloudCapture.test.tsindex 81cef5bf..5a4dd867 100644--- a/site/src/cut/lib/backend/cloudCapture.test.ts+++ b/site/src/cut/lib/backend/cloudCapture.test.ts@@ -1,5 +1,6 @@import { afterEach, expect, spyOn, test } from "bun:test";-import { bindCloudSession, captureCloudBackend } from "./cloud";+import { bindCloudSession, captureCloudBackend, cloudBackend } from "./cloud";+import { DOC_SEQ_HEADER, DOC_WRITER_HEADER } from "./types";afterEach(() => { bindCloudSession("", {}); });@@ -17,3 +18,22 @@ test("background requests retain captured credentials after the process is rebouexpect(first.url("/api/cut/export/example/file")).toBe("https://first.invalid/api/cut-cloud/export/example/file");} finally { fetcher.mockRestore(); }});++test("each transport saves as its own writer, numbering its saves in order", async () => {+ const fetcher = spyOn(globalThis, "fetch").mockImplementation((async () => Response.json({ ok: true })) as typeof fetch);+ try {+ // A bound session and a backend captured from it are separate writers:+ // the server never builds one's stale save over the other's.+ bindCloudSession("https://worker.invalid", {});+ const captured = captureCloudBackend();+ const put = { method: "PUT", body: "{}" };+ await cloudBackend.fetch("/api/cut/projects/p1", put);+ await cloudBackend.fetch("/api/cut/projects/p1", put);+ await captured.fetch("/api/cut/projects/p1", put);+ const sent = fetcher.mock.calls.map(([, options]) => new Headers(options?.headers));+ const writers = sent.map((h) => h.get(DOC_WRITER_HEADER));+ expect(writers[0]).toBe(writers[1]);+ expect(writers[2]).not.toBe(writers[0]);+ expect(sent.map((h) => h.get(DOC_SEQ_HEADER))).toEqual(["1", "2", "1"]);+ } finally { fetcher.mockRestore(); }+});diff --git a/site/src/cut/lib/backend/types.ts b/site/src/cut/lib/backend/types.tsindex b484f78a..e152fb34 100644--- a/site/src/cut/lib/backend/types.ts+++ b/site/src/cut/lib/backend/types.ts@@ -36,3 +36,13 @@ export interface CutBackend {/** Absolute-or-relative URL for a backend API path (media src, downloads). */url(path: string): string;}++/** Headers a cloud transport stamps on every project doc save: its writer id+ * (one per page load, headless session, or captured backend) and the save's+ * place in that writer's sequence. The server applies a save over a newer+ * version only when the same writer saved it earlier in the sequence. */+export const DOC_WRITER_HEADER = "x-cut-doc-writer";+export const DOC_SEQ_HEADER = "x-cut-doc-seq";++/** The status a versioned doc save answers when another writer moved on. */+export const HTTP_CONFLICT = 409;diff --git a/site/src/cut/server/cloud/projects.test.ts b/site/src/cut/server/cloud/projects.test.tsnew file mode 100644index 00000000..67146083--- /dev/null+++ b/site/src/cut/server/cloud/projects.test.ts@@ -0,0 +1,121 @@+import { beforeEach, describe, expect, mock, test } from "bun:test";+import { DOC_SEQ_HEADER, DOC_WRITER_HEADER } from "@/cut/lib/backend/types";++type Row = {+ id: string;+ userId: string;+ name: string;+ doc: Record<string, unknown>;+ folderId: string | null;+ version: number;+ writer: string | null;+ writerSeq: number | null;+};+let row: Row;+let queries = 0;++const TAB_1 = "00000000-0000-4000-8000-000000000001";+const TAB_2 = "00000000-0000-4000-8000-000000000002";++// The row behaves like Postgres under the versioned write: an update applies+// only when its where-version matches, and bumps the version when it does.+type Data = { doc: Record<string, unknown>; writer: string | null; writerSeq: number | null };+const updateMany = async (args: { where: { version: number }; data: Data }) => {+ queries++;+ if (args.where.version !== row.version) {+ return { count: 0 };+ }+ const { doc, writer, writerSeq } = args.data;+ row = { ...row, doc, writer, writerSeq, version: row.version + 1 };+ return { count: 1 };+};+mock.module("@/lib/prisma", () => ({+ configureDatabasePool: () => {},+ prisma: {+ cutProject: {+ findFirst: async () => {+ queries++;+ return { ...row, previewKey: null, createdAt: new Date(1), updatedAt: new Date(1) };+ },+ updateMany,+ },+ },+}));+const { projectsCloud } = await import("./projects");++const save = (v: number, body: Record<string, unknown>, headers: Record<string, string> = {}) =>+ projectsCloud.put("owner", "p1", new Request(`https://donkeycut.com/api/cut-cloud/projects/p1?v=${v}`, {+ method: "PUT",+ headers: { "Content-Type": "application/json", ...headers },+ body: JSON.stringify(body),+ }));+const as = (writer: string, seq: number) => ({ [DOC_WRITER_HEADER]: writer, [DOC_SEQ_HEADER]: String(seq) });++beforeEach(() => {+ row = {+ id: "p1",+ userId: "owner",+ name: "Growth",+ doc: { name: "Growth", assets: [], clips: [] },+ folderId: null,+ version: 1,+ writer: null,+ writerSeq: null,+ };+ queries = 0;+});++describe("versioned cloud project saves", () => {+ test("a tab saving over its own unacknowledged save is not a conflict", async () => {+ // The first save lands but its response never reaches the tab, so the+ // tab's next save still carries the version it loaded.+ expect((await save(1, { clips: [{ id: "a" }] }, as(TAB_1, 1))).status).toBe(200);+ const res = await save(1, { clips: [{ id: "a" }, { id: "b" }] }, as(TAB_1, 2));+ expect(res.status).toBe(200);+ expect(await res.json()).toEqual({ ok: true, version: 3 });+ expect(row.doc.clips).toEqual([{ id: "a" }, { id: "b" }]);+ });++ test("a delayed save its own later save overtook conflicts", async () => {+ // Save 2 commits first; save 1, sent earlier, arrives after it.+ expect((await save(1, { clips: [{ id: "a" }, { id: "b" }] }, as(TAB_1, 2))).status).toBe(200);+ expect((await save(1, { clips: [{ id: "a" }] }, as(TAB_1, 1))).status).toBe(409);+ expect(row.doc.clips).toEqual([{ id: "a" }, { id: "b" }]);+ });++ test("a save over another writer's newer version conflicts", async () => {+ expect((await save(1, { clips: [{ id: "a" }] }, as(TAB_1, 1))).status).toBe(200);+ expect((await save(1, { clips: [{ id: "b" }] }, as(TAB_2, 1))).status).toBe(409);+ expect((await save(1, { clips: [{ id: "b" }] })).status).toBe(409);+ expect(row.doc.clips).toEqual([{ id: "a" }]);+ });++ test("a writerless save ends the last writer's run", async () => {+ // An API client saves over the tab's version; the tab's retry on the+ // old version must not build over it.+ expect((await save(1, { clips: [{ id: "a" }] }, as(TAB_1, 1))).status).toBe(200);+ expect((await save(2, { clips: [{ id: "api" }] })).status).toBe(200);+ expect((await save(1, { clips: [{ id: "b" }] }, as(TAB_1, 2))).status).toBe(409);+ expect(row.doc.clips).toEqual([{ id: "api" }]);+ });++ test("a cross-device conflict reads the row once", async () => {+ await save(1, { clips: [] }, as(TAB_1, 1));+ queries = 0;+ expect((await save(1, { clips: [] }, as(TAB_2, 1))).status).toBe(409);+ expect(queries).toBe(1);+ });++ test("a malformed writer is refused", async () => {+ expect((await save(1, { clips: [] }, as("x".repeat(4096), 1))).status).toBe(400);+ expect((await save(1, { clips: [] }, { [DOC_WRITER_HEADER]: TAB_1 })).status).toBe(400);+ expect((await save(1, { clips: [] }, as(TAB_1, 0))).status).toBe(400);+ expect(row.version).toBe(1);+ });++ test("the writer stays out of the stored doc", async () => {+ await save(1, { clips: [] }, as(TAB_1, 1));+ expect(JSON.stringify(row.doc)).not.toContain(TAB_1);+ expect(row.writer).toBe(TAB_1);+ });+});diff --git a/site/src/cut/server/cloud/projects.ts b/site/src/cut/server/cloud/projects.tsindex 6fa80336..6ce6e805 100644--- a/site/src/cut/server/cloud/projects.ts+++ b/site/src/cut/server/cloud/projects.ts@@ -3,6 +3,7 @@import { sanitizeGuideLines, sanitizeGuides } from "@/cut/lib/guides";import { resolveParent, subtreeOf } from "@/cut/lib/folderTree";import { timelineEnd } from "@/cut/lib/timelineEnd";+import { DOC_SEQ_HEADER, DOC_WRITER_HEADER, HTTP_CONFLICT } from "@/cut/lib/backend/types";import { isTimelineId, normalizeAspect, sanitizeTimelines, type ProjectDoc, type ProjectFolder, type ProjectSummary } from "@/cut/lib/types";import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";@@ -24,6 +25,15 @@ const projectFolderView = (row: {createdAt: row.createdAt.getTime(),});+const HTTP_BAD_REQUEST = 400;+const HTTP_NOT_FOUND = 404;++// Writer ids are the transport's crypto.randomUUID().+const WRITER_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;+const WRITER_SEQ = /^[1-9][0-9]{0,8}$/;++type DocWriter = { id: string; seq: number };+type ProjectRow = {id: string;userId: string;@@ -31,6 +41,8 @@ type ProjectRow = {doc: unknown;folderId: string | null;version: number;+ writer: string | null;+ writerSeq: number | null;previewKey: string | null;createdAt: Date;updatedAt: Date;@@ -57,6 +69,71 @@ function docOf(row: ProjectRow): ProjectDoc {return { ...doc, folderId: row.folderId ?? null };}+/** The save's writer headers. Both or neither: a save without them is a plain+ * versioned save, and a malformed pair is refused. */+function writerOf(req: Request): DocWriter | null | "invalid" {+ const id = req.headers.get(DOC_WRITER_HEADER);+ const seq = req.headers.get(DOC_SEQ_HEADER);+ if (id === null && seq === null) {+ return null;+ }+ if (!id || !seq || !WRITER_ID.test(id) || !WRITER_SEQ.test(seq)) {+ return "invalid";+ }+ return { id, seq: Number(seq) };+}++/** Whether a save may apply over the row's version though it was made on an+ * older one: the same writer saved that version, earlier in its sequence. Its+ * state only moves forward, so this save already holds what that one did. */+function follows(row: ProjectRow, writer: DocWriter | null): boolean {+ return !!writer && row.writer === writer.id && row.writerSeq !== null && writer.seq > row.writerSeq;+}++/** A save's body over the stored doc: fields the body leaves out keep their+ * stored values. */+function mergeDoc(existing: ProjectDoc, body: Partial<ProjectDoc>): ProjectDoc {+ const doc: ProjectDoc = {+ ...existing,+ name: typeof body.name === "string" && body.name.trim() ? body.name.trim() : existing.name,+ assets: Array.isArray(body.assets) ? body.assets : existing.assets,+ clips: Array.isArray(body.clips) ? body.clips : existing.clips,+ transitions: Array.isArray(body.transitions) ? body.transitions : existing.transitions,+ audioClips: Array.isArray(body.audioClips) ? body.audioClips : existing.audioClips,+ // Same legacy-shape handling as the engine: a merged client saving+ // `clips` clears the old separate overlay array.+ overlayClips: Array.isArray(body.overlayClips)+ ? body.overlayClips+ : Array.isArray(body.clips)+ ? []+ : existing.overlayClips,+ overlays: Array.isArray(body.overlays) ? body.overlays : existing.overlays,+ timeline: isTimelineId(body.timeline) ? body.timeline : existing.timeline,+ timelines: sanitizeTimelines(body.timelines, existing) ?? existing.timelines,+ templates: Array.isArray(body.templates) ? body.templates : existing.templates,+ mediaFolders: Array.isArray(body.mediaFolders) ? body.mediaFolders : existing.mediaFolders,+ aspect: normalizeAspect(body.aspect) ?? existing.aspect,+ guides: Array.isArray(body.guides) ? sanitizeGuides(body.guides) : existing.guides,+ guideLines: body.guideLines !== undefined ? sanitizeGuideLines(body.guideLines) : existing.guideLines,+ background: typeof body.background === "string" ? body.background : existing.background,+ subtitles:+ body.subtitles && typeof body.subtitles === "object" ? body.subtitles : existing.subtitles,+ ui: body.ui && typeof body.ui === "object" ? { ...existing.ui, ...body.ui } : existing.ui,+ publish:+ body.publish && typeof body.publish === "object"+ ? { ...existing.publish, ...body.publish }+ : existing.publish,+ notes:+ body.notes && typeof body.notes === "object"+ ? { ...existing.notes, ...body.notes }+ : existing.notes,+ genvideo: body.genvideo !== undefined ? body.genvideo ?? undefined : existing.genvideo,+ renders: Array.isArray(body.renders) ? body.renders : existing.renders,+ };+ doc.updatedAt = Date.now();+ return doc;+}+/** Mirror of the engine's summarize(): track 0 is the cut. sizeBytes comes from* the project's complete media objects instead of a directory walk. */function summarize(row: ProjectRow, sizeBytes: number): ProjectSummary {@@ -205,67 +282,58 @@ export const projectsCloud = {const row = await getProject(userId, id);if (!row) return err("Project not found.", 404);const vParam = new URL(req.url).searchParams.get("v");- const existing = docOf(row);+ const writer = writerOf(req);+ if (writer === "invalid") {+ return err("Bad writer.", HTTP_BAD_REQUEST);+ }const body = (await req.json()) as Partial<ProjectDoc>;- const doc: ProjectDoc = {- ...existing,- name: typeof body.name === "string" && body.name.trim() ? body.name.trim() : existing.name,- assets: Array.isArray(body.assets) ? body.assets : existing.assets,- clips: Array.isArray(body.clips) ? body.clips : existing.clips,- transitions: Array.isArray(body.transitions) ? body.transitions : existing.transitions,- audioClips: Array.isArray(body.audioClips) ? body.audioClips : existing.audioClips,- // Same legacy-shape handling as the engine: a merged client saving- // `clips` clears the old separate overlay array.- overlayClips: Array.isArray(body.overlayClips)- ? body.overlayClips- : Array.isArray(body.clips)- ? []- : existing.overlayClips,- overlays: Array.isArray(body.overlays) ? body.overlays : existing.overlays,- timeline: isTimelineId(body.timeline) ? body.timeline : existing.timeline,- timelines: sanitizeTimelines(body.timelines, existing) ?? existing.timelines,- templates: Array.isArray(body.templates) ? body.templates : existing.templates,- mediaFolders: Array.isArray(body.mediaFolders) ? body.mediaFolders : existing.mediaFolders,- aspect: normalizeAspect(body.aspect) ?? existing.aspect,- guides: Array.isArray(body.guides) ? sanitizeGuides(body.guides) : existing.guides,- guideLines: body.guideLines !== undefined ? sanitizeGuideLines(body.guideLines) : existing.guideLines,- background: typeof body.background === "string" ? body.background : existing.background,- subtitles:- body.subtitles && typeof body.subtitles === "object" ? body.subtitles : existing.subtitles,- ui: body.ui && typeof body.ui === "object" ? { ...existing.ui, ...body.ui } : existing.ui,- publish:- body.publish && typeof body.publish === "object"- ? { ...existing.publish, ...body.publish }- : existing.publish,- notes:- body.notes && typeof body.notes === "object"- ? { ...existing.notes, ...body.notes }- : existing.notes,- genvideo: body.genvideo !== undefined ? body.genvideo ?? undefined : existing.genvideo,- renders: Array.isArray(body.renders) ? body.renders : existing.renders,++ // Every save records its writer, or clears the last one's, so only a+ // version the same writer saved can be built over by that writer.+ const dataOver = (base: ProjectRow) => {+ const doc = mergeDoc(docOf(base), body);+ return {+ doc: asJson(doc),+ name: doc.name,+ version: { increment: 1 },+ writer: writer?.id ?? null,+ writerSeq: writer?.seq ?? null,+ };};- doc.updatedAt = Date.now();- const data = { doc: asJson(doc), name: doc.name, version: { increment: 1 } };if (vParam === null) {// First save: apply unconditionally, report the authoritative version.- const updated = await prisma.cutProject.update({ where: { id }, data });+ const updated = await prisma.cutProject.update({ where: { id }, data: dataOver(row) });return Response.json({ ok: true, version: updated.version });}const v = Number(vParam);- if (!Number.isInteger(v)) return err("Bad version.", 400);- const updated = await prisma.cutProject.updateMany({- where: { id, userId, version: v },- data,- });- if (updated.count === 0) {- const current = await getProject(userId, id);- if (!current) return err("Project not found.", 404);- return Response.json(- { error: "conflict", doc: docOf(current), version: current.version },- { status: 409 }- );+ if (!Number.isInteger(v)) {+ return err("Bad version.", HTTP_BAD_REQUEST);+ }++ // The save applies on the version it was made on, or on a newer one its+ // own writer saved earlier. The write is guarded on the row's version,+ // so another save landing in between still conflicts.+ const base = row.version === v || follows(row, writer) ? row : null;+ if (base) {+ const updated = await prisma.cutProject.updateMany({+ where: { id, userId, version: base.version },+ data: dataOver(base),+ });+ if (updated.count > 0) {+ return Response.json({ ok: true, version: base.version + 1 });+ }}- return Response.json({ ok: true, version: v + 1 });++ // The conflict hands back the newest doc: the row already read, unless a+ // write raced the guarded update.+ const current = base ? await getProject(userId, id) : row;+ if (!current) {+ return err("Project not found.", HTTP_NOT_FOUND);+ }+ return Response.json(+ { error: "conflict", doc: docOf(current), version: current.version },+ { status: HTTP_CONFLICT }+ );} catch (e) {return caught(e, "Could not save project.");}