@rocicorp/zero 0.2.2024101101 → 0.3.2024102300

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 (600) hide show
  1. package/out/react.js +92 -39
  2. package/out/react.js.map +2 -2
  3. package/out/replicache/src/async-iterable-to-array.js +8 -0
  4. package/out/replicache/src/async-iterable-to-array.js.map +1 -0
  5. package/out/replicache/src/bg-interval.js +38 -0
  6. package/out/replicache/src/bg-interval.js.map +1 -0
  7. package/out/replicache/src/binary-search.js +31 -0
  8. package/out/replicache/src/binary-search.js.map +1 -0
  9. package/out/replicache/src/broadcast-channel.js +29 -0
  10. package/out/replicache/src/broadcast-channel.js.map +1 -0
  11. package/out/replicache/src/btree/diff.js +6 -0
  12. package/out/replicache/src/btree/diff.js.map +1 -0
  13. package/out/replicache/src/btree/node.js +392 -0
  14. package/out/replicache/src/btree/node.js.map +1 -0
  15. package/out/replicache/src/btree/read.js +227 -0
  16. package/out/replicache/src/btree/read.js.map +1 -0
  17. package/out/replicache/src/btree/splice.js +79 -0
  18. package/out/replicache/src/btree/splice.js.map +1 -0
  19. package/out/replicache/src/btree/write.js +154 -0
  20. package/out/replicache/src/btree/write.js.map +1 -0
  21. package/out/replicache/src/call-default-fetch.js +37 -0
  22. package/out/replicache/src/call-default-fetch.js.map +1 -0
  23. package/out/replicache/src/config.js +13 -0
  24. package/out/replicache/src/config.js.map +1 -0
  25. package/out/replicache/src/connection-loop-delegates.js +30 -0
  26. package/out/replicache/src/connection-loop-delegates.js.map +1 -0
  27. package/out/replicache/src/connection-loop.js +268 -0
  28. package/out/replicache/src/connection-loop.js.map +1 -0
  29. package/out/replicache/src/cookies.js +41 -0
  30. package/out/replicache/src/cookies.js.map +1 -0
  31. package/out/replicache/src/dag/chunk.js +60 -0
  32. package/out/replicache/src/dag/chunk.js.map +1 -0
  33. package/out/replicache/src/dag/gc.js +126 -0
  34. package/out/replicache/src/dag/gc.js.map +1 -0
  35. package/out/replicache/src/dag/key-type-enum.js +6 -0
  36. package/out/replicache/src/dag/key-type-enum.js.map +1 -0
  37. package/out/replicache/src/dag/key.js +54 -0
  38. package/out/replicache/src/dag/key.js.map +1 -0
  39. package/out/replicache/src/dag/lazy-store.js +532 -0
  40. package/out/replicache/src/dag/lazy-store.js.map +1 -0
  41. package/out/replicache/src/dag/store-impl.js +175 -0
  42. package/out/replicache/src/dag/store-impl.js.map +1 -0
  43. package/out/replicache/src/dag/store.js +22 -0
  44. package/out/replicache/src/dag/store.js.map +1 -0
  45. package/out/replicache/src/dag/visitor.js +22 -0
  46. package/out/replicache/src/dag/visitor.js.map +1 -0
  47. package/out/replicache/src/db/commit.js +443 -0
  48. package/out/replicache/src/db/commit.js.map +1 -0
  49. package/out/replicache/src/db/index-operation-enum.js +4 -0
  50. package/out/replicache/src/db/index-operation-enum.js.map +1 -0
  51. package/out/replicache/src/db/index.js +170 -0
  52. package/out/replicache/src/db/index.js.map +1 -0
  53. package/out/replicache/src/db/meta-type-enum.js +7 -0
  54. package/out/replicache/src/db/meta-type-enum.js.map +1 -0
  55. package/out/replicache/src/db/read.js +59 -0
  56. package/out/replicache/src/db/read.js.map +1 -0
  57. package/out/replicache/src/db/rebase.js +56 -0
  58. package/out/replicache/src/db/rebase.js.map +1 -0
  59. package/out/replicache/src/db/scan.js +2 -0
  60. package/out/replicache/src/db/scan.js.map +1 -0
  61. package/out/replicache/src/db/write.js +260 -0
  62. package/out/replicache/src/db/write.js.map +1 -0
  63. package/out/replicache/src/error-responses.js +30 -0
  64. package/out/replicache/src/error-responses.js.map +1 -0
  65. package/out/replicache/src/filter-async-iterable.js +15 -0
  66. package/out/replicache/src/filter-async-iterable.js.map +1 -0
  67. package/out/replicache/src/format-version-enum.js +9 -0
  68. package/out/replicache/src/format-version-enum.js.map +1 -0
  69. package/out/replicache/src/format-version.js +8 -0
  70. package/out/replicache/src/format-version.js.map +1 -0
  71. package/out/replicache/src/frozen-json.js +151 -0
  72. package/out/replicache/src/frozen-json.js.map +1 -0
  73. package/out/replicache/src/get-default-puller.js +74 -0
  74. package/out/replicache/src/get-default-puller.js.map +1 -0
  75. package/out/replicache/src/get-default-pusher.js +36 -0
  76. package/out/replicache/src/get-default-pusher.js.map +1 -0
  77. package/out/replicache/src/hash.js +73 -0
  78. package/out/replicache/src/hash.js.map +1 -0
  79. package/out/replicache/src/http-request-info.js +7 -0
  80. package/out/replicache/src/http-request-info.js.map +1 -0
  81. package/out/replicache/src/impl.js +2 -0
  82. package/out/replicache/src/impl.js.map +1 -0
  83. package/out/replicache/src/index-defs.js +28 -0
  84. package/out/replicache/src/index-defs.js.map +1 -0
  85. package/out/replicache/src/invoke-kind-enum.js +4 -0
  86. package/out/replicache/src/invoke-kind-enum.js.map +1 -0
  87. package/out/replicache/src/iterable-union.js +5 -0
  88. package/out/replicache/src/iterable-union.js.map +1 -0
  89. package/out/replicache/src/kv/idb-store-with-mem-fallback.js +93 -0
  90. package/out/replicache/src/kv/idb-store-with-mem-fallback.js.map +1 -0
  91. package/out/replicache/src/kv/idb-store.js +179 -0
  92. package/out/replicache/src/kv/idb-store.js.map +1 -0
  93. package/out/replicache/src/kv/mem-store.js +61 -0
  94. package/out/replicache/src/kv/mem-store.js.map +1 -0
  95. package/out/replicache/src/kv/read-impl.js +23 -0
  96. package/out/replicache/src/kv/read-impl.js.map +1 -0
  97. package/out/replicache/src/kv/store.js +2 -0
  98. package/out/replicache/src/kv/store.js.map +1 -0
  99. package/out/replicache/src/kv/write-impl-base.js +48 -0
  100. package/out/replicache/src/kv/write-impl-base.js.map +1 -0
  101. package/out/replicache/src/kv/write-impl.js +25 -0
  102. package/out/replicache/src/kv/write-impl.js.map +1 -0
  103. package/out/replicache/src/lazy.js +10 -0
  104. package/out/replicache/src/lazy.js.map +1 -0
  105. package/out/replicache/src/log-options.js +13 -0
  106. package/out/replicache/src/log-options.js.map +1 -0
  107. package/out/replicache/src/merge-async-iterables.js +61 -0
  108. package/out/replicache/src/merge-async-iterables.js.map +1 -0
  109. package/out/replicache/src/mod.js +14 -0
  110. package/out/replicache/src/mod.js.map +1 -0
  111. package/out/replicache/src/mutation-recovery.js +439 -0
  112. package/out/replicache/src/mutation-recovery.js.map +1 -0
  113. package/out/replicache/src/new-client-channel.js +77 -0
  114. package/out/replicache/src/new-client-channel.js.map +1 -0
  115. package/out/replicache/src/on-persist-channel.js +33 -0
  116. package/out/replicache/src/on-persist-channel.js.map +1 -0
  117. package/out/replicache/src/patch-operation.js +37 -0
  118. package/out/replicache/src/patch-operation.js.map +1 -0
  119. package/out/replicache/src/pending-mutations.js +18 -0
  120. package/out/replicache/src/pending-mutations.js.map +1 -0
  121. package/out/replicache/src/persist/client-gc.js +38 -0
  122. package/out/replicache/src/persist/client-gc.js.map +1 -0
  123. package/out/replicache/src/persist/client-group-gc.js +38 -0
  124. package/out/replicache/src/persist/client-group-gc.js.map +1 -0
  125. package/out/replicache/src/persist/client-groups.js +180 -0
  126. package/out/replicache/src/persist/client-groups.js.map +1 -0
  127. package/out/replicache/src/persist/clients.js +390 -0
  128. package/out/replicache/src/persist/clients.js.map +1 -0
  129. package/out/replicache/src/persist/collect-idb-databases.js +174 -0
  130. package/out/replicache/src/persist/collect-idb-databases.js.map +1 -0
  131. package/out/replicache/src/persist/gather-mem-only-visitor.js +25 -0
  132. package/out/replicache/src/persist/gather-mem-only-visitor.js.map +1 -0
  133. package/out/replicache/src/persist/gather-not-cached-visitor.js +35 -0
  134. package/out/replicache/src/persist/gather-not-cached-visitor.js.map +1 -0
  135. package/out/replicache/src/persist/heartbeat.js +37 -0
  136. package/out/replicache/src/persist/heartbeat.js.map +1 -0
  137. package/out/replicache/src/persist/idb-databases-store-db-name.js +18 -0
  138. package/out/replicache/src/persist/idb-databases-store-db-name.js.map +1 -0
  139. package/out/replicache/src/persist/idb-databases-store.js +90 -0
  140. package/out/replicache/src/persist/idb-databases-store.js.map +1 -0
  141. package/out/replicache/src/persist/make-client-id.js +13 -0
  142. package/out/replicache/src/persist/make-client-id.js.map +1 -0
  143. package/out/replicache/src/persist/persist.js +132 -0
  144. package/out/replicache/src/persist/persist.js.map +1 -0
  145. package/out/replicache/src/persist/refresh.js +147 -0
  146. package/out/replicache/src/persist/refresh.js.map +1 -0
  147. package/out/replicache/src/process-scheduler.js +93 -0
  148. package/out/replicache/src/process-scheduler.js.map +1 -0
  149. package/out/replicache/src/puller.js +2 -0
  150. package/out/replicache/src/puller.js.map +1 -0
  151. package/out/replicache/src/pusher.js +32 -0
  152. package/out/replicache/src/pusher.js.map +1 -0
  153. package/out/replicache/src/replicache-impl.js +1007 -0
  154. package/out/replicache/src/replicache-impl.js.map +1 -0
  155. package/out/replicache/src/replicache-options.js +2 -0
  156. package/out/replicache/src/replicache-options.js.map +1 -0
  157. package/out/replicache/src/replicache.js +387 -0
  158. package/out/replicache/src/replicache.js.map +1 -0
  159. package/out/replicache/src/request-idle.js +15 -0
  160. package/out/replicache/src/request-idle.js.map +1 -0
  161. package/out/replicache/src/scan-iterator.js +202 -0
  162. package/out/replicache/src/scan-iterator.js.map +1 -0
  163. package/out/replicache/src/scan-options.js +45 -0
  164. package/out/replicache/src/scan-options.js.map +1 -0
  165. package/out/replicache/src/set-interval-with-signal.js +7 -0
  166. package/out/replicache/src/set-interval-with-signal.js.map +1 -0
  167. package/out/replicache/src/size-of-value.js +77 -0
  168. package/out/replicache/src/size-of-value.js.map +1 -0
  169. package/out/replicache/src/subscriptions.js +357 -0
  170. package/out/replicache/src/subscriptions.js.map +1 -0
  171. package/out/replicache/src/sync/diff.js +75 -0
  172. package/out/replicache/src/sync/diff.js.map +1 -0
  173. package/out/replicache/src/sync/handle-pull-response-result-type-enum.js +5 -0
  174. package/out/replicache/src/sync/handle-pull-response-result-type-enum.js.map +1 -0
  175. package/out/replicache/src/sync/ids.js +4 -0
  176. package/out/replicache/src/sync/ids.js.map +1 -0
  177. package/out/replicache/src/sync/patch.js +41 -0
  178. package/out/replicache/src/sync/patch.js.map +1 -0
  179. package/out/replicache/src/sync/pull-error.js +16 -0
  180. package/out/replicache/src/sync/pull-error.js.map +1 -0
  181. package/out/replicache/src/sync/pull.js +375 -0
  182. package/out/replicache/src/sync/pull.js.map +1 -0
  183. package/out/replicache/src/sync/push.js +141 -0
  184. package/out/replicache/src/sync/push.js.map +1 -0
  185. package/out/replicache/src/sync/request-id.js +31 -0
  186. package/out/replicache/src/sync/request-id.js.map +1 -0
  187. package/out/replicache/src/sync/sync-head-name.js +2 -0
  188. package/out/replicache/src/sync/sync-head-name.js.map +1 -0
  189. package/out/replicache/src/test-license-key.js +3 -0
  190. package/out/replicache/src/test-license-key.js.map +1 -0
  191. package/out/replicache/src/to-error.js +7 -0
  192. package/out/replicache/src/to-error.js.map +1 -0
  193. package/out/replicache/src/transaction-closed-error.js +17 -0
  194. package/out/replicache/src/transaction-closed-error.js.map +1 -0
  195. package/out/replicache/src/transactions.js +144 -0
  196. package/out/replicache/src/transactions.js.map +1 -0
  197. package/out/replicache/src/types.js +2 -0
  198. package/out/replicache/src/types.js.map +1 -0
  199. package/out/replicache/src/version.js +5 -0
  200. package/out/replicache/src/version.js.map +1 -0
  201. package/out/replicache/src/with-transactions.js +28 -0
  202. package/out/replicache/src/with-transactions.js.map +1 -0
  203. package/out/shared/src/browser-env.js +14 -0
  204. package/out/shared/src/browser-env.js.map +1 -0
  205. package/out/shared/src/document-visible.js +76 -0
  206. package/out/shared/src/document-visible.js.map +1 -0
  207. package/out/shared/src/immutable.js +2 -0
  208. package/out/shared/src/immutable.js.map +1 -0
  209. package/out/shared/src/iterables.d.ts +2 -2
  210. package/out/shared/src/iterables.d.ts.map +1 -1
  211. package/out/shared/src/iterables.js +40 -0
  212. package/out/shared/src/iterables.js.map +1 -0
  213. package/out/shared/src/navigator.js +3 -0
  214. package/out/shared/src/navigator.js.map +1 -0
  215. package/out/shared/src/random-uint64.js +8 -0
  216. package/out/shared/src/random-uint64.js.map +1 -0
  217. package/out/shared/src/random-values.d.ts +1 -0
  218. package/out/shared/src/random-values.d.ts.map +1 -1
  219. package/out/shared/src/random-values.js +22 -0
  220. package/out/shared/src/random-values.js.map +1 -0
  221. package/out/shared/src/set-utils.d.ts +1 -1
  222. package/out/shared/src/set-utils.d.ts.map +1 -1
  223. package/out/shared/src/set-utils.js +6 -4
  224. package/out/shared/src/set-utils.js.map +1 -1
  225. package/out/shared/src/sorted-entries.d.ts +2 -0
  226. package/out/shared/src/sorted-entries.d.ts.map +1 -0
  227. package/out/shared/src/sorted-entries.js +6 -0
  228. package/out/shared/src/sorted-entries.js.map +1 -0
  229. package/out/shared/src/types.js +2 -0
  230. package/out/shared/src/types.js.map +1 -0
  231. package/out/shared/src/valita.d.ts +1 -1
  232. package/out/shared/src/valita.d.ts.map +1 -1
  233. package/out/shared/src/valita.js.map +1 -1
  234. package/out/shared/src/writable.d.ts +4 -0
  235. package/out/shared/src/writable.d.ts.map +1 -0
  236. package/out/shared/src/writable.js +2 -0
  237. package/out/shared/src/writable.js.map +1 -0
  238. package/out/zero/src/config.d.ts +2 -0
  239. package/out/zero/src/config.d.ts.map +1 -0
  240. package/out/zero/src/config.js +2 -0
  241. package/out/zero/src/config.js.map +1 -0
  242. package/out/zero-cache/src/config/config-query.d.ts +18 -0
  243. package/out/zero-cache/src/config/config-query.d.ts.map +1 -0
  244. package/out/zero-cache/src/config/config-query.js +19 -0
  245. package/out/zero-cache/src/config/config-query.js.map +1 -0
  246. package/out/zero-cache/src/config/define-config.d.ts +43 -0
  247. package/out/zero-cache/src/config/define-config.d.ts.map +1 -0
  248. package/out/zero-cache/src/config/define-config.js +120 -0
  249. package/out/zero-cache/src/config/define-config.js.map +1 -0
  250. package/out/zero-cache/src/config/refs.d.ts +3 -0
  251. package/out/zero-cache/src/config/refs.d.ts.map +1 -0
  252. package/out/zero-cache/src/config/refs.js +14 -0
  253. package/out/zero-cache/src/config/refs.js.map +1 -0
  254. package/out/zero-cache/src/config/zero-config.d.ts +126 -686
  255. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  256. package/out/zero-cache/src/config/zero-config.js +43 -161
  257. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  258. package/out/zero-cache/src/db/create.d.ts +8 -0
  259. package/out/zero-cache/src/db/create.d.ts.map +1 -0
  260. package/out/zero-cache/src/db/create.js +39 -0
  261. package/out/zero-cache/src/db/create.js.map +1 -0
  262. package/out/zero-cache/src/db/lite-tables.d.ts +3 -3
  263. package/out/zero-cache/src/db/lite-tables.d.ts.map +1 -1
  264. package/out/zero-cache/src/db/lite-tables.js +29 -20
  265. package/out/zero-cache/src/db/lite-tables.js.map +1 -1
  266. package/out/zero-cache/src/db/{migration-lite.d.ts → old-migration-lite.d.ts} +1 -1
  267. package/out/zero-cache/src/db/old-migration-lite.d.ts.map +1 -0
  268. package/out/zero-cache/src/db/{migration-lite.js → old-migration-lite.js} +1 -1
  269. package/out/zero-cache/src/db/old-migration-lite.js.map +1 -0
  270. package/out/zero-cache/src/db/{migration.d.ts → old-migration.d.ts} +1 -1
  271. package/out/zero-cache/src/db/old-migration.d.ts.map +1 -0
  272. package/out/zero-cache/src/db/{migration.js → old-migration.js} +1 -1
  273. package/out/zero-cache/src/db/old-migration.js.map +1 -0
  274. package/out/zero-cache/src/db/pg-to-lite.d.ts +11 -0
  275. package/out/zero-cache/src/db/pg-to-lite.d.ts.map +1 -0
  276. package/out/zero-cache/src/{services/change-streamer/pg/schema/lite.js → db/pg-to-lite.js} +27 -15
  277. package/out/zero-cache/src/db/pg-to-lite.js.map +1 -0
  278. package/out/zero-cache/src/db/specs.d.ts +73 -0
  279. package/out/zero-cache/src/db/specs.d.ts.map +1 -0
  280. package/out/zero-cache/src/db/specs.js +30 -0
  281. package/out/zero-cache/src/db/specs.js.map +1 -0
  282. package/out/zero-cache/src/db/statements.d.ts +1 -1
  283. package/out/zero-cache/src/db/statements.d.ts.map +1 -1
  284. package/out/zero-cache/src/server/life-cycle.d.ts.map +1 -1
  285. package/out/zero-cache/src/server/life-cycle.js +9 -5
  286. package/out/zero-cache/src/server/life-cycle.js.map +1 -1
  287. package/out/zero-cache/src/server/main.js +2 -2
  288. package/out/zero-cache/src/server/main.js.map +1 -1
  289. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  290. package/out/zero-cache/src/server/syncer.js +2 -1
  291. package/out/zero-cache/src/server/syncer.js.map +1 -1
  292. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +1 -1
  293. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  294. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +1 -4
  295. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  296. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +455 -13
  297. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  298. package/out/zero-cache/src/services/change-streamer/change-streamer.js +16 -0
  299. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  300. package/out/zero-cache/src/services/change-streamer/pg/change-source.d.ts.map +1 -1
  301. package/out/zero-cache/src/services/change-streamer/pg/change-source.js +311 -86
  302. package/out/zero-cache/src/services/change-streamer/pg/change-source.js.map +1 -1
  303. package/out/zero-cache/src/services/change-streamer/pg/initial-sync.d.ts.map +1 -1
  304. package/out/zero-cache/src/services/change-streamer/pg/initial-sync.js +7 -51
  305. package/out/zero-cache/src/services/change-streamer/pg/initial-sync.js.map +1 -1
  306. package/out/zero-cache/src/services/change-streamer/pg/schema/ddl.d.ts +239 -59
  307. package/out/zero-cache/src/services/change-streamer/pg/schema/ddl.d.ts.map +1 -1
  308. package/out/zero-cache/src/services/change-streamer/pg/schema/ddl.js +263 -183
  309. package/out/zero-cache/src/services/change-streamer/pg/schema/ddl.js.map +1 -1
  310. package/out/zero-cache/src/services/change-streamer/pg/schema/published.d.ts +4 -4
  311. package/out/zero-cache/src/services/change-streamer/pg/schema/published.d.ts.map +1 -1
  312. package/out/zero-cache/src/services/change-streamer/pg/schema/published.js +14 -19
  313. package/out/zero-cache/src/services/change-streamer/pg/schema/published.js.map +1 -1
  314. package/out/zero-cache/src/services/change-streamer/pg/schema/zero.d.ts +2 -1
  315. package/out/zero-cache/src/services/change-streamer/pg/schema/zero.d.ts.map +1 -1
  316. package/out/zero-cache/src/services/change-streamer/pg/schema/zero.js +51 -6
  317. package/out/zero-cache/src/services/change-streamer/pg/schema/zero.js.map +1 -1
  318. package/out/zero-cache/src/services/change-streamer/pg/sync-schema.js +1 -1
  319. package/out/zero-cache/src/services/change-streamer/pg/sync-schema.js.map +1 -1
  320. package/out/zero-cache/src/services/change-streamer/schema/change.d.ts +325 -26
  321. package/out/zero-cache/src/services/change-streamer/schema/change.d.ts.map +1 -1
  322. package/out/zero-cache/src/services/change-streamer/schema/change.js +84 -1
  323. package/out/zero-cache/src/services/change-streamer/schema/change.js.map +1 -1
  324. package/out/zero-cache/src/services/change-streamer/schema/init.js +1 -1
  325. package/out/zero-cache/src/services/change-streamer/schema/init.js.map +1 -1
  326. package/out/zero-cache/src/services/dispatcher/connect-params.d.ts +1 -0
  327. package/out/zero-cache/src/services/dispatcher/connect-params.d.ts.map +1 -1
  328. package/out/zero-cache/src/services/dispatcher/connect-params.js +5 -2
  329. package/out/zero-cache/src/services/dispatcher/connect-params.js.map +1 -1
  330. package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
  331. package/out/zero-cache/src/services/mutagen/mutagen.js +17 -19
  332. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  333. package/out/zero-cache/src/services/mutagen/write-authorizer.d.ts.map +1 -1
  334. package/out/zero-cache/src/services/mutagen/write-authorizer.js +18 -9
  335. package/out/zero-cache/src/services/mutagen/write-authorizer.js.map +1 -1
  336. package/out/zero-cache/src/services/replicator/incremental-sync.d.ts.map +1 -1
  337. package/out/zero-cache/src/services/replicator/incremental-sync.js +120 -16
  338. package/out/zero-cache/src/services/replicator/incremental-sync.js.map +1 -1
  339. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts +20 -3
  340. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts.map +1 -1
  341. package/out/zero-cache/src/services/replicator/schema/change-log.js +44 -12
  342. package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
  343. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts +3 -2
  344. package/out/zero-cache/src/services/view-syncer/client-handler.d.ts.map +1 -1
  345. package/out/zero-cache/src/services/view-syncer/client-handler.js +21 -18
  346. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  347. package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts +5 -1
  348. package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts.map +1 -1
  349. package/out/zero-cache/src/services/view-syncer/cvr-store.js +49 -8
  350. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  351. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +1 -1
  352. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  353. package/out/zero-cache/src/services/view-syncer/cvr.js +1 -1
  354. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  355. package/out/zero-cache/src/services/view-syncer/database-storage.js +1 -1
  356. package/out/zero-cache/src/services/view-syncer/database-storage.js.map +1 -1
  357. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts +31 -0
  358. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts.map +1 -0
  359. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +53 -0
  360. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -0
  361. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +11 -5
  362. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  363. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +17 -0
  364. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  365. package/out/zero-cache/src/services/view-syncer/schema/cvr.d.ts.map +1 -1
  366. package/out/zero-cache/src/services/view-syncer/schema/cvr.js +2 -2
  367. package/out/zero-cache/src/services/view-syncer/schema/cvr.js.map +1 -1
  368. package/out/zero-cache/src/services/view-syncer/schema/pg-migrations.js +1 -1
  369. package/out/zero-cache/src/services/view-syncer/schema/pg-migrations.js.map +1 -1
  370. package/out/zero-cache/src/services/view-syncer/schema/types.d.ts +11 -11
  371. package/out/zero-cache/src/services/view-syncer/schema/types.js +2 -2
  372. package/out/zero-cache/src/services/view-syncer/schema/types.js.map +1 -1
  373. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +12 -4
  374. package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
  375. package/out/zero-cache/src/services/view-syncer/snapshotter.js +59 -49
  376. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  377. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +2 -3
  378. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  379. package/out/zero-cache/src/services/view-syncer/view-syncer.js +50 -10
  380. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  381. package/out/zero-cache/src/types/pg.d.ts +7 -1
  382. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  383. package/out/zero-cache/src/types/pg.js +14 -1
  384. package/out/zero-cache/src/types/pg.js.map +1 -1
  385. package/out/zero-cache/src/workers/connection.d.ts +3 -1
  386. package/out/zero-cache/src/workers/connection.d.ts.map +1 -1
  387. package/out/zero-cache/src/workers/connection.js +19 -1
  388. package/out/zero-cache/src/workers/connection.js.map +1 -1
  389. package/out/zero-cache/src/workers/syncer.d.ts +2 -1
  390. package/out/zero-cache/src/workers/syncer.d.ts.map +1 -1
  391. package/out/zero-cache/src/workers/syncer.js +20 -9
  392. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  393. package/out/zero-client/src/client/context.d.ts +1 -1
  394. package/out/zero-client/src/client/context.d.ts.map +1 -1
  395. package/out/zero-client/src/client/context.js +99 -0
  396. package/out/zero-client/src/client/context.js.map +1 -0
  397. package/out/zero-client/src/client/crud.d.ts +8 -10
  398. package/out/zero-client/src/client/crud.d.ts.map +1 -1
  399. package/out/zero-client/src/client/crud.js +181 -0
  400. package/out/zero-client/src/client/crud.js.map +1 -0
  401. package/out/zero-client/src/client/enable-analytics.js +21 -0
  402. package/out/zero-client/src/client/enable-analytics.js.map +1 -0
  403. package/out/zero-client/src/client/http-string.js +14 -0
  404. package/out/zero-client/src/client/http-string.js.map +1 -0
  405. package/out/zero-client/src/client/keys.d.ts +3 -2
  406. package/out/zero-client/src/client/keys.d.ts.map +1 -1
  407. package/out/zero-client/src/client/keys.js +32 -0
  408. package/out/zero-client/src/client/keys.js.map +1 -0
  409. package/out/zero-client/src/client/log-options.js +57 -0
  410. package/out/zero-client/src/client/log-options.js.map +1 -0
  411. package/out/zero-client/src/client/metrics.js +268 -0
  412. package/out/zero-client/src/client/metrics.js.map +1 -0
  413. package/out/zero-client/src/client/normalized-schema.d.ts +17 -0
  414. package/out/zero-client/src/client/normalized-schema.d.ts.map +1 -0
  415. package/out/zero-client/src/client/normalized-schema.js +31 -0
  416. package/out/zero-client/src/client/normalized-schema.js.map +1 -0
  417. package/out/zero-client/src/client/options.js +2 -0
  418. package/out/zero-client/src/client/options.js.map +1 -0
  419. package/out/zero-client/src/client/query-manager.d.ts +18 -3
  420. package/out/zero-client/src/client/query-manager.d.ts.map +1 -1
  421. package/out/zero-client/src/client/query-manager.js +149 -0
  422. package/out/zero-client/src/client/query-manager.js.map +1 -0
  423. package/out/zero-client/src/client/reload-error-handler.js +23 -0
  424. package/out/zero-client/src/client/reload-error-handler.js.map +1 -0
  425. package/out/zero-client/src/client/replicache-types.js +2 -0
  426. package/out/zero-client/src/client/replicache-types.js.map +1 -0
  427. package/out/zero-client/src/client/server-error.js +22 -0
  428. package/out/zero-client/src/client/server-error.js.map +1 -0
  429. package/out/zero-client/src/client/server-option.js +37 -0
  430. package/out/zero-client/src/client/server-option.js.map +1 -0
  431. package/out/zero-client/src/client/version.js +5 -0
  432. package/out/zero-client/src/client/version.js.map +1 -0
  433. package/out/zero-client/src/client/zero-poke-handler.d.ts +3 -3
  434. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  435. package/out/zero-client/src/client/zero-poke-handler.js +240 -0
  436. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -0
  437. package/out/zero-client/src/client/zero.d.ts +3 -2
  438. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  439. package/out/zero-client/src/client/zero.js +1212 -0
  440. package/out/zero-client/src/client/zero.js.map +1 -0
  441. package/out/zero-client/src/mod.js +4 -0
  442. package/out/zero-client/src/mod.js.map +1 -0
  443. package/out/zero-client/src/util/nanoid.js +34 -0
  444. package/out/zero-client/src/util/nanoid.js.map +1 -0
  445. package/out/zero-client/src/util/socket.js +4 -0
  446. package/out/zero-client/src/util/socket.js.map +1 -0
  447. package/out/zero-protocol/src/ast.d.ts +87 -1
  448. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  449. package/out/zero-protocol/src/ast.js +53 -0
  450. package/out/zero-protocol/src/ast.js.map +1 -1
  451. package/out/zero-protocol/src/change-desired-queries.d.ts +6 -6
  452. package/out/zero-protocol/src/change-desired-queries.d.ts.map +1 -1
  453. package/out/zero-protocol/src/clients-patch.d.ts +1 -1
  454. package/out/zero-protocol/src/connect.d.ts +15 -8
  455. package/out/zero-protocol/src/connect.d.ts.map +1 -1
  456. package/out/zero-protocol/src/connect.js +21 -0
  457. package/out/zero-protocol/src/connect.js.map +1 -1
  458. package/out/zero-protocol/src/data.d.ts +36 -0
  459. package/out/zero-protocol/src/data.d.ts.map +1 -0
  460. package/out/zero-protocol/src/data.js +4 -0
  461. package/out/zero-protocol/src/data.js.map +1 -0
  462. package/out/zero-protocol/src/delete-clients.d.ts +4 -4
  463. package/out/zero-protocol/src/delete-clients.d.ts.map +1 -1
  464. package/out/zero-protocol/src/down.d.ts +18 -16
  465. package/out/zero-protocol/src/down.d.ts.map +1 -1
  466. package/out/zero-protocol/src/down.js +2 -1
  467. package/out/zero-protocol/src/down.js.map +1 -1
  468. package/out/zero-protocol/src/mod.d.ts +1 -2
  469. package/out/zero-protocol/src/mod.d.ts.map +1 -1
  470. package/out/zero-protocol/src/mod.js +2 -2
  471. package/out/zero-protocol/src/mod.js.map +1 -1
  472. package/out/zero-protocol/src/ping.d.ts +1 -1
  473. package/out/zero-protocol/src/ping.d.ts.map +1 -1
  474. package/out/zero-protocol/src/poke.d.ts +26 -26
  475. package/out/zero-protocol/src/poke.d.ts.map +1 -1
  476. package/out/zero-protocol/src/poke.js +8 -5
  477. package/out/zero-protocol/src/poke.js.map +1 -1
  478. package/out/zero-protocol/src/pong.d.ts +1 -1
  479. package/out/zero-protocol/src/pong.d.ts.map +1 -1
  480. package/out/zero-protocol/src/primary-key.d.ts +2 -3
  481. package/out/zero-protocol/src/primary-key.d.ts.map +1 -1
  482. package/out/zero-protocol/src/primary-key.js +1 -3
  483. package/out/zero-protocol/src/primary-key.js.map +1 -1
  484. package/out/zero-protocol/src/pull.d.ts +4 -4
  485. package/out/zero-protocol/src/pull.d.ts.map +1 -1
  486. package/out/zero-protocol/src/push.d.ts +105 -98
  487. package/out/zero-protocol/src/push.d.ts.map +1 -1
  488. package/out/zero-protocol/src/push.js +17 -13
  489. package/out/zero-protocol/src/push.js.map +1 -1
  490. package/out/zero-protocol/src/queries-patch.d.ts +4 -4
  491. package/out/zero-protocol/src/row-patch.d.ts +38 -0
  492. package/out/zero-protocol/src/row-patch.d.ts.map +1 -0
  493. package/out/zero-protocol/src/{entities-patch.js → row-patch.js} +10 -11
  494. package/out/zero-protocol/src/row-patch.js.map +1 -0
  495. package/out/zero-protocol/src/up.d.ts +28 -27
  496. package/out/zero-protocol/src/up.d.ts.map +1 -1
  497. package/out/zero-protocol/src/warm.d.ts +10 -0
  498. package/out/zero-protocol/src/warm.d.ts.map +1 -0
  499. package/out/zero-protocol/src/warm.js +6 -0
  500. package/out/zero-protocol/src/warm.js.map +1 -0
  501. package/out/zero-react/src/use-query.d.ts.map +1 -1
  502. package/out/zero.js +499 -208
  503. package/out/zero.js.map +4 -4
  504. package/out/zql/src/zql/builder/builder.d.ts +3 -3
  505. package/out/zql/src/zql/builder/builder.d.ts.map +1 -1
  506. package/out/zql/src/zql/builder/builder.js +1 -1
  507. package/out/zql/src/zql/builder/builder.js.map +1 -1
  508. package/out/zql/src/zql/builder/filter.d.ts +2 -2
  509. package/out/zql/src/zql/builder/filter.d.ts.map +1 -1
  510. package/out/zql/src/zql/builder/filter.js.map +1 -1
  511. package/out/zql/src/zql/ivm/array-view.d.ts +1 -1
  512. package/out/zql/src/zql/ivm/array-view.d.ts.map +1 -1
  513. package/out/zql/src/zql/ivm/array-view.js +232 -0
  514. package/out/zql/src/zql/ivm/array-view.js.map +1 -0
  515. package/out/zql/src/zql/ivm/change.d.ts +2 -1
  516. package/out/zql/src/zql/ivm/change.d.ts.map +1 -1
  517. package/out/zql/src/zql/ivm/data.d.ts +2 -33
  518. package/out/zql/src/zql/ivm/data.d.ts.map +1 -1
  519. package/out/zql/src/zql/ivm/data.js.map +1 -1
  520. package/out/zql/src/zql/ivm/filter.d.ts +2 -1
  521. package/out/zql/src/zql/ivm/filter.d.ts.map +1 -1
  522. package/out/zql/src/zql/ivm/filter.js.map +1 -1
  523. package/out/zql/src/zql/ivm/join.d.ts +1 -1
  524. package/out/zql/src/zql/ivm/join.d.ts.map +1 -1
  525. package/out/zql/src/zql/ivm/join.js +68 -24
  526. package/out/zql/src/zql/ivm/join.js.map +1 -1
  527. package/out/zql/src/zql/ivm/maybe-split-and-push-edit-change.d.ts +1 -1
  528. package/out/zql/src/zql/ivm/maybe-split-and-push-edit-change.d.ts.map +1 -1
  529. package/out/zql/src/zql/ivm/memory-source.d.ts +8 -6
  530. package/out/zql/src/zql/ivm/memory-source.d.ts.map +1 -1
  531. package/out/zql/src/zql/ivm/memory-source.js +2 -0
  532. package/out/zql/src/zql/ivm/memory-source.js.map +1 -1
  533. package/out/zql/src/zql/ivm/memory-storage.js +33 -0
  534. package/out/zql/src/zql/ivm/memory-storage.js.map +1 -0
  535. package/out/zql/src/zql/ivm/operator.d.ts +2 -1
  536. package/out/zql/src/zql/ivm/operator.d.ts.map +1 -1
  537. package/out/zql/src/zql/ivm/schema.d.ts +3 -3
  538. package/out/zql/src/zql/ivm/schema.d.ts.map +1 -1
  539. package/out/zql/src/zql/ivm/schema.js.map +1 -1
  540. package/out/zql/src/zql/ivm/skip.d.ts +2 -1
  541. package/out/zql/src/zql/ivm/skip.d.ts.map +1 -1
  542. package/out/zql/src/zql/ivm/skip.js.map +1 -1
  543. package/out/zql/src/zql/ivm/source.d.ts +2 -2
  544. package/out/zql/src/zql/ivm/source.d.ts.map +1 -1
  545. package/out/zql/src/zql/ivm/take.d.ts.map +1 -1
  546. package/out/zql/src/zql/ivm/take.js.map +1 -1
  547. package/out/zql/src/zql/query/normalize-table-schema.d.ts +48 -0
  548. package/out/zql/src/zql/query/normalize-table-schema.d.ts.map +1 -0
  549. package/out/zql/src/zql/query/normalize-table-schema.js +116 -0
  550. package/out/zql/src/zql/query/normalize-table-schema.js.map +1 -0
  551. package/out/zql/src/zql/query/query-impl.d.ts +6 -5
  552. package/out/zql/src/zql/query/query-impl.d.ts.map +1 -1
  553. package/out/zql/src/zql/query/query-impl.js +263 -0
  554. package/out/zql/src/zql/query/query-impl.js.map +1 -0
  555. package/out/zql/src/zql/query/query.d.ts +1 -1
  556. package/out/zql/src/zql/query/query.d.ts.map +1 -1
  557. package/out/zql/src/zql/query/query.js +3 -0
  558. package/out/zql/src/zql/query/query.js.map +1 -0
  559. package/out/zql/src/zql/query/schema.d.ts +13 -4
  560. package/out/zql/src/zql/query/schema.d.ts.map +1 -1
  561. package/out/zql/src/zql/query/schema.js +7 -0
  562. package/out/zql/src/zql/query/schema.js.map +1 -0
  563. package/out/zql/src/zql/query/typed-view.js +2 -0
  564. package/out/zql/src/zql/query/typed-view.js.map +1 -0
  565. package/out/zqlite/src/db.d.ts +1 -1
  566. package/out/zqlite/src/db.d.ts.map +1 -1
  567. package/out/zqlite/src/db.js +1 -1
  568. package/out/zqlite/src/db.js.map +1 -1
  569. package/out/zqlite/src/table-source.d.ts +2 -2
  570. package/out/zqlite/src/table-source.d.ts.map +1 -1
  571. package/out/zqlite/src/table-source.js.map +1 -1
  572. package/package.json +10 -8
  573. package/deps/sqlite3/sqlite3.c +0 -260574
  574. package/deps/sqlite3/sqlite3.h +0 -13572
  575. package/deps/sqlite3/sqlite3ext.h +0 -719
  576. package/out/zero-cache/src/db/migration-lite.d.ts.map +0 -1
  577. package/out/zero-cache/src/db/migration-lite.js.map +0 -1
  578. package/out/zero-cache/src/db/migration.d.ts.map +0 -1
  579. package/out/zero-cache/src/db/migration.js.map +0 -1
  580. package/out/zero-cache/src/services/change-streamer/pg/schema/create.d.ts +0 -6
  581. package/out/zero-cache/src/services/change-streamer/pg/schema/create.d.ts.map +0 -1
  582. package/out/zero-cache/src/services/change-streamer/pg/schema/create.js +0 -29
  583. package/out/zero-cache/src/services/change-streamer/pg/schema/create.js.map +0 -1
  584. package/out/zero-cache/src/services/change-streamer/pg/schema/lite.d.ts +0 -6
  585. package/out/zero-cache/src/services/change-streamer/pg/schema/lite.d.ts.map +0 -1
  586. package/out/zero-cache/src/services/change-streamer/pg/schema/lite.js.map +0 -1
  587. package/out/zero-cache/src/types/specs.d.ts +0 -29
  588. package/out/zero-cache/src/types/specs.d.ts.map +0 -1
  589. package/out/zero-cache/src/types/specs.js +0 -2
  590. package/out/zero-cache/src/types/specs.js.map +0 -1
  591. package/out/zero-client/src/client/make-id-from-primary-key.d.ts +0 -5
  592. package/out/zero-client/src/client/make-id-from-primary-key.d.ts.map +0 -1
  593. package/out/zero-protocol/src/entities-patch.d.ts +0 -66
  594. package/out/zero-protocol/src/entities-patch.d.ts.map +0 -1
  595. package/out/zero-protocol/src/entities-patch.js.map +0 -1
  596. package/out/zql/src/zql/ast/ast.d.ts +0 -84
  597. package/out/zql/src/zql/ast/ast.d.ts.map +0 -1
  598. package/out/zql/src/zql/ast/ast.js +0 -54
  599. package/out/zql/src/zql/ast/ast.js.map +0 -1
  600. package/tool/install-sqlite3.js +0 -37
