@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
package/words.ts ADDED
@@ -0,0 +1,123 @@
1
+ // The three enumerated values this package defines, the helpers that read one
2
+ // out of storage, and the three access rules stated once.
3
+ //
4
+ // A role is what a `member` row records: you are a space's owner, or one of its
5
+ // members. A level is what a `grant` gives one principal on one app: owner,
6
+ // editor, viewer. A mode is what an app allows everyone else — the people with
7
+ // no membership and no grant at all.
8
+ //
9
+ // Roles and levels are kept apart on purpose. `owner` and `member` record
10
+ // whether you belong to the space; `owner`, `editor` and `viewer` record what
11
+ // you may do with one app in it. Membership is not permission: a member with no
12
+ // grant reaches an app exactly as far as a stranger does, which is what makes a
13
+ // roster safe to be generous with.
14
+
15
+ /** What a `member` row records. An `owner` runs the space and holds `owner`
16
+ * permission on every app in it; a `member` belongs, and holds only what a
17
+ * grant gives them. */
18
+ export type Role = 'owner' | 'member'
19
+
20
+ /** What a `grant` gives one principal on one app: `owner` shares and deletes
21
+ * it, `editor` writes it, `viewer` reads it. */
22
+ export type Level = 'owner' | 'editor' | 'viewer'
23
+
24
+ /** What an app allows everyone with no grant on it:
25
+ *
26
+ * - `public` — anyone with the link reads it; only the granted write.
27
+ * - `open` — anyone with the link reads it and writes it, signed in or not.
28
+ * - `private` — only principals holding a permission see it at all.
29
+ * - `friends` — confirmed friends of a space owner may also read it.
30
+ */
31
+ export type Mode = typeof MODES[number]
32
+
33
+ /** The roles, least to most. */
34
+ export let ROLES: Role[] = ['member', 'owner']
35
+
36
+ /** The levels a grant confers, least to most. */
37
+ export let LEVELS: Level[] = ['viewer', 'editor', 'owner']
38
+
39
+ /** The modes an app can be in. */
40
+ export let MODES = ['public', 'open', 'private', 'friends'] as const
41
+
42
+ /** Read a stored `member.role`: an unset one is a plain `member`. */
43
+ export let role = (v: unknown): Role => v == 'owner' ? 'owner' : 'member'
44
+
45
+ /** Read a stored `grant.access`: an unset one is a `viewer` — the least a grant
46
+ * can mean, never the most. */
47
+ export let level = (v: unknown): Level =>
48
+ v == 'owner' ? 'owner' : v == 'editor' ? 'editor' : 'viewer'
49
+
50
+ /** Read a stored `access.mode`. An app whose mode cannot be read, because its
51
+ * `access` is missing or says something else, is `private`: a lost row locks
52
+ * the app, never opens it. Every app is born with an explicit mode, so report
53
+ * a missing mode as a failure. */
54
+ export let mode = (v: unknown): Mode =>
55
+ MODES.includes(v as Mode) ? v as Mode : 'private'
56
+
57
+ /** May a principal holding this level write? An `owner` and an `editor` write;
58
+ * a `viewer`, and a principal holding nothing, do not. */
59
+ export let writes = (l: Level | null): boolean => l == 'owner' || l == 'editor'
60
+
61
+ /** Does this level reach that one? Holding nothing reaches nothing. */
62
+ export let reaches = (l: Level | null, floor: Level): boolean =>
63
+ l != null && LEVELS.indexOf(l) >= LEVELS.indexOf(floor)
64
+
65
+ /** The least a component asks of whoever writes it, whatever the app's mode:
66
+ * a level, or `person` — anyone signed in, level or none. */
67
+ export type Floor = 'person' | Level
68
+
69
+ /** The floor each component asks of whoever writes it, by name. */
70
+ export type Floors = Record<string, Floor>
71
+
72
+ /** The floors, least to most. */
73
+ export let FLOORS: Floor[] = ['person', ...LEVELS]
74
+
75
+ /** Is this a floor? */
76
+ export let isFloor = (v: unknown): v is Floor => FLOORS.includes(v as Floor)
77
+
78
+ /** Read a declared floor: one this package does not know asks the most. */
79
+ export let floor = (v: unknown): Floor => isFloor(v) ? v : 'owner'
80
+
81
+ /** Does a principal holding this level stand on that floor? Anyone signed in
82
+ * stands on `person`, and nobody stands on any.
83
+ *
84
+ * ```ts
85
+ * import { assertEquals } from '@std/assert'
86
+ * import { stands } from '@yaks/member'
87
+ * assertEquals(stands('kim', null, 'person'), true)
88
+ * assertEquals(stands(null, null, 'person'), false)
89
+ * assertEquals(stands('kim', 'viewer', 'editor'), false)
90
+ * ```
91
+ */
92
+ export let stands = (
93
+ who: string | null,
94
+ l: Level | null,
95
+ f: Floor,
96
+ ): boolean => f == 'person' ? who != null : reaches(l, f)
97
+
98
+ // The rules, stated once. Everything else in this package — the `policy`
99
+ // helpers the HTTP layer calls, the `precondition` hook a transaction passes —
100
+ // reads a mode and a level out of storage and then calls `reads` or `edits`,
101
+ // and the egress (@yaks/egress) calls `callsOut`. A service that already knows
102
+ // both (one that authenticated the caller at its edge and keeps modes in a
103
+ // directory) calls them directly, with no storage at all, and so cannot drift
104
+ // from the graph that enforces them.
105
+
106
+ /** May this principal read the app? Friendship is a read permission on its
107
+ * own, independent of membership or a grant. */
108
+ export let reads = (m: Mode, l: Level | null, friend = false): boolean =>
109
+ l != null || m == 'public' || m == 'open' || m == 'friends' && friend
110
+
111
+ /** May a principal holding this level write an app in this mode? An `open` app
112
+ * is written by anyone, signed in or not; anything else requires owner or
113
+ * editor. */
114
+ export let edits = (m: Mode, l: Level | null): boolean =>
115
+ m == 'open' || writes(l)
116
+
117
+ /** May a principal holding this level call out through a credential an app
118
+ * uses (egress-use)? Whoever holds any level on the app may; anyone else only
119
+ * through one the app opened to `anyone`, as a public widget's is. The app's
120
+ * mode does not enter into it: the sentinel a caller presents points at a
121
+ * credential and is not one, so it is never enough on its own. */
122
+ export let callsOut = (anyone: boolean, l: Level | null): boolean =>
123
+ anyone || l != null