@geonosis/policy 2.12.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/conformance.cjs +169 -0
  3. package/dist/conformance.d.cts +35 -0
  4. package/dist/conformance.d.ts +35 -0
  5. package/dist/conformance.js +129 -0
  6. package/dist/define.cjs +118 -0
  7. package/dist/define.d.cts +34 -0
  8. package/dist/define.d.ts +34 -0
  9. package/dist/define.js +85 -0
  10. package/dist/drizzle/index.cjs +37 -32
  11. package/dist/drizzle/index.d.cts +20 -23
  12. package/dist/drizzle/index.d.ts +20 -23
  13. package/dist/drizzle/index.js +2 -5
  14. package/dist/index.cjs +79 -856
  15. package/dist/index.d.cts +19 -428
  16. package/dist/index.d.ts +19 -428
  17. package/dist/index.js +35 -797
  18. package/dist/journal-events.cjs +68 -0
  19. package/dist/journal-events.d.cts +26 -0
  20. package/dist/journal-events.d.ts +26 -0
  21. package/dist/journal-events.js +28 -0
  22. package/dist/lattice.cjs +150 -0
  23. package/dist/lattice.d.cts +48 -0
  24. package/dist/lattice.d.ts +48 -0
  25. package/dist/lattice.js +110 -0
  26. package/dist/learning.cjs +246 -0
  27. package/dist/learning.d.cts +121 -0
  28. package/dist/learning.d.ts +121 -0
  29. package/dist/learning.js +206 -0
  30. package/dist/markdown.cjs +118 -0
  31. package/dist/markdown.d.cts +35 -0
  32. package/dist/markdown.d.ts +35 -0
  33. package/dist/markdown.js +78 -0
  34. package/dist/medusa/index.cjs +39 -36
  35. package/dist/medusa/index.d.cts +14 -16
  36. package/dist/medusa/index.d.ts +14 -16
  37. package/dist/medusa/index.js +4 -9
  38. package/dist/permissions/index.cjs +49 -40
  39. package/dist/permissions/index.d.cts +18 -20
  40. package/dist/permissions/index.d.ts +18 -20
  41. package/dist/permissions/index.js +15 -25
  42. package/dist/read.cjs +111 -0
  43. package/dist/read.d.cts +51 -0
  44. package/dist/read.d.ts +51 -0
  45. package/dist/read.js +71 -0
  46. package/dist/rows.cjs +54 -0
  47. package/dist/rows.d.cts +11 -0
  48. package/dist/rows.d.ts +11 -0
  49. package/dist/{chunk-JICJ4XEX.js → rows.js} +0 -3
  50. package/dist/seed.cjs +53 -0
  51. package/dist/seed.d.cts +18 -0
  52. package/dist/seed.d.ts +18 -0
  53. package/dist/seed.js +13 -0
  54. package/dist/threshold.cjs +73 -0
  55. package/dist/threshold.d.cts +45 -0
  56. package/dist/threshold.d.ts +45 -0
  57. package/dist/threshold.js +33 -0
  58. package/dist/types.cjs +47 -0
  59. package/dist/{types-C-Q3r-In.d.cts → types.d.cts} +21 -23
  60. package/dist/{types-C-Q3r-In.d.ts → types.d.ts} +21 -23
  61. package/dist/types.js +7 -0
  62. package/package.json +3 -3
@@ -1,25 +1,44 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
2
  var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
11
13
  if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
15
20
  }
16
- return to;
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
17
37
  };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
38
 
20
39
  // src/permissions/index.ts
