Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
Claim a download's destination file name when it starts
Anmol Kapil515202b68452
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 4a3bf32..86889db 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -14,7 +14,7 @@ import type {StartDownloadRequest} from '../../shared/types'import { downloadChunk } from './chunkDownloader'-import { getAvailableDestinationPath } from './paths'+import { reserveDestinationPath } from './paths'import { isResourceUnchanged } from './probe'interface ChunkRuntime {@@ -179,7 +179,9 @@ export class DownloadManager {const tempDir = join(app.getPath('temp'), 'plexo', id)await mkdir(tempDir, { recursive: true })- const destinationPath = getAvailableDestinationPath(+ // Claimed on disk, not just picked, so a second download of the same file+ // name can't pick it too and overwrite this one at reassembly time.+ const destinationPath = await reserveDestinationPath(requestPayload.destinationDir,requestPayload.suggestedFileName)@@ -357,6 +359,7 @@ export class DownloadManager {runtime.state.error ='The remote file changed while this download was paused, so resuming would corrupt it. Start the download over instead.'this.pushUpdate(runtime)+ await this.discardUnfinishedDestination(runtime)return}@@ -400,6 +403,7 @@ export class DownloadManager {}this.pushUpdate(runtime)void this.cleanupTempDir(runtime)+ void this.discardUnfinishedDestination(runtime)}remove(id: string): void {@@ -439,6 +443,7 @@ export class DownloadManager {if (runtime.state.status === 'error' || runtime.state.status === 'cancelled') {this.pushUpdate(runtime)await this.cleanupTempDir(runtime)+ await this.discardUnfinishedDestination(runtime)}return}@@ -455,6 +460,7 @@ export class DownloadManager {this.pushUpdate(runtime)await this.cleanupTempDir(runtime)+ await this.discardUnfinishedDestination(runtime)}private async runWorker(runtime: DownloadRuntime, chunk: ChunkState): Promise<void> {@@ -720,6 +726,21 @@ export class DownloadManager {}}+ /**+ * Releases the placeholder file reserved at start when the download won't be+ * filling it in, so its name is free for the next attempt. Only ever removes+ * a path this download created and never finished writing — a completed+ * download keeps its file.+ */+ private async discardUnfinishedDestination(runtime: DownloadRuntime): Promise<void> {+ if (runtime.state.status === 'completed') return+ try {+ await rm(runtime.state.destinationPath, { force: true })+ } catch {+ // Best-effort — a stray empty file isn't worth failing the download over.+ }+ }+private async cleanupTempDir(runtime: DownloadRuntime): Promise<void> {try {await rm(runtime.tempDir, { recursive: true, force: true })diff --git a/src/main/download/paths.ts b/src/main/download/paths.tsindex dd60e4f..01c7a96 100644--- a/src/main/download/paths.ts+++ b/src/main/download/paths.ts@@ -1,5 +1,5 @@import { app } from 'electron'-import { existsSync } from 'node:fs'+import { open } from 'node:fs/promises'import { basename, extname, join } from 'node:path'export function getDefaultDownloadsDir(): string {@@ -10,16 +10,39 @@ export function getHomeDir(): string {return app.getPath('home')}-/** <directory>/<fileName>, or <directory>/<fileName> (1), (2), ... if it already exists. */-export function getAvailableDestinationPath(directory: string, fileName: string): string {+// Somewhere to stop rather than spin forever if every candidate is taken+// (or if something outside the app is creating them as fast as we try).+const MAX_NAME_ATTEMPTS = 10_000++/**+ * Claims <directory>/<fileName>, or <directory>/<fileName> (1), (2), ... if it+ * already exists, by creating an empty file at that path.+ *+ * Creating it is the point. A download's bytes go to part files and only reach+ * the destination at reassembly, so merely *checking* that a name is free+ * leaves it free: two downloads of the same file name started minutes apart+ * would both pick it, and the one that finished second would overwrite the+ * first — or, if their assemblies overlapped, both would write into one file+ * and neither would survive. An exclusive create is what makes the name ours.+ *+ * The caller owns the placeholder from here: it must be removed if the+ * download doesn't end up producing a file.+ */+export async function reserveDestinationPath(directory: string, fileName: string): Promise<string> {const ext = extname(fileName)const base = basename(fileName, ext)- let candidate = join(directory, fileName)- let counter = 1- while (existsSync(candidate)) {- candidate = join(directory, `${base} (${counter})${ext}`)- counter += 1+ for (let counter = 0; counter < MAX_NAME_ATTEMPTS; counter += 1) {+ const candidate =+ counter === 0 ? join(directory, fileName) : join(directory, `${base} (${counter})${ext}`)+ try {+ const handle = await open(candidate, 'wx')+ await handle.close()+ return candidate+ } catch (error) {+ if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error+ }}- return candidate++ throw new Error(`Could not find an unused file name for "${fileName}" in ${directory}`)}