trellis 3.2.0 → 3.2.3

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 (260) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +61 -171
  3. package/dist/browser/index.d.ts +18 -0
  4. package/dist/browser/index.d.ts.map +1 -0
  5. package/dist/browser/index.js +95 -0
  6. package/dist/{chunk-3ETIIPPB.js → chunk-2AACZRII.js} +312 -30
  7. package/dist/{chunk-3GDRZTAW.js → chunk-5KSGGKET.js} +1 -1
  8. package/dist/{chunk-MBNBTJCR.js → chunk-6GEZ6DN5.js} +2 -2
  9. package/dist/chunk-6RWGFC3N.js +403 -0
  10. package/dist/{chunk-X762XWVC.js → chunk-7DUMWO5L.js} +1 -1
  11. package/dist/chunk-CAG4FULI.js +121 -0
  12. package/dist/{chunk-GD5QJXSS.js → chunk-FWRS4CSC.js} +2 -2
  13. package/dist/{chunk-36VSSTL7.js → chunk-GU374KWZ.js} +1 -1
  14. package/dist/{chunk-BJ7EZ7FP.js → chunk-JHHEXEDM.js} +17 -0
  15. package/dist/{chunk-7WQGTFVJ.js → chunk-L5N5PR2S.js} +130 -75
  16. package/dist/{chunk-BXSTJVE3.js → chunk-N6MYZT4O.js} +2 -1
  17. package/dist/chunk-NWSW5YNS.js +225 -0
  18. package/dist/{chunk-HO4TURXW.js → chunk-S3XLFZ27.js} +28 -11
  19. package/dist/{chunk-BQBUVMRN.js → chunk-S6GBJ2ZB.js} +25 -14
  20. package/dist/{chunk-B3ZIFA2O.js → chunk-VBYUTRXX.js} +1 -1
  21. package/dist/cli/deploy-cli.d.ts +18 -0
  22. package/dist/cli/deploy-cli.d.ts.map +1 -0
  23. package/dist/cli/deploy-cli.js +67 -0
  24. package/dist/cli/index.d.ts +1 -1
  25. package/dist/cli/index.d.ts.map +1 -1
  26. package/dist/cli/index.js +153 -100
  27. package/dist/client/index.browser.d.ts +29 -0
  28. package/dist/client/index.browser.d.ts.map +1 -0
  29. package/dist/client/index.browser.js +64 -0
  30. package/dist/client/index.js +10 -10
  31. package/dist/client/sdk.browser.d.ts +131 -0
  32. package/dist/client/sdk.browser.d.ts.map +1 -0
  33. package/dist/{sdk-LZ7W3EEY.js → client/sdk.browser.js} +2 -2
  34. package/dist/client/sdk.js +1 -1
  35. package/dist/db/index.js +16 -16
  36. package/dist/db/inspector.js +6 -6
  37. package/dist/decisions/index.js +2 -2
  38. package/dist/embeddings/auto-embed.d.ts.map +1 -1
  39. package/dist/embeddings/index.js +1 -1
  40. package/dist/embeddings/store.d.ts.map +1 -1
  41. package/dist/engine.d.ts +27 -7
  42. package/dist/engine.d.ts.map +1 -1
  43. package/dist/index.js +6 -6
  44. package/dist/mcp/server.d.ts.map +1 -1
  45. package/dist/react/index.js +2 -2
  46. package/dist/react/schema-hooks.js +7 -7
  47. package/dist/{remote-manager-SG545WNY.js → remote-manager-ODE3T2KR.js} +5 -5
  48. package/dist/scaffold/seed.d.ts.map +1 -1
  49. package/dist/scaffold/write.d.ts +1 -1
  50. package/dist/scaffold/write.d.ts.map +1 -1
  51. package/dist/schema/index.js +4 -3
  52. package/dist/server/cors.d.ts +13 -0
  53. package/dist/server/cors.d.ts.map +1 -0
  54. package/dist/server/deploy-meta.d.ts +25 -0
  55. package/dist/server/deploy-meta.d.ts.map +1 -0
  56. package/dist/server/deploy.d.ts +8 -3
  57. package/dist/server/deploy.d.ts.map +1 -1
  58. package/dist/server/deploy.js +9 -0
  59. package/dist/server/index.d.ts +5 -2
  60. package/dist/server/index.d.ts.map +1 -1
  61. package/dist/server/index.js +51 -17
  62. package/dist/server/realtime.d.ts +4 -1
  63. package/dist/server/realtime.d.ts.map +1 -1
  64. package/dist/server/server.d.ts +1 -0
  65. package/dist/server/server.d.ts.map +1 -1
  66. package/dist/server/sprites.d.ts +18 -1
  67. package/dist/server/sprites.d.ts.map +1 -1
  68. package/dist/server/tenancy.d.ts +4 -0
  69. package/dist/server/tenancy.d.ts.map +1 -1
  70. package/dist/server/usage-meter.d.ts +53 -0
  71. package/dist/server/usage-meter.d.ts.map +1 -0
  72. package/dist/{server-IGBIIJSF.js → server-OGVWNDNK.js} +6 -1
  73. package/dist/sprites-KQGTGM6W.js +27 -0
  74. package/dist/{tenancy-OP34NFFS.js → tenancy-POMWQP6M.js} +5 -3
  75. package/dist/vcs/index.js +3 -3
  76. package/dist/vcs/types.d.ts +2 -0
  77. package/dist/vcs/types.d.ts.map +1 -1
  78. package/package.json +17 -6
  79. package/dist/better-sqlite-backend-PII64VE6.js +0 -159
  80. package/dist/better-sqlite-backend-VUBVQ7SN.js +0 -209
  81. package/dist/better-sqlite-backend-Y3AQN65E.js +0 -159
  82. package/dist/better-sqlite-backend-Y3M7DXOX.js +0 -159
  83. package/dist/boot-middleware-FJDGVUM4.js +0 -9
  84. package/dist/boot-middleware-MLJUTLUM.js +0 -9
  85. package/dist/boot-middleware-ZZPDZNYR.js +0 -9
  86. package/dist/chunk-276LKLZR.js +0 -359
  87. package/dist/chunk-2CX5UCRV.js +0 -111
  88. package/dist/chunk-2ESYSVXG.js +0 -48
  89. package/dist/chunk-2I5JU73Y.js +0 -340
  90. package/dist/chunk-2UNSRRNB.js +0 -171
  91. package/dist/chunk-335UEHQR.js +0 -907
  92. package/dist/chunk-4CF2L46N.js +0 -29
  93. package/dist/chunk-5CINODKT.js +0 -41
  94. package/dist/chunk-5ESGWJ5L.js +0 -45
  95. package/dist/chunk-5FGLQGMF.js +0 -1676
  96. package/dist/chunk-5IQZFITY.js +0 -357
  97. package/dist/chunk-5JWFAQDW.js +0 -150
  98. package/dist/chunk-62UHTVU3.js +0 -141
  99. package/dist/chunk-66UJQT2I.js +0 -35
  100. package/dist/chunk-6RNNVOAS.js +0 -355
  101. package/dist/chunk-6X23PZ6H.js +0 -969
  102. package/dist/chunk-7C7LL6MB.js +0 -315
  103. package/dist/chunk-7F4HM3JG.js +0 -394
  104. package/dist/chunk-7KJ7T4D3.js +0 -910
  105. package/dist/chunk-7QQQNUB5.js +0 -92
  106. package/dist/chunk-7QWJBS72.js +0 -946
  107. package/dist/chunk-7R36OZK3.js +0 -957
  108. package/dist/chunk-7UPKCOHE.js +0 -946
  109. package/dist/chunk-A3BONW34.js +0 -35
  110. package/dist/chunk-AHMDVHBL.js +0 -141
  111. package/dist/chunk-AW6A4OZY.js +0 -171
  112. package/dist/chunk-BJ5EKCJ5.js +0 -458
  113. package/dist/chunk-BRGYDO7D.js +0 -223
  114. package/dist/chunk-C5KPSEDN.js +0 -359
  115. package/dist/chunk-CCTNYTUC.js +0 -940
  116. package/dist/chunk-CYQ2OVD2.js +0 -35
  117. package/dist/chunk-DDAK7YOE.js +0 -299
  118. package/dist/chunk-DRILKBY6.js +0 -965
  119. package/dist/chunk-DXE24HNN.js +0 -45
  120. package/dist/chunk-E7ICUNW7.js +0 -945
  121. package/dist/chunk-EAL2PUOE.js +0 -10022
  122. package/dist/chunk-ELT7MLZB.js +0 -109
  123. package/dist/chunk-EWEIRF6G.js +0 -171
  124. package/dist/chunk-F5VDGQJC.js +0 -120
  125. package/dist/chunk-FCPL3IEQ.js +0 -98
  126. package/dist/chunk-FDPKYSV7.js +0 -96
  127. package/dist/chunk-FES3TC24.js +0 -171
  128. package/dist/chunk-FF36CQHZ.js +0 -61
  129. package/dist/chunk-FOWKR5TW.js +0 -129
  130. package/dist/chunk-FZVCXDYW.js +0 -150
  131. package/dist/chunk-G3XIHPSQ.js +0 -361
  132. package/dist/chunk-GKJLYLVU.js +0 -359
  133. package/dist/chunk-GNXHXNVQ.js +0 -113
  134. package/dist/chunk-GSR6ILTM.js +0 -277
  135. package/dist/chunk-GWRIMRSX.js +0 -45
  136. package/dist/chunk-H72UW2HJ.js +0 -45
  137. package/dist/chunk-HGI74PXB.js +0 -7417
  138. package/dist/chunk-HGRA37LO.js +0 -145
  139. package/dist/chunk-HXSRQO3L.js +0 -145
  140. package/dist/chunk-IG2X2BVO.js +0 -373
  141. package/dist/chunk-IIJVDD54.js +0 -373
  142. package/dist/chunk-IUWVEEOX.js +0 -145
  143. package/dist/chunk-IX4BBOIT.js +0 -394
  144. package/dist/chunk-IXU4RMQM.js +0 -349
  145. package/dist/chunk-J6OUIION.js +0 -857
  146. package/dist/chunk-K46RNFDE.js +0 -171
  147. package/dist/chunk-KAGU2XMY.js +0 -359
  148. package/dist/chunk-KCVYJNVX.js +0 -394
  149. package/dist/chunk-KKDRMIV6.js +0 -419
  150. package/dist/chunk-KYLIQ3YI.js +0 -399
  151. package/dist/chunk-KZYJJIRN.js +0 -946
  152. package/dist/chunk-LFOX744K.js +0 -96
  153. package/dist/chunk-LHOVJEWR.js +0 -7403
  154. package/dist/chunk-LOPXTPF3.js +0 -120
  155. package/dist/chunk-LZR3VIFU.js +0 -907
  156. package/dist/chunk-MCKGQKYU.js +0 -15
  157. package/dist/chunk-MU6EOS6F.js +0 -0
  158. package/dist/chunk-MWRQ2TXT.js +0 -357
  159. package/dist/chunk-NO73OK2I.js +0 -102
  160. package/dist/chunk-NPE2YJHK.js +0 -394
  161. package/dist/chunk-NSDXU6QX.js +0 -2213
  162. package/dist/chunk-NXIRDSZ6.js +0 -145
  163. package/dist/chunk-NZWFNHCS.js +0 -407
  164. package/dist/chunk-OB4CMMX6.js +0 -260
  165. package/dist/chunk-OBKU74FU.js +0 -7411
  166. package/dist/chunk-OQNDWRDE.js +0 -145
  167. package/dist/chunk-OVWAJMB6.js +0 -394
  168. package/dist/chunk-PDHLBTTD.js +0 -45
  169. package/dist/chunk-PEQNRKLG.js +0 -210
  170. package/dist/chunk-QERIS7QY.js +0 -7403
  171. package/dist/chunk-QHA2DM3C.js +0 -373
  172. package/dist/chunk-QLM4HSYC.js +0 -45
  173. package/dist/chunk-S2OT3TQJ.js +0 -283
  174. package/dist/chunk-S5PHSN5K.js +0 -907
  175. package/dist/chunk-SIAA4J6H.js +0 -21
  176. package/dist/chunk-SQBNLUIA.js +0 -171
  177. package/dist/chunk-STLN6A6B.js +0 -355
  178. package/dist/chunk-U7HV7GU2.js +0 -17
  179. package/dist/chunk-UGZHOGRA.js +0 -39
  180. package/dist/chunk-UICF66FS.js +0 -359
  181. package/dist/chunk-UK3EQREE.js +0 -1078
  182. package/dist/chunk-UPKPURVY.js +0 -394
  183. package/dist/chunk-URC74VCN.js +0 -959
  184. package/dist/chunk-V3G7Q3QQ.js +0 -102
  185. package/dist/chunk-VUJ6SA55.js +0 -303
  186. package/dist/chunk-VUJQLIML.js +0 -141
  187. package/dist/chunk-VWKLKLYB.js +0 -267
  188. package/dist/chunk-VYC3RZMK.js +0 -277
  189. package/dist/chunk-WMERMSWI.js +0 -154
  190. package/dist/chunk-WMR777PO.js +0 -2213
  191. package/dist/chunk-X46IECAC.js +0 -517
  192. package/dist/chunk-XEN3OEYY.js +0 -2234
  193. package/dist/chunk-XZQ66K7A.js +0 -124
  194. package/dist/chunk-YY3GF7DH.js +0 -7411
  195. package/dist/chunk-ZE5R3H4C.js +0 -907
  196. package/dist/chunk-ZHVI7BQN.js +0 -1077
  197. package/dist/chunk-ZMN5LUB7.js +0 -7411
  198. package/dist/chunk-ZUV4MD5U.js +0 -94
  199. package/dist/config-KMY6RRK3.js +0 -19
  200. package/dist/config-VDMK2DUJ.js +0 -19
  201. package/dist/db/trellis.css +0 -1
  202. package/dist/deploy-66EC752W.js +0 -9
  203. package/dist/deploy-SO7AFK6L.js +0 -9
  204. package/dist/engine-7GXBM4IT.js +0 -7
  205. package/dist/engine-NSR6BIF7.js +0 -7
  206. package/dist/factory-2P64OC7M.js +0 -34
  207. package/dist/factory-YP4ZZK5K.js +0 -34
  208. package/dist/import-AJLYHFIA.js +0 -10
  209. package/dist/launch-explorer-AI6L2AWJ.js +0 -91
  210. package/dist/query-GJL3CVGN.js +0 -31
  211. package/dist/query-MBJSK3EB.js +0 -31
  212. package/dist/query-TJHEVSKL.js +0 -31
  213. package/dist/query-WUIKOHPE.js +0 -524
  214. package/dist/remote-manager-BLJZ6KF6.js +0 -224
  215. package/dist/remote-manager-BPYE46SI.js +0 -223
  216. package/dist/remote-manager-FMESEAFA.js +0 -224
  217. package/dist/remote-manager-K2WZ5RHY.js +0 -223
  218. package/dist/remote-manager-SG5PQKCH.js +0 -224
  219. package/dist/remote-manager-WGP6MIQZ.js +0 -224
  220. package/dist/sdk-3KBB3E6V.js +0 -16
  221. package/dist/sdk-4ZQO6UTU.js +0 -16
  222. package/dist/sdk-6JA6GV3G.js +0 -16
  223. package/dist/sdk-6VTP5UTZ.js +0 -16
  224. package/dist/sdk-7VS6TYCD.js +0 -16
  225. package/dist/sdk-HQ3FTLRT.js +0 -16
  226. package/dist/sdk-HZ7NU3XE.js +0 -15
  227. package/dist/sdk-KBQE2OXT.js +0 -16
  228. package/dist/sdk-MSCDZ7JU.js +0 -15
  229. package/dist/sdk-WNZOGO3S.js +0 -15
  230. package/dist/sdk-YLV33ZCW.js +0 -9
  231. package/dist/sdk-ZSUD2LLJ.js +0 -16
  232. package/dist/server-AFXFFBWA.js +0 -11
  233. package/dist/server-AGHC4VUL.js +0 -11
  234. package/dist/server-BOH7M4TT.js +0 -13
  235. package/dist/server-DBUY2KY5.js +0 -11
  236. package/dist/server-GWBQJPRG.js +0 -11
  237. package/dist/server-JZZFULVU.js +0 -11
  238. package/dist/server-L5XAF4M5.js +0 -11
  239. package/dist/server-NYFUHANA.js +0 -12
  240. package/dist/server-OABUB2LO.js +0 -12
  241. package/dist/server-P2CKZWWV.js +0 -12
  242. package/dist/sprites-RU3E4XQN.js +0 -15
  243. package/dist/sprites-XVSMQLSI.js +0 -15
  244. package/dist/sql-wasm-FORCZFHZ.js +0 -2148
  245. package/dist/sql-wasm-JZH76RLO.js +0 -2148
  246. package/dist/sqljs-backend-CJQOIQYE.js +0 -266
  247. package/dist/sqljs-backend-KWXBUDUO.js +0 -266
  248. package/dist/state-demo.bundle.js +0 -1033
  249. package/dist/tenancy-CBWYEVUB.js +0 -11
  250. package/dist/tenancy-FO6WVYFN.js +0 -13
  251. package/dist/tenancy-FWDBESMX.js +0 -14
  252. package/dist/tenancy-HGPIS5QA.js +0 -13
  253. package/dist/tenancy-HXFALA37.js +0 -14
  254. package/dist/tenancy-S676GLID.js +0 -14
  255. package/dist/tenancy-V7ARWNNV.js +0 -14
  256. package/dist/tenancy-ZDN4GXK5.js +0 -13
  257. package/dist/tenancy-ZT5VMY7B.js +0 -14
  258. package/dist/vm-config-YZRJ3KUB.js +0 -21
  259. /package/dist/{chunk-2BRTCEHB.js → chunk-PZ4XYZ4J.js} +0 -0
  260. /package/dist/{chunk-EO3J7GRP.js → chunk-WXSWA3MV.js} +0 -0
