55// No `electron` import: the IPC surface and the window factory are handed in,
66// so the routing stays unit-testable.
77
8+ import { randomUUID } from "node:crypto" ;
89import { type FileHandle , mkdir , open , rename , rm } from "node:fs/promises" ;
910import path from "node:path" ;
1011import type { IpcMain } from "electron" ;
@@ -69,8 +70,8 @@ interface PendingRun {
6970 request : CliRequest ;
7071 handlers : RunnerHandlers ;
7172 resolve ( result : CliDoneResult ) : void ;
72- /** The export's output while its runner writes it, open on `partialPath` . */
73- partial ?: Promise < FileHandle > ;
73+ /** The export's output while its runner writes it: a file of this run's own beside the output . */
74+ partial ?: { path : string ; handle : Promise < FileHandle > } ;
7475}
7576
7677/** Largest read or write a runner makes in one call. */
@@ -81,9 +82,13 @@ function exportOutPath(request: CliExportRequest): string {
8182 return request . outPath ?? defaultExportPath ( request . projectPath , request . format ?? "mp4" ) ;
8283}
8384
84- /** The output is written beside itself and renamed once whole: a mix reads the file it replaces. */
85- function partialPath ( request : CliExportRequest ) : string {
86- return `${ exportOutPath ( request ) } .part` ;
85+ /**
86+ * The output is written beside itself and renamed once whole: a mix reads the file it replaces.
87+ * Each run has its own, so two exports to one path never write into each other: the last to
88+ * commit wins.
89+ */
90+ function newPartialPath ( request : CliExportRequest ) : string {
91+ return `${ exportOutPath ( request ) } .${ randomUUID ( ) } .part` ;
8792}
8893
8994/** The file of its own export a runner names by role. It never passes a path. */
@@ -97,11 +102,11 @@ function exportFilePath(request: CliExportRequest, file: unknown): string {
97102
98103/** Drops an output the runner did not commit. */
99104async function discardPartial ( run : PendingRun ) : Promise < void > {
100- if ( ! run . partial || run . request . kind !== "export" ) return ;
101- const opening = run . partial ;
105+ const partial = run . partial ;
106+ if ( ! partial ) return ;
102107 run . partial = undefined ;
103- await opening . then ( ( handle ) => handle . close ( ) ) . catch ( ( ) => undefined ) ;
104- await rm ( partialPath ( run . request ) , { force : true } ) . catch ( ( ) => undefined ) ;
108+ await partial . handle . then ( ( handle ) => handle . close ( ) ) . catch ( ( ) => undefined ) ;
109+ await rm ( partial . path , { force : true } ) . catch ( ( ) => undefined ) ;
105110}
106111
107112export const RUNNER_WINDOW_TYPES = {
@@ -186,19 +191,31 @@ export function createRunnerHost<W extends RunnerWindow>(
186191 if ( ! Number . isSafeInteger ( position ) || ( position as number ) < 0 ) {
187192 throw new Error ( "Invalid chunk position" ) ;
188193 }
189- run . partial ??= mkdir ( path . dirname ( partialPath ( request ) ) , { recursive : true } ) . then ( ( ) =>
190- open ( partialPath ( request ) , "w" ) ,
191- ) ;
192- const handle = await run . partial ;
194+ if ( ! run . partial ) {
195+ const partialPath = newPartialPath ( request ) ;
196+ run . partial = {
197+ path : partialPath ,
198+ handle : mkdir ( path . dirname ( partialPath ) , { recursive : true } ) . then ( ( ) =>
199+ open ( partialPath , "wx" ) ,
200+ ) ,
201+ } ;
202+ }
203+ const handle = await run . partial . handle ;
193204 await handle . write ( bytes , 0 , bytes . byteLength , position as number ) ;
194205 } ) ;
195206 ipc . handle ( "cli-export-file-commit" , async ( event ) => {
196207 const { run, request } = exportRunFor ( event ) ;
197- if ( ! run . partial ) throw new Error ( "This export wrote no output" ) ;
198- const opening = run . partial ;
208+ const partial = run . partial ;
209+ if ( ! partial ) throw new Error ( "This export wrote no output" ) ;
199210 run . partial = undefined ;
200- await ( await opening ) . close ( ) ;
201- await rename ( partialPath ( request ) , exportOutPath ( request ) ) ;
211+ try {
212+ await ( await partial . handle ) . close ( ) ;
213+ await rename ( partial . path , exportOutPath ( request ) ) ;
214+ } catch ( error ) {
215+ // A commit that fails leaves no partial file behind: it can be as large as the export.
216+ await rm ( partial . path , { force : true } ) . catch ( ( ) => undefined ) ;
217+ throw error ;
218+ }
202219 } ) ;
203220
204221 return {
0 commit comments