@powersync/service-core 0.0.2

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 (412) hide show
  1. package/.probes/.gitkeep +0 -0
  2. package/CHANGELOG.md +13 -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 +24 -0
  72. package/dist/index.js +26 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/metrics/metrics.d.ts +16 -0
  75. package/dist/metrics/metrics.js +139 -0
  76. package/dist/metrics/metrics.js.map +1 -0
  77. package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
  78. package/dist/migrations/db/migrations/1684951997326-init.js +31 -0
  79. package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
  80. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
  81. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
  82. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
  83. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
  84. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
  85. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
  86. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
  87. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +27 -0
  88. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
  89. package/dist/migrations/db/store.d.ts +3 -0
  90. package/dist/migrations/db/store.js +10 -0
  91. package/dist/migrations/db/store.js.map +1 -0
  92. package/dist/migrations/migrations.d.ts +10 -0
  93. package/dist/migrations/migrations.js +94 -0
  94. package/dist/migrations/migrations.js.map +1 -0
  95. package/dist/replication/ErrorRateLimiter.d.ts +17 -0
  96. package/dist/replication/ErrorRateLimiter.js +42 -0
  97. package/dist/replication/ErrorRateLimiter.js.map +1 -0
  98. package/dist/replication/PgRelation.d.ts +16 -0
  99. package/dist/replication/PgRelation.js +26 -0
  100. package/dist/replication/PgRelation.js.map +1 -0
  101. package/dist/replication/WalConnection.d.ts +34 -0
  102. package/dist/replication/WalConnection.js +190 -0
  103. package/dist/replication/WalConnection.js.map +1 -0
  104. package/dist/replication/WalStream.d.ts +58 -0
  105. package/dist/replication/WalStream.js +517 -0
  106. package/dist/replication/WalStream.js.map +1 -0
  107. package/dist/replication/WalStreamManager.d.ts +30 -0
  108. package/dist/replication/WalStreamManager.js +199 -0
  109. package/dist/replication/WalStreamManager.js.map +1 -0
  110. package/dist/replication/WalStreamRunner.d.ts +38 -0
  111. package/dist/replication/WalStreamRunner.js +155 -0
  112. package/dist/replication/WalStreamRunner.js.map +1 -0
  113. package/dist/replication/replication-index.d.ts +7 -0
  114. package/dist/replication/replication-index.js +8 -0
  115. package/dist/replication/replication-index.js.map +1 -0
  116. package/dist/replication/util.d.ts +9 -0
  117. package/dist/replication/util.js +62 -0
  118. package/dist/replication/util.js.map +1 -0
  119. package/dist/routes/admin.d.ts +7 -0
  120. package/dist/routes/admin.js +192 -0
  121. package/dist/routes/admin.js.map +1 -0
  122. package/dist/routes/auth.d.ts +58 -0
  123. package/dist/routes/auth.js +182 -0
  124. package/dist/routes/auth.js.map +1 -0
  125. package/dist/routes/checkpointing.d.ts +3 -0
  126. package/dist/routes/checkpointing.js +30 -0
  127. package/dist/routes/checkpointing.js.map +1 -0
  128. package/dist/routes/dev.d.ts +6 -0
  129. package/dist/routes/dev.js +163 -0
  130. package/dist/routes/dev.js.map +1 -0
  131. package/dist/routes/route-generators.d.ts +15 -0
  132. package/dist/routes/route-generators.js +32 -0
  133. package/dist/routes/route-generators.js.map +1 -0
  134. package/dist/routes/router-socket.d.ts +10 -0
  135. package/dist/routes/router-socket.js +5 -0
  136. package/dist/routes/router-socket.js.map +1 -0
  137. package/dist/routes/router.d.ts +13 -0
  138. package/dist/routes/router.js +2 -0
  139. package/dist/routes/router.js.map +1 -0
  140. package/dist/routes/routes-index.d.ts +4 -0
  141. package/dist/routes/routes-index.js +5 -0
  142. package/dist/routes/routes-index.js.map +1 -0
  143. package/dist/routes/socket-route.d.ts +2 -0
  144. package/dist/routes/socket-route.js +119 -0
  145. package/dist/routes/socket-route.js.map +1 -0
  146. package/dist/routes/sync-rules.d.ts +6 -0
  147. package/dist/routes/sync-rules.js +182 -0
  148. package/dist/routes/sync-rules.js.map +1 -0
  149. package/dist/routes/sync-stream.d.ts +5 -0
  150. package/dist/routes/sync-stream.js +74 -0
  151. package/dist/routes/sync-stream.js.map +1 -0
  152. package/dist/runner/teardown.d.ts +2 -0
  153. package/dist/runner/teardown.js +79 -0
  154. package/dist/runner/teardown.js.map +1 -0
  155. package/dist/storage/BucketStorage.d.ts +298 -0
  156. package/dist/storage/BucketStorage.js +25 -0
  157. package/dist/storage/BucketStorage.js.map +1 -0
  158. package/dist/storage/MongoBucketStorage.d.ts +51 -0
  159. package/dist/storage/MongoBucketStorage.js +388 -0
  160. package/dist/storage/MongoBucketStorage.js.map +1 -0
  161. package/dist/storage/SourceTable.d.ts +39 -0
  162. package/dist/storage/SourceTable.js +50 -0
  163. package/dist/storage/SourceTable.js.map +1 -0
  164. package/dist/storage/mongo/MongoBucketBatch.d.ts +48 -0
  165. package/dist/storage/mongo/MongoBucketBatch.js +584 -0
  166. package/dist/storage/mongo/MongoBucketBatch.js.map +1 -0
  167. package/dist/storage/mongo/MongoIdSequence.d.ts +12 -0
  168. package/dist/storage/mongo/MongoIdSequence.js +21 -0
  169. package/dist/storage/mongo/MongoIdSequence.js.map +1 -0
  170. package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +9 -0
  171. package/dist/storage/mongo/MongoPersistedSyncRules.js +9 -0
  172. package/dist/storage/mongo/MongoPersistedSyncRules.js.map +1 -0
  173. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +20 -0
  174. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +26 -0
  175. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +1 -0
  176. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +27 -0
  177. package/dist/storage/mongo/MongoSyncBucketStorage.js +379 -0
  178. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +1 -0
  179. package/dist/storage/mongo/MongoSyncRulesLock.d.ts +16 -0
  180. package/dist/storage/mongo/MongoSyncRulesLock.js +65 -0
  181. package/dist/storage/mongo/MongoSyncRulesLock.js.map +1 -0
  182. package/dist/storage/mongo/OperationBatch.d.ts +26 -0
  183. package/dist/storage/mongo/OperationBatch.js +101 -0
  184. package/dist/storage/mongo/OperationBatch.js.map +1 -0
  185. package/dist/storage/mongo/PersistedBatch.d.ts +42 -0
  186. package/dist/storage/mongo/PersistedBatch.js +200 -0
  187. package/dist/storage/mongo/PersistedBatch.js.map +1 -0
  188. package/dist/storage/mongo/db.d.ts +23 -0
  189. package/dist/storage/mongo/db.js +34 -0
  190. package/dist/storage/mongo/db.js.map +1 -0
  191. package/dist/storage/mongo/models.d.ts +137 -0
  192. package/dist/storage/mongo/models.js +27 -0
  193. package/dist/storage/mongo/models.js.map +1 -0
  194. package/dist/storage/mongo/util.d.ts +26 -0
  195. package/dist/storage/mongo/util.js +81 -0
  196. package/dist/storage/mongo/util.js.map +1 -0
  197. package/dist/storage/storage-index.d.ts +14 -0
  198. package/dist/storage/storage-index.js +15 -0
  199. package/dist/storage/storage-index.js.map +1 -0
  200. package/dist/sync/BroadcastIterable.d.ts +38 -0
  201. package/dist/sync/BroadcastIterable.js +153 -0
  202. package/dist/sync/BroadcastIterable.js.map +1 -0
  203. package/dist/sync/LastValueSink.d.ts +25 -0
  204. package/dist/sync/LastValueSink.js +84 -0
  205. package/dist/sync/LastValueSink.js.map +1 -0
  206. package/dist/sync/merge.d.ts +39 -0
  207. package/dist/sync/merge.js +175 -0
  208. package/dist/sync/merge.js.map +1 -0
  209. package/dist/sync/safeRace.d.ts +1 -0
  210. package/dist/sync/safeRace.js +91 -0
  211. package/dist/sync/safeRace.js.map +1 -0
  212. package/dist/sync/sync-index.d.ts +6 -0
  213. package/dist/sync/sync-index.js +7 -0
  214. package/dist/sync/sync-index.js.map +1 -0
  215. package/dist/sync/sync.d.ts +18 -0
  216. package/dist/sync/sync.js +248 -0
  217. package/dist/sync/sync.js.map +1 -0
  218. package/dist/sync/util.d.ts +26 -0
  219. package/dist/sync/util.js +73 -0
  220. package/dist/sync/util.js.map +1 -0
  221. package/dist/system/CorePowerSyncSystem.d.ts +18 -0
  222. package/dist/system/CorePowerSyncSystem.js +28 -0
  223. package/dist/system/CorePowerSyncSystem.js.map +1 -0
  224. package/dist/util/Mutex.d.ts +47 -0
  225. package/dist/util/Mutex.js +132 -0
  226. package/dist/util/Mutex.js.map +1 -0
  227. package/dist/util/PgManager.d.ts +24 -0
  228. package/dist/util/PgManager.js +55 -0
  229. package/dist/util/PgManager.js.map +1 -0
  230. package/dist/util/alerting.d.ts +4 -0
  231. package/dist/util/alerting.js +14 -0
  232. package/dist/util/alerting.js.map +1 -0
  233. package/dist/util/config/collectors/config-collector.d.ts +29 -0
  234. package/dist/util/config/collectors/config-collector.js +116 -0
  235. package/dist/util/config/collectors/config-collector.js.map +1 -0
  236. package/dist/util/config/collectors/impl/base64-config-collector.d.ts +6 -0
  237. package/dist/util/config/collectors/impl/base64-config-collector.js +15 -0
  238. package/dist/util/config/collectors/impl/base64-config-collector.js.map +1 -0
  239. package/dist/util/config/collectors/impl/fallback-config-collector.d.ts +11 -0
  240. package/dist/util/config/collectors/impl/fallback-config-collector.js +19 -0
  241. package/dist/util/config/collectors/impl/fallback-config-collector.js.map +1 -0
  242. package/dist/util/config/collectors/impl/filesystem-config-collector.d.ts +6 -0
  243. package/dist/util/config/collectors/impl/filesystem-config-collector.js +35 -0
  244. package/dist/util/config/collectors/impl/filesystem-config-collector.js.map +1 -0
  245. package/dist/util/config/compound-config-collector.d.ts +32 -0
  246. package/dist/util/config/compound-config-collector.js +126 -0
  247. package/dist/util/config/compound-config-collector.js.map +1 -0
  248. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.d.ts +7 -0
  249. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js +17 -0
  250. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js.map +1 -0
  251. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.d.ts +7 -0
  252. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js +21 -0
  253. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js.map +1 -0
  254. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.d.ts +7 -0
  255. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js +17 -0
  256. package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js.map +1 -0
  257. package/dist/util/config/sync-rules/sync-collector.d.ts +6 -0
  258. package/dist/util/config/sync-rules/sync-collector.js +3 -0
  259. package/dist/util/config/sync-rules/sync-collector.js.map +1 -0
  260. package/dist/util/config/types.d.ts +53 -0
  261. package/dist/util/config/types.js +7 -0
  262. package/dist/util/config/types.js.map +1 -0
  263. package/dist/util/config.d.ts +7 -0
  264. package/dist/util/config.js +35 -0
  265. package/dist/util/config.js.map +1 -0
  266. package/dist/util/env.d.ts +10 -0
  267. package/dist/util/env.js +25 -0
  268. package/dist/util/env.js.map +1 -0
  269. package/dist/util/memory-tracking.d.ts +7 -0
  270. package/dist/util/memory-tracking.js +58 -0
  271. package/dist/util/memory-tracking.js.map +1 -0
  272. package/dist/util/migration_lib.d.ts +11 -0
  273. package/dist/util/migration_lib.js +64 -0
  274. package/dist/util/migration_lib.js.map +1 -0
  275. package/dist/util/pgwire_utils.d.ts +24 -0
  276. package/dist/util/pgwire_utils.js +117 -0
  277. package/dist/util/pgwire_utils.js.map +1 -0
  278. package/dist/util/populate_test_data.d.ts +8 -0
  279. package/dist/util/populate_test_data.js +65 -0
  280. package/dist/util/populate_test_data.js.map +1 -0
  281. package/dist/util/protocol-types.d.ts +178 -0
  282. package/dist/util/protocol-types.js +38 -0
  283. package/dist/util/protocol-types.js.map +1 -0
  284. package/dist/util/secs.d.ts +2 -0
  285. package/dist/util/secs.js +49 -0
  286. package/dist/util/secs.js.map +1 -0
  287. package/dist/util/util-index.d.ts +22 -0
  288. package/dist/util/util-index.js +23 -0
  289. package/dist/util/util-index.js.map +1 -0
  290. package/dist/util/utils.d.ts +14 -0
  291. package/dist/util/utils.js +75 -0
  292. package/dist/util/utils.js.map +1 -0
  293. package/package.json +55 -0
  294. package/src/api/api-index.ts +2 -0
  295. package/src/api/diagnostics.ts +221 -0
  296. package/src/api/schema.ts +99 -0
  297. package/src/auth/CachedKeyCollector.ts +132 -0
  298. package/src/auth/CompoundKeyCollector.ts +33 -0
  299. package/src/auth/JwtPayload.ts +11 -0
  300. package/src/auth/KeyCollector.ts +27 -0
  301. package/src/auth/KeySpec.ts +67 -0
  302. package/src/auth/KeyStore.ts +156 -0
  303. package/src/auth/LeakyBucket.ts +66 -0
  304. package/src/auth/RemoteJWKSCollector.ts +130 -0
  305. package/src/auth/StaticKeyCollector.ts +21 -0
  306. package/src/auth/SupabaseKeyCollector.ts +67 -0
  307. package/src/auth/auth-index.ts +10 -0
  308. package/src/db/db-index.ts +1 -0
  309. package/src/db/mongo.ts +72 -0
  310. package/src/entry/cli-entry.ts +41 -0
  311. package/src/entry/commands/config-command.ts +36 -0
  312. package/src/entry/commands/migrate-action.ts +25 -0
  313. package/src/entry/commands/start-action.ts +24 -0
  314. package/src/entry/commands/teardown-action.ts +23 -0
  315. package/src/entry/entry-index.ts +5 -0
  316. package/src/index.ts +37 -0
  317. package/src/metrics/metrics.ts +169 -0
  318. package/src/migrations/db/migrations/1684951997326-init.ts +33 -0
  319. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
  320. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +99 -0
  321. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +32 -0
  322. package/src/migrations/db/store.ts +11 -0
  323. package/src/migrations/migrations.ts +122 -0
  324. package/src/replication/ErrorRateLimiter.ts +49 -0
  325. package/src/replication/PgRelation.ts +42 -0
  326. package/src/replication/WalConnection.ts +227 -0
  327. package/src/replication/WalStream.ts +626 -0
  328. package/src/replication/WalStreamManager.ts +214 -0
  329. package/src/replication/WalStreamRunner.ts +180 -0
  330. package/src/replication/replication-index.ts +7 -0
  331. package/src/replication/util.ts +76 -0
  332. package/src/routes/admin.ts +229 -0
  333. package/src/routes/auth.ts +209 -0
  334. package/src/routes/checkpointing.ts +38 -0
  335. package/src/routes/dev.ts +194 -0
  336. package/src/routes/route-generators.ts +39 -0
  337. package/src/routes/router-socket.ts +13 -0
  338. package/src/routes/router.ts +17 -0
  339. package/src/routes/routes-index.ts +5 -0
  340. package/src/routes/socket-route.ts +131 -0
  341. package/src/routes/sync-rules.ts +210 -0
  342. package/src/routes/sync-stream.ts +92 -0
  343. package/src/runner/teardown.ts +91 -0
  344. package/src/storage/BucketStorage.ts +386 -0
  345. package/src/storage/MongoBucketStorage.ts +493 -0
  346. package/src/storage/SourceTable.ts +60 -0
  347. package/src/storage/mongo/MongoBucketBatch.ts +756 -0
  348. package/src/storage/mongo/MongoIdSequence.ts +24 -0
  349. package/src/storage/mongo/MongoPersistedSyncRules.ts +16 -0
  350. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +47 -0
  351. package/src/storage/mongo/MongoSyncBucketStorage.ts +517 -0
  352. package/src/storage/mongo/MongoSyncRulesLock.ts +81 -0
  353. package/src/storage/mongo/OperationBatch.ts +115 -0
  354. package/src/storage/mongo/PersistedBatch.ts +245 -0
  355. package/src/storage/mongo/db.ts +69 -0
  356. package/src/storage/mongo/models.ts +157 -0
  357. package/src/storage/mongo/util.ts +88 -0
  358. package/src/storage/storage-index.ts +15 -0
  359. package/src/sync/BroadcastIterable.ts +161 -0
  360. package/src/sync/LastValueSink.ts +100 -0
  361. package/src/sync/merge.ts +200 -0
  362. package/src/sync/safeRace.ts +99 -0
  363. package/src/sync/sync-index.ts +6 -0
  364. package/src/sync/sync.ts +312 -0
  365. package/src/sync/util.ts +98 -0
  366. package/src/system/CorePowerSyncSystem.ts +43 -0
  367. package/src/util/Mutex.ts +159 -0
  368. package/src/util/PgManager.ts +64 -0
  369. package/src/util/alerting.ts +17 -0
  370. package/src/util/config/collectors/config-collector.ts +141 -0
  371. package/src/util/config/collectors/impl/base64-config-collector.ts +18 -0
  372. package/src/util/config/collectors/impl/fallback-config-collector.ts +22 -0
  373. package/src/util/config/collectors/impl/filesystem-config-collector.ts +41 -0
  374. package/src/util/config/compound-config-collector.ts +171 -0
  375. package/src/util/config/sync-rules/impl/base64-sync-rules-collector.ts +21 -0
  376. package/src/util/config/sync-rules/impl/filesystem-sync-rules-collector.ts +26 -0
  377. package/src/util/config/sync-rules/impl/inline-sync-rules-collector.ts +21 -0
  378. package/src/util/config/sync-rules/sync-collector.ts +8 -0
  379. package/src/util/config/types.ts +60 -0
  380. package/src/util/config.ts +39 -0
  381. package/src/util/env.ts +28 -0
  382. package/src/util/memory-tracking.ts +67 -0
  383. package/src/util/migration_lib.ts +79 -0
  384. package/src/util/pgwire_utils.ts +139 -0
  385. package/src/util/populate_test_data.ts +78 -0
  386. package/src/util/protocol-types.ts +223 -0
  387. package/src/util/secs.ts +54 -0
  388. package/src/util/util-index.ts +25 -0
  389. package/src/util/utils.ts +102 -0
  390. package/test/src/__snapshots__/pg_test.test.ts.snap +256 -0
  391. package/test/src/__snapshots__/sync.test.ts.snap +235 -0
  392. package/test/src/auth.test.ts +340 -0
  393. package/test/src/broadcast_iterable.test.ts +156 -0
  394. package/test/src/data_storage.test.ts +1176 -0
  395. package/test/src/env.ts +8 -0
  396. package/test/src/large_batch.test.ts +194 -0
  397. package/test/src/merge_iterable.test.ts +355 -0
  398. package/test/src/pg_test.test.ts +432 -0
  399. package/test/src/schema_changes.test.ts +545 -0
  400. package/test/src/slow_tests.test.ts +257 -0
  401. package/test/src/sql_functions.test.ts +254 -0
  402. package/test/src/sql_operators.test.ts +132 -0
  403. package/test/src/sync.test.ts +293 -0
  404. package/test/src/sync_rules.test.ts +1051 -0
  405. package/test/src/util.ts +67 -0
  406. package/test/src/validation.test.ts +63 -0
  407. package/test/src/wal_stream.test.ts +310 -0
  408. package/test/src/wal_stream_utils.ts +147 -0
  409. package/test/tsconfig.json +20 -0
  410. package/tsconfig.json +20 -0
  411. package/tsconfig.tsbuildinfo +1 -0
  412. package/vitest.config.ts +11 -0
