@rocicorp/zero 1.11.0-canary.8 → 1.11.0-canary.9

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 (268) hide show
  1. package/out/analyze-query/src/analyze-cli.js +2 -2
  2. package/out/analyze-query/src/analyze-cli.js.map +1 -1
  3. package/out/replicache/src/dag/gc.d.ts.map +1 -1
  4. package/out/replicache/src/dag/gc.js +2 -1
  5. package/out/replicache/src/dag/gc.js.map +1 -1
  6. package/out/replicache/src/dag/invalid-ref-count-error.d.ts +12 -0
  7. package/out/replicache/src/dag/invalid-ref-count-error.d.ts.map +1 -0
  8. package/out/replicache/src/dag/invalid-ref-count-error.js +19 -0
  9. package/out/replicache/src/dag/invalid-ref-count-error.js.map +1 -0
  10. package/out/replicache/src/dag/store-impl.d.ts +12 -2
  11. package/out/replicache/src/dag/store-impl.d.ts.map +1 -1
  12. package/out/replicache/src/dag/store-impl.js +22 -8
  13. package/out/replicache/src/dag/store-impl.js.map +1 -1
  14. package/out/replicache/src/db/rebase.d.ts +14 -2
  15. package/out/replicache/src/db/rebase.d.ts.map +1 -1
  16. package/out/replicache/src/db/rebase.js +27 -5
  17. package/out/replicache/src/db/rebase.js.map +1 -1
  18. package/out/replicache/src/persist/collect-idb-databases.d.ts +10 -5
  19. package/out/replicache/src/persist/collect-idb-databases.d.ts.map +1 -1
  20. package/out/replicache/src/persist/collect-idb-databases.js +16 -9
  21. package/out/replicache/src/persist/collect-idb-databases.js.map +1 -1
  22. package/out/replicache/src/persist/persist.d.ts.map +1 -1
  23. package/out/replicache/src/persist/persist.js +12 -5
  24. package/out/replicache/src/persist/persist.js.map +1 -1
  25. package/out/replicache/src/persist/refresh.js +6 -2
  26. package/out/replicache/src/persist/refresh.js.map +1 -1
  27. package/out/replicache/src/replicache-impl.d.ts +7 -3
  28. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  29. package/out/replicache/src/replicache-impl.js +98 -11
  30. package/out/replicache/src/replicache-impl.js.map +1 -1
  31. package/out/replicache/src/replicache-options.d.ts +9 -0
  32. package/out/replicache/src/replicache-options.d.ts.map +1 -1
  33. package/out/shared/src/iterables.d.ts +2 -0
  34. package/out/shared/src/iterables.d.ts.map +1 -1
  35. package/out/shared/src/iterables.js +18 -1
  36. package/out/shared/src/iterables.js.map +1 -1
  37. package/out/shared/src/ring-buffer.d.ts +8 -0
  38. package/out/shared/src/ring-buffer.d.ts.map +1 -1
  39. package/out/shared/src/ring-buffer.js +22 -1
  40. package/out/shared/src/ring-buffer.js.map +1 -1
  41. package/out/zero/package.js +2 -3
  42. package/out/zero/package.js.map +1 -1
  43. package/out/zero-cache/src/config/zero-config.js +2 -2
  44. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  45. package/out/zero-cache/src/server/change-streamer.js +1 -1
  46. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  47. package/out/zero-cache/src/server/inspector-delegate.d.ts +18 -2
  48. package/out/zero-cache/src/server/inspector-delegate.d.ts.map +1 -1
  49. package/out/zero-cache/src/server/inspector-delegate.js +20 -7
  50. package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
  51. package/out/zero-cache/src/services/change-source/custom/change-source.js +2 -2
  52. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  53. package/out/zero-cache/src/services/change-source/pg/backfill-resume.d.ts +156 -0
  54. package/out/zero-cache/src/services/change-source/pg/backfill-resume.d.ts.map +1 -0
  55. package/out/zero-cache/src/services/change-source/pg/backfill-resume.js +15 -0
  56. package/out/zero-cache/src/services/change-source/pg/backfill-resume.js.map +1 -0
  57. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts +15 -0
  58. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts.map +1 -0
  59. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +99 -0
  60. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -0
  61. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +14 -16
  62. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  63. package/out/zero-cache/src/services/change-source/pg/change-source.js +33 -126
  64. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  65. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +16 -1
  66. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  67. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +8 -4
  68. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  69. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.d.ts +16 -0
  70. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.d.ts.map +1 -0
  71. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.js +113 -0
  72. package/out/zero-cache/src/services/change-source/pg/replication-slot-cleanup-monitor.js.map +1 -0
  73. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts +1 -15
  74. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  75. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +18 -21
  76. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  77. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +3 -3
  78. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  79. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +8 -9
  80. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  81. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts +17 -1
  82. package/out/zero-cache/src/services/change-streamer/upstream-acker.d.ts.map +1 -1
  83. package/out/zero-cache/src/services/change-streamer/upstream-acker.js +19 -3
  84. package/out/zero-cache/src/services/change-streamer/upstream-acker.js.map +1 -1
  85. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.d.ts.map +1 -1
  86. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js +1 -0
  87. package/out/zero-cache/src/services/change-streamer/vfs-watermark-poller.js.map +1 -1
  88. package/out/zero-cache/src/services/replicator/change-processor.js +1 -0
  89. package/out/zero-cache/src/services/replicator/change-processor.js.map +1 -1
  90. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts +14 -1
  91. package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts.map +1 -1
  92. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +30 -4
  93. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
  94. package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts +1 -1
  95. package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts.map +1 -1
  96. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +2 -2
  97. package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
  98. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  99. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +6 -10
  100. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  101. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  102. package/out/zero-cache/src/services/view-syncer/view-syncer.js +14 -4
  103. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  104. package/out/zero-cache/src/types/streams.d.ts.map +1 -1
  105. package/out/zero-cache/src/types/streams.js +1 -1
  106. package/out/zero-cache/src/types/streams.js.map +1 -1
  107. package/out/zero-cache/src/workers/connection.js +3 -3
  108. package/out/zero-cache/src/workers/connection.js.map +1 -1
  109. package/out/zero-cache/src/workers/syncer.js +2 -2
  110. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  111. package/out/zero-client/src/client/connection-manager.d.ts +20 -0
  112. package/out/zero-client/src/client/connection-manager.d.ts.map +1 -1
  113. package/out/zero-client/src/client/connection-manager.js +32 -14
  114. package/out/zero-client/src/client/connection-manager.js.map +1 -1
  115. package/out/zero-client/src/client/connection-status-enum.d.ts +2 -0
  116. package/out/zero-client/src/client/connection-status-enum.d.ts.map +1 -1
  117. package/out/zero-client/src/client/connection-status-enum.js +3 -1
  118. package/out/zero-client/src/client/connection-status-enum.js.map +1 -1
  119. package/out/zero-client/src/client/connection.d.ts +6 -0
  120. package/out/zero-client/src/client/connection.d.ts.map +1 -1
  121. package/out/zero-client/src/client/connection.js +2 -1
  122. package/out/zero-client/src/client/connection.js.map +1 -1
  123. package/out/zero-client/src/client/context.d.ts +8 -11
  124. package/out/zero-client/src/client/context.d.ts.map +1 -1
  125. package/out/zero-client/src/client/context.js +11 -15
  126. package/out/zero-client/src/client/context.js.map +1 -1
  127. package/out/zero-client/src/client/custom.js +2 -2
  128. package/out/zero-client/src/client/custom.js.map +1 -1
  129. package/out/zero-client/src/client/error.js +1 -1
  130. package/out/zero-client/src/client/mutator-proxy.d.ts.map +1 -1
  131. package/out/zero-client/src/client/mutator-proxy.js +2 -1
  132. package/out/zero-client/src/client/mutator-proxy.js.map +1 -1
  133. package/out/zero-client/src/client/options.d.ts +3 -0
  134. package/out/zero-client/src/client/options.d.ts.map +1 -1
  135. package/out/zero-client/src/client/options.js.map +1 -1
  136. package/out/zero-client/src/client/query-manager.d.ts.map +1 -1
  137. package/out/zero-client/src/client/query-manager.js +42 -6
  138. package/out/zero-client/src/client/query-manager.js.map +1 -1
  139. package/out/zero-client/src/client/version.js +1 -1
  140. package/out/zero-client/src/client/zero-rep.d.ts +1 -0
  141. package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
  142. package/out/zero-client/src/client/zero-rep.js +7 -5
  143. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  144. package/out/zero-client/src/client/zero.d.ts +9 -3
  145. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  146. package/out/zero-client/src/client/zero.js +48 -11
  147. package/out/zero-client/src/client/zero.js.map +1 -1
  148. package/out/zero-client/src/types/query-result.d.ts +2 -0
  149. package/out/zero-client/src/types/query-result.d.ts.map +1 -1
  150. package/out/zero-react/src/use-query.d.ts +5 -4
  151. package/out/zero-react/src/use-query.d.ts.map +1 -1
  152. package/out/zero-react/src/use-query.js +12 -1
  153. package/out/zero-react/src/use-query.js.map +1 -1
  154. package/out/zero-server/src/adapters/drizzle.d.ts +2 -1
  155. package/out/zero-server/src/adapters/drizzle.d.ts.map +1 -1
  156. package/out/zero-server/src/adapters/drizzle.js +12 -7
  157. package/out/zero-server/src/adapters/drizzle.js.map +1 -1
  158. package/out/zero-server/src/adapters/kysely.d.ts +2 -1
  159. package/out/zero-server/src/adapters/kysely.d.ts.map +1 -1
  160. package/out/zero-server/src/adapters/kysely.js +8 -2
  161. package/out/zero-server/src/adapters/kysely.js.map +1 -1
  162. package/out/zero-server/src/adapters/pg.d.ts +1 -0
  163. package/out/zero-server/src/adapters/pg.d.ts.map +1 -1
  164. package/out/zero-server/src/adapters/pg.js +8 -2
  165. package/out/zero-server/src/adapters/pg.js.map +1 -1
  166. package/out/zero-server/src/adapters/postgresjs.d.ts +1 -0
  167. package/out/zero-server/src/adapters/postgresjs.d.ts.map +1 -1
  168. package/out/zero-server/src/adapters/postgresjs.js +7 -1
  169. package/out/zero-server/src/adapters/postgresjs.js.map +1 -1
  170. package/out/zero-server/src/adapters/prisma.d.ts +3 -2
  171. package/out/zero-server/src/adapters/prisma.d.ts.map +1 -1
  172. package/out/zero-server/src/adapters/prisma.js +8 -2
  173. package/out/zero-server/src/adapters/prisma.js.map +1 -1
  174. package/out/zero-server/src/custom.d.ts +5 -1
  175. package/out/zero-server/src/custom.d.ts.map +1 -1
  176. package/out/zero-server/src/custom.js +6 -3
  177. package/out/zero-server/src/custom.js.map +1 -1
  178. package/out/zero-server/src/mod.d.ts +1 -1
  179. package/out/zero-server/src/mod.d.ts.map +1 -1
  180. package/out/zero-server/src/pg-query-executor.d.ts +2 -2
  181. package/out/zero-server/src/pg-query-executor.d.ts.map +1 -1
  182. package/out/zero-server/src/pg-query-executor.js +2 -2
  183. package/out/zero-server/src/pg-query-executor.js.map +1 -1
  184. package/out/zero-server/src/schema.d.ts +2 -2
  185. package/out/zero-server/src/schema.d.ts.map +1 -1
  186. package/out/zero-server/src/schema.js +2 -2
  187. package/out/zero-server/src/schema.js.map +1 -1
  188. package/out/zero-server/src/zql-database.d.ts +21 -1
  189. package/out/zero-server/src/zql-database.d.ts.map +1 -1
  190. package/out/zero-server/src/zql-database.js +33 -2
  191. package/out/zero-server/src/zql-database.js.map +1 -1
  192. package/out/zero-solid/src/solid-view.d.ts +10 -0
  193. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  194. package/out/zero-solid/src/solid-view.js +27 -0
  195. package/out/zero-solid/src/solid-view.js.map +1 -1
  196. package/out/zql/src/ivm/array-view.d.ts +10 -0
  197. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  198. package/out/zql/src/ivm/array-view.js +20 -8
  199. package/out/zql/src/ivm/array-view.js.map +1 -1
  200. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  201. package/out/zql/src/ivm/cap.js +5 -3
  202. package/out/zql/src/ivm/cap.js.map +1 -1
  203. package/out/zql/src/ivm/deferred-input.d.ts.map +1 -1
  204. package/out/zql/src/ivm/deferred-input.js +3 -2
  205. package/out/zql/src/ivm/deferred-input.js.map +1 -1
  206. package/out/zql/src/ivm/exists.d.ts +1 -1
  207. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  208. package/out/zql/src/ivm/exists.js.map +1 -1
  209. package/out/zql/src/ivm/fan-in.d.ts +2 -2
  210. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  211. package/out/zql/src/ivm/fan-in.js +6 -5
  212. package/out/zql/src/ivm/fan-in.js.map +1 -1
  213. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  214. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  215. package/out/zql/src/ivm/fan-out.js.map +1 -1
  216. package/out/zql/src/ivm/filter-operators.d.ts +6 -6
  217. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  218. package/out/zql/src/ivm/filter-operators.js +10 -8
  219. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  220. package/out/zql/src/ivm/filter.d.ts +2 -2
  221. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  222. package/out/zql/src/ivm/filter.js +7 -4
  223. package/out/zql/src/ivm/filter.js.map +1 -1
  224. package/out/zql/src/ivm/flipped-join.js +2 -2
  225. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  226. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts +1 -1
  227. package/out/zql/src/ivm/maybe-split-and-push-edit-change.d.ts.map +1 -1
  228. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +6 -4
  229. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js.map +1 -1
  230. package/out/zql/src/ivm/memory-source.d.ts +2 -2
  231. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  232. package/out/zql/src/ivm/memory-source.js +9 -9
  233. package/out/zql/src/ivm/memory-source.js.map +1 -1
  234. package/out/zql/src/ivm/take.d.ts.map +1 -1
  235. package/out/zql/src/ivm/take.js +3 -4
  236. package/out/zql/src/ivm/take.js.map +1 -1
  237. package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
  238. package/out/zql/src/ivm/union-fan-in.js +9 -7
  239. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  240. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  241. package/out/zql/src/ivm/view-apply-change.js +3 -3
  242. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  243. package/out/zql/src/mutate/custom.d.ts +24 -1
  244. package/out/zql/src/mutate/custom.d.ts.map +1 -1
  245. package/out/zql/src/mutate/custom.js.map +1 -1
  246. package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
  247. package/out/zql/src/mutate/mutator-registry.js +2 -2
  248. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  249. package/out/zql/src/query/query-delegate-base.d.ts +2 -0
  250. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  251. package/out/zql/src/query/query-delegate-base.js +55 -28
  252. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  253. package/out/zql/src/query/query-delegate.d.ts +11 -1
  254. package/out/zql/src/query/query-delegate.d.ts.map +1 -1
  255. package/out/zql/src/query/query-impl.d.ts +1 -0
  256. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  257. package/out/zql/src/query/query-impl.js.map +1 -1
  258. package/out/zql/src/query/query.d.ts +7 -1
  259. package/out/zql/src/query/query.d.ts.map +1 -1
  260. package/out/zql/src/query/runnable-query-impl.d.ts +1 -0
  261. package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
  262. package/out/zql/src/query/runnable-query-impl.js.map +1 -1
  263. package/out/zql/src/query/typed-view.d.ts +1 -1
  264. package/out/zql/src/query/typed-view.d.ts.map +1 -1
  265. package/out/zqlite/src/table-source.d.ts.map +1 -1
  266. package/out/zqlite/src/table-source.js +7 -5
  267. package/out/zqlite/src/table-source.js.map +1 -1
  268. package/package.json +2 -3
@@ -9,7 +9,7 @@ import { parseOptions } from "../../shared/src/options.js";
9
9
  import { ZERO_ENV_VAR_PREFIX } from "../../zero-cache/src/config/zero-config.js";
