@powersync/service-core 0.18.1 → 1.7.1
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.
- package/CHANGELOG.md +29 -0
- package/dist/api/RouteAPI.d.ts +1 -1
- package/dist/api/diagnostics.js +107 -169
- package/dist/api/diagnostics.js.map +1 -1
- package/dist/entry/commands/compact-action.js +10 -73
- package/dist/entry/commands/compact-action.js.map +1 -1
- package/dist/modules/AbstractModule.d.ts +1 -1
- package/dist/replication/AbstractReplicator.js +8 -76
- package/dist/replication/AbstractReplicator.js.map +1 -1
- package/dist/routes/endpoints/checkpointing.js +3 -2
- package/dist/routes/endpoints/checkpointing.js.map +1 -1
- package/dist/routes/endpoints/socket-route.js +5 -5
- package/dist/routes/endpoints/socket-route.js.map +1 -1
- package/dist/routes/endpoints/sync-stream.js +5 -5
- package/dist/routes/endpoints/sync-stream.js.map +1 -1
- package/dist/runner/teardown.js +3 -65
- package/dist/runner/teardown.js.map +1 -1
- package/dist/storage/BucketStorage.d.ts +8 -441
- package/dist/storage/BucketStorage.js +9 -10
- package/dist/storage/BucketStorage.js.map +1 -1
- package/dist/storage/BucketStorageBatch.d.ts +130 -0
- package/dist/storage/BucketStorageBatch.js +10 -0
- package/dist/storage/BucketStorageBatch.js.map +1 -0
- package/dist/storage/BucketStorageFactory.d.ts +145 -0
- package/dist/storage/BucketStorageFactory.js +2 -0
- package/dist/storage/BucketStorageFactory.js.map +1 -0
- package/dist/storage/ChecksumCache.js.map +1 -1
- package/dist/storage/PersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/PersistedSyncRulesContent.js +2 -0
- package/dist/storage/PersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/ReplicationEventPayload.d.ts +1 -1
- package/dist/storage/ReplicationLock.d.ts +4 -0
- package/dist/storage/ReplicationLock.js +2 -0
- package/dist/storage/ReplicationLock.js.map +1 -0
- package/dist/storage/SourceEntity.d.ts +6 -2
- package/dist/storage/SourceTable.d.ts +2 -2
- package/dist/storage/SourceTable.js.map +1 -1
- package/dist/storage/StorageEngine.d.ts +4 -4
- package/dist/storage/StorageEngine.js +2 -2
- package/dist/storage/StorageEngine.js.map +1 -1
- package/dist/storage/StorageProvider.d.ts +4 -1
- package/dist/storage/SyncRulesBucketStorage.d.ts +207 -0
- package/dist/storage/SyncRulesBucketStorage.js +7 -0
- package/dist/storage/SyncRulesBucketStorage.js.map +1 -0
- package/dist/storage/bson.d.ts +14 -3
- package/dist/storage/bson.js +18 -2
- package/dist/storage/bson.js.map +1 -1
- package/dist/storage/storage-index.d.ts +5 -0
- package/dist/storage/storage-index.js +5 -0
- package/dist/storage/storage-index.js.map +1 -1
- package/dist/sync/BucketChecksumState.d.ts +91 -0
- package/dist/sync/BucketChecksumState.js +313 -0
- package/dist/sync/BucketChecksumState.js.map +1 -0
- package/dist/sync/sync-index.d.ts +1 -0
- package/dist/sync/sync-index.js +1 -0
- package/dist/sync/sync-index.js.map +1 -1
- package/dist/sync/sync.d.ts +7 -3
- package/dist/sync/sync.js +139 -135
- package/dist/sync/sync.js.map +1 -1
- package/dist/sync/util.d.ts +9 -0
- package/dist/sync/util.js +44 -0
- package/dist/sync/util.js.map +1 -1
- package/dist/util/checkpointing.d.ts +1 -1
- package/dist/util/checkpointing.js +15 -78
- package/dist/util/checkpointing.js.map +1 -1
- package/dist/util/protocol-types.d.ts +13 -4
- package/package.json +5 -5
- package/src/api/RouteAPI.ts +1 -1
- package/src/api/diagnostics.ts +1 -1
- package/src/entry/commands/compact-action.ts +2 -3
- package/src/modules/AbstractModule.ts +1 -1
- package/src/replication/AbstractReplicator.ts +7 -12
- package/src/routes/endpoints/checkpointing.ts +3 -3
- package/src/routes/endpoints/socket-route.ts +7 -5
- package/src/routes/endpoints/sync-stream.ts +8 -5
- package/src/runner/teardown.ts +1 -1
- package/src/storage/BucketStorage.ts +8 -550
- package/src/storage/BucketStorageBatch.ts +158 -0
- package/src/storage/BucketStorageFactory.ts +166 -0
- package/src/storage/ChecksumCache.ts +1 -0
- package/src/storage/PersistedSyncRulesContent.ts +26 -0
- package/src/storage/ReplicationEventPayload.ts +1 -1
- package/src/storage/ReplicationLock.ts +5 -0
- package/src/storage/SourceEntity.ts +6 -2
- package/src/storage/SourceTable.ts +1 -1
- package/src/storage/StorageEngine.ts +4 -4
- package/src/storage/StorageProvider.ts +4 -1
- package/src/storage/SyncRulesBucketStorage.ts +265 -0
- package/src/storage/bson.ts +22 -4
- package/src/storage/storage-index.ts +5 -0
- package/src/sync/BucketChecksumState.ts +392 -0
- package/src/sync/sync-index.ts +1 -0
- package/src/sync/sync.ts +182 -157
- package/src/sync/util.ts +54 -0
- package/src/util/checkpointing.ts +4 -6
- package/src/util/protocol-types.ts +16 -4
- package/test/src/auth.test.ts +5 -5
- package/test/src/sync/BucketChecksumState.test.ts +565 -0
- package/test/src/sync/util.test.ts +34 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { ObserverClient } from '@powersync/lib-services-framework';
|
|
2
|
+
import { SqlSyncRules, SqliteJsonRow, SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
3
|
+
import * as util from '../util/util-index.js';
|
|
4
|
+
import { BucketStorageBatch, FlushedResult } from './BucketStorageBatch.js';
|
|
5
|
+
import { BucketStorageFactory } from './BucketStorageFactory.js';
|
|
6
|
+
import { ParseSyncRulesOptions } from './PersistedSyncRulesContent.js';
|
|
7
|
+
import { SourceEntityDescriptor } from './SourceEntity.js';
|
|
8
|
+
import { SourceTable } from './SourceTable.js';
|
|
9
|
+
import { SyncStorageWriteCheckpointAPI } from './WriteCheckpointAPI.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Storage for a specific copy of sync rules.
|
|
13
|
+
*/
|
|
14
|
+
export interface SyncRulesBucketStorage
|
|
15
|
+
extends ObserverClient<SyncRulesBucketStorageListener>,
|
|
16
|
+
SyncStorageWriteCheckpointAPI {
|
|
17
|
+
readonly group_id: number;
|
|
18
|
+
readonly slot_name: string;
|
|
19
|
+
|
|
20
|
+
readonly factory: BucketStorageFactory;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolve a table, keeping track of it internally.
|
|
24
|
+
*/
|
|
25
|
+
resolveTable(options: ResolveTableOptions): Promise<ResolveTableResult>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Use this to get access to update storage data.
|
|
29
|
+
*/
|
|
30
|
+
startBatch(
|
|
31
|
+
options: StartBatchOptions,
|
|
32
|
+
callback: (batch: BucketStorageBatch) => Promise<void>
|
|
33
|
+
): Promise<FlushedResult | null>;
|
|
34
|
+
|
|
35
|
+
getParsedSyncRules(options: ParseSyncRulesOptions): SqlSyncRules;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Terminate the sync rules.
|
|
39
|
+
*
|
|
40
|
+
* This clears the storage, and sets state to TERMINATED.
|
|
41
|
+
*
|
|
42
|
+
* Must only be called on stopped sync rules.
|
|
43
|
+
*/
|
|
44
|
+
terminate(options?: TerminateOptions): Promise<void>;
|
|
45
|
+
|
|
46
|
+
getStatus(): Promise<SyncRuleStatus>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Clear the storage, without changing state.
|
|
50
|
+
*/
|
|
51
|
+
clear(): Promise<void>;
|
|
52
|
+
|
|
53
|
+
autoActivate(): Promise<void>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Record a replication error.
|
|
57
|
+
*
|
|
58
|
+
* This could be a recoverable error (e.g. temporary network failure),
|
|
59
|
+
* or a permanent error (e.g. missing toast data).
|
|
60
|
+
*
|
|
61
|
+
* Errors are cleared on commit.
|
|
62
|
+
*/
|
|
63
|
+
reportError(e: any): Promise<void>;
|
|
64
|
+
|
|
65
|
+
compact(options?: CompactOptions): Promise<void>;
|
|
66
|
+
|
|
67
|
+
// ## Read operations
|
|
68
|
+
|
|
69
|
+
getCheckpoint(): Promise<ReplicationCheckpoint>;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Used to resolve "dynamic" parameter queries.
|
|
73
|
+
*/
|
|
74
|
+
getParameterSets(checkpoint: util.OpId, lookups: SqliteJsonValue[][]): Promise<SqliteJsonRow[]>;
|
|
75
|
+
|
|
76
|
+
getCheckpointChanges(options: GetCheckpointChangesOptions): Promise<CheckpointChanges>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Yields the latest user write checkpoint whenever the sync checkpoint updates.
|
|
80
|
+
*
|
|
81
|
+
* The stream stops or errors if this is not the active sync rules (anymore).
|
|
82
|
+
*/
|
|
83
|
+
watchWriteCheckpoint(options: WatchWriteCheckpointOptions): AsyncIterable<StorageCheckpointUpdate>;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get a "batch" of data for a checkpoint.
|
|
87
|
+
*
|
|
88
|
+
* The results will be split into separate SyncBucketData chunks to:
|
|
89
|
+
* 1. Separate buckets.
|
|
90
|
+
* 2. Limit the size of each individual chunk according to options.batchSizeLimitBytes.
|
|
91
|
+
*
|
|
92
|
+
* @param checkpoint the checkpoint
|
|
93
|
+
* @param dataBuckets current bucket states
|
|
94
|
+
* @param options batch size options
|
|
95
|
+
*/
|
|
96
|
+
getBucketDataBatch(
|
|
97
|
+
checkpoint: util.OpId,
|
|
98
|
+
dataBuckets: Map<string, string>,
|
|
99
|
+
options?: BucketDataBatchOptions
|
|
100
|
+
): AsyncIterable<SyncBucketDataBatch>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Compute checksums for a given list of buckets.
|
|
104
|
+
*
|
|
105
|
+
* Returns zero checksums for any buckets not found.
|
|
106
|
+
*/
|
|
107
|
+
getChecksums(checkpoint: util.OpId, buckets: string[]): Promise<util.ChecksumMap>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface SyncRulesBucketStorageListener {
|
|
111
|
+
batchStarted: (batch: BucketStorageBatch) => void;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface SyncRuleStatus {
|
|
115
|
+
checkpoint_lsn: string | null;
|
|
116
|
+
active: boolean;
|
|
117
|
+
snapshot_done: boolean;
|
|
118
|
+
}
|
|
119
|
+
export interface ResolveTableOptions {
|
|
120
|
+
group_id: number;
|
|
121
|
+
connection_id: number;
|
|
122
|
+
connection_tag: string;
|
|
123
|
+
entity_descriptor: SourceEntityDescriptor;
|
|
124
|
+
|
|
125
|
+
sync_rules: SqlSyncRules;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface ResolveTableResult {
|
|
129
|
+
table: SourceTable;
|
|
130
|
+
dropTables: SourceTable[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface StartBatchOptions extends ParseSyncRulesOptions {
|
|
134
|
+
zeroLSN: string;
|
|
135
|
+
/**
|
|
136
|
+
* Whether or not to store a copy of the current data.
|
|
137
|
+
*
|
|
138
|
+
* This is needed if we need to apply partial updates, for example
|
|
139
|
+
* when we get TOAST values from Postgres.
|
|
140
|
+
*
|
|
141
|
+
* This is not needed when we get the full document from the source
|
|
142
|
+
* database, for example from MongoDB.
|
|
143
|
+
*/
|
|
144
|
+
storeCurrentData: boolean;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Set to true for initial replication.
|
|
148
|
+
*
|
|
149
|
+
* This will avoid creating new operations for rows previously replicated.
|
|
150
|
+
*/
|
|
151
|
+
skipExistingRows?: boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface CompactOptions {
|
|
155
|
+
/**
|
|
156
|
+
* Heap memory limit for the compact process.
|
|
157
|
+
*
|
|
158
|
+
* Add around 64MB to this to determine the "--max-old-space-size" argument.
|
|
159
|
+
* Add another 80MB to get RSS usage / memory limits.
|
|
160
|
+
*/
|
|
161
|
+
memoryLimitMB?: number;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* If specified, ignore any operations newer than this when compacting.
|
|
165
|
+
*
|
|
166
|
+
* This is primarily for tests, where we want to test compacting at a specific
|
|
167
|
+
* point.
|
|
168
|
+
*
|
|
169
|
+
* This can also be used to create a "safe buffer" of recent operations that should
|
|
170
|
+
* not be compacted, to avoid invalidating checkpoints in use.
|
|
171
|
+
*/
|
|
172
|
+
maxOpId?: bigint;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* If specified, compact only the specific buckets.
|
|
176
|
+
*
|
|
177
|
+
* If not specified, compacts all buckets.
|
|
178
|
+
*
|
|
179
|
+
* These can be individual bucket names, or bucket definition names.
|
|
180
|
+
*/
|
|
181
|
+
compactBuckets?: string[];
|
|
182
|
+
|
|
183
|
+
/** Minimum of 2 */
|
|
184
|
+
clearBatchLimit?: number;
|
|
185
|
+
|
|
186
|
+
/** Minimum of 1 */
|
|
187
|
+
moveBatchLimit?: number;
|
|
188
|
+
|
|
189
|
+
/** Minimum of 1 */
|
|
190
|
+
moveBatchQueryLimit?: number;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface TerminateOptions {
|
|
194
|
+
/**
|
|
195
|
+
* If true, also clear the storage before terminating.
|
|
196
|
+
*/
|
|
197
|
+
clearStorage: boolean;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface BucketDataBatchOptions {
|
|
201
|
+
/** Limit number of documents returned. Defaults to 1000. */
|
|
202
|
+
limit?: number;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Limit size of chunks returned. Defaults to 1MB.
|
|
206
|
+
*
|
|
207
|
+
* This is a lower bound, not an upper bound. As soon as the chunk size goes over this limit,
|
|
208
|
+
* it is returned.
|
|
209
|
+
*
|
|
210
|
+
* Note that an individual data row can be close to 16MB in size, so this does not help in
|
|
211
|
+
* extreme cases.
|
|
212
|
+
*/
|
|
213
|
+
chunkLimitBytes?: number;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface SyncBucketDataBatch {
|
|
217
|
+
batch: util.SyncBucketData;
|
|
218
|
+
targetOp: bigint | null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface ReplicationCheckpoint {
|
|
222
|
+
readonly checkpoint: util.OpId;
|
|
223
|
+
readonly lsn: string | null;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface WatchWriteCheckpointOptions {
|
|
227
|
+
/** user_id and client_id combined. */
|
|
228
|
+
user_id: string;
|
|
229
|
+
|
|
230
|
+
signal: AbortSignal;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface WatchFilterEvent {
|
|
234
|
+
changedDataBucket?: string;
|
|
235
|
+
changedParameterBucketDefinition?: string;
|
|
236
|
+
invalidate?: boolean;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export interface WriteCheckpoint {
|
|
240
|
+
base: ReplicationCheckpoint;
|
|
241
|
+
writeCheckpoint: bigint | null;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export interface StorageCheckpointUpdate extends WriteCheckpoint {
|
|
245
|
+
update: CheckpointChanges;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export interface GetCheckpointChangesOptions {
|
|
249
|
+
lastCheckpoint: util.OpId;
|
|
250
|
+
nextCheckpoint: util.OpId;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export interface CheckpointChanges {
|
|
254
|
+
updatedDataBuckets: string[];
|
|
255
|
+
invalidateDataBuckets: boolean;
|
|
256
|
+
updatedParameterBucketDefinitions: string[];
|
|
257
|
+
invalidateParameterBuckets: boolean;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export const CHECKPOINT_INVALIDATE_ALL: CheckpointChanges = {
|
|
261
|
+
updatedDataBuckets: [],
|
|
262
|
+
invalidateDataBuckets: true,
|
|
263
|
+
updatedParameterBucketDefinitions: [],
|
|
264
|
+
invalidateParameterBuckets: true
|
|
265
|
+
};
|
package/src/storage/bson.ts
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import * as bson from 'bson';
|
|
2
2
|
|
|
3
3
|
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
4
|
-
import { ReplicaId } from './
|
|
4
|
+
import { ReplicaId } from './BucketStorageBatch.js';
|
|
5
5
|
|
|
6
6
|
type NodeBuffer = Buffer<ArrayBuffer>;
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Use for internal (bucket storage) data, where we control each field.
|
|
10
|
+
*/
|
|
11
|
+
export const BSON_DESERIALIZE_INTERNAL_OPTIONS: bson.DeserializeOptions = {
|
|
9
12
|
// use bigint instead of Long
|
|
10
13
|
useBigInt64: true
|
|
11
14
|
};
|
|
12
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Use for data from external sources, which could contain arbitrary fields.
|
|
18
|
+
*/
|
|
19
|
+
export const BSON_DESERIALIZE_DATA_OPTIONS: bson.DeserializeOptions = {
|
|
20
|
+
useBigInt64: true
|
|
21
|
+
};
|
|
22
|
+
|
|
13
23
|
/**
|
|
14
24
|
* Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
|
|
15
25
|
* @param lookup
|
|
@@ -29,6 +39,11 @@ export const serializeLookup = (lookup: SqliteJsonValue[]) => {
|
|
|
29
39
|
return new bson.Binary(serializeLookupBuffer(lookup));
|
|
30
40
|
};
|
|
31
41
|
|
|
42
|
+
export const getLookupBucketDefinitionName = (lookup: bson.Binary) => {
|
|
43
|
+
const parsed = bson.deserialize(lookup.buffer, BSON_DESERIALIZE_INTERNAL_OPTIONS).l as SqliteJsonValue[];
|
|
44
|
+
return parsed[0] as string;
|
|
45
|
+
};
|
|
46
|
+
|
|
32
47
|
/**
|
|
33
48
|
* True if this is a bson.UUID.
|
|
34
49
|
*
|
|
@@ -51,8 +66,11 @@ export const deserializeReplicaId = (id: Buffer): ReplicaId => {
|
|
|
51
66
|
return deserialized.id;
|
|
52
67
|
};
|
|
53
68
|
|
|
54
|
-
|
|
55
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Deserialize BSON - can be used for BSON containing arbitrary user data.
|
|
71
|
+
*/
|
|
72
|
+
export const deserializeBson = (buffer: Uint8Array): bson.Document => {
|
|
73
|
+
return bson.deserialize(buffer, BSON_DESERIALIZE_DATA_OPTIONS);
|
|
56
74
|
};
|
|
57
75
|
|
|
58
76
|
export const serializeBson = (document: any): NodeBuffer => {
|
|
@@ -7,3 +7,8 @@ export * from './SourceTable.js';
|
|
|
7
7
|
export * from './StorageEngine.js';
|
|
8
8
|
export * from './StorageProvider.js';
|
|
9
9
|
export * from './WriteCheckpointAPI.js';
|
|
10
|
+
export * from './BucketStorageFactory.js';
|
|
11
|
+
export * from './BucketStorageBatch.js';
|
|
12
|
+
export * from './SyncRulesBucketStorage.js';
|
|
13
|
+
export * from './PersistedSyncRulesContent.js';
|
|
14
|
+
export * from './ReplicationLock.js';
|