@@ -0,0 +1,132 @@
1
+ const DEBUG_MUTEX = false;
2
+ class ExclusiveContext {
3
+ constructor(mutex) {
4
+ this.mutex = mutex;
5
+ }
6
+ exclusiveLock(promiseFn) {
7
+ return promiseFn(this);
8
+ }
9
+ sharedLock(promiseFn) {
10
+ return promiseFn(this);
11
+ }
12
+ }
13
+ class SharedContext {
14
+ constructor(mutex) {
15
+ this.mutex = mutex;
16
+ }
17
+ async exclusiveLock(promiseFn) {
18
+ throw new Error('Cannot upgrade a shared lock to an exclusive lock.');
19
+ }
20
+ sharedLock(promiseFn) {
21
+ return promiseFn(this);
22
+ }
23
+ }
24
+ /**
25
+ * Mutex maintains a queue of Promise-returning functions that
26
+ * are executed sequentially (whereas normally they would execute their async code concurrently).
27
+ */
28
+ export class Mutex {
29
+ constructor() {
30
+ this.queue = [];
31
+ this.sharedCount = 0;
32
+ this.exclusiveLocked = false;
33
+ }
34
+ /**
35
+ * Place a function on the queue.
36
+ * The function may either return a Promise or a value.
37
+ * Return a Promise that is resolved with the result of the function.
38
+ */
39
+ async exclusiveLock(promiseFn) {
40
+ return this.lock(promiseFn, true);
41
+ }
42
+ /**
43
+ * Place a function on the queue.
44
+ * This function may execute in parallel with other "multi" functions, but not with other functions on the exclusive
45
+ * queue.
46
+ */
47
+ sharedLock(promiseFn) {
48
+ return this.lock(promiseFn, false);
49
+ }
50
+ async lock(promiseFn, exclusive) {
51
+ const context = await this._lockNext(exclusive);
52
+ let timeout;
53
+ try {
54
+ if (DEBUG_MUTEX) {
55
+ const stack = new Error().stack;
56
+ timeout = setTimeout(() => {
57
+ console.warn('Mutex not released in 10 seconds\n', stack);
58
+ }, 10000);
59
+ }
60
+ return await promiseFn(context);
61
+ }
62
+ finally {
63
+ if (DEBUG_MUTEX) {
64
+ clearTimeout(timeout);
65
+ }
66
+ if (!exclusive) {
67
+ this.sharedCount -= 1;
68
+ }
69
+ else {
70
+ this.exclusiveLocked = false;
71
+ }
72
+ this._tryNext();
73
+ }
74
+ }
75
+ /**
76
+ * Convert a normal Promise-returning function into one that is automatically enqueued.
77
+ * The signature of the function stays the same - only the execution is potentially delayed.
78
+ * The only exception is that if the function would have returned a scalar value, it now
79
+ * returns a Promise.
80
+ */
81
+ qu(fn) {
82
+ var self = this;
83
+ return function () {
84
+ var args = arguments;
85
+ return self.exclusiveLock(function () {
86
+ return fn.apply(null, args);
87
+ });
88
+ };
89
+ }
90
+ /**
91
+ * Wait until we are ready to execute the next task on the queue.
92
+ *
93
+ * This places a "Task" marker on the queue, and waits until we get to it.
94
+ *
95
+ * @param exclusive
96
+ */
97
+ _lockNext(exclusive) {
98
+ var self = this;
99
+ return new Promise(function (resolve, reject) {
100
+ const task = {
101
+ execute: resolve,
102
+ exclusive: exclusive ?? false
103
+ };
104
+ self.queue.push(task);
105
+ self._tryNext();
106
+ });
107
+ }
108
+ _tryNext() {
109
+ if (this.queue.length == 0) {
110
+ return false;
111
+ }
112
+ if (this.exclusiveLocked) {
113
+ return false;
114
+ }
115
+ var task = this.queue[0];
116
+ if (!task.exclusive) {
117
+ this.sharedCount += 1;
118
+ this.queue.shift();
119
+ task.execute(new SharedContext(this));
120
+ }
121
+ else if (this.sharedCount == 0) {
122
+ this.exclusiveLocked = true;
123
+ this.queue.shift();
124
+ task.execute(new ExclusiveContext(this));
125
+ }
126
+ else {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ }
132
+ //# sourceMappingURL=Mutex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Mutex.js","sourceRoot":"","sources":["../../src/util/Mutex.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,GAAG,KAAK,CAAC;AAe1B,MAAM,gBAAgB;IACpB,YAAoB,KAAY;QAAZ,UAAK,GAAL,KAAK,CAAO;IAAG,CAAC;IAEpC,aAAa,CAAI,SAA6B;QAC5C,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAED,UAAU,CAAI,SAA6B;QACzC,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;CACF;AAED,MAAM,aAAa;IACjB,YAAoB,KAAY;QAAZ,UAAK,GAAL,K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IAAI,CAAC,OAAO,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,IAAI,CAAC,WAAW,IAAI,CAAC,EAAE,CAAC;YACjC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;YAC5B,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YACnB,IAAI,CAAC,OAAO,CAAC,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;QAC3C,CAAC;aAAM,CAAC;YACN,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,24 @@
1
+ import * as pgwire from '@powersync/service-jpgwire';
2
+ import { NormalizedPostgresConnection } from '@powersync/service-types';
3
+ export declare class PgManager {
4
+ options: NormalizedPostgresConnection;
5
+ poolOptions: pgwire.PgPoolOptions;
6
+ /**
7
+ * Do not use this for any transactions.
8
+ */
9
+ readonly pool: pgwire.PgClient;
10
+ private connectionPromises;
11
+ constructor(options: NormalizedPostgresConnection, poolOptions: pgwire.PgPoolOptions);
12
+ /**
13
+ * Create a new replication connection.
14
+ */
15
+ replicationConnection(): Promise<pgwire.PgConnection>;
16
+ /**
17
+ * Create a new standard connection, used for initial snapshot.
18
+ *
19
+ * This connection must not be shared between multiple async contexts.
20
+ */
21
+ snapshotConnection(): Promise<pgwire.PgConnection>;
22
+ end(): Promise<void>;
23
+ destroy(): Promise<void>;
24
+ }
@@ -0,0 +1,55 @@
1
+ import * as pgwire from '@powersync/service-jpgwire';
2
+ export class PgManager {
3
+ constructor(options, poolOptions) {
4
+ this.options = options;
5
+ this.poolOptions = poolOptions;
6
+ this.connectionPromises = [];
7
+ // The pool is lazy - no connections are opened until a query is performed.
8
+ this.pool = pgwire.connectPgWirePool(this.options, poolOptions);
9
+ }
10
+ /**
11
+ * Create a new replication connection.
12
+ */
13
+ async replicationConnection() {
14
+ const p = pgwire.connectPgWire(this.options, { type: 'replication' });
15
+ this.connectionPromises.push(p);
16
+ return await p;
17
+ }
18
+ /**
19
+ * Create a new standard connection, used for initial snapshot.
20
+ *
21
+ * This connection must not be shared between multiple async contexts.
22
+ */
23
+ async snapshotConnection() {
24
+ const p = pgwire.connectPgWire(this.options, { type: 'standard' });
25
+ this.connectionPromises.push(p);
26
+ return await p;
27
+ }
28
+ async end() {
29
+ for (let result of await Promise.allSettled([
30
+ this.pool.end(),
31
+ ...this.connectionPromises.map((promise) => {
32
+ return promise.then((connection) => connection.end());
33
+ })
34
+ ])) {
35
+ // Throw the first error, if any
36
+ if (result.status == 'rejected') {
37
+ throw result.reason;
38
+ }
39
+ }
40
+ }
41
+ async destroy() {
42
+ this.pool.destroy();
43
+ for (let result of await Promise.allSettled([
44
+ ...this.connectionPromises.map((promise) => {
45
+ return promise.then((connection) => connection.destroy());
46
+ })
47
+ ])) {
48
+ // Throw the first error, if any
49
+ if (result.status == 'rejected') {
50
+ throw result.reason;
51
+ }
52
+ }
53
+ }
54
+ }
55
+ //# sourceMappingURL=PgManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PgManager.js","sourceRoot":"","sources":["../../src/util/PgManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,4BAA4B,CAAC;AAGrD,MAAM,OAAO,SAAS;IAQpB,YAAmB,OAAqC,EAAS,WAAiC;QAA/E,YAAO,GAAP,OAAO,CAA8B;QAAS,gBAAW,GAAX,WAAW,CAAsB;QAF1F,uBAAkB,GAAmC,EAAE,CAAC;QAG9D,2EAA2E;QAC3E,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,iBAAiB,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IAClE,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,qBAAqB;QACzB,MAAM,CAAC,GAAG,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC,CAAC;QACtE,IA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AAI,UAAU,EAAE,CAAC;gBAChC,MAAM,MAAM,CAAC,MAAM,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import * as micro from '@journeyapps-platform/micro';
2
+ export declare function setTags(tags: Record<string, string>): void;
3
+ export declare function getGlobalTags(): Record<string, string>;
4
+ export declare function captureException(error: any, options?: micro.alerts.CaptureOptions): void;
@@ -0,0 +1,14 @@
1
+ import * as micro from '@journeyapps-platform/micro';
2
+ let globalTags = {};
3
+ export function setTags(tags) {
4
+ globalTags = tags;
5
+ }
6
+ export function getGlobalTags() {
7
+ return globalTags;
8
+ }
9
+ export function captureException(error, options) {
10
+ micro.alerts.captureException(error, {
11
+ ...options
12
+ });
13
+ }
14
+ //# sourceMappingURL=alerting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alerting.js","sourceRoot":"","sources":["../../src/util/alerting.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,6BAA6B,CAAC;AAErD,IAAI,UAAU,GAA2B,EAAE,CAAC;AAE5C,MAAM,UAAU,OAAO,CAAC,IAA4B;IAClD,UAAU,GAAG,IAAI,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,aAAa;IAC3B,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,KAAU,EAAE,OAAqC;IAChF,KAAK,CAAC,MAAM,CAAC,gBAAgB,CAAC,KAAK,EAAE;QACnC,GAAG,OAAO;KACX,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { configFile } from '@powersync/service-types';
2
+ import { RunnerConfig } from '../types.js';
3
+ export declare enum ConfigFileFormat {
4
+ YAML = "yaml",
5
+ JSON = "json"
6
+ }
7
+ export declare abstract class ConfigCollector {
8
+ abstract get name(): string;
9
+ /**
10
+ * Collects the serialized base PowerSyncConfig.
11
+ * @returns null if this collector cannot provide a config
12
+ */
13
+ abstract collectSerialized(runnerConfig: RunnerConfig): Promise<configFile.SerializedPowerSyncConfig | null>;
14
+ /**
15
+ * Collects the PowerSyncConfig settings.
16
+ * Validates and decodes the config.
17
+ * @returns null if this collector cannot provide a config
18
+ */
19
+ collect(runner_config: RunnerConfig): Promise<configFile.PowerSyncConfig | null>;
20
+ /**
21
+ * Validates input config
22
+ * ts-codec itself doesn't give great validation errors, so we use json schema for that
23
+ */
24
+ validate(config: configFile.PowerSyncConfig): void;
25
+ decode(encoded: configFile.SerializedPowerSyncConfig): configFile.PowerSyncConfig;
26
+ protected parseContent(content: string, contentType?: ConfigFileFormat): any;
27
+ protected parseYaml(content: string): any;
28
+ protected parseJSON(content: string): any;
29
+ }
@@ -0,0 +1,116 @@
1
+ import * as t from 'ts-codec';
2
+ import { configFile } from '@powersync/service-types';
3
+ import * as micro from '@journeyapps-platform/micro';
4
+ import * as yaml from 'yaml';
5
+ export var ConfigFileFormat;
6
+ (function (ConfigFileFormat) {
7
+ ConfigFileFormat["YAML"] = "yaml";
8
+ ConfigFileFormat["JSON"] = "json";
9
+ })(ConfigFileFormat || (ConfigFileFormat = {}));
10
+ /**
11
+ * Environment variables can be substituted into the YAML config
12
+ * when parsing if the environment variable name starts with this prefix.
13
+ * Attempting to substitute any other environment variable will throw an exception.
14
+ *
15
+ * Example of substitution:
16
+ * storage:
17
+ * type: mongodb
18
+ * uri: !env PS_MONGO_URI
19
+ */
20
+ const YAML_ENV_PREFIX = 'PS_';
21
+ // ts-codec itself doesn't give great validation errors, so we use json schema for that
22
+ const configSchemaValidator = micro.schema
23
+ .parseJSONSchema(t.generateJSONSchema(configFile.powerSyncConfig, { allowAdditional: true, parsers: [configFile.portParser] }))
24
+ .validator();
25
+ export class ConfigCollector {
26
+ /**
27
+ * Collects the PowerSyncConfig settings.
28
+ * Validates and decodes the config.
29
+ * @returns null if this collector cannot provide a config
30
+ */
31
+ async collect(runner_config) {
32
+ const serialized = await this.collectSerialized(runner_config);
33
+ if (!serialized) {
34
+ return null;
35
+ }
36
+ /**
37
+ * After this point a serialized config has been found. Any failures to decode or validate
38
+ * will result in a hard stop.
39
+ */
40
+ const decoded = this.decode(serialized);
41
+ this.validate(decoded);
42
+ return decoded;
43
+ }
44
+ /**
45
+ * Validates input config
46
+ * ts-codec itself doesn't give great validation errors, so we use json schema for that
47
+ */
48
+ validate(config) {
49
+ const valid = configSchemaValidator.validate(config);
50
+ if (!valid.valid) {
51
+ throw new Error(`Failed to validate PowerSync config: ${valid.errors.join(', ')}`);
52
+ }
53
+ }
54
+ decode(encoded) {
55
+ try {
56
+ return configFile.powerSyncConfig.decode(encoded);
57
+ }
58
+ catch (ex) {
59
+ throw new Error(`Failed to decode PowerSync config: ${ex}`);
60
+ }
61
+ }
62
+ parseContent(content, contentType) {
63
+ switch (contentType) {
64
+ case ConfigFileFormat.YAML:
65
+ return this.parseYaml(content);
66
+ case ConfigFileFormat.JSON:
67
+ return this.parseJSON(content);
68
+ default: {
69
+ // No content type provided, need to try both
70
+ try {
71
+ return this.parseYaml(content);
72
+ }
73
+ catch (ex) { }
74
+ try {
75
+ return this.parseJSON(content);
76
+ }
77
+ catch (ex) {
78
+ throw new Error(`Could not parse PowerSync config file content as JSON or YAML: ${ex}`);
79
+ }
80
+ }
81
+ }
82
+ }
83
+ parseYaml(content) {
84
+ const lineCounter = new yaml.LineCounter();
85
+ const parsed = yaml.parseDocument(content, {
86
+ schema: 'core',
87
+ keepSourceTokens: true,
88
+ lineCounter,
89
+ customTags: [
90
+ {
91
+ tag: '!env',
92
+ resolve(envName, onError) {
93
+ if (!envName.startsWith(YAML_ENV_PREFIX)) {
94
+ onError(`Attempting to substitute environment variable ${envName} is not allowed. Variables must start with "${YAML_ENV_PREFIX}"`);
95
+ return envName;
96
+ }
97
+ const value = process.env[envName];
98
+ if (typeof value == 'undefined') {
99
+ onError(`Attempted to substitute environment variable "${envName}" which is undefined. Set this variable on the environment.`);
100
+ return envName;
101
+ }
102
+ return value;
103
+ }
104
+ }
105
+ ]
106
+ });
107
+ if (parsed.errors.length) {
108
+ throw new Error(`Could not parse YAML configuration file. Received errors: \n ${parsed.errors.map((e) => e.message).join('\n')}`);
109
+ }
110
+ return parsed.toJS();
111
+ }
112
+ parseJSON(content) {
113
+ return JSON.parse(content);
114
+ }
115
+ }
116
+ //# sourceMappingURL=config-collector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config-collector.js","sourceRoot":"","sources":["../../../../src/util/config/collectors/config-collector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,KAAK,KAAK,MAAM,6BAA6B,CAAC;AAErD,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,MAAM,CAAN,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,iCAAa,CAAA;IACb,iCAAa,CAAA;AACf,CAAC,EAHW,gBAAgB,KAAhB,gBAAgB,QAG3B;AAED;;;;;;;;;GASG;AACH,MAAM,eAAe,GAAG,KAAK,CAAC;AAE9B,uFAAuF;AACvF,MAAM,qBAAqB,GAAG,KAAK,CAAC,MAAM;KACvC,eAAe,CACd,CAAC,CAAC,kBAAkB,CAAC,UAAU,CAAC,eAAe,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,CAC9G;KACA,SAAS,EAAE,CAAC;AAEf,MAAM,OAAgB,eAAe;IASnC;;;;OAIG;IACH,KAAK,CAAC,OAAO,CAAC,aAA2B;QACvC,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC;QAC/D,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC;QACd,CAAC;QAED;;;WAGG;QACH,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,C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AS,CAAC,OAAe;QACjC,MAAM,WAAW,GAAG,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;QAE3C,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE;YACzC,MAAM,EAAE,MAAM;YACd,gBAAgB,EAAE,IAAI;YACtB,WAAW;YACX,UAAU,EAAE;gBACV;oBACE,GAAG,EAAE,MAAM;oBACX,OAAO,CAAC,OAAe,EAAE,OAAgC;wBACvD,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,EAAE,CAAC;4BACzC,OAAO,CACL,iDAAiD,OAAO,+CAA+C,eAAe,GAAG,CAC1H,CAAC;4BACF,OAAO,OAAO,CAAC;wBACjB,CAAC;wBACD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;wBACnC,IAAI,OAAO,KAAK,IAAI,WAAW,EAAE,CAAC;4BAChC,OAAO,CACL,iDAAiD,OAAO,6DAA6D,CACtH,CAAC;4BACF,OAAO,OAAO,CAAC;wBACjB,CAAC;wBACD,OAAO,KAAK,CAAC;oBACf,CAAC;iBACF;aACF;SACF,CAAC,CAAC;QAEH,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CACb,gEAAgE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACjH,CAAC;QACJ,CAAC;QAED,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;IACvB,CAAC;IAES,SAAS,CAAC,OAAe;QACjC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ import { ConfigCollector } from '../config-collector.js';
2
+ import { RunnerConfig } from '../../types.js';
3
+ export declare class Base64ConfigCollector extends ConfigCollector {
4
+ get name(): string;
5
+ collectSerialized(runnerConfig: RunnerConfig): Promise<any>;
6
+ }
@@ -0,0 +1,15 @@
1
+ import { ConfigCollector } from '../config-collector.js';
2
+ export class Base64ConfigCollector extends ConfigCollector {
3
+ get name() {
4
+ return 'Base64';
5
+ }
6
+ async collectSerialized(runnerConfig) {
7
+ const { config_base64 } = runnerConfig;
8
+ if (!config_base64) {
9
+ return null;
10
+ }
11
+ // Could be JSON or YAML at this point
12
+ return this.parseContent(Buffer.from(config_base64, 'base64').toString());
13
+ }
14
+ }
15
+ //# sourceMappingURL=base64-config-collector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64-config-collector.js","sourceRoot":"","sources":["../../../../../src/util/config/collectors/impl/base64-config-collector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAGzD,MAAM,OAAO,qBAAsB,SAAQ,eAAe;IACxD,IAAI,IAAI;QACN,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,YAA0B;QAChD,MAAM,EAAE,aAAa,EAAE,GAAG,YAAY,CAAC;QACvC,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,sCAAsC;QACtC,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC5E,CAAC;CACF"}
@@ -0,0 +1,11 @@
1
+ import { RunnerConfig } from '../../types.js';
2
+ import { FileSystemConfigCollector } from './filesystem-config-collector.js';
3
+ export declare const DEFAULT_CONFIG_LOCATION = "powersync.yaml";
4
+ /**
5
+ * Falls back to reading the PowerSync config file from the previous
6
+ * default location of `powersync.yaml`
7
+ */
8
+ export declare class FallbackConfigCollector extends FileSystemConfigCollector {
9
+ get name(): string;
10
+ collectSerialized(runnerConfig: RunnerConfig): Promise<any>;
11
+ }
@@ -0,0 +1,19 @@
1
+ import { FileSystemConfigCollector } from './filesystem-config-collector.js';
2
+ export const DEFAULT_CONFIG_LOCATION = 'powersync.yaml';
3
+ /**
4
+ * Falls back to reading the PowerSync config file from the previous
5
+ * default location of `powersync.yaml`
6
+ */
7
+ export class FallbackConfigCollector extends FileSystemConfigCollector {
8
+ get name() {
9
+ return 'Fallback powersync.yaml';
10
+ }
11
+ async collectSerialized(runnerConfig) {
12
+ return super.collectSerialized({
13
+ ...runnerConfig,
14
+ // Use the fallback config location
15
+ config_path: DEFAULT_CONFIG_LOCATION
16
+ });
17
+ }
18
+ }
19
+ //# sourceMappingURL=fallback-config-collector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fallback-config-collector.js","sourceRoot":"","sources":["../../../../../src/util/config/collectors/impl/fallback-config-collector.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAE7E,MAAM,CAAC,MAAM,uBAAuB,GAAG,gBAAgB,CAAC;AAExD;;;GAGG;AACH,MAAM,OAAO,uBAAwB,SAAQ,yBAAyB;IACpE,IAAI,IAAI;QACN,OAAO,yBAAyB,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,YAA0B;QAChD,OAAO,KAAK,CAAC,iBAAiB,CAAC;YAC7B,GAAG,YAAY;YACf,mCAAmC;YACnC,WAAW,EAAE,uBAAuB;SACrC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ import { ConfigCollector } from '../config-collector.js';
2
+ import { RunnerConfig } from '../../types.js';
3
+ export declare class FileSystemConfigCollector extends ConfigCollector {
4
+ get name(): string;
5
+ collectSerialized(runnerConfig: RunnerConfig): Promise<any>;
6
+ }
@@ -0,0 +1,35 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as micro from '@journeyapps-platform/micro';
3
+ import { ConfigCollector, ConfigFileFormat } from '../config-collector.js';
4
+ export class FileSystemConfigCollector extends ConfigCollector {
5
+ get name() {
6
+ return 'FileSystem';
7
+ }
8
+ async collectSerialized(runnerConfig) {
9
+ const { config_path } = runnerConfig;
10
+ if (!config_path) {
11
+ return null;
12
+ }
13
+ // Check if file exists
14
+ try {
15
+ await fs.access(config_path, fs.constants.F_OK);
16
+ }
17
+ catch (ex) {
18
+ throw new Error(`Config file path ${config_path} was specified, but the file does not exist.`);
19
+ }
20
+ micro.logger.info(`Collecting PowerSync configuration from File: ${config_path}`);
21
+ const content = await fs.readFile(config_path, 'utf-8');
22
+ let contentType;
23
+ switch (true) {
24
+ case config_path.endsWith('.yaml'):
25
+ case config_path.endsWith('.yml'):
26
+ contentType = ConfigFileFormat.YAML;
27
+ break;
28
+ case config_path.endsWith('.json'):
29
+ contentType = ConfigFileFormat.JSON;
30
+ break;
31
+ }
32
+ return this.parseContent(content, contentType);
33
+ }
34
+ }
35
+ //# sourceMappingURL=filesystem-config-collector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filesystem-config-collector.js","sourceRoot":"","sources":["../../../../../src/util/config/collectors/impl/filesystem-config-collector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,KAAK,KAAK,MAAM,6BAA6B,CAAC;AAErD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAG3E,MAAM,OAAO,yBAA0B,SAAQ,eAAe;IAC5D,IAAI,IAAI;QACN,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,YAA0B;QAChD,MAAM,EAAE,WAAW,EAAE,GAAG,YAAY,CAAC;QACrC,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,uBAAuB;QACvB,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;QAClD,CAAC;QAAC,OAAO,EAAE,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,oBAAoB,WAAW,8CAA8C,CAAC,CAAC;QACjG,CAAC;QAED,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,iDAAiD,WAAW,EAAE,CAAC,CAAC;QAElF,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAExD,IAAI,WAAyC,CAAC;QAC9C,QAAQ,IAAI,EAAE,CAAC;YACb,KAAK,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YACnC,KAAK,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC;gBAC/B,WAAW,GAAG,gBAAgB,CAAC,IAAI,CAAC;gBACpC,MAAM;YACR,KAAK,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC;gBAChC,WAAW,GAAG,gBAAgB,CAAC,IAAI,CAAC;gBACpC,MAAM;QACV,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;IACjD,CAAC;CACF"}
@@ -0,0 +1,32 @@
1
+ import { configFile } from '@powersync/service-types';
2
+ import { ConfigCollector } from './collectors/config-collector.js';
3
+ import { ResolvedPowerSyncConfig, RunnerConfig, SyncRulesConfig } from './types.js';
4
+ import { SyncRulesCollector } from './sync-rules/sync-collector.js';
5
+ export type CompoundConfigCollectorOptions = {
6
+ /**
7
+ * Collectors for PowerSync configuration content.
8
+ * The configuration from first collector to provide a configuration
9
+ * is used. The order of the collectors specifies precedence
10
+ */
11
+ configCollectors: ConfigCollector[];
12
+ /**
13
+ * Collectors for PowerSync sync rules content.
14
+ * The configuration from first collector to provide a configuration
15
+ * is used. The order of the collectors specifies precedence
16
+ */
17
+ syncRulesCollectors: SyncRulesCollector[];
18
+ };
19
+ export declare class CompoundConfigCollector {
20
+ protected options: CompoundConfigCollectorOptions;
21
+ constructor(options?: CompoundConfigCollectorOptions);
22
+ /**
23
+ * Collects and resolves base config
24
+ */
25
+ collectConfig(runner_config?: RunnerConfig): Promise<ResolvedPowerSyncConfig>;
26
+ /**
27
+ * Collects the base PowerSyncConfig from various registered collectors.
28
+ * @throws if no collector could return a configuration.
29
+ */
30
+ protected collectBaseConfig(runner_config: RunnerConfig): Promise<configFile.PowerSyncConfig>;
31
+ protected collectSyncRules(baseConfig: configFile.PowerSyncConfig, runnerConfig: RunnerConfig): Promise<SyncRulesConfig>;
32
+ }