@powersync/service-core 0.0.0-dev-20260602073133 → 0.0.0-dev-20260909133214

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 (230) hide show
  1. package/CHANGELOG.md +152 -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 +36 -22
  5. package/dist/api/diagnostics.js.map +1 -1
  6. package/dist/entry/commands/compact-action.js +30 -22
  7. package/dist/entry/commands/compact-action.js.map +1 -1
  8. package/dist/metrics/metrics-interfaces.d.ts +5 -1
  9. package/dist/metrics/metrics-interfaces.js.map +1 -1
  10. package/dist/metrics/open-telemetry/MetricAttributeFilteringExporter.d.ts +36 -0
  11. package/dist/metrics/open-telemetry/MetricAttributeFilteringExporter.js +70 -0
  12. package/dist/metrics/open-telemetry/MetricAttributeFilteringExporter.js.map +1 -0
  13. package/dist/metrics/open-telemetry/OpenTelemetryMetricsFactory.js +6 -1
  14. package/dist/metrics/open-telemetry/OpenTelemetryMetricsFactory.js.map +1 -1
  15. package/dist/metrics/open-telemetry/util.js +7 -3
  16. package/dist/metrics/open-telemetry/util.js.map +1 -1
  17. package/dist/modules/AbstractModule.d.ts +6 -2
  18. package/dist/modules/AbstractModule.js.map +1 -1
  19. package/dist/replication/AbstractReplicator.d.ts +31 -7
  20. package/dist/replication/AbstractReplicator.js +168 -74
  21. package/dist/replication/AbstractReplicator.js.map +1 -1
  22. package/dist/replication/ReplicationModule.d.ts +3 -3
  23. package/dist/replication/ReplicationModule.js.map +1 -1
  24. package/dist/routes/RouterEngine.d.ts +1 -0
  25. package/dist/routes/RouterEngine.js +6 -1
  26. package/dist/routes/RouterEngine.js.map +1 -1
  27. package/dist/routes/configure-fastify.d.ts +104 -56
  28. package/dist/routes/configure-fastify.js +15 -2
  29. package/dist/routes/configure-fastify.js.map +1 -1
  30. package/dist/routes/endpoints/admin.d.ts +42 -24
  31. package/dist/routes/endpoints/admin.js +49 -36
  32. package/dist/routes/endpoints/admin.js.map +1 -1
  33. package/dist/routes/endpoints/checkpointing.d.ts +62 -0
  34. package/dist/routes/endpoints/checkpointing.js +65 -7
  35. package/dist/routes/endpoints/checkpointing.js.map +1 -1
  36. package/dist/routes/endpoints/socket-route.js +3 -2
  37. package/dist/routes/endpoints/socket-route.js.map +1 -1
  38. package/dist/routes/endpoints/sync-rules.js +25 -14
  39. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  40. package/dist/routes/endpoints/sync-stream.js +1 -1
  41. package/dist/routes/endpoints/sync-stream.js.map +1 -1
  42. package/dist/runner/teardown.js +8 -8
  43. package/dist/runner/teardown.js.map +1 -1
  44. package/dist/storage/BucketStorageBatch.d.ts +64 -17
  45. package/dist/storage/BucketStorageBatch.js.map +1 -1
  46. package/dist/storage/BucketStorageFactory.d.ts +50 -31
  47. package/dist/storage/BucketStorageFactory.js +10 -20
  48. package/dist/storage/BucketStorageFactory.js.map +1 -1
  49. package/dist/storage/CheckpointChecksumInvalidatedError.d.ts +12 -0
  50. package/dist/storage/CheckpointChecksumInvalidatedError.js +17 -0
  51. package/dist/storage/CheckpointChecksumInvalidatedError.js.map +1 -0
  52. package/dist/storage/ChecksumCache.d.ts +11 -6
  53. package/dist/storage/ChecksumCache.js +5 -4
  54. package/dist/storage/ChecksumCache.js.map +1 -1
  55. package/dist/storage/ParsedSyncConfigSet.d.ts +23 -0
  56. package/dist/storage/ParsedSyncConfigSet.js +2 -0
  57. package/dist/storage/ParsedSyncConfigSet.js.map +1 -0
  58. package/dist/storage/PersistedReplicationStream.d.ts +32 -0
  59. package/dist/storage/PersistedReplicationStream.js +26 -0
  60. package/dist/storage/PersistedReplicationStream.js.map +1 -0
  61. package/dist/storage/PersistedSyncConfigContent.d.ts +65 -0
  62. package/dist/storage/PersistedSyncConfigContent.js +138 -0
  63. package/dist/storage/PersistedSyncConfigContent.js.map +1 -0
  64. package/dist/storage/PersistedSyncConfigStatus.d.ts +13 -0
  65. package/dist/storage/PersistedSyncConfigStatus.js +2 -0
  66. package/dist/storage/PersistedSyncConfigStatus.js.map +1 -0
  67. package/dist/storage/ReplicationEventPayload.d.ts +6 -1
  68. package/dist/storage/SourceEntity.d.ts +13 -2
  69. package/dist/storage/SourceTable.d.ts +53 -4
  70. package/dist/storage/SourceTable.js +62 -7
  71. package/dist/storage/SourceTable.js.map +1 -1
  72. package/dist/storage/SourceTableReconciler.d.ts +83 -0
  73. package/dist/storage/SourceTableReconciler.js +95 -0
  74. package/dist/storage/SourceTableReconciler.js.map +1 -0
  75. package/dist/storage/StorageVersionConfig.d.ts +4 -0
  76. package/dist/storage/StorageVersionConfig.js +10 -1
  77. package/dist/storage/StorageVersionConfig.js.map +1 -1
  78. package/dist/storage/SyncRulesBucketStorage.d.ts +162 -26
  79. package/dist/storage/SyncRulesBucketStorage.js +3 -0
  80. package/dist/storage/SyncRulesBucketStorage.js.map +1 -1
  81. package/dist/storage/WriteCheckpointAPI.d.ts +85 -6
  82. package/dist/storage/WriteCheckpointAPI.js +33 -0
  83. package/dist/storage/WriteCheckpointAPI.js.map +1 -1
  84. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +133 -0
  85. package/dist/storage/implementation/BucketDefinitionMapping.js +351 -0
  86. package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
  87. package/dist/storage/implementation/IncrementalReprocessingCompatibility.d.ts +6 -0
  88. package/dist/storage/implementation/IncrementalReprocessingCompatibility.js +31 -0
  89. package/dist/storage/implementation/IncrementalReprocessingCompatibility.js.map +1 -0
  90. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.d.ts +17 -0
  91. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js +70 -0
  92. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js.map +1 -0
  93. package/dist/storage/implementation/ReplicationStreamStorageIds.d.ts +15 -0
  94. package/dist/storage/implementation/ReplicationStreamStorageIds.js +20 -0
  95. package/dist/storage/implementation/ReplicationStreamStorageIds.js.map +1 -0
  96. package/dist/storage/storage-index.d.ts +10 -1
  97. package/dist/storage/storage-index.js +10 -1
  98. package/dist/storage/storage-index.js.map +1 -1
  99. package/dist/storage/storage-metrics.js +120 -4
  100. package/dist/storage/storage-metrics.js.map +1 -1
  101. package/dist/sync/BucketChecksumState.d.ts +12 -2
  102. package/dist/sync/BucketChecksumState.js +69 -21
  103. package/dist/sync/BucketChecksumState.js.map +1 -1
  104. package/dist/sync/RequestTracker.d.ts +7 -0
  105. package/dist/sync/RequestTracker.js +12 -0
  106. package/dist/sync/RequestTracker.js.map +1 -1
  107. package/dist/sync/sync.js +184 -43
  108. package/dist/sync/sync.js.map +1 -1
  109. package/dist/sync/util.d.ts +5 -0
  110. package/dist/sync/util.js +19 -1
  111. package/dist/sync/util.js.map +1 -1
  112. package/dist/system/ServiceContext.d.ts +2 -0
  113. package/dist/system/ServiceContext.js +2 -0
  114. package/dist/system/ServiceContext.js.map +1 -1
  115. package/dist/tracing/PerformanceTracer.d.ts +23 -1
  116. package/dist/tracing/PerformanceTracer.js +11 -0
  117. package/dist/tracing/PerformanceTracer.js.map +1 -1
  118. package/dist/util/checkpointing.d.ts +4 -8
  119. package/dist/util/checkpointing.js +1 -16
  120. package/dist/util/checkpointing.js.map +1 -1
  121. package/dist/util/config/compound-config-collector.js +23 -2
  122. package/dist/util/config/compound-config-collector.js.map +1 -1
  123. package/dist/util/config/defaults.d.ts +2 -0
  124. package/dist/util/config/defaults.js +2 -0
  125. package/dist/util/config/defaults.js.map +1 -1
  126. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js +1 -0
  127. package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js.map +1 -1
  128. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js +1 -0
  129. package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js.map +1 -1
  130. package/dist/util/config/sync-rules/sync-rules-provider.d.ts +2 -0
  131. package/dist/util/config/sync-rules/sync-rules-provider.js +3 -0
  132. package/dist/util/config/sync-rules/sync-rules-provider.js.map +1 -1
  133. package/dist/util/config/types.d.ts +4 -1
  134. package/dist/util/lsn.d.ts +4 -0
  135. package/dist/util/lsn.js +10 -0
  136. package/dist/util/lsn.js.map +1 -1
  137. package/dist/util/protocol-types.d.ts +4 -4
  138. package/dist/util/protocol-types.js +3 -2
  139. package/dist/util/protocol-types.js.map +1 -1
  140. package/dist/util/util-index.d.ts +1 -0
  141. package/dist/util/util-index.js +1 -0
  142. package/dist/util/util-index.js.map +1 -1
  143. package/dist/util/utils.d.ts +3 -2
  144. package/dist/util/utils.js +17 -2
  145. package/dist/util/utils.js.map +1 -1
  146. package/dist/util/write-checkpoint-batcher.d.ts +19 -0
  147. package/dist/util/write-checkpoint-batcher.js +100 -0
  148. package/dist/util/write-checkpoint-batcher.js.map +1 -0
  149. package/package.json +7 -7
  150. package/src/api/RouteAPI.ts +43 -14
  151. package/src/api/diagnostics.ts +45 -26
  152. package/src/entry/commands/compact-action.ts +37 -23
  153. package/src/metrics/metrics-interfaces.ts +6 -1
  154. package/src/metrics/open-telemetry/MetricAttributeFilteringExporter.ts +85 -0
  155. package/src/metrics/open-telemetry/OpenTelemetryMetricsFactory.ts +7 -2
  156. package/src/metrics/open-telemetry/util.ts +7 -3
  157. package/src/modules/AbstractModule.ts +6 -2
  158. package/src/replication/AbstractReplicator.ts +212 -78
  159. package/src/replication/ReplicationModule.ts +2 -3
  160. package/src/routes/RouterEngine.ts +7 -1
  161. package/src/routes/configure-fastify.ts +25 -2
  162. package/src/routes/endpoints/admin.ts +75 -51
  163. package/src/routes/endpoints/checkpointing.ts +79 -8
  164. package/src/routes/endpoints/socket-route.ts +3 -2
  165. package/src/routes/endpoints/sync-rules.ts +26 -14
  166. package/src/routes/endpoints/sync-stream.ts +1 -1
  167. package/src/runner/teardown.ts +8 -8
  168. package/src/storage/BucketStorageBatch.ts +65 -18
  169. package/src/storage/BucketStorageFactory.ts +73 -46
  170. package/src/storage/CheckpointChecksumInvalidatedError.ts +17 -0
  171. package/src/storage/ChecksumCache.ts +27 -14
  172. package/src/storage/ParsedSyncConfigSet.ts +25 -0
  173. package/src/storage/PersistedReplicationStream.ts +55 -0
  174. package/src/storage/PersistedSyncConfigContent.ts +207 -0
  175. package/src/storage/PersistedSyncConfigStatus.ts +14 -0
  176. package/src/storage/ReplicationEventPayload.ts +6 -1
  177. package/src/storage/SourceEntity.ts +12 -2
  178. package/src/storage/SourceTable.ts +96 -8
  179. package/src/storage/SourceTableReconciler.ts +188 -0
  180. package/src/storage/StorageVersionConfig.ts +11 -1
  181. package/src/storage/SyncRulesBucketStorage.ts +189 -27
  182. package/src/storage/WriteCheckpointAPI.ts +140 -6
  183. package/src/storage/implementation/BucketDefinitionMapping.ts +539 -0
  184. package/src/storage/implementation/IncrementalReprocessingCompatibility.ts +41 -0
  185. package/src/storage/implementation/IncrementalReprocessingSyncConfigLog.ts +126 -0
  186. package/src/storage/implementation/ReplicationStreamStorageIds.ts +21 -0
  187. package/src/storage/storage-index.ts +11 -1
  188. package/src/storage/storage-metrics.ts +154 -8
  189. package/src/sync/BucketChecksumState.ts +105 -21
  190. package/src/sync/RequestTracker.ts +20 -0
  191. package/src/sync/sync.ts +214 -50
  192. package/src/sync/util.ts +33 -8
  193. package/src/system/ServiceContext.ts +7 -0
  194. package/src/tracing/PerformanceTracer.ts +41 -1
  195. package/src/util/checkpointing.ts +4 -22
  196. package/src/util/config/compound-config-collector.ts +31 -1
  197. package/src/util/config/defaults.ts +2 -0
  198. package/src/util/config/sync-rules/impl/base64-sync-rules-collector.ts +1 -0
  199. package/src/util/config/sync-rules/impl/filesystem-sync-rules-collector.ts +1 -0
  200. package/src/util/config/sync-rules/sync-rules-provider.ts +5 -0
  201. package/src/util/config/types.ts +4 -1
  202. package/src/util/lsn.ts +9 -0
  203. package/src/util/protocol-types.ts +3 -2
  204. package/src/util/util-index.ts +1 -0
  205. package/src/util/utils.ts +16 -2
  206. package/src/util/write-checkpoint-batcher.ts +142 -0
  207. package/test/src/AbstractReplicator.test.ts +177 -0
  208. package/test/src/MetricAttributeFilteringExporter.test.ts +96 -0
  209. package/test/src/PersistedSyncConfigContent.test.ts +152 -0
  210. package/test/src/checksum_cache.test.ts +22 -0
  211. package/test/src/config.test.ts +142 -0
  212. package/test/src/diagnostics.test.ts +87 -34
  213. package/test/src/module-loader.test.ts +33 -1
  214. package/test/src/routes/admin.test.ts +188 -18
  215. package/test/src/routes/checkpointing.test.ts +54 -0
  216. package/test/src/routes/mocks.ts +6 -2
  217. package/test/src/routes/stream.test.ts +123 -2
  218. package/test/src/source-table-reconciler.test.ts +244 -0
  219. package/test/src/storage/SourceTable.test.ts +92 -0
  220. package/test/src/storage-metrics.test.ts +148 -0
  221. package/test/src/sync/BucketChecksumState.test.ts +98 -12
  222. package/test/src/sync/util.test.ts +28 -1
  223. package/test/src/util/checkpointing.test.ts +249 -0
  224. package/test/src/util/utils.test.ts +23 -0
  225. package/tsconfig.json +1 -4
  226. package/tsconfig.tsbuildinfo +1 -1
  227. package/dist/storage/PersistedSyncRulesContent.d.ts +0 -59
  228. package/dist/storage/PersistedSyncRulesContent.js +0 -100
  229. package/dist/storage/PersistedSyncRulesContent.js.map +0 -1
  230. 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
+ });
@@ -0,0 +1,23 @@
1
+ import { formatBytes } from '@/index.js';
2
+ import { describe, expect, test } from 'vitest';
3
+
4
+ describe('formatBytes', () => {
5
+ test('formats bytes', () => {
6
+ expect(formatBytes(500)).toBe('500B');
7
+ });
8
+
9
+ test('formats kilobytes', () => {
10
+ expect(formatBytes(1024)).toBe('1.0KB');
11
+ expect(formatBytes(1536)).toBe('1.5KB');
12
+ });
13
+
14
+ test('formats megabytes', () => {
15
+ expect(formatBytes(1024 * 1024)).toBe('1.0MB');
16
+ expect(formatBytes(1.5 * 1024 * 1024)).toBe('1.5MB');
17
+ });
18
+
19
+ test('formats gigabytes', () => {
20
+ expect(formatBytes(1024 * 1024 * 1024)).toBe('1.0GB');
21
+ expect(formatBytes(10.5 * 1024 * 1024 * 1024)).toBe('10.5GB');
22
+ });
23
+ });
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": [