@yaks/api 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 (152) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +956 -2
  3. package/_dist/actor.d.ts +59 -0
  4. package/_dist/actor.d.ts.map +1 -0
  5. package/_dist/address.d.ts +33 -0
  6. package/_dist/address.d.ts.map +1 -0
  7. package/_dist/asset.d.ts +15 -0
  8. package/_dist/asset.d.ts.map +1 -0
  9. package/_dist/batches.d.ts +4 -0
  10. package/_dist/batches.d.ts.map +1 -0
  11. package/_dist/body.d.ts +7 -0
  12. package/_dist/body.d.ts.map +1 -0
  13. package/_dist/deno.d.ts +38 -0
  14. package/_dist/deno.d.ts.map +1 -0
  15. package/_dist/doors.d.ts +134 -0
  16. package/_dist/doors.d.ts.map +1 -0
  17. package/_dist/graph.d.ts +17 -0
  18. package/_dist/graph.d.ts.map +1 -0
  19. package/_dist/headers.d.ts +5 -0
  20. package/_dist/headers.d.ts.map +1 -0
  21. package/_dist/log.d.ts +253 -0
  22. package/_dist/log.d.ts.map +1 -0
  23. package/_dist/mod.d.ts +71 -0
  24. package/_dist/mod.d.ts.map +1 -0
  25. package/_dist/publish.d.ts +7 -0
  26. package/_dist/publish.d.ts.map +1 -0
  27. package/_dist/reached.d.ts +145 -0
  28. package/_dist/reached.d.ts.map +1 -0
  29. package/_dist/receipts.d.ts +153 -0
  30. package/_dist/receipts.d.ts.map +1 -0
  31. package/_dist/refuse.d.ts +51 -0
  32. package/_dist/refuse.d.ts.map +1 -0
  33. package/_dist/relay.d.ts +3 -0
  34. package/_dist/relay.d.ts.map +1 -0
  35. package/_dist/request.d.ts +79 -0
  36. package/_dist/request.d.ts.map +1 -0
  37. package/_dist/requests.d.ts +7 -0
  38. package/_dist/requests.d.ts.map +1 -0
  39. package/_dist/route.d.ts +71 -0
  40. package/_dist/route.d.ts.map +1 -0
  41. package/_dist/routed.d.ts +33 -0
  42. package/_dist/routed.d.ts.map +1 -0
  43. package/_dist/routes.d.ts +46 -0
  44. package/_dist/routes.d.ts.map +1 -0
  45. package/_dist/save.d.ts +6 -0
  46. package/_dist/save.d.ts.map +1 -0
  47. package/_dist/socket.d.ts +73 -0
  48. package/_dist/socket.d.ts.map +1 -0
  49. package/_dist/subs.d.ts +134 -0
  50. package/_dist/subs.d.ts.map +1 -0
  51. package/_dist/timing.d.ts +8 -0
  52. package/_dist/timing.d.ts.map +1 -0
  53. package/_dist/tools.d.ts +80 -0
  54. package/_dist/tools.d.ts.map +1 -0
  55. package/_dist/vocab.d.ts +6 -0
  56. package/_dist/vocab.d.ts.map +1 -0
  57. package/access.js +32 -0
  58. package/access.js.map +1 -0
  59. package/access.ts +42 -0
  60. package/actor.js +75 -0
  61. package/actor.js.map +1 -0
  62. package/actor.ts +106 -0
  63. package/address.js +42 -0
  64. package/address.js.map +1 -0
  65. package/address.ts +49 -0
  66. package/admission.js +71 -0
  67. package/admission.js.map +1 -0
  68. package/admission.ts +64 -0
  69. package/asset.js +37 -0
  70. package/asset.js.map +1 -0
  71. package/asset.ts +47 -0
  72. package/batches.js +92 -0
  73. package/batches.js.map +1 -0
  74. package/batches.ts +96 -0
  75. package/body.js +51 -0
  76. package/body.js.map +1 -0
  77. package/body.ts +82 -0
  78. package/deno.js +88 -0
  79. package/deno.js.map +1 -0
  80. package/deno.json +1 -0
  81. package/deno.ts +150 -0
  82. package/doors.js +435 -0
  83. package/doors.js.map +1 -0
  84. package/doors.ts +570 -0
  85. package/graph.js +12 -0
  86. package/graph.js.map +1 -0
  87. package/graph.ts +23 -0
  88. package/headers.js +5 -0
  89. package/headers.js.map +1 -0
  90. package/headers.ts +9 -0
  91. package/interest.js +224 -0
  92. package/interest.js.map +1 -0
  93. package/interest.ts +264 -0
  94. package/log.js +1011 -0
  95. package/log.js.map +1 -0
  96. package/log.ts +1035 -0
  97. package/mod.js +69 -0
  98. package/mod.js.map +1 -0
  99. package/mod.ts +142 -0
  100. package/package.json +75 -4
  101. package/peer_query.js +164 -0
  102. package/peer_query.js.map +1 -0
  103. package/peer_query.ts +158 -0
  104. package/publish.js +7 -0
  105. package/publish.js.map +1 -0
  106. package/publish.ts +16 -0
  107. package/reached.js +456 -0
  108. package/reached.js.map +1 -0
  109. package/reached.ts +549 -0
  110. package/receipts.js +543 -0
  111. package/receipts.js.map +1 -0
  112. package/receipts.ts +749 -0
  113. package/refuse.js +120 -0
  114. package/refuse.js.map +1 -0
  115. package/refuse.ts +179 -0
  116. package/relay.js +249 -0
  117. package/relay.js.map +1 -0
  118. package/relay.ts +284 -0
  119. package/request.js +218 -0
  120. package/request.js.map +1 -0
  121. package/request.ts +235 -0
  122. package/requests.js +250 -0
  123. package/requests.js.map +1 -0
  124. package/requests.ts +257 -0
  125. package/route.js +139 -0
  126. package/route.js.map +1 -0
  127. package/route.ts +228 -0
  128. package/routed.js +25 -0
  129. package/routed.js.map +1 -0
  130. package/routed.ts +53 -0
  131. package/routes.js +154 -0
  132. package/routes.js.map +1 -0
  133. package/routes.ts +201 -0
  134. package/save.js +341 -0
  135. package/save.js.map +1 -0
  136. package/save.ts +393 -0
  137. package/socket.js +398 -0
  138. package/socket.js.map +1 -0
  139. package/socket.ts +496 -0
  140. package/subs.js +2295 -0
  141. package/subs.js.map +1 -0
  142. package/subs.ts +2979 -0
  143. package/timing.js +17 -0
  144. package/timing.js.map +1 -0
  145. package/timing.ts +26 -0
  146. package/tools.js +251 -0
  147. package/tools.js.map +1 -0
  148. package/tools.ts +315 -0
  149. package/vocab.js +18 -0
  150. package/vocab.js.map +1 -0
  151. package/vocab.json +93 -0
  152. package/vocab.ts +18 -0
