foldkit-guard 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -7
- package/dist/agent.d.mts +1 -3
- package/dist/agent.d.mts.map +1 -1
- package/dist/agent.mjs +2 -3
- package/dist/{attach-BVG6LsFf.d.mts → attach-BnP9_Uca.d.mts} +18 -15
- package/dist/attach-BnP9_Uca.d.mts.map +1 -0
- package/dist/{attach-BZyVvs7H.mjs → attach-CRCF5XHS.mjs} +16 -4
- package/dist/attach-CRCF5XHS.mjs.map +1 -0
- package/dist/{condition-Dunujvzy.mjs → condition-Bl1UgEpM.mjs} +6 -6
- package/dist/condition-Bl1UgEpM.mjs.map +1 -0
- package/dist/drizzle.d.mts +8 -5
- package/dist/drizzle.d.mts.map +1 -1
- package/dist/drizzle.mjs +11 -14
- package/dist/drizzle.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +12 -11
- package/dist/index.mjs.map +1 -1
- package/dist/{may-DK-2lomO.mjs → may-DzCAt_5M.mjs} +2 -3
- package/dist/{may-DK-2lomO.mjs.map → may-DzCAt_5M.mjs.map} +1 -1
- package/dist/{me-DeSZOGt3.mjs → me-BYpuVqJE.mjs} +4 -5
- package/dist/{me-DeSZOGt3.mjs.map → me-BYpuVqJE.mjs.map} +1 -1
- package/dist/media.d.mts +1 -3
- package/dist/media.d.mts.map +1 -1
- package/dist/media.mjs +1 -2
- package/dist/{origin-CN6ThFTp.mjs → origin-igJoiz74.mjs} +2 -3
- package/dist/{origin-CN6ThFTp.mjs.map → origin-igJoiz74.mjs.map} +1 -1
- package/dist/{placed-CkyMqxFw.mjs → placed-BvdNmZJu.mjs} +4 -5
- package/dist/{placed-CkyMqxFw.mjs.map → placed-BvdNmZJu.mjs.map} +1 -1
- package/dist/server.d.mts +12 -3
- package/dist/server.d.mts.map +1 -1
- package/dist/server.mjs +211 -67
- package/dist/server.mjs.map +1 -1
- package/dist/sync.d.mts +1 -3
- package/dist/sync.d.mts.map +1 -1
- package/dist/sync.mjs +4 -5
- package/dist/sync.mjs.map +1 -1
- package/package.json +14 -14
- package/dist/attach-BVG6LsFf.d.mts.map +0 -1
- package/dist/attach-BZyVvs7H.mjs.map +0 -1
- package/dist/condition-Dunujvzy.mjs.map +0 -1
package/dist/server.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as Condition } from "./condition-
|
|
3
|
-
import "./me-
|
|
4
|
-
import { n as rowRules, r as writeRules, t as memberRules } from "./placed-
|
|
5
|
-
import { Effect, Match, Option, Schema } from "effect";
|
|
6
|
-
import { Query } from "foldkit-entity";
|
|
7
|
-
import { RemoteServer, RemoteServerError } from "foldkit-remote-server";
|
|
8
|
-
|
|
1
|
+
import { c as readRowsRuled, i as attachedRules, l as readRuled, r as attached } from "./attach-CRCF5XHS.mjs";
|
|
2
|
+
import { t as Condition } from "./condition-Bl1UgEpM.mjs";
|
|
3
|
+
import "./me-BYpuVqJE.mjs";
|
|
4
|
+
import { n as rowRules, r as writeRules, t as memberRules } from "./placed-BvdNmZJu.mjs";
|
|
5
|
+
import { Effect, Match, Option, Schema, SchemaAST } from "effect";
|
|
6
|
+
import { Entity, Query, supported } from "foldkit-entity";
|
|
7
|
+
import { RemoteInvalid, RemoteServer, RemoteServerError } from "foldkit-remote-server";
|
|
8
|
+
// src/server.ts
|
|
9
9
|
/**
|
|
10
10
|
* A guard set's read rules as `foldkit-remote-server`'s seams: an
|
|
11
11
|
* `EntitySource.filter` from each member's Read rule, and `RemoteServer.memory`'s
|
|
@@ -14,6 +14,12 @@ import { RemoteServer, RemoteServerError } from "foldkit-remote-server";
|
|
|
14
14
|
* brings the server along.
|
|
15
15
|
*/
|
|
16
16
|
const noMembers = (record) => record;
|
|
17
|
+
/** Two stored values, which are JSON: equal when they encode the same. */
|
|
18
|
+
const sameStored = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
19
|
+
/** The members of `fields` that have a Read rule, once each. */
|
|
20
|
+
const hiddenOf = (attachment, fields) => [...new Set(fields.map((field) => field.key).filter((key) => readRuled(attachment, key)))];
|
|
21
|
+
/** Each guarded writer's `revision`, or none: one a change can fall between check and write of. */
|
|
22
|
+
const pins = /* @__PURE__ */ new WeakMap();
|
|
17
23
|
/** A ref key's two halves, `Entity:id`; `none` for anything else. */
|
|
18
24
|
const refOf = (value) => {
|
|
19
25
|
if (typeof value !== "string") return Option.none();
|
|
@@ -85,7 +91,7 @@ const GuardServer = {
|
|
|
85
91
|
apply: (record) => {
|
|
86
92
|
const values = { ...record.values };
|
|
87
93
|
for (const { member, condition, redacted } of decided) {
|
|
88
|
-
if (Condition.holds(condition, record.values, input, () => false)) continue;
|
|
94
|
+
if (Condition.holds(condition, record.values, input, () => false, policy.operators)) continue;
|
|
89
95
|
Option.match(redacted, {
|
|
90
96
|
onNone: () => {
|
|
91
97
|
delete values[member];
|
|
@@ -128,7 +134,7 @@ const GuardServer = {
|
|
|
128
134
|
onSome: (target) => holds(ref.entity, target, /* @__PURE__ */ new Set([...seen, key]))
|
|
129
135
|
});
|
|
130
136
|
}
|
|
131
|
-
}));
|
|
137
|
+
}), policy.operators);
|
|
132
138
|
};
|
|
133
139
|
return (entity, values) => holds(entity, values, /* @__PURE__ */ new Set());
|
|
134
140
|
}), policy, entities),
|
|
@@ -153,11 +159,27 @@ const GuardServer = {
|
|
|
153
159
|
* from, and a create whose input sets a preset field is a mistake of the
|
|
154
160
|
* server's, refused.
|
|
155
161
|
*
|
|
162
|
+
* `revision` names a field stored as a number, which the writer moves on by
|
|
163
|
+
* one with each write it lands. An update or a delete then lands only on the
|
|
164
|
+
* revision its rules were decided over (the writer's `expect`), so a change
|
|
165
|
+
* between the check and the write is never written over; it is decided
|
|
166
|
+
* again against the row as it now is, up to three times. A change that does
|
|
167
|
+
* not move the revision (a handler's, `applyEdits`', raw SQL) is not seen.
|
|
168
|
+
* Without `revision`, none is, and `validate` warns. An input that expects a
|
|
169
|
+
* revision the row read is not at is a `Conflict`, decided before any rule;
|
|
170
|
+
* an input may expect only the field the writer pins.
|
|
171
|
+
*
|
|
156
172
|
* ```ts
|
|
157
173
|
* RemoteServer.write(EditTodo, GuardServer.writer(Todo, Policy, writer(Db.Todo)))
|
|
158
174
|
* ```
|
|
159
175
|
*/
|
|
160
176
|
writer: (entity, policy, writer, options = {}) => {
|
|
177
|
+
const revision = options.revision;
|
|
178
|
+
if (revision !== void 0) {
|
|
179
|
+
const field = Object.hasOwn(entity.fields, revision) ? entity.fields[revision] : void 0;
|
|
180
|
+
if (field === void 0) throw new Error(`[foldkit-guard] GuardServer.writer: ${entity.name} has no field "${revision}" for its revision`);
|
|
181
|
+
if (!SchemaAST.isNumber(Schema.toEncoded(field.schema).ast)) throw new Error(`[foldkit-guard] GuardServer.writer: ${entity.name}'s revision "${revision}" is not stored as a number`);
|
|
182
|
+
}
|
|
161
183
|
const presets = Object.entries(options.presets ?? {}).map(([field, preset]) => {
|
|
162
184
|
const declared = Object.hasOwn(entity.fields, field) ? entity.fields[field] : void 0;
|
|
163
185
|
if (declared === void 0) throw new Error(`[foldkit-guard] GuardServer.writer: ${entity.name} has no field "${field}" to preset`);
|
|
@@ -170,7 +192,7 @@ const GuardServer = {
|
|
|
170
192
|
if (writer.entity !== entity.name) throw new Error(`[foldkit-guard] GuardServer.writer: ${entity.name}'s rules over a writer for ${writer.entity}`);
|
|
171
193
|
const read = writer.read;
|
|
172
194
|
if (read === void 0) throw new Error(`[foldkit-guard] GuardServer.writer: ${entity.name}'s writer reads no row as its writer may see it; bind it with a visible rule (GuardDrizzle.visible)`);
|
|
173
|
-
const holds = (rule, values, principal) => Condition.holds(rule.condition, values, policy.inputOf(principal), () => false);
|
|
195
|
+
const holds = (rule, values, principal) => Condition.holds(rule.condition, values, policy.inputOf(principal), () => false, policy.operators);
|
|
174
196
|
/** Fails with the first rule of `rules` that does not hold over `values`. */
|
|
175
197
|
const require = (rules, values, principal) => Option.match(Option.fromUndefinedOr(rules.find((rule) => !holds(rule, values, principal))), {
|
|
176
198
|
onNone: () => Effect.void,
|
|
@@ -204,59 +226,155 @@ const GuardServer = {
|
|
|
204
226
|
...more,
|
|
205
227
|
...rules.flatMap(({ condition }) => Condition.members(condition))
|
|
206
228
|
])];
|
|
229
|
+
/** The fields that say which version of the row was read: the revision, and the input's. */
|
|
230
|
+
const versionFields = (expect) => [...revision === void 0 ? [] : [revision], ...Option.match(expect, {
|
|
231
|
+
onNone: () => [],
|
|
232
|
+
onSome: ({ field }) => [field]
|
|
233
|
+
})];
|
|
234
|
+
/** Whether the row read is not at the revision the input expects. */
|
|
235
|
+
const moved = (expect, before) => Option.exists(expect, ({ field, revision }) => !sameStored(before[field], revision));
|
|
236
|
+
/** The write held to the row its rules were decided over, where there is a revision. */
|
|
237
|
+
const pin = (expect, before) => revision === void 0 ? expect : Option.some({
|
|
238
|
+
field: revision,
|
|
239
|
+
revision: before[revision]
|
|
240
|
+
});
|
|
241
|
+
/**
|
|
242
|
+
* `attempt` run again while its pinned write misses: the row moved after it
|
|
243
|
+
* was read, so it is decided again over the row as it now is. A write the
|
|
244
|
+
* input pinned misses as a conflict, and is not run again.
|
|
245
|
+
*/
|
|
246
|
+
const pinned = (id, expect, missed, attempt) => {
|
|
247
|
+
if (revision === void 0) return attempt();
|
|
248
|
+
if (Option.isSome(expect)) return expect.value.field === revision ? attempt() : Effect.fail(new RemoteServerError({ message: `${entity.name}'s input expects "${expect.value.field}", but its writer pins "${revision}"` }));
|
|
249
|
+
const loop = (left) => Effect.flatMap(attempt(), (outcome) => !missed(outcome) ? Effect.succeed(outcome) : left > 1 ? loop(left - 1) : Effect.fail(new RemoteServerError({ message: `${entity.name} ${id} changed while it was written` })));
|
|
250
|
+
return loop(3);
|
|
251
|
+
};
|
|
252
|
+
const attachment = attached(entity);
|
|
253
|
+
const hiddenByBody = /* @__PURE__ */ new WeakMap();
|
|
254
|
+
const unnamed = new Set(Option.match(attachment, {
|
|
255
|
+
onNone: () => [],
|
|
256
|
+
onSome: (ruled) => Entity.invariants(entity).filter((held) => [...held.before, ...held.after].some((key) => readRuled(ruled, key))).map((held) => held.id)
|
|
257
|
+
}));
|
|
258
|
+
const told = (error) => error instanceof RemoteInvalid && Option.exists(error.rule, (rule) => unnamed.has(rule)) ? RemoteServer.invalid("The row would be invalid") : error;
|
|
259
|
+
/**
|
|
260
|
+
* Fails a write whose `when` reads, or whose `expect` names, a member with
|
|
261
|
+
* a Read rule: Unmet or written, Conflict or not, would answer for it.
|
|
262
|
+
* `validate` refuses the same at startup; this holds where it was not run.
|
|
263
|
+
*/
|
|
264
|
+
const discloses = (expect, when) => Option.match(attachment, {
|
|
265
|
+
onNone: () => Effect.void,
|
|
266
|
+
onSome: (ruled) => {
|
|
267
|
+
const read = Option.match(when, {
|
|
268
|
+
onNone: () => [],
|
|
269
|
+
onSome: ({ body }) => {
|
|
270
|
+
const known = hiddenByBody.get(body);
|
|
271
|
+
if (known !== void 0) return known;
|
|
272
|
+
const hidden = hiddenOf(ruled, Query.dependencies(body).predicate);
|
|
273
|
+
hiddenByBody.set(body, hidden);
|
|
274
|
+
return hidden;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
const expected = Option.match(expect, {
|
|
278
|
+
onNone: () => [],
|
|
279
|
+
onSome: ({ field }) => readRuled(ruled, field) ? [field] : []
|
|
280
|
+
});
|
|
281
|
+
const hidden = [.../* @__PURE__ */ new Set([...read, ...expected])];
|
|
282
|
+
return hidden.length === 0 ? Effect.void : Effect.fail(new RemoteServerError({ message: `${entity.name}'s write reads ${hidden.join(", ")}, which a Read rule hides from its writer` }));
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
/**
|
|
286
|
+
* The answer the writer's ledger holds for this operation, or else `write`:
|
|
287
|
+
* a retry of a write that landed is answered as it landed, not decided
|
|
288
|
+
* again over a row that may have moved since.
|
|
289
|
+
*/
|
|
290
|
+
const answeredOr = (context, write) => {
|
|
291
|
+
const recorded = writer.recorded;
|
|
292
|
+
if (recorded === void 0 || Option.isNone(context.operation)) return write();
|
|
293
|
+
return Effect.flatMap(recorded({
|
|
294
|
+
operation: context.operation.value,
|
|
295
|
+
id: context.id,
|
|
296
|
+
principal: context.principal
|
|
297
|
+
}), Option.match({
|
|
298
|
+
onSome: (answer) => Effect.succeed(Option.some(answer)),
|
|
299
|
+
onNone: write
|
|
300
|
+
}));
|
|
301
|
+
};
|
|
207
302
|
const memberRead = GuardServer.filter(entity, policy);
|
|
303
|
+
/** Refused before any rule, for what the write declares rather than what the row holds. */
|
|
304
|
+
const admitUpdate = (context) => Effect.andThen(discloses(context.expect, context.when), () => Object.keys(context.increments).length > 0 && Option.isNone(context.expect) && revision === void 0 ? Effect.fail(new RemoteServerError({ message: `${entity.name}'s increment expects no revision and its writer pins none, so its rules would judge a sum over a row that may move; give the writer a revision` })) : Effect.void);
|
|
305
|
+
/** The rules an update must meet: the row's, each member it sets, each relation it points. */
|
|
306
|
+
const updateRules = (context) => Effect.gen(function* () {
|
|
307
|
+
return {
|
|
308
|
+
row: yield* policy.writable(entity, context.principal, "Update"),
|
|
309
|
+
members: yield* policy.writeMembers(entity, context.principal, [...Object.keys(context.values), ...Object.keys(context.increments)], "Update"),
|
|
310
|
+
relating: yield* linkRules(context.principal, context.links)
|
|
311
|
+
};
|
|
312
|
+
});
|
|
313
|
+
/** The row as its writer may see it, with every field the rules and the version read. */
|
|
314
|
+
const readBefore = (context, rules) => read({
|
|
315
|
+
id: context.id,
|
|
316
|
+
fields: fieldsOf([
|
|
317
|
+
rules.row,
|
|
318
|
+
...rules.members,
|
|
319
|
+
...ruleOf(rules.relating)
|
|
320
|
+
], [...Object.keys(context.links), ...versionFields(context.expect)]),
|
|
321
|
+
principal: context.principal
|
|
322
|
+
});
|
|
323
|
+
/** The rules, over the row as it is and as the write would leave it. */
|
|
324
|
+
const decideUpdate = (context, rules, before) => Effect.gen(function* () {
|
|
325
|
+
const { principal, values, increments, links } = context;
|
|
326
|
+
const linked = Object.keys(links);
|
|
327
|
+
const stored = writer.linked === void 0 || linked.length === 0 ? Option.none() : yield* writer.linked({
|
|
328
|
+
id: context.id,
|
|
329
|
+
relations: linked
|
|
330
|
+
});
|
|
331
|
+
const relations = linking(rules.relating, Option.some(Option.match(stored, {
|
|
332
|
+
onNone: () => ({
|
|
333
|
+
links: before,
|
|
334
|
+
exact: false
|
|
335
|
+
}),
|
|
336
|
+
onSome: (links) => ({
|
|
337
|
+
links,
|
|
338
|
+
exact: true
|
|
339
|
+
})
|
|
340
|
+
})));
|
|
341
|
+
const after = {
|
|
342
|
+
...before,
|
|
343
|
+
...values,
|
|
344
|
+
...Object.fromEntries(Object.entries(increments).map(([field, amount]) => [field, Number(before[field]) + amount])),
|
|
345
|
+
...Object.fromEntries(Object.entries(links).map(([relation, link]) => [relation, linkKey(link)]))
|
|
346
|
+
};
|
|
347
|
+
yield* require([
|
|
348
|
+
rules.row,
|
|
349
|
+
...rules.members,
|
|
350
|
+
...relations
|
|
351
|
+
], before, principal);
|
|
352
|
+
yield* require([rules.row], after, principal);
|
|
353
|
+
});
|
|
208
354
|
const guarded = {
|
|
209
355
|
entity: writer.entity,
|
|
356
|
+
...writer.preconditions === true ? { preconditions: true } : {},
|
|
357
|
+
...writer.increments === true ? { increments: true } : {},
|
|
358
|
+
...writer.ledger === true ? { ledger: true } : {},
|
|
359
|
+
...writer.invariants === true ? { invariants: true } : {},
|
|
360
|
+
...writer.refuses === void 0 ? {} : { refuses: writer.refuses },
|
|
361
|
+
...writer.recorded === void 0 ? {} : { recorded: writer.recorded },
|
|
210
362
|
read: ({ id, fields, principal }) => seenRow({ filter: memberRead }, (seen) => read({
|
|
211
363
|
id,
|
|
212
364
|
fields: seen,
|
|
213
365
|
principal
|
|
214
366
|
}), principal, fields, id),
|
|
215
|
-
update: (context) => Effect.gen(function* () {
|
|
216
|
-
const
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
...
|
|
223
|
-
|
|
224
|
-
], Object.keys(links));
|
|
225
|
-
const current = yield* read({
|
|
226
|
-
id: context.id,
|
|
227
|
-
fields,
|
|
228
|
-
principal
|
|
229
|
-
});
|
|
230
|
-
if (Option.isNone(current)) return yield* Effect.fail(refused(row.deny));
|
|
231
|
-
const before = current.value;
|
|
232
|
-
const linked = Object.keys(links);
|
|
233
|
-
const stored = writer.linked === void 0 || linked.length === 0 ? Option.none() : yield* writer.linked({
|
|
234
|
-
id: context.id,
|
|
235
|
-
relations: linked
|
|
367
|
+
update: (context) => Effect.mapError(Effect.andThen(admitUpdate(context), () => answeredOr(context, () => pinned(context.id, context.expect, Option.isNone, () => Effect.gen(function* () {
|
|
368
|
+
const rules = yield* updateRules(context);
|
|
369
|
+
const current = yield* readBefore(context, rules);
|
|
370
|
+
if (Option.isNone(current)) return yield* Effect.fail(refused(rules.row.deny));
|
|
371
|
+
if (moved(context.expect, current.value)) return Option.none();
|
|
372
|
+
yield* decideUpdate(context, rules, current.value);
|
|
373
|
+
return yield* writer.update({
|
|
374
|
+
...context,
|
|
375
|
+
expect: pin(context.expect, current.value)
|
|
236
376
|
});
|
|
237
|
-
|
|
238
|
-
onNone: () => ({
|
|
239
|
-
links: before,
|
|
240
|
-
exact: false
|
|
241
|
-
}),
|
|
242
|
-
onSome: (links) => ({
|
|
243
|
-
links,
|
|
244
|
-
exact: true
|
|
245
|
-
})
|
|
246
|
-
})));
|
|
247
|
-
const after = {
|
|
248
|
-
...before,
|
|
249
|
-
...values,
|
|
250
|
-
...Object.fromEntries(Object.entries(links).map(([relation, link]) => [relation, linkKey(link)]))
|
|
251
|
-
};
|
|
252
|
-
yield* require([
|
|
253
|
-
row,
|
|
254
|
-
...members,
|
|
255
|
-
...relations
|
|
256
|
-
], before, principal);
|
|
257
|
-
yield* require([row], after, principal);
|
|
258
|
-
return yield* writer.update(context);
|
|
259
|
-
}),
|
|
377
|
+
})))), told),
|
|
260
378
|
insert: (given) => Effect.gen(function* () {
|
|
261
379
|
const preset = presets.find(({ field }) => Object.hasOwn(given.values, field));
|
|
262
380
|
if (preset !== void 0) return yield* new RemoteServerError({ message: `${entity.name}'s "${preset.field}" is preset by the server; the input may not set it` });
|
|
@@ -291,20 +409,25 @@ const GuardServer = {
|
|
|
291
409
|
...rule,
|
|
292
410
|
condition: Condition.unsetting(rule.condition, unset)
|
|
293
411
|
})), proposed, principal);
|
|
294
|
-
return yield* writer.insert(context);
|
|
412
|
+
return yield* Effect.mapError(writer.insert(context), told);
|
|
295
413
|
}),
|
|
296
|
-
delete: (context) => Effect.gen(function* () {
|
|
414
|
+
delete: (context) => Effect.andThen(discloses(context.expect, context.when), () => pinned(context.id, context.expect, (outcome) => outcome === "moved", () => Effect.gen(function* () {
|
|
297
415
|
const row = yield* policy.writable(entity, context.principal, "Delete");
|
|
298
416
|
const current = yield* read({
|
|
299
417
|
id: context.id,
|
|
300
|
-
fields: fieldsOf([row],
|
|
418
|
+
fields: fieldsOf([row], versionFields(context.expect)),
|
|
301
419
|
principal: context.principal
|
|
302
420
|
});
|
|
303
421
|
if (Option.isNone(current)) return "absent";
|
|
422
|
+
if (moved(context.expect, current.value)) return "moved";
|
|
304
423
|
yield* require([row], current.value, context.principal);
|
|
305
|
-
return yield* writer.delete(
|
|
306
|
-
|
|
424
|
+
return yield* writer.delete({
|
|
425
|
+
...context,
|
|
426
|
+
expect: pin(context.expect, current.value)
|
|
427
|
+
});
|
|
428
|
+
})))
|
|
307
429
|
};
|
|
430
|
+
pins.set(guarded, Option.fromUndefinedOr(revision));
|
|
308
431
|
return writeRules.add(guarded, policy, [entity]);
|
|
309
432
|
},
|
|
310
433
|
/**
|
|
@@ -356,7 +479,7 @@ const GuardServer = {
|
|
|
356
479
|
const input = policy.inputOf(principal);
|
|
357
480
|
if (Option.isNone(target)) {
|
|
358
481
|
const unset = Condition.unsetting(rule.condition, new Set(ruled));
|
|
359
|
-
if (!Condition.holds(unset, {}, input, () => false)) return yield* Effect.fail(refused(rule.deny));
|
|
482
|
+
if (!Condition.holds(unset, {}, input, () => false, policy.operators)) return yield* Effect.fail(refused(rule.deny));
|
|
360
483
|
return yield* shown(yield* run({
|
|
361
484
|
...context,
|
|
362
485
|
row: Option.none()
|
|
@@ -368,7 +491,7 @@ const GuardServer = {
|
|
|
368
491
|
...guard.fields ?? []
|
|
369
492
|
])];
|
|
370
493
|
const row = Option.fromUndefinedOr(yield* rowOf(source, principal, fields, target.value));
|
|
371
|
-
if (Option.isNone(row) || !Condition.holds(rule.condition, row.value, input, () => false)) return yield* Effect.fail(refused(rule.deny));
|
|
494
|
+
if (Option.isNone(row) || !Condition.holds(rule.condition, row.value, input, () => false, policy.operators)) return yield* Effect.fail(refused(rule.deny));
|
|
372
495
|
return yield* shown(yield* run({
|
|
373
496
|
...context,
|
|
374
497
|
row
|
|
@@ -414,10 +537,24 @@ const GuardServer = {
|
|
|
414
537
|
const bodyProblems = [...config.domain.registry.queries].flatMap(([queryName, { body }]) => {
|
|
415
538
|
if (body === void 0 || body.entity.name !== name) return [];
|
|
416
539
|
const { predicate, order } = Query.dependencies(body);
|
|
417
|
-
|
|
418
|
-
|
|
540
|
+
return [...hiddenOf(attachment, predicate).map((member) => `query ${queryName} filters ${name} by ${member}, which has a Read rule`), ...hiddenOf(attachment, order).map((member) => `query ${queryName} orders ${name} by ${member}, which has a Read rule`)];
|
|
541
|
+
});
|
|
542
|
+
const writeProblems = [...config.server.mutations].flatMap(([mutation, source]) => {
|
|
543
|
+
if (source.writes === void 0 || source.writes.entity !== name) return [];
|
|
544
|
+
if (!writeRules.has(source.writes.writer, policy, entity)) return [`write ${mutation} writes ${name} without its write rules`];
|
|
545
|
+
const { write, writer } = source.writes;
|
|
546
|
+
const when = Option.match(write.when, {
|
|
547
|
+
onNone: () => [],
|
|
548
|
+
onSome: (body) => hiddenOf(attachment, Query.dependencies(body).predicate)
|
|
549
|
+
});
|
|
550
|
+
const expected = write.expect?.field.key;
|
|
551
|
+
const pin = typeof writer === "object" && writer !== null ? pins.get(writer) : void 0;
|
|
552
|
+
return [
|
|
553
|
+
...when.map((member) => `write ${mutation}'s precondition reads ${name}'s ${member}, which has a Read rule`),
|
|
554
|
+
...expected !== void 0 && readRuled(attachment, expected) ? [`write ${mutation} expects ${name}'s ${expected}, which has a Read rule`] : [],
|
|
555
|
+
...expected !== void 0 && pin !== void 0 && Option.exists(pin, (field) => field !== expected) ? [`write ${mutation} expects ${name}'s ${expected}, but its writer pins ${Option.getOrThrow(pin)}`] : []
|
|
556
|
+
];
|
|
419
557
|
});
|
|
420
|
-
const writeProblems = [...config.server.mutations].flatMap(([mutation, source]) => source.writes !== void 0 && source.writes.entity === name && !writeRules.has(source.writes.writer, policy, entity) ? [`write ${mutation} writes ${name} without its write rules`] : []);
|
|
421
558
|
const shared = Option.match(source, {
|
|
422
559
|
onNone: () => [],
|
|
423
560
|
onSome: (source) => [...source.shared ?? []]
|
|
@@ -429,6 +566,7 @@ const GuardServer = {
|
|
|
429
566
|
if (!byName.has(target)) return [...own, `${name}'s Source shares ${member}, whose ${target} is not validated`];
|
|
430
567
|
return rowsRuled(byName.get(target)) ? [...own, `${name}'s Source shares ${member}, whose rows have a Read rule`] : own;
|
|
431
568
|
})];
|
|
569
|
+
const operatorProblems = attachedRules(attachment).flatMap((rule) => rule._tag === "RowGuard" ? Query.unsupported(rule.query, supported, policy.operators).map((missing) => `${name}'s ${rule.name} calls ${missing}, which the policy does not run`) : []);
|
|
432
570
|
const liveProblems = config.server.live.has(name) ? [`${name} has a live Source of its own; serve its changes through a live hub`] : [];
|
|
433
571
|
return [
|
|
434
572
|
...sourceProblems,
|
|
@@ -436,12 +574,19 @@ const GuardServer = {
|
|
|
436
574
|
...bodyProblems,
|
|
437
575
|
...writeProblems,
|
|
438
576
|
...sharedProblems,
|
|
577
|
+
...operatorProblems,
|
|
439
578
|
...liveProblems
|
|
440
579
|
];
|
|
441
580
|
}
|
|
442
581
|
}));
|
|
443
582
|
const problems = [...undeclared, ...entityProblems];
|
|
444
583
|
if (problems.length > 0) throw new Error(`[foldkit-guard] GuardServer.validate: ${problems.join("; ")}`);
|
|
584
|
+
const unpinnedWrites = [...config.server.mutations].flatMap(([mutation, source]) => {
|
|
585
|
+
const writer = source.writes?.writer;
|
|
586
|
+
const pin = typeof writer === "object" && writer !== null ? pins.get(writer) : void 0;
|
|
587
|
+
return pin !== void 0 && Option.isNone(pin) ? [mutation] : [];
|
|
588
|
+
});
|
|
589
|
+
if (unpinnedWrites.length > 0) console.warn(`[foldkit-guard] GuardServer.validate: ${unpinnedWrites.join(", ")} ${unpinnedWrites.length === 1 ? "writes" : "write"} through a GuardServer.writer with no revision, so a change between its check and its write is not seen; pass { revision }`);
|
|
445
590
|
}
|
|
446
591
|
};
|
|
447
592
|
/** Whether a read asks for `may`, of an Entity that has it (`Guards.may`). */
|
|
@@ -486,7 +631,7 @@ const mayOf = (entity, policy, principal) => {
|
|
|
486
631
|
})))
|
|
487
632
|
]), ([rows, members, relations]) => {
|
|
488
633
|
const input = policy.inputOf(principal);
|
|
489
|
-
const holds = (condition, values) => Condition.holds(condition, values, input, () => false);
|
|
634
|
+
const holds = (condition, values) => Condition.holds(condition, values, input, () => false, policy.operators);
|
|
490
635
|
const allows = (rule, values) => Option.match(rule, {
|
|
491
636
|
onNone: () => true,
|
|
492
637
|
onSome: ({ condition }) => holds(condition, values)
|
|
@@ -557,7 +702,6 @@ const linkKey = (link) => Option.match(link, {
|
|
|
557
702
|
});
|
|
558
703
|
/** Whether a query's result is a connection over `entity`. */
|
|
559
704
|
const lists = (result, entity) => typeof result === "object" && result !== null && "entity" in result && result.entity === entity;
|
|
560
|
-
//#endregion
|
|
561
705
|
export { GuardServer };
|
|
562
706
|
|
|
563
707
|
//# sourceMappingURL=server.mjs.map
|