calmlens 0.18.3 → 0.20.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.
Files changed (82) hide show
  1. package/bin/calmlens.js +5682 -5036
  2. package/cjs/{BrowserUpload-BT091eOB.d.ts → BrowserUpload-C07aL9Vq.d.ts} +2 -2
  3. package/cjs/{BrowserUpload-UxIAGNdv.js → BrowserUpload-CjSHjx3C.js} +1 -1
  4. package/{esm/CalmLensAppClient-CV_K26Ce.d.ts → cjs/CalmLensAppClient-DDLojj0P.d.ts} +3 -3
  5. package/cjs/{CalmLensAppClient-D6p7MAFz.js → CalmLensAppClient-NaCxOVlM.js} +32 -11
  6. package/cjs/{CalmLensConnection-CYoUl5go.js → CalmLensOptionKeys-p2QK687A.js} +430 -9
  7. package/cjs/{CalmLensServerClient-p23cl97D.js → CalmLensServerClient-BDzTthxE.js} +173 -55
  8. package/cjs/{CalmLensServerClient-JtP687a3.d.ts → CalmLensServerClient-C-jsjn8m.d.ts} +54 -30
  9. package/cjs/{CalmLensTypes-9Vt16pc7.d.ts → CalmLensTypes-COJbOuiv.d.ts} +2677 -30
  10. package/cjs/{ModerationContextLimits-DCLXMxyA.js → ModerationContextLimits-2nIv7Zh3.js} +495 -49
  11. package/cjs/{ReactUpload-BcdNWLiU.js → ReactUpload-BdTRCTGA.js} +1 -1
  12. package/cjs/{ReactUpload-CAyCsyG6.d.ts → ReactUpload-CRJNPB5T.d.ts} +1 -1
  13. package/{esm/TestTriggers-By07LR31-CmK0yTyR.d.ts → cjs/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts} +13 -7
  14. package/cjs/{TestTriggers-DTc_iMJF-BjMPq4_3.js → TestTriggers-DbhsNPdg-DjvVuc2h.js} +434 -74
  15. package/cjs/{UploadController-Cxu0bufo.js → UploadController-MLVs97Zt.js} +17 -5
  16. package/{esm/WebhookPayload-2aL31v0J.d.ts → cjs/WebhookPayload-DkukERKE.d.ts} +29 -3
  17. package/cjs/app.d.ts +6 -6
  18. package/cjs/app.js +2 -2
  19. package/cjs/elements.d.ts +3 -3
  20. package/cjs/elements.js +3 -3
  21. package/cjs/{index-CI4BzN4W.d.ts → index-DxbJ_Xaa.d.ts} +7 -6
  22. package/cjs/index.d.ts +5 -5
  23. package/cjs/index.js +1 -1
  24. package/cjs/react-native.d.ts +3 -3
  25. package/cjs/react-native.js +3 -3
  26. package/cjs/react.d.ts +3 -3
  27. package/cjs/react.js +4 -4
  28. package/cjs/server.d.ts +6 -6
  29. package/cjs/server.js +2 -2
  30. package/cjs/test-inputs.d.ts +1 -1
  31. package/cjs/test-inputs.js +9 -9
  32. package/cjs/testing.d.ts +12 -8
  33. package/cjs/testing.js +298 -35
  34. package/{esm/ui-wIiMfqMB.d.ts → cjs/ui-BSqPGKrZ.d.ts} +17 -4
  35. package/cjs/{ui-C0XXJS52.js → ui-CX1z2pD9.js} +13 -10
  36. package/cjs/ui.d.ts +2 -2
  37. package/cjs/ui.js +2 -2
  38. package/cjs/vue.d.ts +5 -5
  39. package/cjs/vue.js +3 -3
  40. package/esm/{BrowserUpload-BT091eOB.d.ts → BrowserUpload-C07aL9Vq.d.ts} +2 -2
  41. package/esm/{BrowserUpload-BXG2isHV.js → BrowserUpload-CAVkRotw.js} +1 -1
  42. package/{cjs/CalmLensAppClient-CV_K26Ce.d.ts → esm/CalmLensAppClient-DDLojj0P.d.ts} +3 -3
  43. package/esm/{CalmLensAppClient-BMgXamoN.js → CalmLensAppClient-PQ9VBvlW.js} +28 -7
  44. package/esm/{CalmLensConnection-DBoDh06c.js → CalmLensOptionKeys-Do2mPP6w.js} +407 -10
  45. package/esm/{CalmLensServerClient-JtP687a3.d.ts → CalmLensServerClient-C-jsjn8m.d.ts} +54 -30
  46. package/esm/{CalmLensServerClient-Dt7rCITa.js → CalmLensServerClient-C_H5aAsV.js} +165 -47
  47. package/esm/{CalmLensTypes-9Vt16pc7.d.ts → CalmLensTypes-COJbOuiv.d.ts} +2677 -30
  48. package/esm/{ModerationContextLimits-KMQ8puoa.js → ModerationContextLimits-Chw-AzgL.js} +462 -46
  49. package/esm/{ReactUpload-CFy_wETY.js → ReactUpload-CMmL0R5Q.js} +1 -1
  50. package/esm/{ReactUpload-CAyCsyG6.d.ts → ReactUpload-CRJNPB5T.d.ts} +1 -1
  51. package/{cjs/TestTriggers-By07LR31-CmK0yTyR.d.ts → esm/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts} +13 -7
  52. package/esm/{TestTriggers-DTc_iMJF-HfB533Gx.js → TestTriggers-DbhsNPdg-CUdrv8Nm.js} +405 -72
  53. package/esm/{UploadController-Cs-bxUbt.js → UploadController-BJZghJpg.js} +17 -5
  54. package/{cjs/WebhookPayload-2aL31v0J.d.ts → esm/WebhookPayload-DkukERKE.d.ts} +29 -3
  55. package/esm/app.d.ts +6 -6
  56. package/esm/app.js +2 -2
  57. package/esm/elements.d.ts +3 -3
  58. package/esm/elements.js +3 -3
  59. package/esm/{index-CI4BzN4W.d.ts → index-DxbJ_Xaa.d.ts} +7 -6
  60. package/esm/index.d.ts +5 -5
  61. package/esm/index.js +1 -1
  62. package/esm/react-native.d.ts +3 -3
  63. package/esm/react-native.js +3 -3
  64. package/esm/react.d.ts +3 -3
  65. package/esm/react.js +4 -4
  66. package/esm/server.d.ts +6 -6
  67. package/esm/server.js +2 -2
  68. package/esm/test-inputs.d.ts +1 -1
  69. package/esm/test-inputs.js +1 -1
  70. package/esm/testing.d.ts +12 -8
  71. package/esm/testing.js +287 -23
  72. package/{cjs/ui-wIiMfqMB.d.ts → esm/ui-BSqPGKrZ.d.ts} +17 -4
  73. package/esm/{ui-BqYkISSE.js → ui-CXH7xJpb.js} +12 -9
  74. package/esm/ui.d.ts +2 -2
  75. package/esm/ui.js +2 -2
  76. package/esm/vue.d.ts +5 -5
  77. package/esm/vue.js +3 -3
  78. package/package.json +1 -1
  79. package/cjs/Jurisdiction-FusEXb4U-zVs21tDv.js +0 -91
  80. package/esm/Jurisdiction-FusEXb4U-1ov6m5yE.js +0 -46
  81. /package/cjs/{Classification-94kSYWZw-CMnQKW13.d.ts → Classification-D3SkprrQ-CMnQKW13.d.ts} +0 -0
  82. /package/esm/{Classification-94kSYWZw-CMnQKW13.d.ts → Classification-D3SkprrQ-CMnQKW13.d.ts} +0 -0
