@powersync/service-core 0.0.0-dev-20260601140105 → 0.0.0-dev-20260803144812

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 (183) hide show
  1. package/CHANGELOG.md +83 -4
  2. package/dist/api/RouteAPI.d.ts +42 -14
  3. package/dist/api/diagnostics.d.ts +12 -1
  4. package/dist/api/diagnostics.js +34 -22
  5. package/dist/api/diagnostics.js.map +1 -1
  6. package/dist/entry/commands/compact-action.js +5 -1
  7. package/dist/entry/commands/compact-action.js.map +1 -1
  8. package/dist/modules/AbstractModule.d.ts +6 -2
  9. package/dist/modules/AbstractModule.js.map +1 -1
  10. package/dist/replication/AbstractReplicator.d.ts +26 -7
  11. package/dist/replication/AbstractReplicator.js +148 -68
  12. package/dist/replication/AbstractReplicator.js.map +1 -1
  13. package/dist/replication/ReplicationModule.d.ts +3 -3
  14. package/dist/replication/ReplicationModule.js.map +1 -1
  15. package/dist/routes/RouterEngine.d.ts +1 -0
  16. package/dist/routes/RouterEngine.js +6 -1
  17. package/dist/routes/RouterEngine.js.map +1 -1
  18. package/dist/routes/configure-fastify.d.ts +95 -56
  19. package/dist/routes/configure-fastify.js +15 -2
  20. package/dist/routes/configure-fastify.js.map +1 -1
  21. package/dist/routes/endpoints/admin.d.ts +24 -24
  22. package/dist/routes/endpoints/admin.js +46 -35
  23. package/dist/routes/endpoints/admin.js.map +1 -1
  24. package/dist/routes/endpoints/checkpointing.d.ts +62 -0
  25. package/dist/routes/endpoints/checkpointing.js +65 -7
  26. package/dist/routes/endpoints/checkpointing.js.map +1 -1
  27. package/dist/routes/endpoints/socket-route.js +3 -2
  28. package/dist/routes/endpoints/socket-route.js.map +1 -1
  29. package/dist/routes/endpoints/sync-rules.js +22 -13
  30. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  31. package/dist/routes/endpoints/sync-stream.js +1 -1
  32. package/dist/routes/endpoints/sync-stream.js.map +1 -1
  33. package/dist/runner/teardown.js +8 -8
  34. package/dist/runner/teardown.js.map +1 -1
  35. package/dist/storage/BucketStorageBatch.d.ts +63 -17
  36. package/dist/storage/BucketStorageBatch.js.map +1 -1
  37. package/dist/storage/BucketStorageFactory.d.ts +22 -27
  38. package/dist/storage/BucketStorageFactory.js +4 -18
  39. package/dist/storage/BucketStorageFactory.js.map +1 -1
  40. package/dist/storage/CheckpointChecksumInvalidatedError.d.ts +12 -0
  41. package/dist/storage/CheckpointChecksumInvalidatedError.js +17 -0
  42. package/dist/storage/CheckpointChecksumInvalidatedError.js.map +1 -0
  43. package/dist/storage/ChecksumCache.d.ts +11 -6
  44. package/dist/storage/ChecksumCache.js +5 -4
  45. package/dist/storage/ChecksumCache.js.map +1 -1
  46. package/dist/storage/ParsedSyncConfigSet.d.ts +23 -0
  47. package/dist/storage/ParsedSyncConfigSet.js +2 -0
  48. package/dist/storage/ParsedSyncConfigSet.js.map +1 -0
  49. package/dist/storage/PersistedReplicationStream.d.ts +32 -0
  50. package/dist/storage/PersistedReplicationStream.js +26 -0
  51. package/dist/storage/PersistedReplicationStream.js.map +1 -0
  52. package/dist/storage/PersistedSyncConfigContent.d.ts +63 -0
  53. package/dist/storage/PersistedSyncConfigContent.js +125 -0
  54. package/dist/storage/PersistedSyncConfigContent.js.map +1 -0
  55. package/dist/storage/PersistedSyncConfigStatus.d.ts +13 -0
  56. package/dist/storage/PersistedSyncConfigStatus.js +2 -0
  57. package/dist/storage/PersistedSyncConfigStatus.js.map +1 -0
  58. package/dist/storage/SourceEntity.d.ts +6 -0
  59. package/dist/storage/SourceTable.d.ts +21 -3
  60. package/dist/storage/SourceTable.js +26 -3
  61. package/dist/storage/SourceTable.js.map +1 -1
  62. package/dist/storage/SyncRulesBucketStorage.d.ts +119 -21
  63. package/dist/storage/SyncRulesBucketStorage.js +3 -0
  64. package/dist/storage/SyncRulesBucketStorage.js.map +1 -1
  65. package/dist/storage/WriteCheckpointAPI.d.ts +60 -3
  66. package/dist/storage/WriteCheckpointAPI.js +33 -0
  67. package/dist/storage/WriteCheckpointAPI.js.map +1 -1
  68. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +117 -0
  69. package/dist/storage/implementation/BucketDefinitionMapping.js +277 -0
  70. package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
  71. package/dist/storage/implementation/IncrementalReprocessingCompatibility.d.ts +6 -0
  72. package/dist/storage/implementation/IncrementalReprocessingCompatibility.js +31 -0
  73. package/dist/storage/implementation/IncrementalReprocessingCompatibility.js.map +1 -0
  74. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.d.ts +17 -0
  75. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js +62 -0
  76. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js.map +1 -0
  77. package/dist/storage/implementation/ReplicationStreamStorageIds.d.ts +15 -0
  78. package/dist/storage/implementation/ReplicationStreamStorageIds.js +20 -0
  79. package/dist/storage/implementation/ReplicationStreamStorageIds.js.map +1 -0
  80. package/dist/storage/storage-index.d.ts +9 -1
  81. package/dist/storage/storage-index.js +9 -1
  82. package/dist/storage/storage-index.js.map +1 -1
  83. package/dist/sync/BucketChecksumState.d.ts +12 -2
  84. package/dist/sync/BucketChecksumState.js +69 -21
  85. package/dist/sync/BucketChecksumState.js.map +1 -1
  86. package/dist/sync/RequestTracker.d.ts +7 -0
  87. package/dist/sync/RequestTracker.js +12 -0
  88. package/dist/sync/RequestTracker.js.map +1 -1
  89. package/dist/sync/sync.js +154 -41
  90. package/dist/sync/sync.js.map +1 -1
  91. package/dist/sync/util.d.ts +5 -0
  92. package/dist/sync/util.js +9 -0
  93. package/dist/sync/util.js.map +1 -1
  94. package/dist/system/ServiceContext.d.ts +2 -0
  95. package/dist/system/ServiceContext.js +2 -0
  96. package/dist/system/ServiceContext.js.map +1 -1
  97. package/dist/tracing/PerformanceTracer.d.ts +23 -1
  98. package/dist/tracing/PerformanceTracer.js +11 -0
  99. package/dist/tracing/PerformanceTracer.js.map +1 -1
  100. package/dist/util/checkpointing.d.ts +4 -8
  101. package/dist/util/checkpointing.js +1 -16
  102. package/dist/util/checkpointing.js.map +1 -1
  103. package/dist/util/config/compound-config-collector.js +18 -2
  104. package/dist/util/config/compound-config-collector.js.map +1 -1
  105. package/dist/util/config/defaults.d.ts +2 -0
  106. package/dist/util/config/defaults.js +2 -0
  107. package/dist/util/config/defaults.js.map +1 -1
  108. package/dist/util/config/types.d.ts +3 -1
  109. package/dist/util/lsn.d.ts +4 -0
  110. package/dist/util/lsn.js +10 -0
  111. package/dist/util/lsn.js.map +1 -1
  112. package/dist/util/util-index.d.ts +1 -0
  113. package/dist/util/util-index.js +1 -0
  114. package/dist/util/util-index.js.map +1 -1
  115. package/dist/util/utils.d.ts +1 -1
  116. package/dist/util/utils.js +1 -1
  117. package/dist/util/write-checkpoint-batcher.d.ts +19 -0
  118. package/dist/util/write-checkpoint-batcher.js +100 -0
  119. package/dist/util/write-checkpoint-batcher.js.map +1 -0
  120. package/package.json +6 -7
  121. package/src/api/RouteAPI.ts +43 -14
  122. package/src/api/diagnostics.ts +43 -26
  123. package/src/entry/commands/compact-action.ts +7 -1
  124. package/src/modules/AbstractModule.ts +6 -2
  125. package/src/replication/AbstractReplicator.ts +181 -72
  126. package/src/replication/ReplicationModule.ts +2 -3
  127. package/src/routes/RouterEngine.ts +7 -1
  128. package/src/routes/configure-fastify.ts +25 -2
  129. package/src/routes/endpoints/admin.ts +72 -50
  130. package/src/routes/endpoints/checkpointing.ts +79 -8
  131. package/src/routes/endpoints/socket-route.ts +3 -2
  132. package/src/routes/endpoints/sync-rules.ts +23 -13
  133. package/src/routes/endpoints/sync-stream.ts +1 -1
  134. package/src/runner/teardown.ts +8 -8
  135. package/src/storage/BucketStorageBatch.ts +64 -18
  136. package/src/storage/BucketStorageFactory.ts +30 -39
  137. package/src/storage/CheckpointChecksumInvalidatedError.ts +17 -0
  138. package/src/storage/ChecksumCache.ts +27 -14
  139. package/src/storage/ParsedSyncConfigSet.ts +25 -0
  140. package/src/storage/PersistedReplicationStream.ts +55 -0
  141. package/src/storage/PersistedSyncConfigContent.ts +192 -0
  142. package/src/storage/PersistedSyncConfigStatus.ts +14 -0
  143. package/src/storage/SourceEntity.ts +6 -0
  144. package/src/storage/SourceTable.ts +34 -3
  145. package/src/storage/SyncRulesBucketStorage.ts +138 -22
  146. package/src/storage/WriteCheckpointAPI.ts +108 -3
  147. package/src/storage/implementation/BucketDefinitionMapping.ts +436 -0
  148. package/src/storage/implementation/IncrementalReprocessingCompatibility.ts +41 -0
  149. package/src/storage/implementation/IncrementalReprocessingSyncConfigLog.ts +109 -0
  150. package/src/storage/implementation/ReplicationStreamStorageIds.ts +21 -0
  151. package/src/storage/storage-index.ts +10 -1
  152. package/src/sync/BucketChecksumState.ts +103 -21
  153. package/src/sync/RequestTracker.ts +20 -0
  154. package/src/sync/sync.ts +183 -48
  155. package/src/sync/util.ts +12 -0
  156. package/src/system/ServiceContext.ts +7 -0
  157. package/src/tracing/PerformanceTracer.ts +41 -1
  158. package/src/util/checkpointing.ts +4 -22
  159. package/src/util/config/compound-config-collector.ts +25 -1
  160. package/src/util/config/defaults.ts +2 -0
  161. package/src/util/config/types.ts +3 -1
  162. package/src/util/lsn.ts +9 -0
  163. package/src/util/util-index.ts +1 -0
  164. package/src/util/utils.ts +1 -1
  165. package/src/util/write-checkpoint-batcher.ts +142 -0
  166. package/test/src/AbstractReplicator.test.ts +115 -0
  167. package/test/src/checksum_cache.test.ts +22 -0
  168. package/test/src/config.test.ts +102 -0
  169. package/test/src/diagnostics.test.ts +62 -34
  170. package/test/src/module-loader.test.ts +33 -1
  171. package/test/src/routes/admin.test.ts +182 -18
  172. package/test/src/routes/checkpointing.test.ts +54 -0
  173. package/test/src/routes/mocks.ts +6 -2
  174. package/test/src/storage/SourceTable.test.ts +89 -0
  175. package/test/src/sync/BucketChecksumState.test.ts +98 -12
  176. package/test/src/sync/util.test.ts +13 -1
  177. package/test/src/util/checkpointing.test.ts +249 -0
  178. package/tsconfig.json +1 -4
  179. package/tsconfig.tsbuildinfo +1 -1
  180. package/dist/storage/PersistedSyncRulesContent.d.ts +0 -59
  181. package/dist/storage/PersistedSyncRulesContent.js +0 -100
  182. package/dist/storage/PersistedSyncRulesContent.js.map +0 -1
  183. package/src/storage/PersistedSyncRulesContent.ts +0 -174
