Speed up downloads by combining multiple network connections in parallel
TypeScript bandwidth-aggregationcross-platformdesktop-appdownloaderdownloadmanagerlinuxmacosmulticonnectionparallel-downloadswindows
Verify remote file identity on resume; check disk space before starting
Anmol Kapil029387efebe1
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.tsindex 0384b91..f559bb8 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, rm, stat, truncate } from 'node:fs/promises'+import { mkdir, rm, stat, statfs, truncate } from 'node:fs/promises'import { basename, join } from 'node:path'import type { BrowserWindow } from 'electron'import { app } from 'electron'@@ -13,6 +13,7 @@ import type {} from '../../shared/types'import { downloadChunk } from './chunkDownloader'import { getAvailableDestinationPath } from './paths'+import { isResourceUnchanged } from './probe'interface ChunkRuntime {controller: AbortController@@ -120,6 +121,24 @@ async function reconcilePartFileSize(partPath: string, expectedBytes: number): Preturn safeBytes}+function formatGigabytes(bytes: number): string {+ return `${(bytes / 1024 ** 3).toFixed(2)} GB`+}++/** Throws if the destination volume doesn't have room for the download — a full disk should+ * fail upfront with a clear reason, not partway through as a confusing ENOSPC write error. */+async function ensureDiskSpace(destinationDir: string, requiredBytes: number): Promise<void> {+ if (requiredBytes <= 0) return // unknown size — nothing to check against++ const stats = await statfs(destinationDir)+ const availableBytes = stats.bavail * stats.bsize+ if (availableBytes < requiredBytes) {+ throw new Error(+ `Not enough disk space: this download needs ${formatGigabytes(requiredBytes)} but only ${formatGigabytes(availableBytes)} is free`+ )+ }+}+/** Appends one file's bytes onto an already-open writable, without ending it. */function appendFileToStream(sourcePath: string, output: NodeJS.WritableStream): Promise<void> {return new Promise((resolve, reject) => {@@ -153,6 +172,8 @@ export class DownloadManager {throw new Error('Select at least one network interface')}+ await ensureDiskSpace(requestPayload.destinationDir, requestPayload.totalBytes)+const id = randomUUID()const tempDir = join(app.getPath('temp'), 'plexo', id)await mkdir(tempDir, { recursive: true })@@ -226,6 +247,28 @@ export class DownloadManager {const runtime = this.runtimes.get(id)if (!runtime || runtime.state.status !== 'paused') return+ void this.resumeAfterVerifying(runtime)+ }++ // Appending onto part files assumes the remote file hasn't changed since+ // it was probed — if the server's ETag/Last-Modified moved on while this+ // download sat paused, resuming would silently stitch old and new bytes+ // together. Check first, and refuse to resume rather than corrupt the+ // output (the user can always start the download over from scratch).+ private async resumeAfterVerifying(runtime: DownloadRuntime): Promise<void> {+ const { url, etag, lastModified } = runtime.requestPayload+ const unchanged = await isResourceUnchanged(url, etag, lastModified)++ if (runtime.state.status !== 'paused') return // cancelled while we were checking++ if (!unchanged) {+ runtime.state.status = 'error'+ 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)+ return+ }+runtime.state.status = 'downloading'const pending = runtime.state.chunks.filter((chunk) => chunk.status !== 'completed')for (const chunk of pending) {diff --git a/src/main/download/probe.ts b/src/main/download/probe.tsindex a6217ad..b8bf1f1 100644--- a/src/main/download/probe.ts+++ b/src/main/download/probe.ts@@ -58,7 +58,9 @@ function fileNameFromHeaders(headers: Headers, url: URL): string {return base && base.length > 0 ? base : 'download'}-export async function probeUrl(rawUrl: string): Promise<ProbeResult> {+async function requestFollowingRedirects(+ rawUrl: string+): Promise<{ current: URL; response: ProbeResponse | null }> {let current = new URL(rawUrl)let response: ProbeResponse | null = null@@ -73,6 +75,42 @@ export async function probeUrl(rawUrl: string): Promise<ProbeResult> {break}+ return { current, response }+}++/**+ * Re-checks a previously probed URL's strong validators before a paused+ * download resumes. Appending onto part files assumes the remote content+ * hasn't changed since it was probed — if it has (a different ETag or+ * Last-Modified), stitching old and new bytes together would silently+ * produce a corrupt file. Returns true when unchanged *or* when we can't+ * tell (no validators, or the check itself failed) — a probe failure isn't+ * proof the file changed, so it shouldn't block a resume on its own.+ */+export async function isResourceUnchanged(+ rawUrl: string,+ etag: string | null,+ lastModified: string | null+): Promise<boolean> {+ if (!etag && !lastModified) return true++ try {+ const { response } = await requestFollowingRedirects(rawUrl)+ if (!response || response.statusCode >= 400) return true++ const currentEtag = headerValue(response.headers, 'etag')+ const currentLastModified = headerValue(response.headers, 'last-modified')+ if (etag && currentEtag) return currentEtag === etag+ if (lastModified && currentLastModified) return currentLastModified === lastModified+ return true+ } catch {+ return true+ }+}++export async function probeUrl(rawUrl: string): Promise<ProbeResult> {+ const { current, response } = await requestFollowingRedirects(rawUrl)+if (!response || response.statusCode === 0 || response.statusCode >= 400) {throw new Error(`Server responded with status ${response?.statusCode || 'unknown'}`)}@@ -100,6 +138,8 @@ export async function probeUrl(rawUrl: string): Promise<ProbeResult> {supportsRanges,totalBytes,suggestedFileName: fileNameFromHeaders(response.headers, current),- contentType: headerValue(response.headers, 'content-type') ?? null+ contentType: headerValue(response.headers, 'content-type') ?? null,+ etag: headerValue(response.headers, 'etag') ?? null,+ lastModified: headerValue(response.headers, 'last-modified') ?? null}}diff --git a/src/renderer/src/screens/IdleScreen.tsx b/src/renderer/src/screens/IdleScreen.tsxindex 1db97e1..d21c083 100644--- a/src/renderer/src/screens/IdleScreen.tsx+++ b/src/renderer/src/screens/IdleScreen.tsx@@ -115,7 +115,9 @@ export function IdleScreen(): React.JSX.Element {suggestedFileName: probe.result.suggestedFileName,totalBytes: probe.result.totalBytes ?? 0,supportsRanges: probe.multiConnectionAllowed,- interfaceIds: selectedInterfaceIds+ interfaceIds: selectedInterfaceIds,+ etag: probe.result.etag,+ lastModified: probe.result.lastModified})} catch (error) {setStartError(error instanceof Error ? error.message : String(error))diff --git a/src/shared/types.ts b/src/shared/types.tsindex ffdde16..72ab304 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -19,6 +19,9 @@ export interface ProbeResult {totalBytes: number | nullsuggestedFileName: stringcontentType: string | null+ /** Strong validators, used to detect if the remote content changes between pause and resume. */+ etag: string | null+ lastModified: string | null}export type DownloadStatus = 'downloading' | 'paused' | 'completed' | 'error' | 'cancelled'@@ -70,4 +73,6 @@ export interface StartDownloadRequest {totalBytes: numbersupportsRanges: booleaninterfaceIds: string[]+ etag: string | null+ lastModified: string | null}