@@ -1,5 +1,5 @@
1
- import { F as UploadItem$1, I as UploadSnapshot$1, M as UploadController, N as UploadControllerOptions$1, O as CalmLensUpload$1, P as UploadFile, f as UploaderProps$1 } from "./ui-wIiMfqMB.js";
2
- import { t as UseCalmLensUploadResult$1 } from "./ReactUpload-CAyCsyG6.js";
1
+ import { F as UploadFile, I as UploadItem$1, L as UploadSnapshot$1, N as UploadController, P as UploadControllerOptions$1, k as CalmLensUpload$1, p as UploaderProps$1 } from "./ui-BSqPGKrZ.js";
2
+ import { t as UseCalmLensUploadResult$1 } from "./ReactUpload-CRJNPB5T.js";
3
3
  type UploadItem<F extends UploadFile = File> = UploadItem$1<F>;
4
4
  type CalmLensUpload<F extends UploadFile = File> = CalmLensUpload$1<F>;
5
5
  type UploadSnapshot<F extends UploadFile = File> = UploadSnapshot$1<F>;
@@ -1,4 +1,4 @@
1
- const require_UploadController = require("./UploadController-Cxu0bufo.js");
1
+ const require_UploadController = require("./UploadController-MLVs97Zt.js");
2
2
  // src/ui/BrowserUpload.ts
3
3
  /** Creates an upload queue for the browser components, such as one a form starts on submit. */
4
4
  const createUploadController = (options) => require_UploadController.createUploadController(options);
@@ -1,4 +1,4 @@
1
- import { Ct as UploadAssetPartOptions, Tt as UploadedAsset, pt as RetrieveAssetOptions, w as CalmLensAppClientOptions, wt as UploadPresetSubmitAssetOptions } from "./CalmLensTypes-9Vt16pc7.js";
1
+ import { At as UploadedAsset, E as CalmLensAppClientOptions, Ot as UploadAssetPartOptions, gt as RetrieveAssetOptions, kt as UploadPresetSubmitAssetOptions } from "./CalmLensTypes-COJbOuiv.js";
2
2
  declare const inspectSymbol: unique symbol;
