@ai4b-team/fsaos-gateway-sdk 5.5.1 → 5.5.2
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/iife/gateway.js +5 -5
- package/dist/iife/gateway.js.map +4 -4
- package/dist/iife/ui.js +1 -1
- package/dist/index.cjs +80 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +80 -9
- package/dist/index.js.map +1 -1
- package/dist/ui.d.cts +1 -1
- package/dist/ui.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -3333,6 +3333,8 @@ interface FileRef {
|
|
|
3333
3333
|
size_bytes: number;
|
|
3334
3334
|
version: number;
|
|
3335
3335
|
etag?: string;
|
|
3336
|
+
/** SHA-256 verified by storage, when the upload used an integrity contract. */
|
|
3337
|
+
content_sha256?: string;
|
|
3336
3338
|
}
|
|
3337
3339
|
interface UploadOptions {
|
|
3338
3340
|
/** VFS path where the file item will be created (required). */
|
package/dist/index.d.ts
CHANGED
|
@@ -3333,6 +3333,8 @@ interface FileRef {
|
|
|
3333
3333
|
size_bytes: number;
|
|
3334
3334
|
version: number;
|
|
3335
3335
|
etag?: string;
|
|
3336
|
+
/** SHA-256 verified by storage, when the upload used an integrity contract. */
|
|
3337
|
+
content_sha256?: string;
|
|
3336
3338
|
}
|
|
3337
3339
|
interface UploadOptions {
|
|
3338
3340
|
/** VFS path where the file item will be created (required). */
|
package/dist/index.js
CHANGED
|
@@ -6239,6 +6239,32 @@ function getRequiredFormFields(typeDef) {
|
|
|
6239
6239
|
(f) => required.has(f.key) || f.default_value === void 0
|
|
6240
6240
|
);
|
|
6241
6241
|
}
|
|
6242
|
+
|
|
6243
|
+
// src/upload-integrity.ts
|
|
6244
|
+
var MAX_BUFFERED_HASH_BYTES = 16 * 1024 * 1024;
|
|
6245
|
+
var hashing = Promise.resolve();
|
|
6246
|
+
function checkUploadCancelled(signal2) {
|
|
6247
|
+
if (signal2?.aborted) throw new Error("Upload cancelled");
|
|
6248
|
+
}
|
|
6249
|
+
async function prepareUploadIntegrity(file, signal2) {
|
|
6250
|
+
checkUploadCancelled(signal2);
|
|
6251
|
+
if (file.size > MAX_BUFFERED_HASH_BYTES || !globalThis.crypto?.subtle) return {};
|
|
6252
|
+
const operation = hashing.then(async () => {
|
|
6253
|
+
checkUploadCancelled(signal2);
|
|
6254
|
+
const bytes = await file.arrayBuffer();
|
|
6255
|
+
checkUploadCancelled(signal2);
|
|
6256
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
|
|
6257
|
+
checkUploadCancelled(signal2);
|
|
6258
|
+
return {
|
|
6259
|
+
expected_sha256: Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""),
|
|
6260
|
+
expected_size_bytes: file.size
|
|
6261
|
+
};
|
|
6262
|
+
});
|
|
6263
|
+
hashing = operation.catch(() => void 0);
|
|
6264
|
+
return operation;
|
|
6265
|
+
}
|
|
6266
|
+
|
|
6267
|
+
// src/upload.ts
|
|
6242
6268
|
function generateUploadId() {
|
|
6243
6269
|
if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
|
|
6244
6270
|
const bytes = new Uint8Array(16);
|
|
@@ -6260,6 +6286,12 @@ async function uploadFile(file, options, onProgress, signal2) {
|
|
|
6260
6286
|
const itemType = options.itemType || "file";
|
|
6261
6287
|
const collisionPolicy = options.collisionPolicy || "keep_both";
|
|
6262
6288
|
const uploadId = collisionPolicy === "keep_both" ? generateUploadId() : void 0;
|
|
6289
|
+
const scopeVersion = getScopeVersion(), sessionGeneration2 = getSessionGeneration();
|
|
6290
|
+
const integrity = await prepareUploadIntegrity(file, signal2);
|
|
6291
|
+
checkUploadCancelled(signal2);
|
|
6292
|
+
if (scopeVersion !== getScopeVersion() || sessionGeneration2 !== getSessionGeneration()) {
|
|
6293
|
+
throw new Error("The account or Space changed while preparing the upload. Start it again.");
|
|
6294
|
+
}
|
|
6263
6295
|
const createParams = {
|
|
6264
6296
|
parent_path: options.parentPath,
|
|
6265
6297
|
item_type: itemType,
|
|
@@ -6270,7 +6302,8 @@ async function uploadFile(file, options, onProgress, signal2) {
|
|
|
6270
6302
|
type: "upload",
|
|
6271
6303
|
content_type: contentType,
|
|
6272
6304
|
collision_policy: collisionPolicy,
|
|
6273
|
-
...uploadId ? { upload_id: uploadId } : {}
|
|
6305
|
+
...uploadId ? { upload_id: uploadId } : {},
|
|
6306
|
+
...integrity
|
|
6274
6307
|
}
|
|
6275
6308
|
}
|
|
6276
6309
|
};
|
|
@@ -6278,6 +6311,7 @@ async function uploadFile(file, options, onProgress, signal2) {
|
|
|
6278
6311
|
createParams.card_parent_path = options.cardParentPath;
|
|
6279
6312
|
}
|
|
6280
6313
|
const createResult = await gatewayCall("create", createParams);
|
|
6314
|
+
checkUploadCancelled(signal2);
|
|
6281
6315
|
const result = createResult;
|
|
6282
6316
|
if (!result.id) {
|
|
6283
6317
|
throw new Error(result.error || result.message || "Failed to create file item");
|
|
@@ -6294,7 +6328,8 @@ async function uploadFile(file, options, onProgress, signal2) {
|
|
|
6294
6328
|
throw new Error("Gateway returned no storage_key \u2014 file_source may not be supported for this item type");
|
|
6295
6329
|
}
|
|
6296
6330
|
await uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, file, contentType, onProgress, signal2);
|
|
6297
|
-
|
|
6331
|
+
checkUploadCancelled(signal2);
|
|
6332
|
+
const fileRef = await confirmUpload(itemId, storageKey, contentType, uploadToken, integrity.expected_sha256, file.size);
|
|
6298
6333
|
return { itemId, itemPath, fileRef };
|
|
6299
6334
|
}
|
|
6300
6335
|
function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, contentType, onProgress, signal2) {
|
|
@@ -6320,6 +6355,7 @@ function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, conten
|
|
|
6320
6355
|
};
|
|
6321
6356
|
xhr.onerror = () => reject(new Error("Storage upload network error"));
|
|
6322
6357
|
xhr.ontimeout = () => reject(new Error("Storage upload timed out"));
|
|
6358
|
+
xhr.onabort = () => reject(new Error("Upload cancelled"));
|
|
6323
6359
|
if (signal2) {
|
|
6324
6360
|
if (signal2.aborted) {
|
|
6325
6361
|
reject(new Error("Upload cancelled"));
|
|
@@ -6333,7 +6369,7 @@ function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, conten
|
|
|
6333
6369
|
xhr.send(body);
|
|
6334
6370
|
});
|
|
6335
6371
|
}
|
|
6336
|
-
async function confirmUpload(itemId, storageKey, contentType) {
|
|
6372
|
+
async function confirmUpload(itemId, storageKey, contentType, uploadToken, contentSha256, sizeBytes) {
|
|
6337
6373
|
const token = await getAccessToken();
|
|
6338
6374
|
const response = await fetch(`${GATEWAY_URL}/storage/upload-complete`, {
|
|
6339
6375
|
method: "POST",
|
|
@@ -6344,7 +6380,9 @@ async function confirmUpload(itemId, storageKey, contentType) {
|
|
|
6344
6380
|
body: JSON.stringify({
|
|
6345
6381
|
item_id: itemId,
|
|
6346
6382
|
storage_key: storageKey,
|
|
6347
|
-
content_type: contentType
|
|
6383
|
+
content_type: contentType,
|
|
6384
|
+
...uploadToken ? { upload_token: uploadToken } : {},
|
|
6385
|
+
...contentSha256 ? { hash: contentSha256 } : {}
|
|
6348
6386
|
})
|
|
6349
6387
|
});
|
|
6350
6388
|
if (!response.ok) {
|
|
@@ -6355,6 +6393,9 @@ async function confirmUpload(itemId, storageKey, contentType) {
|
|
|
6355
6393
|
if (!result.success) {
|
|
6356
6394
|
throw new Error(result.error || result.message || "Upload confirmation rejected");
|
|
6357
6395
|
}
|
|
6396
|
+
if (contentSha256 && (result.file_ref?.content_sha256 !== contentSha256 || result.file_ref?.size_bytes !== sizeBytes || result.file_ref?.content_type !== contentType)) {
|
|
6397
|
+
throw new Error("Stored file did not confirm the expected checksum, size and type");
|
|
6398
|
+
}
|
|
6358
6399
|
return result.file_ref;
|
|
6359
6400
|
}
|
|
6360
6401
|
function useFileUpload() {
|
|
@@ -6362,6 +6403,7 @@ function useFileUpload() {
|
|
|
6362
6403
|
const qc = useQueryClient();
|
|
6363
6404
|
const uploadsRef = useRef(uploads);
|
|
6364
6405
|
uploadsRef.current = uploads;
|
|
6406
|
+
const attempts = useRef(/* @__PURE__ */ new Map());
|
|
6365
6407
|
const updateUpload = useCallback((id, patch) => {
|
|
6366
6408
|
setUploads((prev) => prev.map((u) => u.id === id ? { ...u, ...patch } : u));
|
|
6367
6409
|
}, []);
|
|
@@ -6378,9 +6420,20 @@ function useFileUpload() {
|
|
|
6378
6420
|
let storageKey = existingState?.storageKey || null;
|
|
6379
6421
|
let uploadToken = existingState?.uploadToken || null;
|
|
6380
6422
|
let storageWorkerUrl = existingState?.storageWorkerUrl || null;
|
|
6423
|
+
let contentSha256 = existingState?.contentSha256 || void 0;
|
|
6424
|
+
const controller = new AbortController();
|
|
6425
|
+
const scopeVersion = getScopeVersion(), sessionGeneration2 = getSessionGeneration();
|
|
6426
|
+
attempts.current.get(uploadId)?.abort();
|
|
6427
|
+
attempts.current.set(uploadId, controller);
|
|
6381
6428
|
try {
|
|
6382
6429
|
if (!resumeFrom || resumeFrom === "create") {
|
|
6383
6430
|
updateUpload(uploadId, { status: "creating", error: null, _failedAt: null });
|
|
6431
|
+
const integrity = await prepareUploadIntegrity(file, controller.signal);
|
|
6432
|
+
checkUploadCancelled(controller.signal);
|
|
6433
|
+
if (scopeVersion !== getScopeVersion() || sessionGeneration2 !== getSessionGeneration()) {
|
|
6434
|
+
throw new Error("The account or Space changed while preparing the upload. Start it again.");
|
|
6435
|
+
}
|
|
6436
|
+
contentSha256 = integrity.expected_sha256;
|
|
6384
6437
|
const createResult = await gatewayCall("create", {
|
|
6385
6438
|
parent_path: options.parentPath,
|
|
6386
6439
|
item_type: itemType,
|
|
@@ -6391,10 +6444,12 @@ function useFileUpload() {
|
|
|
6391
6444
|
type: "upload",
|
|
6392
6445
|
content_type: contentType,
|
|
6393
6446
|
collision_policy: collisionPolicy,
|
|
6394
|
-
...collisionPolicy === "keep_both" ? { upload_id: uploadId } : {}
|
|
6447
|
+
...collisionPolicy === "keep_both" ? { upload_id: uploadId } : {},
|
|
6448
|
+
...integrity
|
|
6395
6449
|
}
|
|
6396
6450
|
}
|
|
6397
6451
|
});
|
|
6452
|
+
checkUploadCancelled(controller.signal);
|
|
6398
6453
|
const result = createResult;
|
|
6399
6454
|
if (!result.id) {
|
|
6400
6455
|
throw Object.assign(
|
|
@@ -6420,7 +6475,8 @@ function useFileUpload() {
|
|
|
6420
6475
|
updateUpload(uploadId, {
|
|
6421
6476
|
_storageKey: storageKey,
|
|
6422
6477
|
_uploadToken: uploadToken,
|
|
6423
|
-
_storageWorkerUrl: storageWorkerUrl
|
|
6478
|
+
_storageWorkerUrl: storageWorkerUrl,
|
|
6479
|
+
_contentSha256: contentSha256 || null
|
|
6424
6480
|
});
|
|
6425
6481
|
if (!storageKey) {
|
|
6426
6482
|
throw Object.assign(
|
|
@@ -6463,19 +6519,27 @@ function useFileUpload() {
|
|
|
6463
6519
|
new Error("Storage upload timed out"),
|
|
6464
6520
|
{ _step: "upload" }
|
|
6465
6521
|
));
|
|
6522
|
+
xhr.onabort = () => reject(Object.assign(new Error("Upload cancelled"), { _step: "upload" }));
|
|
6523
|
+
controller.signal.addEventListener("abort", () => xhr.abort(), { once: true });
|
|
6524
|
+
checkUploadCancelled(controller.signal);
|
|
6466
6525
|
xhr.send(file);
|
|
6467
6526
|
});
|
|
6468
6527
|
await xhrPromise;
|
|
6528
|
+
checkUploadCancelled(controller.signal);
|
|
6469
6529
|
updateUpload(uploadId, { progress: 100, _xhr: null });
|
|
6470
6530
|
}
|
|
6471
6531
|
updateUpload(uploadId, { status: "confirming", error: null, _failedAt: null });
|
|
6472
|
-
const fileRef = await confirmUpload(itemId, storageKey, contentType)
|
|
6532
|
+
const fileRef = await confirmUpload(itemId, storageKey, contentType, uploadToken || "", contentSha256, file.size).catch((error) => {
|
|
6533
|
+
throw Object.assign(error, { _step: "confirm" });
|
|
6534
|
+
});
|
|
6535
|
+
checkUploadCancelled(controller.signal);
|
|
6473
6536
|
updateUpload(uploadId, {
|
|
6474
6537
|
status: "complete",
|
|
6475
6538
|
fileRef
|
|
6476
6539
|
});
|
|
6477
6540
|
qc.invalidateQueries({ queryKey: vfsKeys.children(options.parentPath) });
|
|
6478
6541
|
} catch (err) {
|
|
6542
|
+
if (attempts.current.get(uploadId) !== controller) return;
|
|
6479
6543
|
const message = err?.message || String(err);
|
|
6480
6544
|
const failedAt = err?._step || "create";
|
|
6481
6545
|
updateUpload(uploadId, {
|
|
@@ -6484,6 +6548,8 @@ function useFileUpload() {
|
|
|
6484
6548
|
_failedAt: failedAt,
|
|
6485
6549
|
_xhr: null
|
|
6486
6550
|
});
|
|
6551
|
+
} finally {
|
|
6552
|
+
if (attempts.current.get(uploadId) === controller) attempts.current.delete(uploadId);
|
|
6487
6553
|
}
|
|
6488
6554
|
}, [updateUpload, qc]);
|
|
6489
6555
|
const upload = useCallback((file, options) => {
|
|
@@ -6501,6 +6567,7 @@ function useFileUpload() {
|
|
|
6501
6567
|
_storageKey: null,
|
|
6502
6568
|
_uploadToken: null,
|
|
6503
6569
|
_storageWorkerUrl: null,
|
|
6570
|
+
_contentSha256: null,
|
|
6504
6571
|
_xhr: null,
|
|
6505
6572
|
_failedAt: null
|
|
6506
6573
|
};
|
|
@@ -6521,11 +6588,13 @@ function useFileUpload() {
|
|
|
6521
6588
|
uploadToken: item._uploadToken || "",
|
|
6522
6589
|
storageWorkerUrl: item._storageWorkerUrl || "https://fsaos-storage.radns.workers.dev",
|
|
6523
6590
|
itemId: item.itemId,
|
|
6524
|
-
itemPath: item.itemPath || ""
|
|
6591
|
+
itemPath: item.itemPath || "",
|
|
6592
|
+
contentSha256: item._contentSha256
|
|
6525
6593
|
} : void 0;
|
|
6526
6594
|
executeUpload(uploadId, item.file, item.options, resumeFrom, existingState);
|
|
6527
6595
|
}, [executeUpload]);
|
|
6528
6596
|
const cancelUpload = useCallback((uploadId) => {
|
|
6597
|
+
attempts.current.get(uploadId)?.abort();
|
|
6529
6598
|
const item = uploadsRef.current.find((u) => u.id === uploadId);
|
|
6530
6599
|
if (!item) return;
|
|
6531
6600
|
if (item._xhr) {
|
|
@@ -6539,6 +6608,8 @@ function useFileUpload() {
|
|
|
6539
6608
|
});
|
|
6540
6609
|
}, [updateUpload]);
|
|
6541
6610
|
const removeUpload = useCallback((uploadId) => {
|
|
6611
|
+
attempts.current.get(uploadId)?.abort();
|
|
6612
|
+
attempts.current.delete(uploadId);
|
|
6542
6613
|
setUploads((prev) => prev.filter((u) => u.id !== uploadId));
|
|
6543
6614
|
}, []);
|
|
6544
6615
|
const clearCompleted = useCallback(() => {
|
|
@@ -6547,7 +6618,7 @@ function useFileUpload() {
|
|
|
6547
6618
|
const isUploading = uploads.some(
|
|
6548
6619
|
(u) => u.status === "creating" || u.status === "uploading" || u.status === "confirming"
|
|
6549
6620
|
);
|
|
6550
|
-
const publicUploads = uploads.map(({ _storageKey, _uploadToken, _storageWorkerUrl, _xhr, _failedAt, ...rest }) => rest);
|
|
6621
|
+
const publicUploads = uploads.map(({ _storageKey, _uploadToken, _storageWorkerUrl, _contentSha256, _xhr, _failedAt, ...rest }) => rest);
|
|
6551
6622
|
return {
|
|
6552
6623
|
upload,
|
|
6553
6624
|
uploadMultiple,
|