package/mod.js ADDED
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @yaks/api — an HTTP and WebSocket interface to a yaks graph, as a plain
3
+ * request handler that runs in any JavaScript runtime.
4
+ *
5
+ * Given a {@link https://jsr.io/@yaks/graph | @yaks/graph} `Graph`, this
6
+ * package serves the three endpoints a client needs and nothing more:
7
+ *
8
+ * - **`POST /apply`** — the request body is a JSON array of bundles, applied
9
+ * in one transaction; the response body is that array as applied.
10
+ * `?check=1` runs every phase and then rolls the transaction back, so a
11
+ * caller spreading one array over several graphs can ask each of them
12
+ * before any of them keeps it. `content-type: application/x-ndjson` is the
13
+ * same endpoint with one bundle per line, for an import too big to parse or
14
+ * commit whole ({@link pour}).
15
+ * - **`GET /query?q=…`** (or `POST /query`) — a query string in, the bundles
16
+ * it selects out.
17
+ * - **`/ws`** — subscriptions: a saved query whose result is pushed again
18
+ * whenever a committed transaction changes it.
19
+ *
20
+ * ```ts
21
+ * import { api } from '@yaks/api'
22
+ * // Deno.serve(api({ graph, authenticate }))
23
+ * ```
24
+ *
25
+ * ## Attribution is decided here, not by the client
26
+ * A client can put anything in the JSON it posts, including whose name is on
27
+ * it. So every array that arrives has its `$actor` component replaced by the
28
+ * identity {@link Authenticate} returned for that request. `authenticate`
29
+ * runs on every request, and throwing {@link Unauthorized} from it answers
30
+ * that request with a 401.
31
+ *
32
+ * ## Two callbacks the application supplies
33
+ * {@link Authenticate} names the writer. {@link Upgrade} turns a request into
34
+ * a WebSocket — the one step no web standard covers — and defaults to
35
+ * {@link denoUpgrade}; on Cloudflare Workers you pass one built on
36
+ * `WebSocketPair` (see the README). Everything else here is standard
37
+ * `Request`, `Response` and `WebSocket`, so the same handler serves on Deno,
38
+ * Node and a Worker.
39
+ *
40
+ * ## The WebSocket protocol
41
+ * ```text
42
+ * → { subscribe: "<query>" | true, id: "<id>" } open (true = every commit)
43
+ * → { unsubscribe: "<id>" } close
44
+ * → { relay: Bundle[] } forward `sync: peers` values
45
+ * ← { id, bundles: Bundle[], gone?: Eid[] } the set, then every change
46
+ * ← { id, relay: Bundle[] } peer values from a client
47
+ * ← { id, refused: { error, message, … } } the subscription was refused
48
+ * ```
49
+ * No durable write crosses the socket: bundles are applied with
50
+ * `POST /apply`, and the socket is how every connected client learns about
51
+ * them. A relayed value is the one exception: it is forwarded to the other
52
+ * subscribers; components declaring `save` also keep periodic snapshots and
53
+ * their last value on disconnect, as the authenticated writer.
54
+ *
55
+ * @module
56
+ */ export { api, DOORS, routed, whoDoor } from "./route.js";
57
+ export { asking, signed, VIA } from "./actor.js";
58
+ export { applier, ask, CHUNK, pour, poured, writeOptions } from "./doors.js";
59
+ export { published } from "./publish.js";
60
+ export { readBody, writeBytes, writeSize } from "./body.js";
61
+ export { applyBatches } from "./batches.js";
62
+ export { cutOf, reads } from "./requests.js";
63
+ export { fault, faults, FILED, json, refusal, refuse, Unauthorized, waits } from "./refuse.js";
64
+ export { subscriptions } from "./subs.js";
65
+ export { attach, clockReply, decode, queue, receive, sink } from "./socket.js";
66
+ export { agent, requested, served } from "./request.js";
67
+ export { timed } from "./timing.js";
68
+ export { denoListen, denoUpgrade } from "./deno.js";
69
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuDC,GAED,SACE,GAAG,EAEH,KAAK,EAML,MAAM,EAGN,OAAO,qBACU;AACnB,SAAS,MAAM,EAAqB,MAAM,EAAE,GAAG,qBAAoB;AAGnE,SAEE,OAAO,EAEP,GAAG,EACH,KAAK,EAEL,IAAI,EACJ,MAAM,EAGN,YAAY,qBACK;AACnB,SAAS,SAAS,uBAAsB;AACxC,SAAS,QAAQ,EAAE,UAAU,EAAE,SAAS,oBAAmB;AAC3D,SAAS,YAAY,uBAAsB;AAC3C,SAAS,KAAK,EAAE,KAAK,wBAAuB;AAC5C,SACE,KAAK,EACL,MAAM,EACN,KAAK,EACL,IAAI,EAEJ,OAAO,EACP,MAAM,EAGN,YAAY,EACZ,KAAK,sBACa;AACpB,SASE,aAAa,oBACG;AAClB,SACE,MAAM,EACN,UAAU,EACV,MAAM,EAEN,KAAK,EACL,OAAO,EACP,IAAI,sBAGc;AACpB,SACE,KAAK,EAGL,SAAS,EACT,MAAM,uBAEa;AACrB,SAAS,KAAK,sBAAqB;AACnC,SAEE,UAAU,EACV,WAAW,oBAGK"}
package/mod.ts ADDED
@@ -0,0 +1,142 @@
1
+ /**
2
+ * @yaks/api — an HTTP and WebSocket interface to a yaks graph, as a plain
3
+ * request handler that runs in any JavaScript runtime.
4
+ *
5
+ * Given a {@link https://jsr.io/@yaks/graph | @yaks/graph} `Graph`, this
6
+ * package serves the three endpoints a client needs and nothing more:
7
+ *
8
+ * - **`POST /apply`** — the request body is a JSON array of bundles, applied
9
+ * in one transaction; the response body is that array as applied.
10
+ * `?check=1` runs every phase and then rolls the transaction back, so a
11
+ * caller spreading one array over several graphs can ask each of them
12
+ * before any of them keeps it. `content-type: application/x-ndjson` is the
13
+ * same endpoint with one bundle per line, for an import too big to parse or
14
+ * commit whole ({@link pour}).
15
+ * - **`GET /query?q=…`** (or `POST /query`) — a query string in, the bundles
16
+ * it selects out.
17
+ * - **`/ws`** — subscriptions: a saved query whose result is pushed again
18
+ * whenever a committed transaction changes it.
19
+ *
20
+ * ```ts
21
+ * import { api } from '@yaks/api'
22
+ * // Deno.serve(api({ graph, authenticate }))
23
+ * ```
24
+ *
25
+ * ## Attribution is decided here, not by the client
26
+ * A client can put anything in the JSON it posts, including whose name is on
27
+ * it. So every array that arrives has its `$actor` component replaced by the
28
+ * identity {@link Authenticate} returned for that request. `authenticate`
29
+ * runs on every request, and throwing {@link Unauthorized} from it answers
30
+ * that request with a 401.
31
+ *
32
+ * ## Two callbacks the application supplies
33
+ * {@link Authenticate} names the writer. {@link Upgrade} turns a request into
34
+ * a WebSocket — the one step no web standard covers — and defaults to
35
+ * {@link denoUpgrade}; on Cloudflare Workers you pass one built on
36
+ * `WebSocketPair` (see the README). Everything else here is standard
37
+ * `Request`, `Response` and `WebSocket`, so the same handler serves on Deno,
38
+ * Node and a Worker.
39
+ *
40
+ * ## The WebSocket protocol
41
+ * ```text
42
+ * → { subscribe: "<query>" | true, id: "<id>" } open (true = every commit)
43
+ * → { unsubscribe: "<id>" } close
44
+ * → { relay: Bundle[] } forward `sync: peers` values
45
+ * ← { id, bundles: Bundle[], gone?: Eid[] } the set, then every change
46
+ * ← { id, relay: Bundle[] } peer values from a client
47
+ * ← { id, refused: { error, message, … } } the subscription was refused
48
+ * ```
49
+ * No durable write crosses the socket: bundles are applied with
50
+ * `POST /apply`, and the socket is how every connected client learns about
51
+ * them. A relayed value is the one exception: it is forwarded to the other
52
+ * subscribers; components declaring `save` also keep periodic snapshots and
53
+ * their last value on disconnect, as the authenticated writer.
54
+ *
55
+ * @module
56
+ */
57
+
58
+ export {
59
+ api,
60
+ type Arrived,
61
+ DOORS,
62
+ type Filter,
63
+ type Handler,
64
+ type Offers,
65
+ type Options,
66
+ type Route,
67
+ routed,
68
+ type Standing,
69
+ type Told,
70
+ whoDoor,
71
+ } from './route.js'
72
+ export { asking, type Authenticate, signed, VIA } from './actor.js'
73
+ export type { PeerWriter } from './save.ts'
74
+ export type { Timer } from './relay.ts'
75
+ export {
76
+ type Applier,
77
+ applier,
78
+ type Applying,
79
+ ask,
80
+ CHUNK,
81
+ type ListQuery,
82
+ pour,
83
+ poured,
84
+ type Trusted,
85
+ type WriteContext,
86
+ writeOptions,
87
+ } from './doors.js'
88
+ export { published } from './publish.js'
89
+ export { readBody, writeBytes, writeSize } from './body.js'
90
+ export { applyBatches } from './batches.js'
91
+ export { cutOf, reads } from './requests.js'
92
+ export {
93
+ fault,
94
+ faults,
95
+ FILED,
96
+ json,
97
+ type Refusal,
98
+ refusal,
99
+ refuse,
100
+ type Reporter,
101
+ type Tags,
102
+ Unauthorized,
103
+ waits,
104
+ } from './refuse.js'
105
+ export {
106
+ type Ask,
107
+ type Frame,
108
+ type Listening,
109
+ type Opening,
110
+ type RecoverableListening,
111
+ type Revision,
112
+ type Sink,
113
+ type Subs,
114
+ subscriptions,
115
+ } from './subs.js'
116
+ export {
117
+ attach,
118
+ clockReply,
119
+ decode,
120
+ type Incoming,
121
+ queue,
122
+ receive,
123
+ sink,
124
+ type Socket,
125
+ type Upgrade,
126
+ } from './socket.js'
127
+ export {
128
+ agent,
129
+ type Answered,
130
+ type Report,
131
+ requested,
132
+ served,
133
+ type Watch,
134
+ } from './request.js'
135
+ export { timed } from './timing.js'
136
+ export {
137
+ type Addr,
138
+ denoListen,
139
+ denoUpgrade,
140
+ type Listen,
141
+ type Listener,
142
+ } from './deno.js'
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
1
  {
2
2
  "name": "@yaks/api",
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/api",
5
+ "type": "module",
6
+ "exports": {
7
+ "./headers": {
8
+ "types": "./_dist/headers.d.ts",
9
+ "default": "./headers.js"
10
+ },
11
+ ".": {
12
+ "types": "./_dist/mod.d.ts",
13
+ "default": "./mod.js"
14
+ },
15
+ "./vocab": {
16
+ "types": "./_dist/vocab.d.ts",
17
+ "default": "./vocab.js"
18
+ },
19
+ "./address": {
20
+ "types": "./_dist/address.d.ts",
21
+ "default": "./address.js"
22
+ },
23
+ "./tools": {
24
+ "types": "./_dist/tools.d.ts",
25
+ "default": "./tools.js"
26
+ },
27
+ "./routes": {
28
+ "types": "./_dist/routes.d.ts",
29
+ "default": "./routes.js"
30
+ },
31
+ "./timing": {
32
+ "types": "./_dist/timing.d.ts",
33
+ "default": "./timing.js"
34
+ },
35
+ "./refuse": {
36
+ "types": "./_dist/refuse.d.ts",
37
+ "default": "./refuse.js"
38
+ },
39
+ "./graph": {
40
+ "types": "./_dist/graph.d.ts",
41
+ "default": "./graph.js"
42
+ },
43
+ "./asset": {
44
+ "types": "./_dist/asset.d.ts",
45
+ "default": "./asset.js"
46
+ },
47
+ "./routed": {
48
+ "types": "./_dist/routed.d.ts",
49
+ "default": "./routed.js"
50
+ },
51
+ "./log": {
52
+ "types": "./_dist/log.d.ts",
53
+ "default": "./log.js"
54
+ },
55
+ "./receipts": {
56
+ "types": "./_dist/receipts.d.ts",
57
+ "default": "./receipts.js"
58
+ },
59
+ "./reached": {
60
+ "types": "./_dist/reached.d.ts",
61
+ "default": "./reached.js"
62
+ }
63
+ },
64
+ "license": "Apache-2.0",
65
+ "dependencies": {
66
+ "@yaks/fp": "0.2.36",
67
+ "@yaks/graph": "0.2.36",
68
+ "@yaks/match": "0.2.36",
69
+ "@yaks/query": "0.2.36",
70
+ "@yaks/sql": "0.2.36",
71
+ "@yaks/tools": "0.2.36",
72
+ "@yaks/trace": "0.2.36",
73
+ "@yaks/vocab": "0.2.36"
74
+ },
75
+ "description": "Serves graph writes, queries, and live query subscriptions through an HTTP and WebSocket request handler.",
76
+ "repository": "https://github.com/yak-sh/yak.git"
77
+ }
package/peer_query.js ADDED
@@ -0,0 +1,164 @@
1
+ // The durable candidates of a query that also reads peer-held components.
2
+ // Replacing a peer condition with false or true gives storage a superset of
3
+ // the durable rows that can match. Held peer entities are fetched by id and
4
+ // the original query decides membership over the joined values.
5
+ import { bare, parse } from "@yaks/query";
6
+ import { syncOf } from "@yaks/vocab";
7
+ let NEVER = {
8
+ kind: 'never'
9
+ };
10
+ let aim = (v, path, facet = false)=>{
11
+ try {
12
+ return v.aim(path.join('.'), facet);
13
+ } catch {
14
+ return [];
15
+ }
16
+ };
17
+ let peer = (v, path, facet = false)=>aim(v, path, facet).some((h)=>syncOf(v, h.comp) == 'peers');
18
+ let uses = (v, clauses)=>clauses.some((c)=>{
19
+ if (c.kind == 'and' || c.kind == 'or') return uses(v, c.clauses);
20
+ if (c.kind == 'refs' && c.op == '=' && c.value) {
21
+ return v.refProps().some(([comp])=>syncOf(v, comp) == 'peers');
22
+ }
23
+ if (c.kind == 'pred') {
24
+ return peer(v, c.path, bare(c)) || !!c.where && uses(v, [
25
+ c.where
26
+ ]);
27
+ }
28
+ if (c.kind == 'order') {
29
+ return peer(v, c.value.replace(/^-/, '').split('.'));
30
+ }
31
+ if (c.kind == 'distinct' || c.kind == 'tally') {
32
+ return peer(v, c.path);
33
+ }
34
+ return false;
35
+ });
36
+ // A positive predicate on the entity's own peer component cannot match a
37
+ // durable row without a held value. Other peer predicates become true here:
38
+ // absence, optional projections and an association's far endpoint can still
39
+ // match a durable row, so they cannot narrow storage candidates.
40
+ let positive = (v, c)=>{
41
+ if (c.kind != 'pred' || c.where || c.not || c.op == '?' || c.op == '!=') {
42
+ return false;
43
+ }
44
+ if (c.op == '=' && c.value?.kind == 'scalar' && c.value.raw == '') {
45
+ return false;
46
+ }
47
+ return syncOf(v, aim(v, c.path, bare(c))[0]?.comp ?? '') == 'peers';
48
+ };
49
+ // null is true and `never` is false while simplifying AND and OR. Directives
50
+ // are dropped so an order or limit never cuts candidates before the full
51
+ // query runs over durable rows and peer values together.
52
+ let relax = (v, c, without = false)=>{
53
+ if (c.kind == 'and' || c.kind == 'or') {
54
+ let parts = c.clauses.map((p)=>relax(v, p, without));
55
+ if (c.kind == 'and') {
56
+ if (parts.some((p)=>p?.kind == 'never')) return NEVER;
57
+ let kept = parts.filter((p)=>p != null);
58
+ return kept.length ? {
59
+ kind: 'and',
60
+ clauses: kept
61
+ } : null;
62
+ }
63
+ if (parts.some((p)=>p == null)) return null;
64
+ let kept = parts.filter((p)=>p?.kind != 'never');
65
+ return kept.length ? {
66
+ kind: 'or',
67
+ clauses: kept
68
+ } : NEVER;
69
+ }
70
+ if (c.kind == 'pred' && uses(v, [
71
+ c
72
+ ])) {
73
+ return without || positive(v, c) ? NEVER : null;
74
+ }
75
+ if (c.kind == 'order' || c.kind == 'limit' || c.kind == 'after' || c.kind == 'fields' || c.kind == 'every' || c.kind == 'count' || c.kind == 'distinct' || c.kind == 'tally') return null;
76
+ return c;
77
+ };
78
+ // A peer predicate reached through one reference can be refreshed by finding
79
+ // the rows that point at the moved peer. Negative predicates also match when
80
+ // that peer has no held value, so they keep the ordinary whole-answer path.
81
+ let referent = (q, v)=>{
82
+ let ref;
83
+ let count = 0;
84
+ let valid = true;
85
+ let walk = (cs)=>{
86
+ for (let c of cs){
87
+ if (c.kind == 'and' || c.kind == 'or') walk(c.clauses);
88
+ else if (c.kind == 'pred' && uses(v, [
89
+ c
90
+ ])) {
91
+ count++;
92
+ let hops = aim(v, c.path, bare(c));
93
+ let first = hops[0], last = hops[1];
94
+ if (c.where || c.not || c.op == '?' || c.op == '!=' || c.op == '=' && c.value?.kind == 'scalar' && c.value.raw == '' || hops.length != 2 || !first || v.prop(first.comp, first.prop)?.category != 'ref' || syncOf(v, last?.comp ?? '') != 'peers' || ref && (ref.comp != first.comp || ref.prop != first.prop)) {
95
+ valid = false;
96
+ continue;
97
+ }
98
+ if (!ref) ref = {
99
+ comp: first.comp,
100
+ prop: first.prop,
101
+ far: new Set()
102
+ };
103
+ ref.far.add(last.comp);
104
+ } else if (c.kind == 'pred') {
105
+ // A second relation could change a row's answer without moving the
106
+ // peer reached through this reference.
107
+ if (c.where || c.not || v.assoc(c.path[0]) || aim(v, c.path, bare(c)).length > 1) valid = false;
108
+ } else if (c.kind == 'order' || c.kind == 'limit' || c.kind == 'after' || c.kind == 'count' || c.kind == 'distinct' || c.kind == 'tally' || c.kind == 'refs' || c.kind == 'near' || c.kind == 'walk') valid = false;
109
+ }
110
+ };
111
+ walk(q.clauses);
112
+ return valid && count ? ref : undefined;
113
+ };
114
+ /** The original query reads peers, and the durable rows that might match it.
115
+ * `durable: null` means every matching row must have a held peer value. */ export let peerPlan = (q, v)=>{
116
+ if (!uses(v, q.clauses)) return {
117
+ peers: false,
118
+ durable: q
119
+ };
120
+ let ref = referent(q, v);
121
+ let clause = relax(v, q, !!ref);
122
+ if (clause?.kind == 'never') return {
123
+ peers: true,
124
+ durable: null,
125
+ ref
126
+ };
127
+ if (!clause) return {
128
+ peers: true,
129
+ durable: parse('.entity&*')
130
+ };
131
+ let clauses = clause.kind == 'and' ? clause.clauses : [
132
+ clause
133
+ ];
134
+ return {
135
+ peers: true,
136
+ durable: {
137
+ kind: 'and',
138
+ clauses: [
139
+ ...clauses,
140
+ {
141
+ kind: 'every'
142
+ }
143
+ ]
144
+ },
145
+ ...ref ? {
146
+ ref
147
+ } : {}
148
+ };
149
+ };
150
+ /** Targets needed by the in-memory matcher beside candidate rows. */ export let peerTargets = (q)=>{
151
+ let targets = new Set();
152
+ let walk = (clauses)=>{
153
+ for (let c of clauses){
154
+ if (c.kind == 'and' || c.kind == 'or') walk(c.clauses);
155
+ else if (c.kind == 'refs' && c.op == '=' && c.value) targets.add(c.value);
156
+ else if (c.kind == 'after' && 'eid' in c) targets.add(c.eid);
157
+ }
158
+ };
159
+ walk(q.clauses);
160
+ return [
161
+ ...targets
162
+ ];
163
+ };
164
+ //# sourceMappingURL=peer_query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peer_query.js","sources":["./peer_query.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,4EAA4E;AAC5E,gEAAgE;AAEhE,SAAmB,IAAI,EAAe,KAAK,2BAAgC;AAC3E,SAAS,MAAM,2BAA4C;AAE3D,IAAI,QAAgB;EAAE,MAAM;AAAQ;AAEpC,IAAI,MAAM,CAAC,GAAU,MAAgB,QAAQ,KAAK;EAChD,IAAI;IACF,OAAO,EAAE,GAAG,CAAC,KAAK,IAAI,CAAC,MAAM;EAC/B,EAAE,OAAM;IACN,OAAO,EAAE;EACX;AACF;AAEA,IAAI,OAAO,CAAC,GAAU,MAAgB,QAAQ,KAAK,GACjD,IAAI,GAAG,MAAM,OAAO,IAAI,CAAC,CAAC,IAAM,OAAO,GAAG,EAAE,IAAI,KAAK;AAEvD,IAAI,OAAO,CAAC,GAAU,UACpB,QAAQ,IAAI,CAAC,CAAC;IACZ,IAAI,EAAE,IAAI,IAAI,SAAS,EAAE,IAAI,IAAI,MAAM,OAAO,KAAK,GAAG,EAAE,OAAO;IAC/D,IAAI,EAAE,IAAI,IAAI,UAAU,EAAE,EAAE,IAAI,OAAO,EAAE,KAAK,EAAE;MAC9C,OAAO,E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,MAAM,GAAG,GAAG,CAAC,CAAC;EAC3B,IAAI,QAAQ,QAAQ,SAAS,OAAO;IAAE,OAAO;IAAM,SAAS;IAAM;EAAI;EACtE,IAAI,CAAC,QAAQ,OAAO;IAAE,OAAO;IAAM,SAAS,MAAM;EAAa;EAC/D,IAAI,UAAU,OAAO,IAAI,IAAI,QAAQ,OAAO,OAAO,GAAG;IAAC;GAAO;EAC9D,OAAO;IACL,OAAO;IACP,SAAS;MAAE,MAAM;MAAO,SAAS;WAAI;QAAS;UAAE,MAAM;QAAQ;OAAE;IAAC;IACjE,GAAI,MAAM;MAAE;IAAI,IAAI,CAAC,CAAC;EACxB;AACF,EAAC;AAED,mEAAmE,GACnE,OAAO,IAAI,cAAc,CAAC;EACxB,IAAI,UAAU,IAAI;EAClB,IAAI,OAAO,CAAC;IACV,KAAK,IAAI,KAAK,QAAS;MACrB,IAAI,EAAE,IAAI,IAAI,SAAS,EAAE,IAAI,IAAI,MAAM,KAAK,EAAE,OAAO;WAChD,IAAI,EAAE,IAAI,IAAI,UAAU,EAAE,EAAE,IAAI,OAAO,EAAE,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,KAAK;WACnE,IAAI,EAAE,IAAI,IAAI,WAAW,SAAS,GAAG,QAAQ,GAAG,CAAC,EAAE,GAAG;IAC7D;EACF;EACA,KAAK,EAAE,OAAO;EACd,OAAO;OAAI;GAAQ;AACrB,EAAC"}
package/peer_query.ts ADDED
@@ -0,0 +1,158 @@
1
+ // The durable candidates of a query that also reads peer-held components.
2
+ // Replacing a peer condition with false or true gives storage a superset of
3
+ // the durable rows that can match. Held peer entities are fetched by id and
4
+ // the original query decides membership over the joined values.
5
+
6
+ import { type And, bare, type Clause, parse } from '@yaks/query'
7
+ import { syncOf, type Vocab } from '@yaks/vocab'
8
+
9
+ let NEVER: Clause = { kind: 'never' }
10
+
11
+ let aim = (v: Vocab, path: string[], facet = false) => {
12
+ try {
13
+ return v.aim(path.join('.'), facet)
14
+ } catch {
15
+ return []
16
+ }
17
+ }
18
+
19
+ let peer = (v: Vocab, path: string[], facet = false) =>
20
+ aim(v, path, facet).some((h) => syncOf(v, h.comp) == 'peers')
21
+
22
+ let uses = (v: Vocab, clauses: Clause[]): boolean =>
23
+ clauses.some((c) => {
24
+ if (c.kind == 'and' || c.kind == 'or') return uses(v, c.clauses)
25
+ if (c.kind == 'refs' && c.op == '=' && c.value) {
26
+ return v.refProps().some(([comp]) => syncOf(v, comp) == 'peers')
27
+ }
28
+ if (c.kind == 'pred') {
29
+ return peer(v, c.path, bare(c)) ||
30
+ !!c.where && uses(v, [c.where])
31
+ }
32
+ if (c.kind == 'order') {
33
+ return peer(v, c.value.replace(/^-/, '').split('.'))
34
+ }
35
+ if (c.kind == 'distinct' || c.kind == 'tally') {
36
+ return peer(v, c.path)
37
+ }
38
+ return false
39
+ })
40
+
41
+ // A positive predicate on the entity's own peer component cannot match a
42
+ // durable row without a held value. Other peer predicates become true here:
43
+ // absence, optional projections and an association's far endpoint can still
44
+ // match a durable row, so they cannot narrow storage candidates.
45
+ let positive = (v: Vocab, c: Clause) => {
46
+ if (c.kind != 'pred' || c.where || c.not || c.op == '?' || c.op == '!=') {
47
+ return false
48
+ }
49
+ if (c.op == '=' && c.value?.kind == 'scalar' && c.value.raw == '') {
50
+ return false
51
+ }
52
+ return syncOf(v, aim(v, c.path, bare(c))[0]?.comp ?? '') == 'peers'
53
+ }
54
+
55
+ // null is true and `never` is false while simplifying AND and OR. Directives
56
+ // are dropped so an order or limit never cuts candidates before the full
57
+ // query runs over durable rows and peer values together.
58
+ let relax = (v: Vocab, c: Clause, without = false): Clause | null => {
59
+ if (c.kind == 'and' || c.kind == 'or') {
60
+ let parts = c.clauses.map((p) => relax(v, p, without))
61
+ if (c.kind == 'and') {
62
+ if (parts.some((p) => p?.kind == 'never')) return NEVER
63
+ let kept = parts.filter((p) => p != null)
64
+ return kept.length ? { kind: 'and', clauses: kept } : null
65
+ }
66
+ if (parts.some((p) => p == null)) return null
67
+ let kept = parts.filter((p) => p?.kind != 'never') as Clause[]
68
+ return kept.length ? { kind: 'or', clauses: kept } : NEVER
69
+ }
70
+ if (c.kind == 'pred' && uses(v, [c])) {
71
+ return without || positive(v, c) ? NEVER : null
72
+ }
73
+ if (
74
+ c.kind == 'order' || c.kind == 'limit' || c.kind == 'after' ||
75
+ c.kind == 'fields' || c.kind == 'every' || c.kind == 'count' ||
76
+ c.kind == 'distinct' || c.kind == 'tally'
77
+ ) return null
78
+ return c
79
+ }
80
+
81
+ // A peer predicate reached through one reference can be refreshed by finding
82
+ // the rows that point at the moved peer. Negative predicates also match when
83
+ // that peer has no held value, so they keep the ordinary whole-answer path.
84
+ let referent = (q: And, v: Vocab) => {
85
+ let ref: { comp: string; prop: string; far: Set<string> } | undefined
86
+ let count = 0
87
+ let valid = true
88
+ let walk = (cs: Clause[]) => {
89
+ for (let c of cs) {
90
+ if (c.kind == 'and' || c.kind == 'or') walk(c.clauses)
91
+ else if (c.kind == 'pred' && uses(v, [c])) {
92
+ count++
93
+ let hops = aim(v, c.path, bare(c))
94
+ let first = hops[0], last = hops[1]
95
+ if (
96
+ c.where || c.not || c.op == '?' || c.op == '!=' ||
97
+ c.op == '=' && c.value?.kind == 'scalar' && c.value.raw == '' ||
98
+ hops.length != 2 ||
99
+ !first || v.prop(first.comp, first.prop)?.category != 'ref' ||
100
+ syncOf(v, last?.comp ?? '') != 'peers' ||
101
+ ref && (ref.comp != first.comp || ref.prop != first.prop)
102
+ ) {
103
+ valid = false
104
+ continue
105
+ }
106
+ if (!ref) ref = { comp: first.comp, prop: first.prop, far: new Set() }
107
+ ref.far.add(last.comp)
108
+ } else if (c.kind == 'pred') {
109
+ // A second relation could change a row's answer without moving the
110
+ // peer reached through this reference.
111
+ if (
112
+ c.where || c.not || v.assoc(c.path[0]) ||
113
+ aim(v, c.path, bare(c)).length > 1
114
+ ) valid = false
115
+ } else if (
116
+ c.kind == 'order' || c.kind == 'limit' || c.kind == 'after' ||
117
+ c.kind == 'count' || c.kind == 'distinct' || c.kind == 'tally' ||
118
+ c.kind == 'refs' || c.kind == 'near' || c.kind == 'walk'
119
+ ) valid = false
120
+ }
121
+ }
122
+ walk(q.clauses)
123
+ return valid && count ? ref : undefined
124
+ }
125
+
126
+ /** The original query reads peers, and the durable rows that might match it.
127
+ * `durable: null` means every matching row must have a held peer value. */
128
+ export let peerPlan = (q: And, v: Vocab): {
129
+ peers: boolean
130
+ durable: And | null
131
+ ref?: { comp: string; prop: string; far: Set<string> }
132
+ } => {
133
+ if (!uses(v, q.clauses)) return { peers: false, durable: q }
134
+ let ref = referent(q, v)
135
+ let clause = relax(v, q, !!ref)
136
+ if (clause?.kind == 'never') return { peers: true, durable: null, ref }
137
+ if (!clause) return { peers: true, durable: parse('.entity&*') }
138
+ let clauses = clause.kind == 'and' ? clause.clauses : [clause]
139
+ return {
140
+ peers: true,
141
+ durable: { kind: 'and', clauses: [...clauses, { kind: 'every' }] },
142
+ ...(ref ? { ref } : {}),
143
+ }
144
+ }
145
+
146
+ /** Targets needed by the in-memory matcher beside candidate rows. */
147
+ export let peerTargets = (q: And): string[] => {
148
+ let targets = new Set<string>()
149
+ let walk = (clauses: Clause[]) => {
150
+ for (let c of clauses) {
151
+ if (c.kind == 'and' || c.kind == 'or') walk(c.clauses)
152
+ else if (c.kind == 'refs' && c.op == '=' && c.value) targets.add(c.value)
153
+ else if (c.kind == 'after' && 'eid' in c) targets.add(c.eid)
154
+ }
155
+ }
156
+ walk(q.clauses)
157
+ return [...targets]
158
+ }
package/publish.js ADDED
@@ -0,0 +1,7 @@
1
+ import { reserved } from "@yaks/graph/bundle";
2
+ import { syncOf } from "@yaks/vocab";
3
+ /** The bundles a transport may send. A `sync: none` component belongs to
4
+ * this node alone; identities, pipeline metadata and every other component
5
+ * retain their values, including server-owned stamps and deletion markers.
6
+ * This projection never changes the graph's stored or applied bundles. */ export let published = (vocab, bundles)=>bundles.map((b)=>Object.fromEntries(Object.entries(b).filter(([name])=>reserved(name) || syncOf(vocab, name) != 'none')));
7
+ //# sourceMappingURL=publish.js.map
package/publish.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publish.js","sources":["./publish.ts"],"names":[],"mappings":"AACA,SAAS,QAAQ,kCAAuC;AACxD,SAAS,MAAM,2BAA4C;AAE3D;;;wEAGwE,GACxE,OAAO,IAAI,YAAY,CAAC,OAAc,UACpC,QAAQ,GAAG,CAAC,CAAC,IACX,OAAO,WAAW,CAChB,OAAO,OAAO,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,GAC9B,SAAS,SAAS,OAAO,OAAO,SAAS,UAG9C"}
package/publish.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
2
+ import { reserved } from '@yaks/graph/bundle'
3
+ import { syncOf, type Vocab } from '@yaks/vocab'
4
+
5
+ /** The bundles a transport may send. A `sync: none` component belongs to
6
+ * this node alone; identities, pipeline metadata and every other component
7
+ * retain their values, including server-owned stamps and deletion markers.
8
+ * This projection never changes the graph's stored or applied bundles. */
9
+ export let published = (vocab: Vocab, bundles: Bundle[]): Bundle[] =>
10
+ bundles.map((b) =>
11
+ Object.fromEntries(
12
+ Object.entries(b).filter(([name]) =>
13
+ reserved(name) || syncOf(vocab, name) != 'none'
14
+ ),
15
+ ) as Bundle
16
+ )