@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.
- package/CHANGELOG.md +6 -0
- package/dist/conformance.cjs +169 -0
- package/dist/conformance.d.cts +35 -0
- package/dist/conformance.d.ts +35 -0
- package/dist/conformance.js +129 -0
- package/dist/define.cjs +118 -0
- package/dist/define.d.cts +34 -0
- package/dist/define.d.ts +34 -0
- package/dist/define.js +85 -0
- package/dist/drizzle/index.cjs +37 -32
- package/dist/drizzle/index.d.cts +20 -23
- package/dist/drizzle/index.d.ts +20 -23
- package/dist/drizzle/index.js +2 -5
- package/dist/index.cjs +79 -856
- package/dist/index.d.cts +19 -428
- package/dist/index.d.ts +19 -428
- package/dist/index.js +35 -797
- package/dist/journal-events.cjs +68 -0
- package/dist/journal-events.d.cts +26 -0
- package/dist/journal-events.d.ts +26 -0
- package/dist/journal-events.js +28 -0
- package/dist/lattice.cjs +150 -0
- package/dist/lattice.d.cts +48 -0
- package/dist/lattice.d.ts +48 -0
- package/dist/lattice.js +110 -0
- package/dist/learning.cjs +246 -0
- package/dist/learning.d.cts +121 -0
- package/dist/learning.d.ts +121 -0
- package/dist/learning.js +206 -0
- package/dist/markdown.cjs +118 -0
- package/dist/markdown.d.cts +35 -0
- package/dist/markdown.d.ts +35 -0
- package/dist/markdown.js +78 -0
- package/dist/medusa/index.cjs +39 -36
- package/dist/medusa/index.d.cts +14 -16
- package/dist/medusa/index.d.ts +14 -16
- package/dist/medusa/index.js +4 -9
- package/dist/permissions/index.cjs +49 -40
- package/dist/permissions/index.d.cts +18 -20
- package/dist/permissions/index.d.ts +18 -20
- package/dist/permissions/index.js +15 -25
- package/dist/read.cjs +111 -0
- package/dist/read.d.cts +51 -0
- package/dist/read.d.ts +51 -0
- package/dist/read.js +71 -0
- package/dist/rows.cjs +54 -0
- package/dist/rows.d.cts +11 -0
- package/dist/rows.d.ts +11 -0
- package/dist/{chunk-JICJ4XEX.js → rows.js} +0 -3
- package/dist/seed.cjs +53 -0
- package/dist/seed.d.cts +18 -0
- package/dist/seed.d.ts +18 -0
- package/dist/seed.js +13 -0
- package/dist/threshold.cjs +73 -0
- package/dist/threshold.d.cts +45 -0
- package/dist/threshold.d.ts +45 -0
- package/dist/threshold.js +33 -0
- package/dist/types.cjs +47 -0
- package/dist/{types-C-Q3r-In.d.cts → types.d.cts} +21 -23
- package/dist/{types-C-Q3r-In.d.ts → types.d.ts} +21 -23
- package/dist/types.js +7 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,169 @@
|
|
|
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/conformance.ts
|
|
40
|
+
var exports_conformance = {};
|
|
41
|
+
__export(exports_conformance, {
|
|
42
|
+
runPolicyAdapterConformance: () => runPolicyAdapterConformance
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(exports_conformance);
|
|
45
|
+
var import_define = require("./define.cjs");
|
|
46
|
+
var import_read = require("./read.cjs");
|
|
47
|
+
var probePolicy = () => import_define.definePolicy({
|
|
48
|
+
kinds: ["quoteExpiryDays", "containerCutoff", "aiCostCapUsd", "unread"],
|
|
49
|
+
scopeKeys: ["companyId"],
|
|
50
|
+
tiers: ["contract", { name: "tenant", overridable: true }]
|
|
51
|
+
});
|
|
52
|
+
var reason = "the reason a person blocked by this rule would read";
|
|
53
|
+
var ROWS = [
|
|
54
|
+
{
|
|
55
|
+
constraint_kind: "quoteExpiryDays",
|
|
56
|
+
constraint_value: 30,
|
|
57
|
+
reason,
|
|
58
|
+
rule_key: "expiry-30",
|
|
59
|
+
tier: "tenant"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
constraint_kind: "quoteExpiryDays",
|
|
63
|
+
constraint_value: 60,
|
|
64
|
+
reason,
|
|
65
|
+
rule_key: "expiry-60-acme",
|
|
66
|
+
scope: { companyId: "comp_acme" },
|
|
67
|
+
tier: "tenant"
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
constraint_kind: "containerCutoff",
|
|
71
|
+
constraint_value: { dayOfWeek: 5, hourUtc: 18 },
|
|
72
|
+
reason,
|
|
73
|
+
rule_key: "cutoff-friday",
|
|
74
|
+
tier: "tenant"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
constraint_kind: "aiCostCapUsd",
|
|
78
|
+
constraint_value: 0.5,
|
|
79
|
+
overridable: false,
|
|
80
|
+
reason,
|
|
81
|
+
rule_key: "cost-cap",
|
|
82
|
+
tier: "tenant"
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
constraint_kind: "quoteExpiryDays",
|
|
86
|
+
constraint_value: 1,
|
|
87
|
+
reason,
|
|
88
|
+
rule_key: "expiry-retired",
|
|
89
|
+
status: "retired",
|
|
90
|
+
tier: "contract"
|
|
91
|
+
}
|
|
92
|
+
];
|
|
93
|
+
var check = async (name, assertion) => {
|
|
94
|
+
try {
|
|
95
|
+
await assertion();
|
|
96
|
+
return { name, passed: true };
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return { detail: error instanceof Error ? error.message : String(error), name, passed: false };
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var must = (condition, detail) => {
|
|
102
|
+
if (!condition)
|
|
103
|
+
throw new Error(detail);
|
|
104
|
+
};
|
|
105
|
+
var same = (actual, expected, what) => must(JSON.stringify(actual) === JSON.stringify(expected), `${what}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
|
|
106
|
+
var runPolicyAdapterConformance = async (harness) => {
|
|
107
|
+
const over = (config) => harness.create({ config, rows: ROWS });
|
|
108
|
+
const checks = [
|
|
109
|
+
await check("reads a stored rule back as one the lattice can use", async () => {
|
|
110
|
+
const rules = await over().adapter.rule.rules("quoteExpiryDays", {});
|
|
111
|
+
const found = rules.find((rule) => rule.id === "expiry-30");
|
|
112
|
+
must(found !== undefined, "no rule came back under the key it was stored with");
|
|
113
|
+
same(found?.constraint, { kind: "quoteExpiryDays", value: 30 }, "the constraint");
|
|
114
|
+
same(found?.tier, "tenant", "the authority");
|
|
115
|
+
same(found?.reason, reason, "the reason");
|
|
116
|
+
}),
|
|
117
|
+
await check("leaves a retired row out, because it is not a current opinion", async () => {
|
|
118
|
+
const rules = await over().adapter.rule.rules("quoteExpiryDays", {});
|
|
119
|
+
must(!rules.some((rule) => rule.id === "expiry-retired"), "a retired rule came back and would have decided");
|
|
120
|
+
}),
|
|
121
|
+
await check("keeps the scope, so a rule reaches only the subject it was aimed at", async () => {
|
|
122
|
+
const { adapter } = over();
|
|
123
|
+
const policy = probePolicy();
|
|
124
|
+
const rules = await adapter.rule.rules("quoteExpiryDays", {});
|
|
125
|
+
same(policy.resolve("quoteExpiryDays", { companyId: "comp_acme" }, rules)?.value, 60, "the scoped rule");
|
|
126
|
+
same(policy.resolve("quoteExpiryDays", { companyId: "comp_other" }, rules)?.value, 30, "the general rule");
|
|
127
|
+
}),
|
|
128
|
+
await check("carries a structured value through the store unchanged", async () => {
|
|
129
|
+
const rules = await over().adapter.rule.rules("containerCutoff", {});
|
|
130
|
+
same(rules.find((rule) => rule.id === "cutoff-friday")?.constraint.value, { dayOfWeek: 5, hourUtc: 18 }, "the schedule");
|
|
131
|
+
}),
|
|
132
|
+
await check("keeps a row that refuses to be overridden", async () => {
|
|
133
|
+
const rules = await over().adapter.rule.rules("aiCostCapUsd", {});
|
|
134
|
+
same(rules.find((rule) => rule.id === "cost-cap")?.overridable, false, "the override flag");
|
|
135
|
+
}),
|
|
136
|
+
await check("answers nothing for a kind the store holds no value for", async () => {
|
|
137
|
+
const value = await over().adapter.config?.get("unread", {});
|
|
138
|
+
must(value === undefined, `an unset kind answered with ${JSON.stringify(value)}`);
|
|
139
|
+
}),
|
|
140
|
+
await check("reads an admin-set value when no rule speaks", async () => {
|
|
141
|
+
const { adapter } = over({ unread: 7 });
|
|
142
|
+
const reading = await import_read.readPolicy(probePolicy(), "unread", {}, {
|
|
143
|
+
config: adapter.config,
|
|
144
|
+
fallback: 1,
|
|
145
|
+
rule: adapter.rule
|
|
146
|
+
});
|
|
147
|
+
same(reading.value, 7, "the admin-set value");
|
|
148
|
+
same(reading.source, "config", "which layer decided");
|
|
149
|
+
}),
|
|
150
|
+
await check("records a fallback, naming the kind that had nothing to say", async () => {
|
|
151
|
+
const { adapter, written } = over();
|
|
152
|
+
await import_read.readPolicy(probePolicy(), "unread", {}, {
|
|
153
|
+
config: adapter.config,
|
|
154
|
+
fallback: 1,
|
|
155
|
+
journal: adapter.journal,
|
|
156
|
+
op: "a-named-operation",
|
|
157
|
+
rule: adapter.rule
|
|
158
|
+
});
|
|
159
|
+
const entries = written();
|
|
160
|
+
same(entries.length, 1, "how many records one fallback produced");
|
|
161
|
+
must(JSON.stringify(entries).includes("unread"), "the record does not name the kind, so nobody can count it");
|
|
162
|
+
must(JSON.stringify(entries).includes("a-named-operation"), "the record does not name the operation, so repeats cannot be told apart");
|
|
163
|
+
}),
|
|
164
|
+
await check("does not let a store that is down throw out of a config read", async () => {
|
|
165
|
+
await harness.createBroken().config?.get("unread", {});
|
|
166
|
+
})
|
|
167
|
+
];
|
|
168
|
+
return { adapter: harness.name, checks, passed: checks.every((entry) => entry.passed) };
|
|
169
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { PolicyAdapter, PolicyRuleRow } from './types.cjs';
|
|
2
|
+
/**
|
|
3
|
+
* One suite every storage adapter passes.
|
|
4
|
+
*
|
|
5
|
+
* A real Medusa module and a real drizzle table are the consumers' to run this against; what runs
|
|
6
|
+
* here is a stub, which is the point — the checks are about what an adapter must MEAN, and neither
|
|
7
|
+
* store's presence changes one of them.
|
|
8
|
+
*
|
|
9
|
+
* It answers with a report rather than an exit code: a gate that cannot say what it measured has
|
|
10
|
+
* not been shown to measure anything.
|
|
11
|
+
*/
|
|
12
|
+
export type ConformanceCheck = {
|
|
13
|
+
detail?: string;
|
|
14
|
+
name: string;
|
|
15
|
+
passed: boolean;
|
|
16
|
+
};
|
|
17
|
+
export type ConformanceReport = {
|
|
18
|
+
adapter: string;
|
|
19
|
+
checks: ConformanceCheck[];
|
|
20
|
+
passed: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type PolicyAdapterHarness = {
|
|
23
|
+
/** An adapter whose store holds exactly this, and a way to read what its journal received. */
|
|
24
|
+
create: (input: {
|
|
25
|
+
config?: Record<string, unknown>;
|
|
26
|
+
rows: readonly PolicyRuleRow[];
|
|
27
|
+
}) => {
|
|
28
|
+
adapter: PolicyAdapter;
|
|
29
|
+
written: () => readonly unknown[];
|
|
30
|
+
};
|
|
31
|
+
/** An adapter over a store that is DOWN. Swallowing belongs inside the adapter, not around it. */
|
|
32
|
+
createBroken: () => PolicyAdapter;
|
|
33
|
+
name: string;
|
|
34
|
+
};
|
|
35
|
+
export declare const runPolicyAdapterConformance: (harness: PolicyAdapterHarness) => Promise<ConformanceReport>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { PolicyAdapter, PolicyRuleRow } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* One suite every storage adapter passes.
|
|
4
|
+
*
|
|
5
|
+
* A real Medusa module and a real drizzle table are the consumers' to run this against; what runs
|
|
6
|
+
* here is a stub, which is the point — the checks are about what an adapter must MEAN, and neither
|
|
7
|
+
* store's presence changes one of them.
|
|
8
|
+
*
|
|
9
|
+
* It answers with a report rather than an exit code: a gate that cannot say what it measured has
|
|
10
|
+
* not been shown to measure anything.
|
|
11
|
+
*/
|
|
12
|
+
export type ConformanceCheck = {
|
|
13
|
+
detail?: string;
|
|
14
|
+
name: string;
|
|
15
|
+
passed: boolean;
|
|
16
|
+
};
|
|
17
|
+
export type ConformanceReport = {
|
|
18
|
+
adapter: string;
|
|
19
|
+
checks: ConformanceCheck[];
|
|
20
|
+
passed: boolean;
|
|
21
|
+
};
|
|
22
|
+
export type PolicyAdapterHarness = {
|
|
23
|
+
/** An adapter whose store holds exactly this, and a way to read what its journal received. */
|
|
24
|
+
create: (input: {
|
|
25
|
+
config?: Record<string, unknown>;
|
|
26
|
+
rows: readonly PolicyRuleRow[];
|
|
27
|
+
}) => {
|
|
28
|
+
adapter: PolicyAdapter;
|
|
29
|
+
written: () => readonly unknown[];
|
|
30
|
+
};
|
|
31
|
+
/** An adapter over a store that is DOWN. Swallowing belongs inside the adapter, not around it. */
|
|
32
|
+
createBroken: () => PolicyAdapter;
|
|
33
|
+
name: string;
|
|
34
|
+
};
|
|
35
|
+
export declare const runPolicyAdapterConformance: (harness: PolicyAdapterHarness) => Promise<ConformanceReport>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/conformance.ts
|
|
2
|
+
import { definePolicy } from "./define.js";
|
|
3
|
+
import { readPolicy } from "./read.js";
|
|
4
|
+
var probePolicy = () => definePolicy({
|
|
5
|
+
kinds: ["quoteExpiryDays", "containerCutoff", "aiCostCapUsd", "unread"],
|
|
6
|
+
scopeKeys: ["companyId"],
|
|
7
|
+
tiers: ["contract", { name: "tenant", overridable: true }]
|
|
8
|
+
});
|
|
9
|
+
var reason = "the reason a person blocked by this rule would read";
|
|
10
|
+
var ROWS = [
|
|
11
|
+
{
|
|
12
|
+
constraint_kind: "quoteExpiryDays",
|
|
13
|
+
constraint_value: 30,
|
|
14
|
+
reason,
|
|
15
|
+
rule_key: "expiry-30",
|
|
16
|
+
tier: "tenant"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
constraint_kind: "quoteExpiryDays",
|
|
20
|
+
constraint_value: 60,
|
|
21
|
+
reason,
|
|
22
|
+
rule_key: "expiry-60-acme",
|
|
23
|
+
scope: { companyId: "comp_acme" },
|
|
24
|
+
tier: "tenant"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
constraint_kind: "containerCutoff",
|
|
28
|
+
constraint_value: { dayOfWeek: 5, hourUtc: 18 },
|
|
29
|
+
reason,
|
|
30
|
+
rule_key: "cutoff-friday",
|
|
31
|
+
tier: "tenant"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
constraint_kind: "aiCostCapUsd",
|
|
35
|
+
constraint_value: 0.5,
|
|
36
|
+
overridable: false,
|
|
37
|
+
reason,
|
|
38
|
+
rule_key: "cost-cap",
|
|
39
|
+
tier: "tenant"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
constraint_kind: "quoteExpiryDays",
|
|
43
|
+
constraint_value: 1,
|
|
44
|
+
reason,
|
|
45
|
+
rule_key: "expiry-retired",
|
|
46
|
+
status: "retired",
|
|
47
|
+
tier: "contract"
|
|
48
|
+
}
|
|
49
|
+
];
|
|
50
|
+
var check = async (name, assertion) => {
|
|
51
|
+
try {
|
|
52
|
+
await assertion();
|
|
53
|
+
return { name, passed: true };
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return { detail: error instanceof Error ? error.message : String(error), name, passed: false };
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var must = (condition, detail) => {
|
|
59
|
+
if (!condition)
|
|
60
|
+
throw new Error(detail);
|
|
61
|
+
};
|
|
62
|
+
var same = (actual, expected, what) => must(JSON.stringify(actual) === JSON.stringify(expected), `${what}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
|
|
63
|
+
var runPolicyAdapterConformance = async (harness) => {
|
|
64
|
+
const over = (config) => harness.create({ config, rows: ROWS });
|
|
65
|
+
const checks = [
|
|
66
|
+
await check("reads a stored rule back as one the lattice can use", async () => {
|
|
67
|
+
const rules = await over().adapter.rule.rules("quoteExpiryDays", {});
|
|
68
|
+
const found = rules.find((rule) => rule.id === "expiry-30");
|
|
69
|
+
must(found !== undefined, "no rule came back under the key it was stored with");
|
|
70
|
+
same(found?.constraint, { kind: "quoteExpiryDays", value: 30 }, "the constraint");
|
|
71
|
+
same(found?.tier, "tenant", "the authority");
|
|
72
|
+
same(found?.reason, reason, "the reason");
|
|
73
|
+
}),
|
|
74
|
+
await check("leaves a retired row out, because it is not a current opinion", async () => {
|
|
75
|
+
const rules = await over().adapter.rule.rules("quoteExpiryDays", {});
|
|
76
|
+
must(!rules.some((rule) => rule.id === "expiry-retired"), "a retired rule came back and would have decided");
|
|
77
|
+
}),
|
|
78
|
+
await check("keeps the scope, so a rule reaches only the subject it was aimed at", async () => {
|
|
79
|
+
const { adapter } = over();
|
|
80
|
+
const policy = probePolicy();
|
|
81
|
+
const rules = await adapter.rule.rules("quoteExpiryDays", {});
|
|
82
|
+
same(policy.resolve("quoteExpiryDays", { companyId: "comp_acme" }, rules)?.value, 60, "the scoped rule");
|
|
83
|
+
same(policy.resolve("quoteExpiryDays", { companyId: "comp_other" }, rules)?.value, 30, "the general rule");
|
|
84
|
+
}),
|
|
85
|
+
await check("carries a structured value through the store unchanged", async () => {
|
|
86
|
+
const rules = await over().adapter.rule.rules("containerCutoff", {});
|
|
87
|
+
same(rules.find((rule) => rule.id === "cutoff-friday")?.constraint.value, { dayOfWeek: 5, hourUtc: 18 }, "the schedule");
|
|
88
|
+
}),
|
|
89
|
+
await check("keeps a row that refuses to be overridden", async () => {
|
|
90
|
+
const rules = await over().adapter.rule.rules("aiCostCapUsd", {});
|
|
91
|
+
same(rules.find((rule) => rule.id === "cost-cap")?.overridable, false, "the override flag");
|
|
92
|
+
}),
|
|
93
|
+
await check("answers nothing for a kind the store holds no value for", async () => {
|
|
94
|
+
const value = await over().adapter.config?.get("unread", {});
|
|
95
|
+
must(value === undefined, `an unset kind answered with ${JSON.stringify(value)}`);
|
|
96
|
+
}),
|
|
97
|
+
await check("reads an admin-set value when no rule speaks", async () => {
|
|
98
|
+
const { adapter } = over({ unread: 7 });
|
|
99
|
+
const reading = await readPolicy(probePolicy(), "unread", {}, {
|
|
100
|
+
config: adapter.config,
|
|
101
|
+
fallback: 1,
|
|
102
|
+
rule: adapter.rule
|
|
103
|
+
});
|
|
104
|
+
same(reading.value, 7, "the admin-set value");
|
|
105
|
+
same(reading.source, "config", "which layer decided");
|
|
106
|
+
}),
|
|
107
|
+
await check("records a fallback, naming the kind that had nothing to say", async () => {
|
|
108
|
+
const { adapter, written } = over();
|
|
109
|
+
await readPolicy(probePolicy(), "unread", {}, {
|
|
110
|
+
config: adapter.config,
|
|
111
|
+
fallback: 1,
|
|
112
|
+
journal: adapter.journal,
|
|
113
|
+
op: "a-named-operation",
|
|
114
|
+
rule: adapter.rule
|
|
115
|
+
});
|
|
116
|
+
const entries = written();
|
|
117
|
+
same(entries.length, 1, "how many records one fallback produced");
|
|
118
|
+
must(JSON.stringify(entries).includes("unread"), "the record does not name the kind, so nobody can count it");
|
|
119
|
+
must(JSON.stringify(entries).includes("a-named-operation"), "the record does not name the operation, so repeats cannot be told apart");
|
|
120
|
+
}),
|
|
121
|
+
await check("does not let a store that is down throw out of a config read", async () => {
|
|
122
|
+
await harness.createBroken().config?.get("unread", {});
|
|
123
|
+
})
|
|
124
|
+
];
|
|
125
|
+
return { adapter: harness.name, checks, passed: checks.every((entry) => entry.passed) };
|
|
126
|
+
};
|
|
127
|
+
export {
|
|
128
|
+
runPolicyAdapterConformance
|
|
129
|
+
};
|
package/dist/define.cjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
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/define.ts
|
|
40
|
+
var exports_define = {};
|
|
41
|
+
__export(exports_define, {
|
|
42
|
+
assertScopeOrder: () => assertScopeOrder,
|
|
43
|
+
definePolicy: () => definePolicy
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(exports_define);
|
|
46
|
+
var import_lattice = require("./lattice.cjs");
|
|
47
|
+
var asTiers = (values) => values.map((value) => typeof value === "string" ? { name: value } : value);
|
|
48
|
+
var asKinds = (values) => values.map((value) => typeof value === "string" ? { kind: value } : value);
|
|
49
|
+
var refuseDuplicates = (values, what) => {
|
|
50
|
+
const seen = new Set;
|
|
51
|
+
for (const value of values) {
|
|
52
|
+
if (seen.has(value)) {
|
|
53
|
+
throw new TypeError(`definePolicy: ${what} "${value}" is declared twice. Which one a rule means would then depend on which was found first.`);
|
|
54
|
+
}
|
|
55
|
+
seen.add(value);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var definePolicy = (input) => {
|
|
59
|
+
const tiers = asTiers(input.tiers);
|
|
60
|
+
const kinds = asKinds(input.kinds);
|
|
61
|
+
if (input.scopeKeys !== undefined && !Array.isArray(input.scopeKeys)) {
|
|
62
|
+
throw new TypeError("definePolicy: `scopeKeys` must be an ARRAY in precedence order (broadest first, narrowest last) — its order is the lattice, and a Set has none. Spell the order; `assertScopeOrder` can hold it against your scope type.");
|
|
63
|
+
}
|
|
64
|
+
const scopeKeys = [...input.scopeKeys ?? []];
|
|
65
|
+
const rules = [...input.rules ?? []];
|
|
66
|
+
if (tiers.length === 0) {
|
|
67
|
+
throw new TypeError("definePolicy: `tiers` is empty. A lattice with no authorities could never decide anything, and every rule written against it would name a tier it does not have.");
|
|
68
|
+
}
|
|
69
|
+
refuseDuplicates(tiers.map((tier) => tier.name), "tier");
|
|
70
|
+
refuseDuplicates(kinds.map((kind) => kind.kind), "kind");
|
|
71
|
+
refuseDuplicates(scopeKeys, "scope key");
|
|
72
|
+
const overriding = tiers.filter((tier) => tier.overrides);
|
|
73
|
+
if (overriding.length > 1) {
|
|
74
|
+
throw new TypeError(`definePolicy: ${overriding.map((tier) => `"${tier.name}"`).join(" and ")} both declare \`overrides\`. The audited escape hatch is one tier or none — two of them race for the same rule.`);
|
|
75
|
+
}
|
|
76
|
+
const lastAuthority = tiers.findLastIndex((tier) => !tier.advisory);
|
|
77
|
+
const firstAdvisory = tiers.findIndex((tier) => tier.advisory);
|
|
78
|
+
if (firstAdvisory !== -1 && firstAdvisory < lastAuthority) {
|
|
79
|
+
throw new TypeError(`definePolicy: advisory tier "${tiers[firstAdvisory]?.name}" is declared stronger than "${tiers[lastAuthority]?.name}". An advisory tier fills silence; one that outranks an authority is not advisory, it is an authority with a reassuring name.`);
|
|
80
|
+
}
|
|
81
|
+
const data = {
|
|
82
|
+
kinds,
|
|
83
|
+
overridableByDefault: input.overridableByDefault ?? true,
|
|
84
|
+
rules,
|
|
85
|
+
scopeKeys,
|
|
86
|
+
tiers
|
|
87
|
+
};
|
|
88
|
+
const journalledOnce = new Set;
|
|
89
|
+
return {
|
|
90
|
+
...data,
|
|
91
|
+
journalledOnce,
|
|
92
|
+
kindOf: (kind) => kinds.find((spec) => spec.kind === kind),
|
|
93
|
+
rank: (tier) => {
|
|
94
|
+
const index = tiers.findIndex((spec) => spec.name === tier);
|
|
95
|
+
return index === -1 ? undefined : index;
|
|
96
|
+
},
|
|
97
|
+
resetJournal: () => journalledOnce.clear(),
|
|
98
|
+
resolve: (kind, query, given) => import_lattice.resolveConstraint(data, kind, query, given),
|
|
99
|
+
specificity: (scope) => import_lattice.specificityOf(scopeKeys, scope),
|
|
100
|
+
tierOf: (tier) => tiers.find((spec) => spec.name === tier),
|
|
101
|
+
unsetKinds: (given = rules) => import_lattice.unsetKindsOf(given, kinds),
|
|
102
|
+
unsupportedScopes: (given = rules) => import_lattice.unsupportedScopesOf(given, scopeKeys),
|
|
103
|
+
unusableRules: (given = rules) => import_lattice.unusableRulesOf(given, data),
|
|
104
|
+
usableRules: (given = rules) => import_lattice.usableRulesOf(given, data)
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
var assertScopeOrder = (policy, expected) => {
|
|
108
|
+
const missing = expected.filter((key) => !policy.scopeKeys.includes(key));
|
|
109
|
+
const extra = policy.scopeKeys.filter((key) => !expected.includes(key));
|
|
110
|
+
if (missing.length > 0 || extra.length > 0) {
|
|
111
|
+
throw new Error(`assertScopeOrder: the declared order and the scope type disagree — ${[...missing.map((k) => `"${k}" is in the type and not the order`), ...extra.map((k) => `"${k}" is in the order and not the type`)].join("; ")}.`);
|
|
112
|
+
}
|
|
113
|
+
for (const [index, key] of expected.entries()) {
|
|
114
|
+
if (policy.scopeKeys[index] !== key) {
|
|
115
|
+
throw new Error(`assertScopeOrder: position ${String(index)} holds "${policy.scopeKeys[index] ?? ""}" where the expected order holds "${key}" — the order is the lattice; if the type's order changed, change the declaration deliberately.`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Policy, PolicyKindSpec, PolicyRule, PolicyTierSpec } from './types.cjs';
|
|
2
|
+
export type PolicyInput = {
|
|
3
|
+
/** What policy may constrain. A kind with no reader is an inert knob; a reader with no kind cannot be authored. */
|
|
4
|
+
kinds: readonly (PolicyKindSpec | string)[];
|
|
5
|
+
/**
|
|
6
|
+
* May the overriding tier beat a rule that does not say either way?
|
|
7
|
+
*
|
|
8
|
+
* A Medusa storefront says yes (`rule.overridable !== false`), musa says no (`&& winner.overridable`).
|
|
9
|
+
* Neither is wrong for the other's business, so it is declared rather than assumed.
|
|
10
|
+
*/
|
|
11
|
+
overridableByDefault?: boolean;
|
|
12
|
+
rules?: readonly PolicyRule[];
|
|
13
|
+
/** VAGUEST first — the weight of a key is derived from where it sits. */
|
|
14
|
+
scopeKeys?: readonly string[];
|
|
15
|
+
/** STRONGEST first. */
|
|
16
|
+
tiers: readonly (PolicyTierSpec | string)[];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Declare a policy: the authorities, the things they may constrain, the subjects they may be aimed
|
|
20
|
+
* at, and — optionally — the rules themselves.
|
|
21
|
+
*
|
|
22
|
+
* A policy with no rules is INERT. Every read falls through to the caller's own constant and says
|
|
23
|
+
* so, which is the posture the mechanism is built for: the loader is generic, the domain limits
|
|
24
|
+
* live in data, and the kernel does nothing at all until someone writes a rule.
|
|
25
|
+
*/
|
|
26
|
+
export declare const definePolicy: (input: PolicyInput) => Policy;
|
|
27
|
+
/**
|
|
28
|
+
* Holds a policy's declared scope order against the scope type's own key list, both directions and
|
|
29
|
+
* in position — the checkable artefact that stops a key added alphabetically from silently
|
|
30
|
+
* reordering the lattice. Call it once in a test beside the type.
|
|
31
|
+
*/
|
|
32
|
+
export declare const assertScopeOrder: (policy: {
|
|
33
|
+
scopeKeys: readonly string[];
|
|
34
|
+
}, expected: readonly string[]) => void;
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Policy, PolicyKindSpec, PolicyRule, PolicyTierSpec } from './types.js';
|
|
2
|
+
export type PolicyInput = {
|
|
3
|
+
/** What policy may constrain. A kind with no reader is an inert knob; a reader with no kind cannot be authored. */
|
|
4
|
+
kinds: readonly (PolicyKindSpec | string)[];
|
|
5
|
+
/**
|
|
6
|
+
* May the overriding tier beat a rule that does not say either way?
|
|
7
|
+
*
|
|
8
|
+
* A Medusa storefront says yes (`rule.overridable !== false`), musa says no (`&& winner.overridable`).
|
|
9
|
+
* Neither is wrong for the other's business, so it is declared rather than assumed.
|
|
10
|
+
*/
|
|
11
|
+
overridableByDefault?: boolean;
|
|
12
|
+
rules?: readonly PolicyRule[];
|
|
13
|
+
/** VAGUEST first — the weight of a key is derived from where it sits. */
|
|
14
|
+
scopeKeys?: readonly string[];
|
|
15
|
+
/** STRONGEST first. */
|
|
16
|
+
tiers: readonly (PolicyTierSpec | string)[];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Declare a policy: the authorities, the things they may constrain, the subjects they may be aimed
|
|
20
|
+
* at, and — optionally — the rules themselves.
|
|
21
|
+
*
|
|
22
|
+
* A policy with no rules is INERT. Every read falls through to the caller's own constant and says
|
|
23
|
+
* so, which is the posture the mechanism is built for: the loader is generic, the domain limits
|
|
24
|
+
* live in data, and the kernel does nothing at all until someone writes a rule.
|
|
25
|
+
*/
|
|
26
|
+
export declare const definePolicy: (input: PolicyInput) => Policy;
|
|
27
|
+
/**
|
|
28
|
+
* Holds a policy's declared scope order against the scope type's own key list, both directions and
|
|
29
|
+
* in position — the checkable artefact that stops a key added alphabetically from silently
|
|
30
|
+
* reordering the lattice. Call it once in a test beside the type.
|
|
31
|
+
*/
|
|
32
|
+
export declare const assertScopeOrder: (policy: {
|
|
33
|
+
scopeKeys: readonly string[];
|
|
34
|
+
}, expected: readonly string[]) => void;
|
package/dist/define.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/define.ts
|
|
2
|
+
import {
|
|
3
|
+
resolveConstraint,
|
|
4
|
+
specificityOf,
|
|
5
|
+
unsetKindsOf,
|
|
6
|
+
unsupportedScopesOf,
|
|
7
|
+
unusableRulesOf,
|
|
8
|
+
usableRulesOf
|
|
9
|
+
} from "./lattice.js";
|
|
10
|
+
var asTiers = (values) => values.map((value) => typeof value === "string" ? { name: value } : value);
|
|
11
|
+
var asKinds = (values) => values.map((value) => typeof value === "string" ? { kind: value } : value);
|
|
12
|
+
var refuseDuplicates = (values, what) => {
|
|
13
|
+
const seen = new Set;
|
|
14
|
+
for (const value of values) {
|
|
15
|
+
if (seen.has(value)) {
|
|
16
|
+
throw new TypeError(`definePolicy: ${what} "${value}" is declared twice. Which one a rule means would then depend on which was found first.`);
|
|
17
|
+
}
|
|
18
|
+
seen.add(value);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var definePolicy = (input) => {
|
|
22
|
+
const tiers = asTiers(input.tiers);
|
|
23
|
+
const kinds = asKinds(input.kinds);
|
|
24
|
+
if (input.scopeKeys !== undefined && !Array.isArray(input.scopeKeys)) {
|
|
25
|
+
throw new TypeError("definePolicy: `scopeKeys` must be an ARRAY in precedence order (broadest first, narrowest last) — its order is the lattice, and a Set has none. Spell the order; `assertScopeOrder` can hold it against your scope type.");
|
|
26
|
+
}
|
|
27
|
+
const scopeKeys = [...input.scopeKeys ?? []];
|
|
28
|
+
const rules = [...input.rules ?? []];
|
|
29
|
+
if (tiers.length === 0) {
|
|
30
|
+
throw new TypeError("definePolicy: `tiers` is empty. A lattice with no authorities could never decide anything, and every rule written against it would name a tier it does not have.");
|
|
31
|
+
}
|
|
32
|
+
refuseDuplicates(tiers.map((tier) => tier.name), "tier");
|
|
33
|
+
refuseDuplicates(kinds.map((kind) => kind.kind), "kind");
|
|
34
|
+
refuseDuplicates(scopeKeys, "scope key");
|
|
35
|
+
const overriding = tiers.filter((tier) => tier.overrides);
|
|
36
|
+
if (overriding.length > 1) {
|
|
37
|
+
throw new TypeError(`definePolicy: ${overriding.map((tier) => `"${tier.name}"`).join(" and ")} both declare \`overrides\`. The audited escape hatch is one tier or none — two of them race for the same rule.`);
|
|
38
|
+
}
|
|
39
|
+
const lastAuthority = tiers.findLastIndex((tier) => !tier.advisory);
|
|
40
|
+
const firstAdvisory = tiers.findIndex((tier) => tier.advisory);
|
|
41
|
+
if (firstAdvisory !== -1 && firstAdvisory < lastAuthority) {
|
|
42
|
+
throw new TypeError(`definePolicy: advisory tier "${tiers[firstAdvisory]?.name}" is declared stronger than "${tiers[lastAuthority]?.name}". An advisory tier fills silence; one that outranks an authority is not advisory, it is an authority with a reassuring name.`);
|
|
43
|
+
}
|
|
44
|
+
const data = {
|
|
45
|
+
kinds,
|
|
46
|
+
overridableByDefault: input.overridableByDefault ?? true,
|
|
47
|
+
rules,
|
|
48
|
+
scopeKeys,
|
|
49
|
+
tiers
|
|
50
|
+
};
|
|
51
|
+
const journalledOnce = new Set;
|
|
52
|
+
return {
|
|
53
|
+
...data,
|
|
54
|
+
journalledOnce,
|
|
55
|
+
kindOf: (kind) => kinds.find((spec) => spec.kind === kind),
|
|
56
|
+
rank: (tier) => {
|
|
57
|
+
const index = tiers.findIndex((spec) => spec.name === tier);
|
|
58
|
+
return index === -1 ? undefined : index;
|
|
59
|
+
},
|
|
60
|
+
resetJournal: () => journalledOnce.clear(),
|
|
61
|
+
resolve: (kind, query, given) => resolveConstraint(data, kind, query, given),
|
|
62
|
+
specificity: (scope) => specificityOf(scopeKeys, scope),
|
|
63
|
+
tierOf: (tier) => tiers.find((spec) => spec.name === tier),
|
|
64
|
+
unsetKinds: (given = rules) => unsetKindsOf(given, kinds),
|
|
65
|
+
unsupportedScopes: (given = rules) => unsupportedScopesOf(given, scopeKeys),
|
|
66
|
+
unusableRules: (given = rules) => unusableRulesOf(given, data),
|
|
67
|
+
usableRules: (given = rules) => usableRulesOf(given, data)
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
var assertScopeOrder = (policy, expected) => {
|
|
71
|
+
const missing = expected.filter((key) => !policy.scopeKeys.includes(key));
|
|
72
|
+
const extra = policy.scopeKeys.filter((key) => !expected.includes(key));
|
|
73
|
+
if (missing.length > 0 || extra.length > 0) {
|
|
74
|
+
throw new Error(`assertScopeOrder: the declared order and the scope type disagree — ${[...missing.map((k) => `"${k}" is in the type and not the order`), ...extra.map((k) => `"${k}" is in the order and not the type`)].join("; ")}.`);
|
|
75
|
+
}
|
|
76
|
+
for (const [index, key] of expected.entries()) {
|
|
77
|
+
if (policy.scopeKeys[index] !== key) {
|
|
78
|
+
throw new Error(`assertScopeOrder: position ${String(index)} holds "${policy.scopeKeys[index] ?? ""}" where the expected order holds "${key}" — the order is the lattice; if the type's order changed, change the declaration deliberately.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
export {
|
|
83
|
+
assertScopeOrder,
|
|
84
|
+
definePolicy
|
|
85
|
+
};
|