@powersync/shared-internals 0.0.0-tmp.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/LICENSE +201 -0
- package/README.md +5 -0
- package/dist/bundle.cjs +11787 -0
- package/dist/bundle.cjs.map +1 -0
- package/dist/bundle.mjs +11757 -0
- package/dist/bundle.mjs.map +1 -0
- package/dist/bundle.node.cjs +9418 -0
- package/dist/bundle.node.cjs.map +1 -0
- package/dist/bundle.node.mjs +9388 -0
- package/dist/bundle.node.mjs.map +1 -0
- package/dist/index.d.cts +1159 -0
- package/dist/websockets.mjs +7558 -0
- package/dist/websockets.mjs.map +1 -0
- package/dist/websockets.node.mjs +5189 -0
- package/dist/websockets.node.mjs.map +1 -0
- package/legacy/sync_protocol.d.ts +103 -0
- package/lib/client/BasePowerSyncDatabase.d.ts +147 -0
- package/lib/client/BasePowerSyncDatabase.js +622 -0
- package/lib/client/BasePowerSyncDatabase.js.map +1 -0
- package/lib/client/ConnectionManager.d.ts +116 -0
- package/lib/client/ConnectionManager.js +306 -0
- package/lib/client/ConnectionManager.js.map +1 -0
- package/lib/client/CustomQuery.d.ts +21 -0
- package/lib/client/CustomQuery.js +43 -0
- package/lib/client/CustomQuery.js.map +1 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +42 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +23 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -0
- package/lib/client/sync/bucket/CrudEntry.d.ts +46 -0
- package/lib/client/sync/bucket/CrudEntry.js +60 -0
- package/lib/client/sync/bucket/CrudEntry.js.map +1 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +33 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +166 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -0
- package/lib/client/sync/options.d.ts +9 -0
- package/lib/client/sync/options.js +16 -0
- package/lib/client/sync/options.js.map +1 -0
- package/lib/client/sync/stream/AbstractRemote.d.ts +125 -0
- package/lib/client/sync/stream/AbstractRemote.js +265 -0
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +138 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +611 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -0
- package/lib/client/sync/stream/WebSocketSupport.d.ts +26 -0
- package/lib/client/sync/stream/WebSocketSupport.js +198 -0
- package/lib/client/sync/stream/WebSocketSupport.js.map +1 -0
- package/lib/client/sync/stream/WebsocketClientTransport.d.ts +15 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js +62 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -0
- package/lib/client/sync/stream/core-instruction.d.ts +75 -0
- package/lib/client/sync/stream/core-instruction.js +4 -0
- package/lib/client/sync/stream/core-instruction.js.map +1 -0
- package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +6 -0
- package/lib/client/triggers/MemoryTriggerClaimManager.js +21 -0
- package/lib/client/triggers/MemoryTriggerClaimManager.js.map +1 -0
- package/lib/client/triggers/TriggerManagerImpl.d.ts +68 -0
- package/lib/client/triggers/TriggerManagerImpl.js +415 -0
- package/lib/client/triggers/TriggerManagerImpl.js.map +1 -0
- package/lib/client/watched/AbstractQueryProcessor.d.ts +68 -0
- package/lib/client/watched/AbstractQueryProcessor.js +155 -0
- package/lib/client/watched/AbstractQueryProcessor.js.map +1 -0
- package/lib/client/watched/DifferentialQueryProcessor.d.ts +50 -0
- package/lib/client/watched/DifferentialQueryProcessor.js +174 -0
- package/lib/client/watched/DifferentialQueryProcessor.js.map +1 -0
- package/lib/client/watched/OnChangeQueryProcessor.d.ts +22 -0
- package/lib/client/watched/OnChangeQueryProcessor.js +80 -0
- package/lib/client/watched/OnChangeQueryProcessor.js.map +1 -0
- package/lib/client/watched/WatchedQuery.d.ts +9 -0
- package/lib/client/watched/WatchedQuery.js +12 -0
- package/lib/client/watched/WatchedQuery.js.map +1 -0
- package/lib/constants.d.ts +5 -0
- package/lib/constants.js +6 -0
- package/lib/constants.js.map +1 -0
- package/lib/db/ConnectionClosedError.d.ts +12 -0
- package/lib/db/ConnectionClosedError.js +23 -0
- package/lib/db/ConnectionClosedError.js.map +1 -0
- package/lib/db/crud/SyncProgress.d.ts +10 -0
- package/lib/db/crud/SyncProgress.js +33 -0
- package/lib/db/crud/SyncProgress.js.map +1 -0
- package/lib/db/crud/SyncStatus.d.ts +48 -0
- package/lib/db/crud/SyncStatus.js +178 -0
- package/lib/db/crud/SyncStatus.js.map +1 -0
- package/lib/db/openDatabase.d.ts +7 -0
- package/lib/db/openDatabase.js +22 -0
- package/lib/db/openDatabase.js.map +1 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.js +22 -0
- package/lib/index.js.map +1 -0
- package/lib/utils/AbortOperation.d.ts +11 -0
- package/lib/utils/AbortOperation.js +21 -0
- package/lib/utils/AbortOperation.js.map +1 -0
- package/lib/utils/ControlledExecutor.d.ts +31 -0
- package/lib/utils/ControlledExecutor.js +54 -0
- package/lib/utils/ControlledExecutor.js.map +1 -0
- package/lib/utils/MetaBaseObserver.d.ts +11 -0
- package/lib/utils/MetaBaseObserver.js +51 -0
- package/lib/utils/MetaBaseObserver.js.map +1 -0
- package/lib/utils/async.d.ts +46 -0
- package/lib/utils/async.js +147 -0
- package/lib/utils/async.js.map +1 -0
- package/lib/utils/compatibility.d.ts +8 -0
- package/lib/utils/compatibility.js +9 -0
- package/lib/utils/compatibility.js.map +1 -0
- package/lib/utils/mutex.d.ts +42 -0
- package/lib/utils/mutex.js +150 -0
- package/lib/utils/mutex.js.map +1 -0
- package/lib/utils/parseQuery.d.ts +12 -0
- package/lib/utils/parseQuery.js +20 -0
- package/lib/utils/parseQuery.js.map +1 -0
- package/lib/utils/queue.d.ts +16 -0
- package/lib/utils/queue.js +42 -0
- package/lib/utils/queue.js.map +1 -0
- package/lib/utils/stream_transform.d.ts +41 -0
- package/lib/utils/stream_transform.js +211 -0
- package/lib/utils/stream_transform.js.map +1 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +4 -0
- package/lib/version.js.map +1 -0
- package/package.json +65 -0
- package/src/client/BasePowerSyncDatabase.ts +853 -0
- package/src/client/ConnectionManager.ts +419 -0
- package/src/client/CustomQuery.ts +60 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +51 -0
- package/src/client/sync/bucket/CrudEntry.ts +116 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +213 -0
- package/src/client/sync/options.ts +24 -0
- package/src/client/sync/stream/AbstractRemote.ts +369 -0
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +795 -0
- package/src/client/sync/stream/WebSocketSupport.ts +259 -0
- package/src/client/sync/stream/WebsocketClientTransport.ts +82 -0
- package/src/client/sync/stream/core-instruction.ts +78 -0
- package/src/client/triggers/MemoryTriggerClaimManager.ts +25 -0
- package/src/client/triggers/TriggerManagerImpl.ts +538 -0
- package/src/client/watched/AbstractQueryProcessor.ts +231 -0
- package/src/client/watched/DifferentialQueryProcessor.ts +226 -0
- package/src/client/watched/OnChangeQueryProcessor.ts +108 -0
- package/src/client/watched/WatchedQuery.ts +14 -0
- package/src/constants.ts +6 -0
- package/src/db/ConnectionClosedError.ts +25 -0
- package/src/db/crud/SyncProgress.ts +39 -0
- package/src/db/crud/SyncStatus.ts +234 -0
- package/src/db/openDatabase.ts +23 -0
- package/src/index.ts +24 -0
- package/src/utils/AbortOperation.ts +19 -0
- package/src/utils/ControlledExecutor.ts +78 -0
- package/src/utils/MetaBaseObserver.ts +65 -0
- package/src/utils/async.ts +185 -0
- package/src/utils/compatibility.ts +9 -0
- package/src/utils/mutex.ts +186 -0
- package/src/utils/parseQuery.ts +31 -0
- package/src/utils/queue.ts +48 -0
- package/src/utils/stream_transform.ts +260 -0
- package/src/version.ts +3 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { FetchStrategy, LogLevels } from '@powersync/common';
|
|
2
|
+
import { AbortOperation } from '../../../utils/AbortOperation.js';
|
|
3
|
+
import { doneResult, extractBsonObjects, extractJsonLines } from '../../../utils/stream_transform.js';
|
|
4
|
+
import { EventQueue } from '../../../utils/async.js';
|
|
5
|
+
import { POWERSYNC_JS_VERSION } from '../../../version.js';
|
|
6
|
+
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
7
|
+
const webSocketPlatform = {
|
|
8
|
+
LogLevels,
|
|
9
|
+
EventQueue,
|
|
10
|
+
AbortOperation
|
|
11
|
+
};
|
|
12
|
+
export function lazyFetchImplementation(getFetch) {
|
|
13
|
+
let resolved;
|
|
14
|
+
return (request) => {
|
|
15
|
+
const fetch = (resolved ??= getFetch());
|
|
16
|
+
return fetch(request);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
export class AbstractRemote {
|
|
23
|
+
connector;
|
|
24
|
+
logger;
|
|
25
|
+
credentials = null;
|
|
26
|
+
constructor(connector, logger) {
|
|
27
|
+
this.connector = connector;
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get credentials currently cached, or fetch new credentials if none are
|
|
32
|
+
* available.
|
|
33
|
+
*
|
|
34
|
+
* These credentials may have expired already.
|
|
35
|
+
*/
|
|
36
|
+
async getCredentials() {
|
|
37
|
+
if (this.credentials) {
|
|
38
|
+
return this.credentials;
|
|
39
|
+
}
|
|
40
|
+
return this.prefetchCredentials();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Fetch a new set of credentials and cache it.
|
|
44
|
+
*
|
|
45
|
+
* Until this call succeeds, `getCredentials` will still return the
|
|
46
|
+
* old credentials.
|
|
47
|
+
*
|
|
48
|
+
* This may be called before the current credentials have expired.
|
|
49
|
+
*/
|
|
50
|
+
async prefetchCredentials() {
|
|
51
|
+
this.credentials = await this.fetchCredentials();
|
|
52
|
+
return this.credentials;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get credentials for PowerSync.
|
|
56
|
+
*
|
|
57
|
+
* This should always fetch a fresh set of credentials - don't use cached
|
|
58
|
+
* values.
|
|
59
|
+
*/
|
|
60
|
+
async fetchCredentials() {
|
|
61
|
+
const credentials = await this.connector.fetchCredentials();
|
|
62
|
+
if (credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
|
|
63
|
+
throw new Error(`A trailing forward slash "/" was found in the fetchCredentials endpoint: "${credentials.endpoint}". Remove the trailing forward slash "/" to fix this error.`);
|
|
64
|
+
}
|
|
65
|
+
return credentials;
|
|
66
|
+
}
|
|
67
|
+
/***
|
|
68
|
+
* Immediately invalidate credentials.
|
|
69
|
+
*
|
|
70
|
+
* This may be called when the current credentials have expired.
|
|
71
|
+
*/
|
|
72
|
+
invalidateCredentials() {
|
|
73
|
+
this.credentials = null;
|
|
74
|
+
this.connector.invalidateCredentials?.();
|
|
75
|
+
}
|
|
76
|
+
getUserAgent() {
|
|
77
|
+
return `powersync-js/${POWERSYNC_JS_VERSION}`;
|
|
78
|
+
}
|
|
79
|
+
async buildRequest(path) {
|
|
80
|
+
const credentials = await this.getCredentials();
|
|
81
|
+
if (credentials != null && (credentials.endpoint == null || credentials.endpoint == '')) {
|
|
82
|
+
throw new Error('PowerSync endpoint not configured');
|
|
83
|
+
}
|
|
84
|
+
else if (credentials?.token == null || credentials?.token == '') {
|
|
85
|
+
const error = new Error(`Not signed in`);
|
|
86
|
+
error.status = 401;
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
const userAgent = this.getUserAgent();
|
|
90
|
+
return {
|
|
91
|
+
url: credentials.endpoint + path,
|
|
92
|
+
headers: {
|
|
93
|
+
'content-type': 'application/json',
|
|
94
|
+
Authorization: `Token ${credentials.token}`,
|
|
95
|
+
'x-user-agent': userAgent
|
|
96
|
+
},
|
|
97
|
+
userAgent,
|
|
98
|
+
path
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
async get(path, headers) {
|
|
102
|
+
const request = await this.buildRequest(path);
|
|
103
|
+
const res = await this.fetch({
|
|
104
|
+
resource: request.url,
|
|
105
|
+
request: {
|
|
106
|
+
method: 'GET',
|
|
107
|
+
headers: {
|
|
108
|
+
...headers,
|
|
109
|
+
...request.headers
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
expectStreamingResponse: false
|
|
113
|
+
});
|
|
114
|
+
if (res.status === 401) {
|
|
115
|
+
this.invalidateCredentials();
|
|
116
|
+
}
|
|
117
|
+
if (!res.ok) {
|
|
118
|
+
throw new Error(`Received ${res.status} - ${res.statusText} when getting from ${path}: ${await res.text()}}`);
|
|
119
|
+
}
|
|
120
|
+
return res.json();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* @returns A text decoder decoding UTF-8. This is a method to allow patching it for Hermes which doesn't support the
|
|
124
|
+
* builtin, without forcing us to bundle a polyfill with `@powersync/common`.
|
|
125
|
+
*/
|
|
126
|
+
createTextDecoder() {
|
|
127
|
+
return new TextDecoder();
|
|
128
|
+
}
|
|
129
|
+
createSocket(url) {
|
|
130
|
+
return new WebSocket(url);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
134
|
+
*
|
|
135
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
136
|
+
*/
|
|
137
|
+
async socketStreamRaw(options) {
|
|
138
|
+
const support = await this.loadWebSocketSupport(webSocketPlatform);
|
|
139
|
+
const request = await this.buildRequest(options.path);
|
|
140
|
+
request.url = request.url.replace(/^https?:\/\//, function (match) {
|
|
141
|
+
return match === 'https://' ? 'wss://' : 'ws://';
|
|
142
|
+
});
|
|
143
|
+
const { fetchStrategy = FetchStrategy.Buffered, abortSignal, data } = options;
|
|
144
|
+
return await support.webSocketSyncStream({
|
|
145
|
+
remote: this,
|
|
146
|
+
buffered: fetchStrategy == FetchStrategy.Buffered,
|
|
147
|
+
abortSignal,
|
|
148
|
+
requestPayload: data,
|
|
149
|
+
request
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
154
|
+
* async iterator of byte blobs.
|
|
155
|
+
*
|
|
156
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
157
|
+
*/
|
|
158
|
+
async fetchStreamRaw(options) {
|
|
159
|
+
const { data, path, abortSignal } = options;
|
|
160
|
+
const request = await this.buildRequest(path);
|
|
161
|
+
/**
|
|
162
|
+
* This abort controller will abort pending fetch requests.
|
|
163
|
+
* If the request has resolved, it will be used to close the readable stream.
|
|
164
|
+
* Which will cancel the network request.
|
|
165
|
+
*
|
|
166
|
+
* This nested controller is required since:
|
|
167
|
+
* Aborting the active fetch request while it is being consumed seems to throw
|
|
168
|
+
* an unhandled exception on the window level.
|
|
169
|
+
*/
|
|
170
|
+
if (abortSignal.aborted) {
|
|
171
|
+
throw new AbortOperation('Abort request received before making fetchStreamRaw request');
|
|
172
|
+
}
|
|
173
|
+
const controller = new AbortController();
|
|
174
|
+
let reader = null;
|
|
175
|
+
abortSignal.addEventListener('abort', () => {
|
|
176
|
+
const reason = abortSignal.reason ??
|
|
177
|
+
new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.');
|
|
178
|
+
if (reader == null) {
|
|
179
|
+
// Only abort via the abort controller if the request has not resolved yet
|
|
180
|
+
controller.abort(reason);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
reader.cancel(reason).catch(() => {
|
|
184
|
+
// Cancelling the reader might rethrow an exception we would have handled by throwing in next(). So we can
|
|
185
|
+
// ignore it here.
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
let res;
|
|
190
|
+
let responseIsBson = false;
|
|
191
|
+
try {
|
|
192
|
+
const ndJson = 'application/x-ndjson';
|
|
193
|
+
const bson = 'application/vnd.powersync.bson-stream';
|
|
194
|
+
res = await this.fetch({
|
|
195
|
+
resource: request.url,
|
|
196
|
+
request: {
|
|
197
|
+
method: 'POST',
|
|
198
|
+
headers: {
|
|
199
|
+
...request.headers,
|
|
200
|
+
accept: `${bson};q=0.9,${ndJson};q=0.8`
|
|
201
|
+
},
|
|
202
|
+
body: JSON.stringify(data),
|
|
203
|
+
signal: controller.signal,
|
|
204
|
+
cache: 'no-store'
|
|
205
|
+
},
|
|
206
|
+
expectStreamingResponse: true
|
|
207
|
+
});
|
|
208
|
+
if (!res.ok || !res.body) {
|
|
209
|
+
const text = await res.text();
|
|
210
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
211
|
+
error.status = res.status;
|
|
212
|
+
this.logger.log({
|
|
213
|
+
level: LogLevels.error,
|
|
214
|
+
message: `Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`,
|
|
215
|
+
error
|
|
216
|
+
});
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
const contentType = res.headers.get('content-type');
|
|
220
|
+
responseIsBson = contentType == bson;
|
|
221
|
+
}
|
|
222
|
+
catch (ex) {
|
|
223
|
+
if (ex.name == 'AbortError') {
|
|
224
|
+
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
225
|
+
}
|
|
226
|
+
throw ex;
|
|
227
|
+
}
|
|
228
|
+
reader = res.body.getReader();
|
|
229
|
+
const stream = {
|
|
230
|
+
next: async () => {
|
|
231
|
+
if (controller.signal.aborted) {
|
|
232
|
+
return doneResult;
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
return await reader.read();
|
|
236
|
+
}
|
|
237
|
+
catch (ex) {
|
|
238
|
+
if (controller.signal.aborted) {
|
|
239
|
+
// .read() completes with an error if we cancel the reader, which we do to disconnect. So this is just
|
|
240
|
+
// things working as intended, we can return a done event and consider the exception handled.
|
|
241
|
+
return doneResult;
|
|
242
|
+
}
|
|
243
|
+
throw ex;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
return { isBson: responseIsBson, stream };
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Posts a `/sync/stream` request.
|
|
251
|
+
*
|
|
252
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
253
|
+
* `Uint8Array`s.
|
|
254
|
+
*/
|
|
255
|
+
async fetchStream(options) {
|
|
256
|
+
const { isBson, stream } = await this.fetchStreamRaw(options);
|
|
257
|
+
if (isBson) {
|
|
258
|
+
return extractBsonObjects(stream);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
return extractJsonLines(stream, this.createTextDecoder());
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=AbstractRemote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractRemote.js","sourceRoot":"","sources":["../../../../src/client/sync/stream/AbstractRemote.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAwB,SAAS,EAAmB,MAAM,mBAAmB,CAAC;AAEpG,OAAO,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAC;AAClE,OAAO,EACL,UAAU,EACV,kBAAkB,EAClB,gBAAgB,EAEjB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAU3D,MAAM,8BAA8B,GAAG,MAAM,CAAC;AAE9C,MAAM,iBAAiB,GAAgC;IACrD,SAAS;IACT,UAAU;IACV,cAAc;CACf,CAAC;AAgBF,MAAM,UAAU,uBAAuB,CAAC,QAAmC;IACzE,IAAI,QAA6B,CAAC;IAElC,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,KAAK,GAAG,CAAC,QAAQ,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BAC7B,CAAC;gBAAC,OAAO,EAAE,EAAE,CAAC;oBACZ,IAAI,UAAU,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;wBAC9B,sGAAsG;wBACtG,6FAA6F;wBAC7F,OAAO,UAAU,CAAC;oBACpB,CAAC;oBAED,MAAM,EAAE,CAAC;gBACX,CAAC;YACH,CAAC;SACF,CAAC;QAEF,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC;IAC5C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,WAAW,CAAC,OAA0B;QAC1C,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAC9D,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC;aAAM,CAAC;YACN,OAAO,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { BaseObserverInterface, BaseListener, Disposable, PowerSyncLogger, SyncStatus, BaseObserver } from '@powersync/common';
|
|
2
|
+
import { BucketStorageAdapter } from '../bucket/BucketStorageAdapter.js';
|
|
3
|
+
import { AbstractRemote } from './AbstractRemote.js';
|
|
4
|
+
import { CoreSyncStatus } from './core-instruction.js';
|
|
5
|
+
import { JavaScriptSyncState, SyncStatusSnapshot } from '../../../db/crud/SyncStatus.js';
|
|
6
|
+
import { ResolvedSyncOptions } from '../options.js';
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare enum LockType {
|
|
11
|
+
CRUD = "crud",
|
|
12
|
+
SYNC = "sync"
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Abstract Lock to be implemented by various JS environments
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export interface LockOptions<T> {
|
|
20
|
+
callback: () => Promise<T>;
|
|
21
|
+
type: LockType;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export interface AbstractStreamingSyncImplementationOptions {
|
|
28
|
+
adapter: BucketStorageAdapter;
|
|
29
|
+
subscriptions: SubscribedStream[];
|
|
30
|
+
uploadCrud: () => Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* An identifier for which PowerSync DB this sync implementation is
|
|
33
|
+
* linked to. Most commonly DB name, but not restricted to DB name.
|
|
34
|
+
*/
|
|
35
|
+
identifier?: string;
|
|
36
|
+
logger: PowerSyncLogger;
|
|
37
|
+
remote: AbstractRemote;
|
|
38
|
+
/**
|
|
39
|
+
* The serialized schema - mainly used to forward information about raw tables to the sync client.
|
|
40
|
+
*/
|
|
41
|
+
serializedSchema: any;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export interface StreamingSyncImplementationListener extends BaseListener {
|
|
47
|
+
/**
|
|
48
|
+
* Triggers whenever the status' members have changed in value
|
|
49
|
+
*/
|
|
50
|
+
statusChanged?: ((status: SyncStatusSnapshot) => void) | undefined;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export interface StreamingSyncImplementation extends BaseObserverInterface<StreamingSyncImplementationListener>, Disposable {
|
|
56
|
+
/**
|
|
57
|
+
* Connects to the sync service
|
|
58
|
+
*/
|
|
59
|
+
connect(options: ResolvedSyncOptions): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Disconnects from the sync services.
|
|
62
|
+
* @throws if not connected or if abort is not controlled internally
|
|
63
|
+
*/
|
|
64
|
+
disconnect(): Promise<void>;
|
|
65
|
+
getWriteCheckpoint: () => Promise<string>;
|
|
66
|
+
isConnected: boolean;
|
|
67
|
+
triggerCrudUpload: () => void;
|
|
68
|
+
waitForReady(): Promise<void>;
|
|
69
|
+
waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
|
|
70
|
+
updateSubscriptions(subscriptions: SubscribedStream[]): void;
|
|
71
|
+
markConnectionMayHaveChanged(): void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
export type SubscribedStream = {
|
|
77
|
+
name: string;
|
|
78
|
+
params: Record<string, any> | null;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
export declare abstract class AbstractStreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener> implements StreamingSyncImplementation {
|
|
84
|
+
protected options: AbstractStreamingSyncImplementationOptions;
|
|
85
|
+
protected abortController: AbortController | null;
|
|
86
|
+
protected crudUpdateListener?: () => void;
|
|
87
|
+
protected streamingSyncPromise?: Promise<[void, void]>;
|
|
88
|
+
protected logger: PowerSyncLogger;
|
|
89
|
+
private activeStreams;
|
|
90
|
+
private connectionMayHaveChanged;
|
|
91
|
+
private crudUploadNotifier;
|
|
92
|
+
private notifyCompletedUploads?;
|
|
93
|
+
private handleActiveStreamsChange?;
|
|
94
|
+
syncStatus: SyncStatusSnapshot;
|
|
95
|
+
constructor(options: AbstractStreamingSyncImplementationOptions);
|
|
96
|
+
triggerCrudUpload(): void;
|
|
97
|
+
waitForReady(): Promise<void>;
|
|
98
|
+
waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
|
|
99
|
+
get lastSyncedAt(): Date | undefined;
|
|
100
|
+
get isConnected(): boolean;
|
|
101
|
+
dispose(): Promise<void>;
|
|
102
|
+
abstract obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
|
|
103
|
+
getWriteCheckpoint(): Promise<string>;
|
|
104
|
+
private crudUploadLoop;
|
|
105
|
+
private _uploadAllCrud;
|
|
106
|
+
connect(options: ResolvedSyncOptions): Promise<void>;
|
|
107
|
+
disconnect(): Promise<void>;
|
|
108
|
+
private markAsDisconnected;
|
|
109
|
+
private streamingSync;
|
|
110
|
+
markConnectionMayHaveChanged(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Older versions of the JS SDK used to encode subkeys as JSON in `OplogEntry.toJSON`.
|
|
113
|
+
* Because subkeys are always strings, this leads to quotes being added around them in `ps_oplog`.
|
|
114
|
+
* While this is not a problem as long as it's done consistently, it causes issues when a database
|
|
115
|
+
* created by the JS SDK is used with other SDKs, or (more likely) when the new Rust sync client
|
|
116
|
+
* is enabled.
|
|
117
|
+
*
|
|
118
|
+
* So, we add a migration from the old key format (with quotes) to the new one (no quotes). The
|
|
119
|
+
* migration is only triggered when necessary (for now). The function returns whether the new format
|
|
120
|
+
* should be used, so that the JS SDK is able to write to updated databases.
|
|
121
|
+
*
|
|
122
|
+
* @param requireFixedKeyFormat - Whether we require the new format or also support the old one.
|
|
123
|
+
* The Rust client requires the new subkey format.
|
|
124
|
+
* @returns Whether the database is now using the new, fixed subkey format.
|
|
125
|
+
*/
|
|
126
|
+
private requireKeyFormat;
|
|
127
|
+
protected streamingSyncIteration(signal: AbortSignal, options: ResolvedSyncOptions): Promise<RustIterationResult | null>;
|
|
128
|
+
private receiveSyncLines;
|
|
129
|
+
private rustSyncIteration;
|
|
130
|
+
protected updateSyncStatus(core: CoreSyncStatus | null, jsState?: JavaScriptSyncState): void;
|
|
131
|
+
protected updateJsSyncState(state: JavaScriptSyncState): void;
|
|
132
|
+
private delayRetry;
|
|
133
|
+
updateSubscriptions(subscriptions: SubscribedStream[]): void;
|
|
134
|
+
}
|
|
135
|
+
interface RustIterationResult {
|
|
136
|
+
immediateRestart: boolean;
|
|
137
|
+
}
|
|
138
|
+
export {};
|