@yaks/key 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 (61) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +108 -2
  3. package/_dist/comp.d.ts +14 -0
  4. package/_dist/comp.d.ts.map +1 -0
  5. package/_dist/eid.d.ts +26 -0
  6. package/_dist/eid.d.ts.map +1 -0
  7. package/_dist/graph.d.ts +6 -0
  8. package/_dist/graph.d.ts.map +1 -0
  9. package/_dist/guard.d.ts +9 -0
  10. package/_dist/guard.d.ts.map +1 -0
  11. package/_dist/keywords.d.ts +8 -0
  12. package/_dist/keywords.d.ts.map +1 -0
  13. package/_dist/kinds.d.ts +14 -0
  14. package/_dist/kinds.d.ts.map +1 -0
  15. package/_dist/mod.d.ts +70 -0
  16. package/_dist/mod.d.ts.map +1 -0
  17. package/_dist/plugin.d.ts +23 -0
  18. package/_dist/plugin.d.ts.map +1 -0
  19. package/_dist/resolve.d.ts +26 -0
  20. package/_dist/resolve.d.ts.map +1 -0
  21. package/_dist/say.d.ts +10 -0
  22. package/_dist/say.d.ts.map +1 -0
  23. package/_dist/vocab.d.ts +9 -0
  24. package/_dist/vocab.d.ts.map +1 -0
  25. package/comp.js +49 -0
  26. package/comp.js.map +1 -0
  27. package/comp.ts +62 -0
  28. package/deno.json +1 -0
  29. package/eid.js +43 -0
  30. package/eid.js.map +1 -0
  31. package/eid.ts +56 -0
  32. package/graph.js +9 -0
  33. package/graph.js.map +1 -0
  34. package/graph.ts +11 -0
  35. package/guard.js +67 -0
  36. package/guard.js.map +1 -0
  37. package/guard.ts +69 -0
  38. package/keywords.js +34 -0
  39. package/keywords.js.map +1 -0
  40. package/keywords.ts +31 -0
  41. package/kinds.js +39 -0
  42. package/kinds.js.map +1 -0
  43. package/kinds.ts +46 -0
  44. package/meta/key.vocab.json +13 -0
  45. package/mod.js +70 -0
  46. package/mod.js.map +1 -0
  47. package/mod.ts +71 -0
  48. package/package.json +26 -4
  49. package/plugin.js +62 -0
  50. package/plugin.js.map +1 -0
  51. package/plugin.ts +58 -0
  52. package/resolve.js +163 -0
  53. package/resolve.js.map +1 -0
  54. package/resolve.ts +155 -0
  55. package/say.js +39 -0
  56. package/say.js.map +1 -0
  57. package/say.ts +37 -0
  58. package/vocab.js +18 -0
  59. package/vocab.js.map +1 -0
  60. package/vocab.json +36 -0
  61. package/vocab.ts +20 -0
