@arbidocs/sdk 0.5.137 → 0.5.139

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/index.cjs CHANGED
@@ -5078,6 +5078,15 @@ function createDocumentWaiter(options) {
5078
5078
  function isNotification(msg) {
5079
5079
  return "sender" in msg && "recipient" in msg;
5080
5080
  }
5081
+ function uploadSetTotals(content) {
5082
+ let meta = {};
5083
+ try {
5084
+ meta = JSON.parse(content || "{}") ?? {};
5085
+ } catch {
5086
+ }
5087
+ const count = (key) => typeof meta[key] === "number" ? meta[key] : 0;
5088
+ return { total: count("total"), completed: count("completed"), failed: count("failed") };
5089
+ }
5081
5090
  function formatWsMessage(msg) {
5082
5091
  if (client.isMessageType(msg, "task_update")) {
5083
5092
  const progress = msg.progress > 0 ? ` (${Math.round(msg.progress)}%)` : "";
@@ -5090,7 +5099,8 @@ function formatWsMessage(msg) {
5090
5099
  const count = msg.doc_ext_ids.length;
5091
5100
  return {
5092
5101
  text: `Batch complete: ${msg.batch_type} \u2014 ${count} doc${count !== 1 ? "s" : ""}`,
5093
- level: "success"
5102
+ // A batch of an upload set is progress: the set's one notification says when it is done.
5103
+ level: msg.upload_set_id ? "info" : "success"
5094
5104
  };
5095
5105
  }
5096
5106
  if (client.isMessageType(msg, "error")) {
@@ -5109,6 +5119,11 @@ function formatWsMessage(msg) {
5109
5119
  if (msg.type === "chat.message") {
5110
5120
  return { text: `New message in ${msg.chat_ext_id}`, level: "info" };
5111
5121
  }
5122
+ if (isNotification(msg) && msg.type === "upload_set_complete") {
5123
+ const totals = uploadSetTotals(msg.content);
5124
+ const text2 = totals.completed === totals.total ? `Upload done: ${totals.completed} doc${totals.completed !== 1 ? "s" : ""} indexed` : `Upload done: ${totals.completed} of ${totals.total} docs indexed` + (totals.failed ? `, ${totals.failed} failed` : "");
5125
+ return { text: text2, level: totals.failed || totals.completed < totals.total ? "warning" : "success" };
5126
+ }
5112
5127
  if (isNotification(msg)) {
5113
5128
  const sender = msg.sender?.email || "someone";
5114
5129
  const content = msg.content ?? msg.type;
@@ -6364,6 +6379,7 @@ __export(documents_exports, {
6364
6379
  listPaginated: () => listPaginated,
6365
6380
  listVersions: () => listVersions,
6366
6381
  mimeFromName: () => mimeFromName,
6382
+ newUploadSet: () => newUploadSet,
6367
6383
  presignedPut: () => presignedPut,
6368
6384
  restoreVersion: () => restoreVersion,
6369
6385
  sanitizeFolderPath: () => sanitizeFolderPath,
@@ -6373,6 +6389,7 @@ __export(documents_exports, {
6373
6389
  uploadDocumentsDirect: () => uploadDocumentsDirect,
6374
6390
  uploadFile: () => uploadFile,
6375
6391
  uploadFiles: () => uploadFiles,
6392
+ uploadSetQuery: () => uploadSetQuery,
6376
6393
  uploadUrl: () => uploadUrl
6377
6394
  });
6378
6395
  var MAX_PAGES = 400;
@@ -6422,6 +6439,17 @@ var SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([
6422
6439
  function sanitizeFolderPath(folderPath) {
6423
6440
  return folderPath.replace(/[\x00-\x1f\x7f]/g, "").replace(/\\/g, "/").replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "").trim();
6424
6441
  }
6442
+ function newUploadSet(size) {
6443
+ const crypto2 = globalThis.crypto;
6444
+ const id = typeof crypto2?.randomUUID === "function" ? crypto2.randomUUID() : Array.from(
6445
+ crypto2.getRandomValues(new Uint8Array(16)),
6446
+ (b2) => b2.toString(16).padStart(2, "0")
6447
+ ).join("");
6448
+ return { id, size: Math.max(1, Math.floor(size)) };
6449
+ }
6450
+ function uploadSetQuery(set) {
6451
+ return set ? { upload_set_id: set.id, upload_set_size: set.size } : {};
6452
+ }
6425
6453
  async function listDocuments(arbi, workspaceExtId) {
6426
6454
  if (!workspaceExtId) {
6427
6455
  return requireData(await arbi.fetch.GET("/v1/document/list"), "Failed to fetch documents");
@@ -6604,6 +6632,7 @@ async function uploadDocuments(arbi, files, options = {}) {
6604
6632
  query2.derived_from = options.derivedFrom.docId;
6605
6633
  query2.derived_role = options.derivedFrom.role;
6606
6634
  }
6635
+ Object.assign(query2, uploadSetQuery(options.uploadSet));
6607
6636
  const result = await arbi.fetch.POST("/v1/document/upload", {
6608
6637
  params: { query: query2 },
6609
6638
  ...options.onProgress ? { fetch: progressFetch(options.onProgress, arbi.http) } : {},
@@ -6664,6 +6693,8 @@ async function uploadFile(auth, fileData, fileName, options) {
6664
6693
  params.set("derived_from", options.derivedFrom.docId);
6665
6694
  params.set("derived_role", options.derivedFrom.role);
6666
6695
  }
6696
+ for (const [key, value] of Object.entries(uploadSetQuery(options?.uploadSet)))
6697
+ params.set(key, String(value));
6667
6698
  const qs = params.toString();
6668
6699
  const res = await authenticatedFetch({
6669
6700
  ...auth,
@@ -6686,6 +6717,8 @@ async function uploadFiles(auth, files, options) {
6686
6717
  params.set("derived_from", options.derivedFrom.docId);
6687
6718
  params.set("derived_role", options.derivedFrom.role);
6688
6719
  }
6720
+ for (const [key, value] of Object.entries(uploadSetQuery(options?.uploadSet)))
6721
+ params.set(key, String(value));
6689
6722
  const qs = params.toString();
6690
6723
  const res = await authenticatedFetch({
6691
6724
  ...auth,
@@ -6759,7 +6792,8 @@ async function presignedPut(url, body, onProgress, headers) {
6759
6792
  });
6760
6793
  xhr.addEventListener("error", () => reject(new Error("Presigned PUT network error")));
6761
6794
  xhr.addEventListener("abort", () => reject(new Error("Presigned PUT aborted")));
6762
- xhr.send(body);
6795
+ const BlobGlobal = globalThis.Blob;
6796
+ xhr.send(BlobGlobal ? new BlobGlobal([body]) : body);
6763
6797
  });
6764
6798
  return;
6765
6799
  }
@@ -6773,19 +6807,24 @@ async function presignedPut(url, body, onProgress, headers) {
6773
6807
  throw new Error(`Presigned PUT failed: ${putRes.status} ${snippet.slice(0, 200)}`);
6774
6808
  }
6775
6809
  }
6810
+ var yieldToEventLoop = () => new Promise((resolve2) => setTimeout(resolve2, 0));
6776
6811
  async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6777
6812
  if (!options?.workspaceExtId) {
6778
6813
  throw new ArbiError("uploadDocumentsDirect needs options.workspaceExtId (the key's workspace)");
6779
6814
  }
6780
6815
  if (files.length === 0) return { doc_ext_ids: [], skipped: [] };
6781
6816
  const SECRETBOX_ENVELOPE_OVERHEAD = 40;
6782
- const declared = files.map((f2) => ({
6783
- file_name: f2.name,
6784
- file_size: f2.data.length,
6785
- encrypted_file_size: f2.data.length + SECRETBOX_ENVELOPE_OVERHEAD,
6786
- content_hash: client.createContentHash(f2.data, workspaceKey),
6787
- content_type: f2.contentType ?? null
6788
- }));
6817
+ const declared = [];
6818
+ for (const f2 of files) {
6819
+ declared.push({
6820
+ file_name: f2.name,
6821
+ file_size: f2.data.length,
6822
+ encrypted_file_size: f2.data.length + SECRETBOX_ENVELOPE_OVERHEAD,
6823
+ content_hash: client.createContentHash(f2.data, workspaceKey),
6824
+ content_type: f2.contentType ?? null
6825
+ });
6826
+ await yieldToEventLoop();
6827
+ }
6789
6828
  const initBody = {
6790
6829
  files: declared,
6791
6830
  shared: options.shared !== false,
@@ -6797,7 +6836,7 @@ async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6797
6836
  folder: options.folder ? sanitizeFolderPath(options.folder) : null,
6798
6837
  presign_expires_in: options.presignExpiresIn ?? 3600
6799
6838
  };
6800
- const query2 = { workspace_ext_id: options.workspaceExtId };
6839
+ const query2 = { workspace_ext_id: options.workspaceExtId, ...uploadSetQuery(options.uploadSet) };
6801
6840
  const initResult = requireUploadData(
6802
6841
  await arbi.fetch.POST("/v1/document/upload-init", { params: { query: query2 }, body: initBody }),
6803
6842
  "Failed to initialize direct upload"
@@ -14786,6 +14825,7 @@ async function runGroupedDirectUpload(arbi, workspaceKey, groups, options) {
14786
14825
  const folders = /* @__PURE__ */ new Map();
14787
14826
  const totalFiles = groups.reduce((s2, g2) => s2 + g2.files.length, 0);
14788
14827
  const totalBatches = groups.length;
14828
+ const uploadSet = newUploadSet(totalFiles);
14789
14829
  let currentBatch = 0;
14790
14830
  let filesUploaded = 0;
14791
14831
  for (const group of groups) {
@@ -14802,7 +14842,8 @@ async function runGroupedDirectUpload(arbi, workspaceKey, groups, options) {
14802
14842
  const result = await uploadDocumentsDirect(arbi, workspaceKey, group.files, {
14803
14843
  folder: group.folder || void 0,
14804
14844
  configExtId: options.configExtId,
14805
- workspaceExtId: options.workspaceExtId
14845
+ workspaceExtId: options.workspaceExtId,
14846
+ uploadSet
14806
14847
  });
14807
14848
  const skipped = result.skipped ?? [];
14808
14849
  allDocIds.push(...result.doc_ext_ids);
@@ -14950,6 +14991,9 @@ async function uploadDirectory(auth, dirPath, options) {
14950
14991
  }
14951
14992
  let currentBatch = 0;
14952
14993
  let filesUploaded = 0;
14994
+ const uploadSet = newUploadSet(
14995
+ [...groups.values()].flat().filter((f2) => f2.size <= MAX_FILE_SIZE_BYTES).length
14996
+ );
14953
14997
  for (const [folder, batches] of folderBatches) {
14954
14998
  const folderDocIds = [];
14955
14999
  const folderSkipped = [];
@@ -14989,7 +15033,8 @@ async function uploadDirectory(auth, dirPath, options) {
14989
15033
  if (uploadable.length > 0) {
14990
15034
  result = await uploadFiles(auth, uploadable, {
14991
15035
  folder,
14992
- configExtId: options?.configExtId
15036
+ configExtId: options?.configExtId,
15037
+ uploadSet
14993
15038
  });
14994
15039
  }
14995
15040
  const skipped = [...batchSkipped, ...result.skipped ?? []];
@@ -15057,6 +15102,7 @@ async function uploadZip(auth, zipPath, options) {
15057
15102
  }
15058
15103
  let currentBatch = 0;
15059
15104
  let filesUploaded = 0;
15105
+ const uploadSet = newUploadSet(entries.length);
15060
15106
  for (const [folder, batches] of folderBatches) {
15061
15107
  const folderDocIds = [];
15062
15108
  const folderSkipped = [];
@@ -15079,7 +15125,8 @@ async function uploadZip(auth, zipPath, options) {
15079
15125
  }));
15080
15126
  const result = await uploadFiles(auth, blobs, {
15081
15127
  folder,
15082
- configExtId: options?.configExtId
15128
+ configExtId: options?.configExtId,
15129
+ uploadSet
15083
15130
  });
15084
15131
  const skipped = result.skipped ?? [];
15085
15132
  folderDocIds.push(...result.doc_ext_ids ?? []);
@@ -15245,6 +15292,7 @@ async function uploadManifest(auth, paths, options) {
15245
15292
  groups.get(pf.folder).push(pf);
15246
15293
  }
15247
15294
  const totalBytes = prepared.reduce((s2, p2) => s2 + p2.size, 0);
15295
+ const uploadSet = newUploadSet(prepared.length);
15248
15296
  let done = outcomes.length;
15249
15297
  let bytesDone = 0;
15250
15298
  for (const [folder, files] of groups) {
@@ -15298,7 +15346,7 @@ async function uploadManifest(auth, paths, options) {
15298
15346
  result = await uploadFiles(
15299
15347
  auth,
15300
15348
  readable.map((r2) => ({ data: r2.blob, name: r2.f.basename })),
15301
- { folder, configExtId: options?.configExtId }
15349
+ { folder, configExtId: options?.configExtId, uploadSet }
15302
15350
  );
15303
15351
  lastErr = void 0;
15304
15352
  break;