3
3
  /**
4
4
  * The CalmLens client for browsers and React Native apps. It uploads through an upload preset,
@@ -32,9 +32,9 @@ declare class CalmLensAppClient {
32
32
  * Uploads the next part of a multipart upload this client started. The upload's queue
33
33
  * mode, chosen when it started, applies when the final part arrives.
34
34
  */
35
- uploadPart: ({ assetId, ...options }: UploadAssetPartOptions) => Promise<UploadedAsset>;
35
+ uploadPart: (partOptions: UploadAssetPartOptions) => Promise<UploadedAsset>;
36
36
  /** Checks an upload this client made. `status` is present when the preset shows decisions. */
37
- retrieve: ({ assetId }: RetrieveAssetOptions) => Promise<UploadedAsset>;
37
+ retrieve: (options: RetrieveAssetOptions) => Promise<UploadedAsset>;
38
38
  };
39
39
  }
40
40
  export { CalmLensAppClient as t };
@@ -1,7 +1,12 @@
1
- const require_ModerationContextLimits = require("./ModerationContextLimits-DCLXMxyA.js");
2
- const require_CalmLensConnection = require("./CalmLensConnection-CYoUl5go.js");
1
+ const require_ModerationContextLimits = require("./ModerationContextLimits-2nIv7Zh3.js");
2
+ const require_CalmLensOptionKeys = require("./CalmLensOptionKeys-p2QK687A.js");
3
3
  // src/client/CalmLensAppClient.ts
4
4
  const inspectSymbol = Symbol.for("nodejs.util.inspect.custom");
5
+ /** `options`, once `checkOptionKeys` finds only keys `operation` takes. */
6
+ const checked = (operation, options) => {
7
+ require_ModerationContextLimits.checkOptionKeys(options, operation, require_CalmLensOptionKeys.APP_OPTION_KEYS);
8
+ return options;
9
+ };
5
10
  /**
6
11
  * The CalmLens client for browsers and React Native apps. It uploads through an upload preset,
7
12
  * with an upload token from your server when the preset requires one, and never holds an API
@@ -19,7 +24,7 @@ var CalmLensAppClient = class {
19
24
  const { token, uploadPresetId, ...rest } = options;
20
25
  this.#uploadPresetId = uploadPresetId;
21
26
  this.#token = token;
22
- this.#connection = new require_CalmLensConnection.CalmLensConnection(require_CalmLensConnection.resolveConnectionOptions(rest));
27
+ this.#connection = new require_CalmLensOptionKeys.CalmLensConnection(require_CalmLensOptionKeys.resolveConnectionOptions(rest));
23
28
  }
24
29
  /** Keeps tokens out of `console.log`, `util.inspect` and `JSON.stringify`. */
