on-zero 0.7.1 → 0.7.2-1784530701923

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 (297) hide show
  1. package/dist/cjs/helpers/createMutators.cjs +4 -3
  2. package/dist/cjs/helpers/createMutators.native.js +6 -3
  3. package/dist/cjs/helpers/createMutators.native.js.map +1 -1
  4. package/dist/cjs/httpPull/transport.test.cjs +109 -0
  5. package/dist/cjs/httpPull/transport.test.native.js +148 -0
  6. package/dist/cjs/httpPull/transport.test.native.js.map +1 -1
  7. package/dist/cjs/httpPullTransport.cjs +41 -24
  8. package/dist/cjs/httpPullTransport.native.js +47 -26
  9. package/dist/cjs/httpPullTransport.native.js.map +1 -1
  10. package/dist/cjs/index.cjs +0 -1
  11. package/dist/cjs/index.native.js +0 -1
  12. package/dist/cjs/index.native.js.map +1 -1
  13. package/dist/cjs/mutations.cjs +5 -1
  14. package/dist/cjs/mutations.native.js +5 -1
  15. package/dist/cjs/mutations.native.js.map +1 -1
  16. package/dist/cjs/mutations.test.cjs +28 -0
  17. package/dist/cjs/mutations.test.native.js +40 -0
  18. package/dist/cjs/mutations.test.native.js.map +1 -0
  19. package/dist/cjs/orez.cjs +227 -0
  20. package/dist/cjs/orez.native.js +314 -0
  21. package/dist/cjs/orez.native.js.map +1 -0
  22. package/dist/cjs/orez.test.cjs +211 -0
  23. package/dist/cjs/orez.test.native.js +226 -0
  24. package/dist/cjs/orez.test.native.js.map +1 -0
  25. package/dist/cjs/run.cjs +1 -1
  26. package/dist/cjs/run.native.js +1 -1
  27. package/dist/cjs/state.cjs +1 -1
  28. package/dist/cjs/state.native.js +1 -1
  29. package/dist/cjs/zeroRunner.cjs +1 -1
  30. package/dist/cjs/zeroRunner.native.js +1 -1
  31. package/dist/esm/helpers/createMutators.mjs +4 -3
  32. package/dist/esm/helpers/createMutators.mjs.map +1 -1
  33. package/dist/esm/helpers/createMutators.native.js +6 -3
  34. package/dist/esm/helpers/createMutators.native.js.map +1 -1
  35. package/dist/esm/httpPull/transport.test.mjs +109 -0
  36. package/dist/esm/httpPull/transport.test.mjs.map +1 -1
  37. package/dist/esm/httpPull/transport.test.native.js +148 -0
  38. package/dist/esm/httpPull/transport.test.native.js.map +1 -1
  39. package/dist/esm/httpPullTransport.mjs +41 -24
  40. package/dist/esm/httpPullTransport.mjs.map +1 -1
  41. package/dist/esm/httpPullTransport.native.js +47 -26
  42. package/dist/esm/httpPullTransport.native.js.map +1 -1
  43. package/dist/esm/index.js +0 -1
  44. package/dist/esm/index.js.map +1 -1
  45. package/dist/esm/index.mjs +0 -1
  46. package/dist/esm/index.mjs.map +1 -1
  47. package/dist/esm/index.native.js +0 -1
  48. package/dist/esm/index.native.js.map +1 -1
  49. package/dist/esm/mutations.mjs +5 -1
  50. package/dist/esm/mutations.mjs.map +1 -1
  51. package/dist/esm/mutations.native.js +5 -1
  52. package/dist/esm/mutations.native.js.map +1 -1
  53. package/dist/esm/mutations.test.mjs +29 -0
  54. package/dist/esm/mutations.test.mjs.map +1 -0
  55. package/dist/esm/mutations.test.native.js +38 -0
  56. package/dist/esm/mutations.test.native.js.map +1 -0
  57. package/dist/esm/orez.mjs +202 -0
  58. package/dist/esm/orez.mjs.map +1 -0
  59. package/dist/esm/orez.native.js +286 -0
  60. package/dist/esm/orez.native.js.map +1 -0
  61. package/dist/esm/orez.test.mjs +188 -0
  62. package/dist/esm/orez.test.mjs.map +1 -0
  63. package/dist/esm/orez.test.native.js +200 -0
  64. package/dist/esm/orez.test.native.js.map +1 -0
  65. package/dist/esm/run.mjs +1 -1
  66. package/dist/esm/run.mjs.map +1 -1
  67. package/dist/esm/run.native.js +1 -1
  68. package/dist/esm/state.mjs +1 -1
  69. package/dist/esm/state.native.js +1 -1
  70. package/dist/esm/zeroRunner.mjs +1 -1
  71. package/dist/esm/zeroRunner.mjs.map +1 -1
  72. package/dist/esm/zeroRunner.native.js +1 -1
  73. package/package.json +8 -23
  74. package/readme.md +42 -67
  75. package/src/createZeroClient.tsx +1 -1
  76. package/src/helpers/createMutators.ts +6 -2
  77. package/src/httpPull/transport.test.ts +132 -0
  78. package/src/httpPullTransport.ts +74 -42
  79. package/src/index.ts +0 -1
  80. package/src/mutations.test.ts +38 -0
  81. package/src/mutations.ts +10 -2
  82. package/src/orez.test.ts +195 -0
  83. package/src/orez.ts +331 -0
  84. package/src/run.ts +1 -1
  85. package/src/state.ts +1 -1
  86. package/src/types.ts +0 -5
  87. package/src/zeroRunner.ts +1 -1
  88. package/test-fixtures/server-return-types.ts +21 -58
  89. package/types/helpers/createMutators.d.ts +2 -1
  90. package/types/helpers/createMutators.d.ts.map +1 -1
  91. package/types/httpPull/fixtureSchema.d.ts +1 -1
  92. package/types/httpPullTransport.d.ts +14 -1
  93. package/types/httpPullTransport.d.ts.map +1 -1
  94. package/types/index.d.ts +0 -1
  95. package/types/index.d.ts.map +1 -1
  96. package/types/mutations.d.ts.map +1 -1
  97. package/types/mutations.test.d.ts +2 -0
  98. package/types/mutations.test.d.ts.map +1 -0
  99. package/types/orez.d.ts +53 -0
  100. package/types/orez.d.ts.map +1 -0
  101. package/types/orez.test.d.ts +2 -0
  102. package/types/orez.test.d.ts.map +1 -0
  103. package/types/types.d.ts +0 -4
  104. package/types/types.d.ts.map +1 -1
  105. package/dist/cjs/asyncTasks.cjs +0 -51
  106. package/dist/cjs/asyncTasks.native.js +0 -84
  107. package/dist/cjs/asyncTasks.native.js.map +0 -1
  108. package/dist/cjs/createZeroDirectServer.cjs +0 -269
  109. package/dist/cjs/createZeroDirectServer.native.js +0 -287
  110. package/dist/cjs/createZeroDirectServer.native.js.map +0 -1
  111. package/dist/cjs/createZeroSQLiteServer.cjs +0 -50
  112. package/dist/cjs/createZeroSQLiteServer.native.js +0 -58
  113. package/dist/cjs/createZeroSQLiteServer.native.js.map +0 -1
  114. package/dist/cjs/createZeroSQLiteServer.test.cjs +0 -553
  115. package/dist/cjs/createZeroSQLiteServer.test.native.js +0 -602
  116. package/dist/cjs/createZeroSQLiteServer.test.native.js.map +0 -1
  117. package/dist/cjs/createZeroServer.cjs +0 -82
  118. package/dist/cjs/createZeroServer.native.js +0 -93
  119. package/dist/cjs/createZeroServer.native.js.map +0 -1
  120. package/dist/cjs/createZeroServer.test.cjs +0 -378
  121. package/dist/cjs/createZeroServer.test.native.js +0 -448
  122. package/dist/cjs/createZeroServer.test.native.js.map +0 -1
  123. package/dist/cjs/createZeroServerCore.cjs +0 -211
  124. package/dist/cjs/createZeroServerCore.native.js +0 -355
  125. package/dist/cjs/createZeroServerCore.native.js.map +0 -1
  126. package/dist/cjs/httpPull/auth.test.cjs +0 -197
  127. package/dist/cjs/httpPull/auth.test.native.js +0 -279
  128. package/dist/cjs/httpPull/auth.test.native.js.map +0 -1
  129. package/dist/cjs/httpPull/churn.test.cjs +0 -132
  130. package/dist/cjs/httpPull/churn.test.native.js +0 -155
  131. package/dist/cjs/httpPull/churn.test.native.js.map +0 -1
  132. package/dist/cjs/httpPull/completeAck.test.cjs +0 -69
  133. package/dist/cjs/httpPull/completeAck.test.native.js +0 -76
  134. package/dist/cjs/httpPull/completeAck.test.native.js.map +0 -1
  135. package/dist/cjs/httpPull/fixtureServer.cjs +0 -340
  136. package/dist/cjs/httpPull/fixtureServer.native.js +0 -534
  137. package/dist/cjs/httpPull/fixtureServer.native.js.map +0 -1
  138. package/dist/cjs/httpPull/integration.test.cjs +0 -53
  139. package/dist/cjs/httpPull/integration.test.native.js +0 -60
  140. package/dist/cjs/httpPull/integration.test.native.js.map +0 -1
  141. package/dist/cjs/httpPull/rebase.test.cjs +0 -360
  142. package/dist/cjs/httpPull/rebase.test.native.js +0 -420
  143. package/dist/cjs/httpPull/rebase.test.native.js.map +0 -1
  144. package/dist/cjs/httpPull/relations.test.cjs +0 -107
  145. package/dist/cjs/httpPull/relations.test.native.js +0 -119
  146. package/dist/cjs/httpPull/relations.test.native.js.map +0 -1
  147. package/dist/cjs/httpPull/testHarness.cjs +0 -100
  148. package/dist/cjs/httpPull/testHarness.native.js +0 -112
  149. package/dist/cjs/httpPull/testHarness.native.js.map +0 -1
  150. package/dist/cjs/server.cjs +0 -22
  151. package/dist/cjs/server.native.js +0 -4
  152. package/dist/cjs/server.native.js.map +0 -1
  153. package/dist/cjs/sqliteRuntime.cjs +0 -318
  154. package/dist/cjs/sqliteRuntime.native.js +0 -438
  155. package/dist/cjs/sqliteRuntime.native.js.map +0 -1
  156. package/dist/cjs/syncHost.cjs +0 -179
  157. package/dist/cjs/syncHost.native.js +0 -388
  158. package/dist/cjs/syncHost.native.js.map +0 -1
  159. package/dist/cjs/syncHost.test.cjs +0 -240
  160. package/dist/cjs/syncHost.test.native.js +0 -290
  161. package/dist/cjs/syncHost.test.native.js.map +0 -1
  162. package/dist/esm/asyncTasks.mjs +0 -26
  163. package/dist/esm/asyncTasks.mjs.map +0 -1
  164. package/dist/esm/asyncTasks.native.js +0 -56
  165. package/dist/esm/asyncTasks.native.js.map +0 -1
  166. package/dist/esm/createZeroDirectServer.mjs +0 -241
  167. package/dist/esm/createZeroDirectServer.mjs.map +0 -1
  168. package/dist/esm/createZeroDirectServer.native.js +0 -256
  169. package/dist/esm/createZeroDirectServer.native.js.map +0 -1
  170. package/dist/esm/createZeroSQLiteServer.mjs +0 -24
  171. package/dist/esm/createZeroSQLiteServer.mjs.map +0 -1
  172. package/dist/esm/createZeroSQLiteServer.native.js +0 -29
  173. package/dist/esm/createZeroSQLiteServer.native.js.map +0 -1
  174. package/dist/esm/createZeroSQLiteServer.test.mjs +0 -554
  175. package/dist/esm/createZeroSQLiteServer.test.mjs.map +0 -1
  176. package/dist/esm/createZeroSQLiteServer.test.native.js +0 -600
  177. package/dist/esm/createZeroSQLiteServer.test.native.js.map +0 -1
  178. package/dist/esm/createZeroServer.mjs +0 -57
  179. package/dist/esm/createZeroServer.mjs.map +0 -1
  180. package/dist/esm/createZeroServer.native.js +0 -65
  181. package/dist/esm/createZeroServer.native.js.map +0 -1
  182. package/dist/esm/createZeroServer.test.mjs +0 -379
  183. package/dist/esm/createZeroServer.test.mjs.map +0 -1
  184. package/dist/esm/createZeroServer.test.native.js +0 -446
  185. package/dist/esm/createZeroServer.test.native.js.map +0 -1
  186. package/dist/esm/createZeroServerCore.mjs +0 -186
  187. package/dist/esm/createZeroServerCore.mjs.map +0 -1
  188. package/dist/esm/createZeroServerCore.native.js +0 -327
  189. package/dist/esm/createZeroServerCore.native.js.map +0 -1
  190. package/dist/esm/httpPull/auth.test.mjs +0 -198
  191. package/dist/esm/httpPull/auth.test.mjs.map +0 -1
  192. package/dist/esm/httpPull/auth.test.native.js +0 -277
  193. package/dist/esm/httpPull/auth.test.native.js.map +0 -1
  194. package/dist/esm/httpPull/churn.test.mjs +0 -133
  195. package/dist/esm/httpPull/churn.test.mjs.map +0 -1
  196. package/dist/esm/httpPull/churn.test.native.js +0 -153
  197. package/dist/esm/httpPull/churn.test.native.js.map +0 -1
  198. package/dist/esm/httpPull/completeAck.test.mjs +0 -70
  199. package/dist/esm/httpPull/completeAck.test.mjs.map +0 -1
  200. package/dist/esm/httpPull/completeAck.test.native.js +0 -74
  201. package/dist/esm/httpPull/completeAck.test.native.js.map +0 -1
  202. package/dist/esm/httpPull/fixtureServer.mjs +0 -315
  203. package/dist/esm/httpPull/fixtureServer.mjs.map +0 -1
  204. package/dist/esm/httpPull/fixtureServer.native.js +0 -506
  205. package/dist/esm/httpPull/fixtureServer.native.js.map +0 -1
  206. package/dist/esm/httpPull/integration.test.mjs +0 -54
  207. package/dist/esm/httpPull/integration.test.mjs.map +0 -1
  208. package/dist/esm/httpPull/integration.test.native.js +0 -58
  209. package/dist/esm/httpPull/integration.test.native.js.map +0 -1
  210. package/dist/esm/httpPull/rebase.test.mjs +0 -361
  211. package/dist/esm/httpPull/rebase.test.mjs.map +0 -1
  212. package/dist/esm/httpPull/rebase.test.native.js +0 -418
  213. package/dist/esm/httpPull/rebase.test.native.js.map +0 -1
  214. package/dist/esm/httpPull/relations.test.mjs +0 -108
  215. package/dist/esm/httpPull/relations.test.mjs.map +0 -1
  216. package/dist/esm/httpPull/relations.test.native.js +0 -117
  217. package/dist/esm/httpPull/relations.test.native.js.map +0 -1
  218. package/dist/esm/httpPull/testHarness.mjs +0 -72
  219. package/dist/esm/httpPull/testHarness.mjs.map +0 -1
  220. package/dist/esm/httpPull/testHarness.native.js +0 -81
  221. package/dist/esm/httpPull/testHarness.native.js.map +0 -1
  222. package/dist/esm/server.mjs +0 -4
  223. package/dist/esm/server.mjs.map +0 -1
  224. package/dist/esm/server.native.js +0 -2
  225. package/dist/esm/server.native.js.map +0 -1
  226. package/dist/esm/sqliteRuntime.mjs +0 -289
  227. package/dist/esm/sqliteRuntime.mjs.map +0 -1
  228. package/dist/esm/sqliteRuntime.native.js +0 -406
  229. package/dist/esm/sqliteRuntime.native.js.map +0 -1
  230. package/dist/esm/syncHost.mjs +0 -153
  231. package/dist/esm/syncHost.mjs.map +0 -1
  232. package/dist/esm/syncHost.native.js +0 -359
  233. package/dist/esm/syncHost.native.js.map +0 -1
  234. package/dist/esm/syncHost.test.mjs +0 -217
  235. package/dist/esm/syncHost.test.mjs.map +0 -1
  236. package/dist/esm/syncHost.test.native.js +0 -264
  237. package/dist/esm/syncHost.test.native.js.map +0 -1
  238. package/src/asyncTasks.ts +0 -36
  239. package/src/createZeroDirectServer.ts +0 -411
  240. package/src/createZeroSQLiteServer.test.ts +0 -571
  241. package/src/createZeroSQLiteServer.ts +0 -56
  242. package/src/createZeroServer.test.ts +0 -421
  243. package/src/createZeroServer.ts +0 -105
  244. package/src/createZeroServerCore.ts +0 -296
  245. package/src/httpPull/auth.test.ts +0 -208
  246. package/src/httpPull/churn.test.ts +0 -147
  247. package/src/httpPull/completeAck.test.ts +0 -80
  248. package/src/httpPull/fixtureServer.ts +0 -391
  249. package/src/httpPull/integration.test.ts +0 -57
  250. package/src/httpPull/rebase.test.ts +0 -368
  251. package/src/httpPull/relations.test.ts +0 -135
  252. package/src/httpPull/testHarness.ts +0 -95
  253. package/src/server.native.ts +0 -3
  254. package/src/server.ts +0 -4
  255. package/src/sqliteRuntime.ts +0 -512
  256. package/src/syncHost.test.ts +0 -223
  257. package/src/syncHost.ts +0 -294
  258. package/types/asyncTasks.d.ts +0 -11
  259. package/types/asyncTasks.d.ts.map +0 -1
  260. package/types/createZeroDirectServer.d.ts +0 -80
  261. package/types/createZeroDirectServer.d.ts.map +0 -1
  262. package/types/createZeroSQLiteServer.d.ts +0 -12
  263. package/types/createZeroSQLiteServer.d.ts.map +0 -1
  264. package/types/createZeroSQLiteServer.test.d.ts +0 -2
  265. package/types/createZeroSQLiteServer.test.d.ts.map +0 -1
  266. package/types/createZeroServer.d.ts +0 -18
  267. package/types/createZeroServer.d.ts.map +0 -1
  268. package/types/createZeroServer.test.d.ts +0 -2
  269. package/types/createZeroServer.test.d.ts.map +0 -1
  270. package/types/createZeroServerCore.d.ts +0 -256
  271. package/types/createZeroServerCore.d.ts.map +0 -1
  272. package/types/httpPull/auth.test.d.ts +0 -2
  273. package/types/httpPull/auth.test.d.ts.map +0 -1
  274. package/types/httpPull/churn.test.d.ts +0 -2
  275. package/types/httpPull/churn.test.d.ts.map +0 -1
  276. package/types/httpPull/completeAck.test.d.ts +0 -2
  277. package/types/httpPull/completeAck.test.d.ts.map +0 -1
  278. package/types/httpPull/fixtureServer.d.ts +0 -14
  279. package/types/httpPull/fixtureServer.d.ts.map +0 -1
  280. package/types/httpPull/integration.test.d.ts +0 -2
  281. package/types/httpPull/integration.test.d.ts.map +0 -1
  282. package/types/httpPull/rebase.test.d.ts +0 -2
  283. package/types/httpPull/rebase.test.d.ts.map +0 -1
  284. package/types/httpPull/relations.test.d.ts +0 -2
  285. package/types/httpPull/relations.test.d.ts.map +0 -1
  286. package/types/httpPull/testHarness.d.ts +0 -32
  287. package/types/httpPull/testHarness.d.ts.map +0 -1
  288. package/types/server.d.ts +0 -5
  289. package/types/server.d.ts.map +0 -1
  290. package/types/server.native.d.ts +0 -1
  291. package/types/server.native.d.ts.map +0 -1
  292. package/types/sqliteRuntime.d.ts +0 -71
  293. package/types/sqliteRuntime.d.ts.map +0 -1
  294. package/types/syncHost.d.ts +0 -42
  295. package/types/syncHost.d.ts.map +0 -1
  296. package/types/syncHost.test.d.ts +0 -2
  297. package/types/syncHost.test.d.ts.map +0 -1
