@dimah-s3/react 1.4.1 → 1.5.0
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/engine.js +10 -5
- package/dist/engine.js.map +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/use-delete.d.ts +1 -1
- package/dist/hooks/use-delete.d.ts.map +1 -1
- package/dist/hooks/use-route-upload-policy.d.ts +12 -1
- package/dist/hooks/use-route-upload-policy.d.ts.map +1 -1
- package/dist/hooks/use-upload.d.ts +2 -2
- package/dist/hooks/use-upload.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +115 -22
- package/dist/index.js.map +1 -1
- package/dist/store/local-storage-store.d.ts.map +1 -1
- package/dist/upload/multipart.d.ts +1 -1
- package/dist/upload/multipart.d.ts.map +1 -1
- package/dist/upload/retry.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIError, createS3Client as createS3Client$1, sha256File, formatFileSize, isAPIError, S3_ERROR_CODES, parseFileName, validateFile } from '@dimah-s3/core';
|
|
1
|
+
import { APIError, createS3Client as createS3Client$1, sha256File, formatFileSize, isAPIError, S3_ERROR_CODES, parseFileName, DELETE_BATCH_MAX_KEYS, isS3ErrorCode, validateFile } from '@dimah-s3/core';
|
|
2
2
|
export { APIError, S3_ERROR_CODES, buildContentDisposition, buildObjectKey, buildPublicObjectUrl, fileNameFromKey, formatFileSize, isAPIError, isS3ErrorCode, matchesMagicBytes, sanitizeFileName, sha256Base64, sha256File, sniffContentType, validateFile } from '@dimah-s3/core';
|
|
3
3
|
import { createContext, useContext, useCallback, useState, useEffect, useRef, useLayoutEffect } from 'react';
|
|
4
4
|
import { TranslationProvider, useTranslations } from '@fuma-translate/react';
|
|
@@ -86,7 +86,10 @@ function createLocalStorageStore() {
|
|
|
86
86
|
const raw = localStorage.getItem(STORAGE_PREFIX + resumeKey);
|
|
87
87
|
if (!raw) return null;
|
|
88
88
|
const stored = JSON.parse(raw);
|
|
89
|
-
|
|
89
|
+
if (stored.resumeKey !== resumeKey || stored.fileSize !== fileSize || !stored.uploadId || !stored.key) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
return stored;
|
|
90
93
|
} catch {
|
|
91
94
|
return null;
|
|
92
95
|
}
|
|
@@ -135,8 +138,12 @@ var RETRY_BASE_DELAY = 1e3;
|
|
|
135
138
|
var DEFAULT_CONCURRENT_PARTS = 2;
|
|
136
139
|
var DEFAULT_CONCURRENT_FILES = 2;
|
|
137
140
|
var DEFAULT_MAX_FILES = 1;
|
|
138
|
-
|
|
139
|
-
|
|
141
|
+
function isNonRetryable(err) {
|
|
142
|
+
if (err.name === "AbortError") return true;
|
|
143
|
+
if (!isAPIError(err)) return false;
|
|
144
|
+
const status = err.statusCode;
|
|
145
|
+
return typeof status === "number" && status >= 400 && status < 500 && status !== 429;
|
|
146
|
+
}
|
|
140
147
|
function waitForRetry(delay, signal) {
|
|
141
148
|
if (signal?.aborted) {
|
|
142
149
|
return Promise.reject(new DOMException("Upload aborted", "AbortError"));
|
|
@@ -162,7 +169,7 @@ async function withRetry(fn, retryConfig, signal) {
|
|
|
162
169
|
try {
|
|
163
170
|
return await fn();
|
|
164
171
|
} catch (err) {
|
|
165
|
-
if (err
|
|
172
|
+
if (isNonRetryable(err)) throw err;
|
|
166
173
|
lastError = err;
|
|
167
174
|
if (attempt < maxRetries) {
|
|
168
175
|
const delay = baseDelay * 2 ** attempt;
|
|
@@ -345,7 +352,8 @@ async function uploadMultipart(api, file, route, partSize, concurrentParts, onPr
|
|
|
345
352
|
key = existing.key;
|
|
346
353
|
for (const p of parts2) completedPartNumbers.add(p.partNumber);
|
|
347
354
|
onMultipartInit?.(uploadId, key);
|
|
348
|
-
} catch {
|
|
355
|
+
} catch (err) {
|
|
356
|
+
if (!isS3ErrorCode(err, "OBJECT_NOT_FOUND")) throw err;
|
|
349
357
|
await store?.delete(resumeKey);
|
|
350
358
|
({ uploadId, key } = await initUpload());
|
|
351
359
|
}
|
|
@@ -1347,31 +1355,79 @@ function useFileIntake(options) {
|
|
|
1347
1355
|
fileRejections
|
|
1348
1356
|
};
|
|
1349
1357
|
}
|
|
1358
|
+
var warnedCatalogKeys = /* @__PURE__ */ new Set();
|
|
1359
|
+
function toError(cause) {
|
|
1360
|
+
return cause instanceof Error ? cause : new Error(String(cause));
|
|
1361
|
+
}
|
|
1362
|
+
function isDevelopment() {
|
|
1363
|
+
const env = globalThis.process?.env?.NODE_ENV;
|
|
1364
|
+
return env === "development";
|
|
1365
|
+
}
|
|
1366
|
+
function warnCatalogFailure(route, hasExplicitConstraints2, cause) {
|
|
1367
|
+
if (!isDevelopment()) return;
|
|
1368
|
+
if (hasExplicitConstraints2) return;
|
|
1369
|
+
const key = String(route);
|
|
1370
|
+
if (warnedCatalogKeys.has(key)) return;
|
|
1371
|
+
warnedCatalogKeys.add(key);
|
|
1372
|
+
console.warn(
|
|
1373
|
+
`[@dimah-s3/react] Route catalog failed for "${key}". Pass accept/maxFileSize on useUpload, or check GET /routes. Client-side constraints will not come from the server.`,
|
|
1374
|
+
cause
|
|
1375
|
+
);
|
|
1376
|
+
}
|
|
1377
|
+
function hasExplicitConstraints(options) {
|
|
1378
|
+
return options.accept != null || options.maxFileSize != null;
|
|
1379
|
+
}
|
|
1350
1380
|
function useRouteUploadPolicy(options) {
|
|
1351
1381
|
const contextApi = useContext(S3Context);
|
|
1352
1382
|
const api = options.api ?? contextApi;
|
|
1353
1383
|
const optsRef = useLiveRef(options);
|
|
1354
1384
|
const [catalogPolicy, setCatalogPolicy] = useState({});
|
|
1385
|
+
const [catalogStatus, setCatalogStatus] = useState(
|
|
1386
|
+
api ? "loading" : "idle"
|
|
1387
|
+
);
|
|
1388
|
+
const [catalogError, setCatalogError] = useState(null);
|
|
1355
1389
|
useEffect(() => {
|
|
1356
|
-
if (!api)
|
|
1390
|
+
if (!api) {
|
|
1391
|
+
setCatalogPolicy({});
|
|
1392
|
+
setCatalogStatus("idle");
|
|
1393
|
+
setCatalogError(null);
|
|
1394
|
+
return;
|
|
1395
|
+
}
|
|
1357
1396
|
let cancelled = false;
|
|
1397
|
+
setCatalogPolicy({});
|
|
1398
|
+
setCatalogStatus("loading");
|
|
1399
|
+
setCatalogError(null);
|
|
1358
1400
|
void loadRouteCatalog(api).then((catalog) => {
|
|
1359
1401
|
if (cancelled) return;
|
|
1360
1402
|
setCatalogPolicy(
|
|
1361
1403
|
mergeRouteUploadPolicy(catalog, optsRef.current.route, {})
|
|
1362
1404
|
);
|
|
1363
|
-
|
|
1405
|
+
setCatalogStatus("ready");
|
|
1406
|
+
}).catch((cause) => {
|
|
1407
|
+
if (cancelled) return;
|
|
1408
|
+
const error = toError(cause);
|
|
1409
|
+
setCatalogError(error);
|
|
1410
|
+
setCatalogStatus("error");
|
|
1411
|
+
warnCatalogFailure(
|
|
1412
|
+
optsRef.current.route,
|
|
1413
|
+
hasExplicitConstraints(optsRef.current),
|
|
1414
|
+
cause
|
|
1415
|
+
);
|
|
1364
1416
|
});
|
|
1365
1417
|
return () => {
|
|
1366
1418
|
cancelled = true;
|
|
1367
1419
|
};
|
|
1368
1420
|
}, [api, options.route, optsRef]);
|
|
1369
|
-
return
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1421
|
+
return {
|
|
1422
|
+
...mergeRouteUploadPolicy(null, options.route, {
|
|
1423
|
+
accept: options.accept ?? catalogPolicy.accept,
|
|
1424
|
+
maxFileSize: options.maxFileSize ?? catalogPolicy.maxFileSize,
|
|
1425
|
+
multipart: options.multipart ?? catalogPolicy.multipart,
|
|
1426
|
+
checksum: options.checksum ?? catalogPolicy.checksum
|
|
1427
|
+
}),
|
|
1428
|
+
catalogStatus,
|
|
1429
|
+
catalogError
|
|
1430
|
+
};
|
|
1375
1431
|
}
|
|
1376
1432
|
|
|
1377
1433
|
// src/hooks/use-upload.ts
|
|
@@ -1467,8 +1523,8 @@ function useDownload(options) {
|
|
|
1467
1523
|
const speedUpdaterRef = useRef(
|
|
1468
1524
|
createThrottledSpeedUpdater(createSpeedTracker())
|
|
1469
1525
|
);
|
|
1470
|
-
const
|
|
1471
|
-
async (key, downloadName) => {
|
|
1526
|
+
const requestDownloadUrl = useCallback(
|
|
1527
|
+
async (key, downloadName, generation) => {
|
|
1472
1528
|
const opts = optsRef.current;
|
|
1473
1529
|
const api = opts.api ?? apiRef.current;
|
|
1474
1530
|
if (!api) throw new Error(missingApiMessage("useDownload"));
|
|
@@ -1486,6 +1542,9 @@ function useDownload(options) {
|
|
|
1486
1542
|
fileName: downloadName,
|
|
1487
1543
|
...opts.disposition ? { disposition: opts.disposition } : {}
|
|
1488
1544
|
});
|
|
1545
|
+
if (generation !== generationRef.current) {
|
|
1546
|
+
return { url: result.url, expiresIn: result.expiresIn };
|
|
1547
|
+
}
|
|
1489
1548
|
patch((draft) => {
|
|
1490
1549
|
draft.phase = "idle";
|
|
1491
1550
|
draft.error = null;
|
|
@@ -1494,6 +1553,7 @@ function useDownload(options) {
|
|
|
1494
1553
|
});
|
|
1495
1554
|
return { url: result.url, expiresIn: result.expiresIn };
|
|
1496
1555
|
} catch (err) {
|
|
1556
|
+
if (generation !== generationRef.current) return null;
|
|
1497
1557
|
patch((draft) => {
|
|
1498
1558
|
draft.phase = "error";
|
|
1499
1559
|
draft.error = toHookError(err, "Download failed");
|
|
@@ -1506,11 +1566,20 @@ function useDownload(options) {
|
|
|
1506
1566
|
},
|
|
1507
1567
|
[apiRef, optsRef, patch]
|
|
1508
1568
|
);
|
|
1569
|
+
const presign = useCallback(
|
|
1570
|
+
async (key, downloadName) => {
|
|
1571
|
+
const generation = ++generationRef.current;
|
|
1572
|
+
return requestDownloadUrl(key, downloadName, generation);
|
|
1573
|
+
},
|
|
1574
|
+
[requestDownloadUrl]
|
|
1575
|
+
);
|
|
1509
1576
|
const downloadNavigate = useCallback(
|
|
1510
1577
|
async (key, downloadName) => {
|
|
1511
1578
|
const opts = optsRef.current;
|
|
1579
|
+
const generation = ++generationRef.current;
|
|
1512
1580
|
if (opts.beforeDownload) {
|
|
1513
1581
|
const allowed = await opts.beforeDownload(key);
|
|
1582
|
+
if (generation !== generationRef.current) return;
|
|
1514
1583
|
if (!allowed) {
|
|
1515
1584
|
patch((draft) => {
|
|
1516
1585
|
draft.phase = "error";
|
|
@@ -1525,12 +1594,12 @@ function useDownload(options) {
|
|
|
1525
1594
|
return;
|
|
1526
1595
|
}
|
|
1527
1596
|
}
|
|
1528
|
-
const result = await
|
|
1529
|
-
if (!result) return;
|
|
1597
|
+
const result = await requestDownloadUrl(key, downloadName, generation);
|
|
1598
|
+
if (!result || generation !== generationRef.current) return;
|
|
1530
1599
|
window.location.href = result.url;
|
|
1531
1600
|
opts.onInitiated?.(key);
|
|
1532
1601
|
},
|
|
1533
|
-
[optsRef,
|
|
1602
|
+
[optsRef, requestDownloadUrl, patch]
|
|
1534
1603
|
);
|
|
1535
1604
|
const downloadFetch = useCallback(
|
|
1536
1605
|
async (key, downloadName) => {
|
|
@@ -1723,8 +1792,10 @@ function useDelete(options) {
|
|
|
1723
1792
|
const apiRef = useLiveRef(contextApi);
|
|
1724
1793
|
const objectKeyRef = useRef(null);
|
|
1725
1794
|
const inFlightRef = useRef(false);
|
|
1795
|
+
const generationRef = useRef(0);
|
|
1726
1796
|
const requestDelete = useCallback(
|
|
1727
1797
|
(key) => {
|
|
1798
|
+
if (inFlightRef.current) return;
|
|
1728
1799
|
objectKeyRef.current = key;
|
|
1729
1800
|
patch((draft) => {
|
|
1730
1801
|
draft.phase = "confirming";
|
|
@@ -1743,9 +1814,13 @@ function useDelete(options) {
|
|
|
1743
1814
|
throw new Error(
|
|
1744
1815
|
"[dimah-s3] No S3Api found. Pass `api` to useDelete or wrap with <S3Provider>."
|
|
1745
1816
|
);
|
|
1817
|
+
const generation = ++generationRef.current;
|
|
1818
|
+
inFlightRef.current = true;
|
|
1746
1819
|
if (opts.beforeDelete) {
|
|
1747
1820
|
const allowed = await opts.beforeDelete(key);
|
|
1821
|
+
if (generation !== generationRef.current) return;
|
|
1748
1822
|
if (!allowed) {
|
|
1823
|
+
inFlightRef.current = false;
|
|
1749
1824
|
objectKeyRef.current = null;
|
|
1750
1825
|
patch((draft) => {
|
|
1751
1826
|
draft.phase = "error";
|
|
@@ -1758,7 +1833,6 @@ function useDelete(options) {
|
|
|
1758
1833
|
return;
|
|
1759
1834
|
}
|
|
1760
1835
|
}
|
|
1761
|
-
inFlightRef.current = true;
|
|
1762
1836
|
patch((draft) => {
|
|
1763
1837
|
draft.phase = "deleting";
|
|
1764
1838
|
draft.error = null;
|
|
@@ -1767,6 +1841,7 @@ function useDelete(options) {
|
|
|
1767
1841
|
opts.onDeleteStart?.(key);
|
|
1768
1842
|
try {
|
|
1769
1843
|
await api.delete({ route: opts.route, key });
|
|
1844
|
+
if (generation !== generationRef.current) return;
|
|
1770
1845
|
objectKeyRef.current = null;
|
|
1771
1846
|
patch((draft) => {
|
|
1772
1847
|
draft.phase = "success";
|
|
@@ -1779,6 +1854,7 @@ function useDelete(options) {
|
|
|
1779
1854
|
opts.onError?.(key, err, "success");
|
|
1780
1855
|
}
|
|
1781
1856
|
} catch (err) {
|
|
1857
|
+
if (generation !== generationRef.current) return;
|
|
1782
1858
|
patch((draft) => {
|
|
1783
1859
|
draft.phase = "error";
|
|
1784
1860
|
draft.error = toHookError(err, "Delete failed");
|
|
@@ -1786,7 +1862,9 @@ function useDelete(options) {
|
|
|
1786
1862
|
});
|
|
1787
1863
|
opts.onError?.(key, err, "deleting");
|
|
1788
1864
|
} finally {
|
|
1789
|
-
|
|
1865
|
+
if (generation === generationRef.current) {
|
|
1866
|
+
inFlightRef.current = false;
|
|
1867
|
+
}
|
|
1790
1868
|
}
|
|
1791
1869
|
},
|
|
1792
1870
|
[apiRef, optsRef, patch]
|
|
@@ -1817,16 +1895,24 @@ function useDelete(options) {
|
|
|
1817
1895
|
throw new Error(
|
|
1818
1896
|
"[dimah-s3] No S3Api found. Pass `api` to useDelete or wrap with <S3Provider>."
|
|
1819
1897
|
);
|
|
1898
|
+
if (keys.length > DELETE_BATCH_MAX_KEYS) {
|
|
1899
|
+
throw new Error(
|
|
1900
|
+
`[dimah-s3] deleteMany accepts at most ${DELETE_BATCH_MAX_KEYS} keys.`
|
|
1901
|
+
);
|
|
1902
|
+
}
|
|
1903
|
+
const generation = ++generationRef.current;
|
|
1820
1904
|
inFlightRef.current = true;
|
|
1821
1905
|
patch((draft) => {
|
|
1822
1906
|
draft.phase = "deleting";
|
|
1823
1907
|
draft.error = null;
|
|
1908
|
+
draft.objectKey = null;
|
|
1824
1909
|
});
|
|
1825
1910
|
try {
|
|
1826
1911
|
const { results } = await api.deleteMany({
|
|
1827
1912
|
route: opts.route,
|
|
1828
1913
|
keys
|
|
1829
1914
|
});
|
|
1915
|
+
if (generation !== generationRef.current) return;
|
|
1830
1916
|
const failed = results.filter((item) => !item.success);
|
|
1831
1917
|
for (const item of results) {
|
|
1832
1918
|
if (item.success) {
|
|
@@ -1858,22 +1944,29 @@ function useDelete(options) {
|
|
|
1858
1944
|
draft.error = null;
|
|
1859
1945
|
});
|
|
1860
1946
|
} catch (err) {
|
|
1947
|
+
if (generation !== generationRef.current) return;
|
|
1861
1948
|
patch((draft) => {
|
|
1862
1949
|
draft.phase = "error";
|
|
1863
1950
|
draft.error = toHookError(err, "Delete failed");
|
|
1864
1951
|
});
|
|
1865
1952
|
opts.onError?.(keys[0] ?? "", err, "deleting");
|
|
1866
1953
|
} finally {
|
|
1867
|
-
|
|
1954
|
+
if (generation === generationRef.current) {
|
|
1955
|
+
inFlightRef.current = false;
|
|
1956
|
+
}
|
|
1868
1957
|
}
|
|
1869
1958
|
},
|
|
1870
1959
|
[apiRef, optsRef, patch]
|
|
1871
1960
|
);
|
|
1872
1961
|
const cancelDelete = useCallback(() => {
|
|
1962
|
+
generationRef.current += 1;
|
|
1963
|
+
inFlightRef.current = false;
|
|
1873
1964
|
objectKeyRef.current = null;
|
|
1874
1965
|
replace(INITIAL_STATE3);
|
|
1875
1966
|
}, [replace]);
|
|
1876
1967
|
const reset = useCallback(() => {
|
|
1968
|
+
generationRef.current += 1;
|
|
1969
|
+
inFlightRef.current = false;
|
|
1877
1970
|
objectKeyRef.current = null;
|
|
1878
1971
|
replace(INITIAL_STATE3);
|
|
1879
1972
|
}, [replace]);
|