Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
feat: implement download manager and path utility modules
Ambaliya Prashant Vrajlal634ffb3b553b
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 269d67f..dc1214c 100644--- a/src/main/download/downloadManager.ts+++ b/src/main/download/downloadManager.ts@@ -1,6 +1,6 @@import { randomUUID } from 'node:crypto'import { createReadStream, createWriteStream } from 'node:fs'-import { mkdir, readFile, readdir, rename, rm, stat, statfs, writeFile } from 'node:fs/promises'+import { readFile, readdir, rename, rm, stat, statfs, writeFile } from 'node:fs/promises'import { basename, join } from 'node:path'import { Readable } from 'node:stream'import { finished, pipeline } from 'node:stream/promises'@@ -29,7 +29,7 @@ import {reconcilePartFileSize,removeHedgeFiles} from './partFiles'-import { reserveDestinationPath } from './paths'+import { ensureDirectory, reserveDestinationPath } from './paths'import { pickWork, type SchedulerPolicy, type Work } from './scheduler'import {createSimSession,@@ -502,8 +502,8 @@ export class DownloadManager {requestPayload: StartDownloadRequest,interfaces: NetworkInterfaceInfo[]): Promise<string> {- await mkdir(this.downloadsRoot(), { recursive: true })- await mkdir(requestPayload.destinationDir, { recursive: true })+ await ensureDirectory(this.downloadsRoot())+ await ensureDirectory(requestPayload.destinationDir)await ensureDiskSpace(requestPayload.destinationDir,this.downloadsRoot(),@@ -522,7 +522,7 @@ export class DownloadManager {const id = randomUUID()const tempDir = join(this.downloadDir(id), 'parts')try {- await mkdir(tempDir, { recursive: true })+ await ensureDirectory(tempDir)} catch (error) {await rm(destinationPath, { force: true })throw error@@ -705,7 +705,7 @@ export class DownloadManager {// The part files are the real record of what's downloaded, not the manifest. If one went// missing or came up short while paused (userData cleaned out, a crash before a write hit// the disk), fetch that block again instead of failing at assembly.- await mkdir(runtime.tempDir, { recursive: true })+ await ensureDirectory(runtime.tempDir)await removeHedgeFiles(runtime.tempDir)await Promise.all(runtime.blocks.map(async (block) => {@@ -1755,7 +1755,7 @@ export class DownloadManager {requestPayload: runtime.requestPayload,activeInterfaces: runtime.activeInterfaces}- await mkdir(dir, { recursive: true })+ await ensureDirectory(dir)await writeFile(temporaryPath, JSON.stringify(persisted), 'utf-8')await rename(temporaryPath, path)})diff --git a/src/main/download/paths.ts b/src/main/download/paths.tsindex 62889ca..cb275d3 100644--- a/src/main/download/paths.ts+++ b/src/main/download/paths.ts@@ -24,6 +24,21 @@ function sanitizeFileName(fileName: string): string {return !safe || /^\.+$/.test(safe) ? 'download' : safe}+import { stat } from 'node:fs/promises'++export async function ensureDirectory(directory: string): Promise<void> {+ try {+ await mkdir(directory, { recursive: true })+ } catch (error) {+ // Windows throws EPERM when trying to mkdir a drive root+ if ((error as NodeJS.ErrnoException).code === 'EPERM') {+ const stats = await stat(directory).catch(() => null)+ if (stats?.isDirectory()) return+ }+ throw error+ }+}+/*** Claims <directory>/<fileName>, or <directory>/<fileName> (1), (2), ... if it* already exists, by creating an empty file at that path.@@ -39,7 +54,7 @@ function sanitizeFileName(fileName: string): string {* download doesn't end up producing a file.*/export async function reserveDestinationPath(directory: string, fileName: string): Promise<string> {- await mkdir(directory, { recursive: true })+ await ensureDirectory(directory)fileName = sanitizeFileName(fileName)const ext = extname(fileName)const base = basename(fileName, ext)