Skip to content

feat: stream update downloads to disk, resume them, and verify a declared digest - #327

Merged
codebytere merged 3 commits into
mainfrom
stream-and-resume-downloads
Aug 11, 2026
Merged

codebytere merged 3 commits into
mainfrom
stream-and-resume-downloads

Conversation

@codebytere

Copy link
Copy Markdown
Collaborator

The update ZIP is fetched with a single NSURLConnection sendAsynchronousRequest: and held as one NSData before it is written out, so a large update is an equally large allocation in the host app, and any interruption (network change, sleep, quit) discards the whole transfer until the next check starts it again from zero. NSURLConnection has also been deprecated since 10.11.

This adds SQRLDownloader, a small wrapper around an NSURLSession download task, and moves -downloadBundleForUpdate:intoDirectory: onto it:

  • the payload streams to a file in the update directory; nothing is buffered in memory
  • when a transfer fails or is cancelled and the response carried a validator, the session's resume data is kept beside the update.XXXXXXX directories (download.resumedata), and the next check continues from that offset with a Range request instead of starting over; this holds after a relaunch too, since downloads still in flight at NSApplicationWillTerminateNotification hand over their resume data first
  • resume data for a different URL, or that the session no longer accepts, is dropped and the download starts fresh, so the worst case is today's behaviour
  • progress is published as SQRLDownloadProgress values on the new SQRLUpdater.downloadProgress
  • SQRLDownloader.sessionConfiguration lets a host set network policy or protocol classes for the download sessions

Separately, an update may now declare sha256 and size for its ZIP (SQRLUpdate.packageDigest / packageSize, both optional). A download that does not match is deleted before ditto runs and the check errors with SQRLUpdaterErrorInvalidUpdatePackage. Signature verification and installation are untouched.

Tests: SQRLDownloaderSpec runs against a small local HTTP server (SquirrelTests/TestServer.py, ranges + ETag, can cut a transfer short) and covers a full download with progress, resuming an interrupted transfer (asserts the Range request and a byte-identical result), keeping resume data on disposal and on +cancelAllWritingResumeDataWithTimeout:, and discarding resume data for another URL. SQRLUpdaterSpec gains the digest/size rejection case and SQRLUpdateSpec the parsing; the existing out-of-process update specs exercise the new path end to end over file://. 94 tests pass locally on macOS 26.5 / Xcode 26.5

…ared digest

The update ZIP was fetched with a single NSURLConnection request and held
in memory as one NSData before being written out, so a large update was
an equally large allocation in the host app, and any interruption
(network change, sleep, quit) threw the whole transfer away until the
next check started it from zero.

SQRLDownloader wraps an NSURLSession download task: the payload streams
to a file, progress is published as it arrives, and when a transfer
fails or is cancelled the session's resume data is kept beside
Squirrel's update directories. The next check, in this process or after
a relaunch, continues from that offset when the server answered with a
validator and honours Range, and starts over otherwise. Downloads still
in flight when the application terminates hand over their resume data
first. SQRLUpdater uses it for the update ZIP and exposes the progress
as `downloadProgress`.

Feeds may now declare `sha256` and `size` for the ZIP; a download that
does not match is discarded before ditto ever sees it and the check
fails with SQRLUpdaterErrorInvalidUpdatePackage. Verification and
installation are otherwise unchanged.

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through this pretty carefully and had most of the below repro'd against a local server rather than just reading it. The streaming side looks solid and nothing here lets a bad package actually get installed, the comments are mostly the resume state machine having a few ways to get stuck or do the wrong thing across checks/relaunches, plus the new feed fields being stricter than they look. The .. url one, resume-fails-again, the plist not being consumed and the etag ordering are the ones I think we should sort out in this PR, the rest are docs/tests.

