Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Write downloads directly in destination staging file

Anmol Kapil0774aa60a806
diff --git a/README.md b/README.md
index 7c226aa..e209529 100644
--- a/README.md
+++ b/README.md
@@ -70,13 +70,13 @@ File ──→ Split ─────┤ ├─
- 🚀 **Multi-interface, multi-connection downloads** — splits files into chunks of up to 8 MB and fans them out across worker connections bound to specific network interfaces (up to 8 parallel connections per interface, 32 total).
- 🔌 **Hardware interface detection** — queries Windows adapters via PowerShell `Get-NetAdapter` and macOS hardware ports via `networksetup` so Wi-Fi, Ethernet, tethered iPhones, and Thunderbolt bridges are labeled by real device names instead of bare BSD names (`en0`, `en6`).
- ⚖️ **Dynamic work-stealing queue** — chunks are leased from a shared pending queue; faster networks pull more chunks instead of waiting for slower connections to finish.
-- ⏸️ **Resumable downloads** — cleanly pause or retry failed downloads without losing progress, preserving completed `part-N` chunk files on disk.
-- 💾 **Relaunch recovery** — interrupted downloads are restored as paused after Plexo restarts, with progress and part files preserved in application data.
+- ⏸️ **Resumable downloads** — cleanly pause and resume downloads with progress saved in a destination-side staging file.
+- 💾 **Relaunch recovery** — interrupted downloads are restored as paused after Plexo restarts, with a small manifest in application data.
- 🛡️ **Safe, integrity-checked resume** — re-verifies remote `ETag` and `Last-Modified` validators before resuming, refusing to resume (rather than corrupting the file) if the server-side file has changed.
- 🔁 **Automatic retry with backoff** — failed chunks are automatically returned to the queue and retried with exponential backoff (up to 5 retries, 1s–15s backoff).
- 💤 **Stall detection & watchdog** — automatically drops and re-queues connections that remain open but silent (>20s without incoming data).
- 🔔 **Desktop notifications** — native desktop alerts when downloads complete or encounter errors.
-- 💾 **Upfront disk-space verification** — verifies free disk space before writing any temporary part files.
+- 💾 **Upfront disk-space verification** — checks the destination volume before writing the staging file.
- 🔀 **Mid-download redirect handling** — transparently follows 3xx HTTP redirects (up to 5 hops) during probing and individual chunk downloads.
- 📊 **Real-time telemetry** — live throughput graphs, rolling-window ETA calculation, and per-connection transfer stats.
- 🗺️ **Interactive progress grid** — 1:1 visual map of individual chunks, color-coded by the network interface that fetched each chunk with accurate per-network byte attribution.
@@ -153,15 +153,13 @@ Shared Pending Queue: [Chunk #4] [Chunk #5] [Chunk #6] [Chunk #7] [Chunk #8]
Work distribution is dynamically proportional to each interface's real-time throughput. If one network slows down or disconnects, remaining workers continue draining the queue without stalled shares.
-### 4. File reassembly & stream pipeline
+### 4. Direct-to-destination storage
-Each worker writes its assigned byte range directly to an isolated temporary file on disk (`part-0`, `part-1`, ... `part-N`).
+Each worker writes its assigned byte range at its final offset in one staging file beside the chosen destination. Workers use separate file handles and explicit byte positions, so non-overlapping ranges can be written in parallel.
-Once the queue is drained and all chunk promises resolve:
+Once every range is complete, Plexo flushes the staging file and renames it to the final filename in the same directory. There is no full-file assembly copy, so the destination needs approximately one file's worth of space.
-- Plexo streams each `part-N` file sequentially into the final destination file using Node.js streams (`createReadStream` piped into `createWriteStream` with `{ flags: 'a' }`).
-- The temporary chunk directory is cleaned up.
-- The assembled file is verified against the expected byte length.
+The visible final filename is reserved as an empty placeholder while the download runs. It is only replaced after the staged file is complete.
---
@@ -170,17 +168,15 @@ Once the queue is drained and all chunk promises resolve:
When you pause a download:
- Plexo aborts all active HTTP socket connections via `AbortController`.
-- All completed `part-N` files remain cached on disk in a temporary directory.
+- The staging file remains beside the chosen destination, and progress is saved in a small manifest.
When you resume:
1. **Validator check**: Plexo sends a probe request to compare the server's current `ETag` and `Last-Modified` headers against the values recorded when the download started.
-2. **Safe resume**: If the validators match, Plexo checks which `part-N` files are already complete on disk, skips them, and queues only the remaining chunks.
+2. **Safe resume**: If the validators match, Plexo resumes each range from the last saved byte offset in the staging file.
3. **Guard against corruption**: If the file on the server has changed, Plexo refuses to resume to prevent combining incompatible slices into a corrupt file.
-Download manifests and partial data are stored under Plexo's application-data directory. If Plexo
-quits or crashes during a transfer, it restores that transfer as paused on the next launch. Explicitly
-cancelling or removing a download still deletes its partial data.
+Download manifests are stored under Plexo's application-data directory; large partial data stays beside the destination. If Plexo quits or crashes during a transfer, it restores that transfer as paused on the next launch. Cancelling or removing a download deletes its staging file.
---
@@ -190,14 +186,14 @@ A **chunk** is the atomic unit of work in Plexo:
- **Size**: Up to 8 MB, with the final chunk sized to the remaining bytes. A file that is small next to its connection count gets smaller chunks (never under 1 MB) — at least two per connection — so a fast network can out-pull a slow one instead of being stuck behind it.
- **Transport**: One independent HTTP range request (`Range: bytes=START-END`).
-- **Storage**: Written directly to an isolated `part-N` file in the download's temp directory.
+- **Storage**: Written at its final byte offset in the destination-side staging file.
- **Assignment**: Leased to an individual worker socket bound to a specific network interface.
```text
-Chunk #0 → Range: bytes=0 - 6,291,455 → part-0 (Wi-Fi)
-Chunk #1 → Range: bytes=6,291,455 - 12,582,911 → part-1 (Ethernet)
-Chunk #2 → Range: bytes=12,582,911 - 18,874,365 → part-2 (USB Tether)
-Chunk #3 → Range: bytes=12,582,911 - 25,165,820 → part-3 (Cellular)
+Chunk #0 → Range: bytes=0-8388607 → staging offset 0 (Wi-Fi)
+Chunk #1 → Range: bytes=8388608-16777215 → staging offset 8388608 (Ethernet)
+Chunk #2 → Range: bytes=16777216-25165823 → staging offset 16777216 (USB Tether)
+Chunk #3 → Range: bytes=25165824-33554431 → staging offset 25165824 (Cellular)
```
### Why up to 8 MB?
diff --git a/e2e/blockProgress.spec.ts b/e2e/blockProgress.spec.ts
index 4b1d61b..e21ad38 100644
--- a/e2e/blockProgress.spec.ts
+++ b/e2e/blockProgress.spec.ts
@@ -1,14 +1,14 @@
-import { mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises'
+import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { expect, test } from '@playwright/test'
import fc from 'fast-check'
import { advanceBlock, retractBlock } from '../src/main/download/blockProgress'
-import { hedgeFile, mergeHedge, partFile, removeHedgeFiles } from '../src/main/download/partFiles'
+import { DownloadFile } from '../src/main/download/downloadFile'
import type { BlockState } from '../src/shared/types'
-// K. What a block's byte counts mean when two attempts race for it, and how a racing attempt's
-// file joins the block's own.
+// K. What a block's byte counts mean when two attempts race for it, and how range writes land
+// in the one destination-side staging file.
const LENGTH = 1000
const fresh = (): BlockState => ({
@@ -74,10 +74,10 @@ test.describe('block progress', () => {
})
})
-test.describe('joining a racing attempt onto its block', () => {
+test.describe('destination-side staging file', () => {
let dir: string
test.beforeEach(async () => {
- dir = await mkdtemp(join(tmpdir(), 'plexo-merge-'))
+ dir = await mkdtemp(join(tmpdir(), 'plexo-staging-'))
})
test.afterEach(async () => {
await rm(dir, { recursive: true, force: true })
@@ -86,49 +86,16 @@ test.describe('joining a racing attempt onto its block', () => {
const bytes = (from: number, to: number): Buffer =>
Buffer.from(Array.from({ length: to - from }, (_, i) => (from + i) % 251))
- test('keeps the block’s first bytes and appends the hedge’s, dropping what the block’s writer got past the join', async () => {
- const part = partFile(dir, 3)
- const hedge = hedgeFile(dir, 3, 7)
- // The block's writer got to 600 before it was stopped; the hedge began at 400.
- await writeFile(part, bytes(0, 600))
- await writeFile(hedge, bytes(400, LENGTH))
- expect(await mergeHedge(part, hedge, 400, LENGTH)).toBe(true)
- expect(await readFile(part)).toEqual(bytes(0, LENGTH))
- })
-
- test('a hedge that began at the very start becomes the block', async () => {
- const part = partFile(dir, 0)
- const hedge = hedgeFile(dir, 0, 1)
- await writeFile(hedge, bytes(0, LENGTH)) // no part file at all
- expect(await mergeHedge(part, hedge, 0, LENGTH)).toBe(true)
- expect(await readFile(part)).toEqual(bytes(0, LENGTH))
- })
-
- test('refuses, changing nothing, when the block’s file is shorter than the join', async () => {
- const part = partFile(dir, 0)
- const hedge = hedgeFile(dir, 0, 1)
- await writeFile(part, bytes(0, 300)) // its writer never flushed the last 100
- await writeFile(hedge, bytes(400, LENGTH))
- expect(await mergeHedge(part, hedge, 400, LENGTH)).toBe(false)
- expect((await stat(part)).size).toBe(300)
- })
-
- test('refuses, changing nothing, when the hedge’s file is not the size its range says', async () => {
- const part = partFile(dir, 0)
- const hedge = hedgeFile(dir, 0, 1)
- await writeFile(part, bytes(0, 500))
- await writeFile(hedge, bytes(400, 900))
- expect(await mergeHedge(part, hedge, 400, LENGTH)).toBe(false)
- expect(await readFile(part)).toEqual(bytes(0, 500))
- })
-
- test('leftover hedge files are cleared without touching the blocks’ own', async () => {
- await writeFile(partFile(dir, 1), 'keep')
- await writeFile(hedgeFile(dir, 1, 4), 'stale')
- await writeFile(hedgeFile(dir, 2, 0), 'stale')
- await removeHedgeFiles(dir)
- expect((await stat(partFile(dir, 1))).size).toBe(4)
- await expect(stat(hedgeFile(dir, 1, 4))).rejects.toThrow()
- await expect(stat(hedgeFile(dir, 2, 0))).rejects.toThrow()
+ test('out-of-order ranges and a hedge overwrite produce one exact file', async () => {
+ const destination = join(dir, 'result.bin')
+ await writeFile(destination, '')
+ const file = new DownloadFile(destination, 'test-id')
+ await file.create()
+ await file.writeBuffers(500, [bytes(500, LENGTH)])
+ await file.writeBuffers(0, [bytes(0, 600)])
+ await file.writeBuffers(400, [bytes(400, LENGTH)])
+ expect(await file.read(390, 20)).toEqual(bytes(390, 410))
+ await file.publish(LENGTH)
+ expect(await readFile(destination)).toEqual(bytes(0, LENGTH))
})
})
diff --git a/e2e/chaos.spec.ts b/e2e/chaos.spec.ts
index 97f8667..8db3474 100644
--- a/e2e/chaos.spec.ts
+++ b/e2e/chaos.spec.ts
@@ -45,7 +45,7 @@ async function status(real: Real): Promise<DownloadStatus | undefined> {
/** Re-reads the app, since a running download may have finished on its own since last looked. */
async function sync(model: Model, real: Real): Promise<void> {
const current = await status(real)
- if (current === 'completed' || current === 'assembling') model.phase = 'done'
+ if (current === 'completed') model.phase = 'done'
if (current === 'error' || current === 'cancelled') {
throw new Error(`download ended ${current}: ${(await real.app.current())?.error}`)
}
@@ -66,7 +66,7 @@ class Pause implements fc.AsyncCommand<Model, Real> {
async run(model: Model, real: Real): Promise<void> {
await real.app.api.pauseDownload(real.id)
// Either it paused, or it had already finished downloading before the pause arrived.
- const state = await real.app.waitForStatus(['paused', 'assembling', 'completed'], 15_000)
+ const state = await real.app.waitForStatus(['paused', 'completed'], 15_000)
model.phase = state.status === 'paused' ? 'paused' : 'done'
}
toString = (): string => 'Pause'
@@ -76,7 +76,7 @@ class Resume implements fc.AsyncCommand<Model, Real> {
check = (model: Readonly<Model>): boolean => model.phase === 'paused'
async run(model: Model, real: Real): Promise<void> {
await real.app.api.resumeDownload(real.id)
- await real.app.waitForStatus(['downloading', 'assembling', 'completed'], 15_000)
+ await real.app.waitForStatus(['downloading', 'completed'], 15_000)
model.phase = 'running'
await sync(model, real)
}
diff --git a/e2e/disk.spec.ts b/e2e/disk.spec.ts
index f0a762e..a40934c 100644
--- a/e2e/disk.spec.ts
+++ b/e2e/disk.spec.ts
@@ -80,12 +80,12 @@ test.describe('disk space @disk', () => {
}
})
- test('a file that fits once but not twice (file + its parts) is refused upfront', async ({
+ test('a file that fits once but not twice completes on the destination volume', async ({
serve
}) => {
const volume = await smallVolume(12)
test.skip(!volume, 'cannot create a small volume on this machine')
- // userData on the same small volume, so the part files and the final file compete for it.
+ // Both metadata and destination are on the small volume; the payload must occupy only one copy.
const app = new PlexoApp({
userData: join(volume!.path, 'userData'),
dest: join(volume!.path, 'dest')
@@ -95,10 +95,9 @@ test.describe('disk space @disk', () => {
await mkdir(app.dirs.dest)
await app.launch()
const origin = await serve({ size: 112 * BLOCK }) // 7 MB: fits once, not twice
- // Without counting the parts, this would start, fill the disk while assembling, and
- // fail there — after downloading the whole file.
- await expect(app.start(origin.url(), origin.sha256)).rejects.toThrow(/Not enough disk space/)
- expect(await readdir(app.dirs.dest)).toEqual([])
+ await app.start(origin.url(), origin.sha256)
+ const done = await app.waitForStatus('completed')
+ expect(await readdir(app.dirs.dest)).toEqual([done.fileName])
} finally {
if (app.alive) await app.kill()
await volume!.dispose()
@@ -107,7 +106,7 @@ test.describe('disk space @disk', () => {
})
test.describe('destination folder problems @smoke', () => {
- // Also the test for assembly write errors: the output stream fails to open, and assembly must
+ // Also tests a destination write error: the staging file cannot be opened, and cleanup must
// surface that instead of waiting forever for a 'drain' that never comes.
test('destination folder deleted mid-download → error, nothing left', async ({
plexo,
diff --git a/e2e/fixtures.ts b/e2e/fixtures.ts
index 85dfa4f..fa050c4 100644
--- a/e2e/fixtures.ts
+++ b/e2e/fixtures.ts
@@ -223,7 +223,7 @@ export class PlexoApp {
}
/** Waits until the download state matches `predicate`. A timeout says what the state was
- * instead, so a hang reads as "stuck in assembling", not as a bare assertion mismatch. */
+ * instead, so a hang shows the last progress and state. */
async waitUntil(
predicate: (state: DownloadState) => boolean,
timeout = 20_000
@@ -251,9 +251,8 @@ export class PlexoApp {
// --- invariants --------------------------------------------------------------------------------
const ALLOWED_NEXT: Record<DownloadStatus, DownloadStatus[]> = {
- downloading: ['downloading', 'paused', 'assembling', 'error', 'cancelled'],
+ downloading: ['downloading', 'paused', 'completed', 'error', 'cancelled'],
paused: ['paused', 'downloading', 'error', 'cancelled'],
- assembling: ['assembling', 'completed', 'error'],
completed: ['completed'],
error: ['error'],
cancelled: ['cancelled']
@@ -367,9 +366,9 @@ export async function checkFinalState(app: PlexoApp): Promise<void> {
expectedAdded
)
- const partsDir = join(app.dirs.userData, 'downloads', state.id, 'parts')
+ const stagingPath = join(tracked.destinationDir, `.plexo-${state.id}.part`)
await expect
- .poll(() => existsSync(partsDir), { message: 'part files cleaned up', timeout: 5000 })
+ .poll(() => existsSync(stagingPath), { message: 'staging file cleaned up', timeout: 5000 })
.toBe(false)
const pid = app.electronApp.process().pid
diff --git a/e2e/recovery.spec.ts b/e2e/recovery.spec.ts
index c5ff8ae..a570a53 100644
--- a/e2e/recovery.spec.ts
+++ b/e2e/recovery.spec.ts
@@ -2,7 +2,7 @@ import { cp, readFile, rm, truncate, writeFile } from 'node:fs/promises'
import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { BLOCK, expect, test } from './fixtures'
-import { seededBytes, sha256 } from './origin'
+import { sha256 } from './origin'
// D. Quitting, crashing and restarting. kill() is SIGKILL: no before-quit, no final save —
// what's on disk is whatever the app had managed to persist, exactly as after a crash or a
@@ -67,37 +67,6 @@ test.describe('crash (SIGKILL) and recover @smoke', () => {
await plexo.waitForStatus('completed')
})
}
-
- test('killed while assembling → relaunch paused → resume re-assembles', async ({
- plexo,
- dirs
- }) => {
- // A simulated download can throttle the assemble step, which keeps it in 'assembling'
- // long enough to kill it there.
- const source = join(dirs.userData, '..', 'source.bin')
- const bytes = seededBytes(SIZE, 21)
- await writeFile(source, bytes)
- const id = await plexo.startSimulated(
- {
- sourceFilePath: source,
- networks: [{ kind: 'ethernet', label: 'sim', speedBytesPerSec: 50e6, faultRatePercent: 0 }],
- chunkCount: 2,
- connectionsPerNetwork: 2,
- assembleSpeedBytesPerSec: SIZE / 4
- },
- sha256(bytes)
- )
- await plexo.waitUntil(
- (state) => state.status === 'assembling' && (state.assembledBytes ?? 0) > 0
- )
-
- await plexo.kill()
- await plexo.launch()
- expect((await plexo.current())?.status).toBe('paused')
-
- await plexo.api.resumeDownload(id)
- await plexo.waitForStatus('completed')
- })
})
test.describe('persisted state on disk @smoke', () => {
@@ -165,21 +134,20 @@ test.describe('persisted state on disk @smoke', () => {
await plexo.waitForStatus('completed')
})
- test('part files deleted while the app was closed → re-downloads them', async ({
+ test('staging file deleted while the app was closed → reports lost progress', async ({
plexo,
serve,
dirs
}) => {
const { id } = await pausedDownload(plexo, serve)
await plexo.quit()
- await rm(join(dirs.userData, 'downloads', id, 'parts'), { recursive: true, force: true })
+ await rm(join(dirs.dest, `.plexo-${id}.part`))
await plexo.launch()
- await plexo.api.resumeDownload(id)
- const state = await plexo.waitForStatus(['completed', 'error'])
- expect(state.error).toBeUndefined()
+ expect((await plexo.current())?.status).toBe('error')
+ expect((await plexo.current())?.error).toMatch(/partial download file is missing/)
})
- test('a finished part file cut short while the app was closed is fetched again', async ({
+ test('a staging file cut short while the app was closed is fetched again', async ({
plexo,
serve,
dirs
@@ -187,10 +155,11 @@ test.describe('persisted state on disk @smoke', () => {
// What a power cut can do: the manifest says a block is done, but its data never all
// reached the disk.
const { id } = await pausedDownload(plexo, serve)
- const done = (await plexo.current())!.blocks!.find((block) => block.status === 'completed')!
+ const state = (await plexo.current())!
+ const done = state.blocks!.find((block) => block.status === 'completed')!
await plexo.quit()
- const part = join(dirs.userData, 'downloads', id, 'parts', `part-${done.index}`)
- await truncate(part, 1000)
+ const staging = join(dirs.dest, `.plexo-${id}.part`)
+ await truncate(staging, done.rangeStart + 1000)
await plexo.launch()
await plexo.api.resumeDownload(id)
diff --git a/e2e/sim.spec.ts b/e2e/sim.spec.ts
index 1eb2d8e..9305fa5 100644
--- a/e2e/sim.spec.ts
+++ b/e2e/sim.spec.ts
@@ -1,11 +1,12 @@
-import { truncate, writeFile } from 'node:fs/promises'
+import { existsSync } from 'node:fs'
+import { writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import type { SimulatedNetworkConfig } from '../src/shared/types'
import { BLOCK, expect, test } from './fixtures'
import { seededBytes, sha256 } from './origin'
-// F. The dev-tool simulated download: a local file pushed through the real chunking, retry and
-// assembly pipeline, with only the HTTP transfer swapped out (see simDownload.ts).
+// F. The dev-tool simulated download: a local file pushed through the real chunking and retry
+// pipeline, with only the HTTP transfer swapped out (see simDownload.ts).
const SIZE = 40 * BLOCK
@@ -78,7 +79,7 @@ test.describe('simulated downloads @smoke', () => {
}
})
- test('a part that goes wrong during assembly fails the download and leaves nothing behind', async ({
+ test('writes one staging file beside the final destination and publishes it', async ({
plexo,
dirs
}) => {
@@ -86,40 +87,47 @@ test.describe('simulated downloads @smoke', () => {
const id = await plexo.startSimulated(
{
sourceFilePath: source.path,
- networks: [network('one')],
+ networks: [{ ...network('one'), speedBytesPerSec: 200_000 }],
chunkCount: 2,
- connectionsPerNetwork: 2,
- assembleSpeedBytesPerSec: SIZE / 2
+ connectionsPerNetwork: 2
},
source.sha
)
- await plexo.waitForStatus('assembling')
- // The last part is still waiting its turn; cut a byte off it.
- const last = join(dirs.userData, 'downloads', id, 'parts', `part-${SIZE / BLOCK - 1}`)
- await truncate(last, BLOCK - 1)
-
- const state = await plexo.waitForStatus('error')
- expect(state.error).toMatch(/refusing to write a corrupt file/)
- // (the automatic checks then confirm there is no file at the destination and no parts left)
+ await plexo.waitUntil((state) => state.bytesDownloaded > 0)
+ const staging = join(dirs.dest, `.plexo-${id}.part`)
+ expect(existsSync(staging)).toBe(true)
+ expect(existsSync(join(dirs.userData, 'downloads', id, 'parts'))).toBe(false)
+ await plexo.waitForStatus('completed', 30_000)
+ expect(existsSync(staging)).toBe(false)
+ const progressUpdates = plexo.sessions
+ .at(-1)!
+ .filter(
+ (state) => state.id === id && state.status === 'downloading' && state.bytesDownloaded > 0
+ )
+ expect(
+ progressUpdates.length,
+ 'progress remains responsive during a slow download'
+ ).toBeGreaterThan(5)
})
- test('pause and cancel are ignored while assembling', async ({ plexo, dirs }) => {
+ test('pausing keeps the staging file; cancellation removes it', async ({ plexo, dirs }) => {
const source = await sourceFile(dirs.userData, 2)
const id = await plexo.startSimulated(
{
sourceFilePath: source.path,
- networks: [network('one')],
+ networks: [{ ...network('one'), speedBytesPerSec: 200_000 }],
chunkCount: 2,
- connectionsPerNetwork: 2,
- assembleSpeedBytesPerSec: SIZE / 2
+ connectionsPerNetwork: 2
},
source.sha
)
- await plexo.waitForStatus('assembling')
+ await plexo.waitUntil((state) => state.bytesDownloaded > 0)
+ const staging = join(dirs.dest, `.plexo-${id}.part`)
await plexo.api.pauseDownload(id)
+ expect((await plexo.current())?.status).toBe('paused')
+ expect(existsSync(staging)).toBe(true)
await plexo.api.cancelDownload(id)
- expect((await plexo.current())?.status).toBe('assembling')
- await plexo.waitForStatus('completed')
+ expect(existsSync(staging)).toBe(false)
})
})
diff --git a/src/main/download/blockProgress.ts b/src/main/download/blockProgress.ts
index 886ace0..f04479b 100644
--- a/src/main/download/blockProgress.ts
+++ b/src/main/download/blockProgress.ts
@@ -46,8 +46,8 @@ export function advanceBlock(block: BlockState, networkId: string, position: num
return gained
}
-/** Pulls the frontier back to `position`, for bytes that turned out not to be there — a part file
- * shorter than counted, a racing attempt that lost. `blamed` is the network they came from.
+/** Pulls the frontier back to `position`, for bytes that turned out not to be there or a racing
+ * attempt that lost. `blamed` is the network they came from.
* Returns how much it removed. */
export function retractBlock(block: BlockState, position: number, blamed?: string): number {
const removed = block.bytesDownloaded - position
diff --git a/src/main/download/chunkDownloader.ts b/src/main/download/chunkDownloader.ts
index 07cc7ee..7d61b83 100644
--- a/src/main/download/chunkDownloader.ts
+++ b/src/main/download/chunkDownloader.ts
@@ -1,4 +1,4 @@
-import { createWriteStream, type WriteStream } from 'node:fs'
+import type { Writable } from 'node:stream'
import type { ClientRequest, IncomingMessage } from 'node:http'
import { URL } from 'node:url'
import type { NetworkInterfaceInfo } from '../../shared/types'
@@ -12,9 +12,10 @@ export interface ChunkDownloadOptions {
/** null = open-ended range, download to end of file. */
rangeEnd: number | null
interfaceInfo: NetworkInterfaceInfo
- destinationPath: string
- /** true when resuming a paused chunk — appends to the existing part file instead of overwriting it. */
- append: boolean
+ createDestination: () => Writable
+ /** Network bytes received, before destination backpressure or disk writes. */
+ onNetworkProgress: (bytesReceivedThisRun: number) => void
+ /** Bytes accepted by the destination writer; safe to include in resumable progress. */
onProgress: (bytesDownloadedThisRun: number) => void
signal: AbortSignal
/** The version the download started on, plus any confirmed to serve identical bytes. */
@@ -83,10 +84,10 @@ function versionOf(res: IncomingMessage, served: ServedRange | null): FileVersio
}
/**
- * Downloads a single byte range of a URL, bound to one network interface, into a part file.
+ * Downloads a single byte range of a URL, bound to one network interface, into a supplied writer.
*
* Resolving means the *entire* requested range was written, and nothing else was:
- * a chunk's bytes land at a fixed offset in the reassembled file, so a response
+ * a chunk's bytes land at a fixed offset in the staged file, so a response
* that is short, starts somewhere else, or overruns the range would corrupt the
* output rather than just this chunk. Every one of those is a rejection, which
* puts the block back on the queue for a retry instead of marking it done.
@@ -97,8 +98,8 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
rangeStart,
rangeEnd,
interfaceInfo,
- destinationPath,
- append,
+ createDestination,
+ onNetworkProgress,
onProgress,
signal,
acceptedVersions,
@@ -116,7 +117,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
let bytesDownloaded = 0
let settled = false
let currentReq: ClientRequest | null = null
- let currentFileStream: WriteStream | null = null
+ let currentFileStream: Writable | null = null
let stallWatchdog: NodeJS.Timeout | null = null
const clearWatchdog = (): void => {
@@ -144,14 +145,11 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
const fail = (error: Error): void =>
finish(() => {
currentReq?.destroy()
- // Closing the part file matters twice over: an abandoned stream holds
+ // Closing the writer matters twice over: an abandoned stream holds
// its descriptor for the life of the process (a paused-and-resumed
// download, or a chunk that retries a few times, leaks one per
// attempt until the process hits its open-file limit), and its
- // buffered writes would otherwise land in the part file *after* a
- // resume has already reconciled that file's length. Discarding those
- // writes is safe: what survives on disk is what the next attempt
- // resumes from.
+ // buffered writes would otherwise land after a retry has started.
const stream = currentFileStream
if (stream && !stream.closed) {
stream.once('close', () => reject(error))
@@ -211,7 +209,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
return
}
- // Each part is fetched separately, so a file republished mid-download would otherwise
+ // Each range is fetched separately, so a file republished mid-download would otherwise
// be stitched together from two versions and still pass every length check.
const served = parseContentRange(res.headers['content-range'])
const seen = versionOf(res, served)
@@ -223,7 +221,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
}
// A 206 says where in the file these bytes belong — check it lines up
- // with what we asked for before writing any of them into the part file.
+ // with what we asked for before writing any of them.
if (status === 206) {
if (!served) {
fail(new Error('Server sent a 206 without a usable Content-Range header'))
@@ -252,9 +250,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
onResponse?.({ ttfbMs: Date.now() - sentAt, reusedSocket: req.reusedSocket })
- const fileStream: WriteStream = createWriteStream(destinationPath, {
- flags: append ? 'a' : 'w'
- })
+ const fileStream = createDestination()
currentFileStream = fileStream
res.on('error', fail)
@@ -265,8 +261,7 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
resetWatchdog()
// Written by hand rather than piped so an overlong body can be cut off
- // at the range boundary: a part file longer than its block would push
- // every byte after it out of place at reassembly time.
+ // at the range boundary: an overlong response could overwrite the next block.
res.on('data', (chunk: Buffer) => {
if (settled) return
resetWatchdog()
@@ -278,8 +273,14 @@ export function downloadChunk(options: ChunkDownloadOptions): Promise<void> {
if (usable.length > 0) {
bytesDownloaded += usable.length
- onProgress(bytesDownloaded)
- if (!fileStream.write(usable)) {
+ const progress = bytesDownloaded
+ onNetworkProgress(progress)
+ if (
+ !fileStream.write(usable, (error) => {
+ if (error) fail(error)
+ else if (!settled) onProgress(progress)
+ })
+ ) {
res.pause()
fileStream.once('drain', () => {
resetWatchdog()
diff --git a/src/main/download/downloadFile.ts b/src/main/download/downloadFile.ts
new file mode 100644
index 0000000..f607bf3
--- /dev/null
+++ b/src/main/download/downloadFile.ts
@@ -0,0 +1,81 @@
+import { createWriteStream, type WriteStream } from 'node:fs'
+import { open, rename, rm, stat } from 'node:fs/promises'
+import { dirname, join } from 'node:path'
+
+/** The only large file owned by a download. It lives beside the final file so publishing it
+ * requires no copy and never needs a second file's worth of disk space. */
+export class DownloadFile {
+ readonly path: string
+
+ constructor(
+ readonly destinationPath: string,
+ id: string
+ ) {
+ // Keep the component short even when the final filename is near the filesystem limit.
+ this.path = join(dirname(destinationPath), `.plexo-${id}.part`)
+ }
+
+ async create(): Promise<void> {
+ const handle = await open(this.path, 'wx+')
+ await handle.close()
+ }
+
+ /** Every writer has its own descriptor and explicit offset. Never use append mode here. */
+ writer(position: number): WriteStream {
+ return createWriteStream(this.path, { flags: 'r+', start: position })
+ }
+
+ async read(position: number, length: number): Promise<Buffer> {
+ const handle = await open(this.path, 'r')
+ try {
+ const buffer = Buffer.alloc(length)
+ const { bytesRead } = await handle.read(buffer, 0, length, position)
+ return buffer.subarray(0, bytesRead)
+ } finally {
+ await handle.close()
+ }
+ }
+
+ async writeBuffers(position: number, buffers: readonly Buffer[]): Promise<void> {
+ const handle = await open(this.path, 'r+')
+ try {
+ for (const buffer of buffers) {
+ let written = 0
+ while (written < buffer.length) {
+ const result = await handle.write(buffer, written, buffer.length - written, position)
+ if (result.bytesWritten === 0) throw new Error('Could not write the download file')
+ written += result.bytesWritten
+ position += result.bytesWritten
+ }
+ }
+ } finally {
+ await handle.close()
+ }
+ }
+
+ /** Flushes the staging file before a recovery checkpoint or final publication. */
+ async sync(): Promise<void> {
+ const handle = await open(this.path, 'r+')
+ try {
+ await handle.sync()
+ } finally {
+ await handle.close()
+ }
+ }
+
+ async size(): Promise<number> {
+ return (await stat(this.path)).size
+ }
+
+ async publish(expectedBytes: number): Promise<void> {
+ if (expectedBytes > 0 && (await this.size()) !== expectedBytes) {
+ throw new Error('Download file size does not match the expected size')
+ }
+ await this.sync()
+ await rename(this.path, this.destinationPath)
+ }
+
+ async discard(): Promise<void> {
+ await rm(this.path, { force: true })
+ }
+}
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index 692b54a..2997a09 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -1,9 +1,7 @@
import { randomUUID } from 'node:crypto'
-import { createReadStream, createWriteStream } from 'node:fs'
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'
+import { Writable } from 'node:stream'
import type { BrowserWindow } from 'electron'
import { app, Notification } from 'electron'
import { IpcChannels } from '../../shared/ipc-channels'
@@ -20,15 +18,8 @@ import { interleave, planDownload } from '../../shared/plan'
import { testKnobs } from '../testKnobs'
import { advanceBlock, retractBlock } from './blockProgress'
import { downloadChunk, fetchRange, RemoteChangedError } from './chunkDownloader'
+import { DownloadFile } from './downloadFile'
import { compareVersion, type FileVersion } from './fileVersion'
-import {
- fileSize,
- hedgeFile,
- mergeHedge,
- partFile,
- reconcilePartFileSize,
- removeHedgeFiles
-} from './partFiles'
import { ensureDirectory, reserveDestinationPath } from './paths'
import { pickWork, type SchedulerPolicy, type Work } from './scheduler'
import {
@@ -40,7 +31,6 @@ import {
import {
createSimSession,
downloadChunkSimulated,
- getSimAssembleSpeed,
isSimulatedUrl,
unregisterSimSession
} from './simDownload'
@@ -60,14 +50,16 @@ interface Attempt {
networkId: string
/** Where the request begins, as an offset into the block. Null until it is known. */
startOffset: number | null
- /** Bytes the request has delivered so far. */
+ /** Bytes the destination writer has accepted; safe to resume from this prefix. */
received: number
+ /** Bytes the network has delivered, independently of disk backpressure. */
+ networkReceived: number
+ lastNetworkAt: number
/** When the request was sent. */
startedAt: number
- /** Where it writes: the block's own part file, or for a hedge a file of its own. */
- file: string
- /** The network that last wrote the part file this attempt resumes — whose tail bytes a
- * truncation would discard. */
+ /** A hedge stays in bounded memory until it wins; only primaries write to the staging file. */
+ hedgeBuffers: Buffer[]
+ /** The network previously credited for this block's prefix. */
previousWriter: string | undefined
/** Aborts this request alone; ChunkRuntime.controller aborts the whole stream. */
abort: AbortController
@@ -77,7 +69,7 @@ interface Attempt {
won: boolean
/** What the server's answer cost, for diagnosing slow connections (see PLEXO_DEBUG). */
response: { ttfbMs: number; reusedSocket: boolean } | null
- /** Resolves once the request is over and its file closed. */
+ /** Resolves once the request is over and its writer closed. */
settled: Promise<void>
settle: () => void
}
@@ -118,7 +110,9 @@ interface DownloadRuntime {
requestPayload: StartDownloadRequest
activeInterfaces: NetworkInterfaceInfo[]
chunkRuntimes: Map<number, ChunkRuntime>
- tempDir: string
+ file: DownloadFile
+ runPromise?: Promise<void>
+ publishing: boolean
speedSamplesByChunk: Map<number, SpeedSample[]>
pushScheduled: boolean
blocks: BlockState[]
@@ -142,7 +136,7 @@ interface DownloadRuntime {
}
interface PersistedDownload {
- version: 2
+ version: 3
savedAt: number
state: DownloadState
requestPayload: StartDownloadRequest
@@ -155,7 +149,10 @@ const debug: (...args: unknown[]) => void = process.env['PLEXO_DEBUG']
? (...args) => console.debug('[plexo]', ...args)
: () => {}
-const PROGRESS_THROTTLE_MS = 200
+const UI_UPDATE_MS = 200
+// Syncing a growing file can briefly monopolize a slow destination drive. Keep recovery
+// checkpoints independent of UI updates; pause and publication still force an immediate sync.
+const CHECKPOINT_INTERVAL_MS = 15_000
// Raw per-event deltas are too noisy to display (socket buffers flush in
// irregular bursts a few ms apart). Averaging over a few seconds instead
@@ -217,6 +214,7 @@ const SCHEDULER_POLICY: SchedulerPolicy = {
hedgeAfterMs: testKnobs.hedgeAfterMs,
maxHedgesPerBlock: 2
}
+const MAX_ACTIVE_HEDGES = 2 // At most two block-sized buffers in memory.
// Idle connections look for work every 250 ms, so waiting a bit longer hands a refreshed block
// to a connection that is already proven fast, if there is one.
const REFRESH_HANDOFF_MS = 300
@@ -259,12 +257,6 @@ function delay(ms: number, signal: AbortSignal): Promise<void> {
})
}
-/** Plain, unabortable wait — used only to throttle a simulated download's assemble step to a
- * configured speed (see reassemble()). Assembling isn't cancellable, so there's nothing to race. */
-function sleep(ms: number): Promise<void> {
- return new Promise((resolve) => setTimeout(resolve, ms))
-}
-
function requestedVersion(request: StartDownloadRequest): FileVersion {
return { etag: request.etag, lastModified: request.lastModified, totalBytes: request.totalBytes }
}
@@ -281,33 +273,15 @@ function formatGigabytes(bytes: number): string {
return `${(bytes / 1024 ** 3).toFixed(2)} GB`
}
-/** Throws if there isn't room for the download — a full disk should fail upfront with a clear
- * reason, not partway through as a confusing ENOSPC write error. The part files and the assembled
- * file both exist until assembly finishes, so when they share a volume it needs room for both. */
-async function ensureDiskSpace(
- destinationDir: string,
- partsRoot: string,
- requiredBytes: number
-): Promise<void> {
+/** The staging file is on the destination volume and becomes the final file by rename. */
+async function ensureDiskSpace(destinationDir: string, requiredBytes: number): Promise<void> {
if (requiredBytes <= 0) return // unknown size — nothing to check against
-
- const [destination, parts] = await Promise.all([stat(destinationDir), stat(partsRoot)])
- const needs: [string, number][] =
- destination.dev === parts.dev
- ? [[destinationDir, requiredBytes * 2]]
- : [
- [destinationDir, requiredBytes],
- [partsRoot, requiredBytes]
- ]
-
- for (const [dir, bytes] of needs) {
- const stats = await statfs(dir)
- const availableBytes = stats.bavail * stats.bsize
- if (availableBytes < bytes) {
- throw new Error(
- `Not enough disk space: this download needs ${formatGigabytes(bytes)} (the file plus its temporary parts) but only ${formatGigabytes(availableBytes)} is free`
- )
- }
+ 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`
+ )
}
}
@@ -352,20 +326,15 @@ export class DownloadManager {
const persisted = JSON.parse(
await readFile(this.manifestPath(id), 'utf-8')
) as PersistedDownload
- if (persisted.version !== 2 || persisted.state.id !== id || !persisted.state.blocks)
+ if (persisted.version !== 3 || persisted.state.id !== id || !persisted.state.blocks)
return
const state = persisted.state
const blocks = state.blocks
if (!blocks) return
- // 'assembling' means every block was already 'completed' and only the reassembly step
- // was interrupted — resuming re-enters runChunksToCompletion with nothing left to
- // download, so it goes straight back into reassemble() rather than needing its own
- // restart path.
- if (state.status === 'downloading' || state.status === 'assembling') {
+ if (state.status === 'downloading') {
state.status = 'paused'
state.pausedAt = persisted.savedAt || Date.now()
- state.assembledBytes = 0
}
state.speedBytesPerSec = 0
for (const chunk of state.chunks) {
@@ -384,12 +353,51 @@ export class DownloadManager {
if (block.status === 'downloading') block.status = 'pending'
}
+ const file = new DownloadFile(state.destinationPath, id)
+ if (state.status === 'paused') {
+ const size = await file.size().catch(() => -1)
+ if (size < 0) {
+ const publishedSize = await stat(state.destinationPath).then(
+ (entry) => entry.size,
+ () => -1
+ )
+ const expected = state.totalBytes || state.bytesDownloaded
+ if (
+ blocks.every((block) => block.status === 'completed') &&
+ publishedSize === expected
+ ) {
+ state.status = 'completed'
+ state.error = undefined
+ state.completedAt ??= persisted.savedAt
+ } else {
+ state.status = 'error'
+ state.error =
+ 'The partial download file is missing. Remove this download and start again.'
+ if (publishedSize === 0) await rm(state.destinationPath, { force: true })
+ }
+ } else {
+ for (const block of blocks) {
+ const length = block.rangeEnd === null ? 0 : block.rangeEnd - block.rangeStart + 1
+ if (
+ block.rangeStart + block.bytesDownloaded > size ||
+ (block.status === 'completed' && block.bytesDownloaded !== length)
+ ) {
+ block.status = 'pending'
+ block.bytesDownloaded = 0
+ block.bytesByInterface = {}
+ }
+ }
+ state.bytesDownloaded = blocks.reduce((sum, block) => sum + block.bytesDownloaded, 0)
+ }
+ }
+
restored.push({
state,
requestPayload: persisted.requestPayload,
activeInterfaces: persisted.activeInterfaces,
chunkRuntimes: new Map(),
- tempDir: join(this.downloadDir(id), 'parts'),
+ file,
+ publishing: false,
speedSamplesByChunk: new Map(),
pushScheduled: false,
blocks,
@@ -433,15 +441,11 @@ export class DownloadManager {
}
/** Plexo shows one download at a time (see useAppStore's currentDownload) — starting a second
- * one while one is already running/paused/assembling would silently race it for disk I/O and
+ * one while one is already running or paused would silently race it for disk I/O and
* scramble the renderer's single-download view as updates from both interleave. */
private hasActiveDownload(): boolean {
for (const runtime of this.runtimes.values()) {
- if (
- runtime.state.status === 'downloading' ||
- runtime.state.status === 'paused' ||
- runtime.state.status === 'assembling'
- ) {
+ if (runtime.state.status === 'downloading' || runtime.state.status === 'paused') {
return true
}
}
@@ -473,8 +477,8 @@ export class DownloadManager {
/**
* Dev-tool entry point: "downloads" a file that's already on disk through the exact same
- * pipeline a real download uses — chunking, the block grid, pause/resume, retries, the
- * assembling phase, reassembly — so every feature can be exercised on demand instead of needing
+ * pipeline a real download uses — chunking, the block grid, pause/resume and retries — so
+ * every feature can be exercised on demand instead of needing
* a real multi-network setup and a slow, flaky remote server to provoke retries and errors.
* Only `chunkDownloader`'s HTTP transfer is swapped out (see simDownload.ts); everything else
* in DownloadManager is unaware this isn't a real network transfer.
@@ -490,8 +494,7 @@ export class DownloadManager {
const { url, interfaces, totalBytes } = await createSimSession(
payload.sourceFilePath,
- payload.networks,
- payload.assembleSpeedBytesPerSec || null
+ payload.networks
)
const requestPayload: StartDownloadRequest = {
@@ -507,23 +510,33 @@ export class DownloadManager {
lastModified: null
}
- return this.startWithInterfaces(requestPayload, interfaces)
+ try {
+ return await this.startWithInterfaces(requestPayload, interfaces)
+ } catch (error) {
+ unregisterSimSession(url)
+ throw error
+ }
}
private async startWithInterfaces(
requestPayload: StartDownloadRequest,
interfaces: NetworkInterfaceInfo[]
): Promise<string> {
- await ensureDirectory(this.downloadsRoot())
await ensureDirectory(requestPayload.destinationDir)
- await ensureDiskSpace(
- requestPayload.destinationDir,
- this.downloadsRoot(),
- requestPayload.totalBytes
- )
+ await ensureDiskSpace(requestPayload.destinationDir, requestPayload.totalBytes)
+
+ const plan = planDownload({
+ totalBytes: requestPayload.totalBytes,
+ splittable: requestPayload.supportsRanges,
+ networkCount: interfaces.length,
+ streamsPerNetwork:
+ requestPayload.connectionsPerNetwork ??
+ Math.round(requestPayload.chunkCount / interfaces.length),
+ maxBlockBytes: testKnobs.blockBytes // 8 MB outside tests
+ })
// 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. Done
+ // name can't pick it too and overwrite this one at publish time. Done
// before anything else is created, so a destination we can't write to
// leaves nothing behind.
const destinationPath = await reserveDestinationPath(
@@ -532,24 +545,14 @@ export class DownloadManager {
)
const id = randomUUID()
- const tempDir = join(this.downloadDir(id), 'parts')
+ const file = new DownloadFile(destinationPath, id)
try {
- await ensureDirectory(tempDir)
+ await file.create()
} catch (error) {
await rm(destinationPath, { force: true })
throw error
}
- const plan = planDownload({
- totalBytes: requestPayload.totalBytes,
- splittable: requestPayload.supportsRanges,
- networkCount: interfaces.length,
- streamsPerNetwork:
- requestPayload.connectionsPerNetwork ??
- Math.round(requestPayload.chunkCount / interfaces.length),
- maxBlockBytes: testKnobs.blockBytes // 8 MB outside tests
- })
-
// The UI caps how many cells it renders separately (see BlockGrid), by bucketing these
// blocks rather than by shrinking their count here.
const blocks: BlockState[] = []
@@ -619,7 +622,8 @@ export class DownloadManager {
requestPayload,
activeInterfaces,
chunkRuntimes: new Map(),
- tempDir,
+ file,
+ publishing: false,
speedSamplesByChunk: new Map(),
pushScheduled: false,
blocks,
@@ -636,14 +640,14 @@ export class DownloadManager {
await this.persistNow(runtime)
this.pushUpdate(runtime)
- void this.runChunksToCompletion(runtime, runtime.state.chunks)
+ runtime.runPromise = this.runChunksToCompletion(runtime, runtime.state.chunks)
return id
}
async pause(id: string): Promise<void> {
const runtime = this.runtimes.get(id)
- if (!runtime || runtime.state.status !== 'downloading') return
+ if (!runtime || runtime.state.status !== 'downloading' || runtime.publishing) return
runtime.state.status = 'paused'
runtime.state.speedBytesPerSec = 0
@@ -673,6 +677,7 @@ export class DownloadManager {
chunkRuntime.controller.abort()
}
this.pushUpdate(runtime)
+ await runtime.runPromise
await this.persistNow(runtime)
}
@@ -728,21 +733,12 @@ export class DownloadManager {
return
}
- // 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 ensureDirectory(runtime.tempDir)
- await removeHedgeFiles(runtime.tempDir)
- await Promise.all(
- runtime.blocks.map(async (block) => {
- if (block.status !== 'completed' || block.rangeEnd === null) return
- const size = await stat(partFile(runtime.tempDir, block.index)).then(
- (stats) => stats.size,
- () => -1
- )
- if (size !== block.rangeEnd - block.rangeStart + 1) block.status = 'pending'
- })
- )
+ if ((await runtime.file.size().catch(() => -1)) < 0) {
+ runtime.state.error =
+ 'The partial download file is unavailable. Reconnect the destination drive and try again.'
+ this.pushUpdate(runtime)
+ return
+ }
if (runtime.state.status !== 'paused' && runtime.state.status !== 'error') return
for (let index = 0; index < runtime.state.chunks.length; index++) {
@@ -781,16 +777,17 @@ export class DownloadManager {
// paused download saved before that was the rule can't hand every block to one network.
const pending = runtime.state.chunks.filter((chunk) => chunk.status !== 'completed')
const toRun = pending.length > 0 ? pending : runtime.state.chunks
- void this.runChunksToCompletion(
+ runtime.runPromise = this.runChunksToCompletion(
runtime,
interleave(toRun, (chunk) => chunk.interfaceId)
)
}
- cancel(id: string): void {
+ async cancel(id: string): Promise<void> {
const runtime = this.runtimes.get(id)
if (
!runtime ||
+ runtime.publishing ||
(runtime.state.status !== 'downloading' &&
runtime.state.status !== 'paused' &&
runtime.state.status !== 'error')
@@ -807,12 +804,14 @@ export class DownloadManager {
chunkRuntime.controller.abort()
}
this.pushUpdate(runtime, false)
- void this.cleanupTempDir(runtime)
- void this.discardUnfinishedDestination(runtime)
- void this.removePersistedDownload(runtime)
+ await runtime.runPromise
+ await runtime.file.discard()
+ unregisterSimSession(runtime.requestPayload.url)
+ await this.discardUnfinishedDestination(runtime)
+ await this.removePersistedDownload(runtime)
}
- remove(id: string): void {
+ async remove(id: string): Promise<void> {
const runtime = this.runtimes.get(id)
if (
runtime &&
@@ -820,10 +819,10 @@ export class DownloadManager {
runtime.state.status === 'paused' ||
runtime.state.status === 'error')
) {
- this.cancel(id)
+ await this.cancel(id)
}
this.runtimes.delete(id)
- if (runtime) void this.removePersistedDownload(runtime)
+ if (runtime) await this.removePersistedDownload(runtime)
}
async suspendAll(): Promise<void> {
@@ -889,19 +888,20 @@ export class DownloadManager {
// Paused, errored, or cancelled — nothing left to do right now.
if (runtime.state.status === 'error' || runtime.state.status === 'cancelled') {
this.pushUpdate(runtime)
- await this.cleanupTempDir(runtime)
+ await runtime.file.discard()
+ unregisterSimSession(runtime.requestPayload.url)
await this.discardUnfinishedDestination(runtime)
}
return
}
- runtime.state.status = 'assembling'
- runtime.state.assembledBytes = 0
- runtime.state.speedBytesPerSec = 0
- this.pushUpdate(runtime)
-
+ runtime.publishing = true
try {
- await this.reassemble(runtime)
+ if (runtime.blocks.some((block) => block.status !== 'completed')) {
+ throw new Error('Download is incomplete — refusing to publish the file')
+ }
+ await this.persistNow(runtime)
+ await runtime.file.publish(runtime.state.totalBytes)
runtime.state.status = 'completed'
runtime.state.completedAt = Date.now()
runtime.state.bytesDownloaded = runtime.state.totalBytes || runtime.state.bytesDownloaded
@@ -911,15 +911,17 @@ export class DownloadManager {
runtime.state.error = error instanceof Error ? error.message : String(error)
this.notify('Download Failed', `${runtime.state.fileName}: ${runtime.state.error}`)
}
+ runtime.publishing = false
this.pushUpdate(runtime)
- await this.cleanupTempDir(runtime)
+ if (runtime.state.status !== 'completed') await runtime.file.discard()
+ unregisterSimSession(runtime.requestPayload.url)
await this.discardUnfinishedDestination(runtime)
}
/**
* Reconnects a connection that isn't carrying its weight. Reconnecting is cheap — the block
- * resumes from its part file — and a fresh connection usually lands somewhere healthy.
+ * resumes from the staging file — and a fresh connection usually lands somewhere healthy.
*
* - Silent: nothing received for SILENT_AFTER_MS while the file is demonstrably being served
* to others. Judged by the clock alone, so it also catches a network that has yet to
@@ -959,7 +961,7 @@ export class DownloadManager {
}
private isSilent(runtime: DownloadRuntime, attempt: Attempt, now: number): boolean {
- if (attempt.received > 0 || now - attempt.startedAt < SILENT_AFTER_MS) return false
+ if (attempt.networkReceived > 0 || now - attempt.startedAt < SILENT_AFTER_MS) return false
// Until something has arrived, a quiet origin is just a slow one — nothing to blame this
// connection for.
return runtime.blocks.some(
@@ -1062,11 +1064,10 @@ export class DownloadManager {
networkId: iface.id,
startOffset: null,
received: 0,
+ networkReceived: 0,
+ lastNetworkAt: 0,
startedAt: Date.now(),
- file:
- work.kind === 'primary'
- ? partFile(runtime.tempDir, block.index)
- : hedgeFile(runtime.tempDir, block.index, chunk.id),
+ hedgeBuffers: [],
// Whoever held this block before now is the one whose tail bytes a truncation would
// discard — captured before the lease overwrites the field.
previousWriter: block.interfaceId,
@@ -1120,25 +1121,19 @@ export class DownloadManager {
const { block } = attempt
try {
if (attempt.kind === 'primary') {
- // Without range support the server can only send the file from the start, so a retry or
- // resume begins again at byte 0 rather than asking for a Range it will ignore.
- const resumeOffset = await reconcilePartFileSize(
- attempt.file,
- runtime.requestPayload.supportsRanges ? block.bytesDownloaded : 0
- )
- attempt.startOffset = resumeOffset
+ // A failed range leaves its written prefix in the staging file. Without range support,
+ // the server can only restart from byte zero.
+ attempt.startOffset = runtime.requestPayload.supportsRanges ? block.bytesDownloaded : 0
// What another attempt has already secured stays counted.
- const keep = Math.max(resumeOffset, this.otherAttemptsPosition(runtime, attempt))
+ const keep = Math.max(attempt.startOffset, this.otherAttemptsPosition(runtime, attempt))
if (retractBlock(block, keep, attempt.previousWriter) > 0) this.recomputeAggregates(runtime)
} else {
- // The block's file only ever grows, so what is on disk now is a prefix it will still
- // hold once its writer has stopped — the point this attempt picks up from.
- attempt.startOffset = await fileSize(partFile(runtime.tempDir, block.index))
+ attempt.startOffset = block.bytesDownloaded
}
const length = block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1
if (length !== null && attempt.startOffset >= length) {
- // The part file already holds the whole block.
+ // The staging file already holds the whole block.
return attempt.kind === 'primary'
? { type: 'completed' }
: { type: 'aborted', reason: 'lost' }
@@ -1153,16 +1148,31 @@ export class DownloadManager {
rangeStart: block.rangeStart + attempt.startOffset,
rangeEnd: block.rangeEnd,
interfaceInfo: iface,
- destinationPath: attempt.file,
- append: attempt.kind === 'primary' && attempt.startOffset > 0,
+ createDestination: () =>
+ attempt.kind === 'primary'
+ ? runtime.file.writer(block.rangeStart + (attempt.startOffset ?? 0))
+ : new Writable({
+ write: (chunk: Buffer, _encoding, callback) => {
+ attempt.hedgeBuffers.push(Buffer.from(chunk))
+ callback()
+ }
+ }),
signal: AbortSignal.any([self.controller.signal, attempt.abort.signal]),
acceptedVersions: runtime.acceptedVersions,
onResponse: (info) => (attempt.response = info),
+ onNetworkProgress: (bytesThisRun) => {
+ const delta = bytesThisRun - attempt.networkReceived
+ if (delta > 0) {
+ attempt.networkReceived = bytesThisRun
+ attempt.lastNetworkAt = Date.now()
+ this.onNetworkProgress(runtime, chunk, self, delta)
+ }
+ },
onProgress: (bytesThisRun) => {
const delta = bytesThisRun - attempt.received
if (delta > 0) {
attempt.received = bytesThisRun
- this.onAttemptProgress(runtime, chunk, self, attempt, delta)
+ this.onAttemptProgress(runtime, chunk, attempt)
}
}
})
@@ -1177,11 +1187,10 @@ export class DownloadManager {
}
}
- private onAttemptProgress(
+ private onNetworkProgress(
runtime: DownloadRuntime,
chunk: ChunkState,
self: ChunkRuntime,
- attempt: Attempt,
deltaBytes: number
): void {
const now = Date.now()
@@ -1192,18 +1201,21 @@ export class DownloadManager {
runtime.speedSamplesByChunk.set(chunk.id, samples)
}
chunk.speedBytesPerSec = pushSpeedSample(samples, self.receivedBytes, now)
+ runtime.state.speedBytesPerSec = sumChunkSpeeds(runtime)
+ this.scheduleUpdate(runtime)
+ }
+ private onAttemptProgress(runtime: DownloadRuntime, chunk: ChunkState, attempt: Attempt): void {
// Only what gets the block further than it already was counts as progress: a racing attempt
// re-fetches bytes the other already has.
const gained = advanceBlock(attempt.block, attempt.networkId, this.attemptPosition(attempt))
chunk.bytesDownloaded += gained
- // This runs on every socket data event, so it folds the delta in rather than re-summing
+ // This runs on every completed writer callback, so it folds the delta in rather than re-summing
// every block — that sum is O(blocks), and a large file has thousands of them. The other
// callers of recomputeAggregates are rare enough to afford the full pass, and each one
// re-derives the true total, so any drift here cannot accumulate.
runtime.state.bytesDownloaded += gained
- runtime.state.speedBytesPerSec = sumChunkSpeeds(runtime)
this.scheduleUpdate(runtime)
}
@@ -1222,7 +1234,8 @@ export class DownloadManager {
network: iface.displayName,
kind: attempt.kind,
outcome: outcome.type === 'aborted' ? `aborted:${outcome.reason}` : outcome.type,
- bytes: attempt.received,
+ networkBytes: attempt.networkReceived,
+ writtenBytes: attempt.received,
ms: Date.now() - attempt.startedAt,
...attempt.response
})
@@ -1255,29 +1268,29 @@ export class DownloadManager {
attempt.won = true
if (attempt.kind === 'hedge') {
- // The block's own file has to stop growing before the hedge's bytes can be joined onto it.
+ // The primary writer must close before the winning bytes overwrite its range.
const rivals = (runtime.attempts.get(block.index) ?? []).filter((a) => a !== attempt)
for (const rival of rivals) this.abortAttempt(rival, 'lost')
await Promise.all(rivals.map((rival) => rival.settled))
-
- const merged = await mergeHedge(
- partFile(runtime.tempDir, block.index),
- attempt.file,
- attempt.startOffset ?? 0,
- block.rangeEnd === null ? 0 : block.rangeEnd - block.rangeStart + 1
- ).catch(() => false)
- await rm(attempt.file, { force: true }).catch(() => {})
+ const expected = block.rangeEnd === null ? 0 : block.rangeEnd - block.rangeStart + 1
+ const hedgeBytes = attempt.hedgeBuffers.reduce((sum, buffer) => sum + buffer.length, 0)
+ let merged = hedgeBytes === expected - (attempt.startOffset ?? 0)
+ if (merged) {
+ try {
+ await runtime.file.writeBuffers(
+ block.rangeStart + (attempt.startOffset ?? 0),
+ attempt.hedgeBuffers
+ )
+ } catch {
+ merged = false
+ }
+ }
+ attempt.hedgeBuffers.length = 0
if (!merged) {
- // The two didn't fit together. Whatever prefix the block's file holds is still good, so
- // the block goes back to the queue from there.
- const durable = await fileSize(partFile(runtime.tempDir, block.index))
+ // A failed hedge commit leaves the primary's prefix; the next worker overwrites the rest.
this.endAttempt(runtime, self, attempt)
- const removed = retractBlock(
- block,
- Math.min(block.bytesDownloaded, durable),
- attempt.networkId
- )
+ const removed = retractBlock(block, attempt.startOffset ?? 0, attempt.networkId)
chunk.bytesDownloaded = Math.max(0, chunk.bytesDownloaded - removed)
block.status = 'pending'
this.recomputeAggregates(runtime)
@@ -1296,7 +1309,8 @@ export class DownloadManager {
const blockBytes = block.rangeEnd - block.rangeStart + 1
chunk.bytesDownloaded += advanceBlock(block, attempt.networkId, blockBytes)
self.lastBlockSpeed =
- ((blockBytes - (attempt.startOffset ?? 0)) / Math.max(1, Date.now() - attempt.startedAt)) *
+ (attempt.networkReceived /
+ Math.max(1, (attempt.lastNetworkAt || Date.now()) - attempt.startedAt)) *
1000
}
self.failures = 0
@@ -1339,8 +1353,14 @@ export class DownloadManager {
): Promise<'continue'> {
// 'lost': another attempt decided the block, so its state is no longer this one's to touch.
// 'refresh': not a failure — no retry counted, no backoff. Whoever takes the block next
- // resumes it from its part file on a new connection.
- await this.letGo(runtime, chunk, self, attempt, reason === 'refresh' && attempt.received === 0)
+ // resumes it from the staging file on a new connection.
+ await this.letGo(
+ runtime,
+ chunk,
+ self,
+ attempt,
+ reason === 'refresh' && attempt.networkReceived === 0
+ )
if (reason === 'refresh' && attempt.kind === 'primary') {
// A moment before this stream asks again, so that a connection already proven fast, if
// there is one, gets to the block before this one does.
@@ -1362,7 +1382,7 @@ export class DownloadManager {
): Promise<'continue' | 'stop'> {
const message = error instanceof Error ? error.message : String(error)
chunk.error = message
- const deliveredNothing = attempt.received === 0
+ const deliveredNothing = attempt.networkReceived === 0
if (error instanceof RemoteChangedError) {
const verdict =
@@ -1475,9 +1495,8 @@ export class DownloadManager {
}
if (deliveredNothing) runtime.avoidNetworkByBlock.set(block.index, attempt.networkId)
this.goIdle(chunk)
- return attempt.kind === 'hedge'
- ? rm(attempt.file, { force: true }).catch(() => {})
- : Promise.resolve()
+ attempt.hedgeBuffers.length = 0
+ return Promise.resolve()
}
/** Takes back the progress a hedge alone had made, now that it is not going to finish. */
@@ -1533,6 +1552,15 @@ export class DownloadManager {
Date.now(),
SCHEDULER_POLICY
)
+ if (
+ work?.kind === 'hedge' &&
+ [...runtime.attempts.values()].flat().filter((attempt) => attempt.kind === 'hedge')
+ .length >= MAX_ACTIVE_HEDGES
+ ) {
+ this.goIdle(chunk)
+ await delay(250, controller.signal)
+ continue
+ }
if (!work) {
this.goIdle(chunk)
// While blocks are still in flight, stay available in case one fails and is handed back,
@@ -1589,10 +1617,12 @@ export class DownloadManager {
let compared = 0
for (const block of picks) {
- const partPath = partFile(runtime.tempDir, block.index)
let local: Buffer
try {
- local = (await readFile(partPath)).subarray(0, SAMPLE_BYTES)
+ local = await runtime.file.read(
+ block.rangeStart,
+ Math.min(SAMPLE_BYTES, block.bytesDownloaded)
+ )
} catch {
continue
}
@@ -1633,7 +1663,7 @@ export class DownloadManager {
setTimeout(() => {
runtime.pushScheduled = false
this.pushUpdate(runtime)
- }, PROGRESS_THROTTLE_MS)
+ }, UI_UPDATE_MS)
}
private pushUpdate(runtime: DownloadRuntime, persist = true): void {
@@ -1649,108 +1679,6 @@ export class DownloadManager {
window.webContents.send(IpcChannels.downloadUpdated, structuredClone(runtime.state))
}
- /**
- * Concatenates the part files into the destination, refusing to write a file
- * that isn't demonstrably the whole download. Every check here is a backstop
- * for a bug elsewhere rather than an expected condition — but the failure
- * mode it guards against is the worst one this app has: handing the user a
- * truncated file, calling it completed, and deleting the parts that would
- * have let them resume it.
- */
- private async reassemble(runtime: DownloadRuntime): Promise<void> {
- const missing = runtime.blocks.filter((block) => block.status !== 'completed')
- if (missing.length > 0) {
- throw new Error(
- `Download is incomplete: ${missing.length} of ${runtime.totalBlocks} parts never finished`
- )
- }
-
- const ASSEMBLE_STREAM_BUFFER_BYTES = 1024 * 1024 // 1 MB buffer for fast sequential disk assembly
- const output = createWriteStream(runtime.state.destinationPath, {
- highWaterMark: ASSEMBLE_STREAM_BUFFER_BYTES
- })
- // pipeline reports a failure by rejecting, but only listens while it runs: destroying the
- // output afterwards can still surface an in-flight write as an 'error' event, and an
- // unhandled 'error' on a stream takes down the main process rather than failing this one
- // download. So one listener stays for the stream's whole life.
- output.on('error', () => {})
-
- let bytesWritten = 0
- // A single pipeline for the whole file, not one per part: pipeline leaves its listeners on
- // a destination it doesn't end, so one per part would pile up on the output — five per part.
- const source = Readable.from(
- this.readParts(runtime, ASSEMBLE_STREAM_BUFFER_BYTES, (partBytes) => {
- bytesWritten += partBytes
- // Reported per part rather than per underlying write so the assembling visualization
- // advances in the same units the block grid already shows — one step per chunk, not a
- // byte stream.
- runtime.state.assembledBytes = bytesWritten
- this.scheduleUpdate(runtime)
- }),
- { objectMode: false, highWaterMark: ASSEMBLE_STREAM_BUFFER_BYTES }
- )
-
- try {
- // Rejects on an error from either side, and destroys both.
- await pipeline(source, output)
-
- if (runtime.state.totalBytes > 0 && bytesWritten !== runtime.state.totalBytes) {
- throw new Error(
- `Assembled file is ${bytesWritten} bytes but should be ${runtime.state.totalBytes} — refusing to keep a corrupt file`
- )
- }
- } catch (error) {
- output.destroy()
- await finished(output).catch(() => {})
- // Leaving a half-written file where the user expects their download is
- // worse than leaving nothing: it looks like the download they asked for.
- await rm(runtime.state.destinationPath, { force: true })
- throw error
- }
- }
-
- /** The download's bytes in file order: each part checked against the size its range says, then
- * read through. `onPartRead` is told how many bytes each part held, once it has been read. */
- private async *readParts(
- runtime: DownloadRuntime,
- bufferBytes: number,
- onPartRead: (partBytes: number) => void
- ): AsyncGenerator<Buffer> {
- // Set only for a dev-tool simulated download that asked for a slowed-down assemble — real
- // downloads always reassemble at full disk speed. Throttling here (rather than faking it in
- // the renderer) exercises the exact same assembledBytes/IPC path a real assemble uses.
- const assembleSpeedBytesPerSec = getSimAssembleSpeed(runtime.requestPayload.url)
-
- for (let i = 0; i < runtime.totalBlocks; i++) {
- const partPath = partFile(runtime.tempDir, i)
- const block = runtime.blocks[i]
- const expectedBytes = block.rangeEnd === null ? null : block.rangeEnd - block.rangeStart + 1
- const actualBytes = (await stat(partPath)).size
-
- if (expectedBytes !== null && actualBytes !== expectedBytes) {
- throw new Error(
- `Part ${i} is ${actualBytes} bytes but should be ${expectedBytes} — refusing to write a corrupt file`
- )
- }
-
- let read = 0
- for await (const chunk of createReadStream(partPath, { highWaterMark: bufferBytes })) {
- read += (chunk as Buffer).length
- yield chunk as Buffer
- }
- if (read !== actualBytes) {
- throw new Error(
- `Part ${i} changed while it was being assembled — refusing to write a corrupt file`
- )
- }
-
- if (assembleSpeedBytesPerSec) {
- await sleep(Math.max(1, (read / assembleSpeedBytesPerSec) * 1000))
- }
- onPartRead(read)
- }
- }
-
/**
* 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
@@ -1766,21 +1694,12 @@ export class DownloadManager {
}
}
- private async cleanupTempDir(runtime: DownloadRuntime): Promise<void> {
- unregisterSimSession(runtime.requestPayload.url)
- try {
- await rm(runtime.tempDir, { recursive: true, force: true })
- } catch {
- // Best-effort cleanup — a leftover temp dir isn't worth surfacing an error for.
- }
- }
-
private schedulePersistence(runtime: DownloadRuntime): void {
if (this.suspending || runtime.removed || runtime.persistenceTimer) return
runtime.persistenceTimer = setTimeout(() => {
runtime.persistenceTimer = undefined
void this.persistNow(runtime)
- }, PROGRESS_THROTTLE_MS)
+ }, CHECKPOINT_INTERVAL_MS)
}
private persistNow(runtime: DownloadRuntime): Promise<void> {
@@ -1798,12 +1717,15 @@ export class DownloadManager {
const path = this.manifestPath(runtime.state.id)
const temporaryPath = `${path}.tmp`
const persisted: PersistedDownload = {
- version: 2,
+ version: 3,
savedAt: Date.now(),
state: structuredClone(runtime.state),
requestPayload: runtime.requestPayload,
activeInterfaces: runtime.activeInterfaces
}
+ if (runtime.state.status === 'downloading' || runtime.state.status === 'paused') {
+ await runtime.file.sync()
+ }
await ensureDirectory(dir)
await writeFile(temporaryPath, JSON.stringify(persisted), 'utf-8')
await rename(temporaryPath, path)
@@ -1818,6 +1740,7 @@ export class DownloadManager {
runtime.removed = true
if (runtime.persistenceTimer) clearTimeout(runtime.persistenceTimer)
await runtime.persistenceChain.catch(() => {})
+ await runtime.file.discard().catch(() => {})
await rm(this.downloadDir(runtime.state.id), { recursive: true, force: true })
}
}
diff --git a/src/main/download/partFiles.ts b/src/main/download/partFiles.ts
deleted file mode 100644
index e8eea4c..0000000
--- a/src/main/download/partFiles.ts
+++ /dev/null
@@ -1,84 +0,0 @@
-import { createReadStream, createWriteStream } from 'node:fs'
-import { readdir, rm, stat, truncate } from 'node:fs/promises'
-import { join } from 'node:path'
-import { pipeline } from 'node:stream/promises'
-
-// Where a block's bytes live while a download is in progress. A block's own file (`part-N`) holds
-// it from its first byte on and only ever grows by appending, so its size is always a valid
-// prefix of the block. A racing attempt (see scheduler.ts) writes its own file, and only becomes
-// part of `part-N` if it wins.
-
-export const partFile = (dir: string, blockIndex: number): string => join(dir, `part-${blockIndex}`)
-
-export const hedgeFile = (dir: string, blockIndex: number, streamId: number): string =>
- join(dir, `part-${blockIndex}.hedge-${streamId}`)
-
-/** Bytes on disk in `path`; 0 if it doesn't exist. */
-export async function fileSize(path: string): Promise<number> {
- try {
- return (await stat(path)).size
- } catch {
- return 0
- }
-}
-
-// A part file's on-disk size is the only thing we can actually trust across a retry or resume —
-// appending opens the file with flag 'a', which always writes at the real end-of-file regardless
-// of what byte count we think we're at, so any mismatch (a crash, a write that hadn't flushed
-// yet) would otherwise silently shift every byte after it. Truncating to the smaller of the two
-// counts keeps the part file's length and our own bookkeeping in agreement before we append
-// another byte to it.
-export async function reconcilePartFileSize(
- partPath: string,
- expectedBytes: number
-): Promise<number> {
- const actualBytes = await fileSize(partPath)
- const safeBytes = Math.min(expectedBytes, actualBytes)
- if (actualBytes !== safeBytes) {
- await truncate(partPath, safeBytes)
- }
- return safeBytes
-}
-
-/**
- * Finishes a block from a racing attempt's file: keeps the block's first `hedgeStart` bytes and
- * appends what the hedge fetched from there to the end. Only valid once nothing else is writing
- * the part file.
- *
- * Resolves true when the part file then holds all `blockBytes`. Resolves false when the two
- * don't fit together — the part file holds less than `hedgeStart` (bytes its writer never
- * flushed), the hedge's file isn't the size its range says, or the joined file comes out the
- * wrong size — and in every such case the part file is left holding exactly the bytes it had
- * before the join point and nothing after it, which are known good. If the copy itself fails it
- * throws, leaving the part file a valid prefix of the block, the same state a crash would.
- */
-export async function mergeHedge(
- partPath: string,
- hedgePath: string,
- hedgeStart: number,
- blockBytes: number
-): Promise<boolean> {
- if ((await fileSize(hedgePath)) !== blockBytes - hedgeStart) return false
- if ((await fileSize(partPath)) < hedgeStart) return false
-
- if (hedgeStart > 0) await truncate(partPath, hedgeStart)
- await pipeline(
- createReadStream(hedgePath),
- createWriteStream(partPath, { flags: hedgeStart > 0 ? 'a' : 'w' })
- )
- if ((await fileSize(partPath)) === blockBytes) return true
-
- // Something else wrote to the file mid-join, so nothing past the join can be trusted.
- await truncate(partPath, hedgeStart)
- return false
-}
-
-/** Removes racing attempts' files left in `dir` — by a crash, or a pause that abandoned them. */
-export async function removeHedgeFiles(dir: string): Promise<void> {
- const names = await readdir(dir).catch(() => [] as string[])
- await Promise.all(
- names
- .filter((name) => name.includes('.hedge-'))
- .map((name) => rm(join(dir, name), { force: true }))
- )
-}
diff --git a/src/main/download/paths.ts b/src/main/download/paths.ts
index cb275d3..83c2c27 100644
--- a/src/main/download/paths.ts
+++ b/src/main/download/paths.ts
@@ -43,11 +43,11 @@ export async function ensureDirectory(directory: string): Promise<void> {
* 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
+ * Creating it is the point. A download's bytes go to a hidden staging file and only become
+ * visible under the final name at publication, 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
+ * first — or, if their publications 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
diff --git a/src/main/download/simDownload.ts b/src/main/download/simDownload.ts
index 7fdc226..a4d2812 100644
--- a/src/main/download/simDownload.ts
+++ b/src/main/download/simDownload.ts
@@ -1,4 +1,4 @@
-import { createReadStream, createWriteStream } from 'node:fs'
+import { createReadStream } from 'node:fs'
import { stat } from 'node:fs/promises'
import { randomUUID } from 'node:crypto'
import type { NetworkInterfaceInfo, SimulatedNetworkConfig } from '../../shared/types'
@@ -18,22 +18,19 @@ interface SimSession {
sourcePath: string
/** Keyed by the synthetic NetworkInterfaceInfo.id assigned to each simulated network. */
networks: Map<string, SimulatedNetworkConfig>
- /** bytes/sec to throttle reassembly to, or null to assemble at full disk speed. */
- assembleSpeedBytesPerSec: number | null
}
const sessions = new Map<string, SimSession>()
/** Registers one dev-tool "virtual download" run and returns the `plexo-sim://<token>` url to
* use as its `StartDownloadRequest.url` — everything downstream (blocks, chunks, persistence,
- * the assembling phase) is unaware this isn't a real network transfer. */
+ * storage) is unaware this isn't a real network transfer. */
function registerSimSession(
sourcePath: string,
- networks: Map<string, SimulatedNetworkConfig>,
- assembleSpeedBytesPerSec: number | null
+ networks: Map<string, SimulatedNetworkConfig>
): string {
const token = randomUUID()
- sessions.set(token, { sourcePath, networks, assembleSpeedBytesPerSec })
+ sessions.set(token, { sourcePath, networks })
return token
}
@@ -42,20 +39,11 @@ export function unregisterSimSession(url: string): void {
sessions.delete(url.slice(SIM_URL_PREFIX.length))
}
-/** How slowly `DownloadManager.reassemble()` should stitch this simulated download's part
- * files together, or null when it isn't simulated (or wasn't given an assemble speed) and
- * should run at full disk speed as usual. */
-export function getSimAssembleSpeed(url: string): number | null {
- if (!isSimulatedUrl(url)) return null
- return sessions.get(url.slice(SIM_URL_PREFIX.length))?.assembleSpeedBytesPerSec ?? null
-}
-
/** Builds the synthetic interfaces + registers the sim session a `StartDownloadRequest` needs
* to drive a simulated download through the normal `DownloadManager.start()` path. */
export async function createSimSession(
sourceFilePath: string,
- networkConfigs: SimulatedNetworkConfig[],
- assembleSpeedBytesPerSec: number | null
+ networkConfigs: SimulatedNetworkConfig[]
): Promise<{ url: string; interfaces: NetworkInterfaceInfo[]; totalBytes: number }> {
const fileStat = await stat(sourceFilePath)
const networks = new Map<string, SimulatedNetworkConfig>()
@@ -71,21 +59,29 @@ export async function createSimSession(
}
})
- const token = registerSimSession(sourceFilePath, networks, assembleSpeedBytesPerSec)
+ const token = registerSimSession(sourceFilePath, networks)
return { url: `${SIM_URL_PREFIX}${token}`, interfaces, totalBytes: fileStat.size }
}
const DEFAULT_SPEED_BYTES_PER_SEC = 3 * 1024 * 1024
/** Local-file stand-in for `downloadChunk()` — same contract (resolves only once the exact
- * byte range is written to `destinationPath`, honors `signal`, calls `onProgress` with
+ * byte range is written to the supplied writer, honors `signal`, calls `onProgress` with
* cumulative bytes) so `DownloadManager` can swap one for the other without knowing which one
* it's running. Reads the requested range off disk instead of over HTTP, throttled to the
* simulated network's configured speed so the UI has something real to show — a chunk grid or
* speed readout that jumped from 0 to 100% instantly would defeat the point of simulating it. */
export function downloadChunkSimulated(options: ChunkDownloadOptions): Promise<void> {
- const { url, rangeStart, rangeEnd, interfaceInfo, destinationPath, append, onProgress, signal } =
- options
+ const {
+ url,
+ rangeStart,
+ rangeEnd,
+ interfaceInfo,
+ createDestination,
+ onNetworkProgress,
+ onProgress,
+ signal
+ } = options
return new Promise((resolve, reject) => {
if (signal.aborted) {
@@ -111,7 +107,7 @@ export function downloadChunkSimulated(options: ChunkDownloadOptions): Promise<v
start: rangeStart,
end: rangeEnd ?? undefined
})
- const output = createWriteStream(destinationPath, { flags: append ? 'a' : 'w' })
+ const output = createDestination()
let bytesWritten = 0
let settled = false
@@ -144,10 +140,16 @@ export function downloadChunkSimulated(options: ChunkDownloadOptions): Promise<v
const delayMs = Math.max(1, (buffer.length / speedBytesPerSec) * 1000)
setTimeout(() => {
if (settled) return
- output.write(buffer)
bytesWritten += buffer.length
- onProgress(bytesWritten)
- input.resume()
+ const progress = bytesWritten
+ onNetworkProgress(progress)
+ output.write(buffer, (error) => {
+ if (error) fail(error)
+ else if (!settled) {
+ onProgress(progress)
+ input.resume()
+ }
+ })
}, delayMs)
})
diff --git a/src/main/ipc/handlers.ts b/src/main/ipc/handlers.ts
index a6c52a4..df716f9 100644
--- a/src/main/ipc/handlers.ts
+++ b/src/main/ipc/handlers.ts
@@ -177,13 +177,9 @@ export function registerIpcHandlers(getWindow: () => BrowserWindow | null): Down
manager.resume(id)
})
- handle('cancelDownload', async (_event, id) => {
- manager.cancel(id)
- })
+ handle('cancelDownload', async (_event, id) => manager.cancel(id))
- handle('removeDownload', async (_event, id) => {
- manager.remove(id)
- })
+ handle('removeDownload', async (_event, id) => manager.remove(id))
// Kicked off once at startup, not per-call — later renderer calls (e.g. a remount) just await
// the same in-flight/settled check instead of re-hitting the GitHub API.
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx
index e528ad6..b9098a4 100644
--- a/src/renderer/src/App.tsx
+++ b/src/renderer/src/App.tsx
@@ -41,11 +41,6 @@ function renderDownload(
networkCount: new Set(download.chunks.map((chunk) => chunk.interfaceId)).size
}
}
- case 'assembling':
- return {
- screen: <DownloadingScreen download={download} />,
- titleBarStatus: { kind: 'assembling' }
- }
case 'completed':
return {
screen: <CompleteScreen download={download} onNewDownload={handlers.onNewDownload} />,
diff --git a/src/renderer/src/components/BlockGrid.tsx b/src/renderer/src/components/BlockGrid.tsx
index 5ad5cbe..f182966 100644
--- a/src/renderer/src/components/BlockGrid.tsx
+++ b/src/renderer/src/components/BlockGrid.tsx
@@ -35,12 +35,6 @@ const GRID_INSET_PX = 3
const UNATTRIBUTED_SOLID = 'var(--text-tertiary)'
const UNATTRIBUTED_BG = 'var(--track-bg)'
-// Assembled bytes are deliberately not painted in any network's color: once a chunk is stitched
-// onto disk it isn't "that network's work" anymore so much as "already part of the file", and a
-// dedicated neutral tone is what makes the sweep across the grid read as progress rather than as
-// chunks quietly losing their color for no reason.
-const ASSEMBLED_SOLID = 'var(--text)'
-
/** One network's share of a cell's downloaded bytes. */
interface CellSegment {
interfaceId: string
@@ -150,14 +144,6 @@ interface BlockGridProps {
knownSize: boolean
remainingBytes: number
isPaused?: boolean
- /** All blocks are 'completed' by the time this is true — the grid switches from showing which
- * network fetched each chunk to showing reassembly progress instead: a wipe, in the same
- * part-file order `reassemble()` actually writes in, that fades a square once its bytes are
- * safely on disk and pulses whichever one is being appended right now. Without this the grid
- * would freeze solid the moment the last byte downloads, and assembling a large file can take
- * long enough that a frozen grid reads as hung rather than finishing up. */
- assembling?: boolean
- assembledBytes?: number
}
export function BlockGrid({
@@ -166,9 +152,7 @@ export function BlockGrid({
visuals,
knownSize,
remainingBytes,
- isPaused = false,
- assembling = false,
- assembledBytes = 0
+ isPaused = false
}: BlockGridProps): React.JSX.Element {
const [gridWidth, setGridWidth] = useState(0)
const [hoveredIndex, setHoveredIndex] = useState<number | null>(null)
@@ -214,10 +198,7 @@ export function BlockGrid({
// appears on an active block — and a tooltip advertises nothing to hover in the first place.
const hoveredCell = hoveredIndex !== null ? cells[hoveredIndex] : undefined
let readout: string
- if (assembling) {
- const totalBytes = cells.reduce((sum, cell) => sum + cell.totalBytes, 0)
- readout = `Assembling into file · ${formatBytes(assembledBytes)} / ${formatBytes(totalBytes)}`
- } else if (hoveredCell) {
+ if (hoveredCell) {
const where =
describeContributors(hoveredCell, visualByInterfaceId) ??
(hoveredCell.status === 'pending' ? 'queued' : '—')
@@ -226,18 +207,6 @@ export function BlockGrid({
readout = `${blocks.length} chunks · ${formatBytes(chunkBytes)} each`
}
- // Cumulative byte offset per cell, in the exact order reassemble() appends part files —
- // computed once here rather than per-cell so each square's assembly state is a simple
- // range comparison against `assembledBytes` below.
- const assembleOffsets = cells.reduce<{ offsets: number[]; total: number }>(
- (acc, cell) => {
- acc.offsets.push(acc.total)
- acc.total += cell.totalBytes
- return acc
- },
- { offsets: [], total: 0 }
- ).offsets
-
return (
<div className="flex flex-col gap-[9px] rounded-[9px] border-[0.5px] border-border bg-card px-[14px] pt-[10px] pb-[11px]">
<div className="flex flex-wrap items-center gap-[14px]">
@@ -256,12 +225,6 @@ export function BlockGrid({
</div>
)
})}
- {assembling && (
- <div className="flex items-center gap-[5.5px] font-mono text-[10.5px] leading-none font-medium text-[var(--text-secondary)]">
- <span className="size-[7px] shrink-0 rounded-full bg-[var(--text)]" />
- <span className="font-semibold text-foreground">Assembled</span>
- </div>
- )}
{cells.length > 0 && chunkBytes > 0 && (
<div className="ml-auto font-mono text-[10px] leading-none font-medium tabular-nums text-muted-foreground">
{readout}
@@ -319,35 +282,6 @@ export function BlockGrid({
opacity = 0.92
}
- let animation: string | undefined
- if (assembling) {
- const start = assembleOffsets[index]
- const end = start + cell.totalBytes
- if (end <= assembledBytes) {
- // Already appended to the destination file — turns neutral rather than just
- // fading, so "assembled" is a distinct state you can read at a glance, not a
- // guess at how dim is dim enough.
- fillColor = ASSEMBLED_SOLID
- border = 'none'
- boxShadow = 'none'
- opacity = 0.85
- } else if (start < assembledBytes) {
- // The one part file being streamed onto disk right now — turning neutral too,
- // with a pulse so the "write head" position is obvious.
- fillColor = ASSEMBLED_SOLID
- border = `1px solid ${ASSEMBLED_SOLID}`
- boxShadow = `0 0 7px ${ASSEMBLED_SOLID}`
- opacity = 1
- animation = 'plexo-glow 0.9s ease-in-out infinite'
- } else {
- // Completed but not yet its turn to be appended — stays in its network's color
- // a little dimmed, to signal "waiting its turn" rather than "already assembled".
- border = 'none'
- boxShadow = 'none'
- opacity = 0.75
- }
- }
-
const rawFillPercent = Math.min(1, Math.max(0, cell.fillRatio)) * 100
// A square is only ~12px wide, so the first bytes of a chunk round to nothing —
// floor a started chunk to a visible sliver rather than 0 width.
@@ -365,7 +299,6 @@ export function BlockGrid({
border,
boxShadow,
opacity,
- animation,
outline: hoveredIndex === index ? '1.5px solid var(--text-secondary)' : 'none',
outlineOffset: 1,
overflow: 'hidden',
diff --git a/src/renderer/src/components/CombineDiagram.tsx b/src/renderer/src/components/CombineDiagram.tsx
index d5ef17e..de321c2 100644
--- a/src/renderer/src/components/CombineDiagram.tsx
+++ b/src/renderer/src/components/CombineDiagram.tsx
@@ -29,16 +29,11 @@ export interface CombineDiagramNetwork {
export function CombineDiagram({
networks,
muted = false,
- paused = false,
- assembling = false
+ paused = false
}: {
networks: CombineDiagramNetwork[]
muted?: boolean
paused?: boolean
- /** The part files are all complete and being stitched into the destination — the individual
- * network feeds have nothing left to send, so their dashed lines stop while the combined
- * stream pulses to show the reassembly step is still actively running rather than stalled. */
- assembling?: boolean
}): React.JSX.Element {
const height = Math.max(78, networks.length * ROW_HEIGHT + 10)
const midY = height / 2
@@ -70,12 +65,9 @@ export function CombineDiagram({
d={`M${CURVE_START_X},${y.toFixed(1)} C${CURVE_START_X + 36},${y.toFixed(1)} ${COMBINE_X - 26},${midY.toFixed(1)} ${COMBINE_X},${midY.toFixed(1)}`}
stroke={color}
strokeDasharray="6 8"
- // Assembling means the network feeds themselves are done — nothing left in flight
- // on these lines — so they go still like a paused download rather than pretending
- // to still be streaming.
- opacity={paused || assembling ? 0.55 : 1}
+ opacity={paused ? 0.55 : 1}
style={
- muted || paused || assembling
+ muted || paused
? undefined
: { animation: `plexo-dash ${1.1 + index * 0.2}s linear infinite` }
}
@@ -89,7 +81,6 @@ export function CombineDiagram({
}
strokeWidth={6.5}
opacity={paused ? 0.6 : 1}
- style={assembling ? { animation: 'plexo-glow 1s ease-in-out infinite' } : undefined}
/>
</g>
<polygon
@@ -156,7 +147,7 @@ export function CombineDiagram({
</g>
)
})}
- {!muted && !assembling && !paused && (
+ {!muted && !paused && (
<text
x={STREAM_END_X + 2}
y={midY - 11}
diff --git a/src/renderer/src/components/DevToolsPanel.tsx b/src/renderer/src/components/DevToolsPanel.tsx
index c94665d..55b0913 100644
--- a/src/renderer/src/components/DevToolsPanel.tsx
+++ b/src/renderer/src/components/DevToolsPanel.tsx
@@ -12,8 +12,6 @@ const KIND_OPTIONS: NetworkInterfaceKind[] = ['wifi', 'usb', 'ethernet', 'bridge
const PRESET_CONNECTIONS = [1, 2, 4, 8] as const
const MAX_SIM_NETWORKS = 4
const MBPS_TO_BYTES_PER_SEC = (1024 * 1024) / 8
-const MB_TO_BYTES_PER_SEC = 1024 * 1024
-const DEFAULT_ASSEMBLE_SPEED_MBPS = 6
interface SimNetworkDraft {
key: number
@@ -44,7 +42,7 @@ const draftBoxClass =
/**
* Dev-only panel that exercises the whole download pipeline against a file already on disk —
- * chunking across fake networks, the block grid, retries/errors, pause/resume, assembling,
+ * chunking across fake networks, the block grid, retries/errors and pause/resume,
* completion — without needing a real multi-network setup or a slow, flaky server to provoke
* the states that are otherwise hard to reproduce on demand. Only rendered when
* `useAppStore.isDev` is true (see App.tsx), so it never reaches a packaged build's UI.
@@ -59,8 +57,6 @@ export function DevToolsPanel(): React.JSX.Element | null {
const [destinationDir, setDestinationDir] = useState('')
const [networks, setNetworks] = useState<SimNetworkDraft[]>(DEFAULT_NETWORKS)
const [connectionsPerNetwork, setConnectionsPerNetwork] = useState(2)
- const [slowAssemble, setSlowAssemble] = useState(true)
- const [assembleSpeedMBps, setAssembleSpeedMBps] = useState(DEFAULT_ASSEMBLE_SPEED_MBPS)
const [starting, setStarting] = useState(false)
const [error, setError] = useState<string | null>(null)
@@ -119,10 +115,7 @@ export function DevToolsPanel(): React.JSX.Element | null {
destinationDir: effectiveDestinationDir,
networks: simulatedNetworks,
chunkCount: networks.length * connectionsPerNetwork,
- connectionsPerNetwork,
- assembleSpeedBytesPerSec: slowAssemble
- ? Math.max(1, Math.round(assembleSpeedMBps * MB_TO_BYTES_PER_SEC))
- : undefined
+ connectionsPerNetwork
})
setOpen(false)
} catch (err) {
@@ -160,7 +153,7 @@ export function DevToolsPanel(): React.JSX.Element | null {
</div>
<div className="font-sans text-[11.5px] leading-[1.4] text-muted-foreground">
Pick a file already on disk to “download” it through the real pipeline — chunking, the
- block grid, pause/resume, retries, assembling — against fake networks you control.
+ block grid, pause/resume and retries — against fake networks you control.
</div>
<div className="flex flex-col gap-[5px]">
@@ -328,36 +321,6 @@ export function DevToolsPanel(): React.JSX.Element | null {
</ToggleGroup>
</div>
- <div className={draftBoxClass}>
- <label className="flex cursor-pointer items-center gap-[7px] font-sans text-[11px] leading-[1.3] text-foreground">
- <input
- type="checkbox"
- checked={slowAssemble}
- onChange={(event) => setSlowAssemble(event.target.checked)}
- />
- Simulate the assembling step
- </label>
- <div className="font-sans text-[10.5px] leading-[1.4] text-muted-foreground">
- Reassembly normally finishes in a blink — this throttles it so the “assembling” screen
- (the block grid sweep, the pulsing combine line) stays on screen long enough to actually
- watch.
- </div>
- {slowAssemble && (
- <label className="flex items-center gap-[5px] font-mono text-[10.5px] leading-none text-muted-foreground">
- Assemble speed
- <input
- type="number"
- min={1}
- max={500}
- value={assembleSpeedMBps}
- onChange={(event) => setAssembleSpeedMBps(Number(event.target.value) || 1)}
- className={`${rowInputClass} w-14`}
- />
- MB/s
- </label>
- )}
- </div>
-
{error && (
<div className="font-sans text-[11.5px] leading-[1.4] text-destructive">⚠ {error}</div>
)}
diff --git a/src/renderer/src/components/TitleBar.tsx b/src/renderer/src/components/TitleBar.tsx
index 4a12eff..7d24391 100644
--- a/src/renderer/src/components/TitleBar.tsx
+++ b/src/renderer/src/components/TitleBar.tsx
@@ -5,7 +5,6 @@ import { UpdateIndicator } from './UpdateIndicator'
export type TitleBarStatus =
| { kind: 'none' }
| { kind: 'combined'; networkCount: number }
- | { kind: 'assembling' }
| { kind: 'paused'; networkCount: number }
| { kind: 'offline' }
@@ -48,19 +47,6 @@ export function TitleBar({ status }: { status: TitleBarStatus }): React.JSX.Elem
{status.networkCount} {status.networkCount === 1 ? 'network' : 'networks'} combined
</ColorBadge>
)}
- {status.kind === 'assembling' && (
- <ColorBadge
- bg="var(--color-ethernet-bg)"
- border="var(--color-ethernet-border)"
- text="var(--color-ethernet-text)"
- className={pillClass}
- >
- <div
- className={`${pillDotClass} bg-[var(--color-ethernet)] animate-[plexo-glow_1s_ease-in-out_infinite]`}
- />
- Assembling file…
- </ColorBadge>
- )}
{status.kind === 'paused' && (
<ColorBadge
bg="var(--color-usb-bg)"
diff --git a/src/renderer/src/screens/CompleteScreen.tsx b/src/renderer/src/screens/CompleteScreen.tsx
index e09762c..929015f 100644
--- a/src/renderer/src/screens/CompleteScreen.tsx
+++ b/src/renderer/src/screens/CompleteScreen.tsx
@@ -46,9 +46,7 @@ export function CompleteScreen({
)
const totalWeight = groups.reduce((sum, group) => sum + group.bytesDownloaded, 0) || 1
const totalRetries = download.chunks.reduce((sum, chunk) => sum + chunk.retryCount, 0)
- // What actually got stitched together at reassembly time is the block count, not the number of
- // parallel connections — "chunks" in this app's own vocabulary (see BlockGrid) means the byte
- // range unit, so this footer's number needs to match that, not `download.chunks.length`.
+ // "Chunks" in the block grid means byte ranges, not parallel connections.
const totalChunkCount = download.totalBlocks ?? download.blocks?.length ?? 1
const handleReveal = (): void => void window.plexo.revealInFolder(download.destinationPath)
@@ -161,7 +159,7 @@ export function CompleteScreen({
<ScreenFooter>
<div className="shrink-0 font-mono text-[11px] leading-[1.4] whitespace-nowrap text-muted-foreground">
- {`reassembled from ${totalChunkCount} chunks · ${totalRetries} ${
+ {`written in ${totalChunkCount} chunks · ${totalRetries} ${
totalRetries === 1 ? 'retry' : 'retries'
}`}
</div>
diff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsx
index a39143e..b28b2d5 100644
--- a/src/renderer/src/screens/DownloadingScreen.tsx
+++ b/src/renderer/src/screens/DownloadingScreen.tsx
@@ -52,8 +52,7 @@ function InlineStat({ label, value }: { label: string; value: string }): React.J
)
}
-/** The hero band's headline figure: a tracked-out label over one big tabular number and its unit.
- * Both states of the band (total speed, assembly progress) are this same shape. */
+/** The hero band's headline figure: a tracked-out label over one big tabular number and its unit. */
function BigStat({
label,
value,
@@ -86,25 +85,6 @@ function BigStat({
)
}
-/** Says why a footer action is unavailable, and only while it is — a tooltip on a button you can
- * actually press has nothing to explain. */
-function WhileAssembling({
- active,
- text,
- children
-}: {
- active: boolean
- text: string
- children: React.ReactElement
-}): React.JSX.Element {
- return (
- <Tooltip open={active ? undefined : false}>
- <TooltipTrigger render={children} />
- <TooltipContent>{text}</TooltipContent>
- </Tooltip>
- )
-}
-
export function DownloadingScreen({ download }: { download: DownloadState }): React.JSX.Element {
useNetworkPolling(true)
@@ -114,10 +94,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
const peakSpeedBytesPerSec = useAppStore((store) => store.peakSpeedBytesPerSec)
const networkVisual = useNetworkVisuals()
const isPaused = download.status === 'paused'
- const isAssembling = download.status === 'assembling'
const percent = formatPercent(download.bytesDownloaded, download.totalBytes)
- const assembledBytes = download.assembledBytes ?? 0
- const assemblePercent = formatPercent(assembledBytes, download.totalBytes)
const knownSize = download.totalBytes > 0
const [now, setNow] = useState(() => Date.now())
@@ -139,9 +116,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
}, [isPaused, download.error])
useEffect(() => {
- if (isAssembling) {
- document.title = `Plexo — Assembling (${assemblePercent}%)`
- } else if (isPaused) {
+ if (isPaused) {
document.title = knownSize ? `Plexo — Paused (${percent}%)` : 'Plexo — Paused'
} else {
document.title = knownSize ? `Plexo — ${percent}%` : 'Plexo — downloading'
@@ -149,7 +124,7 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
return () => {
document.title = 'Plexo'
}
- }, [percent, assemblePercent, knownSize, isPaused, isAssembling])
+ }, [percent, knownSize, isPaused])
const totalPausedMs =
(download.totalPausedMs || 0) +
@@ -209,17 +184,9 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
const activeChipOption =
chipOptions.length > 0 ? chipOptions[chipModeIndex % chipOptions.length] : null
- const statusBadge = isAssembling
- ? { label: 'ASSEMBLING', palette: KIND_PALETTE.ethernet }
- : isPaused
- ? { label: 'PAUSED', palette: KIND_PALETTE.usb }
- : null
+ const statusBadge = isPaused ? { label: 'PAUSED', palette: KIND_PALETTE.usb } : null
- const throughputStatusLabel = isAssembling
- ? 'ASSEMBLING'
- : isPaused
- ? null
- : `LAST ${speedHistory.length}S`
+ const throughputStatusLabel = isPaused ? null : `LAST ${speedHistory.length}S`
const pauseResumeLabel = resuming ? 'Resuming…' : isPaused ? 'Resume' : 'Pause'
return (
@@ -231,60 +198,50 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
networks={groups.map((group, index) => ({
solid: visuals[index].solid,
label: visuals[index].name,
- speedBytesPerSec: isPaused || isAssembling ? 0 : group.speedBytesPerSec
+ speedBytesPerSec: isPaused ? 0 : group.speedBytesPerSec
}))}
paused={isPaused}
- assembling={isAssembling}
/>
<div className="flex min-w-[130px] shrink-0 flex-col gap-[7px]">
- {isAssembling ? (
+ <>
<BigStat
- label="ASSEMBLING"
- value={assemblePercent}
- unit="%"
- valueClass="text-[var(--color-ethernet)]"
+ label="TOTAL SPEED"
+ value={isPaused ? '—' : speed.value}
+ unit={isPaused ? undefined : `${speed.unit}/s`}
+ valueClass={isPaused ? 'text-muted-foreground' : 'text-foreground'}
/>
- ) : (
- <>
- <BigStat
- label="TOTAL SPEED"
- value={isPaused ? '—' : speed.value}
- unit={isPaused ? undefined : `${speed.unit}/s`}
- valueClass={isPaused ? 'text-muted-foreground' : 'text-foreground'}
- />
- <div className="flex items-center gap-2 font-mono text-[10px] leading-none font-medium tabular-nums text-muted-foreground">
- <InlineStat label="AVG" value={formatSpeed(avgSpeedBytesPerSec)} />
- <Dot />
- <InlineStat label="PEAK" value={formatSpeed(peakSpeedBytesPerSec)} />
- </div>
- {isPaused
- ? download.error && (
- <div
- role="alert"
- className="mt-0.5 font-sans text-[11px] leading-[1.2] font-medium text-destructive"
- >
- {download.error}
- </div>
- )
- : activeChipOption && (
- <CyclableChip
- label={activeChipOption.label}
- tooltip={`${activeChipOption.tooltip}${chipOptions.length > 1 ? ' (click to toggle)' : ''}`}
- bg={activeChipOption.visual.bg}
- border={activeChipOption.visual.border}
- color={activeChipOption.visual.text}
- cyclable={chipOptions.length > 1}
- onClick={() => setChipModeIndex((i) => (i + 1) % chipOptions.length)}
- />
- )}
- </>
- )}
+ <div className="flex items-center gap-2 font-mono text-[10px] leading-none font-medium tabular-nums text-muted-foreground">
+ <InlineStat label="AVG" value={formatSpeed(avgSpeedBytesPerSec)} />
+ <Dot />
+ <InlineStat label="PEAK" value={formatSpeed(peakSpeedBytesPerSec)} />
+ </div>
+ {isPaused
+ ? download.error && (
+ <div
+ role="alert"
+ className="mt-0.5 font-sans text-[11px] leading-[1.2] font-medium text-destructive"
+ >
+ {download.error}
+ </div>
+ )
+ : activeChipOption && (
+ <CyclableChip
+ label={activeChipOption.label}
+ tooltip={`${activeChipOption.tooltip}${chipOptions.length > 1 ? ' (click to toggle)' : ''}`}
+ bg={activeChipOption.visual.bg}
+ border={activeChipOption.visual.border}
+ color={activeChipOption.visual.text}
+ cyclable={chipOptions.length > 1}
+ onClick={() => setChipModeIndex((i) => (i + 1) % chipOptions.length)}
+ />
+ )}
+ </>
</div>
<div
className={`min-w-0 flex-1 transition-opacity duration-200 ${
- isPaused || isAssembling ? 'opacity-45' : 'opacity-100'
+ isPaused ? 'opacity-45' : 'opacity-100'
}`}
>
<div className="font-mono text-[9.5px] leading-none font-medium tracking-[0.12em] text-muted-foreground">
@@ -314,18 +271,16 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
/>
<div className="mt-1 flex items-center gap-[7px] font-mono text-[12.5px] leading-[1.2] tabular-nums text-[var(--text-secondary)]">
<span>
- {formatBytes(isAssembling ? assembledBytes : download.bytesDownloaded)}
+ {formatBytes(download.bytesDownloaded)}
{knownSize ? ` of ${formatBytes(download.totalBytes)}` : ''}
</span>
{knownSize && (
<>
<Dot />
- <span className="font-semibold text-foreground">
- {isAssembling ? assemblePercent : percent}%
- </span>
+ <span className="font-semibold text-foreground">{percent}%</span>
</>
)}
- {!isPaused && !isAssembling && knownSize && effectiveSpeed > 0 && (
+ {!isPaused && knownSize && effectiveSpeed > 0 && (
<>
<Dot />
<span className="text-[var(--text-secondary)]">
@@ -361,8 +316,6 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
knownSize={knownSize}
remainingBytes={remainingBytes}
isPaused={isPaused}
- assembling={isAssembling}
- assembledBytes={assembledBytes}
/>
<div className="mt-3">
@@ -431,34 +384,22 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
</>
)}
</div>
- <WhileAssembling active={isAssembling} text="Can't pause while assembling the file">
- <Button
- type="button"
- variant={isPaused ? 'default' : 'secondary'}
- onClick={handlePauseResume}
- disabled={isAssembling || resuming}
- // Disabled natively means unhoverable/unfocusable, which would silence this button's
- // own explanatory tooltip exactly when it's needed — keep it reachable instead.
- focusableWhenDisabled
- >
- {pauseResumeLabel}
- </Button>
- </WhileAssembling>
+ <Button
+ type="button"
+ variant={isPaused ? 'default' : 'secondary'}
+ onClick={handlePauseResume}
+ disabled={resuming}
+ >
+ {pauseResumeLabel}
+ </Button>
<AlertDialog>
- <WhileAssembling active={isAssembling} text="Can't cancel while assembling the file">
- <AlertDialogTrigger
- render={
- <Button
- type="button"
- variant="destructive"
- disabled={isAssembling}
- focusableWhenDisabled
- >
- Cancel
- </Button>
- }
- />
- </WhileAssembling>
+ <AlertDialogTrigger
+ render={
+ <Button type="button" variant="destructive">
+ Cancel
+ </Button>
+ }
+ />
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Cancel this download?</AlertDialogTitle>
diff --git a/src/renderer/src/utils/format.ts b/src/renderer/src/utils/format.ts
index 7b391f8..d86a8e6 100644
--- a/src/renderer/src/utils/format.ts
+++ b/src/renderer/src/utils/format.ts
@@ -131,19 +131,12 @@ const NESTED_ERROR_PREFIX = /^Error:\s*/
const ERROR_HINTS: Array<{ pattern: RegExp; message: string }> = [
{
- pattern: /parts never finished/,
- message:
- 'The download never fully finished, so Plexo couldn’t assemble it. Try downloading again.'
+ pattern: /Download is incomplete/,
+ message: 'The download did not finish every range. Try downloading again.'
},
{
- pattern: /refusing to write a corrupt file/,
- message:
- 'One of the downloaded pieces didn’t match its expected size, so Plexo stopped rather than save a corrupted file. Try downloading again.'
- },
- {
- pattern: /refusing to keep a corrupt file/,
- message:
- 'The assembled file didn’t match its expected size, so Plexo removed it rather than keep a corrupted file. Try downloading again.'
+ pattern: /Download file size does not match/,
+ message: 'The downloaded file did not match its expected size, so Plexo did not publish it.'
},
{
pattern: /ENOTFOUND|EAI_AGAIN/,
diff --git a/src/shared/types.ts b/src/shared/types.ts
index f89f96d..ab73bd7 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -35,8 +35,7 @@ export interface ProbeResult {
lastModified: string | null
}
-export type DownloadStatus =
- 'downloading' | 'assembling' | 'paused' | 'completed' | 'error' | 'cancelled'
+export type DownloadStatus = 'downloading' | 'paused' | 'completed' | 'error' | 'cancelled'
/** A stream's state. `pending` means it is waiting for work: it holds no block, either because
* none is free for it right now or because it hasn't started. `downloading` always means it is
@@ -103,10 +102,6 @@ export interface DownloadState {
pausedAt?: number
totalPausedMs?: number
completedAt?: number
- /** Bytes written to the destination file so far while `status` is 'assembling' — the part
- * files are already all complete at that point, so this tracks the sequential reassembly step
- * rather than the network transfer. */
- assembledBytes?: number
}
/** User customization for one physical network, keyed by NetworkInterfaceInfo.id — lets a
@@ -123,7 +118,7 @@ export type NetworkPreferences = Record<string, NetworkPreference>
/** One fake network in a dev-tool "virtual download" — see SimulatedNetworkConfig callers in
* main/download/simDownload.ts. Lets a developer exercise the multi-network UI (the block grid,
- * per-network speed/throughput, retries, errors, assembling) against a file already on disk,
+ * per-network speed/throughput, retries and errors) against a file already on disk,
* without needing a real flaky connection or a slow remote server to test against. */
export interface SimulatedNetworkConfig {
kind: NetworkInterfaceKind
@@ -142,11 +137,6 @@ export interface StartSimulatedDownloadRequest {
networks: SimulatedNetworkConfig[]
chunkCount: number
connectionsPerNetwork?: number
- /** Throttles the reassembly step to this many bytes/sec, so the 'assembling' phase's UI (the
- * block grid sweep, the combine diagram) stays visible long enough to watch even on a small
- * file that would otherwise reassemble in a single tick. Omitted or 0 assembles at full disk
- * speed, same as a real download. */
- assembleSpeedBytesPerSec?: number
}
export interface UpdateInfo {