@rocicorp/zero 1.12.0-canary.8 → 1.12.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 (516) hide show
  1. package/out/replicache/src/btree/node.d.ts +0 -2
  2. package/out/replicache/src/btree/node.d.ts.map +1 -1
  3. package/out/replicache/src/btree/node.js.map +1 -1
  4. package/out/replicache/src/connection-loop.d.ts.map +1 -1
  5. package/out/replicache/src/connection-loop.js +1 -1
  6. package/out/replicache/src/connection-loop.js.map +1 -1
  7. package/out/replicache/src/db/commit.d.ts +0 -1
  8. package/out/replicache/src/db/commit.d.ts.map +1 -1
  9. package/out/replicache/src/db/commit.js +1 -1
  10. package/out/replicache/src/db/commit.js.map +1 -1
  11. package/out/replicache/src/db/write.d.ts +0 -2
  12. package/out/replicache/src/db/write.d.ts.map +1 -1
  13. package/out/replicache/src/db/write.js +1 -1
  14. package/out/replicache/src/db/write.js.map +1 -1
  15. package/out/replicache/src/deleted-clients.d.ts +0 -2
  16. package/out/replicache/src/deleted-clients.d.ts.map +1 -1
  17. package/out/replicache/src/deleted-clients.js +1 -1
  18. package/out/replicache/src/deleted-clients.js.map +1 -1
  19. package/out/replicache/src/frozen-json.d.ts +0 -1
  20. package/out/replicache/src/frozen-json.d.ts.map +1 -1
  21. package/out/replicache/src/frozen-json.js.map +1 -1
  22. package/out/replicache/src/index-defs.d.ts +0 -2
  23. package/out/replicache/src/index-defs.d.ts.map +1 -1
  24. package/out/replicache/src/index-defs.js +1 -1
  25. package/out/replicache/src/index-defs.js.map +1 -1
  26. package/out/replicache/src/kv/expo-sqlite/store.d.ts +0 -1
  27. package/out/replicache/src/kv/expo-sqlite/store.d.ts.map +1 -1
  28. package/out/replicache/src/kv/expo-sqlite/store.js +1 -1
  29. package/out/replicache/src/kv/expo-sqlite/store.js.map +1 -1
  30. package/out/replicache/src/kv/idb-store.js +1 -1
  31. package/out/replicache/src/kv/idb-store.js.map +1 -1
  32. package/out/replicache/src/kv/throw-if-closed.d.ts +0 -3
  33. package/out/replicache/src/kv/throw-if-closed.d.ts.map +1 -1
  34. package/out/replicache/src/kv/throw-if-closed.js.map +1 -1
  35. package/out/replicache/src/persist/client-group-gc.d.ts +0 -7
  36. package/out/replicache/src/persist/client-group-gc.d.ts.map +1 -1
  37. package/out/replicache/src/persist/client-group-gc.js +1 -1
  38. package/out/replicache/src/persist/client-group-gc.js.map +1 -1
  39. package/out/replicache/src/process-scheduler.js +1 -1
  40. package/out/replicache/src/process-scheduler.js.map +1 -1
  41. package/out/replicache/src/replicache-impl.d.ts +2 -7
  42. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  43. package/out/replicache/src/replicache-impl.js +1 -1
  44. package/out/replicache/src/replicache-impl.js.map +1 -1
  45. package/out/replicache/src/sync/patch.d.ts +0 -5
  46. package/out/replicache/src/sync/patch.d.ts.map +1 -1
  47. package/out/replicache/src/sync/patch.js +1 -1
  48. package/out/replicache/src/sync/patch.js.map +1 -1
  49. package/out/replicache/src/sync/pull.d.ts +0 -1
  50. package/out/replicache/src/sync/pull.d.ts.map +1 -1
  51. package/out/replicache/src/sync/pull.js.map +1 -1
  52. package/out/replicache/src/sync/push.d.ts +2 -1
  53. package/out/replicache/src/sync/push.d.ts.map +1 -1
  54. package/out/replicache/src/sync/push.js.map +1 -1
  55. package/out/replicache/src/types.d.ts +2 -1
  56. package/out/replicache/src/types.d.ts.map +1 -1
  57. package/out/shared/src/asserts.d.ts +0 -5
  58. package/out/shared/src/asserts.d.ts.map +1 -1
  59. package/out/shared/src/asserts.js +1 -1
  60. package/out/shared/src/asserts.js.map +1 -1
  61. package/out/shared/src/browser-env.d.ts +0 -17
  62. package/out/shared/src/browser-env.d.ts.map +1 -1
  63. package/out/shared/src/browser-env.js.map +1 -1
  64. package/out/shared/src/deep-clone.d.ts +0 -1
  65. package/out/shared/src/deep-clone.d.ts.map +1 -1
  66. package/out/shared/src/deep-clone.js +1 -1
  67. package/out/shared/src/deep-clone.js.map +1 -1
  68. package/out/shared/src/document-visible.js +1 -1
  69. package/out/shared/src/document-visible.js.map +1 -1
  70. package/out/shared/src/expand.d.ts +0 -3
  71. package/out/shared/src/expand.d.ts.map +1 -1
  72. package/out/shared/src/immutable.d.ts +1 -1
  73. package/out/shared/src/immutable.d.ts.map +1 -1
  74. package/out/shared/src/iterables.d.ts +0 -1
  75. package/out/shared/src/iterables.d.ts.map +1 -1
  76. package/out/shared/src/iterables.js.map +1 -1
  77. package/out/shared/src/logging-test-utils.js +1 -1
  78. package/out/shared/src/logging-test-utils.js.map +1 -1
  79. package/out/shared/src/options-types.d.ts +1 -1
  80. package/out/shared/src/options-types.d.ts.map +1 -1
  81. package/out/shared/src/options.d.ts +2 -3
  82. package/out/shared/src/options.d.ts.map +1 -1
  83. package/out/shared/src/options.js +1 -1
  84. package/out/shared/src/options.js.map +1 -1
  85. package/out/shared/src/promise-race.js +1 -1
  86. package/out/shared/src/promise-race.js.map +1 -1
  87. package/out/shared/src/queue.js +1 -1
  88. package/out/shared/src/queue.js.map +1 -1
  89. package/out/shared/src/resolved-promises.d.ts +0 -4
  90. package/out/shared/src/resolved-promises.d.ts.map +1 -1
  91. package/out/shared/src/resolved-promises.js +0 -1
  92. package/out/shared/src/resolved-promises.js.map +1 -1
  93. package/out/shared/src/resolver.d.ts +9 -0
  94. package/out/shared/src/resolver.d.ts.map +1 -0
  95. package/out/shared/src/resolver.js +6 -0
  96. package/out/shared/src/resolver.js.map +1 -0
  97. package/out/shared/src/sleep.js +1 -1
  98. package/out/shared/src/sleep.js.map +1 -1
  99. package/out/shared/src/tdigest.d.ts +0 -1
  100. package/out/shared/src/tdigest.d.ts.map +1 -1
  101. package/out/shared/src/tdigest.js.map +1 -1
  102. package/out/z2s/src/sql.d.ts +1 -5
  103. package/out/z2s/src/sql.d.ts.map +1 -1
  104. package/out/z2s/src/sql.js.map +1 -1
  105. package/out/zero/package.js +2 -3
  106. package/out/zero/package.js.map +1 -1
  107. package/out/zero/src/zero-cache-dev.js +1 -1
  108. package/out/zero/src/zero-cache-dev.js.map +1 -1
  109. package/out/zero-cache/src/config/zero-config.d.ts +14 -11
  110. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  111. package/out/zero-cache/src/config/zero-config.js +23 -0
  112. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  113. package/out/zero-cache/src/db/migration-lite.js.map +1 -1
  114. package/out/zero-cache/src/db/migration.js +1 -1
  115. package/out/zero-cache/src/db/migration.js.map +1 -1
  116. package/out/zero-cache/src/db/pg-copy-binary.js +1 -1
  117. package/out/zero-cache/src/db/pg-copy-binary.js.map +1 -1
  118. package/out/zero-cache/src/db/pg-type-parser.js.map +1 -1
  119. package/out/zero-cache/src/db/specs.d.ts +3 -4
  120. package/out/zero-cache/src/db/specs.d.ts.map +1 -1
  121. package/out/zero-cache/src/db/specs.js +5 -7
  122. package/out/zero-cache/src/db/specs.js.map +1 -1
  123. package/out/zero-cache/src/db/transaction-pool.js +2 -2
  124. package/out/zero-cache/src/db/transaction-pool.js.map +1 -1
  125. package/out/zero-cache/src/observability/events.js +1 -1
  126. package/out/zero-cache/src/observability/events.js.map +1 -1
  127. package/out/zero-cache/src/server/change-streamer.js +1 -1
  128. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  129. package/out/zero-cache/src/server/main.js +1 -1
  130. package/out/zero-cache/src/server/main.js.map +1 -1
  131. package/out/zero-cache/src/server/runner/run-worker.js +1 -1
  132. package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
  133. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +1 -1
  134. package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
  135. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +1 -1
  136. package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
  137. package/out/zero-cache/src/services/change-source/common/replica-restore.js.map +1 -1
  138. package/out/zero-cache/src/services/change-source/custom/change-source.js +1 -1
  139. package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
  140. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
  141. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  142. package/out/zero-cache/src/services/change-source/pg/change-source.js +1 -1
  143. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  144. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +1 -1
  145. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  146. package/out/zero-cache/src/services/change-source/pg/lsn.d.ts +0 -2
  147. package/out/zero-cache/src/services/change-source/pg/lsn.d.ts.map +1 -1
  148. package/out/zero-cache/src/services/change-source/pg/lsn.js.map +1 -1
  149. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +1 -1
  150. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  151. package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts +0 -12
  152. package/out/zero-cache/src/services/change-source/pg/schema/ddl.d.ts.map +1 -1
  153. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +5 -3
  154. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js.map +1 -1
  155. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts +1 -1
  156. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.d.ts.map +1 -1
  157. package/out/zero-cache/src/services/change-source/pg/schema/index-predicate.js.map +1 -1
  158. package/out/zero-cache/src/services/change-source/pg/schema/validation.d.ts +0 -1
  159. package/out/zero-cache/src/services/change-source/pg/schema/validation.d.ts.map +1 -1
  160. package/out/zero-cache/src/services/change-source/pg/schema/validation.js.map +1 -1
  161. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +1 -1
  162. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  163. package/out/zero-cache/src/services/change-streamer/broadcast.js +1 -1
  164. package/out/zero-cache/src/services/change-streamer/broadcast.js.map +1 -1
  165. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +1 -1
  166. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  167. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +1 -1
  168. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  169. package/out/zero-cache/src/services/change-streamer/change-streamer.js +1 -1
  170. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  171. package/out/zero-cache/src/services/change-streamer/drain-tracker.js +1 -1
  172. package/out/zero-cache/src/services/change-streamer/drain-tracker.js.map +1 -1
  173. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js +2 -2
  174. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js.map +1 -1
  175. package/out/zero-cache/src/services/change-streamer/restore-admission.js +2 -2
  176. package/out/zero-cache/src/services/change-streamer/restore-admission.js.map +1 -1
  177. package/out/zero-cache/src/services/change-streamer/schema/tables.js +1 -1
  178. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  179. package/out/zero-cache/src/services/change-streamer/snapshot-message.js +2 -3
  180. package/out/zero-cache/src/services/change-streamer/snapshot-message.js.map +1 -1
  181. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js +2 -2
  182. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-catchup.js.map +1 -1
  183. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-read-router.js.map +1 -1
  184. package/out/zero-cache/src/services/change-streamer/storer.js +1 -1
  185. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  186. package/out/zero-cache/src/services/change-streamer/subscribe.js +1 -1
  187. package/out/zero-cache/src/services/change-streamer/subscribe.js.map +1 -1
  188. package/out/zero-cache/src/services/change-streamer/subscriber.js +1 -1
  189. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  190. package/out/zero-cache/src/services/life-cycle.js +1 -1
  191. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  192. package/out/zero-cache/src/services/litestream/commands.js +1 -1
  193. package/out/zero-cache/src/services/litestream/commands.js.map +1 -1
  194. package/out/zero-cache/src/services/litestream/metrics.js.map +1 -1
  195. package/out/zero-cache/src/services/mutagen/mutagen.js +2 -2
  196. package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
  197. package/out/zero-cache/src/services/plannerz.d.ts +4 -12
  198. package/out/zero-cache/src/services/plannerz.d.ts.map +1 -1
  199. package/out/zero-cache/src/services/plannerz.js +1 -3
  200. package/out/zero-cache/src/services/plannerz.js.map +1 -1
  201. package/out/zero-cache/src/services/replicator/change-log-db.js +1 -1
  202. package/out/zero-cache/src/services/replicator/change-log-db.js.map +1 -1
  203. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js +1 -1
  204. package/out/zero-cache/src/services/replicator/change-log-stream-writer.js.map +1 -1
  205. package/out/zero-cache/src/services/replicator/drain-handler.js +1 -1
  206. package/out/zero-cache/src/services/replicator/drain-handler.js.map +1 -1
  207. package/out/zero-cache/src/services/replicator/reporter/recorder.js +1 -1
  208. package/out/zero-cache/src/services/replicator/reporter/recorder.js.map +1 -1
  209. package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts +2 -11
  210. package/out/zero-cache/src/services/replicator/reporter/report-schema.d.ts.map +1 -1
  211. package/out/zero-cache/src/services/replicator/reporter/report-schema.js +1 -1
  212. package/out/zero-cache/src/services/replicator/reporter/report-schema.js.map +1 -1
  213. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts +0 -10
  214. package/out/zero-cache/src/services/replicator/schema/change-log.d.ts.map +1 -1
  215. package/out/zero-cache/src/services/replicator/schema/change-log.js.map +1 -1
  216. package/out/zero-cache/src/services/replicator/schema/column-metadata.d.ts +0 -7
  217. package/out/zero-cache/src/services/replicator/schema/column-metadata.d.ts.map +1 -1
  218. package/out/zero-cache/src/services/replicator/schema/column-metadata.js +1 -1
  219. package/out/zero-cache/src/services/replicator/schema/column-metadata.js.map +1 -1
  220. package/out/zero-cache/src/services/replicator/write-worker-client.js +1 -1
  221. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  222. package/out/zero-cache/src/services/running-state.js +1 -1
  223. package/out/zero-cache/src/services/running-state.js.map +1 -1
  224. package/out/zero-cache/src/services/view-syncer/client-handler.js.map +1 -1
  225. package/out/zero-cache/src/services/view-syncer/connection-context-manager.js.map +1 -1
  226. package/out/zero-cache/src/services/view-syncer/cvr-store.js +1 -1
  227. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  228. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  229. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +1 -1
  230. package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
  231. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  232. package/out/zero-cache/src/services/view-syncer/query-stats.js.map +1 -1
  233. package/out/zero-cache/src/services/view-syncer/row-record-cache.js +1 -1
  234. package/out/zero-cache/src/services/view-syncer/row-record-cache.js.map +1 -1
  235. package/out/zero-cache/src/services/view-syncer/schema/cvr.js +1 -1
  236. package/out/zero-cache/src/services/view-syncer/schema/cvr.js.map +1 -1
  237. package/out/zero-cache/src/services/view-syncer/schema/types.js +3 -17
  238. package/out/zero-cache/src/services/view-syncer/schema/types.js.map +1 -1
  239. package/out/zero-cache/src/services/view-syncer/snapshotter.js +1 -1
  240. package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
  241. package/out/zero-cache/src/services/view-syncer/view-syncer.js +10 -10
  242. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  243. package/out/zero-cache/src/types/lite.d.ts +0 -1
  244. package/out/zero-cache/src/types/lite.d.ts.map +1 -1
  245. package/out/zero-cache/src/types/lite.js +2 -2
  246. package/out/zero-cache/src/types/lite.js.map +1 -1
  247. package/out/zero-cache/src/types/pg-types.d.ts +0 -40
  248. package/out/zero-cache/src/types/pg-types.d.ts.map +1 -1
  249. package/out/zero-cache/src/types/pg-types.js.map +1 -1
  250. package/out/zero-cache/src/types/poke-chunk.js +1 -1
  251. package/out/zero-cache/src/types/poke-chunk.js.map +1 -1
  252. package/out/zero-cache/src/types/row-key.d.ts +0 -5
  253. package/out/zero-cache/src/types/row-key.d.ts.map +1 -1
  254. package/out/zero-cache/src/types/row-key.js.map +1 -1
  255. package/out/zero-cache/src/types/streams.js +1 -1
  256. package/out/zero-cache/src/types/streams.js.map +1 -1
  257. package/out/zero-cache/src/types/subscription.js +1 -1
  258. package/out/zero-cache/src/types/subscription.js.map +1 -1
  259. package/out/zero-cache/src/types/websocket-handoff.d.ts +0 -6
  260. package/out/zero-cache/src/types/websocket-handoff.d.ts.map +1 -1
  261. package/out/zero-cache/src/types/websocket-handoff.js +8 -2
  262. package/out/zero-cache/src/types/websocket-handoff.js.map +1 -1
  263. package/out/zero-cache/src/types/ws.d.ts +2 -1
  264. package/out/zero-cache/src/types/ws.d.ts.map +1 -1
  265. package/out/zero-cache/src/types/ws.js +1 -1
  266. package/out/zero-cache/src/types/ws.js.map +1 -1
  267. package/out/zero-cache/src/workers/connection.js +4 -1
  268. package/out/zero-cache/src/workers/connection.js.map +1 -1
  269. package/out/zero-cache/src/workers/mutator.js +1 -1
  270. package/out/zero-cache/src/workers/mutator.js.map +1 -1
  271. package/out/zero-cache/src/workers/syncer.js +36 -2
  272. package/out/zero-cache/src/workers/syncer.js.map +1 -1
  273. package/out/zero-client/src/client/active-clients-manager.d.ts.map +1 -1
  274. package/out/zero-client/src/client/active-clients-manager.js +1 -1
  275. package/out/zero-client/src/client/active-clients-manager.js.map +1 -1
  276. package/out/zero-client/src/client/connection-manager.js +1 -1
  277. package/out/zero-client/src/client/connection-manager.js.map +1 -1
  278. package/out/zero-client/src/client/crud.d.ts +2 -8
  279. package/out/zero-client/src/client/crud.d.ts.map +1 -1
  280. package/out/zero-client/src/client/crud.js +1 -1
  281. package/out/zero-client/src/client/crud.js.map +1 -1
  282. package/out/zero-client/src/client/custom.d.ts +0 -2
  283. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  284. package/out/zero-client/src/client/custom.js +1 -1
  285. package/out/zero-client/src/client/custom.js.map +1 -1
  286. package/out/zero-client/src/client/enable-analytics.d.ts +0 -1
  287. package/out/zero-client/src/client/enable-analytics.d.ts.map +1 -1
  288. package/out/zero-client/src/client/enable-analytics.js +2 -1
  289. package/out/zero-client/src/client/enable-analytics.js.map +1 -1
  290. package/out/zero-client/src/client/error.d.ts +4 -5
  291. package/out/zero-client/src/client/error.d.ts.map +1 -1
  292. package/out/zero-client/src/client/error.js.map +1 -1
  293. package/out/zero-client/src/client/keys.d.ts +0 -1
  294. package/out/zero-client/src/client/keys.d.ts.map +1 -1
  295. package/out/zero-client/src/client/keys.js +4 -2
  296. package/out/zero-client/src/client/keys.js.map +1 -1
  297. package/out/zero-client/src/client/mutation-tracker.d.ts.map +1 -1
  298. package/out/zero-client/src/client/mutation-tracker.js +1 -1
  299. package/out/zero-client/src/client/mutation-tracker.js.map +1 -1
  300. package/out/zero-client/src/client/options.d.ts +0 -4
  301. package/out/zero-client/src/client/options.d.ts.map +1 -1
  302. package/out/zero-client/src/client/options.js.map +1 -1
  303. package/out/zero-client/src/client/replicache-types.d.ts +3 -2
  304. package/out/zero-client/src/client/replicache-types.d.ts.map +1 -1
  305. package/out/zero-client/src/client/version.js +1 -1
  306. package/out/zero-client/src/client/zero-poke-handler.d.ts +1 -2
  307. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  308. package/out/zero-client/src/client/zero-poke-handler.js +1 -1
  309. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
  310. package/out/zero-client/src/client/zero.d.ts +1 -5
  311. package/out/zero-client/src/client/zero.d.ts.map +1 -1
  312. package/out/zero-client/src/client/zero.js +1 -1
  313. package/out/zero-client/src/client/zero.js.map +1 -1
  314. package/out/zero-client/src/types/client-state.d.ts +0 -6
  315. package/out/zero-client/src/types/client-state.d.ts.map +1 -1
  316. package/out/zero-permissions/src/permissions.d.ts +1 -12
  317. package/out/zero-permissions/src/permissions.d.ts.map +1 -1
  318. package/out/zero-permissions/src/permissions.js +1 -2
  319. package/out/zero-permissions/src/permissions.js.map +1 -1
  320. package/out/zero-protocol/src/ast.d.ts +0 -4
  321. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  322. package/out/zero-protocol/src/ast.js +1 -2
  323. package/out/zero-protocol/src/ast.js.map +1 -1
  324. package/out/zero-protocol/src/change-desired-queries.d.ts +0 -20
  325. package/out/zero-protocol/src/change-desired-queries.d.ts.map +1 -1
  326. package/out/zero-protocol/src/change-desired-queries.js.map +1 -1
  327. package/out/zero-protocol/src/client-schema.d.ts +2 -5
  328. package/out/zero-protocol/src/client-schema.d.ts.map +1 -1
  329. package/out/zero-protocol/src/client-schema.js +1 -1
  330. package/out/zero-protocol/src/client-schema.js.map +1 -1
  331. package/out/zero-protocol/src/close-connection.d.ts +0 -8
  332. package/out/zero-protocol/src/close-connection.d.ts.map +1 -1
  333. package/out/zero-protocol/src/close-connection.js +1 -1
  334. package/out/zero-protocol/src/close-connection.js.map +1 -1
  335. package/out/zero-protocol/src/connect.d.ts +0 -12
  336. package/out/zero-protocol/src/connect.d.ts.map +1 -1
  337. package/out/zero-protocol/src/connect.js +1 -1
  338. package/out/zero-protocol/src/connect.js.map +1 -1
  339. package/out/zero-protocol/src/custom-queries.d.ts +3 -17
  340. package/out/zero-protocol/src/custom-queries.d.ts.map +1 -1
  341. package/out/zero-protocol/src/custom-queries.js +1 -1
  342. package/out/zero-protocol/src/custom-queries.js.map +1 -1
  343. package/out/zero-protocol/src/data.d.ts +0 -1
  344. package/out/zero-protocol/src/data.d.ts.map +1 -1
  345. package/out/zero-protocol/src/data.js +1 -1
  346. package/out/zero-protocol/src/data.js.map +1 -1
  347. package/out/zero-protocol/src/down.d.ts +27 -27
  348. package/out/zero-protocol/src/inspect-down.d.ts +82 -84
  349. package/out/zero-protocol/src/inspect-down.d.ts.map +1 -1
  350. package/out/zero-protocol/src/inspect-down.js +1 -1
  351. package/out/zero-protocol/src/inspect-down.js.map +1 -1
  352. package/out/zero-protocol/src/inspect-up.d.ts +0 -28
  353. package/out/zero-protocol/src/inspect-up.d.ts.map +1 -1
  354. package/out/zero-protocol/src/inspect-up.js +1 -1
  355. package/out/zero-protocol/src/inspect-up.js.map +1 -1
  356. package/out/zero-protocol/src/mutate-server.d.ts +0 -21
  357. package/out/zero-protocol/src/mutate-server.d.ts.map +1 -1
  358. package/out/zero-protocol/src/mutate-server.js +1 -1
  359. package/out/zero-protocol/src/mutate-server.js.map +1 -1
  360. package/out/zero-protocol/src/mutation-type-enum.d.ts +0 -1
  361. package/out/zero-protocol/src/mutation-type-enum.d.ts.map +1 -1
  362. package/out/zero-protocol/src/mutation-type-enum.js.map +1 -1
  363. package/out/zero-protocol/src/mutation.d.ts +2 -4
  364. package/out/zero-protocol/src/mutation.d.ts.map +1 -1
  365. package/out/zero-protocol/src/mutation.js +1 -1
  366. package/out/zero-protocol/src/mutation.js.map +1 -1
  367. package/out/zero-protocol/src/mutations-patch.d.ts +0 -33
  368. package/out/zero-protocol/src/mutations-patch.d.ts.map +1 -1
  369. package/out/zero-protocol/src/mutations-patch.js +1 -1
  370. package/out/zero-protocol/src/mutations-patch.js.map +1 -1
  371. package/out/zero-protocol/src/ping.d.ts +0 -2
  372. package/out/zero-protocol/src/ping.d.ts.map +1 -1
  373. package/out/zero-protocol/src/ping.js +1 -1
  374. package/out/zero-protocol/src/ping.js.map +1 -1
  375. package/out/zero-protocol/src/poke.d.ts +3 -2
  376. package/out/zero-protocol/src/poke.d.ts.map +1 -1
  377. package/out/zero-protocol/src/poke.js +1 -1
  378. package/out/zero-protocol/src/poke.js.map +1 -1
  379. package/out/zero-protocol/src/pong.d.ts +0 -2
  380. package/out/zero-protocol/src/pong.d.ts.map +1 -1
  381. package/out/zero-protocol/src/pong.js +1 -1
  382. package/out/zero-protocol/src/pong.js.map +1 -1
  383. package/out/zero-protocol/src/pull.d.ts +2 -7
  384. package/out/zero-protocol/src/pull.d.ts.map +1 -1
  385. package/out/zero-protocol/src/pull.js +1 -1
  386. package/out/zero-protocol/src/pull.js.map +1 -1
  387. package/out/zero-protocol/src/push.d.ts +1 -3
  388. package/out/zero-protocol/src/push.d.ts.map +1 -1
  389. package/out/zero-protocol/src/push.js +1 -1
  390. package/out/zero-protocol/src/push.js.map +1 -1
  391. package/out/zero-protocol/src/queries-patch.d.ts +0 -16
  392. package/out/zero-protocol/src/queries-patch.d.ts.map +1 -1
  393. package/out/zero-protocol/src/queries-patch.js +1 -1
  394. package/out/zero-protocol/src/queries-patch.js.map +1 -1
  395. package/out/zero-protocol/src/query-server.d.ts +2 -41
  396. package/out/zero-protocol/src/query-server.d.ts.map +1 -1
  397. package/out/zero-protocol/src/query-server.js +1 -1
  398. package/out/zero-protocol/src/query-server.js.map +1 -1
  399. package/out/zero-protocol/src/up.d.ts +42 -42
  400. package/out/zero-protocol/src/version.d.ts +0 -1
  401. package/out/zero-protocol/src/version.d.ts.map +1 -1
  402. package/out/zero-protocol/src/version.js.map +1 -1
  403. package/out/zero-react/src/use-query.js +1 -1
  404. package/out/zero-react/src/use-query.js.map +1 -1
  405. package/out/zero-schema/src/builder/relationship-builder.d.ts +0 -2
  406. package/out/zero-schema/src/builder/relationship-builder.d.ts.map +1 -1
  407. package/out/zero-schema/src/builder/relationship-builder.js.map +1 -1
  408. package/out/zero-schema/src/builder/table-builder.d.ts +1 -1
  409. package/out/zero-schema/src/builder/table-builder.d.ts.map +1 -1
  410. package/out/zero-schema/src/builder/table-builder.js +1 -1
  411. package/out/zero-schema/src/builder/table-builder.js.map +1 -1
  412. package/out/zero-schema/src/schema-config.d.ts +0 -1
  413. package/out/zero-schema/src/schema-config.d.ts.map +1 -1
  414. package/out/zero-schema/src/schema-config.js +1 -1
  415. package/out/zero-schema/src/schema-config.js.map +1 -1
  416. package/out/zero-schema/src/table-schema.d.ts +3 -11
  417. package/out/zero-schema/src/table-schema.d.ts.map +1 -1
  418. package/out/zero-server/src/queries/process-queries.d.ts +2 -4
  419. package/out/zero-server/src/queries/process-queries.d.ts.map +1 -1
  420. package/out/zero-server/src/queries/process-queries.js.map +1 -1
  421. package/out/zero-solid/src/solid-view.d.ts +0 -2
  422. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  423. package/out/zero-solid/src/solid-view.js +1 -1
  424. package/out/zero-solid/src/solid-view.js.map +1 -1
  425. package/out/zero-types/src/name-mapper.d.ts +2 -1
  426. package/out/zero-types/src/name-mapper.d.ts.map +1 -1
  427. package/out/zero-types/src/name-mapper.js.map +1 -1
  428. package/out/zero-types/src/schema-value.d.ts +3 -2
  429. package/out/zero-types/src/schema-value.d.ts.map +1 -1
  430. package/out/zero-types/src/schema.d.ts +1 -1
  431. package/out/zero-types/src/schema.d.ts.map +1 -1
  432. package/out/zql/src/builder/builder.d.ts +1 -11
  433. package/out/zql/src/builder/builder.d.ts.map +1 -1
  434. package/out/zql/src/builder/builder.js +1 -1
  435. package/out/zql/src/builder/builder.js.map +1 -1
  436. package/out/zql/src/builder/filter.d.ts +0 -1
  437. package/out/zql/src/builder/filter.d.ts.map +1 -1
  438. package/out/zql/src/builder/filter.js.map +1 -1
  439. package/out/zql/src/error.d.ts +0 -5
  440. package/out/zql/src/error.d.ts.map +1 -1
  441. package/out/zql/src/error.js.map +1 -1
  442. package/out/zql/src/ivm/constraint.d.ts +0 -7
  443. package/out/zql/src/ivm/constraint.d.ts.map +1 -1
  444. package/out/zql/src/ivm/constraint.js +1 -1
  445. package/out/zql/src/ivm/constraint.js.map +1 -1
  446. package/out/zql/src/ivm/data.d.ts +0 -1
  447. package/out/zql/src/ivm/data.d.ts.map +1 -1
  448. package/out/zql/src/ivm/data.js.map +1 -1
  449. package/out/zql/src/ivm/memory-source.d.ts +1 -2
  450. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  451. package/out/zql/src/ivm/memory-source.js +1 -1
  452. package/out/zql/src/ivm/memory-source.js.map +1 -1
  453. package/out/zql/src/ivm/stream.d.ts +0 -1
  454. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  455. package/out/zql/src/ivm/stream.js.map +1 -1
  456. package/out/zql/src/ivm/union-fan-in.d.ts +0 -1
  457. package/out/zql/src/ivm/union-fan-in.d.ts.map +1 -1
  458. package/out/zql/src/ivm/union-fan-in.js +1 -1
  459. package/out/zql/src/ivm/union-fan-in.js.map +1 -1
  460. package/out/zql/src/ivm/view-apply-change.d.ts +5 -35
  461. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  462. package/out/zql/src/ivm/view-apply-change.js +1 -1
  463. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  464. package/out/zql/src/mutate/crud.d.ts +1 -3
  465. package/out/zql/src/mutate/crud.d.ts.map +1 -1
  466. package/out/zql/src/mutate/crud.js +1 -1
  467. package/out/zql/src/mutate/crud.js.map +1 -1
  468. package/out/zql/src/mutate/mutator-registry.d.ts +1 -1
  469. package/out/zql/src/mutate/mutator-registry.d.ts.map +1 -1
  470. package/out/zql/src/mutate/mutator-registry.js.map +1 -1
  471. package/out/zql/src/mutate/mutator.d.ts +1 -1
  472. package/out/zql/src/mutate/mutator.d.ts.map +1 -1
  473. package/out/zql/src/mutate/mutator.js.map +1 -1
  474. package/out/zql/src/planner/planner-debug.d.ts +10 -9
  475. package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
  476. package/out/zql/src/planner/planner-debug.js.map +1 -1
  477. package/out/zql/src/planner/planner-node.d.ts +0 -1
  478. package/out/zql/src/planner/planner-node.d.ts.map +1 -1
  479. package/out/zql/src/planner/planner-node.js.map +1 -1
  480. package/out/zql/src/planner/planner-warnings.d.ts +3 -3
  481. package/out/zql/src/planner/planner-warnings.d.ts.map +1 -1
  482. package/out/zql/src/planner/planner-warnings.js.map +1 -1
  483. package/out/zql/src/query/expression.d.ts +0 -22
  484. package/out/zql/src/query/expression.d.ts.map +1 -1
  485. package/out/zql/src/query/expression.js +1 -1
  486. package/out/zql/src/query/expression.js.map +1 -1
  487. package/out/zql/src/query/query-delegate-base.d.ts +0 -7
  488. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  489. package/out/zql/src/query/query-delegate-base.js +2 -2
  490. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  491. package/out/zql/src/query/query-delegate.d.ts +1 -4
  492. package/out/zql/src/query/query-delegate.d.ts.map +1 -1
  493. package/out/zql/src/query/query-internals.d.ts +0 -3
  494. package/out/zql/src/query/query-internals.d.ts.map +1 -1
  495. package/out/zql/src/query/query-internals.js.map +1 -1
  496. package/out/zql/src/query/query-registry.d.ts +5 -5
  497. package/out/zql/src/query/query-registry.d.ts.map +1 -1
  498. package/out/zql/src/query/query-registry.js.map +1 -1
  499. package/out/zql/src/query/query.d.ts +2 -13
  500. package/out/zql/src/query/query.d.ts.map +1 -1
  501. package/out/zql/src/query/runnable-query-impl.d.ts +1 -19
  502. package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
  503. package/out/zql/src/query/runnable-query-impl.js +1 -1
  504. package/out/zql/src/query/runnable-query-impl.js.map +1 -1
  505. package/out/zql/src/query/ttl.d.ts +2 -3
  506. package/out/zql/src/query/ttl.d.ts.map +1 -1
  507. package/out/zql/src/query/ttl.js.map +1 -1
  508. package/out/zqlite/src/query-builder.d.ts +0 -2
  509. package/out/zqlite/src/query-builder.d.ts.map +1 -1
  510. package/out/zqlite/src/query-builder.js +1 -1
  511. package/out/zqlite/src/query-builder.js.map +1 -1
  512. package/out/zqlite/src/table-source.d.ts +0 -1
  513. package/out/zqlite/src/table-source.d.ts.map +1 -1
  514. package/out/zqlite/src/table-source.js +1 -1
  515. package/out/zqlite/src/table-source.js.map +1 -1
  516. package/package.json +2 -3