@@ -1,80 +0,0 @@
1
- import { afterEach, expect, test } from 'vitest'
2
-
3
- import {
4
- eventually,
5
- startZeroHttpHarness,
6
- waitForComplete,
7
- type ZeroHttpHarness,
8
- } from './testHarness'
9
-
10
- // repro for the soot `sb read-state` hang (2026-07-10): named-query `complete`
11
- // never resolves for queries that were already desired when the socket
12
- // (re)connects, while queries desired after connect ack fine. these tests pin
13
- // the got-ack contract across the transport's reconnect path.
14
-
15
- let harness: ZeroHttpHarness | undefined
16
-
17
- afterEach(async () => {
18
- await harness?.close()
19
- harness = undefined
20
- })
21
-
22
- test('query desired before first connect reaches complete', async () => {
23
- harness = await startZeroHttpHarness({
24
- seed: { user: [{ id: 'u1', name: 'ada' }] },
25
- })
26
- const zero = harness.createZero('u1')
27
- // materialize synchronously after construction so the desired query rides
28
- // the initial connection handshake, not a later changeDesiredQueries.
29
- const view = zero.query.user.materialize()
30
- const rows = await waitForComplete<any[]>(view)
31
- expect(rows).toEqual([{ id: 'u1', name: 'ada' }])
32
- view.destroy()
33
- })
34
-
35
- test('existing + new queries reach complete again after a transport drop', async () => {
36
- let failNextPull = false
37
- harness = await startZeroHttpHarness({
38
- seed: {
39
- user: [{ id: 'u1', name: 'ada' }],
40
- project: [{ id: 'p1', ownerId: 'u1', name: 'first' }],
41
- },
42
- interceptFetch: (next) => (input, init) => {
43
- const url = String(input)
44
- if (failNextPull && url.endsWith('/pull')) {
45
- failNextPull = false
46
- return Promise.reject(new Error('injected transient pull failure'))
47
- }
48
- return next(input, init)
49
- },
50
- })
51
- const zero = harness.createZero('u1')
52
-
53
- const userView = zero.query.user.materialize()
54
- await waitForComplete<any[]>(userView)
55
-
56
- // drop the socket: a failed pull closes it (1011) and zero reconnects with
57
- // every currently-desired query in the connection handshake.
58
- failNextPull = true
59
- await harness.transport.pull().catch(() => {})
60
-
61
- // wait until sync is live again (a write round-trips).
62
- const mutation = zero.mutate.project.create({ id: 'p2', ownerId: 'u1', name: 'second' })
63
- await mutation.client
64
- await mutation.server.catch(() => {})
65
-
66
- // the already-desired query must be able to reach complete again on the new
67
- // socket (this is what sb read-state does: re-run an already-subscribed
68
- // named query with {type:'complete'}).
69
- const userAgain = zero.query.user.materialize()
70
- await waitForComplete<any[]>(userAgain)
71
-
72
- // and a query desired only after the reconnect must too.
73
- const projectView = zero.query.project.materialize()
74
- const projects = await waitForComplete<any[]>(projectView)
75
- expect(projects.map((p: any) => p.id).sort()).toEqual(['p1', 'p2'])
76
-
77
- userView.destroy()
78
- userAgain.destroy()
79
- projectView.destroy()
80
- })
@@ -1,391 +0,0 @@
1
- import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'
2
-
3
- import type { AddressInfo } from 'node:net'
4
-
5
- export type Row = Record<string, string>
6
-
7
- type TableName = 'user' | 'project' | 'member'
8
- type Tables = Record<TableName, Map<string, Row>>
9
- type ClientMutationResults = Map<string, Map<string, Map<number, MutationResult>>>
10
-
11
- interface PullBody {
12
- clientID: string
13
- clientGroupID: string
14
- cookie: number | null
15
- }
16
-
17
- interface PushMutation {
18
- type: string
19
- name: string
20
- clientID: string
21
- id: number
22
- args: Row[]
23
- }
24
-
25
- interface PushBody {
26
- clientGroupID: string
27
- mutations: PushMutation[]
28
- }
29
-
30
- type MutationResult = Record<string, never> | { error: 'app'; details: string }
31
-
32
- const tableNames: TableName[] = ['user', 'project', 'member']
33
-
34
- export async function startZeroHttpServer(opts?: {
35
- seed?: { user?: Row[]; project?: Row[]; member?: Row[] }
36
- }): Promise<{
37
- url: string
38
- version(): number
39
- rows(table: string): Row[]
40
- close(): Promise<void>
41
- }> {
42
- const tables = seedTables(opts?.seed)
43
- const lmids = new Map<string, Map<string, number>>()
44
- const mutationResults: ClientMutationResults = new Map()
45
- const clientGroupUsers = new Map<string, string>()
46
- let cookie = 1
47
-
48
- const server = createServer(async (req, res) => {
49
- try {
50
- if (req.method !== 'POST') {
51
- sendJSON(res, 404, { error: 'not found' })
52
- return
53
- }
54
-
55
- const path = new URL(req.url || '/', 'http://127.0.0.1').pathname
56
- const userID = authenticate(req, tables)
57
- if (!userID) {
58
- sendJSON(res, 401, { error: 'unauthorized' })
59
- return
60
- }
61
-
62
- if (path === '/pull') {
63
- const body = (await readJSON(req)) as PullBody
64
- if (!bindClientGroup(clientGroupUsers, body.clientGroupID, userID)) {
65
- sendJSON(res, 403, { error: 'client group belongs to a different user' })
66
- return
67
- }
68
- if (body.cookie === cookie) {
69
- sendJSON(res, 200, { cookie, unchanged: true })
70
- return
71
- }
72
- if (typeof body.cookie === 'number' && body.cookie > cookie) {
73
- sendJSON(res, 409, {
74
- error: `future cookie ${body.cookie} is ahead of server cookie ${cookie}`,
75
- })
76
- return
77
- }
78
-
79
- sendJSON(res, 200, {
80
- cookie,
81
- lastMutationIDChanges: lastMutationIDChanges(lmids, body.clientGroupID),
82
- rowsPatch: [{ op: 'clear' }, ...visibleRowsPatch(tables, userID)],
83
- })
84
- return
85
- }
86
-
87
- if (path === '/push') {
88
- const body = (await readJSON(req)) as PushBody
89
- if (!bindClientGroup(clientGroupUsers, body.clientGroupID, userID)) {
90
- sendJSON(res, 403, { error: 'client group belongs to a different user' })
91
- return
92
- }
93
- const mutations = Array.isArray(body.mutations) ? body.mutations : []
94
- const gap = findMutationGap(lmids, body.clientGroupID, mutations)
95
- if (gap) {
96
- sendJSON(res, 500, { error: gap })
97
- return
98
- }
99
-
100
- const pushResults: Array<{
101
- id: { clientID: string; id: number }
102
- result: MutationResult
103
- }> = []
104
- let processedNewMutation = false
105
-
106
- for (const mutation of mutations) {
107
- const current = lmidFor(lmids, body.clientGroupID, mutation.clientID)
108
- if (mutation.id <= current) {
109
- pushResults.push({
110
- id: { clientID: mutation.clientID, id: mutation.id },
111
- result:
112
- resultForMutation(
113
- mutationResults,
114
- body.clientGroupID,
115
- mutation.clientID,
116
- mutation.id,
117
- ) || {},
118
- })
119
- continue
120
- }
121
-
122
- const result = applyMutation(tables, userID, mutation)
123
- setLMID(lmids, body.clientGroupID, mutation.clientID, mutation.id)
124
- setMutationResult(
125
- mutationResults,
126
- body.clientGroupID,
127
- mutation.clientID,
128
- mutation.id,
129
- result,
130
- )
131
- processedNewMutation = true
132
- pushResults.push({
133
- id: { clientID: mutation.clientID, id: mutation.id },
134
- result,
135
- })
136
- }
137
-
138
- if (processedNewMutation) cookie += 1
139
- sendJSON(res, 200, { pushResponse: { mutations: pushResults } })
140
- return
141
- }
142
-
143
- sendJSON(res, 404, { error: 'not found' })
144
- } catch (err) {
145
- sendJSON(res, 500, { error: err instanceof Error ? err.message : String(err) })
146
- }
147
- })
148
-
149
- await new Promise<void>((resolve, reject) => {
150
- server.once('error', reject)
151
- server.listen(0, '127.0.0.1', () => {
152
- server.off('error', reject)
153
- resolve()
154
- })
155
- })
156
-
157
- const address = server.address() as AddressInfo
158
- return {
159
- url: `http://127.0.0.1:${address.port}`,
160
- version: () => cookie,
161
- rows: (table) => rowsForTable(tables, table),
162
- close: () =>
163
- new Promise((resolve, reject) => {
164
- server.close((err) => (err ? reject(err) : resolve()))
165
- }),
166
- }
167
- }
168
-
169
- function seedTables(seed?: { user?: Row[]; project?: Row[]; member?: Row[] }) {
170
- const tables: Tables = {
171
- user: new Map(),
172
- project: new Map(),
173
- member: new Map(),
174
- }
175
- for (const table of tableNames) {
176
- for (const row of seed?.[table] || []) {
177
- if (typeof row.id === 'string') tables[table].set(row.id, cloneRow(row))
178
- }
179
- }
180
- return tables
181
- }
182
-
183
- function authenticate(req: IncomingMessage, tables: Tables) {
184
- const header = req.headers.authorization
185
- if (!header?.startsWith('Bearer token-')) return null
186
- const userID = header.slice('Bearer token-'.length)
187
- return tables.user.has(userID) ? userID : null
188
- }
189
-
190
- function bindClientGroup(
191
- clientGroupUsers: Map<string, string>,
192
- clientGroupID: string,
193
- userID: string,
194
- ) {
195
- const owner = clientGroupUsers.get(clientGroupID)
196
- if (owner) return owner === userID
197
- clientGroupUsers.set(clientGroupID, userID)
198
- return true
199
- }
200
-
201
- function visibleRowsPatch(tables: Tables, userID: string) {
202
- const visibleProjectIDs = visibleProjectIDSet(tables, userID)
203
- const rows: Array<{ op: 'put'; tableName: TableName; value: Row }> = []
204
- const user = tables.user.get(userID)
205
- if (user) rows.push({ op: 'put', tableName: 'user', value: cloneRow(user) })
206
-
207
- for (const project of tables.project.values()) {
208
- if (visibleProjectIDs.has(project.id)) {
209
- rows.push({ op: 'put', tableName: 'project', value: cloneRow(project) })
210
- }
211
- }
212
-
213
- for (const member of tables.member.values()) {
214
- if (visibleProjectIDs.has(member.projectId)) {
215
- rows.push({ op: 'put', tableName: 'member', value: cloneRow(member) })
216
- }
217
- }
218
- return rows
219
- }
220
-
221
- function visibleProjectIDSet(tables: Tables, userID: string) {
222
- const projectIDs = new Set<string>()
223
- for (const project of tables.project.values()) {
224
- if (project.ownerId === userID) projectIDs.add(project.id)
225
- }
226
- for (const member of tables.member.values()) {
227
- if (member.userId === userID && tables.project.has(member.projectId)) {
228
- projectIDs.add(member.projectId)
229
- }
230
- }
231
- return projectIDs
232
- }
233
-
234
- function findMutationGap(
235
- lmids: Map<string, Map<string, number>>,
236
- clientGroupID: string,
237
- mutations: PushMutation[],
238
- ) {
239
- const nextLMIDs = new Map<string, number>()
240
- for (const mutation of mutations) {
241
- const current =
242
- nextLMIDs.get(mutation.clientID) ?? lmidFor(lmids, clientGroupID, mutation.clientID)
243
- if (mutation.id <= current) continue
244
- if (mutation.id !== current + 1) {
245
- return `mutation id gap for ${mutation.clientID}: got ${mutation.id}, expected ${
246
- current + 1
247
- }`
248
- }
249
- nextLMIDs.set(mutation.clientID, mutation.id)
250
- }
251
- return null
252
- }
253
-
254
- function applyMutation(
255
- tables: Tables,
256
- userID: string,
257
- mutation: PushMutation,
258
- ): MutationResult {
259
- if (mutation.type !== 'custom') return appError('unsupported')
260
- const args = mutation.args[0] || {}
261
-
262
- if (mutation.name === 'project|create') {
263
- if (tables.project.has(args.id)) return appError('exists')
264
- if (args.ownerId !== userID) return appError('forbidden')
265
- tables.project.set(args.id, {
266
- id: args.id,
267
- ownerId: args.ownerId,
268
- name: args.name,
269
- })
270
- return {}
271
- }
272
-
273
- if (mutation.name === 'project|rename') {
274
- const project = tables.project.get(args.id)
275
- if (!project) return appError('not-found')
276
- if (project.ownerId !== userID) return appError('forbidden')
277
- tables.project.set(args.id, { ...project, name: args.name })
278
- return {}
279
- }
280
-
281
- if (mutation.name === 'member|add') {
282
- const project = tables.project.get(args.projectId)
283
- if (!project) return appError('not-found')
284
- if (project.ownerId !== userID) return appError('forbidden')
285
- if (tables.member.has(args.id)) return appError('exists')
286
- tables.member.set(args.id, {
287
- id: args.id,
288
- projectId: args.projectId,
289
- userId: args.userId,
290
- })
291
- return {}
292
- }
293
-
294
- if (mutation.name === 'member|remove') {
295
- const member = tables.member.get(args.id)
296
- if (!member) return appError('not-found')
297
- const project = tables.project.get(member.projectId)
298
- if (!project) return appError('not-found')
299
- if (project.ownerId !== userID) return appError('forbidden')
300
- tables.member.delete(args.id)
301
- return {}
302
- }
303
-
304
- return appError('unsupported')
305
- }
306
-
307
- function appError(details: string): MutationResult {
308
- return { error: 'app', details }
309
- }
310
-
311
- function lastMutationIDChanges(
312
- lmids: Map<string, Map<string, number>>,
313
- clientGroupID: string,
314
- ) {
315
- return Object.fromEntries(lmids.get(clientGroupID) || [])
316
- }
317
-
318
- function lmidFor(
319
- lmids: Map<string, Map<string, number>>,
320
- clientGroupID: string,
321
- clientID: string,
322
- ) {
323
- return lmids.get(clientGroupID)?.get(clientID) || 0
324
- }
325
-
326
- function setLMID(
327
- lmids: Map<string, Map<string, number>>,
328
- clientGroupID: string,
329
- clientID: string,
330
- id: number,
331
- ) {
332
- let group = lmids.get(clientGroupID)
333
- if (!group) {
334
- group = new Map()
335
- lmids.set(clientGroupID, group)
336
- }
337
- group.set(clientID, id)
338
- }
339
-
340
- function resultForMutation(
341
- results: ClientMutationResults,
342
- clientGroupID: string,
343
- clientID: string,
344
- id: number,
345
- ) {
346
- return results.get(clientGroupID)?.get(clientID)?.get(id)
347
- }
348
-
349
- function setMutationResult(
350
- results: ClientMutationResults,
351
- clientGroupID: string,
352
- clientID: string,
353
- id: number,
354
- result: MutationResult,
355
- ) {
356
- let group = results.get(clientGroupID)
357
- if (!group) {
358
- group = new Map()
359
- results.set(clientGroupID, group)
360
- }
361
- let client = group.get(clientID)
362
- if (!client) {
363
- client = new Map()
364
- group.set(clientID, client)
365
- }
366
- client.set(id, result)
367
- }
368
-
369
- function rowsForTable(tables: Tables, table: string): Row[] {
370
- if (!isTableName(table)) return []
371
- return [...tables[table].values()].map(cloneRow)
372
- }
373
-
374
- function isTableName(table: string): table is TableName {
375
- return (tableNames as string[]).includes(table)
376
- }
377
-
378
- function cloneRow(row: Row): Row {
379
- return { ...row }
380
- }
381
-
382
- async function readJSON(req: IncomingMessage): Promise<unknown> {
383
- let body = ''
384
- for await (const chunk of req) body += chunk
385
- return body ? JSON.parse(body) : {}
386
- }
387
-
388
- function sendJSON(res: ServerResponse, status: number, body: unknown) {
389
- res.writeHead(status, { 'content-type': 'application/json' })
390
- res.end(JSON.stringify(body))
391
- }
@@ -1,57 +0,0 @@
1
- import { afterEach, expect, test } from 'vitest'
2
-
3
- import {
4
- eventually,
5
- startZeroHttpHarness,
6
- waitForComplete,
7
- type ZeroHttpHarness,
8
- } from './testHarness'
9
-
10
- let harness: ZeroHttpHarness | undefined
11
-
12
- afterEach(async () => {
13
- await harness?.close()
14
- harness = undefined
15
- })
16
-
17
- test('e2e smoke: stock zero client syncs over http against the fixture server', async () => {
18
- harness = await startZeroHttpHarness({
19
- seed: {
20
- user: [{ id: 'u1', name: 'ada' }],
21
- project: [{ id: 'p1', ownerId: 'u1', name: 'first' }],
22
- member: [{ id: 'm1', projectId: 'p1', userId: 'u1' }],
23
- },
24
- })
25
- const zero = harness.createZero('u1')
26
-
27
- const view = zero.query.project.related('members').materialize()
28
- const initial = await waitForComplete<any[]>(view)
29
- expect(initial).toEqual([
30
- {
31
- id: 'p1',
32
- ownerId: 'u1',
33
- name: 'first',
34
- members: [{ id: 'm1', projectId: 'p1', userId: 'u1' }],
35
- },
36
- ])
37
-
38
- const mutation = zero.mutate.project.create({
39
- id: 'p2',
40
- ownerId: 'u1',
41
- name: 'second',
42
- })
43
- await mutation.client
44
- await mutation.server
45
-
46
- await eventually(() => {
47
- const names = view.data.map((project: any) => project.name).sort()
48
- expect(names).toEqual(['first', 'second'])
49
- })
50
- expect(
51
- harness.server
52
- .rows('project')
53
- .map((row) => row.id)
54
- .sort(),
55
- ).toEqual(['p1', 'p2'])
56
- view.destroy()
57
- })