calmlens 0.2.1 → 0.3.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/cjs/testing.d.ts CHANGED
@@ -1,18 +1,28 @@
1
1
  import { A as AssetBinaryPayloadInput, D as GetEventsResponse, E as GetAssetsPageResponse, M as Folder, N as Asset, O as PostAssetSignedUrlResponse, P as AssetStatus, S as UploadAssetPartOptions, T as GetAssetResponse, a as CreateFolderOptions, c as EnsureLensesOptions, d as ListAssetsOptions, f as ListEventsOptions, h as MoveFolderOptions, i as CreateAssetSignedUrlOptions, j as Lens, l as FolderIdOptions, m as MoveAssetOptions, n as CdnAssetUrlOptions, p as ModerateTextOptions, r as CdnServeOptions, s as EnsureLensOptions, t as CalmLensClientOptions, u as ListAssetChildrenOptions, v as RetrieveAssetOptions, w as GetAssetChildrenResponse, x as UpdateFolderOptions, y as SubmitAssetOptions } from "./CalmLensTypes-Bnq4u8HJ.js";
2
2
  import * as zod from "zod";
3
- //#region ../shared/src/Roles.d.ts
4
- declare const ROLE_SCHEMA: zod.ZodEnum<{
5
- "assets:view-private": "assets:view-private";
6
- "system:assets:view-all": "system:assets:view-all";
7
- "system:banned": "system:banned";
8
- "system:super-admin": "system:super-admin";
9
- "system:uploads:unlimited": "system:uploads:unlimited";
10
- }>;
11
- type Role = zod.infer<typeof ROLE_SCHEMA>;
3
+ //#region ../shared/src/Permissions.d.ts
4
+ declare const GRANT_SCHEMA: zod.ZodObject<{
5
+ actions: zod.ZodArray<zod.ZodEnum<{
6
+ "assets:create": "assets:create";
7
+ "assets:delete": "assets:delete";
8
+ "assets:download": "assets:download";
9
+ "assets:read": "assets:read";
10
+ "assets:share": "assets:share";
11
+ "assets:update": "assets:update";
12
+ "folders:create": "folders:create";
13
+ "folders:delete": "folders:delete";
14
+ "folders:read": "folders:read";
15
+ "folders:update": "folders:update";
16
+ "project:manage": "project:manage";
17
+ }>>;
18
+ folderId: zod.ZodNullable<zod.ZodString>;
19
+ includeSubfolders: zod.ZodBoolean;
20
+ }, zod.core.$strict>;
21
+ type PermissionGrant = zod.infer<typeof GRANT_SCHEMA>;
12
22
  //#endregion
13
23
  //#region src/testing/MockApiKey.d.ts
14
24
  export declare const MOCK_API_KEY_PREFIX = "sk_test_";
15
- export declare const createMockApiKey: (roles?: Role[]) => string;
25
+ export declare const createMockApiKey: (grants?: PermissionGrant[]) => string;
16
26
  //#endregion
17
27
  //#region src/testing/MockStorage.d.ts
