foldkit-guard 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +659 -2
- package/dist/agent.d.mts +39 -0
- package/dist/agent.d.mts.map +1 -0
- package/dist/agent.mjs +56 -0
- package/dist/agent.mjs.map +1 -0
- package/dist/attach-COgJIox7.mjs +272 -0
- package/dist/attach-COgJIox7.mjs.map +1 -0
- package/dist/attach-q8S9tsHk.d.mts +435 -0
- package/dist/attach-q8S9tsHk.d.mts.map +1 -0
- package/dist/condition-C6Czs2Xn.mjs +213 -0
- package/dist/condition-C6Czs2Xn.mjs.map +1 -0
- package/dist/drizzle.d.mts +20 -0
- package/dist/drizzle.d.mts.map +1 -0
- package/dist/drizzle.mjs +97 -0
- package/dist/drizzle.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +150 -0
- package/dist/index.mjs.map +1 -0
- package/dist/may-DK-2lomO.mjs +76 -0
- package/dist/may-DK-2lomO.mjs.map +1 -0
- package/dist/placed-Bs_mnBTF.mjs +39 -0
- package/dist/placed-Bs_mnBTF.mjs.map +1 -0
- package/dist/server.d.mts +125 -0
- package/dist/server.d.mts.map +1 -0
- package/dist/server.mjs +522 -0
- package/dist/server.mjs.map +1 -0
- package/dist/sync.d.mts +81 -0
- package/dist/sync.d.mts.map +1 -0
- package/dist/sync.mjs +174 -0
- package/dist/sync.mjs.map +1 -0
- package/package.json +101 -3
package/dist/agent.d.mts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { k as MayAnswer, l as InteractionKey } from "./attach-q8S9tsHk.mjs";
|
|
2
|
+
import { RemoteData } from "foldkit-remote";
|
|
3
|
+
//#region src/agent.d.ts
|
|
4
|
+
/** A row read with `may`, as `Data.get(Row, id).read(model)` gives it. */
|
|
5
|
+
type MayRow = RemoteData<{
|
|
6
|
+
readonly may: MayAnswer;
|
|
7
|
+
}>;
|
|
8
|
+
export declare const GuardAgent: {
|
|
9
|
+
/**
|
|
10
|
+
* A capability's `authorize`: the row the request names, read from the
|
|
11
|
+
* Model, must let the principal do `interaction`. A row not read yet, or
|
|
12
|
+
* whose read failed, refuses: there is no answer to go on. A refusal its
|
|
13
|
+
* rule explained says so, as the agent's error.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* authorize: GuardAgent.authorize(Archive.key, ({ input, model }) =>
|
|
17
|
+
* Data.get(TodoRow, input.id).read(model),
|
|
18
|
+
* )
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
authorize: <Input, Model>(interaction: "Update" | "Delete" | InteractionKey, row: (request: {
|
|
22
|
+
readonly input: Input;
|
|
23
|
+
readonly model: Model;
|
|
24
|
+
}) => MayRow) => (request: {
|
|
25
|
+
readonly input: Input;
|
|
26
|
+
readonly model: Model;
|
|
27
|
+
}) => boolean | {
|
|
28
|
+
readonly allowed: false;
|
|
29
|
+
readonly reason: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A capability's `available`, for one that acts on a row the Model alone
|
|
33
|
+
* names (the todo that is selected): offered only while that row lets the
|
|
34
|
+
* principal do `interaction`, so a visitor is not offered it at all.
|
|
35
|
+
*/
|
|
36
|
+
available: <Model>(interaction: "Update" | "Delete" | InteractionKey, row: (model: Model) => MayRow) => (model: Model) => boolean;
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
//# sourceMappingURL=agent.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.mts","names":[],"sources":["../src/agent.ts"],"mappings":";;;;KAaK,SAAS;WAAsB,KAAK;;qBA6B5B;;;;;;;;;;;;;EAcR,YAAA,OAAO,OAAK,mCACwB,gBAAc,MAC3C;aAAoB,OAAO;aAAgB,OAAO;QAAY,YAErE;aACU,OAAO;aACP,OAAO;;aACO;aAAyB;;;;;;;EASjD,YAAA,OAAK,mCAAqC,gBAAc,MAAQ,OAAO,UAAU,YACjF,OAAO"}
|
package/dist/agent.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { t as May } from "./may-DK-2lomO.mjs";
|
|
2
|
+
import { Option } from "effect";
|
|
3
|
+
import { RemoteData } from "foldkit-remote";
|
|
4
|
+
//#region src/agent.ts
|
|
5
|
+
/**
|
|
6
|
+
* `foldkit-guard/agent`: a `foldkit-agent` capability's `authorize` and
|
|
7
|
+
* `available`, read from the `may` the server answered for a row the Model
|
|
8
|
+
* holds. The client has no rule to run, and is not where a write is checked:
|
|
9
|
+
* this keeps an agent from offering or attempting what the server would
|
|
10
|
+
* refuse, and the Message's mutation is checked on the server all the same.
|
|
11
|
+
*/
|
|
12
|
+
/** The answer, while the row's value is there to read: refreshing or not. */
|
|
13
|
+
const answered = (row) => RemoteData.match(row, {
|
|
14
|
+
Initial: () => Option.none(),
|
|
15
|
+
Loading: () => Option.none(),
|
|
16
|
+
Ready: ({ may }) => Option.some(may),
|
|
17
|
+
Refreshing: ({ may }) => Option.some(may),
|
|
18
|
+
Failed: () => Option.none(),
|
|
19
|
+
NotFound: () => Option.none()
|
|
20
|
+
});
|
|
21
|
+
/** Allowed, refused, or refused with the `explain` its rule gave: what `authorize` answers. */
|
|
22
|
+
const decided = (row, interaction) => Option.match(answered(row), {
|
|
23
|
+
onNone: () => false,
|
|
24
|
+
onSome: (may) => May.can(may, interaction) || Option.match(May.reason(may, interaction), {
|
|
25
|
+
onNone: () => false,
|
|
26
|
+
onSome: (reason) => ({
|
|
27
|
+
allowed: false,
|
|
28
|
+
reason
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
});
|
|
32
|
+
const GuardAgent = {
|
|
33
|
+
/**
|
|
34
|
+
* A capability's `authorize`: the row the request names, read from the
|
|
35
|
+
* Model, must let the principal do `interaction`. A row not read yet, or
|
|
36
|
+
* whose read failed, refuses: there is no answer to go on. A refusal its
|
|
37
|
+
* rule explained says so, as the agent's error.
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* authorize: GuardAgent.authorize(Archive.key, ({ input, model }) =>
|
|
41
|
+
* Data.get(TodoRow, input.id).read(model),
|
|
42
|
+
* )
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
authorize: (interaction, row) => (request) => decided(row(request), interaction),
|
|
46
|
+
/**
|
|
47
|
+
* A capability's `available`, for one that acts on a row the Model alone
|
|
48
|
+
* names (the todo that is selected): offered only while that row lets the
|
|
49
|
+
* principal do `interaction`, so a visitor is not offered it at all.
|
|
50
|
+
*/
|
|
51
|
+
available: (interaction, row) => (model) => decided(row(model), interaction) === true
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
export { GuardAgent };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=agent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.mjs","names":[],"sources":["../src/agent.ts"],"sourcesContent":["/**\n * `foldkit-guard/agent`: a `foldkit-agent` capability's `authorize` and\n * `available`, read from the `may` the server answered for a row the Model\n * holds. The client has no rule to run, and is not where a write is checked:\n * this keeps an agent from offering or attempting what the server would\n * refuse, and the Message's mutation is checked on the server all the same.\n */\nimport { Option } from 'effect'\nimport { RemoteData } from 'foldkit-remote'\nimport type { InteractionKey } from './attach.js'\nimport { May, type MayAnswer } from './may.js'\n\n/** A row read with `may`, as `Data.get(Row, id).read(model)` gives it. */\ntype MayRow = RemoteData<{ readonly may: MayAnswer }>\n\n/** The answer, while the row's value is there to read: refreshing or not. */\nconst answered = (row: MayRow): Option.Option<MayAnswer> =>\n RemoteData.match(row, {\n Initial: () => Option.none(),\n Loading: () => Option.none(),\n Ready: ({ may }) => Option.some(may),\n Refreshing: ({ may }) => Option.some(may),\n // Not the last answer known good: a refusal may be why the read failed.\n Failed: () => Option.none(),\n NotFound: () => Option.none(),\n })\n\n/** Allowed, refused, or refused with the `explain` its rule gave: what `authorize` answers. */\nconst decided = (\n row: MayRow,\n interaction: 'Update' | 'Delete' | InteractionKey,\n): boolean | { readonly allowed: false; readonly reason: string } =>\n Option.match(answered(row), {\n onNone: () => false,\n onSome: may =>\n May.can(may, interaction) ||\n Option.match(May.reason(may, interaction), {\n onNone: () => false,\n onSome: reason => ({ allowed: false as const, reason }),\n }),\n })\n\nexport const GuardAgent = {\n /**\n * A capability's `authorize`: the row the request names, read from the\n * Model, must let the principal do `interaction`. A row not read yet, or\n * whose read failed, refuses: there is no answer to go on. A refusal its\n * rule explained says so, as the agent's error.\n *\n * ```ts\n * authorize: GuardAgent.authorize(Archive.key, ({ input, model }) =>\n * Data.get(TodoRow, input.id).read(model),\n * )\n * ```\n */\n authorize:\n <Input, Model>(\n interaction: 'Update' | 'Delete' | InteractionKey,\n row: (request: { readonly input: Input; readonly model: Model }) => MayRow,\n ) =>\n (request: {\n readonly input: Input\n readonly model: Model\n }): boolean | { readonly allowed: false; readonly reason: string } =>\n decided(row(request), interaction),\n\n /**\n * A capability's `available`, for one that acts on a row the Model alone\n * names (the todo that is selected): offered only while that row lets the\n * principal do `interaction`, so a visitor is not offered it at all.\n */\n available:\n <Model>(interaction: 'Update' | 'Delete' | InteractionKey, row: (model: Model) => MayRow) =>\n (model: Model): boolean =>\n decided(row(model), interaction) === true,\n}\n"],"mappings":";;;;;;;;;;;;AAgBA,MAAM,YAAY,QAChB,WAAW,MAAM,KAAK;CACpB,eAAe,OAAO,KAAK;CAC3B,eAAe,OAAO,KAAK;CAC3B,QAAQ,EAAE,UAAU,OAAO,KAAK,GAAG;CACnC,aAAa,EAAE,UAAU,OAAO,KAAK,GAAG;CAExC,cAAc,OAAO,KAAK;CAC1B,gBAAgB,OAAO,KAAK;AAC9B,CAAC;;AAGH,MAAM,WACJ,KACA,gBAEA,OAAO,MAAM,SAAS,GAAG,GAAG;CAC1B,cAAc;CACd,SAAQ,QACN,IAAI,IAAI,KAAK,WAAW,KACxB,OAAO,MAAM,IAAI,OAAO,KAAK,WAAW,GAAG;EACzC,cAAc;EACd,SAAQ,YAAW;GAAE,SAAS;GAAgB;EAAO;CACvD,CAAC;AACL,CAAC;AAEH,MAAa,aAAa;;;;;;;;;;;;;CAaxB,YAEI,aACA,SAED,YAIC,QAAQ,IAAI,OAAO,GAAG,WAAW;;;;;;CAOrC,YACU,aAAmD,SAC1D,UACC,QAAQ,IAAI,KAAK,GAAG,WAAW,MAAM;AAC3C"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { Match, Option } from "effect";
|
|
2
|
+
import { Entity, Query, ReadFilter } from "foldkit-entity";
|
|
3
|
+
import { Metadata } from "foldkit-metadata";
|
|
4
|
+
//#region src/attach.ts
|
|
5
|
+
/**
|
|
6
|
+
* Attaching a guard set's guards to an Entity, per surface and interaction,
|
|
7
|
+
* as metadata only `foldkit-guard` reads. See `guard-DESIGN.md` §3.2.
|
|
8
|
+
*/
|
|
9
|
+
/** The interactions every row has, and the ones a relation member has. */
|
|
10
|
+
const builtIn = [
|
|
11
|
+
"Read",
|
|
12
|
+
"Create",
|
|
13
|
+
"Update",
|
|
14
|
+
"Delete",
|
|
15
|
+
"Write",
|
|
16
|
+
"Link",
|
|
17
|
+
"Unlink"
|
|
18
|
+
];
|
|
19
|
+
const Interaction = {
|
|
20
|
+
/** An interaction beyond Read, Create, Update and Delete, such as `Archive`. */
|
|
21
|
+
make: (name) => {
|
|
22
|
+
if (builtIn.includes(name)) throw new Error(`[foldkit-guard] Interaction.make: "${name}" is an interaction already`);
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
name,
|
|
25
|
+
key: name
|
|
26
|
+
});
|
|
27
|
+
} };
|
|
28
|
+
const record = () => Object.create(null);
|
|
29
|
+
const appendSurface = (into, interaction, added) => {
|
|
30
|
+
const before = into[interaction];
|
|
31
|
+
into[interaction] = before === void 0 ? added : {
|
|
32
|
+
rules: [...before.rules, ...added.rules],
|
|
33
|
+
deny: Option.orElse(added.deny, () => before.deny)
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const appendSurfaces = (into, added) => {
|
|
37
|
+
for (const [interaction, surface] of Object.entries(added)) appendSurface(into, interaction, surface);
|
|
38
|
+
};
|
|
39
|
+
const freezeSurfaces = (surfaces) => {
|
|
40
|
+
for (const key of Object.keys(surfaces)) Object.freeze(surfaces[key]);
|
|
41
|
+
return Object.freeze(surfaces);
|
|
42
|
+
};
|
|
43
|
+
/** Later attachments append to each surface's disjunction and replace its refusal. */
|
|
44
|
+
const mergeAttachments = (attachments) => {
|
|
45
|
+
const row = record();
|
|
46
|
+
const members = record();
|
|
47
|
+
const relations = record();
|
|
48
|
+
const always = [];
|
|
49
|
+
for (const attachment of attachments) {
|
|
50
|
+
appendSurfaces(row, attachment.row);
|
|
51
|
+
for (const [key, surfaces] of Object.entries(attachment.members)) appendSurfaces(members[key] ??= record(), surfaces);
|
|
52
|
+
for (const [key, surfaces] of Object.entries(attachment.relations)) appendSurfaces(relations[key] ??= record(), surfaces);
|
|
53
|
+
always.push(...attachment.always);
|
|
54
|
+
}
|
|
55
|
+
const { set, unguarded } = attachments[0];
|
|
56
|
+
return Object.freeze({
|
|
57
|
+
set,
|
|
58
|
+
unguarded,
|
|
59
|
+
row: Object.freeze(row),
|
|
60
|
+
members: freezeSurfaces(members),
|
|
61
|
+
relations: freezeSurfaces(relations),
|
|
62
|
+
always: Object.freeze(always)
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
const showRule = (rule) => Match.valueTags(rule, {
|
|
66
|
+
PrincipalGuard: ({ name }) => name,
|
|
67
|
+
RowGuard: ({ name }) => name,
|
|
68
|
+
All: ({ rules }) => `all(${rules.map(showRule).join(", ")})`,
|
|
69
|
+
Inherit: ({ relation }) => `inherit(${relation})`
|
|
70
|
+
});
|
|
71
|
+
const showDeny = (deny) => Match.valueTags(deny, {
|
|
72
|
+
NotFound: () => "not_found",
|
|
73
|
+
Omit: () => "omit",
|
|
74
|
+
Redact: ({ value }) => `redact(${JSON.stringify(value)})`,
|
|
75
|
+
Forbidden: () => "forbidden",
|
|
76
|
+
Explain: ({ reason }) => `explain(${JSON.stringify(reason)})`
|
|
77
|
+
});
|
|
78
|
+
const notFound = Object.freeze({ _tag: "NotFound" });
|
|
79
|
+
const omit = Object.freeze({ _tag: "Omit" });
|
|
80
|
+
const forbidden = Object.freeze({ _tag: "Forbidden" });
|
|
81
|
+
/** The refusal a surface takes when none was chosen. */
|
|
82
|
+
const defaultDeny = (target) => target.interaction === "Read" ? Match.valueTags(target, {
|
|
83
|
+
Row: () => notFound,
|
|
84
|
+
Member: () => omit,
|
|
85
|
+
Relation: () => omit
|
|
86
|
+
}) : forbidden;
|
|
87
|
+
/** The guards a surface carries; `none` where it falls to the set's `unguarded`. */
|
|
88
|
+
const surfaceOf = (attachment, target) => Match.valueTags(target, {
|
|
89
|
+
Row: ({ interaction }) => Option.fromUndefinedOr(attachment.row[interaction]),
|
|
90
|
+
Member: ({ member, interaction }) => {
|
|
91
|
+
const surfaces = attachment.members[member];
|
|
92
|
+
if (surfaces === void 0) return Option.none();
|
|
93
|
+
const own = surfaces[interaction];
|
|
94
|
+
return Option.fromUndefinedOr(own === void 0 && interaction === "Create" ? surfaces["Update"] : own);
|
|
95
|
+
},
|
|
96
|
+
Relation: ({ relation, interaction }) => Option.fromUndefinedOr(attachment.relations[relation]?.[interaction])
|
|
97
|
+
});
|
|
98
|
+
/** Whether who may read the rows depends on who is asking: a row Read rule, or `always`. */
|
|
99
|
+
const readRowsRuled = (attachment) => attachment.row["Read"] !== void 0 || attachment.always.length > 0;
|
|
100
|
+
/** Whether `member`, a field or a relation, has a Read rule of its own. */
|
|
101
|
+
const readRuled = (attachment, member) => Option.isSome(surfaceOf(attachment, {
|
|
102
|
+
_tag: "Member",
|
|
103
|
+
member,
|
|
104
|
+
interaction: "Read"
|
|
105
|
+
})) || Option.isSome(surfaceOf(attachment, {
|
|
106
|
+
_tag: "Relation",
|
|
107
|
+
relation: member,
|
|
108
|
+
interaction: "Read"
|
|
109
|
+
}));
|
|
110
|
+
const key = Metadata.key("foldkit-guard/attachment", {
|
|
111
|
+
merge: (attachments) => [mergeAttachments(attachments)],
|
|
112
|
+
summarize: (attachment) => linesOf(attachment).map(([where, rule]) => `${where}: ${rule}`).join("; ")
|
|
113
|
+
});
|
|
114
|
+
/** The rows of an attachment's matrix: each surface it decides, and how. */
|
|
115
|
+
const linesOf = (attachment) => {
|
|
116
|
+
const open = attachment.unguarded === "allow" ? "open" : "closed";
|
|
117
|
+
const show = (target) => Option.match(surfaceOf(attachment, target), {
|
|
118
|
+
onNone: () => open,
|
|
119
|
+
onSome: ({ rules, deny }) => `${rules.length === 0 ? "nobody" : rules.map(showRule).join(" | ")} → ${showDeny(Option.getOrElse(deny, () => defaultDeny(target)))}`
|
|
120
|
+
});
|
|
121
|
+
const interactions = [
|
|
122
|
+
"Read",
|
|
123
|
+
"Create",
|
|
124
|
+
"Update",
|
|
125
|
+
"Delete",
|
|
126
|
+
...Object.keys(attachment.row).filter((interaction) => !builtIn.includes(interaction))
|
|
127
|
+
];
|
|
128
|
+
return [
|
|
129
|
+
...attachment.always.length > 0 ? [["always", attachment.always.map(showRule).join(" & ")]] : [],
|
|
130
|
+
...interactions.map((interaction) => [interaction, show({
|
|
131
|
+
_tag: "Row",
|
|
132
|
+
interaction
|
|
133
|
+
})]),
|
|
134
|
+
...Object.keys(attachment.members).flatMap((member) => [
|
|
135
|
+
"Read",
|
|
136
|
+
"Create",
|
|
137
|
+
"Update"
|
|
138
|
+
].flatMap((interaction) => {
|
|
139
|
+
const target = {
|
|
140
|
+
_tag: "Member",
|
|
141
|
+
member,
|
|
142
|
+
interaction
|
|
143
|
+
};
|
|
144
|
+
return Option.isSome(surfaceOf(attachment, target)) ? [[`${member}.${interaction}`, show(target)]] : [];
|
|
145
|
+
})),
|
|
146
|
+
...Object.entries(attachment.relations).flatMap(([relation, surfaces]) => Object.keys(surfaces).map((interaction) => [`${relation}.${interaction}`, show({
|
|
147
|
+
_tag: "Relation",
|
|
148
|
+
relation,
|
|
149
|
+
interaction
|
|
150
|
+
})]))
|
|
151
|
+
];
|
|
152
|
+
};
|
|
153
|
+
const isOn = (slot) => !Array.isArray(slot);
|
|
154
|
+
const surfaceFrom = (slot) => isOn(slot) ? {
|
|
155
|
+
rules: slot.rules,
|
|
156
|
+
deny: Option.some(slot.deny)
|
|
157
|
+
} : {
|
|
158
|
+
rules: Object.freeze([...slot]),
|
|
159
|
+
deny: Option.none()
|
|
160
|
+
};
|
|
161
|
+
/** Throws for a row guard over another Entity that shares the target's name. */
|
|
162
|
+
const checkRules = (set, entity, where, rules) => {
|
|
163
|
+
for (const rule of rules) Match.valueTags(rule, {
|
|
164
|
+
PrincipalGuard: () => {},
|
|
165
|
+
RowGuard: (guard) => {
|
|
166
|
+
if (!Entity.same(guard.entity, entity)) throw new Error(`[foldkit-guard] ${set}.attach: ${where} names ${guard.name}, a guard over another Entity named ${entity.name}`);
|
|
167
|
+
},
|
|
168
|
+
All: ({ rules }) => checkRules(set, entity, where, rules),
|
|
169
|
+
Inherit: () => {}
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
const surfacesFrom = (set, entity, where, slots) => {
|
|
173
|
+
const surfaces = record();
|
|
174
|
+
const ordered = Object.entries(slots).sort(([left], [right]) => Number(right === "Write") - Number(left === "Write"));
|
|
175
|
+
for (const [interaction, slot] of ordered) {
|
|
176
|
+
if (slot === void 0) continue;
|
|
177
|
+
const added = surfaceFrom(slot);
|
|
178
|
+
checkRules(set, entity, `${where}${interaction}`, added.rules);
|
|
179
|
+
for (const each of interaction === "Write" ? [
|
|
180
|
+
"Create",
|
|
181
|
+
"Update",
|
|
182
|
+
"Delete"
|
|
183
|
+
] : [interaction]) appendSurface(surfaces, each, added);
|
|
184
|
+
}
|
|
185
|
+
return Object.freeze(surfaces);
|
|
186
|
+
};
|
|
187
|
+
const nested = (set, entity, given) => {
|
|
188
|
+
const surfaces = record();
|
|
189
|
+
for (const [member, slots] of Object.entries(given)) if (slots !== void 0) surfaces[member] = surfacesFrom(set, entity, `${member}.`, slots);
|
|
190
|
+
return Object.freeze(surfaces);
|
|
191
|
+
};
|
|
192
|
+
/** A row guard's own fields, and the relation an inherited Read follows. */
|
|
193
|
+
const membersRead = (rule) => Match.valueTags(rule, {
|
|
194
|
+
PrincipalGuard: () => [],
|
|
195
|
+
RowGuard: ({ query }) => Query.dependencies(query).predicate.map((field) => field.key),
|
|
196
|
+
All: ({ rules }) => rules.flatMap(membersRead),
|
|
197
|
+
Inherit: ({ relation }) => [relation]
|
|
198
|
+
});
|
|
199
|
+
const attached = (entity) => Option.fromUndefinedOr(key.get(entity.metadata)[0]);
|
|
200
|
+
const Attaching = {
|
|
201
|
+
/**
|
|
202
|
+
* Attaches guards to an Entity, per surface and interaction. A surface's list
|
|
203
|
+
* is a disjunction; `always` is ANDed after it. Attaching twice appends to a
|
|
204
|
+
* surface's list and replaces its refusal.
|
|
205
|
+
*/
|
|
206
|
+
attach: (G, config) => (entity) => {
|
|
207
|
+
Option.map(attached(entity), ({ set }) => {
|
|
208
|
+
if (set !== G.name) throw new Error(`[foldkit-guard] ${G.name}.attach: ${entity.name} carries the guards of ${set} already`);
|
|
209
|
+
});
|
|
210
|
+
const always = config.always ?? [];
|
|
211
|
+
checkRules(G.name, entity, "always", always);
|
|
212
|
+
const attachment = Object.freeze({
|
|
213
|
+
set: G.name,
|
|
214
|
+
unguarded: G.unguarded,
|
|
215
|
+
row: surfacesFrom(G.name, entity, "", {
|
|
216
|
+
...config.row,
|
|
217
|
+
...config.interactions
|
|
218
|
+
}),
|
|
219
|
+
members: nested(G.name, entity, config.members ?? {}),
|
|
220
|
+
relations: nested(G.name, entity, config.relations ?? {}),
|
|
221
|
+
always: Object.freeze([...always])
|
|
222
|
+
});
|
|
223
|
+
const filtered = [...new Set([...attachment.row["Read"]?.rules ?? [], ...attachment.always].flatMap(membersRead))];
|
|
224
|
+
return entity.pipe(Entity.annotate(Metadata.combine([key.of(attachment), ReadFilter.of(filtered)])));
|
|
225
|
+
},
|
|
226
|
+
/** The guards attached to `entity`, every attachment merged. */
|
|
227
|
+
attached,
|
|
228
|
+
/** Every rule must allow, as one rule. Principal and row guards may mix. */
|
|
229
|
+
all: (...rules) => Object.freeze({
|
|
230
|
+
_tag: "All",
|
|
231
|
+
rules: Object.freeze([...rules])
|
|
232
|
+
}),
|
|
233
|
+
/** This row's Read is the Read of the row its `one` relation points at. */
|
|
234
|
+
inherit: (relation) => Object.freeze({
|
|
235
|
+
_tag: "Inherit",
|
|
236
|
+
relation
|
|
237
|
+
}),
|
|
238
|
+
/** A surface's guards with a refusal other than its default. */
|
|
239
|
+
on: (rules, deny) => Object.freeze({
|
|
240
|
+
_tag: "On",
|
|
241
|
+
rules: Object.freeze([...rules]),
|
|
242
|
+
deny
|
|
243
|
+
}),
|
|
244
|
+
/** A hidden member reads as `value`, typed by the member's schema. */
|
|
245
|
+
redact: (value) => Object.freeze({
|
|
246
|
+
_tag: "Redact",
|
|
247
|
+
value
|
|
248
|
+
}),
|
|
249
|
+
/** A refused write tells the caller `reason`. */
|
|
250
|
+
explain: (reason) => Object.freeze({
|
|
251
|
+
_tag: "Explain",
|
|
252
|
+
reason
|
|
253
|
+
}),
|
|
254
|
+
/**
|
|
255
|
+
* The members a row interaction's guards read, `always` included: a change
|
|
256
|
+
* to one can change whether a principal may. A relation an inherited Read
|
|
257
|
+
* follows counts.
|
|
258
|
+
*/
|
|
259
|
+
guardFields: (entity, interaction) => Option.match(attached(entity), {
|
|
260
|
+
onNone: () => [],
|
|
261
|
+
onSome: (attachment) => [...new Set([...attachment.row[interaction]?.rules ?? [], ...attachment.always].flatMap(membersRead))]
|
|
262
|
+
}),
|
|
263
|
+
/** Who may do what, one line per surface, for review and for a committed snapshot. */
|
|
264
|
+
matrix: (entities) => entities.flatMap((entity) => Option.match(attached(entity), {
|
|
265
|
+
onNone: () => [`${entity.name}: no guards`],
|
|
266
|
+
onSome: (attachment) => linesOf(attachment).map(([where, rule]) => `${entity.name}.${where}: ${rule}`)
|
|
267
|
+
})).join("\n")
|
|
268
|
+
};
|
|
269
|
+
//#endregion
|
|
270
|
+
export { readRowsRuled as a, defaultDeny as i, Interaction as n, readRuled as o, attached as r, surfaceOf as s, Attaching as t };
|
|
271
|
+
|
|
272
|
+
//# sourceMappingURL=attach-COgJIox7.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attach-COgJIox7.mjs","names":[],"sources":["../src/attach.ts"],"sourcesContent":["/**\n * Attaching a guard set's guards to an Entity, per surface and interaction,\n * as metadata only `foldkit-guard` reads. See `guard-DESIGN.md` §3.2.\n */\nimport { Match, Option, type Schema } from 'effect'\nimport { Entity, Query, ReadFilter, type AnyEntity } from 'foldkit-entity'\nimport { Metadata } from 'foldkit-metadata'\nimport type { PrincipalGuard, RowGuard } from './index.js'\n\ndeclare const InteractionBrand: unique symbol\n\n/** An interaction the application names, used as a computed key: `[Archive.key]`. */\nexport type InteractionKey<Name extends string = string> = Name & {\n readonly [InteractionBrand]: Name\n}\n\nexport interface Interaction<Name extends string> {\n readonly name: Name\n readonly key: InteractionKey<Name>\n}\n\n/** The interactions every row has, and the ones a relation member has. */\nconst builtIn = ['Read', 'Create', 'Update', 'Delete', 'Write', 'Link', 'Unlink'] as const\n\nexport const Interaction = {\n /** An interaction beyond Read, Create, Update and Delete, such as `Archive`. */\n make: <const Name extends string>(name: Name): Interaction<Name> => {\n if ((builtIn as ReadonlyArray<string>).includes(name)) {\n throw new Error(`[foldkit-guard] Interaction.make: \"${name}\" is an interaction already`)\n }\n return Object.freeze({ name, key: name as InteractionKey<Name> })\n },\n}\n\n/** Every guard of `Rules` must allow, as one guard: `Guards.all(signedIn, isOwner)`. */\nexport interface All<Rules> {\n readonly _tag: 'All'\n readonly rules: ReadonlyArray<Rules>\n}\n\n/** This row's Read is the Read of the row its `one` relation points at. */\nexport interface Inherit<Relation extends string> {\n readonly _tag: 'Inherit'\n readonly relation: Relation\n}\n\n/** What a member hidden by its Read guards reads as instead of its value. */\nexport interface Redact<A> {\n readonly _tag: 'Redact'\n readonly value: A\n}\n\n/** A refused write's sentence, for the application's own users. */\nexport interface Explain {\n readonly _tag: 'Explain'\n readonly reason: string\n}\n\n/**\n * What a refusal looks like. The defaults need no constructor: a row Read\n * answers `NotFound`, a member or relation Read `Omit`, a write `Forbidden`.\n */\nexport type Deny =\n | { readonly _tag: 'NotFound' }\n | { readonly _tag: 'Omit' }\n | Redact<unknown>\n | { readonly _tag: 'Forbidden' }\n | Explain\n\n/** A surface's guards with a refusal other than its default. */\nexport interface On<Rules, D extends Deny> {\n readonly _tag: 'On'\n readonly rules: ReadonlyArray<Rules>\n readonly deny: D\n}\n\n/** Anything a surface's list may hold, of any guard set. */\nexport type AnyRule =\n PrincipalGuard<any, string> | RowGuard<any, AnyEntity, string> | All<any> | Inherit<string>\n\n/**\n * What a surface of the Entity named `Name` accepts from the guard set `Set`:\n * its principal guards, its row guards over that Entity, and conjunctions of\n * both. A row guard over another Entity, or a guard of another set, is refused.\n */\nexport type RuleFor<User, Set extends string, Name extends string> =\n | PrincipalGuard<User, Set>\n | RowGuard<User, Entity<Name, any, any, any>, Set>\n | All<RuleFor<User, Set, Name>>\n\ntype Slot<Rule, D extends Deny> = ReadonlyArray<Rule> | On<Rule, D>\n\ntype WriteSlot<Rule> = Slot<Rule, Explain>\n\ntype OneRelations<E extends AnyEntity> = {\n [K in keyof E['relations']]: E['relations'][K] extends { readonly cardinality: 'one' } ? K : never\n}[keyof E['relations']] &\n string\n\ntype MemberKeys<E extends AnyEntity> = (keyof E['fields'] | keyof E['derived']) & string\n\ntype TypeOf<E extends AnyEntity, K extends keyof E['members']> = E['members'][K]['schema']['Type']\n\n/** What `Guards.attach` takes for the Entity `E`, every key checked against it. */\nexport type Attach<User, Set extends string, E extends AnyEntity> = {\n readonly row?: {\n readonly Read?: ReadonlyArray<RuleFor<User, Set, E['name']> | Inherit<OneRelations<E>>>\n readonly Create?: WriteSlot<RuleFor<User, Set, E['name']>>\n readonly Update?: WriteSlot<RuleFor<User, Set, E['name']>>\n readonly Delete?: WriteSlot<RuleFor<User, Set, E['name']>>\n /** The same guards on Create, Update and Delete. */\n readonly Write?: WriteSlot<RuleFor<User, Set, E['name']>>\n }\n /**\n * The row's interactions the application named, by key:\n * `{ [Archive.key]: [G.isAdmin] }`. Apart from `row`, whose `Read` may\n * inherit: a literal with a computed key is typed as an index signature,\n * which `Read`'s rules would have to fit too.\n */\n readonly interactions?: {\n readonly [K: InteractionKey]: WriteSlot<RuleFor<User, Set, E['name']>>\n }\n /** Narrow a field or derived member: a member is reachable only if its row is. */\n readonly members?: {\n readonly [K in MemberKeys<E>]?: {\n readonly Read?: Slot<RuleFor<User, Set, E['name']>, Redact<TypeOf<E, K>>>\n /** When absent, `Update`'s guards govern setting the member on creation too. */\n readonly Create?: WriteSlot<RuleFor<User, Set, E['name']>>\n readonly Update?: WriteSlot<RuleFor<User, Set, E['name']>>\n }\n }\n readonly relations?: {\n readonly [K in keyof E['relations']]?: {\n readonly Read?: ReadonlyArray<RuleFor<User, Set, E['name']>>\n readonly Link?: WriteSlot<RuleFor<User, Set, E['name']>>\n readonly Unlink?: WriteSlot<RuleFor<User, Set, E['name']>>\n }\n }\n /** ANDed with every surface's guards, and never out-voted by them. */\n readonly always?: ReadonlyArray<RuleFor<User, Set, E['name']>>\n}\n\n/** One surface's guards as attached: a disjunction, and a refusal if one was chosen. */\nexport interface Surface {\n readonly rules: ReadonlyArray<AnyRule>\n readonly deny: Option.Option<Deny>\n}\n\ntype Surfaces = Readonly<Record<string, Surface>>\n\n/** An Entity's guards, every attachment merged. */\nexport interface Attachment {\n readonly set: string\n readonly unguarded: 'allow' | 'deny'\n readonly row: Surfaces\n readonly members: Readonly<Record<string, Surfaces>>\n readonly relations: Readonly<Record<string, Surfaces>>\n readonly always: ReadonlyArray<AnyRule>\n}\n\n/** Which surface a rule is asked about. */\ntype Target =\n | { readonly _tag: 'Row'; readonly interaction: string }\n | { readonly _tag: 'Member'; readonly member: string; readonly interaction: string }\n | { readonly _tag: 'Relation'; readonly relation: string; readonly interaction: string }\n\nconst record = <A>(): Record<string, A> => Object.create(null)\n\nconst appendSurface = (into: Record<string, Surface>, interaction: string, added: Surface) => {\n const before = into[interaction]\n into[interaction] =\n before === undefined\n ? added\n : {\n rules: [...before.rules, ...added.rules],\n deny: Option.orElse(added.deny, () => before.deny),\n }\n}\n\nconst appendSurfaces = (into: Record<string, Surface>, added: Surfaces) => {\n for (const [interaction, surface] of Object.entries(added)) {\n appendSurface(into, interaction, surface)\n }\n}\n\nconst freezeSurfaces = (surfaces: Record<string, Surfaces>) => {\n for (const key of Object.keys(surfaces)) Object.freeze(surfaces[key])\n return Object.freeze(surfaces)\n}\n\n/** Later attachments append to each surface's disjunction and replace its refusal. */\nconst mergeAttachments = (attachments: ReadonlyArray<Attachment>): Attachment => {\n const row = record<Surface>()\n const members = record<Record<string, Surface>>()\n const relations = record<Record<string, Surface>>()\n const always: Array<AnyRule> = []\n for (const attachment of attachments) {\n appendSurfaces(row, attachment.row)\n for (const [key, surfaces] of Object.entries(attachment.members)) {\n appendSurfaces((members[key] ??= record()), surfaces)\n }\n for (const [key, surfaces] of Object.entries(attachment.relations)) {\n appendSurfaces((relations[key] ??= record()), surfaces)\n }\n always.push(...attachment.always)\n }\n const { set, unguarded } = attachments[0]!\n return Object.freeze({\n set,\n unguarded,\n row: Object.freeze(row),\n members: freezeSurfaces(members),\n relations: freezeSurfaces(relations),\n always: Object.freeze(always),\n })\n}\n\nconst showRule = (rule: AnyRule): string =>\n Match.valueTags(rule, {\n PrincipalGuard: ({ name }) => name,\n RowGuard: ({ name }) => name,\n All: ({ rules }) => `all(${rules.map(showRule).join(', ')})`,\n Inherit: ({ relation }) => `inherit(${relation})`,\n })\n\nconst showDeny = (deny: Deny): string =>\n Match.valueTags(deny, {\n NotFound: () => 'not_found',\n Omit: () => 'omit',\n Redact: ({ value }) => `redact(${JSON.stringify(value)})`,\n Forbidden: () => 'forbidden',\n Explain: ({ reason }) => `explain(${JSON.stringify(reason)})`,\n })\n\nconst notFound: Deny = Object.freeze({ _tag: 'NotFound' })\nconst omit: Deny = Object.freeze({ _tag: 'Omit' })\nconst forbidden: Deny = Object.freeze({ _tag: 'Forbidden' })\n\n/** The refusal a surface takes when none was chosen. */\nexport const defaultDeny = (target: Target): Deny =>\n target.interaction === 'Read'\n ? Match.valueTags(target, { Row: () => notFound, Member: () => omit, Relation: () => omit })\n : forbidden\n\n/** The guards a surface carries; `none` where it falls to the set's `unguarded`. */\nexport const surfaceOf = (attachment: Attachment, target: Target): Option.Option<Surface> =>\n Match.valueTags(target, {\n Row: ({ interaction }) => Option.fromUndefinedOr(attachment.row[interaction]),\n Member: ({ member, interaction }) => {\n const surfaces = attachment.members[member]\n if (surfaces === undefined) return Option.none()\n const own = surfaces[interaction]\n // A member a caller may not change is rarely one it may set freely on creation.\n return Option.fromUndefinedOr(\n own === undefined && interaction === 'Create' ? surfaces['Update'] : own,\n )\n },\n Relation: ({ relation, interaction }) =>\n Option.fromUndefinedOr(attachment.relations[relation]?.[interaction]),\n })\n\n/** Whether who may read the rows depends on who is asking: a row Read rule, or `always`. */\nexport const readRowsRuled = (attachment: Attachment): boolean =>\n attachment.row['Read'] !== undefined || attachment.always.length > 0\n\n/** Whether `member`, a field or a relation, has a Read rule of its own. */\nexport const readRuled = (attachment: Attachment, member: string): boolean =>\n Option.isSome(surfaceOf(attachment, { _tag: 'Member', member, interaction: 'Read' })) ||\n Option.isSome(surfaceOf(attachment, { _tag: 'Relation', relation: member, interaction: 'Read' }))\n\nconst key = Metadata.key<Attachment>('foldkit-guard/attachment', {\n merge: attachments => [mergeAttachments(attachments)],\n summarize: attachment =>\n linesOf(attachment)\n .map(([where, rule]) => `${where}: ${rule}`)\n .join('; '),\n})\n\n/** The rows of an attachment's matrix: each surface it decides, and how. */\nconst linesOf = (attachment: Attachment): ReadonlyArray<readonly [string, string]> => {\n const open = attachment.unguarded === 'allow' ? 'open' : 'closed'\n const show = (target: Target) =>\n Option.match(surfaceOf(attachment, target), {\n onNone: () => open,\n onSome: ({ rules, deny }) =>\n // An empty disjunction allows no one.\n `${rules.length === 0 ? 'nobody' : rules.map(showRule).join(' | ')} → ${showDeny(Option.getOrElse(deny, () => defaultDeny(target)))}`,\n })\n const interactions = [\n 'Read',\n 'Create',\n 'Update',\n 'Delete',\n ...Object.keys(attachment.row).filter(interaction => !builtIn.includes(interaction as never)),\n ]\n return [\n ...(attachment.always.length > 0\n ? [['always', attachment.always.map(showRule).join(' & ')] as const]\n : []),\n ...interactions.map(interaction => [interaction, show({ _tag: 'Row', interaction })] as const),\n ...Object.keys(attachment.members).flatMap(member =>\n ['Read', 'Create', 'Update'].flatMap(interaction => {\n const target: Target = { _tag: 'Member', member, interaction }\n return Option.isSome(surfaceOf(attachment, target))\n ? [[`${member}.${interaction}`, show(target)] as const]\n : []\n }),\n ),\n ...Object.entries(attachment.relations).flatMap(([relation, surfaces]) =>\n Object.keys(surfaces).map(\n interaction =>\n [\n `${relation}.${interaction}`,\n show({ _tag: 'Relation', relation, interaction }),\n ] as const,\n ),\n ),\n ]\n}\n\nconst isOn = (slot: ReadonlyArray<AnyRule> | On<AnyRule, Deny>): slot is On<AnyRule, Deny> =>\n !Array.isArray(slot)\n\nconst surfaceFrom = (slot: ReadonlyArray<AnyRule> | On<AnyRule, Deny>): Surface =>\n isOn(slot)\n ? { rules: slot.rules, deny: Option.some(slot.deny) }\n : { rules: Object.freeze([...slot]), deny: Option.none() }\n\n/** Throws for a row guard over another Entity that shares the target's name. */\nconst checkRules = (\n set: string,\n entity: AnyEntity,\n where: string,\n rules: ReadonlyArray<AnyRule>,\n): void => {\n for (const rule of rules) {\n Match.valueTags(rule, {\n PrincipalGuard: () => {},\n RowGuard: guard => {\n if (!Entity.same(guard.entity, entity)) {\n throw new Error(\n `[foldkit-guard] ${set}.attach: ${where} names ${guard.name}, a guard over another Entity named ${entity.name}`,\n )\n }\n },\n All: ({ rules }) => checkRules(set, entity, where, rules),\n Inherit: () => {},\n })\n }\n}\n\nconst surfacesFrom = (\n set: string,\n entity: AnyEntity,\n where: string,\n slots: Readonly<Record<string, ReadonlyArray<AnyRule> | On<AnyRule, Deny> | undefined>>,\n): Surfaces => {\n const surfaces = record<Surface>()\n // `Write` goes first, whatever order the keys were written in: beside it, a\n // slot of its own adds rules to it and its refusal replaces Write's.\n const ordered = Object.entries(slots).sort(\n ([left], [right]) => Number(right === 'Write') - Number(left === 'Write'),\n )\n for (const [interaction, slot] of ordered) {\n if (slot === undefined) continue\n const added = surfaceFrom(slot)\n checkRules(set, entity, `${where}${interaction}`, added.rules)\n // Write is shorthand, so it is stored as the three it stands for.\n for (const each of interaction === 'Write' ? ['Create', 'Update', 'Delete'] : [interaction]) {\n appendSurface(surfaces, each, added)\n }\n }\n return Object.freeze(surfaces)\n}\n\nconst nested = (\n set: string,\n entity: AnyEntity,\n given: Readonly<\n Record<\n string,\n Readonly<Record<string, ReadonlyArray<AnyRule> | On<AnyRule, Deny> | undefined>> | undefined\n >\n >,\n): Readonly<Record<string, Surfaces>> => {\n const surfaces = record<Surfaces>()\n for (const [member, slots] of Object.entries(given)) {\n if (slots !== undefined) surfaces[member] = surfacesFrom(set, entity, `${member}.`, slots)\n }\n return Object.freeze(surfaces)\n}\n\n/** A row guard's own fields, and the relation an inherited Read follows. */\nconst membersRead = (rule: AnyRule): ReadonlyArray<string> =>\n Match.valueTags(rule, {\n PrincipalGuard: () => [],\n RowGuard: ({ query }) => Query.dependencies(query).predicate.map(field => field.key),\n All: ({ rules }) => rules.flatMap(membersRead),\n Inherit: ({ relation }) => [relation],\n })\n\nexport const attached = (entity: AnyEntity): Option.Option<Attachment> =>\n Option.fromUndefinedOr(key.get(entity.metadata)[0])\n\nexport const Attaching = {\n /**\n * Attaches guards to an Entity, per surface and interaction. A surface's list\n * is a disjunction; `always` is ANDed after it. Attaching twice appends to a\n * surface's list and replaces its refusal.\n */\n attach:\n <Name extends string, User, E extends AnyEntity>(\n G: {\n readonly name: Name\n readonly schema: Schema.Codec<User, unknown>\n readonly unguarded: 'allow' | 'deny'\n },\n config: Attach<NoInfer<User>, NoInfer<Name>, E>,\n ) =>\n (entity: E): E => {\n Option.map(attached(entity), ({ set }) => {\n if (set !== G.name) {\n throw new Error(\n `[foldkit-guard] ${G.name}.attach: ${entity.name} carries the guards of ${set} already`,\n )\n }\n })\n const always = config.always ?? []\n checkRules(G.name, entity, 'always', always)\n const attachment: Attachment = Object.freeze({\n set: G.name,\n unguarded: G.unguarded,\n // A named interaction is the row's, as the matrix and the decisions read it.\n row: surfacesFrom(G.name, entity, '', { ...config.row, ...config.interactions }),\n members: nested(G.name, entity, config.members ?? {}),\n relations: nested(G.name, entity, config.relations ?? {}),\n always: Object.freeze([...always]),\n })\n // What the row's Read rules read, so a client placing rows knows it cannot judge a change to them.\n const filtered = [\n ...new Set(\n [...(attachment.row['Read']?.rules ?? []), ...attachment.always].flatMap(membersRead),\n ),\n ]\n return entity.pipe(\n Entity.annotate(Metadata.combine([key.of(attachment), ReadFilter.of(filtered)])),\n )\n },\n\n /** The guards attached to `entity`, every attachment merged. */\n attached,\n\n /** Every rule must allow, as one rule. Principal and row guards may mix. */\n all: <const Rules extends ReadonlyArray<Exclude<AnyRule, Inherit<string>>>>(\n ...rules: Rules\n ): All<Rules[number]> => Object.freeze({ _tag: 'All', rules: Object.freeze([...rules]) }),\n\n /** This row's Read is the Read of the row its `one` relation points at. */\n inherit: <const Relation extends string>(relation: Relation): Inherit<Relation> =>\n Object.freeze({ _tag: 'Inherit', relation }),\n\n /** A surface's guards with a refusal other than its default. */\n on: <const Rules extends ReadonlyArray<AnyRule>, D extends Redact<unknown> | Explain>(\n rules: Rules,\n deny: D,\n ): On<Rules[number], D> => Object.freeze({ _tag: 'On', rules: Object.freeze([...rules]), deny }),\n\n /** A hidden member reads as `value`, typed by the member's schema. */\n redact: <const A>(value: A): Redact<A> => Object.freeze({ _tag: 'Redact', value }),\n\n /** A refused write tells the caller `reason`. */\n explain: (reason: string): Explain => Object.freeze({ _tag: 'Explain', reason }),\n\n /**\n * The members a row interaction's guards read, `always` included: a change\n * to one can change whether a principal may. A relation an inherited Read\n * follows counts.\n */\n guardFields: (\n entity: AnyEntity,\n interaction: 'Read' | 'Create' | 'Update' | 'Delete' | InteractionKey,\n ): ReadonlyArray<string> =>\n Option.match(attached(entity), {\n onNone: () => [],\n onSome: attachment => [\n ...new Set(\n [...(attachment.row[interaction]?.rules ?? []), ...attachment.always].flatMap(\n membersRead,\n ),\n ),\n ],\n }),\n\n /** Who may do what, one line per surface, for review and for a committed snapshot. */\n matrix: (entities: ReadonlyArray<AnyEntity>): string =>\n entities\n .flatMap(entity =>\n Option.match(attached(entity), {\n onNone: () => [`${entity.name}: no guards`],\n onSome: attachment =>\n linesOf(attachment).map(([where, rule]) => `${entity.name}.${where}: ${rule}`),\n }),\n )\n .join('\\n'),\n}\n"],"mappings":";;;;;;;;;AAsBA,MAAM,UAAU;CAAC;CAAQ;CAAU;CAAU;CAAU;CAAS;CAAQ;AAAQ;AAEhF,MAAa,cAAc;;AAEzB,OAAkC,SAAkC;CAClE,IAAK,QAAkC,SAAS,IAAI,GAClD,MAAM,IAAI,MAAM,sCAAsC,KAAK,4BAA4B;CAEzF,OAAO,OAAO,OAAO;EAAE;EAAM,KAAK;CAA6B,CAAC;AAClE,EACF;AAsIA,MAAM,eAAqC,OAAO,OAAO,IAAI;AAE7D,MAAM,iBAAiB,MAA+B,aAAqB,UAAmB;CAC5F,MAAM,SAAS,KAAK;CACpB,KAAK,eACH,WAAW,KAAA,IACP,QACA;EACE,OAAO,CAAC,GAAG,OAAO,OAAO,GAAG,MAAM,KAAK;EACvC,MAAM,OAAO,OAAO,MAAM,YAAY,OAAO,IAAI;CACnD;AACR;AAEA,MAAM,kBAAkB,MAA+B,UAAoB;CACzE,KAAK,MAAM,CAAC,aAAa,YAAY,OAAO,QAAQ,KAAK,GACvD,cAAc,MAAM,aAAa,OAAO;AAE5C;AAEA,MAAM,kBAAkB,aAAuC;CAC7D,KAAK,MAA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