@rocicorp/zero 0.26.1 → 0.26.2-canary.4

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 (1086) hide show
  1. package/out/_virtual/_@oxc-project_runtime@0.115.0/helpers/usingCtx.js +57 -0
  2. package/out/_virtual/_rolldown/runtime.js +27 -0
  3. package/out/analyze-query/src/bin-analyze.js +195 -283
  4. package/out/analyze-query/src/bin-analyze.js.map +1 -1
  5. package/out/analyze-query/src/bin-transform.js +35 -40
  6. package/out/analyze-query/src/bin-transform.js.map +1 -1
  7. package/out/analyze-query/src/explain-queries.js +11 -13
  8. package/out/analyze-query/src/explain-queries.js.map +1 -1
  9. package/out/analyze-query/src/run-ast.js +68 -103
  10. package/out/analyze-query/src/run-ast.js.map +1 -1
  11. package/out/ast-to-zql/src/ast-to-zql.js +105 -153
  12. package/out/ast-to-zql/src/ast-to-zql.js.map +1 -1
  13. package/out/ast-to-zql/src/bin.js +57 -62
  14. package/out/ast-to-zql/src/bin.js.map +1 -1
  15. package/out/ast-to-zql/src/format.js +14 -13
  16. package/out/ast-to-zql/src/format.js.map +1 -1
  17. package/out/datadog/src/datadog-log-sink.js +148 -213
  18. package/out/datadog/src/datadog-log-sink.js.map +1 -1
  19. package/out/otel/src/enabled.js +9 -11
  20. package/out/otel/src/enabled.js.map +1 -1
  21. package/out/otel/src/log-options.js +25 -35
  22. package/out/otel/src/log-options.js.map +1 -1
  23. package/out/otel/src/maybe-time.js +13 -14
  24. package/out/otel/src/maybe-time.js.map +1 -1
  25. package/out/otel/src/span.js +23 -26
  26. package/out/otel/src/span.js.map +1 -1
  27. package/out/otel/src/test-log-config.js +11 -10
  28. package/out/otel/src/test-log-config.js.map +1 -1
  29. package/out/otel/src/version.js +6 -5
  30. package/out/otel/src/version.js.map +1 -1
  31. package/out/replicache/src/async-iterable-to-array.js +8 -9
  32. package/out/replicache/src/async-iterable-to-array.js.map +1 -1
  33. package/out/replicache/src/bg-interval.js +28 -35
  34. package/out/replicache/src/bg-interval.js.map +1 -1
  35. package/out/replicache/src/btree/diff.js +6 -5
  36. package/out/replicache/src/btree/diff.js.map +1 -1
  37. package/out/replicache/src/btree/node.js +281 -372
  38. package/out/replicache/src/btree/node.js.map +1 -1
  39. package/out/replicache/src/btree/read.js +155 -256
  40. package/out/replicache/src/btree/read.js.map +1 -1
  41. package/out/replicache/src/btree/splice.js +60 -80
  42. package/out/replicache/src/btree/splice.js.map +1 -1
  43. package/out/replicache/src/btree/write.js +134 -158
  44. package/out/replicache/src/btree/write.js.map +1 -1
  45. package/out/replicache/src/call-default-fetch.js +28 -32
  46. package/out/replicache/src/call-default-fetch.js.map +1 -1
  47. package/out/replicache/src/config.js +2 -0
  48. package/out/replicache/src/connection-loop-delegates.js +31 -33
  49. package/out/replicache/src/connection-loop-delegates.js.map +1 -1
  50. package/out/replicache/src/connection-loop.js +174 -240
  51. package/out/replicache/src/connection-loop.js.map +1 -1
  52. package/out/replicache/src/cookies.js +22 -32
  53. package/out/replicache/src/cookies.js.map +1 -1
  54. package/out/replicache/src/dag/chunk.js +44 -50
  55. package/out/replicache/src/dag/chunk.js.map +1 -1
  56. package/out/replicache/src/dag/gc.js +94 -114
  57. package/out/replicache/src/dag/gc.js.map +1 -1
  58. package/out/replicache/src/dag/key.js +9 -11
  59. package/out/replicache/src/dag/key.js.map +1 -1
  60. package/out/replicache/src/dag/lazy-store.js +458 -510
  61. package/out/replicache/src/dag/lazy-store.js.map +1 -1
  62. package/out/replicache/src/dag/store-impl.js +147 -178
  63. package/out/replicache/src/dag/store-impl.js.map +1 -1
  64. package/out/replicache/src/dag/store.js +19 -22
  65. package/out/replicache/src/dag/store.js.map +1 -1
  66. package/out/replicache/src/dag/visitor.js +23 -21
  67. package/out/replicache/src/dag/visitor.js.map +1 -1
  68. package/out/replicache/src/db/commit.js +209 -283
  69. package/out/replicache/src/db/commit.js.map +1 -1
  70. package/out/replicache/src/db/index.js +79 -122
  71. package/out/replicache/src/db/index.js.map +1 -1
  72. package/out/replicache/src/db/read.js +44 -60
  73. package/out/replicache/src/db/read.js.map +1 -1
  74. package/out/replicache/src/db/rebase.js +22 -77
  75. package/out/replicache/src/db/rebase.js.map +1 -1
  76. package/out/replicache/src/db/write.js +162 -296
  77. package/out/replicache/src/db/write.js.map +1 -1
  78. package/out/replicache/src/deleted-clients.js +59 -87
  79. package/out/replicache/src/deleted-clients.js.map +1 -1
  80. package/out/replicache/src/error-responses.js +18 -26
  81. package/out/replicache/src/error-responses.js.map +1 -1
  82. package/out/replicache/src/expo-sqlite.js +2 -0
  83. package/out/replicache/src/frozen-json.js +74 -108
  84. package/out/replicache/src/frozen-json.js.map +1 -1
  85. package/out/replicache/src/get-default-puller.js +34 -46
  86. package/out/replicache/src/get-default-puller.js.map +1 -1
  87. package/out/replicache/src/get-default-pusher.js +25 -33
  88. package/out/replicache/src/get-default-pusher.js.map +1 -1
  89. package/out/replicache/src/get-kv-store-provider.js +18 -20
  90. package/out/replicache/src/get-kv-store-provider.js.map +1 -1
  91. package/out/replicache/src/hash.js +29 -29
  92. package/out/replicache/src/hash.js.map +1 -1
  93. package/out/replicache/src/http-request-info.js +9 -8
  94. package/out/replicache/src/http-request-info.js.map +1 -1
  95. package/out/replicache/src/impl.js +2 -0
  96. package/out/replicache/src/index-defs.js +17 -28
  97. package/out/replicache/src/index-defs.js.map +1 -1
  98. package/out/replicache/src/kv/expo-sqlite/store.js +52 -50
  99. package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
  100. package/out/replicache/src/kv/idb-store-with-mem-fallback.js +71 -68
  101. package/out/replicache/src/kv/idb-store-with-mem-fallback.js.map +1 -1
  102. package/out/replicache/src/kv/idb-store.js +144 -168
  103. package/out/replicache/src/kv/idb-store.js.map +1 -1
  104. package/out/replicache/src/kv/mem-store.js +57 -45
  105. package/out/replicache/src/kv/mem-store.js.map +1 -1
  106. package/out/replicache/src/kv/op-sqlite/store.js +56 -62
  107. package/out/replicache/src/kv/op-sqlite/store.js.map +1 -1
  108. package/out/replicache/src/kv/op-sqlite/types.d.ts.map +1 -1
  109. package/out/replicache/src/kv/op-sqlite/types.js +7 -6
  110. package/out/replicache/src/kv/op-sqlite/types.js.map +1 -1
  111. package/out/replicache/src/kv/read-impl.js +26 -25
  112. package/out/replicache/src/kv/read-impl.js.map +1 -1
  113. package/out/replicache/src/kv/sqlite-store.js +194 -207
  114. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  115. package/out/replicache/src/kv/throw-if-closed.js +12 -19
  116. package/out/replicache/src/kv/throw-if-closed.js.map +1 -1
  117. package/out/replicache/src/kv/write-impl-base.js +44 -56
  118. package/out/replicache/src/kv/write-impl-base.js.map +1 -1
  119. package/out/replicache/src/kv/write-impl.js +22 -26
  120. package/out/replicache/src/kv/write-impl.js.map +1 -1
  121. package/out/replicache/src/lazy.js +10 -11
  122. package/out/replicache/src/lazy.js.map +1 -1
  123. package/out/replicache/src/log-options.js +14 -7
  124. package/out/replicache/src/log-options.js.map +1 -1
  125. package/out/replicache/src/make-idb-name.js +14 -9
  126. package/out/replicache/src/make-idb-name.js.map +1 -1
  127. package/out/replicache/src/mutation-recovery.js +12 -0
  128. package/out/replicache/src/mutation-recovery.js.map +1 -0
  129. package/out/replicache/src/new-client-channel.js +34 -42
  130. package/out/replicache/src/new-client-channel.js.map +1 -1
  131. package/out/replicache/src/on-persist-channel.js +26 -29
  132. package/out/replicache/src/on-persist-channel.js.map +1 -1
  133. package/out/replicache/src/op-sqlite.js +2 -0
  134. package/out/replicache/src/patch-operation.js +27 -36
  135. package/out/replicache/src/patch-operation.js.map +1 -1
  136. package/out/replicache/src/pending-mutations.js +14 -12
  137. package/out/replicache/src/pending-mutations.js.map +1 -1
  138. package/out/replicache/src/persist/client-gc.js +36 -51
  139. package/out/replicache/src/persist/client-gc.js.map +1 -1
  140. package/out/replicache/src/persist/client-group-gc.js +29 -36
  141. package/out/replicache/src/persist/client-group-gc.js.map +1 -1
  142. package/out/replicache/src/persist/client-groups.js +80 -154
  143. package/out/replicache/src/persist/client-groups.js.map +1 -1
  144. package/out/replicache/src/persist/clients.js +212 -307
  145. package/out/replicache/src/persist/clients.js.map +1 -1
  146. package/out/replicache/src/persist/collect-idb-databases.js +109 -171
  147. package/out/replicache/src/persist/collect-idb-databases.js.map +1 -1
  148. package/out/replicache/src/persist/gather-mem-only-visitor.js +23 -24
  149. package/out/replicache/src/persist/gather-mem-only-visitor.js.map +1 -1
  150. package/out/replicache/src/persist/gather-not-cached-visitor.js +35 -33
  151. package/out/replicache/src/persist/gather-not-cached-visitor.js.map +1 -1
  152. package/out/replicache/src/persist/heartbeat.js +31 -41
  153. package/out/replicache/src/persist/heartbeat.js.map +1 -1
  154. package/out/replicache/src/persist/idb-databases-store-db-name.js +9 -12
  155. package/out/replicache/src/persist/idb-databases-store-db-name.js.map +1 -1
  156. package/out/replicache/src/persist/idb-databases-store.js +78 -97
  157. package/out/replicache/src/persist/idb-databases-store.js.map +1 -1
  158. package/out/replicache/src/persist/make-client-id.js +13 -9
  159. package/out/replicache/src/persist/make-client-id.js.map +1 -1
  160. package/out/replicache/src/persist/persist.js +113 -174
  161. package/out/replicache/src/persist/persist.js.map +1 -1
  162. package/out/replicache/src/persist/refresh.js +94 -183
  163. package/out/replicache/src/persist/refresh.js.map +1 -1
  164. package/out/replicache/src/process-scheduler.js +122 -143
  165. package/out/replicache/src/process-scheduler.js.map +1 -1
  166. package/out/replicache/src/pusher.js +21 -26
  167. package/out/replicache/src/pusher.js.map +1 -1
  168. package/out/replicache/src/replicache-impl.js +844 -1184
  169. package/out/replicache/src/replicache-impl.js.map +1 -1
  170. package/out/replicache/src/report-error.js +9 -6
  171. package/out/replicache/src/report-error.js.map +1 -1
  172. package/out/replicache/src/request-idle.js +13 -11
  173. package/out/replicache/src/request-idle.js.map +1 -1
  174. package/out/replicache/src/scan-iterator.d.ts.map +1 -1
  175. package/out/replicache/src/scan-iterator.js +108 -135
  176. package/out/replicache/src/scan-iterator.js.map +1 -1
  177. package/out/replicache/src/scan-options.js +33 -39
  178. package/out/replicache/src/scan-options.js.map +1 -1
  179. package/out/replicache/src/set-interval-with-signal.js +11 -10
  180. package/out/replicache/src/set-interval-with-signal.js.map +1 -1
  181. package/out/replicache/src/sqlite.js +2 -0
  182. package/out/replicache/src/subscriptions.js +222 -338
  183. package/out/replicache/src/subscriptions.js.map +1 -1
  184. package/out/replicache/src/sync/diff.js +52 -65
  185. package/out/replicache/src/sync/diff.js.map +1 -1
  186. package/out/replicache/src/sync/ids.js +8 -9
  187. package/out/replicache/src/sync/ids.js.map +1 -1
  188. package/out/replicache/src/sync/patch.js +34 -45
  189. package/out/replicache/src/sync/patch.js.map +1 -1
  190. package/out/replicache/src/sync/pull-error.js +15 -15
  191. package/out/replicache/src/sync/pull-error.js.map +1 -1
  192. package/out/replicache/src/sync/pull.js +145 -283
  193. package/out/replicache/src/sync/pull.js.map +1 -1
  194. package/out/replicache/src/sync/push.js +64 -79
  195. package/out/replicache/src/sync/push.js.map +1 -1
  196. package/out/replicache/src/sync/request-id.js +23 -15
  197. package/out/replicache/src/sync/request-id.js.map +1 -1
  198. package/out/replicache/src/sync/sync-head-name.js +6 -5
  199. package/out/replicache/src/sync/sync-head-name.js.map +1 -1
  200. package/out/replicache/src/to-error.js +7 -8
  201. package/out/replicache/src/to-error.js.map +1 -1
  202. package/out/replicache/src/transaction-closed-error.js +15 -15
  203. package/out/replicache/src/transaction-closed-error.js.map +1 -1
  204. package/out/replicache/src/transactions.js +120 -140
  205. package/out/replicache/src/transactions.js.map +1 -1
  206. package/out/replicache/src/version.js +9 -5
  207. package/out/replicache/src/version.js.map +1 -1
  208. package/out/replicache/src/with-transactions.js +23 -20
  209. package/out/replicache/src/with-transactions.js.map +1 -1
  210. package/out/shared/src/abort-error.js +7 -6
  211. package/out/shared/src/abort-error.js.map +1 -1
  212. package/out/shared/src/arrays.js +35 -42
  213. package/out/shared/src/arrays.js.map +1 -1
  214. package/out/shared/src/asserts.js +21 -45
  215. package/out/shared/src/asserts.js.map +1 -1
  216. package/out/shared/src/bigint-json.js +42 -38
  217. package/out/shared/src/bigint-json.js.map +1 -1
  218. package/out/shared/src/binary-search.js +27 -18
  219. package/out/shared/src/binary-search.js.map +1 -1
  220. package/out/shared/src/broadcast-channel.js +20 -23
  221. package/out/shared/src/broadcast-channel.js.map +1 -1
  222. package/out/shared/src/browser-env.js +11 -17
  223. package/out/shared/src/browser-env.js.map +1 -1
  224. package/out/shared/src/btree-set.js +419 -481
  225. package/out/shared/src/btree-set.js.map +1 -1
  226. package/out/shared/src/cache.js +43 -36
  227. package/out/shared/src/cache.js.map +1 -1
  228. package/out/shared/src/centroid.js +24 -26
  229. package/out/shared/src/centroid.js.map +1 -1
  230. package/out/shared/src/config.js +6 -6
  231. package/out/shared/src/config.js.map +1 -1
  232. package/out/shared/src/custom-key-map.js +54 -58
  233. package/out/shared/src/custom-key-map.js.map +1 -1
  234. package/out/shared/src/custom-key-set.js +53 -51
  235. package/out/shared/src/custom-key-set.js.map +1 -1
  236. package/out/shared/src/deep-clone.js +30 -41
  237. package/out/shared/src/deep-clone.js.map +1 -1
  238. package/out/shared/src/deep-merge.js +25 -24
  239. package/out/shared/src/deep-merge.js.map +1 -1
  240. package/out/shared/src/document-visible.js +63 -70
  241. package/out/shared/src/document-visible.js.map +1 -1
  242. package/out/shared/src/dotenv.js +7 -3
  243. package/out/shared/src/dotenv.js.map +1 -1
  244. package/out/shared/src/error.js +43 -64
  245. package/out/shared/src/error.js.map +1 -1
  246. package/out/shared/src/has-own.js +6 -5
  247. package/out/shared/src/has-own.js.map +1 -1
  248. package/out/shared/src/hash.js +15 -14
  249. package/out/shared/src/hash.js.map +1 -1
  250. package/out/shared/src/iterables.js +34 -47
  251. package/out/shared/src/iterables.js.map +1 -1
  252. package/out/shared/src/json-schema.js +25 -30
  253. package/out/shared/src/json-schema.js.map +1 -1
  254. package/out/shared/src/json.js +90 -129
  255. package/out/shared/src/json.js.map +1 -1
  256. package/out/shared/src/logging-test-utils.js +9 -11
  257. package/out/shared/src/logging-test-utils.js.map +1 -1
  258. package/out/shared/src/logging.js +75 -95
  259. package/out/shared/src/logging.js.map +1 -1
  260. package/out/shared/src/must.js +7 -8
  261. package/out/shared/src/must.js.map +1 -1
  262. package/out/shared/src/navigator.js +6 -5
  263. package/out/shared/src/navigator.js.map +1 -1
  264. package/out/shared/src/object-traversal.js +23 -23
  265. package/out/shared/src/object-traversal.js.map +1 -1
  266. package/out/shared/src/objects.js +15 -18
  267. package/out/shared/src/objects.js.map +1 -1
  268. package/out/shared/src/options.js +225 -302
  269. package/out/shared/src/options.js.map +1 -1
  270. package/out/shared/src/parse-big-int.js +12 -11
  271. package/out/shared/src/parse-big-int.js.map +1 -1
  272. package/out/shared/src/promise-race.js +21 -17
  273. package/out/shared/src/promise-race.js.map +1 -1
  274. package/out/shared/src/queue.js +124 -124
  275. package/out/shared/src/queue.js.map +1 -1
  276. package/out/shared/src/rand.js +13 -7
  277. package/out/shared/src/rand.js.map +1 -1
  278. package/out/shared/src/random-uint64.js +8 -7
  279. package/out/shared/src/random-uint64.js.map +1 -1
  280. package/out/shared/src/random-values.js +8 -11
  281. package/out/shared/src/random-values.js.map +1 -1
  282. package/out/shared/src/record-proxy.js +68 -57
  283. package/out/shared/src/record-proxy.js.map +1 -1
  284. package/out/shared/src/resolved-promises.js +9 -11
  285. package/out/shared/src/resolved-promises.js.map +1 -1
  286. package/out/shared/src/sentinels.js +9 -12
  287. package/out/shared/src/sentinels.js.map +1 -1
  288. package/out/shared/src/set-utils.js +41 -63
  289. package/out/shared/src/set-utils.js.map +1 -1
  290. package/out/shared/src/size-of-value.js +55 -51
  291. package/out/shared/src/size-of-value.js.map +1 -1
  292. package/out/shared/src/sleep.js +50 -45
  293. package/out/shared/src/sleep.js.map +1 -1
  294. package/out/shared/src/string-compare.js +8 -11
  295. package/out/shared/src/string-compare.js.map +1 -1
  296. package/out/shared/src/subscribable.js +34 -33
  297. package/out/shared/src/subscribable.js.map +1 -1
  298. package/out/shared/src/tdigest-schema.js +11 -7
  299. package/out/shared/src/tdigest-schema.js.map +1 -1
  300. package/out/shared/src/tdigest.js +197 -270
  301. package/out/shared/src/tdigest.js.map +1 -1
  302. package/out/shared/src/valita.js +145 -174
  303. package/out/shared/src/valita.js.map +1 -1
  304. package/out/z2s/src/compiler.d.ts.map +1 -1
  305. package/out/z2s/src/compiler.js +238 -468
  306. package/out/z2s/src/compiler.js.map +1 -1
  307. package/out/z2s/src/sql.d.ts +0 -1
  308. package/out/z2s/src/sql.d.ts.map +1 -1
  309. package/out/z2s/src/sql.js +149 -194
  310. package/out/z2s/src/sql.js.map +1 -1
  311. package/out/zero/package.js +194 -0
  312. package/out/zero/package.js.map +1 -0
  313. package/out/zero/src/adapters/drizzle.js +1 -6
  314. package/out/zero/src/adapters/pg.js +1 -6
  315. package/out/zero/src/adapters/postgresjs.js +1 -6
  316. package/out/zero/src/adapters/prisma.js +1 -5
  317. package/out/zero/src/analyze-query.js +1 -1
  318. package/out/zero/src/ast-to-zql.js +1 -1
  319. package/out/zero/src/bindings.js +6 -21
  320. package/out/zero/src/build-schema.js +5 -1
  321. package/out/zero/src/build-schema.js.map +1 -1
  322. package/out/zero/src/change-protocol/v0.js +3 -5
  323. package/out/zero/src/cli.js +2 -2
  324. package/out/zero/src/deploy-permissions.js +1 -1
  325. package/out/zero/src/expo-sqlite.js +2 -4
  326. package/out/zero/src/op-sqlite.js +2 -4
  327. package/out/zero/src/pg.js +2 -20
  328. package/out/zero/src/react-native.js +16 -12
  329. package/out/zero/src/react-native.js.map +1 -1
  330. package/out/zero/src/react.js +3 -12
  331. package/out/zero/src/server/runner/main.js +2 -0
  332. package/out/zero/src/server.js +2 -17
  333. package/out/zero/src/solid.js +3 -12
  334. package/out/zero/src/sqlite.js +2 -6
  335. package/out/zero/src/transform-query.js +1 -1
  336. package/out/zero/src/zero-cache-dev.js +124 -151
  337. package/out/zero/src/zero-cache-dev.js.map +1 -1
  338. package/out/zero/src/zero-out.js +9 -6
  339. package/out/zero/src/zero-out.js.map +1 -1
  340. package/out/zero/src/zero.js +6 -55
  341. package/out/zero/src/zqlite.js +2 -7
  342. package/out/zero-cache/src/auth/auth.js +138 -172
  343. package/out/zero-cache/src/auth/auth.js.map +1 -1
  344. package/out/zero-cache/src/auth/jwt.js +25 -33
  345. package/out/zero-cache/src/auth/jwt.js.map +1 -1
  346. package/out/zero-cache/src/auth/load-permissions.js +54 -62
  347. package/out/zero-cache/src/auth/load-permissions.js.map +1 -1
  348. package/out/zero-cache/src/auth/read-authorizer.js +70 -80
  349. package/out/zero-cache/src/auth/read-authorizer.js.map +1 -1
  350. package/out/zero-cache/src/auth/write-authorizer.js +284 -432
  351. package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
  352. package/out/zero-cache/src/config/network.js +31 -45
  353. package/out/zero-cache/src/config/network.js.map +1 -1
  354. package/out/zero-cache/src/config/normalize.js +81 -83
  355. package/out/zero-cache/src/config/normalize.js.map +1 -1
  356. package/out/zero-cache/src/config/server-context.js +32 -29
  357. package/out/zero-cache/src/config/server-context.js.map +1 -1
  358. package/out/zero-cache/src/config/zero-config.js +753 -833
  359. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  360. package/out/zero-cache/src/custom/fetch.js +183 -230
  361. package/out/zero-cache/src/custom/fetch.js.map +1 -1
  362. package/out/zero-cache/src/custom-queries/transform-query.js +93 -99
  363. package/out/zero-cache/src/custom-queries/transform-query.js.map +1 -1
  364. package/out/zero-cache/src/db/create.js +27 -29
  365. package/out/zero-cache/src/db/create.js.map +1 -1
  366. package/out/zero-cache/src/db/delete-lite-db.js +11 -7
  367. package/out/zero-cache/src/db/delete-lite-db.js.map +1 -1
  368. package/out/zero-cache/src/db/lite-tables.js +118 -158
  369. package/out/zero-cache/src/db/lite-tables.js.map +1 -1
  370. package/out/zero-cache/src/db/migration-lite.js +110 -178
  371. package/out/zero-cache/src/db/migration-lite.js.map +1 -1
  372. package/out/zero-cache/src/db/migration.js +82 -151
  373. package/out/zero-cache/src/db/migration.js.map +1 -1
  374. package/out/zero-cache/src/db/mode-enum.js +8 -9
  375. package/out/zero-cache/src/db/mode-enum.js.map +1 -1
  376. package/out/zero-cache/src/db/pg-copy.js +56 -54
  377. package/out/zero-cache/src/db/pg-copy.js.map +1 -1
  378. package/out/zero-cache/src/db/pg-to-lite.js +74 -110
  379. package/out/zero-cache/src/db/pg-to-lite.js.map +1 -1
  380. package/out/zero-cache/src/db/pg-type-parser.js +19 -36
  381. package/out/zero-cache/src/db/pg-type-parser.js.map +1 -1
  382. package/out/zero-cache/src/db/run-transaction.js +19 -20
  383. package/out/zero-cache/src/db/run-transaction.js.map +1 -1
  384. package/out/zero-cache/src/db/specs.js +42 -78
  385. package/out/zero-cache/src/db/specs.js.map +1 -1
  386. package/out/zero-cache/src/db/statements.js +52 -59
  387. package/out/zero-cache/src/db/statements.js.map +1 -1
  388. package/out/zero-cache/src/db/transaction-pool.js +376 -400
  389. package/out/zero-cache/src/db/transaction-pool.js.map +1 -1
  390. package/out/zero-cache/src/db/warmup.js +13 -24
  391. package/out/zero-cache/src/db/warmup.js.map +1 -1
  392. package/out/zero-cache/src/observability/events.js +89 -99
  393. package/out/zero-cache/src/observability/events.js.map +1 -1
  394. package/out/zero-cache/src/observability/metrics.js +30 -54
  395. package/out/zero-cache/src/observability/metrics.js.map +1 -1
  396. package/out/zero-cache/src/scripts/decommission.js +42 -47
  397. package/out/zero-cache/src/scripts/decommission.js.map +1 -1
  398. package/out/zero-cache/src/scripts/deploy-permissions.js +106 -144
  399. package/out/zero-cache/src/scripts/deploy-permissions.js.map +1 -1
  400. package/out/zero-cache/src/scripts/permissions.js +86 -107
  401. package/out/zero-cache/src/scripts/permissions.js.map +1 -1
  402. package/out/zero-cache/src/server/anonymous-otel-start.js +306 -440
  403. package/out/zero-cache/src/server/anonymous-otel-start.js.map +1 -1
  404. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  405. package/out/zero-cache/src/server/change-streamer.js +57 -130
  406. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  407. package/out/zero-cache/src/server/inspector-delegate.js +89 -100
  408. package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
  409. package/out/zero-cache/src/server/logging.js +18 -26
  410. package/out/zero-cache/src/server/logging.js.map +1 -1
  411. package/out/zero-cache/src/server/main.js +85 -142
  412. package/out/zero-cache/src/server/main.js.map +1 -1
  413. package/out/zero-cache/src/server/mutator.js +16 -13
  414. package/out/zero-cache/src/server/mutator.js.map +1 -1
  415. package/out/zero-cache/src/server/otel-diag-logger.js +42 -49
  416. package/out/zero-cache/src/server/otel-diag-logger.js.map +1 -1
  417. package/out/zero-cache/src/server/otel-log-sink.js +34 -44
  418. package/out/zero-cache/src/server/otel-log-sink.js.map +1 -1
  419. package/out/zero-cache/src/server/otel-start.js +43 -51
  420. package/out/zero-cache/src/server/otel-start.js.map +1 -1
  421. package/out/zero-cache/src/server/priority-op.js +27 -25
  422. package/out/zero-cache/src/server/priority-op.js.map +1 -1
  423. package/out/zero-cache/src/server/reaper.js +32 -43
  424. package/out/zero-cache/src/server/reaper.js.map +1 -1
  425. package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
  426. package/out/zero-cache/src/server/replicator.js +41 -57
  427. package/out/zero-cache/src/server/replicator.js.map +1 -1
  428. package/out/zero-cache/src/server/runner/main.js +7 -8
  429. package/out/zero-cache/src/server/runner/main.js.map +1 -1
  430. package/out/zero-cache/src/server/runner/run-worker.js +56 -52
  431. package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
  432. package/out/zero-cache/src/server/runner/runtime.js +26 -32
  433. package/out/zero-cache/src/server/runner/runtime.js.map +1 -1
  434. package/out/zero-cache/src/server/runner/zero-dispatcher.js +22 -27
  435. package/out/zero-cache/src/server/runner/zero-dispatcher.js.map +1 -1
  436. package/out/zero-cache/src/server/syncer.js +79 -148
  437. package/out/zero-cache/src/server/syncer.js.map +1 -1
  438. package/out/zero-cache/src/server/worker-dispatcher.js +84 -113
  439. package/out/zero-cache/src/server/worker-dispatcher.js.map +1 -1
  440. package/out/zero-cache/src/server/worker-urls.d.ts +2 -1
  441. package/out/zero-cache/src/server/worker-urls.d.ts.map +1 -1
  442. package/out/zero-cache/src/server/worker-urls.js +14 -18
  443. package/out/zero-cache/src/server/worker-urls.js.map +1 -1
  444. package/out/zero-cache/src/server/write-worker.js +2 -0
  445. package/out/zero-cache/src/services/analyze.js +61 -130
  446. package/out/zero-cache/src/services/analyze.js.map +1 -1
  447. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +420 -419
  448. package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
  449. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +111 -114
  450. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
  451. package/out/zero-cache/src/services/change-source/common/replica-schema.js +80 -148
  452. package/out/zero-cache/src/services/change-source/common/replica-schema.js.map +1 -1
  453. package/out/zero-cache/src/services/change-source/custom/change-source.js +154 -216
  454. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  455. package/out/zero-cache/src/services/change-source/pg/backfill-metadata.js +11 -14
  456. package/out/zero-cache/src/services/change-source/pg/backfill-metadata.js.map +1 -1
  457. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +168 -212
  458. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  459. package/out/zero-cache/src/services/change-source/pg/change-source.js +672 -892
  460. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  461. package/out/zero-cache/src/services/change-source/pg/decommission.js +19 -23
  462. package/out/zero-cache/src/services/change-source/pg/decommission.js.map +1 -1
  463. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +258 -411
  464. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  465. package/out/zero-cache/src/services/change-source/pg/logical-replication/binary-reader.js +59 -65
  466. package/out/zero-cache/src/services/change-source/pg/logical-replication/binary-reader.js.map +1 -1
  467. package/out/zero-cache/src/services/change-source/pg/logical-replication/pgoutput-parser.js +218 -247
  468. package/out/zero-cache/src/services/change-source/pg/logical-replication/pgoutput-parser.js.map +1 -1
  469. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +100 -142
  470. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js.map +1 -1
  471. package/out/zero-cache/src/services/change-source/pg/lsn.js +17 -19
  472. package/out/zero-cache/src/services/change-source/pg/lsn.js.map +1 -1
  473. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +88 -98
  474. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
  475. package/out/zero-cache/src/services/change-source/pg/schema/init.js +96 -177
  476. package/out/zero-cache/src/services/change-source/pg/schema/init.js.map +1 -1
  477. package/out/zero-cache/src/services/change-source/pg/schema/published.js +69 -107
  478. package/out/zero-cache/src/services/change-source/pg/schema/published.js.map +1 -1
  479. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +151 -212
  480. package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
  481. package/out/zero-cache/src/services/change-source/pg/schema/validation.js +22 -53
  482. package/out/zero-cache/src/services/change-source/pg/schema/validation.js.map +1 -1
  483. package/out/zero-cache/src/services/change-source/protocol/current/control.js +24 -12
  484. package/out/zero-cache/src/services/change-source/protocol/current/control.js.map +1 -1
  485. package/out/zero-cache/src/services/change-source/protocol/current/data.js +180 -290
  486. package/out/zero-cache/src/services/change-source/protocol/current/data.js.map +1 -1
  487. package/out/zero-cache/src/services/change-source/protocol/current/downstream.js +21 -33
  488. package/out/zero-cache/src/services/change-source/protocol/current/downstream.js.map +1 -1
  489. package/out/zero-cache/src/services/change-source/protocol/current/json.js +7 -18
  490. package/out/zero-cache/src/services/change-source/protocol/current/json.js.map +1 -1
  491. package/out/zero-cache/src/services/change-source/protocol/current/path.js +24 -5
  492. package/out/zero-cache/src/services/change-source/protocol/current/path.js.map +1 -1
  493. package/out/zero-cache/src/services/change-source/protocol/current/status.js +25 -19
  494. package/out/zero-cache/src/services/change-source/protocol/current/status.js.map +1 -1
  495. package/out/zero-cache/src/services/change-source/protocol/current/upstream.js +24 -16
  496. package/out/zero-cache/src/services/change-source/protocol/current/upstream.js.map +1 -1
  497. package/out/zero-cache/src/services/change-source/protocol/current.js +51 -46
  498. package/out/zero-cache/src/services/change-source/protocol/current.js.map +1 -1
  499. package/out/zero-cache/src/services/change-source/protocol/mod.js +2 -0
  500. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +165 -171
  501. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  502. package/out/zero-cache/src/services/change-streamer/broadcast.js +163 -169
  503. package/out/zero-cache/src/services/change-streamer/broadcast.js.map +1 -1
  504. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +154 -221
  505. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  506. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  507. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +340 -299
  508. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  509. package/out/zero-cache/src/services/change-streamer/change-streamer.js +17 -24
  510. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  511. package/out/zero-cache/src/services/change-streamer/forwarder.js +84 -103
  512. package/out/zero-cache/src/services/change-streamer/forwarder.js.map +1 -1
  513. package/out/zero-cache/src/services/change-streamer/replica-monitor.js +49 -43
  514. package/out/zero-cache/src/services/change-streamer/replica-monitor.js.map +1 -1
  515. package/out/zero-cache/src/services/change-streamer/schema/init.js +61 -89
  516. package/out/zero-cache/src/services/change-streamer/schema/init.js.map +1 -1
  517. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts +20 -1
  518. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts.map +1 -1
  519. package/out/zero-cache/src/services/change-streamer/schema/tables.js +131 -109
  520. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  521. package/out/zero-cache/src/services/change-streamer/snapshot.js +26 -28
  522. package/out/zero-cache/src/services/change-streamer/snapshot.js.map +1 -1
  523. package/out/zero-cache/src/services/change-streamer/storer.js +434 -513
  524. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  525. package/out/zero-cache/src/services/change-streamer/subscriber.js +142 -155
  526. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  527. package/out/zero-cache/src/services/heapz.js +18 -20
  528. package/out/zero-cache/src/services/heapz.js.map +1 -1
  529. package/out/zero-cache/src/services/http-service.js +59 -57
  530. package/out/zero-cache/src/services/http-service.js.map +1 -1
  531. package/out/zero-cache/src/services/life-cycle.js +182 -214
  532. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  533. package/out/zero-cache/src/services/limiter/sliding-window-limiter.js +102 -81
  534. package/out/zero-cache/src/services/limiter/sliding-window-limiter.js.map +1 -1
  535. package/out/zero-cache/src/services/litestream/commands.js +144 -205
  536. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  537. package/out/zero-cache/src/services/mutagen/error.js +10 -14
  538. package/out/zero-cache/src/services/mutagen/error.js.map +1 -1
  539. package/out/zero-cache/src/services/mutagen/mutagen.js +166 -264
  540. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  541. package/out/zero-cache/src/services/mutagen/pusher.js +372 -487
  542. package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
  543. package/out/zero-cache/src/services/replicator/change-processor.js +483 -592
  544. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  545. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts +4 -2
  546. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  547. package/out/zero-cache/src/services/replicator/incremental-sync.js +118 -143
  548. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  549. package/out/zero-cache/src/services/replicator/notifier.js +52 -28
  550. package/out/zero-cache/src/services/replicator/notifier.js.map +1 -1
  551. package/out/zero-cache/src/services/replicator/replication-status.js +105 -128
  552. package/out/zero-cache/src/services/replicator/replication-status.js.map +1 -1
  553. package/out/zero-cache/src/services/replicator/replicator.d.ts +2 -1
  554. package/out/zero-cache/src/services/replicator/replicator.d.ts.map +1 -1
  555. package/out/zero-cache/src/services/replicator/replicator.js +32 -34
  556. package/out/zero-cache/src/services/replicator/replicator.js.map +1 -1
  557. package/out/zero-cache/src/services/replicator/schema/change-log.js +101 -133
  558. package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
  559. package/out/zero-cache/src/services/replicator/schema/column-metadata.js +145 -174
  560. package/out/zero-cache/src/services/replicator/schema/column-metadata.js.map +1 -1
  561. package/out/zero-cache/src/services/replicator/schema/constants.js +11 -5
  562. package/out/zero-cache/src/services/replicator/schema/constants.js.map +1 -1
  563. package/out/zero-cache/src/services/replicator/schema/replication-state.js +56 -107
  564. package/out/zero-cache/src/services/replicator/schema/replication-state.js.map +1 -1
  565. package/out/zero-cache/src/services/replicator/schema/table-metadata.js +81 -66
  566. package/out/zero-cache/src/services/replicator/schema/table-metadata.js.map +1 -1
  567. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts +69 -0
  568. package/out/zero-cache/src/services/replicator/write-worker-client.d.ts.map +1 -0
  569. package/out/zero-cache/src/services/replicator/write-worker-client.js +96 -0
  570. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -0
  571. package/out/zero-cache/src/services/replicator/write-worker.js +68 -0
  572. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -0
  573. package/out/zero-cache/src/services/run-ast.js +79 -120
  574. package/out/zero-cache/src/services/run-ast.js.map +1 -1
  575. package/out/zero-cache/src/services/runner.js +39 -41
  576. package/out/zero-cache/src/services/runner.js.map +1 -1
  577. package/out/zero-cache/src/services/running-state.js +129 -134
  578. package/out/zero-cache/src/services/running-state.js.map +1 -1
  579. package/out/zero-cache/src/services/statz.js +139 -200
  580. package/out/zero-cache/src/services/statz.js.map +1 -1
  581. package/out/zero-cache/src/services/view-syncer/active-users-gauge.js +46 -49
  582. package/out/zero-cache/src/services/view-syncer/active-users-gauge.js.map +1 -1
  583. package/out/zero-cache/src/services/view-syncer/client-handler.js +257 -299
  584. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  585. package/out/zero-cache/src/services/view-syncer/client-schema.js +52 -82
  586. package/out/zero-cache/src/services/view-syncer/client-schema.js.map +1 -1
  587. package/out/zero-cache/src/services/view-syncer/cvr-purger.js +85 -107
  588. package/out/zero-cache/src/services/view-syncer/cvr-purger.js.map +1 -1
  589. package/out/zero-cache/src/services/view-syncer/cvr-store.js +604 -757
  590. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  591. package/out/zero-cache/src/services/view-syncer/cvr.js +631 -739
  592. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  593. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +60 -40
  594. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
  595. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +95 -178
  596. package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
  597. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  598. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +571 -722
  599. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  600. package/out/zero-cache/src/services/view-syncer/row-record-cache.d.ts.map +1 -1
  601. package/out/zero-cache/src/services/view-syncer/row-record-cache.js +246 -257
  602. package/out/zero-cache/src/services/view-syncer/row-record-cache.js.map +1 -1
  603. package/out/zero-cache/src/services/view-syncer/schema/cvr.js +59 -45
  604. package/out/zero-cache/src/services/view-syncer/schema/cvr.js.map +1 -1
  605. package/out/zero-cache/src/services/view-syncer/schema/init.js +121 -189
  606. package/out/zero-cache/src/services/view-syncer/schema/init.js.map +1 -1
  607. package/out/zero-cache/src/services/view-syncer/schema/types.js +138 -263
  608. package/out/zero-cache/src/services/view-syncer/schema/types.js.map +1 -1
  609. package/out/zero-cache/src/services/view-syncer/snapshotter.js +322 -335
  610. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  611. package/out/zero-cache/src/services/view-syncer/tracer.js +7 -6
  612. package/out/zero-cache/src/services/view-syncer/tracer.js.map +1 -1
  613. package/out/zero-cache/src/services/view-syncer/ttl-clock.js +9 -11
  614. package/out/zero-cache/src/services/view-syncer/ttl-clock.js.map +1 -1
  615. package/out/zero-cache/src/services/view-syncer/view-syncer.js +1067 -1603
  616. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  617. package/out/zero-cache/src/types/error-with-level.js +19 -25
  618. package/out/zero-cache/src/types/error-with-level.js.map +1 -1
  619. package/out/zero-cache/src/types/http.js +17 -26
  620. package/out/zero-cache/src/types/http.js.map +1 -1
  621. package/out/zero-cache/src/types/lexi-version.js +28 -42
  622. package/out/zero-cache/src/types/lexi-version.js.map +1 -1
  623. package/out/zero-cache/src/types/lite.js +101 -121
  624. package/out/zero-cache/src/types/lite.js.map +1 -1
  625. package/out/zero-cache/src/types/names.js +6 -5
  626. package/out/zero-cache/src/types/names.js.map +1 -1
  627. package/out/zero-cache/src/types/pg-data-type.d.ts +1 -0
  628. package/out/zero-cache/src/types/pg-data-type.d.ts.map +1 -1
  629. package/out/zero-cache/src/types/pg-data-type.js +58 -73
  630. package/out/zero-cache/src/types/pg-data-type.js.map +1 -1
  631. package/out/zero-cache/src/types/pg-types.js +12 -19
  632. package/out/zero-cache/src/types/pg-types.js.map +1 -1
  633. package/out/zero-cache/src/types/pg.js +144 -218
  634. package/out/zero-cache/src/types/pg.js.map +1 -1
  635. package/out/zero-cache/src/types/processes.js +95 -90
  636. package/out/zero-cache/src/types/processes.js.map +1 -1
  637. package/out/zero-cache/src/types/profiler.js +32 -27
  638. package/out/zero-cache/src/types/profiler.js.map +1 -1
  639. package/out/zero-cache/src/types/row-key.js +42 -30
  640. package/out/zero-cache/src/types/row-key.js.map +1 -1
  641. package/out/zero-cache/src/types/shards.js +36 -45
  642. package/out/zero-cache/src/types/shards.js.map +1 -1
  643. package/out/zero-cache/src/types/sql.js +20 -9
  644. package/out/zero-cache/src/types/sql.js.map +1 -1
  645. package/out/zero-cache/src/types/state-version.js +17 -23
  646. package/out/zero-cache/src/types/state-version.js.map +1 -1
  647. package/out/zero-cache/src/types/streams.js +234 -270
  648. package/out/zero-cache/src/types/streams.js.map +1 -1
  649. package/out/zero-cache/src/types/strings.js +10 -13
  650. package/out/zero-cache/src/types/strings.js.map +1 -1
  651. package/out/zero-cache/src/types/subscription.js +266 -226
  652. package/out/zero-cache/src/types/subscription.js.map +1 -1
  653. package/out/zero-cache/src/types/url-params.js +30 -39
  654. package/out/zero-cache/src/types/url-params.js.map +1 -1
  655. package/out/zero-cache/src/types/websocket-handoff.js +62 -75
  656. package/out/zero-cache/src/types/websocket-handoff.js.map +1 -1
  657. package/out/zero-cache/src/types/ws.js +43 -53
  658. package/out/zero-cache/src/types/ws.js.map +1 -1
  659. package/out/zero-cache/src/workers/connect-params.js +42 -43
  660. package/out/zero-cache/src/workers/connect-params.js.map +1 -1
  661. package/out/zero-cache/src/workers/connection.js +213 -282
  662. package/out/zero-cache/src/workers/connection.js.map +1 -1
  663. package/out/zero-cache/src/workers/mutator.js +22 -21
  664. package/out/zero-cache/src/workers/mutator.js.map +1 -1
  665. package/out/zero-cache/src/workers/replicator.d.ts +7 -0
  666. package/out/zero-cache/src/workers/replicator.d.ts.map +1 -1
  667. package/out/zero-cache/src/workers/replicator.js +92 -97
  668. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  669. package/out/zero-cache/src/workers/syncer-ws-message-handler.js +121 -203
  670. package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
  671. package/out/zero-cache/src/workers/syncer.js +147 -201
  672. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  673. package/out/zero-client/src/client/active-clients-manager.js +178 -187
  674. package/out/zero-client/src/client/active-clients-manager.js.map +1 -1
  675. package/out/zero-client/src/client/bindings.js +11 -0
  676. package/out/zero-client/src/client/client-error-kind-enum.js +18 -29
  677. package/out/zero-client/src/client/client-error-kind-enum.js.map +1 -1
  678. package/out/zero-client/src/client/connection-manager.js +291 -346
  679. package/out/zero-client/src/client/connection-manager.js.map +1 -1
  680. package/out/zero-client/src/client/connection-status-enum.js +20 -15
  681. package/out/zero-client/src/client/connection-status-enum.js.map +1 -1
  682. package/out/zero-client/src/client/connection.js +92 -110
  683. package/out/zero-client/src/client/connection.js.map +1 -1
  684. package/out/zero-client/src/client/context.js +84 -100
  685. package/out/zero-client/src/client/context.js.map +1 -1
  686. package/out/zero-client/src/client/crud-impl.js +56 -88
  687. package/out/zero-client/src/client/crud-impl.js.map +1 -1
  688. package/out/zero-client/src/client/crud.js +127 -129
  689. package/out/zero-client/src/client/crud.js.map +1 -1
  690. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  691. package/out/zero-client/src/client/custom.js +50 -74
  692. package/out/zero-client/src/client/custom.js.map +1 -1
  693. package/out/zero-client/src/client/delete-clients-manager.js +72 -93
  694. package/out/zero-client/src/client/delete-clients-manager.js.map +1 -1
  695. package/out/zero-client/src/client/enable-analytics.js +8 -16
  696. package/out/zero-client/src/client/enable-analytics.js.map +1 -1
  697. package/out/zero-client/src/client/error.js +118 -133
  698. package/out/zero-client/src/client/error.js.map +1 -1
  699. package/out/zero-client/src/client/http-string.js +7 -7
  700. package/out/zero-client/src/client/http-string.js.map +1 -1
  701. package/out/zero-client/src/client/inspector/client-group.js +21 -26
  702. package/out/zero-client/src/client/inspector/client-group.js.map +1 -1
  703. package/out/zero-client/src/client/inspector/client.js +23 -26
  704. package/out/zero-client/src/client/inspector/client.js.map +1 -1
  705. package/out/zero-client/src/client/inspector/html-dialog-prompt.js +72 -73
  706. package/out/zero-client/src/client/inspector/html-dialog-prompt.js.map +1 -1
  707. package/out/zero-client/src/client/inspector/inspector.js +46 -51
  708. package/out/zero-client/src/client/inspector/inspector.js.map +1 -1
  709. package/out/zero-client/src/client/inspector/lazy-inspector.js +132 -192
  710. package/out/zero-client/src/client/inspector/lazy-inspector.js.map +1 -1
  711. package/out/zero-client/src/client/inspector/query.js +72 -77
  712. package/out/zero-client/src/client/inspector/query.js.map +1 -1
  713. package/out/zero-client/src/client/ivm-branch.js +118 -145
  714. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  715. package/out/zero-client/src/client/keys.js +15 -31
  716. package/out/zero-client/src/client/keys.js.map +1 -1
  717. package/out/zero-client/src/client/log-options.js +43 -57
  718. package/out/zero-client/src/client/log-options.js.map +1 -1
  719. package/out/zero-client/src/client/make-mutate-property.js +46 -29
  720. package/out/zero-client/src/client/make-mutate-property.js.map +1 -1
  721. package/out/zero-client/src/client/make-replicache-mutators.js +80 -96
  722. package/out/zero-client/src/client/make-replicache-mutators.js.map +1 -1
  723. package/out/zero-client/src/client/metric-name-enum.js +11 -15
  724. package/out/zero-client/src/client/metric-name-enum.js.map +1 -1
  725. package/out/zero-client/src/client/metrics.js +210 -237
  726. package/out/zero-client/src/client/metrics.js.map +1 -1
  727. package/out/zero-client/src/client/mutation-tracker.js +264 -354
  728. package/out/zero-client/src/client/mutation-tracker.js.map +1 -1
  729. package/out/zero-client/src/client/mutator-proxy.js +122 -151
  730. package/out/zero-client/src/client/mutator-proxy.js.map +1 -1
  731. package/out/zero-client/src/client/options.js +7 -10
  732. package/out/zero-client/src/client/options.js.map +1 -1
  733. package/out/zero-client/src/client/query-manager.js +305 -373
  734. package/out/zero-client/src/client/query-manager.js.map +1 -1
  735. package/out/zero-client/src/client/reload-error-handler.js +80 -101
  736. package/out/zero-client/src/client/reload-error-handler.js.map +1 -1
  737. package/out/zero-client/src/client/server-option.js +30 -59
  738. package/out/zero-client/src/client/server-option.js.map +1 -1
  739. package/out/zero-client/src/client/update-needed-reason-type-enum.js +27 -9
  740. package/out/zero-client/src/client/update-needed-reason-type-enum.js.map +1 -1
  741. package/out/zero-client/src/client/version.js +9 -5
  742. package/out/zero-client/src/client/version.js.map +1 -1
  743. package/out/zero-client/src/client/zero-poke-handler.d.ts +1 -1
  744. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  745. package/out/zero-client/src/client/zero-poke-handler.js +205 -293
  746. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
  747. package/out/zero-client/src/client/zero-rep.js +61 -68
  748. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  749. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  750. package/out/zero-client/src/client/zero.js +1367 -1834
  751. package/out/zero-client/src/client/zero.js.map +1 -1
  752. package/out/zero-client/src/mod.js +21 -0
  753. package/out/zero-client/src/util/nanoid.js +13 -18
  754. package/out/zero-client/src/util/nanoid.js.map +1 -1
  755. package/out/zero-client/src/util/socket.js +6 -5
  756. package/out/zero-client/src/util/socket.js.map +1 -1
  757. package/out/zero-pg/src/mod.js +10 -0
  758. package/out/zero-protocol/src/analyze-query-result.js +108 -148
  759. package/out/zero-protocol/src/analyze-query-result.js.map +1 -1
  760. package/out/zero-protocol/src/application-error.js +36 -34
  761. package/out/zero-protocol/src/application-error.js.map +1 -1
  762. package/out/zero-protocol/src/ast.js +236 -309
  763. package/out/zero-protocol/src/ast.js.map +1 -1
  764. package/out/zero-protocol/src/change-desired-queries.js +8 -13
  765. package/out/zero-protocol/src/change-desired-queries.js.map +1 -1
  766. package/out/zero-protocol/src/client-schema.js +21 -42
  767. package/out/zero-protocol/src/client-schema.js.map +1 -1
  768. package/out/zero-protocol/src/close-connection.js +20 -12
  769. package/out/zero-protocol/src/close-connection.js.map +1 -1
  770. package/out/zero-protocol/src/connect.js +37 -52
  771. package/out/zero-protocol/src/connect.js.map +1 -1
  772. package/out/zero-protocol/src/custom-queries.js +34 -65
  773. package/out/zero-protocol/src/custom-queries.js.map +1 -1
  774. package/out/zero-protocol/src/data.js +6 -9
  775. package/out/zero-protocol/src/data.js.map +1 -1
  776. package/out/zero-protocol/src/delete-clients.js +11 -17
  777. package/out/zero-protocol/src/delete-clients.js.map +1 -1
  778. package/out/zero-protocol/src/down.js +11 -23
  779. package/out/zero-protocol/src/down.js.map +1 -1
  780. package/out/zero-protocol/src/error-kind-enum.js +24 -41
  781. package/out/zero-protocol/src/error-kind-enum.js.map +1 -1
  782. package/out/zero-protocol/src/error-origin-enum.js +8 -9
  783. package/out/zero-protocol/src/error-origin-enum.js.map +1 -1
  784. package/out/zero-protocol/src/error-reason-enum.js +12 -17
  785. package/out/zero-protocol/src/error-reason-enum.js.map +1 -1
  786. package/out/zero-protocol/src/error.js +76 -152
  787. package/out/zero-protocol/src/error.js.map +1 -1
  788. package/out/zero-protocol/src/inspect-down.js +51 -74
  789. package/out/zero-protocol/src/inspect-down.js.map +1 -1
  790. package/out/zero-protocol/src/inspect-up.js +28 -46
  791. package/out/zero-protocol/src/inspect-up.js.map +1 -1
  792. package/out/zero-protocol/src/mutation-id.js +9 -9
  793. package/out/zero-protocol/src/mutation-id.js.map +1 -1
  794. package/out/zero-protocol/src/mutation-type-enum.js +7 -7
  795. package/out/zero-protocol/src/mutation-type-enum.js.map +1 -1
  796. package/out/zero-protocol/src/mutations-patch.js +21 -16
  797. package/out/zero-protocol/src/mutations-patch.js.map +1 -1
  798. package/out/zero-protocol/src/ping.js +8 -9
  799. package/out/zero-protocol/src/ping.js.map +1 -1
  800. package/out/zero-protocol/src/poke.js +53 -59
  801. package/out/zero-protocol/src/poke.js.map +1 -1
  802. package/out/zero-protocol/src/pong.js +8 -9
  803. package/out/zero-protocol/src/pong.js.map +1 -1
  804. package/out/zero-protocol/src/primary-key.js +9 -19
  805. package/out/zero-protocol/src/primary-key.js.map +1 -1
  806. package/out/zero-protocol/src/protocol-version.js +5 -11
  807. package/out/zero-protocol/src/protocol-version.js.map +1 -1
  808. package/out/zero-protocol/src/pull.js +16 -28
  809. package/out/zero-protocol/src/pull.js.map +1 -1
  810. package/out/zero-protocol/src/push.js +162 -209
  811. package/out/zero-protocol/src/push.js.map +1 -1
  812. package/out/zero-protocol/src/queries-patch.js +22 -30
  813. package/out/zero-protocol/src/queries-patch.js.map +1 -1
  814. package/out/zero-protocol/src/query-hash.js +14 -17
  815. package/out/zero-protocol/src/query-hash.js.map +1 -1
  816. package/out/zero-protocol/src/row-patch.js +23 -30
  817. package/out/zero-protocol/src/row-patch.js.map +1 -1
  818. package/out/zero-protocol/src/up.js +11 -22
  819. package/out/zero-protocol/src/up.js.map +1 -1
  820. package/out/zero-protocol/src/update-auth.js +8 -13
  821. package/out/zero-protocol/src/update-auth.js.map +1 -1
  822. package/out/zero-protocol/src/version.js +8 -9
  823. package/out/zero-protocol/src/version.js.map +1 -1
  824. package/out/zero-react/src/bindings.js +12 -0
  825. package/out/zero-react/src/mod.js +5 -0
  826. package/out/zero-react/src/use-connection-state.js +14 -11
  827. package/out/zero-react/src/use-connection-state.js.map +1 -1
  828. package/out/zero-react/src/use-query.js +283 -281
  829. package/out/zero-react/src/use-query.js.map +1 -1
  830. package/out/zero-react/src/use-zero-online.js +17 -11
  831. package/out/zero-react/src/use-zero-online.js.map +1 -1
  832. package/out/zero-react/src/zero-provider.js +53 -69
  833. package/out/zero-react/src/zero-provider.js.map +1 -1
  834. package/out/zero-react/src/zero.js +22 -0
  835. package/out/zero-schema/src/builder/relationship-builder.js +25 -21
  836. package/out/zero-schema/src/builder/relationship-builder.js.map +1 -1
  837. package/out/zero-schema/src/builder/schema-builder.js +51 -79
  838. package/out/zero-schema/src/builder/schema-builder.js.map +1 -1
  839. package/out/zero-schema/src/builder/table-builder.js +99 -116
  840. package/out/zero-schema/src/builder/table-builder.js.map +1 -1
  841. package/out/zero-schema/src/compiled-permissions.js +21 -25
  842. package/out/zero-schema/src/compiled-permissions.js.map +1 -1
  843. package/out/zero-schema/src/name-mapper.js +31 -47
  844. package/out/zero-schema/src/name-mapper.js.map +1 -1
  845. package/out/zero-schema/src/permissions.js +94 -181
  846. package/out/zero-schema/src/permissions.js.map +1 -1
  847. package/out/zero-schema/src/schema-config.js +26 -32
  848. package/out/zero-schema/src/schema-config.js.map +1 -1
  849. package/out/zero-server/src/adapters/drizzle.d.ts.map +1 -1
  850. package/out/zero-server/src/adapters/drizzle.js +79 -76
  851. package/out/zero-server/src/adapters/drizzle.js.map +1 -1
  852. package/out/zero-server/src/adapters/pg.d.ts.map +1 -1
  853. package/out/zero-server/src/adapters/pg.js +79 -55
  854. package/out/zero-server/src/adapters/pg.js.map +1 -1
  855. package/out/zero-server/src/adapters/postgresjs.d.ts.map +1 -1
  856. package/out/zero-server/src/adapters/postgresjs.js +66 -40
  857. package/out/zero-server/src/adapters/postgresjs.js.map +1 -1
  858. package/out/zero-server/src/adapters/prisma.d.ts.map +1 -1
  859. package/out/zero-server/src/adapters/prisma.js +75 -55
  860. package/out/zero-server/src/adapters/prisma.js.map +1 -1
  861. package/out/zero-server/src/custom.d.ts.map +1 -1
  862. package/out/zero-server/src/custom.js +188 -265
  863. package/out/zero-server/src/custom.js.map +1 -1
  864. package/out/zero-server/src/logging.js +6 -5
  865. package/out/zero-server/src/logging.js.map +1 -1
  866. package/out/zero-server/src/mod.js +8 -0
  867. package/out/zero-server/src/pg-query-executor.js +14 -17
  868. package/out/zero-server/src/pg-query-executor.js.map +1 -1
  869. package/out/zero-server/src/process-mutations.js +293 -365
  870. package/out/zero-server/src/process-mutations.js.map +1 -1
  871. package/out/zero-server/src/push-processor.js +33 -49
  872. package/out/zero-server/src/push-processor.js.map +1 -1
  873. package/out/zero-server/src/queries/process-queries.js +106 -96
  874. package/out/zero-server/src/queries/process-queries.js.map +1 -1
  875. package/out/zero-server/src/schema.js +98 -144
  876. package/out/zero-server/src/schema.js.map +1 -1
  877. package/out/zero-server/src/zql-database.d.ts.map +1 -1
  878. package/out/zero-server/src/zql-database.js +54 -69
  879. package/out/zero-server/src/zql-database.js.map +1 -1
  880. package/out/zero-solid/src/bindings.js +12 -0
  881. package/out/zero-solid/src/mod.js +5 -0
  882. package/out/zero-solid/src/solid-view.js +135 -227
  883. package/out/zero-solid/src/solid-view.js.map +1 -1
  884. package/out/zero-solid/src/use-connection-state.js +18 -14
  885. package/out/zero-solid/src/use-connection-state.js.map +1 -1
  886. package/out/zero-solid/src/use-query.js +55 -100
  887. package/out/zero-solid/src/use-query.js.map +1 -1
  888. package/out/zero-solid/src/use-zero-online.js +18 -12
  889. package/out/zero-solid/src/use-zero-online.js.map +1 -1
  890. package/out/zero-solid/src/use-zero.js +65 -77
  891. package/out/zero-solid/src/use-zero.js.map +1 -1
  892. package/out/zero-solid/src/zero.js +22 -0
  893. package/out/zero-types/src/format.js +8 -7
  894. package/out/zero-types/src/format.js.map +1 -1
  895. package/out/zero-types/src/name-mapper.js +34 -47
  896. package/out/zero-types/src/name-mapper.js.map +1 -1
  897. package/out/zql/src/builder/builder.d.ts.map +1 -1
  898. package/out/zql/src/builder/builder.js +315 -476
  899. package/out/zql/src/builder/builder.js.map +1 -1
  900. package/out/zql/src/builder/debug-delegate.js +69 -74
  901. package/out/zql/src/builder/debug-delegate.js.map +1 -1
  902. package/out/zql/src/builder/filter.js +116 -140
  903. package/out/zql/src/builder/filter.js.map +1 -1
  904. package/out/zql/src/builder/like.js +41 -46
  905. package/out/zql/src/builder/like.js.map +1 -1
  906. package/out/zql/src/error.js +10 -9
  907. package/out/zql/src/error.js.map +1 -1
  908. package/out/zql/src/ivm/array-view.js +89 -91
  909. package/out/zql/src/ivm/array-view.js.map +1 -1
  910. package/out/zql/src/ivm/constraint.js +65 -74
  911. package/out/zql/src/ivm/constraint.js.map +1 -1
  912. package/out/zql/src/ivm/data.js +61 -48
  913. package/out/zql/src/ivm/data.js.map +1 -1
  914. package/out/zql/src/ivm/exists.js +164 -213
  915. package/out/zql/src/ivm/exists.js.map +1 -1
  916. package/out/zql/src/ivm/fan-in.js +62 -59
  917. package/out/zql/src/ivm/fan-in.js.map +1 -1
  918. package/out/zql/src/ivm/fan-out.js +52 -61
  919. package/out/zql/src/ivm/fan-out.js.map +1 -1
  920. package/out/zql/src/ivm/filter-operators.js +91 -96
  921. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  922. package/out/zql/src/ivm/filter-push.js +22 -26
  923. package/out/zql/src/ivm/filter-push.js.map +1 -1
  924. package/out/zql/src/ivm/filter.js +41 -35
  925. package/out/zql/src/ivm/filter.js.map +1 -1
  926. package/out/zql/src/ivm/flipped-join.js +282 -391
  927. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  928. package/out/zql/src/ivm/join-utils.js +85 -115
  929. package/out/zql/src/ivm/join-utils.js.map +1 -1
  930. package/out/zql/src/ivm/join.js +162 -231
  931. package/out/zql/src/ivm/join.js.map +1 -1
  932. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +21 -25
  933. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js.map +1 -1
  934. package/out/zql/src/ivm/memory-source.js +364 -503
  935. package/out/zql/src/ivm/memory-source.js.map +1 -1
  936. package/out/zql/src/ivm/memory-storage.js +33 -34
  937. package/out/zql/src/ivm/memory-storage.js.map +1 -1
  938. package/out/zql/src/ivm/operator.js +13 -15
  939. package/out/zql/src/ivm/operator.js.map +1 -1
  940. package/out/zql/src/ivm/push-accumulated.js +267 -270
  941. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  942. package/out/zql/src/ivm/skip.js +91 -104
  943. package/out/zql/src/ivm/skip.js.map +1 -1
  944. package/out/zql/src/ivm/stream.js +10 -10
  945. package/out/zql/src/ivm/stream.js.map +1 -1
  946. package/out/zql/src/ivm/take.js +422 -569
  947. package/out/zql/src/ivm/take.js.map +1 -1
  948. package/out/zql/src/ivm/union-fan-in.js +157 -231
  949. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  950. package/out/zql/src/ivm/union-fan-out.js +38 -43
  951. package/out/zql/src/ivm/union-fan-out.js.map +1 -1
  952. package/out/zql/src/ivm/view-apply-change.js +166 -255
  953. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  954. package/out/zql/src/mutate/crud.js +35 -34
  955. package/out/zql/src/mutate/crud.js.map +1 -1
  956. package/out/zql/src/mutate/custom.d.ts.map +1 -1
  957. package/out/zql/src/mutate/custom.js +7 -11
  958. package/out/zql/src/mutate/custom.js.map +1 -1
  959. package/out/zql/src/mutate/mutator-registry.js +67 -71
  960. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  961. package/out/zql/src/mutate/mutator.js +26 -25
  962. package/out/zql/src/mutate/mutator.js.map +1 -1
  963. package/out/zql/src/planner/planner-builder.js +134 -239
  964. package/out/zql/src/planner/planner-builder.js.map +1 -1
  965. package/out/zql/src/planner/planner-connection.js +222 -212
  966. package/out/zql/src/planner/planner-connection.js.map +1 -1
  967. package/out/zql/src/planner/planner-constraint.js +15 -7
  968. package/out/zql/src/planner/planner-constraint.js.map +1 -1
  969. package/out/zql/src/planner/planner-debug.js +199 -224
  970. package/out/zql/src/planner/planner-debug.js.map +1 -1
  971. package/out/zql/src/planner/planner-fan-in.js +146 -162
  972. package/out/zql/src/planner/planner-fan-in.js.map +1 -1
  973. package/out/zql/src/planner/planner-fan-out.js +62 -74
  974. package/out/zql/src/planner/planner-fan-out.js.map +1 -1
  975. package/out/zql/src/planner/planner-graph.js +302 -334
  976. package/out/zql/src/planner/planner-graph.js.map +1 -1
  977. package/out/zql/src/planner/planner-join.js +255 -240
  978. package/out/zql/src/planner/planner-join.js.map +1 -1
  979. package/out/zql/src/planner/planner-node.js +10 -6
  980. package/out/zql/src/planner/planner-node.js.map +1 -1
  981. package/out/zql/src/planner/planner-source.js +15 -22
  982. package/out/zql/src/planner/planner-source.js.map +1 -1
  983. package/out/zql/src/planner/planner-terminus.js +28 -28
  984. package/out/zql/src/planner/planner-terminus.js.map +1 -1
  985. package/out/zql/src/query/complete-ordering.js +37 -61
  986. package/out/zql/src/query/complete-ordering.js.map +1 -1
  987. package/out/zql/src/query/create-builder.js +14 -22
  988. package/out/zql/src/query/create-builder.js.map +1 -1
  989. package/out/zql/src/query/error.js +10 -12
  990. package/out/zql/src/query/error.js.map +1 -1
  991. package/out/zql/src/query/escape-like.js +6 -5
  992. package/out/zql/src/query/escape-like.js.map +1 -1
  993. package/out/zql/src/query/expression.js +138 -157
  994. package/out/zql/src/query/expression.js.map +1 -1
  995. package/out/zql/src/query/measure-push-operator.js +35 -38
  996. package/out/zql/src/query/measure-push-operator.js.map +1 -1
  997. package/out/zql/src/query/metrics-delegate.js +7 -7
  998. package/out/zql/src/query/metrics-delegate.js.map +1 -1
  999. package/out/zql/src/query/named.js +52 -51
  1000. package/out/zql/src/query/named.js.map +1 -1
  1001. package/out/zql/src/query/query-delegate-base.js +190 -238
  1002. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  1003. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  1004. package/out/zql/src/query/query-impl.js +271 -405
  1005. package/out/zql/src/query/query-impl.js.map +1 -1
  1006. package/out/zql/src/query/query-internals.js +16 -8
  1007. package/out/zql/src/query/query-internals.js.map +1 -1
  1008. package/out/zql/src/query/query-registry.js +83 -98
  1009. package/out/zql/src/query/query-registry.js.map +1 -1
  1010. package/out/zql/src/query/query.d.ts.map +1 -1
  1011. package/out/zql/src/query/query.js +2 -0
  1012. package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
  1013. package/out/zql/src/query/runnable-query-impl.js +30 -55
  1014. package/out/zql/src/query/runnable-query-impl.js.map +1 -1
  1015. package/out/zql/src/query/static-query.js +7 -14
  1016. package/out/zql/src/query/static-query.js.map +1 -1
  1017. package/out/zql/src/query/ttl.js +45 -67
  1018. package/out/zql/src/query/ttl.js.map +1 -1
  1019. package/out/zql/src/query/validate-input.js +23 -20
  1020. package/out/zql/src/query/validate-input.js.map +1 -1
  1021. package/out/zqlite/src/database-storage.js +99 -103
  1022. package/out/zqlite/src/database-storage.js.map +1 -1
  1023. package/out/zqlite/src/db.js +206 -249
  1024. package/out/zqlite/src/db.js.map +1 -1
  1025. package/out/zqlite/src/explain-queries.js +11 -13
  1026. package/out/zqlite/src/explain-queries.js.map +1 -1
  1027. package/out/zqlite/src/internal/sql-inline.js +54 -37
  1028. package/out/zqlite/src/internal/sql-inline.js.map +1 -1
  1029. package/out/zqlite/src/internal/sql.js +17 -15
  1030. package/out/zqlite/src/internal/sql.js.map +1 -1
  1031. package/out/zqlite/src/internal/statement-cache.js +117 -92
  1032. package/out/zqlite/src/internal/statement-cache.js.map +1 -1
  1033. package/out/zqlite/src/mod.js +5 -0
  1034. package/out/zqlite/src/query-builder.js +81 -172
  1035. package/out/zqlite/src/query-builder.js.map +1 -1
  1036. package/out/zqlite/src/query-delegate.js +45 -55
  1037. package/out/zqlite/src/query-delegate.js.map +1 -1
  1038. package/out/zqlite/src/resolve-scalar-subqueries.js +134 -124
  1039. package/out/zqlite/src/resolve-scalar-subqueries.js.map +1 -1
  1040. package/out/zqlite/src/sqlite-cost-model.js +92 -97
  1041. package/out/zqlite/src/sqlite-cost-model.js.map +1 -1
  1042. package/out/zqlite/src/sqlite-stat-fanout.js +304 -286
  1043. package/out/zqlite/src/sqlite-stat-fanout.js.map +1 -1
  1044. package/out/zqlite/src/table-source.js +281 -455
  1045. package/out/zqlite/src/table-source.js.map +1 -1
  1046. package/package.json +8 -7
  1047. package/out/replicache/src/db/index-operation-enum.js +0 -7
  1048. package/out/replicache/src/db/index-operation-enum.js.map +0 -1
  1049. package/out/replicache/src/db/meta-type-enum.js +0 -7
  1050. package/out/replicache/src/db/meta-type-enum.js.map +0 -1
  1051. package/out/replicache/src/format-version-enum.js +0 -11
  1052. package/out/replicache/src/format-version-enum.js.map +0 -1
  1053. package/out/replicache/src/http-status-unauthorized.js +0 -5
  1054. package/out/replicache/src/http-status-unauthorized.js.map +0 -1
  1055. package/out/replicache/src/invoke-kind-enum.js +0 -7
  1056. package/out/replicache/src/invoke-kind-enum.js.map +0 -1
  1057. package/out/replicache/src/sync/handle-pull-response-result-type-enum.js +0 -9
  1058. package/out/replicache/src/sync/handle-pull-response-result-type-enum.js.map +0 -1
  1059. package/out/zero/package.json.js +0 -9
  1060. package/out/zero/package.json.js.map +0 -1
  1061. package/out/zero/src/adapters/drizzle.js.map +0 -1
  1062. package/out/zero/src/adapters/pg.js.map +0 -1
  1063. package/out/zero/src/adapters/postgresjs.js.map +0 -1
  1064. package/out/zero/src/adapters/prisma.js.map +0 -1
  1065. package/out/zero/src/analyze-query.js.map +0 -1
  1066. package/out/zero/src/ast-to-zql.js.map +0 -1
  1067. package/out/zero/src/bindings.js.map +0 -1
  1068. package/out/zero/src/change-protocol/v0.js.map +0 -1
  1069. package/out/zero/src/cli.js.map +0 -1
  1070. package/out/zero/src/deploy-permissions.js.map +0 -1
  1071. package/out/zero/src/expo-sqlite.js.map +0 -1
  1072. package/out/zero/src/op-sqlite.js.map +0 -1
  1073. package/out/zero/src/pg.js.map +0 -1
  1074. package/out/zero/src/react.js.map +0 -1
  1075. package/out/zero/src/server.js.map +0 -1
  1076. package/out/zero/src/solid.js.map +0 -1
  1077. package/out/zero/src/sqlite.js.map +0 -1
  1078. package/out/zero/src/transform-query.js.map +0 -1
  1079. package/out/zero/src/zero.js.map +0 -1
  1080. package/out/zero/src/zqlite.js.map +0 -1
  1081. package/out/zero-cache/src/db/postgres-replica-identity-enum.js +0 -11
  1082. package/out/zero-cache/src/db/postgres-replica-identity-enum.js.map +0 -1
  1083. package/out/zero-cache/src/db/postgres-type-class-enum.js +0 -17
  1084. package/out/zero-cache/src/db/postgres-type-class-enum.js.map +0 -1
  1085. package/out/zero-cache/src/services/change-streamer/error-type-enum.js +0 -9
  1086. package/out/zero-cache/src/services/change-streamer/error-type-enum.js.map +0 -1
