@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/ui.js CHANGED
@@ -1,3 +1,3 @@
1
- /* @fsaos/ui v2.0.0 — FSAOS UI Primitives — 2026-11-15T22:37:34.259Z */
1
+ /* @fsaos/ui v2.0.0 — FSAOS UI Primitives — 2026-11-15T22:46:38.568Z */
2
2
  "use strict";var __FSAOS_UI__=(()=>{var m=Object.create;var r=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var f=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var y=(e,i)=>()=>(i||e((i={exports:{}}).exports,i),i.exports),N=(e,i)=>{for(var a in i)r(e,a,{get:i[a],enumerable:!0})},s=(e,i,a,l)=>{if(i&&typeof i=="object"||typeof i=="function")for(let t of u(i))!h.call(e,t)&&t!==a&&r(e,t,{get:()=>i[t],enumerable:!(l=g(i,t))||l.enumerable});return e};var x=(e,i,a)=>(a=e!=null?m(f(e)):{},s(i||!e||!e.__esModule?r(a,"default",{value:e,enumerable:!0}):a,e)),b=e=>s(r({},"__esModule",{value:!0}),e);var d=y((V,c)=>{c.exports=React});var P={};N(P,{Badge:()=>z,Breadcrumbs:()=>A,Button:()=>D,Card:()=>w,DataView:()=>R,Detail:()=>_,Empty:()=>I,FilePreview:()=>O,Form:()=>F,Grid:()=>k,Image:()=>G,Link:()=>E,Page:()=>v,Router:()=>T,Sidebar:()=>S,Spinner:()=>B,Stack:()=>C,Table:()=>j,Tabs:()=>W,Video:()=>H});var n=x(d(),1);function o(){return(typeof window<"u"&&window.__FSAOS_CONFIG__||{}).edgeBaseUrl||""}function v(e){return(0,n.createElement)("div",{style:{minHeight:"100vh",display:"flex",flexDirection:"column"},className:e.className||""},e.children)}function C(e){let i=e.direction||"vertical";return(0,n.createElement)("div",{style:{display:"flex",flexDirection:i==="horizontal"?"row":"column",gap:e.gap||"0.5rem",padding:e.padding||"0",alignItems:e.align||"stretch",justifyContent:e.justify||"flex-start",flex:e.flex||"initial"},className:e.className||""},e.children)}function k(e){return(0,n.createElement)("div",{style:{display:"grid",gridTemplateColumns:"repeat("+(e.columns||3)+", 1fr)",gap:e.gap||"1rem",padding:e.padding||"0"},className:e.className||""},e.children)}function S(e){return(0,n.createElement)("div",{style:{display:"flex",flexDirection:"row",minHeight:"100vh"},className:e.className||""},e.children)}function w(e){return(0,n.createElement)("div",{style:{border:"1px solid var(--color-border, #e5e7eb)",borderRadius:"var(--border-radius, 0.5rem)",padding:e.padding||"1rem",background:"var(--color-surface, #fff)"},className:e.className||"",onClick:e.onClick},e.children)}function z(e){return(0,n.createElement)("span",{style:{display:"inline-flex",alignItems:"center",padding:"0.125rem 0.5rem",fontSize:"0.75rem",fontWeight:"500",borderRadius:"9999px",background:e.color||"#e5e7eb",color:e.textColor||"#374151"},className:e.className||""},e.children||e.label)}function B(e){let i=e.size||"1.5rem";return(0,n.createElement)("div",{style:{width:i,height:i,border:"2px solid #e5e7eb",borderTopColor:"var(--color-primary, #3b82f6)",borderRadius:"50%",animation:"fsaos-spin 0.6s linear infinite"}})}function D(e){return(0,n.createElement)("button",{style:{display:"inline-flex",alignItems:"center",justifyContent:"center",padding:"0.5rem 1rem",fontSize:"0.875rem",fontWeight:"500",borderRadius:"var(--border-radius, 0.375rem)",border:e.variant==="outline"?"1px solid var(--color-border, #d1d5db)":"none",background:e.variant==="outline"||e.variant==="ghost"?"transparent":"var(--color-primary, #3b82f6)",color:e.variant==="outline"||e.variant==="ghost"?"var(--color-text, #1a1a1a)":"#fff",cursor:e.disabled?"not-allowed":"pointer",opacity:e.disabled?"0.5":"1",gap:"0.5rem"},className:e.className||"",onClick:e.onClick,disabled:e.disabled},e.children||e.label)}function I(e){return(0,n.createElement)("div",{style:{display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",padding:"3rem",color:"var(--color-text-secondary, #6b7280)",textAlign:"center"}},e.icon?(0,n.createElement)("div",{style:{fontSize:"2rem",marginBottom:"0.5rem"}},e.icon):null,(0,n.createElement)("div",{style:{fontWeight:500,marginBottom:"0.25rem"}},e.title||e.message||e.children||"No items"),e.description?(0,n.createElement)("div",{style:{fontSize:"0.875rem",opacity:.7}},e.description):null)}function R(e){return(0,n.createElement)("div",{className:e.className||""},e.children)}function j(e){return(0,n.createElement)("table",{className:e.className||""},e.children)}function F(e){return(0,n.createElement)("form",{className:e.className||"",onSubmit:e.onSubmit},e.children)}function _(e){return(0,n.createElement)("div",{className:e.className||""},e.children)}function T(e){return(0,n.createElement)("div",null,e.children)}function E(e){return(0,n.createElement)("a",{href:e.href||e.to,className:e.className||"",onClick:e.onClick},e.children)}function W(e){return(0,n.createElement)("div",{className:e.className||""},e.children)}function A(e){return(0,n.createElement)("nav",{className:e.className||""},e.children)}function G(e){return(0,n.createElement)("img",{src:e.src||(e.path?o()+e.path:""),alt:e.alt||"",className:e.className||"",style:e.style})}function H(e){return(0,n.createElement)("video",{src:e.src||(e.path?o()+e.path:""),controls:!0,className:e.className||""})}function O(e){return(0,n.createElement)("div",{className:e.className||""},e.children||"File preview")}return b(P);})();
3
3
  //# sourceMappingURL=ui.js.map
package/dist/index.cjs CHANGED
@@ -6264,6 +6264,32 @@ function getRequiredFormFields(typeDef) {
6264
6264
  (f) => required.has(f.key) || f.default_value === void 0
6265
6265
  );
6266
6266
  }
6267
+
6268
+ // src/upload-integrity.ts
6269
+ var MAX_BUFFERED_HASH_BYTES = 16 * 1024 * 1024;
6270
+ var hashing = Promise.resolve();
6271
+ function checkUploadCancelled(signal2) {
6272
+ if (signal2?.aborted) throw new Error("Upload cancelled");
6273
+ }
6274
+ async function prepareUploadIntegrity(file, signal2) {
6275
+ checkUploadCancelled(signal2);
6276
+ if (file.size > MAX_BUFFERED_HASH_BYTES || !globalThis.crypto?.subtle) return {};
6277
+ const operation = hashing.then(async () => {
6278
+ checkUploadCancelled(signal2);
6279
+ const bytes = await file.arrayBuffer();
6280
+ checkUploadCancelled(signal2);
6281
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
6282
+ checkUploadCancelled(signal2);
6283
+ return {
6284
+ expected_sha256: Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""),
6285
+ expected_size_bytes: file.size
6286
+ };
6287
+ });
6288
+ hashing = operation.catch(() => void 0);
6289
+ return operation;
6290
+ }
6291
+
6292
+ // src/upload.ts
6267
6293
  function generateUploadId() {
6268
6294
  if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
6269
6295
  const bytes = new Uint8Array(16);
@@ -6285,6 +6311,12 @@ async function uploadFile(file, options, onProgress, signal2) {
6285
6311
  const itemType = options.itemType || "file";
6286
6312
  const collisionPolicy = options.collisionPolicy || "keep_both";
6287
6313
  const uploadId = collisionPolicy === "keep_both" ? generateUploadId() : void 0;
6314
+ const scopeVersion = getScopeVersion(), sessionGeneration2 = getSessionGeneration();
6315
+ const integrity = await prepareUploadIntegrity(file, signal2);
6316
+ checkUploadCancelled(signal2);
6317
+ if (scopeVersion !== getScopeVersion() || sessionGeneration2 !== getSessionGeneration()) {
6318
+ throw new Error("The account or Space changed while preparing the upload. Start it again.");
6319
+ }
6288
6320
  const createParams = {
6289
6321
  parent_path: options.parentPath,
6290
6322
  item_type: itemType,
@@ -6295,7 +6327,8 @@ async function uploadFile(file, options, onProgress, signal2) {
6295
6327
  type: "upload",
6296
6328
  content_type: contentType,
6297
6329
  collision_policy: collisionPolicy,
6298
- ...uploadId ? { upload_id: uploadId } : {}
6330
+ ...uploadId ? { upload_id: uploadId } : {},
6331
+ ...integrity
6299
6332
  }
6300
6333
  }
6301
6334
  };
@@ -6303,6 +6336,7 @@ async function uploadFile(file, options, onProgress, signal2) {
6303
6336
  createParams.card_parent_path = options.cardParentPath;
6304
6337
  }
6305
6338
  const createResult = await gatewayCall("create", createParams);
6339
+ checkUploadCancelled(signal2);
6306
6340
  const result = createResult;
6307
6341
  if (!result.id) {
6308
6342
  throw new Error(result.error || result.message || "Failed to create file item");
@@ -6319,7 +6353,8 @@ async function uploadFile(file, options, onProgress, signal2) {
6319
6353
  throw new Error("Gateway returned no storage_key \u2014 file_source may not be supported for this item type");
6320
6354
  }
6321
6355
  await uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, file, contentType, onProgress, signal2);
6322
- const fileRef = await confirmUpload(itemId, storageKey, contentType);
6356
+ checkUploadCancelled(signal2);
6357
+ const fileRef = await confirmUpload(itemId, storageKey, contentType, uploadToken, integrity.expected_sha256, file.size);
6323
6358
  return { itemId, itemPath, fileRef };
6324
6359
  }
6325
6360
  function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, contentType, onProgress, signal2) {
@@ -6345,6 +6380,7 @@ function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, conten
6345
6380
  };
6346
6381
  xhr.onerror = () => reject(new Error("Storage upload network error"));
6347
6382
  xhr.ontimeout = () => reject(new Error("Storage upload timed out"));
6383
+ xhr.onabort = () => reject(new Error("Upload cancelled"));
6348
6384
  if (signal2) {
6349
6385
  if (signal2.aborted) {
6350
6386
  reject(new Error("Upload cancelled"));
@@ -6358,7 +6394,7 @@ function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, conten
6358
6394
  xhr.send(body);
6359
6395
  });
6360
6396
  }
6361
- async function confirmUpload(itemId, storageKey, contentType) {
6397
+ async function confirmUpload(itemId, storageKey, contentType, uploadToken, contentSha256, sizeBytes) {
6362
6398
  const token = await getAccessToken();
6363
6399
  const response = await fetch(`${GATEWAY_URL}/storage/upload-complete`, {
6364
6400
  method: "POST",
@@ -6369,7 +6405,9 @@ async function confirmUpload(itemId, storageKey, contentType) {
6369
6405
  body: JSON.stringify({
6370
6406
  item_id: itemId,
6371
6407
  storage_key: storageKey,
6372
- content_type: contentType
6408
+ content_type: contentType,
6409
+ ...uploadToken ? { upload_token: uploadToken } : {},
6410
+ ...contentSha256 ? { hash: contentSha256 } : {}
6373
6411
  })
6374
6412
  });
6375
6413
  if (!response.ok) {
@@ -6380,6 +6418,9 @@ async function confirmUpload(itemId, storageKey, contentType) {
6380
6418
  if (!result.success) {
6381
6419
  throw new Error(result.error || result.message || "Upload confirmation rejected");
6382
6420
  }
6421
+ if (contentSha256 && (result.file_ref?.content_sha256 !== contentSha256 || result.file_ref?.size_bytes !== sizeBytes || result.file_ref?.content_type !== contentType)) {
6422
+ throw new Error("Stored file did not confirm the expected checksum, size and type");
6423
+ }
6383
6424
  return result.file_ref;
6384
6425
  }
6385
6426
  function useFileUpload() {
@@ -6387,6 +6428,7 @@ function useFileUpload() {
6387
6428
  const qc = reactQuery.useQueryClient();
6388
6429
  const uploadsRef = React.useRef(uploads);
6389
6430
  uploadsRef.current = uploads;
6431
+ const attempts = React.useRef(/* @__PURE__ */ new Map());
6390
6432
  const updateUpload = React.useCallback((id, patch) => {
6391
6433
  setUploads((prev) => prev.map((u) => u.id === id ? { ...u, ...patch } : u));
6392
6434
  }, []);
@@ -6403,9 +6445,20 @@ function useFileUpload() {
6403
6445
  let storageKey = existingState?.storageKey || null;
6404
6446
  let uploadToken = existingState?.uploadToken || null;
6405
6447
  let storageWorkerUrl = existingState?.storageWorkerUrl || null;
6448
+ let contentSha256 = existingState?.contentSha256 || void 0;
6449
+ const controller = new AbortController();
6450
+ const scopeVersion = getScopeVersion(), sessionGeneration2 = getSessionGeneration();
6451
+ attempts.current.get(uploadId)?.abort();
6452
+ attempts.current.set(uploadId, controller);
6406
6453
  try {
6407
6454
  if (!resumeFrom || resumeFrom === "create") {
6408
6455
  updateUpload(uploadId, { status: "creating", error: null, _failedAt: null });
6456
+ const integrity = await prepareUploadIntegrity(file, controller.signal);
6457
+ checkUploadCancelled(controller.signal);
6458
+ if (scopeVersion !== getScopeVersion() || sessionGeneration2 !== getSessionGeneration()) {
6459
+ throw new Error("The account or Space changed while preparing the upload. Start it again.");
6460
+ }
6461
+ contentSha256 = integrity.expected_sha256;
6409
6462
  const createResult = await gatewayCall("create", {
6410
6463
  parent_path: options.parentPath,
6411
6464
  item_type: itemType,
@@ -6416,10 +6469,12 @@ function useFileUpload() {
6416
6469
  type: "upload",
6417
6470
  content_type: contentType,
6418
6471
  collision_policy: collisionPolicy,
6419
- ...collisionPolicy === "keep_both" ? { upload_id: uploadId } : {}
6472
+ ...collisionPolicy === "keep_both" ? { upload_id: uploadId } : {},
6473
+ ...integrity
6420
6474
  }
6421
6475
  }
6422
6476
  });
6477
+ checkUploadCancelled(controller.signal);
6423
6478
  const result = createResult;
6424
6479
  if (!result.id) {
6425
6480
  throw Object.assign(
@@ -6445,7 +6500,8 @@ function useFileUpload() {
6445
6500
  updateUpload(uploadId, {
6446
6501
  _storageKey: storageKey,
6447
6502
  _uploadToken: uploadToken,
6448
- _storageWorkerUrl: storageWorkerUrl
6503
+ _storageWorkerUrl: storageWorkerUrl,
6504
+ _contentSha256: contentSha256 || null
6449
6505
  });
6450
6506
  if (!storageKey) {
6451
6507
  throw Object.assign(
@@ -6488,19 +6544,27 @@ function useFileUpload() {
6488
6544
  new Error("Storage upload timed out"),
6489
6545
  { _step: "upload" }
6490
6546
  ));
6547
+ xhr.onabort = () => reject(Object.assign(new Error("Upload cancelled"), { _step: "upload" }));
6548
+ controller.signal.addEventListener("abort", () => xhr.abort(), { once: true });
6549
+ checkUploadCancelled(controller.signal);
6491
6550
  xhr.send(file);
6492
6551
  });
6493
6552
  await xhrPromise;
6553
+ checkUploadCancelled(controller.signal);
6494
6554
  updateUpload(uploadId, { progress: 100, _xhr: null });
6495
6555
  }
6496
6556
  updateUpload(uploadId, { status: "confirming", error: null, _failedAt: null });
6497
- const fileRef = await confirmUpload(itemId, storageKey, contentType);
6557
+ const fileRef = await confirmUpload(itemId, storageKey, contentType, uploadToken || "", contentSha256, file.size).catch((error) => {
6558
+ throw Object.assign(error, { _step: "confirm" });
6559
+ });
6560
+ checkUploadCancelled(controller.signal);
6498
6561
  updateUpload(uploadId, {
6499
6562
  status: "complete",
6500
6563
  fileRef
6501
6564
  });
6502
6565
  qc.invalidateQueries({ queryKey: vfsKeys.children(options.parentPath) });
6503
6566
  } catch (err) {
6567
+ if (attempts.current.get(uploadId) !== controller) return;
6504
6568
  const message = err?.message || String(err);
6505
6569
  const failedAt = err?._step || "create";
6506
6570
  updateUpload(uploadId, {
@@ -6509,6 +6573,8 @@ function useFileUpload() {
6509
6573
  _failedAt: failedAt,
6510
6574
  _xhr: null
6511
6575
  });
6576
+ } finally {
6577
+ if (attempts.current.get(uploadId) === controller) attempts.current.delete(uploadId);
6512
6578
  }
6513
6579
  }, [updateUpload, qc]);
6514
6580
  const upload = React.useCallback((file, options) => {
@@ -6526,6 +6592,7 @@ function useFileUpload() {
6526
6592
  _storageKey: null,
6527
6593
  _uploadToken: null,
6528
6594
  _storageWorkerUrl: null,
6595
+ _contentSha256: null,
6529
6596
  _xhr: null,
6530
6597
  _failedAt: null
6531
6598
  };
@@ -6546,11 +6613,13 @@ function useFileUpload() {
6546
6613
  uploadToken: item._uploadToken || "",
6547
6614
  storageWorkerUrl: item._storageWorkerUrl || "https://fsaos-storage.radns.workers.dev",
6548
6615
  itemId: item.itemId,
6549
- itemPath: item.itemPath || ""
6616
+ itemPath: item.itemPath || "",
6617
+ contentSha256: item._contentSha256
6550
6618
  } : void 0;
6551
6619
  executeUpload(uploadId, item.file, item.options, resumeFrom, existingState);
6552
6620
  }, [executeUpload]);
6553
6621
  const cancelUpload = React.useCallback((uploadId) => {
6622
+ attempts.current.get(uploadId)?.abort();
6554
6623
  const item = uploadsRef.current.find((u) => u.id === uploadId);
6555
6624
  if (!item) return;
6556
6625
  if (item._xhr) {
@@ -6564,6 +6633,8 @@ function useFileUpload() {
6564
6633
  });
6565
6634
  }, [updateUpload]);
6566
6635
  const removeUpload = React.useCallback((uploadId) => {
6636
+ attempts.current.get(uploadId)?.abort();
6637
+ attempts.current.delete(uploadId);
6567
6638
  setUploads((prev) => prev.filter((u) => u.id !== uploadId));
6568
6639
  }, []);
6569
6640
  const clearCompleted = React.useCallback(() => {
@@ -6572,7 +6643,7 @@ function useFileUpload() {
6572
6643
  const isUploading = uploads.some(
6573
6644
  (u) => u.status === "creating" || u.status === "uploading" || u.status === "confirming"
6574
6645
  );
6575
- const publicUploads = uploads.map(({ _storageKey, _uploadToken, _storageWorkerUrl, _xhr, _failedAt, ...rest }) => rest);
6646
+ const publicUploads = uploads.map(({ _storageKey, _uploadToken, _storageWorkerUrl, _contentSha256, _xhr, _failedAt, ...rest }) => rest);
6576
6647
  return {
6577
6648
  upload,
6578
6649
  uploadMultiple,