@arbidocs/sdk 0.5.68 → 0.5.69

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
@@ -6022,16 +6022,40 @@ async function updateDocuments(arbi, documents) {
6022
6022
  "Failed to update documents"
6023
6023
  );
6024
6024
  }
6025
- async function uploadUrl(arbi, urls, shared = false, configExtId) {
6026
- return requireData(
6025
+ async function uploadUrl(arbi, urls, options) {
6026
+ return requireUploadData(
6027
6027
  await arbi.fetch.POST("/v1/document/upload-url", {
6028
6028
  params: {
6029
- query: { urls, shared, ...configExtId ? { config_ext_id: configExtId } : {} }
6029
+ query: {
6030
+ urls,
6031
+ shared: options.shared ?? false,
6032
+ workspace_ext_id: options.workspaceExtId,
6033
+ ...options.configExtId ? { config_ext_id: options.configExtId } : {}
6034
+ }
6030
6035
  }
6031
6036
  }),
6032
6037
  "Failed to upload from URLs"
6033
6038
  );
6034
6039
  }
6040
+ var UPLOAD_WORKSPACE_CONFLICT_MESSAGES = {
6041
+ workspace_mismatch: "These files were prepared for another workspace, so they were not saved. Upload them again.",
6042
+ workspace_key_required: "The workspace couldn't be unlocked for this upload, so the files were not saved."
6043
+ };
6044
+ function requireUploadData(result, message) {
6045
+ const status = result.response?.status;
6046
+ const conflict = result.error && status === 409 ? workspaceConflictOf(result.error) : null;
6047
+ if (conflict) {
6048
+ throw new ArbiApiError(
6049
+ UPLOAD_WORKSPACE_CONFLICT_MESSAGES[conflict.code],
6050
+ result.error,
6051
+ status,
6052
+ {
6053
+ code: conflict.code
6054
+ }
6055
+ );
6056
+ }
6057
+ return requireData(result, message);
6058
+ }
6035
6059
  async function uploadDocuments(arbi, files, options = {}) {
6036
6060
  const query2 = {};
6037
6061
  if (options.shared !== void 0) query2.shared = options.shared;
@@ -6056,7 +6080,7 @@ async function uploadDocuments(arbi, files, options = {}) {
6056
6080
  return form;
6057
6081
  }
6058
6082
  });
6059
- return requireData(result, "Failed to upload documents");
6083
+ return requireUploadData(result, "Failed to upload documents");
6060
6084
  }