18
28
  interface MockStoredContent {
@@ -93,7 +103,7 @@ export declare class MockCalmLensClient {
93
103
  private readonly now;
94
104
  private readonly queuedStatus;
95
105
  private readonly analyzedStatus;
96
- private readonly apiKeyRoles;
106
+ private readonly apiKeyGrants;
97
107
  private readonly signedUrlBaseUrl;
98
108
  private readonly cdnBaseUrl;
99
109
  private readonly lenses;
package/cjs/testing.js CHANGED
@@ -24,14 +24,46 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  const require_CalmLensUtils = require("./CalmLensUtils-DioFUAmI.js");
25
25
  let zod = require("zod");
26
26
  zod = __toESM(zod);
27
- //#region ../shared/src/Roles.ts
28
- const ROLE_SCHEMA = zod.enum([
29
- "system:super-admin",
30
- "system:banned",
31
- "system:assets:view-all",
32
- "assets:view-private",
33
- "system:uploads:unlimited"
27
+ //#region ../shared/src/Permissions.ts
28
+ const ACTION_SCHEMA = zod.enum([
29
+ "assets:read",
30
+ "assets:download",
31
+ "assets:create",
32
+ "assets:update",
33
+ "assets:delete",
34
+ "assets:share",
35
+ "folders:read",
36
+ "folders:create",
37
+ "folders:update",
38
+ "folders:delete",
39
+ "project:manage"
34
40
  ]);
41
+ const GRANT_SCHEMA = zod.object({
42
+ actions: zod.array(ACTION_SCHEMA),
43
+ folderId: zod.string().uuid().nullable(),
44
+ includeSubfolders: zod.boolean()
45
+ }).strict().refine((g) => g.folderId === null || !g.actions.includes("project:manage"), "Project management requires project scope");
46
+ const GRANTS_SCHEMA = zod.array(GRANT_SCHEMA).max(100);
47
+ zod.enum([
48
+ "admin",
49
+ "member",
50
+ "viewer",
51
+ "custom"
52
+ ]);
53
+ const permits = (grants, action, folderId, folders) => grants.some((grant) => {
54
+ if (!grant.actions.includes(action)) return false;
55
+ if (grant.folderId === null) return true;
56
+ let current = folderId;
57
+ const visited = /* @__PURE__ */ new Set();
58
+ while (current && !visited.has(current)) {
59
+ var _folders$find$parentI, _folders$find;
60
+ if (current === grant.folderId) return true;
61
+ if (!grant.includeSubfolders) return false;
62
+ visited.add(current);
63
+ current = (_folders$find$parentI = (_folders$find = folders.find((folder) => folder.id === current)) === null || _folders$find === void 0 ? void 0 : _folders$find.parentId) !== null && _folders$find$parentI !== void 0 ? _folders$find$parentI : null;
64
+ }
65
+ return false;
66
+ });
35
67
  //#endregion
36
68
  //#region src/testing/MockApiKey.ts
37
69
  const MOCK_API_KEY_PREFIX = "sk_test_";
@@ -40,16 +72,11 @@ const fromBase64Url = (value) => {
40
72
  const padded = value.padEnd(value.length + (4 - value.length % 4) % 4, "=");
41
73
  return atob(padded.replaceAll("-", "+").replaceAll("_", "/"));
42
74
  };
43
- const isRole = (value) => typeof value === "string" && ROLE_SCHEMA.options.includes(value);
44
- const createMockApiKey = (roles = []) => {
45
- const encodedRoles = toBase64Url(JSON.stringify([...new Set(roles)].sort()));
46
- return `${MOCK_API_KEY_PREFIX}${encodedRoles}`;
47
- };
48
- const getMockApiKeyRoles = (apiKey) => {
75
+ const createMockApiKey = (grants = []) => `${MOCK_API_KEY_PREFIX}${toBase64Url(JSON.stringify(GRANTS_SCHEMA.parse(grants)))}`;
76
+ const getMockApiKeyGrants = (apiKey) => {
49
77
  if (!apiKey.startsWith("sk_test_")) return [];
50
78
  try {
51
- const roles = JSON.parse(fromBase64Url(apiKey.slice(8)));
52
- return Array.isArray(roles) ? roles.filter(isRole) : [];
79
+ return GRANTS_SCHEMA.parse(JSON.parse(fromBase64Url(apiKey.slice(8))));
53
80
  } catch (_unused) {
54
81
  return [];
55
82
  }
@@ -245,7 +272,7 @@ var MockCalmLensClient = class {
245
272
  this.now = (_options$mockOptions$4 = (_options$mockOptions4 = options.mockOptions) === null || _options$mockOptions4 === void 0 ? void 0 : _options$mockOptions4.now) !== null && _options$mockOptions$4 !== void 0 ? _options$mockOptions$4 : Date.now;
246
273
  this.queuedStatus = (_options$mockOptions$5 = (_options$mockOptions5 = options.mockOptions) === null || _options$mockOptions5 === void 0 ? void 0 : _options$mockOptions5.queuedStatus) !== null && _options$mockOptions$5 !== void 0 ? _options$mockOptions$5 : "pending";
247
274
  this.analyzedStatus = (_options$mockOptions$6 = (_options$mockOptions6 = options.mockOptions) === null || _options$mockOptions6 === void 0 ? void 0 : _options$mockOptions6.analyzedStatus) !== null && _options$mockOptions$6 !== void 0 ? _options$mockOptions$6 : "approved";
248
- this.apiKeyRoles = getMockApiKeyRoles(options.apiKey);
275
+ this.apiKeyGrants = getMockApiKeyGrants(options.apiKey);
249
276
  this.signedUrlBaseUrl = (_options$mockOptions$7 = (_options$mockOptions7 = options.mockOptions) === null || _options$mockOptions7 === void 0 ? void 0 : _options$mockOptions7.signedUrlBaseUrl) !== null && _options$mockOptions$7 !== void 0 ? _options$mockOptions$7 : "mock://calmlens";
250
277
  this.cdnBaseUrl = (_options$mockOptions$8 = (_options$mockOptions8 = options.mockOptions) === null || _options$mockOptions8 === void 0 ? void 0 : _options$mockOptions8.cdnBaseUrl) !== null && _options$mockOptions$8 !== void 0 ? _options$mockOptions$8 : "mock://calmlens/v1";
251
278
  }
@@ -262,14 +289,14 @@ var MockCalmLensClient = class {
262
289
  },
263
290
  serve: function() {
264
291
  var _ref3 = require_CalmLensUtils._asyncToGenerator(function* (_ref2) {
265
- var _storedContent$type;
292
+ var _asset$folderId, _await$this$databaseS, _this$databaseStorage, _this$databaseStorage2, _storedContent$type;
266
293
  let { assetIdentifier } = _ref2, options = require_CalmLensUtils._objectWithoutProperties(_ref2, _excluded2);
267
294
  if (!assetIdentifier) throw new Error("Asset identifier is required");
268
295
  const asset = (yield _this.databaseStorage.listAssets(_this.options.projectId)).find((asset) => asset.id === assetIdentifier || asset.externalId === assetIdentifier);
269
296
  if (!asset) return new Response("Asset not found", { status: 404 });
270
- if (asset.visibility !== "public" && !_this.apiKeyRoles.includes("assets:view-private") && !_this.apiKeyRoles.includes("system:assets:view-all") && !_this.apiKeyRoles.includes("system:super-admin")) return new Response(JSON.stringify({
271
- code: "asset/api-key-missing-role",
272
- message: "This API key needs assets:view-private to read private assets. Use a signed asset URL or enable the permission."
297
+ if (asset.visibility !== "public" && !permits(_this.apiKeyGrants, "assets:download", (_asset$folderId = asset.folderId) !== null && _asset$folderId !== void 0 ? _asset$folderId : null, (_await$this$databaseS = yield (_this$databaseStorage = (_this$databaseStorage2 = _this.databaseStorage).listFolders) === null || _this$databaseStorage === void 0 ? void 0 : _this$databaseStorage.call(_this$databaseStorage2, _this.options.projectId)) !== null && _await$this$databaseS !== void 0 ? _await$this$databaseS : [])) return new Response(JSON.stringify({
298
+ code: "asset/api-key-missing-permission",
299
+ message: "This API key needs assets:download permission for this asset’s folder."
273
300
  }), {
274
301
  status: 403,
275
302
  headers: { "Content-Type": "application/json" }
package/esm/testing.d.ts CHANGED
@@ -1,18 +1,28 @@
1
1
  import { A as AssetBinaryPayloadInput, D as GetEventsResponse, E as GetAssetsPageResponse, M as Folder, N as Asset, O as PostAssetSignedUrlResponse, P as AssetStatus, S as UploadAssetPartOptions, T as GetAssetResponse, a as CreateFolderOptions, c as EnsureLensesOptions, d as ListAssetsOptions, f as ListEventsOptions, h as MoveFolderOptions, i as CreateAssetSignedUrlOptions, j as Lens, l as FolderIdOptions, m as MoveAssetOptions, n as CdnAssetUrlOptions, p as ModerateTextOptions, r as CdnServeOptions, s as EnsureLensOptions, t as CalmLensClientOptions, u as ListAssetChildrenOptions, v as RetrieveAssetOptions, w as GetAssetChildrenResponse, x as UpdateFolderOptions, y as SubmitAssetOptions } from "./CalmLensTypes-Bnq4u8HJ.js";
2
2
  import * as zod from "zod";
3
- //#region ../shared/src/Roles.d.ts
4
- declare const ROLE_SCHEMA: zod.ZodEnum<{
5
- "assets:view-private": "assets:view-private";
6
- "system:assets:view-all": "system:assets:view-all";
7
- "system:banned": "system:banned";
8
- "system:super-admin": "system:super-admin";
9
- "system:uploads:unlimited": "system:uploads:unlimited";
10
- }>;
11
- type Role = zod.infer<typeof ROLE_SCHEMA>;
3
+ //#region ../shared/src/Permissions.d.ts
4
+ declare const GRANT_SCHEMA: zod.ZodObject<{
5
+ actions: zod.ZodArray<zod.ZodEnum<{
6
+ "assets:create": "assets:create";
7
+ "assets:delete": "assets:delete";
8
+ "assets:download": "assets:download";
9
+ "assets:read": "assets:read";
10
+ "assets:share": "assets:share";
11
+ "assets:update": "assets:update";
12
+ "folders:create": "folders:create";
13
+ "folders:delete": "folders:delete";
14
+ "folders:read": "folders:read";
15
+ "folders:update": "folders:update";
16
+ "project:manage": "project:manage";
17
+ }>>;
18
+ folderId: zod.ZodNullable<zod.ZodString>;
19
+ includeSubfolders: zod.ZodBoolean;
20
+ }, zod.core.$strict>;
21
+ type PermissionGrant = zod.infer<typeof GRANT_SCHEMA>;
12
22
  //#endregion
13
23
  //#region src/testing/MockApiKey.d.ts
14
24
  export declare const MOCK_API_KEY_PREFIX = "sk_test_";
15
- export declare const createMockApiKey: (roles?: Role[]) => string;
25
+ export declare const createMockApiKey: (grants?: PermissionGrant[]) => string;
16
26
  //#endregion
17
27
  //#region src/testing/MockStorage.d.ts
18
28
  interface MockStoredContent {
@@ -93,7 +103,7 @@ export declare class MockCalmLensClient {
93
103
  private readonly now;
94
104
  private readonly queuedStatus;
95
105
  private readonly analyzedStatus;
96
- private readonly apiKeyRoles;
106
+ private readonly apiKeyGrants;
97
107
  private readonly signedUrlBaseUrl;
98
108
  private readonly cdnBaseUrl;
99
109
  private readonly lenses;
package/esm/testing.js CHANGED
@@ -1,13 +1,45 @@
1
1
  import { _ as getAssetPayloadByteLength, b as _asyncToGenerator, d as getRejectedUploadMimeTypeError, f as isRejectedUploadMimeType, g as assetToData, h as assetDataToUint8Array, i as _objectWithoutProperties, m as stringifyJsonCanonical, p as PUBLIC_ERROR_CODES, r as _objectSpread2, s as calmLensError, t as toTextAssetOptions, y as validateAssetPayload } from "./CalmLensUtils-Dixed1Ox.js";
2
2
  import * as zod from "zod";
3
- //#region ../shared/src/Roles.ts
4
- const ROLE_SCHEMA = zod.enum([
5
- "system:super-admin",
6
- "system:banned",
7
- "system:assets:view-all",
8
- "assets:view-private",
9
- "system:uploads:unlimited"
3
+ //#region ../shared/src/Permissions.ts
4
+ const ACTION_SCHEMA = zod.enum([
5
+ "assets:read",
6
+ "assets:download",
7
+ "assets:create",
8
+ "assets:update",
9
+ "assets:delete",
10
+ "assets:share",
11
+ "folders:read",
12
+ "folders:create",
13
+ "folders:update",
14
+ "folders:delete",
15
+ "project:manage"
10
16
  ]);
17
+ const GRANT_SCHEMA = zod.object({
18
+ actions: zod.array(ACTION_SCHEMA),
19
+ folderId: zod.string().uuid().nullable(),
20
+ includeSubfolders: zod.boolean()
21
+ }).strict().refine((g) => g.folderId === null || !g.actions.includes("project:manage"), "Project management requires project scope");
22
+ const GRANTS_SCHEMA = zod.array(GRANT_SCHEMA).max(100);
23
+ zod.enum([
24
+ "admin",
25
+ "member",
26
+ "viewer",
27
+ "custom"
28
+ ]);
29
+ const permits = (grants, action, folderId, folders) => grants.some((grant) => {
30
+ if (!grant.actions.includes(action)) return false;
31
+ if (grant.folderId === null) return true;
32
+ let current = folderId;
33
+ const visited = /* @__PURE__ */ new Set();
34
+ while (current && !visited.has(current)) {
35
+ var _folders$find$parentI, _folders$find;
36
+ if (current === grant.folderId) return true;
37
+ if (!grant.includeSubfolders) return false;
38
+ visited.add(current);
39
+ current = (_folders$find$parentI = (_folders$find = folders.find((folder) => folder.id === current)) === null || _folders$find === void 0 ? void 0 : _folders$find.parentId) !== null && _folders$find$parentI !== void 0 ? _folders$find$parentI : null;
40
+ }
41
+ return false;
42
+ });
11
43
  //#endregion
12
44
  //#region src/testing/MockApiKey.ts
13
45
  const MOCK_API_KEY_PREFIX = "sk_test_";
@@ -16,16 +48,11 @@ const fromBase64Url = (value) => {
16
48
  const padded = value.padEnd(value.length + (4 - value.length % 4) % 4, "=");
17
49
  return atob(padded.replaceAll("-", "+").replaceAll("_", "/"));
18
50
  };
19
- const isRole = (value) => typeof value === "string" && ROLE_SCHEMA.options.includes(value);
20
- const createMockApiKey = (roles = []) => {
21
- const encodedRoles = toBase64Url(JSON.stringify([...new Set(roles)].sort()));
22
- return `${MOCK_API_KEY_PREFIX}${encodedRoles}`;
23
- };
24
- const getMockApiKeyRoles = (apiKey) => {
51
+ const createMockApiKey = (grants = []) => `${MOCK_API_KEY_PREFIX}${toBase64Url(JSON.stringify(GRANTS_SCHEMA.parse(grants)))}`;
52
+ const getMockApiKeyGrants = (apiKey) => {
25
53
  if (!apiKey.startsWith("sk_test_")) return [];
26
54
  try {
27
- const roles = JSON.parse(fromBase64Url(apiKey.slice(8)));
28
- return Array.isArray(roles) ? roles.filter(isRole) : [];
55
+ return GRANTS_SCHEMA.parse(JSON.parse(fromBase64Url(apiKey.slice(8))));
29
56
  } catch (_unused) {
30
57
  return [];
31
58
  }
@@ -221,7 +248,7 @@ var MockCalmLensClient = class {
221
248
  this.now = (_options$mockOptions$4 = (_options$mockOptions4 = options.mockOptions) === null || _options$mockOptions4 === void 0 ? void 0 : _options$mockOptions4.now) !== null && _options$mockOptions$4 !== void 0 ? _options$mockOptions$4 : Date.now;
222
249
  this.queuedStatus = (_options$mockOptions$5 = (_options$mockOptions5 = options.mockOptions) === null || _options$mockOptions5 === void 0 ? void 0 : _options$mockOptions5.queuedStatus) !== null && _options$mockOptions$5 !== void 0 ? _options$mockOptions$5 : "pending";
223
250
  this.analyzedStatus = (_options$mockOptions$6 = (_options$mockOptions6 = options.mockOptions) === null || _options$mockOptions6 === void 0 ? void 0 : _options$mockOptions6.analyzedStatus) !== null && _options$mockOptions$6 !== void 0 ? _options$mockOptions$6 : "approved";
224
- this.apiKeyRoles = getMockApiKeyRoles(options.apiKey);
251
+ this.apiKeyGrants = getMockApiKeyGrants(options.apiKey);
225
252
  this.signedUrlBaseUrl = (_options$mockOptions$7 = (_options$mockOptions7 = options.mockOptions) === null || _options$mockOptions7 === void 0 ? void 0 : _options$mockOptions7.signedUrlBaseUrl) !== null && _options$mockOptions$7 !== void 0 ? _options$mockOptions$7 : "mock://calmlens";
226
253
  this.cdnBaseUrl = (_options$mockOptions$8 = (_options$mockOptions8 = options.mockOptions) === null || _options$mockOptions8 === void 0 ? void 0 : _options$mockOptions8.cdnBaseUrl) !== null && _options$mockOptions$8 !== void 0 ? _options$mockOptions$8 : "mock://calmlens/v1";
227
254
  }
@@ -238,14 +265,14 @@ var MockCalmLensClient = class {
238
265
  },
239
266
  serve: function() {
240
267
  var _ref3 = _asyncToGenerator(function* (_ref2) {
241
- var _storedContent$type;
268
+ var _asset$folderId, _await$this$databaseS, _this$databaseStorage, _this$databaseStorage2, _storedContent$type;
242
269
  let { assetIdentifier } = _ref2, options = _objectWithoutProperties(_ref2, _excluded2);
243
270
  if (!assetIdentifier) throw new Error("Asset identifier is required");
244
271
  const asset = (yield _this.databaseStorage.listAssets(_this.options.projectId)).find((asset) => asset.id === assetIdentifier || asset.externalId === assetIdentifier);
245
272
  if (!asset) return new Response("Asset not found", { status: 404 });
246
- if (asset.visibility !== "public" && !_this.apiKeyRoles.includes("assets:view-private") && !_this.apiKeyRoles.includes("system:assets:view-all") && !_this.apiKeyRoles.includes("system:super-admin")) return new Response(JSON.stringify({
247
- code: "asset/api-key-missing-role",
248
- message: "This API key needs assets:view-private to read private assets. Use a signed asset URL or enable the permission."
273
+ if (asset.visibility !== "public" && !permits(_this.apiKeyGrants, "assets:download", (_asset$folderId = asset.folderId) !== null && _asset$folderId !== void 0 ? _asset$folderId : null, (_await$this$databaseS = yield (_this$databaseStorage = (_this$databaseStorage2 = _this.databaseStorage).listFolders) === null || _this$databaseStorage === void 0 ? void 0 : _this$databaseStorage.call(_this$databaseStorage2, _this.options.projectId)) !== null && _await$this$databaseS !== void 0 ? _await$this$databaseS : [])) return new Response(JSON.stringify({
274
+ code: "asset/api-key-missing-permission",
275
+ message: "This API key needs assets:download permission for this asset’s folder."
249
276
  }), {
250
277
  status: 403,
251
278
  headers: { "Content-Type": "application/json" }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "calmlens",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/Censkh/CalmLens.git",