21
- var permissions_exports = {};
22
- __export(permissions_exports, {
40
+ var exports_permissions = {};
41
+ __export(exports_permissions, {
23
42
  WILDCARD: () => WILDCARD,
24
43
  checkPermissions: () => checkPermissions,
25
44
  createPolicyCatalog: () => createPolicyCatalog,
@@ -30,7 +49,7 @@ __export(permissions_exports, {
30
49
  permissionKey: () => permissionKey,
31
50
  resolvePermissions: () => resolvePermissions
32
51
  });
33
- module.exports = __toCommonJS(permissions_exports);
52
+ module.exports = __toCommonJS(exports_permissions);
34
53
  var WILDCARD = "*";
35
54
  var permissionKey = (resource, operation) => `${resource}:${operation}`;
36
55
  var normalizePolicyKey = (key) => key === WILDCARD ? WILDCARD : key.replace(/([a-z])([A-Z])/g, "$1_$2").replace(/[^a-zA-Z0-9]+/g, "_").replace(/^_+|_+$/g, "").toLowerCase();
@@ -41,9 +60,7 @@ var definePolicies = (policies, options = {}) => {
41
60
  policies: declared.map((policy) => {
42
61
  for (const field of ["name", "operation", "resource"]) {
43
62
  if (!policy[field]) {
44
- throw new TypeError(
45
- `definePolicies: a policy needs a \`${field}\`. Received ${JSON.stringify(policy)} \u2014 a policy missing one of the three cannot be granted, looked up, or named in a refusal.`
46
- );
63
+ throw new TypeError(`definePolicies: a policy needs a \`${field}\`. Received ${JSON.stringify(policy)} — a policy missing one of the three cannot be granted, looked up, or named in a refusal.`);
47
64
  }
48
65
  }
49
66
  return {
@@ -55,17 +72,13 @@ var definePolicies = (policies, options = {}) => {
55
72
  };
56
73
  };
57
74
  var createPolicyCatalog = (options = {}) => {
58
- const byName = /* @__PURE__ */ new Map();
75
+ const byName = new Map;
59
76
  return {
60
77
  declare: (policies) => {
61
78
  const declared = definePolicies(policies, options).policies;
62
79
  for (const policy of declared) {
63
80
  if (byName.has(policy.name)) {
64
- throw new TypeError(
65
- `policy catalogue: "${policy.name}" is already declared as ${JSON.stringify(
66
- byName.get(policy.name)
67
- )}. Upstream keeps these in a global object with no duplicate check, so the one that wins is decided by load order.`
68
- );
81
+ throw new TypeError(`policy catalogue: "${policy.name}" is already declared as ${JSON.stringify(byName.get(policy.name))}. Upstream keeps these in a global object with no duplicate check, so the one that wins is decided by load order.`);
69
82
  }
70
83
  byName.set(policy.name, policy);
71
84
  }
@@ -78,7 +91,7 @@ var createPolicyCatalog = (options = {}) => {
78
91
  };
79
92
  };
80
93
  var expandPolicies = (actions) => {
81
- const byKey = /* @__PURE__ */ new Map();
94
+ const byKey = new Map;
82
95
  for (const action of actions) {
83
96
  const operations = Array.isArray(action.operation) ? action.operation : [action.operation];
84
97
  for (const operation of operations) {
@@ -91,32 +104,28 @@ var expandPolicies = (actions) => {
91
104
  var grantsFor = (grants, role) => grants instanceof Map ? grants.get(role) ?? [] : grants[role] ?? [];
92
105
  var asArray = (value) => Array.isArray(value) ? value : [value];
93
106
  var policyAllows = (grants, roles, resource, operation) => roles.some((role) => {
94
- const allowed = new Set(
95
- expandPolicies(grantsFor(grants, role)).filter((policy) => policy.resource === resource || policy.resource === WILDCARD).map((policy) => policy.operation)
96
- );
107
+ const allowed = new Set(expandPolicies(grantsFor(grants, role)).filter((policy) => policy.resource === resource || policy.resource === WILDCARD).map((policy) => policy.operation));
97
108
  return allowed.has(operation) || allowed.has(WILDCARD);
98
109
  });
99
110
  var hasPermission = (input) => {
100
111
  const roles = asArray(input.roles);
101
112
  const actions = asArray(input.actions);
102
- if (input.enabled === false) return true;
103
- if (actions.length === 0) return true;
104
- if (roles.length === 0) return (input.onEmptyRoles ?? "allow") === "allow";
105
- return actions.every(
106
- (action) => asArray(action.operation).every(
107
- (operation) => policyAllows(input.grants, roles, action.resource, operation)
108
- )
109
- );
113
+ if (input.enabled === false)
114
+ return true;
115
+ if (actions.length === 0)
116
+ return true;
117
+ if (roles.length === 0)
118
+ return (input.onEmptyRoles ?? "allow") === "allow";
119
+ return actions.every((action) => asArray(action.operation).every((operation) => policyAllows(input.grants, roles, action.resource, operation)));
110
120
  };
111
121
  var resolvePermissions = (input) => {
112
122
  const roles = asArray(input.roles);
113
123
  if (input.enabled === false) {
114
124
  return new Set(input.universe.map((entry) => permissionKey(entry.resource, entry.operation)));
115
125
  }
116
- if (roles.length === 0) return /* @__PURE__ */ new Set();
117
- return new Set(
118
- input.universe.filter((entry) => policyAllows(input.grants, roles, entry.resource, entry.operation)).map((entry) => permissionKey(entry.resource, entry.operation))
119
- );
126
+ if (roles.length === 0)
127
+ return new Set;
128
+ return new Set(input.universe.filter((entry) => policyAllows(input.grants, roles, entry.resource, entry.operation)).map((entry) => permissionKey(entry.resource, entry.operation)));
120
129
  };
121
130
  var checkPermissions = (input) => ({
122
131
  allowed: hasPermission({ ...input, onEmptyRoles: "deny" }),
@@ -11,27 +11,27 @@
11
11
  * Every semantic is pinned from the shipped implementation. No test file ships in either tarball.
12
12
  */
13
13
  /** The single character used as the wildcard in BOTH the resource and the operation slot. */
14
- declare const WILDCARD = "*";
15
- type PolicyDefinition = {
14
+ export declare const WILDCARD = "*";
15
+ export type PolicyDefinition = {
16
16
  description?: string;
17
17
  name: string;
18
18
  operation: string;
19
19
  resource: string;
20
20
  };
21
21
  /** A resource with one or more operations on it — what a route asks for. */
22
- type PermissionAction = {
22
+ export type PermissionAction = {
23
23
  operation: readonly string[] | string;
24
24
  resource: string;
25
25
  };
26
26
  /** A single (resource, operation) pair with the key a store would hold it under. */
27
- type ExpandedPolicy = {
27
+ export type ExpandedPolicy = {
28
28
  key: string;
29
29
  operation: string;
30
30
  resource: string;
31
31
  };
32
32
  /** What each role has been granted. The caller fetched it; this decides with it. */
33
- type PolicyGrants = ReadonlyMap<string, readonly PermissionAction[]> | Readonly<Record<string, readonly PermissionAction[]>>;
34
- declare const permissionKey: (resource: string, operation: string) => string;
33
+ export type PolicyGrants = ReadonlyMap<string, readonly PermissionAction[]> | Readonly<Record<string, readonly PermissionAction[]>>;
34
+ export declare const permissionKey: (resource: string, operation: string) => string;
35
35
  /**
36
36
  * `ReadBrands` → `read_brands`, and the wildcard through untouched.
37
37
  *
@@ -40,8 +40,8 @@ declare const permissionKey: (resource: string, operation: string) => string;
40
40
  * directly (as a Medusa storefront does) and ALSO declares them through here would end up with two
41
41
  * spellings of one permission and no error anywhere.
42
42
  */
43
- declare const normalizePolicyKey: (key: string) => string;
44
- type DefinePoliciesOptions = {
43
+ export declare const normalizePolicyKey: (key: string) => string;
44
+ export type DefinePoliciesOptions = {
45
45
  normalize?: (key: string) => string;
46
46
  };
47
47
  /**
@@ -52,10 +52,10 @@ type DefinePoliciesOptions = {
52
52
  * silently replaces the first. Neither is load-bearing for the decision, and both are surprising, so
53
53
  * neither is reproduced — `createPolicyCatalog` is the registry, and it refuses a duplicate.
54
54
  */
55
- declare const definePolicies: (policies: PolicyDefinition | readonly PolicyDefinition[], options?: DefinePoliciesOptions) => {
55
+ export declare const definePolicies: (policies: PolicyDefinition | readonly PolicyDefinition[], options?: DefinePoliciesOptions) => {
56
56
  policies: PolicyDefinition[];
57
57
  };
58
- type PolicyCatalog = {
58
+ export type PolicyCatalog = {
59
59
  declare: (policies: PolicyDefinition | readonly PolicyDefinition[]) => PolicyDefinition[];
60
60
  get: (name: string) => PolicyDefinition | undefined;
61
61
  list: () => PolicyDefinition[];
@@ -63,7 +63,7 @@ type PolicyCatalog = {
63
63
  resources: () => string[];
64
64
  };
65
65
  /** The registry upstream keeps globally, made local and made to refuse a name already taken. */
66
- declare const createPolicyCatalog: (options?: DefinePoliciesOptions) => PolicyCatalog;
66
+ export declare const createPolicyCatalog: (options?: DefinePoliciesOptions) => PolicyCatalog;
67
67
  /**
68
68
  * Expand actions into the unique one-operation rows a store keeps, deduped, key derived.
69
69
  *
@@ -71,8 +71,8 @@ declare const createPolicyCatalog: (options?: DefinePoliciesOptions) => PolicyCa
71
71
  * the single `*:*` row that authorises every route — granting the union of a repo's own named
72
72
  * policies instead covers its custom resources only, and 403s every native one.
73
73
  */
74
- declare const expandPolicies: (actions: readonly PermissionAction[]) => ExpandedPolicy[];
75
- type HasPermissionInput = {
74
+ export declare const expandPolicies: (actions: readonly PermissionAction[]) => ExpandedPolicy[];
75
+ export type HasPermissionInput = {
76
76
  actions: PermissionAction | readonly PermissionAction[];
77
77
  /**
78
78
  * Whether the mechanism is on at all. `false` allows everything, which is what the `rbac` feature
@@ -89,8 +89,8 @@ type HasPermissionInput = {
89
89
  roles: readonly string[] | string;
90
90
  };
91
91
  /** Do these roles grant every one of these actions? */
92
- declare const hasPermission: (input: HasPermissionInput) => boolean;
93
- type ResolvePermissionsInput = {
92
+ export declare const hasPermission: (input: HasPermissionInput) => boolean;
93
+ export type ResolvePermissionsInput = {
94
94
  enabled?: boolean;
95
95
  grants: PolicyGrants;
96
96
  roles: readonly string[] | string;
@@ -108,8 +108,8 @@ type ResolvePermissionsInput = {
108
108
  * answers with EVERYTHING, and for an actor with no roles it answers with NOTHING — where
109
109
  * `hasPermission` answers true for that same actor.
110
110
  */
111
- declare const resolvePermissions: (input: ResolvePermissionsInput) => Set<string>;
112
- type PermissionCheck = {
111
+ export declare const resolvePermissions: (input: ResolvePermissionsInput) => Set<string>;
112
+ export type PermissionCheck = {
113
113
  allowed: boolean;
114
114
  required: string[];
115
115
  };
@@ -121,6 +121,4 @@ type PermissionCheck = {
121
121
  * but unlike the wrapper it still honours the flag, which the wrapper reads only after it has
122
122
  * already thrown.
123
123
  */
124
- declare const checkPermissions: (input: Omit<HasPermissionInput, "onEmptyRoles">) => PermissionCheck;
125
-
126
- export { type DefinePoliciesOptions, type ExpandedPolicy, type HasPermissionInput, type PermissionAction, type PermissionCheck, type PolicyCatalog, type PolicyDefinition, type PolicyGrants, type ResolvePermissionsInput, WILDCARD, checkPermissions, createPolicyCatalog, definePolicies, expandPolicies, hasPermission, normalizePolicyKey, permissionKey, resolvePermissions };
124
+ export declare const checkPermissions: (input: Omit<HasPermissionInput, 'onEmptyRoles'>) => PermissionCheck;
@@ -11,27 +11,27 @@
11
11
  * Every semantic is pinned from the shipped implementation. No test file ships in either tarball.
12
12
  */
13
13
  /** The single character used as the wildcard in BOTH the resource and the operation slot. */
14
- declare const WILDCARD = "*";
15
- type PolicyDefinition = {
14
+ export declare const WILDCARD = "*";
15
+ export type PolicyDefinition = {
16
16
  description?: string;
17
17
  name: string;
18
18
  operation: string;
19
19
  resource: string;
20
20
  };
21
21
  /** A resource with one or more operations on it — what a route asks for. */
22
- type PermissionAction = {
22
+ export type PermissionAction = {
23
23
  operation: readonly string[] | string;
24
24
  resource: string;
25
25
  };
26
26
  /** A single (resource, operation) pair with the key a store would hold it under. */
27
- type ExpandedPolicy = {
27
+ export type ExpandedPolicy = {
28
28
  key: string;
29
29
  operation: string;
30
30
  resource: string;
31
31
  };
32
32
  /** What each role has been granted. The caller fetched it; this decides with it. */
33
- type PolicyGrants = ReadonlyMap<string, readonly PermissionAction[]> | Readonly<Record<string, readonly PermissionAction[]>>;
34
- declare const permissionKey: (resource: string, operation: string) => string;
33
+ export type PolicyGrants = ReadonlyMap<string, readonly PermissionAction[]> | Readonly<Record<string, readonly PermissionAction[]>>;
34
+ export declare const permissionKey: (resource: string, operation: string) => string;
35
35
  /**
36
36
  * `ReadBrands` → `read_brands`, and the wildcard through untouched.
37
37
  *
@@ -40,8 +40,8 @@ declare const permissionKey: (resource: string, operation: string) => string;
40
40
  * directly (as a Medusa storefront does) and ALSO declares them through here would end up with two
41
41
  * spellings of one permission and no error anywhere.
42
42
  */
43
- declare const normalizePolicyKey: (key: string) => string;
44
- type DefinePoliciesOptions = {
43
+ export declare const normalizePolicyKey: (key: string) => string;
44
+ export type DefinePoliciesOptions = {
45
45
  normalize?: (key: string) => string;
46
46
  };
47
47
  /**
@@ -52,10 +52,10 @@ type DefinePoliciesOptions = {
52
52
  * silently replaces the first. Neither is load-bearing for the decision, and both are surprising, so
53
53
  * neither is reproduced — `createPolicyCatalog` is the registry, and it refuses a duplicate.
54
54
  */
55
- declare const definePolicies: (policies: PolicyDefinition | readonly PolicyDefinition[], options?: DefinePoliciesOptions) => {
55
+ export declare const definePolicies: (policies: PolicyDefinition | readonly PolicyDefinition[], options?: DefinePoliciesOptions) => {
56
56
  policies: PolicyDefinition[];
57
57
  };
58
- type PolicyCatalog = {
58
+ export type PolicyCatalog = {
59
59
  declare: (policies: PolicyDefinition | readonly PolicyDefinition[]) => PolicyDefinition[];
60
60
  get: (name: string) => PolicyDefinition | undefined;
61
61
  list: () => PolicyDefinition[];
@@ -63,7 +63,7 @@ type PolicyCatalog = {
63
63
  resources: () => string[];
64
64
  };
65
65
  /** The registry upstream keeps globally, made local and made to refuse a name already taken. */
66
- declare const createPolicyCatalog: (options?: DefinePoliciesOptions) => PolicyCatalog;
66
+ export declare const createPolicyCatalog: (options?: DefinePoliciesOptions) => PolicyCatalog;
67
67
  /**
68
68
  * Expand actions into the unique one-operation rows a store keeps, deduped, key derived.
69
69
  *
@@ -71,8 +71,8 @@ declare const createPolicyCatalog: (options?: DefinePoliciesOptions) => PolicyCa
71
71
  * the single `*:*` row that authorises every route — granting the union of a repo's own named
72
72
  * policies instead covers its custom resources only, and 403s every native one.
73
73
  */
74
- declare const expandPolicies: (actions: readonly PermissionAction[]) => ExpandedPolicy[];
75
- type HasPermissionInput = {
74
+ export declare const expandPolicies: (actions: readonly PermissionAction[]) => ExpandedPolicy[];
75
+ export type HasPermissionInput = {
76
76
  actions: PermissionAction | readonly PermissionAction[];
77
77
  /**
78
78
  * Whether the mechanism is on at all. `false` allows everything, which is what the `rbac` feature
@@ -89,8 +89,8 @@ type HasPermissionInput = {
89
89
  roles: readonly string[] | string;
90
90
  };
91
91
  /** Do these roles grant every one of these actions? */
92
- declare const hasPermission: (input: HasPermissionInput) => boolean;
93
- type ResolvePermissionsInput = {
92
+ export declare const hasPermission: (input: HasPermissionInput) => boolean;
93
+ export type ResolvePermissionsInput = {
94
94
  enabled?: boolean;
95
95
  grants: PolicyGrants;
96
96
  roles: readonly string[] | string;
@@ -108,8 +108,8 @@ type ResolvePermissionsInput = {
108
108
  * answers with EVERYTHING, and for an actor with no roles it answers with NOTHING — where
109
109
  * `hasPermission` answers true for that same actor.
110
110
  */
111
- declare const resolvePermissions: (input: ResolvePermissionsInput) => Set<string>;
112
- type PermissionCheck = {
111
+ export declare const resolvePermissions: (input: ResolvePermissionsInput) => Set<string>;
112
+ export type PermissionCheck = {
113
113
  allowed: boolean;
114
114
  required: string[];
115
115
  };
@@ -121,6 +121,4 @@ type PermissionCheck = {
121
121
  * but unlike the wrapper it still honours the flag, which the wrapper reads only after it has
122
122
  * already thrown.
123
123
  */
124
- declare const checkPermissions: (input: Omit<HasPermissionInput, "onEmptyRoles">) => PermissionCheck;
125
-
126
- export { type DefinePoliciesOptions, type ExpandedPolicy, type HasPermissionInput, type PermissionAction, type PermissionCheck, type PolicyCatalog, type PolicyDefinition, type PolicyGrants, type ResolvePermissionsInput, WILDCARD, checkPermissions, createPolicyCatalog, definePolicies, expandPolicies, hasPermission, normalizePolicyKey, permissionKey, resolvePermissions };
124
+ export declare const checkPermissions: (input: Omit<HasPermissionInput, 'onEmptyRoles'>) => PermissionCheck;
@@ -9,9 +9,7 @@ var definePolicies = (policies, options = {}) => {
9
9
  policies: declared.map((policy) => {
10
10
  for (const field of ["name", "operation", "resource"]) {
11
11
  if (!policy[field]) {
12
- throw new TypeError(
13
- `definePolicies: a policy needs a \`${field}\`. Received ${JSON.stringify(policy)} \u2014 a policy missing one of the three cannot be granted, looked up, or named in a refusal.`
14
- );
12
+ throw new TypeError(`definePolicies: a policy needs a \`${field}\`. Received ${JSON.stringify(policy)} — a policy missing one of the three cannot be granted, looked up, or named in a refusal.`);
15
13
  }
16
14
  }
17
15
  return {
@@ -23,17 +21,13 @@ var definePolicies = (policies, options = {}) => {
23
21
  };
24
22
  };
25
23
  var createPolicyCatalog = (options = {}) => {
26
- const byName = /* @__PURE__ */ new Map();
24
+ const byName = new Map;
27
25
  return {
28
26
  declare: (policies) => {
29
27
  const declared = definePolicies(policies, options).policies;
30
28
  for (const policy of declared) {
31
29
  if (byName.has(policy.name)) {
32
- throw new TypeError(
33
- `policy catalogue: "${policy.name}" is already declared as ${JSON.stringify(
34
- byName.get(policy.name)
35
- )}. Upstream keeps these in a global object with no duplicate check, so the one that wins is decided by load order.`
36
- );
30
+ throw new TypeError(`policy catalogue: "${policy.name}" is already declared as ${JSON.stringify(byName.get(policy.name))}. Upstream keeps these in a global object with no duplicate check, so the one that wins is decided by load order.`);
37
31
  }
38
32
  byName.set(policy.name, policy);
39
33
  }
@@ -46,7 +40,7 @@ var createPolicyCatalog = (options = {}) => {
46
40
  };
47
41
  };
48
42
  var expandPolicies = (actions) => {
49
- const byKey = /* @__PURE__ */ new Map();
43
+ const byKey = new Map;
50
44
  for (const action of actions) {
51
45
  const operations = Array.isArray(action.operation) ? action.operation : [action.operation];
52
46
  for (const operation of operations) {
@@ -59,32 +53,28 @@ var expandPolicies = (actions) => {
59
53
  var grantsFor = (grants, role) => grants instanceof Map ? grants.get(role) ?? [] : grants[role] ?? [];
60
54
  var asArray = (value) => Array.isArray(value) ? value : [value];
61
55
  var policyAllows = (grants, roles, resource, operation) => roles.some((role) => {
62
- const allowed = new Set(
63
- expandPolicies(grantsFor(grants, role)).filter((policy) => policy.resource === resource || policy.resource === WILDCARD).map((policy) => policy.operation)
64
- );
56
+ const allowed = new Set(expandPolicies(grantsFor(grants, role)).filter((policy) => policy.resource === resource || policy.resource === WILDCARD).map((policy) => policy.operation));
65
57
  return allowed.has(operation) || allowed.has(WILDCARD);
66
58
  });
67
59
  var hasPermission = (input) => {
68
60
  const roles = asArray(input.roles);
69
61
  const actions = asArray(input.actions);
70
- if (input.enabled === false) return true;
71
- if (actions.length === 0) return true;
72
- if (roles.length === 0) return (input.onEmptyRoles ?? "allow") === "allow";
73
- return actions.every(
74
- (action) => asArray(action.operation).every(
75
- (operation) => policyAllows(input.grants, roles, action.resource, operation)
76
- )
77
- );
62
+ if (input.enabled === false)
63
+ return true;
64
+ if (actions.length === 0)
65
+ return true;
66
+ if (roles.length === 0)
67
+ return (input.onEmptyRoles ?? "allow") === "allow";
68
+ return actions.every((action) => asArray(action.operation).every((operation) => policyAllows(input.grants, roles, action.resource, operation)));
78
69
  };
79
70
  var resolvePermissions = (input) => {
80
71
  const roles = asArray(input.roles);
81
72
  if (input.enabled === false) {
82
73
  return new Set(input.universe.map((entry) => permissionKey(entry.resource, entry.operation)));
83
74
  }
84
- if (roles.length === 0) return /* @__PURE__ */ new Set();
85
- return new Set(
86
- input.universe.filter((entry) => policyAllows(input.grants, roles, entry.resource, entry.operation)).map((entry) => permissionKey(entry.resource, entry.operation))
87
- );
75
+ if (roles.length === 0)
76
+ return new Set;
77
+ return new Set(input.universe.filter((entry) => policyAllows(input.grants, roles, entry.resource, entry.operation)).map((entry) => permissionKey(entry.resource, entry.operation)));
88
78
  };
89
79
  var checkPermissions = (input) => ({
90
80
  allowed: hasPermission({ ...input, onEmptyRoles: "deny" }),
package/dist/read.cjs ADDED
@@ -0,0 +1,111 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
37
+ };
38
+
39
+ // src/read.ts
40
+ var exports_read = {};
41
+ __export(exports_read, {
42
+ policyOrDefault: () => policyOrDefault,
43
+ readPolicy: () => readPolicy
44
+ });
45
+ module.exports = __toCommonJS(exports_read);
46
+ var import_types = require("./types.cjs");
47
+ var attempt = async (what) => {
48
+ try {
49
+ return await what();
50
+ } catch {
51
+ return;
52
+ }
53
+ };
54
+ var accepts = (policy, kind, value) => {
55
+ const schema = policy.kindOf(kind)?.schema;
56
+ return schema ? schema.safeParse(value).success : true;
57
+ };
58
+ var record = async (policy, journal, entry) => {
59
+ const key = `${entry.event}::${entry.kind}::${entry.op ?? ""}`;
60
+ if (policy.journalledOnce.has(key))
61
+ return;
62
+ policy.journalledOnce.add(key);
63
+ await attempt(() => journal?.write(entry));
64
+ };
65
+ var readPolicy = async (policy, kind, query = {}, options) => {
66
+ const rules = options.rule ? await attempt(() => options.rule?.rules(kind, query)) ?? [] : policy.rules;
67
+ const decision = policy.resolve(kind, query, rules);
68
+ if (decision) {
69
+ const spec = policy.kindOf(kind);
70
+ if (spec?.isReference?.(decision.value)) {
71
+ const referenced = await attempt(() => spec.dereference?.(decision.value, { config: options.config, query }));
72
+ if (referenced !== undefined) {
73
+ return { decision, source: "rule", usedFallback: false, value: referenced };
74
+ }
75
+ } else {
76
+ return { decision, source: "rule", usedFallback: false, value: decision.value };
77
+ }
78
+ }
79
+ const configured = await attempt(() => options.config?.get(kind, query));
80
+ if (configured !== undefined && accepts(policy, kind, configured)) {
81
+ return { source: "config", usedFallback: false, value: configured };
82
+ }
83
+ if (!options.silent) {
84
+ await record(policy, options.journal, {
85
+ event: import_types.POLICY_FALLBACK,
86
+ fellBackTo: options.fallback,
87
+ kind,
88
+ ...options.op ? { op: options.op } : {},
89
+ ...options.subject ? { subject: options.subject } : {}
90
+ });
91
+ }
92
+ return { source: "fallback", usedFallback: true, value: options.fallback };
93
+ };
94
+ var policyOrDefault = async (policy, input, apply) => {
95
+ try {
96
+ return apply(input.value);
97
+ } catch (error) {
98
+ await record(policy, input.journal, {
99
+ event: import_types.POLICY_INVALID_VALUE,
100
+ fellBackTo: input.fallback,
101
+ kind: input.kind,
102
+ op: input.op,
103
+ rejected: {
104
+ reason: error instanceof Error ? error.message : String(error),
105
+ value: input.value
106
+ },
107
+ ...input.subject ? { subject: input.subject } : {}
108
+ });
109
+ return apply(input.fallback);
110
+ }
111
+ };
@@ -0,0 +1,51 @@
1
+ import type { Policy, PolicyConfigReader, PolicyJournal, PolicyQuery, PolicyReading, PolicyRuleReader } from './types.cjs';
2
+ export type ReadPolicyOptions<V> = {
3
+ config?: PolicyConfigReader;
4
+ /** The caller's own constant — the floor, and the type of the answer. */
5
+ fallback: V;
6
+ journal?: PolicyJournal;
7
+ /** A named operation, so repeats become countable per operation rather than per request. */
8
+ op?: string;
9
+ rule?: PolicyRuleReader;
10
+ /** Opt out of recording. Only for a caller that runs per request inside a loop. */
11
+ silent?: boolean;
12
+ /** What the decision is about — an order id, a company id. */
13
+ subject?: string;
14
+ };
15
+ /**
16
+ * Read one constraint, with the caller's own default as the floor.
17
+ *
18
+ * This is what a reader calls instead of reaching for a hardcoded constant. It answers with the
19
+ * resolved value when policy speaks, the admin-set value when it does not, and the caller's own
20
+ * constant when nobody has said anything — and, the part that matters, it RECORDS that last case,
21
+ * so "nothing sets this" becomes a countable signal rather than a silence indistinguishable from
22
+ * working.
23
+ *
24
+ * The resolution order is authored rule → config row → constant. A rule wins because it carries an
25
+ * authority, a subject and a reason where a config row carries only a value; a config row beats the
26
+ * constant because a value an admin actually typed must beat one a developer compiled in, or the
27
+ * settings screen is a screen of knobs nothing reads.
28
+ */
29
+ export declare const readPolicy: <V>(policy: Policy, kind: string, query: PolicyQuery | undefined, options: ReadPolicyOptions<V>) => Promise<PolicyReading<V>>;
30
+ export type PolicyOrDefaultInput<V> = {
31
+ fallback: V;
32
+ journal?: PolicyJournal;
33
+ kind: string;
34
+ op: string;
35
+ subject?: string;
36
+ value: V;
37
+ };
38
+ /**
39
+ * Apply a resolved value, falling back to the caller's own default when it turns out to be
40
+ * unusable.
41
+ *
42
+ * A validator that refuses a zero-day expiry or a zero-hour SLA is right to. But a number an admin
43
+ * mistypes would then throw inside the work it was informing, and every one of those operations
44
+ * would fail until somebody noticed — the customer paying for a misconfiguration. An unusable value
45
+ * is a gap that arrived through a different door, so it is treated like every other gap: recorded
46
+ * once per boot, and the default takes over while the mistake stays visible.
47
+ *
48
+ * A default that ALSO fails still throws. That is a bug in our own code rather than a
49
+ * misconfiguration, and swallowing it would leave the caller with no value at all.
50
+ */
51
+ export declare const policyOrDefault: <T, V>(policy: Policy, input: PolicyOrDefaultInput<V>, apply: (value: V) => T) => Promise<T>;
package/dist/read.d.ts ADDED
@@ -0,0 +1,51 @@
1
+ import type { Policy, PolicyConfigReader, PolicyJournal, PolicyQuery, PolicyReading, PolicyRuleReader } from './types.js';
2
+ export type ReadPolicyOptions<V> = {
3
+ config?: PolicyConfigReader;
4
+ /** The caller's own constant — the floor, and the type of the answer. */
5
+ fallback: V;
6
+ journal?: PolicyJournal;
7
+ /** A named operation, so repeats become countable per operation rather than per request. */
8
+ op?: string;
9
+ rule?: PolicyRuleReader;
10
+ /** Opt out of recording. Only for a caller that runs per request inside a loop. */
11
+ silent?: boolean;
12
+ /** What the decision is about — an order id, a company id. */
13
+ subject?: string;
14
+ };
15
+ /**
16
+ * Read one constraint, with the caller's own default as the floor.
17
+ *
18
+ * This is what a reader calls instead of reaching for a hardcoded constant. It answers with the
19
+ * resolved value when policy speaks, the admin-set value when it does not, and the caller's own
20
+ * constant when nobody has said anything — and, the part that matters, it RECORDS that last case,
21
+ * so "nothing sets this" becomes a countable signal rather than a silence indistinguishable from
22
+ * working.
23
+ *
24
+ * The resolution order is authored rule → config row → constant. A rule wins because it carries an
25
+ * authority, a subject and a reason where a config row carries only a value; a config row beats the
26
+ * constant because a value an admin actually typed must beat one a developer compiled in, or the
27
+ * settings screen is a screen of knobs nothing reads.
28
+ */
29
+ export declare const readPolicy: <V>(policy: Policy, kind: string, query: PolicyQuery | undefined, options: ReadPolicyOptions<V>) => Promise<PolicyReading<V>>;
30
+ export type PolicyOrDefaultInput<V> = {
31
+ fallback: V;
32
+ journal?: PolicyJournal;
33
+ kind: string;
34
+ op: string;
35
+ subject?: string;
36
+ value: V;
37
+ };
38
+ /**
39
+ * Apply a resolved value, falling back to the caller's own default when it turns out to be
40
+ * unusable.
41
+ *
42
+ * A validator that refuses a zero-day expiry or a zero-hour SLA is right to. But a number an admin
43
+ * mistypes would then throw inside the work it was informing, and every one of those operations
44
+ * would fail until somebody noticed — the customer paying for a misconfiguration. An unusable value
45
+ * is a gap that arrived through a different door, so it is treated like every other gap: recorded
46
+ * once per boot, and the default takes over while the mistake stays visible.
47
+ *
48
+ * A default that ALSO fails still throws. That is a bug in our own code rather than a
49
+ * misconfiguration, and swallowing it would leave the caller with no value at all.
50
+ */
51
+ export declare const policyOrDefault: <T, V>(policy: Policy, input: PolicyOrDefaultInput<V>, apply: (value: V) => T) => Promise<T>;