6061
6085
  async function similarDocuments(arbi, options = {}) {
6062
6086
  return requireData(
@@ -6213,6 +6237,9 @@ async function presignedPut(url, body, onProgress, headers) {
6213
6237
  }
6214
6238
  }
6215
6239
  async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6240
+ if (!options?.workspaceExtId) {
6241
+ throw new ArbiError("uploadDocumentsDirect needs options.workspaceExtId (the key's workspace)");
6242
+ }
6216
6243
  if (files.length === 0) return { doc_ext_ids: [], skipped: [] };
6217
6244
  const SECRETBOX_ENVELOPE_OVERHEAD = 40;
6218
6245
  const declared = files.map((f2) => ({
@@ -6224,21 +6251,19 @@ async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6224
6251
  }));
6225
6252
  const initBody = {
6226
6253
  files: declared,
6227
- shared: options?.shared !== false,
6228
- config_ext_id: options?.configExtId ?? null,
6254
+ shared: options.shared !== false,
6255
+ config_ext_id: options.configExtId ?? null,
6229
6256
  // Only sent when set, so a plain upload is accepted by any server version.
6230
- ...options?.versionOf ? { version_of: options.versionOf } : {},
6231
- wp_type: options?.wpType ?? null,
6232
- tag_ext_id: options?.tagExtId ?? null,
6233
- folder: options?.folder ? sanitizeFolderPath(options.folder) : null,
6234
- presign_expires_in: options?.presignExpiresIn ?? 3600
6257
+ ...options.versionOf ? { version_of: options.versionOf } : {},
6258
+ wp_type: options.wpType ?? null,
6259
+ tag_ext_id: options.tagExtId ?? null,
6260
+ folder: options.folder ? sanitizeFolderPath(options.folder) : null,
6261
+ presign_expires_in: options.presignExpiresIn ?? 3600
6235
6262
  };
6236
- const guarded = options?.inWorkspace ?? ((send) => send());
6237
- const initResult = await guarded(
6238
- async () => requireData(
6239
- await arbi.fetch.POST("/v1/document/upload-init", { body: initBody }),
6240
- "Failed to initialize direct upload"
6241
- )
6263
+ const query2 = { workspace_ext_id: options.workspaceExtId };
6264
+ const initResult = requireUploadData(
6265
+ await arbi.fetch.POST("/v1/document/upload-init", { params: { query: query2 }, body: initBody }),
6266
+ "Failed to initialize direct upload"
6242
6267
  );
6243
6268
  const items = initResult.items ?? [];
6244
6269
  const bytesByName = /* @__PURE__ */ new Map();
@@ -6272,7 +6297,7 @@ async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6272
6297
  const total = ciphertext.length;
6273
6298
  try {
6274
6299
  await presignedPut(item.upload_url, ciphertext, (loaded, putTotal) => {
6275
- options?.onBytesProgress?.({
6300
+ options.onBytesProgress?.({
6276
6301
  fileIndex,
6277
6302
  fileName: item.file_name,
6278
6303
  loaded,
@@ -6284,7 +6309,7 @@ async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6284
6309
  `Presigned PUT for ${item.doc_ext_id} failed: ${err instanceof Error ? err.message : String(err)}`
6285
6310
  );
6286
6311
  }
6287
- options?.onBytesProgress?.({
6312
+ options.onBytesProgress?.({
6288
6313
  fileIndex,
6289
6314
  fileName: item.file_name,
6290
6315
  loaded: total,
@@ -6295,16 +6320,15 @@ async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6295
6320
  if (toCommit.length === 0) {
6296
6321
  return { doc_ext_ids: [], skipped };
6297
6322
  }
6298
- const commitResult = await guarded(
6299
- async () => requireData(
6300
- await arbi.fetch.POST("/v1/document/upload-commit", {
6301
- body: {
6302
- doc_ext_ids: toCommit,
6303
- tag_ext_id: options?.tagExtId ?? null
6304
- }
6305
- }),
6306
- "Failed to commit direct upload"
6307
- )
6323
+ const commitResult = requireUploadData(
6324
+ await arbi.fetch.POST("/v1/document/upload-commit", {
6325
+ params: { query: query2 },
6326
+ body: {
6327
+ doc_ext_ids: toCommit,
6328
+ tag_ext_id: options.tagExtId ?? null
6329
+ }
6330
+ }),
6331
+ "Failed to commit direct upload"
6308
6332
  );
6309
6333
  return {
6310
6334
  doc_ext_ids: commitResult.doc_ext_ids ?? [],
@@ -11799,6 +11823,10 @@ var Arbi = class {
11799
11823
  if (target) await this.openWorkspace(target);
11800
11824
  return target;
11801
11825
  }
11826
+ /** {@link targetWorkspace} for a call that cannot go without one: throws when none is known. */
11827
+ async requireTargetWorkspace(workspaceId) {
11828
+ return await this.targetWorkspace(workspaceId) ?? this.requireWorkspace();
11829
+ }
11802
11830
  /**
11803
11831
  * Make a workspace the CURRENT one: decrypt its key, open it on the server (which makes it the
11804
11832
  * session's active workspace) and record it in the session. This is navigation: only the host's
@@ -12412,20 +12440,23 @@ var Arbi = class {
12412
12440
  finaliseTranslation: (docId) => finaliseTranslation(this.requireClient(), docId),
12413
12441
  delete: (externalIds) => deleteDocuments(this.requireClient(), externalIds),
12414
12442
  update: (documents) => updateDocuments(this.requireClient(), documents),
12415
- uploadUrl: (urls, options) => {
12416
- this.requireWorkspace();
12417
- return uploadUrl(
12418
- this.requireClient(),
12419
- urls,
12420
- options?.shared,
12421
- options?.configExtId
12422
- );
12443
+ /** Make documents from web addresses in the current workspace, or `options.workspaceId`. */
12444
+ uploadUrl: async (urls, options) => {
12445
+ const { workspaceId, ...rest } = options ?? {};
12446
+ return uploadUrl(this.requireClient(), urls, {
12447
+ workspaceExtId: await this.requireTargetWorkspace(workspaceId),
12448
+ ...rest
12449
+ });
12423
12450
  },
12424
12451
  /**
12425
- * Upload files in one request (the open workspace, or `options.workspaceExtId`): folder, tag,
12426
- * sharing, config, a new version of a document.
12452
+ * Upload files in one request into the current workspace, or `options.workspaceExtId`: folder,
12453
+ * tag, sharing, config, a new version of a document. The request names the workspace either
12454
+ * way, so a switch while it is in flight cannot move it.
12427
12455
  */
12428
- upload: (files, options) => uploadDocuments(this.requireClient(), files, options),
12456
+ upload: async (files, options) => uploadDocuments(this.requireClient(), files, {
12457
+ ...options,
12458
+ workspaceExtId: await this.requireTargetWorkspace(options?.workspaceExtId ?? void 0)
12459
+ }),
12429
12460
  /** Near-duplicate pairs at or above `threshold` (or one document's, `docId`). */
12430
12461
  similar: (options) => similarDocuments(this.requireClient(), options),
12431
12462
  /**
@@ -12450,19 +12481,15 @@ var Arbi = class {
12450
12481
  },
12451
12482
  /**
12452
12483
  * Upload files straight to storage, encrypted here with `workspaceKey` (`uploadDocumentsDirect`),
12453
- * into `workspaceId`, which must be the workspace open NOW. `upload-init` and `upload-commit`
12454
- * name no workspace yet (they act on the session's open one), so each runs under
12455
- * `readInWorkspace`: it is not sent, or its answer not kept, unless `workspaceId` is open when
12456
- * it is sent and still when it returns. A switch refuses the upload
12457
- * (`ArbiWorkspaceChangedError`) instead of storing files encrypted with one workspace's key in
12458
- * another. For any other workspace use `upload` with `workspaceExtId`, which names it.
12484
+ * into `workspaceId`, whose key it must be. `upload-init` and `upload-commit` name that
12485
+ * workspace (made usable for this session first, not switched to), so a switch while the
12486
+ * upload runs cannot move the files into another workspace.
12459
12487
  */
12460
- uploadDirect: (workspaceId, workspaceKey, files, options) => {
12461
- if (this.currentWorkspaceId !== workspaceId)
12462
- return Promise.reject(new ArbiWorkspaceChangedError(workspaceId, "uploading to"));
12488
+ uploadDirect: async (workspaceId, workspaceKey, files, options) => {
12489
+ const workspaceExtId = await this.requireTargetWorkspace(workspaceId);
12463
12490
  return uploadDocumentsDirect(this.requireClient(), workspaceKey, files, {
12464
12491
  ...options,
12465
- inWorkspace: (send) => this.readInWorkspace(workspaceId, send)
12492
+ workspaceExtId
12466
12493
  });
12467
12494
  },
12468
12495
  /** Download a document's original file — its current version, or `version`. */
@@ -13829,7 +13856,8 @@ async function runGroupedDirectUpload(arbi, workspaceKey, groups, options) {
13829
13856
  options?.onBatchStart?.(progress);
13830
13857
  const result = await uploadDocumentsDirect(arbi, workspaceKey, group.files, {
13831
13858
  folder: group.folder || void 0,
13832
- configExtId: options?.configExtId
13859
+ configExtId: options.configExtId,
13860
+ workspaceExtId: options.workspaceExtId
13833
13861
  });
13834
13862
  const skipped = result.skipped ?? [];
13835
13863
  allDocIds.push(...result.doc_ext_ids);