@@ -0,0 +1,249 @@
1
+ import { createWriteCheckpoint, storage, WriteCheckpointBatcher } from '@/index.js';
2
+ import { describe, expect, test, vi } from 'vitest';
3
+
4
+ function deferred<T = void>() {
5
+ return Promise.withResolvers<T>();
6
+ }
7
+
8
+ async function waitForAsyncWork() {
9
+ await new Promise((resolve) => setTimeout(resolve, 0));
10
+ await new Promise((resolve) => setImmediate(resolve));
11
+ }
12
+
13
+ function createStorage(options?: { shouldAdvance?: (checkpoint: { user_id: string }) => boolean }) {
14
+ let nextCheckpoint = 1n;
15
+ const bucketStorage = {
16
+ createManagedWriteCheckpoints: vi.fn(async (checkpoints: { user_id: string }[]) => {
17
+ return {
18
+ writeCheckpoints: new Map(checkpoints.map((checkpoint) => [checkpoint.user_id, nextCheckpoint++])),
19
+ shouldAdvance: checkpoints.some((checkpoint) => options?.shouldAdvance?.(checkpoint) ?? true)
20
+ };
21
+ })
22
+ };
23
+
24
+ const storage = {
25
+ getActiveSyncConfig: vi.fn(async () => ({ storage: bucketStorage }))
26
+ };
27
+
28
+ return { bucketStorage, storage };
29
+ }
30
+
31
+ /**
32
+ * Mock RouteAPI.createReplicationHead. The callback persists the write-checkpoint
33
+ * mapping and returns { response, shouldAdvance }; the source marker (tracked by
34
+ * `advanceMarker`) is only forced when shouldAdvance is true. An optional gate per
35
+ * batch keeps the call in flight so concurrency can be exercised.
36
+ */
37
+ function createApi(options?: { gates?: PromiseWithResolvers<void>[] }) {
38
+ const advanceMarker = vi.fn(async (_head: string) => undefined);
39
+ const createReplicationHead = vi.fn(
40
+ async (callback: (head: string) => Promise<{ response: unknown; shouldAdvance: boolean }>) => {
41
+ const batch = createReplicationHead.mock.calls.length;
42
+ const head = `head-${batch}`;
43
+ const { response, shouldAdvance } = await callback(head);
44
+ if (shouldAdvance) {
45
+ await advanceMarker(head);
46
+ }
47
+ if (options?.gates) {
48
+ await options.gates[batch - 1].promise;
49
+ }
50
+ return response;
51
+ }
52
+ );
53
+ return { createReplicationHead, advanceMarker };
54
+ }
55
+
56
+ function createBatcher(api: any, storage: any) {
57
+ return new WriteCheckpointBatcher(
58
+ () => api,
59
+ () => storage
60
+ );
61
+ }
62
+
63
+ describe('write checkpoint batching', () => {
64
+ test('deduplicates managed checkpoints by greatest supplied request id per user', () => {
65
+ expect(
66
+ storage.uniqueManagedWriteCheckpoints([
67
+ { user_id: 'user-a', heads: { '1': 'generated' } },
68
+ { user_id: 'user-a', heads: { '1': 'stale' }, checkpoint_request_id: 41n },
69
+ { user_id: 'user-a', heads: { '1': 'winner' }, checkpoint_request_id: 43n },
70
+ { user_id: 'user-a', heads: { '1': 'lower' }, checkpoint_request_id: 42n },
71
+ { user_id: 'user-b', heads: { '1': 'generated-b' } }
72
+ ])
73
+ ).toEqual([
74
+ { user_id: 'user-a', heads: { '1': 'winner' }, checkpoint_request_id: 43n },
75
+ { user_id: 'user-b', heads: { '1': 'generated-b' } }
76
+ ]);
77
+ });
78
+
79
+ test('coalesces same-turn requests and dispatches queued requests as capacity becomes available', async () => {
80
+ const gates = [deferred(), deferred()];
81
+ const { bucketStorage, storage } = createStorage();
82
+ const api = createApi({ gates });
83
+ const batcher = createBatcher(api, storage);
84
+
85
+ const firstBatch = [
86
+ createWriteCheckpoint({
87
+ userId: 'user-a',
88
+ clientId: 'client-1',
89
+ batcher
90
+ }),
91
+ createWriteCheckpoint({
92
+ userId: 'user-b',
93
+ clientId: undefined,
94
+ batcher
95
+ })
96
+ ];
97
+ await waitForAsyncWork();
98
+
99
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(1);
100
+ expect(bucketStorage.createManagedWriteCheckpoints).toHaveBeenNthCalledWith(1, [
101
+ { user_id: 'user-a/client-1', heads: { '1': 'head-1' } },
102
+ { user_id: 'user-b', heads: { '1': 'head-1' } }
103
+ ]);
104
+
105
+ const secondBatch = [
106
+ createWriteCheckpoint({ userId: 'user-c', clientId: 'client-3', batcher }),
107
+ createWriteCheckpoint({ userId: 'user-d', clientId: undefined, batcher }),
108
+ createWriteCheckpoint({ userId: 'user-e', clientId: undefined, batcher })
109
+ ];
110
+
111
+ await waitForAsyncWork();
112
+
113
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(2);
114
+ expect(bucketStorage.createManagedWriteCheckpoints).toHaveBeenNthCalledWith(2, [
115
+ { user_id: 'user-c/client-3', heads: { '1': 'head-2' } },
116
+ { user_id: 'user-d', heads: { '1': 'head-2' } },
117
+ { user_id: 'user-e', heads: { '1': 'head-2' } }
118
+ ]);
119
+
120
+ gates[0].resolve();
121
+ gates[1].resolve();
122
+ await expect(Promise.all([...firstBatch, ...secondBatch])).resolves.toEqual([
123
+ { writeCheckpoint: '1', replicationHead: 'head-1' },
124
+ { writeCheckpoint: '2', replicationHead: 'head-1' },
125
+ { writeCheckpoint: '3', replicationHead: 'head-2' },
126
+ { writeCheckpoint: '4', replicationHead: 'head-2' },
127
+ { writeCheckpoint: '5', replicationHead: 'head-2' }
128
+ ]);
129
+ });
130
+
131
+ test('passes supplied checkpoint request ids into the storage batch', async () => {
132
+ const { bucketStorage, storage } = createStorage();
133
+ const api = createApi();
134
+ const batcher = createBatcher(api, storage);
135
+
136
+ await expect(
137
+ createWriteCheckpoint({
138
+ userId: 'user-a',
139
+ clientId: 'client-1',
140
+ checkpointRequestId: 42n,
141
+ batcher
142
+ })
143
+ ).resolves.toEqual({ writeCheckpoint: '1', replicationHead: 'head-1' });
144
+
145
+ expect(bucketStorage.createManagedWriteCheckpoints).toHaveBeenCalledWith([
146
+ {
147
+ user_id: 'user-a/client-1',
148
+ heads: { '1': 'head-1' },
149
+ checkpoint_request_id: 42n
150
+ }
151
+ ]);
152
+ });
153
+
154
+ test('does not advance the source marker when storage reports no checkpoint updates', async () => {
155
+ const { storage } = createStorage({ shouldAdvance: () => false });
156
+ const api = createApi();
157
+ const batcher = createBatcher(api, storage);
158
+
159
+ await expect(createWriteCheckpoint({ userId: 'user-a', clientId: undefined, batcher })).resolves.toEqual({
160
+ writeCheckpoint: '1',
161
+ replicationHead: 'head-1'
162
+ });
163
+
164
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(1);
165
+ expect(api.advanceMarker).not.toHaveBeenCalled();
166
+ });
167
+
168
+ test('advances the source marker when storage reports a checkpoint update', async () => {
169
+ const { storage } = createStorage();
170
+ const api = createApi();
171
+ const batcher = createBatcher(api, storage);
172
+
173
+ await createWriteCheckpoint({ userId: 'user-a', clientId: undefined, batcher });
174
+
175
+ expect(api.advanceMarker).toHaveBeenCalledTimes(1);
176
+ expect(api.advanceMarker).toHaveBeenCalledWith('head-1');
177
+ });
178
+
179
+ test('allows three executing batches and queues later requests until one completes', async () => {
180
+ const gates = [deferred(), deferred(), deferred(), deferred()];
181
+ const { storage } = createStorage();
182
+ const api = createApi({ gates });
183
+ const batcher = createBatcher(api, storage);
184
+
185
+ const first = createWriteCheckpoint({
186
+ userId: 'user-a',
187
+ clientId: undefined,
188
+ batcher
189
+ });
190
+ await waitForAsyncWork();
191
+
192
+ const second = createWriteCheckpoint({
193
+ userId: 'user-b',
194
+ clientId: undefined,
195
+ batcher
196
+ });
197
+ await waitForAsyncWork();
198
+
199
+ const third = createWriteCheckpoint({
200
+ userId: 'user-c',
201
+ clientId: undefined,
202
+ batcher
203
+ });
204
+ await waitForAsyncWork();
205
+
206
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(3);
207
+
208
+ const fourth = createWriteCheckpoint({
209
+ userId: 'user-d',
210
+ clientId: undefined,
211
+ batcher
212
+ });
213
+ await waitForAsyncWork();
214
+
215
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(3);
216
+
217
+ gates[0].resolve();
218
+ await first;
219
+ await waitForAsyncWork();
220
+
221
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(4);
222
+
223
+ gates[1].resolve();
224
+ gates[2].resolve();
225
+ gates[3].resolve();
226
+
227
+ await expect(Promise.all([second, third, fourth])).resolves.toEqual([
228
+ { writeCheckpoint: '2', replicationHead: 'head-2' },
229
+ { writeCheckpoint: '3', replicationHead: 'head-3' },
230
+ { writeCheckpoint: '4', replicationHead: 'head-4' }
231
+ ]);
232
+ });
233
+
234
+ test('passes batch errors through without retrying', async () => {
235
+ const error = new Error('source unavailable');
236
+ const { storage } = createStorage();
237
+ const api = {
238
+ createReplicationHead: vi.fn(async () => {
239
+ throw error;
240
+ }),
241
+ advanceMarker: vi.fn(async () => undefined)
242
+ };
243
+ const batcher = createBatcher(api, storage);
244
+
245
+ await expect(createWriteCheckpoint({ userId: 'user-a', clientId: undefined, batcher })).rejects.toBe(error);
246
+ expect(api.createReplicationHead).toHaveBeenCalledTimes(1);
247
+ expect(api.advanceMarker).not.toHaveBeenCalled();
248
+ });
249
+ });
package/tsconfig.json CHANGED
@@ -2,10 +2,7 @@
2
2
  "extends": "../../tsconfig.base.json",
3
3
  "compilerOptions": {
4
4
  "rootDir": "src",
5
- "outDir": "dist",
6
- "esModuleInterop": true,
7
- "skipLibCheck": true,
8
- "sourceMap": true
5
+ "outDir": "dist"
9
6
  },
10
7
  "include": ["src"],
11
8
  "references": [