@@ -1,17 +1,5 @@
1
1
  import * as v from '../../../../../../shared/src/valita.ts';
2
2
  import { type ShardConfig } from '../../../../types/shards.ts';
3
- export declare const PROTOCOL_VERSION = 1;
4
- export declare const ddlEventSchema: v.ObjectType<Omit<{
5
- context: v.ObjectType<{
6
- query: v.Type<string>;
7
- }, v.Type<string>>;
8
- }, "event" | "newColumns" | "version"> & {
9
- version: v.Type<1 | 2>;
10
- event: v.ObjectType<{
11
- tag: v.Type<string>;
12
- }, undefined>;
13
- newColumns: v.Optional<Record<string, number[]> | null>;
14
- }, undefined>;
15
3
  /**
16
4
  * A {@link DdlStartEvent} message is emitted before every DDL event,
17
5
  * containing the command `tag`.
@@ -1 +1 @@
1
- {"version":3,"file":"ddl.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/ddl.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,wCAAwC,CAAC;AAC5D,OAAO,EAAiB,KAAK,WAAW,EAAC,MAAM,6BAA6B,CAAC;AAU7E,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAkBlC,eAAO,MAAM,cAAc;;;;;;;;;;aAgBzB,CAAC;AAEH;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAc9B,CAAC;AAEH,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAC;AAEhE;;;;GAIG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAU/B,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,yBAAyB;;;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eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eAIlC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,sBAAsB,CAAC,CAAC;AAYtE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,WAAW,EAClB,qBAAqB,UAAO,UAyM7B;AAGD,eAAO,MAAM,IAAI,YACf,cAAc,EACd,aAAa,EACb,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,mBAAmB,EACnB,cAAc,CACN,CAAC;AAEX,wBAAgB,4BAA4B,CAAC,KAAK,EAAE,WAAW,UAoC9D;AAGD,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GAAG,MAAM,UAMzB"}
@@ -4,7 +4,9 @@ import { upstreamSchema } from "../../../../types/shards.js";
4
4
  import { publishedSchema, publishedSchemaQuery } from "./published.js";
5
5
  import { id } from "../../../../types/sql.js";
6
6
  import { literal } from "pg-format";
7
- var versionSchema = literalUnion(1, 2);
7
+ //#region ../zero-cache/src/services/change-source/pg/schema/ddl.ts
8
+ var PROTOCOL_VERSION = 1;
9
+ var versionSchema = literalUnion(PROTOCOL_VERSION, 2);
8
10
  var ddlEventSchema = valita_exports.object({ context: valita_exports.object({ query: valita_exports.string() }).rest(valita_exports.string()) }).extend({
9
11
  version: versionSchema,
10
12
  event: valita_exports.object({ tag: valita_exports.string() }),
@@ -170,7 +172,7 @@ BEGIN
170
172
 
171
173
  SELECT json_build_object(
172
174
  'type', event_type,
173
- 'version', 1,
175
+ 'version', ${PROTOCOL_VERSION},
174
176
  'previousSchema', prev_schema_specs,
175
177
  'schema', schema_specs,
176
178
  'event', json_build_object('tag', tag),
@@ -334,6 +336,6 @@ function dropEventTriggerStatements(appID, shardID) {
334
336
  `;
335
337
  }
336
338
  //#endregion
337
- export { TAGS, createEventFunctionStatements, createEventTriggerStatements, ddlEventSchema, ddlStartEventSchema, ddlUpdateEventSchema, dropEventTriggerStatements, replicationEventSchema, schemaSnapshotEventSchema };
339
+ export { TAGS, createEventFunctionStatements, createEventTriggerStatements, ddlStartEventSchema, ddlUpdateEventSchema, dropEventTriggerStatements, replicationEventSchema, schemaSnapshotEventSchema };
338
340
 
339
341
  //# sourceMappingURL=ddl.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ddl.js","names":["v.literalUnion","v","lit"],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/ddl.ts"],"sourcesContent":["import {literal as lit} from 'pg-format';\nimport {assert} from '../../../../../../shared/src/asserts.ts';\nimport * as v from '../../../../../../shared/src/valita.ts';\nimport {upstreamSchema, type ShardConfig} from '../../../../types/shards.ts';\nimport {id} from '../../../../types/sql.ts';\nimport {publishedSchema, publishedSchemaQuery} from './published.ts';\n\n// Sent in the 'version' tag of \"ddlStart\" and \"ddlUpdate\" event messages.\n// This is used to ensure that the message constructed in the upstream\n// Trigger function is compatible with the code processing it in the zero-cache.\n//\n// Increment this when changing the format of the contents of the \"ddl\" events.\n// This will allow old / incompatible code to detect the change and abort.\nexport const PROTOCOL_VERSION = 1;\n\n// In protocol v2 (planned, not yet emitted), \"ddlStart\" events that are not\n// associated with a schema change will be context-only, omitting the\n// `schema` snapshot entirely. This avoids bloating the WAL with (large)\n// redundant schema snapshots for DDL commands that do not affect the\n// published schema, e.g. the CREATE/ALTER/DROP TABLE sub-commands executed\n// by REFRESH MATERIALIZED VIEW CONCURRENTLY.\n//\n// This release already accepts v2 events so that the (subsequent) release\n// that upgrades the triggers to emit them remains rollback safe with respect\n// to this one.\nconst versionSchema = v.literalUnion(PROTOCOL_VERSION, 2);\n\nconst triggerEvent = v.object({\n context: v.object({query: v.string()}).rest(v.string()),\n});\n\nexport const ddlEventSchema = triggerEvent.extend({\n version: versionSchema,\n event: v.object({tag: v.string()}),\n // Maps the OID of each published table to the `attnum`s of published\n // columns that were created in the (upstream) transaction that emitted\n // the event. Such columns are guaranteed to have the column default in\n // all pre-existing rows, and can thus be replicated without backfill if\n // the default value itself is replicable. Tables whose publication\n // entries (e.g. column lists) were modified in the same transaction are\n // excluded, since a newly *published* (as opposed to newly *created*)\n // column may hold arbitrary values in existing rows.\n //\n // The field is absent in messages from older versions of the upstream\n // functions (in which case backfill decisions fall back to the command\n // tag heuristic), and `null` when there are no such columns.\n newColumns: v.record(v.array(v.number())).nullable().optional(),\n});\n\n/**\n * A {@link DdlStartEvent} message is emitted before every DDL event,\n * containing the command `tag`.\n *\n * In most cases, the `DdlStartEvent` itself will not be associated with a\n * schema change, in which case the event is context-only (in protocol v2,\n * planned, both `previousSchema` and `schema` will be absent; in v1,\n * `schema` is set and `previousSchema` is `null`). The message is still\n * emitted to provide the command `tag` context in case an immediately\n * following `DdlStartEvent` tag is emitted with a schema change (which can\n * happen when another event trigger results in a nested ddl statement).\n *\n * In such cases, the `previousSchema` and `schema` fields of the latter event\n * are used to determine the necessary schema change operations (as they are\n * with `ddlUpdate` and `schemaSnapshot` events), and the `tag` of the\n * preceding start event indicates the command that precipitated the schema\n * change (e.g. a CREATE vs ALTER) to determine whether a backfill is\n * necessary.\n */\nexport const ddlStartEventSchema = ddlEventSchema.extend({\n type: v.literal('ddlStart'),\n // Set (along with `previousSchema`) only if the ddlStart event itself is\n // associated with a schema change. Absent in context-only (protocol v2,\n // planned) events. v1 events always contain the current `schema`.\n schema: publishedSchema.optional(),\n // For ddlStart messages, previousSchema is `null` (v1) or absent\n // (protocol v2, planned) if there was no change in schema detected.\n previousSchema: publishedSchema.nullable().optional(),\n // For backwards compatibility with previous versions of the trigger,\n // default an absent `event` field with a semantic equivalent. This\n // field override can be removed in a version that is rollback safe\n // with 1.4.0.\n event: v.object({tag: v.string()}).optional(() => ({tag: 'UNKNOWN'})),\n});\n\nexport type DdlStartEvent = v.Infer<typeof ddlStartEventSchema>;\n\n/**\n * A {@link DdlUpdateEvent} is emitted if there was a change in the schema.\n * It always contains `previousSchema` and (current) `schema` fields, leaving\n * it to the receiver to compute the necessary schema change operations.\n */\nexport const ddlUpdateEventSchema = ddlEventSchema.extend({\n type: v.literal('ddlUpdate'),\n // ddlUpdate messages are only emitted if the schema changed. `schema`\n // contains the current (i.e. post-change) snapshot.\n schema: publishedSchema,\n // The `previousSchema` contains the schema before the change.\n //\n // In 1.5.0 it is always set, and can be made non-optional when\n // rollback safe.\n previousSchema: publishedSchema.optional(),\n});\n\nexport type DdlUpdateEvent = v.Infer<typeof ddlUpdateEventSchema>;\n\n/**\n * The `schemaSnapshot` message is a snapshot of a schema taken in response to\n * a `COMMENT ON PUBLICATION` command, which is a hook recognized by zero\n * to manually emit `previousSchema` and `schema` snapshots when a difference\n * is detected. This is a workaround provided to support detection of schema\n * changes from `ALTER PUBLICATION` commands on supabase, which does not fire\n * event triggers for them (https://github.com/supabase/supautils/issues/123).\n *\n * The hook is exercised by trailing the publication change with a\n * `COMMENT ON PUBLICATION` statement, e.g.\n *\n * ```sql\n * BEGIN;\n * ALTER PUBLICATION my_publication ...;\n * COMMENT ON PUBLICATION my_publication IS 'whatever';\n * COMMIT;\n * ```\n *\n * Note that it is fine to invoke `COMMENT ON PUBLICATION` statements\n * on a database that *does* support event triggers on\n * `ALTER PUBLICATION` statements, as it will simply be a no-op.\n */\nexport const schemaSnapshotEventSchema = ddlEventSchema.extend({\n type: v.literal('schemaSnapshot'),\n schema: publishedSchema,\n previousSchema: publishedSchema.optional(),\n});\n\nexport type SchemaSnapshotEvent = v.Infer<typeof schemaSnapshotEventSchema>;\n\nexport const replicationEventSchema = v.union(\n ddlStartEventSchema,\n ddlUpdateEventSchema,\n schemaSnapshotEventSchema,\n);\n\nexport type ReplicationEvent = v.Infer<typeof replicationEventSchema>;\n\n// Creates a function that appends `_{shard-num}` to the input and\n// quotes the result to be a valid identifier.\nfunction append(shardNum: number) {\n return (name: string) => id(name + '_' + String(shardNum));\n}\n\n// pg_advisory_xact_lock key for serializing ddl statements in order to\n// produce correct schema change diffs.\nconst DDL_SERIALIZATION_LOCK = 0x3c6b8468f1bac0b0n;\n\n/**\n * Event trigger functions contain the core logic that are invoked by triggers.\n *\n * Note that although many of these functions can theoretically be parameterized and\n * shared across shards, it is advantageous to keep the functions in each shard\n * isolated from each other in order to avoid the complexity of shared-function\n * versioning.\n *\n * In a sense, shards (and their triggers and functions) should be thought of as\n * execution environments that can be updated at different schedules. If per-shard\n * triggers called into shared functions, we would have to consider versioning the\n * functions when changing their behavior, backwards compatibility, removal of\n * unused versions, etc. (not unlike versioning of npm packages).\n *\n * Instead, we opt for the simplicity and isolation of having each shard\n * completely own (and maintain) the entirety of its trigger/function stack.\n */\nexport function createEventFunctionStatements(\n shard: ShardConfig,\n includePartialIndexes = true,\n) {\n const {appID, shardNum, publications} = shard;\n const schema = id(upstreamSchema(shard)); // e.g. \"{APP_ID}_{SHARD_ID}\"\n return /*sql*/ `\nCREATE SCHEMA IF NOT EXISTS ${schema};\n\nCREATE OR REPLACE FUNCTION ${schema}.get_trigger_context()\nRETURNS record AS $$\nDECLARE\n result record;\nBEGIN\n SELECT COALESCE(current_query(), 'current_query() returned NULL') AS \"query\" into result;\n RETURN result;\nEND\n$$ LANGUAGE plpgsql;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.notice_ignore(reason TEXT, tag TEXT, target record)\nRETURNS void AS $$\nBEGIN\n RAISE NOTICE '${appID}_${shardNum} ignoring % % %', reason, tag, \n COALESCE(row_to_json(target)::text, '');\nEND\n$$ LANGUAGE plpgsql;\n\n\n-- Note: DROP and CREATE to upgrade from v20 to v21 because the\n-- return type has changed. This can be simplified to CREATE OR REPLACE\n-- once 1.5.0 is rollback safe.\nDROP FUNCTION IF EXISTS ${schema}.schema_specs();\nCREATE FUNCTION ${schema}.schema_specs()\nRETURNS JSON \nSTABLE\nAS $$\n ${publishedSchemaQuery(publications, includePartialIndexes)}\n$$ LANGUAGE sql;\n\n\n-- Stores the most recent published schema\nCREATE TABLE IF NOT EXISTS ${schema}.\"publishedSchema\" (\n current JSON,\n exists BOOL PRIMARY KEY DEFAULT true CHECK (exists)\n);\n\nINSERT INTO ${schema}.\"publishedSchema\" (current) VALUES (${schema}.schema_specs())\n ON CONFLICT (exists) DO \n UPDATE SET current = excluded.current;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.update_schemas(event_type text, tag text, target record)\nRETURNS void AS $$\nDECLARE\n prev_schema_specs JSON;\n schema_specs JSON;\n message TEXT;\nBEGIN\n SELECT current FROM ${schema}.\"publishedSchema\" INTO prev_schema_specs;\n SELECT ${schema}.schema_specs() INTO schema_specs;\n \n IF prev_schema_specs::text != schema_specs::text THEN\n UPDATE ${schema}.\"publishedSchema\" SET current = schema_specs;\n ELSIF event_type = 'ddlStart' THEN\n -- ddlStart events are always be emitted to allow the zero-cache\n -- to track the context of the current command tag in the face of\n -- nested event triggers (e.g. start->start->end->end).\n prev_schema_specs = NULL;\n ELSIF event_type = 'ddlUpdate' THEN\n -- TODO: fold 'schemaSnapshot' into this condition too (i.e. make it \"ELSE\")\n -- when 1.5.0 is rollback safe. Until then, noop schemaSnapshots are sent\n -- for compatibility with 1.0.0 ~ 1.4.0.\n PERFORM ${schema}.notice_ignore('noop', tag, target);\n RETURN;\n END IF;\n\n SELECT json_build_object(\n 'type', event_type,\n 'version', ${PROTOCOL_VERSION},\n 'previousSchema', prev_schema_specs,\n 'schema', schema_specs,\n 'event', json_build_object('tag', tag),\n 'context', ${schema}.get_trigger_context()\n ) INTO message;\n\n PERFORM pg_logical_emit_message(true, '${appID}/${shardNum}/ddl', message);\n\n RAISE NOTICE 'Emitted ${appID}_${shardNum} % for % %', event_type, tag,\n COALESCE(row_to_json(target)::text, '');\nEND\n$$ LANGUAGE plpgsql;\n\n\n-- Hook/workaround to manually trigger replication of schema changes on DBs \n-- that do not support/allow event triggers.\nCREATE OR REPLACE FUNCTION ${schema}.update_schemas()\nRETURNS void AS $$\nBEGIN\n PERFORM ${schema}.update_schemas('schemaSnapshot', 'MANUAL', NULL);\nEND\n$$ LANGUAGE plpgsql;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.emit_ddl_start()\nRETURNS event_trigger AS $$\nDECLARE\n schema_specs JSON;\n message TEXT;\nBEGIN\n -- serialize DDL statements to compute correct schema change diffs\n PERFORM pg_advisory_xact_lock(${DDL_SERIALIZATION_LOCK});\n PERFORM ${schema}.update_schemas('ddlStart', TG_TAG, NULL);\nEND\n$$ LANGUAGE plpgsql;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.emit_ddl_end()\nRETURNS event_trigger AS $$\nDECLARE\n publications TEXT[];\n target RECORD;\n relevant RECORD;\n schema_specs JSON;\n message TEXT;\n event TEXT;\nBEGIN\n publications := ARRAY[${lit(publications)}];\n\n SELECT objid, object_type, object_identity \n FROM pg_event_trigger_ddl_commands() \n LIMIT 1 INTO target;\n\n -- Filter DDL updates that are not relevant to the shard (i.e. publications) when possible.\n SELECT true INTO relevant;\n\n -- Note: ALTER TABLE statements may *remove* the table from the set of published\n -- tables, and there is no way to determine if the table \"used to be\" in the\n -- set. Thus, all ALTER TABLE statements must produce a ddl update, similar to\n -- any DROP * statement.\n IF (target.object_type = 'table' AND TG_TAG != 'ALTER TABLE') \n OR target.object_type = 'table column' THEN\n SELECT ns.nspname AS \"schema\", c.relname AS \"name\" FROM pg_class AS c\n JOIN pg_namespace AS ns ON c.relnamespace = ns.oid\n JOIN pg_publication_tables AS pb ON pb.schemaname = ns.nspname AND pb.tablename = c.relname\n WHERE c.oid = target.objid AND pb.pubname = ANY (publications)\n INTO relevant;\n\n ELSIF target.object_type = 'index' THEN\n SELECT ns.nspname AS \"schema\", c.relname AS \"name\" FROM pg_class AS c\n JOIN pg_namespace AS ns ON c.relnamespace = ns.oid\n JOIN pg_indexes as ind ON ind.schemaname = ns.nspname AND ind.indexname = c.relname\n JOIN pg_publication_tables AS pb ON pb.schemaname = ns.nspname AND pb.tablename = ind.tablename\n WHERE c.oid = target.objid AND pb.pubname = ANY (publications)\n INTO relevant;\n\n ELSIF target.object_type = 'publication relation' THEN\n SELECT pb.pubname FROM pg_publication_rel AS rel\n JOIN pg_publication AS pb ON pb.oid = rel.prpubid\n WHERE rel.oid = target.objid AND pb.pubname = ANY (publications) \n INTO relevant;\n\n ELSIF target.object_type = 'publication namespace' THEN\n SELECT pb.pubname FROM pg_publication_namespace AS ns\n JOIN pg_publication AS pb ON pb.oid = ns.pnpubid\n WHERE ns.oid = target.objid AND pb.pubname = ANY (publications) \n INTO relevant;\n\n ELSIF target.object_type = 'schema' THEN\n SELECT ns.nspname AS \"schema\", c.relname AS \"name\" FROM pg_class AS c\n JOIN pg_namespace AS ns ON c.relnamespace = ns.oid\n JOIN pg_publication_tables AS pb ON pb.schemaname = ns.nspname AND pb.tablename = c.relname\n WHERE ns.oid = target.objid AND pb.pubname = ANY (publications)\n INTO relevant;\n\n ELSIF target.object_type = 'publication' THEN\n SELECT 1 WHERE target.object_identity = ANY (publications)\n INTO relevant;\n\n -- no-op CREATE IF NOT EXIST statements\n ELSIF TG_TAG LIKE 'CREATE %' AND target.object_type IS NULL THEN\n relevant := NULL;\n END IF;\n\n IF relevant IS NULL THEN\n PERFORM ${schema}.notice_ignore('irrelevant', TG_TAG, target);\n RETURN;\n END IF;\n\n IF TG_TAG = 'COMMENT' THEN\n -- Only make schemaSnapshots for COMMENT ON PUBLICATION\n IF target.object_type != 'publication' THEN\n PERFORM ${schema}.notice_ignore('irrelevant', TG_TAG, target);\n RETURN;\n END IF;\n PERFORM ${schema}.update_schemas('schemaSnapshot', TG_TAG, target);\n ELSE\n PERFORM ${schema}.update_schemas('ddlUpdate', TG_TAG, target);\n END IF;\n\nEND\n$$ LANGUAGE plpgsql;\n`;\n}\n\n// Exported for testing.\nexport const TAGS = [\n 'CREATE TABLE',\n 'ALTER TABLE',\n 'CREATE INDEX',\n 'DROP TABLE',\n 'DROP INDEX',\n 'ALTER PUBLICATION',\n 'ALTER SCHEMA',\n] as const;\n\nexport function createEventTriggerStatements(shard: ShardConfig) {\n // Better to assert here than get a cryptic syntax error from Postgres.\n assert(shard.publications.length, `shard publications must be non-empty`);\n\n // Unlike functions, which are namespaced in shard-specific schemas,\n // EVENT TRIGGER names are in the global namespace and thus must include\n // the appID and shardNum.\n const {appID, shardNum} = shard;\n const sharded = append(shardNum);\n const schema = id(upstreamSchema(shard));\n\n const triggers = [\n dropEventTriggerStatements(shard.appID, shard.shardNum),\n /*sql*/ `\nCREATE EVENT TRIGGER ${sharded(`${appID}_ddl_start`)}\n ON ddl_command_start\n WHEN TAG IN (${lit(TAGS)})\n EXECUTE PROCEDURE ${schema}.emit_ddl_start();\n\nCREATE EVENT TRIGGER ${sharded(`${appID}_ddl_end`)}\n ON ddl_command_end\n WHEN TAG IN (${lit([...TAGS, 'COMMENT'])})\n EXECUTE PROCEDURE ${schema}.emit_ddl_end();\n`,\n ];\n\n // Drop legacy functions / triggers.\n triggers.push(\n `DROP FUNCTION IF EXISTS ${schema}.emit_ddl_end(text) CASCADE;`,\n `DROP FUNCTION IF EXISTS ${schema}.notice_ignore(text, record);`,\n );\n for (const tag of [...TAGS, 'COMMENT']) {\n const tagID = tag.toLowerCase().replace(' ', '_');\n triggers.push(`DROP FUNCTION IF EXISTS ${schema}.emit_${tagID}() CASCADE;`);\n }\n return triggers.join('');\n}\n\n// Exported for testing.\nexport function dropEventTriggerStatements(\n appID: string,\n shardID: string | number,\n) {\n return /*sql*/ `\n DROP EVENT TRIGGER IF EXISTS ${id(`${appID}_ddl_start_${shardID}`)};\n DROP EVENT TRIGGER IF EXISTS ${id(`${appID}_ddl_end_${shardID}`)};\n `;\n}\n"],"mappings":";;;;;;AAyBA,IAAM,gBAAgBA,aAAAA,GAAiC,CAAC;AAMxD,IAAa,iBAJQC,eAAE,OAAO,EAC5B,SAASA,eAAE,OAAO,EAAC,OAAOA,eAAE,OAAO,EAAC,CAAC,CAAC,CAAC,KAAKA,eAAE,OAAO,CAAC,EACxD,CAE8B,CAAA,CAAa,OAAO;CAChD,SAAS;CACT,OAAOA,eAAE,OAAO,EAAC,KAAKA,eAAE,OAAO,EAAC,CAAC;CAajC,YAAYA,eAAE,OAAOA,eAAE,MAAMA,eAAE,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS;AAChE,CAAC;;;;;;;;;;;;;;;;;;;;AAqBD,IAAa,sBAAsB,eAAe,OAAO;CACvD,MAAMA,eAAE,QAAQ,UAAU;CAI1B,QAAQ,gBAAgB,SAAS;CAGjC,gBAAgB,gBAAgB,SAAS,CAAC,CAAC,SAAS;CAKpD,OAAOA,eAAE,OAAO,EAAC,KAAKA,eAAE,OAAO,EAAC,CAAC,CAAC,CAAC,gBAAgB,EAAC,KAAK,UAAS,EAAE;AACtE,CAAC;;;;;;AASD,IAAa,uBAAuB,eAAe,OAAO;CACxD,MAAMA,eAAE,QAAQ,WAAW;CAG3B,QAAQ;CAKR,gBAAgB,gBAAgB,SAAS;AAC3C,CAAC;;;;;;;;;;;;;;;;;;;;;;;AA0BD,IAAa,4BAA4B,eAAe,OAAO;CAC7D,MAAMA,eAAE,QAAQ,gBAAgB;CAChC,QAAQ;CACR,gBAAgB,gBAAgB,SAAS;AAC3C,CAAC;AAID,IAAa,yBAAyBA,eAAE,MACtC,qBACA,sBACA,yB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C;uBACW,QAAQ,GAAG,MAAM,WAAW,EAAE;;iBAEpCA,QAAI,IAAI,EAAE;sBACL,OAAO;;uBAEN,QAAQ,GAAG,MAAM,SAAS,EAAE;;iBAElCA,QAAI,CAAC,GAAG,MAAM,SAAS,CAAC,EAAE;sBACrB,OAAO;CAE3B;CAGA,SAAS,KACP,2BAA2B,OAAO,+BAClC,2BAA2B,OAAO,8BACpC;CACA,KAAK,MAAM,OAAO,CAAC,GAAG,MAAM,SAAS,GAAG;EACtC,MAAM,QAAQ,IAAI,YAAY,CAAC,CAAC,QAAQ,KAAK,GAAG;EAChD,SAAS,KAAK,2BAA2B,OAAO,QAAQ,MAAM,YAAY;CAC5E;CACA,OAAO,SAAS,KAAK,EAAE;AACzB;AAGA,SAAgB,2BACd,OACA,SACA;CACA,OAAe;mCACkB,GAAG,GAAG,MAAM,aAAa,SAAS,EAAE;mCACpC,GAAG,GAAG,MAAM,WAAW,SAAS,EAAE;;AAErE"}
1
+ {"version":3,"file":"ddl.js","names":["v.literalUnion","v","lit"],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/ddl.ts"],"sourcesContent":["import {literal as lit} from 'pg-format';\nimport {assert} from '../../../../../../shared/src/asserts.ts';\nimport * as v from '../../../../../../shared/src/valita.ts';\nimport {upstreamSchema, type ShardConfig} from '../../../../types/shards.ts';\nimport {id} from '../../../../types/sql.ts';\nimport {publishedSchema, publishedSchemaQuery} from './published.ts';\n\n// Sent in the 'version' tag of \"ddlStart\" and \"ddlUpdate\" event messages.\n// This is used to ensure that the message constructed in the upstream\n// Trigger function is compatible with the code processing it in the zero-cache.\n//\n// Increment this when changing the format of the contents of the \"ddl\" events.\n// This will allow old / incompatible code to detect the change and abort.\nconst PROTOCOL_VERSION = 1;\n\n// In protocol v2 (planned, not yet emitted), \"ddlStart\" events that are not\n// associated with a schema change will be context-only, omitting the\n// `schema` snapshot entirely. This avoids bloating the WAL with (large)\n// redundant schema snapshots for DDL commands that do not affect the\n// published schema, e.g. the CREATE/ALTER/DROP TABLE sub-commands executed\n// by REFRESH MATERIALIZED VIEW CONCURRENTLY.\n//\n// This release already accepts v2 events so that the (subsequent) release\n// that upgrades the triggers to emit them remains rollback safe with respect\n// to this one.\nconst versionSchema = v.literalUnion(PROTOCOL_VERSION, 2);\n\nconst triggerEvent = v.object({\n context: v.object({query: v.string()}).rest(v.string()),\n});\n\nconst ddlEventSchema = triggerEvent.extend({\n version: versionSchema,\n event: v.object({tag: v.string()}),\n // Maps the OID of each published table to the `attnum`s of published\n // columns that were created in the (upstream) transaction that emitted\n // the event. Such columns are guaranteed to have the column default in\n // all pre-existing rows, and can thus be replicated without backfill if\n // the default value itself is replicable. Tables whose publication\n // entries (e.g. column lists) were modified in the same transaction are\n // excluded, since a newly *published* (as opposed to newly *created*)\n // column may hold arbitrary values in existing rows.\n //\n // The field is absent in messages from older versions of the upstream\n // functions (in which case backfill decisions fall back to the command\n // tag heuristic), and `null` when there are no such columns.\n newColumns: v.record(v.array(v.number())).nullable().optional(),\n});\n\n/**\n * A {@link DdlStartEvent} message is emitted before every DDL event,\n * containing the command `tag`.\n *\n * In most cases, the `DdlStartEvent` itself will not be associated with a\n * schema change, in which case the event is context-only (in protocol v2,\n * planned, both `previousSchema` and `schema` will be absent; in v1,\n * `schema` is set and `previousSchema` is `null`). The message is still\n * emitted to provide the command `tag` context in case an immediately\n * following `DdlStartEvent` tag is emitted with a schema change (which can\n * happen when another event trigger results in a nested ddl statement).\n *\n * In such cases, the `previousSchema` and `schema` fields of the latter event\n * are used to determine the necessary schema change operations (as they are\n * with `ddlUpdate` and `schemaSnapshot` events), and the `tag` of the\n * preceding start event indicates the command that precipitated the schema\n * change (e.g. a CREATE vs ALTER) to determine whether a backfill is\n * necessary.\n */\nexport const ddlStartEventSchema = ddlEventSchema.extend({\n type: v.literal('ddlStart'),\n // Set (along with `previousSchema`) only if the ddlStart event itself is\n // associated with a schema change. Absent in context-only (protocol v2,\n // planned) events. v1 events always contain the current `schema`.\n schema: publishedSchema.optional(),\n // For ddlStart messages, previousSchema is `null` (v1) or absent\n // (protocol v2, planned) if there was no change in schema detected.\n previousSchema: publishedSchema.nullable().optional(),\n // For backwards compatibility with previous versions of the trigger,\n // default an absent `event` field with a semantic equivalent. This\n // field override can be removed in a version that is rollback safe\n // with 1.4.0.\n event: v.object({tag: v.string()}).optional(() => ({tag: 'UNKNOWN'})),\n});\n\nexport type DdlStartEvent = v.Infer<typeof ddlStartEventSchema>;\n\n/**\n * A {@link DdlUpdateEvent} is emitted if there was a change in the schema.\n * It always contains `previousSchema` and (current) `schema` fields, leaving\n * it to the receiver to compute the necessary schema change operations.\n */\nexport const ddlUpdateEventSchema = ddlEventSchema.extend({\n type: v.literal('ddlUpdate'),\n // ddlUpdate messages are only emitted if the schema changed. `schema`\n // contains the current (i.e. post-change) snapshot.\n schema: publishedSchema,\n // The `previousSchema` contains the schema before the change.\n //\n // In 1.5.0 it is always set, and can be made non-optional when\n // rollback safe.\n previousSchema: publishedSchema.optional(),\n});\n\nexport type DdlUpdateEvent = v.Infer<typeof ddlUpdateEventSchema>;\n\n/**\n * The `schemaSnapshot` message is a snapshot of a schema taken in response to\n * a `COMMENT ON PUBLICATION` command, which is a hook recognized by zero\n * to manually emit `previousSchema` and `schema` snapshots when a difference\n * is detected. This is a workaround provided to support detection of schema\n * changes from `ALTER PUBLICATION` commands on supabase, which does not fire\n * event triggers for them (https://github.com/supabase/supautils/issues/123).\n *\n * The hook is exercised by trailing the publication change with a\n * `COMMENT ON PUBLICATION` statement, e.g.\n *\n * ```sql\n * BEGIN;\n * ALTER PUBLICATION my_publication ...;\n * COMMENT ON PUBLICATION my_publication IS 'whatever';\n * COMMIT;\n * ```\n *\n * Note that it is fine to invoke `COMMENT ON PUBLICATION` statements\n * on a database that *does* support event triggers on\n * `ALTER PUBLICATION` statements, as it will simply be a no-op.\n */\nexport const schemaSnapshotEventSchema = ddlEventSchema.extend({\n type: v.literal('schemaSnapshot'),\n schema: publishedSchema,\n previousSchema: publishedSchema.optional(),\n});\n\nexport type SchemaSnapshotEvent = v.Infer<typeof schemaSnapshotEventSchema>;\n\nexport const replicationEventSchema = v.union(\n ddlStartEventSchema,\n ddlUpdateEventSchema,\n schemaSnapshotEventSchema,\n);\n\nexport type ReplicationEvent = v.Infer<typeof replicationEventSchema>;\n\n// Creates a function that appends `_{shard-num}` to the input and\n// quotes the result to be a valid identifier.\nfunction append(shardNum: number) {\n return (name: string) => id(name + '_' + String(shardNum));\n}\n\n// pg_advisory_xact_lock key for serializing ddl statements in order to\n// produce correct schema change diffs.\nconst DDL_SERIALIZATION_LOCK = 0x3c6b8468f1bac0b0n;\n\n/**\n * Event trigger functions contain the core logic that are invoked by triggers.\n *\n * Note that although many of these functions can theoretically be parameterized and\n * shared across shards, it is advantageous to keep the functions in each shard\n * isolated from each other in order to avoid the complexity of shared-function\n * versioning.\n *\n * In a sense, shards (and their triggers and functions) should be thought of as\n * execution environments that can be updated at different schedules. If per-shard\n * triggers called into shared functions, we would have to consider versioning the\n * functions when changing their behavior, backwards compatibility, removal of\n * unused versions, etc. (not unlike versioning of npm packages).\n *\n * Instead, we opt for the simplicity and isolation of having each shard\n * completely own (and maintain) the entirety of its trigger/function stack.\n */\nexport function createEventFunctionStatements(\n shard: ShardConfig,\n includePartialIndexes = true,\n) {\n const {appID, shardNum, publications} = shard;\n const schema = id(upstreamSchema(shard)); // e.g. \"{APP_ID}_{SHARD_ID}\"\n return /*sql*/ `\nCREATE SCHEMA IF NOT EXISTS ${schema};\n\nCREATE OR REPLACE FUNCTION ${schema}.get_trigger_context()\nRETURNS record AS $$\nDECLARE\n result record;\nBEGIN\n SELECT COALESCE(current_query(), 'current_query() returned NULL') AS \"query\" into result;\n RETURN result;\nEND\n$$ LANGUAGE plpgsql;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.notice_ignore(reason TEXT, tag TEXT, target record)\nRETURNS void AS $$\nBEGIN\n RAISE NOTICE '${appID}_${shardNum} ignoring % % %', reason, tag, \n COALESCE(row_to_json(target)::text, '');\nEND\n$$ LANGUAGE plpgsql;\n\n\n-- Note: DROP and CREATE to upgrade from v20 to v21 because the\n-- return type has changed. This can be simplified to CREATE OR REPLACE\n-- once 1.5.0 is rollback safe.\nDROP FUNCTION IF EXISTS ${schema}.schema_specs();\nCREATE FUNCTION ${schema}.schema_specs()\nRETURNS JSON \nSTABLE\nAS $$\n ${publishedSchemaQuery(publications, includePartialIndexes)}\n$$ LANGUAGE sql;\n\n\n-- Stores the most recent published schema\nCREATE TABLE IF NOT EXISTS ${schema}.\"publishedSchema\" (\n current JSON,\n exists BOOL PRIMARY KEY DEFAULT true CHECK (exists)\n);\n\nINSERT INTO ${schema}.\"publishedSchema\" (current) VALUES (${schema}.schema_specs())\n ON CONFLICT (exists) DO \n UPDATE SET current = excluded.current;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.update_schemas(event_type text, tag text, target record)\nRETURNS void AS $$\nDECLARE\n prev_schema_specs JSON;\n schema_specs JSON;\n message TEXT;\nBEGIN\n SELECT current FROM ${schema}.\"publishedSchema\" INTO prev_schema_specs;\n SELECT ${schema}.schema_specs() INTO schema_specs;\n \n IF prev_schema_specs::text != schema_specs::text THEN\n UPDATE ${schema}.\"publishedSchema\" SET current = schema_specs;\n ELSIF event_type = 'ddlStart' THEN\n -- ddlStart events are always be emitted to allow the zero-cache\n -- to track the context of the current command tag in the face of\n -- nested event triggers (e.g. start->start->end->end).\n prev_schema_specs = NULL;\n ELSIF event_type = 'ddlUpdate' THEN\n -- TODO: fold 'schemaSnapshot' into this condition too (i.e. make it \"ELSE\")\n -- when 1.5.0 is rollback safe. Until then, noop schemaSnapshots are sent\n -- for compatibility with 1.0.0 ~ 1.4.0.\n PERFORM ${schema}.notice_ignore('noop', tag, target);\n RETURN;\n END IF;\n\n SELECT json_build_object(\n 'type', event_type,\n 'version', ${PROTOCOL_VERSION},\n 'previousSchema', prev_schema_specs,\n 'schema', schema_specs,\n 'event', json_build_object('tag', tag),\n 'context', ${schema}.get_trigger_context()\n ) INTO message;\n\n PERFORM pg_logical_emit_message(true, '${appID}/${shardNum}/ddl', message);\n\n RAISE NOTICE 'Emitted ${appID}_${shardNum} % for % %', event_type, tag,\n COALESCE(row_to_json(target)::text, '');\nEND\n$$ LANGUAGE plpgsql;\n\n\n-- Hook/workaround to manually trigger replication of schema changes on DBs \n-- that do not support/allow event triggers.\nCREATE OR REPLACE FUNCTION ${schema}.update_schemas()\nRETURNS void AS $$\nBEGIN\n PERFORM ${schema}.update_schemas('schemaSnapshot', 'MANUAL', NULL);\nEND\n$$ LANGUAGE plpgsql;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.emit_ddl_start()\nRETURNS event_trigger AS $$\nDECLARE\n schema_specs JSON;\n message TEXT;\nBEGIN\n -- serialize DDL statements to compute correct schema change diffs\n PERFORM pg_advisory_xact_lock(${DDL_SERIALIZATION_LOCK});\n PERFORM ${schema}.update_schemas('ddlStart', TG_TAG, NULL);\nEND\n$$ LANGUAGE plpgsql;\n\n\nCREATE OR REPLACE FUNCTION ${schema}.emit_ddl_end()\nRETURNS event_trigger AS $$\nDECLARE\n publications TEXT[];\n target RECORD;\n relevant RECORD;\n schema_specs JSON;\n message TEXT;\n event TEXT;\nBEGIN\n publications := ARRAY[${lit(publications)}];\n\n SELECT objid, object_type, object_identity \n FROM pg_event_trigger_ddl_commands() \n LIMIT 1 INTO target;\n\n -- Filter DDL updates that are not relevant to the shard (i.e. publications) when possible.\n SELECT true INTO relevant;\n\n -- Note: ALTER TABLE statements may *remove* the table from the set of published\n -- tables, and there is no way to determine if the table \"used to be\" in the\n -- set. Thus, all ALTER TABLE statements must produce a ddl update, similar to\n -- any DROP * statement.\n IF (target.object_type = 'table' AND TG_TAG != 'ALTER TABLE') \n OR target.object_type = 'table column' THEN\n SELECT ns.nspname AS \"schema\", c.relname AS \"name\" FROM pg_class AS c\n JOIN pg_namespace AS ns ON c.relnamespace = ns.oid\n JOIN pg_publication_tables AS pb ON pb.schemaname = ns.nspname AND pb.tablename = c.relname\n WHERE c.oid = target.objid AND pb.pubname = ANY (publications)\n INTO relevant;\n\n ELSIF target.object_type = 'index' THEN\n SELECT ns.nspname AS \"schema\", c.relname AS \"name\" FROM pg_class AS c\n JOIN pg_namespace AS ns ON c.relnamespace = ns.oid\n JOIN pg_indexes as ind ON ind.schemaname = ns.nspname AND ind.indexname = c.relname\n JOIN pg_publication_tables AS pb ON pb.schemaname = ns.nspname AND pb.tablename = ind.tablename\n WHERE c.oid = target.objid AND pb.pubname = ANY (publications)\n INTO relevant;\n\n ELSIF target.object_type = 'publication relation' THEN\n SELECT pb.pubname FROM pg_publication_rel AS rel\n JOIN pg_publication AS pb ON pb.oid = rel.prpubid\n WHERE rel.oid = target.objid AND pb.pubname = ANY (publications) \n INTO relevant;\n\n ELSIF target.object_type = 'publication namespace' THEN\n SELECT pb.pubname FROM pg_publication_namespace AS ns\n JOIN pg_publication AS pb ON pb.oid = ns.pnpubid\n WHERE ns.oid = target.objid AND pb.pubname = ANY (publications) \n INTO relevant;\n\n ELSIF target.object_type = 'schema' THEN\n SELECT ns.nspname AS \"schema\", c.relname AS \"name\" FROM pg_class AS c\n JOIN pg_namespace AS ns ON c.relnamespace = ns.oid\n JOIN pg_publication_tables AS pb ON pb.schemaname = ns.nspname AND pb.tablename = c.relname\n WHERE ns.oid = target.objid AND pb.pubname = ANY (publications)\n INTO relevant;\n\n ELSIF target.object_type = 'publication' THEN\n SELECT 1 WHERE target.object_identity = ANY (publications)\n INTO relevant;\n\n -- no-op CREATE IF NOT EXIST statements\n ELSIF TG_TAG LIKE 'CREATE %' AND target.object_type IS NULL THEN\n relevant := NULL;\n END IF;\n\n IF relevant IS NULL THEN\n PERFORM ${schema}.notice_ignore('irrelevant', TG_TAG, target);\n RETURN;\n END IF;\n\n IF TG_TAG = 'COMMENT' THEN\n -- Only make schemaSnapshots for COMMENT ON PUBLICATION\n IF target.object_type != 'publication' THEN\n PERFORM ${schema}.notice_ignore('irrelevant', TG_TAG, target);\n RETURN;\n END IF;\n PERFORM ${schema}.update_schemas('schemaSnapshot', TG_TAG, target);\n ELSE\n PERFORM ${schema}.update_schemas('ddlUpdate', TG_TAG, target);\n END IF;\n\nEND\n$$ LANGUAGE plpgsql;\n`;\n}\n\n// Exported for testing.\nexport const TAGS = [\n 'CREATE TABLE',\n 'ALTER TABLE',\n 'CREATE INDEX',\n 'DROP TABLE',\n 'DROP INDEX',\n 'ALTER PUBLICATION',\n 'ALTER SCHEMA',\n] as const;\n\nexport function createEventTriggerStatements(shard: ShardConfig) {\n // Better to assert here than get a cryptic syntax error from Postgres.\n assert(shard.publications.length, `shard publications must be non-empty`);\n\n // Unlike functions, which are namespaced in shard-specific schemas,\n // EVENT TRIGGER names are in the global namespace and thus must include\n // the appID and shardNum.\n const {appID, shardNum} = shard;\n const sharded = append(shardNum);\n const schema = id(upstreamSchema(shard));\n\n const triggers = [\n dropEventTriggerStatements(shard.appID, shard.shardNum),\n /*sql*/ `\nCREATE EVENT TRIGGER ${sharded(`${appID}_ddl_start`)}\n ON ddl_command_start\n WHEN TAG IN (${lit(TAGS)})\n EXECUTE PROCEDURE ${schema}.emit_ddl_start();\n\nCREATE EVENT TRIGGER ${sharded(`${appID}_ddl_end`)}\n ON ddl_command_end\n WHEN TAG IN (${lit([...TAGS, 'COMMENT'])})\n EXECUTE PROCEDURE ${schema}.emit_ddl_end();\n`,\n ];\n\n // Drop legacy functions / triggers.\n triggers.push(\n `DROP FUNCTION IF EXISTS ${schema}.emit_ddl_end(text) CASCADE;`,\n `DROP FUNCTION IF EXISTS ${schema}.notice_ignore(text, record);`,\n );\n for (const tag of [...TAGS, 'COMMENT']) {\n const tagID = tag.toLowerCase().replace(' ', '_');\n triggers.push(`DROP FUNCTION IF EXISTS ${schema}.emit_${tagID}() CASCADE;`);\n }\n return triggers.join('');\n}\n\n// Exported for testing.\nexport function dropEventTriggerStatements(\n appID: string,\n shardID: string | number,\n) {\n return /*sql*/ `\n DROP EVENT TRIGGER IF EXISTS ${id(`${appID}_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Q,GAAG,MAAM,SAAS,EAAE;;iBAElCA,QAAI,CAAC,GAAG,MAAM,SAAS,CAAC,EAAE;sBACrB,OAAO;CAE3B;CAGA,SAAS,KACP,2BAA2B,OAAO,+BAClC,2BAA2B,OAAO,8BACpC;CACA,KAAK,MAAM,OAAO,CAAC,GAAG,MAAM,SAAS,GAAG;EACtC,MAAM,QAAQ,IAAI,YAAY,CAAC,CAAC,QAAQ,KAAK,GAAG;EAChD,SAAS,KAAK,2BAA2B,OAAO,QAAQ,MAAM,YAAY;CAC5E;CACA,OAAO,SAAS,KAAK,EAAE;AACzB;AAGA,SAAgB,2BACd,OACA,SACA;CACA,OAAe;mCACkB,GAAG,GAAG,MAAM,aAAa,SAAS,EAAE;mCACpC,GAAG,GAAG,MAAM,WAAW,SAAS,EAAE;;AAErE"}
@@ -1,5 +1,5 @@
1
1
  import type { IndexPredicate, PublishedTableSpec } from '../../../../db/specs.ts';
2
- export declare const unsupportedPredicateReasons: readonly ['unpublished-column', 'unsupported-type', 'unsupported-operator', 'unsupported-function', 'unsupported-cast', 'unsupported-syntax', 'non-deterministic-collation'];
2
+ declare const unsupportedPredicateReasons: readonly ['unpublished-column', 'unsupported-type', 'unsupported-operator', 'unsupported-function', 'unsupported-cast', 'unsupported-syntax', 'non-deterministic-collation'];
3
3
  export type UnsupportedPredicateReason = (typeof unsupportedPredicateReasons)[number];
4
4
  export type PredicateTranslation = {
5
5
  readonly supported: true;
@@ -1 +1 @@
1
- {"version":3,"file":"index-predicate.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/index-predicate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EAEd,kBAAkB,EACnB,MAAM,yBAAyB,CAAC;AAmBjC,eAAO,MAAM,2BAA2B,YACtC,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,sBAAsB,EACtB,kBAAkB,EAClB,oBAAoB,EACpB,6BAA6B,CACrB,CAAC;AAEX,MAAM,MAAM,0BAA0B,GACpC,CAAC,OAAO,2BAA2B,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/C,MAAM,MAAM,oBAAoB,GAC5B;IAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,cAAc,CAAA;CAAC,GAC9D;IAAC,QAAQ,CAAC,SAAS,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,0BAA0B,CAAA;CAAC,CAAC;AAE7E,KAAK,mBAAmB,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG;IACjE,QAAQ,CAAC,wBAAwB,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;CAChE,CAAC;AAEF,KAAK,kBAAkB,GAAG,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC,GAAG;IAC9D,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC;CACjE,CAAC;AAoEF,wBAAgB,+BAA+B,CAC7C,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,kBAAkB,GACxB,oBAAoB,CAetB"}
1
+ {"version":3,"file":"index-predicate.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/index-predicate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EAEd,kBAAkB,EACnB,MAAM,yBAAyB,CAAC;AAmBjC,QAAA,MAAM,2BAA2B,YAC/B,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,sBAAsB,EACtB,kBAAkB,EAClB,oBAAoB,EACpB,6BAA6B,CACrB,CAAC;AAEX,MAAM,MAAM,0BAA0B,GACpC,CAAC,OAAO,2BAA2B,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/C,MAAM,MAAM,oBAAoB,GAC5B;IAAC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,cAAc,CAAA;CAAC,GAC9D;IAAC,QAAQ,CAAC,SAAS,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,0BAA0B,CAAA;CAAC,CAAC;AAE7E,KAAK,mBAAmB,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG;IACjE,QAAQ,CAAC,wBAAwB,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;CAChE,CAAC;AAEF,KAAK,kBAAkB,GAAG,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC,GAAG;IAC9D,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC,CAAC;CACjE,CAAC;AAoEF,wBAAgB,+BAA+B,CAC7C,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,kBAAkB,GACxB,oBAAoB,CAetB"}
@@ -1 +1 @@
1
- {"version":3,"file":"index-predicate.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/index-predicate.ts"],"sourcesContent":["import * as PostgresTypeClass from '../../../../db/postgres-type-class-enum.ts';\nimport type {\n IndexPredicate,\n IndexPredicateValue,\n PublishedTableSpec,\n} from '../../../../db/specs.ts';\n\nconst UUID_RE =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;\nconst INTEGER_RE = /^[+-]?\\d+$/;\nconst WHITESPACE_RE = /\\s/;\nconst OPERATOR_TOKEN_RE = /^(?:::|<>|!=|<=|>=|=|<|>)/;\nconst UNSUPPORTED_OPERATOR_TOKEN_RE = /^(?:!~\\*|!~|~\\*|~)/;\nconst INTEGER_TOKEN_RE = /^[+-]?\\d+/;\nconst WORD_TOKEN_RE = /^[A-Za-z_][A-Za-z0-9_$]*/;\nconst SIMPLE_ESCAPES: Readonly<Record<string, string>> = {\n b: '\\b',\n f: '\\f',\n n: '\\n',\n r: '\\r',\n t: '\\t',\n v: '\\v',\n};\n\nexport const unsupportedPredicateReasons = [\n 'unpublished-column',\n 'unsupported-type',\n 'unsupported-operator',\n 'unsupported-function',\n 'unsupported-cast',\n 'unsupported-syntax',\n 'non-deterministic-collation',\n] as const;\n\nexport type UnsupportedPredicateReason =\n (typeof unsupportedPredicateReasons)[number];\n\nexport type PredicateTranslation =\n | {readonly supported: true; readonly predicate: IndexPredicate}\n | {readonly supported: false; readonly reason: UnsupportedPredicateReason};\n\ntype PredicateColumnSpec = PublishedTableSpec['columns'][string] & {\n readonly collationIsDeterministic?: boolean | null | undefined;\n};\n\ntype PredicateTableSpec = Omit<PublishedTableSpec, 'columns'> & {\n readonly columns: Readonly<Record<string, PredicateColumnSpec>>;\n};\n\ntype Expr =\n | {readonly type: 'column'; readonly name: string}\n | {\n readonly type: 'literal';\n readonly value: boolean | string | null;\n readonly literalType: 'boolean' | 'integer' | 'string' | 'null';\n }\n | {readonly type: 'cast'; readonly expression: Expr; readonly cast: string}\n | {readonly type: 'not'; readonly expression: Expr}\n | {\n readonly type: 'and' | 'or';\n readonly expressions: readonly Expr[];\n }\n | {\n readonly type: 'comparison';\n readonly left: Expr;\n readonly op: '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=';\n readonly right: Expr;\n }\n | {\n readonly type: 'null-test';\n readonly expression: Expr;\n readonly not: boolean;\n };\n\ntype Token =\n | {readonly type: 'identifier'; readonly value: string}\n | {\n readonly type: 'literal';\n readonly value: boolean | string | null;\n readonly literalType: 'boolean' | 'integer' | 'string' | 'null';\n }\n | {\n readonly type:\n | '('\n | ')'\n | '['\n | ']'\n | ','\n | '.'\n | '::'\n | '='\n | '<>'\n | '!='\n | '<'\n | '<='\n | '>'\n | '>='\n | 'AND'\n | 'OR'\n | 'NOT'\n | 'IN'\n | 'IS'\n | 'EOF';\n }\n | {readonly type: 'unsupported'; readonly value: string};\n\nclass UnsupportedPredicateError extends Error {\n readonly reason: UnsupportedPredicateReason;\n\n constructor(reason: UnsupportedPredicateReason) {\n super(reason);\n this.reason = reason;\n }\n}\n\nexport function translatePostgresIndexPredicate(\n sql: string,\n table: PredicateTableSpec,\n): PredicateTranslation {\n try {\n const parser = new Parser(tokenize(sql));\n const expression = parser.parse();\n parser.expect('EOF');\n return {supported: true, predicate: normalize(expression, table)};\n } catch (error) {\n return {\n supported: false,\n reason:\n error instanceof UnsupportedPredicateError\n ? error.reason\n : 'unsupported-syntax',\n };\n }\n}\n\nclass Parser {\n readonly #tokens: readonly Token[];\n #position = 0;\n\n constructor(tokens: readonly Token[]) {\n this.#tokens = tokens;\n }\n\n parse(): Expr {\n return this.#parseOr();\n }\n\n #parseOr(): Expr {\n const expressions = [this.#parseAnd()];\n while (this.#take('OR')) expressions.push(this.#parseAnd());\n return expressions.length === 1\n ? expressions[0]\n : {type: 'or', expressions};\n }\n\n #parseAnd(): Expr {\n const expressions = [this.#parseNot()];\n while (this.#take('AND')) expressions.push(this.#parseNot());\n return expressions.length === 1\n ? expressions[0]\n : {type: 'and', expressions};\n }\n\n #parseNot(): Expr {\n if (this.#take('NOT')) {\n return {type: 'not', expression: this.#parseNot()};\n }\n return this.#parseComparison();\n }\n\n #parseComparison(): Expr {\n const left = this.#parseOperand();\n if (this.#take('IS')) {\n const not = this.#take('NOT');\n const token = this.#tokens[this.#position++];\n if (token?.type !== 'literal' || token.literalType !== 'null') {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n return {type: 'null-test', expression: left, not};\n }\n if (this.#take('IN')) {\n this.expect('(');\n const values = this.#parseOperandList(')');\n this.expect(')');\n return comparisons(left, values);\n }\n const op = this.#tokens[this.#position]?.type;\n if (isComparison(op)) {\n this.#position++;\n if (op === '=' && this.#takeIdentifier('ANY')) {\n return comparisons(left, this.#parseLiteralArray());\n }\n return {\n type: 'comparison',\n left,\n op,\n right: this.#parseOperand(),\n };\n }\n const next = this.#tokens[this.#position];\n if (next?.type === 'identifier' && next.value.toUpperCase() === 'COLLATE') {\n throw new UnsupportedPredicateError('non-deterministic-collation');\n }\n if (\n next?.type === 'unsupported' &&\n UNSUPPORTED_OPERATOR_TOKEN_RE.test(next.value)\n ) {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n if (\n next?.type === 'identifier' &&\n unsupportedOperatorWords.has(next.value.toUpperCase())\n ) {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n return left;\n }\n\n #parseLiteralArray(): readonly Expr[] {\n this.expect('(');\n this.#expectIdentifier('ARRAY');\n this.expect('[');\n const values = this.#parseOperandList(']');\n this.expect(']');\n if (this.#take('::')) {\n this.#parseCastName();\n this.expect('[');\n this.expect(']');\n }\n this.expect(')');\n return values;\n }\n\n #parseOperandList(end: ')' | ']'): readonly Expr[] {\n const values: Expr[] = [];\n if (this.#tokens[this.#position]?.type === end) return values;\n values.push(this.#parseOperand());\n while (this.#take(',')) values.push(this.#parseOperand());\n return values;\n }\n\n #parseOperand(): Expr {\n let expression = this.#parsePrimary();\n while (this.#take('::')) {\n expression = {\n type: 'cast',\n expression,\n cast: this.#parseCastName(),\n };\n }\n return expression;\n }\n\n #parsePrimary(): Expr {\n if (this.#take('(')) {\n const expression = this.#parseOr();\n this.expect(')');\n return expression;\n }\n const token = this.#tokens[this.#position++];\n if (token?.type === 'literal') {\n return {\n type: 'literal',\n value: token.value,\n literalType: token.literalType,\n };\n }\n if (token?.type === 'identifier') {\n if (this.#tokens[this.#position]?.type === '(') {\n throw new UnsupportedPredicateError('unsupported-function');\n }\n if (this.#tokens[this.#position]?.type === '.') {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n if (token.value.toUpperCase() === 'COLLATE') {\n throw new UnsupportedPredicateError('non-deterministic-collation');\n }\n return {type: 'column', name: token.value};\n }\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n\n #parseCastName(): string {\n const first = this.#expectIdentifier();\n const parts = [first];\n let separator = '.';\n while (this.#take('.')) parts.push(this.#expectIdentifier());\n const next = this.#tokens[this.#position];\n if (\n first.toLowerCase() === 'character' &&\n next?.type === 'identifier' &&\n next.value.toLowerCase() === 'varying'\n ) {\n parts.push(this.#expectIdentifier());\n separator = ' ';\n }\n if (this.#tokens[this.#position]?.type === '(') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return parts.join(separator);\n }\n\n #expectIdentifier(expected?: string): string {\n const token = this.#tokens[this.#position++];\n if (\n token?.type !== 'identifier' ||\n (expected !== undefined && token.value.toUpperCase() !== expected)\n ) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return token.value;\n }\n\n #takeIdentifier(expected: string): boolean {\n const token = this.#tokens[this.#position];\n if (\n token?.type !== 'identifier' ||\n token.value.toUpperCase() !== expected\n ) {\n return false;\n }\n this.#position++;\n return true;\n }\n\n #take(type: Token['type']): boolean {\n if (this.#tokens[this.#position]?.type !== type) return false;\n this.#position++;\n return true;\n }\n\n expect(type: Token['type']): void {\n if (!this.#take(type)) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n }\n}\n\nconst unsupportedOperatorWords = new Set([\n 'ALL',\n 'BETWEEN',\n 'ILIKE',\n 'LIKE',\n 'SIMILAR',\n]);\n\nfunction comparisons(left: Expr, values: readonly Expr[]): Expr {\n if (values.length === 0) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n const expressions = values.map(\n right => ({type: 'comparison', left, op: '=', right}) as const,\n );\n return expressions.length === 1 ? expressions[0] : {type: 'or', expressions};\n}\n\nfunction normalize(\n expression: Expr,\n table: PredicateTableSpec,\n): IndexPredicate {\n switch (expression.type) {\n case 'and':\n case 'or': {\n const conditions = expression.expressions.map(expr =>\n normalize(expr, table),\n );\n return {\n type: expression.type,\n conditions: conditions.flatMap(condition =>\n condition.type === expression.type\n ? condition.conditions\n : [condition],\n ),\n };\n }\n case 'not': {\n const booleanColumn = resolveBareBooleanColumn(\n expression.expression,\n table,\n );\n if (booleanColumn) {\n return booleanComparison(booleanColumn, false);\n }\n return {type: 'not', condition: normalize(expression.expression, table)};\n }\n case 'column': {\n const column = resolveColumn(expression, table);\n if (column.category !== 'boolean') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return booleanComparison(column.name, true);\n }\n case 'cast': {\n const column = resolveColumnOperand(expression, table);\n if (column.category !== 'boolean') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return booleanComparison(column.name, true);\n }\n case 'null-test': {\n const column = resolveNullTestColumn(expression.expression, table);\n return {\n type: 'null-test',\n column,\n op: expression.not ? 'IS NOT NULL' : 'IS NULL',\n };\n }\n case 'comparison':\n return normalizeComparison(expression, table);\n case 'literal':\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n}\n\nfunction resolveNullTestColumn(\n expression: Expr,\n table: PredicateTableSpec,\n): string {\n if (expression.type === 'cast') {\n return resolveColumnOperand(expression, table).name;\n }\n if (expression.type === 'column' && !table.columns[expression.name]) {\n throw new UnsupportedPredicateError('unpublished-column');\n }\n if (expression.type === 'column') return expression.name;\n throw new UnsupportedPredicateError('unsupported-syntax');\n}\n\nfunction resolveBareBooleanColumn(\n expression: Expr,\n table: PredicateTableSpec,\n): string | undefined {\n if (expression.type !== 'column' && expression.type !== 'cast') {\n return undefined;\n }\n const column = resolveColumnOperand(expression, table);\n return column.category === 'boolean' ? column.name : undefined;\n}\n\nfunction booleanComparison(column: string, value: boolean): IndexPredicate {\n return {\n type: 'comparison',\n column,\n op: '=',\n value: {type: 'boolean', value},\n };\n}\n\ntype ColumnCategory =\n | {readonly category: 'boolean'; readonly name: string}\n | {\n readonly category: 'integer';\n readonly name: string;\n readonly rank: 0 | 1 | 2;\n }\n | {readonly category: 'text'; readonly name: string}\n | {\n readonly category: 'enum';\n readonly name: string;\n readonly enumName: string;\n }\n | {readonly category: 'uuid'; readonly name: string};\n\nfunction normalizeComparison(\n expression: Extract<Expr, {type: 'comparison'}>,\n table: PredicateTableSpec,\n): IndexPredicate {\n const column = resolveColumnOperand(expression.left, table);\n const op = expression.op === '!=' ? '<>' : expression.op;\n if (column.category !== 'integer' && op !== '=' && op !== '<>') {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n const value = resolveLiteral(expression.right, column);\n return {type: 'comparison', column: column.name, op, value};\n}\n\nfunction resolveColumnOperand(\n expression: Expr,\n table: PredicateTableSpec,\n): ColumnCategory {\n if (expression.type === 'column') return resolveColumn(expression, table);\n if (expression.type !== 'cast') {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n const column = resolveColumnOperand(expression.expression, table);\n const cast = canonicalTypeName(expression.cast);\n switch (column.category) {\n case 'integer': {\n const rank = integerRank(cast);\n if (rank === undefined || rank < column.rank) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return {...column, rank};\n }\n case 'text':\n if (cast !== 'text' && cast !== 'varchar') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n case 'boolean':\n if (cast !== 'bool') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n case 'uuid':\n if (cast !== 'uuid') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n case 'enum':\n if (lastTypeName(expression.cast) !== column.enumName.toLowerCase()) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n }\n}\n\nfunction resolveColumn(\n expression: Extract<Expr, {type: 'column'}>,\n table: PredicateTableSpec,\n): ColumnCategory {\n const spec = table.columns[expression.name];\n if (!spec) throw new UnsupportedPredicateError('unpublished-column');\n const dataType = canonicalTypeName(spec.dataType);\n if (dataType === 'bool') return {category: 'boolean', name: expression.name};\n const rank = integerRank(dataType);\n if (rank !== undefined) {\n return {category: 'integer', name: expression.name, rank};\n }\n if (dataType === 'text' || dataType === 'varchar') {\n if (spec.collationIsDeterministic === false) {\n throw new UnsupportedPredicateError('non-deterministic-collation');\n }\n return {category: 'text', name: expression.name};\n }\n if (dataType === 'uuid') return {category: 'uuid', name: expression.name};\n if (spec.pgTypeClass === PostgresTypeClass.Enum) {\n return {\n category: 'enum',\n name: expression.name,\n enumName: spec.dataType.toLowerCase(),\n };\n }\n throw new UnsupportedPredicateError('unsupported-type');\n}\n\nfunction resolveLiteral(\n expression: Expr,\n column: ColumnCategory,\n): IndexPredicateValue {\n let literal = expression;\n let cast: string | undefined;\n while (literal.type === 'cast') {\n cast = literal.cast;\n literal = literal.expression;\n }\n if (literal.type !== 'literal' || literal.value === null) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n\n switch (column.category) {\n case 'boolean': {\n validateLiteralCast(cast, new Set(['bool']));\n if (literal.literalType !== 'boolean') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'boolean', value: literal.value as boolean};\n }\n case 'integer': {\n if (cast !== undefined) {\n const castRank = integerRank(canonicalTypeName(cast));\n if (castRank === undefined || castRank > column.rank) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n }\n if (\n literal.literalType !== 'integer' &&\n !(literal.literalType === 'string' && cast !== undefined)\n ) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {\n type: 'integer',\n value: canonicalInteger(String(literal.value), column.rank),\n };\n }\n case 'text':\n validateLiteralCast(cast, new Set(['text', 'varchar']));\n if (literal.literalType !== 'string') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'string', value: String(literal.value)};\n case 'enum':\n if (\n cast !== undefined &&\n lastTypeName(cast) !== column.enumName.toLowerCase()\n ) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n if (literal.literalType !== 'string') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'string', value: String(literal.value)};\n case 'uuid': {\n validateLiteralCast(cast, new Set(['uuid']));\n const value = String(literal.value).toLowerCase();\n if (literal.literalType !== 'string' || !UUID_RE.test(value)) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'string', value};\n }\n }\n}\n\nfunction validateLiteralCast(\n cast: string | undefined,\n allowed: ReadonlySet<string>,\n): void {\n if (cast !== undefined && !allowed.has(canonicalTypeName(cast))) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n}\n\nfunction canonicalInteger(value: string, rank: 0 | 1 | 2): string {\n if (!INTEGER_RE.test(value)) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n let integer: bigint;\n try {\n integer = BigInt(value);\n } catch {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n const bounds = [\n [-32768n, 32767n],\n [-2147483648n, 2147483647n],\n [-9223372036854775808n, 9223372036854775807n],\n ] as const;\n if (integer < bounds[rank][0] || integer > bounds[rank][1]) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return integer.toString();\n}\n\nfunction canonicalTypeName(type: string): string {\n const name = lastTypeName(type).replace(/\\s+/g, ' ');\n switch (name) {\n case 'boolean':\n return 'bool';\n case 'smallint':\n return 'int2';\n case 'integer':\n return 'int4';\n case 'bigint':\n return 'int8';\n case 'character varying':\n return 'varchar';\n default:\n return name;\n }\n}\n\nfunction lastTypeName(type: string): string {\n return type.split('.').at(-1)?.toLowerCase() ?? '';\n}\n\nfunction integerRank(type: string): 0 | 1 | 2 | undefined {\n switch (type) {\n case 'int2':\n return 0;\n case 'int4':\n return 1;\n case 'int8':\n return 2;\n default:\n return undefined;\n }\n}\n\nfunction isComparison(\n type: Token['type'] | undefined,\n): type is '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=' {\n return (\n type === '=' ||\n type === '<>' ||\n type === '!=' ||\n type === '<' ||\n type === '<=' ||\n type === '>' ||\n type === '>='\n );\n}\n\nfunction tokenize(sql: string): readonly Token[] {\n const tokens: Token[] = [];\n for (let i = 0; i < sql.length;) {\n const char = sql[i];\n if (WHITESPACE_RE.test(char)) {\n i++;\n continue;\n }\n if (char === '\"') {\n const result = readQuoted(sql, i, '\"', false);\n tokens.push({type: 'identifier', value: result.value});\n i = result.end;\n continue;\n }\n const escapeString = (char === 'E' || char === 'e') && sql[i + 1] === \"'\";\n if (char === \"'\" || escapeString) {\n const result = readQuoted(\n sql,\n i + (escapeString ? 1 : 0),\n \"'\",\n escapeString,\n );\n tokens.push({\n type: 'literal',\n literalType: 'string',\n value: result.value,\n });\n i = result.end;\n continue;\n }\n const operator = OPERATOR_TOKEN_RE.exec(sql.slice(i))?.[0];\n if (operator) {\n tokens.push({\n type: operator as '::' | '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=',\n });\n i += operator.length;\n continue;\n }\n const unsupportedOperator = UNSUPPORTED_OPERATOR_TOKEN_RE.exec(\n sql.slice(i),\n )?.[0];\n if (unsupportedOperator) {\n tokens.push({type: 'unsupported', value: unsupportedOperator});\n i += unsupportedOperator.length;\n continue;\n }\n if (\n char === '(' ||\n char === ')' ||\n char === '[' ||\n char === ']' ||\n char === ',' ||\n char === '.'\n ) {\n tokens.push({type: char});\n i++;\n continue;\n }\n const integer = INTEGER_TOKEN_RE.exec(sql.slice(i))?.[0];\n if (integer) {\n tokens.push({type: 'literal', literalType: 'integer', value: integer});\n i += integer.length;\n continue;\n }\n const word = WORD_TOKEN_RE.exec(sql.slice(i))?.[0];\n if (word) {\n const keyword = word.toUpperCase();\n if (unsupportedOperatorWords.has(keyword)) {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n if (keyword === 'TRUE' || keyword === 'FALSE') {\n tokens.push({\n type: 'literal',\n literalType: 'boolean',\n value: keyword === 'TRUE',\n });\n } else if (keyword === 'NULL') {\n tokens.push({type: 'literal', literalType: 'null', value: null});\n } else if (\n keyword === 'AND' ||\n keyword === 'OR' ||\n keyword === 'NOT' ||\n keyword === 'IN' ||\n keyword === 'IS'\n ) {\n tokens.push({type: keyword});\n } else {\n tokens.push({type: 'identifier', value: word.toLowerCase()});\n }\n i += word.length;\n continue;\n }\n tokens.push({type: 'unsupported', value: char});\n i++;\n }\n tokens.push({type: 'EOF'});\n return tokens;\n}\n\nfunction readQuoted(\n sql: string,\n start: number,\n quote: '\"' | \"'\",\n escapes: boolean,\n): {readonly value: string; readonly end: number} {\n let value = '';\n for (let i = start + 1; i < sql.length; i++) {\n const char = sql[i];\n if (char === quote) {\n if (sql[i + 1] === quote) {\n value += quote;\n i++;\n } else {\n return {value, end: i + 1};\n }\n } else if (escapes && char === '\\\\') {\n const escape = sql[++i];\n if (escape === undefined) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n if (escape === 'x') {\n const hex = readEscapeDigits(sql, i + 1, 2, 16, 1);\n value += String.fromCharCode(hex.value);\n i = hex.end - 1;\n } else if (escape === 'u' || escape === 'U') {\n const hex = readEscapeDigits(sql, i + 1, escape === 'u' ? 4 : 8, 16);\n try {\n value += String.fromCodePoint(hex.value);\n } catch {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n i = hex.end - 1;\n } else if (escape >= '0' && escape <= '7') {\n let octal = escape;\n while (\n octal.length < 3 &&\n sql[i + 1] !== undefined &&\n sql[i + 1] >= '0' &&\n sql[i + 1] <= '7'\n ) {\n octal += sql[++i];\n }\n value += String.fromCharCode(Number.parseInt(octal, 8));\n } else if (escape === '\\n') {\n // A backslash-newline pair is a line continuation.\n } else if (escape === '\\r' && sql[i + 1] === '\\n') {\n i++;\n } else {\n value += SIMPLE_ESCAPES[escape] ?? escape;\n }\n } else {\n value += char;\n }\n }\n throw new UnsupportedPredicateError('unsupported-syntax');\n}\n\nfunction readEscapeDigits(\n sql: string,\n start: number,\n length: number,\n radix: 16,\n minLength = length,\n): {readonly value: number; readonly end: number} {\n let end = start;\n while (end < start + length) {\n const digit = sql[end];\n if (\n digit === undefined ||\n !(\n (digit >= '0' && digit <= '9') ||\n (digit.toLowerCase() >= 'a' && digit.toLowerCase() <= 'f')\n )\n ) {\n break;\n }\n end++;\n }\n const digits = sql.slice(start, end);\n if (digits.length < minLength) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n return {value: Number.parseInt(digits, radix), end};\n}\n"],"mappings":";AAOA,IAAM,UACJ;AACF,IAAM,aAAa;AACnB,IAAM,gBAAgB;AACtB,IAAM,oBAAoB;AAC1B,IAAM,gCAAgC;AACtC,IAAM,mBAAmB;AACzB,IAAM,gBAAgB;AACtB,IAAM,iBAAmD;CACvD,GAAG;CACH,GAAG;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;IACZ,OACE,MAAM,SAAS,KACf,IAAI,IAAI,OAAO,KAAA,KACf,IAAI,IAAI,MAAM,OACd,IAAI,IAAI,MAAM,KAEd,SAAS,IAAI,EAAE;IAEjB,SAAS,OAAO,aAAa,OAAO,SAAS,OAAO,CAAC,CAAC;GACxD,OAAO,IAAI,WAAW,MAAM,CAE5B,OAAO,IAAI,WAAW,QAAQ,IAAI,IAAI,OAAO,MAC3C;QAEA,SAAS,eAAe,WAAW;EAEvC,OACE,SAAS;CAEb;CACA,MAAM,IAAI,0BAA0B,oBAAoB;AAC1D;AAEA,SAAS,iBACP,KACA,OACA,QACA,OACA,YAAY,QACoC;CAChD,IAAI,MAAM;CACV,OAAO,MAAM,QAAQ,QAAQ;EAC3B,MAAM,QAAQ,IAAI;EAClB,IACE,UAAU,KAAA,KACV,EACG,SAAS,OAAO,SAAS,OACzB,MAAM,YAAY,KAAK,OAAO,MAAM,YAAY,KAAK,MAGxD;EAEF;CACF;CACA,MAAM,SAAS,IAAI,MAAM,OAAO,GAAG;CACnC,IAAI,OAAO,SAAS,WAClB,MAAM,IAAI,0BAA0B,oBAAoB;CAE1D,OAAO;EAAC,OAAO,OAAO,SAAS,QAAQ,KAAK;EAAG;CAAG;AACpD"}
1
+ {"version":3,"file":"index-predicate.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/index-predicate.ts"],"sourcesContent":["import * as PostgresTypeClass from '../../../../db/postgres-type-class-enum.ts';\nimport type {\n IndexPredicate,\n IndexPredicateValue,\n PublishedTableSpec,\n} from '../../../../db/specs.ts';\n\nconst UUID_RE =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;\nconst INTEGER_RE = /^[+-]?\\d+$/;\nconst WHITESPACE_RE = /\\s/;\nconst OPERATOR_TOKEN_RE = /^(?:::|<>|!=|<=|>=|=|<|>)/;\nconst UNSUPPORTED_OPERATOR_TOKEN_RE = /^(?:!~\\*|!~|~\\*|~)/;\nconst INTEGER_TOKEN_RE = /^[+-]?\\d+/;\nconst WORD_TOKEN_RE = /^[A-Za-z_][A-Za-z0-9_$]*/;\nconst SIMPLE_ESCAPES: Readonly<Record<string, string>> = {\n b: '\\b',\n f: '\\f',\n n: '\\n',\n r: '\\r',\n t: '\\t',\n v: '\\v',\n};\n\nconst unsupportedPredicateReasons = [\n 'unpublished-column',\n 'unsupported-type',\n 'unsupported-operator',\n 'unsupported-function',\n 'unsupported-cast',\n 'unsupported-syntax',\n 'non-deterministic-collation',\n] as const;\n\nexport type UnsupportedPredicateReason =\n (typeof unsupportedPredicateReasons)[number];\n\nexport type PredicateTranslation =\n | {readonly supported: true; readonly predicate: IndexPredicate}\n | {readonly supported: false; readonly reason: UnsupportedPredicateReason};\n\ntype PredicateColumnSpec = PublishedTableSpec['columns'][string] & {\n readonly collationIsDeterministic?: boolean | null | undefined;\n};\n\ntype PredicateTableSpec = Omit<PublishedTableSpec, 'columns'> & {\n readonly columns: Readonly<Record<string, PredicateColumnSpec>>;\n};\n\ntype Expr =\n | {readonly type: 'column'; readonly name: string}\n | {\n readonly type: 'literal';\n readonly value: boolean | string | null;\n readonly literalType: 'boolean' | 'integer' | 'string' | 'null';\n }\n | {readonly type: 'cast'; readonly expression: Expr; readonly cast: string}\n | {readonly type: 'not'; readonly expression: Expr}\n | {\n readonly type: 'and' | 'or';\n readonly expressions: readonly Expr[];\n }\n | {\n readonly type: 'comparison';\n readonly left: Expr;\n readonly op: '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=';\n readonly right: Expr;\n }\n | {\n readonly type: 'null-test';\n readonly expression: Expr;\n readonly not: boolean;\n };\n\ntype Token =\n | {readonly type: 'identifier'; readonly value: string}\n | {\n readonly type: 'literal';\n readonly value: boolean | string | null;\n readonly literalType: 'boolean' | 'integer' | 'string' | 'null';\n }\n | {\n readonly type:\n | '('\n | ')'\n | '['\n | ']'\n | ','\n | '.'\n | '::'\n | '='\n | '<>'\n | '!='\n | '<'\n | '<='\n | '>'\n | '>='\n | 'AND'\n | 'OR'\n | 'NOT'\n | 'IN'\n | 'IS'\n | 'EOF';\n }\n | {readonly type: 'unsupported'; readonly value: string};\n\nclass UnsupportedPredicateError extends Error {\n readonly reason: UnsupportedPredicateReason;\n\n constructor(reason: UnsupportedPredicateReason) {\n super(reason);\n this.reason = reason;\n }\n}\n\nexport function translatePostgresIndexPredicate(\n sql: string,\n table: PredicateTableSpec,\n): PredicateTranslation {\n try {\n const parser = new Parser(tokenize(sql));\n const expression = parser.parse();\n parser.expect('EOF');\n return {supported: true, predicate: normalize(expression, table)};\n } catch (error) {\n return {\n supported: false,\n reason:\n error instanceof UnsupportedPredicateError\n ? error.reason\n : 'unsupported-syntax',\n };\n }\n}\n\nclass Parser {\n readonly #tokens: readonly Token[];\n #position = 0;\n\n constructor(tokens: readonly Token[]) {\n this.#tokens = tokens;\n }\n\n parse(): Expr {\n return this.#parseOr();\n }\n\n #parseOr(): Expr {\n const expressions = [this.#parseAnd()];\n while (this.#take('OR')) expressions.push(this.#parseAnd());\n return expressions.length === 1\n ? expressions[0]\n : {type: 'or', expressions};\n }\n\n #parseAnd(): Expr {\n const expressions = [this.#parseNot()];\n while (this.#take('AND')) expressions.push(this.#parseNot());\n return expressions.length === 1\n ? expressions[0]\n : {type: 'and', expressions};\n }\n\n #parseNot(): Expr {\n if (this.#take('NOT')) {\n return {type: 'not', expression: this.#parseNot()};\n }\n return this.#parseComparison();\n }\n\n #parseComparison(): Expr {\n const left = this.#parseOperand();\n if (this.#take('IS')) {\n const not = this.#take('NOT');\n const token = this.#tokens[this.#position++];\n if (token?.type !== 'literal' || token.literalType !== 'null') {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n return {type: 'null-test', expression: left, not};\n }\n if (this.#take('IN')) {\n this.expect('(');\n const values = this.#parseOperandList(')');\n this.expect(')');\n return comparisons(left, values);\n }\n const op = this.#tokens[this.#position]?.type;\n if (isComparison(op)) {\n this.#position++;\n if (op === '=' && this.#takeIdentifier('ANY')) {\n return comparisons(left, this.#parseLiteralArray());\n }\n return {\n type: 'comparison',\n left,\n op,\n right: this.#parseOperand(),\n };\n }\n const next = this.#tokens[this.#position];\n if (next?.type === 'identifier' && next.value.toUpperCase() === 'COLLATE') {\n throw new UnsupportedPredicateError('non-deterministic-collation');\n }\n if (\n next?.type === 'unsupported' &&\n UNSUPPORTED_OPERATOR_TOKEN_RE.test(next.value)\n ) {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n if (\n next?.type === 'identifier' &&\n unsupportedOperatorWords.has(next.value.toUpperCase())\n ) {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n return left;\n }\n\n #parseLiteralArray(): readonly Expr[] {\n this.expect('(');\n this.#expectIdentifier('ARRAY');\n this.expect('[');\n const values = this.#parseOperandList(']');\n this.expect(']');\n if (this.#take('::')) {\n this.#parseCastName();\n this.expect('[');\n this.expect(']');\n }\n this.expect(')');\n return values;\n }\n\n #parseOperandList(end: ')' | ']'): readonly Expr[] {\n const values: Expr[] = [];\n if (this.#tokens[this.#position]?.type === end) return values;\n values.push(this.#parseOperand());\n while (this.#take(',')) values.push(this.#parseOperand());\n return values;\n }\n\n #parseOperand(): Expr {\n let expression = this.#parsePrimary();\n while (this.#take('::')) {\n expression = {\n type: 'cast',\n expression,\n cast: this.#parseCastName(),\n };\n }\n return expression;\n }\n\n #parsePrimary(): Expr {\n if (this.#take('(')) {\n const expression = this.#parseOr();\n this.expect(')');\n return expression;\n }\n const token = this.#tokens[this.#position++];\n if (token?.type === 'literal') {\n return {\n type: 'literal',\n value: token.value,\n literalType: token.literalType,\n };\n }\n if (token?.type === 'identifier') {\n if (this.#tokens[this.#position]?.type === '(') {\n throw new UnsupportedPredicateError('unsupported-function');\n }\n if (this.#tokens[this.#position]?.type === '.') {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n if (token.value.toUpperCase() === 'COLLATE') {\n throw new UnsupportedPredicateError('non-deterministic-collation');\n }\n return {type: 'column', name: token.value};\n }\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n\n #parseCastName(): string {\n const first = this.#expectIdentifier();\n const parts = [first];\n let separator = '.';\n while (this.#take('.')) parts.push(this.#expectIdentifier());\n const next = this.#tokens[this.#position];\n if (\n first.toLowerCase() === 'character' &&\n next?.type === 'identifier' &&\n next.value.toLowerCase() === 'varying'\n ) {\n parts.push(this.#expectIdentifier());\n separator = ' ';\n }\n if (this.#tokens[this.#position]?.type === '(') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return parts.join(separator);\n }\n\n #expectIdentifier(expected?: string): string {\n const token = this.#tokens[this.#position++];\n if (\n token?.type !== 'identifier' ||\n (expected !== undefined && token.value.toUpperCase() !== expected)\n ) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return token.value;\n }\n\n #takeIdentifier(expected: string): boolean {\n const token = this.#tokens[this.#position];\n if (\n token?.type !== 'identifier' ||\n token.value.toUpperCase() !== expected\n ) {\n return false;\n }\n this.#position++;\n return true;\n }\n\n #take(type: Token['type']): boolean {\n if (this.#tokens[this.#position]?.type !== type) return false;\n this.#position++;\n return true;\n }\n\n expect(type: Token['type']): void {\n if (!this.#take(type)) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n }\n}\n\nconst unsupportedOperatorWords = new Set([\n 'ALL',\n 'BETWEEN',\n 'ILIKE',\n 'LIKE',\n 'SIMILAR',\n]);\n\nfunction comparisons(left: Expr, values: readonly Expr[]): Expr {\n if (values.length === 0) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n const expressions = values.map(\n right => ({type: 'comparison', left, op: '=', right}) as const,\n );\n return expressions.length === 1 ? expressions[0] : {type: 'or', expressions};\n}\n\nfunction normalize(\n expression: Expr,\n table: PredicateTableSpec,\n): IndexPredicate {\n switch (expression.type) {\n case 'and':\n case 'or': {\n const conditions = expression.expressions.map(expr =>\n normalize(expr, table),\n );\n return {\n type: expression.type,\n conditions: conditions.flatMap(condition =>\n condition.type === expression.type\n ? condition.conditions\n : [condition],\n ),\n };\n }\n case 'not': {\n const booleanColumn = resolveBareBooleanColumn(\n expression.expression,\n table,\n );\n if (booleanColumn) {\n return booleanComparison(booleanColumn, false);\n }\n return {type: 'not', condition: normalize(expression.expression, table)};\n }\n case 'column': {\n const column = resolveColumn(expression, table);\n if (column.category !== 'boolean') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return booleanComparison(column.name, true);\n }\n case 'cast': {\n const column = resolveColumnOperand(expression, table);\n if (column.category !== 'boolean') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return booleanComparison(column.name, true);\n }\n case 'null-test': {\n const column = resolveNullTestColumn(expression.expression, table);\n return {\n type: 'null-test',\n column,\n op: expression.not ? 'IS NOT NULL' : 'IS NULL',\n };\n }\n case 'comparison':\n return normalizeComparison(expression, table);\n case 'literal':\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n}\n\nfunction resolveNullTestColumn(\n expression: Expr,\n table: PredicateTableSpec,\n): string {\n if (expression.type === 'cast') {\n return resolveColumnOperand(expression, table).name;\n }\n if (expression.type === 'column' && !table.columns[expression.name]) {\n throw new UnsupportedPredicateError('unpublished-column');\n }\n if (expression.type === 'column') return expression.name;\n throw new UnsupportedPredicateError('unsupported-syntax');\n}\n\nfunction resolveBareBooleanColumn(\n expression: Expr,\n table: PredicateTableSpec,\n): string | undefined {\n if (expression.type !== 'column' && expression.type !== 'cast') {\n return undefined;\n }\n const column = resolveColumnOperand(expression, table);\n return column.category === 'boolean' ? column.name : undefined;\n}\n\nfunction booleanComparison(column: string, value: boolean): IndexPredicate {\n return {\n type: 'comparison',\n column,\n op: '=',\n value: {type: 'boolean', value},\n };\n}\n\ntype ColumnCategory =\n | {readonly category: 'boolean'; readonly name: string}\n | {\n readonly category: 'integer';\n readonly name: string;\n readonly rank: 0 | 1 | 2;\n }\n | {readonly category: 'text'; readonly name: string}\n | {\n readonly category: 'enum';\n readonly name: string;\n readonly enumName: string;\n }\n | {readonly category: 'uuid'; readonly name: string};\n\nfunction normalizeComparison(\n expression: Extract<Expr, {type: 'comparison'}>,\n table: PredicateTableSpec,\n): IndexPredicate {\n const column = resolveColumnOperand(expression.left, table);\n const op = expression.op === '!=' ? '<>' : expression.op;\n if (column.category !== 'integer' && op !== '=' && op !== '<>') {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n const value = resolveLiteral(expression.right, column);\n return {type: 'comparison', column: column.name, op, value};\n}\n\nfunction resolveColumnOperand(\n expression: Expr,\n table: PredicateTableSpec,\n): ColumnCategory {\n if (expression.type === 'column') return resolveColumn(expression, table);\n if (expression.type !== 'cast') {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n const column = resolveColumnOperand(expression.expression, table);\n const cast = canonicalTypeName(expression.cast);\n switch (column.category) {\n case 'integer': {\n const rank = integerRank(cast);\n if (rank === undefined || rank < column.rank) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return {...column, rank};\n }\n case 'text':\n if (cast !== 'text' && cast !== 'varchar') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n case 'boolean':\n if (cast !== 'bool') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n case 'uuid':\n if (cast !== 'uuid') {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n case 'enum':\n if (lastTypeName(expression.cast) !== column.enumName.toLowerCase()) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n return column;\n }\n}\n\nfunction resolveColumn(\n expression: Extract<Expr, {type: 'column'}>,\n table: PredicateTableSpec,\n): ColumnCategory {\n const spec = table.columns[expression.name];\n if (!spec) throw new UnsupportedPredicateError('unpublished-column');\n const dataType = canonicalTypeName(spec.dataType);\n if (dataType === 'bool') return {category: 'boolean', name: expression.name};\n const rank = integerRank(dataType);\n if (rank !== undefined) {\n return {category: 'integer', name: expression.name, rank};\n }\n if (dataType === 'text' || dataType === 'varchar') {\n if (spec.collationIsDeterministic === false) {\n throw new UnsupportedPredicateError('non-deterministic-collation');\n }\n return {category: 'text', name: expression.name};\n }\n if (dataType === 'uuid') return {category: 'uuid', name: expression.name};\n if (spec.pgTypeClass === PostgresTypeClass.Enum) {\n return {\n category: 'enum',\n name: expression.name,\n enumName: spec.dataType.toLowerCase(),\n };\n }\n throw new UnsupportedPredicateError('unsupported-type');\n}\n\nfunction resolveLiteral(\n expression: Expr,\n column: ColumnCategory,\n): IndexPredicateValue {\n let literal = expression;\n let cast: string | undefined;\n while (literal.type === 'cast') {\n cast = literal.cast;\n literal = literal.expression;\n }\n if (literal.type !== 'literal' || literal.value === null) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n\n switch (column.category) {\n case 'boolean': {\n validateLiteralCast(cast, new Set(['bool']));\n if (literal.literalType !== 'boolean') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'boolean', value: literal.value as boolean};\n }\n case 'integer': {\n if (cast !== undefined) {\n const castRank = integerRank(canonicalTypeName(cast));\n if (castRank === undefined || castRank > column.rank) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n }\n if (\n literal.literalType !== 'integer' &&\n !(literal.literalType === 'string' && cast !== undefined)\n ) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {\n type: 'integer',\n value: canonicalInteger(String(literal.value), column.rank),\n };\n }\n case 'text':\n validateLiteralCast(cast, new Set(['text', 'varchar']));\n if (literal.literalType !== 'string') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'string', value: String(literal.value)};\n case 'enum':\n if (\n cast !== undefined &&\n lastTypeName(cast) !== column.enumName.toLowerCase()\n ) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n if (literal.literalType !== 'string') {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'string', value: String(literal.value)};\n case 'uuid': {\n validateLiteralCast(cast, new Set(['uuid']));\n const value = String(literal.value).toLowerCase();\n if (literal.literalType !== 'string' || !UUID_RE.test(value)) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return {type: 'string', value};\n }\n }\n}\n\nfunction validateLiteralCast(\n cast: string | undefined,\n allowed: ReadonlySet<string>,\n): void {\n if (cast !== undefined && !allowed.has(canonicalTypeName(cast))) {\n throw new UnsupportedPredicateError('unsupported-cast');\n }\n}\n\nfunction canonicalInteger(value: string, rank: 0 | 1 | 2): string {\n if (!INTEGER_RE.test(value)) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n let integer: bigint;\n try {\n integer = BigInt(value);\n } catch {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n const bounds = [\n [-32768n, 32767n],\n [-2147483648n, 2147483647n],\n [-9223372036854775808n, 9223372036854775807n],\n ] as const;\n if (integer < bounds[rank][0] || integer > bounds[rank][1]) {\n throw new UnsupportedPredicateError('unsupported-type');\n }\n return integer.toString();\n}\n\nfunction canonicalTypeName(type: string): string {\n const name = lastTypeName(type).replace(/\\s+/g, ' ');\n switch (name) {\n case 'boolean':\n return 'bool';\n case 'smallint':\n return 'int2';\n case 'integer':\n return 'int4';\n case 'bigint':\n return 'int8';\n case 'character varying':\n return 'varchar';\n default:\n return name;\n }\n}\n\nfunction lastTypeName(type: string): string {\n return type.split('.').at(-1)?.toLowerCase() ?? '';\n}\n\nfunction integerRank(type: string): 0 | 1 | 2 | undefined {\n switch (type) {\n case 'int2':\n return 0;\n case 'int4':\n return 1;\n case 'int8':\n return 2;\n default:\n return undefined;\n }\n}\n\nfunction isComparison(\n type: Token['type'] | undefined,\n): type is '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=' {\n return (\n type === '=' ||\n type === '<>' ||\n type === '!=' ||\n type === '<' ||\n type === '<=' ||\n type === '>' ||\n type === '>='\n );\n}\n\nfunction tokenize(sql: string): readonly Token[] {\n const tokens: Token[] = [];\n for (let i = 0; i < sql.length;) {\n const char = sql[i];\n if (WHITESPACE_RE.test(char)) {\n i++;\n continue;\n }\n if (char === '\"') {\n const result = readQuoted(sql, i, '\"', false);\n tokens.push({type: 'identifier', value: result.value});\n i = result.end;\n continue;\n }\n const escapeString = (char === 'E' || char === 'e') && sql[i + 1] === \"'\";\n if (char === \"'\" || escapeString) {\n const result = readQuoted(\n sql,\n i + (escapeString ? 1 : 0),\n \"'\",\n escapeString,\n );\n tokens.push({\n type: 'literal',\n literalType: 'string',\n value: result.value,\n });\n i = result.end;\n continue;\n }\n const operator = OPERATOR_TOKEN_RE.exec(sql.slice(i))?.[0];\n if (operator) {\n tokens.push({\n type: operator as '::' | '=' | '<>' | '!=' | '<' | '<=' | '>' | '>=',\n });\n i += operator.length;\n continue;\n }\n const unsupportedOperator = UNSUPPORTED_OPERATOR_TOKEN_RE.exec(\n sql.slice(i),\n )?.[0];\n if (unsupportedOperator) {\n tokens.push({type: 'unsupported', value: unsupportedOperator});\n i += unsupportedOperator.length;\n continue;\n }\n if (\n char === '(' ||\n char === ')' ||\n char === '[' ||\n char === ']' ||\n char === ',' ||\n char === '.'\n ) {\n tokens.push({type: char});\n i++;\n continue;\n }\n const integer = INTEGER_TOKEN_RE.exec(sql.slice(i))?.[0];\n if (integer) {\n tokens.push({type: 'literal', literalType: 'integer', value: integer});\n i += integer.length;\n continue;\n }\n const word = WORD_TOKEN_RE.exec(sql.slice(i))?.[0];\n if (word) {\n const keyword = word.toUpperCase();\n if (unsupportedOperatorWords.has(keyword)) {\n throw new UnsupportedPredicateError('unsupported-operator');\n }\n if (keyword === 'TRUE' || keyword === 'FALSE') {\n tokens.push({\n type: 'literal',\n literalType: 'boolean',\n value: keyword === 'TRUE',\n });\n } else if (keyword === 'NULL') {\n tokens.push({type: 'literal', literalType: 'null', value: null});\n } else if (\n keyword === 'AND' ||\n keyword === 'OR' ||\n keyword === 'NOT' ||\n keyword === 'IN' ||\n keyword === 'IS'\n ) {\n tokens.push({type: keyword});\n } else {\n tokens.push({type: 'identifier', value: word.toLowerCase()});\n }\n i += word.length;\n continue;\n }\n tokens.push({type: 'unsupported', value: char});\n i++;\n }\n tokens.push({type: 'EOF'});\n return tokens;\n}\n\nfunction readQuoted(\n sql: string,\n start: number,\n quote: '\"' | \"'\",\n escapes: boolean,\n): {readonly value: string; readonly end: number} {\n let value = '';\n for (let i = start + 1; i < sql.length; i++) {\n const char = sql[i];\n if (char === quote) {\n if (sql[i + 1] === quote) {\n value += quote;\n i++;\n } else {\n return {value, end: i + 1};\n }\n } else if (escapes && char === '\\\\') {\n const escape = sql[++i];\n if (escape === undefined) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n if (escape === 'x') {\n const hex = readEscapeDigits(sql, i + 1, 2, 16, 1);\n value += String.fromCharCode(hex.value);\n i = hex.end - 1;\n } else if (escape === 'u' || escape === 'U') {\n const hex = readEscapeDigits(sql, i + 1, escape === 'u' ? 4 : 8, 16);\n try {\n value += String.fromCodePoint(hex.value);\n } catch {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n i = hex.end - 1;\n } else if (escape >= '0' && escape <= '7') {\n let octal = escape;\n while (\n octal.length < 3 &&\n sql[i + 1] !== undefined &&\n sql[i + 1] >= '0' &&\n sql[i + 1] <= '7'\n ) {\n octal += sql[++i];\n }\n value += String.fromCharCode(Number.parseInt(octal, 8));\n } else if (escape === '\\n') {\n // A backslash-newline pair is a line continuation.\n } else if (escape === '\\r' && sql[i + 1] === '\\n') {\n i++;\n } else {\n value += SIMPLE_ESCAPES[escape] ?? escape;\n }\n } else {\n value += char;\n }\n }\n throw new UnsupportedPredicateError('unsupported-syntax');\n}\n\nfunction readEscapeDigits(\n sql: string,\n start: number,\n length: number,\n radix: 16,\n minLength = length,\n): {readonly value: number; readonly end: number} {\n let end = start;\n while (end < start + length) {\n const digit = sql[end];\n if (\n digit === undefined ||\n !(\n (digit >= '0' && digit <= '9') ||\n (digit.toLowerCase() >= 'a' && digit.toLowerCase() <= 'f')\n )\n ) {\n break;\n }\n end++;\n }\n const digits = sql.slice(start, end);\n if (digits.length < minLength) {\n throw new UnsupportedPredicateError('unsupported-syntax');\n }\n return {value: Number.parseInt(digits, radix), end};\n}\n"],"mappings":";AAOA,IAAM,UACJ;AACF,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AO,cAAc,IAAI,KAAK;IACzC,QAAQ;KACN,MAAM,IAAI,0BAA0B,oBAAoB;IAC1D;IACA,IAAI,IAAI,MAAM;GAChB,OAAO,IAAI,UAAU,OAAO,UAAU,KAAK;IACzC,IAAI,QAAQ;IACZ,OACE,MAAM,SAAS,KACf,IAAI,IAAI,OAAO,KAAA,KACf,IAAI,IAAI,MAAM,OACd,IAAI,IAAI,MAAM,KAEd,SAAS,IAAI,EAAE;IAEjB,SAAS,OAAO,aAAa,OAAO,SAAS,OAAO,CAAC,CAAC;GACxD,OAAO,IAAI,WAAW,MAAM,CAE5B,OAAO,IAAI,WAAW,QAAQ,IAAI,IAAI,OAAO,MAC3C;QAEA,SAAS,eAAe,WAAW;EAEvC,OACE,SAAS;CAEb;CACA,MAAM,IAAI,0BAA0B,oBAAoB;AAC1D;AAEA,SAAS,iBACP,KACA,OACA,QACA,OACA,YAAY,QACoC;CAChD,IAAI,MAAM;CACV,OAAO,MAAM,QAAQ,QAAQ;EAC3B,MAAM,QAAQ,IAAI;EAClB,IACE,UAAU,KAAA,KACV,EACG,SAAS,OAAO,SAAS,OACzB,MAAM,YAAY,KAAK,OAAO,MAAM,YAAY,KAAK,MAGxD;EAEF;CACF;CACA,MAAM,SAAS,IAAI,MAAM,OAAO,GAAG;CACnC,IAAI,OAAO,SAAS,WAClB,MAAM,IAAI,0BAA0B,oBAAoB;CAE1D,OAAO;EAAC,OAAO,OAAO,SAAS,QAAQ,KAAK;EAAG;CAAG;AACpD"}
@@ -1,6 +1,5 @@
1
1
  import type { LogContext } from '@rocicorp/logger';
2
2
  import type { PublishedTableWithReplicaIdentity } from './published.ts';
3
- export declare const ALLOWED_APP_ID_CHARACTERS: RegExp;
4
3
  export declare function validate(lc: LogContext, table: PublishedTableWithReplicaIdentity): void;
5
4
  export declare class UnsupportedTableSchemaError extends Error {
6
5
  readonly name = "UnsupportedTableSchemaError";
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAWjD,OAAO,KAAK,EAAC,iCAAiC,EAAC,MAAM,gBAAgB,CAAC;AAEtE,eAAO,MAAM,yBAAyB,QAAiB,CAAC;AAQxD,wBAAgB,QAAQ,CACtB,EAAE,EAAE,UAAU,EACd,KAAK,EAAE,iCAAiC,QAyCzC;AAED,qBAAa,2BAA4B,SAAQ,KAAK;IACpD,QAAQ,CAAC,IAAI,iCAAiC;IAE9C,YAAY,GAAG,EAAE,MAAM,EAEtB;CACF"}
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAWjD,OAAO,KAAK,EAAC,iCAAiC,EAAC,MAAM,gBAAgB,CAAC;AAQtE,wBAAgB,QAAQ,CACtB,EAAE,EAAE,UAAU,EACd,KAAK,EAAE,iCAAiC,QAyCzC;AAED,qBAAa,2BAA4B,SAAQ,KAAK;IACpD,QAAQ,CAAC,IAAI,iCAAiC;IAE9C,YAAY,GAAG,EAAE,MAAM,EAEtB;CACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"validation.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/validation.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {\n mapPostgresToLite,\n warnIfDataTypeSupported,\n} from '../../../../db/pg-to-lite.ts';\nimport {\n Default,\n Index,\n Nothing,\n} from '../../../../db/postgres-replica-identity-enum.ts';\nimport {ZERO_VERSION_COLUMN_NAME} from '../../../replicator/schema/constants.ts';\nimport type {PublishedTableWithReplicaIdentity} from './published.ts';\n\nexport const ALLOWED_APP_ID_CHARACTERS = /^[a-z0-9_]+$/;\n\nconst ALLOWED_TABLE_CHARS = /^[A-Za-z_]+[A-Za-z0-9_-]*$/;\n\n// Dots are allowed in column names since there is no need for\n// a schema/table delimiter when mapped to SQLite names.\nconst ALLOWED_COLUMN_CHARS = /^[A-Za-z_]+[.A-Za-z0-9_-]*$/;\n\nexport function validate(\n lc: LogContext,\n table: PublishedTableWithReplicaIdentity,\n) {\n if (ZERO_VERSION_COLUMN_NAME in table.columns) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" uses reserved column name \"${ZERO_VERSION_COLUMN_NAME}\"`,\n );\n }\n if (!table.primaryKey?.length && table.replicaIdentity === Default) {\n lc.warn?.(\n `\\n\\n\\n` +\n `Table \"${table.name}\" needs a primary key in order to be synced to clients. ` +\n `Add one with 'ALTER TABLE \"${table.name}\" ADD PRIMARY KEY (...)'.` +\n `\\n\\n\\n`,\n );\n }\n if (table.replicaIdentity === Nothing) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" with REPLICA IDENTITY NOTHING cannot be replicated`,\n );\n }\n if (\n table.replicaIdentity === Index &&\n table.replicaIdentityColumns.length === 0\n ) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" is missing its REPLICA IDENTITY INDEX`,\n );\n }\n if (!ALLOWED_TABLE_CHARS.test(table.name)) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" has invalid characters.`,\n );\n }\n for (const [col, spec] of Object.entries(mapPostgresToLite(table).columns)) {\n if (!ALLOWED_COLUMN_CHARS.test(col)) {\n throw new UnsupportedTableSchemaError(\n `Column \"${col}\" in table \"${table.name}\" has invalid characters.`,\n );\n }\n warnIfDataTypeSupported(lc, spec.dataType, table.name, col);\n }\n}\n\nexport class UnsupportedTableSchemaError extends Error {\n readonly name = 'UnsupportedTableSchemaError';\n\n constructor(msg: string) {\n super(msg);\n }\n}\n"],"mappings":";;;AAeA,IAAM,sBAAsB;AAI5B,IAAM,uBAAuB;AAE7B,SAAgB,SACd,IACA,OACA;CACA,IAAA,gBAAgC,MAAM,SACpC,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,+BAA+B,yBAAyB,EAC/E;CAEF,IAAI,CAAC,MAAM,YAAY,UAAU,MAAM,oBAAA,KACrC,GAAG,OACD,gBACY,MAAM,KAAK,qFACS,MAAM,KAAK,gCAE7C;CAEF,IAAI,MAAM,oBAAA,KACR,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,qDACvB;CAEF,IACE,MAAM,oBAAA,OACN,MAAM,uBAAuB,WAAW,GAExC,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,wCACvB;CAEF,IAAI,CAAC,oBAAoB,KAAK,MAAM,IAAI,GACtC,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,0BACvB;CAEF,KAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,kBAAkB,KAAK,CAAC,CAAC,OAAO,GAAG;EAC1E,IAAI,CAAC,qBAAqB,KAAK,GAAG,GAChC,MAAM,IAAI,4BACR,WAAW,IAAI,cAAc,MAAM,KAAK,0BAC1C;EAEF,wBAAwB,IAAI,KAAK,UAAU,MAAM,MAAM,GAAG;CAC5D;AACF;AAEA,IAAa,8BAAb,cAAiD,MAAM;CACrD,OAAgB;CAEhB,YAAY,KAAa;EACvB,MAAM,GAAG;CACX;AACF"}
1
+ {"version":3,"file":"validation.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/schema/validation.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {\n mapPostgresToLite,\n warnIfDataTypeSupported,\n} from '../../../../db/pg-to-lite.ts';\nimport {\n Default,\n Index,\n Nothing,\n} from '../../../../db/postgres-replica-identity-enum.ts';\nimport {ZERO_VERSION_COLUMN_NAME} from '../../../replicator/schema/constants.ts';\nimport type {PublishedTableWithReplicaIdentity} from './published.ts';\n\nconst ALLOWED_TABLE_CHARS = /^[A-Za-z_]+[A-Za-z0-9_-]*$/;\n\n// Dots are allowed in column names since there is no need for\n// a schema/table delimiter when mapped to SQLite names.\nconst ALLOWED_COLUMN_CHARS = /^[A-Za-z_]+[.A-Za-z0-9_-]*$/;\n\nexport function validate(\n lc: LogContext,\n table: PublishedTableWithReplicaIdentity,\n) {\n if (ZERO_VERSION_COLUMN_NAME in table.columns) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" uses reserved column name \"${ZERO_VERSION_COLUMN_NAME}\"`,\n );\n }\n if (!table.primaryKey?.length && table.replicaIdentity === Default) {\n lc.warn?.(\n `\\n\\n\\n` +\n `Table \"${table.name}\" needs a primary key in order to be synced to clients. ` +\n `Add one with 'ALTER TABLE \"${table.name}\" ADD PRIMARY KEY (...)'.` +\n `\\n\\n\\n`,\n );\n }\n if (table.replicaIdentity === Nothing) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" with REPLICA IDENTITY NOTHING cannot be replicated`,\n );\n }\n if (\n table.replicaIdentity === Index &&\n table.replicaIdentityColumns.length === 0\n ) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" is missing its REPLICA IDENTITY INDEX`,\n );\n }\n if (!ALLOWED_TABLE_CHARS.test(table.name)) {\n throw new UnsupportedTableSchemaError(\n `Table \"${table.name}\" has invalid characters.`,\n );\n }\n for (const [col, spec] of Object.entries(mapPostgresToLite(table).columns)) {\n if (!ALLOWED_COLUMN_CHARS.test(col)) {\n throw new UnsupportedTableSchemaError(\n `Column \"${col}\" in table \"${table.name}\" has invalid characters.`,\n );\n }\n warnIfDataTypeSupported(lc, spec.dataType, table.name, col);\n }\n}\n\nexport class UnsupportedTableSchemaError extends Error {\n readonly name = 'UnsupportedTableSchemaError';\n\n constructor(msg: string) {\n super(msg);\n }\n}\n"],"mappings":";;;AAaA,IAAM,sBAAsB;AAI5B,IAAM,uBAAuB;AAE7B,SAAgB,SACd,IACA,OACA;CACA,IAAA,gBAAgC,MAAM,SACpC,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,+BAA+B,yBAAyB,EAC/E;CAEF,IAAI,CAAC,MAAM,YAAY,UAAU,MAAM,oBAAA,KACrC,GAAG,OACD,gBACY,MAAM,KAAK,qFACS,MAAM,KAAK,gCAE7C;CAEF,IAAI,MAAM,oBAAA,KACR,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,qDACvB;CAEF,IACE,MAAM,oBAAA,OACN,MAAM,uBAAuB,WAAW,GAExC,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,wCACvB;CAEF,IAAI,CAAC,oBAAoB,KAAK,MAAM,IAAI,GACtC,MAAM,IAAI,4BACR,UAAU,MAAM,KAAK,0BACvB;CAEF,KAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,kBAAkB,KAAK,CAAC,CAAC,OAAO,GAAG;EAC1E,IAAI,CAAC,qBAAqB,KAAK,GAAG,GAChC,MAAM,IAAI,4BACR,WAAW,IAAI,cAAc,MAAM,KAAK,0BAC1C;EAEF,wBAAwB,IAAI,KAAK,UAAU,MAAM,MAAM,GAAG;CAC5D;AACF;AAEA,IAAa,8BAAb,cAAiD,MAAM;CACrD,OAAgB;CAEhB,YAAY,KAAa;EACvB,MAAM,GAAG;CACX;AACF"}
@@ -1,8 +1,8 @@
1
1
  import { promiseVoid } from "../../../../shared/src/resolved-promises.js";