@@ -0,0 +1,1007 @@
1
+ import { consoleLogSink, LogContext } from '@rocicorp/logger';
2
+ import { resolver } from '@rocicorp/resolver';
3
+ import { AbortError } from '../../shared/src/abort-error.js';
4
+ import { assert } from '../../shared/src/asserts.js';
5
+ import { getBrowserGlobal } from '../../shared/src/browser-env.js';
6
+ import { getDocumentVisibilityWatcher } from '../../shared/src/document-visible.js';
7
+ import { PullDelegate, PushDelegate } from './connection-loop-delegates.js';
8
+ import { ConnectionLoop, MAX_DELAY_MS, MIN_DELAY_MS } from './connection-loop.js';
9
+ import { assertCookie } from './cookies.js';
10
+ import { LazyStore } from './dag/lazy-store.js';
11
+ import { StoreImpl } from './dag/store-impl.js';
12
+ import { ChunkNotFoundError, mustGetHeadHash } from './dag/store.js';
13
+ import { baseSnapshotFromHash, DEFAULT_HEAD_NAME, isLocalMetaDD31, } from './db/commit.js';
14
+ import { readFromDefaultHead } from './db/read.js';
15
+ import { rebaseMutationAndCommit } from './db/rebase.js';
16
+ import { newWriteLocal } from './db/write.js';
17
+ import { isClientStateNotFoundResponse, isVersionNotSupportedResponse, } from './error-responses.js';
18
+ import * as FormatVersion from './format-version-enum.js';
19
+ import { deepFreeze } from './frozen-json.js';
20
+ import { getDefaultPuller, isDefaultPuller } from './get-default-puller.js';
21
+ import { getDefaultPusher, isDefaultPusher } from './get-default-pusher.js';
22
+ import { assertHash, emptyHash, newRandomHash } from './hash.js';
23
+ import { createLogContext } from './log-options.js';
24
+ import { MutationRecovery } from './mutation-recovery.js';
25
+ import { initNewClientChannel } from './new-client-channel.js';
26
+ import { initOnPersistChannel, } from './on-persist-channel.js';
27
+ import { pendingMutationsForAPI, } from './pending-mutations.js';
28
+ import { CLIENT_MAX_INACTIVE_TIME, GC_INTERVAL, initClientGC, } from './persist/client-gc.js';
29
+ import { initClientGroupGC } from './persist/client-group-gc.js';
30
+ import { disableClientGroup } from './persist/client-groups.js';
31
+ import { ClientStateNotFoundError, initClientV6, hasClientState as persistHasClientState, } from './persist/clients.js';
32
+ import { COLLECT_IDB_INTERVAL, initCollectIDBDatabases, INITIAL_COLLECT_IDB_DELAY, SDD_IDB_MAX_AGE, } from './persist/collect-idb-databases.js';
33
+ import { HEARTBEAT_INTERVAL, startHeartbeats } from './persist/heartbeat.js';
34
+ import { IDBDatabasesStore, } from './persist/idb-databases-store.js';
35
+ import { makeClientID } from './persist/make-client-id.js';
36
+ import { persistDD31 } from './persist/persist.js';
37
+ import { refresh } from './persist/refresh.js';
38
+ import { ProcessScheduler } from './process-scheduler.js';
39
+ import { PushError } from './pusher.js';
40
+ import { getKVStoreProvider, httpStatusUnauthorized, makeIDBName, ReportError, } from './replicache.js';
41
+ import { setIntervalWithSignal } from './set-interval-with-signal.js';
42
+ import { SubscriptionImpl, SubscriptionsManagerImpl, WatchSubscription, } from './subscriptions.js';
43
+ import * as HandlePullResponseResultEnum from './sync/handle-pull-response-result-type-enum.js';
44
+ import { PullError } from './sync/pull-error.js';
45
+ import { beginPullV1, handlePullResponseV1, maybeEndPull } from './sync/pull.js';
46
+ import { push, PUSH_VERSION_DD31 } from './sync/push.js';
47
+ import { newRequestID } from './sync/request-id.js';
48
+ import { SYNC_HEAD_NAME } from './sync/sync-head-name.js';
49
+ import { throwIfClosed } from './transaction-closed-error.js';
50
+ import { ReadTransactionImpl, WriteTransactionImpl } from './transactions.js';
51
+ import { version } from './version.js';
52
+ import { withRead, withWrite, withWriteNoImplicitCommit, } from './with-transactions.js';
53
+ /**
54
+ * The maximum number of time to call out to getAuth before giving up
55
+ * and throwing an error.
56
+ */
57
+ const MAX_REAUTH_TRIES = 8;
58
+ const PERSIST_IDLE_TIMEOUT_MS = 1000;
59
+ const REFRESH_IDLE_TIMEOUT_MS = 1000;
60
+ const PERSIST_THROTTLE_MS = 500;
61
+ const REFRESH_THROTTLE_MS = 500;
62
+ const LAZY_STORE_SOURCE_CHUNK_CACHE_SIZE_LIMIT = 100 * 2 ** 20; // 100 MB
63
+ const RECOVER_MUTATIONS_INTERVAL_MS = 5 * 60 * 1000; // 5 mins
64
+ const noop = () => {
65
+ // noop
66
+ };
67
+ const updateNeededReasonNewClientGroup = {
68
+ type: 'NewClientGroup',
69
+ };
70
+ // eslint-disable-next-line @typescript-eslint/ban-types
71
+ export class ReplicacheImpl {
72
+ /** The URL to use when doing a pull request. */
73
+ pullURL;
74
+ /** The URL to use when doing a push request. */
75
+ pushURL;
76
+ /** The authorization token used when doing a push request. */
77
+ auth;
78
+ /** The name of the Replicache database. Populated by {@link ReplicacheOptions#name}. */
79
+ name;
80
+ #subscriptions;
81
+ #mutationRecovery;
82
+ /**
83
+ * Client groups gets disabled when the server does not know about it.
84
+ * A disabled client group prevents the client from pushing and pulling.
85
+ */
86
+ isClientGroupDisabled = false;
87
+ #kvStoreProvider;
88
+ lastMutationID = 0;
89
+ /**
90
+ * This is the name Replicache uses for the IndexedDB database where data is
91
+ * stored.
92
+ */
93
+ get idbName() {
94
+ return makeIDBName(this.name, this.schemaVersion);
95
+ }
96
+ /** The schema version of the data understood by this application. */
97
+ schemaVersion;
98
+ get #idbDatabase() {
99
+ return {
100
+ name: this.idbName,
101
+ replicacheName: this.name,
102
+ replicacheFormatVersion: FormatVersion.Latest,
103
+ schemaVersion: this.schemaVersion,
104
+ };
105
+ }
106
+ #closed = false;
107
+ #online = true;
108
+ #clientID = makeClientID();
109
+ #ready;
110
+ #profileIDPromise;
111
+ #clientGroupIDPromise;
112
+ #mutatorRegistry = {};
113
+ /**
114
+ * The mutators that was registered in the constructor.
115
+ */
116
+ mutate;
117
+ // Number of pushes/pulls at the moment.
118
+ #pushCounter = 0;
119
+ #pullCounter = 0;
120
+ #pullConnectionLoop;
121
+ #pushConnectionLoop;
122
+ /**
123
+ * The duration between each periodic {@link pull}. Setting this to `null`
124
+ * disables periodic pull completely. Pull will still happen if you call
125
+ * {@link pull} manually.
126
+ */
127
+ pullInterval;
128
+ /**
129
+ * The delay between when a change is made to Replicache and when Replicache
130
+ * attempts to push that change.
131
+ */
132
+ pushDelay;
133
+ #requestOptions;
134
+ /**
135
+ * The function to use to pull data from the server.
136
+ */
137
+ puller;
138
+ /**
139
+ * The function to use to push data to the server.
140
+ */
141
+ pusher;
142
+ memdag;
143
+ perdag;
144
+ #idbDatabases;
145
+ #lc;
146
+ #closeAbortController = new AbortController();
147
+ #persistIsRunning = false;
148
+ #enableScheduledPersist;
149
+ #enableScheduledRefresh;
150
+ #enablePullAndPushInOpen;
151
+ #persistScheduler = new ProcessScheduler(() => this.persist(), PERSIST_IDLE_TIMEOUT_MS, PERSIST_THROTTLE_MS, this.#closeAbortController.signal);
152
+ #onPersist;
153
+ #refreshScheduler = new ProcessScheduler(() => this.refresh(), REFRESH_IDLE_TIMEOUT_MS, REFRESH_THROTTLE_MS, this.#closeAbortController.signal);
154
+ /**
155
+ * The options used to control the {@link pull} and push request behavior. This
156
+ * object is live so changes to it will affect the next pull or push call.
157
+ */
158
+ get requestOptions() {
159
+ return this.#requestOptions;
160
+ }
161
+ /**
162
+ * `onSync(true)` is called when Replicache transitions from no push or pull
163
+ * happening to at least one happening. `onSync(false)` is called in the
164
+ * opposite case: when Replicache transitions from at least one push or pull
165
+ * happening to none happening.
166
+ *
167
+ * This can be used in a React like app by doing something like the following:
168
+ *
169
+ * ```js
170
+ * const [syncing, setSyncing] = useState(false);
171
+ * useEffect(() => {
172
+ * rep.onSync = setSyncing;
173
+ * }, [rep]);
174
+ * ```
175
+ */
176
+ onSync = null;
177
+ /**
178
+ * `onClientStateNotFound` is called when the persistent client has been
179
+ * garbage collected. This can happen if the client has no pending mutations
180
+ * and has not been used for a while.
181
+ *
182
+ * The default behavior is to reload the page (using `location.reload()`). Set
183
+ * this to `null` or provide your own function to prevent the page from
184
+ * reloading automatically.
185
+ */
186
+ onClientStateNotFound = reload;
187
+ /**
188
+ * `onUpdateNeeded` is called when a code update is needed.
189
+ *
190
+ * A code update can be needed because:
191
+ * - the server no longer supports the {@link pushVersion},
192
+ * {@link pullVersion} or {@link schemaVersion} of the current code.
193
+ * - a new Replicache client has created a new client group, because its code
194
+ * has different mutators, indexes, schema version and/or format version
195
+ * from this Replicache client. This is likely due to the new client having
196
+ * newer code. A code update is needed to be able to locally sync with this
197
+ * new Replicache client (i.e. to sync while offline, the clients can still
198
+ * sync with each other via the server).
199
+ *
200
+ * The default behavior is to reload the page (using `location.reload()`). Set
201
+ * this to `null` or provide your own function to prevent the page from
202
+ * reloading automatically. You may want to provide your own function to
203
+ * display a toast to inform the end user there is a new version of your app
204
+ * available and prompting them to refresh.
205
+ */
206
+ onUpdateNeeded = reload;
207
+ /**
208
+ * This gets called when we get an HTTP unauthorized (401) response from the
209
+ * push or pull endpoint. Set this to a function that will ask your user to
210
+ * reauthenticate.
211
+ */
212
+ getAuth = null;
213
+ // These three are used for testing
214
+ onPushInvoked = () => undefined;
215
+ onBeginPull = () => undefined;
216
+ onRecoverMutations = (r) => r;
217
+ constructor(options, implOptions = {}) {
218
+ validateOptions(options);
219
+ const { name, logLevel = 'info', logSinks = [consoleLogSink], pullURL = '', auth, pushDelay = 10, pushURL = '', schemaVersion = '', pullInterval = 60000, mutators = {}, requestOptions = {}, puller, pusher, indexes = {}, clientMaxAgeMs = CLIENT_MAX_INACTIVE_TIME, } = options;
220
+ const { enableMutationRecovery = true, enableScheduledPersist = true, enableScheduledRefresh = true, enablePullAndPushInOpen = true, enableClientGroupForking = true, } = implOptions;
221
+ this.auth = auth ?? '';
222
+ this.pullURL = pullURL;
223
+ this.pushURL = pushURL;
224
+ this.name = name;
225
+ this.schemaVersion = schemaVersion;
226
+ this.pullInterval = pullInterval;
227
+ this.pushDelay = pushDelay;
228
+ this.puller = puller ?? getDefaultPuller(this);
229
+ this.pusher = pusher ?? getDefaultPusher(this);
230
+ this.#enableScheduledPersist = enableScheduledPersist;
231
+ this.#enableScheduledRefresh = enableScheduledRefresh;
232
+ this.#enablePullAndPushInOpen = enablePullAndPushInOpen;
233
+ this.#lc = createLogContext(logLevel, logSinks, { name });
234
+ this.#lc.debug?.('Constructing Replicache', {
235
+ name,
236
+ 'replicache version': version,
237
+ });
238
+ this.#subscriptions = new SubscriptionsManagerImpl(this.#queryInternal, this.#lc, this.#closeAbortController.signal);
239
+ const kvStoreProvider = getKVStoreProvider(this.#lc, options.kvStore);
240
+ this.#kvStoreProvider = kvStoreProvider;
241
+ const perKVStore = kvStoreProvider.create(this.idbName);
242
+ this.#idbDatabases = new IDBDatabasesStore(kvStoreProvider.create);
243
+ this.perdag = new StoreImpl(perKVStore, newRandomHash, assertHash);
244
+ this.memdag = new LazyStore(this.perdag, LAZY_STORE_SOURCE_CHUNK_CACHE_SIZE_LIMIT, newRandomHash, assertHash);
245
+ // Use a promise-resolve pair so that we have a promise to use even before
246
+ // we call the Open RPC.
247
+ const readyResolver = resolver();
248
+ this.#ready = readyResolver.promise;
249
+ const { minDelayMs = MIN_DELAY_MS, maxDelayMs = MAX_DELAY_MS } = requestOptions;
250
+ this.#requestOptions = { maxDelayMs, minDelayMs };
251
+ const visibilityWatcher = getDocumentVisibilityWatcher(getBrowserGlobal('document'), 0, this.#closeAbortController.signal);
252
+ this.#pullConnectionLoop = new ConnectionLoop(this.#lc.withContext('PULL'), new PullDelegate(this, () => this.#invokePull()), visibilityWatcher);
253
+ this.#pushConnectionLoop = new ConnectionLoop(this.#lc.withContext('PUSH'), new PushDelegate(this, () => this.#invokePush()));
254
+ this.mutate = this.#registerMutators(mutators);
255
+ const profileIDResolver = resolver();
256
+ this.#profileIDPromise = profileIDResolver.promise;
257
+ const clientGroupIDResolver = resolver();
258
+ this.#clientGroupIDPromise = clientGroupIDResolver.promise;
259
+ this.#mutationRecovery = new MutationRecovery({
260
+ delegate: this,
261
+ lc: this.#lc,
262
+ enableMutationRecovery,
263
+ wrapInOnlineCheck: this.#wrapInOnlineCheck.bind(this),
264
+ wrapInReauthRetries: this.#wrapInReauthRetries.bind(this),
265
+ isPullDisabled: this.#isPullDisabled.bind(this),
266
+ isPushDisabled: this.#isPushDisabled.bind(this),
267
+ clientGroupIDPromise: this.#clientGroupIDPromise,
268
+ });
269
+ this.#onPersist = initOnPersistChannel(this.name, this.#closeAbortController.signal, persistInfo => {
270
+ void this.#handlePersist(persistInfo);
271
+ });
272
+ void this.#open(indexes, enableClientGroupForking, clientMaxAgeMs, profileIDResolver.resolve, clientGroupIDResolver.resolve, readyResolver.resolve);
273
+ }
274
+ async #open(indexes, enableClientGroupForking, clientMaxAgeMs, profileIDResolver, resolveClientGroupID, resolveReady) {
275
+ const { clientID } = this;
276
+ // If we are currently closing a Replicache instance with the same name,
277
+ // wait for it to finish closing.
278
+ await closingInstances.get(this.name);
279
+ await this.#idbDatabases.getProfileID().then(profileIDResolver);
280
+ await this.#idbDatabases.putDatabase(this.#idbDatabase);
281
+ const [client, headHash, clients, isNewClientGroup] = await initClientV6(clientID, this.#lc, this.perdag, Object.keys(this.#mutatorRegistry), indexes, FormatVersion.Latest, enableClientGroupForking);
282
+ resolveClientGroupID(client.clientGroupID);
283
+ await withWrite(this.memdag, write => write.setHead(DEFAULT_HEAD_NAME, headHash));
284
+ // Now we have a profileID, a clientID, a clientGroupID and DB!
285
+ resolveReady();
286
+ if (this.#enablePullAndPushInOpen) {
287
+ this.pull().catch(noop);
288
+ this.push().catch(noop);
289
+ }
290
+ const { signal } = this.#closeAbortController;
291
+ startHeartbeats(clientID, this.perdag, () => {
292
+ this.#clientStateNotFoundOnClient(clientID);
293
+ }, HEARTBEAT_INTERVAL, this.#lc, signal);
294
+ initClientGC(clientID, this.perdag, clientMaxAgeMs, GC_INTERVAL, this.#lc, signal);
295
+ initCollectIDBDatabases(this.#idbDatabases, this.#kvStoreProvider.drop, COLLECT_IDB_INTERVAL, INITIAL_COLLECT_IDB_DELAY, SDD_IDB_MAX_AGE, 2 * clientMaxAgeMs, this.#lc, signal);
296
+ initClientGroupGC(this.perdag, this.#lc, signal);
297
+ initNewClientChannel(this.name, this.idbName, signal, client.clientGroupID, isNewClientGroup, () => {
298
+ this.#fireOnUpdateNeeded(updateNeededReasonNewClientGroup);
299
+ }, this.perdag);
300
+ setIntervalWithSignal(() => this.recoverMutations(), RECOVER_MUTATIONS_INTERVAL_MS, signal);
301
+ void this.recoverMutations(clients);
302
+ getBrowserGlobal('document')?.addEventListener('visibilitychange', this.#onVisibilityChange);
303
+ }
304
+ #onVisibilityChange = async () => {
305
+ if (this.#closed) {
306
+ return;
307
+ }
308
+ // In case of running in a worker, we don't have a document.
309
+ if (getBrowserGlobal('document')?.visibilityState !== 'visible') {
310
+ return;
311
+ }
312
+ await this.#checkForClientStateNotFoundAndCallHandler();
313
+ };
314
+ async #checkForClientStateNotFoundAndCallHandler() {
315
+ const { clientID } = this;
316
+ const hasClientState = await withRead(this.perdag, read => persistHasClientState(clientID, read));
317
+ if (!hasClientState) {
318
+ this.#clientStateNotFoundOnClient(clientID);
319
+ }
320
+ return !hasClientState;
321
+ }
322
+ /**
323
+ * The browser profile ID for this browser profile. Every instance of Replicache
324
+ * browser-profile-wide shares the same profile ID.
325
+ */
326
+ get profileID() {
327
+ return this.#profileIDPromise;
328
+ }
329
+ /**
330
+ * The client ID for this instance of Replicache. Each instance of Replicache
331
+ * gets a unique client ID.
332
+ */
333
+ get clientID() {
334
+ return this.#clientID;
335
+ }
336
+ /**
337
+ * The client group ID for this instance of Replicache. Instances of
338
+ * Replicache will have the same client group ID if and only if they have
339
+ * the same name, mutators, indexes, schema version, format version, and
340
+ * browser profile.
341
+ */
342
+ get clientGroupID() {
343
+ return this.#clientGroupIDPromise;
344
+ }
345
+ /**
346
+ * `onOnlineChange` is called when the {@link online} property changes. See
347
+ * {@link online} for more details.
348
+ */
349
+ onOnlineChange = null;
350
+ /**
351
+ * A rough heuristic for whether the client is currently online. Note that
352
+ * there is no way to know for certain whether a client is online - the next
353
+ * request can always fail. This property returns true if the last sync attempt succeeded,
354
+ * and false otherwise.
355
+ */
356
+ get online() {
357
+ return this.#online;
358
+ }
359
+ /**
360
+ * Whether the Replicache database has been closed. Once Replicache has been
361
+ * closed it no longer syncs and you can no longer read or write data out of
362
+ * it. After it has been closed it is pretty much useless and should not be
363
+ * used any more.
364
+ */
365
+ get closed() {
366
+ return this.#closed;
367
+ }
368
+ /**
369
+ * Closes this Replicache instance.
370
+ *
371
+ * When closed all subscriptions end and no more read or writes are allowed.
372
+ */
373
+ async close() {
374
+ this.#closed = true;
375
+ const { promise, resolve } = resolver();
376
+ closingInstances.set(this.name, promise);
377
+ this.#closeAbortController.abort();
378
+ getBrowserGlobal('document')?.removeEventListener('visibilitychange', this.#onVisibilityChange);
379
+ await this.#ready;
380
+ const closingPromises = [
381
+ this.memdag.close(),
382
+ this.perdag.close(),
383
+ this.#idbDatabases.close(),
384
+ ];
385
+ this.#pullConnectionLoop.close();
386
+ this.#pushConnectionLoop.close();
387
+ this.#subscriptions.clear();
388
+ await Promise.all(closingPromises);
389
+ closingInstances.delete(this.name);
390
+ resolve();
391
+ }
392
+ async maybeEndPull(syncHead, requestID) {
393
+ for (;;) {
394
+ if (this.#closed) {
395
+ return;
396
+ }
397
+ await this.#ready;
398
+ const { clientID } = this;
399
+ const lc = this.#lc
400
+ .withContext('maybeEndPull')
401
+ .withContext('requestID', requestID);
402
+ const { replayMutations, diffs } = await maybeEndPull(this.memdag, lc, syncHead, clientID, this.#subscriptions, FormatVersion.Latest);
403
+ if (!replayMutations || replayMutations.length === 0) {
404
+ // All done.
405
+ await this.#subscriptions.fire(diffs);
406
+ void this.#schedulePersist();
407
+ return;
408
+ }
409
+ // Replay.
410
+ for (const mutation of replayMutations) {
411
+ // TODO(greg): I'm not sure why this was in Replicache#_mutate...
412
+ // Ensure that we run initial pending subscribe functions before starting a
413
+ // write transaction.
414
+ if (this.#subscriptions.hasPendingSubscriptionRuns) {
415
+ await Promise.resolve();
416
+ }
417
+ const { meta } = mutation;
418
+ syncHead = await withWriteNoImplicitCommit(this.memdag, dagWrite => rebaseMutationAndCommit(mutation, dagWrite, syncHead, SYNC_HEAD_NAME, this.#mutatorRegistry, lc, isLocalMetaDD31(meta) ? meta.clientID : clientID, FormatVersion.Latest));
419
+ }
420
+ }
421
+ }
422
+ #invokePull() {
423
+ if (this.#isPullDisabled()) {
424
+ return Promise.resolve(true);
425
+ }
426
+ return this.#wrapInOnlineCheck(async () => {
427
+ try {
428
+ this.#changeSyncCounters(0, 1);
429
+ const { syncHead, requestID, ok } = await this.beginPull();
430
+ if (!ok) {
431
+ return false;
432
+ }
433
+ if (syncHead !== emptyHash) {
434
+ await this.maybeEndPull(syncHead, requestID);
435
+ }
436
+ }
437
+ catch (e) {
438
+ throw await this.#convertToClientStateNotFoundError(e);
439
+ }
440
+ finally {
441
+ this.#changeSyncCounters(0, -1);
442
+ }
443
+ return true;
444
+ }, 'Pull');
445
+ }
446
+ #isPullDisabled() {
447
+ return (this.isClientGroupDisabled ||
448
+ (this.pullURL === '' && isDefaultPuller(this.puller)));
449
+ }
450
+ async #wrapInOnlineCheck(f, name) {
451
+ let online = true;
452
+ try {
453
+ return await f();
454
+ }
455
+ catch (e) {
456
+ // The error paths of beginPull and maybeEndPull need to be reworked.
457
+ //
458
+ // We want to distinguish between:
459
+ // a) network requests failed -- we're offline basically
460
+ // b) sync was aborted because one's already in progress
461
+ // c) oh noes - something unexpected happened
462
+ //
463
+ // Right now, all of these come out as errors. We distinguish (b) with a
464
+ // hacky string search. (a) and (c) are not distinguishable currently
465
+ // because repc doesn't provide sufficient information, so we treat all
466
+ // errors that aren't (b) as (a).
467
+ if (e instanceof PushError || e instanceof PullError) {
468
+ online = false;
469
+ this.#lc.debug?.(`${name} threw:\n`, e, '\nwith cause:\n', e.causedBy);
470
+ }
471
+ else if (e instanceof ReportError) {
472
+ this.#lc.error?.(e);
473
+ }
474
+ else {
475
+ this.#lc.info?.(`${name} threw:\n`, e);
476
+ }
477
+ return false;
478
+ }
479
+ finally {
480
+ if (this.#online !== online) {
481
+ this.#online = online;
482
+ this.onOnlineChange?.(online);
483
+ if (online) {
484
+ void this.recoverMutations();
485
+ }
486
+ }
487
+ }
488
+ }
489
+ async #wrapInReauthRetries(f, verb, lc, preAuth = noop, postAuth = noop) {
490
+ const { clientID } = this;
491
+ let reauthAttempts = 0;
492
+ let lastResult;
493
+ lc = lc.withContext(verb);
494
+ do {
495
+ const requestID = newRequestID(clientID);
496
+ const requestLc = lc.withContext('requestID', requestID);
497
+ const { httpRequestInfo, result } = await f(requestID, requestLc);
498
+ lastResult = result;
499
+ if (!httpRequestInfo) {
500
+ return {
501
+ result,
502
+ authFailure: false,
503
+ };
504
+ }
505
+ const { errorMessage, httpStatusCode } = httpRequestInfo;
506
+ if (errorMessage || httpStatusCode !== 200) {
507
+ // TODO(arv): Maybe we should not log the server URL when the error comes
508
+ // from a Pusher/Puller?
509
+ requestLc.error?.(`Got a non 200 response doing ${verb}: ${httpStatusCode}` +
510
+ (errorMessage ? `: ${errorMessage}` : ''));
511
+ }
512
+ if (httpStatusCode !== httpStatusUnauthorized) {
513
+ return {
514
+ result,
515
+ authFailure: false,
516
+ };
517
+ }
518
+ if (!this.getAuth) {
519
+ return {
520
+ result,
521
+ authFailure: true,
522
+ };
523
+ }
524
+ let auth;
525
+ try {
526
+ await preAuth();
527
+ auth = await this.getAuth();
528
+ }
529
+ finally {
530
+ await postAuth();
531
+ }
532
+ if (auth === null || auth === undefined) {
533
+ return {
534
+ result,
535
+ authFailure: true,
536
+ };
537
+ }
538
+ this.auth = auth;
539
+ reauthAttempts++;
540
+ } while (reauthAttempts < MAX_REAUTH_TRIES);
541
+ lc.info?.('Tried to reauthenticate too many times');
542
+ return {
543
+ result: lastResult,
544
+ authFailure: true,
545
+ };
546
+ }
547
+ #isPushDisabled() {
548
+ return (this.isClientGroupDisabled ||
549
+ (this.pushURL === '' && isDefaultPusher(this.pusher)));
550
+ }
551
+ async #invokePush() {
552
+ if (TESTING) {
553
+ this.onPushInvoked();
554
+ }
555
+ if (this.#isPushDisabled()) {
556
+ return true;
557
+ }
558
+ await this.#ready;
559
+ const profileID = await this.#profileIDPromise;
560
+ const { clientID } = this;
561
+ const clientGroupID = await this.#clientGroupIDPromise;
562
+ return this.#wrapInOnlineCheck(async () => {
563
+ const { result: pusherResult } = await this.#wrapInReauthRetries(async (requestID, requestLc) => {
564
+ try {
565
+ this.#changeSyncCounters(1, 0);
566
+ const pusherResult = await push(requestID, this.memdag, requestLc, profileID, clientGroupID, clientID, this.pusher, this.schemaVersion, PUSH_VERSION_DD31);
567
+ return {
568
+ result: pusherResult,
569
+ httpRequestInfo: pusherResult?.httpRequestInfo,
570
+ };
571
+ }
572
+ finally {
573
+ this.#changeSyncCounters(-1, 0);
574
+ }
575
+ }, 'push', this.#lc);
576
+ if (pusherResult === undefined) {
577
+ // No pending mutations.
578
+ return true;
579
+ }
580
+ const { response, httpRequestInfo } = pusherResult;
581
+ if (isVersionNotSupportedResponse(response)) {
582
+ this.#handleVersionNotSupportedResponse(response);
583
+ }
584
+ else if (isClientStateNotFoundResponse(response)) {
585
+ await this.#clientStateNotFoundOnServer();
586
+ }
587
+ // No pushResponse means we didn't do a push because there were no
588
+ // pending mutations.
589
+ return httpRequestInfo.httpStatusCode === 200;
590
+ }, 'Push');
591
+ }
592
+ #handleVersionNotSupportedResponse(response) {
593
+ const reason = {
594
+ type: response.error,
595
+ };
596
+ if (response.versionType) {
597
+ reason.versionType = response.versionType;
598
+ }
599
+ this.#fireOnUpdateNeeded(reason);
600
+ }
601
+ /**
602
+ * Push pushes pending changes to the {@link pushURL}.
603
+ *
604
+ * You do not usually need to manually call push. If {@link pushDelay} is
605
+ * non-zero (which it is by default) pushes happen automatically shortly after
606
+ * mutations.
607
+ *
608
+ * If the server endpoint fails push will be continuously retried with an
609
+ * exponential backoff.
610
+ *
611
+ * @param [now=false] If true, push will happen immediately and ignore
612
+ * {@link pushDelay}, {@link RequestOptions.minDelayMs} as well as the
613
+ * exponential backoff in case of errors.
614
+ * @returns A promise that resolves when the next push completes. In case of
615
+ * errors the first error will reject the returned promise. Subsequent errors
616
+ * will not be reflected in the promise.
617
+ */
618
+ push({ now = false } = {}) {
619
+ return throwIfError(this.#pushConnectionLoop.send(now));
620
+ }
621
+ /**
622
+ * Pull pulls changes from the {@link pullURL}. If there are any changes local
623
+ * changes will get replayed on top of the new server state.
624
+ *
625
+ * If the server endpoint fails pull will be continuously retried with an
626
+ * exponential backoff.
627
+ *
628
+ * @param [now=false] If true, pull will happen immediately and ignore
629
+ * {@link RequestOptions.minDelayMs} as well as the exponential backoff in
630
+ * case of errors.
631
+ * @returns A promise that resolves when the next pull completes. In case of
632
+ * errors the first error will reject the returned promise. Subsequent errors
633
+ * will not be reflected in the promise.
634
+ */
635
+ pull({ now = false } = {}) {
636
+ return throwIfError(this.#pullConnectionLoop.send(now));
637
+ }
638
+ /**
639
+ * Applies an update from the server to Replicache.
640
+ * Throws an error if cookie does not match. In that case the server thinks
641
+ * this client has a different cookie than it does; the caller should disconnect
642
+ * from the server and re-register, which transmits the cookie the client actually
643
+ * has.
644
+ *
645
+ * @experimental This method is under development and its semantics will change.
646
+ */
647
+ async poke(poke) {
648
+ await this.#ready;
649
+ // TODO(MP) Previously we created a request ID here and included it with the
650
+ // PullRequest to the server so we could tie events across client and server
651
+ // together. Since the direction is now reversed, creating and adding a request ID
652
+ // here is kind of silly. We should consider creating the request ID
653
+ // on the *server* and passing it down in the poke for inclusion here in the log
654
+ // context
655
+ const { clientID } = this;
656
+ const requestID = newRequestID(clientID);
657
+ const lc = this.#lc
658
+ .withContext('handlePullResponse')
659
+ .withContext('requestID', requestID);
660
+ const { pullResponse } = poke;
661
+ if (isVersionNotSupportedResponse(pullResponse)) {
662
+ this.#handleVersionNotSupportedResponse(pullResponse);
663
+ return;
664
+ }
665
+ if (isClientStateNotFoundResponse(pullResponse)) {
666
+ await this.#clientStateNotFoundOnServer();
667
+ return;
668
+ }
669
+ const result = await handlePullResponseV1(lc, this.memdag, deepFreeze(poke.baseCookie), pullResponse, clientID, FormatVersion.Latest);
670
+ switch (result.type) {
671
+ case HandlePullResponseResultEnum.Applied:
672
+ await this.maybeEndPull(result.syncHead, requestID);
673
+ break;
674
+ case HandlePullResponseResultEnum.CookieMismatch:
675
+ throw new Error('unexpected base cookie for poke: ' + JSON.stringify(poke));
676
+ case HandlePullResponseResultEnum.NoOp:
677
+ break;
678
+ }
679
+ }
680
+ async beginPull() {
681
+ if (TESTING) {
682
+ this.onBeginPull();
683
+ }
684
+ await this.#ready;
685
+ const profileID = await this.profileID;
686
+ const { clientID } = this;
687
+ const clientGroupID = await this.#clientGroupIDPromise;
688
+ const { result: { beginPullResponse, requestID }, } = await this.#wrapInReauthRetries(async (requestID, requestLc) => {
689
+ const beginPullResponse = await beginPullV1(profileID, clientID, clientGroupID, this.schemaVersion, this.puller, requestID, this.memdag, FormatVersion.Latest, requestLc);
690
+ return {
691
+ result: { beginPullResponse, requestID },
692
+ httpRequestInfo: beginPullResponse.httpRequestInfo,
693
+ };
694
+ }, 'pull', this.#lc, () => this.#changeSyncCounters(0, -1), () => this.#changeSyncCounters(0, 1));
695
+ const { pullResponse } = beginPullResponse;
696
+ if (isVersionNotSupportedResponse(pullResponse)) {
697
+ this.#handleVersionNotSupportedResponse(pullResponse);
698
+ }
699
+ else if (isClientStateNotFoundResponse(beginPullResponse.pullResponse)) {
700
+ await this.#clientStateNotFoundOnServer();
701
+ }
702
+ const { syncHead, httpRequestInfo } = beginPullResponse;
703
+ return { requestID, syncHead, ok: httpRequestInfo.httpStatusCode === 200 };
704
+ }
705
+ async persist() {
706
+ assert(!this.#persistIsRunning);
707
+ this.#persistIsRunning = true;
708
+ try {
709
+ const { clientID } = this;
710
+ await this.#ready;
711
+ if (this.#closed) {
712
+ return;
713
+ }
714
+ try {
715
+ await persistDD31(this.#lc, clientID, this.memdag, this.perdag, this.#mutatorRegistry, () => this.closed, FormatVersion.Latest);
716
+ }
717
+ catch (e) {
718
+ if (e instanceof ClientStateNotFoundError) {
719
+ this.#clientStateNotFoundOnClient(clientID);
720
+ }
721
+ else if (this.#closed) {
722
+ this.#lc.debug?.('Exception persisting during close', e);
723
+ }
724
+ else {
725
+ throw e;
726
+ }
727
+ }
728
+ }
729
+ finally {
730
+ this.#persistIsRunning = false;
731
+ }
732
+ const { clientID } = this;
733
+ const clientGroupID = await this.#clientGroupIDPromise;
734
+ assert(clientGroupID);
735
+ this.#onPersist({ clientID, clientGroupID });
736
+ }
737
+ async refresh() {
738
+ await this.#ready;
739
+ const { clientID } = this;
740
+ if (this.#closed) {
741
+ return;
742
+ }
743
+ let diffs;
744
+ try {
745
+ diffs = await refresh(this.#lc, this.memdag, this.perdag, clientID, this.#mutatorRegistry, this.#subscriptions, () => this.closed, FormatVersion.Latest);
746
+ }
747
+ catch (e) {
748
+ if (e instanceof ClientStateNotFoundError) {
749
+ this.#clientStateNotFoundOnClient(clientID);
750
+ }
751
+ else if (this.#closed) {
752
+ this.#lc.debug?.('Exception refreshing during close', e);
753
+ }
754
+ else {
755
+ throw e;
756
+ }
757
+ }
758
+ if (diffs !== undefined) {
759
+ await this.#subscriptions.fire(diffs);
760
+ }
761
+ }
762
+ #fireOnClientStateNotFound() {
763
+ this.onClientStateNotFound?.();
764
+ }
765
+ #clientStateNotFoundOnClient(clientID) {
766
+ this.#lc.error?.(`Client state not found on client, clientID: ${clientID}`);
767
+ this.#fireOnClientStateNotFound();
768
+ }
769
+ async #clientStateNotFoundOnServer() {
770
+ const clientGroupID = await this.#clientGroupIDPromise;
771
+ this.#lc.error?.(`Client state not found on server, clientGroupID: ${clientGroupID}`);
772
+ await this.disableClientGroup();
773
+ this.#fireOnClientStateNotFound();
774
+ }
775
+ async disableClientGroup() {
776
+ const clientGroupID = await this.#clientGroupIDPromise;
777
+ assert(clientGroupID);
778
+ this.isClientGroupDisabled = true;
779
+ await withWrite(this.perdag, dagWrite => disableClientGroup(clientGroupID, dagWrite));
780
+ }
781
+ #fireOnUpdateNeeded(reason) {
782
+ this.#lc.debug?.(`Update needed, reason: ${reason}`);
783
+ this.onUpdateNeeded?.(reason);
784
+ }
785
+ async #schedulePersist() {
786
+ if (!this.#enableScheduledPersist) {
787
+ return;
788
+ }
789
+ await this.#schedule('persist', this.#persistScheduler);
790
+ }
791
+ async #handlePersist(persistInfo) {
792
+ this.#lc.debug?.('Handling persist', persistInfo);
793
+ const clientGroupID = await this.#clientGroupIDPromise;
794
+ if (persistInfo.clientGroupID === clientGroupID) {
795
+ void this.#scheduleRefresh();
796
+ }
797
+ }
798
+ async #scheduleRefresh() {
799
+ if (!this.#enableScheduledRefresh) {
800
+ return;
801
+ }
802
+ await this.#schedule('refresh from storage', this.#refreshScheduler);
803
+ }
804
+ async #schedule(name, scheduler) {
805
+ try {
806
+ await scheduler.schedule();
807
+ }
808
+ catch (e) {
809
+ if (e instanceof AbortError) {
810
+ this.#lc.debug?.(`Scheduled ${name} did not complete before close.`);
811
+ }
812
+ else {
813
+ this.#lc.error?.(`Error during ${name}`, e);
814
+ }
815
+ }
816
+ }
817
+ #changeSyncCounters(pushDelta, pullDelta) {
818
+ this.#pushCounter += pushDelta;
819
+ this.#pullCounter += pullDelta;
820
+ const delta = pushDelta + pullDelta;
821
+ const counter = this.#pushCounter + this.#pullCounter;
822
+ if ((delta === 1 && counter === 1) || counter === 0) {
823
+ const syncing = counter > 0;
824
+ // Run in a new microtask.
825
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
826
+ Promise.resolve().then(() => this.onSync?.(syncing));
827
+ }
828
+ }
829
+ /**
830
+ * Subscribe to the result of a {@link query}. The `body` function is
831
+ * evaluated once and its results are returned via `onData`.
832
+ *
833
+ * Thereafter, each time the the result of `body` changes, `onData` is fired
834
+ * again with the new result.
835
+ *
836
+ * `subscribe()` goes to significant effort to avoid extraneous work
837
+ * re-evaluating subscriptions:
838
+ *
839
+ * 1. subscribe tracks the keys that `body` accesses each time it runs. `body`
840
+ * is only re-evaluated when those keys change.
841
+ * 2. subscribe only re-fires `onData` in the case that a result changes by
842
+ * way of the `isEqual` option which defaults to doing a deep JSON value
843
+ * equality check.
844
+ *
845
+ * Because of (1), `body` must be a pure function of the data in Replicache.
846
+ * `body` must not access anything other than the `tx` parameter passed to it.
847
+ *
848
+ * Although subscribe is as efficient as it can be, it is somewhat constrained
849
+ * by the goal of returning an arbitrary computation of the cache. For even
850
+ * better performance (but worse dx), see {@link experimentalWatch}.
851
+ *
852
+ * If an error occurs in the `body` the `onError` function is called if
853
+ * present. Otherwise, the error is logged at log level 'error'.
854
+ *
855
+ * To cancel the subscription, call the returned function.
856
+ *
857
+ * @param body The function to evaluate to get the value to pass into
858
+ * `onData`.
859
+ * @param options Options is either a function or an object. If it is a
860
+ * function it is equivalent to passing it as the `onData` property of an
861
+ * object.
862
+ */
863
+ subscribe(body, options) {
864
+ if (typeof options === 'function') {
865
+ options = { onData: options };
866
+ }
867
+ const { onData, onError, onDone, isEqual } = options;
868
+ return this.#subscriptions.add(new SubscriptionImpl(body, onData, onError, onDone, isEqual));
869
+ }
870
+ experimentalWatch(callback, options) {
871
+ return this.#subscriptions.add(new WatchSubscription(callback, options));
872
+ }
873
+ /**
874
+ * Query is used for read transactions. It is recommended to use transactions
875
+ * to ensure you get a consistent view across multiple calls to `get`, `has`
876
+ * and `scan`.
877
+ */
878
+ query(body) {
879
+ return this.#queryInternal(body);
880
+ }
881
+ get cookie() {
882
+ return this.#ready.then(() => withRead(this.memdag, async (dagRead) => {
883
+ const mainHeadHash = await dagRead.getHead(DEFAULT_HEAD_NAME);
884
+ if (!mainHeadHash) {
885
+ throw new Error('Internal no main head found');
886
+ }
887
+ const baseSnapshot = await baseSnapshotFromHash(mainHeadHash, dagRead);
888
+ const baseSnapshotMeta = baseSnapshot.meta;
889
+ const cookie = baseSnapshotMeta.cookieJSON;
890
+ assertCookie(cookie);
891
+ return cookie;
892
+ }));
893
+ }
894
+ #queryInternal = async (body) => {
895
+ await this.#ready;
896
+ const { clientID } = this;
897
+ return withRead(this.memdag, async (dagRead) => {
898
+ try {
899
+ const dbRead = await readFromDefaultHead(dagRead, FormatVersion.Latest);
900
+ const tx = new ReadTransactionImpl(clientID, dbRead, this.#lc);
901
+ return await body(tx);
902
+ }
903
+ catch (ex) {
904
+ throw await this.#convertToClientStateNotFoundError(ex);
905
+ }
906
+ });
907
+ };
908
+ #register(name, mutatorImpl) {
909
+ this.#mutatorRegistry[name] = mutatorImpl;
910
+ return (args) => this.#mutate(name, mutatorImpl, args, performance.now());
911
+ }
912
+ #registerMutators(regs) {
913
+ const rv = Object.create(null);
914
+ for (const k in regs) {
915
+ rv[k] = this.#register(k, regs[k]);
916
+ }
917
+ return rv;
918
+ }
919
+ async #mutate(name, mutatorImpl, args, timestamp) {
920
+ const frozenArgs = deepFreeze(args ?? null);
921
+ // Ensure that we run initial pending subscribe functions before starting a
922
+ // write transaction.
923
+ if (this.#subscriptions.hasPendingSubscriptionRuns) {
924
+ await Promise.resolve();
925
+ }
926
+ await this.#ready;
927
+ const { clientID } = this;
928
+ return withWriteNoImplicitCommit(this.memdag, async (dagWrite) => {
929
+ try {
930
+ const headHash = await mustGetHeadHash(DEFAULT_HEAD_NAME, dagWrite);
931
+ const originalHash = null;
932
+ const dbWrite = await newWriteLocal(headHash, name, frozenArgs, originalHash, dagWrite, timestamp, clientID, FormatVersion.Latest);
933
+ const tx = new WriteTransactionImpl(clientID, await dbWrite.getMutationID(), 'initial', dbWrite, this.#lc);
934
+ const result = await mutatorImpl(tx, args);
935
+ throwIfClosed(dbWrite);
936
+ const lastMutationID = await dbWrite.getMutationID();
937
+ const diffs = await dbWrite.commitWithDiffs(DEFAULT_HEAD_NAME, this.#subscriptions);
938
+ // Update this after the commit in case the commit fails.
939
+ this.lastMutationID = lastMutationID;
940
+ // Send is not supposed to reject
941
+ this.#pushConnectionLoop.send(false).catch(() => void 0);
942
+ await this.#subscriptions.fire(diffs);
943
+ void this.#schedulePersist();
944
+ return result;
945
+ }
946
+ catch (ex) {
947
+ throw await this.#convertToClientStateNotFoundError(ex);
948
+ }
949
+ });
950
+ }
951
+ /**
952
+ * In the case we get a ChunkNotFoundError we check if the client got garbage
953
+ * collected and if so change the error to a ClientStateNotFoundError instead
954
+ */
955
+ async #convertToClientStateNotFoundError(ex) {
956
+ if (ex instanceof ChunkNotFoundError &&
957
+ (await this.#checkForClientStateNotFoundAndCallHandler())) {
958
+ return new ClientStateNotFoundError(this.clientID);
959
+ }
960
+ return ex;
961
+ }
962
+ recoverMutations(preReadClientMap) {
963
+ const result = this.#mutationRecovery.recoverMutations(preReadClientMap, this.#ready, this.perdag, this.#idbDatabase, this.#idbDatabases, this.#kvStoreProvider.create);
964
+ if (TESTING) {
965
+ void this.onRecoverMutations(result);
966
+ }
967
+ return result;
968
+ }
969
+ /**
970
+ * List of pending mutations. The order of this is from oldest to newest.
971
+ *
972
+ * Gives a list of local mutations that have `mutationID` >
973
+ * `syncHead.mutationID` that exists on the main client group.
974
+ *
975
+ * @experimental This method is experimental and may change in the future.
976
+ */
977
+ experimentalPendingMutations() {
978
+ return withRead(this.memdag, pendingMutationsForAPI);
979
+ }
980
+ }
981
+ // This map is used to keep track of closing instances of Replicache. When an
982
+ // instance is opening we wait for any currently closing instances.
983
+ const closingInstances = new Map();
984
+ async function throwIfError(p) {
985
+ const res = await p;
986
+ if (res) {
987
+ throw res.error;
988
+ }
989
+ }
990
+ function reload() {
991
+ if (typeof location !== 'undefined') {
992
+ location.reload();
993
+ }
994
+ }
995
+ function validateOptions(options) {
996
+ const { name, clientMaxAgeMs } = options;
997
+ if (typeof name !== 'string' || !name) {
998
+ throw new TypeError('name is required and must be non-empty');
999
+ }
1000
+ if (clientMaxAgeMs !== undefined) {
1001
+ const min = Math.max(GC_INTERVAL, HEARTBEAT_INTERVAL);
1002
+ if (typeof clientMaxAgeMs !== 'number' || clientMaxAgeMs <= min) {
1003
+ throw new TypeError(`clientAgeMaxMs must be a number larger than ${min}ms`);
1004
+ }
1005
+ }
1006
+ }
1007
+ //# sourceMappingURL=replicache-impl.js.map