@powersync/service-core 0.0.0-dev-20240620165206

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 (454) hide show
  1. package/.probes/.gitkeep +0 -0
  2. package/CHANGELOG.md +82 -0
  3. package/LICENSE +67 -0
  4. package/README.md +3 -0
  5. package/dist/api/api-index.d.ts +2 -0
  6. package/dist/api/api-index.js +3 -0
  7. package/dist/api/api-index.js.map +1 -0
  8. package/dist/api/diagnostics.d.ts +21 -0
  9. package/dist/api/diagnostics.js +183 -0
  10. package/dist/api/diagnostics.js.map +1 -0
  11. package/dist/api/schema.d.ts +5 -0
  12. package/dist/api/schema.js +88 -0
  13. package/dist/api/schema.js.map +1 -0
  14. package/dist/auth/CachedKeyCollector.d.ts +46 -0
  15. package/dist/auth/CachedKeyCollector.js +116 -0
  16. package/dist/auth/CachedKeyCollector.js.map +1 -0
  17. package/dist/auth/CompoundKeyCollector.d.ts +8 -0
  18. package/dist/auth/CompoundKeyCollector.js +23 -0
  19. package/dist/auth/CompoundKeyCollector.js.map +1 -0
  20. package/dist/auth/JwtPayload.d.ts +10 -0
  21. package/dist/auth/JwtPayload.js +2 -0
  22. package/dist/auth/JwtPayload.js.map +1 -0
  23. package/dist/auth/KeyCollector.d.ts +24 -0
  24. package/dist/auth/KeyCollector.js +2 -0
  25. package/dist/auth/KeyCollector.js.map +1 -0
  26. package/dist/auth/KeySpec.d.ts +26 -0
  27. package/dist/auth/KeySpec.js +49 -0
  28. package/dist/auth/KeySpec.js.map +1 -0
  29. package/dist/auth/KeyStore.d.ts +39 -0
  30. package/dist/auth/KeyStore.js +131 -0
  31. package/dist/auth/KeyStore.js.map +1 -0
  32. package/dist/auth/LeakyBucket.d.ts +39 -0
  33. package/dist/auth/LeakyBucket.js +57 -0
  34. package/dist/auth/LeakyBucket.js.map +1 -0
  35. package/dist/auth/RemoteJWKSCollector.d.ts +24 -0
  36. package/dist/auth/RemoteJWKSCollector.js +106 -0
  37. package/dist/auth/RemoteJWKSCollector.js.map +1 -0
  38. package/dist/auth/StaticKeyCollector.d.ts +14 -0
  39. package/dist/auth/StaticKeyCollector.js +19 -0
  40. package/dist/auth/StaticKeyCollector.js.map +1 -0
  41. package/dist/auth/SupabaseKeyCollector.d.ts +22 -0
  42. package/dist/auth/SupabaseKeyCollector.js +61 -0
  43. package/dist/auth/SupabaseKeyCollector.js.map +1 -0
  44. package/dist/auth/auth-index.d.ts +10 -0
  45. package/dist/auth/auth-index.js +11 -0
  46. package/dist/auth/auth-index.js.map +1 -0
  47. package/dist/db/db-index.d.ts +1 -0
  48. package/dist/db/db-index.js +2 -0
  49. package/dist/db/db-index.js.map +1 -0
  50. package/dist/db/mongo.d.ts +29 -0
  51. package/dist/db/mongo.js +65 -0
  52. package/dist/db/mongo.js.map +1 -0
  53. package/dist/entry/cli-entry.d.ts +15 -0
  54. package/dist/entry/cli-entry.js +36 -0
  55. package/dist/entry/cli-entry.js.map +1 -0
  56. package/dist/entry/commands/config-command.d.ts +10 -0
  57. package/dist/entry/commands/config-command.js +21 -0
  58. package/dist/entry/commands/config-command.js.map +1 -0
  59. package/dist/entry/commands/migrate-action.d.ts +2 -0
  60. package/dist/entry/commands/migrate-action.js +18 -0
  61. package/dist/entry/commands/migrate-action.js.map +1 -0
  62. package/dist/entry/commands/start-action.d.ts +3 -0
  63. package/dist/entry/commands/start-action.js +15 -0
  64. package/dist/entry/commands/start-action.js.map +1 -0
  65. package/dist/entry/commands/teardown-action.d.ts +2 -0
  66. package/dist/entry/commands/teardown-action.js +17 -0
  67. package/dist/entry/commands/teardown-action.js.map +1 -0
  68. package/dist/entry/entry-index.d.ts +5 -0
  69. package/dist/entry/entry-index.js +6 -0
  70. package/dist/entry/entry-index.js.map +1 -0
  71. package/dist/index.d.ts +25 -0
  72. package/dist/index.js +28 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/locks/LockManager.d.ts +10 -0
  75. package/dist/locks/LockManager.js +7 -0
  76. package/dist/locks/LockManager.js.map +1 -0
  77. package/dist/locks/MongoLocks.d.ts +36 -0
  78. package/dist/locks/MongoLocks.js +81 -0
  79. package/dist/locks/MongoLocks.js.map +1 -0
  80. package/dist/locks/locks-index.d.ts +2 -0
  81. package/dist/locks/locks-index.js +3 -0
  82. package/dist/locks/locks-index.js.map +1 -0
  83. package/dist/metrics/Metrics.d.ts +30 -0
  84. package/dist/metrics/Metrics.js +176 -0
  85. package/dist/metrics/Metrics.js.map +1 -0
  86. package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
  87. package/dist/migrations/db/migrations/1684951997326-init.js +31 -0
  88. package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
  89. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
  90. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
  91. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
  92. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
  93. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
  94. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
  95. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
  96. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +27 -0
  97. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
  98. package/dist/migrations/definitions.d.ts +18 -0
  99. package/dist/migrations/definitions.js +6 -0
  100. package/dist/migrations/definitions.js.map +1 -0
  101. package/dist/migrations/executor.d.ts +16 -0
  102. package/dist/migrations/executor.js +64 -0
  103. package/dist/migrations/executor.js.map +1 -0
  104. package/dist/migrations/migrations-index.d.ts +3 -0
  105. package/dist/migrations/migrations-index.js +4 -0
  106. package/dist/migrations/migrations-index.js.map +1 -0
  107. package/dist/migrations/migrations.d.ts +10 -0
  108. package/dist/migrations/migrations.js +90 -0
  109. package/dist/migrations/migrations.js.map +1 -0
  110. package/dist/migrations/store/migration-store.d.ts +11 -0
  111. package/dist/migrations/store/migration-store.js +46 -0
  112. package/dist/migrations/store/migration-store.js.map +1 -0
  113. package/dist/replication/ErrorRateLimiter.d.ts +17 -0
  114. package/dist/replication/ErrorRateLimiter.js +43 -0
  115. package/dist/replication/ErrorRateLimiter.js.map +1 -0
  116. package/dist/replication/PgRelation.d.ts +16 -0
  117. package/dist/replication/PgRelation.js +26 -0
  118. package/dist/replication/PgRelation.js.map +1 -0
  119. package/dist/replication/WalConnection.d.ts +34 -0
  120. package/dist/replication/WalConnection.js +190 -0
  121. package/dist/replication/WalConnection.js.map +1 -0
  122. package/dist/replication/WalStream.d.ts +57 -0
  123. package/dist/replication/WalStream.js +515 -0
  124. package/dist/replication/WalStream.js.map +1 -0
  125. package/dist/replication/WalStreamManager.d.ts +30 -0
  126. package/dist/replication/WalStreamManager.js +198 -0
  127. package/dist/replication/WalStreamManager.js.map +1 -0
  128. package/dist/replication/WalStreamRunner.d.ts +38 -0
  129. package/dist/replication/WalStreamRunner.js +155 -0
  130. package/dist/replication/WalStreamRunner.js.map +1 -0
  131. package/dist/replication/replication-index.d.ts +7 -0
  132. package/dist/replication/replication-index.js +8 -0
  133. package/dist/replication/replication-index.js.map +1 -0
  134. package/dist/replication/util.d.ts +9 -0
  135. package/dist/replication/util.js +62 -0
  136. package/dist/replication/util.js.map +1 -0
  137. package/dist/routes/auth.d.ts +56 -0
  138. package/dist/routes/auth.js +182 -0
  139. package/dist/routes/auth.js.map +1 -0
  140. package/dist/routes/endpoints/admin.d.ts +1011 -0
  141. package/dist/routes/endpoints/admin.js +207 -0
  142. package/dist/routes/endpoints/admin.js.map +1 -0
  143. package/dist/routes/endpoints/checkpointing.d.ts +76 -0
  144. package/dist/routes/endpoints/checkpointing.js +36 -0
  145. package/dist/routes/endpoints/checkpointing.js.map +1 -0
  146. package/dist/routes/endpoints/dev.d.ts +312 -0
  147. package/dist/routes/endpoints/dev.js +172 -0
  148. package/dist/routes/endpoints/dev.js.map +1 -0
  149. package/dist/routes/endpoints/route-endpoints-index.d.ts +6 -0
  150. package/dist/routes/endpoints/route-endpoints-index.js +7 -0
  151. package/dist/routes/endpoints/route-endpoints-index.js.map +1 -0
  152. package/dist/routes/endpoints/socket-route.d.ts +2 -0
  153. package/dist/routes/endpoints/socket-route.js +119 -0
  154. package/dist/routes/endpoints/socket-route.js.map +1 -0
  155. package/dist/routes/endpoints/sync-rules.d.ts +174 -0
  156. package/dist/routes/endpoints/sync-rules.js +202 -0
  157. package/dist/routes/endpoints/sync-rules.js.map +1 -0
  158. package/dist/routes/endpoints/sync-stream.d.ts +132 -0
  159. package/dist/routes/endpoints/sync-stream.js +83 -0
  160. package/dist/routes/endpoints/sync-stream.js.map +1 -0
  161. package/dist/routes/hooks.d.ts +10 -0
  162. package/dist/routes/hooks.js +32 -0
  163. package/dist/routes/hooks.js.map +1 -0
  164. package/dist/routes/route-register.d.ts +10 -0
  165. package/dist/routes/route-register.js +87 -0
  166. package/dist/routes/route-register.js.map +1 -0
  167. package/dist/routes/router-socket.d.ts +10 -0
  168. package/dist/routes/router-socket.js +5 -0
  169. package/dist/routes/router-socket.js.map +1 -0
  170. package/dist/routes/router.d.ts +26 -0
  171. package/dist/routes/router.js +7 -0
  172. package/dist/routes/router.js.map +1 -0
  173. package/dist/routes/routes-index.d.ts +6 -0
  174. package/dist/routes/routes-index.js +7 -0
  175. package/dist/routes/routes-index.js.map +1 -0
  176. package/dist/runner/teardown.d.ts +2 -0
  177. package/dist/runner/teardown.js +94 -0
  178. package/dist/runner/teardown.js.map +1 -0
  179. package/dist/storage/BucketStorage.d.ts +307 -0
  180. package/dist/storage/BucketStorage.js +25 -0
  181. package/dist/storage/BucketStorage.js.map +1 -0
  182. package/dist/storage/ChecksumCache.d.ts +50 -0
  183. package/dist/storage/ChecksumCache.js +234 -0
  184. package/dist/storage/ChecksumCache.js.map +1 -0
  185. package/dist/storage/MongoBucketStorage.d.ts +52 -0
  186. package/dist/storage/MongoBucketStorage.js +409 -0
  187. package/dist/storage/MongoBucketStorage.js.map +1 -0
  188. package/dist/storage/SourceTable.d.ts +39 -0
  189. package/dist/storage/SourceTable.js +50 -0
  190. package/dist/storage/SourceTable.js.map +1 -0
  191. package/dist/storage/mongo/MongoBucketBatch.d.ts +48 -0
  192. package/dist/storage/mongo/MongoBucketBatch.js +581 -0
  193. package/dist/storage/mongo/MongoBucketBatch.js.map +1 -0
  194. package/dist/storage/mongo/MongoIdSequence.d.ts +12 -0
  195. package/dist/storage/mongo/MongoIdSequence.js +21 -0
  196. package/dist/storage/mongo/MongoIdSequence.js.map +1 -0
  197. package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +9 -0
  198. package/dist/storage/mongo/MongoPersistedSyncRules.js +9 -0
  199. package/dist/storage/mongo/MongoPersistedSyncRules.js.map +1 -0
  200. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +20 -0
  201. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +26 -0
  202. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +1 -0
  203. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +29 -0
  204. package/dist/storage/mongo/MongoSyncBucketStorage.js +391 -0
  205. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +1 -0
  206. package/dist/storage/mongo/MongoSyncRulesLock.d.ts +16 -0
  207. package/dist/storage/mongo/MongoSyncRulesLock.js +65 -0
  208. package/dist/storage/mongo/MongoSyncRulesLock.js.map +1 -0
  209. package/dist/storage/mongo/OperationBatch.d.ts +26 -0
  210. package/dist/storage/mongo/OperationBatch.js +101 -0
  211. package/dist/storage/mongo/OperationBatch.js.map +1 -0
  212. package/dist/storage/mongo/PersistedBatch.d.ts +46 -0
  213. package/dist/storage/mongo/PersistedBatch.js +213 -0
  214. package/dist/storage/mongo/PersistedBatch.js.map +1 -0
  215. package/dist/storage/mongo/db.d.ts +26 -0
  216. package/dist/storage/mongo/db.js +35 -0
  217. package/dist/storage/mongo/db.js.map +1 -0
  218. package/dist/storage/mongo/models.d.ts +140 -0
  219. package/dist/storage/mongo/models.js +27 -0
  220. package/dist/storage/mongo/models.js.map +1 -0
  221. package/dist/storage/mongo/util.d.ts +26 -0
  222. package/dist/storage/mongo/util.js +81 -0
  223. package/dist/storage/mongo/util.js.map +1 -0
  224. package/dist/storage/storage-index.d.ts +14 -0
  225. package/dist/storage/storage-index.js +15 -0
  226. package/dist/storage/storage-index.js.map +1 -0
  227. package/dist/sync/BroadcastIterable.d.ts +38 -0
  228. package/dist/sync/BroadcastIterable.js +153 -0
  229. package/dist/sync/BroadcastIterable.js.map +1 -0
  230. package/dist/sync/LastValueSink.d.ts +25 -0
  231. package/dist/sync/LastValueSink.js +84 -0
  232. package/dist/sync/LastValueSink.js.map +1 -0
  233. package/dist/sync/merge.d.ts +39 -0
  234. package/dist/sync/merge.js +175 -0
  235. package/dist/sync/merge.js.map +1 -0
  236. package/dist/sync/safeRace.d.ts +1 -0
  237. package/dist/sync/safeRace.js +91 -0
  238. package/dist/sync/safeRace.js.map +1 -0
  239. package/dist/sync/sync-index.d.ts +6 -0
  240. package/dist/sync/sync-index.js +7 -0
  241. package/dist/sync/sync-index.js.map +1 -0
  242. package/dist/sync/sync.d.ts +18 -0
  243. package/dist/sync/sync.js +259 -0
  244. package/dist/sync/sync.js.map +1 -0
  245. package/dist/sync/util.d.ts +26 -0
  246. package/dist/sync/util.js +73 -0
  247. package/dist/sync/util.js.map +1 -0
  248. package/dist/system/CorePowerSyncSystem.d.ts +23 -0
  249. package/dist/system/CorePowerSyncSystem.js +52 -0
  250. package/dist/system/CorePowerSyncSystem.js.map +1 -0
  251. package/dist/system/system-index.d.ts +1 -0
  252. package/dist/system/system-index.js +2 -0
  253. package/dist/system/system-index.js.map +1 -0
  254. package/dist/util/Mutex.d.ts +47 -0
  255. package/dist/util/Mutex.js +132 -0
  256. package/dist/util/Mutex.js.map +1 -0
  257. package/dist/util/PgManager.d.ts +24 -0
  258. package/dist/util/PgManager.js +55 -0
  259. package/dist/util/PgManager.js.map +1 -0
  260. package/dist/util/alerting.d.ts +2 -0
  261. package/dist/util/alerting.js +8 -0
  262. package/dist/util/alerting.js.map +1 -0
  263. package/dist/util/config/collectors/config-collector.d.ts +29 -0
  264. package/dist/util/config/collectors/config-collector.js +116 -0
  265. package/dist/util/config/collectors/config-collector.js.map +1 -0
  266. package/dist/util/config/collectors/impl/base64-config-collector.d.ts +6 -0
  267. package/dist/util/config/collectors/impl/base64-config-collector.js +15 -0
  268. package/dist/util/config/collectors/impl/base64-config-collector.js.map +1 -0
  269. package/dist/util/config/collectors/impl/fallback-config-collector.d.ts +11 -0
  270. package/dist/util/config/collectors/impl/fallback-config-collector.js +19 -0
  271. package/dist/util/config/collectors/impl/fallback-config-collector.js.map +1 -0
  272. package/dist/util/config/collectors/impl/filesystem-config-collector.d.ts +6 -0
  273. package/dist/util/config/collectors/impl/filesystem-config-collector.js +37 -0
  274. package/dist/util/config/collectors/impl/filesystem-config-collector.js.map +1 -0
  275. package/dist/util/config/compound-config-collector.d.ts +32 -0
  276. package/dist/util/config/compound-config-collector.js +130 -0
  277. package/dist/util/config/compound-config-collector.js.map +1 -0
  278. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.d.ts +7 -0
  279. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js +17 -0
  280. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js.map +1 -0
  281. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.d.ts +7 -0
  282. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js +21 -0
  283. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js.map +1 -0
  284. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.d.ts +7 -0
  285. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js +17 -0
  286. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js.map +1 -0
  287. package/dist/util/config/sync-rules/sync-collector.d.ts +6 -0
  288. package/dist/util/config/sync-rules/sync-collector.js +3 -0
  289. package/dist/util/config/sync-rules/sync-collector.js.map +1 -0
  290. package/dist/util/config/types.d.ts +57 -0
  291. package/dist/util/config/types.js +7 -0
  292. package/dist/util/config/types.js.map +1 -0
  293. package/dist/util/config.d.ts +7 -0
  294. package/dist/util/config.js +35 -0
  295. package/dist/util/config.js.map +1 -0
  296. package/dist/util/env.d.ts +9 -0
  297. package/dist/util/env.js +26 -0
  298. package/dist/util/env.js.map +1 -0
  299. package/dist/util/memory-tracking.d.ts +7 -0
  300. package/dist/util/memory-tracking.js +58 -0
  301. package/dist/util/memory-tracking.js.map +1 -0
  302. package/dist/util/migration_lib.d.ts +11 -0
  303. package/dist/util/migration_lib.js +64 -0
  304. package/dist/util/migration_lib.js.map +1 -0
  305. package/dist/util/pgwire_utils.d.ts +24 -0
  306. package/dist/util/pgwire_utils.js +117 -0
  307. package/dist/util/pgwire_utils.js.map +1 -0
  308. package/dist/util/populate_test_data.d.ts +8 -0
  309. package/dist/util/populate_test_data.js +65 -0
  310. package/dist/util/populate_test_data.js.map +1 -0
  311. package/dist/util/protocol-types.d.ts +182 -0
  312. package/dist/util/protocol-types.js +42 -0
  313. package/dist/util/protocol-types.js.map +1 -0
  314. package/dist/util/secs.d.ts +2 -0
  315. package/dist/util/secs.js +49 -0
  316. package/dist/util/secs.js.map +1 -0
  317. package/dist/util/util-index.d.ts +22 -0
  318. package/dist/util/util-index.js +23 -0
  319. package/dist/util/util-index.js.map +1 -0
  320. package/dist/util/utils.d.ts +17 -0
  321. package/dist/util/utils.js +92 -0
  322. package/dist/util/utils.js.map +1 -0
  323. package/package.json +59 -0
  324. package/src/api/api-index.ts +2 -0
  325. package/src/api/diagnostics.ts +221 -0
  326. package/src/api/schema.ts +99 -0
  327. package/src/auth/CachedKeyCollector.ts +132 -0
  328. package/src/auth/CompoundKeyCollector.ts +33 -0
  329. package/src/auth/JwtPayload.ts +11 -0
  330. package/src/auth/KeyCollector.ts +27 -0
  331. package/src/auth/KeySpec.ts +67 -0
  332. package/src/auth/KeyStore.ts +156 -0
  333. package/src/auth/LeakyBucket.ts +66 -0
  334. package/src/auth/RemoteJWKSCollector.ts +130 -0
  335. package/src/auth/StaticKeyCollector.ts +21 -0
  336. package/src/auth/SupabaseKeyCollector.ts +67 -0
  337. package/src/auth/auth-index.ts +10 -0
  338. package/src/db/db-index.ts +1 -0
  339. package/src/db/mongo.ts +72 -0
  340. package/src/entry/cli-entry.ts +40 -0
  341. package/src/entry/commands/config-command.ts +36 -0
  342. package/src/entry/commands/migrate-action.ts +25 -0
  343. package/src/entry/commands/start-action.ts +24 -0
  344. package/src/entry/commands/teardown-action.ts +23 -0
  345. package/src/entry/entry-index.ts +5 -0
  346. package/src/index.ts +40 -0
  347. package/src/locks/LockManager.ts +16 -0
  348. package/src/locks/MongoLocks.ts +142 -0
  349. package/src/locks/locks-index.ts +2 -0
  350. package/src/metrics/Metrics.ts +265 -0
  351. package/src/migrations/db/migrations/1684951997326-init.ts +33 -0
  352. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
  353. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +99 -0
  354. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +32 -0
  355. package/src/migrations/definitions.ts +21 -0
  356. package/src/migrations/executor.ts +87 -0
  357. package/src/migrations/migrations-index.ts +3 -0
  358. package/src/migrations/migrations.ts +118 -0
  359. package/src/migrations/store/migration-store.ts +63 -0
  360. package/src/replication/ErrorRateLimiter.ts +50 -0
  361. package/src/replication/PgRelation.ts +42 -0
  362. package/src/replication/WalConnection.ts +227 -0
  363. package/src/replication/WalStream.ts +624 -0
  364. package/src/replication/WalStreamManager.ts +213 -0
  365. package/src/replication/WalStreamRunner.ts +180 -0
  366. package/src/replication/replication-index.ts +7 -0
  367. package/src/replication/util.ts +76 -0
  368. package/src/routes/auth.ts +215 -0
  369. package/src/routes/endpoints/admin.ts +237 -0
  370. package/src/routes/endpoints/checkpointing.ts +41 -0
  371. package/src/routes/endpoints/dev.ts +199 -0
  372. package/src/routes/endpoints/route-endpoints-index.ts +6 -0
  373. package/src/routes/endpoints/socket-route.ts +135 -0
  374. package/src/routes/endpoints/sync-rules.ts +227 -0
  375. package/src/routes/endpoints/sync-stream.ts +101 -0
  376. package/src/routes/hooks.ts +46 -0
  377. package/src/routes/route-register.ts +104 -0
  378. package/src/routes/router-socket.ts +13 -0
  379. package/src/routes/router.ts +46 -0
  380. package/src/routes/routes-index.ts +6 -0
  381. package/src/runner/teardown.ts +108 -0
  382. package/src/storage/BucketStorage.ts +396 -0
  383. package/src/storage/ChecksumCache.ts +294 -0
  384. package/src/storage/MongoBucketStorage.ts +519 -0
  385. package/src/storage/SourceTable.ts +60 -0
  386. package/src/storage/mongo/MongoBucketBatch.ts +752 -0
  387. package/src/storage/mongo/MongoIdSequence.ts +24 -0
  388. package/src/storage/mongo/MongoPersistedSyncRules.ts +16 -0
  389. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +47 -0
  390. package/src/storage/mongo/MongoSyncBucketStorage.ts +533 -0
  391. package/src/storage/mongo/MongoSyncRulesLock.ts +81 -0
  392. package/src/storage/mongo/OperationBatch.ts +115 -0
  393. package/src/storage/mongo/PersistedBatch.ts +268 -0
  394. package/src/storage/mongo/db.ts +73 -0
  395. package/src/storage/mongo/models.ts +162 -0
  396. package/src/storage/mongo/util.ts +88 -0
  397. package/src/storage/storage-index.ts +15 -0
  398. package/src/sync/BroadcastIterable.ts +161 -0
  399. package/src/sync/LastValueSink.ts +100 -0
  400. package/src/sync/merge.ts +200 -0
  401. package/src/sync/safeRace.ts +99 -0
  402. package/src/sync/sync-index.ts +6 -0
  403. package/src/sync/sync.ts +319 -0
  404. package/src/sync/util.ts +98 -0
  405. package/src/system/CorePowerSyncSystem.ts +64 -0
  406. package/src/system/system-index.ts +1 -0
  407. package/src/util/Mutex.ts +159 -0
  408. package/src/util/PgManager.ts +64 -0
  409. package/src/util/alerting.ts +9 -0
  410. package/src/util/config/collectors/config-collector.ts +143 -0
  411. package/src/util/config/collectors/impl/base64-config-collector.ts +18 -0
  412. package/src/util/config/collectors/impl/fallback-config-collector.ts +22 -0
  413. package/src/util/config/collectors/impl/filesystem-config-collector.ts +43 -0
  414. package/src/util/config/compound-config-collector.ts +176 -0
  415. package/src/util/config/sync-rules/impl/base64-sync-rules-collector.ts +21 -0
  416. package/src/util/config/sync-rules/impl/filesystem-sync-rules-collector.ts +26 -0
  417. package/src/util/config/sync-rules/impl/inline-sync-rules-collector.ts +21 -0
  418. package/src/util/config/sync-rules/sync-collector.ts +8 -0
  419. package/src/util/config/types.ts +66 -0
  420. package/src/util/config.ts +39 -0
  421. package/src/util/env.ts +30 -0
  422. package/src/util/memory-tracking.ts +67 -0
  423. package/src/util/migration_lib.ts +79 -0
  424. package/src/util/pgwire_utils.ts +139 -0
  425. package/src/util/populate_test_data.ts +78 -0
  426. package/src/util/protocol-types.ts +228 -0
  427. package/src/util/secs.ts +54 -0
  428. package/src/util/util-index.ts +25 -0
  429. package/src/util/utils.ts +122 -0
  430. package/test/src/__snapshots__/pg_test.test.ts.snap +256 -0
  431. package/test/src/__snapshots__/sync.test.ts.snap +247 -0
  432. package/test/src/auth.test.ts +342 -0
  433. package/test/src/broadcast_iterable.test.ts +156 -0
  434. package/test/src/checksum_cache.test.ts +436 -0
  435. package/test/src/data_storage.test.ts +1176 -0
  436. package/test/src/env.ts +8 -0
  437. package/test/src/large_batch.test.ts +194 -0
  438. package/test/src/merge_iterable.test.ts +355 -0
  439. package/test/src/pg_test.test.ts +450 -0
  440. package/test/src/schema_changes.test.ts +545 -0
  441. package/test/src/setup.ts +7 -0
  442. package/test/src/slow_tests.test.ts +257 -0
  443. package/test/src/sql_functions.test.ts +254 -0
  444. package/test/src/sql_operators.test.ts +132 -0
  445. package/test/src/sync.test.ts +293 -0
  446. package/test/src/sync_rules.test.ts +1053 -0
  447. package/test/src/util.ts +76 -0
  448. package/test/src/validation.test.ts +63 -0
  449. package/test/src/wal_stream.test.ts +319 -0
  450. package/test/src/wal_stream_utils.ts +147 -0
  451. package/test/tsconfig.json +20 -0
  452. package/tsconfig.json +31 -0
  453. package/tsconfig.tsbuildinfo +1 -0
  454. package/vitest.config.ts +9 -0
