calmlens 0.19.0 → 0.21.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/README.md +13 -9
- package/bin/calmlens.js +1646 -706
- package/cjs/{BrowserUpload-CJ4Xc9bR.js → BrowserUpload-CeSwiMo5.js} +1 -1
- package/{esm/BrowserUpload-RkxV_Vv7.d.ts → cjs/BrowserUpload-D-NnAN7g.d.ts} +2 -2
- package/cjs/{CalmLensAppClient-LHMEQ-UZ.js → CalmLensAppClient-0UVCuNqW.js} +30 -10
- package/cjs/{CalmLensAppClient-B5gfOB6J.d.ts → CalmLensAppClient-gG16T2py.d.ts} +13 -3
- package/cjs/{CalmLensOptionKeys-BUj7v5AG.js → CalmLensOptionKeys-CYY6elxg.js} +242 -345
- package/cjs/{CalmLensServerClient-DCSbwIBs.d.ts → CalmLensServerClient-BHx3x_nZ.d.ts} +107 -49
- package/cjs/{CalmLensServerClient-BVOEUT_v.js → CalmLensServerClient-nfu6_YEW.js} +297 -92
- package/cjs/{CalmLensTypes-DpU-hXq2.d.ts → CalmLensTypes-CUB0YYVz.d.ts} +26601 -27354
- package/cjs/{Classification-D3SkprrQ-CMnQKW13.d.ts → Classification-Q5zGpR8l-CEWjnE39.d.ts} +731 -607
- package/cjs/Core-C82YmjjX.d.ts +823 -0
- package/cjs/Core-Dc3cteU7.js +998 -0
- package/cjs/FileIcons-4qSX7DW2.d.ts +67 -0
- package/cjs/{ModerationContextLimits-FDpQmw_T.js → ModerationContextLimits-BxOmeSqq.js} +970 -278
- package/cjs/{ReactUpload-BvpW4hJi.js → ReactUpload-BK2O3dnK.js} +1 -1
- package/cjs/{ReactUpload-huIMnOJ4.d.ts → ReactUpload-DiiXfKvj.d.ts} +1 -1
- package/cjs/{TestTriggers-CMwMrbjy-K0YBzu2a.js → TestTriggers-BTyXtX0Q-ATuFDhLX.js} +343 -35
- package/{esm/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → cjs/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
- package/cjs/{UploadController-D4qOZZc3.js → UploadController-DLyiLDTB.js} +34 -14
- package/cjs/{WebhookPayload-CQg8BBZQ.d.ts → WebhookPayload-BLT2y_Jy.d.ts} +93 -86
- package/cjs/app.d.ts +6 -7
- package/cjs/app.js +4 -3
- package/cjs/elements.d.ts +123 -22
- package/cjs/elements.js +553 -156
- package/cjs/file-icons.d.ts +2 -67
- package/cjs/index-CKRgjV16.d.ts +16 -0
- package/cjs/index.d.ts +5 -6
- package/cjs/index.js +3 -2
- package/cjs/react-native.d.ts +52 -11
- package/cjs/react-native.js +231 -99
- package/cjs/react.d.ts +42 -9
- package/cjs/react.js +381 -118
- package/cjs/server.d.ts +48 -7
- package/cjs/server.js +7 -3
- package/cjs/test-inputs.d.ts +2 -2
- package/cjs/test-inputs.js +13 -9
- package/cjs/testing.d.ts +29 -16
- package/cjs/testing.js +1511 -397
- package/cjs/ui-CTBUxnVp.d.ts +139 -0
- package/cjs/ui-CZ8exACq.js +1359 -0
- package/cjs/ui.d.ts +3 -2
- package/cjs/ui.js +45 -22
- package/cjs/vue.d.ts +802 -199
- package/cjs/vue.js +359 -127
- package/{cjs/BrowserUpload-RkxV_Vv7.d.ts → esm/BrowserUpload-D-NnAN7g.d.ts} +2 -2
- package/esm/{BrowserUpload-DpzS2bbj.js → BrowserUpload-i9OhCzMJ.js} +1 -1
- package/esm/{CalmLensAppClient-BjlvBweS.js → CalmLensAppClient-CK4_Py0c.js} +30 -10
- package/esm/{CalmLensAppClient-B5gfOB6J.d.ts → CalmLensAppClient-gG16T2py.d.ts} +13 -3
- package/esm/{CalmLensOptionKeys-BKez0Wiw.js → CalmLensOptionKeys-BGZlIsT2.js} +235 -338
- package/esm/{CalmLensServerClient-DCSbwIBs.d.ts → CalmLensServerClient-BHx3x_nZ.d.ts} +107 -49
- package/esm/{CalmLensServerClient-DemxbNMn.js → CalmLensServerClient-dAQUcDWb.js} +274 -93
- package/esm/{CalmLensTypes-DpU-hXq2.d.ts → CalmLensTypes-CUB0YYVz.d.ts} +26601 -27354
- package/esm/{Classification-D3SkprrQ-CMnQKW13.d.ts → Classification-Q5zGpR8l-CEWjnE39.d.ts} +731 -607
- package/esm/Core-BxeIud_S.js +843 -0
- package/esm/Core-C82YmjjX.d.ts +823 -0
- package/esm/FileIcons-4qSX7DW2.d.ts +67 -0
- package/esm/{ModerationContextLimits-lLfmyLni.js → ModerationContextLimits-B6_RalCQ.js} +911 -261
- package/esm/{ReactUpload-huIMnOJ4.d.ts → ReactUpload-DiiXfKvj.d.ts} +1 -1
- package/esm/{ReactUpload-6klucehl.js → ReactUpload-DxwVqfX1.js} +1 -1
- package/esm/{TestTriggers-CMwMrbjy-bdowBBKw.js → TestTriggers-BTyXtX0Q-GRB9jQwo.js} +320 -36
- package/{cjs/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → esm/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
- package/esm/{UploadController-UxP6gj7A.js → UploadController-D9Z-h00Q.js} +34 -14
- package/esm/{WebhookPayload-CQg8BBZQ.d.ts → WebhookPayload-BLT2y_Jy.d.ts} +93 -86
- package/esm/app.d.ts +6 -7
- package/esm/app.js +3 -3
- package/esm/elements.d.ts +123 -22
- package/esm/elements.js +497 -128
- package/esm/file-icons.d.ts +2 -67
- package/esm/index-CKRgjV16.d.ts +16 -0
- package/esm/index.d.ts +5 -6
- package/esm/index.js +1 -1
- package/esm/react-native.d.ts +52 -11
- package/esm/react-native.js +205 -75
- package/esm/react.d.ts +42 -9
- package/esm/react.js +327 -91
- package/esm/server.d.ts +48 -7
- package/esm/server.js +3 -3
- package/esm/styles.css +142 -22
- package/esm/svelte/AssetViewer.svelte +25 -0
- package/esm/svelte/CalmLensAsset.svelte +318 -0
- package/esm/svelte/CalmLensDocumentViewer.svelte +7 -0
- package/esm/svelte/CalmLensImageViewer.svelte +7 -0
- package/esm/svelte/CalmLensSpreadsheetViewer.svelte +7 -0
- package/esm/svelte/CalmLensTextViewer.svelte +7 -0
- package/esm/svelte/CalmLensWebsiteViewer.svelte +7 -0
- package/esm/svelte/index.d.ts +29 -6
- package/esm/svelte/index.js +7 -2
- package/esm/test-inputs.d.ts +2 -2
- package/esm/test-inputs.js +1 -1
- package/esm/testing.d.ts +29 -16
- package/esm/testing.js +1507 -393
- package/esm/ui-CTBUxnVp.d.ts +139 -0
- package/esm/ui-Cr7s6JHm.js +1258 -0
- package/esm/ui.d.ts +3 -2
- package/esm/ui.js +4 -3
- package/esm/vue.d.ts +802 -199
- package/esm/vue.js +308 -103
- package/package.json +3 -1
- package/cjs/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
- package/cjs/index-BcDDQJF2.d.ts +0 -253
- package/cjs/ui-B23r8BvW.d.ts +0 -539
- package/cjs/ui-Dq-cm6Rx.js +0 -586
- package/esm/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
- package/esm/index-BcDDQJF2.d.ts +0 -253
- package/esm/svelte/CalmLensMedia.svelte +0 -134
- package/esm/ui-B23r8BvW.d.ts +0 -539
- package/esm/ui-Bn29rfAi.js +0 -461
|
@@ -1,63 +1,63 @@
|
|
|
1
1
|
// src/client/CalmLensErrorCodes.ts
|
|
2
2
|
/** Error codes the CalmLens API documents, by name. */
|
|
3
3
|
const API_ERROR_CODES = {
|
|
4
|
-
INVALID_VIEWER_CLAIMS: "viewer/invalid
|
|
4
|
+
INVALID_VIEWER_CLAIMS: "viewer-claims/invalid",
|
|
5
5
|
AGE_CONFIRMATION_REQUIRED: "viewer/age-confirmation-required",
|
|
6
|
-
INVALID_VIEWER_REGION: "viewer/invalid
|
|
6
|
+
INVALID_VIEWER_REGION: "viewer-region/invalid",
|
|
7
7
|
VIEWER_REGION_REQUIRED: "viewer/region-required",
|
|
8
8
|
REGIONAL_EVIDENCE_REQUIRED: "asset/regional-evidence-required",
|
|
9
9
|
SIGNED_URL_EXPIRED: "signed-url/expired",
|
|
10
10
|
SIGNED_URL_INVALID: "signed-url/invalid",
|
|
11
|
+
SIGNED_URL_ORIGINAL_NOT_GRANTED: "signed-url/original-not-granted",
|
|
12
|
+
INVALID_REQUEST: "validation/invalid-request",
|
|
11
13
|
INVALID_TYPE: "validation/invalid-type",
|
|
12
14
|
TOO_SMALL: "validation/too-small",
|
|
13
15
|
TOO_BIG: "validation/too-big",
|
|
14
16
|
INVALID_FORMAT: "validation/invalid-format",
|
|
15
17
|
INVALID_VALUE: "validation/invalid-value",
|
|
16
18
|
UNKNOWN_FIELD: "validation/unknown-field",
|
|
17
|
-
UNSUPPORTED_CONTENT_TYPE: "
|
|
18
|
-
INVALID_FILTER: "
|
|
19
|
-
|
|
20
|
-
ROUTE_NOT_FOUND: "
|
|
21
|
-
INTERNAL_ERROR: "
|
|
19
|
+
UNSUPPORTED_CONTENT_TYPE: "request/unsupported-content-type",
|
|
20
|
+
INVALID_FILTER: "filter/invalid",
|
|
21
|
+
UNSUPPORTED_ORDERING_FIELD: "ordering/unsupported-field",
|
|
22
|
+
ROUTE_NOT_FOUND: "request/route-not-found",
|
|
23
|
+
INTERNAL_ERROR: "server/internal-error",
|
|
22
24
|
UNAUTHENTICATED: "auth/unauthenticated",
|
|
23
|
-
TOKEN_EXPIRED: "
|
|
24
|
-
INVALID_TOKEN: "
|
|
25
|
+
TOKEN_EXPIRED: "session-token/expired",
|
|
26
|
+
INVALID_TOKEN: "session-token/invalid",
|
|
25
27
|
SEARCH_UNAVAILABLE: "search/unavailable",
|
|
26
|
-
INVALID_TRANSFORM: "
|
|
27
|
-
INVALID_TRANSFORM_OPTIONS: "
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
API_KEY_PROJECT_MISMATCH: "auth/project-mismatch",
|
|
35
|
-
API_KEY_NOT_ACCEPTED: "auth/api-key-not-accepted",
|
|
28
|
+
INVALID_TRANSFORM: "transform/invalid",
|
|
29
|
+
INVALID_TRANSFORM_OPTIONS: "transform-options/invalid",
|
|
30
|
+
REMOTE_URL_TOO_MANY_REDIRECTS: "remote-url/too-many-redirects",
|
|
31
|
+
INVALID_CURSOR: "cursor/invalid",
|
|
32
|
+
MISSING_API_KEY: "api-key/missing",
|
|
33
|
+
INVALID_API_KEY: "api-key/invalid",
|
|
34
|
+
API_KEY_PROJECT_MISMATCH: "api-key/project-mismatch",
|
|
35
|
+
API_KEY_NOT_ACCEPTED: "api-key/not-accepted",
|
|
36
36
|
PERMISSION_DENIED: "auth/permission-denied",
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
INVALID_UPLOAD_MIME_TYPE: "upload/invalid-mime-type",
|
|
46
|
-
INVALID_UPLOAD_PAYLOAD: "upload/invalid-payload",
|
|
37
|
+
UPLOAD_TOO_LARGE: "upload/too-large",
|
|
38
|
+
DOCUMENT_TOO_MANY_ITEMS: "document/too-many-items",
|
|
39
|
+
INVALID_IMAGE: "image/invalid",
|
|
40
|
+
IMAGE_TOO_LARGE: "image/too-large",
|
|
41
|
+
FREE_UPLOADS_EXHAUSTED: "usage/free-uploads-exhausted",
|
|
42
|
+
UPLOADS_PAUSED: "usage/uploads-paused",
|
|
43
|
+
UNSUPPORTED_UPLOAD_TYPE: "upload/unsupported-type",
|
|
44
|
+
INVALID_UPLOAD_PAYLOAD: "upload-payload/invalid",
|
|
47
45
|
USE_UPLOAD_PART: "upload/use-upload-part",
|
|
48
46
|
UPLOAD_NOT_OPEN: "upload/not-open",
|
|
49
|
-
INVALID_UPLOAD_PART_NUMBER: "upload/invalid-
|
|
47
|
+
INVALID_UPLOAD_PART_NUMBER: "upload-part/invalid-number",
|
|
50
48
|
UPLOAD_MISSING: "upload/missing",
|
|
51
49
|
UPLOAD_SIZE_MISMATCH: "upload/size-mismatch",
|
|
52
50
|
INVALID_IDEMPOTENCY_KEY: "idempotency/invalid-key",
|
|
53
51
|
IDEMPOTENCY_KEY_REUSED: "idempotency/key-reused",
|
|
54
52
|
IDEMPOTENT_REQUEST_IN_PROGRESS: "idempotency/request-in-progress",
|
|
53
|
+
NO_RENDERED_VERSION: "asset/no-rendered-version",
|
|
55
54
|
ASSET_NOT_FOUND: "asset/not-found",
|
|
56
55
|
PROJECT_NOT_FOUND: "project/not-found",
|
|
56
|
+
ASSET_CHILD_NOT_DELETABLE: "asset/child-not-deletable",
|
|
57
57
|
ASSET_REVIEW_CHANGED: "asset/review-changed",
|
|
58
58
|
ASSET_REVIEW_NOTE_REQUIRED: "asset/review-note-required",
|
|
59
59
|
ASSET_UPDATE_FAILED: "asset/update-failed",
|
|
60
|
-
|
|
60
|
+
ASSET_HELD: "asset/held",
|
|
61
61
|
ASSET_PROCESSING: "asset/processing",
|
|
62
62
|
ASSET_DECISION_NOT_ALLOWED: "asset/decision-not-allowed",
|
|
63
63
|
ASSET_DECISION_UNCHANGED: "asset/decision-unchanged",
|
|
@@ -67,18 +67,19 @@ const API_ERROR_CODES = {
|
|
|
67
67
|
ASSET_NOT_DECIDED: "asset/not-decided",
|
|
68
68
|
ASSET_POLICY_CHANGED: "asset/policy-changed",
|
|
69
69
|
ASSET_REVIEWED: "asset/reviewed",
|
|
70
|
-
SUPPORT_TICKET_NOT_FOUND: "support/
|
|
70
|
+
SUPPORT_TICKET_NOT_FOUND: "support-ticket/not-found",
|
|
71
71
|
DUPLICATE_EXTERNAL_ID: "asset/duplicate-external-id",
|
|
72
72
|
FOLDER_NOT_FOUND: "folder/not-found",
|
|
73
73
|
DUPLICATE_FOLDER_NAME: "folder/duplicate-name",
|
|
74
74
|
FOLDER_NOT_EMPTY: "folder/not-empty",
|
|
75
75
|
FOLDER_PARENT_CYCLE: "folder/parent-cycle",
|
|
76
|
-
|
|
76
|
+
LENS_NOT_FOUND: "lens/not-found",
|
|
77
|
+
LENS_IDENTIFIER_TAKEN: "lens/identifier-taken",
|
|
77
78
|
LENS_IDENTIFIER_RESERVED: "lens/identifier-reserved",
|
|
78
79
|
HASH_LIST_NOT_FOUND: "hash-list/not-found",
|
|
79
|
-
HASH_LIST_ENTRY_NOT_FOUND: "hash-list/
|
|
80
|
+
HASH_LIST_ENTRY_NOT_FOUND: "hash-list-entry/not-found",
|
|
80
81
|
HASH_LIST_LIMIT_REACHED: "hash-list/limit-reached",
|
|
81
|
-
HASH_LIST_ENTRY_LIMIT_REACHED: "hash-list/
|
|
82
|
+
HASH_LIST_ENTRY_LIMIT_REACHED: "hash-list-entry/limit-reached",
|
|
82
83
|
HASH_LIST_ASSET_NOT_HASHED: "hash-list/asset-not-hashed",
|
|
83
84
|
POLICY_NOT_FOUND: "policy/not-found",
|
|
84
85
|
POLICY_REVISION_CONFLICT: "policy/revision-conflict",
|
|
@@ -92,18 +93,16 @@ const API_ERROR_CODES = {
|
|
|
92
93
|
UPLOAD_PRESET_NOT_FOUND: "upload-preset/not-found",
|
|
93
94
|
UPLOAD_PRESET_DISABLED: "upload-preset/disabled",
|
|
94
95
|
UPLOAD_PRESET_TOKEN_REQUIRED: "upload-preset/token-required",
|
|
95
|
-
REMOTE_URL_NOT_ALLOWED: "
|
|
96
|
-
|
|
97
|
-
UPLOAD_PRESET_CONTENT_TYPE_NOT_ALLOWED: "upload-preset/content-type-not-allowed",
|
|
98
|
-
UPLOAD_PRESET_TOO_LARGE: "upload-preset/too-large",
|
|
96
|
+
REMOTE_URL_NOT_ALLOWED: "remote-url/not-allowed",
|
|
97
|
+
WEBSITE_RATE_LIMIT_REACHED: "upload/website-rate-limit-reached",
|
|
99
98
|
UPLOAD_PRESET_DAILY_LIMIT_REACHED: "upload-preset/daily-limit-reached",
|
|
100
99
|
UPLOAD_PRESET_ORIGIN_NOT_ALLOWED: "upload-preset/origin-not-allowed",
|
|
101
100
|
UPLOAD_PRESET_FIELD_NOT_ALLOWED: "upload-preset/field-not-allowed",
|
|
102
101
|
UPLOAD_TOKEN_INVALID: "upload-token/invalid",
|
|
103
102
|
UPLOAD_TOKEN_EXPIRED: "upload-token/expired",
|
|
104
103
|
UPLOAD_TOKEN_USED: "upload-token/used",
|
|
105
|
-
INVALID_REMOTE_URL: "
|
|
106
|
-
REMOTE_URL_UNAVAILABLE: "
|
|
104
|
+
INVALID_REMOTE_URL: "remote-url/invalid",
|
|
105
|
+
REMOTE_URL_UNAVAILABLE: "remote-url/unavailable",
|
|
107
106
|
APPEAL_NOT_FOUND: "appeal/not-found",
|
|
108
107
|
APPEAL_JUDGMENT_NOT_FOUND: "appeal/judgment-not-found",
|
|
109
108
|
APPEAL_ASSET_NOT_FOUND: "appeal/asset-not-found",
|
|
@@ -111,12 +110,11 @@ const API_ERROR_CODES = {
|
|
|
111
110
|
APPEAL_ALREADY_REVIEWED: "appeal/already-reviewed",
|
|
112
111
|
APPEAL_DECISION_CHANGED: "appeal/decision-changed",
|
|
113
112
|
APPEAL_INVALID_ACTION: "appeal/invalid-action",
|
|
114
|
-
APPEAL_SAFETY_HELD: "appeal/safety-held",
|
|
115
113
|
APPEAL_CONTENT_UNAVAILABLE: "appeal/content-unavailable"
|
|
116
114
|
};
|
|
117
115
|
/** The HTTP status and default message the API returns with each documented error code. */
|
|
118
116
|
const API_ERROR_DEFINITIONS = {
|
|
119
|
-
"viewer/invalid
|
|
117
|
+
"viewer-claims/invalid": {
|
|
120
118
|
status: 400,
|
|
121
119
|
message: "Supply a viewerClaims object with an optional region and boolean ageConfirmed."
|
|
122
120
|
},
|
|
@@ -124,7 +122,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
124
122
|
status: 403,
|
|
125
123
|
message: "Your application has not confirmed age-restricted delivery for this viewer."
|
|
126
124
|
},
|
|
127
|
-
"viewer/invalid
|
|
125
|
+
"viewer-region/invalid": {
|
|
128
126
|
status: 400,
|
|
129
127
|
message: "Supply the viewer's ISO country code, such as DE or GB."
|
|
130
128
|
},
|
|
@@ -142,49 +140,57 @@ const API_ERROR_DEFINITIONS = {
|
|
|
142
140
|
},
|
|
143
141
|
"signed-url/invalid": {
|
|
144
142
|
status: 403,
|
|
145
|
-
message: "The signed URL's
|
|
143
|
+
message: "The signed asset URL's grant wasn't signed for this project, or was changed after signing. Use the URL as created, adding only resources, selectors and viewer claims."
|
|
144
|
+
},
|
|
145
|
+
"signed-url/original-not-granted": {
|
|
146
|
+
status: 403,
|
|
147
|
+
message: "This signed asset URL doesn't grant the original file. Sign it with original access."
|
|
148
|
+
},
|
|
149
|
+
"validation/invalid-request": {
|
|
150
|
+
status: 400,
|
|
151
|
+
message: "The request's parameters, query or body are invalid. details.issues lists each problem, with its path and issue code."
|
|
146
152
|
},
|
|
147
153
|
"validation/invalid-type": {
|
|
148
154
|
status: 400,
|
|
149
|
-
message: "
|
|
155
|
+
message: "Issue code in details.issues: a field has the wrong type."
|
|
150
156
|
},
|
|
151
157
|
"validation/too-small": {
|
|
152
158
|
status: 400,
|
|
153
|
-
message: "
|
|
159
|
+
message: "Issue code in details.issues: a value is below its minimum."
|
|
154
160
|
},
|
|
155
161
|
"validation/too-big": {
|
|
156
162
|
status: 400,
|
|
157
|
-
message: "
|
|
163
|
+
message: "Issue code in details.issues: a value is above its maximum."
|
|
158
164
|
},
|
|
159
165
|
"validation/invalid-format": {
|
|
160
166
|
status: 400,
|
|
161
|
-
message: "
|
|
167
|
+
message: "Issue code in details.issues: a string is not in the required format, such as an ID or URL."
|
|
162
168
|
},
|
|
163
169
|
"validation/invalid-value": {
|
|
164
170
|
status: 400,
|
|
165
|
-
message: "
|
|
171
|
+
message: "Issue code in details.issues: a field has a value it cannot take."
|
|
166
172
|
},
|
|
167
173
|
"validation/unknown-field": {
|
|
168
174
|
status: 400,
|
|
169
|
-
message: "
|
|
175
|
+
message: "Issue code in details.issues: the request has a field this endpoint does not accept."
|
|
170
176
|
},
|
|
171
|
-
"
|
|
177
|
+
"request/unsupported-content-type": {
|
|
172
178
|
status: 415,
|
|
173
179
|
message: "Send the request body as application/json."
|
|
174
180
|
},
|
|
175
|
-
"
|
|
181
|
+
"filter/invalid": {
|
|
176
182
|
status: 400,
|
|
177
183
|
message: "The filter has invalid syntax or names a field that can't be filtered."
|
|
178
184
|
},
|
|
179
|
-
"
|
|
185
|
+
"ordering/unsupported-field": {
|
|
180
186
|
status: 400,
|
|
181
187
|
message: "The list cannot be ordered by this field."
|
|
182
188
|
},
|
|
183
|
-
"
|
|
189
|
+
"request/route-not-found": {
|
|
184
190
|
status: 404,
|
|
185
191
|
message: "No endpoint matches this method and path."
|
|
186
192
|
},
|
|
187
|
-
"
|
|
193
|
+
"server/internal-error": {
|
|
188
194
|
status: 500,
|
|
189
195
|
message: "An internal error occurred. Retry later, and quote requestId to support."
|
|
190
196
|
},
|
|
@@ -192,11 +198,11 @@ const API_ERROR_DEFINITIONS = {
|
|
|
192
198
|
status: 401,
|
|
193
199
|
message: "Authentication required."
|
|
194
200
|
},
|
|
195
|
-
"
|
|
201
|
+
"session-token/expired": {
|
|
196
202
|
status: 401,
|
|
197
203
|
message: "The session token has expired. Sign in again."
|
|
198
204
|
},
|
|
199
|
-
"
|
|
205
|
+
"session-token/invalid": {
|
|
200
206
|
status: 401,
|
|
201
207
|
message: "The session token is invalid. Sign in again."
|
|
202
208
|
},
|
|
@@ -204,87 +210,71 @@ const API_ERROR_DEFINITIONS = {
|
|
|
204
210
|
status: 503,
|
|
205
211
|
message: "Search is temporarily unavailable. Retry later."
|
|
206
212
|
},
|
|
207
|
-
"
|
|
213
|
+
"transform/invalid": {
|
|
208
214
|
status: 400,
|
|
209
215
|
message: "The transform names an unknown type or is not a valid selector."
|
|
210
216
|
},
|
|
211
|
-
"
|
|
217
|
+
"transform-options/invalid": {
|
|
212
218
|
status: 400,
|
|
213
219
|
message: "The transform's options are invalid for its type."
|
|
214
220
|
},
|
|
215
|
-
"
|
|
221
|
+
"remote-url/too-many-redirects": {
|
|
216
222
|
status: 400,
|
|
217
223
|
message: "The remote URL redirected too many times."
|
|
218
224
|
},
|
|
219
|
-
"
|
|
220
|
-
status: 409,
|
|
221
|
-
message: "The asset's content is held for a copyright claim and cannot be changed."
|
|
222
|
-
},
|
|
223
|
-
"asset/api-key-missing-permission": {
|
|
224
|
-
status: 403,
|
|
225
|
-
message: "This API key needs assets:download permission for this asset's folder."
|
|
226
|
-
},
|
|
227
|
-
"validation/invalid-cursor": {
|
|
225
|
+
"cursor/invalid": {
|
|
228
226
|
status: 400,
|
|
229
227
|
message: "The cursor is invalid or belongs to a different query."
|
|
230
228
|
},
|
|
231
|
-
"
|
|
229
|
+
"api-key/missing": {
|
|
232
230
|
status: 401,
|
|
233
231
|
message: "Authentication required. Send your project API key in the X-Api-Key header."
|
|
234
232
|
},
|
|
235
|
-
"
|
|
233
|
+
"api-key/invalid": {
|
|
236
234
|
status: 401,
|
|
237
235
|
message: "The API key is invalid, revoked or expired. Create a key under Developer → API Keys."
|
|
238
236
|
},
|
|
239
|
-
"
|
|
237
|
+
"api-key/project-mismatch": {
|
|
240
238
|
status: 403,
|
|
241
239
|
message: "This API key belongs to a different project. Use a key created for the project that owns this resource."
|
|
242
240
|
},
|
|
243
|
-
"
|
|
241
|
+
"api-key/not-accepted": {
|
|
244
242
|
status: 403,
|
|
245
243
|
message: "API keys are only accepted by the public API at /v1."
|
|
246
244
|
},
|
|
247
245
|
"auth/permission-denied": {
|
|
248
246
|
status: 403,
|
|
249
|
-
message: "This API key lacks a permission the request needs, such as assets:
|
|
250
|
-
},
|
|
251
|
-
"upload/file-too-large": {
|
|
252
|
-
status: 400,
|
|
253
|
-
message: "File size exceeds limit. Maximum direct image size is 10 MiB, audio/video size is 50 MiB, and other files is 5,000 MiB."
|
|
247
|
+
message: "This API key lacks a permission the request needs (details.permission names it, such as assets:download for an asset's folder), or the resource belongs to another project."
|
|
254
248
|
},
|
|
255
|
-
"upload/
|
|
256
|
-
status:
|
|
257
|
-
message: "
|
|
258
|
-
},
|
|
259
|
-
"upload/image-exceeded-max-dimensions": {
|
|
260
|
-
status: 400,
|
|
261
|
-
message: "Image exceeds maximum dimensions. Maximum dimensions are 12,000px."
|
|
249
|
+
"upload/too-large": {
|
|
250
|
+
status: 413,
|
|
251
|
+
message: "The file is larger than allowed. details.limitBytes is the limit, and details.source says whether it is the plan's (`plan`) or the upload preset's (`preset`)."
|
|
262
252
|
},
|
|
263
|
-
"
|
|
253
|
+
"document/too-many-items": {
|
|
264
254
|
status: 422,
|
|
265
|
-
message: "The
|
|
255
|
+
message: "The document holds more embedded images or audio and video files than can be moderated: details.limit is `images` or `media` and details.maximum the most allowed. Split it into smaller documents."
|
|
266
256
|
},
|
|
267
|
-
"image/
|
|
257
|
+
"image/invalid": {
|
|
268
258
|
status: 422,
|
|
269
|
-
message: "
|
|
259
|
+
message: "The image is corrupt or cannot be decoded. Upload a valid image file."
|
|
270
260
|
},
|
|
271
|
-
"image/
|
|
261
|
+
"image/too-large": {
|
|
272
262
|
status: 422,
|
|
273
|
-
message: "The image exceeds
|
|
263
|
+
message: "The image exceeds a processing limit: details.limit is `bytes`, `pixels` or `dimension`. Reduce its size before uploading."
|
|
274
264
|
},
|
|
275
|
-
"
|
|
265
|
+
"usage/free-uploads-exhausted": {
|
|
276
266
|
status: 403,
|
|
277
|
-
message: "
|
|
267
|
+
message: "This project's organisation has used its free uploads. Set up billing to continue uploading."
|
|
278
268
|
},
|
|
279
|
-
"
|
|
269
|
+
"usage/uploads-paused": {
|
|
280
270
|
status: 402,
|
|
281
271
|
message: "Uploads to this project are paused by its organisation's usage limits. See details.reason; an owner can review them in Billing."
|
|
282
272
|
},
|
|
283
|
-
"upload/
|
|
284
|
-
status:
|
|
285
|
-
message: "
|
|
273
|
+
"upload/unsupported-type": {
|
|
274
|
+
status: 415,
|
|
275
|
+
message: "The file type is not accepted. details.source is `calmlens` when CalmLens cannot store and process it (use a concrete MIME type such as image/png, application/pdf or text/plain) or `preset` when the upload preset doesn't allow it."
|
|
286
276
|
},
|
|
287
|
-
"upload/invalid
|
|
277
|
+
"upload-payload/invalid": {
|
|
288
278
|
status: 400,
|
|
289
279
|
message: "Upload payload is invalid."
|
|
290
280
|
},
|
|
@@ -296,7 +286,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
296
286
|
status: 409,
|
|
297
287
|
message: "Asset is not accepting additional parts"
|
|
298
288
|
},
|
|
299
|
-
"upload/invalid-
|
|
289
|
+
"upload-part/invalid-number": {
|
|
300
290
|
status: 409,
|
|
301
291
|
message: "Multipart upload part number is invalid."
|
|
302
292
|
},
|
|
@@ -320,6 +310,10 @@ const API_ERROR_DEFINITIONS = {
|
|
|
320
310
|
status: 409,
|
|
321
311
|
message: "A request with this Idempotency-Key is still being processed. Retry after the Retry-After delay to receive its result."
|
|
322
312
|
},
|
|
313
|
+
"asset/no-rendered-version": {
|
|
314
|
+
status: 404,
|
|
315
|
+
message: "This asset has no such resource: text and spreadsheets have no rendered version or preview, and documents, audio and video get theirs once processing makes them. Check the asset's `content.rendered` and `content.preview`, or request its original resource instead."
|
|
316
|
+
},
|
|
323
317
|
"asset/not-found": {
|
|
324
318
|
status: 404,
|
|
325
319
|
message: "Asset not found"
|
|
@@ -328,6 +322,10 @@ const API_ERROR_DEFINITIONS = {
|
|
|
328
322
|
status: 404,
|
|
329
323
|
message: "Project not found"
|
|
330
324
|
},
|
|
325
|
+
"asset/child-not-deletable": {
|
|
326
|
+
status: 409,
|
|
327
|
+
message: "This asset was extracted from another asset (its `parentId`), so it can't be deleted on its own. Delete the asset it was extracted from instead, which deletes it too."
|
|
328
|
+
},
|
|
331
329
|
"asset/review-changed": {
|
|
332
330
|
status: 409,
|
|
333
331
|
message: "The asset changed during review. Refresh and review the current decision."
|
|
@@ -340,9 +338,9 @@ const API_ERROR_DEFINITIONS = {
|
|
|
340
338
|
status: 500,
|
|
341
339
|
message: "Failed to update asset"
|
|
342
340
|
},
|
|
343
|
-
"asset/
|
|
341
|
+
"asset/held": {
|
|
344
342
|
status: 423,
|
|
345
|
-
message: "
|
|
343
|
+
message: "The asset is held and this request can't change or deliver it. details.hold is `safety` for a safety hold or `copyright` for a copyright claim."
|
|
346
344
|
},
|
|
347
345
|
"asset/processing": {
|
|
348
346
|
status: 409,
|
|
@@ -380,7 +378,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
380
378
|
status: 409,
|
|
381
379
|
message: "A reviewer has decided this asset, so its moderation can't be re-run."
|
|
382
380
|
},
|
|
383
|
-
"support/
|
|
381
|
+
"support-ticket/not-found": {
|
|
384
382
|
status: 404,
|
|
385
383
|
message: "Support ticket not found."
|
|
386
384
|
},
|
|
@@ -404,9 +402,13 @@ const API_ERROR_DEFINITIONS = {
|
|
|
404
402
|
status: 409,
|
|
405
403
|
message: "A folder cannot be moved inside itself or one of its descendants."
|
|
406
404
|
},
|
|
407
|
-
"lens/
|
|
405
|
+
"lens/not-found": {
|
|
406
|
+
status: 404,
|
|
407
|
+
message: "Lens not found. Check the lens ID or identifier in the portal."
|
|
408
|
+
},
|
|
409
|
+
"lens/identifier-taken": {
|
|
408
410
|
status: 409,
|
|
409
|
-
message: "
|
|
411
|
+
message: "The project already has a lens with this identifier. Update that lens, or give this one another identifier."
|
|
410
412
|
},
|
|
411
413
|
"lens/identifier-reserved": {
|
|
412
414
|
status: 409,
|
|
@@ -416,7 +418,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
416
418
|
status: 404,
|
|
417
419
|
message: "Hash list not found. Check the list ID in the portal."
|
|
418
420
|
},
|
|
419
|
-
"hash-list/
|
|
421
|
+
"hash-list-entry/not-found": {
|
|
420
422
|
status: 404,
|
|
421
423
|
message: "Hash list entry not found."
|
|
422
424
|
},
|
|
@@ -424,7 +426,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
424
426
|
status: 409,
|
|
425
427
|
message: "This project has the most hash lists it can hold. Delete a list to add another."
|
|
426
428
|
},
|
|
427
|
-
"hash-list/
|
|
429
|
+
"hash-list-entry/limit-reached": {
|
|
428
430
|
status: 409,
|
|
429
431
|
message: "This project's hash lists hold the most entries they can. Remove entries to add more."
|
|
430
432
|
},
|
|
@@ -450,7 +452,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
450
452
|
},
|
|
451
453
|
"policy/used-by-another-project": {
|
|
452
454
|
status: 409,
|
|
453
|
-
message: "Another project uses
|
|
455
|
+
message: "Another project uses this policy, so it can only be changed from the portal or that project."
|
|
454
456
|
},
|
|
455
457
|
"policy/no-preset": {
|
|
456
458
|
status: 400,
|
|
@@ -466,7 +468,7 @@ const API_ERROR_DEFINITIONS = {
|
|
|
466
468
|
},
|
|
467
469
|
"policy/automatic-updates-unavailable": {
|
|
468
470
|
status: 409,
|
|
469
|
-
message: "Automatic updates can only be switched on for a saved copy of
|
|
471
|
+
message: "Automatic updates can only be switched on for a saved copy of one of the organisation's own preset policies, and off for one that has them."
|
|
470
472
|
},
|
|
471
473
|
"upload-preset/not-found": {
|
|
472
474
|
status: 404,
|
|
@@ -480,22 +482,14 @@ const API_ERROR_DEFINITIONS = {
|
|
|
480
482
|
status: 401,
|
|
481
483
|
message: "This upload preset only accepts uploads with an upload token. Sign one on your server with uploadTokens.sign and pass it to the client."
|
|
482
484
|
},
|
|
483
|
-
"
|
|
485
|
+
"remote-url/not-allowed": {
|
|
484
486
|
status: 403,
|
|
485
487
|
message: "This project doesn't accept URLs from this domain. Change the allowed or blocked domains under Project settings."
|
|
486
488
|
},
|
|
487
|
-
"upload/website-rate-limit-
|
|
489
|
+
"upload/website-rate-limit-reached": {
|
|
488
490
|
status: 429,
|
|
489
491
|
message: "This project has submitted too many websites in the last minute. Wait for the Retry-After period, or lower the pace of submissions."
|
|
490
492
|
},
|
|
491
|
-
"upload-preset/content-type-not-allowed": {
|
|
492
|
-
status: 415,
|
|
493
|
-
message: "This upload preset doesn't accept this file type."
|
|
494
|
-
},
|
|
495
|
-
"upload-preset/too-large": {
|
|
496
|
-
status: 413,
|
|
497
|
-
message: "This file is larger than the upload preset allows."
|
|
498
|
-
},
|
|
499
493
|
"upload-preset/daily-limit-reached": {
|
|
500
494
|
status: 429,
|
|
501
495
|
message: "You have reached this upload preset's daily upload limit. Try again after midnight UTC."
|
|
@@ -520,11 +514,11 @@ const API_ERROR_DEFINITIONS = {
|
|
|
520
514
|
status: 409,
|
|
521
515
|
message: "The upload token has already been used. Each token uploads one asset."
|
|
522
516
|
},
|
|
523
|
-
"
|
|
517
|
+
"remote-url/invalid": {
|
|
524
518
|
status: 400,
|
|
525
519
|
message: "Remote URLs must use HTTP or HTTPS and cannot target private networks, localhost names or URL credentials."
|
|
526
520
|
},
|
|
527
|
-
"
|
|
521
|
+
"remote-url/unavailable": {
|
|
528
522
|
status: 422,
|
|
529
523
|
message: "The URL responded with an error status. Check that it is publicly reachable."
|
|
530
524
|
},
|
|
@@ -556,10 +550,6 @@ const API_ERROR_DEFINITIONS = {
|
|
|
556
550
|
status: 400,
|
|
557
551
|
message: "The decision's action is not valid for this appeal."
|
|
558
552
|
},
|
|
559
|
-
"appeal/safety-held": {
|
|
560
|
-
status: 409,
|
|
561
|
-
message: "A safety hold prevents this appeal from being submitted or applied."
|
|
562
|
-
},
|
|
563
553
|
"appeal/content-unavailable": {
|
|
564
554
|
status: 409,
|
|
565
555
|
message: "The appeal's content is unavailable, or the asset's content was purged, so it can only be blocked."
|
|
@@ -597,8 +587,9 @@ const CALMLENS_ERROR_CODES = {
|
|
|
597
587
|
...SDK_ERROR_CODES
|
|
598
588
|
};
|
|
599
589
|
const BRAND = Symbol.for("calmlens.sdk.CalmLensError");
|
|
600
|
-
const RETRYABLE_STATUSES = /* @__PURE__ */ new Set([
|
|
590
|
+
const RETRYABLE_STATUSES$1 = /* @__PURE__ */ new Set([
|
|
601
591
|
429,
|
|
592
|
+
500,
|
|
602
593
|
502,
|
|
603
594
|
503,
|
|
604
595
|
504
|
|
@@ -643,12 +634,12 @@ var CalmLensError = class CalmLensError extends Error {
|
|
|
643
634
|
}
|
|
644
635
|
/**
|
|
645
636
|
* Whether repeating the same request may succeed: transport failures, timeouts (a request's
|
|
646
|
-
* `timeoutMs` or `waitForDecision`'s deadline), throttling (429), gateway errors (502, 503, 504)
|
|
637
|
+
* `timeoutMs` or `waitForDecision`'s deadline), throttling (429), internal and gateway errors (500, 502, 503, 504)
|
|
647
638
|
* and idempotent requests still in progress. Only repeat a create or upload with the same
|
|
648
639
|
* `idempotencyKey`.
|
|
649
640
|
*/
|
|
650
641
|
isRetryable() {
|
|
651
|
-
return RETRYABLE_CODES.has(this.code) || this.status !== void 0 && RETRYABLE_STATUSES.has(this.status);
|
|
642
|
+
return RETRYABLE_CODES.has(this.code) || this.status !== void 0 && RETRYABLE_STATUSES$1.has(this.status);
|
|
652
643
|
}
|
|
653
644
|
};
|
|
654
645
|
const invalidArgument = (message, details) => new CalmLensError(SDK_ERROR_CODES.INVALID_ARGUMENT, {
|
|
@@ -656,7 +647,305 @@ const invalidArgument = (message, details) => new CalmLensError(SDK_ERROR_CODES.
|
|
|
656
647
|
details
|
|
657
648
|
});
|
|
658
649
|
const invalidConfig = (message) => new CalmLensError(SDK_ERROR_CODES.INVALID_CONFIG, { message });
|
|
659
|
-
// src/
|
|
650
|
+
// src/client/CalmLensVersion.ts
|
|
651
|
+
/** This SDK's version, from its package.json. */
|
|
652
|
+
const SDK_VERSION = "0.21.0";
|
|
653
|
+
const runtimeName = () => {
|
|
654
|
+
const runtime = globalThis;
|
|
655
|
+
if (isReactNativeRuntime()) return "react-native";
|
|
656
|
+
if (runtime.Bun?.version) return `bun/${runtime.Bun.version}`;
|
|
657
|
+
if (runtime.Deno?.version?.deno) return `deno/${runtime.Deno.version.deno}`;
|
|
658
|
+
if (runtime.navigator?.userAgent === "Cloudflare-Workers") return "workerd";
|
|
659
|
+
if (runtime.process?.versions?.node) return `node/${runtime.process.versions.node}`;
|
|
660
|
+
return "unknown";
|
|
661
|
+
};
|
|
662
|
+
/** The `User-Agent` the SDK sends from server runtimes, e.g. `calmlens-js/0.10.0 (node/22.12.0)`. */
|
|
663
|
+
const getUserAgent = () => `calmlens-js/${SDK_VERSION} (${runtimeName()})`;
|
|
664
|
+
/** True in a web page, where API keys would be visible to every visitor. */
|
|
665
|
+
const isBrowserRuntime = () => typeof globalThis.window !== "undefined" && typeof globalThis.document !== "undefined";
|
|
666
|
+
/** True in a React Native app, where an API key ships inside the app for anyone to extract. */
|
|
667
|
+
const isReactNativeRuntime = () => globalThis.navigator?.product === "ReactNative";
|
|
668
|
+
const RETRY_MAX_DELAY_MS = 8e3;
|
|
669
|
+
const RETRYABLE_STATUSES = /* @__PURE__ */ new Set([
|
|
670
|
+
429,
|
|
671
|
+
500,
|
|
672
|
+
502,
|
|
673
|
+
503,
|
|
674
|
+
504
|
|
675
|
+
]);
|
|
676
|
+
const timeoutReason = () => {
|
|
677
|
+
const reason = /* @__PURE__ */ new Error("The operation timed out.");
|
|
678
|
+
reason.name = "TimeoutError";
|
|
679
|
+
return reason;
|
|
680
|
+
};
|
|
681
|
+
/**
|
|
682
|
+
* Links the callers' signals and a per-attempt deadline. `AbortSignal.any` holds its sources
|
|
683
|
+
* weakly, so a long-lived caller signal doesn't collect a listener per streamed response;
|
|
684
|
+
* runtimes without it get explicit listeners that `release` removes.
|
|
685
|
+
*/
|
|
686
|
+
const createAttemptSignal = (signals, timeoutMs) => {
|
|
687
|
+
const callers = signals.filter((signal) => signal !== void 0);
|
|
688
|
+
const deadline = new AbortController();
|
|
689
|
+
let timedOut = false;
|
|
690
|
+
let signal;
|
|
691
|
+
let onAbort;
|
|
692
|
+
if (typeof AbortSignal.any === "function") signal = AbortSignal.any([...callers, deadline.signal]);
|
|
693
|
+
else {
|
|
694
|
+
const controller = new AbortController();
|
|
695
|
+
signal = controller.signal;
|
|
696
|
+
onAbort = (event) => controller.abort(event.target.reason);
|
|
697
|
+
for (const source of [...callers, deadline.signal]) if (source.aborted) controller.abort(source.reason);
|
|
698
|
+
else source.addEventListener("abort", onAbort, { once: true });
|
|
699
|
+
}
|
|
700
|
+
let timer = timeoutMs === void 0 ? void 0 : setTimeout(() => {
|
|
701
|
+
timedOut = true;
|
|
702
|
+
deadline.abort(timeoutReason());
|
|
703
|
+
}, timeoutMs);
|
|
704
|
+
return {
|
|
705
|
+
signal,
|
|
706
|
+
isTimedOut: () => timedOut,
|
|
707
|
+
isCancelled: () => callers.some((caller) => caller.aborted),
|
|
708
|
+
clearDeadline: () => {
|
|
709
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
710
|
+
timer = void 0;
|
|
711
|
+
},
|
|
712
|
+
release: () => {
|
|
713
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
714
|
+
timer = void 0;
|
|
715
|
+
if (onAbort) for (const caller of callers) caller.removeEventListener("abort", onAbort);
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
};
|
|
719
|
+
/**
|
|
720
|
+
* A redirect the SDK must not follow: it would send the API key to another origin. Browsers report
|
|
721
|
+
* a manual redirect as an opaque response; other runtimes return the 3xx with its Location.
|
|
722
|
+
*/
|
|
723
|
+
const isRedirect = (response) => response.type === "opaqueredirect" || response.status >= 300 && response.status < 400 && response.headers.has("location");
|
|
724
|
+
/** Turns anything thrown while sending or reading a request into a `CalmLensError`. */
|
|
725
|
+
const toFailure = (cause, attempt) => {
|
|
726
|
+
if (CalmLensError.isCalmLensError(cause)) return cause;
|
|
727
|
+
if (attempt.isCancelled()) return new CalmLensError(SDK_ERROR_CODES.CANCELLED, {
|
|
728
|
+
message: "Request cancelled",
|
|
729
|
+
cause
|
|
730
|
+
});
|
|
731
|
+
if (attempt.isTimedOut()) return new CalmLensError(SDK_ERROR_CODES.TIMEOUT, {
|
|
732
|
+
message: "Request timed out",
|
|
733
|
+
cause
|
|
734
|
+
});
|
|
735
|
+
return new CalmLensError(SDK_ERROR_CODES.TRANSPORT_ERROR, {
|
|
736
|
+
message: "Request failed",
|
|
737
|
+
cause
|
|
738
|
+
});
|
|
739
|
+
};
|
|
740
|
+
const sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
741
|
+
if (signal?.aborted) {
|
|
742
|
+
reject(new CalmLensError(SDK_ERROR_CODES.CANCELLED, { message: "Request cancelled" }));
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const onAbort = () => {
|
|
746
|
+
clearTimeout(timer);
|
|
747
|
+
reject(new CalmLensError(SDK_ERROR_CODES.CANCELLED, { message: "Request cancelled" }));
|
|
748
|
+
};
|
|
749
|
+
const timer = setTimeout(() => {
|
|
750
|
+
signal?.removeEventListener("abort", onAbort);
|
|
751
|
+
resolve();
|
|
752
|
+
}, ms);
|
|
753
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
754
|
+
});
|
|
755
|
+
/** Exponential backoff with jitter: half the delay is fixed, the other half random. */
|
|
756
|
+
const getRetryDelayMs = (attempt, random = Math.random) => {
|
|
757
|
+
const delay = Math.min(RETRY_MAX_DELAY_MS, 500 * 2 ** attempt);
|
|
758
|
+
return Math.round(delay / 2 + random() * (delay / 2));
|
|
759
|
+
};
|
|
760
|
+
/** Parses `Retry-After` as seconds or an HTTP date, in seconds from `now`. */
|
|
761
|
+
const parseRetryAfter = (value, now = Date.now()) => {
|
|
762
|
+
if (!value) return void 0;
|
|
763
|
+
const trimmed = value.trim();
|
|
764
|
+
if (/^\d+$/.test(trimmed)) return Number(trimmed);
|
|
765
|
+
const date = Date.parse(trimmed);
|
|
766
|
+
if (Number.isNaN(date)) return void 0;
|
|
767
|
+
return Math.max(0, Math.ceil((date - now) / 1e3));
|
|
768
|
+
};
|
|
769
|
+
/** Bodies that can be sent again unchanged. Streams are consumed by the first attempt. */
|
|
770
|
+
const isReplayableBody = (body) => body === void 0 || body === null || typeof body === "string" || body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof URLSearchParams;
|
|
771
|
+
const isApiErrorPayload = (value) => !!value && typeof value === "object" && typeof value.code === "string";
|
|
772
|
+
/** Reads at most `limit` bytes of a body, failing with `sdk/response-too-large` beyond that. */
|
|
773
|
+
const readBody = async (response, limit) => {
|
|
774
|
+
const declared = Number(response.headers.get("content-length"));
|
|
775
|
+
const tooLarge = () => new CalmLensError(SDK_ERROR_CODES.RESPONSE_TOO_LARGE, {
|
|
776
|
+
message: `Response body exceeds ${limit} bytes`,
|
|
777
|
+
status: response.status,
|
|
778
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
779
|
+
headers: response.headers
|
|
780
|
+
});
|
|
781
|
+
if (Number.isFinite(declared) && declared > limit) {
|
|
782
|
+
await response.body?.cancel().catch(() => {});
|
|
783
|
+
throw tooLarge();
|
|
784
|
+
}
|
|
785
|
+
if (!response.body) return /* @__PURE__ */ new Uint8Array(0);
|
|
786
|
+
const reader = response.body.getReader();
|
|
787
|
+
const chunks = [];
|
|
788
|
+
let total = 0;
|
|
789
|
+
for (;;) {
|
|
790
|
+
const { done, value } = await reader.read();
|
|
791
|
+
if (done) break;
|
|
792
|
+
total += value.byteLength;
|
|
793
|
+
if (total > limit) {
|
|
794
|
+
await reader.cancel().catch(() => {});
|
|
795
|
+
throw tooLarge();
|
|
796
|
+
}
|
|
797
|
+
chunks.push(value);
|
|
798
|
+
}
|
|
799
|
+
const bytes = new Uint8Array(total);
|
|
800
|
+
let offset = 0;
|
|
801
|
+
for (const chunk of chunks) {
|
|
802
|
+
bytes.set(chunk, offset);
|
|
803
|
+
offset += chunk.byteLength;
|
|
804
|
+
}
|
|
805
|
+
return bytes;
|
|
806
|
+
};
|
|
807
|
+
const responseInfo = (response) => ({
|
|
808
|
+
status: response.status,
|
|
809
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
810
|
+
headers: response.headers,
|
|
811
|
+
retryAfter: parseRetryAfter(response.headers.get("retry-after"))
|
|
812
|
+
});
|
|
813
|
+
/** The `CalmLensError` for a non-2xx response whose body has been read. */
|
|
814
|
+
const toHttpError = (response, bytes) => {
|
|
815
|
+
let data;
|
|
816
|
+
try {
|
|
817
|
+
data = JSON.parse(new TextDecoder().decode(bytes));
|
|
818
|
+
} catch {}
|
|
819
|
+
if (isApiErrorPayload(data)) {
|
|
820
|
+
const { code, message, details, requestId } = data;
|
|
821
|
+
const info = responseInfo(response);
|
|
822
|
+
return new CalmLensError(code, {
|
|
823
|
+
message: message ?? code,
|
|
824
|
+
...info,
|
|
825
|
+
requestId: info.requestId ?? (typeof requestId === "string" ? requestId : void 0),
|
|
826
|
+
details
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
return new CalmLensError(SDK_ERROR_CODES.HTTP_ERROR, {
|
|
830
|
+
message: `HTTP ${response.status}`,
|
|
831
|
+
...responseInfo(response)
|
|
832
|
+
});
|
|
833
|
+
};
|
|
834
|
+
/** Sends requests with the client's credentials, deadlines, retries and response limits. */
|
|
835
|
+
var CalmLensTransport = class {
|
|
836
|
+
constructor(settings) {
|
|
837
|
+
this.settings = settings;
|
|
838
|
+
this.sleep = settings.sleep ?? sleep;
|
|
839
|
+
}
|
|
840
|
+
headersFor(request) {
|
|
841
|
+
const headers = new Headers(request.init?.headers);
|
|
842
|
+
for (const [name, value] of Object.entries(request.headers ?? {})) headers.set(name, value);
|
|
843
|
+
if (!headers.has("accept") && request.deadline === "body") headers.set("accept", "application/json");
|
|
844
|
+
if (this.settings.apiKey) headers.set("x-api-key", this.settings.apiKey);
|
|
845
|
+
if (!isBrowserRuntime()) headers.set("user-agent", getUserAgent());
|
|
846
|
+
if (request.idempotencyKey) headers.set("idempotency-key", request.idempotencyKey);
|
|
847
|
+
return Object.fromEntries(headers.entries());
|
|
848
|
+
}
|
|
849
|
+
canRetry(request) {
|
|
850
|
+
return this.settings.maxRetries > 0 && (request.method === "GET" || request.method === "HEAD" || !!request.idempotencyKey) && isReplayableBody(request.body);
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Sends a request and returns the response, retrying transport failures, timeouts, 429, 500, 502–504 and
|
|
854
|
+
* `idempotency/request-in-progress` when the request is safe to repeat. Non-2xx responses
|
|
855
|
+
* throw unless `allowHttpErrors` is set. Call `release` once the body has been read.
|
|
856
|
+
*/
|
|
857
|
+
async send(request, { allowHttpErrors = false } = {}) {
|
|
858
|
+
const fetchImpl = request.fetch ?? this.settings.fetch;
|
|
859
|
+
const retryable = this.canRetry(request);
|
|
860
|
+
const headers = this.headersFor(request);
|
|
861
|
+
for (let attemptNumber = 0;; attemptNumber++) {
|
|
862
|
+
const canRetryAgain = retryable && attemptNumber < this.settings.maxRetries;
|
|
863
|
+
const attempt = createAttemptSignal([this.settings.signal, request.init?.signal ?? void 0], request.timeoutMs);
|
|
864
|
+
let response;
|
|
865
|
+
try {
|
|
866
|
+
if (attempt.signal.aborted) throw attempt.signal.reason;
|
|
867
|
+
response = await fetchImpl(request.url, {
|
|
868
|
+
...request.init,
|
|
869
|
+
method: request.method,
|
|
870
|
+
headers,
|
|
871
|
+
body: request.body,
|
|
872
|
+
redirect: "manual",
|
|
873
|
+
signal: attempt.signal,
|
|
874
|
+
duplex: "half"
|
|
875
|
+
});
|
|
876
|
+
if (isRedirect(response)) {
|
|
877
|
+
await response.body?.cancel().catch(() => {});
|
|
878
|
+
throw new CalmLensError(SDK_ERROR_CODES.HTTP_ERROR, {
|
|
879
|
+
message: `Unexpected redirect (HTTP ${response.status})`,
|
|
880
|
+
...responseInfo(response),
|
|
881
|
+
status: response.status || void 0
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
} catch (cause) {
|
|
885
|
+
attempt.release();
|
|
886
|
+
const failure = toFailure(cause, attempt);
|
|
887
|
+
if (canRetryAgain && failure.isRetryable()) {
|
|
888
|
+
await this.sleep(getRetryDelayMs(attemptNumber), this.settings.signal);
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
throw failure;
|
|
892
|
+
}
|
|
893
|
+
if (request.deadline === "headers") attempt.clearDeadline();
|
|
894
|
+
if (response.ok) return {
|
|
895
|
+
response,
|
|
896
|
+
attempt
|
|
897
|
+
};
|
|
898
|
+
if (allowHttpErrors) {
|
|
899
|
+
const retryAfter = parseRetryAfter(response.headers.get("retry-after"));
|
|
900
|
+
const delayMs = retryAfter !== void 0 ? retryAfter * 1e3 : getRetryDelayMs(attemptNumber);
|
|
901
|
+
if (!canRetryAgain || !RETRYABLE_STATUSES.has(response.status) || delayMs > 6e4) return {
|
|
902
|
+
response,
|
|
903
|
+
attempt
|
|
904
|
+
};
|
|
905
|
+
attempt.release();
|
|
906
|
+
await response.body?.cancel().catch(() => {});
|
|
907
|
+
await this.sleep(delayMs, this.settings.signal);
|
|
908
|
+
continue;
|
|
909
|
+
}
|
|
910
|
+
let error;
|
|
911
|
+
try {
|
|
912
|
+
error = toHttpError(response, await readBody(response, this.settings.maxResponseBytes));
|
|
913
|
+
} catch (cause) {
|
|
914
|
+
error = toFailure(cause, attempt);
|
|
915
|
+
} finally {
|
|
916
|
+
attempt.release();
|
|
917
|
+
}
|
|
918
|
+
const retryDelayMs = error.retryAfter !== void 0 ? error.retryAfter * 1e3 : getRetryDelayMs(attemptNumber);
|
|
919
|
+
if (canRetryAgain && retryDelayMs <= 6e4 && error.isRetryable()) {
|
|
920
|
+
await this.sleep(retryDelayMs, this.settings.signal);
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
throw error;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
/** Sends a request and parses its JSON response body. */
|
|
927
|
+
async json(request) {
|
|
928
|
+
const { response, attempt } = await this.send(request);
|
|
929
|
+
try {
|
|
930
|
+
const bytes = await readBody(response, this.settings.maxResponseBytes);
|
|
931
|
+
if (bytes.byteLength === 0 && (response.status === 204 || request.method === "DELETE")) return;
|
|
932
|
+
try {
|
|
933
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
934
|
+
} catch (cause) {
|
|
935
|
+
throw new CalmLensError(SDK_ERROR_CODES.INVALID_RESPONSE, {
|
|
936
|
+
message: `Invalid JSON response (HTTP ${response.status})`,
|
|
937
|
+
...responseInfo(response),
|
|
938
|
+
cause
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
} catch (cause) {
|
|
942
|
+
throw toFailure(cause, attempt);
|
|
943
|
+
} finally {
|
|
944
|
+
attempt.release();
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
// src/contract/SharedConstants-DxUCZwLZ.js
|
|
660
949
|
const IMPERSONATION_COOKIE_NAME = "CL_impersonation";
|
|
661
950
|
const DAY_MS = 864e5;
|
|
662
951
|
const YEAR_MS = 365 * DAY_MS;
|
|
@@ -857,7 +1146,156 @@ const MAX_IMAGE_UPLOAD_BYTES = /* @__PURE__ */ (() => 20971520)();
|
|
|
857
1146
|
const MAX_DIRECT_IMAGE_UPLOAD_BYTES = /* @__PURE__ */ (() => 10485760)();
|
|
858
1147
|
const MAX_NON_IMAGE_UPLOAD_MB = 5e3;
|
|
859
1148
|
const MAX_NON_IMAGE_UPLOAD_BYTES = /* @__PURE__ */ (() => MAX_NON_IMAGE_UPLOAD_MB * 1024 * 1024)();
|
|
860
|
-
const
|
|
1149
|
+
const normalizeUploadMimeType = (mimeType) => mimeType.toLowerCase().split(";")[0]?.trim() || mimeType.toLowerCase().trim();
|
|
1150
|
+
const getRejectedUploadMimeTypeError = (mimeType) => `${normalizeUploadMimeType(mimeType)} is not a valid upload file type. Uploads must use a concrete file MIME type that CalmLens can store and process, such as image/png, image/jpeg, application/pdf, or text/plain.`;
|
|
1151
|
+
const DOMAIN = "calmlens.com";
|
|
1152
|
+
// src/client/CalmLensTransformSchema.ts
|
|
1153
|
+
/** The image transform options' schema, with only the rules the SDK checks. */
|
|
1154
|
+
const TRANSFORM_OPTIONS_SCHEMA = {
|
|
1155
|
+
type: "object",
|
|
1156
|
+
properties: {
|
|
1157
|
+
format: {
|
|
1158
|
+
type: "string",
|
|
1159
|
+
enum: [
|
|
1160
|
+
"jpg",
|
|
1161
|
+
"png",
|
|
1162
|
+
"webp",
|
|
1163
|
+
"avif",
|
|
1164
|
+
"gif"
|
|
1165
|
+
]
|
|
1166
|
+
},
|
|
1167
|
+
width: {
|
|
1168
|
+
type: "integer",
|
|
1169
|
+
minimum: 1,
|
|
1170
|
+
maximum: 1e4
|
|
1171
|
+
},
|
|
1172
|
+
height: {
|
|
1173
|
+
type: "integer",
|
|
1174
|
+
minimum: 1,
|
|
1175
|
+
maximum: 1e4
|
|
1176
|
+
},
|
|
1177
|
+
dpr: {
|
|
1178
|
+
type: "number",
|
|
1179
|
+
minimum: 1,
|
|
1180
|
+
maximum: 3
|
|
1181
|
+
},
|
|
1182
|
+
anim: { type: "boolean" },
|
|
1183
|
+
background: { type: "string" },
|
|
1184
|
+
blur: { type: "number" },
|
|
1185
|
+
redact: {
|
|
1186
|
+
type: "object",
|
|
1187
|
+
properties: {
|
|
1188
|
+
text: { type: "boolean" },
|
|
1189
|
+
personalInformation: { type: "boolean" },
|
|
1190
|
+
faces: { type: "boolean" }
|
|
1191
|
+
}
|
|
1192
|
+
},
|
|
1193
|
+
border: {
|
|
1194
|
+
type: "object",
|
|
1195
|
+
properties: {
|
|
1196
|
+
color: { type: "string" },
|
|
1197
|
+
width: { type: "number" }
|
|
1198
|
+
}
|
|
1199
|
+
},
|
|
1200
|
+
brightness: { type: "number" },
|
|
1201
|
+
contrast: { type: "number" },
|
|
1202
|
+
quality: { oneOf: [{
|
|
1203
|
+
type: "integer",
|
|
1204
|
+
minimum: 1,
|
|
1205
|
+
maximum: 100
|
|
1206
|
+
}, {
|
|
1207
|
+
type: "string",
|
|
1208
|
+
enum: [
|
|
1209
|
+
"low",
|
|
1210
|
+
"medium",
|
|
1211
|
+
"high"
|
|
1212
|
+
]
|
|
1213
|
+
}] },
|
|
1214
|
+
fit: {
|
|
1215
|
+
type: "string",
|
|
1216
|
+
enum: [
|
|
1217
|
+
"scale-down",
|
|
1218
|
+
"contain",
|
|
1219
|
+
"pad",
|
|
1220
|
+
"squeeze",
|
|
1221
|
+
"cover",
|
|
1222
|
+
"crop"
|
|
1223
|
+
]
|
|
1224
|
+
},
|
|
1225
|
+
flip: {
|
|
1226
|
+
type: "string",
|
|
1227
|
+
enum: [
|
|
1228
|
+
"h",
|
|
1229
|
+
"v",
|
|
1230
|
+
"hv"
|
|
1231
|
+
]
|
|
1232
|
+
},
|
|
1233
|
+
gamma: { type: "number" },
|
|
1234
|
+
segment: {
|
|
1235
|
+
type: "string",
|
|
1236
|
+
enum: ["foreground"]
|
|
1237
|
+
},
|
|
1238
|
+
gravity: { oneOf: [{
|
|
1239
|
+
type: "string",
|
|
1240
|
+
enum: [
|
|
1241
|
+
"face",
|
|
1242
|
+
"left",
|
|
1243
|
+
"right",
|
|
1244
|
+
"top",
|
|
1245
|
+
"bottom",
|
|
1246
|
+
"center",
|
|
1247
|
+
"auto",
|
|
1248
|
+
"entropy"
|
|
1249
|
+
]
|
|
1250
|
+
}, {
|
|
1251
|
+
type: "object",
|
|
1252
|
+
properties: {
|
|
1253
|
+
mode: {
|
|
1254
|
+
type: "string",
|
|
1255
|
+
enum: ["remainder", "box-center"]
|
|
1256
|
+
},
|
|
1257
|
+
x: { type: "number" },
|
|
1258
|
+
y: { type: "number" }
|
|
1259
|
+
},
|
|
1260
|
+
required: ["mode"]
|
|
1261
|
+
}] },
|
|
1262
|
+
rotate: {
|
|
1263
|
+
type: "integer",
|
|
1264
|
+
enum: [
|
|
1265
|
+
0,
|
|
1266
|
+
90,
|
|
1267
|
+
180,
|
|
1268
|
+
270
|
|
1269
|
+
]
|
|
1270
|
+
},
|
|
1271
|
+
saturation: { type: "number" },
|
|
1272
|
+
sharpen: { type: "number" },
|
|
1273
|
+
trim: { oneOf: [{
|
|
1274
|
+
type: "string",
|
|
1275
|
+
enum: ["border"]
|
|
1276
|
+
}, {
|
|
1277
|
+
type: "object",
|
|
1278
|
+
properties: {
|
|
1279
|
+
top: { type: "number" },
|
|
1280
|
+
bottom: { type: "number" },
|
|
1281
|
+
left: { type: "number" },
|
|
1282
|
+
right: { type: "number" },
|
|
1283
|
+
width: { type: "number" },
|
|
1284
|
+
height: { type: "number" },
|
|
1285
|
+
border: { oneOf: [{ type: "boolean" }, {
|
|
1286
|
+
type: "object",
|
|
1287
|
+
properties: {
|
|
1288
|
+
color: { type: "string" },
|
|
1289
|
+
tolerance: { type: "number" },
|
|
1290
|
+
keep: { type: "number" }
|
|
1291
|
+
}
|
|
1292
|
+
}] }
|
|
1293
|
+
}
|
|
1294
|
+
}] }
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
// src/client/CalmLensUploadMimeTypes.ts
|
|
1298
|
+
const UPLOAD_MIME_REJECTED = [
|
|
861
1299
|
"application/octet-stream",
|
|
862
1300
|
"multipart/form-data",
|
|
863
1301
|
"multipart/mixed",
|
|
@@ -865,31 +1303,35 @@ const REJECTED_UPLOAD_MIME_TYPE_SET = /* @__PURE__ */ new Set([
|
|
|
865
1303
|
"multipart/related",
|
|
866
1304
|
"multipart/signed",
|
|
867
1305
|
"multipart/encrypted"
|
|
868
|
-
]
|
|
869
|
-
const
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
const
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
1306
|
+
];
|
|
1307
|
+
const UPLOAD_MIME_PREFIXES = [
|
|
1308
|
+
"image/",
|
|
1309
|
+
"video/",
|
|
1310
|
+
"audio/",
|
|
1311
|
+
"text/"
|
|
1312
|
+
];
|
|
1313
|
+
const UPLOAD_MIME_SUFFIXES = ["+json", "+xml"];
|
|
1314
|
+
const UPLOAD_MIME_TYPES = [
|
|
1315
|
+
"application/pdf",
|
|
1316
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1317
|
+
"application/msword",
|
|
1318
|
+
"application/rtf",
|
|
1319
|
+
"application/vnd.oasis.opendocument.text",
|
|
1320
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1321
|
+
"application/vnd.ms-excel.sheet.macroenabled.12",
|
|
1322
|
+
"application/vnd.ms-excel",
|
|
1323
|
+
"application/vnd.oasis.opendocument.spreadsheet",
|
|
1324
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
1325
|
+
"application/vnd.ms-powerpoint.presentation.macroenabled.12",
|
|
1326
|
+
"application/vnd.ms-powerpoint",
|
|
1327
|
+
"application/vnd.oasis.opendocument.presentation",
|
|
1328
|
+
"application/javascript",
|
|
1329
|
+
"application/json",
|
|
1330
|
+
"application/x-javascript",
|
|
1331
|
+
"application/x-yaml",
|
|
1332
|
+
"application/xml",
|
|
1333
|
+
"application/yaml"
|
|
1334
|
+
];
|
|
893
1335
|
// src/client/CalmLensUtils.ts
|
|
894
1336
|
const assertHttpUrl = (name, value) => {
|
|
895
1337
|
if (value === void 0) return;
|
|
@@ -938,6 +1380,8 @@ const validateAppClientOptions = (options) => {
|
|
|
938
1380
|
if ("apiKey" in options && options.apiKey !== void 0) throw invalidConfig("CalmLensAppClient never takes an API key, which anyone using the browser or app could read. Upload through uploadPresetId, and keep the API key in CalmLensServerClient on your server.");
|
|
939
1381
|
if (!options.uploadPresetId) throw invalidConfig("Upload preset ID is required");
|
|
940
1382
|
configPathSegment(options.uploadPresetId, "Upload preset ID");
|
|
1383
|
+
if (options.projectId !== void 0) configPathSegment(options.projectId, "Project ID");
|
|
1384
|
+
assertHttpUrl("cdnBaseUrl", options.cdnBaseUrl);
|
|
941
1385
|
if (options.token !== void 0 && typeof options.token !== "string" && typeof options.token !== "function") throw invalidConfig("token must be a string or a function that returns one");
|
|
942
1386
|
validateConnectionOptions(options);
|
|
943
1387
|
};
|
|
@@ -947,6 +1391,16 @@ const validateRequestOptions = (options) => {
|
|
|
947
1391
|
assertInteger("uploadTimeoutMs", options.uploadTimeoutMs, 1);
|
|
948
1392
|
assertInteger("maxRetries", options.maxRetries, 0);
|
|
949
1393
|
};
|
|
1394
|
+
/** The options that name an asset's CDN URL and a resource beneath it. */
|
|
1395
|
+
const CDN_LOCATION_KEYS = [
|
|
1396
|
+
"assetId",
|
|
1397
|
+
"externalId",
|
|
1398
|
+
"lens",
|
|
1399
|
+
"projectId",
|
|
1400
|
+
"resource",
|
|
1401
|
+
"transform",
|
|
1402
|
+
"viewerClaims"
|
|
1403
|
+
];
|
|
950
1404
|
/**
|
|
951
1405
|
* Option keys of methods that send no request of their own, or that take keys of their own beside
|
|
952
1406
|
* a request's; CalmLensOptionKeys, generated from the API, has the rest.
|
|
@@ -957,23 +1411,10 @@ const LOCAL_OPTION_KEYS = {
|
|
|
957
1411
|
"signal",
|
|
958
1412
|
"timeoutMs"
|
|
959
1413
|
],
|
|
960
|
-
"cdn.getAssetUrl":
|
|
961
|
-
|
|
962
|
-
"lens",
|
|
963
|
-
"resource",
|
|
964
|
-
"transform",
|
|
965
|
-
"viewerClaims"
|
|
966
|
-
],
|
|
967
|
-
"cdn.serve": [
|
|
968
|
-
"assetIdentifier",
|
|
969
|
-
"init",
|
|
970
|
-
"lens",
|
|
971
|
-
"resource",
|
|
972
|
-
"transform",
|
|
973
|
-
"viewerClaims"
|
|
974
|
-
],
|
|
1414
|
+
"cdn.getAssetUrl": CDN_LOCATION_KEYS,
|
|
1415
|
+
"cdn.serve": [...CDN_LOCATION_KEYS, "init"],
|
|
975
1416
|
"uploadTokens.sign": [
|
|
976
|
-
"
|
|
1417
|
+
"expiresIn",
|
|
977
1418
|
"externalId",
|
|
978
1419
|
"uploadPresetId"
|
|
979
1420
|
]
|
|
@@ -1036,6 +1477,16 @@ const encodePathSegment = (value, name) => {
|
|
|
1036
1477
|
if (value === "." || value === "..") throw invalidArgument(`${name} cannot be "." or ".."`);
|
|
1037
1478
|
return encodeURIComponent(value);
|
|
1038
1479
|
};
|
|
1480
|
+
/**
|
|
1481
|
+
* Whether the API accepts an upload of this MIME type: an image, audio, video, text, document or
|
|
1482
|
+
* website type, not a container such as `application/octet-stream`. It refuses any other type with
|
|
1483
|
+
* `upload/unsupported-type`.
|
|
1484
|
+
*/
|
|
1485
|
+
const isAcceptedUploadMimeType = (mimeType) => {
|
|
1486
|
+
const type = normalizeUploadMimeType(mimeType);
|
|
1487
|
+
if (UPLOAD_MIME_REJECTED.includes(type)) return false;
|
|
1488
|
+
return UPLOAD_MIME_PREFIXES.some((prefix) => type.startsWith(prefix)) || UPLOAD_MIME_SUFFIXES.some((suffix) => type.endsWith(suffix)) || UPLOAD_MIME_TYPES.includes(type);
|
|
1489
|
+
};
|
|
1039
1490
|
const TRANSFORM_OPTION_NAME = /^[a-z][a-zA-Z0-9]*$/;
|
|
1040
1491
|
const TRANSFORM_COLOUR_KEYS = /* @__PURE__ */ new Set([
|
|
1041
1492
|
"background",
|
|
@@ -1064,6 +1515,110 @@ const appendTransformOptions = (pairs, key, value) => {
|
|
|
1064
1515
|
else throw invalidArgument(`Transform option "${key}" must be a string, number, boolean or object`);
|
|
1065
1516
|
pairs.push(`${key}=${encodeTransformComponent(text)}`);
|
|
1066
1517
|
};
|
|
1518
|
+
const hasRuleType = (type, value) => {
|
|
1519
|
+
switch (type) {
|
|
1520
|
+
case "object": return isPlainObject(value);
|
|
1521
|
+
case "string":
|
|
1522
|
+
case "boolean": return typeof value === type;
|
|
1523
|
+
case "number": return typeof value === "number" && Number.isFinite(value);
|
|
1524
|
+
case "integer": return typeof value === "number" && Number.isInteger(value);
|
|
1525
|
+
default: return true;
|
|
1526
|
+
}
|
|
1527
|
+
};
|
|
1528
|
+
/**
|
|
1529
|
+
* Checks transform options against the image transform schema, as the CDN does, and returns the
|
|
1530
|
+
* dotted keys it doesn't know and those whose values it refuses (or that are required and missing).
|
|
1531
|
+
* `null` options are left out of a selector, so they are not checked.
|
|
1532
|
+
*/
|
|
1533
|
+
const checkTransformOptions = (options) => {
|
|
1534
|
+
const unknownKeys = [];
|
|
1535
|
+
const invalidKeys = [];
|
|
1536
|
+
const check = (schema, value, key) => {
|
|
1537
|
+
const rule = schema.oneOf ? schema.oneOf.find(({ type }) => hasRuleType(type, value)) : schema;
|
|
1538
|
+
if (!rule || !hasRuleType(rule.type, value) || rule.enum && !rule.enum.includes(value) || rule.minimum !== void 0 && value < rule.minimum || rule.maximum !== void 0 && value > rule.maximum) {
|
|
1539
|
+
invalidKeys.push(key);
|
|
1540
|
+
return;
|
|
1541
|
+
}
|
|
1542
|
+
if (rule.type !== "object") return;
|
|
1543
|
+
const object = value;
|
|
1544
|
+
const path = (name) => key ? `${key}.${name}` : name;
|
|
1545
|
+
for (const name of rule.required ?? []) if (object[name] === void 0 || object[name] === null) invalidKeys.push(path(name));
|
|
1546
|
+
for (const name of Object.keys(object).sort()) {
|
|
1547
|
+
if (object[name] === void 0 || object[name] === null) continue;
|
|
1548
|
+
const child = rule.properties?.[name];
|
|
1549
|
+
if (child) check(child, object[name], path(name));
|
|
1550
|
+
else unknownKeys.push(path(name));
|
|
1551
|
+
}
|
|
1552
|
+
};
|
|
1553
|
+
check(TRANSFORM_OPTIONS_SCHEMA, options, "");
|
|
1554
|
+
return {
|
|
1555
|
+
unknownKeys,
|
|
1556
|
+
invalidKeys
|
|
1557
|
+
};
|
|
1558
|
+
};
|
|
1559
|
+
/**
|
|
1560
|
+
* Reads a CDN selector, the path segment after `/rendered` as sent (still percent-encoded), as the
|
|
1561
|
+
* CDN does (packages/shared/src/TransformSelector.ts): an optional first `@lens`, then `key=value`
|
|
1562
|
+
* options with dotted keys for nested ones (a bare key is `true`). Values are numbers, `true`,
|
|
1563
|
+
* `false`, hex colours (`#` added) for colour options, or text. Throws on anything the CDN refuses
|
|
1564
|
+
* as `transform/invalid`. Used by the mock CDN.
|
|
1565
|
+
*/
|
|
1566
|
+
const parseTransformSelector = (selector) => {
|
|
1567
|
+
if (!selector) return {};
|
|
1568
|
+
const fail = () => /* @__PURE__ */ new Error(`Invalid transform selector "${selector}"`);
|
|
1569
|
+
if (selector.includes("/")) throw fail();
|
|
1570
|
+
const decode = (text) => {
|
|
1571
|
+
try {
|
|
1572
|
+
return decodeURIComponent(text);
|
|
1573
|
+
} catch {
|
|
1574
|
+
throw fail();
|
|
1575
|
+
}
|
|
1576
|
+
};
|
|
1577
|
+
const entries = selector.split(",");
|
|
1578
|
+
let lens;
|
|
1579
|
+
if (entries[0].startsWith("@")) {
|
|
1580
|
+
lens = decode(entries.shift().slice(1));
|
|
1581
|
+
if (!/^[a-z0-9-]{1,50}$/.test(lens)) throw fail();
|
|
1582
|
+
}
|
|
1583
|
+
if (!entries.length) return { lens };
|
|
1584
|
+
const options = {};
|
|
1585
|
+
for (const entry of entries) {
|
|
1586
|
+
if (!entry || entry.startsWith("@")) throw fail();
|
|
1587
|
+
const separator = entry.indexOf("=");
|
|
1588
|
+
const key = separator === -1 ? entry : entry.slice(0, separator);
|
|
1589
|
+
const parts = key.split(".");
|
|
1590
|
+
if (!parts.every((part) => TRANSFORM_OPTION_NAME.test(part))) throw fail();
|
|
1591
|
+
let value = true;
|
|
1592
|
+
if (separator !== -1) {
|
|
1593
|
+
const text = decode(entry.slice(separator + 1));
|
|
1594
|
+
if (text === "") throw fail();
|
|
1595
|
+
value = TRANSFORM_COLOUR_KEYS.has(key) && HEX_COLOUR.test(`#${text}`) ? `#${text.toLowerCase()}` : /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/.test(text) ? Number(text) : text === "true" || text === "false" ? text === "true" : text;
|
|
1596
|
+
}
|
|
1597
|
+
let target = options;
|
|
1598
|
+
for (const part of parts.slice(0, -1)) {
|
|
1599
|
+
const next = target[part];
|
|
1600
|
+
if (next !== void 0 && !isPlainObject(next)) throw fail();
|
|
1601
|
+
target = target[part] ??= {};
|
|
1602
|
+
}
|
|
1603
|
+
const last = parts.at(-1);
|
|
1604
|
+
if (last in target) throw fail();
|
|
1605
|
+
target[last] = value;
|
|
1606
|
+
}
|
|
1607
|
+
return {
|
|
1608
|
+
lens,
|
|
1609
|
+
options
|
|
1610
|
+
};
|
|
1611
|
+
};
|
|
1612
|
+
/** Throws `sdk/invalid-argument` naming each transform option the CDN would refuse. */
|
|
1613
|
+
const assertTransformOptions = (options) => {
|
|
1614
|
+
const { unknownKeys, invalidKeys } = checkTransformOptions(options);
|
|
1615
|
+
if (!unknownKeys.length && !invalidKeys.length) return;
|
|
1616
|
+
const problems = [...unknownKeys.length ? [`unknown option${unknownKeys.length > 1 ? "s" : ""} ${unknownKeys.join(", ")}`] : [], ...invalidKeys.length ? [`invalid value${invalidKeys.length > 1 ? "s" : ""} for ${invalidKeys.join(", ")}`] : []];
|
|
1617
|
+
throw invalidArgument(`Transform options are invalid: ${problems.join("; ")}. Pass the options themselves, such as { width: 640, format: "webp" }.`, {
|
|
1618
|
+
unknownKeys,
|
|
1619
|
+
invalidKeys
|
|
1620
|
+
});
|
|
1621
|
+
};
|
|
1067
1622
|
/**
|
|
1068
1623
|
* Serializes structured transform options as a CDN selector, such as `fit=cover,width=640`. Option
|
|
1069
1624
|
* keys are sorted so equal transforms share a URL, nested keys are dotted (`border.color`),
|
|
@@ -1071,6 +1626,7 @@ const appendTransformOptions = (pairs, key, value) => {
|
|
|
1071
1626
|
*/
|
|
1072
1627
|
const serializeTransform = (transform) => {
|
|
1073
1628
|
if (!isPlainObject(transform)) throw invalidArgument("Transform options must be an object");
|
|
1629
|
+
assertTransformOptions(transform);
|
|
1074
1630
|
const pairs = [];
|
|
1075
1631
|
for (const key of Object.keys(transform).sort()) {
|
|
1076
1632
|
if (!TRANSFORM_OPTION_NAME.test(key)) throw invalidArgument(`Transform option "${key}" is not a valid name`);
|
|
@@ -1086,13 +1642,6 @@ const transformSelector = (transform) => {
|
|
|
1086
1642
|
return transform;
|
|
1087
1643
|
};
|
|
1088
1644
|
const DEFAULT_CDN_BASE_URL = `https://cdn.${DOMAIN}/v1`;
|
|
1089
|
-
const CDN_RESOURCES = /* @__PURE__ */ new Set([
|
|
1090
|
-
"original",
|
|
1091
|
-
"rendered",
|
|
1092
|
-
"placeholder"
|
|
1093
|
-
]);
|
|
1094
|
-
/** The longest signed URL lifetime the API accepts: 24 hours. */
|
|
1095
|
-
const MAX_SIGNED_URL_SECONDS = 86400;
|
|
1096
1645
|
/** The operations `assets.batch` runs. */
|
|
1097
1646
|
const ASSET_BATCH_OPERATIONS = ["delete", "remove-content"];
|
|
1098
1647
|
/** Refuses a batch the API would reject for its size, an ID or the operation. */
|
|
@@ -1101,79 +1650,27 @@ const validateBatchAssetsOptions = ({ assetIds, operation }) => {
|
|
|
1101
1650
|
if (assetIds.some((assetId) => typeof assetId !== "string" || !assetId)) throw invalidArgument("Every asset ID must be a non-empty string");
|
|
1102
1651
|
if (!ASSET_BATCH_OPERATIONS.includes(operation)) throw invalidArgument(`operation must be one of ${ASSET_BATCH_OPERATIONS.join(", ")}`);
|
|
1103
1652
|
};
|
|
1104
|
-
const validateSignedUrlOptions = (options) => {
|
|
1105
|
-
const { resource = "rendered", lens, transform, expiresInSeconds } = options;
|
|
1106
|
-
if (resource !== "original" && resource !== "rendered") throw invalidArgument("A signed URL's resource must be original or rendered");
|
|
1107
|
-
if (resource === "original" && (lens !== void 0 || transform !== void 0)) throw invalidArgument("Lenses and transforms apply only to the rendered resource");
|
|
1108
|
-
if (expiresInSeconds !== void 0 && (!Number.isInteger(expiresInSeconds) || expiresInSeconds < 1 || expiresInSeconds > MAX_SIGNED_URL_SECONDS)) throw invalidArgument("expiresInSeconds must be a whole number from 1 to 86400");
|
|
1109
|
-
};
|
|
1110
|
-
/**
|
|
1111
|
-
* Builds an asset's CDN URL without a client: no API key and no request, so it works in browsers.
|
|
1112
|
-
* `CalmLensServerClient`'s `cdn.getAssetUrl` uses it too.
|
|
1113
|
-
*
|
|
1114
|
-
* `resource` is `rendered` (default), `original` or `placeholder`. Only `rendered` takes a lens or
|
|
1115
|
-
* transform, and it serves images in the best format the browser accepts. A
|
|
1116
|
-
* placeholder is a small blurred PNG that the CDN serves publicly for every asset whatever its
|
|
1117
|
-
* visibility or moderation status, without requiring age confirmation.
|
|
1118
|
-
*/
|
|
1119
|
-
const getAssetUrl = ({ projectId, assetIdentifier, cdnBaseUrl = DEFAULT_CDN_BASE_URL, resource = "rendered", ...options }) => {
|
|
1120
|
-
if (!CDN_RESOURCES.has(resource)) throw invalidArgument("resource must be original, rendered or placeholder");
|
|
1121
|
-
if (resource === "original" && (options.lens !== void 0 || options.transform !== void 0)) throw invalidArgument("Lenses and transforms apply only to the rendered resource");
|
|
1122
|
-
if (resource === "placeholder" && (options.lens !== void 0 || options.transform !== void 0)) throw invalidArgument("A placeholder takes no lens or transform");
|
|
1123
|
-
const url = `${cdnBaseUrl.replace(/\/+$/, "")}/assets/${encodePathSegment(projectId, "Project ID")}/${encodePathSegment(assetIdentifier, "Asset identifier")}/${resource}`;
|
|
1124
|
-
return appendCdnSelector(url, options);
|
|
1125
|
-
};
|
|
1126
|
-
/**
|
|
1127
|
-
* Appends a CDN lens (`/@name`) followed by any direct transform selector to an asset URL, and
|
|
1128
|
-
* optional JSON `viewerClaims` when supplied.
|
|
1129
|
-
*/
|
|
1130
|
-
const appendCdnSelector = (url, options) => {
|
|
1131
|
-
const path = appendCdnPathSelector(url, options);
|
|
1132
|
-
const query = new URLSearchParams();
|
|
1133
|
-
if (options.viewerClaims !== void 0) {
|
|
1134
|
-
const claims = parseViewerClaims(options.viewerClaims);
|
|
1135
|
-
if (!claims) throw invalidArgument("Invalid viewerClaims");
|
|
1136
|
-
query.set("viewerClaims", JSON.stringify(claims));
|
|
1137
|
-
}
|
|
1138
|
-
return query.size ? `${path}?${query}` : path;
|
|
1139
|
-
};
|
|
1140
|
-
const appendCdnPathSelector = (url, options) => {
|
|
1141
|
-
const transform = transformSelector(options.transform);
|
|
1142
|
-
const selector = [...options.lens ? [`@${encodePathSegment(options.lens, "Lens")}`] : [], ...transform ? [typeof options.transform === "string" ? encodeURI(transform) : transform] : []].join(",");
|
|
1143
|
-
return selector ? `${url}/${selector}` : url;
|
|
1144
|
-
};
|
|
1145
|
-
const appendQueryValue = (parts, key, value) => {
|
|
1146
|
-
if (value === void 0 || value === null) return;
|
|
1147
|
-
if (Array.isArray(value)) {
|
|
1148
|
-
for (const [index, item] of value.entries()) appendQueryValue(parts, `${key}[${index}]`, item);
|
|
1149
|
-
return;
|
|
1150
|
-
}
|
|
1151
|
-
if (typeof value === "object") {
|
|
1152
|
-
for (const [field, item] of Object.entries(value)) appendQueryValue(parts, `${key}[${field}]`, item);
|
|
1153
|
-
return;
|
|
1154
|
-
}
|
|
1155
|
-
parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
1156
|
-
};
|
|
1157
1653
|
/**
|
|
1158
|
-
* Serialises a query object the way the API
|
|
1159
|
-
*
|
|
1160
|
-
*
|
|
1654
|
+
* Serialises a query object the way the API reads it: plain values as text, lists of IDs and enum
|
|
1655
|
+
* values comma-separated, and structured values as JSON. Every key and value is percent-encoded
|
|
1656
|
+
* and missing values are omitted.
|
|
1161
1657
|
*/
|
|
1162
|
-
const stringifyQuery = (query) => {
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
return
|
|
1166
|
-
};
|
|
1658
|
+
const stringifyQuery = (query) => Object.entries(query).flatMap(([key, value]) => {
|
|
1659
|
+
if (value === void 0 || value === null) return [];
|
|
1660
|
+
const text = Array.isArray(value) ? value.every((item) => typeof item === "string" || typeof item === "number") ? value.join(",") : JSON.stringify(value) : typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
1661
|
+
return [`${encodeURIComponent(key)}=${encodeURIComponent(text)}`];
|
|
1662
|
+
}).join("&");
|
|
1167
1663
|
/**
|
|
1168
|
-
* Whether an asset
|
|
1169
|
-
*
|
|
1664
|
+
* Whether an asset is settled: its `classification.status` is `completed` (moderation decided it)
|
|
1665
|
+
* or `failed`. `assets.waitForDecision` returns at the first such state.
|
|
1170
1666
|
*/
|
|
1171
|
-
const isAssetDecided = (asset) => asset.classification.
|
|
1667
|
+
const isAssetDecided = (asset) => asset.classification.status === "completed" || asset.classification.status === "failed";
|
|
1172
1668
|
const DECISION_POLL_DELAY_FACTOR = 1.5;
|
|
1173
1669
|
const DECISION_POLL_MAX_DELAY_MS = 2e3;
|
|
1174
1670
|
const DEFAULT_DECISION_TIMEOUT_MS = 6e4;
|
|
1175
1671
|
/**
|
|
1176
|
-
* Retrieves an asset until it
|
|
1672
|
+
* Retrieves an asset until it is settled (see `isAssetDecided`), waiting 0.5s between checks and
|
|
1673
|
+
* 1.5 times longer
|
|
1177
1674
|
* each time, up to 2s. Rejects with `sdk/timeout` once `timeoutMs` passes without one.
|
|
1178
1675
|
*/
|
|
1179
1676
|
const pollForDecision = async (retrieve, { timeoutMs = DEFAULT_DECISION_TIMEOUT_MS, signal }, sleep, now = Date.now) => {
|
|
@@ -1197,24 +1694,177 @@ const pollForDecision = async (retrieve, { timeoutMs = DEFAULT_DECISION_TIMEOUT_
|
|
|
1197
1694
|
delay = Math.min(DECISION_POLL_MAX_DELAY_MS, delay * DECISION_POLL_DELAY_FACTOR);
|
|
1198
1695
|
}
|
|
1199
1696
|
};
|
|
1200
|
-
// src/client/
|
|
1201
|
-
/**
|
|
1202
|
-
const
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
if (
|
|
1210
|
-
|
|
1697
|
+
// src/client/CalmLensViewerClaims.ts
|
|
1698
|
+
/** The ISO 3166-1 alpha-2 countries a viewer's `region` can be. EU is not a viewer country. */
|
|
1699
|
+
const VIEWER_COUNTRIES = new Set("AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW".split(" "));
|
|
1700
|
+
new Set("AT BE BG HR CY CZ DK EE FI FR DE GR HU IE IT LV LT LU MT NL PL PT RO SK SI ES SE".split(" "));
|
|
1701
|
+
/**
|
|
1702
|
+
* The claims to send, with `region` and `ageConfirmed` in that order, or `undefined` when the
|
|
1703
|
+
* value isn't an object of only a viewer country `region` and a boolean `ageConfirmed`.
|
|
1704
|
+
*/
|
|
1705
|
+
const parseViewerClaims = (value) => {
|
|
1706
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
1707
|
+
const { region, ageConfirmed, ...rest } = value;
|
|
1708
|
+
if (Object.keys(rest).length) return void 0;
|
|
1709
|
+
if (region !== void 0 && (typeof region !== "string" || !VIEWER_COUNTRIES.has(region))) return void 0;
|
|
1710
|
+
if (ageConfirmed !== void 0 && typeof ageConfirmed !== "boolean") return void 0;
|
|
1711
|
+
return {
|
|
1712
|
+
...region !== void 0 && { region },
|
|
1713
|
+
...ageConfirmed !== void 0 && { ageConfirmed }
|
|
1714
|
+
};
|
|
1715
|
+
};
|
|
1716
|
+
// src/client/CalmLensCdn.ts
|
|
1717
|
+
const ASSET_RESOURCE_PATHS = {
|
|
1718
|
+
original: "original",
|
|
1719
|
+
rendered: "rendered",
|
|
1720
|
+
preview: "preview",
|
|
1721
|
+
placeholder: "placeholder",
|
|
1722
|
+
hls: "hls/index.m3u8",
|
|
1723
|
+
embed: "embed",
|
|
1724
|
+
info: "info"
|
|
1725
|
+
};
|
|
1726
|
+
/** The prefix of an asset URL segment that names the asset by its external ID. */
|
|
1727
|
+
const EXTERNAL_ID_SEGMENT_PREFIX = "ext:";
|
|
1728
|
+
/** Every CDN resource beneath an asset URL, as `getAssetUrl` and `signAssetUrl` accept them. */
|
|
1729
|
+
const CDN_RESOURCES = Object.keys(ASSET_RESOURCE_PATHS);
|
|
1730
|
+
const UNSELECTABLE = /* @__PURE__ */ new Set([
|
|
1731
|
+
"original",
|
|
1732
|
+
"hls",
|
|
1733
|
+
"embed",
|
|
1734
|
+
"placeholder",
|
|
1735
|
+
"info"
|
|
1736
|
+
]);
|
|
1737
|
+
/** The longest signed URL lifetime the API accepts: 24 hours. */
|
|
1738
|
+
const MAX_SIGNED_URL_EXPIRES_IN = 86400;
|
|
1739
|
+
/**
|
|
1740
|
+
* The asset segment of a CDN URL: the asset ID, or `ext:` and the URL-encoded external ID. Exactly
|
|
1741
|
+
* one of them must be given.
|
|
1742
|
+
*/
|
|
1743
|
+
const getAssetSegment = (options) => {
|
|
1744
|
+
const { assetId, externalId } = options;
|
|
1745
|
+
if (assetId === void 0 === (externalId === void 0)) throw invalidArgument("Pass either assetId or externalId");
|
|
1746
|
+
if (assetId !== void 0) return encodePathSegment(assetId, "Asset ID");
|
|
1747
|
+
if (typeof externalId !== "string" || !externalId) throw invalidArgument("External ID is required");
|
|
1748
|
+
return `${EXTERNAL_ID_SEGMENT_PREFIX}${encodeURIComponent(externalId)}`;
|
|
1749
|
+
};
|
|
1750
|
+
/**
|
|
1751
|
+
* The viewer claims a URL carries: the region, and `ageConfirmed` only when true. Unconfirmed age
|
|
1752
|
+
* and no region are the CDN's defaults, left out so default URLs stay cacheable.
|
|
1753
|
+
*/
|
|
1754
|
+
const toUrlViewerClaims = (claims) => {
|
|
1755
|
+
if (claims === void 0) return void 0;
|
|
1756
|
+
const parsed = parseViewerClaims(claims);
|
|
1757
|
+
if (!parsed) throw invalidArgument("Invalid viewerClaims");
|
|
1758
|
+
const { region, ageConfirmed } = parsed;
|
|
1759
|
+
if (!region && ageConfirmed !== true) return void 0;
|
|
1760
|
+
return {
|
|
1761
|
+
...region ? { region } : {},
|
|
1762
|
+
...ageConfirmed === true ? { ageConfirmed } : {}
|
|
1763
|
+
};
|
|
1764
|
+
};
|
|
1765
|
+
/** The `?viewerClaims=` query of a URL with these claims, or an empty string for the defaults. */
|
|
1766
|
+
const viewerClaimsQuery = (claims) => {
|
|
1767
|
+
const sent = toUrlViewerClaims(claims);
|
|
1768
|
+
return sent ? `?${new URLSearchParams({ viewerClaims: JSON.stringify(sent) })}` : "";
|
|
1769
|
+
};
|
|
1770
|
+
/**
|
|
1771
|
+
* Appends a resource, its `@lens` and transform selector, and the viewer claims to an asset URL
|
|
1772
|
+
* (public or signed). Without `resource` the URL stays the asset URL, which takes no selector.
|
|
1773
|
+
*/
|
|
1774
|
+
const appendAssetLocation = (assetUrl, location) => {
|
|
1775
|
+
const { resource, lens, transform, viewerClaims } = location;
|
|
1776
|
+
const selected = lens !== void 0 || transform !== void 0;
|
|
1777
|
+
let path = assetUrl;
|
|
1778
|
+
if (resource === void 0) {
|
|
1779
|
+
if (selected) throw invalidArgument("A lens or transform needs a resource, such as rendered");
|
|
1780
|
+
} else {
|
|
1781
|
+
if (typeof resource !== "string" || !Object.hasOwn(ASSET_RESOURCE_PATHS, resource)) throw invalidArgument(`resource must be one of ${CDN_RESOURCES.join(", ")}`);
|
|
1782
|
+
if (UNSELECTABLE.has(resource) && selected) throw invalidArgument(`The ${resource} resource takes no lens or transform`);
|
|
1783
|
+
path = `${assetUrl}/${ASSET_RESOURCE_PATHS[resource]}`;
|
|
1784
|
+
const selector = transformSelector(transform);
|
|
1785
|
+
const parts = [...lens !== void 0 ? [`@${encodePathSegment(lens, "Lens")}`] : [], ...selector ? [typeof transform === "string" ? encodeURI(selector) : selector] : []];
|
|
1786
|
+
if (parts.length) path = `${path}/${parts.join(",")}`;
|
|
1787
|
+
}
|
|
1788
|
+
return `${path}${viewerClaimsQuery(viewerClaims)}`;
|
|
1789
|
+
};
|
|
1790
|
+
/** An asset's public URL beneath a CDN base URL, with any resource, selector and claims. */
|
|
1791
|
+
const buildAssetUrl = (cdnBaseUrl, projectId, options) => {
|
|
1792
|
+
const { assetId, externalId, resource, lens, transform, viewerClaims } = options;
|
|
1793
|
+
const base = `${cdnBaseUrl.replace(/\/+$/, "")}/assets/${encodePathSegment(projectId, "Project ID")}/${getAssetSegment({
|
|
1794
|
+
assetId,
|
|
1795
|
+
externalId
|
|
1796
|
+
})}`;
|
|
1797
|
+
return appendAssetLocation(base, {
|
|
1798
|
+
resource,
|
|
1799
|
+
lens,
|
|
1800
|
+
transform,
|
|
1801
|
+
viewerClaims
|
|
1802
|
+
});
|
|
1803
|
+
};
|
|
1804
|
+
/** Checks `signAssetUrl`'s own options as the API does, before anything is sent. */
|
|
1805
|
+
const validateSignAssetUrlOptions = (options) => {
|
|
1806
|
+
const { expiresIn, original } = options;
|
|
1807
|
+
if (expiresIn !== void 0 && (!Number.isInteger(expiresIn) || expiresIn < 1 || expiresIn > 86400)) throw invalidArgument(`expiresIn must be a whole number from 1 to ${MAX_SIGNED_URL_EXPIRES_IN}`);
|
|
1808
|
+
if (original !== void 0 && typeof original !== "boolean") throw invalidArgument("original must be a boolean");
|
|
1809
|
+
getAssetSegment(options);
|
|
1810
|
+
appendAssetLocation("", options);
|
|
1811
|
+
};
|
|
1812
|
+
/** An asset URL without its query: the viewer claims it carried, if any, are returned apart. */
|
|
1813
|
+
const splitAssetUrl = (url) => {
|
|
1814
|
+
const [path = "", query] = url.split("?", 2);
|
|
1815
|
+
const base = path.replace(/\/+$/, "");
|
|
1816
|
+
const raw = query ? new URLSearchParams(query).get("viewerClaims") : null;
|
|
1817
|
+
if (raw === null) return { base };
|
|
1818
|
+
try {
|
|
1819
|
+
return {
|
|
1820
|
+
base,
|
|
1821
|
+
claims: JSON.parse(raw)
|
|
1822
|
+
};
|
|
1823
|
+
} catch {
|
|
1824
|
+
throw invalidArgument("The asset URL's viewerClaims are not JSON");
|
|
1825
|
+
}
|
|
1826
|
+
};
|
|
1827
|
+
const isAssetInfo = (value) => {
|
|
1828
|
+
if (!value || typeof value !== "object") return false;
|
|
1829
|
+
const info = value;
|
|
1830
|
+
return typeof info.id === "string" && typeof info.projectId === "string" && typeof info.type === "string" && !!info.content && typeof info.content === "object" && (info.availability === "available" || info.availability === "age-restricted" || info.availability === "unavailable");
|
|
1831
|
+
};
|
|
1832
|
+
/**
|
|
1833
|
+
* The display info of an asset URL (public or signed): `{url}/info`, an allowlist of the asset's
|
|
1834
|
+
* public fields and whether this viewer can see it now (`availability`). Viewer claims come from
|
|
1835
|
+
* `viewerClaims`, else from the URL. A refusal throws `CalmLensError` with the CDN's code, such as
|
|
1836
|
+
* `signed-url/expired` or `asset/not-found`.
|
|
1837
|
+
*/
|
|
1838
|
+
const getAssetInfo = async (url, options = {}) => {
|
|
1839
|
+
if (typeof url !== "string" || !URL.canParse(url)) throw invalidArgument("url must be an absolute asset URL");
|
|
1840
|
+
const { base, claims } = splitAssetUrl(url);
|
|
1841
|
+
const infoUrl = `${base}/info${viewerClaimsQuery(options.viewerClaims ?? claims)}`;
|
|
1842
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
1843
|
+
let response;
|
|
1844
|
+
try {
|
|
1845
|
+
response = await fetcher(infoUrl, {
|
|
1846
|
+
headers: { Accept: "application/json" },
|
|
1847
|
+
signal: options.signal
|
|
1848
|
+
});
|
|
1849
|
+
} catch (cause) {
|
|
1850
|
+
if (options.signal?.aborted) throw cause;
|
|
1851
|
+
throw new CalmLensError(SDK_ERROR_CODES.TRANSPORT_ERROR, {
|
|
1852
|
+
message: "The asset's info could not be fetched",
|
|
1853
|
+
cause
|
|
1854
|
+
});
|
|
1855
|
+
}
|
|
1856
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
1857
|
+
if (!response.ok) throw toHttpError(response, bytes);
|
|
1858
|
+
let data;
|
|
1859
|
+
try {
|
|
1860
|
+
data = JSON.parse(new TextDecoder().decode(bytes));
|
|
1861
|
+
} catch {}
|
|
1862
|
+
if (!isAssetInfo(data)) throw new CalmLensError(SDK_ERROR_CODES.INVALID_RESPONSE, {
|
|
1863
|
+
message: "The CDN returned no asset info",
|
|
1864
|
+
status: response.status
|
|
1865
|
+
});
|
|
1866
|
+
return data;
|
|
1211
1867
|
};
|
|
1212
|
-
/** The `User-Agent` the SDK sends from server runtimes, e.g. `calmlens-js/0.10.0 (node/22.12.0)`. */
|
|
1213
|
-
const getUserAgent = () => `calmlens-js/${SDK_VERSION} (${runtimeName()})`;
|
|
1214
|
-
/** True in a web page, where API keys would be visible to every visitor. */
|
|
1215
|
-
const isBrowserRuntime = () => typeof globalThis.window !== "undefined" && typeof globalThis.document !== "undefined";
|
|
1216
|
-
/** True in a React Native app, where an API key ships inside the app for anyone to extract. */
|
|
1217
|
-
const isReactNativeRuntime = () => globalThis.navigator?.product === "ReactNative";
|
|
1218
1868
|
// src/contract/ModerationContextLimits-BIMUlk2e.js
|
|
1219
1869
|
/** The most recent earlier posts CalmLens uses. */
|
|
1220
1870
|
const MODERATION_CONTEXT_USED_ITEMS = 5;
|
|
@@ -1265,28 +1915,34 @@ Object.defineProperty(exports, "CALMLENS_ERROR_CODES", {
|
|
|
1265
1915
|
return CALMLENS_ERROR_CODES;
|
|
1266
1916
|
}
|
|
1267
1917
|
});
|
|
1918
|
+
Object.defineProperty(exports, "CDN_RESOURCES", {
|
|
1919
|
+
enumerable: true,
|
|
1920
|
+
get: function() {
|
|
1921
|
+
return CDN_RESOURCES;
|
|
1922
|
+
}
|
|
1923
|
+
});
|
|
1268
1924
|
Object.defineProperty(exports, "CalmLensError", {
|
|
1269
1925
|
enumerable: true,
|
|
1270
1926
|
get: function() {
|
|
1271
1927
|
return CalmLensError;
|
|
1272
1928
|
}
|
|
1273
1929
|
});
|
|
1274
|
-
Object.defineProperty(exports, "
|
|
1930
|
+
Object.defineProperty(exports, "CalmLensTransport", {
|
|
1275
1931
|
enumerable: true,
|
|
1276
1932
|
get: function() {
|
|
1277
|
-
return
|
|
1933
|
+
return CalmLensTransport;
|
|
1278
1934
|
}
|
|
1279
1935
|
});
|
|
1280
|
-
Object.defineProperty(exports, "
|
|
1936
|
+
Object.defineProperty(exports, "DEFAULT_CDN_BASE_URL", {
|
|
1281
1937
|
enumerable: true,
|
|
1282
1938
|
get: function() {
|
|
1283
|
-
return
|
|
1939
|
+
return DEFAULT_CDN_BASE_URL;
|
|
1284
1940
|
}
|
|
1285
1941
|
});
|
|
1286
|
-
Object.defineProperty(exports, "
|
|
1942
|
+
Object.defineProperty(exports, "DOMAIN", {
|
|
1287
1943
|
enumerable: true,
|
|
1288
1944
|
get: function() {
|
|
1289
|
-
return
|
|
1945
|
+
return DOMAIN;
|
|
1290
1946
|
}
|
|
1291
1947
|
});
|
|
1292
1948
|
Object.defineProperty(exports, "MAX_DIRECT_IMAGE_UPLOAD_BYTES", {
|
|
@@ -1331,34 +1987,46 @@ Object.defineProperty(exports, "SDK_VERSION", {
|
|
|
1331
1987
|
return SDK_VERSION;
|
|
1332
1988
|
}
|
|
1333
1989
|
});
|
|
1990
|
+
Object.defineProperty(exports, "appendAssetLocation", {
|
|
1991
|
+
enumerable: true,
|
|
1992
|
+
get: function() {
|
|
1993
|
+
return appendAssetLocation;
|
|
1994
|
+
}
|
|
1995
|
+
});
|
|
1996
|
+
Object.defineProperty(exports, "buildAssetUrl", {
|
|
1997
|
+
enumerable: true,
|
|
1998
|
+
get: function() {
|
|
1999
|
+
return buildAssetUrl;
|
|
2000
|
+
}
|
|
2001
|
+
});
|
|
1334
2002
|
Object.defineProperty(exports, "checkOptionKeys", {
|
|
1335
2003
|
enumerable: true,
|
|
1336
2004
|
get: function() {
|
|
1337
2005
|
return checkOptionKeys;
|
|
1338
2006
|
}
|
|
1339
2007
|
});
|
|
1340
|
-
Object.defineProperty(exports, "
|
|
2008
|
+
Object.defineProperty(exports, "checkTransformOptions", {
|
|
1341
2009
|
enumerable: true,
|
|
1342
2010
|
get: function() {
|
|
1343
|
-
return
|
|
2011
|
+
return checkTransformOptions;
|
|
1344
2012
|
}
|
|
1345
2013
|
});
|
|
1346
|
-
Object.defineProperty(exports, "
|
|
2014
|
+
Object.defineProperty(exports, "encodePathSegment", {
|
|
1347
2015
|
enumerable: true,
|
|
1348
2016
|
get: function() {
|
|
1349
|
-
return
|
|
2017
|
+
return encodePathSegment;
|
|
1350
2018
|
}
|
|
1351
2019
|
});
|
|
1352
|
-
Object.defineProperty(exports, "
|
|
2020
|
+
Object.defineProperty(exports, "getAssetInfo", {
|
|
1353
2021
|
enumerable: true,
|
|
1354
2022
|
get: function() {
|
|
1355
|
-
return
|
|
2023
|
+
return getAssetInfo;
|
|
1356
2024
|
}
|
|
1357
2025
|
});
|
|
1358
|
-
Object.defineProperty(exports, "
|
|
2026
|
+
Object.defineProperty(exports, "getRejectedUploadMimeTypeError", {
|
|
1359
2027
|
enumerable: true,
|
|
1360
2028
|
get: function() {
|
|
1361
|
-
return
|
|
2029
|
+
return getRejectedUploadMimeTypeError;
|
|
1362
2030
|
}
|
|
1363
2031
|
});
|
|
1364
2032
|
Object.defineProperty(exports, "invalidArgument", {
|
|
@@ -1373,6 +2041,12 @@ Object.defineProperty(exports, "invalidConfig", {
|
|
|
1373
2041
|
return invalidConfig;
|
|
1374
2042
|
}
|
|
1375
2043
|
});
|
|
2044
|
+
Object.defineProperty(exports, "isAcceptedUploadMimeType", {
|
|
2045
|
+
enumerable: true,
|
|
2046
|
+
get: function() {
|
|
2047
|
+
return isAcceptedUploadMimeType;
|
|
2048
|
+
}
|
|
2049
|
+
});
|
|
1376
2050
|
Object.defineProperty(exports, "isBrowserRuntime", {
|
|
1377
2051
|
enumerable: true,
|
|
1378
2052
|
get: function() {
|
|
@@ -1385,10 +2059,16 @@ Object.defineProperty(exports, "isReactNativeRuntime", {
|
|
|
1385
2059
|
return isReactNativeRuntime;
|
|
1386
2060
|
}
|
|
1387
2061
|
});
|
|
1388
|
-
Object.defineProperty(exports, "
|
|
2062
|
+
Object.defineProperty(exports, "normalizeUploadMimeType", {
|
|
2063
|
+
enumerable: true,
|
|
2064
|
+
get: function() {
|
|
2065
|
+
return normalizeUploadMimeType;
|
|
2066
|
+
}
|
|
2067
|
+
});
|
|
2068
|
+
Object.defineProperty(exports, "parseTransformSelector", {
|
|
1389
2069
|
enumerable: true,
|
|
1390
2070
|
get: function() {
|
|
1391
|
-
return
|
|
2071
|
+
return parseTransformSelector;
|
|
1392
2072
|
}
|
|
1393
2073
|
});
|
|
1394
2074
|
Object.defineProperty(exports, "pollForDecision", {
|
|
@@ -1415,6 +2095,12 @@ Object.defineProperty(exports, "serializeTransform", {
|
|
|
1415
2095
|
return serializeTransform;
|
|
1416
2096
|
}
|
|
1417
2097
|
});
|
|
2098
|
+
Object.defineProperty(exports, "sleep", {
|
|
2099
|
+
enumerable: true,
|
|
2100
|
+
get: function() {
|
|
2101
|
+
return sleep;
|
|
2102
|
+
}
|
|
2103
|
+
});
|
|
1418
2104
|
Object.defineProperty(exports, "stringifyQuery", {
|
|
1419
2105
|
enumerable: true,
|
|
1420
2106
|
get: function() {
|
|
@@ -1427,6 +2113,12 @@ Object.defineProperty(exports, "toTextAssetOptions", {
|
|
|
1427
2113
|
return toTextAssetOptions;
|
|
1428
2114
|
}
|
|
1429
2115
|
});
|
|
2116
|
+
Object.defineProperty(exports, "toUrlViewerClaims", {
|
|
2117
|
+
enumerable: true,
|
|
2118
|
+
get: function() {
|
|
2119
|
+
return toUrlViewerClaims;
|
|
2120
|
+
}
|
|
2121
|
+
});
|
|
1430
2122
|
Object.defineProperty(exports, "transformSelector", {
|
|
1431
2123
|
enumerable: true,
|
|
1432
2124
|
get: function() {
|
|
@@ -1457,9 +2149,9 @@ Object.defineProperty(exports, "validateServerClientOptions", {
|
|
|
1457
2149
|
return validateServerClientOptions;
|
|
1458
2150
|
}
|
|
1459
2151
|
});
|
|
1460
|
-
Object.defineProperty(exports, "
|
|
2152
|
+
Object.defineProperty(exports, "validateSignAssetUrlOptions", {
|
|
1461
2153
|
enumerable: true,
|
|
1462
2154
|
get: function() {
|
|
1463
|
-
return
|
|
2155
|
+
return validateSignAssetUrlOptions;
|
|
1464
2156
|
}
|
|
1465
2157
|
});
|