@latticecms/core 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +1297 -2
  2. package/dist/access.d.ts +209 -0
  3. package/dist/access.d.ts.map +1 -0
  4. package/dist/access.js +241 -0
  5. package/dist/access.js.map +1 -0
  6. package/dist/build.d.ts +555 -0
  7. package/dist/build.d.ts.map +1 -0
  8. package/dist/build.js +655 -0
  9. package/dist/build.js.map +1 -0
  10. package/dist/collection.d.ts +480 -0
  11. package/dist/collection.d.ts.map +1 -0
  12. package/dist/collection.js +351 -0
  13. package/dist/collection.js.map +1 -0
  14. package/dist/crud.d.ts +46 -0
  15. package/dist/crud.d.ts.map +1 -0
  16. package/dist/crud.js +527 -0
  17. package/dist/crud.js.map +1 -0
  18. package/dist/describe.d.ts +89 -0
  19. package/dist/describe.d.ts.map +1 -0
  20. package/dist/describe.js +119 -0
  21. package/dist/describe.js.map +1 -0
  22. package/dist/errors.d.ts +183 -0
  23. package/dist/errors.d.ts.map +1 -0
  24. package/dist/errors.js +150 -0
  25. package/dist/errors.js.map +1 -0
  26. package/dist/family.d.ts +80 -0
  27. package/dist/family.d.ts.map +1 -0
  28. package/dist/family.js +227 -0
  29. package/dist/family.js.map +1 -0
  30. package/dist/index.d.ts +14 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +10 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/marker.d.ts +15 -0
  35. package/dist/marker.d.ts.map +1 -0
  36. package/dist/marker.js +28 -0
  37. package/dist/marker.js.map +1 -0
  38. package/dist/mask.d.ts +69 -0
  39. package/dist/mask.d.ts.map +1 -0
  40. package/dist/mask.js +277 -0
  41. package/dist/mask.js.map +1 -0
  42. package/dist/populate.d.ts +39 -0
  43. package/dist/populate.d.ts.map +1 -0
  44. package/dist/populate.js +72 -0
  45. package/dist/populate.js.map +1 -0
  46. package/dist/relations.d.ts +144 -0
  47. package/dist/relations.d.ts.map +1 -0
  48. package/dist/relations.js +144 -0
  49. package/dist/relations.js.map +1 -0
  50. package/dist/repo.d.ts +99 -0
  51. package/dist/repo.d.ts.map +1 -0
  52. package/dist/repo.js +92 -0
  53. package/dist/repo.js.map +1 -0
  54. package/dist/sort.d.ts +86 -0
  55. package/dist/sort.d.ts.map +1 -0
  56. package/dist/sort.js +187 -0
  57. package/dist/sort.js.map +1 -0
  58. package/dist/store/conformance.d.ts +15 -0
  59. package/dist/store/conformance.d.ts.map +1 -0
  60. package/dist/store/conformance.js +811 -0
  61. package/dist/store/conformance.js.map +1 -0
  62. package/dist/store/index.d.ts +9 -0
  63. package/dist/store/index.d.ts.map +1 -0
  64. package/dist/store/index.js +8 -0
  65. package/dist/store/index.js.map +1 -0
  66. package/dist/store.d.ts +119 -0
  67. package/dist/store.d.ts.map +1 -0
  68. package/dist/store.js +176 -0
  69. package/dist/store.js.map +1 -0
  70. package/dist/tenancy.d.ts +66 -0
  71. package/dist/tenancy.d.ts.map +1 -0
  72. package/dist/tenancy.js +69 -0
  73. package/dist/tenancy.js.map +1 -0
  74. package/dist/tracing.d.ts +12 -0
  75. package/dist/tracing.d.ts.map +1 -0
  76. package/dist/tracing.js +27 -0
  77. package/dist/tracing.js.map +1 -0
  78. package/dist/where.d.ts +95 -0
  79. package/dist/where.d.ts.map +1 -0
  80. package/dist/where.js +308 -0
  81. package/dist/where.js.map +1 -0
  82. package/package.json +80 -2
  83. package/skills/lattice/SKILL.md +106 -0
  84. package/skills/lattice/references/access.md +90 -0
  85. package/skills/lattice/references/cli.md +87 -0
  86. package/skills/lattice/references/collection.md +94 -0
  87. package/skills/lattice/references/event-handler.md +88 -0
  88. package/skills/lattice/references/masking.md +72 -0
  89. package/skills/lattice/references/method.md +88 -0
  90. package/skills/lattice/references/override.md +160 -0
  91. package/skills/lattice/references/relations.md +91 -0
  92. package/skills/lattice/references/tenancy.md +82 -0
  93. package/skills/lattice/references/testing.md +75 -0
  94. package/src/access.ts +449 -0
  95. package/src/build.ts +1888 -0
  96. package/src/collection.ts +1025 -0
  97. package/src/crud.ts +797 -0
  98. package/src/describe.ts +232 -0
  99. package/src/errors.ts +174 -0
  100. package/src/family.ts +334 -0
  101. package/src/index.ts +44 -0
  102. package/src/marker.ts +54 -0
  103. package/src/mask.ts +336 -0
  104. package/src/populate.ts +103 -0
  105. package/src/relations.ts +370 -0
  106. package/src/repo.ts +218 -0
  107. package/src/sort.ts +271 -0
  108. package/src/store/conformance.ts +1179 -0
  109. package/src/store/index.ts +22 -0
  110. package/src/store.ts +315 -0
  111. package/src/tenancy.ts +124 -0
  112. package/src/tracing.ts +29 -0
  113. package/src/where.ts +463 -0