@@ -1,458 +0,0 @@
1
- import {
2
- init_reconciler,
3
- reconcile
4
- } from "./chunk-4HYPLFJD.js";
5
-
6
- // src/sync/types.ts
7
- var PROTOCOL_VERSION = 1;
8
- var MIN_SUPPORTED_VERSION = 1;
9
- var MAX_SUPPORTED_VERSION = 1;
10
-
11
- // src/sync/sync-engine.ts
12
- init_reconciler();
13
- var SyncEngine = class {
14
- localPeerId;
15
- state;
16
- transport;
17
- getLocalOps;
18
- onOpsReceived;
19
- onNackReceived;
20
- branchPolicy;
21
- constructor(opts) {
22
- this.localPeerId = opts.localPeerId;
23
- this.transport = opts.transport;
24
- this.getLocalOps = opts.getLocalOps;
25
- this.onOpsReceived = opts.onOpsReceived;
26
- this.onNackReceived = opts.onNackReceived;
27
- this.branchPolicy = opts.branchPolicy ?? { linear: true };
28
- this.state = {
29
- localPeerId: opts.localPeerId,
30
- peerHeads: /* @__PURE__ */ new Map(),
31
- pendingAcks: /* @__PURE__ */ new Set(),
32
- lastSync: /* @__PURE__ */ new Map()
33
- };
34
- this.transport.onMessage((msg) => this.handleMessage(msg));
35
- }
36
- // -------------------------------------------------------------------------
37
- // Public API
38
- // -------------------------------------------------------------------------
39
- /**
40
- * Initiate a sync with a specific peer.
41
- * Sends a 'have' message advertising our heads.
42
- */
43
- async pushTo(peerId) {
44
- const ops = this.getLocalOps();
45
- const heads = {};
46
- if (ops.length > 0) {
47
- heads["main"] = ops[ops.length - 1].hash;
48
- }
49
- await this.transport.send(peerId, {
50
- version: PROTOCOL_VERSION,
51
- type: "have",
52
- peerId: this.localPeerId,
53
- heads,
54
- opCount: ops.length
55
- });
56
- }
57
- /**
58
- * Request ops from a peer.
59
- */
60
- async pullFrom(peerId) {
61
- const ops = this.getLocalOps();
62
- const lastHash = ops.length > 0 ? ops[ops.length - 1].hash : void 0;
63
- await this.transport.send(peerId, {
64
- version: PROTOCOL_VERSION,
65
- type: "want",
66
- peerId: this.localPeerId,
67
- wantHashes: [],
68
- afterHash: lastHash
69
- });
70
- }
71
- /**
72
- * Request the peer's complete op set and rely on hash dedupe during ingest.
73
- */
74
- async pullAllFrom(peerId) {
75
- await this.transport.send(peerId, {
76
- version: PROTOCOL_VERSION,
77
- type: "want",
78
- peerId: this.localPeerId,
79
- wantHashes: []
80
- });
81
- }
82
- /**
83
- * Send all our ops to a peer (full push).
84
- */
85
- async sendOps(peerId, ops) {
86
- const opsToSend = ops ?? this.getLocalOps();
87
- await this.sendOpsMessage(peerId, opsToSend);
88
- }
89
- /**
90
- * Internal: send an `ops` message and track outbound hashes in
91
- * `pendingAcks` until the receiver acks or nacks them. All three sites
92
- * that emit `ops` (sendOps, handleHave, handleWant) route through here so
93
- * pendingAcks reflects every outbound op uniformly.
94
- */
95
- async sendOpsMessage(peerId, ops) {
96
- if (ops.length === 0) return;
97
- for (const op of ops) {
98
- this.state.pendingAcks.add(op.hash);
99
- }
100
- await this.transport.send(peerId, {
101
- version: PROTOCOL_VERSION,
102
- type: "ops",
103
- peerId: this.localPeerId,
104
- ops
105
- });
106
- }
107
- /**
108
- * Reconcile our ops with a remote peer's ops.
109
- */
110
- reconcileWith(remoteOps) {
111
- const localOps = this.getLocalOps();
112
- return reconcile(localOps, remoteOps);
113
- }
114
- /**
115
- * Get current sync state.
116
- */
117
- getState() {
118
- return this.state;
119
- }
120
- /**
121
- * Get branch policy.
122
- */
123
- getBranchPolicy() {
124
- return this.branchPolicy;
125
- }
126
- /**
127
- * Set branch policy.
128
- */
129
- setBranchPolicy(policy) {
130
- this.branchPolicy = policy;
131
- }
132
- /**
133
- * List known peers.
134
- */
135
- listPeers() {
136
- return this.transport.peers();
137
- }
138
- // -------------------------------------------------------------------------
139
- // Message handling
140
- // -------------------------------------------------------------------------
141
- async handleMessage(msg) {
142
- if (typeof msg.version !== "number" || msg.version < MIN_SUPPORTED_VERSION || msg.version > MAX_SUPPORTED_VERSION) {
143
- await this.transport.send(msg.peerId, {
144
- version: PROTOCOL_VERSION,
145
- type: "nack",
146
- peerId: this.localPeerId,
147
- refs: [],
148
- reason: "protocol-version",
149
- details: `Unsupported protocol version ${msg.version}; supported range is ${MIN_SUPPORTED_VERSION}-${MAX_SUPPORTED_VERSION}.`
150
- });
151
- return;
152
- }
153
- switch (msg.type) {
154
- case "have":
155
- await this.handleHave(msg);
156
- break;
157
- case "want":
158
- await this.handleWant(msg);
159
- break;
160
- case "ops":
161
- await this.handleOps(msg);
162
- break;
163
- case "ack":
164
- this.handleAck(msg);
165
- break;
166
- case "nack":
167
- await this.handleNack(msg);
168
- break;
169
- }
170
- }
171
- async handleHave(msg) {
172
- this.state.peerHeads.set(msg.peerId, msg.heads);
173
- const localOps = this.getLocalOps();
174
- const localHashes = new Set(localOps.map((o) => o.hash));
175
- for (const [, hash] of Object.entries(msg.heads)) {
176
- if (!localHashes.has(hash)) {
177
- const afterHash = msg.opCount > localOps.length ? void 0 : localOps.length > 0 ? localOps[localOps.length - 1].hash : void 0;
178
- await this.transport.send(msg.peerId, {
179
- version: PROTOCOL_VERSION,
180
- type: "want",
181
- peerId: this.localPeerId,
182
- wantHashes: [],
183
- afterHash
184
- });
185
- return;
186
- }
187
- }
188
- if (msg.opCount > localOps.length) {
189
- await this.transport.send(msg.peerId, {
190
- version: PROTOCOL_VERSION,
191
- type: "want",
192
- peerId: this.localPeerId,
193
- wantHashes: []
194
- });
195
- return;
196
- }
197
- const peerOpCount = msg.opCount;
198
- if (localOps.length > peerOpCount) {
199
- await this.sendOpsMessage(msg.peerId, localOps.slice(peerOpCount));
200
- }
201
- }
202
- async handleWant(msg) {
203
- const localOps = this.getLocalOps();
204
- let opsToSend;
205
- if (msg.afterHash) {
206
- const idx = localOps.findIndex((o) => o.hash === msg.afterHash);
207
- opsToSend = idx >= 0 ? localOps.slice(idx + 1) : localOps;
208
- } else if (msg.wantHashes.length > 0) {
209
- const wanted = new Set(msg.wantHashes);
210
- opsToSend = localOps.filter((o) => wanted.has(o.hash));
211
- } else {
212
- opsToSend = localOps;
213
- }
214
- await this.sendOpsMessage(msg.peerId, opsToSend);
215
- }
216
- async handleOps(msg) {
217
- if (msg.ops.length === 0) return;
218
- let result;
219
- if (this.branchPolicy.linear) {
220
- const localOps = this.getLocalOps();
221
- const localHashes = new Set(localOps.map((o) => o.hash));
222
- const newOps = msg.ops.filter((o) => !localHashes.has(o.hash));
223
- if (newOps.length > 0) {
224
- result = await this.onOpsReceived(newOps) ?? void 0;
225
- }
226
- } else {
227
- const reconciled = this.reconcileWith(msg.ops);
228
- if (reconciled.uniqueToB.length > 0) {
229
- result = await this.onOpsReceived(reconciled.uniqueToB) ?? void 0;
230
- }
231
- }
232
- const rejections = result?.rejections ?? [];
233
- if (rejections.length > 0) {
234
- const byReason = /* @__PURE__ */ new Map();
235
- for (const r of rejections) {
236
- const entry = byReason.get(r.reason) ?? {
237
- refs: [],
238
- details: r.details
239
- };
240
- entry.refs.push(r.hash);
241
- byReason.set(r.reason, entry);
242
- }
243
- for (const [reason, entry] of byReason) {
244
- await this.transport.send(msg.peerId, {
245
- version: PROTOCOL_VERSION,
246
- type: "nack",
247
- peerId: this.localPeerId,
248
- refs: entry.refs,
249
- reason,
250
- details: entry.details
251
- });
252
- }
253
- }
254
- const rejectedSet = new Set(rejections.map((r) => r.hash));
255
- const ackHashes = msg.ops.map((o) => o.hash).filter((h) => !rejectedSet.has(h));
256
- if (ackHashes.length > 0) {
257
- await this.transport.send(msg.peerId, {
258
- version: PROTOCOL_VERSION,
259
- type: "ack",
260
- peerId: this.localPeerId,
261
- integrated: ackHashes
262
- });
263
- }
264
- this.state.lastSync.set(msg.peerId, (/* @__PURE__ */ new Date()).toISOString());
265
- }
266
- handleAck(msg) {
267
- for (const hash of msg.integrated) {
268
- this.state.pendingAcks.delete(hash);
269
- }
270
- this.state.lastSync.set(msg.peerId, (/* @__PURE__ */ new Date()).toISOString());
271
- }
272
- async handleNack(msg) {
273
- for (const ref of msg.refs) {
274
- this.state.pendingAcks.delete(ref);
275
- }
276
- this.state.lastSync.set(msg.peerId, (/* @__PURE__ */ new Date()).toISOString());
277
- if (this.onNackReceived) {
278
- await this.onNackReceived(msg);
279
- }
280
- }
281
- };
282
-
283
- // src/sync/vcs-sync-peer.ts
284
- var TrellisVcsSyncPeer = class {
285
- engine;
286
- syncEngine;
287
- integrationResults = [];
288
- remoteNacks = [];
289
- constructor(opts) {
290
- this.engine = opts.engine;
291
- this.syncEngine = new SyncEngine({
292
- localPeerId: opts.peerId,
293
- transport: opts.transport,
294
- getLocalOps: () => this.engine.getOps(),
295
- onOpsReceived: async (ops) => {
296
- const result = await this.engine.integrateOps(ops);
297
- this.integrationResults.push(result);
298
- await opts.onIntegrate?.(result);
299
- const rejections = result.rejected.map(
300
- (r) => ({
301
- hash: r.op.hash,
302
- reason: r.reason,
303
- details: r.message
304
- })
305
- );
306
- return { rejections };
307
- },
308
- onNackReceived: async (nack) => {
309
- const info = {
310
- peerId: nack.peerId,
311
- reason: nack.reason,
312
- refs: nack.refs,
313
- details: nack.details
314
- };
315
- this.remoteNacks.push(info);
316
- await opts.onRemoteNack?.(nack);
317
- },
318
- branchPolicy: opts.branchPolicy
319
- });
320
- }
321
- async pushTo(peerId) {
322
- return this.captureSync(peerId, () => this.syncEngine.pushTo(peerId));
323
- }
324
- async pullFrom(peerId) {
325
- return this.captureSync(peerId, () => this.syncEngine.pullAllFrom(peerId));
326
- }
327
- async syncWith(peerId) {
328
- return this.captureSync(peerId, async () => {
329
- await this.syncEngine.pushTo(peerId);
330
- await this.syncEngine.pullAllFrom(peerId);
331
- });
332
- }
333
- listPeers() {
334
- return this.syncEngine.listPeers();
335
- }
336
- getSyncEngine() {
337
- return this.syncEngine;
338
- }
339
- /**
340
- * Cumulative nacks received from remote peers since construction.
341
- * Useful for tests and for consumers that want to inspect the full history.
342
- * Sync results returned by `pushTo`/`pullFrom`/`syncWith` already include
343
- * the per-session slice in `remoteRejected`.
344
- */
345
- getRemoteNacks() {
346
- return this.remoteNacks;
347
- }
348
- async captureSync(peerId, run) {
349
- const beforeOpCount = this.engine.getOpCount();
350
- const start = this.integrationResults.length;
351
- const nackStart = this.remoteNacks.length;
352
- await run();
353
- const batches = this.integrationResults.slice(start);
354
- const remoteRejected = this.remoteNacks.slice(nackStart);
355
- const afterOpCount = this.engine.getOpCount();
356
- return {
357
- peerId,
358
- beforeOpCount,
359
- afterOpCount,
360
- batches,
361
- applied: batches.reduce((sum, batch) => sum + batch.applied, 0),
362
- skipped: batches.reduce((sum, batch) => sum + batch.skipped, 0),
363
- rejected: batches.reduce(
364
- (sum, batch) => sum + batch.rejected.length,
365
- 0
366
- ),
367
- remoteRejected
368
- };
369
- }
370
- };
371
-
372
- // src/sync/partykit-transport.ts
373
- var PartyKitRoomTransport = class {
374
- peerId;
375
- room;
376
- roomUrl;
377
- WebSocketImpl;
378
- ws = null;
379
- messageHandler = null;
380
- constructor(opts) {
381
- this.peerId = opts.peerId;
382
- this.roomUrl = opts.roomUrl;
383
- this.room = {
384
- id: opts.roomId ?? "room",
385
- name: opts.roomName ?? opts.roomId ?? "room"
386
- };
387
- const WebSocketGlobal = globalThis.WebSocket;
388
- const WebSocketImpl = opts.WebSocketImpl ?? WebSocketGlobal;
389
- if (!WebSocketImpl) {
390
- throw new Error(
391
- "PartyKitRoomTransport requires WebSocket or opts.WebSocketImpl."
392
- );
393
- }
394
- this.WebSocketImpl = WebSocketImpl;
395
- }
396
- async connect() {
397
- if (this.ws?.readyState === 1) return;
398
- await new Promise((resolve, reject) => {
399
- const ws = new this.WebSocketImpl(this.roomUrl);
400
- this.ws = ws;
401
- ws.onopen = () => resolve();
402
- ws.onerror = (event) => {
403
- reject(new Error(`PartyKit room connection failed: ${String(event)}`));
404
- };
405
- ws.onmessage = async (event) => {
406
- const message = this.parseMessage(event.data);
407
- if (!message || !this.messageHandler) return;
408
- await this.messageHandler(message);
409
- };
410
- ws.onclose = () => {
411
- if (this.ws === ws) {
412
- this.ws = null;
413
- }
414
- };
415
- });
416
- }
417
- async send(peerId, message) {
418
- if (peerId !== this.room.id) {
419
- throw new Error(`PartyKitRoomTransport can only send to ${this.room.id}.`);
420
- }
421
- await this.connect();
422
- this.ws.send(JSON.stringify(message));
423
- }
424
- onMessage(handler) {
425
- this.messageHandler = handler;
426
- }
427
- peers() {
428
- return [this.room];
429
- }
430
- close() {
431
- this.ws?.close();
432
- this.ws = null;
433
- }
434
- getPeerId() {
435
- return this.peerId;
436
- }
437
- getRoomPeer() {
438
- return { ...this.room };
439
- }
440
- parseMessage(data) {
441
- try {
442
- const raw = typeof data === "string" ? data : String(data);
443
- const parsed = JSON.parse(raw);
444
- return parsed && typeof parsed.type === "string" ? parsed : null;
445
- } catch {
446
- return null;
447
- }
448
- }
449
- };
450
-
451
- export {
452
- PROTOCOL_VERSION,
453
- MIN_SUPPORTED_VERSION,
454
- MAX_SUPPORTED_VERSION,
455
- SyncEngine,
456
- TrellisVcsSyncPeer,
457
- PartyKitRoomTransport
458
- };
@@ -1,223 +0,0 @@
1
- // src/schema/entity-projection.ts
2
- function entityRecordToPlain(entity) {
3
- const obj = { id: entity.id, type: entity.type };
4
- for (const f of entity.facts) {
5
- if (f.a !== "type") obj[f.a] = f.v;
6
- }
7
- return obj;
8
- }
9
- function bindingEntityId(row) {
10
- const id = row.id ?? row.e ?? row["?e"];
11
- return typeof id === "string" && id.length > 0 ? id : null;
12
- }
13
- function isSparseBinding(row) {
14
- const id = bindingEntityId(row);
15
- if (!id) return false;
16
- return typeof row.type !== "string";
17
- }
18
- function bindingToEntity(row) {
19
- const id = bindingEntityId(row);
20
- if (id && typeof row.type === "string") {
21
- return { ...row, id, type: row.type };
22
- }
23
- if (id) return { id, type: String(row.type ?? ""), ...row };
24
- return row;
25
- }
26
- function hydrateBindings(kernel, bindings) {
27
- return bindings.map((row) => {
28
- if (!isSparseBinding(row)) return bindingToEntity(row);
29
- const id = bindingEntityId(row);
30
- const entity = kernel.getEntity(id);
31
- return entity ? entityRecordToPlain(entity) : bindingToEntity(row);
32
- });
33
- }
34
-
35
- // src/schema/eql.ts
36
- function escapeValue(v) {
37
- return v.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
38
- }
39
- var WHERE_OP_TO_EQL = {
40
- eq: "=",
41
- ne: "!=",
42
- lt: "<",
43
- lte: "<=",
44
- gt: ">",
45
- gte: ">=",
46
- contains: "contains",
47
- startsWith: "startsWith",
48
- endsWith: "endsWith"
49
- };
50
- function isWhereFilter(value) {
51
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
52
- return false;
53
- }
54
- return Object.keys(WHERE_OP_TO_EQL).some(
55
- (op) => value[op] !== void 0
56
- );
57
- }
58
- function formatEqlLiteral(value) {
59
- if (typeof value === "string") return `"${escapeValue(value)}"`;
60
- if (typeof value === "boolean") return value ? "true" : "false";
61
- if (typeof value === "number" && Number.isFinite(value)) return String(value);
62
- if (value === null || value === void 0) {
63
- throw new Error("Cannot format null/undefined as an EQL literal");
64
- }
65
- return `"${escapeValue(String(value))}"`;
66
- }
67
- function whereCondition(attr, value) {
68
- if (isWhereFilter(value)) {
69
- const ops = Object.keys(WHERE_OP_TO_EQL).filter(
70
- (op2) => value[op2] !== void 0
71
- );
72
- if (ops.length === 0) {
73
- throw new Error(`Empty where filter for attribute "${attr}"`);
74
- }
75
- if (ops.length > 1) {
76
- throw new Error(
77
- `Where filter for "${attr}" must specify one operator, got: ${ops.join(", ")}`
78
- );
79
- }
80
- const op = ops[0];
81
- return `${attr} ${WHERE_OP_TO_EQL[op]} ${formatEqlLiteral(value[op])}`;
82
- }
83
- return `${attr} = ${formatEqlLiteral(value)}`;
84
- }
85
- function entitiesQuery(type, where) {
86
- const conds = [`type = "${escapeValue(type)}"`];
87
- for (const [k, v] of Object.entries(where ?? {})) {
88
- if (v === void 0 || v === null) continue;
89
- conds.push(whereCondition(k, v));
90
- }
91
- return `find ?e where ${conds.join(" and ")}`;
92
- }
93
-
94
- // src/schema/resolve.ts
95
- function isNestedResolveSpec(v) {
96
- return typeof v === "object" && v !== null;
97
- }
98
- function relationTargetName(r) {
99
- return typeof r.target === "string" ? r.target : r.target().type;
100
- }
101
- function relationTargetSchema(r, schemaLookup) {
102
- if (typeof r.target !== "string") return r.target();
103
- return schemaLookup?.(r.target) ?? null;
104
- }
105
- function inverseForeignKey(parent, relationName, child) {
106
- for (const [key, r] of Object.entries(child.relations)) {
107
- if (relationTargetName(r) === parent.type) return key;
108
- }
109
- return null;
110
- }
111
- async function loadByIds(client, ids, cache) {
112
- const missing = ids.filter((id) => !cache.has(id));
113
- if (missing.length === 0) return;
114
- await Promise.all(
115
- missing.map(async (id) => {
116
- cache.set(id, await client.read(id));
117
- })
118
- );
119
- }
120
- async function resolveReverseMany(client, parent, relationName, parents, cache, schemaLookup) {
121
- const rel = parent.relations[relationName];
122
- if (!rel || rel.cardinality !== "many") return;
123
- const childSchema = relationTargetSchema(rel, schemaLookup);
124
- if (!childSchema) return;
125
- const foreignKey = inverseForeignKey(parent, relationName, childSchema);
126
- if (!foreignKey) return;
127
- const parentIds = new Set(parents.map((p) => p.id));
128
- const qr = await client.query(entitiesQuery(childSchema.type));
129
- const childIds = qr.bindings.map((b) => bindingEntityId(b)).filter((id) => Boolean(id));
130
- await loadByIds(client, childIds, cache);
131
- const grouped = /* @__PURE__ */ new Map();
132
- for (const id of childIds) {
133
- const child = cache.get(id);
134
- if (!child) continue;
135
- const fk = child[foreignKey];
136
- if (typeof fk !== "string" || !parentIds.has(fk)) continue;
137
- if (!grouped.has(fk)) grouped.set(fk, []);
138
- grouped.get(fk).push(child);
139
- }
140
- for (const row of parents) {
141
- row[relationName] = grouped.get(row.id) ?? [];
142
- }
143
- }
144
- async function resolveForwardOne(client, parents, relationName, cache) {
145
- const ids = parents.map((p) => p[relationName]).filter((v) => typeof v === "string" && v.length > 0);
146
- await loadByIds(client, ids, cache);
147
- for (const row of parents) {
148
- const ref = row[relationName];
149
- if (typeof ref !== "string") continue;
150
- const loaded = cache.get(ref);
151
- if (loaded) row[relationName] = loaded;
152
- }
153
- }
154
- async function resolveRelations(client, schema, entities, spec, opts) {
155
- if (entities.length === 0 || Object.keys(spec).length === 0) return entities;
156
- const cache = /* @__PURE__ */ new Map();
157
- const rows = opts?.copy === false ? entities : entities.map((e) => ({ ...e }));
158
- for (const [name, enabled] of Object.entries(spec)) {
159
- if (!enabled) continue;
160
- const rel = schema.relations[name];
161
- if (!rel) continue;
162
- if (rel.cardinality === "many") {
163
- await resolveReverseMany(
164
- client,
165
- schema,
166
- name,
167
- rows,
168
- cache,
169
- opts?.schemaLookup
170
- );
171
- if (isNestedResolveSpec(enabled)) {
172
- const childSchema = relationTargetSchema(rel, opts?.schemaLookup);
173
- if (childSchema) {
174
- for (const row of rows) {
175
- const kids = row[name];
176
- if (kids?.length) {
177
- await resolveRelations(client, childSchema, kids, enabled, {
178
- copy: false,
179
- schemaLookup: opts?.schemaLookup
180
- });
181
- }
182
- }
183
- }
184
- }
185
- } else if (isNestedResolveSpec(enabled)) {
186
- await resolveForwardOne(client, rows, name, cache);
187
- const childSchema = relationTargetSchema(rel, opts?.schemaLookup);
188
- if (childSchema) {
189
- const nested = [];
190
- for (const row of rows) {
191
- const loaded = row[name];
192
- if (loaded && typeof loaded === "object" && "id" in loaded) {
193
- nested.push(loaded);
194
- }
195
- }
196
- if (nested.length) {
197
- await resolveRelations(client, childSchema, nested, enabled, {
198
- copy: false,
199
- schemaLookup: opts?.schemaLookup
200
- });
201
- }
202
- }
203
- } else {
204
- await resolveForwardOne(client, rows, name, cache);
205
- }
206
- }
207
- return rows;
208
- }
209
-
210
- export {
211
- entityRecordToPlain,
212
- bindingEntityId,
213
- isSparseBinding,
214
- bindingToEntity,
215
- hydrateBindings,
216
- escapeValue,
217
- isWhereFilter,
218
- formatEqlLiteral,
219
- whereCondition,
220
- entitiesQuery,
221
- inverseForeignKey,
222
- resolveRelations
223
- };