@powersync/shared-internals 1.3.1 → 1.5.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 +29 -1
- package/dist/websockets.mjs.map +1 -1
- package/dist/websockets.node.mjs +29 -1
- package/dist/websockets.node.mjs.map +1 -1
- package/lib/client/BasePowerSyncDatabase.d.ts +12 -6
- package/lib/client/BasePowerSyncDatabase.js +11 -15
- package/lib/client/BasePowerSyncDatabase.js.map +1 -1
- package/lib/client/ConnectionManager.d.ts +7 -5
- package/lib/client/ConnectionManager.js +29 -15
- package/lib/client/ConnectionManager.js.map +1 -1
- package/lib/client/InternalConnector.d.ts +9 -0
- package/lib/client/InternalConnector.js +42 -0
- package/lib/client/InternalConnector.js.map +1 -0
- package/lib/client/sync/options.d.ts +4 -0
- package/lib/client/sync/options.js +7 -1
- package/lib/client/sync/options.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +7 -9
- package/lib/client/sync/stream/AbstractRemote.js +13 -5
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +12 -14
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +147 -82
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/client/sync/stream/WebSocketSupport.js +29 -1
- package/lib/client/sync/stream/WebSocketSupport.js.map +1 -1
- package/lib/client/sync/stream/core-instruction.d.ts +28 -1
- package/lib/client/sync/stream/core-instruction.js.map +1 -1
- package/lib/client/watched/DifferentialQueryProcessor.js +5 -0
- package/lib/client/watched/DifferentialQueryProcessor.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +2 -5
- package/lib/db/crud/SyncStatus.js +5 -18
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/index.d.ts +3 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/utils/async.d.ts +6 -0
- package/lib/utils/async.js +31 -0
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/error-serialization.d.ts +27 -0
- package/lib/utils/error-serialization.js +90 -0
- package/lib/utils/error-serialization.js.map +1 -0
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +3 -3
- package/src/client/BasePowerSyncDatabase.ts +24 -19
- package/src/client/ConnectionManager.ts +32 -19
- package/src/client/InternalConnector.ts +68 -0
- package/src/client/sync/options.ts +8 -1
- package/src/client/sync/stream/AbstractRemote.ts +33 -25
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +177 -106
- package/src/client/sync/stream/WebSocketSupport.ts +32 -1
- package/src/client/sync/stream/core-instruction.ts +25 -2
- package/src/client/watched/DifferentialQueryProcessor.ts +5 -0
- package/src/db/crud/SyncStatus.ts +6 -19
- package/src/index.ts +3 -0
- package/src/utils/async.ts +36 -0
- package/src/utils/error-serialization.ts +116 -0
- package/src/version.ts +1 -1
|
@@ -33,7 +33,9 @@ import {
|
|
|
33
33
|
BaseObserver,
|
|
34
34
|
SqliteRecord,
|
|
35
35
|
SyncStreamConnectionMethod,
|
|
36
|
-
CheckpointRequest
|
|
36
|
+
CheckpointRequest,
|
|
37
|
+
DownloadOptions,
|
|
38
|
+
UploadOptions
|
|
37
39
|
} from '@powersync/common';
|
|
38
40
|
import { BucketStorageAdapter, PSInternalTable } from './sync/bucket/BucketStorageAdapter.js';
|
|
39
41
|
import { SyncStatusSnapshot } from '../db/crud/SyncStatus.js';
|
|
@@ -57,13 +59,13 @@ import { DEFAULT_WATCH_THROTTLE_MS } from './watched/WatchedQuery.js';
|
|
|
57
59
|
import { CustomQuery } from './CustomQuery.js';
|
|
58
60
|
import { MEMORY_TRIGGER_CLAIM_MANAGER } from './triggers/MemoryTriggerClaimManager.js';
|
|
59
61
|
import { symbolAsyncIterator } from '../utils/compatibility.js';
|
|
60
|
-
import { MAX_OP_ID } from '../constants.js';
|
|
61
62
|
import { SqliteBucketStorage } from './sync/bucket/SqliteBucketStorage.js';
|
|
62
63
|
import {
|
|
63
64
|
cannotRequestDueToDisconnectedError,
|
|
64
65
|
checkpointRequestsNotEnabledError
|
|
65
66
|
} from './sync/stream/CheckpointState.js';
|
|
66
67
|
import { CheckpointRequestImpl } from './sync/CheckpointRequestImpl.js';
|
|
68
|
+
import { InternalConnector, normalizeConnectCall } from './InternalConnector.js';
|
|
67
69
|
|
|
68
70
|
const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
|
|
69
71
|
|
|
@@ -220,6 +222,14 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
220
222
|
return SyncStreamConnectionMethod.HTTP;
|
|
221
223
|
}
|
|
222
224
|
|
|
225
|
+
/**
|
|
226
|
+
* The name of the SDK package this database belongs to, e.g. `@powersync/web`. Development tooling
|
|
227
|
+
* shows it to identify the app. SDKs override it with their package name.
|
|
228
|
+
*/
|
|
229
|
+
get sdkName(): string {
|
|
230
|
+
return 'powersync-js';
|
|
231
|
+
}
|
|
232
|
+
|
|
223
233
|
get schema() {
|
|
224
234
|
return this._schema;
|
|
225
235
|
}
|
|
@@ -260,27 +270,15 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
260
270
|
}
|
|
261
271
|
|
|
262
272
|
protected abstract generateSyncStreamImplementation(
|
|
263
|
-
connector:
|
|
273
|
+
connector: InternalConnector,
|
|
264
274
|
options: CreateSyncImplementationOptions
|
|
265
275
|
): StreamingSyncImplementation;
|
|
266
276
|
|
|
267
|
-
protected commonSyncOptions(connector:
|
|
277
|
+
protected commonSyncOptions(connector: InternalConnector, options: CreateSyncImplementationOptions) {
|
|
268
278
|
return {
|
|
269
279
|
...options,
|
|
270
280
|
adapter: this.bucketStorageAdapter,
|
|
271
|
-
|
|
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
|
-
},
|
|
281
|
+
connector,
|
|
284
282
|
identifier: this.database.name,
|
|
285
283
|
logger: this.logger
|
|
286
284
|
} satisfies Partial<AbstractStreamingSyncImplementationOptions>;
|
|
@@ -443,8 +441,15 @@ export abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabas
|
|
|
443
441
|
return this.runExclusiveMutex.runExclusive(callback);
|
|
444
442
|
}
|
|
445
443
|
|
|
446
|
-
|
|
447
|
-
|
|
444
|
+
connect(connector: PowerSyncBackendConnector, options?: SyncOptions): Promise<void>;
|
|
445
|
+
connect(options: SyncOptions & (DownloadOptions | UploadOptions)): Promise<void>;
|
|
446
|
+
|
|
447
|
+
async connect(
|
|
448
|
+
connector: PowerSyncBackendConnector | (SyncOptions & (DownloadOptions | UploadOptions)),
|
|
449
|
+
options?: SyncOptions
|
|
450
|
+
) {
|
|
451
|
+
const [resolvedConnector, resolvedOptions] = normalizeConnectCall(this, connector, options);
|
|
452
|
+
return this.connectionManager.connect(resolvedConnector, resolvedOptions, this.schema.toJSON());
|
|
448
453
|
}
|
|
449
454
|
|
|
450
455
|
async disconnect() {
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
PowerSyncLogger,
|
|
5
5
|
SyncStatus,
|
|
6
6
|
BaseListener,
|
|
7
|
-
PowerSyncBackendConnector,
|
|
8
7
|
SyncStream,
|
|
9
8
|
SyncStreamDescription,
|
|
10
9
|
SyncStreamSubscribeOptions,
|
|
@@ -15,6 +14,7 @@ import {
|
|
|
15
14
|
|
|
16
15
|
import { StreamingSyncImplementation, SubscribedStream } from './sync/stream/AbstractStreamingSyncImplementation.js';
|
|
17
16
|
import { ResolvedSyncOptions, resolveSyncOptions } from './sync/options.js';
|
|
17
|
+
import { InternalConnector } from './InternalConnector.js';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* @internal
|
|
@@ -52,7 +52,7 @@ export interface InternalSubscriptionAdapter {
|
|
|
52
52
|
*/
|
|
53
53
|
export interface ConnectionManagerOptions {
|
|
54
54
|
createSyncImplementation(
|
|
55
|
-
connector:
|
|
55
|
+
connector: InternalConnector,
|
|
56
56
|
options: CreateSyncImplementationOptions
|
|
57
57
|
): Promise<ConnectionManagerSyncImplementationResult>;
|
|
58
58
|
readonly defaultConnectionMethod: SyncStreamConnectionMethod;
|
|
@@ -61,7 +61,7 @@ export interface ConnectionManagerOptions {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
type StoredConnectionOptions = {
|
|
64
|
-
connector:
|
|
64
|
+
connector: InternalConnector;
|
|
65
65
|
options: ResolvedSyncOptions;
|
|
66
66
|
schema: any;
|
|
67
67
|
};
|
|
@@ -102,6 +102,10 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
102
102
|
|
|
103
103
|
private currentOptions: ResolvedSyncOptions | null;
|
|
104
104
|
|
|
105
|
+
// The connector of the active connection. Retained alongside currentOptions so `connector` reflects
|
|
106
|
+
// the live connection, not just the brief pending-options window before connecting starts.
|
|
107
|
+
private currentConnector: InternalConnector | null;
|
|
108
|
+
|
|
105
109
|
syncStreamImplementation: StreamingSyncImplementation | null;
|
|
106
110
|
|
|
107
111
|
/**
|
|
@@ -125,11 +129,12 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
125
129
|
this.pendingConnectionOptions = null;
|
|
126
130
|
this.syncStreamImplementation = null;
|
|
127
131
|
this.currentOptions = null;
|
|
132
|
+
this.currentConnector = null;
|
|
128
133
|
this.syncDisposer = null;
|
|
129
134
|
}
|
|
130
135
|
|
|
131
136
|
get connector() {
|
|
132
|
-
return this.pendingConnectionOptions?.connector ?? null;
|
|
137
|
+
return this.currentConnector ?? this.pendingConnectionOptions?.connector ?? null;
|
|
133
138
|
}
|
|
134
139
|
|
|
135
140
|
get connectionOptions() {
|
|
@@ -145,17 +150,18 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
145
150
|
await this.syncDisposer?.();
|
|
146
151
|
}
|
|
147
152
|
|
|
148
|
-
async connect(connector:
|
|
153
|
+
async connect(connector: InternalConnector, options: SyncOptions, serializedSchema: any) {
|
|
149
154
|
// Keep track if there were pending operations before this call
|
|
150
155
|
const hadPendingOptions = !!this.pendingConnectionOptions;
|
|
151
156
|
|
|
152
157
|
// Update pending options to the latest values
|
|
153
158
|
const resolvedOptions = resolveSyncOptions(options, this.options.defaultConnectionMethod);
|
|
154
|
-
|
|
159
|
+
const pendingOptions: StoredConnectionOptions = {
|
|
155
160
|
connector,
|
|
156
161
|
options: resolvedOptions,
|
|
157
162
|
schema: serializedSchema
|
|
158
163
|
};
|
|
164
|
+
this.pendingConnectionOptions = pendingOptions;
|
|
159
165
|
|
|
160
166
|
if (connector.postCheckpointRequest && resolvedOptions.checkpointMode == 'legacy') {
|
|
161
167
|
this.logger.log({
|
|
@@ -182,10 +188,7 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
182
188
|
if (this.pendingConnectionOptions) {
|
|
183
189
|
// Pending options have been placed while connecting.
|
|
184
190
|
// Need to reconnect.
|
|
185
|
-
this.connectingPromise = this.connectInternal()
|
|
186
|
-
.catch(() => {})
|
|
187
|
-
.finally(checkConnection);
|
|
188
|
-
return this.connectingPromise;
|
|
191
|
+
return (this.connectingPromise = this.connectInternal().finally(checkConnection));
|
|
189
192
|
} else {
|
|
190
193
|
// Clear the connecting promise, done.
|
|
191
194
|
this.connectingPromise = null;
|
|
@@ -193,10 +196,7 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
193
196
|
}
|
|
194
197
|
};
|
|
195
198
|
|
|
196
|
-
this.connectingPromise ??= this.connectInternal()
|
|
197
|
-
.catch(() => {})
|
|
198
|
-
.finally(checkConnection);
|
|
199
|
-
return this.connectingPromise;
|
|
199
|
+
return (this.connectingPromise ??= this.connectInternal().finally(checkConnection));
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
protected async connectInternal() {
|
|
@@ -212,6 +212,7 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
212
212
|
* The promise tracks the creation which is used to synchronize disconnect attempts.
|
|
213
213
|
*/
|
|
214
214
|
this.syncStreamInitPromise = new Promise(async (resolve, reject) => {
|
|
215
|
+
let consumedOptions: StoredConnectionOptions | null = null;
|
|
215
216
|
try {
|
|
216
217
|
if (!this.pendingConnectionOptions) {
|
|
217
218
|
this.logger.log({
|
|
@@ -228,9 +229,11 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
228
229
|
return;
|
|
229
230
|
}
|
|
230
231
|
|
|
231
|
-
|
|
232
|
+
consumedOptions = this.pendingConnectionOptions;
|
|
233
|
+
const { connector, options, schema } = consumedOptions;
|
|
232
234
|
appliedOptions = options;
|
|
233
235
|
this.currentOptions = options;
|
|
236
|
+
this.currentConnector = connector;
|
|
234
237
|
|
|
235
238
|
this.pendingConnectionOptions = null;
|
|
236
239
|
|
|
@@ -254,8 +257,13 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
254
257
|
}
|
|
255
258
|
});
|
|
256
259
|
|
|
257
|
-
|
|
258
|
-
|
|
260
|
+
try {
|
|
261
|
+
await this.syncStreamInitPromise;
|
|
262
|
+
} finally {
|
|
263
|
+
// Also cleared when the creation fails: performDisconnect() awaits this promise, so a rejected one
|
|
264
|
+
// left behind would fail every later disconnect.
|
|
265
|
+
this.syncStreamInitPromise = null;
|
|
266
|
+
}
|
|
259
267
|
|
|
260
268
|
if (!appliedOptions) {
|
|
261
269
|
// A disconnect could have cleared the options which did not create a syncStreamImplementation
|
|
@@ -279,6 +287,7 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
279
287
|
// This will help abort pending connects
|
|
280
288
|
this.pendingConnectionOptions = null;
|
|
281
289
|
this.currentOptions = null;
|
|
290
|
+
this.currentConnector = null;
|
|
282
291
|
await this.disconnectInternal();
|
|
283
292
|
}
|
|
284
293
|
|
|
@@ -290,8 +299,12 @@ export class ConnectionManager extends BaseObserver<ConnectionManagerListener> {
|
|
|
290
299
|
|
|
291
300
|
this.disconnectingPromise = this.performDisconnect();
|
|
292
301
|
|
|
293
|
-
|
|
294
|
-
|
|
302
|
+
try {
|
|
303
|
+
await this.disconnectingPromise;
|
|
304
|
+
} finally {
|
|
305
|
+
// Also cleared when the disconnect fails, so the next one runs instead of returning this rejection.
|
|
306
|
+
this.disconnectingPromise = null;
|
|
307
|
+
}
|
|
295
308
|
}
|
|
296
309
|
|
|
297
310
|
protected async performDisconnect() {
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DownloadOptions,
|
|
3
|
+
PowerSyncBackendConnector,
|
|
4
|
+
PowerSyncCredentials,
|
|
5
|
+
SyncOptions,
|
|
6
|
+
UploadOptions
|
|
7
|
+
} from '@powersync/common';
|
|
8
|
+
import { BasePowerSyncDatabase } from './BasePowerSyncDatabase.js';
|
|
9
|
+
|
|
10
|
+
export interface InternalConnector {
|
|
11
|
+
fetchCredentials?: (signal?: AbortSignal) => Promise<PowerSyncCredentials | null>;
|
|
12
|
+
invalidateCredentials?: () => void | Promise<void>;
|
|
13
|
+
postCheckpointRequest?: (clientId: string, requestId: string, signal?: AbortSignal) => Promise<string | null> | null;
|
|
14
|
+
uploadCrud?: (signal?: AbortSignal) => Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function normalizeConnectCall(
|
|
18
|
+
database: BasePowerSyncDatabase,
|
|
19
|
+
connector: PowerSyncBackendConnector | (SyncOptions & (DownloadOptions | UploadOptions)),
|
|
20
|
+
options?: SyncOptions
|
|
21
|
+
): [InternalConnector, SyncOptions] {
|
|
22
|
+
const internalConnector: InternalConnector = {};
|
|
23
|
+
let resolvedOptions: SyncOptions | undefined;
|
|
24
|
+
|
|
25
|
+
if ('fetchCredentials' in connector) {
|
|
26
|
+
// This is an old-style PowerSyncBackendConnector
|
|
27
|
+
resolvedOptions = options;
|
|
28
|
+
|
|
29
|
+
internalConnector.fetchCredentials = connector.fetchCredentials.bind(connector);
|
|
30
|
+
internalConnector.uploadCrud = () => {
|
|
31
|
+
return connector.uploadData(database);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const customCheckpoints = connector.postCheckpointRequest;
|
|
35
|
+
if (customCheckpoints) {
|
|
36
|
+
internalConnector.postCheckpointRequest = customCheckpoints.bind(connector);
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
resolvedOptions = connector;
|
|
40
|
+
|
|
41
|
+
if ('authenticator' in connector) {
|
|
42
|
+
const auth = connector.authenticator;
|
|
43
|
+
const endpoint = connector.powerSyncEndpoint;
|
|
44
|
+
|
|
45
|
+
internalConnector.fetchCredentials = async (signal) => {
|
|
46
|
+
const token = await auth.resolveCredentials({ abort: signal });
|
|
47
|
+
return { endpoint, token };
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const customCheckpoints = auth.postCheckpointRequest;
|
|
51
|
+
if (customCheckpoints) {
|
|
52
|
+
internalConnector.postCheckpointRequest = customCheckpoints.bind(auth);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const invalidateCredentials = auth.invalidateCredentials;
|
|
56
|
+
if (invalidateCredentials) {
|
|
57
|
+
internalConnector.invalidateCredentials = invalidateCredentials.bind(auth);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if ('upload' in connector) {
|
|
61
|
+
internalConnector.uploadCrud = (signal) => {
|
|
62
|
+
return connector.upload({ database, abort: signal });
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return [internalConnector, resolvedOptions ?? {}];
|
|
68
|
+
}
|
|
@@ -5,6 +5,11 @@ import { FetchStrategy, SyncOptions, SyncStreamConnectionMethod } from '@powersy
|
|
|
5
5
|
*/
|
|
6
6
|
export type ResolvedSyncOptions = Required<SyncOptions>;
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_CONNECT_TIMEOUT_MS = 30_000;
|
|
12
|
+
|
|
8
13
|
/**
|
|
9
14
|
* @internal
|
|
10
15
|
*/
|
|
@@ -20,6 +25,8 @@ export function resolveSyncOptions(
|
|
|
20
25
|
includeDefaultStreams: options.includeDefaultStreams ?? true,
|
|
21
26
|
retryDelayMs: options.retryDelayMs ?? 5000,
|
|
22
27
|
crudUploadThrottleMs: options.crudUploadThrottleMs ?? 1000,
|
|
23
|
-
checkpointMode: options.checkpointMode ?? 'legacy'
|
|
28
|
+
checkpointMode: options.checkpointMode ?? 'legacy',
|
|
29
|
+
connectTimeoutMs: options.connectTimeoutMs ?? DEFAULT_CONNECT_TIMEOUT_MS,
|
|
30
|
+
diagnostics: options.diagnostics ?? false
|
|
24
31
|
};
|
|
25
32
|
}
|
|
@@ -9,16 +9,9 @@ import {
|
|
|
9
9
|
SimpleAsyncIterator
|
|
10
10
|
} from '../../../utils/stream_transform.js';
|
|
11
11
|
import { WebSocketSupport, WebSocketSyncStreamPlatform } from './WebSocketSupport.js';
|
|
12
|
-
import { EventQueue } from '../../../utils/async.js';
|
|
12
|
+
import { EventQueue, runWithTimeout } from '../../../utils/async.js';
|
|
13
13
|
import { POWERSYNC_JS_VERSION } from '../../../version.js';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @internal
|
|
17
|
-
*/
|
|
18
|
-
export type RemoteConnector = {
|
|
19
|
-
fetchCredentials: (signal?: AbortSignal) => Promise<PowerSyncCredentials | null>;
|
|
20
|
-
invalidateCredentials?: () => void;
|
|
21
|
-
};
|
|
14
|
+
import { InternalConnector } from '../../InternalConnector.js';
|
|
22
15
|
|
|
23
16
|
const POWERSYNC_TRAILING_SLASH_MATCH = /\/+$/;
|
|
24
17
|
|
|
@@ -35,6 +28,10 @@ export type SyncStreamOptions = {
|
|
|
35
28
|
path: string;
|
|
36
29
|
data: unknown;
|
|
37
30
|
abortSignal: AbortSignal;
|
|
31
|
+
/**
|
|
32
|
+
* How long to wait for the response to start. Without it, only {@link abortSignal} ends a pending request.
|
|
33
|
+
*/
|
|
34
|
+
connectTimeoutMs?: number;
|
|
38
35
|
};
|
|
39
36
|
|
|
40
37
|
/**
|
|
@@ -81,7 +78,7 @@ export abstract class AbstractRemote {
|
|
|
81
78
|
protected credentials: PowerSyncCredentials | null = null;
|
|
82
79
|
|
|
83
80
|
constructor(
|
|
84
|
-
protected connector:
|
|
81
|
+
protected connector: InternalConnector,
|
|
85
82
|
readonly logger: PowerSyncLogger
|
|
86
83
|
) {}
|
|
87
84
|
|
|
@@ -125,7 +122,12 @@ export abstract class AbstractRemote {
|
|
|
125
122
|
* values.
|
|
126
123
|
*/
|
|
127
124
|
async fetchCredentials(signal?: AbortSignal) {
|
|
128
|
-
const
|
|
125
|
+
const fetchCredentials = this.connector.fetchCredentials;
|
|
126
|
+
if (!fetchCredentials) {
|
|
127
|
+
throw new Error('No connector configured for downloads');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const credentials = await fetchCredentials(signal);
|
|
129
131
|
if (credentials?.endpoint.match(POWERSYNC_TRAILING_SLASH_MATCH)) {
|
|
130
132
|
throw new Error(
|
|
131
133
|
`A trailing forward slash "/" was found in the fetchCredentials endpoint: "${credentials.endpoint}". Remove the trailing forward slash "/" to fix this error.`
|
|
@@ -264,7 +266,7 @@ export abstract class AbstractRemote {
|
|
|
264
266
|
protected async fetchStreamRaw(
|
|
265
267
|
options: SyncStreamOptions
|
|
266
268
|
): Promise<{ isBson: boolean; stream: SimpleAsyncIterator<Uint8Array> }> {
|
|
267
|
-
const { data, path, abortSignal } = options;
|
|
269
|
+
const { data, path, abortSignal, connectTimeoutMs } = options;
|
|
268
270
|
const request = await this.buildRequest(path, abortSignal);
|
|
269
271
|
|
|
270
272
|
/**
|
|
@@ -304,20 +306,26 @@ export abstract class AbstractRemote {
|
|
|
304
306
|
const ndJson = 'application/x-ndjson';
|
|
305
307
|
const bson = 'application/vnd.powersync.bson-stream';
|
|
306
308
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
309
|
+
const postStream = (signal: AbortSignal) =>
|
|
310
|
+
this.fetch({
|
|
311
|
+
resource: request.url,
|
|
312
|
+
request: {
|
|
313
|
+
method: 'POST',
|
|
314
|
+
headers: {
|
|
315
|
+
...request.headers,
|
|
316
|
+
accept: `${bson};q=0.9,${ndJson};q=0.8`
|
|
317
|
+
},
|
|
318
|
+
body: JSON.stringify(data),
|
|
319
|
+
signal,
|
|
320
|
+
cache: 'no-store'
|
|
314
321
|
},
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
322
|
+
expectStreamingResponse: true
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// The timeout ends once the response starts, so it never aborts the response body.
|
|
326
|
+
res = await (connectTimeoutMs == null
|
|
327
|
+
? postStream(controller.signal)
|
|
328
|
+
: runWithTimeout(controller.signal, connectTimeoutMs, 'Sync stream connection', postStream));
|
|
321
329
|
|
|
322
330
|
if (!res.ok || !res.body) {
|
|
323
331
|
const text = await res.text();
|