2
+ import { resolver } from "../../../../shared/src/resolver.js";
2
3
  import { Database } from "../../../../zqlite/src/db.js";
3
4
  import { getOrCreateGauge } from "../../observability/metrics.js";
4
5
  import { Subscription } from "../../types/subscription.js";
5
- import { resolver } from "@rocicorp/resolver";
6
6
  //#region ../zero-cache/src/services/change-streamer/backup-monitor.ts
7
7
  /**
8
8
  * Relays the watermarks backed up by a {@link BackupPoller} to the
@@ -1 +1 @@
1
- {"version":3,"file":"backup-monitor.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/backup-monitor.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {resolver} from '@rocicorp/resolver';\nimport {promiseVoid} from '../../../../shared/src/resolved-promises.ts';\nimport {Database} from '../../../../zqlite/src/db.ts';\nimport {getOrCreateGauge} from '../../observability/metrics.ts';\nimport type {Source} from '../../types/streams.ts';\nimport {Subscription} from '../../types/subscription.ts';\nimport type {SingletonService} from '../service.ts';\n\nexport type BackedUpWatermark = {\n watermark: string;\n writeTimeMs?: number | undefined; // optional for debugging; available on v5\n backupTimeMs: number;\n};\n\n/**\n * Determines which watermarks of the replica are backed up, by whatever means\n * the backup (or its absence) allows.\n */\nexport interface BackupPoller {\n /**\n * Starts polling, returning the stream of backed-up watermarks. Must only\n * be called once.\n */\n start(): Source<BackedUpWatermark>;\n\n /**\n * Requests that the replica be backed up through `watermark` promptly,\n * rather than at the poller's regular cadence, once the replica has reached\n * it. The resulting watermark is emitted on the stream as usual. Pollers\n * that cannot initiate backups do not implement this.\n */\n requestSync?(watermark: string): void;\n}\n\n/**\n * The interface through which the change-streamer consumes backed-up\n * watermarks and requests prompt backups (see {@link BackupMonitor}).\n */\nexport type BackupSource = Pick<BackupMonitor, 'backups' | 'requestSync'>;\n\n/**\n * Relays the watermarks backed up by a {@link BackupPoller} to the\n * change-streamer, which consumes them (see {@link backups}) and requests\n * prompt backups (see {@link requestSync}) as needed.\n */\nexport class BackupMonitor implements SingletonService {\n readonly id = 'backup-monitor';\n readonly #lc: LogContext;\n readonly #poller: BackupPoller;\n readonly #replicaFile: string;\n readonly #firstBackupReceived = resolver();\n // Backed-up watermarks only move forward, so the consumer only needs the\n // latest one. Coalescing keeps at most one outstanding, so that the monitor\n // neither waits on, nor queues for, the consumer.\n readonly #backups = Subscription.create<BackedUpWatermark>({\n coalesce: curr => curr,\n });\n\n #watermarks: Source<BackedUpWatermark> | undefined;\n #stopped = false;\n #latestBackup: BackedUpWatermark | undefined;\n #firstBackupResolved = false;\n /**\n * The replica's `stateVersion` when the monitor started, i.e. the point the\n * backup has to reach before it covers what this task is about to serve.\n * `undefined` when the replica could not be read, which degrades the gate to\n * \"any watermark\" rather than hanging startup forever.\n */\n #coverageTarget: string | undefined;\n\n constructor(lc: LogContext, poller: BackupPoller, replicaFile: string) {\n this.#lc = lc;\n this.#poller = poller;\n this.#replicaFile = replicaFile;\n }\n\n firstBackupReceived() {\n return this.#firstBackupReceived.promise;\n }\n\n /**\n * The stream of backed-up watermarks, each later than the last, coalesced\n * to the latest one that has not been consumed. It ends when the monitor\n * stops.\n */\n backups(): Source<BackedUpWatermark> {\n return this.#backups;\n }\n\n /**\n * Requests that the replica be backed up through `watermark` promptly, if\n * the poller supports it (see {@link BackupPoller.requestSync}). A no-op\n * unless the monitor is running.\n */\n requestSync(watermark: string) {\n if (\n !this.#watermarks ||\n this.#stopped ||\n (this.#latestBackup && this.#latestBackup.watermark >= watermark)\n ) {\n return;\n }\n if (this.#poller.requestSync) {\n this.#lc.info?.(`requesting backup through ${watermark}`);\n this.#poller.requestSync(watermark);\n }\n }\n\n async run() {\n if (this.#stopped) {\n return;\n }\n this.#lc.info?.('starting backup monitor');\n this.#initBackupLagMetric();\n this.#coverageTarget = this.#readReplicaStateVersion();\n\n this.#watermarks = this.#poller.start();\n try {\n for await (const backedUp of this.#watermarks) {\n if (this.#latestBackup) {\n if (backedUp.watermark < this.#latestBackup.watermark) {\n this.#lc.warn?.(\n `ignoring earlier backup watermark ${backedUp.watermark}`,\n {backedUp},\n );\n continue;\n }\n if (backedUp.watermark === this.#latestBackup.watermark) {\n this.#lc.debug?.(\n `ignoring redundant backup watermark ${backedUp.watermark}`,\n {backedUp},\n );\n continue;\n }\n }\n this.#lc.info?.(`received backup watermark ${backedUp.watermark}`, {\n backedUp,\n });\n this.#latestBackup = backedUp;\n this.#backups.push(backedUp);\n this.#checkFirstBackupCovers(backedUp);\n }\n this.#lc.info?.('watermark stream closed. BackupMonitor stopped.');\n } finally {\n this.#backups.cancel();\n }\n }\n\n /**\n * Resolves {@link firstBackupReceived} once the backup actually covers the\n * replica, rather than on the first watermark of any value.\n *\n * A new backup destination starts empty and is filled by re-uploading the\n * local LTX chain, so the first watermarks read back out of it are real but\n * far behind the replica. Releasing the readiness gate on one of those lets\n * the task serve against a backup that does not yet cover it, which is what\n * demotes a restoring view-syncer to Postgres catchup.\n *\n * The target is pinned at startup rather than compared against the replica's\n * current position, which keeps the gate satisfiable under sustained writes.\n */\n #checkFirstBackupCovers(backedUp: BackedUpWatermark) {\n if (this.#firstBackupResolved) {\n return;\n }\n const target = this.#coverageTarget;\n if (target !== undefined && backedUp.watermark < target) {\n this.#lc.info?.(`backup does not yet cover the replica`, {\n backedUp,\n coverageTarget: target,\n });\n return;\n }\n this.#firstBackupResolved = true;\n this.#firstBackupReceived.resolve();\n }\n\n #readReplicaStateVersion(): string | undefined {\n let db;\n try {\n db = new Database(this.#lc, this.#replicaFile, {readonly: true});\n const {stateVersion} = db\n .prepare(/*sql*/ `SELECT stateVersion FROM \"_zero.replicationState\"`)\n .get<{stateVersion: string}>();\n return stateVersion;\n } catch (e) {\n // Without a target the gate degrades to its previous behavior. That is\n // strictly better than blocking startup on a replica we cannot read.\n this.#lc.warn?.(\n `unable to read the replica's stateVersion; ` +\n `the initial backup gate will accept the first watermark`,\n e,\n );\n return undefined;\n } finally {\n db?.close();\n }\n }\n\n stop(): Promise<void> {\n this.#stopped = true;\n this.#watermarks?.cancel();\n this.#backups.cancel();\n return promiseVoid;\n }\n\n #initBackupLagMetric() {\n getOrCreateGauge('replica', 'backup_lag', {\n description:\n 'Latency from when a change is written to the replica ' +\n 'to when it is backed up to litestream. It is expected to create a saw ' +\n 'pattern from 0 to the configured ZERO_LITESTREAM_INCREMENTAL_BACKUP_INTERVAL_MINUTES.',\n unit: 'millisecond',\n }).addCallback(o => {\n const latestBackup = this.#latestBackup;\n if (!latestBackup) {\n this.#lc.warn?.(\n `no backed up watermarks. unable to report replica.backup_lag`,\n );\n return;\n }\n const db = new Database(this.#lc, this.#replicaFile, {readonly: true});\n try {\n const {writeTimeMs} = db\n .prepare(/*sql*/ `SELECT writeTimeMs FROM \"_zero.replicationState\"`)\n .get<{writeTimeMs: number}>();\n const backupLag = Math.max(0, writeTimeMs - latestBackup.backupTimeMs);\n o.observe(backupLag);\n } catch (e) {\n this.#lc.warn?.(`error measuring replica.backup_lag metric`, e);\n } finally {\n db.close();\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;AA8CA,IAAa,gBAAb,MAAuD;CACrD,KAAc;CACd;CACA;CACA;CACA,uBAAgC,SAAS;CAIzC,WAAoB,aAAa,OAA0B,EACzD,WAAU,SAAQ,KACpB,CAAC;CAED;CACA,WAAW;CACX;CACA,uBAAuB;;;;;;;CAOvB;CAEA,YAAY,IAAgB,QAAsB,aAAqB;EACrE,KAAK,MAAM;EACX,KAAK,UAAU;EACf,KAAK,eAAe;CACtB;CAEA,sBAAsB;EACpB,OAAO,KAAK,qBAAqB;CACnC;;;;;;CAOA,UAAqC;EACnC,OAAO,KAAK;CACd;;;;;;CAOA,YAAY,WAAmB;EAC7B,IACE,CAAC,KAAK,eACN,KAAK,YACJ,KAAK,iBAAiB,KAAK,cAAc,aAAa,WAEvD;EAEF,IAAI,KAAK,QAAQ,aAAa;GAC5B,KAAK,IAAI,OAAO,6BAA6B,WAAW;GACxD,KAAK,QAAQ,YAAY,SAAS;EACpC;CACF;CAEA,MAAM,MAAM;EACV,IAAI,KAAK,UACP;EAEF,KAAK,IAAI,OAAO,yBAAyB;EACzC,KAAK,qBAAqB;EAC1B,KAAK,kBAAkB,KAAK,yBAAyB;EAErD,KAAK,cAAc,KAAK,QAAQ,MAAM;EACtC,IAAI;GACF,WAAW,MAAM,YAAY,KAAK,aAAa;IAC7C,IAAI,KAAK,eAAe;KACtB,IAAI,SAAS,YAAY,KAAK,cAAc,WAAW;MACrD,KAAK,IAAI,OACP,qCAAqC,SAAS,aAC9C,EAAC,SAAQ,CACX;MACA;KACF;KACA,IAAI,SAAS,cAAc,KAAK,cAAc,WAAW;MACvD,KAAK,IAAI,QACP,uCAAuC,SAAS,aAChD,EAAC,SAAQ,CACX;MACA;KACF;IACF;IACA,KAAK,IAAI,OAAO,6BAA6B,SAAS,aAAa,EACjE,SACF,CAAC;IACD,KAAK,gBAAgB;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,KAAK,IAAI,OAAO,6CAA6C,CAAC;GAChE,UAAU;IACR,GAAG,MAAM;GACX;EACF,CAAC;CACH;AACF"}
1
+ {"version":3,"file":"backup-monitor.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/backup-monitor.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {promiseVoid} from '../../../../shared/src/resolved-promises.ts';\nimport {resolver} from '../../../../shared/src/resolver.ts';\nimport {Database} from '../../../../zqlite/src/db.ts';\nimport {getOrCreateGauge} from '../../observability/metrics.ts';\nimport type {Source} from '../../types/streams.ts';\nimport {Subscription} from '../../types/subscription.ts';\nimport type {SingletonService} from '../service.ts';\n\nexport type BackedUpWatermark = {\n watermark: string;\n writeTimeMs?: number | undefined; // optional for debugging; available on v5\n backupTimeMs: number;\n};\n\n/**\n * Determines which watermarks of the replica are backed up, by whatever means\n * the backup (or its absence) allows.\n */\nexport interface BackupPoller {\n /**\n * Starts polling, returning the stream of backed-up watermarks. Must only\n * be called once.\n */\n start(): Source<BackedUpWatermark>;\n\n /**\n * Requests that the replica be backed up through `watermark` promptly,\n * rather than at the poller's regular cadence, once the replica has reached\n * it. The resulting watermark is emitted on the stream as usual. Pollers\n * that cannot initiate backups do not implement this.\n */\n requestSync?(watermark: string): void;\n}\n\n/**\n * The interface through which the change-streamer consumes backed-up\n * watermarks and requests prompt backups (see {@link BackupMonitor}).\n */\nexport type BackupSource = Pick<BackupMonitor, 'backups' | 'requestSync'>;\n\n/**\n * Relays the watermarks backed up by a {@link BackupPoller} to the\n * change-streamer, which consumes them (see {@link backups}) and requests\n * prompt backups (see {@link requestSync}) as needed.\n */\nexport class BackupMonitor implements SingletonService {\n readonly id = 'backup-monitor';\n readonly #lc: LogContext;\n readonly #poller: BackupPoller;\n readonly #replicaFile: string;\n readonly #firstBackupReceived = resolver();\n // Backed-up watermarks only move forward, so the consumer only needs the\n // latest one. Coalescing keeps at most one outstanding, so that the monitor\n // neither waits on, nor queues for, the consumer.\n readonly #backups = Subscription.create<BackedUpWatermark>({\n coalesce: curr => curr,\n });\n\n #watermarks: Source<BackedUpWatermark> | undefined;\n #stopped = false;\n #latestBackup: BackedUpWatermark | undefined;\n #firstBackupResolved = false;\n /**\n * The replica's `stateVersion` when the monitor started, i.e. the point the\n * backup has to reach before it covers what this task is about to serve.\n * `undefined` when the replica could not be read, which degrades the gate to\n * \"any watermark\" rather than hanging startup forever.\n */\n #coverageTarget: string | undefined;\n\n constructor(lc: LogContext, poller: BackupPoller, replicaFile: string) {\n this.#lc = lc;\n this.#poller = poller;\n this.#replicaFile = replicaFile;\n }\n\n firstBackupReceived() {\n return this.#firstBackupReceived.promise;\n }\n\n /**\n * The stream of backed-up watermarks, each later than the last, coalesced\n * to the latest one that has not been consumed. It ends when the monitor\n * stops.\n */\n backups(): Source<BackedUpWatermark> {\n return this.#backups;\n }\n\n /**\n * Requests that the replica be backed up through `watermark` promptly, if\n * the poller supports it (see {@link BackupPoller.requestSync}). A no-op\n * unless the monitor is running.\n */\n requestSync(watermark: string) {\n if (\n !this.#watermarks ||\n this.#stopped ||\n (this.#latestBackup && this.#latestBackup.watermark >= watermark)\n ) {\n return;\n }\n if (this.#poller.requestSync) {\n this.#lc.info?.(`requesting backup through ${watermark}`);\n this.#poller.requestSync(watermark);\n }\n }\n\n async run() {\n if (this.#stopped) {\n return;\n }\n this.#lc.info?.('starting backup monitor');\n this.#initBackupLagMetric();\n this.#coverageTarget = this.#readReplicaStateVersion();\n\n this.#watermarks = this.#poller.start();\n try {\n for await (const backedUp of this.#watermarks) {\n if (this.#latestBackup) {\n if (backedUp.watermark < this.#latestBackup.watermark) {\n this.#lc.warn?.(\n `ignoring earlier backup watermark ${backedUp.watermark}`,\n {backedUp},\n );\n continue;\n }\n if (backedUp.watermark === this.#latestBackup.watermark) {\n this.#lc.debug?.(\n `ignoring redundant backup watermark ${backedUp.watermark}`,\n {backedUp},\n );\n continue;\n }\n }\n this.#lc.info?.(`received backup watermark ${backedUp.watermark}`, {\n backedUp,\n });\n this.#latestBackup = backedUp;\n this.#backups.push(backedUp);\n this.#checkFirstBackupCovers(backedUp);\n }\n this.#lc.info?.('watermark stream closed. BackupMonitor stopped.');\n } finally {\n this.#backups.cancel();\n }\n }\n\n /**\n * Resolves {@link firstBackupReceived} once the backup actually covers the\n * replica, rather than on the first watermark of any value.\n *\n * A new backup destination starts empty and is filled by re-uploading the\n * local LTX chain, so the first watermarks read back out of it are real but\n * far behind the replica. Releasing the readiness gate on one of those lets\n * the task serve against a backup that does not yet cover it, which is what\n * demotes a restoring view-syncer to Postgres catchup.\n *\n * The target is pinned at startup rather than compared against the replica's\n * current position, which keeps the gate satisfiable under sustained writes.\n */\n #checkFirstBackupCovers(backedUp: BackedUpWatermark) {\n if (this.#firstBackupResolved) {\n return;\n }\n const target = this.#coverageTarget;\n if (target !== undefined && backedUp.watermark < target) {\n this.#lc.info?.(`backup does not yet cover the replica`, {\n backedUp,\n coverageTarget: target,\n });\n return;\n }\n this.#firstBackupResolved = true;\n this.#firstBackupReceived.resolve();\n }\n\n #readReplicaStateVersion(): string | undefined {\n let db;\n try {\n db = new Database(this.#lc, this.#replicaFile, {readonly: true});\n const {stateVersion} = db\n .prepare(/*sql*/ `SELECT stateVersion FROM \"_zero.replicationState\"`)\n .get<{stateVersion: string}>();\n return stateVersion;\n } catch (e) {\n // Without a target the gate degrades to its previous behavior. That is\n // strictly better than blocking startup on a replica we cannot read.\n this.#lc.warn?.(\n `unable to read the replica's stateVersion; ` +\n `the initial backup gate will accept the first watermark`,\n e,\n );\n return undefined;\n } finally {\n db?.close();\n }\n }\n\n stop(): Promise<void> {\n this.#stopped = true;\n this.#watermarks?.cancel();\n this.#backups.cancel();\n return promiseVoid;\n }\n\n #initBackupLagMetric() {\n getOrCreateGauge('replica', 'backup_lag', {\n description:\n 'Latency from when a change is written to the replica ' +\n 'to when it is backed up to litestream. It is expected to create a saw ' +\n 'pattern from 0 to the configured ZERO_LITESTREAM_INCREMENTAL_BACKUP_INTERVAL_MINUTES.',\n unit: 'millisecond',\n }).addCallback(o => {\n const latestBackup = this.#latestBackup;\n if (!latestBackup) {\n this.#lc.warn?.(\n `no backed up watermarks. unable to report replica.backup_lag`,\n );\n return;\n }\n const db = new Database(this.#lc, this.#replicaFile, {readonly: true});\n try {\n const {writeTimeMs} = db\n .prepare(/*sql*/ `SELECT writeTimeMs FROM \"_zero.replicationState\"`)\n .get<{writeTimeMs: number}>();\n const backupLag = Math.max(0, writeTimeMs - latestBackup.backupTimeMs);\n o.observe(backupLag);\n } catch (e) {\n this.#lc.warn?.(`error measuring replica.backup_lag metric`, e);\n } finally {\n db.close();\n }\n });\n }\n}\n"],"mappings":";;;;;;;;;;;AA8CA,IAAa,gBAAb,MAAuD;CACrD,KAAc;CACd;CACA;CACA;CACA,uBAAgC,SAAS;CAIzC,WAAoB,aAAa,OAA0B,EACzD,WAAU,SAAQ,KACpB,CAAC;CAED;CACA,WAAW;CACX;CACA,uBAAuB;;;;;;;CAOvB;CAEA,YAAY,IAAgB,QAAsB,aAAqB;EACrE,KAAK,MAAM;EACX,KAAK,UAAU;EACf,KAAK,eAAe;CACtB;CAEA,sBAAsB;EACpB,OAAO,KAAK,qBAAqB;CACnC;;;;;;CAOA,UAAqC;EACnC,OAAO,KAAK;CACd;;;;;;CAOA,YAAY,WAAmB;EAC7B,IACE,CAAC,KAAK,eACN,KAAK,YACJ,KAAK,iBAAiB,KAAK,cAAc,aAAa,WAEvD;EAEF,IAAI,KAAK,QAAQ,aAAa;GAC5B,KAAK,IAAI,OAAO,6BAA6B,WAAW;GACxD,KAAK,QAAQ,YAAY,SAAS;EACpC;CACF;CAEA,MAAM,MAAM;EACV,IAAI,KAAK,UACP;EAEF,KAAK,IAAI,OAAO,yBAAyB;EACzC,KAAK,qBAAqB;EAC1B,KAAK,kBAAkB,KAAK,yBAAyB;EAErD,KAAK,cAAc,KAAK,QAAQ,MAAM;EACtC,IAAI;GACF,WAAW,MAAM,YAAY,KAAK,aAAa;IAC7C,IAAI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AK;GAC1B,IAAI,CAAC,cAAc;IACjB,KAAK,IAAI,OACP,8DACF;IACA;GACF;GACA,MAAM,KAAK,IAAI,SAAS,KAAK,KAAK,KAAK,cAAc,EAAC,UAAU,KAAI,CAAC;GACrE,IAAI;IACF,MAAM,EAAC,gBAAe,GACnB,QAAgB,kDAAkD,CAAC,CACnE,IAA2B;IAC9B,MAAM,YAAY,KAAK,IAAI,GAAG,cAAc,aAAa,YAAY;IACrE,EAAE,QAAQ,SAAS;GACrB,SAAS,GAAG;IACV,KAAK,IAAI,OAAO,6CAA6C,CAAC;GAChE,UAAU;IACR,GAAG,MAAM;GACX;EACF,CAAC;CACH;AACF"}
@@ -1,6 +1,6 @@
1
+ import { resolver } from "../../../../shared/src/resolver.js";
1
2
  import { must } from "../../../../shared/src/must.js";
