← All notes

· 4 min read · Haul, Flutter, Dart, Architecture

Building Haul's cross-platform download engine

How I built a segmented download engine in Dart, saved progress to SQLite so downloads can resume, and kept platform code out of the Flutter UI.

Haul is a desktop download manager built with Flutter. I started it because browser downloads felt unreliable for large files: one network interruption could ruin a download that was most of the way done, and I'd have to start again. I wanted downloads that survive interruptions, an interface that feels at home on macOS, and to understand how download managers work by building the engine myself.

Context

There are other download managers for macOS, but I didn't like their interfaces. I wanted something that looked and felt like a native Mac app.

It was also a chance to work on something I hadn't built before: long-running network operations, splitting downloads into segments, handling failures, and keeping progress across interruptions.

I chose Flutter for the interface, but I didn't want the download engine to depend on the UI or be tied to macOS. If the engine stayed independent, bringing Haul to other desktop platforms later would be much easier.

How it works

Haul has three main parts: the Flutter interface, the download engine and the platform services. Browser extensions for Chrome and Firefox are a second way to start a download.

  1. 1.Browser handoff

    The extension intercepts a download and captures its URL

  2. 2.Local API

    It sends the URL to Haul's local HTTP server

  3. 3.Setup

    The engine splits the file into eight segments

  4. 4.Parallel download

    Segments download at the same time

  5. 5.Progress saved

    Drift records each segment's progress in SQLite

  6. 6.Retries

    A failed segment retries on its own, with backoff

  7. 7.Complete

    Finished segments make up the final file

A download handed over from the browser, through Haul's Dart engine, with progress saved as it goes.

The engine divides a file into eight segments and downloads them concurrently. When a segment finishes, its share of the connection capacity goes to the segments still running.

Progress is saved to SQLite through Drift. Because the engine knows exactly how many bytes of each segment it already has, a paused or interrupted download picks up where it stopped instead of starting over.

Each segment handles its own retries. A failed segment backs off exponentially and retries up to eight times, and if a retry makes progress, its failure count resets to zero. If a segment runs out of retries, it signals the other segments to stop.

The browser extensions talk to a local HTTP server that the desktop app runs. When an extension intercepts a download, it sends the link to Haul, and Haul takes over from there.

Options

The main choice was whether to use an existing download library or write the engine myself.

A library would have saved me a lot of infrastructure work. But segmentation, retries and resume are the core of what Haul does, and I didn't want that behaviour to be someone else's code that I couldn't shape. Writing it myself gave me control over how those pieces work together, and it was the part I most wanted to learn.

The second choice was how to keep the engine separate from the Flutter interface, so neither would have to change when the other did.

Decision

I wrote the engine in plain Dart with no download library. The Flutter UI shows downloads and sends commands, but it doesn't own any of the download logic.

The engine reaches the operating system through a PlatformServices interface instead of checking which platform it's on or calling platform-specific dart:io behaviour directly. Right now MacOsPlatformService is the only implementation. Adding Windows or Linux means writing one new class, not redesigning the engine around each operating system.

Trade-offs

Writing the engine myself means I own all of its complexity. Segmentation, retries, persistence and resume all have to work together, and every bug in them is mine to find.

One problem I haven't solved yet is download links that change. Some links expire or stop working partway through, and Haul doesn't handle that yet.

The platform layer is also groundwork, not proof of cross-platform support. macOS is the only platform that works today, and Windows and Linux each still need their own implementation.

Looking back

I'd keep the overall structure. Keeping the engine, UI and platform services separate means the download logic doesn't depend on any one operating system. Next I want to handle expiring links, then explore torrent downloads and a mobile companion app that can send links to Haul on your computer when you're away from it.

A working release build is available on GitHub, and I'm starting to share it with people and gather feedback.