package/mod.ts ADDED
@@ -0,0 +1,71 @@
1
+ /**
2
+ * @yaks/key — the values that identify an entity, stored as entities.
3
+ *
4
+ * A value that identifies something is not a property on the thing it
5
+ * identifies; it is an entity of its own, carrying the `key{of, value}`
6
+ * component and a kind tag component naming what sort of value it is. A store
7
+ * identifies its recipes by a short name (`alias`), a directory its people by
8
+ * address (`email`), a library its books by `isbn`; the structure is the same,
9
+ * and the kinds are yours to declare — this package ships the carrier and the
10
+ * mechanism and not one kind.
11
+ *
12
+ * It is to a has-many value exactly what
13
+ * {@link https://jsr.io/@yaks/edge | @yaks/edge} is to a link: one generic
14
+ * carrier component, tagged by the application's own components, with the
15
+ * entity's id derived from what it holds — so an entity has as many values as
16
+ * you write, writing one twice writes one row, and retiring one means deleting
17
+ * that row.
18
+ *
19
+ * ```ts
20
+ * import { graph } from '@yaks/graph'
21
+ * import { ram } from '@yaks/ram'
22
+ * import { loadVocab } from '@yaks/vocab'
23
+ * import { keyDoc, keyed, keyKeywords, keys } from '@yaks/key'
24
+ *
25
+ * let library = {
26
+ * $defs: {
27
+ * book: { component: true, kind: true, properties: {} },
28
+ * // one component, and `isbn` is a kind of value
29
+ * isbn: { component: true, key: true },
30
+ * },
31
+ * }
32
+ * let vocab = loadVocab([keyDoc, library], [keyKeywords])
33
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [keys(vocab)] })
34
+ * g.apply([
35
+ * { entity: { eid: 'b1' }, book: {} },
36
+ * keyed('isbn', 'b1', '9780441013593'),
37
+ * ])
38
+ * ```
39
+ *
40
+ * Four things follow from that:
41
+ *
42
+ * - **A key's id is derived from the value.** {@link keyEid} hashes the kind
43
+ * and the value, so two writers giving the same value land on one entity, a
44
+ * value is unique within its kind by construction, and reading one back is a
45
+ * `get` rather than a query.
46
+ * - **A key lives only as long as what it identifies.** `of` is a reference
47
+ * declared `death: release`, so deleting a book removes its isbn row and
48
+ * frees the value.
49
+ * - **An incomplete key is refused**, naming what is missing: a key with no
50
+ * kind, no value or no `of` never reaches storage.
51
+ * - **Claiming a value somebody already holds lands on the holder.** A write
52
+ * that mints an entity under a `$alias` and gives it a value another entity
53
+ * already holds patches that entity instead of creating a second one; a
54
+ * caller who wrote an id down rather than using an alias is refused, with the
55
+ * holder named. An explicit deletion of the holder in the same batch permits
56
+ * a transfer, guarded against ownership changing before that batch commits.
57
+ *
58
+ * It imports no platform API, so the same code runs on a server, in a worker,
59
+ * and in a browser tab.
60
+ *
61
+ * @module
62
+ */
63
+
64
+ export * from './keywords.js'
65
+ export * from './kinds.js'
66
+ export * from './comp.js'
67
+ export * from './eid.js'
68
+ export * from './say.js'
69
+ export * from './guard.js'
70
+ export * from './resolve.js'
71
+ export * from './plugin.js'
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@yaks/key",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/key",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./vocab": {
12
+ "types": "./_dist/vocab.d.ts",
13
+ "default": "./vocab.js"
14
+ },
15
+ "./graph": {
16
+ "types": "./_dist/graph.d.ts",
17
+ "default": "./graph.js"
18
+ }
19
+ },
20
+ "license": "Apache-2.0",
21
+ "dependencies": {
22
+ "@yaks/fp": "0.2.36",
23
+ "@yaks/graph": "0.2.36",
24
+ "@yaks/vocab": "0.2.36"
25
+ },
26
+ "description": "Represents an entity's identifying values as separate graph entities, with ids derived from each value and its kind to enforce uniqueness.",
27
+ "repository": "https://github.com/yak-sh/yak.git"
28
+ }
package/plugin.js ADDED
@@ -0,0 +1,62 @@
1
+ // The package as a graph plugin: the `key` component, the id a key derives from
2
+ // its own kind and value, the refusal that keeps incomplete keys out, and the
3
+ // resolution that makes claiming a value twice land on one entity.
4
+ //
5
+ // It takes the loaded vocabulary as an argument because the kinds are the
6
+ // application's, not this package's: which components tag a key is something
7
+ // only a loaded vocabulary knows. So a graph is built in two steps — load the
8
+ // documents, then pass the same vocabulary to the plugin.
9
+ import { hook } from "@yaks/graph/plugin";
10
+ import { dead } from "@yaks/graph/bundle";
11
+ import { after } from "@yaks/fp";
12
+ import { KEY, names } from "./kinds.js";
13
+ import { derive } from "./eid.js";
14
+ import { stated } from "./guard.js";
15
+ import { retired, settled } from "./resolve.js";
16
+ import { keyDoc } from "./comp.js";
17
+ // The mint phase: refuse incomplete keys first, so the resolution below only
18
+ // ever reads complete ones.
19
+ let minting = (vocab)=>{
20
+ let whole = stated(vocab);
21
+ let once = settled(vocab);
22
+ return (bundles, tx)=>after(whole(bundles, tx), (b)=>once(b, tx));
23
+ };
24
+ /**
25
+ * The key plugin:
26
+ *
27
+ * ```ts
28
+ * import { loadVocab } from '@yaks/vocab'
29
+ * import { graph } from '@yaks/graph'
30
+ * import { ram } from '@yaks/ram'
31
+ * import { keyDoc, keyKeywords, keys } from '@yaks/key'
32
+ *
33
+ * let isbn = { component: true, key: true }
34
+ * let vocab = loadVocab([keyDoc, { $defs: { isbn } }], [keyKeywords])
35
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [keys(vocab)] })
36
+ * ```
37
+ *
38
+ * It contributes {@link keyDoc}, derives a key's id from the kind and value it
39
+ * holds (so a value written under a `$alias` lands on the same entity every
40
+ * time it is claimed), refuses at `mint` any key missing its kind, its value or
41
+ * its `of`, and resolves a write claiming a value somebody already holds onto
42
+ * that holder.
43
+ */ export let keys = (vocab)=>({
44
+ name: '@yaks/key',
45
+ // Releasing a key or deleting its holder writes kind tags during cascade.
46
+ admission: (bundles)=>bundles.every((b)=>!dead(b) && b[KEY] !== null),
47
+ vocab: [
48
+ keyDoc
49
+ ],
50
+ derive: {
51
+ [KEY]: derive(names(vocab))
52
+ },
53
+ hooks: {
54
+ mint: hook([
55
+ KEY
56
+ ], minting(vocab)),
57
+ cascade: hook([
58
+ KEY
59
+ ], retired(vocab))
60
+ }
61
+ });
62
+ //# sourceMappingURL=plugin.js.map
package/plugin.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["./plugin.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,8EAA8E;AAC9E,mEAAmE;AACnE,EAAE;AACF,0EAA0E;AAC1E,6EAA6E;AAC7E,8EAA8E;AAC9E,0DAA0D;AAE1D,SAAoB,IAAI,kCAAoD;AAC5E,SAAS,IAAI,kCAAuC;AACpD,SAAS,KAAK,wBAA6B;AAE3C,SAAS,GAAG,EAAE,KAAK,qBAAoB;AACvC,SAAS,MAAM,mBAAkB;AACjC,SAAS,MAAM,qBAAoB;AACnC,SAAS,OAAO,EAAE,OAAO,uBAAsB;AAC/C,SAAS,MAAM,oBAAmB;AAElC,6EAA6E;AAC7E,4BAA4B;AAC5B,IAAI,UAAU,CAAC;EACb,IAAI,QAAQ,OAAO;EACnB,IAAI,OAAO,QAAQ;EACnB,OAAO,CAAC,SAAS,KAAO,MAAM,MAAM,SAAS,KAAK,CAAC,IAAM,KAAK,GAAG;AACnE;AAEA;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,IAAI,OAAO,CAAC,QAAyB,CAAC;IAC3C,MAAM;IACN,0EAA0E;IAC1E,WAAW,CAAC,UAAY,QAAQ,KAAK,CAAC,CAAC,IAAM,CAAC,KAAK,MAAM,CAAC,CAAC,IAAI,KAAK;IACpE,OAAO;MAAC;KAAO;IACf,QAAQ;MAAE,CAAC,IAAI,EAAE,OAAO,MAAM;IAAQ;IACtC,OAAO;MACL,MAAM,KAAK;QAAC;OAAI,EAAE,QAAQ;MAC1B,SAAS,KAAK;QAAC;OAAI,EAAE,QAAQ;IAC/B;EACF,CAAC,EAAC"}
package/plugin.ts ADDED
@@ -0,0 +1,58 @@
1
+ // The package as a graph plugin: the `key` component, the id a key derives from
2
+ // its own kind and value, the refusal that keeps incomplete keys out, and the
3
+ // resolution that makes claiming a value twice land on one entity.
4
+ //
5
+ // It takes the loaded vocabulary as an argument because the kinds are the
6
+ // application's, not this package's: which components tag a key is something
7
+ // only a loaded vocabulary knows. So a graph is built in two steps — load the
8
+ // documents, then pass the same vocabulary to the plugin.
9
+
10
+ import { type Hook, hook, type Plugin } from '@yaks/graph/plugin'
11
+ import { dead } from '@yaks/graph/bundle'
12
+ import { after } from '@yaks/fp'
13
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
14
+ import { KEY, names } from './kinds.js'
15
+ import { derive } from './eid.js'
16
+ import { stated } from './guard.js'
17
+ import { retired, settled } from './resolve.js'
18
+ import { keyDoc } from './comp.js'
19
+
20
+ // The mint phase: refuse incomplete keys first, so the resolution below only
21
+ // ever reads complete ones.
22
+ let minting = (vocab: Vocab): Hook => {
23
+ let whole = stated(vocab)
24
+ let once = settled(vocab)
25
+ return (bundles, tx) => after(whole(bundles, tx), (b) => once(b, tx))
26
+ }
27
+
28
+ /**
29
+ * The key plugin:
30
+ *
31
+ * ```ts
32
+ * import { loadVocab } from '@yaks/vocab'
33
+ * import { graph } from '@yaks/graph'
34
+ * import { ram } from '@yaks/ram'
35
+ * import { keyDoc, keyKeywords, keys } from '@yaks/key'
36
+ *
37
+ * let isbn = { component: true, key: true }
38
+ * let vocab = loadVocab([keyDoc, { $defs: { isbn } }], [keyKeywords])
39
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [keys(vocab)] })
40
+ * ```
41
+ *
42
+ * It contributes {@link keyDoc}, derives a key's id from the kind and value it
43
+ * holds (so a value written under a `$alias` lands on the same entity every
44
+ * time it is claimed), refuses at `mint` any key missing its kind, its value or
45
+ * its `of`, and resolves a write claiming a value somebody already holds onto
46
+ * that holder.
47
+ */
48
+ export let keys = (vocab: Vocab): Plugin => ({
49
+ name: '@yaks/key',
50
+ // Releasing a key or deleting its holder writes kind tags during cascade.
51
+ admission: (bundles) => bundles.every((b) => !dead(b) && b[KEY] !== null),
52
+ vocab: [keyDoc],
53
+ derive: { [KEY]: derive(names(vocab)) },
54
+ hooks: {
55
+ mint: hook([KEY], minting(vocab)),
56
+ cascade: hook([KEY], retired(vocab)),
57
+ },
58
+ })
package/resolve.js ADDED
@@ -0,0 +1,163 @@
1
+ // Writing a value somebody already holds, and reading a value back.
2
+ //
3
+ // A key's id is derived from its kind and value (./eid.ts), so a value that has
4
+ // been claimed is a row sitting at an id anyone can compute. Two things follow,
5
+ // and they are this file.
6
+ //
7
+ // Resolving onto the holder. A write that mints an entity under a `$alias` and
8
+ // gives it a value — which is what a seed, a chunked import and a page that
9
+ // saves the same row every time it opens all do — should land on the entity
10
+ // that already holds that value rather than beside it. So after @yaks/graph has
11
+ // assigned an id to every `$alias`, the derived rows are read (one `get` by id
12
+ // for the whole write; no query), and where a value is already held the minted
13
+ // id gives way to the holder's, in the bundle and in everything pointing at it
14
+ // (`substitute`). The rest of the write then patches the entity that was
15
+ // already there.
16
+ //
17
+ // The refusal. A key whose `of` was not minted in this write — a caller who
18
+ // wrote an id down — is refused instead, unless the same batch explicitly
19
+ // deletes the holder. A transfer guards the observed key.of inside the
20
+ // transaction. Otherwise the caller named both the entity and the value and
21
+ // the two disagree; swapping the id underneath
22
+ // them would be a lie, and the holder's id is the one they wanted.
23
+ //
24
+ // The read happens in `mint`, which runs outside the transaction. That leaves a
25
+ // window: two processes claiming one free value at the same instant both find
26
+ // nothing and both write. The derived id closes it — both writes address the
27
+ // same row, so the second patches the first rather than creating a second row,
28
+ // and the `of` that ends up stored is the one that committed last.
29
+ import { after } from "@yaks/fp";
30
+ import { dead } from "@yaks/graph/bundle";
31
+ import { Refused } from "@yaks/graph/errors";
32
+ import { substitute } from "@yaks/graph/identity";
33
+ import { token } from "@yaks/graph/sha256";
34
+ import { ofOf, valueOf } from "./comp.js";
35
+ import { keyEid, tagOf } from "./eid.js";
36
+ import { KEY, names } from "./kinds.js";
37
+ /**
38
+ * Who holds each of these values in a kind — one `get` for the whole list, with
39
+ * a value nobody holds simply absent from the result. This is the lookup
40
+ * everything else makes: no query, no index, no scan.
41
+ */ export let held = (tx, kind, values)=>{
42
+ let ask = [
43
+ ...new Set(values)
44
+ ].filter(Boolean);
45
+ if (!ask.length) return new Map();
46
+ return after(tx.get(ask.map((v)=>keyEid(kind, v))), (rows)=>{
47
+ let by = new Map(rows.map((b)=>[
48
+ b.entity.eid,
49
+ b
50
+ ]));
51
+ let out = new Map();
52
+ for (let v of ask){
53
+ let of = ofOf(by.get(keyEid(kind, v)));
54
+ if (of) out.set(v, of);
55
+ }
56
+ return out;
57
+ });
58
+ };
59
+ /**
60
+ * The `cascade` hook that finishes a release: when the entity a key identified
61
+ * is deleted, the `key` row goes because the vocabulary declares
62
+ * `death: release`, and this removes the kind tag beside it, so no entity is
63
+ * left holding half a key.
64
+ *
65
+ * It is a release and not a cascade on purpose. A cascade would tombstone the
66
+ * key entity, and that id is derived from the value — so the value could never
67
+ * be claimed again, by anyone, for the life of the store. Deleting a recipe
68
+ * must free its name.
69
+ */ export let retired = (vocab)=>{
70
+ let tags = names(vocab);
71
+ return (bundles, tx)=>{
72
+ let gone = bundles.filter((b)=>b[KEY] === null).map((b)=>b.entity.eid);
73
+ if (!gone.length) return bundles;
74
+ return after(tx.get(gone), (rows)=>{
75
+ let out = rows.flatMap((r)=>{
76
+ let tag = tagOf(r, tags);
77
+ return tag ? [
78
+ {
79
+ entity: r.entity,
80
+ [tag]: null
81
+ }
82
+ ] : [];
83
+ });
84
+ return out.length ? after(tx.patch(out), ()=>[
85
+ ...bundles,
86
+ ...out
87
+ ]) : bundles;
88
+ });
89
+ };
90
+ };
91
+ /**
92
+ * The `mint` hook that keeps one value on one entity: claiming a value somebody
93
+ * already holds resolves this write's entity onto theirs, and a clash is
94
+ * refused with the holder named. Registered by
95
+ * {@link https://jsr.io/@yaks/key/doc/~/keys | keys}; exported on its own for a
96
+ * graph that wants the behaviour without the rest of the plugin.
97
+ */ export let settled = (vocab)=>{
98
+ let tags = names(vocab);
99
+ return (bundles, tx)=>{
100
+ let claims = bundles.flatMap((b)=>{
101
+ let value = valueOf(b);
102
+ let tag = tagOf(b, tags);
103
+ return value && tag && ofOf(b) ? [
104
+ [
105
+ b,
106
+ tag,
107
+ value
108
+ ]
109
+ ] : [];
110
+ });
111
+ if (!claims.length) return bundles;
112
+ // A key's id is derived from its kind and value. A bundle written at any
113
+ // other id would be a second row for one value, which is the one thing this
114
+ // component may not be.
115
+ for (let [b, tag, value] of claims){
116
+ let at = keyEid(tag, value);
117
+ if (b.entity.eid != at) {
118
+ throw new Refused(`a key is named by its kind and value — write ${tag} ${value} at ` + `${at}, or under a $alias and let the graph name it`);
119
+ }
120
+ }
121
+ // Which entities this write minted under a `$alias`: the ones whose id was
122
+ // the graph's to pick, and so the ones an existing holder may replace.
123
+ let ours = new Set(bundles.flatMap((b)=>b.$alias == null ? [] : [
124
+ b.entity.eid
125
+ ]));
126
+ let gone = new Set(bundles.filter(dead).map((b)=>b.entity.eid));
127
+ return after(tx.get(claims.map(([b])=>b.entity.eid)), (rows)=>{
128
+ let was = new Map(rows.map((b)=>[
129
+ b.entity.eid,
130
+ b
131
+ ]));
132
+ let at = new Map();
133
+ for (let [b, tag, value] of claims){
134
+ let holder = ofOf(was.get(b.entity.eid));
135
+ let of = ofOf(b);
136
+ if (!holder || holder == of) continue;
137
+ if (gone.has(holder)) {
138
+ // Mint runs outside the transaction. Transfer only the ownership we
139
+ // saw, without replacing any precondition the caller already gave.
140
+ let want = token(holder);
141
+ let expected = b.$was?.[KEY];
142
+ if (expected && 'of' in expected && expected.of !== want) {
143
+ throw new Refused(`${tag} ${value} has a conflicting $was key.of`);
144
+ }
145
+ b.$was = {
146
+ ...b.$was,
147
+ [KEY]: {
148
+ ...expected,
149
+ of: want
150
+ }
151
+ };
152
+ continue;
153
+ }
154
+ if (!ours.has(of)) {
155
+ throw new Refused(`${tag} ${value} is ${holder}'s — say it of that entity, or give ` + 'this one another value');
156
+ }
157
+ at.set(of, holder);
158
+ }
159
+ return substitute(bundles, vocab, at);
160
+ });
161
+ };
162
+ };
163
+ //# sourceMappingURL=resolve.js.map
package/resolve.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve.js","sources":["./resolve.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,0BAA0B;AAC1B,EAAE;AACF,+EAA+E;AAC/E,4EAA4E;AAC5E,4EAA4E;AAC5E,gFAAgF;AAChF,+EAA+E;AAC/E,+EAA+E;AAC/E,+EAA+E;AAC/E,yEAAyE;AACzE,iBAAiB;AACjB,EAAE;AACF,4EAA4E;AAC5E,0EAA0E;AAC1E,uEAAuE;AACvE,4EAA4E;AAC5E,+CAA+C;AAC/C,mEAAmE;AACnE,EAAE;AACF,gFAAgF;AAChF,8EAA8E;AAC9E,6EAA6E;AAC7E,+EAA+E;AAC/E,mEAAmE;AAGnE,SAAS,KAAK,wBAA6B;AAC3C,SAAS,IAAI,kCAAuC;AACpD,SAAS,OAAO,kCAAuC;AACvD,SAAS,UAAU,oCAAyC;AAC5D,SAAS,KAAK,kCAAuC;AAErD,SAAS,IAAI,EAAE,OAAO,oBAAmB;AACzC,SAAS,MAAM,EAAE,KAAK,mBAAkB;AACxC,SAAS,GAAG,EAAE,KAAK,qBAAoB;AAEvC;;;;CAIC,GACD,OAAO,IAAI,OAAO,CAChB,IACA,MACA;EAEA,IAAI,MAAM;OAAI,IAAI,IAAI;GAAQ,CAAC,MAAM,CAAC;EACtC,IAAI,CAAC,IAAI,MAAM,EAAE,OAAO,IAAI;EAC5B,OAAO,MAAM,GAAG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ACxD,MAAM,IAAI,QAAQ,GAAG,IAAI,CAAC,EAAE,MAAM,8BAA8B,CAAC;UACnE;UACA,EAAE,IAAI,GAAG;YAAE,GAAG,EAAE,IAAI;YAAE,CAAC,IAAI,EAAE;cAAE,GAAG,QAAQ;cAAE,IAAI;YAAK;UAAE;UACvD;QACF;QACA,IAAI,CAAC,KAAK,GAAG,CAAC,KAAK;UACjB,MAAM,IAAI,QACR,GAAG,IAAI,CAAC,EAAE,MAAM,IAAI,EAAE,OAAO,oCAAoC,CAAC,GAChE;QAEN;QACA,GAAG,GAAG,CAAC,IAAI;MACb;MACA,OAAO,WAAW,SAAS,OAAO;IACpC;EACF;AACF,EAAC"}
package/resolve.ts ADDED
@@ -0,0 +1,155 @@
1
+ // Writing a value somebody already holds, and reading a value back.
2
+ //
3
+ // A key's id is derived from its kind and value (./eid.ts), so a value that has
4
+ // been claimed is a row sitting at an id anyone can compute. Two things follow,
5
+ // and they are this file.
6
+ //
7
+ // Resolving onto the holder. A write that mints an entity under a `$alias` and
8
+ // gives it a value — which is what a seed, a chunked import and a page that
9
+ // saves the same row every time it opens all do — should land on the entity
10
+ // that already holds that value rather than beside it. So after @yaks/graph has
11
+ // assigned an id to every `$alias`, the derived rows are read (one `get` by id
12
+ // for the whole write; no query), and where a value is already held the minted
13
+ // id gives way to the holder's, in the bundle and in everything pointing at it
14
+ // (`substitute`). The rest of the write then patches the entity that was
15
+ // already there.
16
+ //
17
+ // The refusal. A key whose `of` was not minted in this write — a caller who
18
+ // wrote an id down — is refused instead, unless the same batch explicitly
19
+ // deletes the holder. A transfer guards the observed key.of inside the
20
+ // transaction. Otherwise the caller named both the entity and the value and
21
+ // the two disagree; swapping the id underneath
22
+ // them would be a lie, and the holder's id is the one they wanted.
23
+ //
24
+ // The read happens in `mint`, which runs outside the transaction. That leaves a
25
+ // window: two processes claiming one free value at the same instant both find
26
+ // nothing and both write. The derived id closes it — both writes address the
27
+ // same row, so the second patches the first rather than creating a second row,
28
+ // and the `of` that ends up stored is the one that committed last.
29
+
30
+ import type { Eid, Hook, Tx } from 'jsr:@yaks/graph@0.2.36'
31
+ import { after } from '@yaks/fp'
32
+ import { dead } from '@yaks/graph/bundle'
33
+ import { Refused } from '@yaks/graph/errors'
34
+ import { substitute } from '@yaks/graph/identity'
35
+ import { token } from '@yaks/graph/sha256'
36
+ import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
37
+ import { ofOf, valueOf } from './comp.js'
38
+ import { keyEid, tagOf } from './eid.js'
39
+ import { KEY, names } from './kinds.js'
40
+
41
+ /**
42
+ * Who holds each of these values in a kind — one `get` for the whole list, with
43
+ * a value nobody holds simply absent from the result. This is the lookup
44
+ * everything else makes: no query, no index, no scan.
45
+ */
46
+ export let held = (
47
+ tx: Pick<Tx, 'get'>,
48
+ kind: string,
49
+ values: string[],
50
+ ): Map<string, Eid> | Promise<Map<string, Eid>> => {
51
+ let ask = [...new Set(values)].filter(Boolean)
52
+ if (!ask.length) return new Map()
53
+ return after(tx.get(ask.map((v) => keyEid(kind, v))), (rows) => {
54
+ let by = new Map(rows.map((b) => [b.entity.eid, b]))
55
+ let out = new Map<string, Eid>()
56
+ for (let v of ask) {
57
+ let of = ofOf(by.get(keyEid(kind, v)))
58
+ if (of) out.set(v, of)
59
+ }
60
+ return out
61
+ })
62
+ }
63
+
64
+ /**
65
+ * The `cascade` hook that finishes a release: when the entity a key identified
66
+ * is deleted, the `key` row goes because the vocabulary declares
67
+ * `death: release`, and this removes the kind tag beside it, so no entity is
68
+ * left holding half a key.
69
+ *
70
+ * It is a release and not a cascade on purpose. A cascade would tombstone the
71
+ * key entity, and that id is derived from the value — so the value could never
72
+ * be claimed again, by anyone, for the life of the store. Deleting a recipe
73
+ * must free its name.
74
+ */
75
+ export let retired = (vocab: Vocab): Hook => {
76
+ let tags = names(vocab)
77
+ return (bundles, tx) => {
78
+ let gone = bundles.filter((b) => b[KEY] === null).map((b) => b.entity.eid)
79
+ if (!gone.length) return bundles
80
+ return after(tx.get(gone), (rows) => {
81
+ let out = rows.flatMap((r) => {
82
+ let tag = tagOf(r, tags)
83
+ return tag ? [{ entity: r.entity, [tag]: null }] : []
84
+ })
85
+ return out.length
86
+ ? after(tx.patch(out), () => [...bundles, ...out])
87
+ : bundles
88
+ })
89
+ }
90
+ }
91
+
92
+ /**
93
+ * The `mint` hook that keeps one value on one entity: claiming a value somebody
94
+ * already holds resolves this write's entity onto theirs, and a clash is
95
+ * refused with the holder named. Registered by
96
+ * {@link https://jsr.io/@yaks/key/doc/~/keys | keys}; exported on its own for a
97
+ * graph that wants the behaviour without the rest of the plugin.
98
+ */
99
+ export let settled = (vocab: Vocab): Hook => {
100
+ let tags = names(vocab)
101
+ return (bundles, tx) => {
102
+ let claims = bundles.flatMap((b) => {
103
+ let value = valueOf(b)
104
+ let tag = tagOf(b, tags)
105
+ return value && tag && ofOf(b) ? [[b, tag, value] as const] : []
106
+ })
107
+ if (!claims.length) return bundles
108
+ // A key's id is derived from its kind and value. A bundle written at any
109
+ // other id would be a second row for one value, which is the one thing this
110
+ // component may not be.
111
+ for (let [b, tag, value] of claims) {
112
+ let at = keyEid(tag, value)
113
+ if (b.entity.eid != at) {
114
+ throw new Refused(
115
+ `a key is named by its kind and value — write ${tag} ${value} at ` +
116
+ `${at}, or under a $alias and let the graph name it`,
117
+ )
118
+ }
119
+ }
120
+ // Which entities this write minted under a `$alias`: the ones whose id was
121
+ // the graph's to pick, and so the ones an existing holder may replace.
122
+ let ours = new Set(
123
+ bundles.flatMap((b) => b.$alias == null ? [] : [b.entity.eid]),
124
+ )
125
+ let gone = new Set(bundles.filter(dead).map((b) => b.entity.eid))
126
+ return after(tx.get(claims.map(([b]) => b.entity.eid)), (rows) => {
127
+ let was = new Map(rows.map((b) => [b.entity.eid, b]))
128
+ let at = new Map<Eid, Eid>()
129
+ for (let [b, tag, value] of claims) {
130
+ let holder = ofOf(was.get(b.entity.eid))
131
+ let of = ofOf(b)!
132
+ if (!holder || holder == of) continue
133
+ if (gone.has(holder)) {
134
+ // Mint runs outside the transaction. Transfer only the ownership we
135
+ // saw, without replacing any precondition the caller already gave.
136
+ let want = token(holder)
137
+ let expected = b.$was?.[KEY]
138
+ if (expected && 'of' in expected && expected.of !== want) {
139
+ throw new Refused(`${tag} ${value} has a conflicting $was key.of`)
140
+ }
141
+ b.$was = { ...b.$was, [KEY]: { ...expected, of: want } }
142
+ continue
143
+ }
144
+ if (!ours.has(of)) {
145
+ throw new Refused(
146
+ `${tag} ${value} is ${holder}'s — say it of that entity, or give ` +
147
+ 'this one another value',
148
+ )
149
+ }
150
+ at.set(of, holder)
151
+ }
152
+ return substitute(bundles, vocab, at)
153
+ })
154
+ }
155
+ }
package/say.js ADDED
@@ -0,0 +1,39 @@
1
+ // `keyed()` and `unkeyed()`: the bundle that claims a value, and the bundle
2
+ // that gives it back.
3
+ //
4
+ // A key is written the way everything else is — as a bundle passed to
5
+ // `graph.apply()`. The entity's id is derived from the kind and the value, so
6
+ // it needs no id from anywhere: `keyed()` computes it, and writing the same
7
+ // value twice in one transaction writes one entity.
8
+ //
9
+ // Retiring a value is not a DELETE of the entity. The value is simply no longer
10
+ // claimed, and the same value may be claimed again tomorrow — by this entity or
11
+ // another — so its components are removed and the entity itself stays. An
12
+ // entity carrying no components is invisible to every reader.
13
+ import { keyEid } from "./eid.js";
14
+ import { KEY } from "./kinds.js";
15
+ import { OF, VALUE } from "./comp.js";
16
+ /**
17
+ * The bundle that claims a value: `keyed('alias', r, 'lemon-cake')`. `kind` is
18
+ * the tag component the key carries.
19
+ */ export let keyed = (kind, of, value)=>({
20
+ entity: {
21
+ eid: keyEid(kind, value)
22
+ },
23
+ [KEY]: {
24
+ [OF]: of,
25
+ [VALUE]: value
26
+ },
27
+ [kind]: {}
28
+ });
29
+ /**
30
+ * The bundle that gives that value back: both components deleted, the entity
31
+ * left standing so the same value can be claimed again.
32
+ */ export let unkeyed = (kind, value)=>({
33
+ entity: {
34
+ eid: keyEid(kind, value)
35
+ },
36
+ [KEY]: null,
37
+ [kind]: null
38
+ });
39
+ //# sourceMappingURL=say.js.map
package/say.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"say.js","sources":["./say.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,sBAAsB;AACtB,EAAE;AACF,sEAAsE;AACtE,8EAA8E;AAC9E,4EAA4E;AAC5E,oDAAoD;AACpD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;AAChF,0EAA0E;AAC1E,8DAA8D;AAG9D,SAAS,MAAM,mBAAkB;AACjC,SAAS,GAAG,qBAAoB;AAChC,SAAS,EAAE,EAAE,KAAK,oBAAmB;AAErC;;;CAGC,GACD,OAAO,IAAI,QAAQ,CAAC,MAAc,IAAS,QAA0B,CAAC;IACpE,QAAQ;MAAE,KAAK,OAAO,MAAM;IAAO;IACnC,CAAC,IAAI,EAAE;MAAE,CAAC,GAAG,EAAE;MAAI,CAAC,MAAM,EAAE;IAAM;IAClC,CAAC,KAAK,EAAE,CAAC;EACX,CAAC,EAAC;AAEF;;;CAGC,GACD,OAAO,IAAI,UAAU,CAAC,MAAc,QAA0B,CAAC;IAC7D,QAAQ;MAAE,KAAK,OAAO,MAAM;IAAO;IACnC,CAAC,IAAI,EAAE;IACP,CAAC,KAAK,EAAE;EACV,CAAC,EAAC"}
package/say.ts ADDED
@@ -0,0 +1,37 @@
1
+ // `keyed()` and `unkeyed()`: the bundle that claims a value, and the bundle
2
+ // that gives it back.
3
+ //
4
+ // A key is written the way everything else is — as a bundle passed to
5
+ // `graph.apply()`. The entity's id is derived from the kind and the value, so
6
+ // it needs no id from anywhere: `keyed()` computes it, and writing the same
7
+ // value twice in one transaction writes one entity.
8
+ //
9
+ // Retiring a value is not a DELETE of the entity. The value is simply no longer
10
+ // claimed, and the same value may be claimed again tomorrow — by this entity or
11
+ // another — so its components are removed and the entity itself stays. An
12
+ // entity carrying no components is invisible to every reader.
13
+
14
+ import type { Bundle, Eid } from 'jsr:@yaks/graph@0.2.36'
15
+ import { keyEid } from './eid.js'
16
+ import { KEY } from './kinds.js'
17
+ import { OF, VALUE } from './comp.js'
18
+
19
+ /**
20
+ * The bundle that claims a value: `keyed('alias', r, 'lemon-cake')`. `kind` is
21
+ * the tag component the key carries.
22
+ */
23
+ export let keyed = (kind: string, of: Eid, value: string): Bundle => ({
24
+ entity: { eid: keyEid(kind, value) },
25
+ [KEY]: { [OF]: of, [VALUE]: value },
26
+ [kind]: {},
27
+ })
28
+
29
+ /**
30
+ * The bundle that gives that value back: both components deleted, the entity
31
+ * left standing so the same value can be claimed again.
32
+ */
33
+ export let unkeyed = (kind: string, value: string): Bundle => ({
34
+ entity: { eid: keyEid(kind, value) },
35
+ [KEY]: null,
36
+ [kind]: null,
37
+ })
package/vocab.js ADDED
@@ -0,0 +1,18 @@
1
+ // The `key` component declaration and nothing else, exported as
2
+ // `@yaks/key/vocab`. It imports no storage, no SQL and no runtime API, so a
3
+ // browser tab loading this vocabulary loads nothing else with it.
4
+ import { keyDoc } from "./comp.js";
5
+ import { keyKeywords } from "./keywords.js";
6
+ import manifest from './package.json' with {
7
+ type: 'json'
8
+ };
9
+ export { keyDoc, keyKeywords };
10
+ /** What this package is: its manifest's description, the one place it is
11
+ * written, which a host stamps on each document here. */ export let description = manifest.description;
12
+ /** Every document this plugin declares. */ export let docs = [
13
+ keyDoc
14
+ ];
15
+ /** The JSON Schema keywords those documents use. */ export let keywords = [
16
+ keyKeywords
17
+ ];
18
+ //# sourceMappingURL=vocab.js.map
package/vocab.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,4EAA4E;AAC5E,kEAAkE;AAGlE,SAAS,MAAM,oBAAmB;AAClC,SAAS,WAAW,wBAAuB;AAC3C,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,SAAS,MAAM,EAAE,WAAW,GAAE;AAE9B;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,yCAAyC,GACzC,OAAO,IAAI,OAAmB;EAAC;CAAO,CAAA;AAEtC,kDAAkD,GAClD,OAAO,IAAI,WAAuB;EAAC;CAAY,CAAA"}