10
10
  import { Console } from "node:console";
11
11
  import { styleText } from "node:util";
12
- import { WebSocket as WebSocket$1 } from "ws";
12
+ import { WebSocket } from "ws";
13
13
  //#region ../analyze-query/src/analyze-cli.ts
14
14
  var options = {
15
15
  zeroCacheURL: {
@@ -223,7 +223,7 @@ function resolveHandshakeHeaders(config) {
223
223
  return headers;
224
224
  }
225
225
  function installWebSocketHeaderShim(headers) {
226
- class HeaderInjectingWebSocket extends WebSocket$1 {
226
+ class HeaderInjectingWebSocket extends WebSocket {
227
227
  constructor(url, protocols) {
228
228
  super(url, protocols, { headers });
229
229
  }
@@ -1 +1 @@
1
- {"version":3,"file":"analyze-cli.js","names":[],"sources":["../../../../analyze-query/src/analyze-cli.ts"],"sourcesContent":["import '../../shared/src/dotenv.ts';\n\nimport {Console} from 'node:console';\nimport {styleText} from 'node:util';\nimport type {LogSink} from '@rocicorp/logger';\nimport {WebSocket as NodeWebSocket} from 'ws';\nimport {logLevel, logOptions} from '../../otel/src/log-options.ts';\nimport {colorConsole} from '../../shared/src/logging.ts';\nimport {parseOptions} from '../../shared/src/options.ts';\nimport * as v from '../../shared/src/valita.ts';\nimport {ZERO_ENV_VAR_PREFIX} from '../../zero-cache/src/config/zero-config.ts';\nimport {Zero} from '../../zero-client/src/client/zero.ts';\nimport type {AnalyzeQueryResult} from '../../zero-protocol/src/analyze-query-result.ts';\nimport type {AST} from '../../zero-protocol/src/ast.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport {formatPlannerEvents} from '../../zql/src/planner/planner-debug.ts';\nimport {createBuilder} from '../../zql/src/query/create-builder.ts';\nimport type {AnyQuery} from '../../zql/src/query/query.ts';\nimport type {SchemaQuery} from '../../zql/src/query/schema-query.ts';\n\nexport type AnalyzeCLIOptions = {\n schema: Schema;\n /** Defaults to `process.argv.slice(2)`. */\n argv?: readonly string[] | undefined;\n};\n\nconst options = {\n zeroCacheURL: {\n type: v.string().optional(),\n desc: [\n 'URL of the remote zero-cache to analyze against.',\n 'Accepts http(s):// or ws(s):// (ws(s) is the transport actually used).',\n ],\n },\n adminPassword: {\n type: v.string().optional(),\n desc: [\n 'Admin password for zero-cache.',\n 'Required when the server is configured with one; ignored in dev mode.',\n ],\n },\n authToken: {\n type: v.string().optional(),\n desc: [\n 'Raw JWT forwarded to zero-cache.',\n 'Used server-side to fill permission variables for the query.',\n ],\n },\n cookie: {\n type: v.string().optional(),\n desc: [\n 'Cookie header value sent on the WebSocket upgrade request,',\n 'e.g. `session=abc; foo=bar`. Use this when zero-cache is behind',\n 'a proxy that resolves auth via cookies. Merged with --headers-json',\n '(--cookie wins on conflict).',\n ],\n },\n headersJson: {\n type: v.string().optional(),\n desc: [\n 'JSON object of arbitrary headers to send on the WebSocket upgrade',\n 'request, e.g. `{\"x-api-key\":\"...\"}`. Escape hatch for exotic auth',\n 'schemes; prefer --auth-token or --cookie when possible.',\n ],\n },\n userId: {\n type: v.string().optional(),\n desc: [\n 'Optional userID to report to zero-cache.',\n 'Omit for anonymous analysis.',\n ],\n },\n ast: {\n type: v.string().optional(),\n desc: [\n 'JSON-encoded AST. Exactly one of --ast / --query / --query-name is required.',\n 'The AST is sent to the server verbatim — provide it in server (post-mapping) form.',\n ],\n },\n query: {\n type: v.string().optional(),\n desc: [\n 'ZQL query in chain form, e.g. `issue.related(\"comments\").limit(10)`.',\n 'Evaluated against the schema you pass to runAnalyzeCLI.',\n ],\n },\n queryName: {\n type: v.string().optional(),\n desc: [\n 'Name of a server-registered custom (named) query.',\n 'The server resolves the name + args via its registered query handler.',\n ],\n },\n queryArgs: {\n type: v.string().optional(),\n desc: [\n 'JSON-encoded array of arguments for --query-name. Defaults to `[]`.',\n ],\n },\n outputVendedRows: {\n type: v.boolean().default(false),\n desc: [\n 'Include the rows read from the replica to execute the query.',\n 'Each row appears once per read.',\n ],\n },\n outputSyncedRows: {\n type: v.boolean().default(false),\n desc: ['Include the rows that would be synced to the client.'],\n },\n joinPlans: {\n type: v.boolean().default(false),\n desc: ['Include join planner diagnostics.'],\n },\n log: {\n ...logOptions,\n level: logLevel.default('error'),\n },\n};\n\ntype QueryPlan =\n | {kind: 'ast'; ast: AST}\n | {kind: 'zql'; text: string}\n | {kind: 'named'; name: string; args: ReadonlyArray<unknown>};\n\n// Route all Zero client log output to stderr so stdout contains only the\n// analyze result. Shell redirection (`2>/dev/null`) can then cleanly silence\n// logs without affecting output.\nconst stderrConsole = new Console({\n stdout: process.stderr,\n stderr: process.stderr,\n});\nconst stderrLogSink: LogSink = {\n log(level, context, ...args) {\n const ctx = context\n ? Object.entries(context).map(([k, v]) =>\n v === undefined ? k : `${k}=${v}`,\n )\n : [];\n stderrConsole[level](...ctx, ...args);\n },\n};\n\n/**\n * Entry point for a user's `cli.ts`. Parses argv, connects to a remote\n * zero-cache by standing up an in-process Zero client (in-memory storage,\n * no subscriptions), calls the inspector's `analyze-query` RPC, and\n * renders the result. Intended to be called as:\n *\n * ```ts\n * import {schema} from './schema.ts';\n * import {runAnalyzeCLI} from '@rocicorp/zero/analyze';\n * await runAnalyzeCLI({schema});\n * ```\n *\n * Exits the process with code 1 on error.\n */\nexport async function runAnalyzeCLI(opts: AnalyzeCLIOptions): Promise<void> {\n const argv = (opts.argv ?? process.argv.slice(2)).map(s =>\n s.replaceAll('\\n', ' '),\n );\n\n const config = parseOptions(options, {\n argv,\n envNamePrefix: ZERO_ENV_VAR_PREFIX,\n description: [\n {\n header: 'analyze-query (remote)',\n content: `Analyze a ZQL query against a remote zero-cache.\n\n Connects to zero-cache's inspector protocol and reports the server-observed\n row scans, SQLite query plans, and timings.`,\n },\n {\n header: 'Examples',\n content: ` node cli.ts --zero-cache-url=https://zero.example.com \\\\\n --admin-password=\"$ZERO_ADMIN_PASSWORD\" \\\\\n --query='issue.related(\"comments\").limit(10)'\n\n node cli.ts --zero-cache-url=http://localhost:4848 \\\\\n --ast='\\\\{\"table\": \"issue\", \"limit\": 5\\\\}'\n\n node cli.ts --zero-cache-url=http://localhost:4848 \\\\\n --query-name=issueList --query-args='[]'`,\n },\n ],\n });\n\n if (!config.zeroCacheURL) {\n colorConsole.error('--zero-cache-url is required. See --help for usage.');\n process.exit(1);\n }\n\n const plan = buildQueryPlan(config);\n\n const handshakeHeaders = resolveHandshakeHeaders(config);\n if (Object.keys(handshakeHeaders).length > 0) {\n installWebSocketHeaderShim(handshakeHeaders);\n }\n\n // zero-client and replicache reference a build-time `TESTING` global that\n // bundlers replace with a boolean literal; under node there's no replacement,\n // so provide a runtime default.\n (globalThis as {TESTING?: boolean}).TESTING ??= false;\n\n const z = new Zero({\n schema: opts.schema,\n server: config.zeroCacheURL,\n auth: config.authToken,\n userID: config.userId,\n kvStore: 'mem',\n logLevel: config.log.level,\n logSink: stderrLogSink,\n });\n\n let result: AnalyzeQueryResult;\n try {\n const authOk = await z.inspector.authenticate(config.adminPassword ?? '');\n if (!authOk) {\n throw new Error(\n 'admin password rejected (or --admin-password is required)',\n );\n }\n\n const rpcOptions = {\n vendedRows: config.outputVendedRows,\n syncedRows: config.outputSyncedRows,\n joinPlans: config.joinPlans,\n };\n\n if (plan.kind === 'ast') {\n result = await z.inspector.analyzeServerAST(plan.ast, rpcOptions);\n } else if (plan.kind === 'named') {\n result = await z.inspector.analyzeNamedQuery(\n plan.name,\n plan.args as ReadonlyArray<never>,\n rpcOptions,\n );\n } else {\n const built = buildZqlQuery(plan.text, createBuilder(opts.schema));\n result = await z.inspector.analyzeQuery(built, rpcOptions);\n }\n } catch (e) {\n colorConsole.error(e instanceof Error ? e.message : String(e));\n await z.close().catch(() => {});\n process.exit(1);\n }\n\n renderResult(result, {\n outputSyncedRows: config.outputSyncedRows,\n outputVendedRows: config.outputVendedRows,\n });\n\n await z.close();\n}\n\nfunction buildQueryPlan(config: {\n ast?: string | undefined;\n query?: string | undefined;\n queryName?: string | undefined;\n queryArgs?: string | undefined;\n}): QueryPlan {\n const selectors = [\n config.ast !== undefined && 'ast',\n config.query !== undefined && 'query',\n config.queryName !== undefined && 'queryName',\n ].filter(Boolean) as string[];\n\n if (selectors.length === 0) {\n colorConsole.error(\n 'Exactly one of --ast / --query / --query-name is required.',\n );\n process.exit(1);\n }\n if (selectors.length > 1) {\n colorConsole.error(\n `Only one of --ast / --query / --query-name may be provided; got: ${selectors.join(', ')}`,\n );\n process.exit(1);\n }\n\n if (config.ast !== undefined) {\n return {kind: 'ast', ast: JSON.parse(config.ast) as AST};\n }\n if (config.query !== undefined) {\n return {kind: 'zql', text: config.query};\n }\n const args = config.queryArgs\n ? (JSON.parse(config.queryArgs) as ReadonlyArray<unknown>)\n : [];\n return {kind: 'named', name: config.queryName as string, args};\n}\n\nfunction buildZqlQuery(\n queryString: string,\n builder: SchemaQuery<Schema>,\n): AnyQuery {\n const f = new Function('builder', `return builder.${queryString};`);\n return f(builder) as AnyQuery;\n}\n\nfunction resolveHandshakeHeaders(config: {\n cookie?: string | undefined;\n headersJson?: string | undefined;\n}): Record<string, string> {\n const headers: Record<string, string> = {};\n if (config.headersJson !== undefined) {\n let parsed: unknown;\n try {\n parsed = JSON.parse(config.headersJson);\n } catch (e) {\n colorConsole.error(\n `--headers-json is not valid JSON: ${e instanceof Error ? e.message : String(e)}`,\n );\n process.exit(1);\n }\n if (\n parsed === null ||\n typeof parsed !== 'object' ||\n Array.isArray(parsed)\n ) {\n colorConsole.error('--headers-json must be a JSON object.');\n process.exit(1);\n }\n for (const [k, val] of Object.entries(parsed)) {\n if (typeof val !== 'string') {\n colorConsole.error(\n `--headers-json values must be strings; got ${typeof val} for \"${k}\".`,\n );\n process.exit(1);\n }\n headers[k] = val;\n }\n }\n if (config.cookie !== undefined) {\n headers.cookie = config.cookie;\n }\n return headers;\n}\n\nfunction installWebSocketHeaderShim(headers: Record<string, string>): void {\n class HeaderInjectingWebSocket extends NodeWebSocket {\n constructor(url: string | URL, protocols?: string | string[]) {\n super(url, protocols, {headers});\n }\n }\n (globalThis as {WebSocket?: unknown}).WebSocket = HeaderInjectingWebSocket;\n}\n\nfunction renderResult(\n result: AnalyzeQueryResult,\n opts: {outputSyncedRows: boolean; outputVendedRows: boolean},\n) {\n if (opts.outputSyncedRows) {\n colorConsole.log(styleText(['blue', 'bold'], '=== Synced Rows: ===\\n'));\n for (const [table, rows] of Object.entries(result.syncedRows ?? {})) {\n colorConsole.log(styleText('bold', table + ':'), rows);\n }\n }\n\n colorConsole.log(styleText(['blue', 'bold'], '=== Query Stats: ===\\n'));\n colorConsole.log(\n styleText('bold', 'total synced rows:'),\n result.syncedRowCount,\n );\n\n const readRowCountsByQuery = result.readRowCountsByQuery ?? {};\n let totalRowsRead = 0;\n for (const table of Object.keys(readRowCountsByQuery).sort()) {\n const counts = readRowCountsByQuery[table];\n for (const n of Object.values(counts)) {\n totalRowsRead += n;\n }\n colorConsole.log(styleText('bold', `${table} vended:`), counts);\n }\n colorConsole.log(\n styleText('bold', 'Rows Read (into JS):'),\n colorRowsConsidered(totalRowsRead),\n );\n const duration = result.elapsed ?? result.end - result.start;\n colorConsole.log(styleText('bold', 'time:'), colorTime(duration), 'ms');\n\n if (opts.outputVendedRows) {\n colorConsole.log(\n styleText(['blue', 'bold'], '=== JS Row Scan Values: ===\\n'),\n );\n for (const [table, rows] of Object.entries(result.readRows ?? {})) {\n colorConsole.log(styleText('bold', `${table}:`), rows);\n }\n }\n\n colorConsole.log(\n styleText(['blue', 'bold'], '\\n=== Rows Scanned (by SQLite): ===\\n'),\n );\n const dbScansByQuery = result.dbScansByQuery ?? {};\n let totalNVisit = 0;\n for (const [table, queries] of Object.entries(dbScansByQuery)) {\n colorConsole.log(styleText('bold', `${table}:`), queries);\n for (const count of Object.values(queries)) {\n totalNVisit += count;\n }\n }\n colorConsole.log(\n styleText('bold', 'total rows scanned:'),\n colorRowsConsidered(totalNVisit),\n );\n\n colorConsole.log(styleText(['blue', 'bold'], '\\n\\n=== Query Plans: ===\\n'));\n const plans = result.sqlitePlans ?? {};\n for (const [query, plan] of Object.entries(plans)) {\n colorConsole.log(styleText('bold', 'query'), query);\n colorConsole.log(plan.map((row, i) => colorPlanRow(row, i)).join('\\n'));\n colorConsole.log('\\n');\n }\n\n if (result.warnings.length > 0) {\n colorConsole.log(styleText(['yellow', 'bold'], '=== Warnings: ===\\n'));\n for (const w of result.warnings) {\n colorConsole.log(styleText('yellow', w));\n }\n }\n\n if (result.joinPlans && result.joinPlans.length > 0) {\n colorConsole.log(styleText(['blue', 'bold'], '\\n=== Join Plans: ===\\n'));\n colorConsole.log(formatPlannerEvents(result.joinPlans));\n }\n}\n\nfunction colorTime(duration: number) {\n if (duration < 100) {\n return styleText('green', duration.toFixed(2) + 'ms');\n } else if (duration < 1000) {\n return styleText('yellow', duration.toFixed(2) + 'ms');\n }\n return styleText('red', duration.toFixed(2) + 'ms');\n}\n\nfunction colorRowsConsidered(n: number) {\n if (n < 1000) {\n return styleText('green', n.toString());\n } else if (n < 10000) {\n return styleText('yellow', n.toString());\n }\n return styleText('red', n.toString());\n}\n\nfunction colorPlanRow(row: string, i: number) {\n if (row.includes('SCAN')) {\n if (i === 0) {\n return styleText('yellow', row);\n }\n return styleText('red', row);\n }\n return styleText('green', row);\n}\n"],"mappings":";;;;;;;;;;;;;AA0BA,IAAM,UAAU;CACd,cAAc;EACZ,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,oDACA,wEACF;CACF;CACA,eAAe;EACb,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,kCACA,uEACF;CACF;CACA,WAAW;EACT,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,oCACA,8DACF;CACF;CACA,QAAQ;EACN,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM;GACJ;GACA;GACA;GACA;EACF;CACF;CACA,aAAa;EACX,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM;GACJ;GACA;GACA;EACF;CACF;CACA,QAAQ;EACN,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,4CACA,8BACF;CACF;CACA,KAAK;EACH,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,gFACA,oFACF;CACF;CACA,OAAO;EACL,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,0EACA,yDACF;CACF;CACA,WAAW;EACT,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,qDACA,uEACF;CACF;CACA,WAAW;EACT,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,qEACF;CACF;CACA,kBAAkB;EAChB,MAAM,eAAE,QAAQ,EAAE,QAAQ,KAAK;EAC/B,MAAM,CACJ,gEACA,iCACF;CACF;CACA,kBAAkB;EAChB,MAAM,eAAE,QAAQ,EAAE,QAAQ,KAAK;EAC/B,MAAM,CAAC,sDAAsD;CAC/D;CACA,WAAW;EACT,MAAM,eAAE,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CACA,OAAO,UAAU,SAAS,GAAG;AAC/B"}
1
+ {"version":3,"file":"analyze-cli.js","names":[],"sources":["../../../../analyze-query/src/analyze-cli.ts"],"sourcesContent":["import '../../shared/src/dotenv.ts';\n\nimport {Console} from 'node:console';\nimport {styleText} from 'node:util';\nimport type {LogSink} from '@rocicorp/logger';\nimport {WebSocket as NodeWebSocket} from 'ws';\nimport {logLevel, logOptions} from '../../otel/src/log-options.ts';\nimport {colorConsole} from '../../shared/src/logging.ts';\nimport {parseOptions} from '../../shared/src/options.ts';\nimport * as v from '../../shared/src/valita.ts';\nimport {ZERO_ENV_VAR_PREFIX} from '../../zero-cache/src/config/zero-config.ts';\nimport {Zero} from '../../zero-client/src/client/zero.ts';\nimport type {AnalyzeQueryResult} from '../../zero-protocol/src/analyze-query-result.ts';\nimport type {AST} from '../../zero-protocol/src/ast.ts';\nimport type {Schema} from '../../zero-types/src/schema.ts';\nimport {formatPlannerEvents} from '../../zql/src/planner/planner-debug.ts';\nimport {createBuilder} from '../../zql/src/query/create-builder.ts';\nimport type {AnyQuery} from '../../zql/src/query/query.ts';\nimport type {SchemaQuery} from '../../zql/src/query/schema-query.ts';\n\nexport type AnalyzeCLIOptions = {\n schema: Schema;\n /** Defaults to `process.argv.slice(2)`. */\n argv?: readonly string[] | undefined;\n};\n\nconst options = {\n zeroCacheURL: {\n type: v.string().optional(),\n desc: [\n 'URL of the remote zero-cache to analyze against.',\n 'Accepts http(s):// or ws(s):// (ws(s) is the transport actually used).',\n ],\n },\n adminPassword: {\n type: v.string().optional(),\n desc: [\n 'Admin password for zero-cache.',\n 'Required when the server is configured with one; ignored in dev mode.',\n ],\n },\n authToken: {\n type: v.string().optional(),\n desc: [\n 'Raw JWT forwarded to zero-cache.',\n 'Used server-side to fill permission variables for the query.',\n ],\n },\n cookie: {\n type: v.string().optional(),\n desc: [\n 'Cookie header value sent on the WebSocket upgrade request,',\n 'e.g. `session=abc; foo=bar`. Use this when zero-cache is behind',\n 'a proxy that resolves auth via cookies. Merged with --headers-json',\n '(--cookie wins on conflict).',\n ],\n },\n headersJson: {\n type: v.string().optional(),\n desc: [\n 'JSON object of arbitrary headers to send on the WebSocket upgrade',\n 'request, e.g. `{\"x-api-key\":\"...\"}`. Escape hatch for exotic auth',\n 'schemes; prefer --auth-token or --cookie when possible.',\n ],\n },\n userId: {\n type: v.string().optional(),\n desc: [\n 'Optional userID to report to zero-cache.',\n 'Omit for anonymous analysis.',\n ],\n },\n ast: {\n type: v.string().optional(),\n desc: [\n 'JSON-encoded AST. Exactly one of --ast / --query / --query-name is required.',\n 'The AST is sent to the server verbatim — provide it in server (post-mapping) form.',\n ],\n },\n query: {\n type: v.string().optional(),\n desc: [\n 'ZQL query in chain form, e.g. `issue.related(\"comments\").limit(10)`.',\n 'Evaluated against the schema you pass to runAnalyzeCLI.',\n ],\n },\n queryName: {\n type: v.string().optional(),\n desc: [\n 'Name of a server-registered custom (named) query.',\n 'The server resolves the name + args via its registered query handler.',\n ],\n },\n queryArgs: {\n type: v.string().optional(),\n desc: [\n 'JSON-encoded array of arguments for --query-name. Defaults to `[]`.',\n ],\n },\n outputVendedRows: {\n type: v.boolean().default(false),\n desc: [\n 'Include the rows read from the replica to execute the query.',\n 'Each row appears once per read.',\n ],\n },\n outputSyncedRows: {\n type: v.boolean().default(false),\n desc: ['Include the rows that would be synced to the client.'],\n },\n joinPlans: {\n type: v.boolean().default(false),\n desc: ['Include join planner diagnostics.'],\n },\n log: {\n ...logOptions,\n level: logLevel.default('error'),\n },\n};\n\ntype QueryPlan =\n | {kind: 'ast'; ast: AST}\n | {kind: 'zql'; text: string}\n | {kind: 'named'; name: string; args: ReadonlyArray<unknown>};\n\n// Route all Zero client log output to stderr so stdout contains only the\n// analyze result. Shell redirection (`2>/dev/null`) can then cleanly silence\n// logs without affecting output.\nconst stderrConsole = new Console({\n stdout: process.stderr,\n stderr: process.stderr,\n});\nconst stderrLogSink: LogSink = {\n log(level, context, ...args) {\n const ctx = context\n ? Object.entries(context).map(([k, v]) =>\n v === undefined ? k : `${k}=${v}`,\n )\n : [];\n stderrConsole[level](...ctx, ...args);\n },\n};\n\n/**\n * Entry point for a user's `cli.ts`. Parses argv, connects to a remote\n * zero-cache by standing up an in-process Zero client (in-memory storage,\n * no subscriptions), calls the inspector's `analyze-query` RPC, and\n * renders the result. Intended to be called as:\n *\n * ```ts\n * import {schema} from './schema.ts';\n * import {runAnalyzeCLI} from '@rocicorp/zero/analyze';\n * await runAnalyzeCLI({schema});\n * ```\n *\n * Exits the process with code 1 on error.\n */\nexport async function runAnalyzeCLI(opts: AnalyzeCLIOptions): Promise<void> {\n const argv = (opts.argv ?? process.argv.slice(2)).map(s =>\n s.replaceAll('\\n', ' '),\n );\n\n const config = parseOptions(options, {\n argv,\n envNamePrefix: ZERO_ENV_VAR_PREFIX,\n description: [\n {\n header: 'analyze-query (remote)',\n content: `Analyze a ZQL query against a remote zero-cache.\n\n Connects to zero-cache's inspector protocol and reports the server-observed\n row scans, SQLite query plans, and timings.`,\n },\n {\n header: 'Examples',\n content: ` node cli.ts --zero-cache-url=https://zero.example.com \\\\\n --admin-password=\"$ZERO_ADMIN_PASSWORD\" \\\\\n --query='issue.related(\"comments\").limit(10)'\n\n node cli.ts --zero-cache-url=http://localhost:4848 \\\\\n --ast='\\\\{\"table\": \"issue\", \"limit\": 5\\\\}'\n\n node cli.ts --zero-cache-url=http://localhost:4848 \\\\\n --query-name=issueList --query-args='[]'`,\n },\n ],\n });\n\n if (!config.zeroCacheURL) {\n colorConsole.error('--zero-cache-url is required. See --help for usage.');\n process.exit(1);\n }\n\n const plan = buildQueryPlan(config);\n\n const handshakeHeaders = resolveHandshakeHeaders(config);\n if (Object.keys(handshakeHeaders).length > 0) {\n installWebSocketHeaderShim(handshakeHeaders);\n }\n\n // zero-client and replicache reference a build-time `TESTING` global that\n // bundlers replace with a boolean literal; under node there's no replacement,\n // so provide a runtime default.\n (globalThis as {TESTING?: boolean}).TESTING ??= false;\n\n const z = new Zero({\n schema: opts.schema,\n server: config.zeroCacheURL,\n auth: config.authToken,\n userID: config.userId,\n kvStore: 'mem',\n logLevel: config.log.level,\n logSink: stderrLogSink,\n });\n\n let result: AnalyzeQueryResult;\n try {\n const authOk = await z.inspector.authenticate(config.adminPassword ?? '');\n if (!authOk) {\n throw new Error(\n 'admin password rejected (or --admin-password is required)',\n );\n }\n\n const rpcOptions = {\n vendedRows: config.outputVendedRows,\n syncedRows: config.outputSyncedRows,\n joinPlans: config.joinPlans,\n };\n\n if (plan.kind === 'ast') {\n result = await z.inspector.analyzeServerAST(plan.ast, rpcOptions);\n } else if (plan.kind === 'named') {\n result = await z.inspector.analyzeNamedQuery(\n plan.name,\n plan.args as ReadonlyArray<never>,\n rpcOptions,\n );\n } else {\n const built = buildZqlQuery(plan.text, createBuilder(opts.schema));\n result = await z.inspector.analyzeQuery(built, rpcOptions);\n }\n } catch (e) {\n colorConsole.error(e instanceof Error ? e.message : String(e));\n await z.close().catch(() => {});\n process.exit(1);\n }\n\n renderResult(result, {\n outputSyncedRows: config.outputSyncedRows,\n outputVendedRows: config.outputVendedRows,\n });\n\n await z.close();\n}\n\nfunction buildQueryPlan(config: {\n ast?: string | undefined;\n query?: string | undefined;\n queryName?: string | undefined;\n queryArgs?: string | undefined;\n}): QueryPlan {\n const selectors = [\n config.ast !== undefined && 'ast',\n config.query !== undefined && 'query',\n config.queryName !== undefined && 'queryName',\n ].filter(Boolean) as string[];\n\n if (selectors.length === 0) {\n colorConsole.error(\n 'Exactly one of --ast / --query / --query-name is required.',\n );\n process.exit(1);\n }\n if (selectors.length > 1) {\n colorConsole.error(\n `Only one of --ast / --query / --query-name may be provided; got: ${selectors.join(', ')}`,\n );\n process.exit(1);\n }\n\n if (config.ast !== undefined) {\n return {kind: 'ast', ast: JSON.parse(config.ast) as AST};\n }\n if (config.query !== undefined) {\n return {kind: 'zql', text: config.query};\n }\n const args = config.queryArgs\n ? (JSON.parse(config.queryArgs) as ReadonlyArray<unknown>)\n : [];\n return {kind: 'named', name: config.queryName as string, args};\n}\n\nfunction buildZqlQuery(\n queryString: string,\n builder: SchemaQuery<Schema>,\n): AnyQuery {\n const f = new Function('builder', `return builder.${queryString};`);\n return f(builder) as AnyQuery;\n}\n\nfunction resolveHandshakeHeaders(config: {\n cookie?: string | undefined;\n headersJson?: string | undefined;\n}): Record<string, string> {\n const headers: Record<string, string> = {};\n if (config.headersJson !== undefined) {\n let parsed: unknown;\n try {\n parsed = JSON.parse(config.headersJson);\n } catch (e) {\n colorConsole.error(\n `--headers-json is not valid JSON: ${e instanceof Error ? e.message : String(e)}`,\n );\n process.exit(1);\n }\n if (\n parsed === null ||\n typeof parsed !== 'object' ||\n Array.isArray(parsed)\n ) {\n colorConsole.error('--headers-json must be a JSON object.');\n process.exit(1);\n }\n for (const [k, val] of Object.entries(parsed)) {\n if (typeof val !== 'string') {\n colorConsole.error(\n `--headers-json values must be strings; got ${typeof val} for \"${k}\".`,\n );\n process.exit(1);\n }\n headers[k] = val;\n }\n }\n if (config.cookie !== undefined) {\n headers.cookie = config.cookie;\n }\n return headers;\n}\n\nfunction installWebSocketHeaderShim(headers: Record<string, string>): void {\n class HeaderInjectingWebSocket extends NodeWebSocket {\n constructor(url: string | URL, protocols?: string | string[]) {\n super(url, protocols, {headers});\n }\n }\n (globalThis as {WebSocket?: unknown}).WebSocket = HeaderInjectingWebSocket;\n}\n\nfunction renderResult(\n result: AnalyzeQueryResult,\n opts: {outputSyncedRows: boolean; outputVendedRows: boolean},\n) {\n if (opts.outputSyncedRows) {\n colorConsole.log(styleText(['blue', 'bold'], '=== Synced Rows: ===\\n'));\n for (const [table, rows] of Object.entries(result.syncedRows ?? {})) {\n colorConsole.log(styleText('bold', table + ':'), rows);\n }\n }\n\n colorConsole.log(styleText(['blue', 'bold'], '=== Query Stats: ===\\n'));\n colorConsole.log(\n styleText('bold', 'total synced rows:'),\n result.syncedRowCount,\n );\n\n const readRowCountsByQuery = result.readRowCountsByQuery ?? {};\n let totalRowsRead = 0;\n for (const table of Object.keys(readRowCountsByQuery).sort()) {\n const counts = readRowCountsByQuery[table];\n for (const n of Object.values(counts)) {\n totalRowsRead += n;\n }\n colorConsole.log(styleText('bold', `${table} vended:`), counts);\n }\n colorConsole.log(\n styleText('bold', 'Rows Read (into JS):'),\n colorRowsConsidered(totalRowsRead),\n );\n const duration = result.elapsed ?? result.end - result.start;\n colorConsole.log(styleText('bold', 'time:'), colorTime(duration), 'ms');\n\n if (opts.outputVendedRows) {\n colorConsole.log(\n styleText(['blue', 'bold'], '=== JS Row Scan Values: ===\\n'),\n );\n for (const [table, rows] of Object.entries(result.readRows ?? {})) {\n colorConsole.log(styleText('bold', `${table}:`), rows);\n }\n }\n\n colorConsole.log(\n styleText(['blue', 'bold'], '\\n=== Rows Scanned (by SQLite): ===\\n'),\n );\n const dbScansByQuery = result.dbScansByQuery ?? {};\n let totalNVisit = 0;\n for (const [table, queries] of Object.entries(dbScansByQuery)) {\n colorConsole.log(styleText('bold', `${table}:`), queries);\n for (const count of Object.values(queries)) {\n totalNVisit += count;\n }\n }\n colorConsole.log(\n styleText('bold', 'total rows scanned:'),\n colorRowsConsidered(totalNVisit),\n );\n\n colorConsole.log(styleText(['blue', 'bold'], '\\n\\n=== Query Plans: ===\\n'));\n const plans = result.sqlitePlans ?? {};\n for (const [query, plan] of Object.entries(plans)) {\n colorConsole.log(styleText('bold', 'query'), query);\n colorConsole.log(plan.map((row, i) => colorPlanRow(row, i)).join('\\n'));\n colorConsole.log('\\n');\n }\n\n if (result.warnings.length > 0) {\n colorConsole.log(styleText(['yellow', 'bold'], '=== Warnings: ===\\n'));\n for (const w of result.warnings) {\n colorConsole.log(styleText('yellow', w));\n }\n }\n\n if (result.joinPlans && result.joinPlans.length > 0) {\n colorConsole.log(styleText(['blue', 'bold'], '\\n=== Join Plans: ===\\n'));\n colorConsole.log(formatPlannerEvents(result.joinPlans));\n }\n}\n\nfunction colorTime(duration: number) {\n if (duration < 100) {\n return styleText('green', duration.toFixed(2) + 'ms');\n } else if (duration < 1000) {\n return styleText('yellow', duration.toFixed(2) + 'ms');\n }\n return styleText('red', duration.toFixed(2) + 'ms');\n}\n\nfunction colorRowsConsidered(n: number) {\n if (n < 1000) {\n return styleText('green', n.toString());\n } else if (n < 10000) {\n return styleText('yellow', n.toString());\n }\n return styleText('red', n.toString());\n}\n\nfunction colorPlanRow(row: string, i: number) {\n if (row.includes('SCAN')) {\n if (i === 0) {\n return styleText('yellow', row);\n }\n return styleText('red', row);\n }\n return styleText('green', row);\n}\n"],"mappings":";;;;;;;;;;;;;AA0BA,IAAM,UAAU;CACd,cAAc;EACZ,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,oDACA,wEACF;CACF;CACA,eAAe;EACb,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,kCACA,uEACF;CACF;CACA,WAAW;EACT,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,oCACA,8DACF;CACF;CACA,QAAQ;EACN,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM;GACJ;GACA;GACA;GACA;EACF;CACF;CACA,aAAa;EACX,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM;GACJ;GACA;GACA;EACF;CACF;CACA,QAAQ;EACN,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,4CACA,8BACF;CACF;CACA,KAAK;EACH,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,gFACA,oFACF;CACF;CACA,OAAO;EACL,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,0EACA,yDACF;CACF;CACA,WAAW;EACT,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,qDACA,uEACF;CACF;CACA,WAAW;EACT,MAAM,eAAE,OAAO,EAAE,SAAS;EAC1B,MAAM,CACJ,qEACF;CACF;CACA,kBAAkB;EAChB,MAAM,eAAE,QAAQ,EAAE,QAAQ,KA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;AACtC;AAEA,SAAS,aAAa,KAAa,GAAW;CAC5C,IAAI,IAAI,SAAS,MAAM,GAAG;EACxB,IAAI,MAAM,GACR,OAAO,UAAU,UAAU,GAAG;EAEhC,OAAO,UAAU,OAAO,GAAG;CAC7B;CACA,OAAO,UAAU,SAAS,GAAG;AAC/B"}
@@ -1 +1 @@
1
- {"version":3,"file":"gc.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/dag/gc.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAAC,KAAK,IAAI,EAAY,MAAM,YAAY,CAAC;AAEhD,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,IAAI,GAAG,SAAS,CAAC;IACtB,GAAG,EAAE,IAAI,GAAG,SAAS,CAAC;CACvB,CAAC;AAIF,MAAM,WAAW,uBAAuB;IACtC,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC9D,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,SAAS,IAAI,EAAE,GAAG,SAAS,CAAC,CAAC;IACnE;;;;;;;;;;;;;;;;;;OAkBG;IACH,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;CAC1C;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,WAAW,EAAE,QAAQ,CAAC,UAAU,CAAC,EACjC,SAAS,EAAE,WAAW,CAAC,IAAI,CAAC,EAC5B,QAAQ,EAAE,uBAAuB,GAChC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAE5B"}
1
+ {"version":3,"file":"gc.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/dag/gc.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAAC,KAAK,IAAI,EAAY,MAAM,YAAY,CAAC;AAGhD,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE,IAAI,GAAG,SAAS,CAAC;IACtB,GAAG,EAAE,IAAI,GAAG,SAAS,CAAC;CACvB,CAAC;AAIF,MAAM,WAAW,uBAAuB;IACtC,WAAW,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAC9D,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,YAAY,CAAC,SAAS,IAAI,EAAE,GAAG,SAAS,CAAC,CAAC;IACnE;;;;;;;;;;;;;;;;;;OAkBG;IACH,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;CAC1C;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,WAAW,EAAE,QAAQ,CAAC,UAAU,CAAC,EACjC,SAAS,EAAE,WAAW,CAAC,IAAI,CAAC,EAC5B,QAAQ,EAAE,uBAAuB,GAChC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAE5B"}
@@ -1,6 +1,7 @@
1
1
  import { assert, assertNumber } from "../../../shared/src/asserts.js";
2
2
  import { isProd } from "../../../shared/src/config.js";
3
3
  import "../config.js";
4
+ import { InvalidRefCountError } from "./invalid-ref-count-error.js";
4
5
  import { emptyHash } from "../hash.js";
5
6
  //#region ../replicache/src/dag/gc.ts
6
7
  /**
@@ -58,7 +59,7 @@ var RefCountUpdates = class {
58
59
  } while (refCountsUpdated);
59
60
  }
60
61
  for (const o of this.#oldHeads) await this.#changeRefCount(o, -1);
61
- if (!isProd) for (const [hash, update] of this.#refCountUpdates) assert(update >= 0, `ref count update must be non-negative. ${hash}:${update}`);
62
+ for (const [hash, update] of this.#refCountUpdates) if (!(update >= 0)) throw new InvalidRefCountError(hash, update);
62
63
  return this.#refCountUpdates;
63
64
  }
64
65
  async #changeRefCount(hash, delta) {
@@ -1 +1 @@
1
- {"version":3,"file":"gc.js","names":["#newHeads","#oldHeads","#putChunks","#delegate","#refsCounted","#refCountUpdates","#loadedRefCountPromises","#isLazyDelegate","#changeRefCount","#ensureRefCountLoaded","#updateRefsCounts","#updateRefCount"],"sources":["../../../../../replicache/src/dag/gc.ts"],"sourcesContent":["import {assert, assertNumber} from '../../../shared/src/asserts.ts';\nimport type {MaybePromise} from '../../../shared/src/types.ts';\nimport {skipGCAsserts} from '../config.ts';\nimport {type Hash, emptyHash} from '../hash.ts';\n\nexport type HeadChange = {\n new: Hash | undefined;\n old: Hash | undefined;\n};\n\ntype LoadedRefCountPromises = Map<Hash, Promise<number>>;\n\nexport interface RefCountUpdatesDelegate {\n getRefCount: (hash: Hash) => MaybePromise<number | undefined>;\n getRefs: (hash: Hash) => MaybePromise<readonly Hash[] | undefined>;\n /**\n * Should be implemented if the store lazily loads refs, returning whether\n * or not the chunks refs have already been counted (i.e. are reflected\n * in `getRefCount`).\n *\n * If defined then:\n * - `getRefs` should return undefined for refs that have not been loaded,\n * but should never return undefined for hashes in `putChunks`.\n * - it is assumed that chunks in `putChunks` may have been reachable before\n * the write, but may not have been counted. This method is used to\n * determine if they have been counted or not. If they have not been\n * counted, and are reachable with the write applied, the returned\n * ref count updates will include updates for counting them.\n *\n * If undefined then:\n * - `getRefs` should never return undefined\n * - it is assumed that the refs of any chunks which were reachable before\n * the write are already counted\n */\n areRefsCounted?: (hash: Hash) => boolean;\n}\n\n/**\n * Computes how ref counts should be updated when a dag write is committed.\n * Does not modify the dag store.\n * @param headChanges Heads that were changed by the dag write.\n * @param putChunks Chunks that were put by the dag write.\n * @param delegate Delegate used for getting ref information from the dag store.\n * @returns Map from chunk Hash to changed ref counts. Chunks with a new ref\n * count of 0 should be deleted. All hashes in `putChunks` will have an entry\n * (which will be zero if the newly put chunk is not reachable from any head).\n */\nexport function computeRefCountUpdates(\n headChanges: Iterable<HeadChange>,\n putChunks: ReadonlySet<Hash>,\n delegate: RefCountUpdatesDelegate,\n): Promise<Map<Hash, number>> {\n return new RefCountUpdates(headChanges, putChunks, delegate).compute();\n}\n\nclass RefCountUpdates {\n readonly #newHeads: Hash[];\n readonly #oldHeads: Hash[];\n readonly #putChunks: ReadonlySet<Hash>;\n readonly #delegate: RefCountUpdatesDelegate;\n readonly #refsCounted: Set<Hash> | null;\n readonly #refCountUpdates: Map<Hash, number>;\n readonly #loadedRefCountPromises: LoadedRefCountPromises;\n readonly #isLazyDelegate: boolean;\n\n constructor(\n headChanges: Iterable<HeadChange>,\n putChunks: ReadonlySet<Hash>,\n delegate: RefCountUpdatesDelegate,\n ) {\n const newHeads: Hash[] = [];\n const oldHeads: Hash[] = [];\n for (const changedHead of headChanges) {\n if (changedHead.old !== changedHead.new) {\n changedHead.old && oldHeads.push(changedHead.old);\n changedHead.new && newHeads.push(changedHead.new);\n }\n }\n this.#newHeads = newHeads;\n this.#oldHeads = oldHeads;\n this.#putChunks = putChunks;\n this.#delegate = delegate;\n this.#refCountUpdates = new Map();\n // This map is used to ensure we do not load the ref count key more than once.\n // Once it is loaded we only operate on a cache of the ref counts.\n this.#loadedRefCountPromises = new Map();\n this.#isLazyDelegate = delegate.areRefsCounted !== undefined;\n this.#refsCounted = this.#isLazyDelegate ? new Set() : null;\n }\n\n async compute(): Promise<Map<Hash, number>> {\n for (const n of this.#newHeads) {\n await this.#changeRefCount(n, 1);\n }\n\n // Now go through the put chunks to ensure each has an entry in\n // refCountUpdates (zero for new chunks which are not reachable from\n // newHeads).\n await Promise.all(\n Array.from(this.#putChunks.values(), hash =>\n this.#ensureRefCountLoaded(hash),\n ),\n );\n\n if (this.#isLazyDelegate) {\n assert(\n this.#delegate.areRefsCounted,\n 'Expected delegate.areRefsCounted to be defined',\n );\n assert(this.#refsCounted, 'Expected refsCounted to be defined');\n let refCountsUpdated;\n do {\n refCountsUpdated = false;\n for (const hash of this.#putChunks.values()) {\n if (\n !this.#delegate.areRefsCounted(hash) &&\n !this.#refsCounted.has(hash) &&\n this.#refCountUpdates.get(hash) !== 0\n ) {\n await this.#updateRefsCounts(hash, 1);\n refCountsUpdated = true;\n break;\n }\n }\n } while (refCountsUpdated);\n }\n\n for (const o of this.#oldHeads) {\n await this.#changeRefCount(o, -1);\n }\n\n if (!skipGCAsserts) {\n for (const [hash, update] of this.#refCountUpdates) {\n assert(\n update >= 0,\n `ref count update must be non-negative. ${hash}:${update}`,\n );\n }\n }\n\n return this.#refCountUpdates;\n }\n\n async #changeRefCount(hash: Hash, delta: number): Promise<void> {\n // First make sure that we have the ref count in the cache. This is async\n // because it might need to load the ref count from the store (via the delegate).\n //\n // Once we have loaded the ref count all the updates to it are sync to\n // prevent race conditions.\n await this.#ensureRefCountLoaded(hash);\n if (this.#updateRefCount(hash, delta)) {\n await this.#updateRefsCounts(hash, delta);\n }\n }\n\n async #updateRefsCounts(hash: Hash, delta: number) {\n if (hash === emptyHash) {\n return;\n }\n const refs = await this.#delegate.getRefs(hash);\n if (!skipGCAsserts) {\n assert(\n refs || (this.#isLazyDelegate && !this.#putChunks.has(hash)),\n 'refs must be defined',\n );\n }\n\n if (refs !== undefined) {\n this.#refsCounted?.add(hash);\n const ps = refs.map(ref => this.#changeRefCount(ref, delta));\n await Promise.all(ps);\n }\n }\n\n #ensureRefCountLoaded(hash: Hash): Promise<number> {\n // Only get the ref count once.\n let p = this.#loadedRefCountPromises.get(hash);\n if (p === undefined) {\n p = (async () => {\n const value = (await this.#delegate.getRefCount(hash)) || 0;\n this.#refCountUpdates.set(hash, value);\n return value;\n })();\n this.#loadedRefCountPromises.set(hash, p);\n }\n return p;\n }\n\n #updateRefCount(hash: Hash, delta: number): boolean {\n const oldCount = this.#refCountUpdates.get(hash);\n assertNumber(oldCount);\n this.#refCountUpdates.set(hash, oldCount + delta);\n return (oldCount === 0 && delta === 1) || (oldCount === 1 && delta === -1);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA+CA,SAAgB,uBACd,aACA,WACA,UAC4B;CAC5B,OAAO,IAA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putChunks","#delegate","#refsCounted","#refCountUpdates","#loadedRefCountPromises","#isLazyDelegate","#changeRefCount","#ensureRefCountLoaded","#updateRefsCounts","#updateRefCount"],"sources":["../../../../../replicache/src/dag/gc.ts"],"sourcesContent":["import {assert, assertNumber} from '../../../shared/src/asserts.ts';\nimport type {MaybePromise} from '../../../shared/src/types.ts';\nimport {skipGCAsserts} from '../config.ts';\nimport {type Hash, emptyHash} from '../hash.ts';\nimport {InvalidRefCountError} from './invalid-ref-count-error.ts';\n\nexport type HeadChange = {\n new: Hash | undefined;\n old: Hash | undefined;\n};\n\ntype LoadedRefCountPromises = Map<Hash, Promise<number>>;\n\nexport interface RefCountUpdatesDelegate {\n getRefCount: (hash: Hash) => MaybePromise<number | undefined>;\n getRefs: (hash: Hash) => MaybePromise<readonly Hash[] | undefined>;\n /**\n * Should be implemented if the store lazily loads refs, returning whether\n * or not the chunks refs have already been counted (i.e. are reflected\n * in `getRefCount`).\n *\n * If defined then:\n * - `getRefs` should return undefined for refs that have not been loaded,\n * but should never return undefined for hashes in `putChunks`.\n * - it is assumed that chunks in `putChunks` may have been reachable before\n * the write, but may not have been counted. This method is used to\n * determine if they have been counted or not. If they have not been\n * counted, and are reachable with the write applied, the returned\n * ref count updates will include updates for counting them.\n *\n * If undefined then:\n * - `getRefs` should never return undefined\n * - it is assumed that the refs of any chunks which were reachable before\n * the write are already counted\n */\n areRefsCounted?: (hash: Hash) => boolean;\n}\n\n/**\n * Computes how ref counts should be updated when a dag write is committed.\n * Does not modify the dag store.\n * @param headChanges Heads that were changed by the dag write.\n * @param putChunks Chunks that were put by the dag write.\n * @param delegate Delegate used for getting ref information from the dag store.\n * @returns Map from chunk Hash to changed ref counts. Chunks with a new ref\n * count of 0 should be deleted. All hashes in `putChunks` will have an entry\n * (which will be zero if the newly put chunk is not reachable from any head).\n */\nexport function computeRefCountUpdates(\n headChanges: Iterable<HeadChange>,\n putChunks: ReadonlySet<Hash>,\n delegate: RefCountUpdatesDelegate,\n): Promise<Map<Hash, number>> {\n return new RefCountUpdates(headChanges, putChunks, delegate).compute();\n}\n\nclass RefCountUpdates {\n readonly #newHeads: Hash[];\n readonly #oldHeads: Hash[];\n readonly #putChunks: ReadonlySet<Hash>;\n readonly #delegate: RefCountUpdatesDelegate;\n readonly #refsCounted: Set<Hash> | null;\n readonly #refCountUpdates: Map<Hash, number>;\n readonly #loadedRefCountPromises: LoadedRefCountPromises;\n readonly #isLazyDelegate: boolean;\n\n constructor(\n headChanges: Iterable<HeadChange>,\n putChunks: ReadonlySet<Hash>,\n delegate: RefCountUpdatesDelegate,\n ) {\n const newHeads: Hash[] = [];\n const oldHeads: Hash[] = [];\n for (const changedHead of headChanges) {\n if (changedHead.old !== changedHead.new) {\n changedHead.old && oldHeads.push(changedHead.old);\n changedHead.new && newHeads.push(changedHead.new);\n }\n }\n this.#newHeads = newHeads;\n this.#oldHeads = oldHeads;\n this.#putChunks = putChunks;\n this.#delegate = delegate;\n this.#refCountUpdates = new Map();\n // This map is used to ensure we do not load the ref count key more than once.\n // Once it is loaded we only operate on a cache of the ref counts.\n this.#loadedRefCountPromises = new Map();\n this.#isLazyDelegate = delegate.areRefsCounted !== undefined;\n this.#refsCounted = this.#isLazyDelegate ? new Set() : null;\n }\n\n async compute(): Promise<Map<Hash, number>> {\n for (const n of this.#newHeads) {\n await this.#changeRefCount(n, 1);\n }\n\n // Now go through the put chunks to ensure each has an entry in\n // refCountUpdates (zero for new chunks which are not reachable from\n // newHeads).\n await Promise.all(\n Array.from(this.#putChunks.values(), hash =>\n this.#ensureRefCountLoaded(hash),\n ),\n );\n\n if (this.#isLazyDelegate) {\n assert(\n this.#delegate.areRefsCounted,\n 'Expected delegate.areRefsCounted to be defined',\n );\n assert(this.#refsCounted, 'Expected refsCounted to be defined');\n let refCountsUpdated;\n do {\n refCountsUpdated = false;\n for (const hash of this.#putChunks.values()) {\n if (\n !this.#delegate.areRefsCounted(hash) &&\n !this.#refsCounted.has(hash) &&\n this.#refCountUpdates.get(hash) !== 0\n ) {\n await this.#updateRefsCounts(hash, 1);\n refCountsUpdated = true;\n break;\n }\n }\n } while (refCountsUpdated);\n }\n\n for (const o of this.#oldHeads) {\n await this.#changeRefCount(o, -1);\n }\n\n // A negative count means a chunk that is still referenced had no (or too\n // low a) ref count in the store, so the store is already corrupt. Always\n // check this, even in production: writing the negative count would only\n // spread the corruption, and the typed error lets the store owner recover.\n // Written as `!(update >= 0)` so that NaN is rejected as well.\n for (const [hash, update] of this.#refCountUpdates) {\n if (!(update >= 0)) {\n throw new InvalidRefCountError(hash, update);\n }\n }\n\n return this.#refCountUpdates;\n }\n\n async #changeRefCount(hash: Hash, delta: number): Promise<void> {\n // First make sure that we have the ref count in the cache. This is async\n // because it might need to load the ref count from the store (via the delegate).\n //\n // Once we have loaded the ref count all the updates to it are sync to\n // prevent race conditions.\n await this.#ensureRefCountLoaded(hash);\n if (this.#updateRefCount(hash, delta)) {\n await this.#updateRefsCounts(hash, delta);\n }\n }\n\n async #updateRefsCounts(hash: Hash, delta: number) {\n if (hash === emptyHash) {\n return;\n }\n const refs = await this.#delegate.getRefs(hash);\n if (!skipGCAsserts) {\n assert(\n refs || (this.#isLazyDelegate && !this.#putChunks.has(hash)),\n 'refs must be defined',\n );\n }\n\n if (refs !== undefined) {\n this.#refsCounted?.add(hash);\n const ps = refs.map(ref => this.#changeRefCount(ref, delta));\n await Promise.all(ps);\n }\n }\n\n #ensureRefCountLoaded(hash: Hash): Promise<number> {\n // Only get the ref count once.\n let p = this.#loadedRefCountPromises.get(hash);\n if (p === undefined) {\n p = (async () => {\n const value = (await this.#delegate.getRefCount(hash)) || 0;\n this.#refCountUpdates.set(hash, value);\n return value;\n })();\n this.#loadedRefCountPromises.set(hash, p);\n }\n return p;\n }\n\n #updateRefCount(hash: Hash, delta: number): boolean {\n const oldCount = this.#refCountUpdates.get(hash);\n assertNumber(oldCount);\n this.#refCountUpdates.set(hash, oldCount + delta);\n return (oldCount === 0 && delta === 1) || (oldCount === 1 && delta === -1);\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UAAU,KAAO,aAAa,KAAK,UAAU;CACzE;AACF"}
@@ -0,0 +1,12 @@
1
+ import type { Hash } from '../hash.ts';
2
+ /**
3
+ * Thrown when a ref count read from the store is not a valid Uint16. This
4
+ * means the store is corrupt (due to some unknown bug) and cannot be repaired.
5
+ */
6
+ export declare class InvalidRefCountError extends Error {
7
+ name: string;
8
+ readonly hash: Hash;
9
+ readonly value: unknown;
10
+ constructor(hash: Hash, value: unknown);
11
+ }
12
+ //# sourceMappingURL=invalid-ref-count-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalid-ref-count-error.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/dag/invalid-ref-count-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,YAAY,CAAC;AAErC;;;GAGG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,IAAI,SAA0B;IAC9B,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,YAAY,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAMrC;CACF"}
@@ -0,0 +1,19 @@
1
+ //#region ../replicache/src/dag/invalid-ref-count-error.ts
2
+ /**
3
+ * Thrown when a ref count read from the store is not a valid Uint16. This
4
+ * means the store is corrupt (due to some unknown bug) and cannot be repaired.
5
+ */
6
+ var InvalidRefCountError = class extends Error {
7
+ name = "InvalidRefCountError";
8
+ hash;
9
+ value;
10
+ constructor(hash, value) {
11
+ super(`Invalid ref count ${String(value)} for ${hash}. We expect the value to be a Uint16`);
12
+ this.hash = hash;
13
+ this.value = value;
14
+ }
15
+ };
16
+ //#endregion
17
+ export { InvalidRefCountError };
18
+
19
+ //# sourceMappingURL=invalid-ref-count-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalid-ref-count-error.js","names":[],"sources":["../../../../../replicache/src/dag/invalid-ref-count-error.ts"],"sourcesContent":["import type {Hash} from '../hash.ts';\n\n/**\n * Thrown when a ref count read from the store is not a valid Uint16. This\n * means the store is corrupt (due to some unknown bug) and cannot be repaired.\n */\nexport class InvalidRefCountError extends Error {\n name = 'InvalidRefCountError';\n readonly hash: Hash;\n readonly value: unknown;\n constructor(hash: Hash, value: unknown) {\n super(\n `Invalid ref count ${String(value)} for ${hash}. We expect the value to be a Uint16`,\n );\n this.hash = hash;\n this.value = value;\n }\n}\n"],"mappings":";;;;;AAMA,IAAa,uBAAb,cAA0C,MAAM;CAC9C,OAAO;CACP;CACA;CACA,YAAY,MAAY,OAAgB;EACtC,MACE,qBAAqB,OAAO,KAAK,EAAE,OAAO,KAAK,qCACjD;EACA,KAAK,OAAO;EACZ,KAAK,QAAQ;CACf;AACF"}
@@ -2,10 +2,20 @@ import { type Hash } from '../hash.ts';
2
2
  import type { Read as KVRead, Store as KVStore, Write as KVWrite } from '../kv/store.ts';
3
3
  import { Chunk, type ChunkHasher, type Refs } from './chunk.ts';
4
4
  import { type RefCountUpdatesDelegate } from './gc.ts';
5
+ import { InvalidRefCountError } from './invalid-ref-count-error.ts';
5
6
  import { type Read, type Store, type Write } from './store.ts';
7
+ /**
8
+ * Called when a write found an invalid ref count in the store. This is the
9
+ * single place where store corruption is detected, so the owner of the store
10
+ * can start recovery no matter which code path performed the write.
11
+ *
12
+ * The callback runs after the write transaction has been released, so it is
13
+ * safe for it to close or drop the underlying kv store.
14
+ */
15
+ export type OnInvalidRefCount = (e: InvalidRefCountError) => void;
6
16
  export declare class StoreImpl implements Store {
7
17
  #private;
8
- constructor(kv: KVStore, chunkHasher: ChunkHasher, assertValidHash: (hash: Hash) => void);
18
+ constructor(kv: KVStore, chunkHasher: ChunkHasher, assertValidHash: (hash: Hash) => void, onInvalidRefCount?: OnInvalidRefCount | undefined);
9
19
  read(): Promise<Read>;
10
20
  write(): Promise<Write>;
11
21
  close(): Promise<void>;
@@ -24,7 +34,7 @@ export declare class ReadImpl implements Read {
24
34
  export declare class WriteImpl extends ReadImpl implements Write, RefCountUpdatesDelegate {
25
35
  #private;
26
36
  protected readonly _tx: KVWrite;
27
- constructor(kvw: KVWrite, chunkHasher: ChunkHasher, assertValidHash: (hash: Hash) => void);
37
+ constructor(kvw: KVWrite, chunkHasher: ChunkHasher, assertValidHash: (hash: Hash) => void, onInvalidRefCount?: OnInvalidRefCount | undefined);
28
38
  createChunk: <V>(data: V, refs: Refs) => Chunk<V>;
29
39
  get kvWrite(): KVWrite;
30
40
  putChunk(c: Chunk): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"store-impl.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/dag/store-impl.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,KAAK,IAAI,EAAa,MAAM,YAAY,CAAC;AACjD,OAAO,KAAK,EACV,IAAI,IAAI,MAAM,EACd,KAAK,IAAI,OAAO,EAChB,KAAK,IAAI,OAAO,EACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,KAAK,EACL,KAAK,WAAW,EAChB,KAAK,IAAI,EAGV,MAAM,YAAY,CAAC;AACpB,OAAO,EAAC,KAAK,uBAAuB,EAAyB,MAAM,SAAS,CAAC;AAE7E,OAAO,EAAC,KAAK,IAAI,EAAE,KAAK,KAAK,EAAE,KAAK,KAAK,EAAe,MAAM,YAAY,CAAC;AAE3E,qBAAa,SAAU,YAAW,KAAK;;IAKrC,YACE,EAAE,EAAE,OAAO,EACX,WAAW,EAAE,WAAW,EACxB,eAAe,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,EAKtC;IAEK,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAE1B;IAEK,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,CAM5B;IAED,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;CACF;AAED,qBAAa,QAAS,YAAW,IAAI;IACnC,SAAS,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,eAAe,EAA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AG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAczD;IAEK,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,SAAS,IAAI,EAAE,CAAC,CAOlD;IAyBD,OAAO,IAAI,IAAI,CAWd;CACF"}
@@ -1,4 +1,4 @@
1
- import { assertNumber } from "../../../shared/src/asserts.js";
1
+ import { InvalidRefCountError } from "./invalid-ref-count-error.js";
2
2
  import { assertHash } from "../hash.js";
3
3
  import { Chunk, assertRefs, createChunk } from "./chunk.js";
4
4
  import { computeRefCountUpdates } from "./gc.js";
@@ -9,16 +9,18 @@ var StoreImpl = class {
9
9
  #kv;
10
10
  #chunkHasher;
11
11
  #assertValidHash;
12
- constructor(kv, chunkHasher, assertValidHash) {
12
+ #onInvalidRefCount;
13
+ constructor(kv, chunkHasher, assertValidHash, onInvalidRefCount) {
13
14
  this.#kv = kv;
14
15
  this.#chunkHasher = chunkHasher;
15
16
  this.#assertValidHash = assertValidHash;
17
+ this.#onInvalidRefCount = onInvalidRefCount;
16
18
  }
17
19
  async read() {
18
20
  return new ReadImpl(await this.#kv.read(), this.#assertValidHash);
19
21
  }
20
22
  async write() {
21
- return new WriteImpl(await this.#kv.write(), this.#chunkHasher, this.#assertValidHash);
23
+ return new WriteImpl(await this.#kv.write(), this.#chunkHasher, this.#assertValidHash, this.#onInvalidRefCount);
22
24
  }
23
25
  close() {
24
26
  return this.#kv.close();
@@ -68,11 +70,14 @@ var ReadImpl = class {
68
70
  };
69
71
  var WriteImpl = class extends ReadImpl {
70
72
  #chunkHasher;
73
+ #onInvalidRefCount;
74
+ #invalidRefCountError;
71
75
  #putChunks = /* @__PURE__ */ new Set();
72
76
  #changedHeads = /* @__PURE__ */ new Map();
73
- constructor(kvw, chunkHasher, assertValidHash) {
77
+ constructor(kvw, chunkHasher, assertValidHash, onInvalidRefCount) {
74
78
  super(kvw, assertValidHash);
75
79
  this.#chunkHasher = chunkHasher;
80
+ this.#onInvalidRefCount = onInvalidRefCount;
76
81
  }
77
82
  createChunk = (data, refs) => createChunk(data, refs, this.#chunkHasher);
78
83
  get kvWrite() {
@@ -113,15 +118,20 @@ var WriteImpl = class extends ReadImpl {
113
118
  await p1;
114
119
  }
115
120
  async commit() {
116
- const refCountUpdates = await computeRefCountUpdates(this.#changedHeads.values(), this.#putChunks, this);
121
+ let refCountUpdates;
122
+ try {
123
+ refCountUpdates = await computeRefCountUpdates(this.#changedHeads.values(), this.#putChunks, this);
124
+ } catch (e) {
125
+ if (e instanceof InvalidRefCountError) this.#invalidRefCountError = e;
126
+ throw e;
127
+ }
117
128
  await this.#applyRefCountUpdates(refCountUpdates);
118
129
  await this._tx.commit();
119
130
  }
120
131
  async getRefCount(hash) {
121
132
  const value = await this._tx.get(chunkRefCountKey(hash));
122
133
  if (value === void 0) return;
123
- assertNumber(value);
124
- if (value < 0 || value > 65535 || value !== (value | 0)) throw new Error(`Invalid ref count ${value}. We expect the value to be a Uint16`);
134
+ if (typeof value !== "number" || value < 0 || value > 65535 || value !== (value | 0)) throw new InvalidRefCountError(hash, value);
125
135
  return value;
126
136
  }
127
137
  async getRefs(hash) {
@@ -148,7 +158,11 @@ var WriteImpl = class extends ReadImpl {
148
158
  this.#putChunks.delete(hash);
149
159
  }
150
160
  release() {
151
- this._tx.release();
161
+ try {
162
+ this._tx.release();
163
+ } finally {
164
+ if (this.#invalidRefCountError) this.#onInvalidRefCount?.(this.#invalidRefCountError);
165
+ }
152
166
  }
153
167
  };
154
168
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"store-impl.js","names":["#kv","#chunkHasher","#assertValidHash","#putChunks","#changedHeads","#setHead","#applyRefCountUpdates","#removeAllRelatedKeys"],"sources":["../../../../../replicache/src/dag/store-impl.ts"],"sourcesContent":["import {assertNumber} from '../../../shared/src/asserts.ts';\nimport type {ReadonlyJSONValue} from '../../../shared/src/json.ts';\nimport {type Hash, assertHash} from '../hash.ts';\nimport type {\n Read as KVRead,\n Store as KVStore,\n Write as KVWrite,\n} from '../kv/store.ts';\nimport {\n Chunk,\n type ChunkHasher,\n type Refs,\n assertRefs,\n createChunk,\n} from './chunk.ts';\nimport {type RefCountUpdatesDelegate, computeRefCountUpdates} from './gc.ts';\nimport {chunkDataKey, chunkMetaKey, chunkRefCountKey, headKey} from './key.ts';\nimport {type Read, type Store, type Write, mustGetChunk} from './store.ts';\n\nexport class StoreImpl implements Store {\n readonly #kv: KVStore;\n readonly #chunkHasher: ChunkHasher;\n readonly #assertValidHash: (hash: Hash) => void;\n\n constructor(\n kv: KVStore,\n chunkHasher: ChunkHasher,\n assertValidHash: (hash: Hash) => void,\n ) {\n this.#kv = kv;\n this.#chunkHasher = chunkHasher;\n this.#assertValidHash = assertValidHash;\n }\n\n async read(): Promise<Read> {\n return new ReadImpl(await this.#kv.read(), this.#assertValidHash);\n }\n\n async write(): Promise<Write> {\n return new WriteImpl(\n await this.#kv.write(),\n this.#chunkHasher,\n this.#assertValidHash,\n );\n }\n\n close(): Promise<void> {\n return this.#kv.close();\n }\n}\n\nexport class ReadImpl implements Read {\n protected readonly _tx: KVRead;\n readonly assertValidHash: (hash: Hash) => void;\n\n constructor(kv: KVRead, assertValidHash: (hash: Hash) => void) {\n this._tx = kv;\n this.assertValidHash = assertValidHash;\n }\n\n hasChunk(hash: Hash): Promise<boolean> {\n return this._tx.has(chunkDataKey(hash));\n }\n\n async getChunk(hash: Hash): Promise<Chunk | undefined> {\n const dataPromise = this._tx.get(chunkDataKey(hash));\n const refsPromise = this._tx.get(chunkMetaKey(hash));\n const data = await dataPromise;\n if (data === undefined) {\n refsPromise.catch(() => {\n // Ignore error since we will return undefined anyway.\n });\n return undefined;\n }\n\n const refsVal = await refsPromise;\n let refs: Refs;\n if (refsVal !== undefined) {\n assertRefs(refsVal);\n refs = refsVal;\n } else {\n refs = [];\n }\n return new Chunk(hash, data, refs);\n }\n\n mustGetChunk(hash: Hash): Promise<Chunk> {\n return mustGetChunk(this, hash);\n }\n\n async getHead(name: string): Promise<Hash | undefined> {\n const data = await this._tx.get(headKey(name));\n if (data === undefined) {\n return undefined;\n }\n assertHash(data);\n return data;\n }\n\n release(): void {\n this._tx.release();\n }\n\n get closed(): boolean {\n return this._tx.closed;\n }\n}\n\ntype HeadChange = {\n new: Hash | undefined;\n old: Hash | undefined;\n};\n\nexport class WriteImpl\n extends ReadImpl\n implements Write, RefCountUpdatesDelegate\n{\n declare protected readonly _tx: KVWrite;\n readonly #chunkHasher: ChunkHasher;\n\n readonly #putChunks = new Set<Hash>();\n readonly #changedHeads = new Map<string, HeadChange>();\n\n constructor(\n kvw: KVWrite,\n chunkHasher: ChunkHasher,\n assertValidHash: (hash: Hash) => void,\n ) {\n super(kvw, assertValidHash);\n this.#chunkHasher = chunkHasher;\n }\n\n createChunk = <V>(data: V, refs: Refs): Chunk<V> =>\n createChunk(data, refs, this.#chunkHasher);\n\n get kvWrite(): KVWrite {\n return this._tx;\n }\n\n async putChunk(c: Chunk): Promise<void> {\n const {hash, data, meta} = c;\n // We never want to write temp hashes to the underlying store.\n this.assertValidHash(hash);\n const key = chunkDataKey(hash);\n // Commit contains InternalValue and Hash which are opaque types.\n const p1 = this._tx.put(key, data as ReadonlyJSONValue);\n let p2;\n if (meta.length > 0) {\n for (const h of meta) {\n this.assertValidHash(h);\n }\n p2 = this._tx.put(chunkMetaKey(hash), meta);\n }\n this.#putChunks.add(hash);\n await p1;\n await p2;\n }\n\n setHead(name: string, hash: Hash): Promise<void> {\n return this.#setHead(name, hash);\n }\n\n removeHead(name: string): Promise<void> {\n return this.#setHead(name, undefined);\n }\n\n async #setHead(name: string, hash: Hash | undefined): Promise<void> {\n const oldHash = await this.getHead(name);\n const hk = headKey(name);\n\n let p1: Promise<void>;\n if (hash === undefined) {\n p1 = this._tx.del(hk);\n } else {\n p1 = this._tx.put(hk, hash);\n }\n\n const v = this.#changedHeads.get(name);\n if (v === undefined) {\n this.#changedHeads.set(name, {new: hash, old: oldHash});\n } else {\n // Keep old if existing\n v.new = hash;\n }\n\n await p1;\n }\n\n async commit(): Promise<void> {\n const refCountUpdates = await computeRefCountUpdates(\n this.#changedHeads.values(),\n this.#putChunks,\n this,\n );\n await this.#applyRefCountUpdates(refCountUpdates);\n await this._tx.commit();\n }\n\n async getRefCount(hash: Hash): Promise<number | undefined> {\n const value = await this._tx.get(chunkRefCountKey(hash));\n if (value === undefined) {\n return undefined;\n }\n assertNumber(value);\n if (value < 0 || value > 0xffff || value !== (value | 0)) {\n throw new Error(\n `Invalid ref count ${value}. We expect the value to be a Uint16`,\n );\n }\n return value;\n }\n\n async getRefs(hash: Hash): Promise<readonly Hash[]> {\n const meta = await this._tx.get(chunkMetaKey(hash));\n if (meta === undefined) {\n return [];\n }\n assertRefs(meta);\n return meta;\n }\n\n async #applyRefCountUpdates(refCountCache: Map<Hash, number>): Promise<void> {\n const ps: Promise<void>[] = [];\n for (const [hash, count] of refCountCache) {\n if (count === 0) {\n ps.push(this.#removeAllRelatedKeys(hash));\n } else {\n const refCountKey = chunkRefCountKey(hash);\n ps.push(this._tx.put(refCountKey, count));\n }\n }\n await Promise.all(ps);\n }\n\n async #removeAllRelatedKeys(hash: Hash): Promise<void> {\n await Promise.all([\n this._tx.del(chunkDataKey(hash)),\n this._tx.del(chunkMetaKey(hash)),\n this._tx.del(chunkRefCountKey(hash)),\n ]);\n\n this.#putChunks.delete(hash);\n }\n\n release(): void {\n this._tx.release();\n }\n}\n"],"mappings":";;;;;;;AAmBA,IAAa,YAAb,MAAwC;CACtC;CACA;CACA;CAEA,YACE,IACA,aACA,iBACA;EACA,KAAKA,MAAM;EACX,KAAKC,eAAe;EACpB,KAAKC,mBAAmB;CAC1B;CAEA,MAAM,OAAsB;EAC1B,OAAO,IAAI,SAAS,MAAM,KAAKF,IAAI,KAAK,GAAG,KAAKE,gBAAgB;CAClE;CAEA,MAAM,QAAwB;EAC5B,OAAO,IAAI,UACT,MAAM,KAAKF,IAAI,MAAM,GACrB,KAAKC,cACL,KAAKC,gBACP;CACF;CAEA,QAAuB;EACrB,OAA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MACR,qBAAqB,MAAM,qCAC7B;EAEF,OAAO;CACT;CAEA,MAAM,QAAQ,MAAsC;EAClD,MAAM,OAAO,MAAM,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;EAClD,IAAI,SAAS,KAAA,GACX,OAAO,CAAC;EAEV,WAAW,IAAI;EACf,OAAO;CACT;CAEA,MAAMA,sBAAsB,eAAiD;EAC3E,MAAM,KAAsB,CAAC;EAC7B,KAAK,MAAM,CAAC,MAAM,UAAU,eAC1B,IAAI,UAAU,GACZ,GAAG,KAAK,KAAKC,sBAAsB,IAAI,CAAC;OACnC;GACL,MAAM,cAAc,iBAAiB,IAAI;GACzC,GAAG,KAAK,KAAK,IAAI,IAAI,aAAa,KAAK,CAAC;EAC1C;EAEF,MAAM,QAAQ,IAAI,EAAE;CACtB;CAEA,MAAMA,sBAAsB,MAA2B;EACrD,MAAM,QAAQ,IAAI;GAChB,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;GAC/B,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;GAC/B,KAAK,IAAI,IAAI,iBAAiB,IAAI,CAAC;EACrC,CAAC;EAED,KAAKJ,WAAW,OAAO,IAAI;CAC7B;CAEA,UAAgB;EACd,KAAK,IAAI,QAAQ;CACnB;AACF"}
1
+ {"version":3,"file":"store-impl.js","names":["#kv","#chunkHasher","#assertValidHash","#onInvalidRefCount","#putChunks","#changedHeads","#setHead","#invalidRefCountError","#applyRefCountUpdates","#removeAllRelatedKeys"],"sources":["../../../../../replicache/src/dag/store-impl.ts"],"sourcesContent":["import type {ReadonlyJSONValue} from '../../../shared/src/json.ts';\nimport {type Hash, assertHash} from '../hash.ts';\nimport type {\n Read as KVRead,\n Store as KVStore,\n Write as KVWrite,\n} from '../kv/store.ts';\nimport {\n Chunk,\n type ChunkHasher,\n type Refs,\n assertRefs,\n createChunk,\n} from './chunk.ts';\nimport {type RefCountUpdatesDelegate, computeRefCountUpdates} from './gc.ts';\nimport {InvalidRefCountError} from './invalid-ref-count-error.ts';\nimport {chunkDataKey, chunkMetaKey, chunkRefCountKey, headKey} from './key.ts';\nimport {type Read, type Store, type Write, mustGetChunk} from './store.ts';\n\n/**\n * Called when a write found an invalid ref count in the store. This is the\n * single place where store corruption is detected, so the owner of the store\n * can start recovery no matter which code path performed the write.\n *\n * The callback runs after the write transaction has been released, so it is\n * safe for it to close or drop the underlying kv store.\n */\nexport type OnInvalidRefCount = (e: InvalidRefCountError) => void;\n\nexport class StoreImpl implements Store {\n readonly #kv: KVStore;\n readonly #chunkHasher: ChunkHasher;\n readonly #assertValidHash: (hash: Hash) => void;\n readonly #onInvalidRefCount: OnInvalidRefCount | undefined;\n\n constructor(\n kv: KVStore,\n chunkHasher: ChunkHasher,\n assertValidHash: (hash: Hash) => void,\n onInvalidRefCount?: OnInvalidRefCount | undefined,\n ) {\n this.#kv = kv;\n this.#chunkHasher = chunkHasher;\n this.#assertValidHash = assertValidHash;\n this.#onInvalidRefCount = onInvalidRefCount;\n }\n\n async read(): Promise<Read> {\n return new ReadImpl(await this.#kv.read(), this.#assertValidHash);\n }\n\n async write(): Promise<Write> {\n return new WriteImpl(\n await this.#kv.write(),\n this.#chunkHasher,\n this.#assertValidHash,\n this.#onInvalidRefCount,\n );\n }\n\n close(): Promise<void> {\n return this.#kv.close();\n }\n}\n\nexport class ReadImpl implements Read {\n protected readonly _tx: KVRead;\n readonly assertValidHash: (hash: Hash) => void;\n\n constructor(kv: KVRead, assertValidHash: (hash: Hash) => void) {\n this._tx = kv;\n this.assertValidHash = assertValidHash;\n }\n\n hasChunk(hash: Hash): Promise<boolean> {\n return this._tx.has(chunkDataKey(hash));\n }\n\n async getChunk(hash: Hash): Promise<Chunk | undefined> {\n const dataPromise = this._tx.get(chunkDataKey(hash));\n const refsPromise = this._tx.get(chunkMetaKey(hash));\n const data = await dataPromise;\n if (data === undefined) {\n refsPromise.catch(() => {\n // Ignore error since we will return undefined anyway.\n });\n return undefined;\n }\n\n const refsVal = await refsPromise;\n let refs: Refs;\n if (refsVal !== undefined) {\n assertRefs(refsVal);\n refs = refsVal;\n } else {\n refs = [];\n }\n return new Chunk(hash, data, refs);\n }\n\n mustGetChunk(hash: Hash): Promise<Chunk> {\n return mustGetChunk(this, hash);\n }\n\n async getHead(name: string): Promise<Hash | undefined> {\n const data = await this._tx.get(headKey(name));\n if (data === undefined) {\n return undefined;\n }\n assertHash(data);\n return data;\n }\n\n release(): void {\n this._tx.release();\n }\n\n get closed(): boolean {\n return this._tx.closed;\n }\n}\n\ntype HeadChange = {\n new: Hash | undefined;\n old: Hash | undefined;\n};\n\nexport class WriteImpl\n extends ReadImpl\n implements Write, RefCountUpdatesDelegate\n{\n declare protected readonly _tx: KVWrite;\n readonly #chunkHasher: ChunkHasher;\n readonly #onInvalidRefCount: OnInvalidRefCount | undefined;\n #invalidRefCountError: InvalidRefCountError | undefined;\n\n readonly #putChunks = new Set<Hash>();\n readonly #changedHeads = new Map<string, HeadChange>();\n\n constructor(\n kvw: KVWrite,\n chunkHasher: ChunkHasher,\n assertValidHash: (hash: Hash) => void,\n onInvalidRefCount?: OnInvalidRefCount | undefined,\n ) {\n super(kvw, assertValidHash);\n this.#chunkHasher = chunkHasher;\n this.#onInvalidRefCount = onInvalidRefCount;\n }\n\n createChunk = <V>(data: V, refs: Refs): Chunk<V> =>\n createChunk(data, refs, this.#chunkHasher);\n\n get kvWrite(): KVWrite {\n return this._tx;\n }\n\n async putChunk(c: Chunk): Promise<void> {\n const {hash, data, meta} = c;\n // We never want to write temp hashes to the underlying store.\n this.assertValidHash(hash);\n const key = chunkDataKey(hash);\n // Commit contains InternalValue and Hash which are opaque types.\n const p1 = this._tx.put(key, data as ReadonlyJSONValue);\n let p2;\n if (meta.length > 0) {\n for (const h of meta) {\n this.assertValidHash(h);\n }\n p2 = this._tx.put(chunkMetaKey(hash), meta);\n }\n this.#putChunks.add(hash);\n await p1;\n await p2;\n }\n\n setHead(name: string, hash: Hash): Promise<void> {\n return this.#setHead(name, hash);\n }\n\n removeHead(name: string): Promise<void> {\n return this.#setHead(name, undefined);\n }\n\n async #setHead(name: string, hash: Hash | undefined): Promise<void> {\n const oldHash = await this.getHead(name);\n const hk = headKey(name);\n\n let p1: Promise<void>;\n if (hash === undefined) {\n p1 = this._tx.del(hk);\n } else {\n p1 = this._tx.put(hk, hash);\n }\n\n const v = this.#changedHeads.get(name);\n if (v === undefined) {\n this.#changedHeads.set(name, {new: hash, old: oldHash});\n } else {\n // Keep old if existing\n v.new = hash;\n }\n\n await p1;\n }\n\n async commit(): Promise<void> {\n let refCountUpdates: Map<Hash, number>;\n try {\n refCountUpdates = await computeRefCountUpdates(\n this.#changedHeads.values(),\n this.#putChunks,\n this,\n );\n } catch (e) {\n if (e instanceof InvalidRefCountError) {\n // Reported from release() rather than here: the owner may drop the\n // store in response and the kv transaction still needs to roll back.\n this.#invalidRefCountError = e;\n }\n throw e;\n }\n await this.#applyRefCountUpdates(refCountUpdates);\n await this._tx.commit();\n }\n\n async getRefCount(hash: Hash): Promise<number | undefined> {\n const value = await this._tx.get(chunkRefCountKey(hash));\n if (value === undefined) {\n return undefined;\n }\n if (\n typeof value !== 'number' ||\n value < 0 ||\n value > 0xffff ||\n value !== (value | 0)\n ) {\n throw new InvalidRefCountError(hash, value);\n }\n return value;\n }\n\n async getRefs(hash: Hash): Promise<readonly Hash[]> {\n const meta = await this._tx.get(chunkMetaKey(hash));\n if (meta === undefined) {\n return [];\n }\n assertRefs(meta);\n return meta;\n }\n\n async #applyRefCountUpdates(refCountCache: Map<Hash, number>): Promise<void> {\n const ps: Promise<void>[] = [];\n for (const [hash, count] of refCountCache) {\n if (count === 0) {\n ps.push(this.#removeAllRelatedKeys(hash));\n } else {\n const refCountKey = chunkRefCountKey(hash);\n ps.push(this._tx.put(refCountKey, count));\n }\n }\n await Promise.all(ps);\n }\n\n async #removeAllRelatedKeys(hash: Hash): Promise<void> {\n await Promise.all([\n this._tx.del(chunkDataKey(hash)),\n this._tx.del(chunkMetaKey(hash)),\n this._tx.del(chunkRefCountKey(hash)),\n ]);\n\n this.#putChunks.delete(hash);\n }\n\n release(): void {\n try {\n this._tx.release();\n } finally {\n // Report even if the kv release threw (for example a failed SQLite\n // ROLLBACK): the store is corrupt either way and the owner must still\n // recover. The release error keeps propagating to the caller.\n if (this.#invalidRefCountError) {\n this.#onInvalidRefCount?.(this.#invalidRefCountError);\n }\n }\n }\n}\n"],"mappings":";;;;;;;AA6BA,IAAa,YAAb,MAAwC;CACtC;CACA;CACA;CACA;CAEA,YACE,IACA,aACA,iBACA,mBACA;EACA,KAAKA,MAAM;EACX,KAAKC,eAAe;EACpB,KAAKC,mBAAmB;EACxB,KAAKC,qBAAqB;CAC5B;CAEA,MAAM,OAAsB;EAC1B,OAAO,IAAI,SAAS,MAAM,KAAKH,IAAI,KAAK,GAAG,KAAKE,gBAAgB;CAClE;CAEA,MAAM,QAAwB;EAC5B,OAAO,IAAI,UACT,MAAM,KAAKF,IAAI,MAAM,GACrB,KAAKC,cACL,KAAKC,kBACL,KAAKC,kBACP;CACF;CAEA,QAAuB;EACrB,OAAO,KAAKH,IAAI,MAAM;CACxB;AACF;AAEA,IAAa,WAAb,MAAsC;CACpC;CACA;CAEA,YAAY,IAAY,iBAAuC;EAC7D,KAAK,MAAM;EACX,KAAK,kBAAkB;CACzB;CAEA,SAAS,MAA8B;EACrC,OAAO,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;CACxC;CAEA,MAAM,SAAS,MAAwC;EACrD,MAAM,cAAc,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;EACnD,MAAM,cAAc,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;EACnD,MAAM,OAAO,MAAM;EACnB,IAAI,SAAS,KAAA,GAAW;GACtB,YAAY,YAAY,CAExB,CAAC;GACD;EACF;EAEA,MAAM,UAAU,MAAM;EACtB,IAAI;EACJ,IAAI,YAAY,KAAA,GAAW;GACzB,WAAW,OAAO;GAClB,OAAO;EACT,OACE,OAAO,CAAC;EAEV,OAAO,IAAI,M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C1C;EAEF,MAAM,QAAQ,IAAI,EAAE;CACtB;CAEA,MAAMA,sBAAsB,MAA2B;EACrD,MAAM,QAAQ,IAAI;GAChB,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;GAC/B,KAAK,IAAI,IAAI,aAAa,IAAI,CAAC;GAC/B,KAAK,IAAI,IAAI,iBAAiB,IAAI,CAAC;EACrC,CAAC;EAED,KAAKL,WAAW,OAAO,IAAI;CAC7B;CAEA,UAAgB;EACd,IAAI;GACF,KAAK,IAAI,QAAQ;EACnB,UAAU;GAIR,IAAI,KAAKG,uBACP,KAAKJ,qBAAqB,KAAKI,qBAAqB;EAExD;CACF;AACF"}
@@ -8,7 +8,19 @@ import type { ClientID } from '../sync/ids.ts';
8
8
  import type { MutatorDefs } from '../types.ts';
9
9
  import { type Commit, type LocalMeta, type Meta } from './commit.ts';
10
10
  type FormatVersion = Enum<typeof FormatVersion>;
11
- export declare function rebaseMutationAndPutCommit(mutation: Commit<LocalMeta>, dagWrite: DagWrite, basis: Hash, mutators: MutatorDefs, lc: LogContext, mutationClientID: ClientID, formatVersion: FormatVersion, zeroData: ZeroTxData | undefined): Promise<Commit<Meta>>;
12
- export declare function rebaseMutationAndCommit(mutation: Commit<LocalMeta>, dagWrite: DagWrite, basis: Hash, headName: string, mutators: MutatorDefs, lc: LogContext, mutationClientID: ClientID, formatVersion: FormatVersion, zeroData: ZeroTxData | undefined): Promise<Hash>;
11
+ /**
12
+ * The result of replaying one mutation.
13
+ *
14
+ * `zeroData` is the Zero transaction data to use for the next mutation in the
15
+ * same replay. Each mutation runs against its own fork. On success that fork is
16
+ * returned, and if the mutator throws the caller gets back the one it passed
17
+ * in, without the failed mutation's IVM writes. See `rebaseMutation`.
18
+ */
19
+ export type RebaseResult<T> = {
20
+ result: T;
21
+ zeroData: ZeroTxData | undefined;
22
+ };
23
+ export declare function rebaseMutationAndPutCommit(mutation: Commit<LocalMeta>, dagWrite: DagWrite, basis: Hash, mutators: MutatorDefs, lc: LogContext, mutationClientID: ClientID, formatVersion: FormatVersion, zeroData: ZeroTxData | undefined): Promise<RebaseResult<Commit<Meta>>>;
24
+ export declare function rebaseMutationAndCommit(mutation: Commit<LocalMeta>, dagWrite: DagWrite, basis: Hash, headName: string, mutators: MutatorDefs, lc: LogContext, mutationClientID: ClientID, formatVersion: FormatVersion, zeroData: ZeroTxData | undefined): Promise<RebaseResult<Hash>>;
13
25
  export {};
14
26
  //# sourceMappingURL=rebase.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"rebase.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/db/rebase.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAEjD,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,6BAA6B,CAAC;AACtD,OAAO,KAAK,EAAC,KAAK,IAAI,QAAQ,EAAC,MAAM,iBAAiB,CAAC;AACvD,OAAO,KAAK,aAAa,MAAM,2BAA2B,CAAC;AAC3D,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,YAAY,CAAC;AACrC,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,0BAA0B,CAAC;AACzD,OAAO,KAAK,EAAC,QAAQ,EAAC,MAAM,gBAAgB,CAAC;AAE7C,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,aAAa,CAAC;AAC7C,OAAO,EACL,KAAK,MAAM,EACX,KAAK,SAAS,EAEd,KAAK,IAAI,EAIV,MAAM,aAAa,CAAC;AAIrB,KAAK,aAAa,GAAG,IAAI,CAAC,OAAO,aAAa,CAAC,CAAC;AA8EhD,wBAAsB,0BAA0B,CAC9C,QAAQ,EAAE,MAAM,CAAC,SAAS,CAAC,EAC3B,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,IAAI,EACX,QAAQ,EAAE,WAAW,EACrB,EAAE,EAAE,UAAU,EAGd,gBAAgB,EAAE,QAAQ,EAC1B,aAAa,EAAE,aAAa,EAC5B,QAAQ,EAAE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A8GF,wBAAsB,0BAA0B,CAC9C,QAAQ,EAAE,MAAM,CAAC,SAAS,CAAC,EAC3B,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,IAAI,EACX,QAAQ,EAAE,WAAW,EACrB,EAAE,EAAE,UAAU,EAGd,gBAAgB,EAAE,QAAQ,EAC1B,aAAa,EAAE,aAAa,EAC5B,QAAQ,EAAE,UAAU,GAAG,SAAS,GAC/B,OAAO,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAYrC;AAED,wBAAsB,uBAAuB,CAC3C,QAAQ,EAAE,MAAM,CAAC,SAAS,CAAC,EAC3B,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,IAAI,EACX,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,WAAW,EACrB,EAAE,EAAE,UAAU,EAGd,gBAAgB,EAAE,QAAQ,EAC1B,aAAa,EAAE,aAAa,EAC5B,QAAQ,EAAE,UAAU,GAAG,SAAS,GAC/B,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAY7B"}
@@ -14,15 +14,37 @@ async function rebaseMutation(mutation, dagWrite, basisHash, mutators, lc, mutat
14
14
  const nextMutationID = await (await commitFromHash(basisHash, dagWrite)).getNextMutationID(mutationClientID, dagWrite);
15
15
  if (nextMutationID !== localMeta.mutationID) throw new Error(`Inconsistent mutation ID: original: ${localMeta.mutationID}, next: ${nextMutationID} - mutationClientID: ${mutationClientID} mutatorName: ${name}`);
16
16
  if (formatVersion >= 5) assertLocalMetaDD31(localMeta);
17
- const dbWrite = await newWriteLocal(basisHash, name, args, mutation.chunk.hash, dagWrite, localMeta.timestamp, mutationClientID, formatVersion);
18
- await mutatorImpl(new WriteTransactionImpl(mutationClientID, await dbWrite.getMutationID(), "rebase", zeroData, dbWrite, lc), args);
19
- return dbWrite;
17
+ const newWrite = () => newWriteLocal(basisHash, name, args, mutation.chunk.hash, dagWrite, localMeta.timestamp, mutationClientID, formatVersion);
18
+ const dbWrite = await newWrite();
19
+ const txData = zeroData?.fork();
20
+ const tx = new WriteTransactionImpl(mutationClientID, await dbWrite.getMutationID(), "rebase", txData, dbWrite, lc);
21
+ try {
22
+ await mutatorImpl(tx, args);
23
+ } catch (e) {
24
+ lc.info?.(`Rebase of mutator ${name} threw, abandoning its prediction`, e);
25
+ return {
26
+ result: await newWrite(),
27
+ zeroData
28
+ };
29
+ }
30
+ return {
31
+ result: dbWrite,
32
+ zeroData: txData
33
+ };
20
34
  }
21
35
  async function rebaseMutationAndPutCommit(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion, zeroData) {
22
- return (await rebaseMutation(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion, zeroData)).putCommit();
36
+ const { result: tx, zeroData: next } = await rebaseMutation(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion, zeroData);
37
+ return {
38
+ result: await tx.putCommit(),
39
+ zeroData: next
40
+ };
23
41
  }
24
42
  async function rebaseMutationAndCommit(mutation, dagWrite, basis, headName, mutators, lc, mutationClientID, formatVersion, zeroData) {
25
- return (await rebaseMutation(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion, zeroData)).commit(headName);
43
+ const { result: dbWrite, zeroData: next } = await rebaseMutation(mutation, dagWrite, basis, mutators, lc, mutationClientID, formatVersion, zeroData);
44
+ return {
45
+ result: await dbWrite.commit(headName),
46
+ zeroData: next
47
+ };
26
48
  }
27
49
  //#endregion
28
50
  export { rebaseMutationAndCommit, rebaseMutationAndPutCommit };
@@ -1 +1 @@
1
- {"version":3,"file":"rebase.js","names":[],"sources":["../../../../../replicache/src/db/rebase.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport type {Enum} from '../../../shared/src/enum.ts';\nimport type {Write as DagWrite} from '../dag/store.ts';\nimport * as FormatVersion from '../format-version-enum.ts';\nimport type {Hash} from '../hash.ts';\nimport type {ZeroTxData} from '../replicache-options.ts';\nimport type {ClientID} from '../sync/ids.ts';\nimport {WriteTransactionImpl} from '../transactions.ts';\nimport type {MutatorDefs} from '../types.ts';\nimport {\n type Commit,\n type LocalMeta,\n type LocalMetaDD31,\n type Meta,\n assertLocalMetaDD31,\n commitFromHash,\n isLocalMetaDD31,\n} from './commit.ts';\nimport type {Write} from './write.ts';\nimport {newWriteLocal} from './write.ts';\n\ntype FormatVersion = Enum<typeof FormatVersion>;\n\nasync function rebaseMutation(\n mutation: Commit<LocalMetaDD31>,\n dagWrite: DagWrite,\n basisHash: Hash,\n mutators: MutatorDefs,\n lc: LogContext,\n mutationClientID: ClientID,\n formatVersion: FormatVersion,\n zeroData: ZeroTxData | undefined,\n): Promise<Write> {\n const localMeta = mutation.meta;\n const name = localMeta.mutatorName;\n if (isLocalMetaDD31(localMeta)) {\n assert(\n localMeta.clientID === mutationClientID,\n 'mutationClientID must match clientID of LocalMeta',\n );\n }\n const maybeMutatorImpl = mutators[name];\n if (!maybeMutatorImpl) {\n // Developers must not remove mutator names from code deployed with the\n // same schemaVersion because Replicache needs to be able to replay\n // mutations during pull.\n //\n // If we detect that this has happened, stub in a no-op mutator so that at\n // least sync can move forward. Note that the server-side mutation will\n // still get sent. This doesn't remove the queued local mutation, it just\n // removes its visible effects.\n lc.error?.(`Cannot rebase unknown mutator ${name}`);\n }\n const mutatorImpl =\n maybeMutatorImpl ||\n (async () => {\n // no op\n });\n\n const args = localMeta.mutatorArgsJSON;\n\n const basisCommit = await commitFromHash(basisHash, dagWrite);\n const nextMutationID = await basisCommit.getNextMutationID(\n mutationClientID,\n dagWrite,\n );\n if (nextMutationID !== localMeta.mutationID) {\n throw new Error(\n `Inconsistent mutation ID: original: ${localMeta.mutationID}, next: ${nextMutationID} - mutationClientID: ${mutationClientID} mutatorName: ${name}`,\n );\n }\n\n if (formatVersion >= FormatVersion.DD31) {\n assertLocalMetaDD31(localMeta);\n }\n\n const dbWrite = await newWriteLocal(\n basisHash,\n name,\n args,\n mutation.chunk.hash,\n dagWrite,\n localMeta.timestamp,\n mutationClientID,\n formatVersion,\n );\n\n const tx = new WriteTransactionImpl(\n mutationClientID,\n await dbWrite.getMutationID(),\n 'rebase',\n zeroData,\n dbWrite,\n lc,\n );\n await mutatorImpl(tx, args);\n return dbWrite;\n}\n\nexport async function rebaseMutationAndPutCommit(\n mutation: Commit<LocalMeta>,\n dagWrite: DagWrite,\n basis: Hash,\n mutators: MutatorDefs,\n lc: LogContext,\n // TODO(greg): mutationClientID can be retrieved from mutation if LocalMeta\n // is a LocalMetaDD31. As part of DD31 cleanup we can remove this arg.\n mutationClientID: ClientID,\n formatVersion: FormatVersion,\n zeroData: ZeroTxData | undefined,\n): Promise<Commit<Meta>> {\n const tx = await rebaseMutation(\n mutation,\n dagWrite,\n basis,\n mutators,\n lc,\n mutationClientID,\n formatVersion,\n zeroData,\n );\n return tx.putCommit();\n}\n\nexport async function rebaseMutationAndCommit(\n mutation: Commit<LocalMeta>,\n dagWrite: DagWrite,\n basis: Hash,\n headName: string,\n mutators: MutatorDefs,\n lc: LogContext,\n // TODO(greg): mutationClientID can be retrieved from mutation if LocalMeta\n // is a LocalMetaDD31. As part of DD31 cleanup we can remove this arg.\n mutationClientID: ClientID,\n formatVersion: FormatVersion,\n zeroData: ZeroTxData | undefined,\n): Promise<Hash> {\n const dbWrite = await rebaseMutation(\n mutation,\n dagWrite,\n basis,\n mutators,\n lc,\n mutationClientID,\n formatVersion,\n zeroData,\n );\n return dbWrite.commit(headName);\n}\n"],"mappings":";;;;;AAwBA,eAAe,eACb,UACA,UACA,WACA,UACA,IACA,kBACA,eACA,UACgB;CAChB,MAAM,YAAY,SAAS;CAC3B,MAAM,OAAO,UAAU;CACvB,IAAI,gBAAgB,SAAS,GAC3B,OACE,UAAU,aAAa,kBACvB,mDACF;CAEF,MAAM,mBAAmB,SAAS;CAClC,IAAI,CAAC,kBASH,GAAG,QAAQ,iCAAiC,MAAM;CAEpD,MAAM,cACJ,qBACC,YAAY,CAEb;CAEF,MAAM,OAAO,UAAU;CAGvB,MAAM,iBAAiB,OAAM,MADH,eAAe,WAAW,QAAQ,GACnB,kBACvC,kBACA,QACF;CACA,IAAI,mBAAmB,UAAU,YAC/B,MAAM,IAAI,MACR,uCAAuC,UAAU,WAAW,UAAU,eAAe,uBAAuB,iBAAiB,gBAAgB,MAC/I;CAGF,IAAI,iBAAiB,GACnB,oBAAoB,SAAS;CAG/B,MAAM,UAAU,MAAM,cACpB,WACA,MACA,MACA,SAAS,MAAM,MACf,UACA,UAAU,WACV,kBACA,aACF;CAUA,MAAM,YAAY,IARH,qBACb,kBACA,MAAM,QAAQ,cAAc,GAC5B,UACA,UACA,SACA,EAEgB,GAAI,IAAI;CAC1B,OAAO;AACT;AAEA,eAAsB,2BACpB,UACA,UACA,OACA,UACA,IAGA,kBACA,eACA,UACuB;CAWvB,QAAO,MAVU,eACf,UACA,UACA,OACA,UACA,IACA,kBACA,eACA,QACF,GACU,UAAU;AACtB;AAEA,eAAsB,wBACpB,UACA,UACA,OACA,UACA,UACA,IAGA,kBACA,eACA,UACe;CAWf,QAAO,MAVe,eACpB,UACA,UACA,OACA,UACA,IACA,kBACA,eACA,QACF,GACe,OAAO,QAAQ;AAChC"}
1
+ {"version":3,"file":"rebase.js","names":[],"sources":["../../../../../replicache/src/db/rebase.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport type {Enum} from '../../../shared/src/enum.ts';\nimport type {Write as DagWrite} from '../dag/store.ts';\nimport * as FormatVersion from '../format-version-enum.ts';\nimport type {Hash} from '../hash.ts';\nimport type {ZeroTxData} from '../replicache-options.ts';\nimport type {ClientID} from '../sync/ids.ts';\nimport {WriteTransactionImpl} from '../transactions.ts';\nimport type {MutatorDefs} from '../types.ts';\nimport {\n type Commit,\n type LocalMeta,\n type LocalMetaDD31,\n type Meta,\n assertLocalMetaDD31,\n commitFromHash,\n isLocalMetaDD31,\n} from './commit.ts';\nimport type {Write} from './write.ts';\nimport {newWriteLocal} from './write.ts';\n\ntype FormatVersion = Enum<typeof FormatVersion>;\n\n/**\n * The result of replaying one mutation.\n *\n * `zeroData` is the Zero transaction data to use for the next mutation in the\n * same replay. Each mutation runs against its own fork. On success that fork is\n * returned, and if the mutator throws the caller gets back the one it passed\n * in, without the failed mutation's IVM writes. See `rebaseMutation`.\n */\nexport type RebaseResult<T> = {\n result: T;\n zeroData: ZeroTxData | undefined;\n};\n\nasync function rebaseMutation(\n mutation: Commit<LocalMetaDD31>,\n dagWrite: DagWrite,\n basisHash: Hash,\n mutators: MutatorDefs,\n lc: LogContext,\n mutationClientID: ClientID,\n formatVersion: FormatVersion,\n zeroData: ZeroTxData | undefined,\n): Promise<RebaseResult<Write>> {\n const localMeta = mutation.meta;\n const name = localMeta.mutatorName;\n if (isLocalMetaDD31(localMeta)) {\n assert(\n localMeta.clientID === mutationClientID,\n 'mutationClientID must match clientID of LocalMeta',\n );\n }\n const maybeMutatorImpl = mutators[name];\n if (!maybeMutatorImpl) {\n // Developers must not remove mutator names from code deployed with the\n // same schemaVersion because Replicache needs to be able to replay\n // mutations during pull.\n //\n // If we detect that this has happened, stub in a no-op mutator so that at\n // least sync can move forward. Note that the server-side mutation will\n // still get sent. This doesn't remove the queued local mutation, it just\n // removes its visible effects.\n lc.error?.(`Cannot rebase unknown mutator ${name}`);\n }\n const mutatorImpl =\n maybeMutatorImpl ||\n (async () => {\n // no op\n });\n\n const args = localMeta.mutatorArgsJSON;\n\n const basisCommit = await commitFromHash(basisHash, dagWrite);\n const nextMutationID = await basisCommit.getNextMutationID(\n mutationClientID,\n dagWrite,\n );\n if (nextMutationID !== localMeta.mutationID) {\n throw new Error(\n `Inconsistent mutation ID: original: ${localMeta.mutationID}, next: ${nextMutationID} - mutationClientID: ${mutationClientID} mutatorName: ${name}`,\n );\n }\n\n if (formatVersion >= FormatVersion.DD31) {\n assertLocalMetaDD31(localMeta);\n }\n\n const newWrite = () =>\n newWriteLocal(\n basisHash,\n name,\n args,\n mutation.chunk.hash,\n dagWrite,\n localMeta.timestamp,\n mutationClientID,\n formatVersion,\n );\n\n const dbWrite = await newWrite();\n\n // Run the mutator against a fork so that if it throws, its IVM writes are\n // discarded along with its `Write`.\n const txData = zeroData?.fork();\n\n const tx = new WriteTransactionImpl(\n mutationClientID,\n await dbWrite.getMutationID(),\n 'rebase',\n txData,\n dbWrite,\n lc,\n );\n\n try {\n await mutatorImpl(tx, args);\n } catch (e) {\n // A mutator can throw here without anything being wrong: it is being run\n // again against a newer server snapshot, so a check that passed when the\n // user first ran it can fail now. Rethrowing fails the whole rebase, which\n // in Zero fails the poke and disconnects the client. The mutation stays\n // pending, so the next poke fails the same way.\n //\n // Only the local prediction is dropped. The mutation is still pending and\n // is still sent to the server, which decides whether it applied.\n //\n // `dbWrite` is dropped rather than closed: it shares `dagWrite` with the\n // caller. Dropping it is enough because a `Write` does not modify anything\n // outside itself and only writes chunks when it is committed.\n //\n // Engine errors raised inside the mutator are retired the same way, rather\n // than being sorted out here. A client whose state was garbage collected\n // throws ChunkNotFoundError from its reads, and that is detected on its\n // next mutation, where replicache-impl converts it to a\n // ClientStateNotFoundError and calls onClientStateNotFound.\n lc.info?.(`Rebase of mutator ${name} threw, abandoning its prediction`, e);\n return {result: await newWrite(), zeroData};\n }\n\n return {result: dbWrite, zeroData: txData};\n}\n\nexport async function rebaseMutationAndPutCommit(\n mutation: Commit<LocalMeta>,\n dagWrite: DagWrite,\n basis: Hash,\n mutators: MutatorDefs,\n lc: LogContext,\n // TODO(greg): mutationClientID can be retrieved from mutation if LocalMeta\n // is a LocalMetaDD31. As part of DD31 cleanup we can remove this arg.\n mutationClientID: ClientID,\n formatVersion: FormatVersion,\n zeroData: ZeroTxData | undefined,\n): Promise<RebaseResult<Commit<Meta>>> {\n const {result: tx, zeroData: next} = await rebaseMutation(\n mutation,\n dagWrite,\n basis,\n mutators,\n lc,\n mutationClientID,\n formatVersion,\n zeroData,\n );\n return {result: await tx.putCommit(), zeroData: next};\n}\n\nexport async function rebaseMutationAndCommit(\n mutation: Commit<LocalMeta>,\n dagWrite: DagWrite,\n basis: Hash,\n headName: string,\n mutators: MutatorDefs,\n lc: LogContext,\n // TODO(greg): mutationClientID can be retrieved from mutation if LocalMeta\n // is a LocalMetaDD31. As part of DD31 cleanup we can remove this arg.\n mutationClientID: ClientID,\n formatVersion: FormatVersion,\n zeroData: ZeroTxData | undefined,\n): Promise<RebaseResult<Hash>> {\n const {result: dbWrite, zeroData: next} = await rebaseMutation(\n mutation,\n dagWrite,\n basis,\n mutators,\n lc,\n mutationClientID,\n formatVersion,\n zeroData,\n );\n return {result: await dbWrite.commit(headName), zeroData: next};\n}\n"],"mappings":";;;;;AAqCA,eAAe,eACb,UACA,UACA,WACA,UACA,IACA,kBACA,eACA,UAC8B;CAC9B,MAAM,YAAY,SAAS;CAC3B,MAAM,OAAO,UAAU;CACvB,IAAI,gBAAgB,SAAS,GAC3B,OACE,UAAU,aAAa,kBACvB,mDACF;CAEF,MAAM,mBAAmB,SAAS;CAClC,IAAI,CAAC,kBASH,GAAG,QAAQ,iCAAiC,MAAM;CAEpD,MAAM,cACJ,qBACC,YAAY,CAEb;CAEF,MAAM,OAAO,UAAU;CAGvB,MAAM,iBAAiB,OAAM,MADH,eAAe,WAAW,QAAQ,GACnB,kBACvC,kBACA,QACF;CACA,IAAI,mBAAmB,UAAU,YAC/B,MAAM,IAAI,MACR,uCAAuC,UAAU,WAAW,UAAU,eAAe,uBAAuB,iBAAiB,gBAAgB,MAC/I;CAGF,IAAI,iBAAiB,GACnB,oBAAoB,SAAS;CAG/B,MAAM,iBACJ,cACE,WACA,MACA,MACA,SAAS,MAAM,MACf,UACA,UAAU,WACV,kBACA,aACF;CAEF,MAAM,UAAU,MAAM,SAAS;CAI/B,MAAM,SAAS,UAAU,KAAK;CAE9B,MAAM,KAAK,IAAI,qBACb,kBACA,MAAM,QAAQ,cAAc,GAC5B,UACA,QACA,SACA,EACF;CAEA,IAAI;EACF,MAAM,YAAY,IAAI,IAAI;CAC5B,SAAS,GAAG;EAmBV,GAAG,OAAO,qBAAqB,KAAK,oCAAoC,CAAC;EACzE,OAAO;GAAC,QAAQ,MAAM,SAAS;GAAG;EAAQ;CAC5C;CAEA,OAAO;EAAC,QAAQ;EAAS,UAAU;CAAM;AAC3C;AAEA,eAAsB,2BACpB,UACA,UACA,OACA,UACA,IAGA,kBACA,eACA,UACqC;CACrC,MAAM,EAAC,QAAQ,IAAI,UAAU,SAAQ,MAAM,eACzC,UACA,UACA,OACA,UACA,IACA,kBACA,eACA,QACF;CACA,OAAO;EAAC,QAAQ,MAAM,GAAG,UAAU;EAAG,UAAU;CAAI;AACtD;AAEA,eAAsB,wBACpB,UACA,UACA,OACA,UACA,UACA,IAGA,kBACA,eACA,UAC6B;CAC7B,MAAM,EAAC,QAAQ,SAAS,UAAU,SAAQ,MAAM,eAC9C,UACA,UACA,OACA,UACA,IACA,kBACA,eACA,QACF;CACA,OAAO;EAAC,QAAQ,MAAM,QAAQ,OAAO,QAAQ;EAAG,UAAU;CAAI;AAChE"}
@@ -1,6 +1,6 @@
1
1
  import type { LogContext, LogLevel, LogSink } from '@rocicorp/logger';
2
2
  import type { Store } from '../dag/store.ts';
3
- import type { CreateStore, StoreProvider } from '../kv/store.ts';
3
+ import type { CreateStore, DropStore, StoreProvider } from '../kv/store.ts';
4
4
  import type { OnClientsDeleted } from './clients.ts';
5
5
  import { IDBDatabasesStore } from './idb-databases-store.ts';
6
6
  /**
@@ -11,12 +11,18 @@ export declare const COLLECT_IDB_INTERVAL: number;
11
11
  * We delay the initial collection to prevent doing it at startup.
12
12
  */
13
13
  export declare const INITIAL_COLLECT_IDB_DELAY: number;
14
- export declare function initCollectIDBDatabases(idbDatabasesStore: IDBDatabasesStore, kvStoreProvider: StoreProvider, collectInterval: number, initialCollectDelay: number, maxAge: number, enableMutationRecovery: boolean, onClientsDeleted: OnClientsDeleted, lc: LogContext, signal: AbortSignal): void;
14
+ export declare function initCollectIDBDatabases(idbDatabasesStore: IDBDatabasesStore, kvStoreProvider: StoreProvider, collectInterval: number, initialCollectDelay: number, maxAge: number, enableMutationRecovery: boolean, onClientsDeleted: OnClientsDeleted, lc: LogContext, signal: AbortSignal, newDagStore?: NewDagStore): void;
15
15
  /**
16
16
  * Collects IDB databases that are no longer needed.
17
17
  */
18
- export declare function collectIDBDatabases(idbDatabasesStore: IDBDatabasesStore, now: number, maxAge: number, kvStoreProvider: StoreProvider, enableMutationRecovery: boolean, onClientsDeleted: OnClientsDeleted, newDagStore?: typeof defaultNewDagStore): Promise<void>;
19
- declare function defaultNewDagStore(name: string, kvCreateStore: CreateStore): Store;
18
+ export declare function collectIDBDatabases(idbDatabasesStore: IDBDatabasesStore, now: number, maxAge: number, kvStoreProvider: StoreProvider, enableMutationRecovery: boolean, onClientsDeleted: OnClientsDeleted, newDagStore?: NewDagStore): Promise<void>;
19
+ export declare function dropDatabaseInternal(name: string, idbDatabasesStore: IDBDatabasesStore, kvDropStore: DropStore): Promise<void>;
20
+ /**
21
+ * Creates the dag store used to inspect and update a database during
22
+ * collection. Callers can override it to install store hooks, for example so
23
+ * a corrupt ref count found while writing deleted clients is recovered from.
24
+ */
25
+ export type NewDagStore = (name: string, kvCreateStore: CreateStore) => Store;
20
26
  /**
21
27
  * Options for `dropDatabase` and `dropAllDatabases`.
22
28
  */
@@ -79,5 +85,4 @@ export declare function deleteAllReplicacheData(opts?: DropDatabaseOptions): Pro
79
85
  dropped: string[];
80
86
  errors: unknown[];
81
87
  }>;
82
- export {};
83
88
  //# sourceMappingURL=collect-idb-databases.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"collect-idb-databases.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/persist/collect-idb-databases.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAKpE,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,iBAAiB,CAAC;AAY3C,OAAO,KAA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AM,EAAE,IAAI,CAAC,EAAE,mBAAmB,iBAU5E;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC;IAC1E,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,EAAE,OAAO,EAAE,CAAC;CACnB,CAAC,CASD;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,CAAC,EAAE,mBAAmB;aArBvD,MAAM,EAAE;YACT,OAAO,EAAE;GAsBlB"}
1
+ {"version":3,"file":"collect-idb-databases.d.ts","sourceRoot":"","sources":["../../../../../replicache/src/persist/collect-idb-databases.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAC,MAAM,kBAAkB,CAAC;AAMpE,OAAO,KAAK,EAAC,KAAK,EAAC,MAAM,iBAAiB,CAAC;AAY3C,OAAO,KAAK,EAAC,WAAW,EAAE,SAAS,EAAE,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAO1E,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAGnD,OAAO,EAAC,iBAAiB,EAAC,MAAM,0BAA0B,CAAC;AAE3D;;GAEG;AACH,eAAO,MAAM,oBAAoB,QAAsB,CAAC;AAExD;;GAEG;AACH,eAAO,MAAM,yBAAyB,QAAgB,CAAC;AAEvD,wBAAgB,uBAAuB,CACrC,iBAAiB,EAAE,iBAAiB,EACpC,eAAe,EAAE,aAAa,EAC9B,eAAe,EAAE,MAAM,EACvB,mBAAmB,EAAE,MAAM,EAC3B,MAAM,EAAE,MAAM,EACd,sBAAsB,EAAE,OAAO,EAC/B,gBAAgB,EAAE,gBAAgB,EAClC,EAAE,EAAE,UAAU,EACd,MAAM,EAAE,WAAW,EACnB,WAAW,GAAE,WAAgC,GAC5C,IAAI,CAyBN;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CACvC,iBAAiB,EAAE,i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