2
3
  import "../../types/streams.js";
3
- import { resolver } from "@rocicorp/resolver";
4
4
  //#region ../zero-cache/src/services/change-streamer/broadcast.ts
5
5
  function preSerializeBatch(changes) {
6
6
  const jsonList = changes.map((c) => c[2]);
@@ -1 +1 @@
1
- {"version":3,"file":"broadcast.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/broadcast.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {resolver} from '@rocicorp/resolver';\nimport {must} from '../../../../shared/src/must.ts';\nimport {isPreSerialized, type PreSerialized} from '../../types/streams.ts';\nimport type {WatermarkedChange} from './change-streamer.ts';\nimport type {Subscriber} from './subscriber.ts';\n\nexport type BroadcastReleaseMode = 'all-subscribers' | 'consensus-timeout';\n\nexport type PreSerializedBatch = PreSerialized & {\n readonly changes: readonly WatermarkedChange[];\n};\n\nexport function isPreSerializedBatch(val: unknown): val is PreSerializedBatch {\n return isPreSerialized(val) && 'changes' in val && Array.isArray(val.changes);\n}\n\nexport function preSerializeBatch(\n changes: readonly WatermarkedChange[],\n): PreSerializedBatch {\n const jsonList = changes.map(c => c[2]);\n const joined = jsonList.join(',');\n const payload = Buffer.from(\n changes.length === 1 ? `,\"msg\":${jsonList[0]}}` : `,\"batch\":[${joined}]}`,\n 'utf8',\n );\n return {\n changes,\n payload,\n byteLength: payload.length,\n };\n}\n\n/**\n * Enables event-driven early release of a {@link Broadcast}. When provided and a\n * majority has acked, the broadcast schedules its own consensus-timeout release\n * proportional to how long the majority took to ack, instead of relying on the\n * Forwarder's periodic progress tick.\n */\nexport type EarlyReleaseOptions = {\n /** Time to wait, proportional to majority completion, before releasing. */\n consensusTimeoutProportion: number;\n /** Injectable timer, for deterministic testing. */\n setTimeoutFn?: typeof setTimeout;\n /** Injectable timer cancellation, for deterministic testing. */\n clearTimeoutFn?: typeof clearTimeout;\n};\n\n/**\n * Initiates and tracks the progress of a change broadcasted to\n * a set of subscribers, implementing **consensus-based timeouts**\n * to effect short-term graceful degradation when a minority of\n * subscribers are behind.\n *\n * ### Background\n *\n * The purpose of flow control is to pull upstream replication changes\n * no faster than the rate they are processed by downstream subscribers\n * in the steady state. In the change-streamer, this is done by occasionally\n * waiting for ACKs from subscribers before continuing; without doing so,\n * I/O buffers fill up and cause the system to spend most of its time in GC.\n *\n * However, the naive algorithm of always waiting for all subscribers (e.g.\n * `Promise.all()`) can behave poorly in scenarios where subscribers\n * are imbalanced:\n * * New subscribers may have a backlog of changes to catch up with.\n * Having all subscribers wait for the new subscriber to catch up results\n * in delaying the entire application.\n * * Broken TCP connections similarly require all subscribers to wait until\n * connection liveness checks kick in and disconnect the subscriber.\n *\n * A simplistic approach is to add a limit to the amount of time waiting for\n * subscribers, i.e. an ack timeout. However, deciding what this timeout\n * should be is non-trivial because of the heterogeneous nature of changes;\n * while most changes operate on single rows and are relatively predictable\n * in terms of running time, some changes are table-wide operations and can\n * legitimately take an arbitrary amount of time. In such scenarios, a\n * timeout that is too short can stop progress on replication altogether.\n *\n * ### Consensus-based Timeout Algorithm\n *\n * To address these shortcomings, a \"consensus-based timeout\" algorithm is\n * used:\n * * Wait for more than half of the subscribers to finish. (In\n * case of a single node, or the case of one replication-manager\n * and one view-syncer, this reduces to waiting for all subscribers.)\n * * Once more than half of the subscribers have finished, proceed after\n * the configured timeout, proportional to the time it took for the\n * majority of subscribers to ack, even if not all subscribers have\n * finished.\n *\n * In other words, the subscribers themselves are used to determine the\n * timeout of each batch of changes; the majority determines this when\n * they complete, upon which a timeout is logically started.\n *\n * In the common case, the remaining subscribers finish soon afterward and\n * the timeout never elapses. However, in pathological cases where a minority\n * of subscribers have a disproportionate amount of load, some will still\n * be processing (or otherwise unresponsive). These subscribers are given\n * a bounded amount of time to catch up at each flushed batch, up to the\n * timeout interval. This guarantees eventual catchup because the\n * subscribers with a backlog of changes necessarily have a higher\n * processing rate than the subscribers that finished (and are made to wait).\n */\nexport class Broadcast {\n /**\n * Sends the change to the subscribers without the tracking machinery.\n * This is suitable for fire-and-forget (i.e. pipelined) sends.\n */\n static withoutTracking(\n subscribers: Iterable<Subscriber>,\n change: WatermarkedChange | readonly WatermarkedChange[],\n ) {\n const changes = toBatch(change);\n const preSerialized = preSerializeBatch(changes);\n for (const sub of subscribers) {\n void sub.sendBatch(changes, preSerialized);\n }\n }\n\n readonly #lc: LogContext;\n readonly #pending: Set<Subscriber>;\n readonly #completed: Completed[];\n readonly #done = resolver();\n #isDone = false;\n #releaseMode: BroadcastReleaseMode | undefined;\n\n readonly #watermark: string;\n readonly #majority: number;\n\n readonly #start = performance.now();\n\n // When set, the broadcast schedules its own consensus-timeout release instead\n // of waiting for the Forwarder's periodic progress tick.\n readonly #earlyReleaseTimeoutProportion: number | undefined;\n readonly #setTimeout: typeof setTimeout;\n readonly #clearTimeout: typeof clearTimeout;\n #earlyReleaseTimer: ReturnType<typeof setTimeout> | undefined;\n\n // Tracks whether the backup-replicator responded. It is a required member\n // of the consensus timeout since it cannot be dropped as a laggard (since\n // that would shutdown the replication-manager itself).\n #needsBackupResponse = false;\n\n /**\n * Broadcasts the `change` to the `subscribers` and tracks their\n * completion.\n */\n constructor(\n lc: LogContext,\n subscribers: Iterable<Subscriber>,\n change: WatermarkedChange | readonly WatermarkedChange[],\n earlyRelease?: EarlyReleaseOptions,\n ) {\n const changes = toBatch(change);\n const preSerialized = preSerializeBatch(changes);\n this.#lc = lc;\n this.#pending = new Set(subscribers);\n this.#completed = [];\n this.#watermark = must(changes.at(-1))[0];\n this.#majority = Math.floor(this.#pending.size / 2) + 1;\n this.#earlyReleaseTimeoutProportion =\n earlyRelease?.consensusTimeoutProportion;\n this.#setTimeout = earlyRelease?.setTimeoutFn ?? setTimeout;\n this.#clearTimeout = earlyRelease?.clearTimeoutFn ?? clearTimeout;\n\n for (const sub of this.#pending) {\n const totalChanges = sub.numPending + changes.length;\n if (sub.mode === 'backup') {\n // Only gate consensus on the backup-replicator after it has finished\n // its initial catchup.\n this.#needsBackupResponse ||= !sub.isBacklogged();\n }\n void sub\n .sendBatch(changes, preSerialized)\n .catch(() => {})\n .finally(() => this.#markCompleted(sub, totalChanges));\n }\n\n // set done if there are no subscribers (mainly for tests)\n if (this.#pending.size === 0) {\n this.#setDone('all-subscribers');\n }\n }\n\n #markCompleted(sub: Subscriber, changes: number) {\n if (this.#isDone) {\n return;\n }\n const elapsed = performance.now() - this.#start;\n sub.trackResponseResult('on-time');\n if (sub.mode === 'backup') {\n this.#needsBackupResponse = false;\n }\n this.#completed.push({sub, changes, elapsed});\n this.#pending.delete(sub);\n if (this.#pending.size === 0) {\n this.#setDone('all-subscribers');\n return;\n }\n // Event-driven consensus-timeout: the moment a majority acks, and it\n // includes the backup-replicator, arm a single release timer\n // proportional to how long the majority took to ack. The broadcast\n // resolves when it fires, unless all subscribers ack first.\n if (\n this.#earlyReleaseTimeoutProportion !== undefined &&\n this.#earlyReleaseTimer === undefined &&\n this.#completed.length >= this.#majority &&\n !this.#needsBackupResponse\n ) {\n const timeoutMs = Math.ceil(\n elapsed * this.#earlyReleaseTimeoutProportion,\n );\n this.#earlyReleaseTimer = this.#setTimeout(\n () => this.#setDone('consensus-timeout'),\n timeoutMs,\n );\n }\n }\n\n #setDone(releaseMode: BroadcastReleaseMode) {\n if (this.#isDone) {\n return;\n }\n if (releaseMode === 'consensus-timeout') {\n const elapsed = performance.now() - this.#start;\n this.#logWithState(\n `continuing with ${this.#pending.size} subscriber(s) still pending`,\n elapsed,\n );\n }\n this.#pending.forEach(sub => sub.trackResponseResult('timed-out'));\n this.#isDone = true;\n this.#releaseMode = releaseMode;\n this.#clearTimeout(this.#earlyReleaseTimer);\n this.#earlyReleaseTimer = undefined;\n this.#done.resolve();\n }\n\n get isDone(): boolean {\n return this.#isDone;\n }\n\n get done(): Promise<void> {\n return this.#done.promise;\n }\n\n get releaseMode(): BroadcastReleaseMode {\n return this.#releaseMode ?? 'all-subscribers';\n }\n\n logSlowRequests(now: number) {\n if (this.#isDone || this.#pending.size === 0) {\n return;\n }\n const elapsed = now - this.#start;\n if (this.#completed.length < this.#majority) {\n if (elapsed >= 1000) {\n this.#logWithState(\n `waiting for at least ${this.#majority} subscribers to finish`,\n elapsed,\n );\n }\n }\n }\n\n #logWithState(msg: string, elapsed: number) {\n this.#lc\n .withContext('watermark', this.#watermark)\n .info?.(`${msg} (${elapsed.toFixed(3)} ms)`, {\n completed: this.#completed.map(d => ({\n id: d.sub.id,\n processed: d.changes,\n elapsed: d.elapsed,\n })),\n pending: Array.from(this.#pending, sub => ({\n id: sub.id,\n ...sub.getStats(),\n })),\n });\n }\n}\n\n/** Tracks the completed result of a single subscriber. */\ntype Completed = {\n sub: Subscriber;\n /** The number of changes processed. */\n changes: number;\n /** The elapsed milliseconds. */\n elapsed: number;\n};\n\nfunction toBatch(\n change: WatermarkedChange | readonly WatermarkedChange[],\n): readonly WatermarkedChange[] {\n if (change.length === 0) {\n return [];\n }\n return Array.isArray(change[0])\n ? (change as readonly WatermarkedChange[])\n : [change as WatermarkedChange];\n}\n"],"mappings":";;;;AAiBA,SAAgB,kBACd,SACoB;CACpB,MAAM,WAAW,QAAQ,KAAI,MAAK,EAAE,EAAE;CACtC,MAAM,SAAS,SAAS,KAAK,GAAG;CAChC,MAAM,UAAU,O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UAAU,yBACvC,OACF;EAAA;CAGN;CAEA,cAAc,KAAa,SAAiB;EAC1C,KAAK,IACF,YAAY,aAAa,KAAK,UAAU,CAAC,CACzC,OAAO,GAAG,IAAI,IAAI,QAAQ,QAAQ,CAAC,EAAE,OAAO;GAC3C,WAAW,KAAK,WAAW,KAAI,OAAM;IACnC,IAAI,EAAE,IAAI;IACV,WAAW,EAAE;IACb,SAAS,EAAE;GACb,EAAE;GACF,SAAS,MAAM,KAAK,KAAK,WAAU,SAAQ;IACzC,IAAI,IAAI;IACR,GAAG,IAAI,SAAS;GAClB,EAAE;EACJ,CAAC;CACL;AACF;AAWA,SAAS,QACP,QAC8B;CAC9B,IAAI,OAAO,WAAW,GACpB,OAAO,CAAC;CAEV,OAAO,MAAM,QAAQ,OAAO,EAAE,IACzB,SACD,CAAC,MAA2B;AAClC"}
1
+ {"version":3,"file":"broadcast.js","names":[],"sources":["../../../../../../zero-cache/src/services/change-streamer/broadcast.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {must} from '../../../../shared/src/must.ts';\nimport {resolver} from '../../../../shared/src/resolver.ts';\nimport {isPreSerialized, type PreSerialized} from '../../types/streams.ts';\nimport type {WatermarkedChange} from './change-streamer.ts';\nimport type {Subscriber} from './subscriber.ts';\n\nexport type BroadcastReleaseMode = 'all-subscribers' | 'consensus-timeout';\n\nexport type PreSerializedBatch = PreSerialized & {\n readonly changes: readonly WatermarkedChange[];\n};\n\nexport function isPreSerializedBatch(val: unknown): val is PreSerializedBatch {\n return isPreSerialized(val) && 'changes' in val && Array.isArray(val.changes);\n}\n\nexport function preSerializeBatch(\n changes: readonly WatermarkedChange[],\n): PreSerializedBatch {\n const jsonList = changes.map(c => c[2]);\n const joined = jsonList.join(',');\n const payload = Buffer.from(\n changes.length === 1 ? `,\"msg\":${jsonList[0]}}` : `,\"batch\":[${joined}]}`,\n 'utf8',\n );\n return {\n changes,\n payload,\n byteLength: payload.length,\n };\n}\n\n/**\n * Enables event-driven early release of a {@link Broadcast}. When provided and a\n * majority has acked, the broadcast schedules its own consensus-timeout release\n * proportional to how long the majority took to ack, instead of relying on the\n * Forwarder's periodic progress tick.\n */\nexport type EarlyReleaseOptions = {\n /** Time to wait, proportional to majority completion, before releasing. */\n consensusTimeoutProportion: number;\n /** Injectable timer, for deterministic testing. */\n setTimeoutFn?: typeof setTimeout;\n /** Injectable timer cancellation, for deterministic testing. */\n clearTimeoutFn?: typeof clearTimeout;\n};\n\n/**\n * Initiates and tracks the progress of a change broadcasted to\n * a set of subscribers, implementing **consensus-based timeouts**\n * to effect short-term graceful degradation when a minority of\n * subscribers are behind.\n *\n * ### Background\n *\n * The purpose of flow control is to pull upstream replication changes\n * no faster than the rate they are processed by downstream subscribers\n * in the steady state. In the change-streamer, this is done by occasionally\n * waiting for ACKs from subscribers before continuing; without doing so,\n * I/O buffers fill up and cause the system to spend most of its time in GC.\n *\n * However, the naive algorithm of always waiting for all subscribers (e.g.\n * `Promise.all()`) can behave poorly in scenarios where subscribers\n * are imbalanced:\n * * New subscribers may have a backlog of changes to catch up with.\n * Having all subscribers wait for the new subscriber to catch up results\n * in delaying the entire application.\n * * Broken TCP connections similarly require all subscribers to wait until\n * connection liveness checks kick in and disconnect the subscriber.\n *\n * A simplistic approach is to add a limit to the amount of time waiting for\n * subscribers, i.e. an ack timeout. However, deciding what this timeout\n * should be is non-trivial because of the heterogeneous nature of changes;\n * while most changes operate on single rows and are relatively predictable\n * in terms of running time, some changes are table-wide operations and can\n * legitimately take an arbitrary amount of time. In such scenarios, a\n * timeout that is too short can stop progress on replication altogether.\n *\n * ### Consensus-based Timeout Algorithm\n *\n * To address these shortcomings, a \"consensus-based timeout\" algorithm is\n * used:\n * * Wait for more than half of the subscribers to finish. (In\n * case of a single node, or the case of one replication-manager\n * and one view-syncer, this reduces to waiting for all subscribers.)\n * * Once more than half of the subscribers have finished, proceed after\n * the configured timeout, proportional to the time it took for the\n * majority of subscribers to ack, even if not all subscribers have\n * finished.\n *\n * In other words, the subscribers themselves are used to determine the\n * timeout of each batch of changes; the majority determines this when\n * they complete, upon which a timeout is logically started.\n *\n * In the common case, the remaining subscribers finish soon afterward and\n * the timeout never elapses. However, in pathological cases where a minority\n * of subscribers have a disproportionate amount of load, some will still\n * be processing (or otherwise unresponsive). These subscribers are given\n * a bounded amount of time to catch up at each flushed batch, up to the\n * timeout interval. This guarantees eventual catchup because the\n * subscribers with a backlog of changes necessarily have a higher\n * processing rate than the subscribers that finished (and are made to wait).\n */\nexport class Broadcast {\n /**\n * Sends the change to the subscribers without the tracking machinery.\n * This is suitable for fire-and-forget (i.e. pipelined) sends.\n */\n static withoutTracking(\n subscribers: Iterable<Subscriber>,\n change: WatermarkedChange | readonly WatermarkedChange[],\n ) {\n const changes = toBatch(change);\n const preSerialized = preSerializeBatch(changes);\n for (const sub of subscribers) {\n void sub.sendBatch(changes, preSerialized);\n }\n }\n\n readonly #lc: LogContext;\n readonly #pending: Set<Subscriber>;\n readonly #completed: Completed[];\n readonly #done = resolver();\n #isDone = false;\n #releaseMode: BroadcastReleaseMode | undefined;\n\n readonly #watermark: string;\n readonly #majority: number;\n\n readonly #start = performance.now();\n\n // When set, the broadcast schedules its own consensus-timeout release instead\n // of waiting for the Forwarder's periodic progress tick.\n readonly #earlyReleaseTimeoutProportion: number | undefined;\n readonly #setTimeout: typeof setTimeout;\n readonly #clearTimeout: typeof clearTimeout;\n #earlyReleaseTimer: ReturnType<typeof setTimeout> | undefined;\n\n // Tracks whether the backup-replicator responded. It is a required member\n // of the consensus timeout since it cannot be dropped as a laggard (since\n // that would shutdown the replication-manager itself).\n #needsBackupResponse = false;\n\n /**\n * Broadcasts the `change` to the `subscribers` and tracks their\n * completion.\n */\n constructor(\n lc: LogContext,\n subscribers: Iterable<Subscriber>,\n change: WatermarkedChange | readonly WatermarkedChange[],\n earlyRelease?: EarlyReleaseOptions,\n ) {\n const changes = toBatch(change);\n const preSerialized = preSerializeBatch(changes);\n this.#lc = lc;\n this.#pending = new Set(subscribers);\n this.#completed = [];\n this.#watermark = must(changes.at(-1))[0];\n this.#majority = Math.floor(this.#pending.size / 2) + 1;\n this.#earlyReleaseTimeoutProportion =\n earlyRelease?.consensusTimeoutProportion;\n this.#setTimeout = earlyRelease?.setTimeoutFn ?? setTimeout;\n this.#clearTimeout = earlyRelease?.clearTimeoutFn ?? clearTimeout;\n\n for (const sub of this.#pending) {\n const totalChanges = sub.numPending + changes.length;\n if (sub.mode === 'backup') {\n // Only gate consensus on the backup-replicator after it has finished\n // its initial catchup.\n this.#needsBackupResponse ||= !sub.isBacklogged();\n }\n void sub\n .sendBatch(changes, preSerialized)\n .catch(() => {})\n .finally(() => this.#markCompleted(sub, totalChanges));\n }\n\n // set done if there are no subscribers (mainly for tests)\n if (this.#pending.size === 0) {\n this.#setDone('all-subscribers');\n }\n }\n\n #markCompleted(sub: Subscriber, changes: number) {\n if (this.#isDone) {\n return;\n }\n const elapsed = performance.now() - this.#start;\n sub.trackResponseResult('on-time');\n if (sub.mode === 'backup') {\n this.#needsBackupResponse = false;\n }\n this.#completed.push({sub, changes, elapsed});\n this.#pending.delete(sub);\n if (this.#pending.size === 0) {\n this.#setDone('all-subscribers');\n return;\n }\n // Event-driven consensus-timeout: the moment a majority acks, and it\n // includes the backup-replicator, arm a single release timer\n // proportional to how long the majority took to ack. The broadcast\n // resolves when it fires, unless all subscribers ack first.\n if (\n this.#earlyReleaseTimeoutProportion !== undefined &&\n this.#earlyReleaseTimer === undefined &&\n this.#completed.length >= this.#majority &&\n !this.#needsBackupResponse\n ) {\n const timeoutMs = Math.ceil(\n elapsed * this.#earlyReleaseTimeoutProportion,\n );\n this.#earlyReleaseTimer = this.#setTimeout(\n () => this.#setDone('consensus-timeout'),\n timeoutMs,\n );\n }\n }\n\n #setDone(releaseMode: BroadcastReleaseMode) {\n if (this.#isDone) {\n return;\n }\n if (releaseMode === 'consensus-timeout') {\n const elapsed = performance.now() - this.#start;\n this.#logWithState(\n `continuing with ${this.#pending.size} subscriber(s) still pending`,\n elapsed,\n );\n }\n this.#pending.forEach(sub => sub.trackResponseResult('timed-out'));\n this.#isDone = true;\n this.#releaseMode = releaseMode;\n this.#clearTimeout(this.#earlyReleaseTimer);\n this.#earlyReleaseTimer = undefined;\n this.#done.resolve();\n }\n\n get isDone(): boolean {\n return this.#isDone;\n }\n\n get done(): Promise<void> {\n return this.#done.promise;\n }\n\n get releaseMode(): BroadcastReleaseMode {\n return this.#releaseMode ?? 'all-subscribers';\n }\n\n logSlowRequests(now: number) {\n if (this.#isDone || this.#pending.size === 0) {\n return;\n }\n const elapsed = now - this.#start;\n if (this.#completed.length < this.#majority) {\n if (elapsed >= 1000) {\n this.#logWithState(\n `waiting for at least ${this.#majority} subscribers to finish`,\n elapsed,\n );\n }\n }\n }\n\n #logWithState(msg: string, elapsed: number) {\n this.#lc\n .withContext('watermark', this.#watermark)\n .info?.(`${msg} (${elapsed.toFixed(3)} ms)`, {\n completed: this.#completed.map(d => ({\n id: d.sub.id,\n processed: d.changes,\n elapsed: d.elapsed,\n })),\n pending: Array.from(this.#pending, sub => ({\n id: sub.id,\n ...sub.getStats(),\n })),\n });\n }\n}\n\n/** Tracks the completed result of a single subscriber. */\ntype Completed = {\n sub: Subscriber;\n /** The number of changes processed. */\n changes: number;\n /** The elapsed milliseconds. */\n elapsed: number;\n};\n\nfunction toBatch(\n change: WatermarkedChange | readonly WatermarkedChange[],\n): readonly WatermarkedChange[] {\n if (change.length === 0) {\n return [];\n }\n return Array.isArray(change[0])\n ? (change as readonly WatermarkedChange[])\n : [change as WatermarkedChange];\n}\n"],"mappings":";;;;AAiBA,SAAgB,kBACd,SACoB;CACpB,MAAM,WAAW,QAAQ,KAAI,MAAK,EAAE,EAAE;CACtC,MAAM,SAAS,SAAS,KAAK,GAAG;CAChC,MAAM,UAAU,OAAO,KACrB,QAAQ,WAAW,IAAI,UAAU,SAAS,GAAG,KAAK,aAAa,OAAO,KACtE,MACF;CACA,OAAO;EACL;EACA;EACA,YAAY,QAAQ;CACtB;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IAAI,IAAI,QAAQ,QAAQ,CAAC,EAAE,OAAO;GAC3C,WAAW,KAAK,WAAW,KAAI,OAAM;IACnC,IAAI,EAAE,IAAI;IACV,WAAW,EAAE;IACb,SAAS,EAAE;GACb,EAAE;GACF,SAAS,MAAM,KAAK,KAAK,WAAU,SAAQ;IACzC,IAAI,IAAI;IACR,GAAG,IAAI,SAAS;GAClB,EAAE;EACJ,CAAC;CACL;AACF;AAWA,SAAS,QACP,QAC8B;CAC9B,IAAI,OAAO,WAAW,GACpB,OAAO,CAAC;CAEV,OAAO,MAAM,QAAQ,OAAO,EAAE,IACzB,SACD,CAAC,MAA2B;AAClC"}
@@ -271,6 +271,6 @@ function toStartSubscriptionContext(ctx) {
271
271
  };
272
272
  }
273
273
  //#endregion
274
- export { ChangeStreamerHttpClient, ChangeStreamerHttpServer, getSubscriberContext };
274
+ export { ChangeStreamerHttpClient, ChangeStreamerHttpServer };
275
275
 
276
276
  //# sourceMappingURL=change-streamer-http.js.map