@powersync/shared-internals 1.1.1 → 1.3.0
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/dist/websockets.mjs.map +1 -1
- package/dist/websockets.node.mjs.map +1 -1
- package/lib/client/BasePowerSyncDatabase.d.ts +13 -3
- package/lib/client/BasePowerSyncDatabase.js +59 -5
- package/lib/client/BasePowerSyncDatabase.js.map +1 -1
- package/lib/client/ConnectionManager.d.ts +4 -0
- package/lib/client/ConnectionManager.js +19 -1
- package/lib/client/ConnectionManager.js.map +1 -1
- package/lib/client/sync/CheckpointRequestImpl.d.ts +11 -0
- package/lib/client/sync/CheckpointRequestImpl.js +42 -0
- package/lib/client/sync/CheckpointRequestImpl.js.map +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +1 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +7 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
- package/lib/client/sync/options.js +2 -1
- package/lib/client/sync/options.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +12 -6
- package/lib/client/sync/stream/AbstractRemote.js +16 -14
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +29 -4
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +177 -23
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/client/sync/stream/CheckpointState.d.ts +44 -0
- package/lib/client/sync/stream/CheckpointState.js +111 -0
- package/lib/client/sync/stream/CheckpointState.js.map +1 -0
- package/lib/client/sync/stream/core-instruction.d.ts +6 -0
- package/lib/client/sync/stream/core-instruction.js.map +1 -1
- package/lib/utils/stream_transform.d.ts +1 -0
- package/lib/utils/stream_transform.js +18 -2
- package/lib/utils/stream_transform.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +4 -3
- package/src/client/BasePowerSyncDatabase.ts +71 -14
- package/src/client/ConnectionManager.ts +23 -1
- package/src/client/sync/CheckpointRequestImpl.ts +54 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +1 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +8 -0
- package/src/client/sync/options.ts +2 -1
- package/src/client/sync/stream/AbstractRemote.ts +29 -15
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +217 -28
- package/src/client/sync/stream/CheckpointState.ts +142 -0
- package/src/client/sync/stream/core-instruction.ts +7 -0
- package/src/utils/stream_transform.ts +19 -3
- package/src/version.ts +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { CoreSyncStatus } from './core-instruction.js';
|
|
2
|
+
export type CheckpointState = {
|
|
3
|
+
state: 'pending';
|
|
4
|
+
} | {
|
|
5
|
+
state: 'disconnected';
|
|
6
|
+
} | {
|
|
7
|
+
state: 'ready';
|
|
8
|
+
} | {
|
|
9
|
+
state: 'error';
|
|
10
|
+
error: unknown;
|
|
11
|
+
};
|
|
12
|
+
export declare class CheckpointStateSignals {
|
|
13
|
+
private currentState;
|
|
14
|
+
private readonly stateChanged;
|
|
15
|
+
/**
|
|
16
|
+
* Use to immediately restart another download iteration if it is retry delay when a new checkpoint is requested.
|
|
17
|
+
*/
|
|
18
|
+
private waitingForCheckpointsReady;
|
|
19
|
+
private updateState;
|
|
20
|
+
/**
|
|
21
|
+
* Marks the current download iteration as ended, blocking new checkpoint requests until the seed performed in the
|
|
22
|
+
* next iteration.
|
|
23
|
+
*/
|
|
24
|
+
downloadIterationEnded(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Marks the sync client as disconnected, failing all outstanding checkpoint requests and preventing new ones.
|
|
27
|
+
*/
|
|
28
|
+
disconnected(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Returns a promise that resolves with the abort signal or when we have a waiter wanting to request a checkpoint.
|
|
31
|
+
*/
|
|
32
|
+
waitForCheckpointWaiter(signal: AbortSignal): Promise<void>;
|
|
33
|
+
markCheckpointsReady(completion: Promise<void>): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Waits until a download iteration is active and has seeded the checkpoint state, meaning that checkpoint ids can
|
|
36
|
+
* safely be allocated.
|
|
37
|
+
*
|
|
38
|
+
* @returns Turue if checkpoints are ready, false if aborted.
|
|
39
|
+
*/
|
|
40
|
+
waitForCheckpointRequestsReady(abort: AbortSignal, wakeDownloadLoop?: boolean): Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
export declare function cannotRequestDueToDisconnectedError(): Error;
|
|
43
|
+
export declare function checkpointRequestsNotEnabledError(): Error;
|
|
44
|
+
export declare function isCheckpointRequestApplied(status: CoreSyncStatus | null, requestId: bigint): boolean;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { BaseObserver } from '@powersync/common';
|
|
2
|
+
import { asyncNotifier } from '../../../utils/async.js';
|
|
3
|
+
const pending = { state: 'pending' };
|
|
4
|
+
export class CheckpointStateSignals {
|
|
5
|
+
currentState = pending;
|
|
6
|
+
stateChanged = new BaseObserver();
|
|
7
|
+
/**
|
|
8
|
+
* Use to immediately restart another download iteration if it is retry delay when a new checkpoint is requested.
|
|
9
|
+
*/
|
|
10
|
+
waitingForCheckpointsReady = asyncNotifier();
|
|
11
|
+
updateState(state) {
|
|
12
|
+
this.currentState = state;
|
|
13
|
+
this.stateChanged.iterateListeners((l) => l.stateChanged?.());
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Marks the current download iteration as ended, blocking new checkpoint requests until the seed performed in the
|
|
17
|
+
* next iteration.
|
|
18
|
+
*/
|
|
19
|
+
downloadIterationEnded() {
|
|
20
|
+
this.updateState(pending);
|
|
21
|
+
// Checkpoint waiters called after this should be able to resume the download iteration.
|
|
22
|
+
this.waitingForCheckpointsReady = asyncNotifier();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Marks the sync client as disconnected, failing all outstanding checkpoint requests and preventing new ones.
|
|
26
|
+
*/
|
|
27
|
+
disconnected() {
|
|
28
|
+
this.updateState({ state: 'disconnected' });
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns a promise that resolves with the abort signal or when we have a waiter wanting to request a checkpoint.
|
|
32
|
+
*/
|
|
33
|
+
waitForCheckpointWaiter(signal) {
|
|
34
|
+
return this.waitingForCheckpointsReady.waitForNotification(signal);
|
|
35
|
+
}
|
|
36
|
+
markCheckpointsReady(completion) {
|
|
37
|
+
return completion.then((_) => this.updateState({ state: 'ready' }), (error) => {
|
|
38
|
+
this.updateState({ state: 'error', error });
|
|
39
|
+
throw error;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Waits until a download iteration is active and has seeded the checkpoint state, meaning that checkpoint ids can
|
|
44
|
+
* safely be allocated.
|
|
45
|
+
*
|
|
46
|
+
* @returns Turue if checkpoints are ready, false if aborted.
|
|
47
|
+
*/
|
|
48
|
+
async waitForCheckpointRequestsReady(abort, wakeDownloadLoop = true) {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
// A retry can reach this waiter after disconnect has already aborted its signal.
|
|
51
|
+
if (abort.aborted) {
|
|
52
|
+
resolve(false);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
// Resolves the promise from the current state if possible, returning true if it was.
|
|
56
|
+
const handleState = () => {
|
|
57
|
+
const state = this.currentState;
|
|
58
|
+
switch (state.state) {
|
|
59
|
+
case 'disconnected':
|
|
60
|
+
reject(cannotRequestDueToDisconnectedError());
|
|
61
|
+
return true;
|
|
62
|
+
case 'ready':
|
|
63
|
+
resolve(true);
|
|
64
|
+
return true;
|
|
65
|
+
case 'error':
|
|
66
|
+
reject(state.error);
|
|
67
|
+
return true;
|
|
68
|
+
case 'pending': {
|
|
69
|
+
// Wait for the next event.
|
|
70
|
+
if (wakeDownloadLoop) {
|
|
71
|
+
this.waitingForCheckpointsReady.notify();
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (handleState()) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
let removeListener;
|
|
81
|
+
let onAbort;
|
|
82
|
+
function cleanup() {
|
|
83
|
+
removeListener();
|
|
84
|
+
abort.removeEventListener('abort', onAbort);
|
|
85
|
+
}
|
|
86
|
+
onAbort = () => {
|
|
87
|
+
resolve(false);
|
|
88
|
+
cleanup();
|
|
89
|
+
};
|
|
90
|
+
removeListener = this.stateChanged.registerListener({
|
|
91
|
+
stateChanged() {
|
|
92
|
+
if (handleState()) {
|
|
93
|
+
cleanup();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
abort.addEventListener('abort', onAbort);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export function cannotRequestDueToDisconnectedError() {
|
|
102
|
+
return new Error('Cannot request checkpoints, sync client is disconnected.');
|
|
103
|
+
}
|
|
104
|
+
export function checkpointRequestsNotEnabledError() {
|
|
105
|
+
return new Error('Connected with legacy checkpoint mode, cannot request checkpoints.');
|
|
106
|
+
}
|
|
107
|
+
export function isCheckpointRequestApplied(status, requestId) {
|
|
108
|
+
const applied = status?.internal_last_applied_checkpoint_request_id;
|
|
109
|
+
return applied != null && BigInt(applied) >= requestId;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=CheckpointState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CheckpointState.js","sourceRoot":"","sources":["../../../../src/client/sync/stream/CheckpointState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAc,MAAM,yBAAyB,CAAC;AASpE,MAAM,OAAO,GAAoB,EAAE,KAAK,E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ACF,CAAC,CAAC;YACH,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAMD,MAAM,UAAU,mCAAmC;IACjD,OAAO,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;AAC/E,CAAC;AAED,MAAM,UAAU,iCAAiC;IAC/C,OAAO,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;AACzF,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,MAA6B,EAAE,SAAiB;IACzF,MAAM,OAAO,GAAG,MAAM,EAAE,2CAA2C,CAAC;IACpE,OAAO,OAAO,IAAI,IAAI,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,SAAS,CAAC;AACzD,CAAC"}
|
|
@@ -30,6 +30,7 @@ export interface LogLine {
|
|
|
30
30
|
}
|
|
31
31
|
export interface EstablishSyncStream {
|
|
32
32
|
request: unknown;
|
|
33
|
+
checkpoint_request?: CheckpointRequestPayload;
|
|
33
34
|
}
|
|
34
35
|
export interface UpdateSyncStatus {
|
|
35
36
|
status: CoreSyncStatus;
|
|
@@ -40,6 +41,7 @@ export interface CoreSyncStatus {
|
|
|
40
41
|
priority_status: SyncPriorityStatus[];
|
|
41
42
|
downloading: DownloadProgress | null;
|
|
42
43
|
streams: CoreStreamSubscription[];
|
|
44
|
+
internal_last_applied_checkpoint_request_id?: string;
|
|
43
45
|
}
|
|
44
46
|
export interface CoreStreamSubscription {
|
|
45
47
|
progress: {
|
|
@@ -72,4 +74,8 @@ export interface BucketProgress {
|
|
|
72
74
|
export interface FetchCredentials {
|
|
73
75
|
did_expire: boolean;
|
|
74
76
|
}
|
|
77
|
+
export interface CheckpointRequestPayload {
|
|
78
|
+
client_id: string;
|
|
79
|
+
checkpoint_request_id: string;
|
|
80
|
+
}
|
|
75
81
|
export declare function isInterruptingInstruction(instruction: Instruction): instruction is InterruptingInstruction;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core-instruction.js","sourceRoot":"","sources":["../../../../src/client/sync/stream/core-instruction.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"core-instruction.js","sourceRoot":"","sources":["../../../../src/client/sync/stream/core-instruction.ts"],"names":[],"mappings":"AAkFA,MAAM,UAAU,yBAAyB,CAAC,WAAwB;IAChE,OAAO,qBAAqB,IAAI,WAAW,IAAI,iBAAiB,IAAI,WAAW,CAAC;AAClF,CAAC"}
|
|
@@ -20,6 +20,7 @@ export declare function valueResult<T>(value: T): {
|
|
|
20
20
|
export declare function map<T1, T2>(source: SimpleAsyncIterator<T1>, map: (source: T1) => T2): SimpleAsyncIterator<T2>;
|
|
21
21
|
export interface InjectableIterator<T> extends SimpleAsyncIterator<T> {
|
|
22
22
|
inject(event: T): void;
|
|
23
|
+
injectError(error: unknown): void;
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
26
|
* Expands a source async iterator by allowing to inject events asynchronously.
|
|
@@ -73,7 +73,14 @@ export function injectable(source) {
|
|
|
73
73
|
}
|
|
74
74
|
// Second priority: Dispatch injected events
|
|
75
75
|
if (pendingInjectedEvents.length) {
|
|
76
|
-
|
|
76
|
+
const event = pendingInjectedEvents.shift();
|
|
77
|
+
if (event.status === 'fulfilled') {
|
|
78
|
+
resolve(valueResult(event.value));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
reject(event.reason);
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
77
84
|
}
|
|
78
85
|
// Nothing pending? Fetch from source
|
|
79
86
|
waiter = { resolve, reject };
|
|
@@ -88,7 +95,16 @@ export function injectable(source) {
|
|
|
88
95
|
pending.resolve(valueResult(event));
|
|
89
96
|
}
|
|
90
97
|
else {
|
|
91
|
-
pendingInjectedEvents.push(event);
|
|
98
|
+
pendingInjectedEvents.push({ status: 'fulfilled', value: event });
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
injectError(error) {
|
|
102
|
+
const pending = consumeWaiter();
|
|
103
|
+
if (pending != null) {
|
|
104
|
+
pending.reject(error);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
pendingInjectedEvents.push({ status: 'rejected', reason: error });
|
|
92
108
|
}
|
|
93
109
|
}
|
|
94
110
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream_transform.js","sourceRoot":"","sources":["../../src/utils/stream_transform.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,UAAU,GAA8B,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAEtF,MAAM,UAAU,WAAW,CAAI,KAAQ;IACrC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG,CAAS,MAA+B,EAAE,GAAuB;IAClF,OAAO;QACL,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAClC,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC;gBACf,OAAO,KAAK,CAAC;YACf,CAAC;iBAAM,CAAC;gBACN,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACrC,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC;
|
|
1
|
+
{"version":3,"file":"stream_transform.js","sourceRoot":"","sources":["../../src/utils/stream_transform.ts"],"names":[],"mappings":"AAYA,MAAM,CAAC,MAAM,UAAU,GAA8B,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAEtF,MAAM,UAAU,WAAW,CAAI,KAAQ;IACrC,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG,CAAS,MAA+B,EAAE,GAAuB;IAClF,OAAO;QACL,IA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
1
|
+
export declare const POWERSYNC_JS_VERSION = "1.3.0";
|
package/lib/version.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// Note: This file gets updated by tool/update_version.js script as part of the Changesets release workflow.
|
|
2
2
|
// The update happens in the changesets PR, before the package is built.
|
|
3
|
-
export const POWERSYNC_JS_VERSION = '1.
|
|
3
|
+
export const POWERSYNC_JS_VERSION = '1.3.0';
|
|
4
4
|
//# sourceMappingURL=version.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/shared-internals",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://docs.powersync.com",
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@powersync/common": "2.
|
|
41
|
+
"@powersync/common": "^2.3.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@rollup/plugin-commonjs": "^29.0.0",
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"rollup": "^4.52.5",
|
|
55
55
|
"rollup-plugin-dts": "^6.2.1",
|
|
56
56
|
"rsocket-core": "1.0.0-alpha.3",
|
|
57
|
-
"rsocket-websocket-client": "1.0.0-alpha.3"
|
|
57
|
+
"rsocket-websocket-client": "1.0.0-alpha.3",
|
|
58
|
+
"@powersync/common": "2.3.0"
|
|
58
59
|
},
|
|
59
60
|
"scripts": {
|
|
60
61
|
"build": "tsc -b && rollup -c rollup.config.mjs",
|
|
@@ -32,13 +32,10 @@ import {
|
|
|
32
32
|
WatchOnChangeHandler,
|
|
33
33
|
BaseObserver,
|
|
34
34
|
SqliteRecord,
|
|
35
|
-
SyncStreamConnectionMethod
|
|
35
|
+
SyncStreamConnectionMethod,
|
|
36
|
+
CheckpointRequest
|
|
36
37
|
} from '@powersync/common';
|
|
37
|
-
import {
|
|
38
|
-
BucketStorageAdapter,
|
|
39
|
-
PSInternalTable,
|
|
40
|
-
targetCheckpointRequestId
|
|
41
|
-
} from './sync/bucket/BucketStorageAdapter.js';
|
|
38
|
+
import { BucketStorageAdapter, PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
|
|
42
39
|
import { SyncStatusSnapshot } from '../db/crud/SyncStatus.js';
|
|
43
40
|
import {
|
|
44
41
|
ConnectionManager,
|
|
@@ -47,7 +44,10 @@ import {
|
|
|
47
44
|
} from './ConnectionManager.js';
|
|
48
45
|
import { Mutex } from '../utils/mutex.js';
|
|
49
46
|
import { TriggerManagerConfig, TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
|
|
50
|
-
import {
|
|
47
|
+
import {
|
|
48
|
+
AbstractStreamingSyncImplementationOptions,
|
|
49
|
+
StreamingSyncImplementation
|
|
50
|
+
} from './sync/stream/AbstractStreamingSyncImplementation.js';
|
|
51
51
|
import { CoreSyncStatus } from './sync/stream/core-instruction.js';
|
|
52
52
|
import { CrudEntryImpl, CrudEntryJSON } from './sync/bucket/CrudEntry.js';
|
|
53
53
|
import { OnChangeQueryProcessor } from './watched/OnChangeQueryProcessor.js';
|
|
@@ -58,11 +58,18 @@ import { CustomQuery } from './CustomQuery.js';
|
|
|
58
58
|
import { MEMORY_TRIGGER_CLAIM_MANAGER } from './triggers/MemoryTriggerClaimManager.js';
|
|
59
59
|
import { symbolAsyncIterator } from '../utils/compatibility.js';
|
|
60
60
|
import { MAX_OP_ID } from '../constants.js';
|
|
61
|
+
import { SqliteBucketStorage } from './sync/bucket/SqliteBucketStorage.js';
|
|
62
|
+
import {
|
|
63
|
+
cannotRequestDueToDisconnectedError,
|
|
64
|
+
checkpointRequestsNotEnabledError
|
|
65
|
+
} from './sync/stream/CheckpointState.js';
|
|
66
|
+
import { CheckpointRequestImpl } from './sync/CheckpointRequestImpl.js';
|
|
61
67
|
|
|
62
68
|
const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
|
|
63
69
|
|
|
64
70
|
const DEFAULT_DISCONNECT_CLEAR_OPTIONS: DisconnectAndClearOptions = {
|
|
65
|
-
clearLocal: true
|
|
71
|
+
clearLocal: true,
|
|
72
|
+
soft: false
|
|
66
73
|
};
|
|
67
74
|
|
|
68
75
|
/**
|
|
@@ -96,7 +103,7 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
96
103
|
|
|
97
104
|
protected bucketStorageAdapter: BucketStorageAdapter;
|
|
98
105
|
protected _isReadyPromise: Promise<void>;
|
|
99
|
-
|
|
106
|
+
connectionManager: ConnectionManager;
|
|
100
107
|
private subscriptions: InternalSubscriptionAdapter;
|
|
101
108
|
|
|
102
109
|
get syncStreamImplementation() {
|
|
@@ -257,7 +264,31 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
257
264
|
options: CreateSyncImplementationOptions
|
|
258
265
|
): StreamingSyncImplementation;
|
|
259
266
|
|
|
260
|
-
protected
|
|
267
|
+
protected commonSyncOptions(connector: PowerSyncBackendConnector, options: CreateSyncImplementationOptions) {
|
|
268
|
+
return {
|
|
269
|
+
...options,
|
|
270
|
+
adapter: this.bucketStorageAdapter,
|
|
271
|
+
// PowerSyncBackendConnector accepts no abort signal; these callbacks await it directly.
|
|
272
|
+
// TODO: We should eventually forward the abort signal to controllers as well.
|
|
273
|
+
uploadCrud: async () => {
|
|
274
|
+
await this.waitForReady();
|
|
275
|
+
await connector.uploadData(this);
|
|
276
|
+
},
|
|
277
|
+
postCheckpointRequest: (clientId, requestId) => {
|
|
278
|
+
if (connector.postCheckpointRequest) {
|
|
279
|
+
return this.waitForReady().then((_) => connector.postCheckpointRequest!(clientId, requestId));
|
|
280
|
+
} else {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
identifier: this.database.name,
|
|
285
|
+
logger: this.logger
|
|
286
|
+
} satisfies Partial<AbstractStreamingSyncImplementationOptions>;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
protected generateBucketStorageAdapter(): BucketStorageAdapter {
|
|
290
|
+
return new SqliteBucketStorage(this.database, this.logger);
|
|
291
|
+
}
|
|
261
292
|
|
|
262
293
|
async waitForReady(): Promise<void> {
|
|
263
294
|
if (this.ready) {
|
|
@@ -284,10 +315,18 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
284
315
|
return;
|
|
285
316
|
}
|
|
286
317
|
|
|
287
|
-
return new Promise((resolve) => {
|
|
318
|
+
return new Promise((resolve, reject) => {
|
|
288
319
|
const dispose = this.registerListener({
|
|
289
320
|
statusChanged: (status) => {
|
|
290
|
-
|
|
321
|
+
let isComplete = false;
|
|
322
|
+
try {
|
|
323
|
+
isComplete = predicate(status);
|
|
324
|
+
} catch (e) {
|
|
325
|
+
reject(e);
|
|
326
|
+
isComplete = true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (isComplete) {
|
|
291
330
|
abort();
|
|
292
331
|
}
|
|
293
332
|
}
|
|
@@ -296,6 +335,7 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
296
335
|
function abort() {
|
|
297
336
|
dispose();
|
|
298
337
|
resolve();
|
|
338
|
+
signal?.removeEventListener('abort', abort);
|
|
299
339
|
}
|
|
300
340
|
|
|
301
341
|
if (signal?.aborted) {
|
|
@@ -415,10 +455,12 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
415
455
|
await this.disconnect();
|
|
416
456
|
await this.waitForReady();
|
|
417
457
|
|
|
418
|
-
|
|
458
|
+
let flags = 0;
|
|
459
|
+
if (options.clearLocal) flags |= 1;
|
|
460
|
+
if (options.soft) flags |= 2;
|
|
419
461
|
|
|
420
462
|
await this.database.writeTransaction(async (tx) => {
|
|
421
|
-
await tx.execute('SELECT powersync_clear(?)', [
|
|
463
|
+
await tx.execute('SELECT powersync_clear(?)', [flags]);
|
|
422
464
|
});
|
|
423
465
|
|
|
424
466
|
// The data has been deleted - reset the sync status
|
|
@@ -429,6 +471,21 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
429
471
|
return this.connectionManager.stream(this.subscriptions, name, params ?? null);
|
|
430
472
|
}
|
|
431
473
|
|
|
474
|
+
async requestCheckpoint(): Promise<CheckpointRequest> {
|
|
475
|
+
await this.waitForReady();
|
|
476
|
+
|
|
477
|
+
const impl = this.connectionManager.syncStreamImplementation;
|
|
478
|
+
if (impl == null) {
|
|
479
|
+
throw cannotRequestDueToDisconnectedError();
|
|
480
|
+
}
|
|
481
|
+
if (this.connectionManager.connectionOptions?.checkpointMode == 'legacy') {
|
|
482
|
+
throw checkpointRequestsNotEnabledError();
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const checkpoint = await impl.requestCheckpoint();
|
|
486
|
+
return new CheckpointRequestImpl(checkpoint, this);
|
|
487
|
+
}
|
|
488
|
+
|
|
432
489
|
async close(options: PowerSyncCloseOptions = DEFAULT_POWERSYNC_CLOSE_OPTIONS) {
|
|
433
490
|
await this.waitForReady();
|
|
434
491
|
|
|
@@ -150,12 +150,21 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
150
150
|
const hadPendingOptions = !!this.pendingConnectionOptions;
|
|
151
151
|
|
|
152
152
|
// Update pending options to the latest values
|
|
153
|
+
const resolvedOptions = resolveSyncOptions(options, this.options.defaultConnectionMethod);
|
|
153
154
|
this.pendingConnectionOptions = {
|
|
154
155
|
connector,
|
|
155
|
-
options:
|
|
156
|
+
options: resolvedOptions,
|
|
156
157
|
schema: serializedSchema
|
|
157
158
|
};
|
|
158
159
|
|
|
160
|
+
if (connector.postCheckpointRequest && resolvedOptions.checkpointMode == 'legacy') {
|
|
161
|
+
this.logger.log({
|
|
162
|
+
level: LogLevels.warn,
|
|
163
|
+
message:
|
|
164
|
+
'The backend connector implements postCheckpointRequest, but connect() was called without checkpoint requests enabled.'
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
159
168
|
// Disconnecting here provides aborting in progress connection attempts.
|
|
160
169
|
// The connectInternal method will clear pending options once it starts connecting (with the options).
|
|
161
170
|
// We only need to trigger a disconnect here if we have already reached the point of connecting.
|
|
@@ -225,6 +234,7 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
225
234
|
|
|
226
235
|
this.pendingConnectionOptions = null;
|
|
227
236
|
|
|
237
|
+
const subscriptionsAtStart = this.subscriptionIdentity;
|
|
228
238
|
const { sync, onDispose } = await this.options.createSyncImplementation(connector, {
|
|
229
239
|
subscriptions: this.activeStreams,
|
|
230
240
|
serializedSchema: schema
|
|
@@ -233,6 +243,11 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
233
243
|
this.syncStreamImplementation = sync;
|
|
234
244
|
this.syncDisposer = onDispose;
|
|
235
245
|
await this.syncStreamImplementation.waitForReady();
|
|
246
|
+
|
|
247
|
+
// Subscriptions changed while creating the sync stream implementation, update it now.
|
|
248
|
+
if (this.subscriptionIdentity !== subscriptionsAtStart) {
|
|
249
|
+
this.syncStreamImplementation.updateSubscriptions(this.activeStreams);
|
|
250
|
+
}
|
|
236
251
|
resolve();
|
|
237
252
|
} catch (error) {
|
|
238
253
|
reject(error);
|
|
@@ -355,6 +370,13 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
355
370
|
return [...this.locallyActiveSubscriptions.values()].map((a) => ({ name: a.name, params: a.parameters }));
|
|
356
371
|
}
|
|
357
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Identifies the current set of subscriptions, for detecting a change across an await.
|
|
375
|
+
*/
|
|
376
|
+
private get subscriptionIdentity() {
|
|
377
|
+
return [...this.locallyActiveSubscriptions.keys()].join('\n');
|
|
378
|
+
}
|
|
379
|
+
|
|
358
380
|
private subscriptionsMayHaveChanged() {
|
|
359
381
|
this.syncStreamImplementation?.updateSubscriptions(this.activeStreams);
|
|
360
382
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CheckpointRequest } from '@powersync/common';
|
|
2
|
+
import { BasePowerSyncDatabase } from '../BasePowerSyncDatabase.js';
|
|
3
|
+
import {
|
|
4
|
+
cannotRequestDueToDisconnectedError,
|
|
5
|
+
checkpointRequestsNotEnabledError,
|
|
6
|
+
isCheckpointRequestApplied
|
|
7
|
+
} from './stream/CheckpointState.js';
|
|
8
|
+
import { SyncStatusSnapshot } from '../../db/crud/SyncStatus.js';
|
|
9
|
+
|
|
10
|
+
export class CheckpointRequestImpl implements CheckpointRequest {
|
|
11
|
+
constructor(
|
|
12
|
+
private readonly requestId: bigint,
|
|
13
|
+
private readonly database: BasePowerSyncDatabase
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
get hasSynced(): boolean {
|
|
17
|
+
const status = this.database.currentStatus.core;
|
|
18
|
+
return isCheckpointRequestApplied(status, this.requestId);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async waitForSync(options?: { signal?: AbortSignal }): Promise<void> {
|
|
22
|
+
if (this.hasSynced) return;
|
|
23
|
+
|
|
24
|
+
const signal = options?.signal;
|
|
25
|
+
const manager = this.database.connectionManager;
|
|
26
|
+
if (manager.syncStreamImplementation == null) {
|
|
27
|
+
throw cannotRequestDueToDisconnectedError();
|
|
28
|
+
}
|
|
29
|
+
if (manager.connectionOptions?.checkpointMode == 'legacy') {
|
|
30
|
+
throw checkpointRequestsNotEnabledError();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await this.database.waitForStatus((status) => {
|
|
34
|
+
if (isCheckpointRequestApplied((status as SyncStatusSnapshot).core, this.requestId)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const anyError = status.downloadError ?? status.uploadError;
|
|
39
|
+
if (anyError) {
|
|
40
|
+
throw new Error('Sync error while waiting for checkpoint request', { cause: anyError });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!status.connected && !status.connecting) {
|
|
44
|
+
throw cannotRequestDueToDisconnectedError();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return false;
|
|
48
|
+
}, signal);
|
|
49
|
+
|
|
50
|
+
if (signal?.aborted) {
|
|
51
|
+
throw signal.reason;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -45,6 +45,7 @@ export interface BucketStorageAdapter extends BaseObserverInterface<BucketStorag
|
|
|
45
45
|
|
|
46
46
|
updateLocalTarget(cb: () => Promise<string>): Promise<boolean>;
|
|
47
47
|
handleCrudCheckpoint(lastClientId: number, writeCheckpoint?: string): Promise<void>;
|
|
48
|
+
readCheckpointRequestId(variant: 'next' | 'current' | 'seed', payload?: string): Promise<string>;
|
|
48
49
|
|
|
49
50
|
/**
|
|
50
51
|
* Get an unique client id.
|
|
@@ -107,6 +107,14 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp
|
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
readCheckpointRequestId(variant: 'next' | 'current' | 'seed', payload: string | null = null): Promise<string> {
|
|
111
|
+
// This needs to run in a write transaction because some checkpoint request reads interact with sync client state
|
|
112
|
+
// bound to the write connection.
|
|
113
|
+
return this.db.writeTransaction(async (tx) => {
|
|
114
|
+
return (await rawPowerSyncControl(tx, `${variant}_checkpoint_request_id`, payload))!;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
110
118
|
async nextCrudItem(): Promise<CrudEntry | undefined> {
|
|
111
119
|
const next = await this.db.getOptional<CrudEntryJSON>('SELECT * FROM ps_crud ORDER BY id ASC LIMIT 1');
|
|
112
120
|
if (!next) {
|
|
@@ -19,6 +19,7 @@ export function resolveSyncOptions(
|
|
|
19
19
|
params: options.params ?? {},
|
|
20
20
|
includeDefaultStreams: options.includeDefaultStreams ?? true,
|
|
21
21
|
retryDelayMs: options.retryDelayMs ?? 5000,
|
|
22
|
-
crudUploadThrottleMs: options.crudUploadThrottleMs ?? 1000
|
|
22
|
+
crudUploadThrottleMs: options.crudUploadThrottleMs ?? 1000,
|
|
23
|
+
checkpointMode: options.checkpointMode ?? 'legacy'
|
|
23
24
|
};
|
|
24
25
|
}
|