@powersync/service-module-mongodb-storage 0.0.0-dev-20260803144812 → 0.0.0-dev-20260929095716

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 (213) hide show
  1. package/CHANGELOG.md +129 -7
  2. package/dist/storage/MongoBucketStorage.d.ts +42 -5
  3. package/dist/storage/MongoBucketStorage.js +404 -215
  4. package/dist/storage/MongoBucketStorage.js.map +1 -1
  5. package/dist/storage/implementation/MongoBucketBatch.d.ts +39 -14
  6. package/dist/storage/implementation/MongoBucketBatch.js +169 -98
  7. package/dist/storage/implementation/MongoBucketBatch.js.map +1 -1
  8. package/dist/storage/implementation/MongoCompactor.d.ts +21 -79
  9. package/dist/storage/implementation/MongoCompactor.js +27 -238
  10. package/dist/storage/implementation/MongoCompactor.js.map +1 -1
  11. package/dist/storage/implementation/MongoIdSequence.d.ts +10 -1
  12. package/dist/storage/implementation/MongoIdSequence.js +14 -1
  13. package/dist/storage/implementation/MongoIdSequence.js.map +1 -1
  14. package/dist/storage/implementation/MongoOpIdAllocator.d.ts +27 -0
  15. package/dist/storage/implementation/MongoOpIdAllocator.js +97 -0
  16. package/dist/storage/implementation/MongoOpIdAllocator.js.map +1 -0
  17. package/dist/storage/implementation/MongoParameterCompactor.d.ts +83 -10
  18. package/dist/storage/implementation/MongoParameterCompactor.js +309 -96
  19. package/dist/storage/implementation/MongoParameterCompactor.js.map +1 -1
  20. package/dist/storage/implementation/MongoParsedSyncConfigSet.d.ts +9 -1
  21. package/dist/storage/implementation/MongoParsedSyncConfigSet.js +35 -2
  22. package/dist/storage/implementation/MongoParsedSyncConfigSet.js.map +1 -1
  23. package/dist/storage/implementation/MongoPersistedReplicationStream.d.ts +2 -1
  24. package/dist/storage/implementation/MongoPersistedReplicationStream.js +7 -4
  25. package/dist/storage/implementation/MongoPersistedReplicationStream.js.map +1 -1
  26. package/dist/storage/implementation/MongoPersistedSyncConfigContent.d.ts +3 -3
  27. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js +10 -8
  28. package/dist/storage/implementation/MongoPersistedSyncConfigContent.js.map +1 -1
  29. package/dist/storage/implementation/MongoStorageProvider.js +8 -4
  30. package/dist/storage/implementation/MongoStorageProvider.js.map +1 -1
  31. package/dist/storage/implementation/MongoSyncBucketStorage.d.ts +32 -11
  32. package/dist/storage/implementation/MongoSyncBucketStorage.js +95 -31
  33. package/dist/storage/implementation/MongoSyncBucketStorage.js.map +1 -1
  34. package/dist/storage/implementation/MongoSyncRulesLock.d.ts +26 -2
  35. package/dist/storage/implementation/MongoSyncRulesLock.js +59 -10
  36. package/dist/storage/implementation/MongoSyncRulesLock.js.map +1 -1
  37. package/dist/storage/implementation/MongoWriteBatch.d.ts +49 -0
  38. package/dist/storage/implementation/MongoWriteBatch.js +149 -0
  39. package/dist/storage/implementation/MongoWriteBatch.js.map +1 -0
  40. package/dist/storage/implementation/MongoWriteCheckpointAPI.d.ts +9 -7
  41. package/dist/storage/implementation/MongoWriteCheckpointAPI.js +9 -30
  42. package/dist/storage/implementation/MongoWriteCheckpointAPI.js.map +1 -1
  43. package/dist/storage/implementation/SyncRuleStateUpdate.d.ts +11 -5
  44. package/dist/storage/implementation/SyncRuleStateUpdate.js +14 -6
  45. package/dist/storage/implementation/SyncRuleStateUpdate.js.map +1 -1
  46. package/dist/storage/implementation/common/MongoSyncBucketStorageCheckpoint.d.ts +22 -2
  47. package/dist/storage/implementation/common/PersistedBatch.d.ts +32 -4
  48. package/dist/storage/implementation/common/PersistedBatch.js +57 -14
  49. package/dist/storage/implementation/common/PersistedBatch.js.map +1 -1
  50. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.d.ts +3 -0
  51. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js +3 -0
  52. package/dist/storage/implementation/common/VersionedPowerSyncMongoBase.js.map +1 -1
  53. package/dist/storage/implementation/db.d.ts +9 -1
  54. package/dist/storage/implementation/db.js +28 -1
  55. package/dist/storage/implementation/db.js.map +1 -1
  56. package/dist/storage/implementation/models.d.ts +35 -0
  57. package/dist/storage/implementation/models.js.map +1 -1
  58. package/dist/storage/implementation/v1/MongoBucketBatchV1.d.ts +11 -0
  59. package/dist/storage/implementation/v1/MongoBucketBatchV1.js +227 -221
  60. package/dist/storage/implementation/v1/MongoBucketBatchV1.js.map +1 -1
  61. package/dist/storage/implementation/v1/MongoCompactorV1.d.ts +38 -6
  62. package/dist/storage/implementation/v1/MongoCompactorV1.js +247 -20
  63. package/dist/storage/implementation/v1/MongoCompactorV1.js.map +1 -1
  64. package/dist/storage/implementation/v1/MongoParameterCompactorV1.d.ts +16 -2
  65. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js +20 -9
  66. package/dist/storage/implementation/v1/MongoParameterCompactorV1.js.map +1 -1
  67. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.d.ts +18 -11
  68. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js +66 -17
  69. package/dist/storage/implementation/v1/MongoSyncBucketStorageV1.js.map +1 -1
  70. package/dist/storage/implementation/v1/PersistedBatchV1.d.ts +7 -5
  71. package/dist/storage/implementation/v1/PersistedBatchV1.js +22 -28
  72. package/dist/storage/implementation/v1/PersistedBatchV1.js.map +1 -1
  73. package/dist/storage/implementation/v1/models.js +1 -0
  74. package/dist/storage/implementation/v1/models.js.map +1 -1
  75. package/dist/storage/implementation/v3/CompactionLease.d.ts +50 -0
  76. package/dist/storage/implementation/v3/CompactionLease.js +131 -0
  77. package/dist/storage/implementation/v3/CompactionLease.js.map +1 -0
  78. package/dist/storage/implementation/v3/MongoBucketBatchV3.d.ts +14 -2
  79. package/dist/storage/implementation/v3/MongoBucketBatchV3.js +261 -194
  80. package/dist/storage/implementation/v3/MongoBucketBatchV3.js.map +1 -1
  81. package/dist/storage/implementation/v3/MongoCompactorV3.d.ts +83 -25
  82. package/dist/storage/implementation/v3/MongoCompactorV3.js +702 -159
  83. package/dist/storage/implementation/v3/MongoCompactorV3.js.map +1 -1
  84. package/dist/storage/implementation/v3/MongoParameterCompactorV3.d.ts +18 -0
  85. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js +23 -0
  86. package/dist/storage/implementation/v3/MongoParameterCompactorV3.js.map +1 -0
  87. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.d.ts +2 -15
  88. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js +60 -92
  89. package/dist/storage/implementation/v3/MongoStoppedSyncConfigCleanup.js.map +1 -1
  90. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.d.ts +19 -9
  91. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js +92 -32
  92. package/dist/storage/implementation/v3/MongoSyncBucketStorageV3.js.map +1 -1
  93. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.d.ts +26 -0
  94. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js +82 -0
  95. package/dist/storage/implementation/v3/MongoWriteCheckpointAPIV3.js.map +1 -0
  96. package/dist/storage/implementation/v3/PersistedBatchV3.d.ts +14 -8
  97. package/dist/storage/implementation/v3/PersistedBatchV3.js +77 -45
  98. package/dist/storage/implementation/v3/PersistedBatchV3.js.map +1 -1
  99. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.d.ts +12 -2
  100. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js +42 -3
  101. package/dist/storage/implementation/v3/VersionedPowerSyncMongoV3.js.map +1 -1
  102. package/dist/storage/implementation/v3/bucket-format.js +2 -0
  103. package/dist/storage/implementation/v3/bucket-format.js.map +1 -1
  104. package/dist/storage/implementation/v3/compact-utils.d.ts +108 -0
  105. package/dist/storage/implementation/v3/compact-utils.js +223 -0
  106. package/dist/storage/implementation/v3/compact-utils.js.map +1 -0
  107. package/dist/storage/implementation/v3/compaction-constants.d.ts +5 -0
  108. package/dist/storage/implementation/v3/compaction-constants.js +6 -0
  109. package/dist/storage/implementation/v3/compaction-constants.js.map +1 -0
  110. package/dist/storage/implementation/v3/models.d.ts +90 -6
  111. package/dist/storage/implementation/v3/models.js.map +1 -1
  112. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.d.ts +7 -8
  113. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js +16 -5
  114. package/dist/storage/implementation/v3/object-storage/BucketDataObjectStorage.js.map +1 -1
  115. package/dist/storage/implementation/v3/object-storage/ObjectStorage.d.ts +14 -11
  116. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.d.ts +6 -8
  117. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js +17 -11
  118. package/dist/storage/implementation/v3/object-storage/ObjectStorageLifecycle.js.map +1 -1
  119. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.d.ts +38 -0
  120. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js +164 -0
  121. package/dist/storage/implementation/v3/object-storage/ObjectStorageUsage.js.map +1 -0
  122. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.d.ts +37 -12
  123. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js +197 -25
  124. package/dist/storage/implementation/v3/object-storage/S3ObjectStorage.js.map +1 -1
  125. package/dist/storage/implementation/v3/source-table-utils.d.ts +15 -7
  126. package/dist/storage/implementation/v3/source-table-utils.js +49 -57
  127. package/dist/storage/implementation/v3/source-table-utils.js.map +1 -1
  128. package/dist/storage/storage-index.d.ts +2 -1
  129. package/dist/storage/storage-index.js +1 -0
  130. package/dist/storage/storage-index.js.map +1 -1
  131. package/dist/types/types.d.ts +28 -1
  132. package/dist/types/types.js +28 -7
  133. package/dist/types/types.js.map +1 -1
  134. package/dist/utils/test-utils.d.ts +4 -2
  135. package/dist/utils/test-utils.js +7 -4
  136. package/dist/utils/test-utils.js.map +1 -1
  137. package/dist/utils/util.js +3 -1
  138. package/dist/utils/util.js.map +1 -1
  139. package/package.json +10 -9
  140. package/src/storage/MongoBucketStorage.ts +545 -277
  141. package/src/storage/implementation/MongoBucketBatch.ts +258 -124
  142. package/src/storage/implementation/MongoCompactor.ts +37 -346
  143. package/src/storage/implementation/MongoIdSequence.ts +21 -1
  144. package/src/storage/implementation/MongoOpIdAllocator.ts +105 -0
  145. package/src/storage/implementation/MongoParameterCompactor.ts +388 -102
  146. package/src/storage/implementation/MongoParsedSyncConfigSet.ts +46 -1
  147. package/src/storage/implementation/MongoPersistedReplicationStream.ts +12 -4
  148. package/src/storage/implementation/MongoPersistedSyncConfigContent.ts +48 -29
  149. package/src/storage/implementation/MongoStorageProvider.ts +16 -4
  150. package/src/storage/implementation/MongoSyncBucketStorage.ts +133 -40
  151. package/src/storage/implementation/MongoSyncRulesLock.ts +78 -12
  152. package/src/storage/implementation/MongoWriteBatch.ts +172 -0
  153. package/src/storage/implementation/MongoWriteCheckpointAPI.ts +16 -46
  154. package/src/storage/implementation/SyncRuleStateUpdate.ts +13 -6
  155. package/src/storage/implementation/common/MongoSyncBucketStorageCheckpoint.ts +24 -2
  156. package/src/storage/implementation/common/PersistedBatch.ts +76 -20
  157. package/src/storage/implementation/common/VersionedPowerSyncMongoBase.ts +4 -0
  158. package/src/storage/implementation/db.ts +36 -2
  159. package/src/storage/implementation/models.ts +39 -0
  160. package/src/storage/implementation/v1/MongoBucketBatchV1.ts +272 -254
  161. package/src/storage/implementation/v1/MongoCompactorV1.ts +327 -40
  162. package/src/storage/implementation/v1/MongoParameterCompactorV1.ts +25 -9
  163. package/src/storage/implementation/v1/MongoSyncBucketStorageV1.ts +88 -28
  164. package/src/storage/implementation/v1/PersistedBatchV1.ts +25 -30
  165. package/src/storage/implementation/v1/models.ts +1 -0
  166. package/src/storage/implementation/v3/CompactionLease.ts +152 -0
  167. package/src/storage/implementation/v3/MongoBucketBatchV3.ts +339 -247
  168. package/src/storage/implementation/v3/MongoCompactorV3.ts +955 -225
  169. package/src/storage/implementation/v3/MongoParameterCompactorV3.ts +34 -0
  170. package/src/storage/implementation/v3/MongoStoppedSyncConfigCleanup.ts +100 -109
  171. package/src/storage/implementation/v3/MongoSyncBucketStorageV3.ts +112 -42
  172. package/src/storage/implementation/v3/MongoWriteCheckpointAPIV3.ts +111 -0
  173. package/src/storage/implementation/v3/PersistedBatchV3.ts +94 -51
  174. package/src/storage/implementation/v3/VersionedPowerSyncMongoV3.ts +55 -4
  175. package/src/storage/implementation/v3/bucket-format.ts +2 -0
  176. package/src/storage/implementation/v3/compact-utils.ts +329 -0
  177. package/src/storage/implementation/v3/compaction-constants.ts +5 -0
  178. package/src/storage/implementation/v3/models.ts +97 -4
  179. package/src/storage/implementation/v3/object-storage/BucketDataObjectStorage.ts +24 -9
  180. package/src/storage/implementation/v3/object-storage/ObjectStorage.ts +20 -5
  181. package/src/storage/implementation/v3/object-storage/ObjectStorageLifecycle.ts +26 -14
  182. package/src/storage/implementation/v3/object-storage/ObjectStorageUsage.ts +229 -0
  183. package/src/storage/implementation/v3/object-storage/S3ObjectStorage.ts +279 -29
  184. package/src/storage/implementation/v3/source-table-utils.ts +82 -76
  185. package/src/storage/storage-index.ts +2 -0
  186. package/src/types/types.ts +42 -7
  187. package/src/utils/test-utils.ts +9 -5
  188. package/src/utils/util.ts +3 -1
  189. package/test/src/__snapshots__/storage.test.ts.snap +25 -0
  190. package/test/src/__snapshots__/storage_sync.test.ts.snap +794 -205
  191. package/test/src/chunk_compaction_config.test.ts +38 -0
  192. package/test/src/cleanup-stopped-sync-configs.test.ts +59 -26
  193. package/test/src/compact-utils.test.ts +251 -0
  194. package/test/src/concurrent_writers.test.ts +599 -0
  195. package/test/src/connection_config.test.ts +159 -0
  196. package/test/src/helpers/MemoryObjectStorage.ts +14 -6
  197. package/test/src/helpers/concurrentWriter.mjs +29 -0
  198. package/test/src/helpers/s3TestFactory.ts +12 -12
  199. package/test/src/object_storage_usage.test.ts +268 -0
  200. package/test/src/parameter_compacting_v1.test.ts +120 -0
  201. package/test/src/parameter_compacting_v3.test.ts +192 -0
  202. package/test/src/parameter_compaction_fence.test.ts +276 -0
  203. package/test/src/storage.test.ts +280 -24
  204. package/test/src/storage_compacting.test.ts +1097 -253
  205. package/test/src/storage_object_storage_inline_threshold.test.ts +3 -3
  206. package/test/src/storage_s3_checksums.test.ts +6 -7
  207. package/test/src/storage_s3_compaction_lifecycle.test.ts +143 -17
  208. package/test/src/storage_s3_reading.test.ts +294 -6
  209. package/test/src/storage_s3_writing.test.ts +4 -3
  210. package/test/src/storage_sync.test.ts +412 -80
  211. package/test/src/storeCurrentData.test.ts +5 -5
  212. package/test/src/util.ts +1 -1
  213. package/tsconfig.tsbuildinfo +1 -1
@@ -1,13 +1,17 @@
1
1
  import { DeleteObjectsCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
2
2
  import { storage, updateSyncRulesFromYaml } from '@powersync/service-core';
3
- import { bucketRequest, test_utils } from '@powersync/service-core-tests';
3
+ import { bucketRequest, getTestStorage, test_utils } from '@powersync/service-core-tests';
4
4
  import * as bson from 'bson';
5
- import { describe, expect, test } from 'vitest';
5
+ import { describe, expect, test, vi } from 'vitest';
6
6
  import { MongoSyncBucketStorage } from '../../src/storage/implementation/createMongoSyncBucketStorage.js';
7
7
  import { VersionedPowerSyncMongoV3 } from '../../src/storage/implementation/v3/VersionedPowerSyncMongoV3.js';
8
8
  import { hydrateBucketDataDocuments } from '../../src/storage/implementation/v3/object-storage/BucketDataObjectStorage.js';
9
9
  import { ObjectStorageError } from '../../src/storage/implementation/v3/object-storage/ObjectStorage.js';
10
- import { S3ObjectStorage } from '../../src/storage/implementation/v3/object-storage/S3ObjectStorage.js';
10
+ import {
11
+ resolveTimeoutProfile,
12
+ S3ObjectStorage,
13
+ type S3TimeoutProfile
14
+ } from '../../src/storage/implementation/v3/object-storage/S3ObjectStorage.js';
11
15
  import { env } from './env.js';
12
16
  import { MemoryObjectStorage } from './helpers/MemoryObjectStorage.js';
13
17
  import { createMemoryS3TestStorageSuite, createS3TestStorageSuite } from './helpers/s3TestFactory.js';
@@ -62,6 +66,234 @@ describe('S3 object storage reads', () => {
62
66
  );
63
67
  });