@@ -0,0 +1,209 @@
1
+ import type { z } from 'zod';
2
+ import type { Action, AnyCollection } from './collection.js';
3
+ import type { CallContext, Family, FamilyContext } from './family.js';
4
+ import type { Doc } from './repo.js';
5
+ import { Scope, type Tenant } from './tenancy.js';
6
+ import { type AnyWhere, type FieldWhere, type Where } from './where.js';
7
+ /** What `Access.check` decides on: who does what to which collection. */
8
+ export type AccessRequest<Actor> = {
9
+ readonly actor: Actor;
10
+ readonly collection: string;
11
+ /** A generated operation, or a method's name. */
12
+ readonly action: Action | (string & {});
13
+ /** The parsed input. Passed for `create` only. */
14
+ readonly data?: unknown;
15
+ };
16
+ declare const Access_base: (abstract new () => {
17
+ readonly "__#private@#brand": true;
18
+ }) & {
19
+ readonly token: "Access";
20
+ };
21
+ /**
22
+ * Decides what a call may see and change (0005). `true` allows every row,
23
+ * `false` refuses the call with FORBIDDEN, and a `Where` limits it to the
24
+ * rows that match. The `Where` names the collection's fields; the port spans
25
+ * every collection, so it is untyped here.
26
+ */
27
+ export declare abstract class Access<Actor = unknown> extends Access_base {
28
+ abstract check(request: AccessRequest<Actor>): Promise<boolean | AnyWhere>;
29
+ /**
30
+ * Which of `fields`, a collection's maskable fields, `actor` may read.
31
+ * A field not in the answer is withheld from what the caller receives,
32
+ * so an adapter that forgets a field withholds it.
33
+ */
34
+ abstract readable(request: {
35
+ readonly actor: Actor;
36
+ readonly collection: string;
37
+ readonly fields: readonly string[];
38
+ }): Promise<readonly string[]>;
39
+ }
40
+ /**
41
+ * One access function as the default adapter stores it. A method signature,
42
+ * so its parameter is bivariant: a function typed by the app's actor and a
43
+ * collection's document fits.
44
+ */
45
+ type AccessFunction = {
46
+ fn(args: {
47
+ readonly actor: unknown;
48
+ readonly data?: unknown;
49
+ }): boolean | AnyWhere | Promise<boolean | AnyWhere>;
50
+ }['fn'];
51
+ /** The access functions given at `.collection()`, by action or method name. */
52
+ export type AccessMap = Readonly<Record<string, AccessFunction>>;
53
+ /** One field rule as the default adapter stores it; bivariant, as above. */
54
+ type ReadRule = {
55
+ fn(args: {
56
+ readonly actor: unknown;
57
+ }): boolean | Promise<boolean>;
58
+ }['fn'];
59
+ /** The `fields` rules given at `.collection()`, by field. */
60
+ export type FieldsMap = Readonly<Record<string, {
61
+ readonly read: ReadRule;
62
+ }>>;
63
+ /** The keys of `access` besides method names. `read` serves find, count and get. */
64
+ type AccessAction = 'read' | 'create' | 'update' | 'delete';
65
+ /** What an access function may return for `A`. Create has no row to filter. */
66
+ type Allowed<A, D> = A extends 'create' ? boolean : boolean | Where<D>;
67
+ /** What an access function receives for `A`. Create also gets its input. */
68
+ type AccessArgs<Actor, A, C extends AnyCollection> = A extends 'create' ? {
69
+ readonly actor: Actor;
70
+ readonly data: z.output<C['schema']>;
71
+ } : {
72
+ readonly actor: Actor;
73
+ };
74
+ /**
75
+ * The return types `R` of a collection's access functions, by key. Inferred
76
+ * from the functions, so `AccessCheck` can read the keys of each `Where`:
77
+ * TypeScript does not check excess properties on an object literal that an
78
+ * arrow function returns.
79
+ */
80
+ export type AccessReturns<R, C extends AnyCollection> = {
81
+ [A in keyof R]: Allowed<A, Doc<C>>;
82
+ };
83
+ /** The `access` option of `.collection()`, typed by the app's actor. */
84
+ export type AccessFns<Actor, R, C extends AnyCollection> = {
85
+ readonly [A in keyof R]: (args: AccessArgs<Actor, A, C>) => R[A] | Promise<R[A]>;
86
+ };
87
+ /** Each key of a returned `Where` that names no field and no operator. */
88
+ type Unknown<W, D> = W extends boolean ? never : Exclude<keyof W, keyof D | 'and' | 'or'> | {
89
+ [K in keyof W & keyof D]: Exclude<keyof W[K], keyof FieldWhere<D[K]>>;
90
+ }[keyof W & keyof D] | (W extends {
91
+ readonly and: readonly (infer I)[];
92
+ } ? Unknown<I, D> : never) | (W extends {
93
+ readonly or: readonly (infer I)[];
94
+ } ? Unknown<I, D> : never);
95
+ type AccessError<R, M, C extends AnyCollection> = {
96
+ [A in keyof R]: A extends AccessAction | keyof M ? [Unknown<R[A], Doc<C>>] extends [never] ? never : `access.${A & string} filters on "${Unknown<R[A], Doc<C>> & string}", which ${C['slug']} does not have` : `"${A & string}" is not an action of ${C['slug']}`;
97
+ }[keyof R] | {
98
+ [N in Exclude<keyof M, keyof R>]: `${C['slug']}.${N & string} has no access function; it is always FORBIDDEN`;
99
+ }[Exclude<keyof M, keyof R>];
100
+ /**
101
+ * Checks `.collection()`'s options: each `Where` names the collection's
102
+ * fields, each key is an action or a method, and each method has a
103
+ * function. On the options object, not on `access`: a check on `access`
104
+ * would depend on the functions it types, and `actor` would lose its type.
105
+ */
106
+ export type AccessCheck<R, M, C extends AnyCollection> = [
107
+ AccessError<R, M, C>
108
+ ] extends [never] ? unknown : {
109
+ readonly '~error': AccessError<R, M, C>;
110
+ };
111
+ declare const CollectionAccess_base: (abstract new (...args: any[]) => {
112
+ start(): Promise<void>;
113
+ stop(): Promise<void>;
114
+ }) & typeof Access;
115
+ /**
116
+ * The default adapter: the functions given at `.collection(C, { access })`.
117
+ * `find`, `count` and `get` run `read`. A missing function denies.
118
+ */
119
+ export declare class CollectionAccess extends CollectionAccess_base {
120
+ private readonly maps;
121
+ private readonly fields;
122
+ constructor(maps: ReadonlyMap<string, AccessMap>, fields?: ReadonlyMap<string, FieldsMap>);
123
+ readable(request: {
124
+ readonly actor: unknown;
125
+ readonly collection: string;
126
+ readonly fields: readonly string[];
127
+ }): Promise<readonly string[]>;
128
+ check(request: AccessRequest<unknown>): Promise<boolean | AnyWhere>;
129
+ }
130
+ /**
131
+ * What trusted code passes to a `Repo` call instead of a `Scope`: event
132
+ * handlers, workers and scripts that act for no caller. It reads and writes
133
+ * every row.
134
+ */
135
+ export declare const unscoped: unique symbol;
136
+ /** What one call's `CallAccess` asks its caller for. */
137
+ type Caller = {
138
+ /** The access answer for `action` on `collection`. */
139
+ check(collection: string, action: string, data: unknown): Promise<boolean | AnyWhere>;
140
+ /** The actor's tenant. Asked after the guards, so the actor is valid. */
141
+ tenant(): Tenant | undefined;
142
+ /** Logs a bypassed scope of a tenanted collection. */
143
+ bypassed(collection: string, action: string): void;
144
+ /** Which of a collection's maskable `fields` the actor may read. */
145
+ readable(collection: string, fields: readonly string[]): Promise<readonly string[]>;
146
+ };
147
+ /**
148
+ * The access and tenant of one call, on `ctx.access`. Each access answer is
149
+ * kept for the life of the context, so a guard and `execute` check once
150
+ * between them. Create is never kept: its answer depends on `data`.
151
+ */
152
+ export declare class CallAccess {
153
+ #private;
154
+ private readonly caller;
155
+ private readonly actor;
156
+ /**
157
+ * @param actor The call's actor as the family's schema parsed it, or a
158
+ * failed parse. The family's first guard reads it: nothing after that
159
+ * guard sees an actor the schema did not produce.
160
+ */
161
+ constructor(caller: Caller, actor: {
162
+ readonly ok: true;
163
+ readonly value: unknown;
164
+ } | {
165
+ readonly ok: false;
166
+ });
167
+ /**
168
+ * Whether `access` is a call's access whose actor parsed, and `actor` is
169
+ * that parsed value. A context made any other way, or whose actor was
170
+ * swapped after the parse, is refused.
171
+ */
172
+ static parsed(access: unknown, actor: unknown): boolean;
173
+ /**
174
+ * The scope of `action` on `collection`: the tenant clause ANDed with the
175
+ * access `Where`. Throws FORBIDDEN when access says no; an unknown action
176
+ * has no function, so it is refused too. A bypass reads across tenants,
177
+ * and a generated write under it is FORBIDDEN.
178
+ */
179
+ scope<C extends AnyCollection>(collection: C, action: Action | (string & {}), data?: unknown): Promise<Scope<C['slug']>>;
180
+ /**
181
+ * The maskable fields of `collection` the caller of `access` may not read,
182
+ * asked of `Access.readable` once per call. Empty when nothing is
183
+ * maskable. Static, like `stampOf`, so it is not on `ctx.access`: the
184
+ * family guard and the router call it, and apps do not.
185
+ */
186
+ static withheld(access: CallAccess, collection: {
187
+ readonly slug: string;
188
+ readonly maskable: readonly string[];
189
+ }): Promise<ReadonlySet<string>>;
190
+ /**
191
+ * The tenant id a generated create stamps on `collection`. Undefined for
192
+ * a global collection.
193
+ */
194
+ static stampOf(access: CallAccess, collection: AnyCollection): string | undefined;
195
+ private answer;
196
+ }
197
+ /**
198
+ * The context of one call. The actor is parsed once, here, with the
199
+ * family's schema, and everything downstream gets the parsed value: access
200
+ * functions, `tenantOf`, `actorId`, the log lines, `execute` and events. So
201
+ * a key the schema strips, or a value it transforms, never reaches them.
202
+ * An actor that does not parse is kept as given, and the family's first
203
+ * guard refuses the call before anything reads it.
204
+ */
205
+ export declare function familyContext<Actor>(access: Access<Actor>, base: CallContext<Actor>, family: Pick<Family<Actor>, 'actor' | 'actorId' | 'tenantOf'>): FamilyContext<Actor>;
206
+ /** The filter a `Repo` call runs under. A scope for another collection throws. */
207
+ export declare function whereOf(slug: string, scope: Scope | typeof unscoped): AnyWhere | undefined;
208
+ export {};
209
+ //# sourceMappingURL=access.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../src/access.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,KAAK,MAAM,EA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Q,CACb,MAAM,EAAE,UAAU,EAClB,UAAU,EAAE;QACV,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;KACtC,GACA,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAgB/B;;;OAGG;IACH,MAAM,CAAC,OAAO,CACZ,MAAM,EAAE,UAAU,EAClB,UAAU,EAAE,aAAa,GACxB,MAAM,GAAG,SAAS;IAiCrB,OAAO,CAAC,MAAM;CAgCf;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,KAAK,EACjC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,EACrB,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,EACxB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,UAAU,CAAC,GAC5D,aAAa,CAAC,KAAK,CAAC,CA+BtB;AAED,kFAAkF;AAClF,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,KAAK,GAAG,OAAO,QAAQ,GAC7B,QAAQ,GAAG,SAAS,CAQtB"}
package/dist/access.js ADDED
@@ -0,0 +1,241 @@
1
+ import { isDeepStrictEqual } from 'node:util';
2
+ import { Adapter, Port, validate } from 'hexok';
3
+ import { LatticeError } from './errors.js';
4
+ import { Scope, tenancy, tenantClause } from './tenancy.js';
5
+ import { both } from './where.js';
6
+ /**
7
+ * Decides what a call may see and change (0005). `true` allows every row,
8
+ * `false` refuses the call with FORBIDDEN, and a `Where` limits it to the
9
+ * rows that match. The `Where` names the collection's fields; the port spans
10
+ * every collection, so it is untyped here.
11
+ */
12
+ export class Access extends Port('Access') {
13
+ }
14
+ /**
15
+ * The default adapter: the functions given at `.collection(C, { access })`.
16
+ * `find`, `count` and `get` run `read`. A missing function denies.
17
+ */
18
+ export class CollectionAccess extends Adapter(Access) {
19
+ maps;
20
+ fields;
21
+ constructor(maps, fields = new Map()) {
22
+ super();
23
+ this.maps = maps;
24
+ this.fields = fields;
25
+ }
26
+ async readable(request) {
27
+ const rules = this.fields.get(request.collection) ?? {};
28
+ const answers = await Promise.all(request.fields.map(async (field) => {
29
+ // Own keys only, as for access; a missing rule withholds.
30
+ const rule = Object.hasOwn(rules, field) ? rules[field] : undefined;
31
+ return rule !== undefined &&
32
+ (await rule.read({ actor: request.actor })) === true
33
+ ? [field]
34
+ : [];
35
+ }));
36
+ return answers.flat();
37
+ }
38
+ async check(request) {
39
+ const { actor, collection, action, data } = request;
40
+ const map = this.maps.get(collection);
41
+ const name = action === 'find' || action === 'get' || action === 'count'
42
+ ? 'read'
43
+ : action;
44
+ // Own keys only: an action named `toString` must not reach the prototype.
45
+ const fn = map !== undefined && Object.hasOwn(map, name) ? map[name] : undefined;
46
+ if (fn === undefined)
47
+ return false;
48
+ return fn(action === 'create' ? { actor, data } : { actor });
49
+ }
50
+ }
51
+ /**
52
+ * What trusted code passes to a `Repo` call instead of a `Scope`: event
53
+ * handlers, workers and scripts that act for no caller. It reads and writes
54
+ * every row.
55
+ */
56
+ export const unscoped = Symbol('lattice.unscoped');
57
+ /** The generated writes a bypass may not make on a tenanted collection. */
58
+ const writes = ['create', 'update', 'delete'];
59
+ /**
60
+ * The access and tenant of one call, on `ctx.access`. Each access answer is
61
+ * kept for the life of the context, so a guard and `execute` check once
62
+ * between them. Create is never kept: its answer depends on `data`.
63
+ */
64
+ export class CallAccess {
65
+ caller;
66
+ actor;
67
+ /** Answers by collection slug, then action. */
68
+ #answers = new Map();
69
+ /** The tenant, once asked. */
70
+ #tenant;
71
+ /** The bypassed scopes already logged, as `slug action`. */
72
+ #logged = new Set();
73
+ /** The maskable fields withheld from the caller, by collection slug. */
74
+ #withheld = new Map();
75
+ /** The last create answer by collection slug, with the data it was asked for. */
76
+ #created = new Map();
77
+ /**
78
+ * @param actor The call's actor as the family's schema parsed it, or a
79
+ * failed parse. The family's first guard reads it: nothing after that
80
+ * guard sees an actor the schema did not produce.
81
+ */
82
+ constructor(caller, actor) {
83
+ this.caller = caller;
84
+ this.actor = actor;
85
+ }
86
+ /**
87
+ * Whether `access` is a call's access whose actor parsed, and `actor` is
88
+ * that parsed value. A context made any other way, or whose actor was
89
+ * swapped after the parse, is refused.
90
+ */
91
+ static parsed(access, actor) {
92
+ return (access instanceof CallAccess &&
93
+ access.actor.ok &&
94
+ access.actor.value === actor);
95
+ }
96
+ /**
97
+ * The scope of `action` on `collection`: the tenant clause ANDed with the
98
+ * access `Where`. Throws FORBIDDEN when access says no; an unknown action
99
+ * has no function, so it is refused too. A bypass reads across tenants,
100
+ * and a generated write under it is FORBIDDEN.
101
+ */
102
+ async scope(collection, action, data) {
103
+ const { slug } = collection;
104
+ const tenant = this.#tenantOf(collection);
105
+ // A relation's set writes too: `<relation>.set`. Method names have no dot.
106
+ if (tenant === tenancy.bypass &&
107
+ (writes.includes(action) || action.endsWith('.set'))) {
108
+ throw LatticeError.FORBIDDEN();
109
+ }
110
+ const answer = await this.answer(slug, action, data);
111
+ if (answer === false)
112
+ throw LatticeError.FORBIDDEN();
113
+ if (tenant === tenancy.bypass)
114
+ this.#logBypass(slug, action);
115
+ return new Scope(slug, both(tenantClause(collection, tenant), answer === true ? undefined : answer), typeof tenant === 'string' ? tenant : undefined);
116
+ }
117
+ /**
118
+ * The maskable fields of `collection` the caller of `access` may not read,
119
+ * asked of `Access.readable` once per call. Empty when nothing is
120
+ * maskable. Static, like `stampOf`, so it is not on `ctx.access`: the
121
+ * family guard and the router call it, and apps do not.
122
+ */
123
+ static withheld(access, collection) {
124
+ const { slug, maskable } = collection;
125
+ if (maskable.length === 0)
126
+ return Promise.resolve(new Set());
127
+ let answer = access.#withheld.get(slug);
128
+ if (answer === undefined) {
129
+ answer = access.caller
130
+ .readable(slug, maskable)
131
+ .then((readable) => new Set(maskable.filter((field) => !readable.includes(field))));
132
+ access.#withheld.set(slug, answer);
133
+ }
134
+ return answer;
135
+ }
136
+ /**
137
+ * The tenant id a generated create stamps on `collection`. Undefined for
138
+ * a global collection.
139
+ */
140
+ static stampOf(access, collection) {
141
+ const tenant = access.#tenantOf(collection);
142
+ return typeof tenant === 'string' ? tenant : undefined;
143
+ }
144
+ /** The caller's tenant on `collection`, or undefined when it is global. */
145
+ #tenantOf(collection) {
146
+ if (collection.tenant === undefined)
147
+ return undefined;
148
+ this.#tenant ??= { value: this.caller.tenant() };
149
+ const { value } = this.#tenant;
150
+ // Fail closed: a null tenant would match rows with no tenant (Mongo's
151
+ // null rule), and an empty one would stamp rows no tenant owns. An app
152
+ // bug, not a client refusal, so a plain Error.
153
+ if (value !== tenancy.bypass &&
154
+ (typeof value !== 'string' || value === '')) {
155
+ throw new Error(value === undefined
156
+ ? `Lattice: "${collection.slug}" is tenanted, but the family has no tenantOf`
157
+ : `Lattice: tenantOf returned ${JSON.stringify(value) ?? String(value)} for "${collection.slug}"; it must return a non-empty string or tenancy.bypass`);
158
+ }
159
+ return value;
160
+ }
161
+ #logBypass(slug, action) {
162
+ const key = `${slug} ${action}`;
163
+ if (this.#logged.has(key))
164
+ return;
165
+ this.#logged.add(key);
166
+ this.caller.bypassed(slug, action);
167
+ }
168
+ answer(slug, action, data) {
169
+ if (action === 'create') {
170
+ // Create's answer depends on its data. The family guard and the
171
+ // create body both ask (0036): with equal data, the function runs
172
+ // once; an override that changed the data is asked again. So is a
173
+ // create whose schema has a default that differs per parse, such as a
174
+ // generated id or a timestamp: the two parses are unequal, which costs
175
+ // a second run and nothing else. The key is a copy, because the body
176
+ // hands its data to beforeChange, which may change it in place.
177
+ const kept = this.#created.get(slug);
178
+ if (kept !== undefined && isDeepStrictEqual(kept.data, data)) {
179
+ return kept.answer;
180
+ }
181
+ const answer = this.caller.check(slug, action, data);
182
+ this.#created.set(slug, { data: structuredClone(data), answer });
183
+ return answer;
184
+ }
185
+ let answers = this.#answers.get(slug);
186
+ if (answers === undefined) {
187
+ answers = new Map();
188
+ this.#answers.set(slug, answers);
189
+ }
190
+ const kept = answers.get(action);
191
+ if (kept !== undefined)
192
+ return kept;
193
+ const answer = this.caller.check(slug, action, undefined);
194
+ answers.set(action, answer);
195
+ return answer;
196
+ }
197
+ }
198
+ /**
199
+ * The context of one call. The actor is parsed once, here, with the
200
+ * family's schema, and everything downstream gets the parsed value: access
201
+ * functions, `tenantOf`, `actorId`, the log lines, `execute` and events. So
202
+ * a key the schema strips, or a value it transforms, never reaches them.
203
+ * An actor that does not parse is kept as given, and the family's first
204
+ * guard refuses the call before anything reads it.
205
+ */
206
+ export function familyContext(access, base, family) {
207
+ const parsed = validate(family.actor, base.actor);
208
+ const actor = parsed.ok ? parsed.value : base.actor;
209
+ const { logger } = base;
210
+ const caller = {
211
+ check: (collection, action, data) => access.check({
212
+ actor,
213
+ collection,
214
+ action,
215
+ ...(action === 'create' ? { data } : {}),
216
+ }),
217
+ tenant: () => family.tenantOf?.(actor),
218
+ bypassed: (collection, action) => logger.info('tenancy.bypass', {
219
+ actorId: family.actorId(actor),
220
+ collection,
221
+ action,
222
+ tenancy: 'bypass',
223
+ }),
224
+ readable: (collection, fields) => access.readable({ actor, collection, fields }),
225
+ };
226
+ return {
227
+ ...base,
228
+ actor,
229
+ access: new CallAccess(caller, parsed.ok ? { ok: true, value: actor } : { ok: false }),
230
+ };
231
+ }
232
+ /** The filter a `Repo` call runs under. A scope for another collection throws. */
233
+ export function whereOf(slug, scope) {
234
+ if (scope === unscoped)
235
+ return undefined;
236
+ if (scope.collection !== slug) {
237
+ throw new Error(`Lattice: the "${slug}" repo was given a scope for "${scope.collection}"`);
238
+ }
239
+ return scope.where;
240
+ }
241
+ //# sourceMappingURL=access.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"access.js","sourceRoot":"","sources":["../src/access.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAG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