25
30
  [inspectSymbol]() {
@@ -49,10 +54,24 @@ var CalmLensAppClient = class {
49
54
  async submit(options, queue) {
50
55
  if (!options || typeof options !== "object") throw require_ModerationContextLimits.invalidArgument("Upload options are required");
51
56
  if (!("content" in options) && !("part" in options)) throw require_ModerationContextLimits.invalidArgument("Uploads through an upload preset need content or a part");
52
- require_CalmLensConnection.prepareSubmitAssetOptions(options, { deferUploadChecks: this.#connection.deferUploadChecks });
57
+ require_CalmLensOptionKeys.prepareSubmitAssetOptions(options, {
58
+ deferUploadChecks: this.#connection.deferUploadChecks,
59
+ partSizeBytes: this.#connection.partSizeBytes
60
+ });
53
61
  const token = await this.newUploadToken();
62
+ if ("content" in options && require_CalmLensOptionKeys.splitsIntoParts(options.content, this.#connection.partSizeBytes)) {
63
+ const data = await this.#connection.submitInParts(this.uploadsPath, options, queue, {
64
+ authHeaders: token ? { Authorization: `Bearer ${token}` } : {},
65
+ partPath: (assetId) => `${this.uploadsPath}/${encodeURIComponent(assetId)}/part`,
66
+ onStarted: (assetId) => {
67
+ if (token) this.#uploadTokens.set(assetId, token);
68
+ },
69
+ retrieve: (assetId) => this.assets.retrieve({ assetId })
70
+ });
71
+ return require_CalmLensOptionKeys.expectUploadedAsset(data);
72
+ }
54
73
  const data = await this.#connection.submitRawUpload(this.uploadsPath, options, queue, token ? { Authorization: `Bearer ${token}` } : {});
55
- const uploaded = require_CalmLensConnection.expectUploadedAsset(data);
74
+ const uploaded = require_CalmLensOptionKeys.expectUploadedAsset(data);
56
75
  if (token) this.#uploadTokens.set(uploaded.id, token);
57
76
  return uploaded;
58
77
  }
@@ -60,23 +79,25 @@ var CalmLensAppClient = class {
60
79
  get assets() {
61
80
  return {
62
81
  /** Uploads a file and queues it for moderation. */
63
- create: (options) => this.submit(options, true),
82
+ create: async (options) => this.submit(checked("assets.create", options), true),
64
83
  /** Uploads a file and waits for its moderation decision. */
65
- classify: (options) => this.submit(options, false),
84
+ classify: async (options) => this.submit(checked("assets.classify", options), false),
66
85
  /**
67
86
  * Uploads the next part of a multipart upload this client started. The upload's queue
68
87
  * mode, chosen when it started, applies when the final part arrives.
69
88
  */
70
- uploadPart: async ({ assetId, ...options }) => {
89
+ uploadPart: async (partOptions) => {
90
+ const { assetId, ...options } = checked("assets.uploadPart", partOptions);
71
91
  const encodedAssetId = require_ModerationContextLimits.encodePathSegment(assetId, "Asset ID");
72
92
  const data = await this.#connection.uploadPart(`${this.uploadsPath}/${encodedAssetId}/part`, options, this.authHeaders(assetId));
73
- return require_CalmLensConnection.expectUploadedAsset(data);
93
+ return require_CalmLensOptionKeys.expectUploadedAsset(data);
74
94
  },
75
95
  /** Checks an upload this client made. `status` is present when the preset shows decisions. */
76
- retrieve: async ({ assetId }) => {
96
+ retrieve: async (options) => {
97
+ const { assetId } = checked("assets.retrieve", options);
77
98
  const encodedAssetId = require_ModerationContextLimits.encodePathSegment(assetId, "Asset ID");
78
99
  const data = await this.#connection.json("GET", `${this.uploadsPath}/${encodedAssetId}`, { headers: this.authHeaders(assetId) });
79
- return require_CalmLensConnection.expectUploadedAsset(data);
100
+ return require_CalmLensOptionKeys.expectUploadedAsset(data);
80
101
  }
81
102
  };
82
103
  }
@@ -1,9 +1,5 @@
1
- const require_ModerationContextLimits = require("./ModerationContextLimits-DCLXMxyA.js");
1
+ const require_ModerationContextLimits = require("./ModerationContextLimits-2nIv7Zh3.js");
2
2
  // src/contract/AssetPayload.js
3
- const uint8ArrayInputToNumbers = (value) => {
4
- if (Array.isArray(value)) return value;
5
- return Array.from(value);
6
- };
7
3
  const bufferInputToBytes = (value) => {
8
4
  if (value instanceof ArrayBuffer) return new Uint8Array(value);
9
5
  return Uint8Array.from(value);
@@ -31,7 +27,7 @@ const assetToData = (payload) => {
31
27
  if (payload.buffer instanceof ArrayBuffer) return payload.buffer;
32
28
  return bufferInputToBytes(payload.buffer);
33
29
  }
34
- if (payload.uint8Array !== void 0) return Uint8Array.from(uint8ArrayInputToNumbers(payload.uint8Array));
30
+ if (payload.uint8Array !== void 0) return Uint8Array.from(payload.uint8Array);
35
31
  if (payload.base64 === void 0) throw new Error("Invalid asset payload");
36
32
  const raw = getRawBase64(payload.base64);
37
33
  if (typeof Buffer !== "undefined") return Uint8Array.from(Buffer.from(raw, "base64"));
@@ -80,6 +76,9 @@ const validateAssetPayload = (payload, options) => {
80
76
  };
81
77
  return { ok: true };
82
78
  };
79
+ // src/contract/Media.js
80
+ /** Limits shared with the media decoder. Oversized clips fail; they are never truncated. */
81
+ const MAX_MEDIA_UPLOAD_BYTES = 52428800;
83
82
  const RETRY_MAX_DELAY_MS = 8e3;
84
83
  const RETRYABLE_STATUSES = /* @__PURE__ */ new Set([
85
84
  429,
@@ -366,6 +365,41 @@ var CalmLensTransport = class {
366
365
  const MOCK_HOOKS = Symbol.for("@calmlens/sdk.mockHooks");
367
366
  /** The mock hooks in client options, if a mock client set them. */
368
367
  const getMockHooks = (options) => options[MOCK_HOOKS];
368
+ /** Content larger than this is uploaded in parts of this size (`partSizeBytes`). */
369
+ const DEFAULT_PART_SIZE_BYTES = 8388608;
370
+ /**
371
+ * The size of content the client can split, read again and resend: bytes, base64 and Blobs.
372
+ * Streams and Responses can be read only once, and text stays one request.
373
+ */
374
+ const getSplittableContent = (content) => {
375
+ if ("plainText" in content && content.plainText !== void 0) return void 0;
376
+ if ("data" in content && content.data !== void 0) {
377
+ const { data } = content;
378
+ if (!(data instanceof Blob)) return void 0;
379
+ return {
380
+ size: data.size,
381
+ slice: (start, end) => ({ data: data.slice(start, end) })
382
+ };
383
+ }
384
+ const size = getAssetPayloadByteLength(content);
385
+ if (size === void 0) return void 0;
386
+ let bytes;
387
+ return {
388
+ size,
389
+ slice: (start, end) => {
390
+ if (!bytes) {
391
+ const data = assetToData(content);
392
+ bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
393
+ }
394
+ return { uint8Array: bytes.subarray(start, end) };
395
+ }
396
+ };
397
+ };
398
+ /** Whether `content` goes in parts: its size is known, it can be resent, and it's over a part. */
399
+ const splitsIntoParts = (content, partSizeBytes) => {
400
+ const splittable = getSplittableContent(content);
401
+ return splittable !== void 0 && splittable.size > partSizeBytes;
402
+ };
369
403
  /**
370
404
  * Bytes as a fetch body. They're sent as they are rather than in a Blob: React Native can't
371
405
  * build a Blob from bytes, and every fetch accepts a Uint8Array.
@@ -385,7 +419,7 @@ const setOptionalQueryValue = (params, key, value) => {
385
419
  if (value !== void 0 && value !== null) params.set(key, typeof value === "string" ? value : JSON.stringify(value));
386
420
  };
387
421
  const assertValidUploadMimeType = (mimeType) => {
388
- if (require_ModerationContextLimits.isRejectedUploadMimeType(mimeType)) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.API_ERROR_CODES.INVALID_UPLOAD_MIME_TYPE, {
422
+ if (!require_ModerationContextLimits.isAcceptedUploadMimeType(mimeType)) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.API_ERROR_CODES.INVALID_UPLOAD_MIME_TYPE, {
389
423
  message: require_ModerationContextLimits.getRejectedUploadMimeTypeError(mimeType),
390
424
  details: { mimeType }
391
425
  });
@@ -398,8 +432,20 @@ const assertDeclaredPayloadSize = (payload, declared = payload.sizeInBytes) => {
398
432
  const measured = data instanceof ArrayBuffer || ArrayBuffer.isView(data) ? data.byteLength : getAssetPayloadByteLength(payload);
399
433
  if (!Number.isSafeInteger(declared) || declared < 0 || measured !== void 0 && measured !== declared) throw invalidPayload("sizeInBytes must match the payload byte length");
400
434
  };
435
+ /**
436
+ * Refuses one-request content whose known size is over the API's limit for its type: images up
437
+ * to 10 MiB (larger ones are uploaded in parts), audio and video up to 50 MiB, other files up to
438
+ * 5,000 MiB. Nothing is sent, so an oversized file fails at once instead of after its upload.
439
+ */
440
+ const assertDirectUploadSize = (content) => {
441
+ const size = content.sizeInBytes ?? getAssetPayloadByteLength(content);
442
+ if (size === void 0) return;
443
+ const type = content.type.toLowerCase();
444
+ const [limit, message] = type.startsWith("image/") ? [require_ModerationContextLimits.MAX_DIRECT_IMAGE_UPLOAD_BYTES, "Images uploaded in one request can be at most 10 MiB; upload larger images in parts"] : type.startsWith("audio/") || type.startsWith("video/") ? [MAX_MEDIA_UPLOAD_BYTES, "Audio and video files can be at most 50 MiB"] : [require_ModerationContextLimits.MAX_NON_IMAGE_UPLOAD_BYTES, "Files can be at most 5,000 MiB"];
445
+ if (size > limit) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.API_ERROR_CODES.FILE_TOO_LARGE, { message });
446
+ };
401
447
  /** Checks upload options before anything is sent. */
402
- const prepareSubmitAssetOptions = (options, { deferUploadChecks = false } = {}) => {
448
+ const prepareSubmitAssetOptions = (options, { deferUploadChecks = false, partSizeBytes = DEFAULT_PART_SIZE_BYTES } = {}) => {
403
449
  if (!options || typeof options !== "object") throw require_ModerationContextLimits.invalidArgument("Asset options are required");
404
450
  if (!("content" in options) && !("url" in options) && !("part" in options)) throw invalidPayload("Either file content, URL, or multipart part is required");
405
451
  if (!options.name) throw invalidPayload("Asset name is required");
@@ -408,6 +454,11 @@ const prepareSubmitAssetOptions = (options, { deferUploadChecks = false } = {})
408
454
  if ("content" in options) {
409
455
  assertDeclaredPayloadSize(options.content);
410
456
  assertValidUploadMimeType(options.content.type);
457
+ const inParts = splitsIntoParts(options.content, partSizeBytes);
458
+ if (inParts && (options.idempotencyKey?.length ?? 0) > 240) throw require_ModerationContextLimits.invalidArgument(`idempotencyKey can be at most 240 characters for an upload sent in parts`);
459
+ if (!deferUploadChecks && inParts && isImageMimeType(options.content.type)) {
460
+ if ((getAssetPayloadByteLength(options.content) ?? 0) > require_ModerationContextLimits.MAX_IMAGE_UPLOAD_BYTES) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.API_ERROR_CODES.MULTIPART_IMAGE_TOO_LARGE, { message: "Multipart image uploads cannot exceed 20MB." });
461
+ } else if (!deferUploadChecks) assertDirectUploadSize(options.content);
411
462
  if (!("plainText" in options.content)) {
412
463
  const validation = validateAssetPayload(options.content);
413
464
  if (!validation.ok) throw invalidPayload(validation.error);
@@ -419,7 +470,6 @@ const prepareSubmitAssetOptions = (options, { deferUploadChecks = false } = {})
419
470
  const partSizeInBytes = options.part.sizeInBytes ?? getAssetPayloadByteLength(options.part);
420
471
  if (!deferUploadChecks && isImageMimeType(options.part.type) && partSizeInBytes !== void 0) {
421
472
  if (partSizeInBytes > require_ModerationContextLimits.MAX_IMAGE_UPLOAD_BYTES) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.API_ERROR_CODES.MULTIPART_IMAGE_TOO_LARGE, { message: "Multipart image uploads cannot exceed 20MB." });
422
- if (options.part.isFinal && partSizeInBytes < require_ModerationContextLimits.MIN_MULTIPART_IMAGE_UPLOAD_BYTES) throw new require_ModerationContextLimits.CalmLensError(require_ModerationContextLimits.API_ERROR_CODES.MULTIPART_IMAGE_TOO_SMALL, { message: "Multipart image uploads must total at least 10MB." });
423
473
  }
424
474
  const validation = validateAssetPayload(options.part, {
425
475
  multipart: true,
@@ -558,6 +608,63 @@ var CalmLensConnection = class {
558
608
  "X-CalmLens-Upload": isMultipart ? "part" : "content"
559
609
  }, options.idempotencyKey ?? newIdempotencyKey());
560
610
  }
611
+ /** Content larger than this is uploaded in parts. */
612
+ get partSizeBytes() {
613
+ return this.options.partSizeBytes ?? 8388608;
614
+ }
615
+ /**
616
+ * Uploads content larger than `partSizeBytes` in parts: the first to `uploadsPath` with the
617
+ * upload's fields, the rest to `partPath(assetId)`. Each part carries its own Idempotency-Key
618
+ * (`<key>:part-<n>`), so it is retried safely and a repeated call with the same key resumes the
619
+ * upload. A part the API already has is skipped, and a final part that finds the upload already
620
+ * completed reads it back with `retrieve`.
621
+ */
622
+ async submitInParts(uploadsPath, options, queue, { authHeaders = {}, partPath, onStarted, retrieve }) {
623
+ const { content, idempotencyKey, ...fields } = options;
624
+ const source = getSplittableContent(content);
625
+ const partSize = this.partSizeBytes;
626
+ const count = Math.ceil(source.size / partSize);
627
+ const key = idempotencyKey ?? newIdempotencyKey();
628
+ const part = (partNumber) => source.slice((partNumber - 1) * partSize, Math.min(partNumber * partSize, source.size));
629
+ const first = await this.submitRawUpload(uploadsPath, {
630
+ ...fields,
631
+ part: {
632
+ ...part(1),
633
+ type: content.type,
634
+ partNumber: 1,
635
+ isFinal: false
636
+ },
637
+ totalSizeInBytes: source.size,
638
+ idempotencyKey: key
639
+ }, queue, authHeaders);
640
+ const assetId = expectUploadedAsset(first).id;
641
+ onStarted?.(assetId);
642
+ let result = first;
643
+ for (let partNumber = 2; partNumber <= count;) {
644
+ const isFinal = partNumber === count;
645
+ try {
646
+ result = await this.uploadPart(partPath(assetId), {
647
+ part: {
648
+ ...part(partNumber),
649
+ partNumber,
650
+ isFinal
651
+ },
652
+ idempotencyKey: `${key}:part-${partNumber}`
653
+ }, authHeaders);
654
+ partNumber++;
655
+ } catch (error) {
656
+ if (!(error instanceof require_ModerationContextLimits.CalmLensError)) throw error;
657
+ const expected = error.details?.expectedPartNumber;
658
+ if (error.code === require_ModerationContextLimits.API_ERROR_CODES.INVALID_UPLOAD_PART_NUMBER && typeof expected === "number" && expected > partNumber && expected <= count) {
659
+ partNumber = expected;
660
+ continue;
661
+ }
662
+ if (isFinal && error.code === require_ModerationContextLimits.API_ERROR_CODES.UPLOAD_NOT_OPEN) return retrieve(assetId);
663
+ throw error;
664
+ }
665
+ }
666
+ return result;
667
+ }
561
668
  /** Uploads the next part of a multipart upload to `partPath` (`.../assets/{id}/part`). */
562
669
  uploadPart(partPath, { part, queue, idempotencyKey }, authHeaders = {}) {
563
670
  if (!part) throw require_ModerationContextLimits.invalidArgument("Part options are required");
@@ -579,6 +686,302 @@ var CalmLensConnection = class {
579
686
  }, idempotencyKey ?? newIdempotencyKey());
580
687
  }
581
688
  };
689
+ // src/client/CalmLensOptionKeys.ts
690
+ /** The option keys each `CalmLensServerClient` method sends to the API. */
691
+ const SERVER_OPTION_KEYS = {
692
+ "assets.batch": ["assetIds", "operation"],
693
+ "assets.classify": [
694
+ "authorId",
695
+ "content",
696
+ "context",
697
+ "description",
698
+ "externalId",
699
+ "externalUrl",
700
+ "folderId",
701
+ "isFinal",
702
+ "metadata",
703
+ "name",
704
+ "part",
705
+ "processingOptions",
706
+ "sizeInBytes",
707
+ "totalSizeInBytes",
708
+ "uploadPresetId",
709
+ "url",
710
+ "visibility"
711
+ ],
712
+ "assets.create": [
713
+ "authorId",
714
+ "content",
715
+ "context",
716
+ "description",
717
+ "externalId",
718
+ "externalUrl",
719
+ "folderId",
720
+ "isFinal",
721
+ "metadata",
722
+ "name",
723
+ "part",
724
+ "processingOptions",
725
+ "sizeInBytes",
726
+ "totalSizeInBytes",
727
+ "uploadPresetId",
728
+ "url",
729
+ "visibility"
730
+ ],
731
+ "assets.createSignedUrl": [
732
+ "assetId",
733
+ "expiresInSeconds",
734
+ "lens",
735
+ "resource",
736
+ "transform",
737
+ "viewerClaims"
738
+ ],
739
+ "assets.decide": [
740
+ "assetId",
741
+ "confirmPurge",
742
+ "decision",
743
+ "note"
744
+ ],
745
+ "assets.delete": ["assetId"],
746
+ "assets.deleteContent": ["assetId"],
747
+ "assets.explain": ["assetId", "rerun"],
748
+ "assets.iterate": [
749
+ "cursor",
750
+ "filter",
751
+ "folderId",
752
+ "ordering",
753
+ "pageSize"
754
+ ],
755
+ "assets.iterateChildren": [
756
+ "assetId",
757
+ "cursor",
758
+ "ordering",
759
+ "pageSize"
760
+ ],
761
+ "assets.list": [
762
+ "cursor",
763
+ "filter",
764
+ "folderId",
765
+ "ordering",
766
+ "pageSize"
767
+ ],
768
+ "assets.listChildren": [
769
+ "assetId",
770
+ "cursor",
771
+ "ordering",
772
+ "pageSize"
773
+ ],
774
+ "assets.move": ["assetId", "folderId"],
775
+ "assets.retrieve": ["assetId"],
776
+ "assets.retry": ["assetId"],
777
+ "assets.search": [
778
+ "limit",
779
+ "mode",
780
+ "query"
781
+ ],
782
+ "assets.update": [
783
+ "assetId",
784
+ "description",
785
+ "externalId",
786
+ "externalUrl",
787
+ "metadata",
788
+ "name",
789
+ "tags",
790
+ "visibility"
791
+ ],
792
+ "assets.uploadPart": [
793
+ "assetId",
794
+ "part",
795
+ "queue"
796
+ ],
797
+ "ensureLens": [
798
+ "active",
799
+ "description",
800
+ "identifier",
801
+ "name",
802
+ "transform",
803
+ "type",
804
+ "version"
805
+ ],
806
+ "ensureLenses": ["lenses"],
807
+ "events.list": ["cursor", "pageSize"],
808
+ "events.pages": ["cursor", "pageSize"],
809
+ "folders.create": ["name", "parentId"],
810
+ "folders.delete": ["folderId"],
811
+ "folders.list": [
812
+ "cursor",
813
+ "ordering",
814
+ "pageSize",
815
+ "parentId",
816
+ "search"
817
+ ],
818
+ "folders.move": ["folderId", "parentId"],
819
+ "folders.retrieve": ["folderId"],
820
+ "folders.update": ["folderId", "name"],
821
+ "hashLists.addEntry": [
822
+ "assetId",
823
+ "hash",
824
+ "hashListId",
825
+ "label",
826
+ "quality"
827
+ ],
828
+ "hashLists.create": [
829
+ "action",
830
+ "description",
831
+ "name"
832
+ ],
833
+ "hashLists.delete": ["hashListId"],
834
+ "hashLists.list": [
835
+ "cursor",
836
+ "ordering",
837
+ "pageSize"
838
+ ],
839
+ "hashLists.listEntries": [
840
+ "cursor",
841
+ "hashListId",
842
+ "ordering",
843
+ "pageSize"
844
+ ],
845
+ "hashLists.removeEntry": ["entryId"],
846
+ "hashLists.retrieve": ["hashListId"],
847
+ "hashLists.update": [
848
+ "action",
849
+ "description",
850
+ "hashListId",
851
+ "name"
852
+ ],
853
+ "policies.applyPreset": [
854
+ "operation",
855
+ "policyId",
856
+ "presetVersion",
857
+ "revision"
858
+ ],
859
+ "policies.assign": ["policyId"],
860
+ "policies.create": [
861
+ "description",
862
+ "name",
863
+ "presetId",
864
+ "settings"
865
+ ],
866
+ "policies.delete": ["policyId"],
867
+ "policies.list": [],
868
+ "policies.retrieve": ["policyId"],
869
+ "policies.setAutomaticUpdates": [
870
+ "enabled",
871
+ "policyId",
872
+ "presetVersion",
873
+ "revision"
874
+ ],
875
+ "policies.update": [
876
+ "description",
877
+ "name",
878
+ "policyId",
879
+ "revision",
880
+ "settings"
881
+ ],
882
+ "text.classify": [
883
+ "authorId",
884
+ "context",
885
+ "description",
886
+ "externalId",
887
+ "externalUrl",
888
+ "folderId",
889
+ "metadata",
890
+ "name",
891
+ "processingOptions",
892
+ "text",
893
+ "uploadPresetId",
894
+ "visibility"
895
+ ]
896
+ };
897
+ /** The option keys each `CalmLensAppClient` method sends to the API. */
898
+ const APP_OPTION_KEYS = {
899
+ "assets.classify": [
900
+ "content",
901
+ "description",
902
+ "externalUrl",
903
+ "name",
904
+ "part",
905
+ "sizeInBytes",
906
+ "totalSizeInBytes"
907
+ ],
908
+ "assets.create": [
909
+ "content",
910
+ "description",
911
+ "externalUrl",
912
+ "name",
913
+ "part",
914
+ "sizeInBytes",
915
+ "totalSizeInBytes"
916
+ ],
917
+ "assets.retrieve": ["assetId"],
918
+ "assets.uploadPart": [
919
+ "assetId",
920
+ "part",
921
+ "queue"
922
+ ]
923
+ };
924
+ /**
925
+ * The option keys each `CalmLensServerClient` method sends as `null` when given `null`; any
926
+ * other option given as `null` is left out, as the API refuses `null` for it.
927
+ */
928
+ const NULLABLE_OPTION_KEYS = {
929
+ "assets.classify": [
930
+ "authorId",
931
+ "context",
932
+ "description",
933
+ "externalId",
934
+ "folderId",
935
+ "metadata",
936
+ "processingOptions",
937
+ "uploadPresetId",
938
+ "visibility"
939
+ ],
940
+ "assets.create": [
941
+ "authorId",
942
+ "context",
943
+ "description",
944
+ "externalId",
945
+ "folderId",
946
+ "metadata",
947
+ "processingOptions",
948
+ "uploadPresetId",
949
+ "visibility"
950
+ ],
951
+ "assets.iterate": ["filter", "folderId"],
952
+ "assets.list": ["filter", "folderId"],
953
+ "assets.move": ["folderId"],
954
+ "assets.update": [
955
+ "description",
956
+ "externalId",
957
+ "externalUrl",
958
+ "metadata",
959
+ "tags",
960
+ "visibility"
961
+ ],
962
+ "folders.create": ["parentId"],
963
+ "folders.move": ["parentId"],
964
+ "hashLists.addEntry": ["label", "quality"],
965
+ "hashLists.create": ["description"],
966
+ "hashLists.update": ["description"],
967
+ "text.classify": [
968
+ "authorId",
969
+ "context",
970
+ "description",
971
+ "externalId",
972
+ "folderId",
973
+ "metadata",
974
+ "processingOptions",
975
+ "uploadPresetId",
976
+ "visibility"
977
+ ]
978
+ };
979
+ Object.defineProperty(exports, "APP_OPTION_KEYS", {
980
+ enumerable: true,
981
+ get: function() {
982
+ return APP_OPTION_KEYS;
983
+ }
984
+ });
582
985
  Object.defineProperty(exports, "CalmLensConnection", {
583
986
  enumerable: true,
584
987
  get: function() {
@@ -591,6 +994,18 @@ Object.defineProperty(exports, "MOCK_HOOKS", {
591
994
  return MOCK_HOOKS;
592
995
  }
593
996
  });
997
+ Object.defineProperty(exports, "NULLABLE_OPTION_KEYS", {
998
+ enumerable: true,
999
+ get: function() {
1000
+ return NULLABLE_OPTION_KEYS;
1001
+ }
1002
+ });
1003
+ Object.defineProperty(exports, "SERVER_OPTION_KEYS", {
1004
+ enumerable: true,
1005
+ get: function() {
1006
+ return SERVER_OPTION_KEYS;
1007
+ }
1008
+ });
594
1009
  Object.defineProperty(exports, "assetDataToUint8Array", {
595
1010
  enumerable: true,
596
1011
  get: function() {
@@ -651,6 +1066,12 @@ Object.defineProperty(exports, "sleep", {
651
1066
  return sleep;
652
1067
  }
653
1068
  });
1069
+ Object.defineProperty(exports, "splitsIntoParts", {
1070
+ enumerable: true,
1071
+ get: function() {
1072
+ return splitsIntoParts;
1073
+ }
1074
+ });
654
1075
  Object.defineProperty(exports, "validateAssetPayload", {
655
1076
  enumerable: true,
656
1077
  get: function() {