@@ -0,0 +1,202 @@
1
+ import * as t from 'ts-codec';
2
+ import { errors, router, schema } from '@powersync/lib-services-framework';
3
+ import { SqlSyncRules, SyncRulesErrors } from '@powersync/service-sync-rules';
4
+ import * as replication from '../../replication/replication-index.js';
5
+ import { authApi } from '../auth.js';
6
+ import { routeDefinition } from '../router.js';
7
+ const DeploySyncRulesRequest = t.object({
8
+ content: t.string
9
+ });
10
+ export const yamlPlugin = async (fastify) => {
11
+ fastify.addContentTypeParser('application/yaml', async (request, payload, _d) => {
12
+ const data = [];
13
+ for await (const chunk of payload) {
14
+ data.push(chunk);
15
+ }
16
+ request.params = { content: Buffer.concat(data).toString('utf8') };
17
+ });
18
+ };
19
+ /**
20
+ * Declares the plugin should be available on the same scope
21
+ * without requiring the `fastify-plugin` package as a dependency.
22
+ * https://fastify.dev/docs/latest/Reference/Plugins/#handle-the-scope
23
+ */
24
+ //@ts-expect-error
25
+ yamlPlugin[Symbol.for('skip-override')] = true;
26
+ export const deploySyncRules = routeDefinition({
27
+ path: '/api/sync-rules/v1/deploy',
28
+ method: router.HTTPMethod.POST,
29
+ authorize: authApi,
30
+ parse: true,
31
+ plugins: [yamlPlugin],
32
+ validator: schema.createTsCodecValidator(DeploySyncRulesRequest, { allowAdditional: true }),
33
+ handler: async (payload) => {
34
+ if (payload.context.system.config.sync_rules.present) {
35
+ // If sync rules are configured via the config, disable deploy via the API.
36
+ throw new errors.JourneyError({
37
+ status: 422,
38
+ code: 'API_DISABLED',
39
+ description: 'Sync rules API disabled',
40
+ details: 'Use the management API to deploy sync rules'
41
+ });
42
+ }
43
+ const content = payload.params.content;
44
+ try {
45
+ SqlSyncRules.fromYaml(payload.params.content);
46
+ }
47
+ catch (e) {
48
+ throw new errors.JourneyError({
49
+ status: 422,
50
+ code: 'INVALID_SYNC_RULES',
51
+ description: 'Sync rules parsing failed',
52
+ details: e.message
53
+ });
54
+ }
55
+ const sync_rules = await payload.context.system.storage.updateSyncRules({
56
+ content: content
57
+ });
58
+ return {
59
+ slot_name: sync_rules.slot_name
60
+ };
61
+ }
62
+ });
63
+ const ValidateSyncRulesRequest = t.object({
64
+ content: t.string
65
+ });
66
+ export const validateSyncRules = routeDefinition({
67
+ path: '/api/sync-rules/v1/validate',
68
+ method: router.HTTPMethod.POST,
69
+ authorize: authApi,
70
+ parse: true,
71
+ plugins: [yamlPlugin],
72
+ validator: schema.createTsCodecValidator(ValidateSyncRulesRequest, { allowAdditional: true }),
73
+ handler: async (payload) => {
74
+ const content = payload.params.content;
75
+ const info = await debugSyncRules(payload.context.system.requirePgPool(), content);
76
+ return replyPrettyJson(info);
77
+ }
78
+ });
79
+ export const currentSyncRules = routeDefinition({
80
+ path: '/api/sync-rules/v1/current',
81
+ method: router.HTTPMethod.GET,
82
+ authorize: authApi,
83
+ handler: async (payload) => {
84
+ const storage = payload.context.system.storage;
85
+ const sync_rules = await storage.getActiveSyncRulesContent();
86
+ if (!sync_rules) {
87
+ throw new errors.JourneyError({
88
+ status: 422,
89
+ code: 'NO_SYNC_RULES',
90
+ description: 'No active sync rules'
91
+ });
92
+ }
93
+ const info = await debugSyncRules(payload.context.system.requirePgPool(), sync_rules.sync_rules_content);
94
+ const next = await storage.getNextSyncRulesContent();
95
+ const next_info = next
96
+ ? await debugSyncRules(payload.context.system.requirePgPool(), next.sync_rules_content)
97
+ : null;
98
+ const response = {
99
+ current: {
100
+ slot_name: sync_rules.slot_name,
101
+ content: sync_rules.sync_rules_content,
102
+ ...info
103
+ },
104
+ next: next == null
105
+ ? null
106
+ : {
107
+ slot_name: next.slot_name,
108
+ content: next.sync_rules_content,
109
+ ...next_info
110
+ }
111
+ };
112
+ return replyPrettyJson({ data: response });
113
+ }
114
+ });
115
+ const ReprocessSyncRulesRequest = t.object({});
116
+ export const reprocessSyncRules = routeDefinition({
117
+ path: '/api/sync-rules/v1/reprocess',
118
+ method: router.HTTPMethod.POST,
119
+ authorize: authApi,
120
+ validator: schema.createTsCodecValidator(ReprocessSyncRulesRequest),
121
+ handler: async (payload) => {
122
+ const storage = payload.context.system.storage;
123
+ const sync_rules = await storage.getActiveSyncRules();
124
+ if (sync_rules == null) {
125
+ throw new errors.JourneyError({
126
+ status: 422,
127
+ code: 'NO_SYNC_RULES',
128
+ description: 'No active sync rules'
129
+ });
130
+ }
131
+ const new_rules = await storage.updateSyncRules({
132
+ content: sync_rules.sync_rules.content
133
+ });
134
+ return {
135
+ slot_name: new_rules.slot_name
136
+ };
137
+ }
138
+ });
139
+ export const SYNC_RULES_ROUTES = [validateSyncRules, deploySyncRules, reprocessSyncRules, currentSyncRules];
140
+ function replyPrettyJson(payload) {
141
+ return new router.RouterResponse({
142
+ status: 200,
143
+ data: JSON.stringify(payload, null, 2) + '\n',
144
+ headers: { 'Content-Type': 'application/json' }
145
+ });
146
+ }
147
+ async function debugSyncRules(db, sync_rules) {
148
+ try {
149
+ const rules = SqlSyncRules.fromYaml(sync_rules);
150
+ const source_table_patterns = rules.getSourceTables();
151
+ const wc = new replication.WalConnection({
152
+ db: db,
153
+ sync_rules: rules
154
+ });
155
+ const resolved_tables = await wc.getDebugTablesInfo(source_table_patterns);
156
+ return {
157
+ valid: true,
158
+ bucket_definitions: rules.bucket_descriptors.map((d) => {
159
+ let all_parameter_queries = [...d.parameter_queries.values()].flat();
160
+ let all_data_queries = [...d.data_queries.values()].flat();
161
+ return {
162
+ name: d.name,
163
+ bucket_parameters: d.bucket_parameters,
164
+ global_parameter_queries: d.global_parameter_queries.map((q) => {
165
+ return {
166
+ sql: q.sql
167
+ };
168
+ }),
169
+ parameter_queries: all_parameter_queries.map((q) => {
170
+ return {
171
+ sql: q.sql,
172
+ table: q.sourceTable,
173
+ input_parameters: q.input_parameters
174
+ };
175
+ }),
176
+ data_queries: all_data_queries.map((q) => {
177
+ return {
178
+ sql: q.sql,
179
+ table: q.sourceTable,
180
+ columns: q.columnOutputNames()
181
+ };
182
+ })
183
+ };
184
+ }),
185
+ source_tables: resolved_tables,
186
+ data_tables: rules.debugGetOutputTables()
187
+ };
188
+ }
189
+ catch (e) {
190
+ if (e instanceof SyncRulesErrors) {
191
+ return {
192
+ valid: false,
193
+ errors: e.errors.map((e) => e.message)
194
+ };
195
+ }
196
+ return {
197
+ valid: false,
198
+ errors: [e.message]
199
+ };
200
+ }
201
+ }
202
+ //# sourceMappingURL=sync-rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-rules.js","sourceRoot":"","sources":["../../../src/routes/endpoints/sync-rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAG9B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAE9E,OAAO,KAAK,WAAW,MAAM,wCAAwC,CAAC;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,sBAAsB,GAAG,CAAC,CAAC,MAAM,CAAC;IACtC,OAAO,EAAE,CAAC,CAAC,MAAM;CAClB,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,UAAU,GAAuB,KAAK,EAAE,OAAO,EAAE,EAAE;IAC9D,OAAO,CAAC,oBAAoB,CAAC,kBAAkB,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE;QAC9E,MAAM,IAAI,GAAU,EAAE,CAAC;QACvB,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,OAAO,EAAE;YACjC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SAClB;QAED,OAAO,CAAC,MAAM,GAAG,EAAE,OAAO,EAAE,MAAM,CAA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W,EAAE,KAAK,CAAC,oBAAoB,EAAE;SAC1C,CAAC;KACH;IAAC,OAAO,CAAC,EAAE;QACV,IAAI,CAAC,YAAY,eAAe,EAAE;YAChC,OAAO;gBACL,KAAK,EAAE,KAAK;gBACZ,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;aACvC,CAAC;SACH;QACD,OAAO;YACL,KAAK,EAAE,KAAK;YACZ,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;SACpB,CAAC;KACH;AACH,CAAC"}
@@ -0,0 +1,132 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { Readable } from 'stream';
3
+ import { router, schema } from '@powersync/lib-services-framework';
4
+ export declare enum SyncRoutes {
5
+ STREAM = "/sync/stream"
6
+ }
7
+ export declare const syncStreamed: router.Endpoint<{
8
+ parameters?: Record<string, any> | undefined;
9
+ buckets?: {
10
+ name: string;
11
+ after: string;
12
+ }[] | undefined;
13
+ only?: string[] | undefined;
14
+ include_checksum?: boolean | undefined;
15
+ raw_data?: boolean | undefined;
16
+ binary_data?: boolean | undefined;
17
+ }, router.RouterResponse<Readable> | undefined, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
18
+ parameters?: Record<string, any> | undefined;
19
+ buckets?: {
20
+ name: string;
21
+ after: string;
22
+ }[] | undefined;
23
+ only?: string[] | undefined;
24
+ include_checksum?: boolean | undefined;
25
+ raw_data?: boolean | undefined;
26
+ binary_data?: boolean | undefined;
27
+ }, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
28
+ parameters?: Record<string, any> | undefined;
29
+ buckets?: {
30
+ name: string;
31
+ after: string;
32
+ }[] | undefined;
33
+ only?: string[] | undefined;
34
+ include_checksum?: boolean | undefined;
35
+ raw_data?: boolean | undefined;
36
+ binary_data?: boolean | undefined;
37
+ }, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.RouterResponse<Readable> | undefined>> & {
38
+ path: SyncRoutes;
39
+ method: router.HTTPMethod.POST;
40
+ authorize: (payload: import("../router.js").RequestEndpointHandlerPayload) => Promise<{
41
+ authorized: boolean;
42
+ errors: any[];
43
+ } | {
44
+ authorized: boolean;
45
+ errors?: undefined;
46
+ }>;
47
+ validator: schema.MicroValidator<{
48
+ parameters?: Record<string, any> | undefined;
49
+ buckets?: {
50
+ name: string;
51
+ after: string;
52
+ }[] | undefined;
53
+ only?: string[] | undefined;
54
+ include_checksum?: boolean | undefined;
55
+ raw_data?: boolean | undefined;
56
+ binary_data?: boolean | undefined;
57
+ }, string[]>;
58
+ handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
59
+ parameters?: Record<string, any> | undefined;
60
+ buckets?: {
61
+ name: string;
62
+ after: string;
63
+ }[] | undefined;
64
+ only?: string[] | undefined;
65
+ include_checksum?: boolean | undefined;
66
+ raw_data?: boolean | undefined;
67
+ binary_data?: boolean | undefined;
68
+ }, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<router.RouterResponse<Readable> | undefined>;
69
+ };
70
+ export declare const SYNC_STREAM_ROUTES: (router.Endpoint<{
71
+ parameters?: Record<string, any> | undefined;
72
+ buckets?: {
73
+ name: string;
74
+ after: string;
75
+ }[] | undefined;
76
+ only?: string[] | undefined;
77
+ include_checksum?: boolean | undefined;
78
+ raw_data?: boolean | undefined;
79
+ binary_data?: boolean | undefined;
80
+ }, router.RouterResponse<Readable> | undefined, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
81
+ parameters?: Record<string, any> | undefined;
82
+ buckets?: {
83
+ name: string;
84
+ after: string;
85
+ }[] | undefined;
86
+ only?: string[] | undefined;
87
+ include_checksum?: boolean | undefined;
88
+ raw_data?: boolean | undefined;
89
+ binary_data?: boolean | undefined;
90
+ }, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
91
+ parameters?: Record<string, any> | undefined;
92
+ buckets?: {
93
+ name: string;
94
+ after: string;
95
+ }[] | undefined;
96
+ only?: string[] | undefined;
97
+ include_checksum?: boolean | undefined;
98
+ raw_data?: boolean | undefined;
99
+ binary_data?: boolean | undefined;
100
+ }, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.RouterResponse<Readable> | undefined>> & {
101
+ path: SyncRoutes;
102
+ method: router.HTTPMethod.POST;
103
+ authorize: (payload: import("../router.js").RequestEndpointHandlerPayload) => Promise<{
104
+ authorized: boolean;
105
+ errors: any[];
106
+ } | {
107
+ authorized: boolean;
108
+ errors?: undefined;
109
+ }>;
110
+ validator: schema.MicroValidator<{
111
+ parameters?: Record<string, any> | undefined;
112
+ buckets?: {
113
+ name: string;
114
+ after: string;
115
+ }[] | undefined;
116
+ only?: string[] | undefined;
117
+ include_checksum?: boolean | undefined;
118
+ raw_data?: boolean | undefined;
119
+ binary_data?: boolean | undefined;
120
+ }, string[]>;
121
+ handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
122
+ parameters?: Record<string, any> | undefined;
123
+ buckets?: {
124
+ name: string;
125
+ after: string;
126
+ }[] | undefined;
127
+ only?: string[] | undefined;
128
+ include_checksum?: boolean | undefined;
129
+ raw_data?: boolean | undefined;
130
+ binary_data?: boolean | undefined;
131
+ }, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<router.RouterResponse<Readable> | undefined>;
132
+ })[];
@@ -0,0 +1,83 @@
1
+ import { Readable } from 'stream';
2
+ import { normalizeTokenParameters } from '@powersync/service-sync-rules';
3
+ import { errors, logger, router, schema } from '@powersync/lib-services-framework';
4
+ import * as sync from '../../sync/sync-index.js';
5
+ import * as util from '../../util/util-index.js';
6
+ import { authUser } from '../auth.js';
7
+ import { routeDefinition } from '../router.js';
8
+ import { Metrics } from '../../metrics/Metrics.js';
9
+ export var SyncRoutes;
10
+ (function (SyncRoutes) {
11
+ SyncRoutes["STREAM"] = "/sync/stream";
12
+ })(SyncRoutes || (SyncRoutes = {}));
13
+ export const syncStreamed = routeDefinition({
14
+ path: SyncRoutes.STREAM,
15
+ method: router.HTTPMethod.POST,
16
+ authorize: authUser,
17
+ validator: schema.createTsCodecValidator(util.StreamingSyncRequest, { allowAdditional: true }),
18
+ handler: async (payload) => {
19
+ const system = payload.context.system;
20
+ if (system.closed) {
21
+ throw new errors.JourneyError({
22
+ status: 503,
23
+ code: 'SERVICE_UNAVAILABLE',
24
+ description: 'Service temporarily unavailable'
25
+ });
26
+ }
27
+ const params = payload.params;
28
+ const syncParams = normalizeTokenParameters(payload.context.token_payload.parameters ?? {}, payload.params.parameters ?? {});
29
+ const storage = system.storage;
30
+ // Sanity check before we start the stream
31
+ const cp = await storage.getActiveCheckpoint();
32
+ if (!cp.hasSyncRules()) {
33
+ throw new errors.JourneyError({
34
+ status: 500,
35
+ code: 'NO_SYNC_RULES',
36
+ description: 'No sync rules available'
37
+ });
38
+ }
39
+ const controller = new AbortController();
40
+ try {
41
+ Metrics.getInstance().concurrent_connections.add(1);
42
+ const stream = Readable.from(sync.transformToBytesTracked(sync.ndjson(sync.streamResponse({
43
+ storage,
44
+ params,
45
+ syncParams,
46
+ token: payload.context.token_payload,
47
+ signal: controller.signal
48
+ }))), { objectMode: false, highWaterMark: 16 * 1024 });
49
+ const deregister = system.addStopHandler(() => {
50
+ // This error is not currently propagated to the client
51
+ controller.abort();
52
+ stream.destroy(new Error('Shutting down system'));
53
+ });
54
+ stream.on('close', () => {
55
+ deregister();
56
+ });
57
+ stream.on('error', (error) => {
58
+ controller.abort();
59
+ // Note: This appears as a 200 response in the logs.
60
+ if (error.message != 'Shutting down system') {
61
+ logger.error('Streaming sync request failed', error);
62
+ }
63
+ });
64
+ return new router.RouterResponse({
65
+ status: 200,
66
+ headers: {
67
+ 'Content-Type': 'application/x-ndjson'
68
+ },
69
+ data: stream,
70
+ afterSend: async () => {
71
+ controller.abort();
72
+ Metrics.getInstance().concurrent_connections.add(-1);
73
+ }
74
+ });
75
+ }
76
+ catch (ex) {
77
+ controller.abort();
78
+ Metrics.getInstance().concurrent_connections.add(-1);
79
+ }
80
+ }
81
+ });
82
+ export const SYNC_STREAM_ROUTES = [syncStreamed];
83
+ //# sourceMappingURL=sync-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-stream.js","sourceRoot":"","sources":["../../../src/routes/endpoints/sync-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAkB,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzF,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAEnF,OAAO,KAAK,IAAI,MAAM,0BAA0B,CAAC;AACjD,OAAO,KAAK,IAAI,MAAM,0BAA0B,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AAEnD,MAAM,CAAN,IAAY,UAEX;AAFD,WAAY,UAAU;IACpB,qCAAuB,CAAA;AACzB,CAAC,EAFW,UAAU,KAAV,UAAU,QAErB;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,eAAe,CAAC;IAC1C,IAAI,EAAE,UAAU,CAAC,MAAM;IACvB,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI;IAC9B,SAAS,EAAE,QAAQ;IACnB,SAAS,EAAE,MAAM,CAAC,sBAAsB,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC;IAC9F,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;QACzB,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;QAEtC,IAAI,MAAM,CAAC,MAAM,EAAE;YACjB,MAAM,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BACvD,UAAU,CAAC,KAAK,EAAE,CAAC;gBACnB,MAAM,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC;YACpD,CAAC,CAAC,CAAC;YACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;gBACtB,UAAU,EAAE,CAAC;YACf,CAAC,CAAC,CAAC;YAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;gBAC3B,UAAU,CAAC,KAAK,EAAE,CAAC;gBACnB,oDAAoD;gBACpD,IAAI,KAAK,CAAC,OAAO,IAAI,sBAAsB,EAAE;oBAC3C,MAAM,CAAC,KAAK,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAC;iBACtD;YACH,CAAC,CAAC,CAAC;YAEH,OAAO,IAAI,MAAM,CAAC,cAAc,CAAC;gBAC/B,MAAM,EAAE,GAAG;gBACX,OAAO,EAAE;oBACP,cAAc,EAAE,sBAAsB;iBACvC;gBACD,IAAI,EAAE,MAAM;gBACZ,SAAS,EAAE,KAAK,IAAI,EAAE;oBACpB,UAAU,CAAC,KAAK,EAAE,CAAC;oBACnB,OAAO,CAAC,WAAW,EAAE,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;gBACvD,CAAC;aACF,CAAC,CAAC;SACJ;QAAC,OAAO,EAAE,EAAE;YACX,UAAU,CAAC,KAAK,EAAE,CAAC;YACnB,OAAO,CAAC,WAAW,EAAE,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;SACtD;IACH,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,YAAY,CAAC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import type fastify from 'fastify';
2
+ export type CreateRequestQueueParams = {
3
+ max_queue_depth: number;
4
+ concurrency: number;
5
+ };
6
+ /**
7
+ * Creates a request queue which limits the amount of concurrent connections which
8
+ * are active at any time.
9
+ */
10
+ export declare const createRequestQueueHook: (params: CreateRequestQueueParams) => fastify.onRequestHookHandler;
@@ -0,0 +1,32 @@
1
+ import a from 'async';
2
+ import { logger } from '@powersync/lib-services-framework';
3
+ /**
4
+ * Creates a request queue which limits the amount of concurrent connections which
5
+ * are active at any time.
6
+ */
7
+ export const createRequestQueueHook = (params) => {
8
+ const request_queue = a.queue((event, done) => {
9
+ event().finally(done);
10
+ }, params.concurrency);
11
+ return (request, reply, next) => {
12
+ if ((params.max_queue_depth == 0 && request_queue.running() == params.concurrency) ||
13
+ (params.max_queue_depth > 0 && request_queue.length() >= params.max_queue_depth)) {
14
+ logger.warn(`${request.method} ${request.url}`, {
15
+ status: 429,
16
+ method: request.method,
17
+ path: request.url,
18
+ route: request.routerPath,
19
+ queue_overflow: true
20
+ });
21
+ return reply.status(429).send();
22
+ }
23
+ const finished = new Promise((resolve) => {
24
+ reply.then(() => resolve(), () => resolve());
25
+ });
26
+ request_queue.push(() => {
27
+ next();
28
+ return finished;
29
+ });
30
+ };
31
+ };
32
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/routes/hooks.ts"],"names":[],"mappings":"AACA,OAAO,CAAC,MAAM,OAAO,CAAC;AACtB,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAO3D;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,MAAgC,EAAgC,EAAE;IACvG,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAsB,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACjE,KAAK,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAEvB,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QAC9B,IACE,CAAC,MAAM,CAAC,eAAe,IAAI,CAAC,IAAI,aAAa,CAAC,OAAO,EAAE,IAAI,MAAM,CAAC,WAAW,CAAC;YAC9E,CAAC,MAAM,CAAC,eAAe,GAAG,CAAC,IAAI,aAAa,CAAC,MAAM,EAAE,IAAI,MAAM,CAAC,eAAe,CAAC,EAChF;YACA,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE;gBAC9C,MAAM,EAAE,GAAG;gBACX,MAAM,EAAE,OAAO,CAAC,MAAM;gBACtB,IAAI,EAAE,OAAO,CAAC,GAAG;gBACjB,KAAK,EAAE,OAAO,CAAC,UAAU;gBACzB,cAAc,EAAE,IAAI;aACrB,CAAC,CAAC;YACH,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;SACjC;QAED,MAAM,QAAQ,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YAC7C,KAAK,CAAC,IAAI,CACR,GAAG,EAAE,CAAC,OAAO,EAAE,EACf,GAAG,EAAE,CAAC,OAAO,EAAE,CAChB,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,aAAa,CAAC,IAAI,CAAC,GAAG,EAAE;YACtB,IAAI,EAAE,CAAC;YACP,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import fastify from 'fastify';
2
+ import { Context, ContextProvider, RequestEndpoint } from './router.js';
3
+ export type FastifyEndpoint<I, O, C> = RequestEndpoint<I, O, C> & {
4
+ parse?: boolean;
5
+ plugins?: fastify.FastifyPluginAsync[];
6
+ };
7
+ /**
8
+ * Registers endpoint definitions as routes on a Fastify app instance.
9
+ */
10
+ export declare function registerFastifyRoutes(app: fastify.FastifyInstance, contextProvider: ContextProvider, endpoints: FastifyEndpoint<any, any, Context>[]): void;
@@ -0,0 +1,87 @@
1
+ import { errors, router, logger } from '@powersync/lib-services-framework';
2
+ /**
3
+ * Registers endpoint definitions as routes on a Fastify app instance.
4
+ */
5
+ export function registerFastifyRoutes(app, contextProvider, endpoints) {
6
+ for (const e of endpoints) {
7
+ // Create a new context for each route
8
+ app.register(async function (fastify) {
9
+ fastify.route({
10
+ url: e.path,
11
+ method: e.method,
12
+ handler: async (request, reply) => {
13
+ const startTime = new Date();
14
+ let response;
15
+ try {
16
+ const context = await contextProvider(request);
17
+ let combined = {
18
+ ...request.params,
19
+ ...request.query
20
+ };
21
+ if (typeof request.body === 'object' && !Buffer.isBuffer(request.body) && !Array.isArray(request.body)) {
22
+ combined = {
23
+ ...combined,
24
+ ...request.body
25
+ };
26
+ }
27
+ const payload = {
28
+ context: context,
29
+ params: combined,
30
+ request
31
+ };
32
+ const endpointResponse = await router.executeEndpoint(e, payload);
33
+ if (router.RouterResponse.isRouterResponse(endpointResponse)) {
34
+ response = endpointResponse;
35
+ }
36
+ else if (router.isAsyncIterable(endpointResponse) || Buffer.isBuffer(endpointResponse)) {
37
+ response = new router.RouterResponse({
38
+ status: 200,
39
+ data: endpointResponse
40
+ });
41
+ }
42
+ else {
43
+ response = new router.RouterResponse({
44
+ status: 200,
45
+ data: { data: endpointResponse }
46
+ });
47
+ }
48
+ }
49
+ catch (ex) {
50
+ const journeyError = errors.JourneyError.isJourneyError(ex) ? ex : new errors.InternalServerError(ex);
51
+ response = new router.RouterResponse({
52
+ status: journeyError.errorData.status || 500,
53
+ headers: {
54
+ 'Content-Type': 'application/json'
55
+ },
56
+ data: {
57
+ error: journeyError.errorData
58
+ }
59
+ });
60
+ }
61
+ Object.keys(response.headers).forEach((key) => {
62
+ reply.header(key, response.headers[key]);
63
+ });
64
+ reply.status(response.status);
65
+ try {
66
+ await reply.send(response.data);
67
+ }
68
+ finally {
69
+ await response.afterSend?.();
70
+ logger.info(`${e.method} ${request.url}`, {
71
+ duration_ms: Math.round(new Date().valueOf() - startTime.valueOf() + Number.EPSILON),
72
+ status: response.status,
73
+ method: e.method,
74
+ path: request.url,
75
+ route: e.path
76
+ });
77
+ }
78
+ }
79
+ });
80
+ if (!(e.parse ?? true)) {
81
+ fastify.removeAllContentTypeParsers();
82
+ }
83
+ e.plugins?.forEach((plugin) => fastify.register(plugin));
84
+ });
85
+ }
86
+ }
87
+ //# sourceMappingURL=route-register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route-register.js","sourceRoot":"","sources":["../../src/routes/route-register.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAc,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAQvF;;GAEG;AACH,MAAM,UAAU,qBAAqB,CACnC,GAA4B,EAC5B,eAAgC,EAChC,SAA+C;IAE/C,KAAK,MAAM,CAAC,IAAI,SAAS,EAAE;QACzB,sCAAsC;QACtC,GAAG,CAAC,QAAQ,CAAC,KAAK,WAAW,OAAO;YAClC,OAAO,CAAC,KAAK,CAAC;gBACZ,GAAG,EAAE,CAAC,CAAC,IAAI;gBACX,MAAM,EAAE,CAAC,CAAC,MAAoB;gBAC9B,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;oBAChC,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;oBAC7B,IAAI,QAA+B,CAAC;oBACpC,IAAI;wBACF,MAAM,OAAO,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC,CAAC;wBAE/C,IAAI,QAAQ,GAAG;4BACb,GAAI,OAAO,CAAC,MAAc;4BAC1B,GAAI,OAAO,CAAC,KAAa;yBAC1B,CAAC;wBAEF,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;4BACtG,QAAQ,GAAG;gCACT,GAAG,QAAQ;gCACX,GAAG,OAAO,CAAC,IAAI;6BACh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,CAAC,CAAC,GAAG,EAAE,EAAE;wBAC5C,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;oBAC3C,CAAC,CAAC,CAAC;oBACH,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;oBAC9B,IAAI;wBACF,MAAM,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;qBACjC;4BAAS;wBACR,MAAM,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC;wBAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,EAAE,EAAE;4BACxC,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,SAAS,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC;4BACpF,MAAM,EAAE,QAAQ,CAAC,MAAM;4BACvB,MAAM,EAAE,CAAC,CAAC,MAAM;4BAChB,IAAI,EAAE,OAAO,CAAC,GAAG;4BACjB,KAAK,EAAE,CAAC,CAAC,IAAI;yBACd,CAAC,CAAC;qBACJ;gBACH,CAAC;aACF,CAAC,CAAC;YAEH,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,EAAE;gBACtB,OAAO,CAAC,2BAA2B,EAAE,CAAC;aACvC;YAED,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;KACJ;AACH,CAAC"}
@@ -0,0 +1,10 @@
1
+ import * as t from 'ts-codec';
2
+ import { ReactiveSocketRouter, IReactiveStream } from '@powersync/service-rsocket-router';
3
+ import { Context } from './router.js';
4
+ export declare const RSocketContextMeta: t.ObjectCodec<{
5
+ token: t.IdentityCodec<t.CodecType.String>;
6
+ }>;
7
+ /**
8
+ * Creates a socket route handler given a router instance
9
+ */
10
+ export type SocketRouteGenerator = (router: ReactiveSocketRouter<Context>) => IReactiveStream;
@@ -0,0 +1,5 @@
1
+ import * as t from 'ts-codec';
2
+ export const RSocketContextMeta = t.object({
3
+ token: t.string
4
+ });
5
+ //# sourceMappingURL=router-socket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router-socket.js","sourceRoot":"","sources":["../../src/routes/router-socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAK9B,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IACzC,KAAK,EAAE,CAAC,CAAC,MAAM;CAChB,CAAC,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { router } from '@powersync/lib-services-framework';
2
+ import * as auth from '../auth/auth-index.js';
3
+ import { CorePowerSyncSystem } from '../system/CorePowerSyncSystem.js';
4
+ /**
5
+ * Common context for routes
6
+ */
7
+ export type Context = {
8
+ user_id?: string;
9
+ system: CorePowerSyncSystem;
10
+ token_payload?: auth.JwtPayload;
11
+ token_errors?: string[];
12
+ };
13
+ export type BasicRouterRequest = {
14
+ headers: Record<string, string | string[] | undefined>;
15
+ protocol: string;
16
+ hostname: string;
17
+ };
18
+ export type ContextProvider = (request: BasicRouterRequest) => Promise<Context>;
19
+ export type RequestEndpoint<I, O, C = Context, Payload = RequestEndpointHandlerPayload<I, C, BasicRouterRequest>> = router.Endpoint<I, O, C, Payload> & {};
20
+ export type RequestEndpointHandlerPayload<I = any, C = Context, Request = BasicRouterRequest> = router.EndpointHandlerPayload<I, C> & {
21
+ request: Request;
22
+ };
23
+ /**
24
+ * Helper function for making generics work well when defining routes
25
+ */
26
+ export declare function routeDefinition<I, O, C = Context, Extension = {}>(params: RequestEndpoint<I, O, C> & Extension): RequestEndpoint<I, O, C> & Extension;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Helper function for making generics work well when defining routes
3
+ */
4
+ export function routeDefinition(params) {
5
+ return params;
6
+ }
7
+ //# sourceMappingURL=router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/routes/router.ts"],"names":[],"mappings":"AAsCA;;GAEG;AACH,MAAM,UAAU,eAAe,CAC7B,MAA4C;IAE5C,OAAO,MAAM,CAAC;AAChB,CAAC"}