calmlens 0.18.3 → 0.19.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.
- package/bin/calmlens.js +5373 -5002
- package/cjs/{BrowserUpload-UxIAGNdv.js → BrowserUpload-CJ4Xc9bR.js} +1 -1
- package/cjs/{BrowserUpload-BT091eOB.d.ts → BrowserUpload-RkxV_Vv7.d.ts} +2 -2
- package/{esm/CalmLensAppClient-CV_K26Ce.d.ts → cjs/CalmLensAppClient-B5gfOB6J.d.ts} +3 -3
- package/cjs/{CalmLensAppClient-D6p7MAFz.js → CalmLensAppClient-LHMEQ-UZ.js} +32 -11
- package/cjs/{CalmLensConnection-CYoUl5go.js → CalmLensOptionKeys-BUj7v5AG.js} +368 -8
- package/cjs/{CalmLensServerClient-p23cl97D.js → CalmLensServerClient-BVOEUT_v.js} +167 -54
- package/cjs/{CalmLensServerClient-JtP687a3.d.ts → CalmLensServerClient-DCSbwIBs.d.ts} +53 -29
- package/cjs/{CalmLensTypes-9Vt16pc7.d.ts → CalmLensTypes-DpU-hXq2.d.ts} +2677 -30
- package/cjs/{ModerationContextLimits-DCLXMxyA.js → ModerationContextLimits-FDpQmw_T.js} +195 -41
- package/cjs/{ReactUpload-BcdNWLiU.js → ReactUpload-BvpW4hJi.js} +1 -1
- package/cjs/{ReactUpload-CAyCsyG6.d.ts → ReactUpload-huIMnOJ4.d.ts} +1 -1
- package/cjs/{TestTriggers-DTc_iMJF-BjMPq4_3.js → TestTriggers-CMwMrbjy-K0YBzu2a.js} +433 -73
- package/{esm/TestTriggers-By07LR31-CmK0yTyR.d.ts → cjs/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts} +13 -7
- package/cjs/{UploadController-Cxu0bufo.js → UploadController-D4qOZZc3.js} +17 -5
- package/{esm/WebhookPayload-2aL31v0J.d.ts → cjs/WebhookPayload-CQg8BBZQ.d.ts} +29 -3
- package/cjs/app.d.ts +6 -6
- package/cjs/app.js +2 -2
- package/cjs/elements.d.ts +3 -3
- package/cjs/elements.js +3 -3
- package/cjs/{index-CI4BzN4W.d.ts → index-BcDDQJF2.d.ts} +7 -6
- package/cjs/index.d.ts +5 -5
- package/cjs/index.js +1 -1
- package/cjs/react-native.d.ts +3 -3
- package/cjs/react-native.js +3 -3
- package/cjs/react.d.ts +3 -3
- package/cjs/react.js +4 -4
- package/cjs/server.d.ts +6 -6
- package/cjs/server.js +2 -2
- package/cjs/test-inputs.d.ts +1 -1
- package/cjs/test-inputs.js +9 -9
- package/cjs/testing.d.ts +7 -6
- package/cjs/testing.js +189 -29
- package/{esm/ui-wIiMfqMB.d.ts → cjs/ui-B23r8BvW.d.ts} +17 -4
- package/cjs/{ui-C0XXJS52.js → ui-Dq-cm6Rx.js} +13 -10
- package/cjs/ui.d.ts +2 -2
- package/cjs/ui.js +2 -2
- package/cjs/vue.d.ts +5 -5
- package/cjs/vue.js +3 -3
- package/esm/{BrowserUpload-BXG2isHV.js → BrowserUpload-DpzS2bbj.js} +1 -1
- package/esm/{BrowserUpload-BT091eOB.d.ts → BrowserUpload-RkxV_Vv7.d.ts} +2 -2
- package/{cjs/CalmLensAppClient-CV_K26Ce.d.ts → esm/CalmLensAppClient-B5gfOB6J.d.ts} +3 -3
- package/esm/{CalmLensAppClient-BMgXamoN.js → CalmLensAppClient-BjlvBweS.js} +28 -7
- package/esm/{CalmLensConnection-DBoDh06c.js → CalmLensOptionKeys-BKez0Wiw.js} +351 -9
- package/esm/{CalmLensServerClient-JtP687a3.d.ts → CalmLensServerClient-DCSbwIBs.d.ts} +53 -29
- package/esm/{CalmLensServerClient-Dt7rCITa.js → CalmLensServerClient-DemxbNMn.js} +159 -46
- package/esm/{CalmLensTypes-9Vt16pc7.d.ts → CalmLensTypes-DpU-hXq2.d.ts} +2677 -30
- package/esm/{ModerationContextLimits-KMQ8puoa.js → ModerationContextLimits-lLfmyLni.js} +176 -40
- package/esm/{ReactUpload-CFy_wETY.js → ReactUpload-6klucehl.js} +1 -1
- package/esm/{ReactUpload-CAyCsyG6.d.ts → ReactUpload-huIMnOJ4.d.ts} +1 -1
- package/esm/{TestTriggers-DTc_iMJF-HfB533Gx.js → TestTriggers-CMwMrbjy-bdowBBKw.js} +404 -71
- package/{cjs/TestTriggers-By07LR31-CmK0yTyR.d.ts → esm/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts} +13 -7
- package/esm/{UploadController-Cs-bxUbt.js → UploadController-UxP6gj7A.js} +17 -5
- package/{cjs/WebhookPayload-2aL31v0J.d.ts → esm/WebhookPayload-CQg8BBZQ.d.ts} +29 -3
- package/esm/app.d.ts +6 -6
- package/esm/app.js +2 -2
- package/esm/elements.d.ts +3 -3
- package/esm/elements.js +3 -3
- package/esm/{index-CI4BzN4W.d.ts → index-BcDDQJF2.d.ts} +7 -6
- package/esm/index.d.ts +5 -5
- package/esm/index.js +1 -1
- package/esm/react-native.d.ts +3 -3
- package/esm/react-native.js +3 -3
- package/esm/react.d.ts +3 -3
- package/esm/react.js +4 -4
- package/esm/server.d.ts +6 -6
- package/esm/server.js +2 -2
- package/esm/test-inputs.d.ts +1 -1
- package/esm/test-inputs.js +1 -1
- package/esm/testing.d.ts +7 -6
- package/esm/testing.js +178 -17
- package/{cjs/ui-wIiMfqMB.d.ts → esm/ui-B23r8BvW.d.ts} +17 -4
- package/esm/{ui-BqYkISSE.js → ui-Bn29rfAi.js} +12 -9
- package/esm/ui.d.ts +2 -2
- package/esm/ui.js +2 -2
- package/esm/vue.d.ts +5 -5
- package/esm/vue.js +3 -3
- package/package.json +1 -1
- package/cjs/Jurisdiction-FusEXb4U-zVs21tDv.js +0 -91
- package/esm/Jurisdiction-FusEXb4U-1ov6m5yE.js +0 -46
- /package/cjs/{Classification-94kSYWZw-CMnQKW13.d.ts → Classification-D3SkprrQ-CMnQKW13.d.ts} +0 -0
- /package/esm/{Classification-94kSYWZw-CMnQKW13.d.ts → Classification-D3SkprrQ-CMnQKW13.d.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_UploadController = require("./UploadController-
|
|
1
|
+
const require_UploadController = require("./UploadController-D4qOZZc3.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,5 +1,5 @@
|
|
|
1
|
-
import { F as UploadItem$1,
|
|
2
|
-
import { t as UseCalmLensUploadResult$1 } from "./ReactUpload-
|
|
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-B23r8BvW.js";
|
|
2
|
+
import { t as UseCalmLensUploadResult$1 } from "./ReactUpload-huIMnOJ4.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
|
-
import {
|
|
1
|
+
import { At as UploadedAsset, E as CalmLensAppClientOptions, Ot as UploadAssetPartOptions, gt as RetrieveAssetOptions, kt as UploadPresetSubmitAssetOptions } from "./CalmLensTypes-DpU-hXq2.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: (
|
|
35
|
+
uploadPart: (partOptions: UploadAssetPartOptions) => Promise<UploadedAsset>;
|
|
36
36
|
/** Checks an upload this client made. `status` is present when the preset shows decisions. */
|
|
37
|
-
retrieve: (
|
|
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-
|
|
2
|
-
const
|
|
1
|
+
const require_ModerationContextLimits = require("./ModerationContextLimits-FDpQmw_T.js");
|
|
2
|
+
const require_CalmLensOptionKeys = require("./CalmLensOptionKeys-BUj7v5AG.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
|
|
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
|
-
|
|
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 =
|
|
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 (
|
|
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
|
|
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 (
|
|
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
|
|
100
|
+
return require_CalmLensOptionKeys.expectUploadedAsset(data);
|
|
80
101
|
}
|
|
81
102
|
};
|
|
82
103
|
}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
const require_ModerationContextLimits = require("./ModerationContextLimits-
|
|
1
|
+
const require_ModerationContextLimits = require("./ModerationContextLimits-FDpQmw_T.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(
|
|
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.
|
|
@@ -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,247 @@ 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
|
+
Object.defineProperty(exports, "APP_OPTION_KEYS", {
|
|
925
|
+
enumerable: true,
|
|
926
|
+
get: function() {
|
|
927
|
+
return APP_OPTION_KEYS;
|
|
928
|
+
}
|
|
929
|
+
});
|
|
582
930
|
Object.defineProperty(exports, "CalmLensConnection", {
|
|
583
931
|
enumerable: true,
|
|
584
932
|
get: function() {
|
|
@@ -591,6 +939,12 @@ Object.defineProperty(exports, "MOCK_HOOKS", {
|
|
|
591
939
|
return MOCK_HOOKS;
|
|
592
940
|
}
|
|
593
941
|
});
|
|
942
|
+
Object.defineProperty(exports, "SERVER_OPTION_KEYS", {
|
|
943
|
+
enumerable: true,
|
|
944
|
+
get: function() {
|
|
945
|
+
return SERVER_OPTION_KEYS;
|
|
946
|
+
}
|
|
947
|
+
});
|
|
594
948
|
Object.defineProperty(exports, "assetDataToUint8Array", {
|
|
595
949
|
enumerable: true,
|
|
596
950
|
get: function() {
|
|
@@ -651,6 +1005,12 @@ Object.defineProperty(exports, "sleep", {
|
|
|
651
1005
|
return sleep;
|
|
652
1006
|
}
|
|
653
1007
|
});
|
|
1008
|
+
Object.defineProperty(exports, "splitsIntoParts", {
|
|
1009
|
+
enumerable: true,
|
|
1010
|
+
get: function() {
|
|
1011
|
+
return splitsIntoParts;
|
|
1012
|
+
}
|
|
1013
|
+
});
|
|
654
1014
|
Object.defineProperty(exports, "validateAssetPayload", {
|
|
655
1015
|
enumerable: true,
|
|
656
1016
|
get: function() {
|