@@ -1,758 +1,650 @@
1
- import { startSpan, startAsyncSpan } from "../../../../otel/src/span.js";
2
1
  import { assert } from "../../../../shared/src/asserts.js";
3
- import "../../../../shared/src/bigint-json.js";
4
- import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
5
2
  import { deepEqual } from "../../../../shared/src/json.js";
6
- import { must } from "../../../../shared/src/must.js";
7
- import { union, intersection, difference } from "../../../../shared/src/set-utils.js";
8
3
  import { stringCompare } from "../../../../shared/src/string-compare.js";
4
+ import { must } from "../../../../shared/src/must.js";
9
5
  import { ZeroCache } from "../../../../zero-protocol/src/error-origin-enum.js";
10
6
  import { ProtocolError } from "../../../../zero-protocol/src/error.js";
11
- import { DEFAULT_TTL_MS, compareTTL, clampTTL } from "../../../../zql/src/query/ttl.js";
12
- import { recordQuery } from "../../server/anonymous-otel-start.js";
13
- import { rowIDString } from "../../types/row-key.js";
7
+ import { DEFAULT_TTL_MS, clampTTL, compareTTL } from "../../../../zql/src/query/ttl.js";
8
+ import { difference, intersection, union } from "../../../../shared/src/set-utils.js";
9
+ import "../../../../shared/src/bigint-json.js";
10
+ import { startAsyncSpan, startSpan } from "../../../../otel/src/span.js";
14
11
  import { upstreamSchema } from "../../types/shards.js";
15
- import "./cvr-store.js";
12
+ import { rowIDString } from "../../types/row-key.js";
13
+ import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
14
+ import { recordQuery } from "../../server/anonymous-otel-start.js";
15
+ import { ttlClockAsNumber } from "./ttl-clock.js";
16
16
  import { cmpVersions, maxVersion, oneAfter } from "./schema/types.js";
17
+ import "./cvr-store.js";
17
18
  import { tracer } from "./tracer.js";
18
- import { ttlClockAsNumber } from "./ttl-clock.js";
19
- const CLIENT_LMID_QUERY_ID = "lmids";
20
- const CLIENT_MUTATION_RESULTS_QUERY_ID = "mutationResults";
19
+ //#region ../zero-cache/src/services/view-syncer/cvr.ts
20
+ var CLIENT_LMID_QUERY_ID = "lmids";
21
+ var CLIENT_MUTATION_RESULTS_QUERY_ID = "mutationResults";
21
22
  function assertNotInternal(query) {
22
- if (query.type === "internal") {
23
- throw new Error(`Query ID ${query.id} is reserved for internal use`);
24
- }
25
- }
26
- function getMutationResultsQuery(upstreamSchema2, clientGroupID) {
27
- return {
28
- id: CLIENT_MUTATION_RESULTS_QUERY_ID,
29
- type: "internal",
30
- ast: {
31
- schema: "",
32
- table: `${upstreamSchema2}.mutations`,
33
- where: {
34
- type: "and",
35
- conditions: [
36
- {
37
- type: "simple",
38
- left: {
39
- type: "column",
40
- name: "clientGroupID"
41
- },
42
- op: "=",
43
- right: {
44
- type: "literal",
45
- value: clientGroupID
46
- }
47
- }
48
- ]
49
- },
50
- orderBy: [
51
- ["clientGroupID", "asc"],
52
- ["clientID", "asc"],
53
- ["mutationID", "asc"]
54
- ]
55
- }
56
- };
57
- }
58
- class CVRUpdater {
59
- _orig;
60
- _cvr;
61
- _cvrStore;
62
- /**
63
- * @param cvrStore The CVRStore to use for storage
64
- * @param cvr The current CVR
65
- */
66
- constructor(cvrStore, cvr, replicaVersion) {
67
- this._cvrStore = cvrStore;
68
- this._orig = cvr;
69
- this._cvr = structuredClone(cvr);
70
- this._cvr.replicaVersion = replicaVersion;
71
- }
72
- _setVersion(version) {
73
- assert(
74
- cmpVersions(this._cvr.version, version) < 0,
75
- "Expected new version to be greater than current version"
76
- );
77
- this._cvr.version = version;
78
- return version;
79
- }
80
- /**
81
- * Ensures that the new CVR has a higher version than the original.
82
- * This method is idempotent in that it will always return the same
83
- * (possibly bumped) version.
84
- */
85
- _ensureNewVersion() {
86
- if (cmpVersions(this._orig.version, this._cvr.version) === 0) {
87
- this._setVersion(oneAfter(this._cvr.version));
88
- }
89
- return this._cvr.version;
90
- }
91
- async flush(lc, lastConnectTime, lastActive, ttlClock) {
92
- this._cvr.ttlClock = ttlClock;
93
- this._cvr.lastActive = lastActive;
94
- const flushed = await this._cvrStore.flush(
95
- lc,
96
- this._orig.version,
97
- this._cvr,
98
- lastConnectTime
99
- );
100
- if (!flushed) {
101
- return { cvr: this._orig, flushed: false };
102
- }
103
- return { cvr: this._cvr, flushed };
104
- }
23
+ if (query.type === "internal") throw new Error(`Query ID ${query.id} is reserved for internal use`);
105
24
  }
106
- class CVRConfigDrivenUpdater extends CVRUpdater {
107
- #shard;
108
- constructor(cvrStore, cvr, shard) {
109
- super(cvrStore, cvr, cvr.replicaVersion);
110
- this.#shard = shard;
111
- }
112
- ensureClient(id) {
113
- return startSpan(tracer, "CVRConfigDrivenUpdater.ensureClient", () => {
114
- let client = this._cvr.clients[id];
115
- if (client) {
116
- return client;
117
- }
118
- client = { id, desiredQueryIDs: [] };
119
- this._cvr.clients[id] = client;
120
- this._ensureNewVersion();
121
- this._cvrStore.insertClient(client);
122
- if (!this._cvr.queries[CLIENT_LMID_QUERY_ID]) {
123
- const lmidsQuery = {
124
- id: CLIENT_LMID_QUERY_ID,
125
- ast: {
126
- schema: "",
127
- table: `${upstreamSchema(this.#shard)}.clients`,
128
- where: {
129
- type: "simple",
130
- left: {
131
- type: "column",
132
- name: "clientGroupID"
133
- },
134
- op: "=",
135
- right: {
136
- type: "literal",
137
- value: this._cvr.id
138
- }
139
- },
140
- orderBy: [
141
- ["clientGroupID", "asc"],
142
- ["clientID", "asc"]
143
- ]
144
- },
145
- type: "internal"
146
- };
147
- this._cvr.queries[CLIENT_LMID_QUERY_ID] = lmidsQuery;
148
- this._cvrStore.putQuery(lmidsQuery);
149
- }
150
- if (!this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID]) {
151
- const mutationResultsQuery = getMutationResultsQuery(upstreamSchema(this.#shard), this._cvr.id);
152
- this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID] = mutationResultsQuery;
153
- this._cvrStore.putQuery(mutationResultsQuery);
154
- }
155
- return client;
156
- });
157
- }
158
- setClientSchema(lc, clientSchema) {
159
- startSpan(tracer, "CVRConfigDrivenUpdater.setClientSchema", () => {
160
- if (this._cvr.clientSchema === null) {
161
- this._cvr.clientSchema = clientSchema;
162
- this._cvrStore.putInstance(this._cvr);
163
- } else if (!deepEqual(this._cvr.clientSchema, clientSchema)) {
164
- lc.warn?.(
165
- `New schema ${JSON.stringify(
166
- clientSchema
167
- )} does not match existing schema ${JSON.stringify(
168
- this._cvr.clientSchema
169
- )}`
170
- );
171
- throw new ProtocolError({
172
- kind: "InvalidConnectionRequest",
173
- message: `Provided schema does not match previous schema`,
174
- origin: ZeroCache
175
- });
176
- }
177
- });
178
- }
179
- setProfileID(lc, profileID) {
180
- if (this._cvr.profileID !== profileID) {
181
- if (this._cvr.profileID !== null && !this._cvr.profileID.startsWith("cg")) {
182
- lc.warn?.(
183
- `changing profile ID from ${this._cvr.profileID} to ${profileID}`
184
- );
185
- }
186
- this._cvr.profileID = profileID;
187
- this._cvrStore.putInstance(this._cvr);
188
- }
189
- }
190
- putDesiredQueries(clientID, queries) {
191
- return startSpan(tracer, "CVRConfigDrivenUpdater.putDesiredQueries", () => {
192
- const patches = [];
193
- const client = this.ensureClient(clientID);
194
- const current = new Set(client.desiredQueryIDs);
195
- const needed = /* @__PURE__ */ new Set();
196
- const recordQueryForTelemetry = (q) => {
197
- const { ast, name, args } = q;
198
- if (ast) {
199
- recordQuery("crud");
200
- } else if (name && args) {
201
- recordQuery("custom");
202
- }
203
- };
204
- for (const q of queries) {
205
- const { hash, ttl = DEFAULT_TTL_MS } = q;
206
- const query = this._cvr.queries[hash];
207
- if (!query) {
208
- recordQueryForTelemetry(q);
209
- needed.add(hash);
210
- continue;
211
- }
212
- if (query.type === "internal") {
213
- continue;
214
- }
215
- const oldClientState = query.clientState[clientID];
216
- if (!oldClientState || oldClientState.inactivatedAt !== void 0) {
217
- recordQueryForTelemetry(q);
218
- needed.add(hash);
219
- continue;
220
- }
221
- if (compareTTL(ttl, oldClientState.ttl) > 0) {
222
- needed.add(hash);
223
- }
224
- }
225
- if (needed.size === 0) {
226
- return patches;
227
- }
228
- const newVersion = this._ensureNewVersion();
229
- client.desiredQueryIDs = [...union(current, needed)].sort(stringCompare);
230
- for (const id of needed) {
231
- const q = must(queries.find(({ hash }) => hash === id));
232
- const { ast, name, args } = q;
233
- const ttl = clampTTL(q.ttl ?? DEFAULT_TTL_MS);
234
- const query = this._cvr.queries[id] ?? newQueryRecord(id, ast, name, args);
235
- assertNotInternal(query);
236
- const inactivatedAt = void 0;
237
- query.clientState[clientID] = {
238
- inactivatedAt,
239
- ttl,
240
- version: newVersion
241
- };
242
- this._cvr.queries[id] = query;
243
- patches.push({
244
- toVersion: newVersion,
245
- patch: { type: "query", op: "put", id, clientID }
246
- });
247
- this._cvrStore.putQuery(query);
248
- this._cvrStore.putDesiredQuery(
249
- newVersion,
250
- query,
251
- client,
252
- false,
253
- inactivatedAt,
254
- ttl
255
- );
256
- }
257
- return patches;
258
- });
259
- }
260
- markDesiredQueriesAsInactive(clientID, queryHashes, ttlClock) {
261
- return this.#deleteQueries(clientID, queryHashes, ttlClock);
262
- }
263
- deleteDesiredQueries(clientID, queryHashes) {
264
- return this.#deleteQueries(clientID, queryHashes, void 0);
265
- }
266
- #deleteQueries(clientID, queryHashes, inactivatedAt) {
267
- return startSpan(tracer, "CVRConfigDrivenUpdater.#deleteQueries", () => {
268
- const patches = [];
269
- const client = this.ensureClient(clientID);
270
- const current = new Set(client.desiredQueryIDs);
271
- const unwanted = new Set(queryHashes);
272
- const remove = intersection(unwanted, current);
273
- if (remove.size === 0) {
274
- return patches;
275
- }
276
- const newVersion = this._ensureNewVersion();
277
- client.desiredQueryIDs = [...difference(current, remove)].sort(
278
- stringCompare
279
- );
280
- for (const id of remove) {
281
- const query = this._cvr.queries[id];
282
- if (!query) {
283
- continue;
284
- }
285
- assertNotInternal(query);
286
- let ttl = DEFAULT_TTL_MS;
287
- if (inactivatedAt === void 0) {
288
- delete query.clientState[clientID];
289
- } else {
290
- const clientState = query.clientState[clientID];
291
- if (clientState !== void 0) {
292
- assert(
293
- clientState.inactivatedAt === void 0,
294
- `Query ${id} is already inactivated`
295
- );
296
- ttl = clampTTL(clientState.ttl);
297
- query.clientState[clientID] = {
298
- inactivatedAt,
299
- ttl,
300
- version: newVersion
301
- };
302
- }
303
- }
304
- this._cvrStore.putQuery(query);
305
- this._cvrStore.putDesiredQuery(
306
- newVersion,
307
- query,
308
- client,
309
- true,
310
- inactivatedAt,
311
- ttl
312
- );
313
- patches.push({
314
- toVersion: newVersion,
315
- patch: { type: "query", op: "del", id, clientID }
316
- });
317
- }
318
- return patches;
319
- });
320
- }
321
- clearDesiredQueries(clientID) {
322
- const client = this.ensureClient(clientID);
323
- return this.#deleteQueries(clientID, client.desiredQueryIDs, void 0);
324
- }
325
- deleteClient(clientID, ttlClock) {
326
- return startSpan(tracer, "CVRConfigDrivenUpdater.deleteClient", () => {
327
- const client = this._cvr.clients[clientID];
328
- if (!client) {
329
- return [];
330
- }
331
- const patches = this.markDesiredQueriesAsInactive(
332
- clientID,
333
- client.desiredQueryIDs,
334
- ttlClock
335
- );
336
- delete this._cvr.clients[clientID];
337
- this._cvrStore.deleteClient(clientID);
338
- return patches;
339
- });
340
- }
341
- }
342
- class CVRQueryDrivenUpdater extends CVRUpdater {
343
- #removedOrExecutedQueryIDs = /* @__PURE__ */ new Set();
344
- #receivedRows = new CustomKeyMap(
345
- rowIDString
346
- );
347
- #lastPatches = new CustomKeyMap(rowIDString);
348
- #existingRows = void 0;
349
- /**
350
- * @param stateVersion The `stateVersion` at which the queries were executed.
351
- */
352
- constructor(cvrStore, cvr, stateVersion, replicaVersion) {
353
- super(cvrStore, cvr, replicaVersion);
354
- assert(
355
- // We should either be setting the cvr.replicaVersion for the first time, or it should
356
- // be something newer than the current cvr.replicaVersion. Otherwise, the CVR should
357
- // have been rejected by the ViewSyncer.
358
- (cvr.replicaVersion ?? replicaVersion) <= replicaVersion,
359
- `Cannot sync from an older replicaVersion: CVR=${cvr.replicaVersion}, DB=${replicaVersion}`
360
- );
361
- assert(
362
- stateVersion >= cvr.version.stateVersion,
363
- () => `stateVersion (${stateVersion}) must be >= cvr.version.stateVersion (${cvr.version.stateVersion})`
364
- );
365
- if (stateVersion > cvr.version.stateVersion) {
366
- this._setVersion({ stateVersion });
367
- }
368
- }
369
- /**
370
- * Initiates the tracking of the specified `executed` and `removed` queries.
371
- * This kicks of a lookup of existing {@link RowRecord}s currently associated
372
- * with those queries, which will be used to reconcile the rows to keep
373
- * after all rows have been {@link received()}.
374
- *
375
- * "transformed" queries are queries that are currently
376
- * gotten and running in the pipeline driver but
377
- * received a new transformation hash due to an auth token
378
- * update.
379
- *
380
- * @returns The new CVRVersion to be used when all changes are committed.
381
- */
382
- trackQueries(lc, executed, removed) {
383
- return startSpan(tracer, "CVRQueryDrivenUpdater.trackQueries", () => {
384
- assert(this.#existingRows === void 0, `trackQueries already called`);
385
- const queryPatches = [
386
- executed.map((q) => this.#trackExecuted(q.id, q.transformationHash)),
387
- removed.map((q) => this.#trackRemoved(q.id))
388
- ].flat(2);
389
- this.#existingRows = this.#lookupRowsForExecutedAndRemovedQueries(lc);
390
- void this.#existingRows.then(() => {
391
- });
392
- return {
393
- newVersion: this._cvr.version,
394
- queryPatches: queryPatches.map((patch) => ({
395
- patch,
396
- toVersion: this._cvr.version
397
- }))
398
- };
399
- });
400
- }
401
- #lookupRowsForExecutedAndRemovedQueries(lc) {
402
- return startAsyncSpan(
403
- tracer,
404
- "CVRQueryDrivenUpdater.#lookupRowsForExecutedAndRemovedQueries",
405
- async () => {
406
- const results = new CustomKeyMap(rowIDString);
407
- if (this.#removedOrExecutedQueryIDs.size === 0) {
408
- return [];
409
- }
410
- const allRowRecords = (await this._cvrStore.getRowRecords()).values();
411
- let total = 0;
412
- for (const existing of allRowRecords) {
413
- total++;
414
- assert(
415
- existing.refCounts !== null,
416
- "allRowRecords should not include null refCounts"
417
- );
418
- for (const id of Object.keys(existing.refCounts)) {
419
- if (this.#removedOrExecutedQueryIDs.has(id)) {
420
- results.set(existing.id, existing);
421
- break;
422
- }
423
- }
424
- }
425
- lc.debug?.(
426
- `found ${results.size} (of ${total}) rows for executed / removed queries ${[
427
- ...this.#removedOrExecutedQueryIDs
428
- ]}`
429
- );
430
- return results.values();
431
- }
432
- );
433
- }
434
- /**
435
- * Tracks an executed query, ensures that it is marked as "gotten",
436
- * updating the CVR and creating put patches if necessary.
437
- *
438
- * This must be called for all executed queries.
439
- */
440
- #trackExecuted(queryID, transformationHash) {
441
- return startSpan(tracer, "CVRQueryDrivenUpdater.#trackExecuted", () => {
442
- assert(
443
- !this.#removedOrExecutedQueryIDs.has(queryID),
444
- () => `Query ${queryID} already tracked as executed or removed`
445
- );
446
- this.#removedOrExecutedQueryIDs.add(queryID);
447
- let gotQueryPatch;
448
- const query = this._cvr.queries[queryID];
449
- if (query.transformationHash !== transformationHash) {
450
- const transformationVersion = this._ensureNewVersion();
451
- if (query.type !== "internal" && query.patchVersion === void 0) {
452
- query.patchVersion = transformationVersion;
453
- gotQueryPatch = {
454
- type: "query",
455
- op: "put",
456
- id: query.id
457
- };
458
- }
459
- query.transformationHash = transformationHash;
460
- query.transformationVersion = transformationVersion;
461
- this._cvrStore.updateQuery(query);
462
- }
463
- return gotQueryPatch ? [gotQueryPatch] : [];
464
- });
465
- }
466
- /**
467
- * Tracks a query removed from the "gotten" set. In addition to producing the
468
- * appropriate patches for deleting the query, the removed query is taken into
469
- * account when computing the final row records in
470
- * {@link deleteUnreferencedRows}.
471
- * Namely, any rows with columns that are no longer referenced by a
472
- * query are deleted.
473
- *
474
- * This must only be called on queries that are not "desired" by any client.
475
- */
476
- #trackRemoved(queryID) {
477
- return startSpan(tracer, "CVRQueryDrivenUpdater.#trackRemoved", () => {
478
- const query = this._cvr.queries[queryID];
479
- assertNotInternal(query);
480
- assert(
481
- !this.#removedOrExecutedQueryIDs.has(queryID),
482
- () => `Query ${queryID} already tracked as executed or removed`
483
- );
484
- this.#removedOrExecutedQueryIDs.add(queryID);
485
- delete this._cvr.queries[queryID];
486
- const newVersion = this._ensureNewVersion();
487
- const queryPatch = { type: "query", op: "del", id: queryID };
488
- this._cvrStore.markQueryAsDeleted(newVersion, queryPatch);
489
- return [queryPatch];
490
- });
491
- }
492
- /**
493
- * Asserts that a new version has already been set.
494
- *
495
- * After {@link #executed} and {@link #removed} are called, we must have properly
496
- * decided on the final CVR version because the poke-start message declares the
497
- * final cookie (i.e. version), and that must be sent before any poke parts
498
- * generated from {@link received} are sent.
499
- */
500
- #assertNewVersion() {
501
- assert(
502
- cmpVersions(this._orig.version, this._cvr.version) < 0,
503
- "Expected CVR version to have been bumped above original"
504
- );
505
- return this._cvr.version;
506
- }
507
- updatedVersion() {
508
- return this._cvr.version;
509
- }
510
- /**
511
- * Tracks rows received from executing queries. This will update row records
512
- * and row patches if the received rows have a new version. The method also
513
- * returns (put) patches to be returned to update their state, versioned by
514
- * patchVersion so that only the patches new to the clients are sent.
515
- */
516
- received(_lc, rows) {
517
- return startAsyncSpan(
518
- tracer,
519
- "CVRQueryDrivenUpdater.received",
520
- async () => {
521
- const patches = [];
522
- const existingRows = await this._cvrStore.getRowRecords();
523
- for (const [id, update] of rows.entries()) {
524
- const { contents, version, refCounts } = update;
525
- let existing = existingRows.get(id);
526
- const previouslyReceived = this.#receivedRows.get(id);
527
- const merged = previouslyReceived !== void 0 ? mergeRefCounts(previouslyReceived, refCounts) : mergeRefCounts(
528
- existing?.refCounts,
529
- refCounts,
530
- this.#removedOrExecutedQueryIDs
531
- );
532
- this.#receivedRows.set(id, merged);
533
- const newRowVersion = merged === null ? void 0 : version;
534
- const patchVersion = existing && existing.rowVersion === newRowVersion ? existing.patchVersion : this.#assertNewVersion();
535
- const rowVersion = version ?? existing?.rowVersion;
536
- if (rowVersion) {
537
- this._cvrStore.putRowRecord({
538
- id,
539
- rowVersion,
540
- patchVersion,
541
- refCounts: merged
542
- });
543
- } else {
544
- this._cvrStore.delRowRecord(id);
545
- }
546
- const lastPatch = this.#lastPatches.get(id);
547
- const toVersion = maxVersion(patchVersion, lastPatch?.toVersion);
548
- if (merged === null) {
549
- if (existing || previouslyReceived) {
550
- if (lastPatch?.rowVersion !== null) {
551
- patches.push({
552
- patch: {
553
- type: "row",
554
- op: "del",
555
- id
556
- },
557
- toVersion
558
- });
559
- this.#lastPatches.set(id, { rowVersion: null, toVersion });
560
- }
561
- }
562
- } else if (contents) {
563
- assert(
564
- rowVersion,
565
- "rowVersion is required when contents is present"
566
- );
567
- if (!lastPatch?.rowVersion || lastPatch.rowVersion < rowVersion) {
568
- patches.push({
569
- patch: {
570
- type: "row",
571
- op: "put",
572
- id,
573
- contents
574
- },
575
- toVersion
576
- });
577
- this.#lastPatches.set(id, { rowVersion, toVersion });
578
- }
579
- }
580
- }
581
- return patches;
582
- }
583
- );
584
- }
585
- /**
586
- * Computes and updates the row records based on:
587
- * * The {@link #executed} queries
588
- * * The {@link #removed} queries
589
- * * The {@link received} rows
590
- *
591
- * Returns the final delete and patch ops that must be sent to the client
592
- * to delete rows that are no longer referenced by any query.
593
- *
594
- * This is Step [5] of the
595
- * [CVR Sync Algorithm](https://www.notion.so/replicache/Sync-and-Client-View-Records-CVR-a18e02ec3ec543449ea22070855ff33d?pvs=4#7874f9b80a514be2b8cd5cf538b88d37).
596
- */
597
- deleteUnreferencedRows(lc) {
598
- return startAsyncSpan(
599
- tracer,
600
- "CVRQueryDrivenUpdater.deleteUnreferencedRows",
601
- async () => {
602
- if (this.#removedOrExecutedQueryIDs.size === 0) {
603
- assert(
604
- this.#receivedRows.size === 0,
605
- () => `Expected no received rows for query-less update, got ${this.#receivedRows.size}`
606
- );
607
- return [];
608
- }
609
- const patches = [];
610
- const start = Date.now();
611
- assert(this.#existingRows, `trackQueries() was not called`);
612
- for (const existing of await this.#existingRows) {
613
- const deletedID = this.#deleteUnreferencedRow(existing);
614
- if (deletedID === null) {
615
- continue;
616
- }
617
- patches.push({
618
- toVersion: this._cvr.version,
619
- patch: { type: "row", op: "del", id: deletedID }
620
- });
621
- }
622
- lc?.debug?.(
623
- `computed ${patches.length} delete patches (${Date.now() - start} ms)`
624
- );
625
- return patches;
626
- }
627
- );
628
- }
629
- #deleteUnreferencedRow(existing) {
630
- return startSpan(
631
- tracer,
632
- "CVRQueryDrivenUpdater.#deleteUnreferencedRow",
633
- () => {
634
- if (this.#receivedRows.get(existing.id)) {
635
- return null;
636
- }
637
- const newRefCounts = mergeRefCounts(
638
- existing.refCounts,
639
- void 0,
640
- this.#removedOrExecutedQueryIDs
641
- );
642
- const patchVersion = newRefCounts ? existing.patchVersion : this.#assertNewVersion();
643
- const rowRecord = {
644
- ...existing,
645
- patchVersion,
646
- refCounts: newRefCounts
647
- };
648
- this._cvrStore.putRowRecord(rowRecord);
649
- return newRefCounts ? null : existing.id;
650
- }
651
- );
652
- }
25
+ function getMutationResultsQuery(upstreamSchema, clientGroupID) {
26
+ return {
27
+ id: CLIENT_MUTATION_RESULTS_QUERY_ID,
28
+ type: "internal",
29
+ ast: {
30
+ schema: "",
31
+ table: `${upstreamSchema}.mutations`,
32
+ where: {
33
+ type: "and",
34
+ conditions: [{
35
+ type: "simple",
36
+ left: {
37
+ type: "column",
38
+ name: "clientGroupID"
39
+ },
40
+ op: "=",
41
+ right: {
42
+ type: "literal",
43
+ value: clientGroupID
44
+ }
45
+ }]
46
+ },
47
+ orderBy: [
48
+ ["clientGroupID", "asc"],
49
+ ["clientID", "asc"],
50
+ ["mutationID", "asc"]
51
+ ]
52
+ }
53
+ };
653
54
  }
55
+ /**
56
+ * The base CVRUpdater contains logic common to the {@link CVRConfigDrivenUpdater} and
57
+ * {@link CVRQueryDrivenUpdater}. The CVRUpdater class itself is exported for updating
58
+ * the `lastActive` time of the CVR in the absence of any changes to the CVR contents.
59
+ * Although activity is automatically tracked when the CVR contents change, there may be
60
+ * edge cases in which a client actively connects to a CVR that doesn't itself change.
61
+ * Calling `new CVRUpdater(...).flush()` will explicitly update the active index and
62
+ * prevent the CVR from being garbage collected.
63
+ */
64
+ var CVRUpdater = class {
65
+ _orig;
66
+ _cvr;
67
+ _cvrStore;
68
+ /**
69
+ * @param cvrStore The CVRStore to use for storage
70
+ * @param cvr The current CVR
71
+ */
72
+ constructor(cvrStore, cvr, replicaVersion) {
73
+ this._cvrStore = cvrStore;
74
+ this._orig = cvr;
75
+ this._cvr = structuredClone(cvr);
76
+ this._cvr.replicaVersion = replicaVersion;
77
+ }
78
+ _setVersion(version) {
79
+ assert(cmpVersions(this._cvr.version, version) < 0, "Expected new version to be greater than current version");
80
+ this._cvr.version = version;
81
+ return version;
82
+ }
83
+ /**
84
+ * Ensures that the new CVR has a higher version than the original.
85
+ * This method is idempotent in that it will always return the same
86
+ * (possibly bumped) version.
87
+ */
88
+ _ensureNewVersion() {
89
+ if (cmpVersions(this._orig.version, this._cvr.version) === 0) this._setVersion(oneAfter(this._cvr.version));
90
+ return this._cvr.version;
91
+ }
92
+ async flush(lc, lastConnectTime, lastActive, ttlClock) {
93
+ this._cvr.ttlClock = ttlClock;
94
+ this._cvr.lastActive = lastActive;
95
+ const flushed = await this._cvrStore.flush(lc, this._orig.version, this._cvr, lastConnectTime);
96
+ if (!flushed) return {
97
+ cvr: this._orig,
98
+ flushed: false
99
+ };
100
+ return {
101
+ cvr: this._cvr,
102
+ flushed
103
+ };
104
+ }
105
+ };
106
+ /**
107
+ * A {@link CVRConfigDrivenUpdater} is used for updating a CVR with config-driven
108
+ * changes. Note that this may result in row deletion (e.g. if queries get dropped),
109
+ * but the `stateVersion` of the CVR does not change.
110
+ */
111
+ var CVRConfigDrivenUpdater = class extends CVRUpdater {
112
+ #shard;
113
+ constructor(cvrStore, cvr, shard) {
114
+ super(cvrStore, cvr, cvr.replicaVersion);
115
+ this.#shard = shard;
116
+ }
117
+ ensureClient(id) {
118
+ return startSpan(tracer, "CVRConfigDrivenUpdater.ensureClient", () => {
119
+ let client = this._cvr.clients[id];
120
+ if (client) return client;
121
+ client = {
122
+ id,
123
+ desiredQueryIDs: []
124
+ };
125
+ this._cvr.clients[id] = client;
126
+ this._ensureNewVersion();
127
+ this._cvrStore.insertClient(client);
128
+ if (!this._cvr.queries[CLIENT_LMID_QUERY_ID]) {
129
+ const lmidsQuery = {
130
+ id: CLIENT_LMID_QUERY_ID,
131
+ ast: {
132
+ schema: "",
133
+ table: `${upstreamSchema(this.#shard)}.clients`,
134
+ where: {
135
+ type: "simple",
136
+ left: {
137
+ type: "column",
138
+ name: "clientGroupID"
139
+ },
140
+ op: "=",
141
+ right: {
142
+ type: "literal",
143
+ value: this._cvr.id
144
+ }
145
+ },
146
+ orderBy: [["clientGroupID", "asc"], ["clientID", "asc"]]
147
+ },
148
+ type: "internal"
149
+ };
150
+ this._cvr.queries[CLIENT_LMID_QUERY_ID] = lmidsQuery;
151
+ this._cvrStore.putQuery(lmidsQuery);
152
+ }
153
+ if (!this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID]) {
154
+ const mutationResultsQuery = getMutationResultsQuery(upstreamSchema(this.#shard), this._cvr.id);
155
+ this._cvr.queries[CLIENT_MUTATION_RESULTS_QUERY_ID] = mutationResultsQuery;
156
+ this._cvrStore.putQuery(mutationResultsQuery);
157
+ }
158
+ return client;
159
+ });
160
+ }
161
+ setClientSchema(lc, clientSchema) {
162
+ startSpan(tracer, "CVRConfigDrivenUpdater.setClientSchema", () => {
163
+ if (this._cvr.clientSchema === null) {
164
+ this._cvr.clientSchema = clientSchema;
165
+ this._cvrStore.putInstance(this._cvr);
166
+ } else if (!deepEqual(this._cvr.clientSchema, clientSchema)) {
167
+ lc.warn?.(`New schema ${JSON.stringify(clientSchema)} does not match existing schema ${JSON.stringify(this._cvr.clientSchema)}`);
168
+ throw new ProtocolError({
169
+ kind: "InvalidConnectionRequest",
170
+ message: `Provided schema does not match previous schema`,
171
+ origin: ZeroCache
172
+ });
173
+ }
174
+ });
175
+ }
176
+ setProfileID(lc, profileID) {
177
+ if (this._cvr.profileID !== profileID) {
178
+ if (this._cvr.profileID !== null && !this._cvr.profileID.startsWith("cg")) lc.warn?.(`changing profile ID from ${this._cvr.profileID} to ${profileID}`);
179
+ this._cvr.profileID = profileID;
180
+ this._cvrStore.putInstance(this._cvr);
181
+ }
182
+ }
183
+ putDesiredQueries(clientID, queries) {
184
+ return startSpan(tracer, "CVRConfigDrivenUpdater.putDesiredQueries", () => {
185
+ const patches = [];
186
+ const client = this.ensureClient(clientID);
187
+ const current = new Set(client.desiredQueryIDs);
188
+ const needed = /* @__PURE__ */ new Set();
189
+ const recordQueryForTelemetry = (q) => {
190
+ const { ast, name, args } = q;
191
+ if (ast) recordQuery("crud");
192
+ else if (name && args) recordQuery("custom");
193
+ };
194
+ for (const q of queries) {
195
+ const { hash, ttl = DEFAULT_TTL_MS } = q;
196
+ const query = this._cvr.queries[hash];
197
+ if (!query) {
198
+ recordQueryForTelemetry(q);
199
+ needed.add(hash);
200
+ continue;
201
+ }
202
+ if (query.type === "internal") continue;
203
+ const oldClientState = query.clientState[clientID];
204
+ if (!oldClientState || oldClientState.inactivatedAt !== void 0) {
205
+ recordQueryForTelemetry(q);
206
+ needed.add(hash);
207
+ continue;
208
+ }
209
+ if (compareTTL(ttl, oldClientState.ttl) > 0) needed.add(hash);
210
+ }
211
+ if (needed.size === 0) return patches;
212
+ const newVersion = this._ensureNewVersion();
213
+ client.desiredQueryIDs = [...union(current, needed)].sort(stringCompare);
214
+ for (const id of needed) {
215
+ const q = must(queries.find(({ hash }) => hash === id));
216
+ const { ast, name, args } = q;
217
+ const ttl = clampTTL(q.ttl ?? 3e5);
218
+ const query = this._cvr.queries[id] ?? newQueryRecord(id, ast, name, args);
219
+ assertNotInternal(query);
220
+ const inactivatedAt = void 0;
221
+ query.clientState[clientID] = {
222
+ inactivatedAt,
223
+ ttl,
224
+ version: newVersion
225
+ };
226
+ this._cvr.queries[id] = query;
227
+ patches.push({
228
+ toVersion: newVersion,
229
+ patch: {
230
+ type: "query",
231
+ op: "put",
232
+ id,
233
+ clientID
234
+ }
235
+ });
236
+ this._cvrStore.putQuery(query);
237
+ this._cvrStore.putDesiredQuery(newVersion, query, client, false, inactivatedAt, ttl);
238
+ }
239
+ return patches;
240
+ });
241
+ }
242
+ markDesiredQueriesAsInactive(clientID, queryHashes, ttlClock) {
243
+ return this.#deleteQueries(clientID, queryHashes, ttlClock);
244
+ }
245
+ deleteDesiredQueries(clientID, queryHashes) {
246
+ return this.#deleteQueries(clientID, queryHashes, void 0);
247
+ }
248
+ #deleteQueries(clientID, queryHashes, inactivatedAt) {
249
+ return startSpan(tracer, "CVRConfigDrivenUpdater.#deleteQueries", () => {
250
+ const patches = [];
251
+ const client = this.ensureClient(clientID);
252
+ const current = new Set(client.desiredQueryIDs);
253
+ const remove = intersection(new Set(queryHashes), current);
254
+ if (remove.size === 0) return patches;
255
+ const newVersion = this._ensureNewVersion();
256
+ client.desiredQueryIDs = [...difference(current, remove)].sort(stringCompare);
257
+ for (const id of remove) {
258
+ const query = this._cvr.queries[id];
259
+ if (!query) continue;
260
+ assertNotInternal(query);
261
+ let ttl = DEFAULT_TTL_MS;
262
+ if (inactivatedAt === void 0) delete query.clientState[clientID];
263
+ else {
264
+ const clientState = query.clientState[clientID];
265
+ if (clientState !== void 0) {
266
+ assert(clientState.inactivatedAt === void 0, `Query ${id} is already inactivated`);
267
+ ttl = clampTTL(clientState.ttl);
268
+ query.clientState[clientID] = {
269
+ inactivatedAt,
270
+ ttl,
271
+ version: newVersion
272
+ };
273
+ }
274
+ }
275
+ this._cvrStore.putQuery(query);
276
+ this._cvrStore.putDesiredQuery(newVersion, query, client, true, inactivatedAt, ttl);
277
+ patches.push({
278
+ toVersion: newVersion,
279
+ patch: {
280
+ type: "query",
281
+ op: "del",
282
+ id,
283
+ clientID
284
+ }
285
+ });
286
+ }
287
+ return patches;
288
+ });
289
+ }
290
+ clearDesiredQueries(clientID) {
291
+ const client = this.ensureClient(clientID);
292
+ return this.#deleteQueries(clientID, client.desiredQueryIDs, void 0);
293
+ }
294
+ deleteClient(clientID, ttlClock) {
295
+ return startSpan(tracer, "CVRConfigDrivenUpdater.deleteClient", () => {
296
+ const client = this._cvr.clients[clientID];
297
+ if (!client) return [];
298
+ const patches = this.markDesiredQueriesAsInactive(clientID, client.desiredQueryIDs, ttlClock);
299
+ delete this._cvr.clients[clientID];
300
+ this._cvrStore.deleteClient(clientID);
301
+ return patches;
302
+ });
303
+ }
304
+ };
305
+ /**
306
+ * A {@link CVRQueryDrivenUpdater} is used for updating a CVR after making queries.
307
+ * The caller should invoke:
308
+ *
309
+ * * {@link trackQueries} for queries that are being executed or removed.
310
+ * * {@link received} for all rows received from the executed queries
311
+ * * {@link deleteUnreferencedRows} to remove any rows that have
312
+ * fallen out of the query result view.
313
+ * * {@link flush}
314
+ *
315
+ * After flushing, the caller should perform any necessary catchup of
316
+ * config and row patches for clients that are behind. See
317
+ * {@link CVRStore.catchupConfigPatches} and {@link CVRStore.catchupRowPatches}.
318
+ */
319
+ var CVRQueryDrivenUpdater = class extends CVRUpdater {
320
+ #removedOrExecutedQueryIDs = /* @__PURE__ */ new Set();
321
+ #receivedRows = new CustomKeyMap(rowIDString);
322
+ #lastPatches = new CustomKeyMap(rowIDString);
323
+ #existingRows = void 0;
324
+ /**
325
+ * @param stateVersion The `stateVersion` at which the queries were executed.
326
+ */
327
+ constructor(cvrStore, cvr, stateVersion, replicaVersion) {
328
+ super(cvrStore, cvr, replicaVersion);
329
+ assert((cvr.replicaVersion ?? replicaVersion) <= replicaVersion, `Cannot sync from an older replicaVersion: CVR=${cvr.replicaVersion}, DB=${replicaVersion}`);
330
+ assert(stateVersion >= cvr.version.stateVersion, () => `stateVersion (${stateVersion}) must be >= cvr.version.stateVersion (${cvr.version.stateVersion})`);
331
+ if (stateVersion > cvr.version.stateVersion) this._setVersion({ stateVersion });
332
+ }
333
+ /**
334
+ * Initiates the tracking of the specified `executed` and `removed` queries.
335
+ * This kicks of a lookup of existing {@link RowRecord}s currently associated
336
+ * with those queries, which will be used to reconcile the rows to keep
337
+ * after all rows have been {@link received()}.
338
+ *
339
+ * "transformed" queries are queries that are currently
340
+ * gotten and running in the pipeline driver but
341
+ * received a new transformation hash due to an auth token
342
+ * update.
343
+ *
344
+ * @returns The new CVRVersion to be used when all changes are committed.
345
+ */
346
+ trackQueries(lc, executed, removed) {
347
+ return startSpan(tracer, "CVRQueryDrivenUpdater.trackQueries", () => {
348
+ assert(this.#existingRows === void 0, `trackQueries already called`);
349
+ const queryPatches = [executed.map((q) => this.#trackExecuted(q.id, q.transformationHash)), removed.map((q) => this.#trackRemoved(q.id))].flat(2);
350
+ this.#existingRows = this.#lookupRowsForExecutedAndRemovedQueries(lc);
351
+ this.#existingRows.then(() => {});
352
+ return {
353
+ newVersion: this._cvr.version,
354
+ queryPatches: queryPatches.map((patch) => ({
355
+ patch,
356
+ toVersion: this._cvr.version
357
+ }))
358
+ };
359
+ });
360
+ }
361
+ #lookupRowsForExecutedAndRemovedQueries(lc) {
362
+ return startAsyncSpan(tracer, "CVRQueryDrivenUpdater.#lookupRowsForExecutedAndRemovedQueries", async () => {
363
+ const results = new CustomKeyMap(rowIDString);
364
+ if (this.#removedOrExecutedQueryIDs.size === 0) return [];
365
+ const allRowRecords = (await this._cvrStore.getRowRecords()).values();
366
+ let total = 0;
367
+ for (const existing of allRowRecords) {
368
+ total++;
369
+ assert(existing.refCounts !== null, "allRowRecords should not include null refCounts");
370
+ for (const id of Object.keys(existing.refCounts)) if (this.#removedOrExecutedQueryIDs.has(id)) {
371
+ results.set(existing.id, existing);
372
+ break;
373
+ }
374
+ }
375
+ lc.debug?.(`found ${results.size} (of ${total}) rows for executed / removed queries ${[...this.#removedOrExecutedQueryIDs]}`);
376
+ return results.values();
377
+ });
378
+ }
379
+ /**
380
+ * Tracks an executed query, ensures that it is marked as "gotten",
381
+ * updating the CVR and creating put patches if necessary.
382
+ *
383
+ * This must be called for all executed queries.
384
+ */
385
+ #trackExecuted(queryID, transformationHash) {
386
+ return startSpan(tracer, "CVRQueryDrivenUpdater.#trackExecuted", () => {
387
+ assert(!this.#removedOrExecutedQueryIDs.has(queryID), () => `Query ${queryID} already tracked as executed or removed`);
388
+ this.#removedOrExecutedQueryIDs.add(queryID);
389
+ let gotQueryPatch;
390
+ const query = this._cvr.queries[queryID];
391
+ if (query.transformationHash !== transformationHash) {
392
+ const transformationVersion = this._ensureNewVersion();
393
+ if (query.type !== "internal" && query.patchVersion === void 0) {
394
+ query.patchVersion = transformationVersion;
395
+ gotQueryPatch = {
396
+ type: "query",
397
+ op: "put",
398
+ id: query.id
399
+ };
400
+ }
401
+ query.transformationHash = transformationHash;
402
+ query.transformationVersion = transformationVersion;
403
+ this._cvrStore.updateQuery(query);
404
+ }
405
+ return gotQueryPatch ? [gotQueryPatch] : [];
406
+ });
407
+ }
408
+ /**
409
+ * Tracks a query removed from the "gotten" set. In addition to producing the
410
+ * appropriate patches for deleting the query, the removed query is taken into
411
+ * account when computing the final row records in
412
+ * {@link deleteUnreferencedRows}.
413
+ * Namely, any rows with columns that are no longer referenced by a
414
+ * query are deleted.
415
+ *
416
+ * This must only be called on queries that are not "desired" by any client.
417
+ */
418
+ #trackRemoved(queryID) {
419
+ return startSpan(tracer, "CVRQueryDrivenUpdater.#trackRemoved", () => {
420
+ const query = this._cvr.queries[queryID];
421
+ assertNotInternal(query);
422
+ assert(!this.#removedOrExecutedQueryIDs.has(queryID), () => `Query ${queryID} already tracked as executed or removed`);
423
+ this.#removedOrExecutedQueryIDs.add(queryID);
424
+ delete this._cvr.queries[queryID];
425
+ const newVersion = this._ensureNewVersion();
426
+ const queryPatch = {
427
+ type: "query",
428
+ op: "del",
429
+ id: queryID
430
+ };
431
+ this._cvrStore.markQueryAsDeleted(newVersion, queryPatch);
432
+ return [queryPatch];
433
+ });
434
+ }
435
+ /**
436
+ * Asserts that a new version has already been set.
437
+ *
438
+ * After {@link #executed} and {@link #removed} are called, we must have properly
439
+ * decided on the final CVR version because the poke-start message declares the
440
+ * final cookie (i.e. version), and that must be sent before any poke parts
441
+ * generated from {@link received} are sent.
442
+ */
443
+ #assertNewVersion() {
444
+ assert(cmpVersions(this._orig.version, this._cvr.version) < 0, "Expected CVR version to have been bumped above original");
445
+ return this._cvr.version;
446
+ }
447
+ updatedVersion() {
448
+ return this._cvr.version;
449
+ }
450
+ /**
451
+ * Tracks rows received from executing queries. This will update row records
452
+ * and row patches if the received rows have a new version. The method also
453
+ * returns (put) patches to be returned to update their state, versioned by
454
+ * patchVersion so that only the patches new to the clients are sent.
455
+ */
456
+ received(_lc, rows) {
457
+ return startAsyncSpan(tracer, "CVRQueryDrivenUpdater.received", async () => {
458
+ const patches = [];
459
+ const existingRows = await this._cvrStore.getRowRecords();
460
+ for (const [id, update] of rows.entries()) {
461
+ const { contents, version, refCounts } = update;
462
+ let existing = existingRows.get(id);
463
+ const previouslyReceived = this.#receivedRows.get(id);
464
+ const merged = previouslyReceived !== void 0 ? mergeRefCounts(previouslyReceived, refCounts) : mergeRefCounts(existing?.refCounts, refCounts, this.#removedOrExecutedQueryIDs);
465
+ this.#receivedRows.set(id, merged);
466
+ const newRowVersion = merged === null ? void 0 : version;
467
+ const patchVersion = existing && existing.rowVersion === newRowVersion ? existing.patchVersion : this.#assertNewVersion();
468
+ const rowVersion = version ?? existing?.rowVersion;
469
+ if (rowVersion) this._cvrStore.putRowRecord({
470
+ id,
471
+ rowVersion,
472
+ patchVersion,
473
+ refCounts: merged
474
+ });
475
+ else this._cvrStore.delRowRecord(id);
476
+ const lastPatch = this.#lastPatches.get(id);
477
+ const toVersion = maxVersion(patchVersion, lastPatch?.toVersion);
478
+ if (merged === null) {
479
+ if (existing || previouslyReceived) {
480
+ if (lastPatch?.rowVersion !== null) {
481
+ patches.push({
482
+ patch: {
483
+ type: "row",
484
+ op: "del",
485
+ id
486
+ },
487
+ toVersion
488
+ });
489
+ this.#lastPatches.set(id, {
490
+ rowVersion: null,
491
+ toVersion
492
+ });
493
+ }
494
+ }
495
+ } else if (contents) {
496
+ assert(rowVersion, "rowVersion is required when contents is present");
497
+ if (!lastPatch?.rowVersion || lastPatch.rowVersion < rowVersion) {
498
+ patches.push({
499
+ patch: {
500
+ type: "row",
501
+ op: "put",
502
+ id,
503
+ contents
504
+ },
505
+ toVersion
506
+ });
507
+ this.#lastPatches.set(id, {
508
+ rowVersion,
509
+ toVersion
510
+ });
511
+ }
512
+ }
513
+ }
514
+ return patches;
515
+ });
516
+ }
517
+ /**
518
+ * Computes and updates the row records based on:
519
+ * * The {@link #executed} queries
520
+ * * The {@link #removed} queries
521
+ * * The {@link received} rows
522
+ *
523
+ * Returns the final delete and patch ops that must be sent to the client
524
+ * to delete rows that are no longer referenced by any query.
525
+ *
526
+ * This is Step [5] of the
527
+ * [CVR Sync Algorithm](https://www.notion.so/replicache/Sync-and-Client-View-Records-CVR-a18e02ec3ec543449ea22070855ff33d?pvs=4#7874f9b80a514be2b8cd5cf538b88d37).
528
+ */
529
+ deleteUnreferencedRows(lc) {
530
+ return startAsyncSpan(tracer, "CVRQueryDrivenUpdater.deleteUnreferencedRows", async () => {
531
+ if (this.#removedOrExecutedQueryIDs.size === 0) {
532
+ assert(this.#receivedRows.size === 0, () => `Expected no received rows for query-less update, got ${this.#receivedRows.size}`);
533
+ return [];
534
+ }
535
+ const patches = [];
536
+ const start = Date.now();
537
+ assert(this.#existingRows, `trackQueries() was not called`);
538
+ for (const existing of await this.#existingRows) {
539
+ const deletedID = this.#deleteUnreferencedRow(existing);
540
+ if (deletedID === null) continue;
541
+ patches.push({
542
+ toVersion: this._cvr.version,
543
+ patch: {
544
+ type: "row",
545
+ op: "del",
546
+ id: deletedID
547
+ }
548
+ });
549
+ }
550
+ lc?.debug?.(`computed ${patches.length} delete patches (${Date.now() - start} ms)`);
551
+ return patches;
552
+ });
553
+ }
554
+ #deleteUnreferencedRow(existing) {
555
+ return startSpan(tracer, "CVRQueryDrivenUpdater.#deleteUnreferencedRow", () => {
556
+ if (this.#receivedRows.get(existing.id)) return null;
557
+ const newRefCounts = mergeRefCounts(existing.refCounts, void 0, this.#removedOrExecutedQueryIDs);
558
+ const patchVersion = newRefCounts ? existing.patchVersion : this.#assertNewVersion();
559
+ const rowRecord = {
560
+ ...existing,
561
+ patchVersion,
562
+ refCounts: newRefCounts
563
+ };
564
+ this._cvrStore.putRowRecord(rowRecord);
565
+ return newRefCounts ? null : existing.id;
566
+ });
567
+ }
568
+ };
654
569
  function mergeRefCounts(existing, received, removeHashes) {
655
- let merged = {};
656
- if (!existing) {
657
- merged = received ?? {};
658
- } else {
659
- [existing, received].forEach((refCounts, i) => {
660
- if (!refCounts) {
661
- return;
662
- }
663
- for (const [hash, count] of Object.entries(refCounts)) {
664
- if (i === 0 && removeHashes?.has(hash)) {
665
- continue;
666
- }
667
- merged[hash] = (merged[hash] ?? 0) + count;
668
- if (merged[hash] === 0) {
669
- delete merged[hash];
670
- }
671
- }
672
- return merged;
673
- });
674
- }
675
- return Object.values(merged).some((v) => v > 0) ? merged : null;
570
+ let merged = {};
571
+ if (!existing) merged = received ?? {};
572
+ else [existing, received].forEach((refCounts, i) => {
573
+ if (!refCounts) return;
574
+ for (const [hash, count] of Object.entries(refCounts)) {
575
+ if (i === 0 && removeHashes?.has(hash)) continue;
576
+ merged[hash] = (merged[hash] ?? 0) + count;
577
+ if (merged[hash] === 0) delete merged[hash];
578
+ }
579
+ return merged;
580
+ });
581
+ return Object.values(merged).some((v) => v > 0) ? merged : null;
676
582
  }
583
+ /**
584
+ * The query must be inactive for all clients to be considered inactive.
585
+ * This is because expiration is defined that way: a query is expired for a client group
586
+ * only if it is expired for all clients in the group.
587
+ *
588
+ * If all clients have inactivated the query, we return
589
+ * the one with the expiration furthest in the future.
590
+ */
677
591
  function getInactiveQueries(cvr) {
678
- const inactive = /* @__PURE__ */ new Map();
679
- for (const [queryID, query] of Object.entries(cvr.queries)) {
680
- if (query.type === "internal") {
681
- continue;
682
- }
683
- for (const clientState of Object.values(query.clientState)) {
684
- const { inactivatedAt, ttl } = clientState;
685
- const existing = inactive.get(queryID);
686
- if (inactivatedAt === void 0) {
687
- if (existing) {
688
- inactive.delete(queryID);
689
- }
690
- break;
691
- }
692
- const clampedTTL = clampTTL(ttl);
693
- if (existing) {
694
- const existingTTL = clampTTL(existing.ttl);
695
- if (existingTTL + ttlClockAsNumber(existing.inactivatedAt) < ttlClockAsNumber(inactivatedAt) + clampedTTL) {
696
- existing.ttl = clampedTTL;
697
- existing.inactivatedAt = inactivatedAt;
698
- }
699
- } else {
700
- inactive.set(queryID, {
701
- hash: queryID,
702
- inactivatedAt,
703
- ttl: clampedTTL
704
- });
705
- }
706
- }
707
- }
708
- return [...inactive.values()].sort((a, b) => {
709
- if (a.ttl === b.ttl) {
710
- return ttlClockAsNumber(a.inactivatedAt) - ttlClockAsNumber(b.inactivatedAt);
711
- }
712
- return ttlClockAsNumber(a.inactivatedAt) + a.ttl - ttlClockAsNumber(b.inactivatedAt) - b.ttl;
713
- });
592
+ const inactive = /* @__PURE__ */ new Map();
593
+ for (const [queryID, query] of Object.entries(cvr.queries)) {
594
+ if (query.type === "internal") continue;
595
+ for (const clientState of Object.values(query.clientState)) {
596
+ const { inactivatedAt, ttl } = clientState;
597
+ const existing = inactive.get(queryID);
598
+ if (inactivatedAt === void 0) {
599
+ if (existing) inactive.delete(queryID);
600
+ break;
601
+ }
602
+ const clampedTTL = clampTTL(ttl);
603
+ if (existing) {
604
+ if (clampTTL(existing.ttl) + ttlClockAsNumber(existing.inactivatedAt) < ttlClockAsNumber(inactivatedAt) + clampedTTL) {
605
+ existing.ttl = clampedTTL;
606
+ existing.inactivatedAt = inactivatedAt;
607
+ }
608
+ } else inactive.set(queryID, {
609
+ hash: queryID,
610
+ inactivatedAt,
611
+ ttl: clampedTTL
612
+ });
613
+ }
614
+ }
615
+ return [...inactive.values()].sort((a, b) => {
616
+ if (a.ttl === b.ttl) return ttlClockAsNumber(a.inactivatedAt) - ttlClockAsNumber(b.inactivatedAt);
617
+ return ttlClockAsNumber(a.inactivatedAt) + a.ttl - ttlClockAsNumber(b.inactivatedAt) - b.ttl;
618
+ });
714
619
  }
715
620
  function nextEvictionTime(cvr) {
716
- let next;
717
- for (const { inactivatedAt, ttl } of getInactiveQueries(cvr)) {
718
- const expire = ttlClockAsNumber(inactivatedAt) + ttl;
719
- if (next === void 0 || expire < next) {
720
- next = expire;
721
- }
722
- }
723
- return next;
621
+ let next;
622
+ for (const { inactivatedAt, ttl } of getInactiveQueries(cvr)) {
623
+ const expire = ttlClockAsNumber(inactivatedAt) + ttl;
624
+ if (next === void 0 || expire < next) next = expire;
625
+ }
626
+ return next;
724
627
  }
725
628
  function newQueryRecord(id, ast, name, args) {
726
- if (ast !== void 0) {
727
- assert(
728
- name === void 0 && args === void 0,
729
- "Cannot provide name or args with ast"
730
- );
731
- return {
732
- id,
733
- type: "client",
734
- ast,
735
- clientState: {}
736
- };
737
- }
738
- assert(
739
- name !== void 0 && args !== void 0,
740
- "Must provide name and args"
741
- );
742
- return {
743
- id,
744
- type: "custom",
745
- name,
746
- args,
747
- clientState: {}
748
- };
629
+ if (ast !== void 0) {
630
+ assert(name === void 0 && args === void 0, "Cannot provide name or args with ast");
631
+ return {
632
+ id,
633
+ type: "client",
634
+ ast,
635
+ clientState: {}
636
+ };
637
+ }
638
+ assert(name !== void 0 && args !== void 0, "Must provide name and args");
639
+ return {
640
+ id,
641
+ type: "custom",
642
+ name,
643
+ args,
644
+ clientState: {}
645
+ };
749
646
  }
750
- export {
751
- CVRConfigDrivenUpdater,
752
- CVRQueryDrivenUpdater,
753
- CVRUpdater,
754
- getInactiveQueries,
755
- getMutationResultsQuery,
756
- nextEvictionTime
757
- };
758
- //# sourceMappingURL=cvr.js.map
647
+ //#endregion
648
+ export { CVRConfigDrivenUpdater, CVRQueryDrivenUpdater, nextEvictionTime };
649
+
650
+ //# sourceMappingURL=cvr.js.map