@aws-amplify/ui-react-storage 3.18.3 → 3.18.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser.js +1 -1
- package/dist/{createStorageBrowser-CIKB1K1X.js → createStorageBrowser-DC1i0FGj.js} +94 -11
- package/dist/download-sw.js +99 -3
- package/dist/esm/components/StorageBrowser/actions/handlers/zipdownload.mjs +90 -9
- package/dist/esm/components/StorageBrowser/service-worker/constants.mjs +6 -0
- package/dist/esm/components/StorageBrowser/service-worker/useServiceWorkerRegistration.mjs +2 -3
- package/dist/esm/version.mjs +1 -1
- package/dist/index.js +1 -1
- package/dist/types/components/StorageBrowser/actions/handlers/zipdownload.d.ts +20 -0
- package/dist/types/components/StorageBrowser/service-worker/constants.d.ts +4 -0
- package/dist/types/components/StorageBrowser/service-worker/useServiceWorkerRegistration.d.ts +0 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var internals = require('@aws-amplify/storage/internals');
|
|
6
6
|
require('aws-amplify');
|
|
7
7
|
require('@aws-amplify/ui');
|
|
8
|
-
var createStorageBrowser = require('./createStorageBrowser-
|
|
8
|
+
var createStorageBrowser = require('./createStorageBrowser-DC1i0FGj.js');
|
|
9
9
|
require('@zip.js/zip.js');
|
|
10
10
|
require('aws-amplify/storage');
|
|
11
11
|
require('react');
|
|
@@ -397,6 +397,11 @@ const downloadHandler = ({ config, data, }) => {
|
|
|
397
397
|
return { result };
|
|
398
398
|
};
|
|
399
399
|
|
|
400
|
+
/** Scope pathname the download service worker is registered under. */
|
|
401
|
+
const SW_DOWNLOAD_SCOPE = '/amplify-storage-download/';
|
|
402
|
+
/** URL the download service worker script is served from. */
|
|
403
|
+
const SW_URL = `${SW_DOWNLOAD_SCOPE}download-sw.js`;
|
|
404
|
+
|
|
400
405
|
/**
|
|
401
406
|
* Zip Download Handler
|
|
402
407
|
*
|
|
@@ -496,7 +501,37 @@ const collectBlob = async (readable) => {
|
|
|
496
501
|
}
|
|
497
502
|
return new Blob(chunks, { type: 'application/zip' });
|
|
498
503
|
};
|
|
499
|
-
|
|
504
|
+
/**
|
|
505
|
+
* Decides how to deliver the zip for a user-agent string.
|
|
506
|
+
*
|
|
507
|
+
* Only engines that route a navigation to the uncontrolled, narrow-scope
|
|
508
|
+
* download SW can stream to disk:
|
|
509
|
+
* - Chromium → an in-scope hidden `<iframe>` navigation (a top-level
|
|
510
|
+
* `<a download>` nav hangs). Covers Chrome, Edge (`Edg`), Opera (`OPR`),
|
|
511
|
+
* Samsung Internet (`SamsungBrowser`) and Android Chrome.
|
|
512
|
+
* - Firefox (Gecko) → the top-level `<a download>` navigation. Covers Firefox
|
|
513
|
+
* desktop and Firefox for Android.
|
|
514
|
+
*
|
|
515
|
+
* Everything else — Safari and all iOS browsers (which are WebKit under the
|
|
516
|
+
* hood, including Chrome `CriOS`, Firefox `FxiOS` and Edge `EdgiOS`) — cannot
|
|
517
|
+
* reach the worker, so the whole zip is collected into an in-memory blob
|
|
518
|
+
* (OOM risk for very large downloads). iOS is matched explicitly rather than
|
|
519
|
+
* relying on the absence of `Chrome/`/`Gecko/` tokens.
|
|
520
|
+
*/
|
|
521
|
+
const getDownloadTrigger = (userAgent) => {
|
|
522
|
+
// iOS browsers carry Chrome-/Firefox-like tokens but are WebKit; force blob.
|
|
523
|
+
if (/\b(CriOS|FxiOS|EdgiOS)\//.test(userAgent) ||
|
|
524
|
+
/iPhone|iPad|iPod/.test(userAgent)) {
|
|
525
|
+
return 'blob';
|
|
526
|
+
}
|
|
527
|
+
if (/\b(Chrome|Chromium|Edg|OPR|SamsungBrowser)\//.test(userAgent)) {
|
|
528
|
+
return 'iframe';
|
|
529
|
+
}
|
|
530
|
+
if (/\bGecko\//.test(userAgent)) {
|
|
531
|
+
return 'anchor';
|
|
532
|
+
}
|
|
533
|
+
return 'blob';
|
|
534
|
+
};
|
|
500
535
|
/**
|
|
501
536
|
* Registers the SW stream transfer (MessageChannel handshake + keepalive)
|
|
502
537
|
* or falls back to in-memory blob collection when SW is unavailable.
|
|
@@ -507,10 +542,25 @@ const initServiceWorkerStream = (state) => {
|
|
|
507
542
|
state.blobPromise = collectBlob(state.zipReadable);
|
|
508
543
|
return;
|
|
509
544
|
}
|
|
545
|
+
// Route the SW-stream trigger by browser engine. Resolve before transferring
|
|
546
|
+
// the stream: Safari/unknown engines never reach the uncontrolled, narrow-scope
|
|
547
|
+
// download SW, and a transferred stream cannot fall back to blob.
|
|
548
|
+
const trigger = getDownloadTrigger(navigator.userAgent);
|
|
549
|
+
if (trigger === 'blob') {
|
|
550
|
+
state.blobPromise = collectBlob(state.zipReadable);
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
510
553
|
state.swReady = navigator.serviceWorker
|
|
511
|
-
.
|
|
512
|
-
.then((
|
|
513
|
-
//
|
|
554
|
+
.getRegistrations()
|
|
555
|
+
.then((registrations) => {
|
|
556
|
+
// Match the download SW by scope pathname. `getRegistration(url)` returns
|
|
557
|
+
// the registration whose scope is the longest prefix of `url`, so an app's
|
|
558
|
+
// root `/` SW matches when the download SW is absent — the handler then
|
|
559
|
+
// treats the root SW as the download SW and skips the blob fallback,
|
|
560
|
+
// silently failing the download. This hits any page on an origin that has
|
|
561
|
+
// a root-scoped SW, not only a page served at `/`.
|
|
562
|
+
const reg = registrations.find((registration) => new URL(registration.scope).pathname === SW_DOWNLOAD_SCOPE);
|
|
563
|
+
// If the batch was cancelled while getRegistrations() was pending, bail out
|
|
514
564
|
// before wiring up the MessageChannel or keepalive interval. Otherwise the
|
|
515
565
|
// interval would be created after reset() already cleared batchMap, leaking
|
|
516
566
|
// a timer with no reference to clear it.
|
|
@@ -526,10 +576,36 @@ const initServiceWorkerStream = (state) => {
|
|
|
526
576
|
// errors → zipWriter.add() rejects → cancelled flag is set.
|
|
527
577
|
const { port1, port2 } = new MessageChannel();
|
|
528
578
|
port1.onmessage = () => {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
579
|
+
// Encode the id for the URL: `downloadId` embeds the raw folder name,
|
|
580
|
+
// which may contain `#`, `?` or `%`. Unencoded, those break the path
|
|
581
|
+
// (fragment/query) or make the SW's decodeURIComponent throw, and the
|
|
582
|
+
// stream is already transferred so there is no fallback. The SW keys its
|
|
583
|
+
// map on the raw id and decodes the path, so this round-trips.
|
|
584
|
+
const downloadUrl = `${SW_DOWNLOAD_SCOPE}${encodeURIComponent(state.downloadId)}`;
|
|
585
|
+
if (trigger === 'iframe') {
|
|
586
|
+
// Chromium routes an in-scope hidden iframe nav to the SW (a top-level
|
|
587
|
+
// <a download> nav hangs). No `download` attr — the SW's
|
|
588
|
+
// Content-Disposition supplies the filename.
|
|
589
|
+
const iframe = document.createElement('iframe');
|
|
590
|
+
iframe.hidden = true;
|
|
591
|
+
iframe.src = downloadUrl;
|
|
592
|
+
const cleanup = () => {
|
|
593
|
+
if (iframe.parentNode) {
|
|
594
|
+
iframe.parentNode.removeChild(iframe);
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
// An attachment response may not fire `load`, so also clean up on a timeout.
|
|
598
|
+
iframe.addEventListener('load', () => setTimeout(cleanup, 5000));
|
|
599
|
+
setTimeout(cleanup, 60000);
|
|
600
|
+
document.body.appendChild(iframe);
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
// Firefox routes the top-level <a download> nav to the SW.
|
|
604
|
+
const a = document.createElement('a');
|
|
605
|
+
a.href = downloadUrl;
|
|
606
|
+
a.download = `${state.folder}.zip`;
|
|
607
|
+
a.click();
|
|
608
|
+
}
|
|
533
609
|
port1.close();
|
|
534
610
|
};
|
|
535
611
|
// Send the user-facing filename explicitly. `downloadId` embeds Date.now()
|
|
@@ -548,6 +624,15 @@ const initServiceWorkerStream = (state) => {
|
|
|
548
624
|
state.keepaliveInterval = setInterval(() => {
|
|
549
625
|
reg.active?.postMessage({ type: 'keepalive' });
|
|
550
626
|
}, 10000);
|
|
627
|
+
})
|
|
628
|
+
.catch(() => {
|
|
629
|
+
// getRegistrations() can reject (e.g. Chromium throws SecurityError when
|
|
630
|
+
// site data is blocked). The stream has not been transferred on this path
|
|
631
|
+
// — zipReadable is only nulled after a successful postMessage — so fall
|
|
632
|
+
// back to blob rather than failing every file in the batch.
|
|
633
|
+
if (!state.cancelled && state.zipReadable) {
|
|
634
|
+
state.blobPromise = collectBlob(state.zipReadable);
|
|
635
|
+
}
|
|
551
636
|
});
|
|
552
637
|
};
|
|
553
638
|
// ─── Per-file Download ───
|
|
@@ -3917,7 +4002,7 @@ const componentsDefault = {
|
|
|
3917
4002
|
Title,
|
|
3918
4003
|
};
|
|
3919
4004
|
|
|
3920
|
-
const VERSION = '3.18.
|
|
4005
|
+
const VERSION = '3.18.4';
|
|
3921
4006
|
|
|
3922
4007
|
const Fallback = () => (React__namespace["default"].createElement("div", { className: STORAGE_BROWSER_BLOCK_TO_BE_UPDATED },
|
|
3923
4008
|
React__namespace["default"].createElement("div", { className: `${STORAGE_BROWSER_BLOCK_TO_BE_UPDATED}__error-boundary` }, "Something went wrong.")));
|
|
@@ -7931,8 +8016,6 @@ function createProvider({ actions, components, config, options, filePreview = {}
|
|
|
7931
8016
|
return Provider;
|
|
7932
8017
|
}
|
|
7933
8018
|
|
|
7934
|
-
const SW_DOWNLOAD_SCOPE = '/amplify-storage-download/';
|
|
7935
|
-
const SW_URL = '/amplify-storage-download/download-sw.js';
|
|
7936
8019
|
function useServiceWorkerRegistration() {
|
|
7937
8020
|
React.useEffect(() => {
|
|
7938
8021
|
if ('serviceWorker' in navigator) {
|
package/dist/download-sw.js
CHANGED
|
@@ -3,6 +3,34 @@
|
|
|
3
3
|
|
|
4
4
|
/// <reference lib="webworker" />
|
|
5
5
|
const pendingStreams = new Map();
|
|
6
|
+
// Inactivity window for a stored stream. The page pings `keepalive` every 10s
|
|
7
|
+
// while a batch is live, and each ping refreshes this timer, so a download that
|
|
8
|
+
// is actively being handed off is not capped mid-flight (e.g. while the user
|
|
9
|
+
// sits on Chrome's "allow multiple downloads" prompt).
|
|
10
|
+
const CAP_MS = 30000;
|
|
11
|
+
// Absolute ceiling on how long a stored stream can be held, regardless of
|
|
12
|
+
// keepalive pings. Keepalive refreshes the inactivity window but never past this
|
|
13
|
+
// bound, so a stream that is never fetched — an abandoned navigation whose page
|
|
14
|
+
// stays open and keeps pinging — still expires and is cancelled rather than
|
|
15
|
+
// pinning the worker and the transferred stream for the life of the tab.
|
|
16
|
+
const CAP_CEILING_MS = 5 * 60000;
|
|
17
|
+
/**
|
|
18
|
+
* (Re)schedules the safety-cap timer for a stored download. When it fires it
|
|
19
|
+
* drops the entry and cancels the transferred stream's producer — not just our
|
|
20
|
+
* reference. The readable was transferred from the page; if we only dropped it,
|
|
21
|
+
* the page's zip writer would block on backpressure once the queue fills and the
|
|
22
|
+
* download task would never settle. Cancelling rejects the page-side write so
|
|
23
|
+
* the task ends FAILED (an Error reason keeps the handler's `err.message`
|
|
24
|
+
* defined). Returns the timer handle to store on the PendingDownload.
|
|
25
|
+
*/
|
|
26
|
+
const scheduleCapTimeout = (downloadId) => setTimeout(() => {
|
|
27
|
+
const expired = pendingStreams.get(downloadId);
|
|
28
|
+
pendingStreams.delete(downloadId);
|
|
29
|
+
expired?.release();
|
|
30
|
+
expired?.stream
|
|
31
|
+
.cancel(new Error('Download expired before the browser requested it'))
|
|
32
|
+
.catch(() => { });
|
|
33
|
+
}, CAP_MS);
|
|
6
34
|
// Skip waiting to activate immediately on first install (no navigation required)
|
|
7
35
|
self.addEventListener('install', () => {
|
|
8
36
|
self.skipWaiting();
|
|
@@ -26,6 +54,20 @@
|
|
|
26
54
|
// the worker while streaming. Each keepalive holds the SW alive for 15s,
|
|
27
55
|
// overlapping with the 10s ping interval from the page.
|
|
28
56
|
event.waitUntil(new Promise((resolve) => setTimeout(resolve, 15000)));
|
|
57
|
+
// Refresh the inactivity cap for every stream still waiting for its fetch,
|
|
58
|
+
// but never past the absolute ceiling. The ping proves the batch is live, so
|
|
59
|
+
// a stream mid-handoff should not be capped out from under it — yet a stream
|
|
60
|
+
// that is never fetched (abandoned navigation on a still-open, still-pinging
|
|
61
|
+
// page) must still expire, so once the ceiling is reached we stop refreshing
|
|
62
|
+
// and let the pending timer fire.
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
for (const [downloadId, pending] of pendingStreams) {
|
|
65
|
+
if (now - pending.storedAt >= CAP_CEILING_MS) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
clearTimeout(pending.timer);
|
|
69
|
+
pending.timer = scheduleCapTimeout(downloadId);
|
|
70
|
+
}
|
|
29
71
|
return;
|
|
30
72
|
}
|
|
31
73
|
const { downloadId, filename, stream } = data;
|
|
@@ -33,7 +75,27 @@
|
|
|
33
75
|
// Fall back to the id's last path segment only if no explicit filename was
|
|
34
76
|
// provided (older callers); the page normally sends `${folder}.zip`.
|
|
35
77
|
const resolvedFilename = filename ?? downloadId.split('/').pop() ?? 'download.zip';
|
|
36
|
-
|
|
78
|
+
// Hold this SW instance alive until the matching fetch consumes the stream.
|
|
79
|
+
// Keepalive refreshes the inactivity window; the absolute ceiling bounds a
|
|
80
|
+
// stream that is never fetched so it cannot be pinned for the life of the tab.
|
|
81
|
+
//
|
|
82
|
+
// If an entry already exists for this id (same folder in the same millisecond
|
|
83
|
+
// across batches/tabs), release and clear it first so its waitUntil promise
|
|
84
|
+
// and timer are not orphaned by the overwrite.
|
|
85
|
+
const existing = pendingStreams.get(downloadId);
|
|
86
|
+
if (existing) {
|
|
87
|
+
clearTimeout(existing.timer);
|
|
88
|
+
existing.release();
|
|
89
|
+
}
|
|
90
|
+
event.waitUntil(new Promise((resolve) => {
|
|
91
|
+
pendingStreams.set(downloadId, {
|
|
92
|
+
stream,
|
|
93
|
+
filename: resolvedFilename,
|
|
94
|
+
release: resolve,
|
|
95
|
+
timer: scheduleCapTimeout(downloadId),
|
|
96
|
+
storedAt: Date.now(),
|
|
97
|
+
});
|
|
98
|
+
}));
|
|
37
99
|
// Acknowledge receipt so the main thread knows it's safe to trigger the download
|
|
38
100
|
if (event.ports[0]) {
|
|
39
101
|
event.ports[0].postMessage({ ready: true });
|
|
@@ -49,11 +111,45 @@
|
|
|
49
111
|
// fires (folder names may contain spaces or other URL-unsafe characters), so
|
|
50
112
|
// decode it before looking up the stream stored under the unencoded key.
|
|
51
113
|
const rawId = url.pathname.split('/amplify-storage-download/')[1];
|
|
52
|
-
|
|
114
|
+
// Pass through the worker's own script rather than treating it as a download
|
|
115
|
+
// id. Register/update script fetches bypass the fetch handler per spec, so in
|
|
116
|
+
// practice only a direct navigation to the script URL reaches here; this is a
|
|
117
|
+
// guard. Derived from self.location so a rename/relocation of the file stays
|
|
118
|
+
// correct without a hardcoded name.
|
|
119
|
+
if (url.pathname === self.location.pathname) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
let downloadId;
|
|
123
|
+
try {
|
|
124
|
+
downloadId = decodeURIComponent(rawId);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// A malformed percent-sequence (e.g. `%zz`) makes decodeURIComponent throw
|
|
128
|
+
// URIError. Answer the same 410 rather than letting it escape the handler
|
|
129
|
+
// and fall through to the network — the silent-hang path the 410 closes.
|
|
130
|
+
event.respondWith(new Response('Download expired or unavailable', {
|
|
131
|
+
status: 410,
|
|
132
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
133
|
+
}));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
53
136
|
const pending = pendingStreams.get(downloadId);
|
|
54
|
-
if (!pending)
|
|
137
|
+
if (!pending) {
|
|
138
|
+
// Fail fast with a visible 410 rather than returning (which falls through to
|
|
139
|
+
// the network, where the URL 404s and Chrome hangs with no error). A miss
|
|
140
|
+
// means the SW restarted and lost the stream, or the id is stale.
|
|
141
|
+
event.respondWith(new Response('Download expired or unavailable', {
|
|
142
|
+
status: 410,
|
|
143
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
144
|
+
}));
|
|
55
145
|
return;
|
|
146
|
+
}
|
|
56
147
|
pendingStreams.delete(downloadId);
|
|
148
|
+
// The fetch consumed the stream: cancel the no-longer-needed 30s cap timer and
|
|
149
|
+
// release the keepalive hold. The respondWith stream now keeps the SW alive
|
|
150
|
+
// for the transfer.
|
|
151
|
+
clearTimeout(pending.timer);
|
|
152
|
+
pending.release();
|
|
57
153
|
const { stream, filename } = pending;
|
|
58
154
|
event.respondWith(new Response(stream, {
|
|
59
155
|
headers: {
|
|
@@ -2,6 +2,7 @@ import { getUrl } from '@aws-amplify/storage/internals';
|
|
|
2
2
|
import { isFunction } from '@aws-amplify/ui';
|
|
3
3
|
import { getProgress } from './utils.mjs';
|
|
4
4
|
import { ZipWriter } from '@zip.js/zip.js';
|
|
5
|
+
import { SW_DOWNLOAD_SCOPE } from '../../service-worker/constants.mjs';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Zip Download Handler
|
|
@@ -102,7 +103,37 @@ const collectBlob = async (readable) => {
|
|
|
102
103
|
}
|
|
103
104
|
return new Blob(chunks, { type: 'application/zip' });
|
|
104
105
|
};
|
|
105
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Decides how to deliver the zip for a user-agent string.
|
|
108
|
+
*
|
|
109
|
+
* Only engines that route a navigation to the uncontrolled, narrow-scope
|
|
110
|
+
* download SW can stream to disk:
|
|
111
|
+
* - Chromium → an in-scope hidden `<iframe>` navigation (a top-level
|
|
112
|
+
* `<a download>` nav hangs). Covers Chrome, Edge (`Edg`), Opera (`OPR`),
|
|
113
|
+
* Samsung Internet (`SamsungBrowser`) and Android Chrome.
|
|
114
|
+
* - Firefox (Gecko) → the top-level `<a download>` navigation. Covers Firefox
|
|
115
|
+
* desktop and Firefox for Android.
|
|
116
|
+
*
|
|
117
|
+
* Everything else — Safari and all iOS browsers (which are WebKit under the
|
|
118
|
+
* hood, including Chrome `CriOS`, Firefox `FxiOS` and Edge `EdgiOS`) — cannot
|
|
119
|
+
* reach the worker, so the whole zip is collected into an in-memory blob
|
|
120
|
+
* (OOM risk for very large downloads). iOS is matched explicitly rather than
|
|
121
|
+
* relying on the absence of `Chrome/`/`Gecko/` tokens.
|
|
122
|
+
*/
|
|
123
|
+
const getDownloadTrigger = (userAgent) => {
|
|
124
|
+
// iOS browsers carry Chrome-/Firefox-like tokens but are WebKit; force blob.
|
|
125
|
+
if (/\b(CriOS|FxiOS|EdgiOS)\//.test(userAgent) ||
|
|
126
|
+
/iPhone|iPad|iPod/.test(userAgent)) {
|
|
127
|
+
return 'blob';
|
|
128
|
+
}
|
|
129
|
+
if (/\b(Chrome|Chromium|Edg|OPR|SamsungBrowser)\//.test(userAgent)) {
|
|
130
|
+
return 'iframe';
|
|
131
|
+
}
|
|
132
|
+
if (/\bGecko\//.test(userAgent)) {
|
|
133
|
+
return 'anchor';
|
|
134
|
+
}
|
|
135
|
+
return 'blob';
|
|
136
|
+
};
|
|
106
137
|
/**
|
|
107
138
|
* Registers the SW stream transfer (MessageChannel handshake + keepalive)
|
|
108
139
|
* or falls back to in-memory blob collection when SW is unavailable.
|
|
@@ -113,10 +144,25 @@ const initServiceWorkerStream = (state) => {
|
|
|
113
144
|
state.blobPromise = collectBlob(state.zipReadable);
|
|
114
145
|
return;
|
|
115
146
|
}
|
|
147
|
+
// Route the SW-stream trigger by browser engine. Resolve before transferring
|
|
148
|
+
// the stream: Safari/unknown engines never reach the uncontrolled, narrow-scope
|
|
149
|
+
// download SW, and a transferred stream cannot fall back to blob.
|
|
150
|
+
const trigger = getDownloadTrigger(navigator.userAgent);
|
|
151
|
+
if (trigger === 'blob') {
|
|
152
|
+
state.blobPromise = collectBlob(state.zipReadable);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
116
155
|
state.swReady = navigator.serviceWorker
|
|
117
|
-
.
|
|
118
|
-
.then((
|
|
119
|
-
//
|
|
156
|
+
.getRegistrations()
|
|
157
|
+
.then((registrations) => {
|
|
158
|
+
// Match the download SW by scope pathname. `getRegistration(url)` returns
|
|
159
|
+
// the registration whose scope is the longest prefix of `url`, so an app's
|
|
160
|
+
// root `/` SW matches when the download SW is absent — the handler then
|
|
161
|
+
// treats the root SW as the download SW and skips the blob fallback,
|
|
162
|
+
// silently failing the download. This hits any page on an origin that has
|
|
163
|
+
// a root-scoped SW, not only a page served at `/`.
|
|
164
|
+
const reg = registrations.find((registration) => new URL(registration.scope).pathname === SW_DOWNLOAD_SCOPE);
|
|
165
|
+
// If the batch was cancelled while getRegistrations() was pending, bail out
|
|
120
166
|
// before wiring up the MessageChannel or keepalive interval. Otherwise the
|
|
121
167
|
// interval would be created after reset() already cleared batchMap, leaking
|
|
122
168
|
// a timer with no reference to clear it.
|
|
@@ -132,10 +178,36 @@ const initServiceWorkerStream = (state) => {
|
|
|
132
178
|
// errors → zipWriter.add() rejects → cancelled flag is set.
|
|
133
179
|
const { port1, port2 } = new MessageChannel();
|
|
134
180
|
port1.onmessage = () => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
181
|
+
// Encode the id for the URL: `downloadId` embeds the raw folder name,
|
|
182
|
+
// which may contain `#`, `?` or `%`. Unencoded, those break the path
|
|
183
|
+
// (fragment/query) or make the SW's decodeURIComponent throw, and the
|
|
184
|
+
// stream is already transferred so there is no fallback. The SW keys its
|
|
185
|
+
// map on the raw id and decodes the path, so this round-trips.
|
|
186
|
+
const downloadUrl = `${SW_DOWNLOAD_SCOPE}${encodeURIComponent(state.downloadId)}`;
|
|
187
|
+
if (trigger === 'iframe') {
|
|
188
|
+
// Chromium routes an in-scope hidden iframe nav to the SW (a top-level
|
|
189
|
+
// <a download> nav hangs). No `download` attr — the SW's
|
|
190
|
+
// Content-Disposition supplies the filename.
|
|
191
|
+
const iframe = document.createElement('iframe');
|
|
192
|
+
iframe.hidden = true;
|
|
193
|
+
iframe.src = downloadUrl;
|
|
194
|
+
const cleanup = () => {
|
|
195
|
+
if (iframe.parentNode) {
|
|
196
|
+
iframe.parentNode.removeChild(iframe);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
// An attachment response may not fire `load`, so also clean up on a timeout.
|
|
200
|
+
iframe.addEventListener('load', () => setTimeout(cleanup, 5000));
|
|
201
|
+
setTimeout(cleanup, 60000);
|
|
202
|
+
document.body.appendChild(iframe);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
// Firefox routes the top-level <a download> nav to the SW.
|
|
206
|
+
const a = document.createElement('a');
|
|
207
|
+
a.href = downloadUrl;
|
|
208
|
+
a.download = `${state.folder}.zip`;
|
|
209
|
+
a.click();
|
|
210
|
+
}
|
|
139
211
|
port1.close();
|
|
140
212
|
};
|
|
141
213
|
// Send the user-facing filename explicitly. `downloadId` embeds Date.now()
|
|
@@ -154,6 +226,15 @@ const initServiceWorkerStream = (state) => {
|
|
|
154
226
|
state.keepaliveInterval = setInterval(() => {
|
|
155
227
|
reg.active?.postMessage({ type: 'keepalive' });
|
|
156
228
|
}, 10000);
|
|
229
|
+
})
|
|
230
|
+
.catch(() => {
|
|
231
|
+
// getRegistrations() can reject (e.g. Chromium throws SecurityError when
|
|
232
|
+
// site data is blocked). The stream has not been transferred on this path
|
|
233
|
+
// — zipReadable is only nulled after a successful postMessage — so fall
|
|
234
|
+
// back to blob rather than failing every file in the batch.
|
|
235
|
+
if (!state.cancelled && state.zipReadable) {
|
|
236
|
+
state.blobPromise = collectBlob(state.zipReadable);
|
|
237
|
+
}
|
|
157
238
|
});
|
|
158
239
|
};
|
|
159
240
|
// ─── Per-file Download ───
|
|
@@ -380,4 +461,4 @@ const zipDownloadHandler = ({ config, data, all, options, }) => {
|
|
|
380
461
|
};
|
|
381
462
|
};
|
|
382
463
|
|
|
383
|
-
export { zipDownloadHandler };
|
|
464
|
+
export { getDownloadTrigger, zipDownloadHandler };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Scope pathname the download service worker is registered under. */
|
|
2
|
+
const SW_DOWNLOAD_SCOPE = '/amplify-storage-download/';
|
|
3
|
+
/** URL the download service worker script is served from. */
|
|
4
|
+
const SW_URL = `${SW_DOWNLOAD_SCOPE}download-sw.js`;
|
|
5
|
+
|
|
6
|
+
export { SW_DOWNLOAD_SCOPE, SW_URL };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
+
import { SW_URL, SW_DOWNLOAD_SCOPE } from './constants.mjs';
|
|
2
3
|
|
|
3
|
-
const SW_DOWNLOAD_SCOPE = '/amplify-storage-download/';
|
|
4
|
-
const SW_URL = '/amplify-storage-download/download-sw.js';
|
|
5
4
|
function useServiceWorkerRegistration() {
|
|
6
5
|
useEffect(() => {
|
|
7
6
|
if ('serviceWorker' in navigator) {
|
|
@@ -22,4 +21,4 @@ function useServiceWorkerRegistration() {
|
|
|
22
21
|
}, []);
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
export {
|
|
24
|
+
export { useServiceWorkerRegistration };
|
package/dist/esm/version.mjs
CHANGED
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ var uiReactCore = require('@aws-amplify/ui-react-core');
|
|
|
9
9
|
var auth = require('aws-amplify/auth');
|
|
10
10
|
var storage = require('aws-amplify/storage');
|
|
11
11
|
var internal = require('@aws-amplify/ui-react/internal');
|
|
12
|
-
var createStorageBrowser = require('./createStorageBrowser-
|
|
12
|
+
var createStorageBrowser = require('./createStorageBrowser-DC1i0FGj.js');
|
|
13
13
|
require('@aws-amplify/storage/internals');
|
|
14
14
|
require('aws-amplify');
|
|
15
15
|
require('@zip.js/zip.js');
|
|
@@ -1,3 +1,23 @@
|
|
|
1
1
|
import type { DownloadHandler } from './download';
|
|
2
|
+
/** How the SW-backed download is triggered for a given browser engine. */
|
|
3
|
+
export type DownloadTrigger = 'iframe' | 'anchor' | 'blob';
|
|
4
|
+
/**
|
|
5
|
+
* Decides how to deliver the zip for a user-agent string.
|
|
6
|
+
*
|
|
7
|
+
* Only engines that route a navigation to the uncontrolled, narrow-scope
|
|
8
|
+
* download SW can stream to disk:
|
|
9
|
+
* - Chromium → an in-scope hidden `<iframe>` navigation (a top-level
|
|
10
|
+
* `<a download>` nav hangs). Covers Chrome, Edge (`Edg`), Opera (`OPR`),
|
|
11
|
+
* Samsung Internet (`SamsungBrowser`) and Android Chrome.
|
|
12
|
+
* - Firefox (Gecko) → the top-level `<a download>` navigation. Covers Firefox
|
|
13
|
+
* desktop and Firefox for Android.
|
|
14
|
+
*
|
|
15
|
+
* Everything else — Safari and all iOS browsers (which are WebKit under the
|
|
16
|
+
* hood, including Chrome `CriOS`, Firefox `FxiOS` and Edge `EdgiOS`) — cannot
|
|
17
|
+
* reach the worker, so the whole zip is collected into an in-memory blob
|
|
18
|
+
* (OOM risk for very large downloads). iOS is matched explicitly rather than
|
|
19
|
+
* relying on the absence of `Chrome/`/`Gecko/` tokens.
|
|
20
|
+
*/
|
|
21
|
+
export declare const getDownloadTrigger: (userAgent: string) => DownloadTrigger;
|
|
2
22
|
/** Main entry point — called once per file in a multi-select download batch. */
|
|
3
23
|
export declare const zipDownloadHandler: DownloadHandler;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Scope pathname the download service worker is registered under. */
|
|
2
|
+
export declare const SW_DOWNLOAD_SCOPE = "/amplify-storage-download/";
|
|
3
|
+
/** URL the download service worker script is served from. */
|
|
4
|
+
export declare const SW_URL = "/amplify-storage-download/download-sw.js";
|
package/dist/types/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "3.18.
|
|
1
|
+
export declare const VERSION = "3.18.4";
|