Commit 51bb14d
authored
feat(storage): service worker streaming for zip download (#7044)
* feat(storage): service worker streaming zip download
Replace the in-memory blob-based zip download with a service worker
streaming architecture for StorageBrowser multi-file downloads.
Falls back to blob collection when the SW is unavailable.
Key changes:
- Add download-sw.ts service worker with skipWaiting + clients.claim
- Add zipdownload handler with WeakMap-scoped batch state (no singleton)
- Stream files sequentially into zip via zip.js (concurrency=1)
- MessageChannel handshake for SW stream transfer + keepalive pings
- Blob fallback with inline cleanup (no fire-and-forget race)
- composedDownloadHandler routes single-file vs multi-file downloads
- useServiceWorkerRegistration hook for automatic SW registration
- copy-serviceworker bin script for npm consumers
- Documentation for SW setup across frameworks
- Comprehensive test suite with deterministic async flushing
* fix(storage): verify message origin in download SW, raise size limit
- Add same-origin verification to the service worker message handler,
addressing CodeQL finding 'Missing origin verification in postMessage
handler'. Cross-origin messages are now rejected before any stream is
stored or acknowledged.
- Add a regression test asserting foreign-origin messages are ignored;
update existing message mocks to carry the same-origin value.
- Raise createStorageBrowser size-limit 130 kB -> 131 kB to accommodate
the SW streaming code (was over by 21 B).
- Add changeset for the feature.
* docs(storage): note download-only progress in changeset
Address review feedback: document that per-file progress is download-only
by design (reflects S3 read bytes, not zip finalization), since level:0
storage makes the finalization phase near-instant.
* fix(storage): address review feedback on SW streaming download
- download-sw.ts: decode the request pathname before the pendingStreams
lookup so folder names with spaces / URL-unsafe characters resolve
(blocker: browser percent-encodes the id in the <a> navigation while the
stream is keyed by the unencoded id).
- download-sw.ts: use RFC 5987 'filename*=UTF-8''...' Content-Disposition so
quotes/backslashes/UTF-8 in S3 keys are handled without escaping issues.
- zipdownload.ts: guard the getRegistration().then() callback with a
cancelled check so a keepalive interval is never created after the batch
was reset (fixes an interval leak when cancel races swReady).
- zipdownload.ts: drop fragile substring matching on zip.js internal error
messages; flag cancellation from batchAbort.signal.aborted or the standard
AbortError name only.
- useServiceWorkerRegistration.ts: warn in development when SW registration
fails (surfaces the common 'forgot copy-serviceworker' setup mistake).
- copy-serviceworker.js: drop redundant existsSync before recursive mkdirSync
(also closes a TOCTOU window).
- tests: SW round-trip test for unencoded-store/encoded-request + RFC 5987
assertions; genuine early-exit cancelled-path test; space-in-folder test.
- Revert an unintended workflow change (reusable-build-system-test-react-native.yml)
that a prior rebase resolution pulled in; file now matches main.
* fix(storage): stop cancel resurrection + leaked timestamp in SW filename
- zipdownload.ts: cancelBatch() no longer calls reset() synchronously. The
cancelled flag and the batchMap entry share one key, so deleting the entry
let the next queued file (concurrency: 1) miss the cancelled early-exit and
build a fresh batch — resurrecting the download (files 2..N still zipped).
The entry is now kept (tombstoned, keepalive stopped) until batchDone ===
batchTotal, so remaining files drain through the early-exit. reset() remains
the sole map-deleter, for both the cancel and completion paths.
- download-sw.ts / zipdownload.ts: the SW filename is now sent explicitly
(`${folder}.zip`) in the postMessage and stored alongside the stream. The
Content-Disposition header uses it instead of deriving from downloadId, which
embeds Date.now() for map-key uniqueness. Previously the same-origin
Content-Disposition (which wins over the anchor download attr) saved
e.g. photos-1720080000000.zip on the SW path while the blob fallback saved
photos.zip — now both agree and no timestamp leaks.
- useServiceWorkerRegistration.ts: drop process.env.NODE_ENV guard (TS2591
'Cannot find name process' in the browser rollup build — no node types) and
warn unconditionally; a SW registration failure is a real degradation.
- tests: SW explicit-filename precedence; UI-cancel no-resurrection guard
(exactly one ZipWriter constructed across a cancelled 2-file batch).1 parent 8a06872 commit 51bb14d
18 files changed
Lines changed: 1286 additions & 229 deletions
File tree
- .changeset
- docs/src/pages/[platform]/connected-components/storage/storage-browser
- packages/react-storage
- bin
- src/components/StorageBrowser
- actions/handlers
- __tests__
- createStorageBrowser
- service-worker
- __tests__
- useAction
- views/LocationActionView/DownloadView
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
Lines changed: 73 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
283 | 283 | | |
284 | 284 | | |
285 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
286 | 359 | | |
287 | 360 | | |
288 | 361 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| |||
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
| 34 | + | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
| |||
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
46 | | - | |
| 50 | + | |
47 | 51 | | |
48 | 52 | | |
49 | 53 | | |
| |||
69 | 73 | | |
70 | 74 | | |
71 | 75 | | |
72 | | - | |
| 76 | + | |
73 | 77 | | |
74 | 78 | | |
75 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
65 | 80 | | |
66 | 81 | | |
67 | 82 | | |
0 commit comments