calmlens 0.20.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 +1329 -664
- package/cjs/{BrowserUpload-CjSHjx3C.js → BrowserUpload-CeSwiMo5.js} +1 -1
- package/cjs/{BrowserUpload-C07aL9Vq.d.ts → BrowserUpload-D-NnAN7g.d.ts} +2 -2
- package/cjs/{CalmLensAppClient-NaCxOVlM.js → CalmLensAppClient-0UVCuNqW.js} +30 -10
- package/cjs/{CalmLensAppClient-DDLojj0P.d.ts → CalmLensAppClient-gG16T2py.d.ts} +13 -3
- package/cjs/{CalmLensOptionKeys-p2QK687A.js → CalmLensOptionKeys-CYY6elxg.js} +183 -347
- package/cjs/{CalmLensServerClient-C-jsjn8m.d.ts → CalmLensServerClient-BHx3x_nZ.d.ts} +106 -48
- package/cjs/{CalmLensServerClient-BDzTthxE.js → CalmLensServerClient-nfu6_YEW.js} +288 -88
- package/cjs/{CalmLensTypes-COJbOuiv.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-2nIv7Zh3.js → ModerationContextLimits-BxOmeSqq.js} +669 -269
- package/cjs/{ReactUpload-BdTRCTGA.js → ReactUpload-BK2O3dnK.js} +1 -1
- package/cjs/{ReactUpload-CRJNPB5T.d.ts → ReactUpload-DiiXfKvj.d.ts} +1 -1
- package/cjs/{TestTriggers-DbhsNPdg-DjvVuc2h.js → TestTriggers-BTyXtX0Q-ATuFDhLX.js} +342 -34
- package/{esm/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → cjs/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
- package/cjs/{UploadController-MLVs97Zt.js → UploadController-DLyiLDTB.js} +34 -14
- package/cjs/{WebhookPayload-DkukERKE.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 +24 -14
- package/cjs/testing.js +1413 -402
- 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/esm/{BrowserUpload-C07aL9Vq.d.ts → BrowserUpload-D-NnAN7g.d.ts} +2 -2
- package/esm/{BrowserUpload-CAVkRotw.js → BrowserUpload-i9OhCzMJ.js} +1 -1
- package/esm/{CalmLensAppClient-PQ9VBvlW.js → CalmLensAppClient-CK4_Py0c.js} +30 -10
- package/esm/{CalmLensAppClient-DDLojj0P.d.ts → CalmLensAppClient-gG16T2py.d.ts} +13 -3
- package/esm/{CalmLensOptionKeys-Do2mPP6w.js → CalmLensOptionKeys-BGZlIsT2.js} +182 -340
- package/esm/{CalmLensServerClient-C-jsjn8m.d.ts → CalmLensServerClient-BHx3x_nZ.d.ts} +106 -48
- package/esm/{CalmLensServerClient-C_H5aAsV.js → CalmLensServerClient-dAQUcDWb.js} +265 -89
- package/esm/{CalmLensTypes-COJbOuiv.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-Chw-AzgL.js → ModerationContextLimits-B6_RalCQ.js} +624 -254
- package/esm/{ReactUpload-CRJNPB5T.d.ts → ReactUpload-DiiXfKvj.d.ts} +1 -1
- package/esm/{ReactUpload-CMmL0R5Q.js → ReactUpload-DxwVqfX1.js} +1 -1
- package/esm/{TestTriggers-DbhsNPdg-CUdrv8Nm.js → TestTriggers-BTyXtX0Q-GRB9jQwo.js} +319 -35
- package/{cjs/TestTriggers-D-fwfhEP-hlaP1oW2.d.ts → esm/TestTriggers-D0pYHbkr-D5T0VdSn.d.ts} +146 -8
- package/esm/{UploadController-BJZghJpg.js → UploadController-D9Z-h00Q.js} +34 -14
- package/esm/{WebhookPayload-DkukERKE.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 +24 -14
- package/esm/testing.js +1409 -398
- 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-DxbJ_Xaa.d.ts +0 -253
- package/cjs/ui-BSqPGKrZ.d.ts +0 -539
- package/cjs/ui-CX1z2pD9.js +0 -586
- package/esm/ModerationContext-9lGm-Mna-CcmbRnOy.d.ts +0 -67
- package/esm/index-DxbJ_Xaa.d.ts +0 -253
- package/esm/svelte/CalmLensMedia.svelte +0 -134
- package/esm/ui-BSqPGKrZ.d.ts +0 -539
- package/esm/ui-CXH7xJpb.js +0 -461
package/esm/testing.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { a as WEBHOOK_TIMESTAMP_HEADER,
|
|
3
|
-
import {
|
|
1
|
+
import { A as getRejectedUploadMimeTypeError, B as invalidArgument, H as API_ERROR_CODES, N as sleep, O as MAX_IMAGE_UPLOAD_BYTES, R as CalmLensError, S as validateAppClientOptions, T as validateServerClientOptions, U as API_ERROR_DEFINITIONS, V as invalidConfig, _ as randomUuid, f as checkTransformOptions, h as parseTransformSelector, j as normalizeUploadMimeType, m as isAcceptedUploadMimeType, r as selectModerationContextItems, z as SDK_ERROR_CODES } from "./ModerationContextLimits-B6_RalCQ.js";
|
|
2
|
+
import { a as WEBHOOK_TIMESTAMP_HEADER, f as signUploadTokenWithApiKey, m as stringifyJsonCanonical, n as WEBHOOK_DELIVERY_HEADER, p as decodeUploadToken, r as WEBHOOK_SIGNATURE_HEADER, t as CalmLensServerClient } from "./CalmLensServerClient-dAQUcDWb.js";
|
|
3
|
+
import { a as MOCK_HOOKS, g as validateAssetPayload, h as assetToData, m as assetDataToUint8Array } from "./CalmLensOptionKeys-BGZlIsT2.js";
|
|
4
4
|
import { t as hmacSha256Hex } from "./CryptoUtils-B_x8lMIx-DhuWGzqn.js";
|
|
5
|
-
import { t as CalmLensAppClient } from "./CalmLensAppClient-
|
|
6
|
-
import { a as placeholderHashToDataURL, r as isFileAccepted } from "./UploadController-
|
|
7
|
-
import {
|
|
5
|
+
import { t as CalmLensAppClient } from "./CalmLensAppClient-CK4_Py0c.js";
|
|
6
|
+
import { a as placeholderHashToDataURL, r as isFileAccepted } from "./UploadController-D9Z-h00Q.js";
|
|
7
|
+
import { d as getTestInputTag, m as CLASSIFICATION_CATEGORIES } from "./TestTriggers-BTyXtX0Q-GRB9jQwo.js";
|
|
8
8
|
import * as zod from "zod";
|
|
9
9
|
// src/contract/MediaTestTriggers.js
|
|
10
10
|
const MEDIA_TEST_SIGNALS = [...CLASSIFICATION_CATEGORIES, "violence.graphic"];
|
|
@@ -31,7 +31,7 @@ async function generateMediaTestCode(signal, testInputKey) {
|
|
|
31
31
|
if (!MEDIA_TEST_SIGNALS.includes(signal)) throw new RangeError("Unsupported media test signal");
|
|
32
32
|
return `Calm Lens test ${[...await getTestInputTag("media", signal, testInputKey)].map((character) => HEX_WORDS[Number.parseInt(character, 16)]).join(" ")} ${signal.replace(/[.-]/g, " ")} end test`;
|
|
33
33
|
}
|
|
34
|
-
// src/contract/Ids-
|
|
34
|
+
// src/contract/Ids-BESMSxGb.js
|
|
35
35
|
var __defProp = Object.defineProperty;
|
|
36
36
|
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod, mod.exports), cb = null), mod.exports);
|
|
37
37
|
var __exportAll = (all, no_symbols) => {
|
|
@@ -7907,6 +7907,7 @@ const ID_PREFIXES = {
|
|
|
7907
7907
|
member: "mem",
|
|
7908
7908
|
invitation: "inv",
|
|
7909
7909
|
transfer: "xfer",
|
|
7910
|
+
assetBatch: "batch",
|
|
7910
7911
|
supportTicket: "tkt",
|
|
7911
7912
|
supportTicketMessage: "msg",
|
|
7912
7913
|
humanReview: "rev",
|
|
@@ -7968,6 +7969,7 @@ const EXAMPLE_IDS = {
|
|
|
7968
7969
|
mem: "mem_9Xm4Rk7Lq2Vt8Wb3Np5C",
|
|
7969
7970
|
inv: "inv_3Qk8Xm2Rt9Lv4Wb7Np6C",
|
|
7970
7971
|
xfer: "xfer_6Lq2Xm8Rk4Vt9Wb3Np7C",
|
|
7972
|
+
batch: "batch_5Rt8Xm3Lq9Vk2Wb7Np4C",
|
|
7971
7973
|
tkt: "tkt_6Xm2Rk9Lq4Vt7Wb3Np8C",
|
|
7972
7974
|
msg: "msg_2Rk7Xm4Lq9Vt3Wb8Np5C",
|
|
7973
7975
|
rev: "rev_8Xm3Rk2Lq7Vt9Wb4Np6C",
|
|
@@ -7985,7 +7987,7 @@ const EXAMPLE_IDS = {
|
|
|
7985
7987
|
* across the packages that build records from plain strings).
|
|
7986
7988
|
*/
|
|
7987
7989
|
const idField = (prefix) => zod.string().regex(idPattern(prefix), { message: `Must be a valid ID: "${prefix}_" followed by 20 letters or digits` }).meta({ examples: [EXAMPLE_IDS[prefix]] });
|
|
7988
|
-
// src/contract/Permissions-
|
|
7990
|
+
// src/contract/Permissions-BqpmE0Qa.js
|
|
7989
7991
|
const ACTION_SCHEMA = zod.enum([
|
|
7990
7992
|
"assets:read",
|
|
7991
7993
|
"assets:download",
|
|
@@ -8106,6 +8108,8 @@ const MOCK_QUALIFIERS = {
|
|
|
8106
8108
|
"sexual-non-consensual",
|
|
8107
8109
|
"sexual-intimate-image-abuse",
|
|
8108
8110
|
"sexual-extreme",
|
|
8111
|
+
"sexual-strangulation",
|
|
8112
|
+
"sexual-incest",
|
|
8109
8113
|
"sexual-minors",
|
|
8110
8114
|
"sexual-solicitation",
|
|
8111
8115
|
"sexual-paid",
|
|
@@ -8317,7 +8321,7 @@ var MockDecision = class {
|
|
|
8317
8321
|
}
|
|
8318
8322
|
/**
|
|
8319
8323
|
* The report a decided asset gets, as the API sends it: every category (unflagged ones
|
|
8320
|
-
* evaluated with score 0), policy-adjusted results and the automated
|
|
8324
|
+
* evaluated with score 0), policy-adjusted results and the automated status and
|
|
8321
8325
|
* `flaggedCategories`. `undefined` for `pending` and `failed`.
|
|
8322
8326
|
*/
|
|
8323
8327
|
report() {
|
|
@@ -8350,10 +8354,16 @@ var MockDecision = class {
|
|
|
8350
8354
|
}])),
|
|
8351
8355
|
explanationComplete: true,
|
|
8352
8356
|
source: "policy",
|
|
8353
|
-
|
|
8357
|
+
rule: null,
|
|
8358
|
+
automatedStatus: status,
|
|
8354
8359
|
regions: [],
|
|
8360
|
+
linkSafety: null,
|
|
8361
|
+
timeline: null,
|
|
8355
8362
|
flaggedCategories: evaluated.filter(([, score]) => action(score) !== "allow").map(([category]) => category),
|
|
8356
|
-
raw: {
|
|
8363
|
+
raw: {
|
|
8364
|
+
categories,
|
|
8365
|
+
hashMatches: []
|
|
8366
|
+
}
|
|
8357
8367
|
};
|
|
8358
8368
|
}
|
|
8359
8369
|
};
|
|
@@ -8361,17 +8371,395 @@ var MockDecision = class {
|
|
|
8361
8371
|
const mockDecision = (status) => new MockDecision(status);
|
|
8362
8372
|
/** The default `moderate`: queued work stays pending until `completeProcessing`, which allows it. */
|
|
8363
8373
|
const defaultModerate = ({ trigger, queued }) => queued && trigger !== "complete" ? "pending" : "allowed";
|
|
8374
|
+
// src/testing/MockAssetFilters.ts
|
|
8375
|
+
/** The asset fields a list filter may name, each with the operators it accepts. */
|
|
8376
|
+
const MOCK_ASSET_FILTER_FIELDS = {
|
|
8377
|
+
id: {
|
|
8378
|
+
kind: "text",
|
|
8379
|
+
nullable: false,
|
|
8380
|
+
operators: [
|
|
8381
|
+
"==",
|
|
8382
|
+
"!=",
|
|
8383
|
+
">",
|
|
8384
|
+
">=",
|
|
8385
|
+
"<",
|
|
8386
|
+
"<=",
|
|
8387
|
+
"=in=",
|
|
8388
|
+
"=out="
|
|
8389
|
+
]
|
|
8390
|
+
},
|
|
8391
|
+
parentId: {
|
|
8392
|
+
kind: "text",
|
|
8393
|
+
nullable: true,
|
|
8394
|
+
operators: [
|
|
8395
|
+
"==",
|
|
8396
|
+
"!=",
|
|
8397
|
+
">",
|
|
8398
|
+
">=",
|
|
8399
|
+
"<",
|
|
8400
|
+
"<=",
|
|
8401
|
+
"=in=",
|
|
8402
|
+
"=out="
|
|
8403
|
+
]
|
|
8404
|
+
},
|
|
8405
|
+
folderId: {
|
|
8406
|
+
kind: "text",
|
|
8407
|
+
nullable: true,
|
|
8408
|
+
operators: [
|
|
8409
|
+
"==",
|
|
8410
|
+
"!=",
|
|
8411
|
+
">",
|
|
8412
|
+
">=",
|
|
8413
|
+
"<",
|
|
8414
|
+
"<=",
|
|
8415
|
+
"=in=",
|
|
8416
|
+
"=out="
|
|
8417
|
+
]
|
|
8418
|
+
},
|
|
8419
|
+
uploadPresetId: {
|
|
8420
|
+
kind: "text",
|
|
8421
|
+
nullable: true,
|
|
8422
|
+
operators: [
|
|
8423
|
+
"==",
|
|
8424
|
+
"!=",
|
|
8425
|
+
">",
|
|
8426
|
+
">=",
|
|
8427
|
+
"<",
|
|
8428
|
+
"<=",
|
|
8429
|
+
"=in=",
|
|
8430
|
+
"=out="
|
|
8431
|
+
]
|
|
8432
|
+
},
|
|
8433
|
+
createdAt: {
|
|
8434
|
+
kind: "number",
|
|
8435
|
+
nullable: false,
|
|
8436
|
+
operators: [
|
|
8437
|
+
"==",
|
|
8438
|
+
"!=",
|
|
8439
|
+
">",
|
|
8440
|
+
">=",
|
|
8441
|
+
"<",
|
|
8442
|
+
"<=",
|
|
8443
|
+
"=in=",
|
|
8444
|
+
"=out="
|
|
8445
|
+
]
|
|
8446
|
+
},
|
|
8447
|
+
updatedAt: {
|
|
8448
|
+
kind: "number",
|
|
8449
|
+
nullable: false,
|
|
8450
|
+
operators: [
|
|
8451
|
+
"==",
|
|
8452
|
+
"!=",
|
|
8453
|
+
">",
|
|
8454
|
+
">=",
|
|
8455
|
+
"<",
|
|
8456
|
+
"<=",
|
|
8457
|
+
"=in=",
|
|
8458
|
+
"=out="
|
|
8459
|
+
]
|
|
8460
|
+
},
|
|
8461
|
+
type: {
|
|
8462
|
+
kind: "text",
|
|
8463
|
+
nullable: false,
|
|
8464
|
+
operators: [
|
|
8465
|
+
"==",
|
|
8466
|
+
"!=",
|
|
8467
|
+
">",
|
|
8468
|
+
">=",
|
|
8469
|
+
"<",
|
|
8470
|
+
"<=",
|
|
8471
|
+
"=in=",
|
|
8472
|
+
"=out="
|
|
8473
|
+
]
|
|
8474
|
+
},
|
|
8475
|
+
name: {
|
|
8476
|
+
kind: "text",
|
|
8477
|
+
nullable: false,
|
|
8478
|
+
operators: [
|
|
8479
|
+
"==",
|
|
8480
|
+
"!=",
|
|
8481
|
+
">",
|
|
8482
|
+
">=",
|
|
8483
|
+
"<",
|
|
8484
|
+
"<=",
|
|
8485
|
+
"=in=",
|
|
8486
|
+
"=out="
|
|
8487
|
+
]
|
|
8488
|
+
},
|
|
8489
|
+
description: {
|
|
8490
|
+
kind: "text",
|
|
8491
|
+
nullable: true,
|
|
8492
|
+
operators: [
|
|
8493
|
+
"==",
|
|
8494
|
+
"!=",
|
|
8495
|
+
">",
|
|
8496
|
+
">=",
|
|
8497
|
+
"<",
|
|
8498
|
+
"<=",
|
|
8499
|
+
"=in=",
|
|
8500
|
+
"=out="
|
|
8501
|
+
]
|
|
8502
|
+
},
|
|
8503
|
+
externalId: {
|
|
8504
|
+
kind: "text",
|
|
8505
|
+
nullable: true,
|
|
8506
|
+
operators: [
|
|
8507
|
+
"==",
|
|
8508
|
+
"!=",
|
|
8509
|
+
">",
|
|
8510
|
+
">=",
|
|
8511
|
+
"<",
|
|
8512
|
+
"<=",
|
|
8513
|
+
"=in=",
|
|
8514
|
+
"=out="
|
|
8515
|
+
]
|
|
8516
|
+
},
|
|
8517
|
+
externalUrl: {
|
|
8518
|
+
kind: "text",
|
|
8519
|
+
nullable: true,
|
|
8520
|
+
operators: [
|
|
8521
|
+
"==",
|
|
8522
|
+
"!=",
|
|
8523
|
+
">",
|
|
8524
|
+
">=",
|
|
8525
|
+
"<",
|
|
8526
|
+
"<=",
|
|
8527
|
+
"=in=",
|
|
8528
|
+
"=out="
|
|
8529
|
+
]
|
|
8530
|
+
},
|
|
8531
|
+
authorId: {
|
|
8532
|
+
kind: "text",
|
|
8533
|
+
nullable: true,
|
|
8534
|
+
operators: [
|
|
8535
|
+
"==",
|
|
8536
|
+
"!=",
|
|
8537
|
+
">",
|
|
8538
|
+
">=",
|
|
8539
|
+
"<",
|
|
8540
|
+
"<=",
|
|
8541
|
+
"=in=",
|
|
8542
|
+
"=out="
|
|
8543
|
+
]
|
|
8544
|
+
},
|
|
8545
|
+
tags: {
|
|
8546
|
+
kind: "list",
|
|
8547
|
+
nullable: false,
|
|
8548
|
+
operators: ["=in=", "=out="]
|
|
8549
|
+
},
|
|
8550
|
+
visibility: {
|
|
8551
|
+
kind: "text",
|
|
8552
|
+
nullable: true,
|
|
8553
|
+
operators: [
|
|
8554
|
+
"==",
|
|
8555
|
+
"!=",
|
|
8556
|
+
">",
|
|
8557
|
+
">=",
|
|
8558
|
+
"<",
|
|
8559
|
+
"<=",
|
|
8560
|
+
"=in=",
|
|
8561
|
+
"=out="
|
|
8562
|
+
]
|
|
8563
|
+
},
|
|
8564
|
+
"upload.status": {
|
|
8565
|
+
kind: "text",
|
|
8566
|
+
nullable: false,
|
|
8567
|
+
operators: [
|
|
8568
|
+
"==",
|
|
8569
|
+
"!=",
|
|
8570
|
+
">",
|
|
8571
|
+
">=",
|
|
8572
|
+
"<",
|
|
8573
|
+
"<=",
|
|
8574
|
+
"=in=",
|
|
8575
|
+
"=out="
|
|
8576
|
+
]
|
|
8577
|
+
},
|
|
8578
|
+
"upload.updatedAt": {
|
|
8579
|
+
kind: "number",
|
|
8580
|
+
nullable: true,
|
|
8581
|
+
operators: [
|
|
8582
|
+
"==",
|
|
8583
|
+
"!=",
|
|
8584
|
+
">",
|
|
8585
|
+
">=",
|
|
8586
|
+
"<",
|
|
8587
|
+
"<=",
|
|
8588
|
+
"=in=",
|
|
8589
|
+
"=out="
|
|
8590
|
+
]
|
|
8591
|
+
},
|
|
8592
|
+
"classification.status": {
|
|
8593
|
+
kind: "text",
|
|
8594
|
+
nullable: false,
|
|
8595
|
+
operators: [
|
|
8596
|
+
"==",
|
|
8597
|
+
"!=",
|
|
8598
|
+
">",
|
|
8599
|
+
">=",
|
|
8600
|
+
"<",
|
|
8601
|
+
"<=",
|
|
8602
|
+
"=in=",
|
|
8603
|
+
"=out="
|
|
8604
|
+
]
|
|
8605
|
+
},
|
|
8606
|
+
"classification.updatedAt": {
|
|
8607
|
+
kind: "number",
|
|
8608
|
+
nullable: true,
|
|
8609
|
+
operators: [
|
|
8610
|
+
"==",
|
|
8611
|
+
"!=",
|
|
8612
|
+
">",
|
|
8613
|
+
">=",
|
|
8614
|
+
"<",
|
|
8615
|
+
"<=",
|
|
8616
|
+
"=in=",
|
|
8617
|
+
"=out="
|
|
8618
|
+
]
|
|
8619
|
+
},
|
|
8620
|
+
"classification.completedAt": {
|
|
8621
|
+
kind: "number",
|
|
8622
|
+
nullable: true,
|
|
8623
|
+
operators: [
|
|
8624
|
+
"==",
|
|
8625
|
+
"!=",
|
|
8626
|
+
">",
|
|
8627
|
+
">=",
|
|
8628
|
+
"<",
|
|
8629
|
+
"<=",
|
|
8630
|
+
"=in=",
|
|
8631
|
+
"=out="
|
|
8632
|
+
]
|
|
8633
|
+
},
|
|
8634
|
+
"classification.moderation.status": {
|
|
8635
|
+
kind: "text",
|
|
8636
|
+
nullable: false,
|
|
8637
|
+
operators: [
|
|
8638
|
+
"==",
|
|
8639
|
+
"!=",
|
|
8640
|
+
">",
|
|
8641
|
+
">=",
|
|
8642
|
+
"<",
|
|
8643
|
+
"<=",
|
|
8644
|
+
"=in=",
|
|
8645
|
+
"=out="
|
|
8646
|
+
]
|
|
8647
|
+
},
|
|
8648
|
+
"content.status": {
|
|
8649
|
+
kind: "text",
|
|
8650
|
+
nullable: false,
|
|
8651
|
+
operators: [
|
|
8652
|
+
"==",
|
|
8653
|
+
"!=",
|
|
8654
|
+
">",
|
|
8655
|
+
">=",
|
|
8656
|
+
"<",
|
|
8657
|
+
"<=",
|
|
8658
|
+
"=in=",
|
|
8659
|
+
"=out="
|
|
8660
|
+
]
|
|
8661
|
+
},
|
|
8662
|
+
"content.removalReason": {
|
|
8663
|
+
kind: "text",
|
|
8664
|
+
nullable: true,
|
|
8665
|
+
operators: [
|
|
8666
|
+
"==",
|
|
8667
|
+
"!=",
|
|
8668
|
+
">",
|
|
8669
|
+
">=",
|
|
8670
|
+
"<",
|
|
8671
|
+
"<=",
|
|
8672
|
+
"=in=",
|
|
8673
|
+
"=out="
|
|
8674
|
+
]
|
|
8675
|
+
},
|
|
8676
|
+
"content.updatedAt": {
|
|
8677
|
+
kind: "number",
|
|
8678
|
+
nullable: true,
|
|
8679
|
+
operators: [
|
|
8680
|
+
"==",
|
|
8681
|
+
"!=",
|
|
8682
|
+
">",
|
|
8683
|
+
">=",
|
|
8684
|
+
"<",
|
|
8685
|
+
"<=",
|
|
8686
|
+
"=in=",
|
|
8687
|
+
"=out="
|
|
8688
|
+
]
|
|
8689
|
+
},
|
|
8690
|
+
"content.format": {
|
|
8691
|
+
kind: "text",
|
|
8692
|
+
nullable: true,
|
|
8693
|
+
operators: [
|
|
8694
|
+
"==",
|
|
8695
|
+
"!=",
|
|
8696
|
+
">",
|
|
8697
|
+
">=",
|
|
8698
|
+
"<",
|
|
8699
|
+
"<=",
|
|
8700
|
+
"=in=",
|
|
8701
|
+
"=out="
|
|
8702
|
+
]
|
|
8703
|
+
},
|
|
8704
|
+
"content.extension": {
|
|
8705
|
+
kind: "text",
|
|
8706
|
+
nullable: true,
|
|
8707
|
+
operators: [
|
|
8708
|
+
"==",
|
|
8709
|
+
"!=",
|
|
8710
|
+
">",
|
|
8711
|
+
">=",
|
|
8712
|
+
"<",
|
|
8713
|
+
"<=",
|
|
8714
|
+
"=in=",
|
|
8715
|
+
"=out="
|
|
8716
|
+
]
|
|
8717
|
+
},
|
|
8718
|
+
"content.sizeInBytes": {
|
|
8719
|
+
kind: "number",
|
|
8720
|
+
nullable: true,
|
|
8721
|
+
operators: [
|
|
8722
|
+
"==",
|
|
8723
|
+
"!=",
|
|
8724
|
+
">",
|
|
8725
|
+
">=",
|
|
8726
|
+
"<",
|
|
8727
|
+
"<=",
|
|
8728
|
+
"=in=",
|
|
8729
|
+
"=out="
|
|
8730
|
+
]
|
|
8731
|
+
}
|
|
8732
|
+
};
|
|
8364
8733
|
// src/testing/MockUtils.ts
|
|
8734
|
+
/**
|
|
8735
|
+
* A `sort` query value as the API parses it (`sortQueryField` in shared/Page.ts): comma-separated
|
|
8736
|
+
* fields from `fields`, descending when prefixed with `-`, none repeated and at most `max`.
|
|
8737
|
+
* Undefined for a list that sends none; `undefined`'s default applies.
|
|
8738
|
+
*/
|
|
8739
|
+
const parseSort = (value, fields, max, fail) => {
|
|
8740
|
+
if (value === null) return void 0;
|
|
8741
|
+
const sort = [];
|
|
8742
|
+
for (const part of value.split(",").map((item) => item.trim())) {
|
|
8743
|
+
const field = part.startsWith("-") ? part.slice(1) : part;
|
|
8744
|
+
if (!/^-?[A-Za-z][A-Za-z0-9.]*$/.test(part) || !fields.includes(field)) throw fail();
|
|
8745
|
+
if (sort.some((rule) => rule.field === field)) throw fail();
|
|
8746
|
+
sort.push({
|
|
8747
|
+
field,
|
|
8748
|
+
direction: part.startsWith("-") ? "desc" : "asc"
|
|
8749
|
+
});
|
|
8750
|
+
}
|
|
8751
|
+
if (sort.length > max) throw fail();
|
|
8752
|
+
return sort;
|
|
8753
|
+
};
|
|
8365
8754
|
const toBase64Utf8 = (value) => btoa(Array.from(new TextEncoder().encode(value), (byte) => String.fromCharCode(byte)).join(""));
|
|
8366
8755
|
const fromBase64Utf8 = (value) => new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(atob(value), (char) => char.charCodeAt(0)));
|
|
8367
8756
|
/**
|
|
8368
8757
|
* One keyset page like the API's (packages/shared-backend/src/CursorPagination.ts): ordered by
|
|
8369
|
-
* `
|
|
8370
|
-
*
|
|
8371
|
-
*
|
|
8372
|
-
* point, like the database.
|
|
8758
|
+
* `sort` (newest first by default) with an `id` tie-breaker added, starting after the cursor's
|
|
8759
|
+
* values. The cursor is tied to `scope` (the filter, folder or parent) and the sort, so it only
|
|
8760
|
+
* continues the query it came from. `name` and `id` are compared by code point, like the database.
|
|
8373
8761
|
*/
|
|
8374
|
-
const createPage = (items, query, { scope, fail, defaultPageSize = 50,
|
|
8762
|
+
const createPage = (items, query, { scope, fail, defaultPageSize = 50, defaultSort = [{
|
|
8375
8763
|
field: "createdAt",
|
|
8376
8764
|
direction: "desc"
|
|
8377
8765
|
}] }) => {
|
|
@@ -8379,7 +8767,7 @@ const createPage = (items, query, { scope, fail, defaultPageSize = 50, defaultOr
|
|
|
8379
8767
|
if (typeof pageSize !== "number" || !Number.isFinite(pageSize) || pageSize < 1) throw fail("validation/too-small", "pageSize");
|
|
8380
8768
|
if (pageSize > 100) throw fail("validation/too-big", "pageSize");
|
|
8381
8769
|
if (!Number.isInteger(pageSize)) throw invalidArgument("Invalid page size");
|
|
8382
|
-
const ordering = query.
|
|
8770
|
+
const ordering = query.sort?.length ? [...query.sort] : [...defaultSort];
|
|
8383
8771
|
if (!ordering.some(({ field }) => field === "id")) ordering.push({
|
|
8384
8772
|
field: "id",
|
|
8385
8773
|
direction: ordering[0].direction
|
|
@@ -8393,7 +8781,7 @@ const createPage = (items, query, { scope, fail, defaultPageSize = 50, defaultOr
|
|
|
8393
8781
|
values = parsed?.values;
|
|
8394
8782
|
if (parsed?.scope !== signature || !Array.isArray(values) || values.length !== ordering.length || values.some((value, i) => STRING_FIELDS.has(ordering[i].field) ? typeof value !== "string" : typeof value !== "number" || !Number.isFinite(value))) throw new Error();
|
|
8395
8783
|
} catch {
|
|
8396
|
-
throw fail("
|
|
8784
|
+
throw fail("cursor/invalid");
|
|
8397
8785
|
}
|
|
8398
8786
|
remaining = remaining.filter((item) => {
|
|
8399
8787
|
for (const [i, rule] of ordering.entries()) {
|
|
@@ -8408,9 +8796,7 @@ const createPage = (items, query, { scope, fail, defaultPageSize = 50, defaultOr
|
|
|
8408
8796
|
const last = pageItems.at(-1);
|
|
8409
8797
|
return {
|
|
8410
8798
|
items: pageItems,
|
|
8411
|
-
pageSize,
|
|
8412
8799
|
hasMore,
|
|
8413
|
-
ordering,
|
|
8414
8800
|
nextCursor: hasMore && last ? toBase64Utf8(JSON.stringify({
|
|
8415
8801
|
scope: signature,
|
|
8416
8802
|
values: ordering.map(({ field }) => last[field])
|
|
@@ -8426,7 +8812,7 @@ const compareValues = (left, right) => {
|
|
|
8426
8812
|
const [a, b] = [String(left), String(right)];
|
|
8427
8813
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
8428
8814
|
};
|
|
8429
|
-
/** Sorts like the API's `
|
|
8815
|
+
/** Sorts like the API's `sort`; without one, newest first. */
|
|
8430
8816
|
const orderItems = (items, ordering) => {
|
|
8431
8817
|
const rules = ordering?.length ? ordering : [{
|
|
8432
8818
|
field: "createdAt",
|
|
@@ -8440,90 +8826,196 @@ const orderItems = (items, ordering) => {
|
|
|
8440
8826
|
return 0;
|
|
8441
8827
|
});
|
|
8442
8828
|
};
|
|
8443
|
-
|
|
8444
|
-
|
|
8445
|
-
|
|
8446
|
-
|
|
8447
|
-
|
|
8448
|
-
|
|
8449
|
-
|
|
8450
|
-
|
|
8451
|
-
|
|
8829
|
+
/** Thrown for a filter the API refuses with `filter/invalid`. */
|
|
8830
|
+
var MockInvalidFilter = class extends Error {};
|
|
8831
|
+
/** The characters that end an unquoted word, as in @rsql/parser. */
|
|
8832
|
+
const RESERVED = "\"'();,=!~<> \n \r";
|
|
8833
|
+
const LOGIC_WORDS = ["and", "or"];
|
|
8834
|
+
/** Splits a filter into tokens like @rsql/parser's lexer. */
|
|
8835
|
+
const tokenize = (filter) => {
|
|
8836
|
+
const tokens = [];
|
|
8837
|
+
let i = 0;
|
|
8838
|
+
while (i < filter.length) {
|
|
8839
|
+
const char = filter[i];
|
|
8840
|
+
if (" \n \r".includes(char)) i++;
|
|
8841
|
+
else if (char === "\"" || char === "'") {
|
|
8842
|
+
let text = "";
|
|
8843
|
+
let j = i + 1;
|
|
8844
|
+
for (; j < filter.length && filter[j] !== char; j++) {
|
|
8845
|
+
if (filter[j] === "\\" && j + 1 < filter.length) j++;
|
|
8846
|
+
text += filter[j];
|
|
8847
|
+
}
|
|
8848
|
+
if (j >= filter.length) throw new MockInvalidFilter();
|
|
8849
|
+
tokens.push({
|
|
8850
|
+
kind: "quoted",
|
|
8851
|
+
text
|
|
8852
|
+
});
|
|
8853
|
+
i = j + 1;
|
|
8854
|
+
} else if (char === "(" || char === ")") {
|
|
8855
|
+
tokens.push({
|
|
8856
|
+
kind: "paren",
|
|
8857
|
+
text: char
|
|
8858
|
+
});
|
|
8859
|
+
i++;
|
|
8860
|
+
} else if (char === ";" || char === ",") {
|
|
8861
|
+
tokens.push({
|
|
8862
|
+
kind: "logic",
|
|
8863
|
+
text: char === ";" ? "and" : "or"
|
|
8864
|
+
});
|
|
8865
|
+
i++;
|
|
8866
|
+
} else if ("=!~<>".includes(char)) {
|
|
8867
|
+
const operator = /^(==|!=|<=|>=|<|>|=[a-z]*=)/.exec(filter.slice(i))?.[0];
|
|
8868
|
+
if (!operator) throw new MockInvalidFilter();
|
|
8869
|
+
tokens.push({
|
|
8870
|
+
kind: "operator",
|
|
8871
|
+
text: operator
|
|
8872
|
+
});
|
|
8873
|
+
i += operator.length;
|
|
8874
|
+
} else {
|
|
8875
|
+
const word = LOGIC_WORDS.find((logic) => filter.startsWith(`${logic} `, i));
|
|
8876
|
+
if (word) {
|
|
8877
|
+
tokens.push({
|
|
8878
|
+
kind: "logic",
|
|
8879
|
+
text: word
|
|
8880
|
+
});
|
|
8881
|
+
i += word.length;
|
|
8882
|
+
continue;
|
|
8883
|
+
}
|
|
8884
|
+
let j = i + 1;
|
|
8885
|
+
while (j < filter.length && !RESERVED.includes(filter[j])) j++;
|
|
8886
|
+
tokens.push({
|
|
8887
|
+
kind: "word",
|
|
8888
|
+
text: filter.slice(i, j)
|
|
8889
|
+
});
|
|
8890
|
+
i = j;
|
|
8891
|
+
}
|
|
8892
|
+
}
|
|
8893
|
+
return tokens;
|
|
8894
|
+
};
|
|
8895
|
+
/**
|
|
8896
|
+
* A field's value as the API reads it (zodbase/rsql): `null` is a missing value only on a
|
|
8897
|
+
* nullable field, and a number field's value must be a finite number.
|
|
8898
|
+
*/
|
|
8899
|
+
const readFilterValue = (field, text) => {
|
|
8900
|
+
const { kind, nullable } = MOCK_ASSET_FILTER_FIELDS[field];
|
|
8901
|
+
if (text === "null" && nullable) return null;
|
|
8902
|
+
if (kind !== "number") return text;
|
|
8903
|
+
const number = Number(text);
|
|
8904
|
+
if (text === "" || !Number.isFinite(number)) throw new MockInvalidFilter();
|
|
8905
|
+
return number;
|
|
8906
|
+
};
|
|
8907
|
+
/** Parses a filter: `;` (and) binds tighter than `,` (or), and parentheses group. */
|
|
8908
|
+
const parseFilter = (filter) => {
|
|
8909
|
+
const tokens = tokenize(filter);
|
|
8910
|
+
let position = 0;
|
|
8911
|
+
const peek = () => tokens[position];
|
|
8912
|
+
const next = () => {
|
|
8913
|
+
const token = tokens[position++];
|
|
8914
|
+
if (!token) throw new MockInvalidFilter();
|
|
8915
|
+
return token;
|
|
8916
|
+
};
|
|
8917
|
+
const value = () => {
|
|
8918
|
+
const token = next();
|
|
8919
|
+
if (token.kind !== "word" && token.kind !== "quoted") throw new MockInvalidFilter();
|
|
8920
|
+
return token.text;
|
|
8921
|
+
};
|
|
8922
|
+
const term = () => {
|
|
8923
|
+
const token = next();
|
|
8924
|
+
if (token.kind === "paren" && token.text === "(") {
|
|
8925
|
+
const node = alternatives();
|
|
8926
|
+
if (next().text !== ")") throw new MockInvalidFilter();
|
|
8927
|
+
return node;
|
|
8928
|
+
}
|
|
8929
|
+
if (token.kind !== "word" || !Object.hasOwn(MOCK_ASSET_FILTER_FIELDS, token.text)) throw new MockInvalidFilter();
|
|
8930
|
+
const field = token.text;
|
|
8931
|
+
const operator = next();
|
|
8932
|
+
if (operator.kind !== "operator" || !MOCK_ASSET_FILTER_FIELDS[field].operators.includes(operator.text)) throw new MockInvalidFilter();
|
|
8933
|
+
let texts;
|
|
8934
|
+
if (peek()?.kind === "paren" && peek()?.text === "(") {
|
|
8935
|
+
next();
|
|
8936
|
+
texts = [value()];
|
|
8937
|
+
while (peek()?.kind === "logic" && peek()?.text === "or") {
|
|
8938
|
+
next();
|
|
8939
|
+
texts.push(value());
|
|
8940
|
+
}
|
|
8941
|
+
if (next().text !== ")") throw new MockInvalidFilter();
|
|
8942
|
+
} else texts = [value()];
|
|
8943
|
+
if (texts.length > 1 && operator.text !== "=in=" && operator.text !== "=out=") throw new MockInvalidFilter();
|
|
8944
|
+
return {
|
|
8945
|
+
type: "comparison",
|
|
8946
|
+
field,
|
|
8947
|
+
operator: operator.text,
|
|
8948
|
+
values: texts.map((text) => readFilterValue(field, text))
|
|
8949
|
+
};
|
|
8950
|
+
};
|
|
8951
|
+
const conjunction = () => {
|
|
8952
|
+
const nodes = [term()];
|
|
8953
|
+
while (peek()?.kind === "logic" && peek()?.text === "and") {
|
|
8954
|
+
next();
|
|
8955
|
+
nodes.push(term());
|
|
8956
|
+
}
|
|
8957
|
+
return nodes.length === 1 ? nodes[0] : {
|
|
8958
|
+
type: "and",
|
|
8959
|
+
nodes
|
|
8960
|
+
};
|
|
8961
|
+
};
|
|
8962
|
+
const alternatives = () => {
|
|
8963
|
+
const nodes = [conjunction()];
|
|
8964
|
+
while (peek()?.kind === "logic" && peek()?.text === "or") {
|
|
8965
|
+
next();
|
|
8966
|
+
nodes.push(conjunction());
|
|
8967
|
+
}
|
|
8968
|
+
return nodes.length === 1 ? nodes[0] : {
|
|
8969
|
+
type: "or",
|
|
8970
|
+
nodes
|
|
8971
|
+
};
|
|
8972
|
+
};
|
|
8973
|
+
const node = alternatives();
|
|
8974
|
+
if (position !== tokens.length) throw new MockInvalidFilter();
|
|
8975
|
+
return node;
|
|
8452
8976
|
};
|
|
8453
8977
|
const readField = (item, path) => path.split(".").reduce((value, key) => value && typeof value === "object" ? value[key] : void 0, item);
|
|
8454
|
-
|
|
8455
|
-
const
|
|
8456
|
-
|
|
8457
|
-
|
|
8458
|
-
const
|
|
8459
|
-
const
|
|
8460
|
-
|
|
8461
|
-
if (
|
|
8462
|
-
|
|
8463
|
-
const
|
|
8978
|
+
/** `*` matches any run of characters, regardless of case; every other character itself. */
|
|
8979
|
+
const wildcard = (pattern) => new RegExp(`^${pattern.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*")}$`, "isu");
|
|
8980
|
+
/** Compares like SQL: a missing value never matches, except `== null`. */
|
|
8981
|
+
const matchesComparison = (item, { field, operator, values }) => {
|
|
8982
|
+
const actual = readField(item, field) ?? null;
|
|
8983
|
+
const [value] = values;
|
|
8984
|
+
if (actual === null && MOCK_ASSET_FILTER_FIELDS[field].kind === "list") return operator === "=out=";
|
|
8985
|
+
if (actual === null) return operator === "==" && value === null;
|
|
8986
|
+
if (operator === "!=" && value === null) return true;
|
|
8987
|
+
const actuals = Array.isArray(actual) ? actual : [actual];
|
|
8988
|
+
const equals = (expected) => actuals.some((each) => each === expected);
|
|
8989
|
+
if ((operator === "==" || operator === "!=") && typeof value === "string" && value.includes("*")) {
|
|
8990
|
+
const pattern = wildcard(value);
|
|
8991
|
+
const matched = actuals.some((each) => typeof each === "string" && pattern.test(each));
|
|
8992
|
+
return operator === "==" ? matched : !matched;
|
|
8993
|
+
}
|
|
8464
8994
|
switch (operator) {
|
|
8465
|
-
case "==": return equals(
|
|
8466
|
-
case "!=": return !equals(
|
|
8995
|
+
case "==": return equals(value);
|
|
8996
|
+
case "!=": return !equals(value);
|
|
8467
8997
|
case "=in=": return values.some(equals);
|
|
8468
|
-
case "=out=": return !values.some(equals);
|
|
8469
|
-
|
|
8470
|
-
|
|
8471
|
-
|
|
8472
|
-
|
|
8473
|
-
case "
|
|
8474
|
-
case "
|
|
8475
|
-
|
|
8476
|
-
|
|
8477
|
-
}
|
|
8478
|
-
|
|
8479
|
-
const
|
|
8480
|
-
const parts = [];
|
|
8481
|
-
let depth = 0;
|
|
8482
|
-
let quote = null;
|
|
8483
|
-
let current = "";
|
|
8484
|
-
for (const char of text) {
|
|
8485
|
-
if (quote) {
|
|
8486
|
-
if (char === quote) quote = null;
|
|
8487
|
-
} else if (char === "\"" || char === "'") quote = char;
|
|
8488
|
-
else if (char === "(") depth++;
|
|
8489
|
-
else if (char === ")") depth--;
|
|
8490
|
-
else if (char === separator && depth === 0) {
|
|
8491
|
-
parts.push(current);
|
|
8492
|
-
current = "";
|
|
8493
|
-
continue;
|
|
8494
|
-
}
|
|
8495
|
-
current += char;
|
|
8496
|
-
}
|
|
8497
|
-
parts.push(current);
|
|
8498
|
-
return parts;
|
|
8499
|
-
};
|
|
8500
|
-
/** The expression inside `( … )` when the parentheses enclose all of `text`, else undefined. */
|
|
8501
|
-
const unwrapGroup = (text) => {
|
|
8502
|
-
if (!text.startsWith("(") || !text.endsWith(")")) return void 0;
|
|
8503
|
-
let depth = 0;
|
|
8504
|
-
let quote = null;
|
|
8505
|
-
for (const [index, char] of [...text].entries()) if (quote) {
|
|
8506
|
-
if (char === quote) quote = null;
|
|
8507
|
-
} else if (char === "\"" || char === "'") quote = char;
|
|
8508
|
-
else if (char === "(") depth++;
|
|
8509
|
-
else if (char === ")" && --depth === 0) return index === text.length - 1 ? text.slice(1, -1) : void 0;
|
|
8510
|
-
};
|
|
8511
|
-
/** Evaluates `,` (or) of `;` (and) terms, where a term is a comparison or a `( … )` group. */
|
|
8512
|
-
const matchesExpression = (item, expression) => splitTopLevel(expression, ",").some((alternative) => splitTopLevel(alternative, ";").every((term) => {
|
|
8513
|
-
const trimmed = term.trim();
|
|
8514
|
-
if (!trimmed) throw unsupportedFilter(expression);
|
|
8515
|
-
const group = unwrapGroup(trimmed);
|
|
8516
|
-
return group === void 0 ? matchesComparison(item, trimmed) : matchesExpression(item, group);
|
|
8517
|
-
}));
|
|
8998
|
+
case "=out=": return !values.includes(null) && !values.some(equals);
|
|
8999
|
+
}
|
|
9000
|
+
if (value === null) return false;
|
|
9001
|
+
const order = compareValues(actual, value);
|
|
9002
|
+
switch (operator) {
|
|
9003
|
+
case ">": return order > 0;
|
|
9004
|
+
case ">=": return order >= 0;
|
|
9005
|
+
case "<": return order < 0;
|
|
9006
|
+
default: return order <= 0;
|
|
9007
|
+
}
|
|
9008
|
+
};
|
|
9009
|
+
const matchesNode = (item, node) => node.type === "comparison" ? matchesComparison(item, node) : node.type === "and" ? node.nodes.every((child) => matchesNode(item, child)) : node.nodes.some((child) => matchesNode(item, child));
|
|
8518
9010
|
/**
|
|
8519
|
-
* Applies an RSQL `filter` like the API
|
|
8520
|
-
* than `,` (or), and
|
|
8521
|
-
*
|
|
9011
|
+
* Applies an RSQL `filter` like the API (zodbase/rsql with MOCK_ASSET_FILTER_FIELDS): only the
|
|
9012
|
+
* listed fields, each with its own operators, `;` (and) binding tighter than `,` (or), and parentheses. Throws
|
|
9013
|
+
* `MockInvalidFilter` for any filter the API refuses with `filter/invalid`.
|
|
8522
9014
|
*/
|
|
8523
9015
|
const filterAssets = (items, filter) => {
|
|
8524
9016
|
if (!filter) return items;
|
|
8525
|
-
|
|
8526
|
-
return items.filter((item) =>
|
|
9017
|
+
const node = parseFilter(filter);
|
|
9018
|
+
return items.filter((item) => matchesNode(item, node));
|
|
8527
9019
|
};
|
|
8528
9020
|
const MAX_SEARCH_TOKENS = 8;
|
|
8529
9021
|
/** The keyword fields the API searches, in the order it reports matches. */
|
|
@@ -8598,43 +9090,58 @@ const MOCK_OPERATIONS = [
|
|
|
8598
9090
|
"assets.update",
|
|
8599
9091
|
"assets.listChildren",
|
|
8600
9092
|
"assets.iterateChildren",
|
|
9093
|
+
"assets.listTimeline",
|
|
9094
|
+
"assets.iterateTimeline",
|
|
8601
9095
|
"assets.create",
|
|
8602
9096
|
"assets.classify",
|
|
8603
9097
|
"assets.uploadPart",
|
|
8604
|
-
"assets.createSignedUrl",
|
|
8605
9098
|
"assets.move",
|
|
8606
9099
|
"assets.retry",
|
|
8607
9100
|
"assets.explain",
|
|
8608
9101
|
"assets.decide",
|
|
8609
|
-
"assets.
|
|
9102
|
+
"assets.removeContent",
|
|
8610
9103
|
"assets.delete",
|
|
8611
9104
|
"assets.batch",
|
|
8612
9105
|
"text.classify",
|
|
8613
9106
|
"cdn.serve",
|
|
9107
|
+
"cdn.getAssetInfo",
|
|
9108
|
+
"cdn.signAssetUrl",
|
|
8614
9109
|
"events.list",
|
|
8615
9110
|
"events.pages",
|
|
8616
9111
|
"folders.list",
|
|
9112
|
+
"folders.iterate",
|
|
8617
9113
|
"folders.retrieve",
|
|
8618
9114
|
"folders.create",
|
|
8619
9115
|
"folders.update",
|
|
8620
9116
|
"folders.delete",
|
|
8621
9117
|
"folders.move",
|
|
8622
9118
|
"hashLists.list",
|
|
9119
|
+
"hashLists.iterate",
|
|
8623
9120
|
"hashLists.retrieve",
|
|
8624
9121
|
"hashLists.create",
|
|
8625
9122
|
"hashLists.update",
|
|
8626
9123
|
"hashLists.delete",
|
|
8627
9124
|
"hashLists.listEntries",
|
|
9125
|
+
"hashLists.iterateEntries",
|
|
8628
9126
|
"hashLists.addEntry",
|
|
8629
9127
|
"hashLists.removeEntry",
|
|
8630
9128
|
"policies.list",
|
|
9129
|
+
"policies.iterate",
|
|
8631
9130
|
"policies.retrieve",
|
|
8632
9131
|
"policies.create",
|
|
8633
9132
|
"policies.update",
|
|
8634
|
-
"policies.
|
|
9133
|
+
"policies.reset",
|
|
9134
|
+
"policies.upgrade",
|
|
8635
9135
|
"policies.setAutomaticUpdates",
|
|
8636
9136
|
"policies.delete",
|
|
9137
|
+
"policies.retrieveAssigned",
|
|
8637
9138
|
"policies.assign",
|
|
9139
|
+
"lenses.list",
|
|
9140
|
+
"lenses.iterate",
|
|
9141
|
+
"lenses.create",
|
|
9142
|
+
"lenses.retrieve",
|
|
9143
|
+
"lenses.update",
|
|
9144
|
+
"lenses.delete",
|
|
8638
9145
|
"ensureLens",
|
|
8639
9146
|
"ensureLenses"
|
|
8640
9147
|
];
|
|
@@ -8642,11 +9149,27 @@ const MAX_PAGE_SIZE = 100;
|
|
|
8642
9149
|
const MAX_DECISION_NOTE_LENGTH = 1499;
|
|
8643
9150
|
/** How the API names a reviewer acting through an unnamed API key. */
|
|
8644
9151
|
const API_KEY_REVIEWER_NAME = "An API key";
|
|
8645
|
-
|
|
9152
|
+
/** The actor of events the mock's API key causes: mock keys have no ID. */
|
|
9153
|
+
const MOCK_API_KEY_ACTOR = {
|
|
9154
|
+
type: "api-key",
|
|
9155
|
+
id: null
|
|
9156
|
+
};
|
|
9157
|
+
/** The actor of a reviewer's decision through the `decide` test hook. */
|
|
9158
|
+
const MOCK_REVIEWER_ACTOR = {
|
|
9159
|
+
type: "user",
|
|
9160
|
+
id: null
|
|
9161
|
+
};
|
|
9162
|
+
const AUTOMATED_ACTOR = {
|
|
9163
|
+
type: "automated",
|
|
9164
|
+
id: null
|
|
9165
|
+
};
|
|
9166
|
+
/** An event type's shape (`resource.action`), as the mock checks `types` without the catalog. */
|
|
9167
|
+
const EVENT_TYPE_PATTERN = /^[a-z][a-z-]*\.[a-z][a-z-]*$/;
|
|
9168
|
+
const MAX_SEARCH_PAGE_SIZE = 20;
|
|
8646
9169
|
const MAX_SEARCH_QUERY_LENGTH = 200;
|
|
8647
9170
|
/**
|
|
8648
|
-
* The ThumbHash the mock gives every
|
|
8649
|
-
* pixels. The `placeholder` resource serves it as a PNG, like the CDN.
|
|
9171
|
+
* The ThumbHash the mock gives every processed asset's preview, standing in for one computed from
|
|
9172
|
+
* its pixels. The `placeholder` resource serves it as a PNG, like the CDN.
|
|
8650
9173
|
*/
|
|
8651
9174
|
const MOCK_THUMBHASH = "1QcSHQRnh493V4dIh4eXh1h4kJUI";
|
|
8652
9175
|
/** Like the API: 1 to 255 printable ASCII characters, once HTTP has trimmed the header value. */
|
|
@@ -8670,10 +9193,19 @@ const toEventAsset = (asset) => ({
|
|
|
8670
9193
|
upload: { status: asset.upload.status },
|
|
8671
9194
|
classification: {
|
|
8672
9195
|
status: asset.classification.status,
|
|
9196
|
+
testInput: asset.classification.testInput,
|
|
8673
9197
|
moderation: { status: asset.classification.moderation.status }
|
|
8674
9198
|
},
|
|
8675
9199
|
content: { status: asset.content.status }
|
|
8676
9200
|
});
|
|
9201
|
+
/** The moderation status each `asset.<status>` event announces. */
|
|
9202
|
+
const ANNOUNCED_STATUS = {
|
|
9203
|
+
"asset.allowed": "allowed",
|
|
9204
|
+
"asset.limited": "limited",
|
|
9205
|
+
"asset.age-restricted": "age-restricted",
|
|
9206
|
+
"asset.blocked": "blocked",
|
|
9207
|
+
"asset.purged": "purged"
|
|
9208
|
+
};
|
|
8677
9209
|
/** A `moderate` answer or `completeProcessing` decision as a checked `MockDecision`. */
|
|
8678
9210
|
const toDecision = (value) => {
|
|
8679
9211
|
if (value instanceof MockDecision) return value;
|
|
@@ -8686,12 +9218,116 @@ const assetNameFact = (asset) => ({
|
|
|
8686
9218
|
value: asset.name,
|
|
8687
9219
|
url: `https://calmlens.com/go/asset/${asset.id}`
|
|
8688
9220
|
});
|
|
8689
|
-
/**
|
|
8690
|
-
const
|
|
8691
|
-
|
|
8692
|
-
|
|
8693
|
-
|
|
8694
|
-
|
|
9221
|
+
/** The page the mock serves at `/embed`, standing in for the CDN's embed page. */
|
|
9222
|
+
const MOCK_EMBED_HTML = "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>Embed</title></head><body></body></html>";
|
|
9223
|
+
/** A ThumbHash as the PNG the placeholder resource serves. */
|
|
9224
|
+
const mockPlaceholderPng = (thumbhash) => Uint8Array.from(atob(placeholderHashToDataURL(thumbhash).split(",", 2)[1]), (char) => char.charCodeAt(0));
|
|
9225
|
+
/** Removing or withholding content removes its rendered file and preview, as the API does. */
|
|
9226
|
+
const NO_RENDERING = {
|
|
9227
|
+
rendered: null,
|
|
9228
|
+
preview: null,
|
|
9229
|
+
hls: null
|
|
9230
|
+
};
|
|
9231
|
+
/** The size of the waveform the API makes as an audio asset's preview. */
|
|
9232
|
+
const WAVEFORM_SIZE = {
|
|
9233
|
+
width: 1200,
|
|
9234
|
+
height: 300
|
|
9235
|
+
};
|
|
9236
|
+
/** Audio and video formats browsers play as uploaded, so the API serves them as stored (Asset.ts). */
|
|
9237
|
+
const BROWSER_SAFE_MEDIA_FORMATS = [
|
|
9238
|
+
"video/mp4",
|
|
9239
|
+
"audio/mpeg",
|
|
9240
|
+
"audio/aac",
|
|
9241
|
+
"audio/mp4",
|
|
9242
|
+
"audio/x-m4a",
|
|
9243
|
+
"audio/ogg"
|
|
9244
|
+
];
|
|
9245
|
+
/** Spreadsheets, which have no rendered file or preview (Asset.ts). */
|
|
9246
|
+
const SPREADSHEET_MIME_TYPES = [
|
|
9247
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
9248
|
+
"application/vnd.ms-excel.sheet.macroenabled.12",
|
|
9249
|
+
"application/vnd.ms-excel",
|
|
9250
|
+
"application/vnd.oasis.opendocument.spreadsheet"
|
|
9251
|
+
];
|
|
9252
|
+
/** Whether browsers play an audio or video upload's format as it is, so it is served as stored. */
|
|
9253
|
+
const isBrowserSafeMediaFormat = (format) => BROWSER_SAFE_MEDIA_FORMATS.includes(normalizeUploadMimeType(format ?? ""));
|
|
9254
|
+
/**
|
|
9255
|
+
* The rendered file and preview the API reports for an asset with its content held (Asset.ts,
|
|
9256
|
+
* getAssetRendered and getAssetPreview). Once uploaded, an image renders as itself with a preview
|
|
9257
|
+
* that has no ThumbHash yet, and a PDF and browser-safe audio and video render as uploaded. Once
|
|
9258
|
+
* processed, a website renders as its screenshot; a document or presentation as a PDF, with its
|
|
9259
|
+
* first page as the preview; video and audio as a browser-safe file (MP4 or M4A unless browsers
|
|
9260
|
+
* play the upload), audio with a waveform preview and video with no poster; and every preview has
|
|
9261
|
+
* its ThumbHash. Text, spreadsheets and other files have neither. The mock decodes nothing, so
|
|
9262
|
+
* sizes are unknown, audio has no peaks, and audio and video never get an HLS stream.
|
|
9263
|
+
*/
|
|
9264
|
+
const mockRendering = (asset, processed) => {
|
|
9265
|
+
const format = asset.content.format;
|
|
9266
|
+
const mimeType = normalizeUploadMimeType(format ?? "");
|
|
9267
|
+
const browserSafe = isBrowserSafeMediaFormat(format);
|
|
9268
|
+
const preview = (size) => ({
|
|
9269
|
+
...size,
|
|
9270
|
+
thumbhash: processed ? MOCK_THUMBHASH : null,
|
|
9271
|
+
averageColor: processed ? "#7f7f7f" : null
|
|
9272
|
+
});
|
|
9273
|
+
const unsized = {
|
|
9274
|
+
width: null,
|
|
9275
|
+
height: null
|
|
9276
|
+
};
|
|
9277
|
+
const rendersOnUpload = asset.type === "image" || mimeType === "application/pdf" || (asset.type === "video" || asset.type === "audio") && browserSafe;
|
|
9278
|
+
if (!processed && !rendersOnUpload) return NO_RENDERING;
|
|
9279
|
+
switch (asset.type) {
|
|
9280
|
+
case "image": return {
|
|
9281
|
+
rendered: {
|
|
9282
|
+
kind: "image",
|
|
9283
|
+
format: format ?? "image/png",
|
|
9284
|
+
...unsized
|
|
9285
|
+
},
|
|
9286
|
+
preview: preview(unsized),
|
|
9287
|
+
hls: null
|
|
9288
|
+
};
|
|
9289
|
+
case "website": return {
|
|
9290
|
+
rendered: {
|
|
9291
|
+
kind: "image",
|
|
9292
|
+
format: "image/png",
|
|
9293
|
+
...unsized
|
|
9294
|
+
},
|
|
9295
|
+
preview: preview(unsized),
|
|
9296
|
+
hls: null
|
|
9297
|
+
};
|
|
9298
|
+
case "multimedia-document":
|
|
9299
|
+
if (SPREADSHEET_MIME_TYPES.includes(mimeType)) return NO_RENDERING;
|
|
9300
|
+
return {
|
|
9301
|
+
rendered: {
|
|
9302
|
+
kind: "pdf",
|
|
9303
|
+
format: "application/pdf",
|
|
9304
|
+
...unsized
|
|
9305
|
+
},
|
|
9306
|
+
preview: processed ? preview(unsized) : null,
|
|
9307
|
+
hls: null
|
|
9308
|
+
};
|
|
9309
|
+
case "video": return {
|
|
9310
|
+
rendered: {
|
|
9311
|
+
kind: "video",
|
|
9312
|
+
format: browserSafe ? format : "video/mp4",
|
|
9313
|
+
...unsized
|
|
9314
|
+
},
|
|
9315
|
+
preview: null,
|
|
9316
|
+
hls: null
|
|
9317
|
+
};
|
|
9318
|
+
case "audio": return {
|
|
9319
|
+
rendered: {
|
|
9320
|
+
kind: "audio",
|
|
9321
|
+
format: browserSafe ? format : "audio/mp4",
|
|
9322
|
+
...unsized,
|
|
9323
|
+
...processed && { peaks: [] }
|
|
9324
|
+
},
|
|
9325
|
+
preview: processed ? preview(WAVEFORM_SIZE) : null,
|
|
9326
|
+
hls: null
|
|
9327
|
+
};
|
|
9328
|
+
default: return NO_RENDERING;
|
|
9329
|
+
}
|
|
9330
|
+
};
|
|
8695
9331
|
/** An asset's type from its content type, or `website` for a URL. */
|
|
8696
9332
|
const inferAssetType = (contentType) => {
|
|
8697
9333
|
if (contentType === void 0) return "website";
|
|
@@ -8713,8 +9349,39 @@ var ApiFailure = class {
|
|
|
8713
9349
|
}
|
|
8714
9350
|
};
|
|
8715
9351
|
const fail = (code, { message, details } = {}) => new ApiFailure(API_ERROR_DEFINITIONS[code].status, code, message ?? API_ERROR_DEFINITIONS[code].message, details);
|
|
8716
|
-
/**
|
|
8717
|
-
|
|
9352
|
+
/**
|
|
9353
|
+
* A request the API's schema refuses: `validation/invalid-request` with HTTP 400, listing the field
|
|
9354
|
+
* and its issue code (such as `validation/too-big`) in `details.issues`.
|
|
9355
|
+
*/
|
|
9356
|
+
/**
|
|
9357
|
+
* The mock's grant, the asset segment of a URL `signAssetUrl` signs: `sig_` and the hex of
|
|
9358
|
+
* `{assetId}.{1|0}.{expiresAt}`, its asset, original access and expiry. Every SDK's mock writes it
|
|
9359
|
+
* the same way. It is readable, not signed, as the mock has no secret to keep.
|
|
9360
|
+
*/
|
|
9361
|
+
const encodeMockGrant = (grant) => `sig_${[...new TextEncoder().encode(`${grant.assetId}.${grant.original ? 1 : 0}.${grant.expiresAt}`)].map((byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
9362
|
+
/** Reads a mock grant, or `undefined` for a segment the mock didn't sign. */
|
|
9363
|
+
const decodeMockGrant = (segment) => {
|
|
9364
|
+
const hex = segment.slice(4);
|
|
9365
|
+
if (!/^(?:[0-9a-f]{2})+$/.test(hex)) return void 0;
|
|
9366
|
+
let text;
|
|
9367
|
+
try {
|
|
9368
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(Uint8Array.from(hex.match(/../g), (pair) => Number.parseInt(pair, 16)));
|
|
9369
|
+
} catch {
|
|
9370
|
+
return;
|
|
9371
|
+
}
|
|
9372
|
+
const match = /^(.+)\.([01])\.(\d+)$/s.exec(text);
|
|
9373
|
+
if (!match) return void 0;
|
|
9374
|
+
return {
|
|
9375
|
+
assetId: match[1],
|
|
9376
|
+
original: match[2] === "1",
|
|
9377
|
+
expiresAt: Number(match[3])
|
|
9378
|
+
};
|
|
9379
|
+
};
|
|
9380
|
+
const invalidField = (code, path) => new ApiFailure(400, API_ERROR_CODES.INVALID_REQUEST, `Invalid ${path}`, { issues: [{
|
|
9381
|
+
path,
|
|
9382
|
+
message: `Invalid ${path}`,
|
|
9383
|
+
code
|
|
9384
|
+
}] });
|
|
8718
9385
|
/**
|
|
8719
9386
|
* Refuses `metadata` the API's schema refuses (METADATA_ITEM_SCHEMA): it is `null` or a list of
|
|
8720
9387
|
* `{ id, value }` objects with a string `id` and any JSON `value`.
|
|
@@ -8735,35 +9402,65 @@ const MOCK_PRESET_NAMES = {
|
|
|
8735
9402
|
"education-support": "Learning and support",
|
|
8736
9403
|
"wholesome-community": "Teen-safe"
|
|
8737
9404
|
};
|
|
9405
|
+
/**
|
|
9406
|
+
* The settings of the mock's presets: every category with its default action (`allow` for
|
|
9407
|
+
* personal information and profanity, `block` otherwise) and no rules. The mock holds no built-in
|
|
9408
|
+
* preset settings, and uploads are never decided by a policy.
|
|
9409
|
+
*/
|
|
9410
|
+
const mockPresetSettings = () => ({ categorySettings: Object.fromEntries(CLASSIFICATION_CATEGORIES.map((category) => [category, {
|
|
9411
|
+
action: category === "personal-information" || category === "profanity" ? "allow" : "block",
|
|
9412
|
+
allowances: [],
|
|
9413
|
+
restrictions: []
|
|
9414
|
+
}])) });
|
|
9415
|
+
/**
|
|
9416
|
+
* Refuses settings the API refuses (MODERATION_POLICY_SETTINGS_SCHEMA): every category, each with
|
|
9417
|
+
* an `action` and its `allowances` and `restrictions` lists.
|
|
9418
|
+
*/
|
|
9419
|
+
const checkPolicySettings = (settings) => {
|
|
9420
|
+
const categories = settings?.categorySettings;
|
|
9421
|
+
if (!categories || typeof categories !== "object") throw invalidField("validation/invalid-type", "settings.categorySettings");
|
|
9422
|
+
for (const category of CLASSIFICATION_CATEGORIES) {
|
|
9423
|
+
const value = categories[category];
|
|
9424
|
+
if (!value || typeof value !== "object") throw invalidField("validation/invalid-type", `settings.categorySettings.${category}`);
|
|
9425
|
+
for (const key of ["allowances", "restrictions"]) if (!Array.isArray(value[key])) throw invalidField("validation/invalid-type", `settings.categorySettings.${category}.${key}`);
|
|
9426
|
+
if (typeof value.action !== "string") throw invalidField("validation/invalid-value", `settings.categorySettings.${category}.action`);
|
|
9427
|
+
}
|
|
9428
|
+
for (const key of Object.keys(categories)) if (!CLASSIFICATION_CATEGORIES.includes(key)) throw invalidField("validation/unknown-field", `settings.categorySettings.${key}`);
|
|
9429
|
+
};
|
|
8738
9430
|
/** The client methods that send a request, the one it is recorded under first. */
|
|
8739
9431
|
const operationsOf = (request) => {
|
|
8740
9432
|
const { method, segments, query, cdn } = request;
|
|
8741
|
-
if (cdn) return ["cdn.serve"];
|
|
9433
|
+
if (cdn) return segments[3] === "info" ? ["cdn.getAssetInfo"] : ["cdn.serve"];
|
|
8742
9434
|
const [root] = segments;
|
|
8743
9435
|
if (root === "projects") {
|
|
8744
9436
|
switch (segments.slice(2).join("/")) {
|
|
8745
9437
|
case "events": return ["events.list", "events.pages"];
|
|
8746
|
-
case "lenses": return ["ensureLenses", "ensureLens"];
|
|
8747
|
-
case "folders": return method === "GET" ? ["folders.list"] : ["folders.create"];
|
|
8748
|
-
case "hash-lists": return method === "GET" ? ["hashLists.list"] : ["hashLists.create"];
|
|
8749
|
-
case "
|
|
8750
|
-
case "policy": return ["policies.assign"];
|
|
9438
|
+
case "lenses": return method === "GET" ? ["lenses.list", "lenses.iterate"] : method === "POST" ? ["lenses.create"] : ["ensureLenses", "ensureLens"];
|
|
9439
|
+
case "folders": return method === "GET" ? ["folders.list", "folders.iterate"] : ["folders.create"];
|
|
9440
|
+
case "hash-lists": return method === "GET" ? ["hashLists.list", "hashLists.iterate"] : ["hashLists.create"];
|
|
9441
|
+
case "policy": return method === "GET" ? ["policies.retrieveAssigned"] : ["policies.assign"];
|
|
8751
9442
|
case "assets/search": return ["assets.search"];
|
|
8752
9443
|
case "assets/batch": return ["assets.batch"];
|
|
9444
|
+
case "signed-asset-urls": return ["cdn.signAssetUrl"];
|
|
8753
9445
|
case "assets":
|
|
8754
9446
|
if (method === "GET") return ["assets.list", "assets.iterate"];
|
|
8755
|
-
if (query.get("
|
|
9447
|
+
if (query.get("wait") === "false") return ["assets.create"];
|
|
8756
9448
|
return isTextUpload(request) ? ["assets.classify", "text.classify"] : ["assets.classify"];
|
|
8757
9449
|
}
|
|
8758
|
-
if (segments[2] === "
|
|
8759
|
-
if (segments[4] === "preset") return ["policies.applyPreset"];
|
|
8760
|
-
if (segments[4] === "automatic-updates") return ["policies.setAutomaticUpdates"];
|
|
8761
|
-
return method === "GET" ? ["policies.retrieve"] : method === "PATCH" ? ["policies.update"] : ["policies.delete"];
|
|
8762
|
-
}
|
|
9450
|
+
if (segments[2] === "assets" && segments[4] === "timeline" && segments.length === 5) return ["assets.listTimeline", "assets.iterateTimeline"];
|
|
8763
9451
|
return [];
|
|
8764
9452
|
}
|
|
9453
|
+
if (root === "policies") {
|
|
9454
|
+
const [, policyId, policyAction] = segments;
|
|
9455
|
+
if (policyId === void 0) return method === "GET" ? ["policies.list", "policies.iterate"] : ["policies.create"];
|
|
9456
|
+
if (policyAction === "reset") return ["policies.reset"];
|
|
9457
|
+
if (policyAction === "upgrade") return ["policies.upgrade"];
|
|
9458
|
+
if (policyAction === "automatic-updates") return ["policies.setAutomaticUpdates"];
|
|
9459
|
+
return method === "GET" ? ["policies.retrieve"] : method === "PATCH" ? ["policies.update"] : ["policies.delete"];
|
|
9460
|
+
}
|
|
9461
|
+
if (root === "lenses" && segments[1] !== void 0) return method === "GET" ? ["lenses.retrieve"] : method === "PATCH" ? ["lenses.update"] : ["lenses.delete"];
|
|
8765
9462
|
if (root === "upload-presets") {
|
|
8766
|
-
if (segments[3] === void 0) return [query.get("
|
|
9463
|
+
if (segments[3] === void 0) return [query.get("wait") === "false" ? "assets.create" : "assets.classify"];
|
|
8767
9464
|
return segments[4] === "part" ? ["assets.uploadPart"] : ["assets.retrieve"];
|
|
8768
9465
|
}
|
|
8769
9466
|
const id = segments[1];
|
|
@@ -8773,16 +9470,15 @@ const operationsOf = (request) => {
|
|
|
8773
9470
|
return method === "GET" ? ["folders.retrieve"] : method === "PATCH" ? ["folders.update"] : ["folders.delete"];
|
|
8774
9471
|
}
|
|
8775
9472
|
if (root === "hash-lists" && id !== void 0) {
|
|
8776
|
-
if (action === "entries") return method === "GET" ? ["hashLists.listEntries"] : ["hashLists.addEntry"];
|
|
9473
|
+
if (action === "entries") return method === "GET" ? ["hashLists.listEntries", "hashLists.iterateEntries"] : ["hashLists.addEntry"];
|
|
8777
9474
|
return method === "GET" ? ["hashLists.retrieve"] : method === "PATCH" ? ["hashLists.update"] : ["hashLists.delete"];
|
|
8778
9475
|
}
|
|
8779
9476
|
if (root === "hash-list-entries" && id !== void 0) return ["hashLists.removeEntry"];
|
|
8780
9477
|
if (root !== "assets" || id === void 0) return [];
|
|
8781
9478
|
switch (action) {
|
|
8782
9479
|
case void 0: return method === "GET" ? ["assets.retrieve", "assets.waitForDecision"] : method === "PATCH" ? ["assets.update"] : ["assets.delete"];
|
|
8783
|
-
case "content": return ["assets.
|
|
9480
|
+
case "content": return ["assets.removeContent"];
|
|
8784
9481
|
case "children": return ["assets.listChildren", "assets.iterateChildren"];
|
|
8785
|
-
case "signed-url": return ["assets.createSignedUrl"];
|
|
8786
9482
|
case "part": return ["assets.uploadPart"];
|
|
8787
9483
|
case "move": return ["assets.move"];
|
|
8788
9484
|
case "retry": return ["assets.retry"];
|
|
@@ -8820,8 +9516,12 @@ const createMonotonicClock = () => {
|
|
|
8820
9516
|
var MockCalmLensApi = class {
|
|
8821
9517
|
constructor(options) {
|
|
8822
9518
|
this.calls = [];
|
|
9519
|
+
this.cdnBaseUrl = "";
|
|
8823
9520
|
this.lenses = /* @__PURE__ */ new Map();
|
|
8824
9521
|
this.eventFeed = [];
|
|
9522
|
+
this.assetEventSequences = /* @__PURE__ */ new Map();
|
|
9523
|
+
this.processingOptions = /* @__PURE__ */ new Map();
|
|
9524
|
+
this.pendingReruns = /* @__PURE__ */ new Set();
|
|
8825
9525
|
this.openUploads = /* @__PURE__ */ new Map();
|
|
8826
9526
|
this.idempotentResults = /* @__PURE__ */ new Map();
|
|
8827
9527
|
this.pendingFailures = [];
|
|
@@ -8830,7 +9530,7 @@ var MockCalmLensApi = class {
|
|
|
8830
9530
|
this.policies = /* @__PURE__ */ new Map();
|
|
8831
9531
|
this.hashLists = /* @__PURE__ */ new Map();
|
|
8832
9532
|
this.hashListEntries = /* @__PURE__ */ new Map();
|
|
8833
|
-
this.pageFailure = (code, path) => code === "
|
|
9533
|
+
this.pageFailure = (code, path) => code === "cursor/invalid" ? fail(API_ERROR_CODES.INVALID_CURSOR) : invalidField(code, path ?? "pageSize");
|
|
8834
9534
|
this.options = options;
|
|
8835
9535
|
this.projectId = options.projectId;
|
|
8836
9536
|
this.apiKey = options.apiKey;
|
|
@@ -8860,10 +9560,14 @@ var MockCalmLensApi = class {
|
|
|
8860
9560
|
*/
|
|
8861
9561
|
async enqueueEvent(input) {
|
|
8862
9562
|
const asset = input.asset === void 0 && input.assetId ? await this.databaseStorage.getAsset(input.assetId) : void 0;
|
|
8863
|
-
return this.pushEvent(
|
|
9563
|
+
if (!asset) return this.pushEvent(input);
|
|
9564
|
+
const snapshot = toEventAsset(asset);
|
|
9565
|
+
const announced = ANNOUNCED_STATUS[input.type];
|
|
9566
|
+
if (announced) snapshot.classification.moderation.status = announced;
|
|
9567
|
+
return this.pushEvent({
|
|
8864
9568
|
...input,
|
|
8865
|
-
asset:
|
|
8866
|
-
}
|
|
9569
|
+
asset: snapshot
|
|
9570
|
+
});
|
|
8867
9571
|
}
|
|
8868
9572
|
/** Adds an event to the feed, filling the fields the API always sets. */
|
|
8869
9573
|
pushEvent(input) {
|
|
@@ -8876,10 +9580,14 @@ var MockCalmLensApi = class {
|
|
|
8876
9580
|
description: null,
|
|
8877
9581
|
assetId: null,
|
|
8878
9582
|
asset: null,
|
|
9583
|
+
actor: MOCK_API_KEY_ACTOR,
|
|
8879
9584
|
facts: [],
|
|
8880
9585
|
createdAt: now,
|
|
8881
9586
|
...input
|
|
8882
9587
|
};
|
|
9588
|
+
const sequence = event.assetId ? (this.assetEventSequences.get(event.assetId) ?? 0) + 1 : null;
|
|
9589
|
+
if (event.assetId && sequence !== null) this.assetEventSequences.set(event.assetId, sequence);
|
|
9590
|
+
event.assetSequence = sequence;
|
|
8883
9591
|
this.eventFeed.push(event);
|
|
8884
9592
|
return event;
|
|
8885
9593
|
}
|
|
@@ -8906,15 +9614,17 @@ var MockCalmLensApi = class {
|
|
|
8906
9614
|
} catch (failure) {
|
|
8907
9615
|
throw this.toError(failure);
|
|
8908
9616
|
}
|
|
8909
|
-
|
|
9617
|
+
const rerun = this.pendingReruns.has(asset.id);
|
|
9618
|
+
if (!rerun && asset.classification.status !== "processing") throw invalidArgument(`asset ${assetId} isn't processing`);
|
|
8910
9619
|
const settled = decision === void 0 ? void 0 : toDecision(decision);
|
|
8911
|
-
return structuredClone(await this.moderateStored(asset, "complete", settled));
|
|
9620
|
+
if (!rerun) return structuredClone(await this.moderateStored(asset, "complete", settled));
|
|
9621
|
+
return structuredClone(await this.settleRerun(asset, settled ?? await this.decisionFor(asset, "complete", true)));
|
|
8912
9622
|
}
|
|
8913
9623
|
/**
|
|
8914
9624
|
* `POST /assets/:assetId/decision`, like the API: an asset still uploading or processing can't
|
|
8915
9625
|
* be decided, one pending moderation can only be blocked or purged, a decision must change the
|
|
8916
|
-
* status and purged content can only be blocked.
|
|
8917
|
-
* "An API key".
|
|
9626
|
+
* status and purged content can only be blocked. The review is the API key's, and mock API keys
|
|
9627
|
+
* have no ID or name, so its ID is `null` and the event names the reviewer "An API key".
|
|
8918
9628
|
*/
|
|
8919
9629
|
async decideAsset(assetId, body) {
|
|
8920
9630
|
const { decision, note: rawNote } = body;
|
|
@@ -8931,43 +9641,38 @@ var MockCalmLensApi = class {
|
|
|
8931
9641
|
if (decision !== "purged" && decision !== "blocked" && status === "pending") throw fail(API_ERROR_CODES.ASSET_DECISION_NOT_ALLOWED);
|
|
8932
9642
|
if (status === decision) throw fail(API_ERROR_CODES.ASSET_DECISION_UNCHANGED);
|
|
8933
9643
|
if (decision !== "purged" && decision !== "blocked" && status === "purged") throw fail(API_ERROR_CODES.ASSET_CONTENT_PURGED);
|
|
8934
|
-
return this.recordDecision(asset, decision, {
|
|
8935
|
-
note,
|
|
8936
|
-
reviewerName: API_KEY_REVIEWER_NAME
|
|
8937
|
-
});
|
|
9644
|
+
return this.recordDecision(asset, decision, { note });
|
|
8938
9645
|
}
|
|
8939
9646
|
/**
|
|
8940
9647
|
* Sets the reviewer's decision: the moderation status, a manual review and the
|
|
8941
|
-
* `asset.<status>` event (after `asset.content-removed` when purging).
|
|
8942
|
-
*
|
|
9648
|
+
* `asset.<status>` event (after `asset.content-removed` when purging). Either way the review is
|
|
9649
|
+
* the mock API key's; the test hook records no note, the API's handler records it.
|
|
8943
9650
|
*/
|
|
8944
9651
|
async recordDecision(asset, decision, review) {
|
|
9652
|
+
this.pendingReruns.delete(asset.id);
|
|
8945
9653
|
const decided = await this.applyOutcome(asset, decision, "requested");
|
|
8946
9654
|
const now = this.now();
|
|
8947
9655
|
decided.classification.moderation.manualReviews = [...decided.classification.moderation.manualReviews ?? [], {
|
|
8948
9656
|
id: this.createId("rev"),
|
|
8949
9657
|
decision,
|
|
8950
9658
|
note: review?.note ?? null,
|
|
8951
|
-
|
|
8952
|
-
|
|
8953
|
-
|
|
9659
|
+
decidedBy: {
|
|
9660
|
+
type: "api-key",
|
|
9661
|
+
id: null
|
|
8954
9662
|
},
|
|
8955
9663
|
reviewedAt: now,
|
|
8956
|
-
|
|
9664
|
+
applied: true
|
|
8957
9665
|
}];
|
|
8958
9666
|
const stored = await this.databaseStorage.updateAsset(decided);
|
|
8959
|
-
if (decision === "purged") this.recordLifecycleEvent("asset.content-removed", stored);
|
|
9667
|
+
if (decision === "purged") this.recordLifecycleEvent("asset.content-removed", stored, { actor: review ? MOCK_API_KEY_ACTOR : MOCK_REVIEWER_ACTOR });
|
|
8960
9668
|
this.pushEvent({
|
|
8961
9669
|
type: `asset.${decision}`,
|
|
8962
9670
|
severity: decision === "allowed" || decision === "limited" ? "low" : "high",
|
|
8963
9671
|
title: `asset.${decision}`,
|
|
8964
9672
|
assetId: stored.id,
|
|
8965
9673
|
asset: toEventAsset(stored),
|
|
9674
|
+
actor: review ? MOCK_API_KEY_ACTOR : MOCK_REVIEWER_ACTOR,
|
|
8966
9675
|
facts: [
|
|
8967
|
-
{
|
|
8968
|
-
key: "decided-by",
|
|
8969
|
-
value: "reviewer"
|
|
8970
|
-
},
|
|
8971
9676
|
{
|
|
8972
9677
|
key: "asset-type",
|
|
8973
9678
|
value: stored.type
|
|
@@ -8978,11 +9683,8 @@ var MockCalmLensApi = class {
|
|
|
8978
9683
|
url: `https://calmlens.com/go/asset/${stored.id}`
|
|
8979
9684
|
},
|
|
8980
9685
|
...review ? [{
|
|
8981
|
-
key: "reviewer-user-id",
|
|
8982
|
-
value: null
|
|
8983
|
-
}, {
|
|
8984
9686
|
key: "reviewer-name",
|
|
8985
|
-
value:
|
|
9687
|
+
value: API_KEY_REVIEWER_NAME
|
|
8986
9688
|
}] : [],
|
|
8987
9689
|
{
|
|
8988
9690
|
key: "note",
|
|
@@ -9010,6 +9712,7 @@ var MockCalmLensApi = class {
|
|
|
9010
9712
|
async handle(input, init, endpoints, session) {
|
|
9011
9713
|
if (init?.signal?.aborted) throw init.signal.reason;
|
|
9012
9714
|
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
9715
|
+
this.cdnBaseUrl = endpoints.cdnBaseUrl;
|
|
9013
9716
|
const cdn = url.startsWith(`${endpoints.cdnBaseUrl}/`);
|
|
9014
9717
|
const base = cdn ? endpoints.cdnBaseUrl : endpoints.baseUrl;
|
|
9015
9718
|
if (!url.startsWith(`${base}/`)) throw new TypeError(`MockCalmLensServerClient does not serve ${url}`);
|
|
@@ -9159,25 +9862,39 @@ var MockCalmLensApi = class {
|
|
|
9159
9862
|
case "GET assets": return [200, await this.listAssets(request.query)];
|
|
9160
9863
|
case "GET assets/search": return [200, await this.searchAssets(request.query)];
|
|
9161
9864
|
case "POST assets/batch": return [202, await this.batch(this.json(request))];
|
|
9865
|
+
case "POST signed-asset-urls": return [200, await this.signAssetUrl(this.json(request))];
|
|
9162
9866
|
case "GET events": return [200, this.listEvents(request.query)];
|
|
9163
9867
|
case "GET folders": return [200, await this.listFolders(request.query)];
|
|
9164
9868
|
case "POST folders": return [201, await this.createFolder(this.json(request))];
|
|
9165
9869
|
case "GET hash-lists": return [200, this.listHashLists(request.query)];
|
|
9166
9870
|
case "POST hash-lists": return [201, this.createHashList(this.json(request))];
|
|
9167
9871
|
case "PUT lenses": return [200, this.ensureLenses(this.json(request))];
|
|
9168
|
-
case "GET
|
|
9169
|
-
case "POST
|
|
9872
|
+
case "GET lenses": return [200, this.listLenses(request.query)];
|
|
9873
|
+
case "POST lenses": return [201, this.createLens(this.json(request))];
|
|
9874
|
+
case "GET policy": return [200, this.toPolicy(this.findPolicy(this.ensurePolicies()))];
|
|
9170
9875
|
case "PUT policy": return [200, this.assignPolicy(this.json(request))];
|
|
9171
9876
|
}
|
|
9172
|
-
const [, , collection,
|
|
9173
|
-
if (collection === "
|
|
9174
|
-
|
|
9175
|
-
|
|
9176
|
-
|
|
9177
|
-
|
|
9178
|
-
|
|
9877
|
+
const [, , collection, itemId, itemAction, ...more] = segments;
|
|
9878
|
+
if (collection === "assets" && itemId !== void 0 && itemAction === "timeline" && more.length === 0 && method === "GET") return [200, await this.listTimeline(itemId, request.query)];
|
|
9879
|
+
}
|
|
9880
|
+
if (root === "policies" && rest.length === 0) {
|
|
9881
|
+
if (id === void 0) {
|
|
9882
|
+
if (method === "GET") return [200, this.listPolicies(request.query)];
|
|
9883
|
+
if (method === "POST") return [201, this.createPolicy(this.json(request))];
|
|
9884
|
+
} else switch (`${method} ${action ?? ""}`) {
|
|
9885
|
+
case "GET ": return [200, this.toPolicy(this.findPolicy(id))];
|
|
9886
|
+
case "PATCH ": return [200, this.updatePolicy(id, this.json(request))];
|
|
9887
|
+
case "DELETE ": return [200, this.deletePolicy(id)];
|
|
9888
|
+
case "POST reset": return [200, this.restorePolicy(id, "reset", this.json(request))];
|
|
9889
|
+
case "POST upgrade": return [200, this.restorePolicy(id, "upgrade", this.json(request))];
|
|
9890
|
+
case "PUT automatic-updates": return [200, this.setPolicyAutomaticUpdates(id, this.json(request))];
|
|
9179
9891
|
}
|
|
9180
9892
|
}
|
|
9893
|
+
if (root === "lenses" && id !== void 0 && action === void 0) switch (method) {
|
|
9894
|
+
case "GET": return [200, this.findLens(id)];
|
|
9895
|
+
case "PATCH": return [200, this.updateLens(id, this.json(request))];
|
|
9896
|
+
case "DELETE": return [200, this.deleteLens(id)];
|
|
9897
|
+
}
|
|
9181
9898
|
if (root === "upload-presets" && id !== void 0 && action === "assets") return [200, await this.presetRoute(request, id, rest, session)];
|
|
9182
9899
|
if (root === "assets" && id !== void 0 && rest.length === 0) switch (`${method} ${action ?? ""}`) {
|
|
9183
9900
|
case "GET ": return [200, await this.findOwnAsset(id)];
|
|
@@ -9185,7 +9902,6 @@ var MockCalmLensApi = class {
|
|
|
9185
9902
|
case "DELETE ": return [200, await this.deleteAsset(id)];
|
|
9186
9903
|
case "GET children": return [200, await this.listChildren(id, request.query)];
|
|
9187
9904
|
case "POST part": return [200, await this.uploadPart(id, request)];
|
|
9188
|
-
case "POST signed-url": return [200, await this.createSignedUrl(id, this.json(request))];
|
|
9189
9905
|
case "POST move": return [200, await this.moveAsset(id, this.json(request))];
|
|
9190
9906
|
case "POST retry": return [200, await this.retryAsset(id)];
|
|
9191
9907
|
case "POST explain": return [200, await this.explainAsset(id, this.json(request))];
|
|
@@ -9211,13 +9927,14 @@ var MockCalmLensApi = class {
|
|
|
9211
9927
|
}
|
|
9212
9928
|
throw fail(API_ERROR_CODES.ROUTE_NOT_FOUND, { message: `No route for ${method} /${segments.join("/")}` });
|
|
9213
9929
|
}
|
|
9930
|
+
/** The project's policy, after creating the organisation's own General policy on first use. */
|
|
9214
9931
|
ensurePolicies() {
|
|
9215
|
-
if (this.projectPolicyId) return;
|
|
9932
|
+
if (this.projectPolicyId) return this.projectPolicyId;
|
|
9216
9933
|
const now = this.now();
|
|
9217
9934
|
const general = this.newPolicy({
|
|
9218
9935
|
name: MOCK_PRESET_NAMES["general-community"],
|
|
9219
|
-
settings:
|
|
9220
|
-
|
|
9936
|
+
settings: mockPresetSettings(),
|
|
9937
|
+
followsPreset: true,
|
|
9221
9938
|
preset: {
|
|
9222
9939
|
id: "general-community",
|
|
9223
9940
|
version: 1,
|
|
@@ -9226,6 +9943,7 @@ var MockCalmLensApi = class {
|
|
|
9226
9943
|
createdAt: now
|
|
9227
9944
|
});
|
|
9228
9945
|
this.projectPolicyId = general.id;
|
|
9946
|
+
return general.id;
|
|
9229
9947
|
}
|
|
9230
9948
|
newPolicy(values) {
|
|
9231
9949
|
const policy = {
|
|
@@ -9234,7 +9952,7 @@ var MockCalmLensApi = class {
|
|
|
9234
9952
|
description: values.description ?? "",
|
|
9235
9953
|
settings: values.settings,
|
|
9236
9954
|
revision: 1,
|
|
9237
|
-
|
|
9955
|
+
followsPreset: values.followsPreset ?? false,
|
|
9238
9956
|
preset: values.preset ?? null,
|
|
9239
9957
|
baseline: structuredClone(values.settings),
|
|
9240
9958
|
createdAt: values.createdAt,
|
|
@@ -9244,12 +9962,14 @@ var MockCalmLensApi = class {
|
|
|
9244
9962
|
return policy;
|
|
9245
9963
|
}
|
|
9246
9964
|
toPolicy(policy) {
|
|
9247
|
-
const { baseline: _baseline, preset, ...rest } = policy;
|
|
9965
|
+
const { baseline: _baseline, preset, followsPreset, ...rest } = policy;
|
|
9248
9966
|
return {
|
|
9249
9967
|
...structuredClone(rest),
|
|
9250
9968
|
preset: preset && {
|
|
9251
|
-
|
|
9252
|
-
|
|
9969
|
+
id: preset.id,
|
|
9970
|
+
version: preset.version,
|
|
9971
|
+
latestVersion: 1,
|
|
9972
|
+
mode: preset.own ? followsPreset ? "live" : "pinned" : "copy"
|
|
9253
9973
|
},
|
|
9254
9974
|
projectIds: policy.id === this.projectPolicyId ? [this.projectId] : []
|
|
9255
9975
|
};
|
|
@@ -9260,22 +9980,28 @@ var MockCalmLensApi = class {
|
|
|
9260
9980
|
if (!policy) throw fail(API_ERROR_CODES.POLICY_NOT_FOUND);
|
|
9261
9981
|
return policy;
|
|
9262
9982
|
}
|
|
9263
|
-
|
|
9983
|
+
/** A page of the organisation's policies, oldest first unless `sort` says otherwise. */
|
|
9984
|
+
listPolicies(query) {
|
|
9264
9985
|
this.ensurePolicies();
|
|
9265
|
-
return {
|
|
9266
|
-
|
|
9267
|
-
|
|
9268
|
-
|
|
9986
|
+
return createPage([...this.policies.values()].map((policy) => this.toPolicy(policy)), this.pageQuery(query), {
|
|
9987
|
+
scope: `policies:${this.projectId}`,
|
|
9988
|
+
fail: this.pageFailure,
|
|
9989
|
+
defaultSort: [{
|
|
9990
|
+
field: "createdAt",
|
|
9991
|
+
direction: "asc"
|
|
9992
|
+
}]
|
|
9993
|
+
});
|
|
9269
9994
|
}
|
|
9270
9995
|
createPolicy(body) {
|
|
9271
9996
|
this.ensurePolicies();
|
|
9272
9997
|
const { name, description, settings, presetId } = body;
|
|
9273
9998
|
const preset = typeof presetId === "string" ? presetId : void 0;
|
|
9274
9999
|
if (settings === void 0 === (preset === void 0) || preset !== void 0 && !(preset in MOCK_PRESET_NAMES) || preset === void 0 && (typeof name !== "string" || !name.trim())) throw fail(API_ERROR_CODES.INVALID_VALUE, { message: "Send settings with a name, or presetId" });
|
|
10000
|
+
if (!preset) checkPolicySettings(settings);
|
|
9275
10001
|
const policy = this.newPolicy({
|
|
9276
10002
|
name: typeof name === "string" && name.trim() ? name.trim() : MOCK_PRESET_NAMES[preset],
|
|
9277
10003
|
...typeof description === "string" ? { description } : {},
|
|
9278
|
-
settings: preset ?
|
|
10004
|
+
settings: preset ? mockPresetSettings() : settings,
|
|
9279
10005
|
...preset ? { preset: {
|
|
9280
10006
|
id: preset,
|
|
9281
10007
|
version: 1,
|
|
@@ -9299,28 +10025,30 @@ var MockCalmLensApi = class {
|
|
|
9299
10025
|
}
|
|
9300
10026
|
updatePolicy(policyId, body) {
|
|
9301
10027
|
const policy = this.findPolicy(policyId);
|
|
10028
|
+
if (body.settings !== void 0) checkPolicySettings(body.settings);
|
|
9302
10029
|
return this.savePolicy(policy, body.revision, {
|
|
9303
10030
|
...typeof body.name === "string" ? { name: body.name.trim() } : {},
|
|
9304
10031
|
...typeof body.description === "string" ? { description: body.description } : {},
|
|
9305
10032
|
...body.settings !== void 0 ? { settings: body.settings } : {},
|
|
9306
|
-
|
|
10033
|
+
followsPreset: false
|
|
9307
10034
|
});
|
|
9308
10035
|
}
|
|
9309
|
-
|
|
10036
|
+
/** Resets a `pinned` or `copy` policy to its preset release, or upgrades it to the latest. */
|
|
10037
|
+
restorePolicy(policyId, operation, body) {
|
|
9310
10038
|
const policy = this.findPolicy(policyId);
|
|
9311
|
-
if (!policy.preset || policy.
|
|
10039
|
+
if (!policy.preset || policy.followsPreset) throw fail(API_ERROR_CODES.POLICY_NO_PRESET);
|
|
9312
10040
|
if (body.presetVersion !== policy.preset.version) throw fail(API_ERROR_CODES.POLICY_PRESET_VERSION_CONFLICT);
|
|
9313
|
-
if (
|
|
10041
|
+
if (operation === "upgrade") throw fail(API_ERROR_CODES.POLICY_PRESET_UPDATE_UNAVAILABLE);
|
|
9314
10042
|
return this.savePolicy(policy, body.revision, { settings: structuredClone(policy.baseline) });
|
|
9315
10043
|
}
|
|
9316
10044
|
setPolicyAutomaticUpdates(policyId, body) {
|
|
9317
10045
|
const policy = this.findPolicy(policyId);
|
|
9318
10046
|
const enabled = body.enabled === true;
|
|
9319
|
-
if (!policy.preset?.own || policy.
|
|
10047
|
+
if (!policy.preset?.own || policy.followsPreset === enabled) throw fail(API_ERROR_CODES.POLICY_AUTOMATIC_UPDATES_UNAVAILABLE);
|
|
9320
10048
|
if (body.revision !== policy.revision) throw fail(API_ERROR_CODES.POLICY_REVISION_CONFLICT);
|
|
9321
10049
|
if (body.presetVersion !== 1) throw fail(API_ERROR_CODES.POLICY_PRESET_VERSION_CONFLICT);
|
|
9322
10050
|
return this.savePolicy(policy, body.revision, {
|
|
9323
|
-
|
|
10051
|
+
followsPreset: enabled,
|
|
9324
10052
|
...enabled ? {
|
|
9325
10053
|
name: MOCK_PRESET_NAMES[policy.preset.id],
|
|
9326
10054
|
description: "",
|
|
@@ -9385,26 +10113,10 @@ var MockCalmLensApi = class {
|
|
|
9385
10113
|
}
|
|
9386
10114
|
listHashListEntries(hashListId, query) {
|
|
9387
10115
|
this.findHashList(hashListId);
|
|
9388
|
-
|
|
9389
|
-
|
|
9390
|
-
|
|
9391
|
-
|
|
9392
|
-
if (!Number.isInteger(start) || start < 0) throw fail(API_ERROR_CODES.INVALID_CURSOR);
|
|
9393
|
-
const items = entries.slice(start, start + pageSize);
|
|
9394
|
-
const hasMore = start + pageSize < entries.length;
|
|
9395
|
-
return {
|
|
9396
|
-
items,
|
|
9397
|
-
hasMore,
|
|
9398
|
-
nextCursor: hasMore ? btoa(String(start + pageSize)) : null,
|
|
9399
|
-
pageSize,
|
|
9400
|
-
ordering: [{
|
|
9401
|
-
field: "createdAt",
|
|
9402
|
-
direction: "desc"
|
|
9403
|
-
}, {
|
|
9404
|
-
field: "id",
|
|
9405
|
-
direction: "desc"
|
|
9406
|
-
}]
|
|
9407
|
-
};
|
|
10116
|
+
return createPage([...this.hashListEntries.values()].filter((entry) => entry.hashListId === hashListId), this.pageQuery(query), {
|
|
10117
|
+
scope: `hash-list-entries:${hashListId}`,
|
|
10118
|
+
fail: this.pageFailure
|
|
10119
|
+
});
|
|
9408
10120
|
}
|
|
9409
10121
|
async addHashListEntry(hashListId, body) {
|
|
9410
10122
|
const list = this.findHashList(hashListId);
|
|
@@ -9413,10 +10125,10 @@ var MockCalmLensApi = class {
|
|
|
9413
10125
|
const assetId = typeof body.assetId === "string" ? body.assetId : void 0;
|
|
9414
10126
|
if (hash === void 0 === (assetId === void 0)) throw fail(API_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Send either hash or assetId" });
|
|
9415
10127
|
if (assetId) {
|
|
9416
|
-
const
|
|
9417
|
-
if (!
|
|
9418
|
-
hash =
|
|
9419
|
-
quality =
|
|
10128
|
+
const { content } = await this.findOwnAsset(assetId);
|
|
10129
|
+
if (!content.perceptualHash) throw fail(API_ERROR_CODES.HASH_LIST_ASSET_NOT_HASHED);
|
|
10130
|
+
hash = content.perceptualHash;
|
|
10131
|
+
quality = content.perceptualHashQuality ?? null;
|
|
9420
10132
|
}
|
|
9421
10133
|
if (!/^[0-9a-f]{64}$/.test(hash)) throw fail(API_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Use a PDQ hash of 64 hexadecimal characters" });
|
|
9422
10134
|
const existing = [...this.hashListEntries.values()].find((entry) => entry.hashListId === list.id && entry.hash === hash);
|
|
@@ -9428,7 +10140,7 @@ var MockCalmLensApi = class {
|
|
|
9428
10140
|
hash,
|
|
9429
10141
|
quality,
|
|
9430
10142
|
label: typeof body.label === "string" ? body.label : null,
|
|
9431
|
-
|
|
10143
|
+
assetId: assetId ?? null,
|
|
9432
10144
|
createdAt: this.now()
|
|
9433
10145
|
};
|
|
9434
10146
|
this.hashListEntries.set(entry.id, entry);
|
|
@@ -9441,7 +10153,7 @@ var MockCalmLensApi = class {
|
|
|
9441
10153
|
const body = JSON.parse(new TextDecoder().decode(request.bytes));
|
|
9442
10154
|
return body && typeof body === "object" ? body : {};
|
|
9443
10155
|
} catch {
|
|
9444
|
-
throw
|
|
10156
|
+
throw invalidField("validation/invalid-type", "");
|
|
9445
10157
|
}
|
|
9446
10158
|
}
|
|
9447
10159
|
/** A whole number from the query, or `fallback` when absent; other values fail like zod. */
|
|
@@ -9453,26 +10165,22 @@ var MockCalmLensApi = class {
|
|
|
9453
10165
|
if (!Number.isInteger(value)) throw invalidField("validation/invalid-type", name);
|
|
9454
10166
|
return value;
|
|
9455
10167
|
}
|
|
9456
|
-
/** `
|
|
9457
|
-
|
|
9458
|
-
|
|
9459
|
-
for (const [key, value] of query) {
|
|
9460
|
-
const match = /^ordering\[(\d+)\]\[(field|direction)\]$/.exec(key);
|
|
9461
|
-
if (!match) continue;
|
|
9462
|
-
const index = Number(match[1]);
|
|
9463
|
-
ordering[index] = {
|
|
9464
|
-
...ordering[index],
|
|
9465
|
-
[match[2]]: value
|
|
9466
|
-
};
|
|
9467
|
-
}
|
|
9468
|
-
return ordering.filter(Boolean);
|
|
10168
|
+
/** The list's `sort`, from `fields`, as the API's `sortQueryField` reads it. */
|
|
10169
|
+
sort(query, fields, max) {
|
|
10170
|
+
return parseSort(query.get("sort"), fields, max, () => invalidField("validation/invalid-value", "sort"));
|
|
9469
10171
|
}
|
|
10172
|
+
/**
|
|
10173
|
+
* A cursor list's query, checked like CURSOR_PAGE_QUERY_SCHEMA before anything else: `pageSize`
|
|
10174
|
+
* from 1 to 100, and a `sort` of at most two fields from `createdAt` and `id`.
|
|
10175
|
+
*/
|
|
9470
10176
|
pageQuery(query) {
|
|
9471
|
-
const
|
|
10177
|
+
const pageSize = this.integer(query, "pageSize", 50);
|
|
10178
|
+
if (pageSize < 1) throw invalidField("validation/too-small", "pageSize");
|
|
10179
|
+
if (pageSize > 100) throw invalidField("validation/too-big", "pageSize");
|
|
9472
10180
|
return {
|
|
9473
|
-
pageSize
|
|
10181
|
+
pageSize,
|
|
9474
10182
|
cursor: query.get("cursor") ?? void 0,
|
|
9475
|
-
|
|
10183
|
+
sort: this.sort(query, ["createdAt", "id"], 2)
|
|
9476
10184
|
};
|
|
9477
10185
|
}
|
|
9478
10186
|
async findOwnAsset(assetId) {
|
|
@@ -9482,16 +10190,16 @@ var MockCalmLensApi = class {
|
|
|
9482
10190
|
}
|
|
9483
10191
|
async listAssets(query) {
|
|
9484
10192
|
let assets = await this.databaseStorage.listAssets(this.projectId);
|
|
9485
|
-
const
|
|
9486
|
-
if (folderId !== null) assets = assets.filter((asset) => (asset.folderId ?? null) === folderId);
|
|
10193
|
+
const page = this.pageQuery(query);
|
|
9487
10194
|
const filter = query.get("filter") ?? void 0;
|
|
9488
|
-
|
|
9489
|
-
|
|
9490
|
-
|
|
9491
|
-
|
|
9492
|
-
|
|
9493
|
-
|
|
9494
|
-
|
|
10195
|
+
try {
|
|
10196
|
+
assets = filterAssets(assets, filter);
|
|
10197
|
+
} catch (error) {
|
|
10198
|
+
if (error instanceof MockInvalidFilter) throw fail(API_ERROR_CODES.INVALID_FILTER);
|
|
10199
|
+
throw error;
|
|
10200
|
+
}
|
|
10201
|
+
return createPage(assets, page, {
|
|
10202
|
+
scope: JSON.stringify([this.projectId, filter ?? null]),
|
|
9495
10203
|
fail: this.pageFailure
|
|
9496
10204
|
});
|
|
9497
10205
|
}
|
|
@@ -9504,12 +10212,12 @@ var MockCalmLensApi = class {
|
|
|
9504
10212
|
const query = (params.get("query") ?? "").trim();
|
|
9505
10213
|
if (!query) throw invalidField("validation/too-small", "query");
|
|
9506
10214
|
if (query.length > MAX_SEARCH_QUERY_LENGTH) throw invalidField("validation/too-big", "query");
|
|
9507
|
-
const
|
|
9508
|
-
if (
|
|
9509
|
-
if (
|
|
10215
|
+
const pageSize = this.integer(params, "pageSize", 10);
|
|
10216
|
+
if (pageSize < 1) throw invalidField("validation/too-small", "pageSize");
|
|
10217
|
+
if (pageSize > MAX_SEARCH_PAGE_SIZE) throw invalidField("validation/too-big", "pageSize");
|
|
9510
10218
|
return {
|
|
9511
10219
|
query,
|
|
9512
|
-
items: searchAssets(await this.databaseStorage.listAssets(this.projectId), query,
|
|
10220
|
+
items: searchAssets(await this.databaseStorage.listAssets(this.projectId), query, pageSize),
|
|
9513
10221
|
semanticStatus: "disabled"
|
|
9514
10222
|
};
|
|
9515
10223
|
}
|
|
@@ -9521,6 +10229,24 @@ var MockCalmLensApi = class {
|
|
|
9521
10229
|
fail: this.pageFailure
|
|
9522
10230
|
});
|
|
9523
10231
|
}
|
|
10232
|
+
/**
|
|
10233
|
+
* An asset's findings timeline, as the API lists it (publicGetAssetTimeline.ts): refused while a
|
|
10234
|
+
* safety hold is on. The mock analyses no audio or video, so it keeps no segments: every
|
|
10235
|
+
* timeline is empty, and no cursor continues one.
|
|
10236
|
+
*/
|
|
10237
|
+
async listTimeline(assetId, query) {
|
|
10238
|
+
const asset = await this.findOwnAsset(assetId);
|
|
10239
|
+
const pageSize = this.integer(query, "pageSize", 50);
|
|
10240
|
+
if (pageSize < 1) throw invalidField("validation/too-small", "pageSize");
|
|
10241
|
+
if (pageSize > 100) throw invalidField("validation/too-big", "pageSize");
|
|
10242
|
+
if (asset.content.safetyHold) throw fail(API_ERROR_CODES.ASSET_HELD, { details: { hold: "safety" } });
|
|
10243
|
+
if (query.get("cursor") !== null) throw fail(API_ERROR_CODES.INVALID_CURSOR);
|
|
10244
|
+
return {
|
|
10245
|
+
items: [],
|
|
10246
|
+
nextCursor: null,
|
|
10247
|
+
hasMore: false
|
|
10248
|
+
};
|
|
10249
|
+
}
|
|
9524
10250
|
async updateAsset(assetId, body) {
|
|
9525
10251
|
checkMetadata(body.metadata);
|
|
9526
10252
|
if (body.name === null) throw invalidField("validation/invalid-type", "name");
|
|
@@ -9550,21 +10276,46 @@ var MockCalmLensApi = class {
|
|
|
9550
10276
|
updatedAt: this.now()
|
|
9551
10277
|
});
|
|
9552
10278
|
}
|
|
9553
|
-
|
|
10279
|
+
/**
|
|
10280
|
+
* Signs an asset URL like the API (`POST /projects/:projectId/signed-asset-urls`): a grant to one
|
|
10281
|
+
* asset, by ID or external ID, until it expires, serving the original only when asked. The mock's
|
|
10282
|
+
* grant is readable (its asset, original access and expiry), unlike the CDN's, and only the
|
|
10283
|
+
* mock's CDN accepts it.
|
|
10284
|
+
*/
|
|
10285
|
+
async signAssetUrl(body) {
|
|
9554
10286
|
for (const key of [
|
|
9555
|
-
"
|
|
9556
|
-
"
|
|
9557
|
-
"
|
|
9558
|
-
"
|
|
9559
|
-
|
|
9560
|
-
|
|
9561
|
-
|
|
9562
|
-
|
|
9563
|
-
|
|
9564
|
-
|
|
10287
|
+
"assetId",
|
|
10288
|
+
"externalId",
|
|
10289
|
+
"expiresIn",
|
|
10290
|
+
"original"
|
|
10291
|
+
]) if (body[key] === null) throw invalidField("validation/invalid-type", key);
|
|
10292
|
+
const { assetId, externalId } = body;
|
|
10293
|
+
if (assetId === void 0 === (externalId === void 0)) throw new ApiFailure(400, API_ERROR_CODES.INVALID_REQUEST, "Send either assetId or externalId", { issues: [{
|
|
10294
|
+
path: "",
|
|
10295
|
+
message: "Send either assetId or externalId",
|
|
10296
|
+
code: "custom"
|
|
10297
|
+
}] });
|
|
10298
|
+
const expiresIn = body.expiresIn ?? 900;
|
|
10299
|
+
if (typeof expiresIn !== "number" || !Number.isInteger(expiresIn)) throw invalidField("validation/invalid-type", "expiresIn");
|
|
10300
|
+
if (expiresIn < 1) throw invalidField("validation/too-small", "expiresIn");
|
|
10301
|
+
if (expiresIn > 86400) throw invalidField("validation/too-big", "expiresIn");
|
|
10302
|
+
const original = body.original ?? false;
|
|
10303
|
+
if (typeof original !== "boolean") throw invalidField("validation/invalid-type", "original");
|
|
10304
|
+
let asset;
|
|
10305
|
+
if (externalId !== void 0) {
|
|
10306
|
+
asset = (await this.databaseStorage.listAssets(this.projectId)).find((candidate) => candidate.externalId === externalId);
|
|
10307
|
+
if (!asset) throw fail(API_ERROR_CODES.ASSET_NOT_FOUND);
|
|
10308
|
+
} else asset = await this.findOwnAsset(String(assetId));
|
|
10309
|
+
if (asset.content.safetyHold) throw fail(API_ERROR_CODES.ASSET_HELD);
|
|
10310
|
+
const expiresAt = this.now() + expiresIn * 1e3;
|
|
10311
|
+
const grant = encodeMockGrant({
|
|
10312
|
+
assetId: asset.id,
|
|
10313
|
+
original,
|
|
10314
|
+
expiresAt
|
|
10315
|
+
});
|
|
9565
10316
|
return {
|
|
9566
|
-
url: `${this.
|
|
9567
|
-
expiresAt
|
|
10317
|
+
url: `${this.cdnBaseUrl}/assets/${encodeURIComponent(this.projectId)}/${grant}`,
|
|
10318
|
+
expiresAt
|
|
9568
10319
|
};
|
|
9569
10320
|
}
|
|
9570
10321
|
async retryAsset(assetId) {
|
|
@@ -9594,8 +10345,27 @@ var MockCalmLensApi = class {
|
|
|
9594
10345
|
}
|
|
9595
10346
|
if (asset.classification.moderation.manualReviews.length) throw fail(API_ERROR_CODES.ASSET_REVIEWED);
|
|
9596
10347
|
if (asset.content.status === "removed") throw fail(API_ERROR_CODES.ASSET_CONTENT_UNAVAILABLE);
|
|
9597
|
-
|
|
9598
|
-
|
|
10348
|
+
this.pushEvent({
|
|
10349
|
+
type: "asset.rerun-requested",
|
|
10350
|
+
actor: MOCK_API_KEY_ACTOR,
|
|
10351
|
+
severity: "low",
|
|
10352
|
+
assetId: asset.id,
|
|
10353
|
+
asset: toEventAsset(asset),
|
|
10354
|
+
facts: [
|
|
10355
|
+
assetNameFact(asset),
|
|
10356
|
+
{
|
|
10357
|
+
key: "asset-type",
|
|
10358
|
+
value: asset.type
|
|
10359
|
+
},
|
|
10360
|
+
{
|
|
10361
|
+
key: "moderation-status",
|
|
10362
|
+
value: asset.classification.moderation.status
|
|
10363
|
+
}
|
|
10364
|
+
]
|
|
10365
|
+
});
|
|
10366
|
+
const response = structuredClone(asset);
|
|
10367
|
+
await this.settleRerun(asset, await this.decisionFor(asset, "rerun", true));
|
|
10368
|
+
return response;
|
|
9599
10369
|
}
|
|
9600
10370
|
/**
|
|
9601
10371
|
* Checks every asset like the API (an ID that is not an asset ID is
|
|
@@ -9603,7 +10373,8 @@ var MockCalmLensApi = class {
|
|
|
9603
10373
|
* one `asset/not-found`, and either refuses the whole batch), then applies the operation at
|
|
9604
10374
|
* once instead of in the background: `delete` deletes each asset with its stored child
|
|
9605
10375
|
* assets and records `asset.deleted` with the asset as it was; `remove-content` removes
|
|
9606
|
-
* the content like `
|
|
10376
|
+
* the content like `removeContent`. Each event carries the batch's `batch-id` fact. Mock assets
|
|
10377
|
+
* carry no safety holds, so none is skipped.
|
|
9607
10378
|
*/
|
|
9608
10379
|
async batch(body) {
|
|
9609
10380
|
const assetIds = Array.isArray(body.assetIds) ? body.assetIds : [];
|
|
@@ -9616,26 +10387,31 @@ var MockCalmLensApi = class {
|
|
|
9616
10387
|
});
|
|
9617
10388
|
const assets = [];
|
|
9618
10389
|
for (const assetId of new Set(assetIds)) assets.push(await this.findOwnAsset(assetId));
|
|
10390
|
+
const batchId = this.createId("batch");
|
|
9619
10391
|
for (const asset of assets) if (operation === "delete") {
|
|
9620
10392
|
await this.deleteAssetTree(asset);
|
|
9621
|
-
this.recordLifecycleEvent("asset.deleted", asset);
|
|
9622
|
-
} else await this.removeContent(asset);
|
|
9623
|
-
return {
|
|
10393
|
+
this.recordLifecycleEvent("asset.deleted", asset, { batchId });
|
|
10394
|
+
} else await this.removeContent(asset, batchId);
|
|
10395
|
+
return {
|
|
10396
|
+
batchId,
|
|
10397
|
+
status: "queued",
|
|
10398
|
+
assetCount: assets.length
|
|
10399
|
+
};
|
|
9624
10400
|
}
|
|
9625
10401
|
/**
|
|
9626
10402
|
* Removes an asset's content like the API: the record and report stay; stored content,
|
|
9627
10403
|
* extracted text and the placeholder go. Content already removed keeps its reason, and an
|
|
9628
10404
|
* `asset.content-removed` event is recorded either way.
|
|
9629
10405
|
*/
|
|
9630
|
-
async removeContent(asset) {
|
|
10406
|
+
async removeContent(asset, batchId) {
|
|
9631
10407
|
await this.imageStorage.deleteContent(asset.id);
|
|
9632
10408
|
let removed = asset;
|
|
9633
10409
|
if (asset.content.status === "removed") {
|
|
9634
|
-
if (asset.content.
|
|
10410
|
+
if (asset.content.rendered || asset.content.preview) removed = await this.databaseStorage.updateAsset({
|
|
9635
10411
|
...asset,
|
|
9636
10412
|
content: {
|
|
9637
10413
|
...asset.content,
|
|
9638
|
-
|
|
10414
|
+
...NO_RENDERING
|
|
9639
10415
|
}
|
|
9640
10416
|
});
|
|
9641
10417
|
} else {
|
|
@@ -9649,13 +10425,13 @@ var MockCalmLensApi = class {
|
|
|
9649
10425
|
removalReason: "requested",
|
|
9650
10426
|
updatedAt: now,
|
|
9651
10427
|
extractedText: null,
|
|
9652
|
-
|
|
9653
|
-
|
|
10428
|
+
metadata: null,
|
|
10429
|
+
...NO_RENDERING
|
|
9654
10430
|
},
|
|
9655
10431
|
updatedAt: now
|
|
9656
10432
|
});
|
|
9657
10433
|
}
|
|
9658
|
-
this.recordLifecycleEvent("asset.content-removed", removed);
|
|
10434
|
+
this.recordLifecycleEvent("asset.content-removed", removed, { batchId });
|
|
9659
10435
|
return removed;
|
|
9660
10436
|
}
|
|
9661
10437
|
/** Deletes an asset, its stored child assets and their content. */
|
|
@@ -9690,7 +10466,7 @@ var MockCalmLensApi = class {
|
|
|
9690
10466
|
try {
|
|
9691
10467
|
return JSON.parse(raw);
|
|
9692
10468
|
} catch {
|
|
9693
|
-
throw invalidField("validation/invalid-
|
|
10469
|
+
throw invalidField("validation/invalid-value", name);
|
|
9694
10470
|
}
|
|
9695
10471
|
};
|
|
9696
10472
|
const total = query.get("totalSizeInBytes");
|
|
@@ -9701,6 +10477,7 @@ var MockCalmLensApi = class {
|
|
|
9701
10477
|
processingOptions: parsed("processingOptions"),
|
|
9702
10478
|
visibility: text("visibility"),
|
|
9703
10479
|
metadata: parsed("metadata"),
|
|
10480
|
+
tags: parsed("tags"),
|
|
9704
10481
|
authorId: text("authorId"),
|
|
9705
10482
|
context: parsed("context"),
|
|
9706
10483
|
externalId: text("externalId"),
|
|
@@ -9717,22 +10494,25 @@ var MockCalmLensApi = class {
|
|
|
9717
10494
|
};
|
|
9718
10495
|
}
|
|
9719
10496
|
async createAsset(request) {
|
|
9720
|
-
return this.storeAsset(this.uploadFields(request), request.query.get("
|
|
10497
|
+
return this.storeAsset(this.uploadFields(request), request.query.get("wait") === "false");
|
|
9721
10498
|
}
|
|
9722
10499
|
async storeAsset(fields, queue) {
|
|
9723
10500
|
if (!fields.name) throw fail(API_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Asset name is required" });
|
|
9724
10501
|
const { contentType, part } = fields;
|
|
9725
10502
|
const bytes = fields.bytes ?? /* @__PURE__ */ new Uint8Array(0);
|
|
9726
|
-
if (contentType !== void 0 && !isAcceptedUploadMimeType(contentType)) throw fail(API_ERROR_CODES.
|
|
10503
|
+
if (contentType !== void 0 && !isAcceptedUploadMimeType(contentType)) throw fail(API_ERROR_CODES.UNSUPPORTED_UPLOAD_TYPE, {
|
|
9727
10504
|
message: getRejectedUploadMimeTypeError(contentType),
|
|
9728
|
-
details: {
|
|
10505
|
+
details: {
|
|
10506
|
+
mimeType: contentType,
|
|
10507
|
+
source: "calmlens"
|
|
10508
|
+
}
|
|
9729
10509
|
});
|
|
9730
10510
|
const type = inferAssetType(fields.url === void 0 ? contentType ?? "" : void 0);
|
|
9731
10511
|
if (part) {
|
|
9732
10512
|
if (part.partNumber > 1) throw fail(API_ERROR_CODES.USE_UPLOAD_PART);
|
|
9733
10513
|
const partSize = bytes.byteLength;
|
|
9734
10514
|
const sizeError = (part.totalSizeInBytes === void 0 ? void 0 : this.multipartImageSizeError(type, part.totalSizeInBytes)) ?? (part.isFinal ? this.multipartTotalSizeError(part.totalSizeInBytes, partSize, true) ?? this.multipartImageSizeError(type, partSize) : void 0);
|
|
9735
|
-
if (sizeError) throw
|
|
10515
|
+
if (sizeError) throw this.sizeFailure(sizeError);
|
|
9736
10516
|
const validation = validateAssetPayload({ uint8Array: bytes }, {
|
|
9737
10517
|
multipart: true,
|
|
9738
10518
|
isFinal: part.isFinal
|
|
@@ -9752,6 +10532,7 @@ var MockCalmLensApi = class {
|
|
|
9752
10532
|
const isOpenUpload = part !== void 0 && !part.isFinal;
|
|
9753
10533
|
const processingOptions = fields.processingOptions ?? { keepContentAfterProcessing: false };
|
|
9754
10534
|
const keepContent = processingOptions.keepContentAfterProcessing === true;
|
|
10535
|
+
this.processingOptions.set(id, processingOptions);
|
|
9755
10536
|
const hasContent = fields.url === void 0;
|
|
9756
10537
|
const contextItems = selectModerationContextItems(fields.context?.items ?? []);
|
|
9757
10538
|
const asset = {
|
|
@@ -9767,11 +10548,10 @@ var MockCalmLensApi = class {
|
|
|
9767
10548
|
externalId: fields.externalId ?? null,
|
|
9768
10549
|
externalUrl: fields.url ?? fields.externalUrl ?? null,
|
|
9769
10550
|
type,
|
|
9770
|
-
metadata: fields.metadata ??
|
|
10551
|
+
metadata: fields.metadata ?? [],
|
|
9771
10552
|
authorId: fields.authorId ?? null,
|
|
9772
10553
|
context: contextItems.length ? { items: contextItems } : null,
|
|
9773
|
-
tags:
|
|
9774
|
-
processingOptions,
|
|
10554
|
+
tags: fields.tags ?? [],
|
|
9775
10555
|
visibility: fields.visibility ?? "private",
|
|
9776
10556
|
content: {
|
|
9777
10557
|
status: keepContent ? "stored" : "temporary",
|
|
@@ -9782,9 +10562,15 @@ var MockCalmLensApi = class {
|
|
|
9782
10562
|
extension: null,
|
|
9783
10563
|
sizeInBytes: hasContent ? bytes.byteLength : null,
|
|
9784
10564
|
checksums: null,
|
|
9785
|
-
|
|
10565
|
+
width: null,
|
|
10566
|
+
height: null,
|
|
10567
|
+
durationSeconds: null,
|
|
10568
|
+
pageCount: null,
|
|
10569
|
+
perceptualHash: null,
|
|
10570
|
+
perceptualHashQuality: null,
|
|
10571
|
+
metadata: null,
|
|
9786
10572
|
extractedText: null,
|
|
9787
|
-
|
|
10573
|
+
...NO_RENDERING
|
|
9788
10574
|
},
|
|
9789
10575
|
upload: {
|
|
9790
10576
|
status: isOpenUpload ? "uploading" : "completed",
|
|
@@ -9792,16 +10578,27 @@ var MockCalmLensApi = class {
|
|
|
9792
10578
|
partsReceived: isOpenUpload ? part.partNumber : null
|
|
9793
10579
|
},
|
|
9794
10580
|
classification: {
|
|
9795
|
-
status: isOpenUpload ? "
|
|
10581
|
+
status: isOpenUpload ? "queued" : "processing",
|
|
9796
10582
|
failureReason: null,
|
|
9797
10583
|
updatedAt: now,
|
|
9798
10584
|
completedAt: null,
|
|
10585
|
+
progress: null,
|
|
10586
|
+
testInput: false,
|
|
9799
10587
|
moderation: {
|
|
9800
10588
|
status: "pending",
|
|
9801
10589
|
manualReviews: [],
|
|
10590
|
+
automatedStatus: null,
|
|
10591
|
+
source: null,
|
|
10592
|
+
rule: null,
|
|
10593
|
+
explanationComplete: null,
|
|
10594
|
+
categories: null,
|
|
9802
10595
|
flaggedCategories: [],
|
|
9803
|
-
regions: []
|
|
9804
|
-
|
|
10596
|
+
regions: [],
|
|
10597
|
+
raw: null,
|
|
10598
|
+
timeline: null
|
|
10599
|
+
},
|
|
10600
|
+
ocr: null,
|
|
10601
|
+
linkDetection: null
|
|
9805
10602
|
}
|
|
9806
10603
|
};
|
|
9807
10604
|
if (part) {
|
|
@@ -9820,6 +10617,10 @@ var MockCalmLensApi = class {
|
|
|
9820
10617
|
type: contentType ?? ""
|
|
9821
10618
|
});
|
|
9822
10619
|
if (isOpenUpload) return this.databaseStorage.createAsset(asset);
|
|
10620
|
+
if (hasContent) asset.content = {
|
|
10621
|
+
...asset.content,
|
|
10622
|
+
...mockRendering(asset, false)
|
|
10623
|
+
};
|
|
9823
10624
|
const decision = await this.decisionFor(asset, "upload", queue, hasContent ? bytes : null);
|
|
9824
10625
|
const stored = await this.databaseStorage.createAsset(decision.status === "pending" ? asset : await this.decided(asset, decision));
|
|
9825
10626
|
this.recordUploaded(asset);
|
|
@@ -9844,7 +10645,7 @@ var MockCalmLensApi = class {
|
|
|
9844
10645
|
const received = (asset.content.sizeInBytes ?? 0) + bytes.byteLength;
|
|
9845
10646
|
const upload = this.openUploads.get(assetId);
|
|
9846
10647
|
const sizeError = this.multipartImageSizeError(asset.type, received) ?? this.multipartTotalSizeError(upload?.totalSizeInBytes, received, isFinal);
|
|
9847
|
-
if (sizeError) throw
|
|
10648
|
+
if (sizeError) throw this.sizeFailure(sizeError);
|
|
9848
10649
|
const validation = validateAssetPayload({ uint8Array: bytes }, {
|
|
9849
10650
|
multipart: true,
|
|
9850
10651
|
isFinal
|
|
@@ -9855,8 +10656,8 @@ var MockCalmLensApi = class {
|
|
|
9855
10656
|
partNumber,
|
|
9856
10657
|
isFinal
|
|
9857
10658
|
});
|
|
9858
|
-
const
|
|
9859
|
-
const queue =
|
|
10659
|
+
const waitOverride = query.get("wait");
|
|
10660
|
+
const queue = waitOverride === null ? upload?.queue ?? false : waitOverride === "false";
|
|
9860
10661
|
if (isFinal) this.openUploads.delete(assetId);
|
|
9861
10662
|
const now = this.now();
|
|
9862
10663
|
const next = {
|
|
@@ -9883,7 +10684,14 @@ var MockCalmLensApi = class {
|
|
|
9883
10684
|
/** Like the API: image uploads in parts total at most 20 MiB, at any size below that. */
|
|
9884
10685
|
multipartImageSizeError(type, sizeInBytes) {
|
|
9885
10686
|
if (type !== "image") return void 0;
|
|
9886
|
-
if (sizeInBytes > MAX_IMAGE_UPLOAD_BYTES) return API_ERROR_CODES.
|
|
10687
|
+
if (sizeInBytes > MAX_IMAGE_UPLOAD_BYTES) return API_ERROR_CODES.UPLOAD_TOO_LARGE;
|
|
10688
|
+
}
|
|
10689
|
+
/** The failure for a size error code, with the limit `upload/too-large` carries. */
|
|
10690
|
+
sizeFailure(code) {
|
|
10691
|
+
return fail(code, code === API_ERROR_CODES.UPLOAD_TOO_LARGE ? { details: {
|
|
10692
|
+
limitBytes: MAX_IMAGE_UPLOAD_BYTES,
|
|
10693
|
+
source: "plan"
|
|
10694
|
+
} } : {});
|
|
9887
10695
|
}
|
|
9888
10696
|
/** Like the API: the parts may not overshoot a declared total, and must reach it at the end. */
|
|
9889
10697
|
multipartTotalSizeError(totalSizeInBytes, receivedSizeInBytes, isFinal) {
|
|
@@ -9912,9 +10720,37 @@ var MockCalmLensApi = class {
|
|
|
9912
10720
|
queued
|
|
9913
10721
|
}));
|
|
9914
10722
|
}
|
|
9915
|
-
/**
|
|
9916
|
-
|
|
9917
|
-
|
|
10723
|
+
/**
|
|
10724
|
+
* `asset` decided as `decision` says, with its report. Like the API for an image with nobody in
|
|
10725
|
+
* it, a decided image reports the person and face detection its upload turned on.
|
|
10726
|
+
*/
|
|
10727
|
+
async decided(asset, decision) {
|
|
10728
|
+
const next = await this.applyOutcome(asset, decision.status, "policy", decision.report());
|
|
10729
|
+
const features = this.processingOptions.get(asset.id)?.features;
|
|
10730
|
+
if (next.classification.status !== "completed" || asset.type !== "image") return next;
|
|
10731
|
+
return {
|
|
10732
|
+
...next,
|
|
10733
|
+
classification: {
|
|
10734
|
+
...next.classification,
|
|
10735
|
+
...features?.ocr?.enabled === true && { ocr: {
|
|
10736
|
+
status: "evaluated",
|
|
10737
|
+
textRegions: []
|
|
10738
|
+
} },
|
|
10739
|
+
...features?.linkDetection?.enabled !== false && { linkDetection: {
|
|
10740
|
+
status: "evaluated",
|
|
10741
|
+
qrCodes: [],
|
|
10742
|
+
links: []
|
|
10743
|
+
} },
|
|
10744
|
+
...features?.personDetection?.enabled === true && { personDetection: {
|
|
10745
|
+
status: "evaluated",
|
|
10746
|
+
people: []
|
|
10747
|
+
} },
|
|
10748
|
+
...features?.faceDetection?.enabled === true && { faceDetection: {
|
|
10749
|
+
status: "evaluated",
|
|
10750
|
+
faces: []
|
|
10751
|
+
} }
|
|
10752
|
+
}
|
|
10753
|
+
};
|
|
9918
10754
|
}
|
|
9919
10755
|
/**
|
|
9920
10756
|
* Stores a processing asset's `decision` and records its events; a `pending` decision leaves it
|
|
@@ -9926,6 +10762,20 @@ var MockCalmLensApi = class {
|
|
|
9926
10762
|
this.recordProcessingEvents(stored, decision);
|
|
9927
10763
|
return stored;
|
|
9928
10764
|
}
|
|
10765
|
+
/**
|
|
10766
|
+
* Applies a re-run's `decision` to a decided asset like the pipeline: a `pending` decision stays
|
|
10767
|
+
* queued for `completeProcessing`, a `failed` one keeps the asset's decision, and any other
|
|
10768
|
+
* replaces it with the pipeline's events.
|
|
10769
|
+
*/
|
|
10770
|
+
async settleRerun(asset, decision) {
|
|
10771
|
+
this.pendingReruns.delete(asset.id);
|
|
10772
|
+
if (decision.status === "pending") {
|
|
10773
|
+
this.pendingReruns.add(asset.id);
|
|
10774
|
+
return asset;
|
|
10775
|
+
}
|
|
10776
|
+
if (decision.status === "failed") return asset;
|
|
10777
|
+
return this.settle(asset, decision);
|
|
10778
|
+
}
|
|
9929
10779
|
/** Asks `moderate` (unless `decision` is given) and settles a processing asset. */
|
|
9930
10780
|
async moderateStored(asset, trigger, decision) {
|
|
9931
10781
|
return this.settle(asset, decision ?? await this.decisionFor(asset, trigger, true));
|
|
@@ -9959,7 +10809,8 @@ var MockCalmLensApi = class {
|
|
|
9959
10809
|
if (decision.status === "pending") return;
|
|
9960
10810
|
const event = {
|
|
9961
10811
|
assetId: asset.id,
|
|
9962
|
-
asset: toEventAsset(asset)
|
|
10812
|
+
asset: toEventAsset(asset),
|
|
10813
|
+
actor: AUTOMATED_ACTOR
|
|
9963
10814
|
};
|
|
9964
10815
|
if (decision.status === "failed") {
|
|
9965
10816
|
this.pushEvent({
|
|
@@ -10010,10 +10861,6 @@ var MockCalmLensApi = class {
|
|
|
10010
10861
|
type: `asset.${decision.status}`,
|
|
10011
10862
|
severity: decision.status === "allowed" || decision.status === "limited" ? "low" : "high",
|
|
10012
10863
|
facts: [
|
|
10013
|
-
{
|
|
10014
|
-
key: "decided-by",
|
|
10015
|
-
value: "automated"
|
|
10016
|
-
},
|
|
10017
10864
|
{
|
|
10018
10865
|
key: "flagged-categories",
|
|
10019
10866
|
value: flaggedCategories
|
|
@@ -10029,8 +10876,8 @@ var MockCalmLensApi = class {
|
|
|
10029
10876
|
/**
|
|
10030
10877
|
* Moves an asset to `outcome` like the pipeline would: a decision finishes processing, `purged`
|
|
10031
10878
|
* deletes the content (with `purgeReason`, `policy` for the pipeline), and finished processing
|
|
10032
|
-
* removes content kept only temporarily (`keepContentAfterProcessing` off). A
|
|
10033
|
-
*
|
|
10879
|
+
* removes content kept only temporarily (`keepContentAfterProcessing` off). A decided asset gets
|
|
10880
|
+
* its rendered file and preview (with a ThumbHash for its placeholder), which go with its content.
|
|
10034
10881
|
*/
|
|
10035
10882
|
async applyOutcome(asset, outcome, purgeReason = "policy", report) {
|
|
10036
10883
|
const now = this.now();
|
|
@@ -10039,15 +10886,7 @@ var MockCalmLensApi = class {
|
|
|
10039
10886
|
const purge = outcome === "purged" && asset.content.status !== "removed";
|
|
10040
10887
|
const removeContent = purge || classificationStatus === "completed" && asset.content.status === "temporary";
|
|
10041
10888
|
if (removeContent) await this.imageStorage.deleteContent(asset.id);
|
|
10042
|
-
const
|
|
10043
|
-
width: null,
|
|
10044
|
-
height: null,
|
|
10045
|
-
perceptualHash: null,
|
|
10046
|
-
perceptualHashQuality: null,
|
|
10047
|
-
...asset.content.image,
|
|
10048
|
-
thumbhash: MOCK_THUMBHASH,
|
|
10049
|
-
averageColor: asset.content.image?.averageColor ?? "#7f7f7f"
|
|
10050
|
-
} : outcome === "purged" ? withoutPlaceholder(asset.content.image) : asset.content.image;
|
|
10889
|
+
const rendering = asset.content.status !== "removed" && !purge && !asset.content.safetyHold ? mockRendering(asset, decided) : NO_RENDERING;
|
|
10051
10890
|
return {
|
|
10052
10891
|
...asset,
|
|
10053
10892
|
updatedAt: now,
|
|
@@ -10058,11 +10897,11 @@ var MockCalmLensApi = class {
|
|
|
10058
10897
|
removalReason: purge ? purgeReason : "retention",
|
|
10059
10898
|
updatedAt: now,
|
|
10060
10899
|
extractedText: null,
|
|
10061
|
-
|
|
10062
|
-
|
|
10900
|
+
metadata: null,
|
|
10901
|
+
...NO_RENDERING
|
|
10063
10902
|
} : {
|
|
10064
10903
|
...asset.content,
|
|
10065
|
-
|
|
10904
|
+
...rendering
|
|
10066
10905
|
},
|
|
10067
10906
|
upload: {
|
|
10068
10907
|
...asset.upload,
|
|
@@ -10084,20 +10923,29 @@ var MockCalmLensApi = class {
|
|
|
10084
10923
|
};
|
|
10085
10924
|
}
|
|
10086
10925
|
/** Adds an `asset.deleted`, `asset.content-removed` or `asset.retried` event, like the API. */
|
|
10087
|
-
recordLifecycleEvent(type, asset) {
|
|
10926
|
+
recordLifecycleEvent(type, asset, options = {}) {
|
|
10927
|
+
const { batchId, actor = MOCK_API_KEY_ACTOR } = options;
|
|
10088
10928
|
this.pushEvent({
|
|
10089
10929
|
type,
|
|
10930
|
+
actor,
|
|
10090
10931
|
severity: "low",
|
|
10091
10932
|
assetId: asset.id,
|
|
10092
10933
|
asset: toEventAsset(asset),
|
|
10093
|
-
facts: [
|
|
10094
|
-
|
|
10095
|
-
|
|
10096
|
-
|
|
10097
|
-
|
|
10098
|
-
|
|
10099
|
-
|
|
10100
|
-
|
|
10934
|
+
facts: [
|
|
10935
|
+
{
|
|
10936
|
+
key: "asset-name",
|
|
10937
|
+
value: asset.name,
|
|
10938
|
+
...type === "asset.deleted" ? {} : { url: `https://calmlens.com/go/asset/${asset.id}` }
|
|
10939
|
+
},
|
|
10940
|
+
{
|
|
10941
|
+
key: "asset-type",
|
|
10942
|
+
value: asset.type
|
|
10943
|
+
},
|
|
10944
|
+
...batchId ? [{
|
|
10945
|
+
key: "batch-id",
|
|
10946
|
+
value: batchId
|
|
10947
|
+
}] : []
|
|
10948
|
+
]
|
|
10101
10949
|
});
|
|
10102
10950
|
}
|
|
10103
10951
|
/**
|
|
@@ -10112,7 +10960,15 @@ var MockCalmLensApi = class {
|
|
|
10112
10960
|
name: asset.name,
|
|
10113
10961
|
uploadPresetId,
|
|
10114
10962
|
createdAt: asset.createdAt,
|
|
10115
|
-
|
|
10963
|
+
upload: {
|
|
10964
|
+
status: asset.upload.status,
|
|
10965
|
+
partsReceived: asset.upload.partsReceived
|
|
10966
|
+
},
|
|
10967
|
+
classification: {
|
|
10968
|
+
status: asset.classification.status,
|
|
10969
|
+
failureReason: asset.classification.failureReason,
|
|
10970
|
+
...preset.showDecisions !== false && { moderation: { status: asset.classification.moderation.status } }
|
|
10971
|
+
}
|
|
10116
10972
|
});
|
|
10117
10973
|
const [assetId, action] = rest;
|
|
10118
10974
|
if (assetId !== void 0) {
|
|
@@ -10126,19 +10982,32 @@ var MockCalmLensApi = class {
|
|
|
10126
10982
|
const fields = this.uploadFields(request);
|
|
10127
10983
|
const token = request.headers.get("authorization");
|
|
10128
10984
|
if (preset.requireToken && !token) throw fail(API_ERROR_CODES.UPLOAD_PRESET_TOKEN_REQUIRED);
|
|
10985
|
+
const tokenExternalId = token ? decodeUploadToken(token.replace(/^Bearer /, ""))?.claims.x : void 0;
|
|
10986
|
+
if (fields.externalId && fields.externalId !== tokenExternalId) throw fail(API_ERROR_CODES.UPLOAD_PRESET_FIELD_NOT_ALLOWED, { details: { field: "externalId" } });
|
|
10987
|
+
for (const field of [
|
|
10988
|
+
"metadata",
|
|
10989
|
+
"tags",
|
|
10990
|
+
"authorId",
|
|
10991
|
+
"context",
|
|
10992
|
+
"processingOptions"
|
|
10993
|
+
]) if (fields[field] !== void 0 && fields[field] !== null) throw fail(API_ERROR_CODES.UPLOAD_PRESET_FIELD_NOT_ALLOWED, { details: { field } });
|
|
10129
10994
|
if (preset.allowedContentTypes && !isFileAccepted({
|
|
10130
10995
|
name: fields.name,
|
|
10131
10996
|
type: fields.contentType ?? ""
|
|
10132
|
-
}, preset.allowedContentTypes.join(","))) throw fail(API_ERROR_CODES.
|
|
10997
|
+
}, preset.allowedContentTypes.join(","))) throw fail(API_ERROR_CODES.UNSUPPORTED_UPLOAD_TYPE, { details: { source: "preset" } });
|
|
10133
10998
|
const size = fields.part?.totalSizeInBytes ?? fields.bytes?.byteLength;
|
|
10134
|
-
if (preset.maxFileSizeInBytes !== void 0 && size !== void 0 && size > preset.maxFileSizeInBytes) throw fail(API_ERROR_CODES.
|
|
10999
|
+
if (preset.maxFileSizeInBytes !== void 0 && size !== void 0 && size > preset.maxFileSizeInBytes) throw fail(API_ERROR_CODES.UPLOAD_TOO_LARGE, { details: {
|
|
11000
|
+
limitBytes: preset.maxFileSizeInBytes,
|
|
11001
|
+
source: "preset"
|
|
11002
|
+
} });
|
|
10135
11003
|
const asset = await this.storeAsset({
|
|
10136
11004
|
...fields,
|
|
11005
|
+
externalId: tokenExternalId,
|
|
10137
11006
|
uploadPresetId,
|
|
10138
11007
|
visibility: preset.visibility ?? "public",
|
|
10139
11008
|
folderId: preset.folderId ?? void 0,
|
|
10140
11009
|
processingOptions: { keepContentAfterProcessing: preset.keepContentAfterProcessing ?? true }
|
|
10141
|
-
}, request.query.get("
|
|
11010
|
+
}, request.query.get("wait") === "false");
|
|
10142
11011
|
session.uploads.add(asset.id);
|
|
10143
11012
|
return toUploaded(asset);
|
|
10144
11013
|
}
|
|
@@ -10151,22 +11020,33 @@ var MockCalmLensApi = class {
|
|
|
10151
11020
|
const pageSize = this.integer(query, "pageSize", 50);
|
|
10152
11021
|
if (pageSize < 1) throw invalidField("validation/too-small", "pageSize");
|
|
10153
11022
|
if (pageSize > MAX_PAGE_SIZE) throw invalidField("validation/too-big", "pageSize");
|
|
11023
|
+
const typesValue = query.get("types");
|
|
11024
|
+
const types = typesValue === null ? [] : typesValue.split(",");
|
|
11025
|
+
if (typesValue !== null && (types.length === 0 || types.some((type) => !EVENT_TYPE_PATTERN.test(type)))) throw invalidField("validation/invalid-value", "types");
|
|
11026
|
+
const assetId = query.get("assetId");
|
|
11027
|
+
if (assetId !== null && !isId(assetId, "asset")) throw invalidField("validation/invalid-format", "assetId");
|
|
11028
|
+
const filter = btoa(JSON.stringify([[...new Set(types)].sort(), assetId]));
|
|
11029
|
+
const matches = (event) => (!types.length || types.includes(event.type)) && (assetId === null || event.assetId === assetId);
|
|
10154
11030
|
const cursor = query.get("cursor");
|
|
10155
11031
|
if (!cursor) return {
|
|
10156
11032
|
items: [],
|
|
10157
|
-
nextCursor: `mock_${this.eventFeed.length}`,
|
|
11033
|
+
nextCursor: `mock_${this.eventFeed.length}~${filter}`,
|
|
10158
11034
|
hasMore: false,
|
|
10159
11035
|
excludedTypes: []
|
|
10160
11036
|
};
|
|
10161
|
-
const match = /^mock_(\d+)$/.exec(cursor);
|
|
10162
|
-
const position = match ? Number(match[1]) : NaN;
|
|
11037
|
+
const match = /^mock_(\d+)~(.*)$/.exec(cursor);
|
|
11038
|
+
const position = match && match[2] === filter ? Number(match[1]) : NaN;
|
|
10163
11039
|
if (!Number.isSafeInteger(position) || position > this.eventFeed.length) throw fail(API_ERROR_CODES.INVALID_CURSOR);
|
|
10164
|
-
const items =
|
|
10165
|
-
|
|
11040
|
+
const items = [];
|
|
11041
|
+
let next = position;
|
|
11042
|
+
while (next < this.eventFeed.length && items.length < pageSize) {
|
|
11043
|
+
const event = this.eventFeed[next++];
|
|
11044
|
+
if (matches(event)) items.push(event);
|
|
11045
|
+
}
|
|
10166
11046
|
return {
|
|
10167
11047
|
items,
|
|
10168
|
-
nextCursor: `mock_${next}`,
|
|
10169
|
-
hasMore:
|
|
11048
|
+
nextCursor: `mock_${next}~${filter}`,
|
|
11049
|
+
hasMore: this.eventFeed.slice(next).some(matches),
|
|
10170
11050
|
excludedTypes: []
|
|
10171
11051
|
};
|
|
10172
11052
|
}
|
|
@@ -10178,12 +11058,12 @@ var MockCalmLensApi = class {
|
|
|
10178
11058
|
const all = await this.databaseStorage.listFolders?.(this.projectId) ?? [];
|
|
10179
11059
|
const parentId = query.get("parentId");
|
|
10180
11060
|
const search = query.get("search")?.trim() || null;
|
|
10181
|
-
const
|
|
11061
|
+
const sort = this.sort(query, ["name", "createdAt"], 1);
|
|
10182
11062
|
const matching = all.filter((folder) => (folder.parentId ?? null) === parentId && (!search || folder.name.toLowerCase().includes(search.toLowerCase())));
|
|
10183
11063
|
const page = createPage(matching, {
|
|
10184
11064
|
cursor: query.get("cursor") ?? void 0,
|
|
10185
11065
|
pageSize: this.integer(query, "pageSize", 50),
|
|
10186
|
-
|
|
11066
|
+
sort
|
|
10187
11067
|
}, {
|
|
10188
11068
|
scope: JSON.stringify([
|
|
10189
11069
|
this.projectId,
|
|
@@ -10191,7 +11071,7 @@ var MockCalmLensApi = class {
|
|
|
10191
11071
|
search
|
|
10192
11072
|
]),
|
|
10193
11073
|
fail: this.pageFailure,
|
|
10194
|
-
|
|
11074
|
+
defaultSort: [{
|
|
10195
11075
|
field: "name",
|
|
10196
11076
|
direction: "asc"
|
|
10197
11077
|
}]
|
|
@@ -10207,21 +11087,9 @@ var MockCalmLensApi = class {
|
|
|
10207
11087
|
items: folders,
|
|
10208
11088
|
ancestors: all.filter((folder) => ids.has(folder.id) && !folders.some((item) => item.id === folder.id)),
|
|
10209
11089
|
nextCursor: page.nextCursor,
|
|
10210
|
-
|
|
10211
|
-
hasMore: page.hasMore,
|
|
10212
|
-
ordering: page.ordering
|
|
11090
|
+
hasMore: page.hasMore
|
|
10213
11091
|
};
|
|
10214
11092
|
}
|
|
10215
|
-
/** The folder list's `ordering`: at most one rule, by `name` or `createdAt`. */
|
|
10216
|
-
folderOrdering(query) {
|
|
10217
|
-
const ordering = this.ordering(query);
|
|
10218
|
-
if (ordering.length > 1) throw invalidField("validation/too-big", "ordering");
|
|
10219
|
-
for (const [index, rule] of ordering.entries()) {
|
|
10220
|
-
if (rule.field !== "name" && rule.field !== "createdAt") throw invalidField("validation/invalid-value", `ordering.${index}.field`);
|
|
10221
|
-
if (rule.direction !== "asc" && rule.direction !== "desc") throw invalidField("validation/invalid-value", `ordering.${index}.direction`);
|
|
10222
|
-
}
|
|
10223
|
-
return ordering;
|
|
10224
|
-
}
|
|
10225
11093
|
async createFolder(body) {
|
|
10226
11094
|
const name = body.name;
|
|
10227
11095
|
if (typeof name !== "string" || !name) throw fail(API_ERROR_CODES.INVALID_UPLOAD_PAYLOAD, { message: "Folder name is required" });
|
|
@@ -10292,7 +11160,7 @@ var MockCalmLensApi = class {
|
|
|
10292
11160
|
async assertUniqueFolderName(name, parentId, excludeFolderId) {
|
|
10293
11161
|
if ((await this.databaseStorage.listFolders?.(this.projectId) ?? []).find((folder) => folder.id !== excludeFolderId && (folder.parentId ?? null) === parentId && folder.name === name)) throw fail(API_ERROR_CODES.DUPLICATE_FOLDER_NAME, { message: `A folder named "${name}" already exists in this location.` });
|
|
10294
11162
|
}
|
|
10295
|
-
/** Saves each lens in order, like the API: new ones, or
|
|
11163
|
+
/** Saves each lens in order, like the API: new ones, or a changed definition. */
|
|
10296
11164
|
ensureLenses(body) {
|
|
10297
11165
|
const lenses = Array.isArray(body.lenses) ? body.lenses : [];
|
|
10298
11166
|
if (lenses.length === 0) throw invalidField("validation/too-small", "lenses");
|
|
@@ -10300,61 +11168,124 @@ var MockCalmLensApi = class {
|
|
|
10300
11168
|
}
|
|
10301
11169
|
ensureLens(options) {
|
|
10302
11170
|
const existing = this.lenses.get(options.identifier);
|
|
10303
|
-
if (!existing)
|
|
10304
|
-
|
|
10305
|
-
|
|
10306
|
-
|
|
10307
|
-
|
|
10308
|
-
|
|
10309
|
-
|
|
10310
|
-
|
|
10311
|
-
|
|
10312
|
-
|
|
10313
|
-
|
|
10314
|
-
|
|
10315
|
-
|
|
10316
|
-
|
|
10317
|
-
|
|
10318
|
-
|
|
10319
|
-
|
|
10320
|
-
|
|
10321
|
-
|
|
10322
|
-
|
|
10323
|
-
|
|
10324
|
-
|
|
10325
|
-
|
|
10326
|
-
|
|
10327
|
-
|
|
10328
|
-
|
|
10329
|
-
|
|
11171
|
+
if (!existing) return this.saveNewLens(options);
|
|
11172
|
+
if (existing.name === options.name && (existing.description ?? null) === (options.description ?? null) && existing.type === options.type && existing.active === options.active && stringifyJsonCanonical(existing.transform ?? null) === stringifyJsonCanonical(options.transform ?? null)) return existing;
|
|
11173
|
+
const lens = {
|
|
11174
|
+
...existing,
|
|
11175
|
+
...options,
|
|
11176
|
+
updatedAt: this.now()
|
|
11177
|
+
};
|
|
11178
|
+
this.lenses.set(lens.identifier, lens);
|
|
11179
|
+
return lens;
|
|
11180
|
+
}
|
|
11181
|
+
saveNewLens(options) {
|
|
11182
|
+
const now = this.now();
|
|
11183
|
+
const lens = {
|
|
11184
|
+
id: this.createId("lens"),
|
|
11185
|
+
projectId: this.projectId,
|
|
11186
|
+
...options,
|
|
11187
|
+
createdAt: now,
|
|
11188
|
+
updatedAt: now
|
|
11189
|
+
};
|
|
11190
|
+
this.lenses.set(lens.identifier, lens);
|
|
11191
|
+
return lens;
|
|
11192
|
+
}
|
|
11193
|
+
/** A page of the project's lenses, newest first unless `sort` says otherwise. */
|
|
11194
|
+
listLenses(query) {
|
|
11195
|
+
return createPage([...this.lenses.values()], this.pageQuery(query), {
|
|
11196
|
+
scope: `lenses:${this.projectId}`,
|
|
11197
|
+
fail: this.pageFailure
|
|
11198
|
+
});
|
|
11199
|
+
}
|
|
11200
|
+
createLens(body) {
|
|
11201
|
+
const definition = body;
|
|
11202
|
+
if (typeof definition.identifier !== "string" || !definition.identifier) throw invalidField("validation/invalid-type", "identifier");
|
|
11203
|
+
if (this.lenses.has(definition.identifier)) throw fail(API_ERROR_CODES.LENS_IDENTIFIER_TAKEN, { details: { identifier: definition.identifier } });
|
|
11204
|
+
return this.saveNewLens(definition);
|
|
11205
|
+
}
|
|
11206
|
+
findLens(lensId) {
|
|
11207
|
+
const lens = [...this.lenses.values()].find((candidate) => candidate.id === lensId);
|
|
11208
|
+
if (!lens) throw fail(API_ERROR_CODES.LENS_NOT_FOUND);
|
|
11209
|
+
return lens;
|
|
11210
|
+
}
|
|
11211
|
+
updateLens(lensId, body) {
|
|
11212
|
+
const lens = this.findLens(lensId);
|
|
11213
|
+
const { identifier: _identifier, ...changes } = body;
|
|
11214
|
+
return this.ensureLens({
|
|
11215
|
+
...lens,
|
|
11216
|
+
...changes
|
|
11217
|
+
});
|
|
11218
|
+
}
|
|
11219
|
+
deleteLens(lensId) {
|
|
11220
|
+
this.lenses.delete(this.findLens(lensId).identifier);
|
|
11221
|
+
return {};
|
|
10330
11222
|
}
|
|
10331
11223
|
/**
|
|
10332
|
-
* Serves
|
|
11224
|
+
* Serves an asset's resources like the CDN (`/assets/:projectId/:asset/:resource`): `200` with
|
|
10333
11225
|
* `Content-Type`, `Content-Length`, `Cache-Control`, `Cache-Tag`, the CDN's protection headers
|
|
10334
|
-
* and, for images, `ETag` and `Expires`; or a JSON error body with `404
|
|
10335
|
-
* `
|
|
11226
|
+
* and, for still images, `ETag` and `Expires`; or a JSON error body with `400`, `403` or `404`.
|
|
11227
|
+
* `original` is the upload; `rendered` the image, PDF, video or audio file `content.rendered`
|
|
11228
|
+
* describes; `preview` the still `content.preview` describes; and `placeholder` its ThumbHash as a
|
|
11229
|
+
* PNG. The mock converts nothing: an upload stands in for its rendered file, an image for its own
|
|
11230
|
+
* preview, and the placeholder PNG for a screenshot, page, poster or waveform.
|
|
10336
11231
|
*/
|
|
10337
11232
|
async serveCdn(request) {
|
|
10338
11233
|
const { url, segments, query } = request;
|
|
10339
|
-
const [root, projectId,
|
|
11234
|
+
const [root, projectId, , resource] = segments;
|
|
11235
|
+
const assetSegment = request.rawSegments[2] ?? "";
|
|
11236
|
+
const selectorPath = request.rawSegments.slice(4);
|
|
10340
11237
|
const notFound = () => this.jsonResponse(404, {
|
|
10341
11238
|
code: "asset/not-found",
|
|
10342
11239
|
message: "Asset not found"
|
|
10343
11240
|
}, url);
|
|
11241
|
+
const noRenderedVersion = () => this.jsonResponse(404, {
|
|
11242
|
+
code: "asset/no-rendered-version",
|
|
11243
|
+
message: "This asset has no such resource"
|
|
11244
|
+
}, url);
|
|
10344
11245
|
const invalidTransform = () => this.jsonResponse(400, {
|
|
10345
|
-
code: "
|
|
11246
|
+
code: "transform/invalid",
|
|
10346
11247
|
message: "Invalid transform selector"
|
|
10347
11248
|
}, url);
|
|
11249
|
+
const isHls = resource === "hls";
|
|
10348
11250
|
let selector;
|
|
10349
11251
|
try {
|
|
10350
|
-
selector = parseTransformSelector(
|
|
11252
|
+
selector = parseTransformSelector(isHls ? "" : selectorPath.join("/"));
|
|
10351
11253
|
} catch {
|
|
10352
11254
|
return invalidTransform();
|
|
10353
11255
|
}
|
|
10354
|
-
|
|
10355
|
-
if (
|
|
10356
|
-
|
|
11256
|
+
const hasSelector = Boolean(selector.lens || selector.options);
|
|
11257
|
+
if ((resource === "original" || resource === "embed" || resource === "info") && hasSelector) return invalidTransform();
|
|
11258
|
+
if (root !== "assets" || projectId !== this.projectId || !assetSegment) return notFound();
|
|
11259
|
+
const assets = await this.databaseStorage.listAssets(this.projectId);
|
|
11260
|
+
let grant;
|
|
11261
|
+
let asset;
|
|
11262
|
+
if (assetSegment.startsWith("sig_")) {
|
|
11263
|
+
grant = decodeMockGrant(assetSegment);
|
|
11264
|
+
if (!grant) return this.jsonResponse(403, {
|
|
11265
|
+
code: "signed-url/invalid",
|
|
11266
|
+
message: "The signed asset URL's grant is invalid"
|
|
11267
|
+
}, url);
|
|
11268
|
+
if (grant.expiresAt <= this.now()) return this.jsonResponse(403, {
|
|
11269
|
+
code: "signed-url/expired",
|
|
11270
|
+
message: "The signed URL has expired"
|
|
11271
|
+
}, url);
|
|
11272
|
+
const grantedId = grant.assetId;
|
|
11273
|
+
asset = assets.find((candidate) => candidate.id === grantedId);
|
|
11274
|
+
} else if (assetSegment.startsWith("ext:")) {
|
|
11275
|
+
let externalId;
|
|
11276
|
+
try {
|
|
11277
|
+
externalId = decodeURIComponent(assetSegment.slice(4));
|
|
11278
|
+
} catch {}
|
|
11279
|
+
asset = externalId ? assets.find((candidate) => candidate.externalId === externalId) : void 0;
|
|
11280
|
+
} else {
|
|
11281
|
+
const assetId = decodeSegment(assetSegment);
|
|
11282
|
+
asset = assets.find((candidate) => candidate.id === assetId);
|
|
11283
|
+
}
|
|
10357
11284
|
if (!asset) return notFound();
|
|
11285
|
+
if (resource === "original" && grant && !grant.original) return this.jsonResponse(403, {
|
|
11286
|
+
code: "signed-url/original-not-granted",
|
|
11287
|
+
message: "This signed asset URL doesn't grant the original file"
|
|
11288
|
+
}, url);
|
|
10358
11289
|
const notAvailable = () => this.jsonResponse(403, {
|
|
10359
11290
|
code: "asset/not-available",
|
|
10360
11291
|
message: "Asset is not available"
|
|
@@ -10364,11 +11295,13 @@ var MockCalmLensApi = class {
|
|
|
10364
11295
|
const canDownload = () => permits(grants, "assets:download", asset.folderId ?? null, folders);
|
|
10365
11296
|
const status = asset.classification.moderation.status;
|
|
10366
11297
|
const isHead = request.method === "HEAD";
|
|
11298
|
+
const { rendered, preview } = asset.content;
|
|
11299
|
+
if (resource === "info") return this.serveInfo(request, asset, Boolean(grant) || canDownload());
|
|
11300
|
+
if (asset.content.safetyHold) return notAvailable();
|
|
10367
11301
|
if (resource === "placeholder") {
|
|
10368
|
-
const thumbhash =
|
|
11302
|
+
const thumbhash = preview?.thumbhash;
|
|
10369
11303
|
if (!thumbhash || status === "purged") return notAvailable();
|
|
10370
|
-
|
|
10371
|
-
return this.cdnResponse(isHead, png, url, {
|
|
11304
|
+
return this.cdnResponse(isHead, mockPlaceholderPng(thumbhash), url, {
|
|
10372
11305
|
"Content-Type": "image/png",
|
|
10373
11306
|
"Cache-Control": "public, max-age=3600",
|
|
10374
11307
|
"Cache-Tag": `asset-${asset.id}`,
|
|
@@ -10376,40 +11309,59 @@ var MockCalmLensApi = class {
|
|
|
10376
11309
|
});
|
|
10377
11310
|
}
|
|
10378
11311
|
const storedContent = await this.imageStorage.getContent(asset.id);
|
|
10379
|
-
if (asset.upload.status === "uploading" || status === "purged" || status === "blocked" || asset.classification.status === "failed" || asset.content.status === "removed" || !storedContent) return notAvailable();
|
|
11312
|
+
if (asset.upload.status === "uploading" || status === "purged" || status === "blocked" || asset.classification.status === "failed" || asset.content.status === "removed" || resource === "original" && !storedContent) return notAvailable();
|
|
10380
11313
|
let claims = {};
|
|
10381
11314
|
try {
|
|
10382
11315
|
claims = JSON.parse(query.get("viewerClaims") ?? request.headers.get("X-CalmLens-Viewer-Claims") ?? "{}");
|
|
10383
11316
|
} catch {
|
|
10384
|
-
return this.jsonResponse(400, { code: "viewer/invalid
|
|
11317
|
+
return this.jsonResponse(400, { code: "viewer-claims/invalid" }, url);
|
|
10385
11318
|
}
|
|
10386
11319
|
if (status === "age-restricted" && claims.ageConfirmed !== true) return this.jsonResponse(403, { code: "viewer/age-confirmation-required" }, url);
|
|
10387
11320
|
const isPrivate = asset.visibility !== "public";
|
|
10388
|
-
if (isPrivate && !canDownload()) return this.jsonResponse(403, {
|
|
10389
|
-
code: "
|
|
11321
|
+
if (isPrivate && !grant && !canDownload()) return this.jsonResponse(403, {
|
|
11322
|
+
code: "auth/permission-denied",
|
|
10390
11323
|
message: "This API key needs assets:download permission for this asset’s folder."
|
|
10391
11324
|
}, url);
|
|
11325
|
+
if (resource === "embed") return this.cdnResponse(isHead, new TextEncoder().encode(MOCK_EMBED_HTML), url, {
|
|
11326
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
11327
|
+
"Cache-Control": Object.keys(claims).length ? "private, no-store" : `${isPrivate ? "private" : "public"}, max-age=3600`,
|
|
11328
|
+
"Cache-Tag": `asset-${asset.id}`,
|
|
11329
|
+
"X-Content-Type-Options": "nosniff"
|
|
11330
|
+
});
|
|
11331
|
+
if (resource === "rendered" && !rendered || resource === "preview" && !preview) return noRenderedVersion();
|
|
11332
|
+
if (isHls && !asset.content.hls) return noRenderedVersion();
|
|
11333
|
+
const stillImage = resource === "preview" || resource === "rendered" && rendered?.kind === "image";
|
|
11334
|
+
if (!stillImage && hasSelector) return invalidTransform();
|
|
10392
11335
|
if (selector.options) {
|
|
10393
11336
|
const { unknownKeys, invalidKeys } = checkTransformOptions(selector.options);
|
|
10394
11337
|
if (unknownKeys.length || invalidKeys.length) return this.jsonResponse(400, {
|
|
10395
|
-
code: "
|
|
11338
|
+
code: "transform-options/invalid",
|
|
10396
11339
|
message: `Invalid transform options: ${[...unknownKeys, ...invalidKeys].join(", ")}`
|
|
10397
11340
|
}, url);
|
|
10398
11341
|
}
|
|
10399
|
-
const
|
|
10400
|
-
|
|
10401
|
-
let
|
|
10402
|
-
|
|
10403
|
-
|
|
10404
|
-
|
|
11342
|
+
const servesUpload = resource === "original" || resource === "rendered" && rendered?.kind !== "image" || asset.type === "image";
|
|
11343
|
+
let bytes;
|
|
11344
|
+
let contentType;
|
|
11345
|
+
if (servesUpload) {
|
|
11346
|
+
if (!storedContent) return notAvailable();
|
|
11347
|
+
const parts = [...storedContent.parts.entries()].sort(([left], [right]) => left - right).map(([, value]) => value);
|
|
11348
|
+
bytes = new Uint8Array(parts.reduce((sum, value) => sum + value.byteLength, 0));
|
|
11349
|
+
let offset = 0;
|
|
11350
|
+
for (const part of parts) {
|
|
11351
|
+
bytes.set(part, offset);
|
|
11352
|
+
offset += part.byteLength;
|
|
11353
|
+
}
|
|
11354
|
+
contentType = resource === "rendered" && rendered ? rendered.format : storedContent.type ?? "application/octet-stream";
|
|
11355
|
+
} else {
|
|
11356
|
+
bytes = mockPlaceholderPng(preview?.thumbhash ?? MOCK_THUMBHASH);
|
|
11357
|
+
contentType = "image/png";
|
|
10405
11358
|
}
|
|
10406
|
-
const
|
|
10407
|
-
const isImage = asset.type === "image";
|
|
11359
|
+
const immutable = stillImage || resource === "original" && asset.type === "image";
|
|
10408
11360
|
return this.cdnResponse(isHead, bytes, url, {
|
|
10409
11361
|
"Content-Type": contentType,
|
|
10410
|
-
"Cache-Control": Object.keys(claims).length ? "private, no-store" : `${isPrivate ? "private" : "public"}, ${
|
|
11362
|
+
"Cache-Control": Object.keys(claims).length ? "private, no-store" : `${isPrivate ? "private" : "public"}, ${immutable ? "max-age=31536000, immutable" : "max-age=3600"}`,
|
|
10411
11363
|
"Cache-Tag": `asset-${asset.id}`,
|
|
10412
|
-
...
|
|
11364
|
+
...immutable ? {
|
|
10413
11365
|
ETag: `"${asset.id}"`,
|
|
10414
11366
|
Expires: new Date(this.now() + 31536e6).toUTCString()
|
|
10415
11367
|
} : {},
|
|
@@ -10418,6 +11370,51 @@ var MockCalmLensApi = class {
|
|
|
10418
11370
|
...isActiveContentType(contentType) ? { "Content-Disposition": "attachment" } : {}
|
|
10419
11371
|
});
|
|
10420
11372
|
}
|
|
11373
|
+
/**
|
|
11374
|
+
* The display info at `/info`, like the CDN's (AssetInfo.ts): the allowlisted fields and whether
|
|
11375
|
+
* the viewer the claims describe can see the content, decided as the other resources decide it.
|
|
11376
|
+
* A private asset's needs a grant or an API key with `assets:download`.
|
|
11377
|
+
*/
|
|
11378
|
+
serveInfo(request, asset, authorised) {
|
|
11379
|
+
const { url, query } = request;
|
|
11380
|
+
const isPrivate = asset.visibility !== "public";
|
|
11381
|
+
if (isPrivate && !authorised) return this.jsonResponse(404, {
|
|
11382
|
+
code: "asset/not-available",
|
|
11383
|
+
message: "Asset is not available"
|
|
11384
|
+
}, url);
|
|
11385
|
+
let claims = {};
|
|
11386
|
+
try {
|
|
11387
|
+
claims = JSON.parse(query.get("viewerClaims") ?? request.headers.get("X-CalmLens-Viewer-Claims") ?? "{}");
|
|
11388
|
+
} catch {
|
|
11389
|
+
return this.jsonResponse(400, { code: "viewer-claims/invalid" }, url);
|
|
11390
|
+
}
|
|
11391
|
+
const status = asset.classification.moderation.status;
|
|
11392
|
+
const availability = asset.content.safetyHold || asset.upload.status === "uploading" || status === "blocked" || status === "purged" || asset.classification.status === "failed" || asset.content.status === "removed" ? "unavailable" : status === "age-restricted" && claims.ageConfirmed !== true ? "age-restricted" : "available";
|
|
11393
|
+
const { content } = asset;
|
|
11394
|
+
const info = {
|
|
11395
|
+
id: asset.id,
|
|
11396
|
+
projectId: asset.projectId,
|
|
11397
|
+
type: asset.type,
|
|
11398
|
+
externalUrl: asset.externalUrl ?? null,
|
|
11399
|
+
content: {
|
|
11400
|
+
format: content.format,
|
|
11401
|
+
width: content.width,
|
|
11402
|
+
height: content.height,
|
|
11403
|
+
durationSeconds: content.durationSeconds,
|
|
11404
|
+
pageCount: content.pageCount,
|
|
11405
|
+
rendered: content.rendered,
|
|
11406
|
+
preview: content.preview,
|
|
11407
|
+
hls: content.hls
|
|
11408
|
+
},
|
|
11409
|
+
availability
|
|
11410
|
+
};
|
|
11411
|
+
const bytes = new TextEncoder().encode(JSON.stringify(info));
|
|
11412
|
+
return this.cdnResponse(request.method === "HEAD", bytes, url, {
|
|
11413
|
+
"Content-Type": "application/json",
|
|
11414
|
+
"Cache-Control": `${isPrivate ? "private" : "public"}, max-age=60`,
|
|
11415
|
+
"Cache-Tag": `asset-${asset.id}`
|
|
11416
|
+
});
|
|
11417
|
+
}
|
|
10421
11418
|
cdnResponse(isHead, bytes, url, headers) {
|
|
10422
11419
|
return this.response(200, isHead ? null : bytes, url, {
|
|
10423
11420
|
...headers,
|
|
@@ -10545,6 +11542,7 @@ const mockClientOptions = (api, endpoints, session) => {
|
|
|
10545
11542
|
*/
|
|
10546
11543
|
var MockCalmLensServerClient = class extends CalmLensServerClient {
|
|
10547
11544
|
#api;
|
|
11545
|
+
#fetch;
|
|
10548
11546
|
#projectId;
|
|
10549
11547
|
/**
|
|
10550
11548
|
* Accepts the same options as `CalmLensServerClient` (validated the same way), plus
|
|
@@ -10566,8 +11564,7 @@ var MockCalmLensServerClient = class extends CalmLensServerClient {
|
|
|
10566
11564
|
imageStorage: mockOptions.imageStorage ?? new InMemoryMockImageStorage(),
|
|
10567
11565
|
createId: mockOptions.createId ?? newId,
|
|
10568
11566
|
now: mockOptions.now ?? createMonotonicClock(),
|
|
10569
|
-
moderate: mockOptions.moderate ?? defaultModerate
|
|
10570
|
-
signedUrlBaseUrl: mockOptions.signedUrlBaseUrl ?? "mock://calmlens"
|
|
11567
|
+
moderate: mockOptions.moderate ?? defaultModerate
|
|
10571
11568
|
});
|
|
10572
11569
|
let endpoints;
|
|
10573
11570
|
super({
|
|
@@ -10582,6 +11579,7 @@ var MockCalmLensServerClient = class extends CalmLensServerClient {
|
|
|
10582
11579
|
cdnBaseUrl
|
|
10583
11580
|
};
|
|
10584
11581
|
this.#api = api;
|
|
11582
|
+
this.#fetch = api.createFetch(() => endpoints);
|
|
10585
11583
|
this.#projectId = options.projectId;
|
|
10586
11584
|
this.databaseStorage = api.databaseStorage;
|
|
10587
11585
|
this.imageStorage = api.imageStorage;
|
|
@@ -10606,6 +11604,14 @@ var MockCalmLensServerClient = class extends CalmLensServerClient {
|
|
|
10606
11604
|
callsTo(operation) {
|
|
10607
11605
|
return this.#api.calls.filter(({ operations }) => operations.includes(operation)).map(({ call }) => call);
|
|
10608
11606
|
}
|
|
11607
|
+
/**
|
|
11608
|
+
* Fetches a URL of the mock's CDN as a browser would, without the API key: a signed asset URL
|
|
11609
|
+
* from `cdn.signAssetUrl` (with a resource appended), or a public one from `cdn.getAssetUrl`.
|
|
11610
|
+
* The request is recorded like any other.
|
|
11611
|
+
*/
|
|
11612
|
+
fetchCdn(url, init) {
|
|
11613
|
+
return this.#fetch(url, init);
|
|
11614
|
+
}
|
|
10609
11615
|
/** Forgets the requests recorded so far, so `calls` lists only later ones. */
|
|
10610
11616
|
clearCalls() {
|
|
10611
11617
|
this.#api.calls.length = 0;
|
|
@@ -10638,7 +11644,7 @@ var MockCalmLensServerClient = class extends CalmLensServerClient {
|
|
|
10638
11644
|
* Applies a reviewer's decision to a stored asset, as the portal or API would: the moderation
|
|
10639
11645
|
* status changes (`purged` also removes the content, with reason `requested` and an
|
|
10640
11646
|
* `asset.content-removed` event), the review joins `manualReviews`, and an `asset.<decision>`
|
|
10641
|
-
* event
|
|
11647
|
+
* event whose `actor` is a `user` joins the events feed. Rejects with
|
|
10642
11648
|
* `asset/not-found` for an unknown asset. It sends no request.
|
|
10643
11649
|
*/
|
|
10644
11650
|
decide(assetId, decision) {
|
|
@@ -10649,8 +11655,11 @@ var MockCalmLensServerClient = class extends CalmLensServerClient {
|
|
|
10649
11655
|
* else what `mockOptions.moderate` returns for the `complete` trigger (`allowed` by default).
|
|
10650
11656
|
* The asset gets the decision's report, and `asset.classified` and the automated
|
|
10651
11657
|
* `asset.<status>` (or `asset.failed`) join the events feed. No review is recorded, so the
|
|
10652
|
-
* asset can still be moderated again.
|
|
10653
|
-
* `
|
|
11658
|
+
* asset can still be moderated again. A decided asset whose re-run (`assets.explain` with
|
|
11659
|
+
* `rerun: true`) is still pending gets the decision in place of its old one, unless it is
|
|
11660
|
+
* `pending` or `failed`. Rejects with `asset/not-found` for an unknown asset and
|
|
11661
|
+
* `sdk/invalid-argument` for one that isn't processing or awaiting a pending re-run. It sends
|
|
11662
|
+
* no request.
|
|
10654
11663
|
*/
|
|
10655
11664
|
completeProcessing(assetId, decision) {
|
|
10656
11665
|
return this.#api.completeProcessing(assetId, decision);
|
|
@@ -10711,11 +11720,13 @@ var MockCalmLensAppClient = class extends CalmLensAppClient {
|
|
|
10711
11720
|
const toWebhookEventPayload = (event) => ({
|
|
10712
11721
|
id: event.id,
|
|
10713
11722
|
projectId: event.projectId,
|
|
11723
|
+
actor: event.actor,
|
|
10714
11724
|
type: event.type,
|
|
10715
11725
|
severity: event.severity,
|
|
10716
11726
|
title: event.title,
|
|
10717
11727
|
description: event.description ?? null,
|
|
10718
11728
|
assetId: event.assetId ?? null,
|
|
11729
|
+
assetSequence: event.assetSequence ?? null,
|
|
10719
11730
|
asset: event.asset ?? null,
|
|
10720
11731
|
facts: event.facts,
|
|
10721
11732
|
createdAt: event.createdAt
|