Comment thread Squirrel/SQRLDownloader.m Resolved
Comment thread Squirrel/SQRLDownloader.m Resolved
Comment thread Squirrel/SQRLDownloader.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/SQRLUpdater.m Resolved
Comment thread Squirrel/Squirrel.h Resolved
Comment thread SquirrelTests/SQRLUpdaterSpec.m Resolved
Comment thread SquirrelTests/SQRLDownloaderSpec.m Resolved
Resume data is taken off disk while an attempt owns the partial file; a
resumed request that gets nowhere (refused, reset, undecodable, or
answered 403/416/304) is forgotten and retried as a plain GET once; a
nil resumed task falls into the fresh path. The archive is written to a
fixed name rather than one derived from the URL, the ETag is dropped
when the download does not become a prepared update, and hashing reads
through NSInputStream so a failing disk is an error, not an exception.
`sha256`/`size` of the wrong type or shape are logged and ignored
instead of failing the model. Specs cover the refused/reset resume, the
no-plist-while-resuming invariant, malformed feed values, and a
prepared update with a matching digest and size; README notes the
protocol-class and quit-path caveats.

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-swept 898109d, everything from the first pass is sorted (re-ran the gzip / reset servers against the new nowhere check and neither gets a didWriteData before failing so both go fresh now, and the suite is green here). Two things on the new resume logic.

Comment thread Squirrel/SQRLDownloader.m Resolved
Comment thread Squirrel/SQRLDownloader.m Resolved
…anch

