@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,1627 +1,1091 @@
1
- import { Lock } from "@rocicorp/lock";
2
- import { resolver } from "@rocicorp/resolver";
3
- import { startSpan, startAsyncSpan, manualSpan } from "../../../../otel/src/span.js";
4
1
  import { assert, unreachable } from "../../../../shared/src/asserts.js";
5
- import { stringify } from "../../../../shared/src/bigint-json.js";
6
- import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
7
2
  import { must } from "../../../../shared/src/must.js";
8
- import { randInt } from "../../../../shared/src/rand.js";
9
- import { Rehome, InvalidConnectionRequest, TransformFailed, InvalidConnectionRequestBaseCookie } from "../../../../zero-protocol/src/error-kind-enum.js";
3
+ import { InvalidConnectionRequest, InvalidConnectionRequestBaseCookie, Rehome } from "../../../../zero-protocol/src/error-kind-enum.js";
10
4
  import { ZeroCache } from "../../../../zero-protocol/src/error-origin-enum.js";
11
- import { HTTP } from "../../../../zero-protocol/src/error-reason-enum.js";
5
+ import "../../../../zero-protocol/src/error-reason-enum.js";
12
6
  import { ProtocolError, isProtocolError } from "../../../../zero-protocol/src/error.js";
13
- import { clampTTL, MAX_TTL_MS } from "../../../../zql/src/query/ttl.js";
14
- import { transformAndHashQuery } from "../../auth/read-authorizer.js";
15
- import { getOrCreateUpDownCounter, getOrCreateCounter, getOrCreateHistogram } from "../../observability/metrics.js";
16
- import { ProtocolErrorWithLevel, getLogLevel } from "../../types/error-with-level.js";
7
+ import { MAX_TTL_MS, clampTTL } from "../../../../zql/src/query/ttl.js";
8
+ import { stringify } from "../../../../shared/src/bigint-json.js";
9
+ import { manualSpan, startAsyncSpan, startSpan } from "../../../../otel/src/span.js";
10
+ import { randInt } from "../../../../shared/src/rand.js";
17
11
  import { rowIDString } from "../../types/row-key.js";
18
- import { Subscription } from "../../types/subscription.js";
12
+ import { ZERO_VERSION_COLUMN_NAME } from "../replicator/schema/constants.js";
19
13
  import "../replicator/schema/replication-state.js";
14
+ import { transformAndHashQuery } from "../../auth/read-authorizer.js";
15
+ import { getOrCreateCounter, getOrCreateHistogram, getOrCreateUpDownCounter } from "../../observability/metrics.js";
16
+ import { ResetPipelinesSignal } from "./snapshotter.js";
17
+ import "./pipeline-driver.js";
18
+ import { Subscription } from "../../types/subscription.js";
19
+ import { CustomKeyMap } from "../../../../shared/src/custom-key-map.js";
20
+ import { ttlClockAsNumber, ttlClockFromNumber } from "./ttl-clock.js";
21
+ import { EMPTY_CVR_VERSION, cmpVersions, versionFromString, versionString, versionToCookie } from "./schema/types.js";
22
+ import { ProtocolErrorWithLevel, getLogLevel } from "../../types/error-with-level.js";
20
23
  import { ClientHandler, startPoke } from "./client-handler.js";
21
24
  import { CVRStore, ClientNotFoundError } from "./cvr-store.js";
22
- import { CVRConfigDrivenUpdater, nextEvictionTime, CVRQueryDrivenUpdater } from "./cvr.js";
23
- import { handleInspect } from "./inspect-handler.js";
24
- import "../../../../shared/src/config.js";
25
- import "compare-utf8";
26
- import "../../../../zero-protocol/src/ast.js";
27
- import "../../../../zero-protocol/src/data.js";
28
- import "@rocicorp/zero-sqlite3";
29
- import "../../../../zqlite/src/internal/sql.js";
30
- import "@databases/escape-identifier";
31
- import "../../../../shared/src/btree-set.js";
32
- import "../../../../shared/src/iterables.js";
33
- import "../../../../shared/src/valita.js";
34
- import "../../../../zero-schema/src/compiled-permissions.js";
35
- import "../../../../zero-protocol/src/primary-key.js";
36
- import { ZERO_VERSION_COLUMN_NAME } from "../replicator/schema/constants.js";
37
- import "../../types/pg-data-type.js";
38
- import "../../db/specs.js";
39
- import { ResetPipelinesSignal } from "./snapshotter.js";
40
- import { versionString, cmpVersions, EMPTY_CVR_VERSION, versionToCookie, versionFromString } from "./schema/types.js";
41
25
  import { tracer } from "./tracer.js";
42
- import { ttlClockFromNumber, ttlClockAsNumber } from "./ttl-clock.js";
43
- const PROTOCOL_VERSION_ATTR = "protocol.version";
44
- const DEFAULT_KEEPALIVE_MS = 5e3;
26
+ import { CVRConfigDrivenUpdater, CVRQueryDrivenUpdater, nextEvictionTime } from "./cvr.js";
27
+ import { handleInspect } from "./inspect-handler.js";
28
+ import { resolver } from "@rocicorp/resolver";
29
+ import { Lock } from "@rocicorp/lock";
30
+ //#region ../zero-cache/src/services/view-syncer/view-syncer.ts
31
+ var PROTOCOL_VERSION_ATTR = "protocol.version";
32
+ var DEFAULT_KEEPALIVE_MS = 5e3;
45
33
  function randomID() {
46
- return randInt(1, Number.MAX_SAFE_INTEGER).toString(36);
34
+ return randInt(1, Number.MAX_SAFE_INTEGER).toString(36);
47
35
  }
48
- const TTL_CLOCK_INTERVAL = 6e4;
49
- const TTL_TIMER_HYSTERESIS = 50;
50
- class ViewSyncerService {
51
- id;
52
- #shard;
53
- #lc;
54
- #pipelines;
55
- #stateChanges;
56
- #drainCoordinator;
57
- #keepaliveMs;
58
- #slowHydrateThreshold;
59
- #queryConfig;
60
- #authSession;
61
- userQueryURL;
62
- userQueryHeaders;
63
- // The ViewSyncerService is only started in response to a connection,
64
- // so #lastConnectTime is always initialized to now(). This is necessary
65
- // to handle race conditions in which, e.g. the replica is ready and the
66
- // CVR is accessed before the first connection sends a request.
67
- //
68
- // Note: It is fine to update this variable outside of the lock.
69
- #lastConnectTime = Date.now();
70
- /**
71
- * The TTL clock is used to determine the time at which queries are considered
72
- * expired.
73
- */
74
- #ttlClock;
75
- /**
76
- * The base time for the TTL clock. This is used to compute the current TTL
77
- * clock value. The first time a connection is made, this is set to the
78
- * current time. On subsequent connections, the TTL clock is computed as the
79
- * difference between the current time and this base time.
80
- *
81
- * Every time we write the ttlClock this is update to the current time. That
82
- * way we can compute how much time has passed since the last time we set the
83
- * ttlClock. When we set the ttlClock we just increment it by the amount of
84
- * time that has passed since the last time we set it.
85
- */
86
- #ttlClockBase = Date.now();
87
- /**
88
- * We update the ttlClock every minute to ensure that it is not too much
89
- * out of sync with the current time.
90
- */
91
- #ttlClockInterval = 0;
92
- // Note: It is okay to add/remove clients without acquiring the lock.
93
- #clients = /* @__PURE__ */ new Map();
94
- // Serialize on this lock for:
95
- // (1) storage or database-dependent operations
96
- // (2) updating member variables.
97
- #lock = new Lock();
98
- #cvrStore;
99
- #stopped = resolver();
100
- #initialized = resolver();
101
- #cvr;
102
- #pipelinesSynced = false;
103
- #httpCookie;
104
- #origin;
105
- #lastAuthRevision = 0;
106
- #expiredQueriesTimer = 0;
107
- #setTimeout;
108
- #customQueryTransformer;
109
- // Track query replacements for thrashing detection
110
- #queryReplacements = /* @__PURE__ */ new Map();
111
- #activeClients = getOrCreateUpDownCounter(
112
- "sync",
113
- "active-clients",
114
- "Number of active sync clients"
115
- );
116
- #hydrations = getOrCreateCounter(
117
- "sync",
118
- "hydration",
119
- "Number of query hydrations"
120
- );
121
- #hydrationTime = getOrCreateHistogram("sync", "hydration-time", {
122
- description: "Time to hydrate a query.",
123
- unit: "s"
124
- });
125
- #transactionAdvanceTime = getOrCreateHistogram(
126
- "sync",
127
- "advance-time",
128
- {
129
- description: "Time to advance all queries for a given client group after applying a new transaction to the replica.",
130
- unit: "s"
131
- }
132
- );
133
- #queryTransformations = getOrCreateCounter(
134
- "sync",
135
- "query.transformations",
136
- "Number of query transformations performed"
137
- );
138
- #queryTransformationTime = getOrCreateHistogram(
139
- "sync",
140
- "query.transformation-time",
141
- {
142
- description: "Time to transform custom queries via API server",
143
- unit: "s"
144
- }
145
- );
146
- #queryTransformationHashChanges = getOrCreateCounter(
147
- "sync",
148
- "query.transformation-hash-changes",
149
- "Number of times query transformation hash changed"
150
- );
151
- #queryTransformationNoOps = getOrCreateCounter(
152
- "sync",
153
- "query.transformation-no-ops",
154
- "Number of times query transformation resulted in no-op (hash unchanged)"
155
- );
156
- #inspectorDelegate;
157
- #config;
158
- #runPriorityOp;
159
- constructor(config, lc, shard, taskID, clientGroupID, cvrDb, pipelineDriver, versionChanges, drainCoordinator, slowHydrateThreshold, inspectorDelegate, customQueryTransformer, runPriorityOp, authSession, keepaliveMs = DEFAULT_KEEPALIVE_MS, setTimeoutFn = setTimeout.bind(globalThis)) {
160
- const queryConfig = config.query?.url ? config.query : config.getQueries;
161
- this.#config = config;
162
- this.id = clientGroupID;
163
- this.#shard = shard;
164
- this.#queryConfig = queryConfig;
165
- this.#lc = lc;
166
- this.#pipelines = pipelineDriver;
167
- this.#stateChanges = versionChanges;
168
- this.#drainCoordinator = drainCoordinator;
169
- this.#keepaliveMs = keepaliveMs;
170
- this.#slowHydrateThreshold = slowHydrateThreshold;
171
- this.#inspectorDelegate = inspectorDelegate;
172
- this.#customQueryTransformer = customQueryTransformer;
173
- this.#authSession = authSession;
174
- this.#cvrStore = new CVRStore(
175
- lc,
176
- cvrDb,
177
- shard,
178
- taskID,
179
- clientGroupID,
180
- // On failure, cancel the #stateChanges subscription. The run()
181
- // loop will then await #cvrStore.flushed() which rejects if necessary.
182
- () => this.#stateChanges.cancel()
183
- );
184
- this.#setTimeout = setTimeoutFn;
185
- this.#runPriorityOp = runPriorityOp;
186
- this.keepalive();
187
- }
188
- get auth() {
189
- return this.#authSession.auth;
190
- }
191
- clearAuth() {
192
- this.#authSession.clear();
193
- this.#lastAuthRevision = 0;
194
- }
195
- initAuthSession(userID, wireAuth) {
196
- return this.#authSession.update(userID, wireAuth);
197
- }
198
- #getHeaderOptions(forwardCookie) {
199
- return {
200
- apiKey: this.#queryConfig.apiKey,
201
- customHeaders: this.userQueryHeaders,
202
- allowedClientHeaders: this.#queryConfig.allowedClientHeaders,
203
- token: this.#authSession.auth?.raw,
204
- cookie: forwardCookie ? this.#httpCookie : void 0,
205
- origin: this.#origin
206
- };
207
- }
208
- #runInLockWithCVR(fn) {
209
- const rid = randomID();
210
- this.#lc.debug?.("about to acquire lock for cvr ", rid);
211
- return this.#lock.withLock(async () => {
212
- this.#lc.debug?.("acquired lock in #runInLockWithCVR ", rid);
213
- const lc = this.#lc.withContext("lock", rid);
214
- if (!this.#stateChanges.active) {
215
- this.#lc.debug?.("state changes are inactive");
216
- clearTimeout(this.#expiredQueriesTimer);
217
- throw new ProtocolErrorWithLevel(
218
- {
219
- kind: Rehome,
220
- message: "Reconnect required",
221
- origin: ZeroCache
222
- },
223
- "info"
224
- );
225
- }
226
- if (await this.#checkForShutdownConditionsInLock()) {
227
- this.#lc.info?.(`closing clientGroupID=${this.id}`);
228
- this.#stateChanges.cancel();
229
- return;
230
- }
231
- if (!this.#cvr) {
232
- this.#lc.debug?.("loading cvr");
233
- this.#cvr = await this.#runPriorityOp(
234
- lc,
235
- "loading cvr",
236
- () => this.#cvrStore.load(lc, this.#lastConnectTime)
237
- );
238
- this.#ttlClock = this.#cvr.ttlClock;
239
- this.#ttlClockBase = Date.now();
240
- } else {
241
- const now = Date.now();
242
- this.#cvr = {
243
- ...this.#cvr,
244
- ttlClock: this.#getTTLClock(now)
245
- };
246
- }
247
- try {
248
- await fn(lc, this.#cvr);
249
- } catch (e) {
250
- this.#cvr = void 0;
251
- throw e;
252
- }
253
- });
254
- }
255
- readyState() {
256
- return Promise.race([
257
- this.#initialized.promise,
258
- this.#drainCoordinator.draining
259
- ]);
260
- }
261
- async run() {
262
- try {
263
- if (await this.readyState() === "draining") {
264
- this.#lc.debug?.(`draining view-syncer ${this.id} before running`);
265
- void this.stop();
266
- }
267
- for await (const { state } of this.#stateChanges) {
268
- if (this.#drainCoordinator.shouldDrain()) {
269
- this.#lc.debug?.(`draining view-syncer ${this.id} (elective)`);
270
- break;
271
- }
272
- assert(state === "version-ready", "state should be version-ready");
273
- await this.#runInLockWithCVR(async (lc, cvr) => {
274
- const clientSchema = must(
275
- cvr.clientSchema,
276
- "cvr.clientSchema missing after initialization"
277
- );
278
- if (!this.#pipelines.initialized()) {
279
- this.#pipelines.init(clientSchema);
280
- }
281
- if (cvr.replicaVersion !== null && cvr.version.stateVersion !== "00" && this.#pipelines.replicaVersion < cvr.replicaVersion) {
282
- const message = `Cannot sync from older replica: CVR=${cvr.replicaVersion}, DB=${this.#pipelines.replicaVersion}`;
283
- lc.info?.(`resetting CVR: ${message}`);
284
- throw new ClientNotFoundError(message);
285
- }
286
- if (this.#pipelinesSynced) {
287
- const result = await this.#advancePipelines(lc, cvr);
288
- if (result === "success") {
289
- return;
290
- }
291
- lc.info?.(`resetting pipelines: ${result.message}`);
292
- this.#pipelines.reset(clientSchema);
293
- this.#pipelinesSynced = false;
294
- }
295
- const version = this.#pipelines.advanceWithoutDiff();
296
- const cvrVer = versionString(cvr.version);
297
- if (version < cvr.version.stateVersion) {
298
- lc.debug?.(`replica@${version} is behind cvr@${cvrVer}`);
299
- return;
300
- }
301
- lc.info?.(`init pipelines@${version} (cvr@${cvrVer})`);
302
- await this.#hydrateUnchangedQueries(lc, cvr);
303
- await this.#syncQueryPipelineSet(lc, cvr, "missing");
304
- this.#pipelinesSynced = true;
305
- });
306
- }
307
- if (this.#drainCoordinator.shouldDrain()) {
308
- this.#drainCoordinator.drainNextIn(this.#totalHydrationTimeMs());
309
- }
310
- await this.#cleanup();
311
- } catch (e) {
312
- this.#lc[getLogLevel(e)]?.(
313
- `stopping view-syncer ${this.id}: ${String(e)}`,
314
- e
315
- );
316
- await this.#cleanup(e);
317
- } finally {
318
- await this.#cvrStore.flushed(this.#lc).catch((e) => this.#lc[getLogLevel(e)]?.(e));
319
- this.#lc.info?.(`view-syncer ${this.id} finished`);
320
- this.#stopped.resolve();
321
- }
322
- }
323
- // must be called from within #lock
324
- #removeExpiredQueries = async (lc, cvr) => {
325
- if (hasExpiredQueries(cvr)) {
326
- lc = lc.withContext("method", "#removeExpiredQueries");
327
- lc.debug?.("Queries have expired");
328
- if (this.#pipelinesSynced) {
329
- await this.#syncQueryPipelineSet(lc, cvr, "missing");
330
- }
331
- }
332
- this.#scheduleExpireEviction(lc, cvr);
333
- };
334
- #totalHydrationTimeMs() {
335
- return this.#pipelines.totalHydrationTimeMs();
336
- }
337
- #keepAliveUntil = 0;
338
- /**
339
- * Guarantees that the ViewSyncer will remain running for at least
340
- * its configured `keepaliveMs`. This is called when establishing a
341
- * new connection to ensure that its associated ViewSyncer isn't
342
- * shutdown before it receives the connection.
343
- *
344
- * @return `true` if the ViewSyncer will stay alive, `false` if the
345
- * ViewSyncer is shutting down.
346
- */
347
- keepalive() {
348
- if (!this.#stateChanges.active) {
349
- return false;
350
- }
351
- this.#keepAliveUntil = Date.now() + this.#keepaliveMs;
352
- return true;
353
- }
354
- // oxlint-disable-next-line no-unused-private-class-members -- False positive, used in #scheduleShutdown
355
- #shutdownTimer = null;
356
- #scheduleShutdown(delayMs = 0) {
357
- this.#shutdownTimer ??= this.#setTimeout(() => {
358
- this.#shutdownTimer = null;
359
- void this.#runInLockWithCVR(() => {
360
- }).catch(
361
- (e) => (
362
- // If an error occurs (e.g. ownership change), propagate the error
363
- // to the main run() loop via the #stateChanges Subscription.
364
- this.#stateChanges.fail(e)
365
- )
366
- );
367
- }, delayMs);
368
- }
369
- async #checkForShutdownConditionsInLock() {
370
- if (this.#clients.size > 0) {
371
- return false;
372
- }
373
- await this.#cvrStore.flushed(this.#lc);
374
- if (Date.now() <= this.#keepAliveUntil) {
375
- this.#scheduleShutdown(this.#keepaliveMs);
376
- return false;
377
- }
378
- return this.#clients.size === 0;
379
- }
380
- #deleteClientDueToDisconnect(clientID, client) {
381
- const c = this.#clients.get(clientID);
382
- if (c === client) {
383
- this.#clients.delete(clientID);
384
- if (this.#clients.size === 0) {
385
- if (this.#ttlClock !== void 0) {
386
- this.#updateTTLClockInCVRWithoutLock(this.#lc);
387
- }
388
- this.#stopExpireTimer();
389
- this.#scheduleShutdown();
390
- }
391
- }
392
- }
393
- #stopExpireTimer() {
394
- this.#lc.debug?.("Stopping expired queries timer");
395
- clearTimeout(this.#expiredQueriesTimer);
396
- this.#expiredQueriesTimer = 0;
397
- }
398
- initConnection(ctx, initConnectionMessage) {
399
- this.#lc.debug?.("viewSyncer.initConnection");
400
- return startSpan(tracer, "vs.initConnection", () => {
401
- const {
402
- clientID,
403
- profileID,
404
- wsID,
405
- baseCookie,
406
- httpCookie,
407
- origin,
408
- protocolVersion
409
- } = ctx;
410
- this.#httpCookie = httpCookie;
411
- this.#origin = origin;
412
- const [, { userQueryURL, userQueryHeaders }] = initConnectionMessage;
413
- if (this.userQueryURL === void 0) {
414
- this.userQueryURL = userQueryURL;
415
- this.userQueryHeaders = userQueryHeaders;
416
- } else {
417
- if (this.userQueryURL !== userQueryURL) {
418
- this.#lc.warn?.(
419
- "Client provided different query parameters than client group",
420
- {
421
- clientID,
422
- clientURL: userQueryURL,
423
- clientGroupURL: this.userQueryURL
424
- }
425
- );
426
- }
427
- }
428
- const lc = this.#lc.withContext("clientID", clientID).withContext("wsID", wsID);
429
- const downstream = Subscription.create({
430
- cleanup: (_, err) => {
431
- err ? lc[getLogLevel(err)]?.(`client closed with error`, err) : lc.info?.("client closed");
432
- this.#deleteClientDueToDisconnect(clientID, newClient);
433
- this.#activeClients.add(-1, {
434
- [PROTOCOL_VERSION_ATTR]: protocolVersion
435
- });
436
- }
437
- });
438
- this.#activeClients.add(1, {
439
- [PROTOCOL_VERSION_ATTR]: protocolVersion
440
- });
441
- if (this.#clients.size === 0) {
442
- const now = Date.now();
443
- this.#ttlClockBase = now;
444
- }
445
- const newClient = new ClientHandler(
446
- lc,
447
- this.id,
448
- clientID,
449
- wsID,
450
- this.#shard,
451
- baseCookie,
452
- downstream
453
- );
454
- this.#clients.get(clientID)?.close(`replaced by wsID: ${wsID}`);
455
- this.#clients.set(clientID, newClient);
456
- void this.#runInLockForClient(
457
- ctx,
458
- initConnectionMessage,
459
- async (lc2, clientID2, msg, cvr) => {
460
- if (cvr.clientSchema === null && !msg.clientSchema) {
461
- throw new ProtocolErrorWithLevel(
462
- {
463
- kind: InvalidConnectionRequest,
464
- message: "The initConnection message for a new client group must include client schema.",
465
- origin: ZeroCache
466
- },
467
- "warn"
468
- );
469
- }
470
- await this.#handleConfigUpdate(
471
- lc2,
472
- clientID2,
473
- msg,
474
- cvr,
475
- "all",
476
- // re transform all on new connections
477
- // Until the profileID is required in the URL, default it to
478
- // `cg${clientGroupID}`, as is done in the schema migration.
479
- // As clients update to the zero version with the profileID logic,
480
- // the value will be correspondingly in the CVR db.
481
- profileID ?? `cg${this.id}`
482
- );
483
- this.#initialized.resolve("initialized");
484
- },
485
- newClient
486
- ).catch((e) => newClient.fail(e));
487
- return downstream;
488
- });
489
- }
490
- async changeDesiredQueries(ctx, msg) {
491
- await this.#runInLockForClient(
492
- ctx,
493
- msg,
494
- async (lc, clientID, msg2, cvr) => {
495
- let customQueryTransformMode = "missing";
496
- const currentAuthRevision = this.#authSession.revision;
497
- if (this.#lastAuthRevision < currentAuthRevision) {
498
- customQueryTransformMode = "all";
499
- lc.debug?.(
500
- "Auth revision changed, setting customQueryTransformMode to all"
501
- );
502
- }
503
- const result = await this.#handleConfigUpdate(
504
- lc,
505
- clientID,
506
- msg2,
507
- cvr,
508
- customQueryTransformMode
509
- );
510
- if (customQueryTransformMode === "all") {
511
- this.#lastAuthRevision = currentAuthRevision;
512
- }
513
- return result;
514
- }
515
- );
516
- }
517
- async updateAuth(ctx, msg) {
518
- await this.#runInLockForClient(ctx, msg, async (lc, clientID, _, cvr) => {
519
- const authResult = await this.#authSession.update(
520
- ctx.userID,
521
- msg[1].auth
522
- );
523
- if (!authResult.ok) {
524
- throw new ProtocolErrorWithLevel(authResult.error, "warn");
525
- }
526
- const currentAuthRevision = this.#authSession.revision;
527
- if (this.#lastAuthRevision >= currentAuthRevision) {
528
- lc.debug?.("Auth revision unchanged, skipping query re-transformation");
529
- return;
530
- } else {
531
- lc.debug?.("Auth revision changed, re-transforming queries");
532
- }
533
- const result = await this.#handleConfigUpdate(
534
- lc,
535
- clientID,
536
- {},
537
- // no config updates, but we want to trigger re-transformation of custom queries if auth changed
538
- cvr,
539
- "all"
540
- );
541
- this.#lastAuthRevision = currentAuthRevision;
542
- return result;
543
- });
544
- }
545
- async deleteClients(ctx, msg) {
546
- const deletedClientIDs = await this.#runInLockForClient(
547
- ctx,
548
- [msg[0], { deleted: msg[1] }],
549
- (lc, clientID, msg2, cvr) => this.#handleConfigUpdate(lc, clientID, msg2, cvr, "missing")
550
- );
551
- return deletedClientIDs ?? [];
552
- }
553
- #getTTLClock(now) {
554
- const delta = now - this.#ttlClockBase;
555
- assert(this.#ttlClock !== void 0, "ttlClock should be defined");
556
- const ttlClock = ttlClockFromNumber(
557
- ttlClockAsNumber(this.#ttlClock) + delta
558
- );
559
- assert(
560
- ttlClockAsNumber(ttlClock) <= now,
561
- "ttlClock should be less than or equal to now"
562
- );
563
- this.#ttlClock = ttlClock;
564
- this.#ttlClockBase = now;
565
- return ttlClock;
566
- }
567
- #flushUpdater(lc, updater) {
568
- return startAsyncSpan(
569
- tracer,
570
- "vs.#flushUpdater",
571
- () => this.#runPriorityOp(lc, "flushing cvr", async () => {
572
- const now = Date.now();
573
- const ttlClock = this.#getTTLClock(now);
574
- const { cvr, flushed } = await updater.flush(
575
- lc,
576
- this.#lastConnectTime,
577
- now,
578
- ttlClock
579
- );
580
- if (flushed) {
581
- this.#startTTLClockInterval(lc);
582
- }
583
- return cvr;
584
- })
585
- );
586
- }
587
- #startTTLClockInterval(lc) {
588
- this.#stopTTLClockInterval();
589
- this.#ttlClockInterval = this.#setTimeout(() => {
590
- this.#updateTTLClockInCVRWithoutLock(lc);
591
- this.#startTTLClockInterval(lc);
592
- }, TTL_CLOCK_INTERVAL);
593
- }
594
- #stopTTLClockInterval() {
595
- clearTimeout(this.#ttlClockInterval);
596
- this.#ttlClockInterval = 0;
597
- }
598
- #updateTTLClockInCVRWithoutLock(lc) {
599
- const rid = randomID();
600
- lc.debug?.("Syncing ttlClock", rid);
601
- const start = Date.now();
602
- const ttlClock = this.#getTTLClock(start);
603
- this.#cvrStore.updateTTLClock(ttlClock, start).then(() => {
604
- lc.debug?.("Synced ttlClock", rid, `in ${Date.now() - start} ms`);
605
- }).catch((e) => {
606
- lc.error?.(
607
- "failed to update TTL clock",
608
- rid,
609
- `after ${Date.now() - start} ms`,
610
- e
611
- );
612
- });
613
- }
614
- async #updateCVRConfig(lc, cvr, clientID, customQueryTransformMode, fn) {
615
- const updater = new CVRConfigDrivenUpdater(
616
- this.#cvrStore,
617
- cvr,
618
- this.#shard
619
- );
620
- updater.ensureClient(clientID);
621
- const patches = fn(updater);
622
- this.#cvr = await this.#flushUpdater(lc, updater);
623
- if (cmpVersions(cvr.version, this.#cvr.version) < 0) {
624
- const newCVR = this.#cvr;
625
- await startAsyncSpan(
626
- tracer,
627
- "vs.#updateCVRConfig.pokeClients",
628
- async () => {
629
- const pokers = startPoke(
630
- this.#getClients(cvr.version),
631
- newCVR.version
632
- );
633
- for (const patch of patches) {
634
- await pokers.addPatch(patch);
635
- }
636
- await pokers.end(newCVR.version);
637
- }
638
- );
639
- }
640
- if (this.#pipelinesSynced) {
641
- await this.#syncQueryPipelineSet(lc, this.#cvr, customQueryTransformMode);
642
- }
643
- return this.#cvr;
644
- }
645
- /**
646
- * Runs the given `fn` to process the `msg` from within the `#lock`,
647
- * optionally adding the `newClient` if supplied.
648
- */
649
- #runInLockForClient(ctx, msg, fn, newClient) {
650
- this.#lc.debug?.("viewSyncer.#runInLockForClient");
651
- const { clientID, wsID } = ctx;
652
- const [cmd, body] = msg;
653
- if (newClient || !this.#clients.has(clientID)) {
654
- this.#lastConnectTime = Date.now();
655
- }
656
- return startAsyncSpan(
657
- tracer,
658
- `vs.#runInLockForClient(${cmd})`,
659
- async () => {
660
- let client;
661
- let result;
662
- try {
663
- await this.#runInLockWithCVR(async (lc, cvr) => {
664
- lc = lc.withContext("clientID", clientID).withContext("wsID", wsID).withContext("cmd", cmd);
665
- lc.debug?.("acquired lock for cvr");
666
- client = this.#clients.get(clientID);
667
- if (client?.wsID !== wsID) {
668
- lc.debug?.("mismatched wsID", client?.wsID, wsID);
669
- return;
670
- }
671
- if (newClient) {
672
- assert(
673
- newClient === client,
674
- "newClient must match existing client"
675
- );
676
- checkClientAndCVRVersions(client.version(), cvr.version);
677
- } else if (!this.#clients.has(clientID)) {
678
- lc.warn?.(`Processing ${cmd} before initConnection was received`);
679
- }
680
- lc.debug?.(cmd, body);
681
- result = await fn(lc, clientID, body, cvr);
682
- });
683
- } catch (e) {
684
- const lc = this.#lc.withContext("clientID", clientID).withContext("wsID", wsID).withContext("cmd", cmd);
685
- lc[getLogLevel(e)]?.(`closing connection with error`, e);
686
- if (isTransformAuthFailure(e)) {
687
- lc.debug?.("Auth failure detected in transform response");
688
- this.clearAuth();
689
- }
690
- if (client) {
691
- client.fail(e);
692
- } else {
693
- throw e;
694
- }
695
- }
696
- return result;
697
- }
698
- );
699
- }
700
- #getClients(atVersion) {
701
- const clients = [...this.#clients.values()];
702
- return atVersion ? clients.filter(
703
- (c) => cmpVersions(c.version() ?? EMPTY_CVR_VERSION, atVersion) === 0
704
- ) : clients;
705
- }
706
- // Must be called from within #lock.
707
- #handleConfigUpdate = (lc, clientID, {
708
- clientSchema,
709
- deleted,
710
- desiredQueriesPatch,
711
- activeClients
712
- }, cvr, customQueryTransformMode, profileID) => startAsyncSpan(tracer, "vs.#handleConfigUpdate", async () => {
713
- const deletedClientIDs = [];
714
- const deletedClientGroupIDs = [];
715
- cvr = await this.#updateCVRConfig(
716
- lc,
717
- cvr,
718
- clientID,
719
- customQueryTransformMode,
720
- (updater) => {
721
- const { ttlClock } = cvr;
722
- const patches = [];
723
- if (clientSchema) {
724
- updater.setClientSchema(lc, clientSchema);
725
- }
726
- if (profileID) {
727
- updater.setProfileID(lc, profileID);
728
- }
729
- lc.debug?.(
730
- `applying ${desiredQueriesPatch?.length ?? 0} query patches`
731
- );
732
- if (desiredQueriesPatch?.length) {
733
- for (const patch of desiredQueriesPatch) {
734
- switch (patch.op) {
735
- case "put":
736
- patches.push(...updater.putDesiredQueries(clientID, [patch]));
737
- break;
738
- case "del":
739
- patches.push(
740
- ...updater.markDesiredQueriesAsInactive(
741
- clientID,
742
- [patch.hash],
743
- ttlClock
744
- )
745
- );
746
- break;
747
- case "clear":
748
- patches.push(...updater.clearDesiredQueries(clientID));
749
- break;
750
- }
751
- }
752
- }
753
- const clientIDsToDelete = /* @__PURE__ */ new Set();
754
- if (activeClients) {
755
- const allClientIDs = Object.keys(cvr.clients);
756
- const activeClientsSet = new Set(activeClients);
757
- for (const id of allClientIDs) {
758
- if (!activeClientsSet.has(id)) {
759
- clientIDsToDelete.add(id);
760
- }
761
- }
762
- }
763
- if (deleted?.clientIDs?.length) {
764
- for (const cid of deleted.clientIDs) {
765
- assert(cid !== clientID, "cannot delete self");
766
- clientIDsToDelete.add(cid);
767
- }
768
- }
769
- for (const cid of clientIDsToDelete) {
770
- const patchesDueToClient = updater.deleteClient(cid, ttlClock);
771
- patches.push(...patchesDueToClient);
772
- deletedClientIDs.push(cid);
773
- }
774
- if (deleted?.clientGroupIDs?.length) {
775
- lc.debug?.(
776
- `ignoring ${deleted.clientGroupIDs.length} deprecated client group deletes`
777
- );
778
- }
779
- return patches;
780
- }
781
- );
782
- if (deletedClientIDs.length && deleted?.clientIDs?.length || deletedClientGroupIDs.length) {
783
- const clients = this.#getClients();
784
- await startAsyncSpan(
785
- tracer,
786
- "vs.#handleConfigUpdate.sendDeleteClients",
787
- () => Promise.allSettled(
788
- clients.map(
789
- (client) => client.sendDeleteClients(
790
- lc,
791
- deletedClientIDs,
792
- deletedClientGroupIDs
793
- )
794
- )
795
- )
796
- );
797
- }
798
- this.#scheduleExpireEviction(lc, cvr);
799
- return deletedClientIDs;
800
- });
801
- #scheduleExpireEviction(lc, cvr) {
802
- const { ttlClock } = cvr;
803
- this.#stopExpireTimer();
804
- const next = nextEvictionTime(cvr);
805
- if (next === void 0) {
806
- lc.debug?.("no inactive queries with ttl");
807
- return;
808
- }
809
- const delay = Math.max(
810
- TTL_TIMER_HYSTERESIS,
811
- Math.min(
812
- ttlClockAsNumber(next) - ttlClockAsNumber(ttlClock) + TTL_TIMER_HYSTERESIS,
813
- MAX_TTL_MS
814
- )
815
- );
816
- lc.debug?.("Scheduling eviction timer to run in ", delay, "ms");
817
- this.#expiredQueriesTimer = this.#setTimeout(() => {
818
- this.#expiredQueriesTimer = 0;
819
- this.#runInLockWithCVR(
820
- (lc2, cvr2) => this.#removeExpiredQueries(lc2, cvr2)
821
- ).catch(
822
- (e) => (
823
- // If an error occurs (e.g. ownership change), propagate the error
824
- // to the main run() loop via the #stateChanges Subscription.
825
- this.#stateChanges.fail(e)
826
- )
827
- );
828
- }, delay);
829
- }
830
- /**
831
- * Adds and hydrates pipelines for queries whose results are already
832
- * recorded in the CVR. Namely:
833
- *
834
- * 1. The CVR state version and database version are the same.
835
- * 2. The transformation hash of the queries equal those in the CVR.
836
- *
837
- * Note that by definition, only "got" queries can satisfy condition (2),
838
- * as desired queries do not have a transformation hash.
839
- *
840
- * This is an initialization step that sets up pipeline state without
841
- * the expensive of loading and diffing CVR row state.
842
- *
843
- * This must be called from within the #lock.
844
- */
845
- async #hydrateUnchangedQueries(lc, cvr) {
846
- assert(this.#pipelines.initialized(), "pipelines must be initialized");
847
- const dbVersion = this.#pipelines.currentVersion();
848
- const cvrVersion = cvr.version;
849
- if (cvrVersion.stateVersion !== dbVersion) {
850
- lc.info?.(
851
- `CVR (${versionToCookie(cvrVersion)}) is behind db ${dbVersion}`
852
- );
853
- return;
854
- }
855
- const gotQueries = Object.entries(cvr.queries).filter(
856
- ([_, state]) => state.transformationHash !== void 0
857
- );
858
- const customQueries = /* @__PURE__ */ new Map();
859
- const otherQueries = [];
860
- for (const [, query] of gotQueries) {
861
- if (query.type !== "internal" && Object.values(query.clientState).every(
862
- ({ inactivatedAt }) => inactivatedAt !== void 0
863
- )) {
864
- continue;
865
- }
866
- if (query.type === "custom") {
867
- customQueries.set(query.id, query);
868
- } else {
869
- otherQueries.push(query);
870
- }
871
- }
872
- const transformedQueries = [];
873
- if (customQueries.size > 0 && !this.#customQueryTransformer) {
874
- lc.warn?.(
875
- "Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache."
876
- );
877
- }
878
- const customQueryTransformer = this.#customQueryTransformer;
879
- if (customQueryTransformer && customQueries.size > 0) {
880
- const transformedCustomQueries = await this.#runPriorityOp(
881
- lc,
882
- "#hydrateUnchangedQueries transforming custom queries",
883
- () => customQueryTransformer.transform(
884
- this.#getHeaderOptions(this.#queryConfig.forwardCookies),
885
- customQueries.values(),
886
- this.userQueryURL
887
- )
888
- );
889
- if (Array.isArray(transformedCustomQueries)) {
890
- for (const q of transformedCustomQueries) {
891
- if (!("error" in q) && q.transformationHash === customQueries.get(q.id)?.transformationHash) {
892
- transformedQueries.push(q);
893
- }
894
- }
895
- }
896
- }
897
- for (const q of otherQueries) {
898
- const auth = this.#authSession.auth;
899
- const transformed = transformAndHashQuery(
900
- lc,
901
- q.id,
902
- q.ast,
903
- must(this.#pipelines.currentPermissions()).permissions ?? {
904
- tables: {}
905
- },
906
- auth?.type === "jwt" ? auth : void 0,
907
- q.type === "internal"
908
- );
909
- if (transformed.transformationHash === q.transformationHash) {
910
- transformedQueries.push(transformed);
911
- }
912
- }
913
- for (const {
914
- id: queryID,
915
- transformationHash,
916
- transformedAst
917
- } of transformedQueries) {
918
- const timer = new TimeSliceTimer(lc);
919
- let count = 0;
920
- await startAsyncSpan(
921
- tracer,
922
- "vs.#hydrateUnchangedQueries.addQuery",
923
- async (span) => {
924
- span.setAttribute("queryHash", queryID);
925
- span.setAttribute("transformationHash", transformationHash);
926
- span.setAttribute("table", transformedAst.table);
927
- for (const change of this.#pipelines.addQuery(
928
- transformationHash,
929
- queryID,
930
- transformedAst,
931
- await timer.start()
932
- )) {
933
- if (change === "yield") {
934
- await timer.yieldProcess("yield in hydrateUnchangedQueries");
935
- } else {
936
- count++;
937
- }
938
- }
939
- }
940
- );
941
- const elapsed = timer.totalElapsed();
942
- this.#hydrations.add(1);
943
- this.#hydrationTime.record(elapsed / 1e3);
944
- this.#addQueryMaterializationServerMetric(transformationHash, elapsed);
945
- lc.debug?.(`hydrated ${count} rows for ${queryID} (${elapsed} ms)`);
946
- }
947
- }
948
- #processTransformedCustomQueries(lc, transformedCustomQueries, cb, customQueryMap) {
949
- if ("kind" in transformedCustomQueries) {
950
- this.#sendQueryTransformErrorToClients(
951
- customQueryMap,
952
- transformedCustomQueries
953
- );
954
- return transformedCustomQueries.queryIDs;
955
- }
956
- const appQueryErrors = [];
957
- for (const q of transformedCustomQueries) {
958
- if ("error" in q) {
959
- const errorMessage = `Error transforming custom query ${q.name}: ${q.error}${q.details ? ` ${JSON.stringify(q.details)}` : ""}`;
960
- lc.warn?.(errorMessage, q);
961
- appQueryErrors.push(q);
962
- continue;
963
- }
964
- cb(q);
965
- }
966
- this.#sendQueryTransformErrorToClients(customQueryMap, appQueryErrors);
967
- return appQueryErrors.map((q) => q.id);
968
- }
969
- #sendQueryTransformErrorToClients(customQueryMap, errorOrErrors) {
970
- const getAffectedClientIDs = (queryIDs) => {
971
- const clientIds = /* @__PURE__ */ new Set();
972
- for (const queryID of queryIDs) {
973
- const q = customQueryMap.get(queryID);
974
- assert(
975
- q,
976
- `got an error for query ${queryID} that does not map back to a custom query`
977
- );
978
- Object.keys(q.clientState).forEach((id) => clientIds.add(id));
979
- }
980
- return clientIds;
981
- };
982
- if ("queryIDs" in errorOrErrors) {
983
- for (const clientId of getAffectedClientIDs(errorOrErrors.queryIDs)) {
984
- this.#clients.get(clientId)?.sendQueryTransformFailedError(errorOrErrors);
985
- }
986
- return;
987
- }
988
- const appErrorGroups = /* @__PURE__ */ new Map();
989
- for (const err of errorOrErrors) {
990
- for (const clientId of getAffectedClientIDs([err.id])) {
991
- const group = appErrorGroups.get(clientId) ?? [];
992
- group.push(err);
993
- appErrorGroups.set(clientId, group);
994
- }
995
- }
996
- for (const [clientId, errors] of appErrorGroups) {
997
- this.#clients.get(clientId)?.sendQueryTransformApplicationErrors(errors);
998
- }
999
- }
1000
- #addQueryMaterializationServerMetric(queryID, elapsed) {
1001
- this.#inspectorDelegate.addMetric(
1002
- "query-materialization-server",
1003
- elapsed,
1004
- queryID
1005
- );
1006
- }
1007
- /**
1008
- * Adds and/or removes queries to/from the PipelineDriver to bring it
1009
- * in sync with the set of queries in the CVR (both got and desired).
1010
- * If queries are added, removed, or queried due to a new state version,
1011
- * a new CVR version is created and pokes sent to connected clients.
1012
- *
1013
- * This must be called from within the #lock.
1014
- */
1015
- #syncQueryPipelineSet(lc, cvr, customQueryTransformMode) {
1016
- return startAsyncSpan(tracer, "vs.#syncQueryPipelineSet", async () => {
1017
- assert(
1018
- this.#pipelines.initialized(),
1019
- "pipelines must be initialized (syncQueryPipelineSet)"
1020
- );
1021
- if (this.#ttlClock === void 0) {
1022
- this.#ttlClock = cvr.ttlClock;
1023
- }
1024
- const now = Date.now();
1025
- const ttlClock = this.#getTTLClock(now);
1026
- const cvrQueryEntires = Object.entries(cvr.queries);
1027
- const customQueries = /* @__PURE__ */ new Map();
1028
- const otherQueries = [];
1029
- const transformedQueries = [];
1030
- for (const [id, query] of cvrQueryEntires) {
1031
- if (query.type === "custom") {
1032
- assert(id === query.id, "custom query id mismatch");
1033
- customQueries.set(id, query);
1034
- } else {
1035
- otherQueries.push({ id, query });
1036
- }
1037
- }
1038
- for (const { id, query: origQuery } of otherQueries) {
1039
- assert(id === origQuery.id, "query id mismatch");
1040
- const auth = this.#authSession.auth;
1041
- const transformed = transformAndHashQuery(
1042
- lc,
1043
- origQuery.id,
1044
- origQuery.ast,
1045
- must(this.#pipelines.currentPermissions()).permissions ?? {
1046
- tables: {}
1047
- },
1048
- auth?.type === "jwt" ? auth : void 0,
1049
- origQuery.type === "internal"
1050
- );
1051
- transformedQueries.push({
1052
- id,
1053
- origQuery,
1054
- transformed
1055
- });
1056
- }
1057
- if (customQueries.size > 0 && !this.#customQueryTransformer) {
1058
- lc.warn?.(
1059
- "Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache."
1060
- );
1061
- }
1062
- let erroredQueryIDs;
1063
- const customQueriesToTransform = customQueryTransformMode === "all" ? [...customQueries.values()] : customQueryTransformMode && [...customQueries.values()].filter(
1064
- (q) => !this.#pipelines.queries().has(q.id)
1065
- );
1066
- const customQueryTransformer = this.#customQueryTransformer;
1067
- if (customQueryTransformer && customQueriesToTransform.length > 0) {
1068
- const transformStart = performance.now();
1069
- let transformedCustomQueries;
1070
- try {
1071
- transformedCustomQueries = await this.#runPriorityOp(
1072
- lc,
1073
- "#syncQueryPipelineSet transforming custom queries",
1074
- () => customQueryTransformer.transform(
1075
- this.#getHeaderOptions(true),
1076
- customQueriesToTransform,
1077
- this.userQueryURL
1078
- )
1079
- );
1080
- if (!Array.isArray(transformedCustomQueries) && transformedCustomQueries.kind === TransformFailed) {
1081
- throw new ProtocolErrorWithLevel(transformedCustomQueries, "warn");
1082
- } else {
1083
- this.#queryTransformations.add(1, { result: "success" });
1084
- }
1085
- } catch (e) {
1086
- this.#queryTransformations.add(1, { result: "error" });
1087
- throw e;
1088
- } finally {
1089
- const transformDuration = (performance.now() - transformStart) / 1e3;
1090
- this.#queryTransformationTime.record(transformDuration);
1091
- }
1092
- const successfullyTransformedCustomQueries = /* @__PURE__ */ new Map();
1093
- erroredQueryIDs = this.#processTransformedCustomQueries(
1094
- lc,
1095
- transformedCustomQueries,
1096
- (q) => {
1097
- const origQuery = customQueries.get(q.id);
1098
- if (origQuery) {
1099
- successfullyTransformedCustomQueries.set(q.id, q);
1100
- transformedQueries.push({
1101
- id: q.id,
1102
- origQuery,
1103
- transformed: q
1104
- });
1105
- }
1106
- },
1107
- customQueries
1108
- );
1109
- for (const [
1110
- queryID,
1111
- newTransform
1112
- ] of successfullyTransformedCustomQueries) {
1113
- const existingTransformHash = cvr.queries[queryID]?.transformationHash;
1114
- if (existingTransformHash) {
1115
- const oldHash = existingTransformHash;
1116
- const newHash = newTransform.transformationHash;
1117
- if (oldHash !== newHash) {
1118
- lc.info?.(
1119
- `Query ${queryID} transformation changed: ${oldHash} -> ${newHash}`
1120
- );
1121
- this.#checkForThrashing(queryID);
1122
- this.#queryTransformationHashChanges.add(1);
1123
- } else {
1124
- this.#queryTransformationNoOps.add(1);
1125
- }
1126
- }
1127
- }
1128
- }
1129
- const removeQueriesQueryIds = new Set(
1130
- Object.values(cvr.queries).filter((q) => expired(ttlClock, q)).map((q) => q.id).concat(erroredQueryIDs || [])
1131
- );
1132
- const addQueries = transformedQueries.map(({ id, transformed }) => ({
1133
- id,
1134
- ast: transformed.transformedAst,
1135
- transformationHash: transformed.transformationHash
1136
- })).filter(
1137
- (q) => !removeQueriesQueryIds.has(q.id) && this.#pipelines.queries().get(q.id)?.transformationHash !== q.transformationHash
1138
- );
1139
- for (const q of addQueries) {
1140
- const orig = cvr.queries[q.id];
1141
- lc.debug?.(
1142
- "ViewSyncer adding query",
1143
- q.ast,
1144
- "transformed from",
1145
- orig.type === "custom" ? orig.name : orig.ast
1146
- );
1147
- }
1148
- if (addQueries.length > 0 || removeQueriesQueryIds.size > 0) {
1149
- await this.#addAndRemoveQueries(
1150
- lc,
1151
- cvr,
1152
- addQueries,
1153
- [...removeQueriesQueryIds].map((id) => ({ id }))
1154
- );
1155
- } else {
1156
- await this.#catchupClients(lc, cvr);
1157
- }
1158
- });
1159
- }
1160
- /**
1161
- * Check if a query is being replaced too frequently (thrashing).
1162
- * Logs a warning if the query has been replaced more than 3 times in 60 seconds.
1163
- */
1164
- #checkForThrashing(queryID) {
1165
- const THRASH_WINDOW_MS = 6e4;
1166
- const THRASH_THRESHOLD = 3;
1167
- const now = Date.now();
1168
- let record = this.#queryReplacements.get(queryID);
1169
- if (!record) {
1170
- record = { count: 1, windowStart: now };
1171
- this.#queryReplacements.set(queryID, record);
1172
- return;
1173
- }
1174
- if (now - record.windowStart > THRASH_WINDOW_MS) {
1175
- this.#queryReplacements.delete(queryID);
1176
- this.#queryReplacements.set(queryID, { count: 1, windowStart: now });
1177
- return;
1178
- }
1179
- record.count++;
1180
- if (record.count >= THRASH_THRESHOLD) {
1181
- this.#lc.warn?.(
1182
- `Query thrashing detected for query ${queryID}. ${record.count} replacements in 60s. This may indicate clients with different auth contexts connecting to the same client group.`
1183
- );
1184
- }
1185
- }
1186
- // This must be called from within the #lock.
1187
- #addAndRemoveQueries(lc, cvr, addQueries, removeQueries) {
1188
- return startAsyncSpan(tracer, "vs.#addAndRemoveQueries", async () => {
1189
- assert(
1190
- addQueries.length > 0 || removeQueries.length > 0,
1191
- "Must have queries to add or remove"
1192
- );
1193
- const start = performance.now();
1194
- const stateVersion = this.#pipelines.currentVersion();
1195
- lc = lc.withContext("stateVersion", stateVersion);
1196
- lc.info?.(`hydrating ${addQueries.length} queries`);
1197
- const updater = new CVRQueryDrivenUpdater(
1198
- this.#cvrStore,
1199
- cvr,
1200
- stateVersion,
1201
- this.#pipelines.replicaVersion
1202
- );
1203
- const { newVersion, queryPatches } = updater.trackQueries(
1204
- lc,
1205
- addQueries,
1206
- removeQueries
1207
- );
1208
- const clients = this.#getClients();
1209
- const pokers = startPoke(clients, newVersion);
1210
- for (const patch of queryPatches) {
1211
- await pokers.addPatch(patch);
1212
- }
1213
- for (const q of removeQueries) {
1214
- this.#pipelines.removeQuery(q.id);
1215
- this.#inspectorDelegate.removeQuery(q.id);
1216
- this.#queryReplacements.delete(q.id);
1217
- }
1218
- let totalProcessTime = 0;
1219
- const timer = new TimeSliceTimer(lc);
1220
- const pipelines = this.#pipelines;
1221
- const hydrations = this.#hydrations;
1222
- const hydrationTime = this.#hydrationTime;
1223
- const self = this;
1224
- await yieldProcess(lc);
1225
- function* generateRowChanges(slowHydrateThreshold) {
1226
- for (const q of addQueries) {
1227
- lc = lc.withContext("hash", q.id).withContext("transformationHash", q.transformationHash);
1228
- lc.debug?.(`adding pipeline for query`, q.ast);
1229
- yield* pipelines.addQuery(
1230
- q.transformationHash,
1231
- q.id,
1232
- q.ast,
1233
- timer.startWithoutYielding()
1234
- );
1235
- const elapsed = timer.stop();
1236
- totalProcessTime += elapsed;
1237
- self.#addQueryMaterializationServerMetric(q.id, elapsed);
1238
- if (elapsed > slowHydrateThreshold) {
1239
- lc.warn?.("Slow query materialization", elapsed, q.ast);
1240
- }
1241
- manualSpan(tracer, "vs.addAndConsumeQuery", elapsed, {
1242
- hash: q.id,
1243
- transformationHash: q.transformationHash
1244
- });
1245
- }
1246
- hydrations.add(1);
1247
- hydrationTime.record(totalProcessTime / 1e3);
1248
- }
1249
- await this.#processChanges(
1250
- lc,
1251
- timer,
1252
- generateRowChanges(this.#slowHydrateThreshold),
1253
- updater,
1254
- pokers
1255
- );
1256
- for (const patch of await updater.deleteUnreferencedRows(lc)) {
1257
- await pokers.addPatch(patch);
1258
- }
1259
- this.#cvr = await this.#flushUpdater(lc, updater);
1260
- const finalVersion = this.#cvr.version;
1261
- await this.#catchupClients(
1262
- lc,
1263
- cvr,
1264
- finalVersion,
1265
- addQueries.map((q) => q.id),
1266
- pokers
1267
- );
1268
- await pokers.end(finalVersion);
1269
- const wallTime = performance.now() - start;
1270
- lc.info?.(
1271
- `finished processing queries (process: ${totalProcessTime} ms, wall: ${wallTime} ms)`
1272
- );
1273
- });
1274
- }
1275
- /**
1276
- * @param cvr The CVR to which clients should be caught up to. This does
1277
- * not necessarily need to be the current CVR.
1278
- * @param current The expected current CVR version. Before performing
1279
- * catchup, the snapshot read will verify that the CVR has not been
1280
- * concurrently modified. Note that this only needs to be done for
1281
- * catchup because it is the only time data from the CVR DB is
1282
- * "exported" without being gated by a CVR flush (which provides
1283
- * concurrency protection in all other cases).
1284
- *
1285
- * If unspecified, the version of the `cvr` is used.
1286
- * @param excludeQueryHashes Exclude patches from rows associated with
1287
- * the specified queries.
1288
- * @param usePokers If specified, sends pokes on existing PokeHandlers,
1289
- * in which case the caller is responsible for sending the `pokeEnd`
1290
- * messages. If unspecified, the pokes will be started and ended
1291
- * using the version from the supplied `cvr`.
1292
- */
1293
- // Must be called within #lock
1294
- #catchupClients(lc, cvr, current, excludeQueryHashes = [], usePokers) {
1295
- return startAsyncSpan(tracer, "vs.#catchupClients", async (span) => {
1296
- current ??= cvr.version;
1297
- const clients = this.#getClients();
1298
- const pokers = usePokers ?? startPoke(clients, cvr.version);
1299
- span.setAttribute("numClients", clients.length);
1300
- const catchupFrom = clients.map((c) => c.version()).reduce((a, b) => cmpVersions(a, b) < 0 ? a : b, cvr.version);
1301
- const rowPatches = this.#cvrStore.catchupRowPatches(
1302
- lc,
1303
- catchupFrom,
1304
- cvr,
1305
- current,
1306
- excludeQueryHashes
1307
- );
1308
- const configPatches = this.#cvrStore.catchupConfigPatches(
1309
- lc,
1310
- catchupFrom,
1311
- cvr,
1312
- current
1313
- );
1314
- configPatches.catch(() => {
1315
- });
1316
- let rowPatchCount = 0;
1317
- for await (const rows of rowPatches) {
1318
- for (const row of rows) {
1319
- const { schema, table } = row;
1320
- const rowKey = row.rowKey;
1321
- const toVersion = versionFromString(row.patchVersion);
1322
- const id = { schema, table, rowKey };
1323
- let patch;
1324
- if (!row.refCounts) {
1325
- patch = { type: "row", op: "del", id };
1326
- } else {
1327
- const row2 = must(
1328
- this.#pipelines.getRow(table, rowKey),
1329
- `Missing row ${table}:${stringify(rowKey)}`
1330
- );
1331
- const { contents } = contentsAndVersion(row2);
1332
- patch = { type: "row", op: "put", id, contents };
1333
- }
1334
- const patchToVersion = { patch, toVersion };
1335
- await pokers.addPatch(patchToVersion);
1336
- rowPatchCount++;
1337
- }
1338
- }
1339
- span.setAttribute("rowPatchCount", rowPatchCount);
1340
- if (rowPatchCount) {
1341
- lc.debug?.(`sent ${rowPatchCount} row patches`);
1342
- }
1343
- for (const patch of await configPatches) {
1344
- await pokers.addPatch(patch);
1345
- }
1346
- if (!usePokers) {
1347
- await pokers.end(cvr.version);
1348
- }
1349
- });
1350
- }
1351
- #processChanges(lc, timer, changes, updater, pokers) {
1352
- return startAsyncSpan(tracer, "vs.#processChanges", async () => {
1353
- const start = performance.now();
1354
- const rows = new CustomKeyMap(rowIDString);
1355
- let total = 0;
1356
- const processBatch = () => startAsyncSpan(tracer, "processBatch", async () => {
1357
- const wallElapsed = performance.now() - start;
1358
- total += rows.size;
1359
- lc.debug?.(
1360
- `processing ${rows.size} (of ${total}) rows (${wallElapsed} ms)`
1361
- );
1362
- const patches = await updater.received(lc, rows);
1363
- for (const patch of patches) {
1364
- await pokers.addPatch(patch);
1365
- }
1366
- rows.clear();
1367
- });
1368
- await startAsyncSpan(tracer, "loopingChanges", async (span) => {
1369
- for (const change of changes) {
1370
- if (change === "yield") {
1371
- await timer.yieldProcess("yield in processChanges");
1372
- continue;
1373
- }
1374
- const { type, queryID, table, rowKey, row } = change;
1375
- const rowID = { schema: "", table, rowKey };
1376
- let parsedRow = rows.get(rowID);
1377
- if (!parsedRow) {
1378
- parsedRow = { refCounts: {} };
1379
- rows.set(rowID, parsedRow);
1380
- }
1381
- parsedRow.refCounts[queryID] ??= 0;
1382
- const updateVersion = (row2) => {
1383
- const { version, contents } = contentsAndVersion(row2);
1384
- parsedRow.version = version;
1385
- parsedRow.contents = contents;
1386
- };
1387
- switch (type) {
1388
- case "add":
1389
- updateVersion(row);
1390
- parsedRow.refCounts[queryID]++;
1391
- break;
1392
- case "edit":
1393
- updateVersion(row);
1394
- break;
1395
- case "remove":
1396
- parsedRow.refCounts[queryID]--;
1397
- break;
1398
- default:
1399
- unreachable(type);
1400
- }
1401
- if (rows.size % CURSOR_PAGE_SIZE === 0) {
1402
- await processBatch();
1403
- }
1404
- }
1405
- if (rows.size) {
1406
- await processBatch();
1407
- }
1408
- span.setAttribute("totalRows", total);
1409
- });
1410
- });
1411
- }
1412
- /**
1413
- * Advance to the current snapshot of the replica and apply / send
1414
- * changes.
1415
- *
1416
- * Must be called from within the #lock.
1417
- *
1418
- * Returns false if the advancement failed due to a schema change.
1419
- */
1420
- #advancePipelines(lc, cvr) {
1421
- return startAsyncSpan(tracer, "vs.#advancePipelines", async () => {
1422
- assert(
1423
- this.#pipelines.initialized(),
1424
- "pipelines must be initialized (advancePipelines"
1425
- );
1426
- const start = performance.now();
1427
- const timer = new TimeSliceTimer(lc);
1428
- const { version, numChanges, changes } = this.#pipelines.advance(timer);
1429
- lc = lc.withContext("newVersion", version);
1430
- const updater = new CVRQueryDrivenUpdater(
1431
- this.#cvrStore,
1432
- cvr,
1433
- version,
1434
- this.#pipelines.replicaVersion
1435
- );
1436
- const pokers = startPoke(
1437
- this.#getClients(cvr.version),
1438
- updater.updatedVersion()
1439
- );
1440
- lc.debug?.(`applying ${numChanges} to advance to ${version}`);
1441
- try {
1442
- await this.#processChanges(
1443
- lc,
1444
- await timer.start(),
1445
- changes,
1446
- updater,
1447
- pokers
1448
- );
1449
- } catch (e) {
1450
- if (e instanceof ResetPipelinesSignal) {
1451
- await pokers.cancel();
1452
- return e;
1453
- }
1454
- throw e;
1455
- }
1456
- this.#cvr = await this.#flushUpdater(lc, updater);
1457
- const finalVersion = this.#cvr.version;
1458
- await pokers.end(finalVersion);
1459
- const wallTime = performance.now() - start;
1460
- const totalProcessTime = timer.totalElapsed();
1461
- lc.info?.(
1462
- `finished processing advancement of ${numChanges} changes ((process: ${totalProcessTime} ms, wall: ${wallTime} ms))`
1463
- );
1464
- this.#transactionAdvanceTime.record(totalProcessTime / 1e3);
1465
- return "success";
1466
- });
1467
- }
1468
- async inspect(context, msg) {
1469
- await this.#runInLockForClient(context, msg, this.#handleInspect);
1470
- }
1471
- // oxlint-disable-next-line require-await
1472
- #handleInspect = async (lc, clientID, body, cvr) => {
1473
- const client = must(this.#clients.get(clientID));
1474
- const auth = this.#authSession.auth;
1475
- return handleInspect(
1476
- lc,
1477
- body,
1478
- cvr,
1479
- client,
1480
- this.#inspectorDelegate,
1481
- this.id,
1482
- this.#cvrStore,
1483
- this.#config,
1484
- this.#getHeaderOptions(this.#queryConfig.forwardCookies ?? false),
1485
- this.userQueryURL,
1486
- auth?.type === "jwt" ? auth : void 0
1487
- );
1488
- };
1489
- stop() {
1490
- this.#lc.info?.("stopping view syncer");
1491
- this.#initialized.reject("shut down before initialization completed");
1492
- this.#stateChanges.cancel();
1493
- return this.#stopped.promise;
1494
- }
1495
- async #cleanup(err) {
1496
- this.clearAuth();
1497
- this.#stopTTLClockInterval();
1498
- this.#stopExpireTimer();
1499
- for (const client of this.#clients.values()) {
1500
- if (err) {
1501
- client.fail(err);
1502
- } else {
1503
- client.close(`closed clientGroupID=${this.id}`);
1504
- }
1505
- }
1506
- await this.#lock.withLock(() => {
1507
- });
1508
- this.#pipelines.destroy();
1509
- }
1510
- /**
1511
- * Test helper: Manually mark initialization as complete.
1512
- * This should only be used in tests that don't call initConnection().
1513
- */
1514
- markInitialized() {
1515
- this.#initialized.resolve("initialized");
1516
- }
1517
- }
1518
- const CURSOR_PAGE_SIZE = 1e4;
1519
- const timeSliceQueue = new Lock();
36
+ /**
37
+ * We update the ttlClock in flush that writes to the CVR but
38
+ * some flushes do not write to the CVR and in those cases we
39
+ * use a timer to update the ttlClock every minute.
40
+ */
41
+ var TTL_CLOCK_INTERVAL = 6e4;
42
+ var ViewSyncerService = class {
43
+ id;
44
+ #shard;
45
+ #lc;
46
+ #pipelines;
47
+ #stateChanges;
48
+ #drainCoordinator;
49
+ #keepaliveMs;
50
+ #slowHydrateThreshold;
51
+ #queryConfig;
52
+ #authSession;
53
+ userQueryURL;
54
+ userQueryHeaders;
55
+ #lastConnectTime = Date.now();
56
+ /**
57
+ * The TTL clock is used to determine the time at which queries are considered
58
+ * expired.
59
+ */
60
+ #ttlClock;
61
+ /**
62
+ * The base time for the TTL clock. This is used to compute the current TTL
63
+ * clock value. The first time a connection is made, this is set to the
64
+ * current time. On subsequent connections, the TTL clock is computed as the
65
+ * difference between the current time and this base time.
66
+ *
67
+ * Every time we write the ttlClock this is update to the current time. That
68
+ * way we can compute how much time has passed since the last time we set the
69
+ * ttlClock. When we set the ttlClock we just increment it by the amount of
70
+ * time that has passed since the last time we set it.
71
+ */
72
+ #ttlClockBase = Date.now();
73
+ /**
74
+ * We update the ttlClock every minute to ensure that it is not too much
75
+ * out of sync with the current time.
76
+ */
77
+ #ttlClockInterval = 0;
78
+ #clients = /* @__PURE__ */ new Map();
79
+ #lock = new Lock();
80
+ #cvrStore;
81
+ #stopped = resolver();
82
+ #initialized = resolver();
83
+ #cvr;
84
+ #pipelinesSynced = false;
85
+ #httpCookie;
86
+ #origin;
87
+ #lastAuthRevision = 0;
88
+ #expiredQueriesTimer = 0;
89
+ #setTimeout;
90
+ #customQueryTransformer;
91
+ #queryReplacements = /* @__PURE__ */ new Map();
92
+ #activeClients = getOrCreateUpDownCounter("sync", "active-clients", "Number of active sync clients");
93
+ #hydrations = getOrCreateCounter("sync", "hydration", "Number of query hydrations");
94
+ #hydrationTime = getOrCreateHistogram("sync", "hydration-time", {
95
+ description: "Time to hydrate a query.",
96
+ unit: "s"
97
+ });
98
+ #transactionAdvanceTime = getOrCreateHistogram("sync", "advance-time", {
99
+ description: "Time to advance all queries for a given client group after applying a new transaction to the replica.",
100
+ unit: "s"
101
+ });
102
+ #queryTransformations = getOrCreateCounter("sync", "query.transformations", "Number of query transformations performed");
103
+ #queryTransformationTime = getOrCreateHistogram("sync", "query.transformation-time", {
104
+ description: "Time to transform custom queries via API server",
105
+ unit: "s"
106
+ });
107
+ #queryTransformationHashChanges = getOrCreateCounter("sync", "query.transformation-hash-changes", "Number of times query transformation hash changed");
108
+ #queryTransformationNoOps = getOrCreateCounter("sync", "query.transformation-no-ops", "Number of times query transformation resulted in no-op (hash unchanged)");
109
+ #inspectorDelegate;
110
+ #config;
111
+ #runPriorityOp;
112
+ constructor(config, lc, shard, taskID, clientGroupID, cvrDb, pipelineDriver, versionChanges, drainCoordinator, slowHydrateThreshold, inspectorDelegate, customQueryTransformer, runPriorityOp, authSession, keepaliveMs = DEFAULT_KEEPALIVE_MS, setTimeoutFn = setTimeout.bind(globalThis)) {
113
+ const queryConfig = config.query?.url ? config.query : config.getQueries;
114
+ this.#config = config;
115
+ this.id = clientGroupID;
116
+ this.#shard = shard;
117
+ this.#queryConfig = queryConfig;
118
+ this.#lc = lc;
119
+ this.#pipelines = pipelineDriver;
120
+ this.#stateChanges = versionChanges;
121
+ this.#drainCoordinator = drainCoordinator;
122
+ this.#keepaliveMs = keepaliveMs;
123
+ this.#slowHydrateThreshold = slowHydrateThreshold;
124
+ this.#inspectorDelegate = inspectorDelegate;
125
+ this.#customQueryTransformer = customQueryTransformer;
126
+ this.#authSession = authSession;
127
+ this.#cvrStore = new CVRStore(lc, cvrDb, shard, taskID, clientGroupID, () => this.#stateChanges.cancel());
128
+ this.#setTimeout = setTimeoutFn;
129
+ this.#runPriorityOp = runPriorityOp;
130
+ this.keepalive();
131
+ }
132
+ get auth() {
133
+ return this.#authSession.auth;
134
+ }
135
+ clearAuth() {
136
+ this.#authSession.clear();
137
+ this.#lastAuthRevision = 0;
138
+ }
139
+ initAuthSession(userID, wireAuth) {
140
+ return this.#authSession.update(userID, wireAuth);
141
+ }
142
+ #getHeaderOptions(forwardCookie) {
143
+ return {
144
+ apiKey: this.#queryConfig.apiKey,
145
+ customHeaders: this.userQueryHeaders,
146
+ allowedClientHeaders: this.#queryConfig.allowedClientHeaders,
147
+ token: this.#authSession.auth?.raw,
148
+ cookie: forwardCookie ? this.#httpCookie : void 0,
149
+ origin: this.#origin
150
+ };
151
+ }
152
+ #runInLockWithCVR(fn) {
153
+ const rid = randomID();
154
+ this.#lc.debug?.("about to acquire lock for cvr ", rid);
155
+ return this.#lock.withLock(async () => {
156
+ this.#lc.debug?.("acquired lock in #runInLockWithCVR ", rid);
157
+ const lc = this.#lc.withContext("lock", rid);
158
+ if (!this.#stateChanges.active) {
159
+ this.#lc.debug?.("state changes are inactive");
160
+ clearTimeout(this.#expiredQueriesTimer);
161
+ throw new ProtocolErrorWithLevel({
162
+ kind: Rehome,
163
+ message: "Reconnect required",
164
+ origin: ZeroCache
165
+ }, "info");
166
+ }
167
+ if (await this.#checkForShutdownConditionsInLock()) {
168
+ this.#lc.info?.(`closing clientGroupID=${this.id}`);
169
+ this.#stateChanges.cancel();
170
+ return;
171
+ }
172
+ if (!this.#cvr) {
173
+ this.#lc.debug?.("loading cvr");
174
+ this.#cvr = await this.#runPriorityOp(lc, "loading cvr", () => this.#cvrStore.load(lc, this.#lastConnectTime));
175
+ this.#ttlClock = this.#cvr.ttlClock;
176
+ this.#ttlClockBase = Date.now();
177
+ } else {
178
+ const now = Date.now();
179
+ this.#cvr = {
180
+ ...this.#cvr,
181
+ ttlClock: this.#getTTLClock(now)
182
+ };
183
+ }
184
+ try {
185
+ await fn(lc, this.#cvr);
186
+ } catch (e) {
187
+ this.#cvr = void 0;
188
+ throw e;
189
+ }
190
+ });
191
+ }
192
+ readyState() {
193
+ return Promise.race([this.#initialized.promise, this.#drainCoordinator.draining]);
194
+ }
195
+ async run() {
196
+ try {
197
+ if (await this.readyState() === "draining") {
198
+ this.#lc.debug?.(`draining view-syncer ${this.id} before running`);
199
+ this.stop();
200
+ }
201
+ for await (const { state } of this.#stateChanges) {
202
+ if (this.#drainCoordinator.shouldDrain()) {
203
+ this.#lc.debug?.(`draining view-syncer ${this.id} (elective)`);
204
+ break;
205
+ }
206
+ assert(state === "version-ready", "state should be version-ready");
207
+ await this.#runInLockWithCVR(async (lc, cvr) => {
208
+ const clientSchema = must(cvr.clientSchema, "cvr.clientSchema missing after initialization");
209
+ if (!this.#pipelines.initialized()) this.#pipelines.init(clientSchema);
210
+ if (cvr.replicaVersion !== null && cvr.version.stateVersion !== "00" && this.#pipelines.replicaVersion < cvr.replicaVersion) {
211
+ const message = `Cannot sync from older replica: CVR=${cvr.replicaVersion}, DB=${this.#pipelines.replicaVersion}`;
212
+ lc.info?.(`resetting CVR: ${message}`);
213
+ throw new ClientNotFoundError(message);
214
+ }
215
+ if (this.#pipelinesSynced) {
216
+ const result = await this.#advancePipelines(lc, cvr);
217
+ if (result === "success") return;
218
+ lc.info?.(`resetting pipelines: ${result.message}`);
219
+ this.#pipelines.reset(clientSchema);
220
+ this.#pipelinesSynced = false;
221
+ }
222
+ const version = this.#pipelines.advanceWithoutDiff();
223
+ const cvrVer = versionString(cvr.version);
224
+ if (version < cvr.version.stateVersion) {
225
+ lc.debug?.(`replica@${version} is behind cvr@${cvrVer}`);
226
+ return;
227
+ }
228
+ lc.info?.(`init pipelines@${version} (cvr@${cvrVer})`);
229
+ await this.#hydrateUnchangedQueries(lc, cvr);
230
+ await this.#syncQueryPipelineSet(lc, cvr, "missing");
231
+ this.#pipelinesSynced = true;
232
+ });
233
+ }
234
+ if (this.#drainCoordinator.shouldDrain()) this.#drainCoordinator.drainNextIn(this.#totalHydrationTimeMs());
235
+ await this.#cleanup();
236
+ } catch (e) {
237
+ this.#lc[getLogLevel(e)]?.(`stopping view-syncer ${this.id}: ${String(e)}`, e);
238
+ await this.#cleanup(e);
239
+ } finally {
240
+ await this.#cvrStore.flushed(this.#lc).catch((e) => this.#lc[getLogLevel(e)]?.(e));
241
+ this.#lc.info?.(`view-syncer ${this.id} finished`);
242
+ this.#stopped.resolve();
243
+ }
244
+ }
245
+ #removeExpiredQueries = async (lc, cvr) => {
246
+ if (hasExpiredQueries(cvr)) {
247
+ lc = lc.withContext("method", "#removeExpiredQueries");
248
+ lc.debug?.("Queries have expired");
249
+ if (this.#pipelinesSynced) await this.#syncQueryPipelineSet(lc, cvr, "missing");
250
+ }
251
+ this.#scheduleExpireEviction(lc, cvr);
252
+ };
253
+ #totalHydrationTimeMs() {
254
+ return this.#pipelines.totalHydrationTimeMs();
255
+ }
256
+ #keepAliveUntil = 0;
257
+ /**
258
+ * Guarantees that the ViewSyncer will remain running for at least
259
+ * its configured `keepaliveMs`. This is called when establishing a
260
+ * new connection to ensure that its associated ViewSyncer isn't
261
+ * shutdown before it receives the connection.
262
+ *
263
+ * @return `true` if the ViewSyncer will stay alive, `false` if the
264
+ * ViewSyncer is shutting down.
265
+ */
266
+ keepalive() {
267
+ if (!this.#stateChanges.active) return false;
268
+ this.#keepAliveUntil = Date.now() + this.#keepaliveMs;
269
+ return true;
270
+ }
271
+ #shutdownTimer = null;
272
+ #scheduleShutdown(delayMs = 0) {
273
+ this.#shutdownTimer ??= this.#setTimeout(() => {
274
+ this.#shutdownTimer = null;
275
+ this.#runInLockWithCVR(() => {}).catch((e) => this.#stateChanges.fail(e));
276
+ }, delayMs);
277
+ }
278
+ async #checkForShutdownConditionsInLock() {
279
+ if (this.#clients.size > 0) return false;
280
+ await this.#cvrStore.flushed(this.#lc);
281
+ if (Date.now() <= this.#keepAliveUntil) {
282
+ this.#scheduleShutdown(this.#keepaliveMs);
283
+ return false;
284
+ }
285
+ return this.#clients.size === 0;
286
+ }
287
+ #deleteClientDueToDisconnect(clientID, client) {
288
+ if (this.#clients.get(clientID) === client) {
289
+ this.#clients.delete(clientID);
290
+ if (this.#clients.size === 0) {
291
+ if (this.#ttlClock !== void 0) this.#updateTTLClockInCVRWithoutLock(this.#lc);
292
+ this.#stopExpireTimer();
293
+ this.#scheduleShutdown();
294
+ }
295
+ }
296
+ }
297
+ #stopExpireTimer() {
298
+ this.#lc.debug?.("Stopping expired queries timer");
299
+ clearTimeout(this.#expiredQueriesTimer);
300
+ this.#expiredQueriesTimer = 0;
301
+ }
302
+ initConnection(ctx, initConnectionMessage) {
303
+ this.#lc.debug?.("viewSyncer.initConnection");
304
+ return startSpan(tracer, "vs.initConnection", () => {
305
+ const { clientID, profileID, wsID, baseCookie, httpCookie, origin, protocolVersion } = ctx;
306
+ this.#httpCookie = httpCookie;
307
+ this.#origin = origin;
308
+ const [, { userQueryURL, userQueryHeaders }] = initConnectionMessage;
309
+ if (this.userQueryURL === void 0) {
310
+ this.userQueryURL = userQueryURL;
311
+ this.userQueryHeaders = userQueryHeaders;
312
+ } else if (this.userQueryURL !== userQueryURL) this.#lc.warn?.("Client provided different query parameters than client group", {
313
+ clientID,
314
+ clientURL: userQueryURL,
315
+ clientGroupURL: this.userQueryURL
316
+ });
317
+ const lc = this.#lc.withContext("clientID", clientID).withContext("wsID", wsID);
318
+ const downstream = Subscription.create({ cleanup: (_, err) => {
319
+ err ? lc[getLogLevel(err)]?.(`client closed with error`, err) : lc.info?.("client closed");
320
+ this.#deleteClientDueToDisconnect(clientID, newClient);
321
+ this.#activeClients.add(-1, { [PROTOCOL_VERSION_ATTR]: protocolVersion });
322
+ } });
323
+ this.#activeClients.add(1, { [PROTOCOL_VERSION_ATTR]: protocolVersion });
324
+ if (this.#clients.size === 0) this.#ttlClockBase = Date.now();
325
+ const newClient = new ClientHandler(lc, this.id, clientID, wsID, this.#shard, baseCookie, downstream);
326
+ this.#clients.get(clientID)?.close(`replaced by wsID: ${wsID}`);
327
+ this.#clients.set(clientID, newClient);
328
+ this.#runInLockForClient(ctx, initConnectionMessage, async (lc, clientID, msg, cvr) => {
329
+ if (cvr.clientSchema === null && !msg.clientSchema) throw new ProtocolErrorWithLevel({
330
+ kind: InvalidConnectionRequest,
331
+ message: "The initConnection message for a new client group must include client schema.",
332
+ origin: ZeroCache
333
+ }, "warn");
334
+ await this.#handleConfigUpdate(lc, clientID, msg, cvr, "all", profileID ?? `cg${this.id}`);
335
+ this.#initialized.resolve("initialized");
336
+ }, newClient).catch((e) => newClient.fail(e));
337
+ return downstream;
338
+ });
339
+ }
340
+ async changeDesiredQueries(ctx, msg) {
341
+ await this.#runInLockForClient(ctx, msg, async (lc, clientID, msg, cvr) => {
342
+ let customQueryTransformMode = "missing";
343
+ const currentAuthRevision = this.#authSession.revision;
344
+ if (this.#lastAuthRevision < currentAuthRevision) {
345
+ customQueryTransformMode = "all";
346
+ lc.debug?.("Auth revision changed, setting customQueryTransformMode to all");
347
+ }
348
+ const result = await this.#handleConfigUpdate(lc, clientID, msg, cvr, customQueryTransformMode);
349
+ if (customQueryTransformMode === "all") this.#lastAuthRevision = currentAuthRevision;
350
+ return result;
351
+ });
352
+ }
353
+ async updateAuth(ctx, msg) {
354
+ await this.#runInLockForClient(ctx, msg, async (lc, clientID, _, cvr) => {
355
+ const authResult = await this.#authSession.update(ctx.userID, msg[1].auth);
356
+ if (!authResult.ok) throw new ProtocolErrorWithLevel(authResult.error, "warn");
357
+ const currentAuthRevision = this.#authSession.revision;
358
+ if (this.#lastAuthRevision >= currentAuthRevision) {
359
+ lc.debug?.("Auth revision unchanged, skipping query re-transformation");
360
+ return;
361
+ } else lc.debug?.("Auth revision changed, re-transforming queries");
362
+ const result = await this.#handleConfigUpdate(lc, clientID, {}, cvr, "all");
363
+ this.#lastAuthRevision = currentAuthRevision;
364
+ return result;
365
+ });
366
+ }
367
+ async deleteClients(ctx, msg) {
368
+ return await this.#runInLockForClient(ctx, [msg[0], { deleted: msg[1] }], (lc, clientID, msg, cvr) => this.#handleConfigUpdate(lc, clientID, msg, cvr, "missing")) ?? [];
369
+ }
370
+ #getTTLClock(now) {
371
+ const delta = now - this.#ttlClockBase;
372
+ assert(this.#ttlClock !== void 0, "ttlClock should be defined");
373
+ const ttlClock = ttlClockFromNumber(ttlClockAsNumber(this.#ttlClock) + delta);
374
+ assert(ttlClockAsNumber(ttlClock) <= now, "ttlClock should be less than or equal to now");
375
+ this.#ttlClock = ttlClock;
376
+ this.#ttlClockBase = now;
377
+ return ttlClock;
378
+ }
379
+ #flushUpdater(lc, updater) {
380
+ return startAsyncSpan(tracer, "vs.#flushUpdater", () => this.#runPriorityOp(lc, "flushing cvr", async () => {
381
+ const now = Date.now();
382
+ const ttlClock = this.#getTTLClock(now);
383
+ const { cvr, flushed } = await updater.flush(lc, this.#lastConnectTime, now, ttlClock);
384
+ if (flushed) this.#startTTLClockInterval(lc);
385
+ return cvr;
386
+ }));
387
+ }
388
+ #startTTLClockInterval(lc) {
389
+ this.#stopTTLClockInterval();
390
+ this.#ttlClockInterval = this.#setTimeout(() => {
391
+ this.#updateTTLClockInCVRWithoutLock(lc);
392
+ this.#startTTLClockInterval(lc);
393
+ }, TTL_CLOCK_INTERVAL);
394
+ }
395
+ #stopTTLClockInterval() {
396
+ clearTimeout(this.#ttlClockInterval);
397
+ this.#ttlClockInterval = 0;
398
+ }
399
+ #updateTTLClockInCVRWithoutLock(lc) {
400
+ const rid = randomID();
401
+ lc.debug?.("Syncing ttlClock", rid);
402
+ const start = Date.now();
403
+ const ttlClock = this.#getTTLClock(start);
404
+ this.#cvrStore.updateTTLClock(ttlClock, start).then(() => {
405
+ lc.debug?.("Synced ttlClock", rid, `in ${Date.now() - start} ms`);
406
+ }).catch((e) => {
407
+ lc.error?.("failed to update TTL clock", rid, `after ${Date.now() - start} ms`, e);
408
+ });
409
+ }
410
+ async #updateCVRConfig(lc, cvr, clientID, customQueryTransformMode, fn) {
411
+ const updater = new CVRConfigDrivenUpdater(this.#cvrStore, cvr, this.#shard);
412
+ updater.ensureClient(clientID);
413
+ const patches = fn(updater);
414
+ this.#cvr = await this.#flushUpdater(lc, updater);
415
+ if (cmpVersions(cvr.version, this.#cvr.version) < 0) {
416
+ const newCVR = this.#cvr;
417
+ await startAsyncSpan(tracer, "vs.#updateCVRConfig.pokeClients", async () => {
418
+ const pokers = startPoke(this.#getClients(cvr.version), newCVR.version);
419
+ for (const patch of patches) await pokers.addPatch(patch);
420
+ await pokers.end(newCVR.version);
421
+ });
422
+ }
423
+ if (this.#pipelinesSynced) await this.#syncQueryPipelineSet(lc, this.#cvr, customQueryTransformMode);
424
+ return this.#cvr;
425
+ }
426
+ /**
427
+ * Runs the given `fn` to process the `msg` from within the `#lock`,
428
+ * optionally adding the `newClient` if supplied.
429
+ */
430
+ #runInLockForClient(ctx, msg, fn, newClient) {
431
+ this.#lc.debug?.("viewSyncer.#runInLockForClient");
432
+ const { clientID, wsID } = ctx;
433
+ const [cmd, body] = msg;
434
+ if (newClient || !this.#clients.has(clientID)) this.#lastConnectTime = Date.now();
435
+ return startAsyncSpan(tracer, `vs.#runInLockForClient(${cmd})`, async () => {
436
+ let client;
437
+ let result;
438
+ try {
439
+ await this.#runInLockWithCVR(async (lc, cvr) => {
440
+ lc = lc.withContext("clientID", clientID).withContext("wsID", wsID).withContext("cmd", cmd);
441
+ lc.debug?.("acquired lock for cvr");
442
+ client = this.#clients.get(clientID);
443
+ if (client?.wsID !== wsID) {
444
+ lc.debug?.("mismatched wsID", client?.wsID, wsID);
445
+ return;
446
+ }
447
+ if (newClient) {
448
+ assert(newClient === client, "newClient must match existing client");
449
+ checkClientAndCVRVersions(client.version(), cvr.version);
450
+ } else if (!this.#clients.has(clientID)) lc.warn?.(`Processing ${cmd} before initConnection was received`);
451
+ lc.debug?.(cmd, body);
452
+ result = await fn(lc, clientID, body, cvr);
453
+ });
454
+ } catch (e) {
455
+ const lc = this.#lc.withContext("clientID", clientID).withContext("wsID", wsID).withContext("cmd", cmd);
456
+ lc[getLogLevel(e)]?.(`closing connection with error`, e);
457
+ if (isTransformAuthFailure(e)) {
458
+ lc.debug?.("Auth failure detected in transform response");
459
+ this.clearAuth();
460
+ }
461
+ if (client) client.fail(e);
462
+ else throw e;
463
+ }
464
+ return result;
465
+ });
466
+ }
467
+ #getClients(atVersion) {
468
+ const clients = [...this.#clients.values()];
469
+ return atVersion ? clients.filter((c) => cmpVersions(c.version() ?? EMPTY_CVR_VERSION, atVersion) === 0) : clients;
470
+ }
471
+ #handleConfigUpdate = (lc, clientID, { clientSchema, deleted, desiredQueriesPatch, activeClients }, cvr, customQueryTransformMode, profileID) => startAsyncSpan(tracer, "vs.#handleConfigUpdate", async () => {
472
+ const deletedClientIDs = [];
473
+ const deletedClientGroupIDs = [];
474
+ cvr = await this.#updateCVRConfig(lc, cvr, clientID, customQueryTransformMode, (updater) => {
475
+ const { ttlClock } = cvr;
476
+ const patches = [];
477
+ if (clientSchema) updater.setClientSchema(lc, clientSchema);
478
+ if (profileID) updater.setProfileID(lc, profileID);
479
+ lc.debug?.(`applying ${desiredQueriesPatch?.length ?? 0} query patches`);
480
+ if (desiredQueriesPatch?.length) for (const patch of desiredQueriesPatch) switch (patch.op) {
481
+ case "put":
482
+ patches.push(...updater.putDesiredQueries(clientID, [patch]));
483
+ break;
484
+ case "del":
485
+ patches.push(...updater.markDesiredQueriesAsInactive(clientID, [patch.hash], ttlClock));
486
+ break;
487
+ case "clear":
488
+ patches.push(...updater.clearDesiredQueries(clientID));
489
+ break;
490
+ }
491
+ const clientIDsToDelete = /* @__PURE__ */ new Set();
492
+ if (activeClients) {
493
+ const allClientIDs = Object.keys(cvr.clients);
494
+ const activeClientsSet = new Set(activeClients);
495
+ for (const id of allClientIDs) if (!activeClientsSet.has(id)) clientIDsToDelete.add(id);
496
+ }
497
+ if (deleted?.clientIDs?.length) for (const cid of deleted.clientIDs) {
498
+ assert(cid !== clientID, "cannot delete self");
499
+ clientIDsToDelete.add(cid);
500
+ }
501
+ for (const cid of clientIDsToDelete) {
502
+ const patchesDueToClient = updater.deleteClient(cid, ttlClock);
503
+ patches.push(...patchesDueToClient);
504
+ deletedClientIDs.push(cid);
505
+ }
506
+ if (deleted?.clientGroupIDs?.length) lc.debug?.(`ignoring ${deleted.clientGroupIDs.length} deprecated client group deletes`);
507
+ return patches;
508
+ });
509
+ if (deletedClientIDs.length && deleted?.clientIDs?.length || deletedClientGroupIDs.length) {
510
+ const clients = this.#getClients();
511
+ await startAsyncSpan(tracer, "vs.#handleConfigUpdate.sendDeleteClients", () => Promise.allSettled(clients.map((client) => client.sendDeleteClients(lc, deletedClientIDs, deletedClientGroupIDs))));
512
+ }
513
+ this.#scheduleExpireEviction(lc, cvr);
514
+ return deletedClientIDs;
515
+ });
516
+ #scheduleExpireEviction(lc, cvr) {
517
+ const { ttlClock } = cvr;
518
+ this.#stopExpireTimer();
519
+ const next = nextEvictionTime(cvr);
520
+ if (next === void 0) {
521
+ lc.debug?.("no inactive queries with ttl");
522
+ return;
523
+ }
524
+ const delay = Math.max(50, Math.min(ttlClockAsNumber(next) - ttlClockAsNumber(ttlClock) + 50, MAX_TTL_MS));
525
+ lc.debug?.("Scheduling eviction timer to run in ", delay, "ms");
526
+ this.#expiredQueriesTimer = this.#setTimeout(() => {
527
+ this.#expiredQueriesTimer = 0;
528
+ this.#runInLockWithCVR((lc, cvr) => this.#removeExpiredQueries(lc, cvr)).catch((e) => this.#stateChanges.fail(e));
529
+ }, delay);
530
+ }
531
+ /**
532
+ * Adds and hydrates pipelines for queries whose results are already
533
+ * recorded in the CVR. Namely:
534
+ *
535
+ * 1. The CVR state version and database version are the same.
536
+ * 2. The transformation hash of the queries equal those in the CVR.
537
+ *
538
+ * Note that by definition, only "got" queries can satisfy condition (2),
539
+ * as desired queries do not have a transformation hash.
540
+ *
541
+ * This is an initialization step that sets up pipeline state without
542
+ * the expensive of loading and diffing CVR row state.
543
+ *
544
+ * This must be called from within the #lock.
545
+ */
546
+ async #hydrateUnchangedQueries(lc, cvr) {
547
+ assert(this.#pipelines.initialized(), "pipelines must be initialized");
548
+ const dbVersion = this.#pipelines.currentVersion();
549
+ const cvrVersion = cvr.version;
550
+ if (cvrVersion.stateVersion !== dbVersion) {
551
+ lc.info?.(`CVR (${versionToCookie(cvrVersion)}) is behind db ${dbVersion}`);
552
+ return;
553
+ }
554
+ const gotQueries = Object.entries(cvr.queries).filter(([_, state]) => state.transformationHash !== void 0);
555
+ const customQueries = /* @__PURE__ */ new Map();
556
+ const otherQueries = [];
557
+ for (const [, query] of gotQueries) {
558
+ if (query.type !== "internal" && Object.values(query.clientState).every(({ inactivatedAt }) => inactivatedAt !== void 0)) continue;
559
+ if (query.type === "custom") customQueries.set(query.id, query);
560
+ else otherQueries.push(query);
561
+ }
562
+ const transformedQueries = [];
563
+ if (customQueries.size > 0 && !this.#customQueryTransformer) lc.warn?.("Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache.");
564
+ const customQueryTransformer = this.#customQueryTransformer;
565
+ if (customQueryTransformer && customQueries.size > 0) {
566
+ const transformedCustomQueries = await this.#runPriorityOp(lc, "#hydrateUnchangedQueries transforming custom queries", () => customQueryTransformer.transform(this.#getHeaderOptions(this.#queryConfig.forwardCookies), customQueries.values(), this.userQueryURL));
567
+ if (Array.isArray(transformedCustomQueries)) {
568
+ for (const q of transformedCustomQueries) if (!("error" in q) && q.transformationHash === customQueries.get(q.id)?.transformationHash) transformedQueries.push(q);
569
+ }
570
+ }
571
+ for (const q of otherQueries) {
572
+ const auth = this.#authSession.auth;
573
+ const transformed = transformAndHashQuery(lc, q.id, q.ast, must(this.#pipelines.currentPermissions()).permissions ?? { tables: {} }, auth?.type === "jwt" ? auth : void 0, q.type === "internal");
574
+ if (transformed.transformationHash === q.transformationHash) transformedQueries.push(transformed);
575
+ }
576
+ for (const { id: queryID, transformationHash, transformedAst } of transformedQueries) {
577
+ const timer = new TimeSliceTimer(lc);
578
+ let count = 0;
579
+ await startAsyncSpan(tracer, "vs.#hydrateUnchangedQueries.addQuery", async (span) => {
580
+ span.setAttribute("queryHash", queryID);
581
+ span.setAttribute("transformationHash", transformationHash);
582
+ span.setAttribute("table", transformedAst.table);
583
+ for (const change of this.#pipelines.addQuery(transformationHash, queryID, transformedAst, await timer.start())) if (change === "yield") await timer.yieldProcess("yield in hydrateUnchangedQueries");
584
+ else count++;
585
+ });
586
+ const elapsed = timer.totalElapsed();
587
+ this.#hydrations.add(1);
588
+ this.#hydrationTime.record(elapsed / 1e3);
589
+ this.#addQueryMaterializationServerMetric(transformationHash, elapsed);
590
+ lc.debug?.(`hydrated ${count} rows for ${queryID} (${elapsed} ms)`);
591
+ }
592
+ }
593
+ #processTransformedCustomQueries(lc, transformedCustomQueries, cb, customQueryMap) {
594
+ if ("kind" in transformedCustomQueries) {
595
+ this.#sendQueryTransformErrorToClients(customQueryMap, transformedCustomQueries);
596
+ return transformedCustomQueries.queryIDs;
597
+ }
598
+ const appQueryErrors = [];
599
+ for (const q of transformedCustomQueries) {
600
+ if ("error" in q) {
601
+ const errorMessage = `Error transforming custom query ${q.name}: ${q.error}${q.details ? ` ${JSON.stringify(q.details)}` : ""}`;
602
+ lc.warn?.(errorMessage, q);
603
+ appQueryErrors.push(q);
604
+ continue;
605
+ }
606
+ cb(q);
607
+ }
608
+ this.#sendQueryTransformErrorToClients(customQueryMap, appQueryErrors);
609
+ return appQueryErrors.map((q) => q.id);
610
+ }
611
+ #sendQueryTransformErrorToClients(customQueryMap, errorOrErrors) {
612
+ const getAffectedClientIDs = (queryIDs) => {
613
+ const clientIds = /* @__PURE__ */ new Set();
614
+ for (const queryID of queryIDs) {
615
+ const q = customQueryMap.get(queryID);
616
+ assert(q, `got an error for query ${queryID} that does not map back to a custom query`);
617
+ Object.keys(q.clientState).forEach((id) => clientIds.add(id));
618
+ }
619
+ return clientIds;
620
+ };
621
+ if ("queryIDs" in errorOrErrors) {
622
+ for (const clientId of getAffectedClientIDs(errorOrErrors.queryIDs)) this.#clients.get(clientId)?.sendQueryTransformFailedError(errorOrErrors);
623
+ return;
624
+ }
625
+ const appErrorGroups = /* @__PURE__ */ new Map();
626
+ for (const err of errorOrErrors) for (const clientId of getAffectedClientIDs([err.id])) {
627
+ const group = appErrorGroups.get(clientId) ?? [];
628
+ group.push(err);
629
+ appErrorGroups.set(clientId, group);
630
+ }
631
+ for (const [clientId, errors] of appErrorGroups) this.#clients.get(clientId)?.sendQueryTransformApplicationErrors(errors);
632
+ }
633
+ #addQueryMaterializationServerMetric(queryID, elapsed) {
634
+ this.#inspectorDelegate.addMetric("query-materialization-server", elapsed, queryID);
635
+ }
636
+ /**
637
+ * Adds and/or removes queries to/from the PipelineDriver to bring it
638
+ * in sync with the set of queries in the CVR (both got and desired).
639
+ * If queries are added, removed, or queried due to a new state version,
640
+ * a new CVR version is created and pokes sent to connected clients.
641
+ *
642
+ * This must be called from within the #lock.
643
+ */
644
+ #syncQueryPipelineSet(lc, cvr, customQueryTransformMode) {
645
+ return startAsyncSpan(tracer, "vs.#syncQueryPipelineSet", async () => {
646
+ assert(this.#pipelines.initialized(), "pipelines must be initialized (syncQueryPipelineSet)");
647
+ if (this.#ttlClock === void 0) this.#ttlClock = cvr.ttlClock;
648
+ const now = Date.now();
649
+ const ttlClock = this.#getTTLClock(now);
650
+ const cvrQueryEntires = Object.entries(cvr.queries);
651
+ const customQueries = /* @__PURE__ */ new Map();
652
+ const otherQueries = [];
653
+ const transformedQueries = [];
654
+ for (const [id, query] of cvrQueryEntires) if (query.type === "custom") {
655
+ assert(id === query.id, "custom query id mismatch");
656
+ customQueries.set(id, query);
657
+ } else otherQueries.push({
658
+ id,
659
+ query
660
+ });
661
+ for (const { id, query: origQuery } of otherQueries) {
662
+ assert(id === origQuery.id, "query id mismatch");
663
+ const auth = this.#authSession.auth;
664
+ const transformed = transformAndHashQuery(lc, origQuery.id, origQuery.ast, must(this.#pipelines.currentPermissions()).permissions ?? { tables: {} }, auth?.type === "jwt" ? auth : void 0, origQuery.type === "internal");
665
+ transformedQueries.push({
666
+ id,
667
+ origQuery,
668
+ transformed
669
+ });
670
+ }
671
+ if (customQueries.size > 0 && !this.#customQueryTransformer) lc.warn?.("Custom/named queries were requested but no `ZERO_QUERY_URL` is configured for Zero Cache.");
672
+ let erroredQueryIDs;
673
+ const customQueriesToTransform = customQueryTransformMode === "all" ? [...customQueries.values()] : customQueryTransformMode && [...customQueries.values()].filter((q) => !this.#pipelines.queries().has(q.id));
674
+ const customQueryTransformer = this.#customQueryTransformer;
675
+ if (customQueryTransformer && customQueriesToTransform.length > 0) {
676
+ const transformStart = performance.now();
677
+ let transformedCustomQueries;
678
+ try {
679
+ transformedCustomQueries = await this.#runPriorityOp(lc, "#syncQueryPipelineSet transforming custom queries", () => customQueryTransformer.transform(this.#getHeaderOptions(true), customQueriesToTransform, this.userQueryURL));
680
+ if (!Array.isArray(transformedCustomQueries) && transformedCustomQueries.kind === "TransformFailed") throw new ProtocolErrorWithLevel(transformedCustomQueries, "warn");
681
+ else this.#queryTransformations.add(1, { result: "success" });
682
+ } catch (e) {
683
+ this.#queryTransformations.add(1, { result: "error" });
684
+ throw e;
685
+ } finally {
686
+ const transformDuration = (performance.now() - transformStart) / 1e3;
687
+ this.#queryTransformationTime.record(transformDuration);
688
+ }
689
+ const successfullyTransformedCustomQueries = /* @__PURE__ */ new Map();
690
+ erroredQueryIDs = this.#processTransformedCustomQueries(lc, transformedCustomQueries, (q) => {
691
+ const origQuery = customQueries.get(q.id);
692
+ if (origQuery) {
693
+ successfullyTransformedCustomQueries.set(q.id, q);
694
+ transformedQueries.push({
695
+ id: q.id,
696
+ origQuery,
697
+ transformed: q
698
+ });
699
+ }
700
+ }, customQueries);
701
+ for (const [queryID, newTransform] of successfullyTransformedCustomQueries) {
702
+ const existingTransformHash = cvr.queries[queryID]?.transformationHash;
703
+ if (existingTransformHash) {
704
+ const oldHash = existingTransformHash;
705
+ const newHash = newTransform.transformationHash;
706
+ if (oldHash !== newHash) {
707
+ lc.info?.(`Query ${queryID} transformation changed: ${oldHash} -> ${newHash}`);
708
+ this.#checkForThrashing(queryID);
709
+ this.#queryTransformationHashChanges.add(1);
710
+ } else this.#queryTransformationNoOps.add(1);
711
+ }
712
+ }
713
+ }
714
+ const removeQueriesQueryIds = new Set(Object.values(cvr.queries).filter((q) => expired(ttlClock, q)).map((q) => q.id).concat(erroredQueryIDs || []));
715
+ const addQueries = transformedQueries.map(({ id, transformed }) => ({
716
+ id,
717
+ ast: transformed.transformedAst,
718
+ transformationHash: transformed.transformationHash
719
+ })).filter((q) => !removeQueriesQueryIds.has(q.id) && this.#pipelines.queries().get(q.id)?.transformationHash !== q.transformationHash);
720
+ for (const q of addQueries) {
721
+ const orig = cvr.queries[q.id];
722
+ lc.debug?.("ViewSyncer adding query", q.ast, "transformed from", orig.type === "custom" ? orig.name : orig.ast);
723
+ }
724
+ if (addQueries.length > 0 || removeQueriesQueryIds.size > 0) await this.#addAndRemoveQueries(lc, cvr, addQueries, [...removeQueriesQueryIds].map((id) => ({ id })));
725
+ else await this.#catchupClients(lc, cvr);
726
+ });
727
+ }
728
+ /**
729
+ * Check if a query is being replaced too frequently (thrashing).
730
+ * Logs a warning if the query has been replaced more than 3 times in 60 seconds.
731
+ */
732
+ #checkForThrashing(queryID) {
733
+ const THRASH_WINDOW_MS = 6e4;
734
+ const THRASH_THRESHOLD = 3;
735
+ const now = Date.now();
736
+ let record = this.#queryReplacements.get(queryID);
737
+ if (!record) {
738
+ record = {
739
+ count: 1,
740
+ windowStart: now
741
+ };
742
+ this.#queryReplacements.set(queryID, record);
743
+ return;
744
+ }
745
+ if (now - record.windowStart > THRASH_WINDOW_MS) {
746
+ this.#queryReplacements.delete(queryID);
747
+ this.#queryReplacements.set(queryID, {
748
+ count: 1,
749
+ windowStart: now
750
+ });
751
+ return;
752
+ }
753
+ record.count++;
754
+ if (record.count >= THRASH_THRESHOLD) this.#lc.warn?.(`Query thrashing detected for query ${queryID}. ${record.count} replacements in 60s. This may indicate clients with different auth contexts connecting to the same client group.`);
755
+ }
756
+ #addAndRemoveQueries(lc, cvr, addQueries, removeQueries) {
757
+ return startAsyncSpan(tracer, "vs.#addAndRemoveQueries", async () => {
758
+ assert(addQueries.length > 0 || removeQueries.length > 0, "Must have queries to add or remove");
759
+ const start = performance.now();
760
+ const stateVersion = this.#pipelines.currentVersion();
761
+ lc = lc.withContext("stateVersion", stateVersion);
762
+ lc.info?.(`hydrating ${addQueries.length} queries`);
763
+ const updater = new CVRQueryDrivenUpdater(this.#cvrStore, cvr, stateVersion, this.#pipelines.replicaVersion);
764
+ const { newVersion, queryPatches } = updater.trackQueries(lc, addQueries, removeQueries);
765
+ const pokers = startPoke(this.#getClients(), newVersion);
766
+ for (const patch of queryPatches) await pokers.addPatch(patch);
767
+ for (const q of removeQueries) {
768
+ this.#pipelines.removeQuery(q.id);
769
+ this.#inspectorDelegate.removeQuery(q.id);
770
+ this.#queryReplacements.delete(q.id);
771
+ }
772
+ let totalProcessTime = 0;
773
+ const timer = new TimeSliceTimer(lc);
774
+ const pipelines = this.#pipelines;
775
+ const hydrations = this.#hydrations;
776
+ const hydrationTime = this.#hydrationTime;
777
+ const self = this;
778
+ await yieldProcess(lc);
779
+ function* generateRowChanges(slowHydrateThreshold) {
780
+ for (const q of addQueries) {
781
+ lc = lc.withContext("hash", q.id).withContext("transformationHash", q.transformationHash);
782
+ lc.debug?.(`adding pipeline for query`, q.ast);
783
+ yield* pipelines.addQuery(q.transformationHash, q.id, q.ast, timer.startWithoutYielding());
784
+ const elapsed = timer.stop();
785
+ totalProcessTime += elapsed;
786
+ self.#addQueryMaterializationServerMetric(q.id, elapsed);
787
+ if (elapsed > slowHydrateThreshold) lc.warn?.("Slow query materialization", elapsed, q.ast);
788
+ manualSpan(tracer, "vs.addAndConsumeQuery", elapsed, {
789
+ hash: q.id,
790
+ transformationHash: q.transformationHash
791
+ });
792
+ }
793
+ hydrations.add(1);
794
+ hydrationTime.record(totalProcessTime / 1e3);
795
+ }
796
+ await this.#processChanges(lc, timer, generateRowChanges(this.#slowHydrateThreshold), updater, pokers);
797
+ for (const patch of await updater.deleteUnreferencedRows(lc)) await pokers.addPatch(patch);
798
+ this.#cvr = await this.#flushUpdater(lc, updater);
799
+ const finalVersion = this.#cvr.version;
800
+ await this.#catchupClients(lc, cvr, finalVersion, addQueries.map((q) => q.id), pokers);
801
+ await pokers.end(finalVersion);
802
+ const wallTime = performance.now() - start;
803
+ lc.info?.(`finished processing queries (process: ${totalProcessTime} ms, wall: ${wallTime} ms)`);
804
+ });
805
+ }
806
+ /**
807
+ * @param cvr The CVR to which clients should be caught up to. This does
808
+ * not necessarily need to be the current CVR.
809
+ * @param current The expected current CVR version. Before performing
810
+ * catchup, the snapshot read will verify that the CVR has not been
811
+ * concurrently modified. Note that this only needs to be done for
812
+ * catchup because it is the only time data from the CVR DB is
813
+ * "exported" without being gated by a CVR flush (which provides
814
+ * concurrency protection in all other cases).
815
+ *
816
+ * If unspecified, the version of the `cvr` is used.
817
+ * @param excludeQueryHashes Exclude patches from rows associated with
818
+ * the specified queries.
819
+ * @param usePokers If specified, sends pokes on existing PokeHandlers,
820
+ * in which case the caller is responsible for sending the `pokeEnd`
821
+ * messages. If unspecified, the pokes will be started and ended
822
+ * using the version from the supplied `cvr`.
823
+ */
824
+ #catchupClients(lc, cvr, current, excludeQueryHashes = [], usePokers) {
825
+ return startAsyncSpan(tracer, "vs.#catchupClients", async (span) => {
826
+ current ??= cvr.version;
827
+ const clients = this.#getClients();
828
+ const pokers = usePokers ?? startPoke(clients, cvr.version);
829
+ span.setAttribute("numClients", clients.length);
830
+ const catchupFrom = clients.map((c) => c.version()).reduce((a, b) => cmpVersions(a, b) < 0 ? a : b, cvr.version);
831
+ const rowPatches = this.#cvrStore.catchupRowPatches(lc, catchupFrom, cvr, current, excludeQueryHashes);
832
+ const configPatches = this.#cvrStore.catchupConfigPatches(lc, catchupFrom, cvr, current);
833
+ configPatches.catch(() => {});
834
+ let rowPatchCount = 0;
835
+ for await (const rows of rowPatches) for (const row of rows) {
836
+ const { schema, table } = row;
837
+ const rowKey = row.rowKey;
838
+ const toVersion = versionFromString(row.patchVersion);
839
+ const id = {
840
+ schema,
841
+ table,
842
+ rowKey
843
+ };
844
+ let patch;
845
+ if (!row.refCounts) patch = {
846
+ type: "row",
847
+ op: "del",
848
+ id
849
+ };
850
+ else {
851
+ const { contents } = contentsAndVersion(must(this.#pipelines.getRow(table, rowKey), `Missing row ${table}:${stringify(rowKey)}`));
852
+ patch = {
853
+ type: "row",
854
+ op: "put",
855
+ id,
856
+ contents
857
+ };
858
+ }
859
+ const patchToVersion = {
860
+ patch,
861
+ toVersion
862
+ };
863
+ await pokers.addPatch(patchToVersion);
864
+ rowPatchCount++;
865
+ }
866
+ span.setAttribute("rowPatchCount", rowPatchCount);
867
+ if (rowPatchCount) lc.debug?.(`sent ${rowPatchCount} row patches`);
868
+ for (const patch of await configPatches) await pokers.addPatch(patch);
869
+ if (!usePokers) await pokers.end(cvr.version);
870
+ });
871
+ }
872
+ #processChanges(lc, timer, changes, updater, pokers) {
873
+ return startAsyncSpan(tracer, "vs.#processChanges", async () => {
874
+ const start = performance.now();
875
+ const rows = new CustomKeyMap(rowIDString);
876
+ let total = 0;
877
+ const processBatch = () => startAsyncSpan(tracer, "processBatch", async () => {
878
+ const wallElapsed = performance.now() - start;
879
+ total += rows.size;
880
+ lc.debug?.(`processing ${rows.size} (of ${total}) rows (${wallElapsed} ms)`);
881
+ const patches = await updater.received(lc, rows);
882
+ for (const patch of patches) await pokers.addPatch(patch);
883
+ rows.clear();
884
+ });
885
+ await startAsyncSpan(tracer, "loopingChanges", async (span) => {
886
+ for (const change of changes) {
887
+ if (change === "yield") {
888
+ await timer.yieldProcess("yield in processChanges");
889
+ continue;
890
+ }
891
+ const { type, queryID, table, rowKey, row } = change;
892
+ const rowID = {
893
+ schema: "",
894
+ table,
895
+ rowKey
896
+ };
897
+ let parsedRow = rows.get(rowID);
898
+ if (!parsedRow) {
899
+ parsedRow = { refCounts: {} };
900
+ rows.set(rowID, parsedRow);
901
+ }
902
+ parsedRow.refCounts[queryID] ??= 0;
903
+ const updateVersion = (row) => {
904
+ const { version, contents } = contentsAndVersion(row);
905
+ parsedRow.version = version;
906
+ parsedRow.contents = contents;
907
+ };
908
+ switch (type) {
909
+ case "add":
910
+ updateVersion(row);
911
+ parsedRow.refCounts[queryID]++;
912
+ break;
913
+ case "edit":
914
+ updateVersion(row);
915
+ break;
916
+ case "remove":
917
+ parsedRow.refCounts[queryID]--;
918
+ break;
919
+ default: unreachable(type);
920
+ }
921
+ if (rows.size % CURSOR_PAGE_SIZE === 0) await processBatch();
922
+ }
923
+ if (rows.size) await processBatch();
924
+ span.setAttribute("totalRows", total);
925
+ });
926
+ });
927
+ }
928
+ /**
929
+ * Advance to the current snapshot of the replica and apply / send
930
+ * changes.
931
+ *
932
+ * Must be called from within the #lock.
933
+ *
934
+ * Returns false if the advancement failed due to a schema change.
935
+ */
936
+ #advancePipelines(lc, cvr) {
937
+ return startAsyncSpan(tracer, "vs.#advancePipelines", async () => {
938
+ assert(this.#pipelines.initialized(), "pipelines must be initialized (advancePipelines");
939
+ const start = performance.now();
940
+ const timer = new TimeSliceTimer(lc);
941
+ const { version, numChanges, changes } = this.#pipelines.advance(timer);
942
+ lc = lc.withContext("newVersion", version);
943
+ const updater = new CVRQueryDrivenUpdater(this.#cvrStore, cvr, version, this.#pipelines.replicaVersion);
944
+ const pokers = startPoke(this.#getClients(cvr.version), updater.updatedVersion());
945
+ lc.debug?.(`applying ${numChanges} to advance to ${version}`);
946
+ try {
947
+ await this.#processChanges(lc, await timer.start(), changes, updater, pokers);
948
+ } catch (e) {
949
+ if (e instanceof ResetPipelinesSignal) {
950
+ await pokers.cancel();
951
+ return e;
952
+ }
953
+ throw e;
954
+ }
955
+ this.#cvr = await this.#flushUpdater(lc, updater);
956
+ const finalVersion = this.#cvr.version;
957
+ await pokers.end(finalVersion);
958
+ const wallTime = performance.now() - start;
959
+ const totalProcessTime = timer.totalElapsed();
960
+ lc.info?.(`finished processing advancement of ${numChanges} changes ((process: ${totalProcessTime} ms, wall: ${wallTime} ms))`);
961
+ this.#transactionAdvanceTime.record(totalProcessTime / 1e3);
962
+ return "success";
963
+ });
964
+ }
965
+ async inspect(context, msg) {
966
+ await this.#runInLockForClient(context, msg, this.#handleInspect);
967
+ }
968
+ #handleInspect = async (lc, clientID, body, cvr) => {
969
+ const client = must(this.#clients.get(clientID));
970
+ const auth = this.#authSession.auth;
971
+ return handleInspect(lc, body, cvr, client, this.#inspectorDelegate, this.id, this.#cvrStore, this.#config, this.#getHeaderOptions(this.#queryConfig.forwardCookies ?? false), this.userQueryURL, auth?.type === "jwt" ? auth : void 0);
972
+ };
973
+ stop() {
974
+ this.#lc.info?.("stopping view syncer");
975
+ this.#initialized.reject("shut down before initialization completed");
976
+ this.#stateChanges.cancel();
977
+ return this.#stopped.promise;
978
+ }
979
+ async #cleanup(err) {
980
+ this.clearAuth();
981
+ this.#stopTTLClockInterval();
982
+ this.#stopExpireTimer();
983
+ for (const client of this.#clients.values()) if (err) client.fail(err);
984
+ else client.close(`closed clientGroupID=${this.id}`);
985
+ await this.#lock.withLock(() => {});
986
+ this.#pipelines.destroy();
987
+ }
988
+ /**
989
+ * Test helper: Manually mark initialization as complete.
990
+ * This should only be used in tests that don't call initConnection().
991
+ */
992
+ markInitialized() {
993
+ this.#initialized.resolve("initialized");
994
+ }
995
+ };
996
+ var CURSOR_PAGE_SIZE = 1e4;
997
+ var timeSliceQueue = new Lock();
1520
998
  function yieldProcess(_lc) {
1521
- return timeSliceQueue.withLock(() => new Promise(setImmediate));
999
+ return timeSliceQueue.withLock(() => new Promise(setImmediate));
1522
1000
  }
1523
1001
  function contentsAndVersion(row) {
1524
- const { [ZERO_VERSION_COLUMN_NAME]: version, ...contents } = row;
1525
- if (typeof version !== "string" || version.length === 0) {
1526
- throw new Error(`Invalid _0_version in ${stringify(row)}`);
1527
- }
1528
- return { contents, version };
1002
+ const { [ZERO_VERSION_COLUMN_NAME]: version, ...contents } = row;
1003
+ if (typeof version !== "string" || version.length === 0) throw new Error(`Invalid _0_version in ${stringify(row)}`);
1004
+ return {
1005
+ contents,
1006
+ version
1007
+ };
1529
1008
  }
1530
- const NEW_CVR_VERSION = { stateVersion: "00" };
1009
+ var NEW_CVR_VERSION = { stateVersion: "00" };
1531
1010
  function checkClientAndCVRVersions(client, cvr) {
1532
- if (cmpVersions(cvr, NEW_CVR_VERSION) === 0 && cmpVersions(client, NEW_CVR_VERSION) > 0) {
1533
- throw new ClientNotFoundError("Client not found");
1534
- }
1535
- if (cmpVersions(client, cvr) > 0) {
1536
- throw new ProtocolError({
1537
- kind: InvalidConnectionRequestBaseCookie,
1538
- message: `CVR is at version ${versionString(cvr)}`,
1539
- origin: ZeroCache
1540
- });
1541
- }
1011
+ if (cmpVersions(cvr, NEW_CVR_VERSION) === 0 && cmpVersions(client, NEW_CVR_VERSION) > 0) throw new ClientNotFoundError("Client not found");
1012
+ if (cmpVersions(client, cvr) > 0) throw new ProtocolError({
1013
+ kind: InvalidConnectionRequestBaseCookie,
1014
+ message: `CVR is at version ${versionString(cvr)}`,
1015
+ origin: ZeroCache
1016
+ });
1542
1017
  }
1543
1018
  function isTransformAuthFailure(error) {
1544
- if (!isProtocolError(error)) {
1545
- return false;
1546
- }
1547
- return error.errorBody.kind === TransformFailed && error.errorBody.reason === HTTP && (error.errorBody.status === 401 || error.errorBody.status === 403);
1019
+ if (!isProtocolError(error)) return false;
1020
+ return error.errorBody.kind === "TransformFailed" && error.errorBody.reason === "http" && (error.errorBody.status === 401 || error.errorBody.status === 403);
1548
1021
  }
1022
+ /**
1023
+ * A query must be expired for all clients in order to be considered
1024
+ * expired.
1025
+ */
1549
1026
  function expired(ttlClock, q) {
1550
- if (q.type === "internal") {
1551
- return false;
1552
- }
1553
- for (const clientState of Object.values(q.clientState)) {
1554
- const { ttl, inactivatedAt } = clientState;
1555
- if (inactivatedAt === void 0) {
1556
- return false;
1557
- }
1558
- const clampedTTL = clampTTL(ttl);
1559
- if (ttlClockAsNumber(inactivatedAt) + clampedTTL > ttlClockAsNumber(ttlClock)) {
1560
- return false;
1561
- }
1562
- }
1563
- return true;
1027
+ if (q.type === "internal") return false;
1028
+ for (const clientState of Object.values(q.clientState)) {
1029
+ const { ttl, inactivatedAt } = clientState;
1030
+ if (inactivatedAt === void 0) return false;
1031
+ const clampedTTL = clampTTL(ttl);
1032
+ if (ttlClockAsNumber(inactivatedAt) + clampedTTL > ttlClockAsNumber(ttlClock)) return false;
1033
+ }
1034
+ return true;
1564
1035
  }
1565
1036
  function hasExpiredQueries(cvr) {
1566
- const { ttlClock } = cvr;
1567
- for (const q of Object.values(cvr.queries)) {
1568
- if (expired(ttlClock, q)) {
1569
- return true;
1570
- }
1571
- }
1572
- return false;
1573
- }
1574
- class TimeSliceTimer {
1575
- #total = 0;
1576
- #start = 0;
1577
- #lc;
1578
- constructor(lc) {
1579
- this.#lc = lc;
1580
- }
1581
- async start() {
1582
- await yieldProcess(this.#lc);
1583
- return this.startWithoutYielding();
1584
- }
1585
- startWithoutYielding() {
1586
- this.#total = 0;
1587
- this.#startLap();
1588
- return this;
1589
- }
1590
- async yieldProcess(_msgForTesting) {
1591
- this.#stopLap();
1592
- await yieldProcess(this.#lc);
1593
- this.#startLap();
1594
- }
1595
- #startLap() {
1596
- assert(this.#start === 0, "already running");
1597
- this.#start = performance.now();
1598
- }
1599
- elapsedLap() {
1600
- assert(this.#start !== 0, "not running");
1601
- return performance.now() - this.#start;
1602
- }
1603
- #stopLap() {
1604
- assert(this.#start !== 0, "not running");
1605
- this.#total += performance.now() - this.#start;
1606
- this.#start = 0;
1607
- }
1608
- /** @returns the total elapsed time */
1609
- stop() {
1610
- this.#stopLap();
1611
- return this.#total;
1612
- }
1613
- /**
1614
- * @returns the elapsed time. This can be called while the Timer is running
1615
- * or after it has been stopped.
1616
- */
1617
- totalElapsed() {
1618
- return this.#start === 0 ? this.#total : this.#total + performance.now() - this.#start;
1619
- }
1037
+ const { ttlClock } = cvr;
1038
+ for (const q of Object.values(cvr.queries)) if (expired(ttlClock, q)) return true;
1039
+ return false;
1620
1040
  }
1621
- export {
1622
- TTL_CLOCK_INTERVAL,
1623
- TTL_TIMER_HYSTERESIS,
1624
- TimeSliceTimer,
1625
- ViewSyncerService
1041
+ var TimeSliceTimer = class {
1042
+ #total = 0;
1043
+ #start = 0;
1044
+ #lc;
1045
+ constructor(lc) {
1046
+ this.#lc = lc;
1047
+ }
1048
+ async start() {
1049
+ await yieldProcess(this.#lc);
1050
+ return this.startWithoutYielding();
1051
+ }
1052
+ startWithoutYielding() {
1053
+ this.#total = 0;
1054
+ this.#startLap();
1055
+ return this;
1056
+ }
1057
+ async yieldProcess(_msgForTesting) {
1058
+ this.#stopLap();
1059
+ await yieldProcess(this.#lc);
1060
+ this.#startLap();
1061
+ }
1062
+ #startLap() {
1063
+ assert(this.#start === 0, "already running");
1064
+ this.#start = performance.now();
1065
+ }
1066
+ elapsedLap() {
1067
+ assert(this.#start !== 0, "not running");
1068
+ return performance.now() - this.#start;
1069
+ }
1070
+ #stopLap() {
1071
+ assert(this.#start !== 0, "not running");
1072
+ this.#total += performance.now() - this.#start;
1073
+ this.#start = 0;
1074
+ }
1075
+ /** @returns the total elapsed time */
1076
+ stop() {
1077
+ this.#stopLap();
1078
+ return this.#total;
1079
+ }
1080
+ /**
1081
+ * @returns the elapsed time. This can be called while the Timer is running
1082
+ * or after it has been stopped.
1083
+ */
1084
+ totalElapsed() {
1085
+ return this.#start === 0 ? this.#total : this.#total + performance.now() - this.#start;
1086
+ }
1626
1087
  };
1627
- //# sourceMappingURL=view-syncer.js.map
1088
+ //#endregion
1089
+ export { TimeSliceTimer, ViewSyncerService };
1090
+
1091
+ //# sourceMappingURL=view-syncer.js.map