64
68
 
69
+ test('derives request timeouts from the AWS defaults mode', () => {
70
+ // legacy - the mode used when nothing is configured - defines no timeouts of its own,
71
+ // and uses the same baseline as standard.
72
+ expect(resolveTimeoutProfile('legacy')).toEqual({
73
+ connectionTimeoutMs: 3_100,
74
+ requestTimeoutMs: 15_500,
75
+ operationTimeoutMs: 31_000,
76
+ queueTimeoutMs: 124_000
77
+ });
78
+ expect(resolveTimeoutProfile('standard')).toEqual(resolveTimeoutProfile('legacy'));
79
+ expect(resolveTimeoutProfile('cross-region')).toEqual(resolveTimeoutProfile('legacy'));
80
+ expect(resolveTimeoutProfile('in-region')).toEqual({
81
+ connectionTimeoutMs: 1_100,
82
+ requestTimeoutMs: 5_500,
83
+ operationTimeoutMs: 11_000,
84
+ queueTimeoutMs: 44_000
85
+ });
86
+ expect(resolveTimeoutProfile('mobile')).toEqual({
87
+ connectionTimeoutMs: 30_000,
88
+ requestTimeoutMs: 150_000,
89
+ operationTimeoutMs: 300_000,
90
+ queueTimeoutMs: 1_200_000
91
+ });
92
+ });
93
+
94
+ test('resolves explicit and environment defaults modes without waiting on configuration', async () => {
95
+ // The timeouts are resolved up front, so operations never wait on configuration. The client
96
+ // wraps the mode in a provider of its own, which resolves to the same value.
97
+ const modeOf = async (objectStorage: S3ObjectStorage) => {
98
+ const configured = objectStorage.client.config.defaultsMode;
99
+ return typeof configured === 'function' ? await configured() : configured;
100
+ };
101
+
102
+ const previousMode = process.env.AWS_DEFAULTS_MODE;
103
+ try {
104
+ process.env.AWS_DEFAULTS_MODE = 'cross-region';
105
+ await expect(modeOf(new S3ObjectStorage({ bucket: 'test', region: 'test' }))).resolves.toEqual('cross-region');
106
+
107
+ // The storage option takes precedence over the environment.
108
+ await expect(
109
+ modeOf(new S3ObjectStorage({ bucket: 'test', region: 'test', defaultsMode: 'in-region' }))
110
+ ).resolves.toEqual('in-region');
111
+
112
+ // 'auto' would query the EC2 instance metadata service, and requires a resolvable region.
113
+ await expect(modeOf(new S3ObjectStorage({ bucket: 'test', defaultsMode: 'auto' }))).resolves.toEqual('standard');
114
+
115
+ delete process.env.AWS_DEFAULTS_MODE;
116
+ await expect(modeOf(new S3ObjectStorage({ bucket: 'test', region: 'test' }))).resolves.toEqual('legacy');
117
+
118
+ // An invalid mode fails on startup, rather than on every operation.
119
+ expect(
120
+ () => new S3ObjectStorage({ bucket: 'test', region: 'test', defaultsMode: 'nonsense' as any })
121
+ ).toThrowError(/Invalid AWS defaults mode "nonsense"/);
122
+ } finally {
123
+ if (previousMode == null) {
124
+ delete process.env.AWS_DEFAULTS_MODE;
125
+ } else {
126
+ process.env.AWS_DEFAULTS_MODE = previousMode;
127
+ }
128
+ }
129
+ });
130
+
131
+ test('cancels active uploads and deletes', async () => {
132
+ const operations = [
133
+ (objectStorage: S3ObjectStorage, signal: AbortSignal) =>
134
+ objectStorage.put(
135
+ 'object',
136
+ new Uint8Array(),
137
+ { contentType: 'application/bson', contentEncoding: null },
138
+ { signal }
139
+ ),
140
+ (objectStorage: S3ObjectStorage, signal: AbortSignal) => objectStorage.delete(['object'], { signal })
141
+ ];
142
+
143
+ for (const startOperation of operations) {
144
+ const objectStorage = new S3ObjectStorage({ bucket: 'test', region: 'test' });
145
+ const controller = new AbortController();
146
+ let requestStarted!: () => void;
147
+ const started = new Promise<void>((resolve) => (requestStarted = resolve));
148
+ objectStorage.client.send = async (_command: any, options: any) => {
149
+ requestStarted();
150
+ await new Promise<void>((_resolve, reject) => {
151
+ const abort = () => reject(options.abortSignal.reason);
152
+ if (options.abortSignal.aborted) {
153
+ abort();
154
+ } else {
155
+ options.abortSignal.addEventListener('abort', abort, { once: true });
156
+ }
157
+ });
158
+ throw new Error('unreachable');
159
+ };
160
+
161
+ const running = startOperation(objectStorage, controller.signal);
162
+ const expectation = expect(running).rejects.toMatchObject({ name: 'AbortError' });
163
+ await started;
164
+ controller.abort();
165
+ await expectation;
166
+ }
167
+ });
168
+
169
+ test('reports why a streaming operation was cancelled', async () => {
170
+ // Aborting mid-stream destroys the socket, and the stream fails with a generic 'aborted'
171
+ // error. The operation reports the actual cause instead.
172
+ const stalledDownload = (objectStorage: S3ObjectStorage) => {
173
+ objectStorage.client.send = async (_command: any, options: any) => ({
174
+ ContentLength: 10,
175
+ ContentType: 'application/bson',
176
+ Body: (async function* () {
177
+ yield new Uint8Array([1, 2, 3]);
178
+ await new Promise<void>((_resolve, reject) => {
179
+ const abort = () => reject(new Error('stream aborted'));
180
+ if (options.abortSignal.aborted) {
181
+ abort();
182
+ } else {
183
+ options.abortSignal.addEventListener('abort', abort, { once: true });
184
+ }
185
+ });
186
+ })()
187
+ });
188
+ };
189
+
190
+ const timingOut = new S3ObjectStorage({ bucket: 'test', region: 'test' });
191
+ (timingOut as any).timeouts = {
192
+ ...resolveTimeoutProfile('standard'),
193
+ operationTimeoutMs: 50
194
+ } satisfies S3TimeoutProfile;
195
+ stalledDownload(timingOut);
196
+ await expect(timingOut.get('stalled-object')).rejects.toMatchObject({
197
+ name: 'ObjectStorageError',
198
+ retryable: true,
199
+ message: expect.stringContaining('did not complete within 50 ms')
200
+ } satisfies Partial<ObjectStorageError>);
201
+
202
+ // A caller abort is a cancellation, not a retryable failure.
203
+ const cancelled = new S3ObjectStorage({ bucket: 'test', region: 'test' });
204
+ stalledDownload(cancelled);
205
+ const controller = new AbortController();
206
+ const reading = cancelled.get('stalled-object', { signal: controller.signal });
207
+ const expectation = expect(reading).rejects.toMatchObject({ name: 'AbortError' });
208
+ controller.abort();
209
+ await expectation;
210
+ });
211
+
212
+ test('reports the caller abort reason unchanged', async () => {
213
+ // Callers abort with the error that stopped them, and upstream error handling matches on that
214
+ // type. Replacing it with an ObjectStorageError would hide it - for MongoDB replication, that
215
+ // loses the invalidation error that triggers a replication restart.
216
+ class StreamInvalidatedError extends Error {}
217
+
218
+ const objectStorage = new S3ObjectStorage({ bucket: 'test', region: 'test' });
219
+ const reason = new StreamInvalidatedError('stream invalidated');
220
+ const controller = new AbortController();
221
+ objectStorage.client.send = async (_command: any, options: any) => {
222
+ controller.abort(reason);
223
+ throw options.abortSignal.reason;
224
+ };
225
+
226
+ await expect(objectStorage.get('object', { signal: controller.signal })).rejects.toBe(reason);
227
+ await expect(
228
+ objectStorage.put(
229
+ 'object',
230
+ new Uint8Array(),
231
+ { contentType: 'application/bson', contentEncoding: null },
232
+ { signal: controller.signal }
233
+ )
234
+ ).rejects.toBe(reason);
235
+ await expect(objectStorage.delete(['object'], { signal: controller.signal })).rejects.toBe(reason);
236
+ });
237
+
238
+ test('times out waiting for an operation slot', async () => {
239
+ const objectStorage = new S3ObjectStorage({ bucket: 'test', region: 'test', concurrencyLimit: 1 });
240
+ (objectStorage as any).timeouts = {
241
+ connectionTimeoutMs: 10,
242
+ requestTimeoutMs: 20,
243
+ operationTimeoutMs: 10_000,
244
+ queueTimeoutMs: 50
245
+ } satisfies S3TimeoutProfile;
246
+ let releaseBlocker!: () => void;
247
+ const blocked = new Promise<void>((resolve) => (releaseBlocker = resolve));
248
+ objectStorage.client.send = async () => {
249
+ await blocked;
250
+ return { ContentLength: 0, Body: (async function* () {})(), ContentType: 'application/bson' };
251
+ };
252
+
253
+ // The single slot is held by the first upload, which has no signal of its own.
254
+ const holding = objectStorage.put('held-object', new Uint8Array(), {
255
+ contentType: 'application/bson',
256
+ contentEncoding: null
257
+ });
258
+ await expect(objectStorage.get('queued-object')).rejects.toMatchObject({
259
+ name: 'ObjectStorageError',
260
+ retryable: true,
261
+ message: expect.stringContaining('waiting for a concurrency slot')
262
+ } satisfies Partial<ObjectStorageError>);
263
+
264
+ releaseBlocker();
265
+ await holding;
266
+ // The slot is usable again once the blocking operation completes.
267
+ await expect(objectStorage.get('later-object')).resolves.toMatchObject({ data: new Uint8Array(0) });
268
+ });
269
+
270
+ test('reports a caller abort while queued as a cancellation', async () => {
271
+ const objectStorage = new S3ObjectStorage({ bucket: 'test', region: 'test', concurrencyLimit: 1 });
272
+ const controller = new AbortController();
273
+ let holderStarted!: () => void;
274
+ const started = new Promise<void>((resolve) => (holderStarted = resolve));
275
+ let releaseHolder!: () => void;
276
+ const blocked = new Promise<void>((resolve) => (releaseHolder = resolve));
277
+ objectStorage.client.send = async () => {
278
+ holderStarted();
279
+ await blocked;
280
+ return {};
281
+ };
282
+
283
+ const holding = objectStorage.put('held-object', new Uint8Array(), {
284
+ contentType: 'application/bson',
285
+ contentEncoding: null
286
+ });
287
+ await started;
288
+
289
+ const queuedRead = objectStorage.get('queued-object', { signal: controller.signal });
290
+ const expectation = expect(queuedRead).rejects.toMatchObject({ name: 'AbortError' });
291
+ controller.abort();
292
+ await expectation;
293
+ releaseHolder();
294
+ await holding;
295
+ });
296
+
65
297
  test('rejects complete S3 keys over the safe byte limit', async () => {
66
298
  const objectStorage = new S3ObjectStorage({
67
299
  bucket: 'test',
@@ -125,6 +357,16 @@ describe('S3 object storage reads', () => {
125
357
  message: expect.stringContaining('S3 download failed for transient-object')
126
358
  } satisfies Partial<ObjectStorageError>);
127
359
 
360
+ const timeoutCause = new DOMException('The operation was aborted due to timeout', 'TimeoutError');
361
+ objectStorage.client.send = async () => {
362
+ throw timeoutCause;
363
+ };
364
+ await expect(objectStorage.get('timed-out-object')).rejects.toMatchObject({
365
+ name: 'ObjectStorageError',
366
+ retryable: true,
367
+ cause: timeoutCause
368
+ } satisfies Partial<ObjectStorageError>);
369
+
128
370
  const permanentCause = Object.assign(new Error('access denied'), {
129
371
  name: 'AccessDenied',
130
372
  $metadata: { httpStatusCode: 403 }
@@ -334,6 +576,52 @@ describe('S3 object storage reads', () => {
334
576
  expect(maxActiveOperations).toBe(4);
335
577
  });
336
578
 
579
+ test('drains sibling downloads before reporting a hydration failure', async () => {
580
+ const objectStorage = new MemoryObjectStorage();
581
+ const gate = Promise.withResolvers<void>();
582
+ const failure = new Error('download failed');
583
+ const documents = ['failed', 'pending'].map((path, index) => ({
584
+ _id: { b: 'bucket', o: BigInt(index + 1) },
585
+ min_op: BigInt(index + 1),
586
+ checksum: 0n,
587
+ count: 0,
588
+ size: 1,
589
+ storage_ref: { path, file_size: 1 }
590
+ }));
591
+ const get = vi.spyOn(objectStorage, 'get').mockImplementation(async (path) => {
592
+ if (path === 'failed') {
593
+ throw failure;
594
+ }
595
+ await gate.promise;
596
+ return {
597
+ data: bson.serialize({ ops: [] }),
598
+ metadata: { contentType: 'application/bson', contentEncoding: null }
599
+ };
600
+ });
601
+ let settled = false;
602
+ const result = hydrateBucketDataDocuments(documents, objectStorage, {}).then(
603
+ () => {
604
+ settled = true;
605
+ return undefined;
606
+ },
607
+ (error) => {
608
+ settled = true;
609
+ return error;
610
+ }
611
+ );
612
+ try {
613
+ await vi.waitFor(() => expect(get).toHaveBeenCalledTimes(2));
614
+ await new Promise<void>((resolve) => setImmediate(resolve));
615
+ expect(settled).toBe(false);
616
+ } finally {
617
+ gate.resolve();
618
+ await result;
619
+ get.mockRestore();
620
+ }
621
+ expect(await result).toBe(failure);
622
+ expect(documents[1]).toHaveProperty('ops', []);
623
+ });
624
+
337
625
  test('aborts active object downloads', async () => {
338
626
  const objectStorage = new MemoryObjectStorage();
339
627
  const controller = new AbortController();
@@ -374,7 +662,7 @@ describe('S3 object storage reads', () => {
374
662
  const { memoryStorage, factory: factoryGen } = memoryS3Factory();
375
663
  await using factory = await factoryGen.factory();
376
664
  const syncRules = await factory.updateSyncRules(updateSyncRulesFromYaml(SYNC_RULES_YAML, { storageVersion: 3 }));
377
- const bucketStorage = factory.getInstance(syncRules) as MongoSyncBucketStorage;
665
+ const bucketStorage = (await getTestStorage(factory, syncRules)) as MongoSyncBucketStorage;
378
666
 
379
667
  await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
380
668
  const sourceTable = await test_utils.resolveTestTable(writer, 'items', ['id'], factoryGen, 1);
@@ -422,7 +710,7 @@ describe('S3 object storage reads', () => {
422
710
  const { factory: factoryGen } = s3Factory();
423
711
  await using factory = await factoryGen.factory();
424
712
  const syncRules = await factory.updateSyncRules(updateSyncRulesFromYaml(SYNC_RULES_YAML, { storageVersion: 3 }));
425
- const bucketStorage = factory.getInstance(syncRules) as MongoSyncBucketStorage;
713
+ const bucketStorage = (await getTestStorage(factory, syncRules)) as MongoSyncBucketStorage;
426
714
 
427
715
  await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
428
716
  const sourceTable = await test_utils.resolveTestTable(writer, 'items', ['id'], factoryGen, 1);
@@ -475,7 +763,7 @@ describe('S3 object storage reads', () => {
475
763
  const { memoryStorage, factory: factoryGen } = memoryS3Factory({ inlineThresholdBytes: 1_000 });
476
764
  await using factory = await factoryGen.factory();
477
765
  const syncRules = await factory.updateSyncRules(updateSyncRulesFromYaml(SYNC_RULES_YAML, { storageVersion: 3 }));
478
- const bucketStorage = factory.getInstance(syncRules) as MongoSyncBucketStorage;
766
+ const bucketStorage = (await getTestStorage(factory, syncRules)) as MongoSyncBucketStorage;
479
767
 
480
768
  await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
481
769
  const sourceTable = await test_utils.resolveTestTable(writer, 'items', ['id'], factoryGen, 1);
@@ -1,5 +1,5 @@
1
1
  import { storage, updateSyncRulesFromYaml } from '@powersync/service-core';
2
- import { test_utils } from '@powersync/service-core-tests';
2
+ import { getTestStorage, test_utils } from '@powersync/service-core-tests';
3
3
  import * as bson from 'bson';
4
4
  import { describe, expect, test } from 'vitest';
5
5
  import { MongoSyncBucketStorage } from '../../src/storage/implementation/createMongoSyncBucketStorage.js';
@@ -28,7 +28,7 @@ describe('S3 object storage writes', () => {
28
28
  const { memoryStorage, factory: factoryGen } = memoryS3Factory();
29
29
  await using factory = await factoryGen.factory();
30
30
  const syncRules = await factory.updateSyncRules(updateSyncRulesFromYaml(SYNC_RULES_YAML, { storageVersion: 3 }));
31
- const bucketStorage = factory.getInstance(syncRules) as MongoSyncBucketStorage;
31
+ const bucketStorage = (await getTestStorage(factory, syncRules)) as MongoSyncBucketStorage;
32
32
 
33
33
  await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
34
34
  const sourceTable = await test_utils.resolveTestTable(writer, 'items', ['id'], factoryGen, 1);
@@ -65,6 +65,7 @@ describe('S3 object storage writes', () => {
65
65
  { op: 'PUT', row_id: 'item2' }
66
66
  ]);
67
67
  expect(ops.every((op) => op.data != null)).toBe(true);
68
+ expect(ops.every((op) => typeof op.subkey == 'string')).toBe(true);
68
69
 
69
70
  // Verify the corresponding MongoDB metadata shell.
70
71
  const db = bucketStorage.db as VersionedPowerSyncMongoV3;
@@ -94,7 +95,7 @@ describe('S3 object storage writes', () => {
94
95
  const { memoryStorage, factory: factoryGen } = memoryS3Factory();
95
96
  await using factory = await factoryGen.factory();
96
97
  const syncRules = await factory.updateSyncRules(updateSyncRulesFromYaml(SYNC_RULES_YAML, { storageVersion: 3 }));
97
- const bucketStorage = factory.getInstance(syncRules) as MongoSyncBucketStorage;
98
+ const bucketStorage = (await getTestStorage(factory, syncRules)) as MongoSyncBucketStorage;
98
99
 
99
100
  await using writer = await bucketStorage.createWriter(test_utils.BATCH_OPTIONS);
100
101
  const sourceTable = await test_utils.resolveTestTable(writer, 'items', ['id'], factoryGen, 4);