A resumed attempt that goes nowhere is retried fresh; if that fresh
attempt also receives nothing, the network rather than the ranged
request is the problem, so the resume data that was taken is put back
and the partial outlives the outage. The went-nowhere path can only be
reached from a resumed attempt, so its second-failure branch (and the
retriedFresh flag) is removed.
@codebytere
codebytere merged commit c4a95b0 into main Aug 11, 2026
6 checks passed
@codebytere
codebytere deleted the stream-and-resume-downloads branch August 11, 2026 21:18
codebytere added a commit that referenced this pull request Aug 14, 2026
…ffers one (#328)

Stacked on #327 (base branch `stream-and-resume-downloads`); the diff
here is only the delta work.

An update may now carry `delta`: a
[Sparkle](https://sparkle-project.org) BinaryDelta patch (format 3 or 4)
from one earlier `CFBundleVersion` to this release, with its URL, size
and SHA-256:

```json
"delta": { "from_version": "412", "url": "https://…/412-to-413.delta", "sha256": "…", "size": 7340032 }
```

When `from_version` is the running application's, Squirrel downloads the
patch instead of the ZIP (same streaming, resumable `SQRLDownloader`),
checks size and digest before opening it, applies it to a copy of the
running bundle inside the update directory, and sends the result through
the existing code signing and `ElectronSquirrelPreventDowngrades`
checks. If the delta is for another version, or downloading, verifying,
applying or checking it fails in any way, the full ZIP is downloaded in
the same check, so offering a delta can never cost an update. All four
keys are required; ShipIt and installation are untouched.

The apply side is Sparkle's own, vendored unmodified under
`Vendor/SparkleDelta` (MIT; format 3/4 reader and applier plus
`bspatch`, ~2.9k lines, pinned to sparkle-project/Sparkle@cfa1190) with
a small force-included config header standing in for Sparkle's xcconfig
defines; the legacy XAR formats and the create side are left out, so
patches are made with Sparkle's `BinaryDelta create old.app new.app
out.delta` from the exact signed bundles that shipped. That keeps one
implementation of the format rather than a Squirrel-flavoured second
one. `objc_direct` is turned off for the vendored classes so the tests
can link Sparkle's generator against them.

Sizing, for a sense of why: between two consecutive releases of a large
Electron app (~230 MB zipped per arch, same Electron version) the
format-4 LZMA delta is 9.5 MB, applies in a few seconds, and the result
is byte-identical to the shipped bundle and passes `codesign --verify
--deep --strict` and Gatekeeper.

Tests: `SQRLUpdaterSpec` builds real patches between the two
TestApplication fixtures with Sparkle's generator (vendored into the
test target only) and drives the out-of-process install twice, once from
the delta alone (ZIP never requested) and once with a corrupted delta
that must fall back to the ZIP, asserting on the fixture server's
request log which files were fetched; `SQRLUpdateSpec` covers parsing
and rejects a delta missing any key. 97 tests pass on macOS 26.5 / Xcode
26.5. README documents the key in both feed formats
codebytere added a commit to electron/electron that referenced this pull request Aug 14, 2026
…nloads

Rolls squirrel.mac to fffea30 (Squirrel/Squirrel.Mac#327, #328, #330):

* the update ZIP streams to disk through an NSURLSession download task
  instead of arriving as one in-memory NSData, an interrupted transfer
  resumes on the next check (and after a relaunch) when the server
  provides a validator and honours Range, and a feed may declare
  `sha256`/`size` for the ZIP, checked before it is unpacked;
* a feed may offer `delta`, a Sparkle BinaryDelta patch from one earlier
  build, applied to a copy of the running app and verified like an
  unpacked ZIP, with the ZIP as fallback in the same check.

Squirrel compiles Sparkle's BinaryDelta apply sources and the bsdiff it
vendors into the framework, so Sparkle 2.9.5 becomes a DEPS entry at
vendor/Sparkle (gclient does not initialise Squirrel's submodule) with a
licenses.py entry, and the GN config gains those sources with the flags
Squirrel's Xcode project gives them, the new Squirrel sources, and
-lbz2 -lcompression -lz. The autoUpdater docs describe the behaviour and
the optional feed keys.
jkleinsc pushed a commit to electron/electron that referenced this pull request Aug 17, 2026
…loads (#52820)

* chore: roll Squirrel.Mac for streamed, resumable and delta update downloads

Rolls squirrel.mac to fffea30 (Squirrel/Squirrel.Mac#327, #328, #330):

* the update ZIP streams to disk through an NSURLSession download task
  instead of arriving as one in-memory NSData, an interrupted transfer
  resumes on the next check (and after a relaunch) when the server
  provides a validator and honours Range, and a feed may declare
  `sha256`/`size` for the ZIP, checked before it is unpacked;
* a feed may offer `delta`, a Sparkle BinaryDelta patch from one earlier
  build, applied to a copy of the running app and verified like an
  unpacked ZIP, with the ZIP as fallback in the same check.

Squirrel compiles Sparkle's BinaryDelta apply sources and the bsdiff it
vendors into the framework, so Sparkle 2.9.5 becomes a DEPS entry at
vendor/Sparkle (gclient does not initialise Squirrel's submodule) with a
licenses.py entry, and the GN config gains those sources with the flags
Squirrel's Xcode project gives them, the new Squirrel sources, and
-lbz2 -lcompression -lz. The autoUpdater docs describe the behaviour and
the optional feed keys.

* fixup! visibility of sparkle_binary_delta

mac_framework_bundle() expands squirrel_framework into inner targets
(squirrel_framework_shared_library links the sources), so limit the
source_set to this file rather than to the template's outer name.
jkleinsc pushed a commit to electron/electron that referenced this pull request Oct 7, 2026
…loads (#54204)

feat: roll Squirrel.Mac for streamed, resumable and delta update downloads (#52820)

* chore: roll Squirrel.Mac for streamed, resumable and delta update downloads

Rolls squirrel.mac to fffea30 (Squirrel/Squirrel.Mac#327, #328, #330):

* the update ZIP streams to disk through an NSURLSession download task
  instead of arriving as one in-memory NSData, an interrupted transfer
  resumes on the next check (and after a relaunch) when the server
  provides a validator and honours Range, and a feed may declare
  `sha256`/`size` for the ZIP, checked before it is unpacked;
* a feed may offer `delta`, a Sparkle BinaryDelta patch from one earlier
  build, applied to a copy of the running app and verified like an
  unpacked ZIP, with the ZIP as fallback in the same check.

Squirrel compiles Sparkle's BinaryDelta apply sources and the bsdiff it
vendors into the framework, so Sparkle 2.9.5 becomes a DEPS entry at
vendor/Sparkle (gclient does not initialise Squirrel's submodule) with a
licenses.py entry, and the GN config gains those sources with the flags
Squirrel's Xcode project gives them, the new Squirrel sources, and
-lbz2 -lcompression -lz. The autoUpdater docs describe the behaviour and
the optional feed keys.

* fixup! visibility of sparkle_binary_delta

mac_framework_bundle() expands squirrel_framework into inner targets
(squirrel_framework_shared_library links the sources), so limit the
source_set to this file rather than to the template's outer name.

(cherry picked from commit fd93bf4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants