Open source CapCut alternative.
TypeScript
fix(site): store local imports as they start
David Le8b5393deaa87
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex 64d2a631..d0f2712e 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -558,10 +558,10 @@ export type PendingImport = {/** The URL the asset plays from until the stored file takes over — a local* object URL for a dropped file, the source URL for remote media. */localUrl: string;- /** Put the bytes in the browser store under a claimed name, where a reload- * finds them, as soon as a cloud import starts. The queue runs it ahead of- * its transfer window, so a long drop is held whole while its uploads wait- * their turn. Safe to call again. */+ /** Put the bytes where a reload finds them — the browser store under a+ * claimed name, or the project's own storage — as soon as the import starts.+ * The queue runs it ahead of its transfer window, so a long drop is held+ * whole while its uploads wait their turn. Safe to call again. */hold?: ImportStep;/** Send the bytes and mark the object complete; resolves to the stored* (deduped) file name the asset swaps to. */@@ -628,24 +628,36 @@ export async function prepareImport(}/** A browser or engine project stores a dropped file by copying it in, on- * this machine, and the send hands back its name. The copy is the whole- * transfer, so it waits for the queue's window like any upload: a long drop- * copied at once would swamp the engine socket or the browser store. A failed- * or cancelled copy is forgotten, so a retry copies again. */+ * this machine: the copy is the hold, and the send hands back its name. The+ * copy is the whole transfer, so the copies of a drop take turns: each starts+ * as soon as the one before it lands, ahead of the queue's window, so a+ * reload finds the drop stored in order and a long drop never swamps the+ * engine socket or the browser store. A failed or cancelled copy is+ * forgotten, so a retry copies again. */function sendDirectImport(backend: CutBackend,projectId: string,file: File-): Pick<PendingImport, "send"> {+): Pick<PendingImport, "hold" | "send"> {let copy: Promise<string> | null = null;const send: PendingImport["send"] = (opts) => {- copy ??= uploadProjectMediaTo(backend, projectId, file, file.name, opts).catch((err: unknown) => {- copy = null;- throw err;- });+ copy ??= copyInTurn(() => uploadProjectMediaTo(backend, projectId, file, file.name, opts)).catch(+ (err: unknown) => {+ copy = null;+ throw err;+ }+ );return copy;};- return { send };+ return { hold: async (opts) => void (await send(opts)), send };+}++/** Copies onto this machine run one at a time, in the order they start. */+let copyChain: Promise<unknown> = Promise.resolve();+function copyInTurn<T>(work: () => Promise<T>): Promise<T> {+ const next = copyChain.then(work, work);+ copyChain = next.catch(() => {});+ return next;}/** Claims run one at a time, each against the names already taken, so twodiff --git a/site/src/cut/lib/mediaImport.test.ts b/site/src/cut/lib/mediaImport.test.tsindex 43bfb346..c63a8e43 100644--- a/site/src/cut/lib/mediaImport.test.ts+++ b/site/src/cut/lib/mediaImport.test.ts@@ -7,12 +7,27 @@ await stubModule<typeof import("./mediaRead")>("./mediaRead", import.meta.url, {});const { prepareImport } = await import("./media");-// A copy onto this machine is the whole transfer, so it waits for the import-// queue's window; holding it up front would start every file of a drop at once.-test("an import onto this machine has no hold outside the queue's window", async () => {- const backend = { kind: "engine" } as unknown as CutBackend;- const file = new File([new Uint8Array(3)], "song.mp3", { type: "audio/mpeg" });- const pending = await prepareImport("p", file, backend);- expect(pending).not.toBeNull();- expect(pending?.hold).toBeUndefined();+// A copy onto this machine is the whole transfer. Each one starts as the+// import does, so a reload finds every file of a drop stored, and they run+// one at a time, so a long drop never swamps the engine socket or the store.+test("imports onto this machine hold their bytes up front, one copy at a time", async () => {+ let running = 0;+ let most = 0;+ const backend = {+ kind: "engine",+ fetch: async () => {+ running++;+ most = Math.max(most, running);+ await new Promise((r) => setTimeout(r, 5));+ running--;+ return new Response(JSON.stringify({ fileName: "song.mp3" }), { status: 200 });+ },+ } as unknown as CutBackend;+ const file = () => new File([new Uint8Array(3)], "song.mp3", { type: "audio/mpeg" });+ const drop = await Promise.all([prepareImport("p", file(), backend), prepareImport("p", file(), backend)]);+ expect(drop.every((pending) => pending?.hold)).toBe(true);++ await Promise.all(drop.map((pending) => pending!.hold!()));+ expect(most).toBe(1);+ expect(await drop[0]!.send()).toBe("song.mp3");});