@yaks/member 0.0.0-stage → 0.2.36

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 (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +471 -2
  3. package/_dist/asking.d.ts +4 -0
  4. package/_dist/asking.d.ts.map +1 -0
  5. package/_dist/comp.d.ts +14 -0
  6. package/_dist/comp.d.ts.map +1 -0
  7. package/_dist/deny.d.ts +19 -0
  8. package/_dist/deny.d.ts.map +1 -0
  9. package/_dist/graph.d.ts +4 -0
  10. package/_dist/graph.d.ts.map +1 -0
  11. package/_dist/guard.d.ts +45 -0
  12. package/_dist/guard.d.ts.map +1 -0
  13. package/_dist/keywords.d.ts +80 -0
  14. package/_dist/keywords.d.ts.map +1 -0
  15. package/_dist/mod.d.ts +112 -0
  16. package/_dist/mod.d.ts.map +1 -0
  17. package/_dist/owns.d.ts +18 -0
  18. package/_dist/owns.d.ts.map +1 -0
  19. package/_dist/pace.d.ts +48 -0
  20. package/_dist/pace.d.ts.map +1 -0
  21. package/_dist/personal.d.ts +46 -0
  22. package/_dist/personal.d.ts.map +1 -0
  23. package/_dist/plugin.d.ts +32 -0
  24. package/_dist/plugin.d.ts.map +1 -0
  25. package/_dist/policy.d.ts +35 -0
  26. package/_dist/policy.d.ts.map +1 -0
  27. package/_dist/privacy.d.ts +55 -0
  28. package/_dist/privacy.d.ts.map +1 -0
  29. package/_dist/roles.d.ts +20 -0
  30. package/_dist/roles.d.ts.map +1 -0
  31. package/_dist/vocab.d.ts +10 -0
  32. package/_dist/vocab.d.ts.map +1 -0
  33. package/_dist/words.d.ts +53 -0
  34. package/_dist/words.d.ts.map +1 -0
  35. package/asking.js +65 -0
  36. package/asking.js.map +1 -0
  37. package/asking.ts +77 -0
  38. package/comp.js +43 -0
  39. package/comp.js.map +1 -0
  40. package/comp.ts +52 -0
  41. package/deno.json +1 -0
  42. package/deny.js +31 -0
  43. package/deny.js.map +1 -0
  44. package/deny.ts +45 -0
  45. package/graph.js +6 -0
  46. package/graph.js.map +1 -0
  47. package/graph.ts +16 -0
  48. package/guard.js +265 -0
  49. package/guard.js.map +1 -0
  50. package/guard.ts +327 -0
  51. package/keywords.js +170 -0
  52. package/keywords.js.map +1 -0
  53. package/keywords.ts +190 -0
  54. package/meta/member.vocab.json +70 -0
  55. package/mod.js +112 -0
  56. package/mod.js.map +1 -0
  57. package/mod.ts +113 -0
  58. package/owns.js +12 -0
  59. package/owns.js.map +1 -0
  60. package/owns.ts +23 -0
  61. package/pace.js +189 -0
  62. package/pace.js.map +1 -0
  63. package/pace.ts +195 -0
  64. package/package.json +33 -4
  65. package/personal.js +448 -0
  66. package/personal.js.map +1 -0
  67. package/personal.ts +550 -0
  68. package/plugin.js +63 -0
  69. package/plugin.js.map +1 -0
  70. package/plugin.ts +62 -0
  71. package/policy.js +115 -0
  72. package/policy.js.map +1 -0
  73. package/policy.ts +176 -0
  74. package/privacy.js +293 -0
  75. package/privacy.js.map +1 -0
  76. package/privacy.ts +379 -0
  77. package/roles.js +164 -0
  78. package/roles.js.map +1 -0
  79. package/roles.ts +245 -0
  80. package/vocab.js +19 -0
  81. package/vocab.js.map +1 -0
  82. package/vocab.json +272 -0
  83. package/vocab.ts +20 -0
  84. package/words.js +75 -0
  85. package/words.js.map +1 -0
  86. package/words.ts +123 -0
@@ -0,0 +1,80 @@
1
+ import type { Keywords, Vocab, VocabDoc } from "@yaks/vocab";
2
+ import { type Asked } from "./asking.js";
3
+ import { type Floors } from "./words.js";
4
+ /** The URI a vocabulary file declares under `$vocabulary` to use member keywords. */ export declare let MEMBER_URI: string;
5
+ /**
6
+ * The member keyword vocabulary, ready to register:
7
+ * `loadVocab(docs, [memberKeywords])` carries each component's declaration onto
8
+ * `v.comp(name).keywords.floor`, which is where {@link floorsIn} reads it.
9
+ */ export declare let memberKeywords: Keywords;
10
+ /**
11
+ * The floors a loaded vocabulary declares, component → floor.
12
+ *
13
+ * ```ts
14
+ * import { assertEquals } from '@std/assert'
15
+ * import { loadVocab } from '@yaks/vocab'
16
+ * import { floorsIn, memberKeywords } from '@yaks/member'
17
+ * let v = loadVocab({
18
+ * $defs: {
19
+ * line: { component: true, type: 'object', floor: 'person' },
20
+ * note: { component: true, type: 'object' },
21
+ * },
22
+ * }, [memberKeywords])
23
+ * assertEquals(floorsIn(v), { line: 'person' })
24
+ * ```
25
+ */ export declare let floorsIn: (v: Vocab) => Floors;
26
+ /**
27
+ * What a loaded vocabulary asks of readers, component → a floor, or the
28
+ * queries that choose one (./asking.ts).
29
+ *
30
+ * ```ts
31
+ * import { assertEquals } from '@std/assert'
32
+ * import { loadVocab } from '@yaks/vocab'
33
+ * import { memberKeywords, readersIn } from '@yaks/member'
34
+ * let v = loadVocab({
35
+ * $defs: {
36
+ * signup: { component: true, type: 'object', readers: 'viewer' },
37
+ * note: { component: true, type: 'object' },
38
+ * },
39
+ * }, [memberKeywords])
40
+ * assertEquals(readersIn(v), { signup: 'viewer' })
41
+ * ```
42
+ */ export declare let readersIn: (v: Vocab) => Record<string, Asked>;
43
+ /**
44
+ * The floor each component a loaded vocabulary declares `shown` asks of
45
+ * whoever reads it, component → floor.
46
+ *
47
+ * ```ts
48
+ * import { assertEquals } from '@std/assert'
49
+ * import { loadVocab } from '@yaks/vocab'
50
+ * import { memberKeywords, shownIn } from '@yaks/member'
51
+ * let v = loadVocab({
52
+ * $defs: {
53
+ * interests: { component: true, type: 'object', shown: 'editor' },
54
+ * note: { component: true, type: 'object' },
55
+ * },
56
+ * }, [memberKeywords])
57
+ * assertEquals(shownIn(v), { interests: 'editor' })
58
+ * ```
59
+ */ export declare let shownIn: (v: Vocab) => Floors;
60
+ /**
61
+ * Every `floor`, `readers` or `shown` a document declares that is not a floor, or a
62
+ * `readers` condition that is no query of the component, as sentences, the way
63
+ * @yaks/vocab's `storable` answers.
64
+ *
65
+ * ```ts
66
+ * import { assertEquals } from '@std/assert'
67
+ * import { floored } from '@yaks/member'
68
+ * assertEquals(floored({ $defs: { line: { floor: 'admin', readers: 'all' } } }), [
69
+ * 'line is floored "admin" — a floor is person, viewer, editor or owner',
70
+ * 'line is read by "all" — readers is person, viewer, editor or owner',
71
+ * ])
72
+ * ```
73
+ */ export declare let floored: (d: VocabDoc) => string[];
74
+ /** The `readers` conditions that name a property the loaded vocabulary does
75
+ * not declare, as sentences: what {@link floored} cannot see in a document
76
+ * alone. */ export declare let conditioned: (v: Vocab) => string[];
77
+ /** Declaring component → companion mark → its local reference property. */ export type Permits = Record<string, Record<string, string>>;
78
+ /** Errors in `permit` declarations, checked against the whole loaded vocabulary. */ export declare let permitted: (v: Vocab) => string[];
79
+ /** Read valid companion permissions; a malformed declaration never grants writes. */ export declare let permitsIn: (v: Vocab) => Permits;
80
+ //# sourceMappingURL=keywords.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keywords.d.ts","sources":["../keywords.ts"],"names":[],"mappings":"AAmBA,cAAc,QAAQ,EAAE,KAAK,EAAE,QAAQ,2BAAgC;AAGvE,SAAS,KAAK,KAAK,sBAAmD;AACtE,SAAgB,KAAK,MAAM,qBAA6B;AAExD,mFAAmF,GACnF,OAAO,YAAI,mBAA0C;AAErD;;;;CAIC,GACD,OAAO,YAAI,gBAAgB,SAI1B;AAWD;;;;;;;;;;;;;;;CAeC,GACD,OAAO,YAAI,WAAY,GAAG,UAAQ,OAAiC;AAEnE;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,YAAI,YAAa,GAAG,UAAQ,OAAO,MAAM,EAAE,OACrB;AAE7B;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,YAAI,UAAW,GAAG,UAAQ,OAAiC;AAElE;;;;;;;;;;;;;CAaC,GACD,OAAO,YAAI,UAAW,GAAG,aAAW,MAAM,GAWtC;AAEJ;;UAEU,GACV,OAAO,YAAI,cAAe,GAAG,UAAQ,MAAM,GAQvC;AAEJ,yEAAyE,GACzE,YAAY,UAAU,OAAO,MAAM,EAAE,OAAO,MAAM,EAAE,MAAM;AAK1D,kFAAkF,GAClF,OAAO,YAAI,YAAa,GAAG,UAAQ,MAAM,GA2BrC;AAEJ,mFAAmF,GACnF,OAAO,YAAI,YAAa,GAAG,UAAQ,QAOlC"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,112 @@
1
+ /**
2
+ * @yaks/member — access control for a
3
+ * {@link https://jsr.io/@yaks/graph | @yaks/graph}: who belongs to a space, and
4
+ * what each of them may do.
5
+ *
6
+ * A **principal** is the entity acting — a person, or the grant behind a share
7
+ * link. An **app** is any entity access is decided about; this package never
8
+ * declares what an app is. Three components answer three questions:
9
+ *
10
+ * - **Who belongs?** A `member{space, person, role}` row puts one person on a
11
+ * space's roster. `owner` runs the space; `member` belongs to it.
12
+ * - **What may they do?** A `grant{app, person, access}` gives one principal
13
+ * one level — `owner`, `editor` or `viewer` — on one app.
14
+ * - **And everyone else?** An `access{mode}` on the app declares `public`
15
+ * (anyone with the link reads it), `open` (anyone with the link reads AND
16
+ * writes it, signed in or not), or `private` (only principals holding a
17
+ * permission see it at all).
18
+ *
19
+ * ## Membership is not permission
20
+ * A row on the roster gives nothing by itself. A member with no grant reaches
21
+ * an app exactly as far as a stranger with the link does — which is what makes
22
+ * a roster safe to be generous with. The one shortcut is the space's **owner**,
23
+ * who holds `owner` on every app in it, never stored per app. Removing someone
24
+ * is then one row: delete the membership and every permission implied by it
25
+ * goes too.
26
+ *
27
+ * ## The three checks
28
+ * ```text
29
+ * read the mode is not `private`, or the principal holds any level
30
+ * write the mode is `open`, or the principal holds owner or editor
31
+ * call out the credential is open to anyone, or the principal holds any level
32
+ * ```
33
+ * A `viewer` never writes, under any mode. A `member` who was never granted
34
+ * anything holds no level at all.
35
+ *
36
+ * ## Where each check runs
37
+ * A write is refused inside `apply()`: {@link members} registers a
38
+ * `precondition` hook, which runs inside the transaction before any row has
39
+ * moved, so a refused batch rolls back whole ({@link Denied}). A read
40
+ * never reaches `apply()`, so the HTTP layer checks first —
41
+ * {@link policy}`(storage).canRead(who, app)`.
42
+ *
43
+ * ```ts
44
+ * import { loadVocab } from '@yaks/vocab'
45
+ * import { graph, mint } from '@yaks/graph'
46
+ * import { ram } from '@yaks/ram'
47
+ * import { memberDoc, members, policy } from '@yaks/member'
48
+ *
49
+ * let vocab = loadVocab([memberDoc])
50
+ * let storage = ram(vocab)
51
+ * let g = graph({ storage, vocab })
52
+ * let club = mint()
53
+ * let dana = mint()
54
+ * let list = mint()
55
+ * await g.apply([
56
+ * { entity: { eid: list }, access: { mode: 'private' } },
57
+ * {
58
+ * entity: { eid: 'm1' },
59
+ * member: { space: club, person: dana, role: 'owner' },
60
+ * },
61
+ * ])
62
+ * g.use(members({ app: list, space: club, vocab }))
63
+ *
64
+ * let may = policy(storage, { space: club })
65
+ * await may.canRead(dana, list) // true
66
+ * ```
67
+ *
68
+ * ## Only an owner edits the access rows
69
+ * Writing a `member`, a `grant` or an `access` requires `owner` — an editor
70
+ * writes the app's data and does not hand out permissions. That matters most on
71
+ * an `open` app, where anyone may write: without the rule, a visitor invited to
72
+ * sign the guest book could rewrite the roster. The first owner is therefore
73
+ * written before the guard is installed; see {@link members}.
74
+ *
75
+ * ## A component says who writes it, and how often
76
+ * A component may declare a `floor`, the least its writer holds whatever the
77
+ * mode — `person` (anyone signed in), `viewer`, `editor` or `owner` — and a
78
+ * `pace`, how often each writer may write it. The guard refuses the rest:
79
+ * {@link Denied} naming the component, or {@link Paced} saying when to try
80
+ * again. Load the vocabulary with {@link memberKeywords} so `floor` is kept.
81
+ *
82
+ * ## Share links
83
+ * A grant may name a `token` instead of a person. Whoever opens that link acts
84
+ * as the grant, so the HTTP layer signs their bundles with the grant's own
85
+ * entity id and everything above works unchanged — no account, no roster row,
86
+ * one revocable row.
87
+ *
88
+ * ## What is deliberately not here
89
+ * Authentication: establishing who someone is belongs to the HTTP layer.
90
+ * Invitations: a new `member` row usually means messaging somebody, which is a
91
+ * `created('member')` handler on
92
+ * {@link https://jsr.io/@yaks/effects | @yaks/effects} — the hook this package
93
+ * leaves for `@yaks/mail` to register. And per-grant filters (a grant good for
94
+ * only part of an app's data) — one level per app, on purpose.
95
+ *
96
+ * It imports no platform API, so the same rules run on a server, in a worker,
97
+ * and in a browser tab.
98
+ *
99
+ * @module
100
+ */ export * from "./words.js";
101
+ export * from "./comp.js";
102
+ export * from "./keywords.js";
103
+ export * from "./policy.js";
104
+ export * from "./deny.js";
105
+ export * from "./pace.js";
106
+ export * from "./guard.js";
107
+ export * from "./plugin.js";
108
+ export * from "./personal.js";
109
+ export * from "./privacy.js";
110
+ export * from "./owns.js";
111
+ export * from "./roles.js";
112
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmGC,GAED,2BAA0B;AAC1B,0BAAyB;AACzB,8BAA6B;AAC7B,4BAA2B;AAC3B,0BAAyB;AACzB,0BAAyB;AACzB,2BAA0B;AAC1B,4BAA2B;AAC3B,8BAA6B;AAC7B,6BAA4B;AAC5B,0BAAyB;AACzB,2BAA0B"}
@@ -0,0 +1,18 @@
1
+ /** A verified person and instrument, or the ownership recorded for a row. */ export type Owner = {
2
+ by?: string | null;
3
+ via?: string | null;
4
+ };
5
+ /** A bylined row belongs to its person; an unbylined row belongs to its
6
+ * instrument, including after that browser signs in. Instruments never grant
7
+ * access to a row bound to another person.
8
+ *
9
+ * ```ts
10
+ * import { owns } from '@yaks/member'
11
+ * owns({ by: 'ana', via: 'browser' }, { by: 'bo', via: 'browser' }) // false
12
+ * owns({ via: 'browser' }, { by: 'ana', via: 'browser' }) // true
13
+ * owns({}, {}) // false
14
+ * ```
15
+ */ export declare let owns: (owner: Owner, who: Owner & {
16
+ browsers?: readonly string[];
17
+ }) => boolean;
18
+ //# sourceMappingURL=owns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"owns.d.ts","sources":["../owns.ts"],"names":[],"mappings":"AAAA,2EAA2E,GAC3E,YAAY;EAAU,KAAK,MAAM,GAAG,IAAI;EAAE,MAAM,MAAM,GAAG,IAAI;;AAE7D;;;;;;;;;;CAUC,GACD,OAAO,YAAI,OACT,OAAO,OACP,KAAK;EAAU,oBAAoB,MAAM;MACxC,OAAO,CAKmB"}
@@ -0,0 +1,48 @@
1
+ import type { Bundle, Eid, ReadTx } from "@yaks/graph";
2
+ import { type Vocab } from "@yaks/vocab";
3
+ /** How often one via writes one entity’s component, in ms, by name. */ export type Paces = Record<string, number>;
4
+ export type Instrument = (via: Eid) => Eid;
5
+ /** A write refused for coming sooner than its component's pace. */ export declare class Paced extends Error {
6
+ actor: Eid | null;
7
+ comp: string;
8
+ pace: number;
9
+ wait: number;
10
+ entity?: Eid;
11
+ /**
12
+ * @param actor the writing instrument, or `null` when no via was supplied
13
+ * @param comp the component they wrote
14
+ * @param pace how often one writer may write it, in ms
15
+ * @param wait how long until they may again, in ms
16
+ */ constructor(actor: Eid | null, comp: string, pace: number, wait: number, entity?: Eid);
17
+ }
18
+ /**
19
+ * The paces a loaded vocabulary declares on stored components. A relayed one
20
+ * is @yaks/sync's to keep: its value never reaches a store.
21
+ *
22
+ * ```ts
23
+ * import { assertEquals } from '@std/assert'
24
+ * import { loadVocab } from '@yaks/vocab'
25
+ * import { pacesIn } from '@yaks/member'
26
+ * let v = loadVocab({
27
+ * $defs: {
28
+ * line: { component: true, type: 'object', pace: '1s' },
29
+ * spot: {
30
+ * component: true,
31
+ * type: 'object',
32
+ * sync: 'peers',
33
+ * durable: 'connection',
34
+ * pace: '100ms',
35
+ * },
36
+ * },
37
+ * })
38
+ * assertEquals(pacesIn(v), { line: 1000 })
39
+ * ```
40
+ */ export declare let pacesIn: (v: Vocab) => Paces;
41
+ /**
42
+ * Refuse writes sooner than the component's pace permits their via on that
43
+ * entity. Append the accepted clocks as server-owned patches for the graph to
44
+ * persist with the batch. Distinct entities, components and vias hold
45
+ * independent clocks; all writers with no via share one on each component.
46
+ * `now` is the moment the batch is taken, in ms.
47
+ */ export declare let pacing: (paces: Paces, tx: ReadTx, bundles: Bundle[], now?: number, instrument?: Instrument) => Bundle[] | Promise<Bundle[]>;
48
+ //# sourceMappingURL=pace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pace.d.ts","sources":["../pace.ts"],"names":[],"mappings":"AAeA,cAAc,MAAM,EAAQ,GAAG,EAAE,MAAM,2BAAgC;AAKvE,SAAyB,KAAK,KAAK,2BAAgC;AAEnE,qEAAqE,GACrE,YAAY,QAAQ,OAAO,MAAM,EAAE,MAAM;AACzC,YAAY,cAAc,KAAK,QAAQ;AAEvC,iEAAiE,GACjE,OAAO,cAAM,cAAc;EAQhB,OAAO,MAAM,IAAI;EACjB,MAAM,MAAM;EACZ,MAAM,MAAM;EACZ,MAAM,MAAM;EACZ,SAAS;EAXlB;;;;;GAKC,GACD,YACE,AAAO,OAAO,MAAM,IAAI,EACxB,AAAO,MAAM,MAAM,EACnB,AAAO,MAAM,MAAM,EACnB,AAAO,MAAM,MAAM,EACnB,AAAO,SAAS,GAAG;AAQvB;AAMA;;;;;;;;;;;;;;;;;;;;;;CAsBC,GACD,OAAO,YAAI,UAAW,GAAG,UAAQ,MAM9B;AAsDH;;;;;;CAMC,GACD,OAAO,YAAI,SACT,OAAO,OACP,IAAI,QACJ,SAAS,UACT,MAAK,MAAM,EACX,aAAa,eACZ,WAAW,QAAQ,UA6CrB"}
@@ -0,0 +1,46 @@
1
+ import type { Plugin } from "@yaks/graph";
2
+ import type { Vocab } from "@yaks/vocab";
3
+ import { type Declared } from "./privacy.js";
4
+ /** Component → owner: `entity` keeps just this aspect private on the person's
5
+ * entity; a local reference property makes the entire entity private. */ export type Personal = Record<string, string>;
6
+ /** Derived component → reference property whose source owns its visibility. */ export type PersonalSources = Record<string, string>;
7
+ /**
8
+ * Every `personal` declaration no row could be kept private by, as sentences
9
+ * naming the component and what to write instead, the way @yaks/vocab's
10
+ * `storable` answers. A deploy holding any is refused, and
11
+ * {@link personalsIn} serves none.
12
+ *
13
+ * ```ts
14
+ * import { assertEquals } from '@std/assert'
15
+ * import { loadVocab, type PropSchema } from '@yaks/vocab'
16
+ * import { memberKeywords, unowned } from '@yaks/member'
17
+ * let note = (by: PropSchema) =>
18
+ * loadVocab({
19
+ * $defs: {
20
+ * note: {
21
+ * component: true,
22
+ * type: 'object',
23
+ * personal: 'by',
24
+ * properties: { by },
25
+ * },
26
+ * },
27
+ * }, [memberKeywords])
28
+ * assertEquals(unowned(note({ type: 'string', ref: 'entity', death: 'keep' })), [])
29
+ * assertEquals(unowned(note({ type: 'string' })), [
30
+ * 'note.personal names by, which is no reference — declare "by": ' +
31
+ * '{"type": "string", "ref": "entity", "death": "keep"}, or say ' +
32
+ * '"personal": "entity" for a component on the person\'s own entity',
33
+ * ])
34
+ * ```
35
+ */ export declare let unowned: (vocab: Vocab, overrides?: Personal) => string[];
36
+ /** Read the policy declarations, refusing the vocabulary when any is no
37
+ * owner ({@link unowned}) before a row is served. */ export declare let personalsIn: (vocab: Vocab, overrides?: Personal) => Personal;
38
+ /** What an entity's privacy is reckoned from (./privacy.ts), as
39
+ * {@link personal} is given it: the vocabulary's `personal` and `readers`
40
+ * declarations, the host's own, and what inherits its source's. */ export declare let declarationsOf: (vocab: Vocab, declared?: Personal, through?: PersonalSources) => Declared;
41
+ /** Protect person-owned data at reads and at every transaction write,
42
+ * including changes made by rules and reference cascades, and keep each entity
43
+ * wearing a component its vocabulary declares `readers` from every reader
44
+ * below that floor. Trusted server writes and explicitly vouched
45
+ * administrative reads are the only bypasses. */ export declare let personal: (vocab: Vocab, declared?: Personal, through?: PersonalSources) => Plugin;
46
+ //# sourceMappingURL=personal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"personal.d.ts","sources":["../personal.ts"],"names":[],"mappings":"AAOA,cAIE,MAAM,2BAIuB;AAC/B,cAAc,KAAK,2BAAgC;AAInD,SAAS,KAAK,QAAQ,uBAAqC;AAG3D;uEACuE,GACvE,YAAY,WAAW,OAAO,MAAM,EAAE,MAAM;AAE5C,6EAA6E,GAC7E,YAAY,kBAAkB,OAAO,MAAM,EAAE,MAAM;AAiBnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4BC,GACD,OAAO,YAAI,UAAW,OAAO,OAAO,YAAW,aAAgB,MAAM,GAgBjE;AAEJ;mDACmD,GACnD,OAAO,YAAI,cAAe,OAAO,OAAO,YAAW,aAAgB,SAIlE;AASD;;iEAEiE,GACjE,OAAO,YAAI,iBACT,OAAO,OACP,WAAU,UACV,UAAS,oBACR,SAOD;AAEF;;;;+CAI+C,GAC/C,OAAO,YAAI,WACT,OAAO,OACP,WAAU,UACV,UAAS,oBACR,OAiaF"}
@@ -0,0 +1,32 @@
1
+ import type { Plugin } from "@yaks/graph";
2
+ import { type Guard } from "./guard.js";
3
+ /**
4
+ * The membership plugin: the `member`, `grant` and `access` components, and a
5
+ * `precondition` hook that refuses a write the principal's role or grant does
6
+ * not allow, or that comes sooner than its component's pace.
7
+ *
8
+ * ```ts
9
+ * import { loadVocab } from '@yaks/vocab'
10
+ * import { graph } from '@yaks/graph'
11
+ * import { ram } from '@yaks/ram'
12
+ * import { memberDoc, memberKeywords, members } from '@yaks/member'
13
+ *
14
+ * let vocab = loadVocab([memberDoc], [memberKeywords])
15
+ * let guard = members({ app: 'list', space: 'club', vocab })
16
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [guard] })
17
+ * ```
18
+ *
19
+ * Reads are not checked here — a query never reaches `apply()`. The HTTP layer
20
+ * calls
21
+ * {@link https://jsr.io/@yaks/member/doc/~/policy | policy}`(storage).canRead`
22
+ * before it answers one.
23
+ *
24
+ * ## The effect hook this package leaves unregistered
25
+ * Adding someone to a roster usually means messaging them. That is a
26
+ * `created('member')` handler on
27
+ * {@link https://jsr.io/@yaks/effects | @yaks/effects} — it runs after the
28
+ * commit, so the row exists before the message goes out, and it is isolated, so
29
+ * a mail server that is down does not refuse the write. This package ships no
30
+ * such handler; `@yaks/mail` provides one.
31
+ */ export declare let members: (where: Guard) => Plugin;
32
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sources":["../plugin.ts"],"names":[],"mappings":"AAsBA,cAAc,MAAM,2BAAgC;AAEpD,SAAS,KAAK,KAAK,qBAAuC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4BC,GACD,OAAO,YAAI,UAAW,OAAO,UAAQ,OAMnC"}
@@ -0,0 +1,35 @@
1
+ import type { Bundle, Eid, ReadTx, Storage } from "@yaks/graph";
2
+ import { type Level, type Mode } from "./words.js";
3
+ /** Who is acting: the entity the HTTP layer signed the request with, or `null`
4
+ * for nobody — an anonymous visitor with only the link. */ export type Viewer = Eid | null;
5
+ /** Where the roster and the grants are read from, and which space's owners
6
+ * count. */ export type Where = {
7
+ /** the space whose owners own every app in it. Omit it and only grants
8
+ * count — a graph holding one app and no roster needs no space. */ space?: Eid;
9
+ /** Standing rows superseded by an authoritative source are left out. Other
10
+ * stored grants and memberships still participate in the same policy. */ ignore?: (row: Bundle) => boolean;
11
+ };
12
+ /** The checks the HTTP layer and the write guard both call. Each returns a
13
+ * value rather than a promise over a synchronous storage. */ export type Policy = {
14
+ /** the app's mode — `private` when it has no `access` component */ modeOf: (app: Eid) => Mode | Promise<Mode>;
15
+ /** what this principal holds on this app, or `null` for nothing */ levelOf: (who: Viewer, app: Eid) => Level | null | Promise<Level | null>;
16
+ /** may they read it? */ canRead: (who: Viewer, app: Eid) => boolean | Promise<boolean>;
17
+ /** may they write it? */ canWrite: (who: Viewer, app: Eid) => boolean | Promise<boolean>;
18
+ };
19
+ /** The app's access mode, read through a transaction: its `access` and
20
+ * nothing else, since this runs on every request and a whole read of an
21
+ * entity can ask every table the vocabulary has. */ export declare let modeOn: (tx: ReadTx, app: Eid) => Mode | Promise<Mode>;
22
+ /**
23
+ * What `who` holds on `app`, read through a transaction, following the
24
+ * resolution order above step by step. Returns `null` when they hold nothing —
25
+ * which is not a refusal, only the answer that the app's mode decides.
26
+ */ export declare let levelOn: (tx: ReadTx, who: Viewer, app: Eid, where?: Where) => Level | null | Promise<Level | null>;
27
+ /** May they read it? The mode is not `private`, or they hold something. */ export declare let readsOn: (tx: ReadTx, who: Viewer, app: Eid, where?: Where) => boolean | Promise<boolean>;
28
+ /** May they write it? The mode is `open`, or they hold owner or editor. */ export declare let writesOn: (tx: ReadTx, who: Viewer, app: Eid, where?: Where) => boolean | Promise<boolean>;
29
+ /**
30
+ * The read-side helper, bound to a storage: what the HTTP layer calls before it
31
+ * answers a query, since a read never reaches `apply()` and so is never seen by
32
+ * the write guard: with `may = policy(storage, { space: club })`, a door
33
+ * answers 404 where `may.canRead(who, app)` is false.
34
+ */ export declare let policy: (storage: Storage, where?: Where) => Policy;
35
+ //# sourceMappingURL=policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.d.ts","sources":["../policy.ts"],"names":[],"mappings":"AAmCA,cAAc,MAAM,EAAQ,GAAG,EAAE,MAAM,EAAE,OAAO,2BAAgC;AAKhF,SAAgB,KAAK,KAAK,EAAS,KAAK,IAAI,qBAAiC;AAE7E;yDACyD,GACzD,YAAY,SAAS,MAAM,IAAI;AAE/B;UACU,GACV,YAAY;EACV;mEACiE,GACjE,QAAQ;EACR;yEACuE,GACvE,UAAU,KAAK,WAAW,OAAO;;AAGnC;2DAC2D,GAC3D,YAAY;EACV,iEAAiE,GACjE,SAAS,KAAK,QAAQ,OAAO,QAAQ;EACrC,iEAAiE,GACjE,UAAU,KAAK,QAAQ,KAAK,QAAQ,QAAQ,IAAI,GAAG,QAAQ,QAAQ,IAAI;EACvE,sBAAsB,GACtB,UAAU,KAAK,QAAQ,KAAK,QAAQ,OAAO,GAAG,QAAQ,OAAO;EAC7D,uBAAuB,GACvB,WAAW,KAAK,QAAQ,KAAK,QAAQ,OAAO,GAAG,QAAQ,OAAO;;AAOhE;;kDAEkD,GAClD,OAAO,YAAI,SAAU,IAAI,QAAQ,KAAK,QAAM,OAAO,QAAQ,MACS;AAqBpE;;;;CAIC,GACD,OAAO,YAAI,UACT,IAAI,QACJ,KAAK,QACL,KAAK,KACL,QAAO,UACN,QAAQ,IAAI,GAAG,QAAQ,QAAQ,IAAI,EAsBrC;AAED,yEAAyE,GACzE,OAAO,YAAI,UACT,IAAI,QACJ,KAAK,QACL,KAAK,KACL,QAAO,UACN,OAAO,GAAG,QAAQ,OAAO,EAOK;AAEjC,yEAAyE,GACzE,OAAO,YAAI,WACT,IAAI,QACJ,KAAK,QACL,KAAK,KACL,QAAO,UACN,OAAO,GAAG,QAAQ,OAAO,EAKK;AAEjC;;;;;CAKC,GACD,OAAO,YAAI,SAAU,SAAS,SAAS,QAAO,UAAa,OAQ1D"}
@@ -0,0 +1,55 @@
1
+ import { type And, type Built } from "@yaks/query/core";
2
+ import type { Bundle, Comp, ReadOpts, Tracker, Tx } from "@yaks/graph";
3
+ import type { Vocab } from "@yaks/vocab";
4
+ import { type Asked } from "./asking.js";
5
+ import { type Level } from "./words.js";
6
+ /** What a store answers, now or later: a synchronous store's transaction
7
+ * stays synchronous through here (a Durable Object's `transactionSync`). */ export type Maybe<T> = T | Promise<T>;
8
+ type Read = (line: Built, opts: ReadOpts, comps: string[]) => Maybe<Bundle[]>;
9
+ /** The component an entity's privacy is kept in. */ export declare let PRIVACY: string;
10
+ /** The declarations an entity's privacy is reckoned from: who owns each
11
+ * `personal` component, the reference each inherited one reads its source
12
+ * through, and what each `readers` one asks, which the `vocab` matches the
13
+ * queries of one against an entity's bundle. */ export type Declared = {
14
+ owners: [string, string][];
15
+ through: [string, string][];
16
+ floors: [string, Asked][];
17
+ vocab?: Vocab;
18
+ };
19
+ /** What the declarations make of entities, and how a store keeps it. */ export type Privacy = {
20
+ /** The declarations', which each privacy they reckon keeps. */ hash: string;
21
+ /** What an entity's privacy is read from. */ bearing: string[];
22
+ /** What a source lends the entities inheriting from it. */ part: string[];
23
+ /** The privacy an entity makes, its sources read through `at`. */ of: (e: Bundle | undefined, at: (eid: string) => Bundle | undefined) => Comp | null;
24
+ /** The sources an entity inherits from. */ sources: (e: Bundle) => string[];
25
+ /** The privacy patches entities owe. */ reckon: (get: (eids: string[], comps: string[]) => Maybe<Bundle[]>, eids: string[]) => Maybe<Bundle[]>;
26
+ /** Bounded disjunctions that find stale entities, one seek per arm. */ stale: Built[];
27
+ /** Whether a store holds no stale entity. */ current: (read: Read) => Maybe<boolean>;
28
+ /** The entities a reader may read by their privacy. */ seen: (who: string | null | undefined, level: Level | null) => And;
29
+ /** The rule that keeps every entity's privacy in a transaction. */ keeping: (tx: Tx, below?: Tx, settling?: () => void) => Tracker;
30
+ };
31
+ /**
32
+ * The privacy these declarations reckon, for a vocabulary that keeps it.
33
+ *
34
+ * ```ts
35
+ * import { assertEquals } from '@std/assert'
36
+ * import { privacy } from '@yaks/member'
37
+ * let p = privacy({
38
+ * owners: [['draft', 'by']],
39
+ * through: [['page', 'document']],
40
+ * floors: [['order', 'editor']],
41
+ * })
42
+ * let docs = { d: { entity: { eid: 'd' }, draft: { by: 'ana' } } }
43
+ * let page = { entity: { eid: 'p' }, page: { document: 'd' }, order: {} }
44
+ * assertEquals(p.of(page, (eid) => docs[eid as 'd']), {
45
+ * person: 'ana',
46
+ * shut: null,
47
+ * signed: null,
48
+ * floor: 'editor',
49
+ * hash: p.hash,
50
+ * })
51
+ * assertEquals(p.of({ entity: { eid: 'n' }, note: {} }, () => undefined), null)
52
+ * ```
53
+ */ export declare let privacy: (declared: Declared) => Privacy;
54
+ /** Whether a vocabulary keeps privacy: it declares `_privacy`. */ export declare let kept: (vocab: Vocab) => boolean;
55
+ //# sourceMappingURL=privacy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"privacy.d.ts","sources":["../privacy.ts"],"names":[],"mappings":"AAmBA,SAEE,KAAK,GAAG,EAER,KAAK,KAAK,gCAMw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@ -0,0 +1,20 @@
1
+ import { type Actor, type Bundle, type Plugin, type ReadTx } from "@yaks/graph";
2
+ import { type Query } from "@yaks/query";
3
+ import type { Vocab } from "@yaks/vocab";
4
+ import { type Level } from "./words.js";
5
+ /** The rows shared by a published app and its installed copies. */ export declare let ROLE_DEFINITIONS: readonly [string, string];
6
+ export declare let builtIn: (name: string) => name is Level;
7
+ export declare let roleRows: (tx: Pick<ReadTx, 'read'>) => Bundle[] | Promise<Bundle[]>;
8
+ /** The highest level among the roles the app recognizes. */ export declare let roleLevel: (names: readonly string[], rows?: Bundle[]) => Level | null;
9
+ /** An ability selects entities using the same predicates as a graph query. */ export declare let abilityQuery: (vocab: Vocab, query: string) => Query;
10
+ type Holder = {
11
+ person: string;
12
+ roles: readonly string[];
13
+ };
14
+ type AppRoles = {
15
+ app: string;
16
+ vocab: Vocab;
17
+ holder: (actor: Actor) => Holder | undefined;
18
+ };
19
+ /** Scope private reads and check writes in both transaction states. */ export declare let appRoles: ({}: AppRoles) => Plugin;
20
+ //# sourceMappingURL=roles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.d.ts","sources":["../roles.ts"],"names":[],"mappings":"AAEA,SACE,KAAK,KAAK,EACV,KAAK,MAAM,EAKX,KAAK,MAAM,EACX,KAAK,MAAM,2BAGkB;AAE/B,SAQE,KAAK,KAAK,2BACmB;AAC/B,cAAc,KAAK,2BAAgC;AAGnD,SAAS,KAAK,KAAK,qBAAoC;AAEvD,iEAAiE,GACjE,OAAO,YAAI,4CAA8C;AAEzD,OAAO,YAAI,UAAW,MAAM,MAAM,KAAG,QAAQ,MACb;AAEhC,OAAO,YAAI,WACT,IAAI,KAAK,QAAQ,YAChB,WAAW,QAAQ,UACiD;AAEvE,0DAA0D,GAC1D,OAAO,YAAI,YACT,gBAAgB,MAAM,IACtB,OAAM,aACL,QAAQ,IAAI,CAYd;AAED,4EAA4E,GAC5E,OAAO,YAAI,eAAgB,OAAO,OAAO,OAAO,MAAM,KAAG,MAaxD;KAwCI;EAAW,QAAQ,MAAM;EAAE,gBAAgB,MAAM;;KACjD;EACH,KAAK,MAAM;EACX,OAAO;EACP,SAAS,OAAO,UAAU,SAAS,SAAS;;AAqB9C,qEAAqE,GACrE,OAAO,YAAI,WAAY,IAAwB,QAAQ,KAAG,OAyGzD"}
@@ -0,0 +1,10 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ import { memberDoc } from "./comp.js";
3
+ export { memberDoc };
4
+ /** What this package is: its manifest's description, the one place it is
5
+ * written, which a host stamps on each document here. */ export declare let description: string;
6
+ /** Every vocabulary document this plugin contributes. */ export declare let docs: VocabDoc[];
7
+ export { memberKeywords } from "./keywords.js";
8
+ import { memberKeywords } from "./keywords.js";
9
+ export declare let keywords: readonly [any];
10
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AACtD,SAAS,SAAS,oBAAmB;AAGrC,SAAS,SAAS,GAAE;AAEpB;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,uDAAuD,GACvD,OAAO,YAAI,MAAM,WAAwB;AAEzC,SAAS,cAAc,wBAAuB;AAC9C,SAAS,cAAc,wBAAuB;AAC9C,OAAO,YAAI,yBAA2B"}
@@ -0,0 +1,53 @@
1
+ /** What a `member` row records. An `owner` runs the space and holds `owner`
2
+ * permission on every app in it; a `member` belongs, and holds only what a
3
+ * grant gives them. */ export type Role = 'owner' | 'member';
4
+ /** What a `grant` gives one principal on one app: `owner` shares and deletes
5
+ * it, `editor` writes it, `viewer` reads it. */ export type Level = 'owner' | 'editor' | 'viewer';
6
+ /** What an app allows everyone with no grant on it:
7
+ *
8
+ * - `public` — anyone with the link reads it; only the granted write.
9
+ * - `open` — anyone with the link reads it and writes it, signed in or not.
10
+ * - `private` — only principals holding a permission see it at all.
11
+ * - `friends` — confirmed friends of a space owner may also read it.
12
+ */ export type Mode = typeof MODES[number];
13
+ /** The roles, least to most. */ export declare let ROLES: Role[];
14
+ /** The levels a grant confers, least to most. */ export declare let LEVELS: Level[];
15
+ /** The modes an app can be in. */ export declare let MODES: readonly ['public', 'open', 'private', 'friends'];
16
+ /** Read a stored `member.role`: an unset one is a plain `member`. */ export declare let role: (v: unknown) => Role;
17
+ /** Read a stored `grant.access`: an unset one is a `viewer` — the least a grant
18
+ * can mean, never the most. */ export declare let level: (v: unknown) => Level;
19
+ /** Read a stored `access.mode`. An app whose mode cannot be read, because its
20
+ * `access` is missing or says something else, is `private`: a lost row locks
21
+ * the app, never opens it. Every app is born with an explicit mode, so report
22
+ * a missing mode as a failure. */ export declare let mode: (v: unknown) => Mode;
23
+ /** May a principal holding this level write? An `owner` and an `editor` write;
24
+ * a `viewer`, and a principal holding nothing, do not. */ export declare let writes: (l: Level | null) => boolean;
25
+ /** Does this level reach that one? Holding nothing reaches nothing. */ export declare let reaches: (l: Level | null, floor: Level) => boolean;
26
+ /** The least a component asks of whoever writes it, whatever the app's mode:
27
+ * a level, or `person` — anyone signed in, level or none. */ export type Floor = 'person' | Level;
28
+ /** The floor each component asks of whoever writes it, by name. */ export type Floors = Record<string, Floor>;
29
+ /** The floors, least to most. */ export declare let FLOORS: Floor[];
30
+ /** Is this a floor? */ export declare let isFloor: (v: unknown) => v is Floor;
31
+ /** Read a declared floor: one this package does not know asks the most. */ export declare let floor: (v: unknown) => Floor;
32
+ /** Does a principal holding this level stand on that floor? Anyone signed in
33
+ * stands on `person`, and nobody stands on any.
34
+ *
35
+ * ```ts
36
+ * import { assertEquals } from '@std/assert'
37
+ * import { stands } from '@yaks/member'
38
+ * assertEquals(stands('kim', null, 'person'), true)
39
+ * assertEquals(stands(null, null, 'person'), false)
40
+ * assertEquals(stands('kim', 'viewer', 'editor'), false)
41
+ * ```
42
+ */ export declare let stands: (who: string | null, l: Level | null, f: Floor) => boolean;
43
+ /** May this principal read the app? Friendship is a read permission on its
44
+ * own, independent of membership or a grant. */ export declare let reads: (m: Mode, l: Level | null, friend?: boolean) => boolean;
45
+ /** May a principal holding this level write an app in this mode? An `open` app
46
+ * is written by anyone, signed in or not; anything else requires owner or
47
+ * editor. */ export declare let edits: (m: Mode, l: Level | null) => boolean;
48
+ /** May a principal holding this level call out through a credential an app
49
+ * uses (egress-use)? Whoever holds any level on the app may; anyone else only
50
+ * through one the app opened to `anyone`, as a public widget's is. The app's
51
+ * mode does not enter into it: the sentinel a caller presents points at a
52
+ * credential and is not one, so it is never enough on its own. */ export declare let callsOut: (anyone: boolean, l: Level | null) => boolean;
53
+ //# sourceMappingURL=words.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"words.d.ts","sources":["../words.ts"],"names":[],"mappings":"AAcA;;qBAEqB,GACrB,YAAY,OAAO,UAAU;AAE7B;8CAC8C,GAC9C,YAAY,QAAQ,UAAU,WAAW;AAEzC;;;;;;CAMC,GACD,YAAY,cAAc,MAAM,MAAM;AAEtC,8BAA8B,GAC9B,OAAO,YAAI,OAAO,OAA4B;AAE9C,+CAA+C,GAC/C,OAAO,YAAI,QAAQ,QAAuC;AAE1D,gCAAgC,GAChC,OAAO,YAAI,iBAAS,UAAU,QAAQ,WAAW,WAAmB;AAEpE,mEAAmE,GACnE,OAAO,YAAI,OAAQ,GAAG,OAAO,KAAG,KAAyC;AAEzE;6BAC6B,GAC7B,OAAO,YAAI,QAAS,GAAG,OAAO,KAAG,MAC6B;AAE9D;;;gCAGgC,GAChC,OAAO,YAAI,OAAQ,GAAG,OAAO,KAAG,KACmB;AAEnD;wDACwD,GACxD,OAAO,YAAI,SAAU,GAAG,QAAQ,IAAI,KAAG,OAAO,CAAiC;AAE/E,qEAAqE,GACrE,OAAO,YAAI,UAAW,GAAG,QAAQ,IAAI,EAAE,OAAO,UAAQ,OAAO,CACJ;AAEzD;2DAC2D,GAC3D,YAAY,QAAQ,WAAW;AAE/B,iEAAiE,GACjE,YAAY,SAAS,OAAO,MAAM,EAAE;AAEpC,+BAA+B,GAC/B,OAAO,YAAI,QAAQ,QAA+B;AAElD,qBAAqB,GACrB,OAAO,YAAI,UAAW,GAAG,OAAO,KAAG,KAAK,MAAoC;AAE5E,yEAAyE,GACzE,OAAO,YAAI,QAAS,GAAG,OAAO,KAAG,MAAiC;AAElE;;;;;;;;;;CAUC,GACD,OAAO,YAAI,SACT,KAAK,MAAM,GAAG,IAAI,EAClB,GAAG,QAAQ,IAAI,EACf,GAAG,UACF,OAAO,CAA+C;AAUzD;8CAC8C,GAC9C,OAAO,YAAI,QAAS,GAAG,MAAM,GAAG,QAAQ,IAAI,EAAE,qBAAiB,OAAO,CACC;AAEvE;;WAEW,GACX,OAAO,YAAI,QAAS,GAAG,MAAM,GAAG,QAAQ,IAAI,KAAG,OAAO,CAC5B;AAE1B;;;;gEAIgE,GAChE,OAAO,YAAI,WAAY,QAAQ,OAAO,EAAE,GAAG,QAAQ,IAAI,KAAG,OAAO,CAC5C"}
package/asking.js ADDED
@@ -0,0 +1,65 @@
1
+ // What a `readers` declaration asks of a reader, for the entity wearing it.
2
+ // A floor asks it of every such entity; an object maps floors to queries, and
3
+ // an entity asks the highest floor whose query its own bundle matches, or
4
+ // nothing when it matches none: `{"editor": ".individual.status!=member"}`.
5
+ import { filter } from "@yaks/match";
6
+ import { parse } from "@yaks/query";
7
+ import { floor, FLOORS, isFloor } from "./words.js";
8
+ let conditional = (v)=>!!v && typeof v == 'object' && !Array.isArray(v);
9
+ /** A declared `readers` as {@link Asked}: a floor this package does not know
10
+ * asks the most. */ export let askedOf = (said)=>conditional(said) ? Object.fromEntries(Object.entries(said).map(([f, q])=>[
11
+ floor(f),
12
+ String(q)
13
+ ])) : floor(said);
14
+ /** The conditions of an {@link Asked}, lowest floor first: `[floor, query]`.
15
+ * A floor alone is one without a query. */ export let conditions = (asked)=>typeof asked == 'string' ? [
16
+ [
17
+ asked,
18
+ null
19
+ ]
20
+ ] : FLOORS.flatMap((f)=>asked[f] === undefined ? [] : [
21
+ [
22
+ f,
23
+ asked[f]
24
+ ]
25
+ ]);
26
+ /** The highest floor `asked` can ask of a reader. */ export let top = (asked)=>conditions(asked).at(-1)?.[0] ?? 'owner';
27
+ /** What `asked` asks of the entity whose bundle is given, its queries run in
28
+ * memory against it. */ export let asking = (asked, vocab)=>{
29
+ if (typeof asked == 'string') return ()=>asked;
30
+ if (!vocab) throw new Error('a readers condition is matched against a vocab');
31
+ let tests = conditions(asked).map(([f, q])=>[
32
+ f,
33
+ filter(q, vocab)
34
+ ]);
35
+ return (b)=>tests.findLast(([, test])=>test(b))?.[0] ?? null;
36
+ };
37
+ // Whether a clause tests only `name`'s own properties.
38
+ let within = (name, c)=>c.kind == 'and' || c.kind == 'or' ? c.clauses.every((k)=>within(name, k)) : c.kind == 'pred' && c.path[0] == name && !c.where;
39
+ /** The reasons a component's `readers` is no declaration: a key that is not a
40
+ * floor, a query that does not parse, or one that tests anything but the
41
+ * component's own properties (an entity's bundle is all that is matched). */ export let askedErrors = (name, said)=>{
42
+ let is = `${name} is read by`;
43
+ let floors = 'readers is person, viewer, editor or owner';
44
+ if (!conditional(said)) {
45
+ return isFloor(said) ? [] : [
46
+ `${is} "${said}" — ${floors}`
47
+ ];
48
+ }
49
+ return Object.entries(said).flatMap(([f, q])=>{
50
+ if (!isFloor(f)) return [
51
+ `${is} "${f}" — ${floors}`
52
+ ];
53
+ try {
54
+ if (typeof q == 'string' && within(name, parse(q))) return [];
55
+ } catch (e) {
56
+ return [
57
+ `${is} ${f}s where "${q}" — it does not parse: ${e}`
58
+ ];
59
+ }
60
+ return [
61
+ `${is} ${f}s where "${q}" — it may test only ${name}'s own properties`
62
+ ];
63
+ });
64
+ };
65
+ //# sourceMappingURL=asking.js.map
package/asking.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asking.js","sources":["./asking.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8EAA8E;AAC9E,0EAA0E;AAC1E,4EAA4E;AAC5E,SAAS,MAAM,2BA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