@powersync/common 1.10.0 → 1.11.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.
|
@@ -16,6 +16,16 @@ export type SyncStreamOptions = {
|
|
|
16
16
|
abortSignal?: AbortSignal;
|
|
17
17
|
fetchOptions?: Request;
|
|
18
18
|
};
|
|
19
|
+
export type FetchImplementation = typeof fetch;
|
|
20
|
+
/**
|
|
21
|
+
* Class wrapper for providing a fetch implementation.
|
|
22
|
+
* The class wrapper is used to distinguish the fetchImplementation
|
|
23
|
+
* option in [AbstractRemoteOptions] from the general fetch method
|
|
24
|
+
* which is typeof "function"
|
|
25
|
+
*/
|
|
26
|
+
export declare class FetchImplementationProvider {
|
|
27
|
+
getFetch(): FetchImplementation;
|
|
28
|
+
}
|
|
19
29
|
export type AbstractRemoteOptions = {
|
|
20
30
|
/**
|
|
21
31
|
* Transforms the PowerSync base URL which might contain
|
|
@@ -28,7 +38,7 @@ export type AbstractRemoteOptions = {
|
|
|
28
38
|
* Note that this usually needs to be bound to the global scope.
|
|
29
39
|
* Binding should be done before passing here.
|
|
30
40
|
*/
|
|
31
|
-
fetchImplementation:
|
|
41
|
+
fetchImplementation: FetchImplementation | FetchImplementationProvider;
|
|
32
42
|
};
|
|
33
43
|
export declare const DEFAULT_REMOTE_OPTIONS: AbstractRemoteOptions;
|
|
34
44
|
export declare abstract class AbstractRemote {
|
|
@@ -37,7 +47,11 @@ export declare abstract class AbstractRemote {
|
|
|
37
47
|
protected credentials: PowerSyncCredentials | null;
|
|
38
48
|
protected options: AbstractRemoteOptions;
|
|
39
49
|
constructor(connector: RemoteConnector, logger?: ILogger, options?: Partial<AbstractRemoteOptions>);
|
|
40
|
-
|
|
50
|
+
/**
|
|
51
|
+
* @returns a fetch implementation (function)
|
|
52
|
+
* which can be called to perform fetch requests
|
|
53
|
+
*/
|
|
54
|
+
get fetch(): FetchImplementation;
|
|
41
55
|
getCredentials(): Promise<PowerSyncCredentials | null>;
|
|
42
56
|
protected buildRequest(path: string): Promise<{
|
|
43
57
|
url: string;
|
|
@@ -15,11 +15,22 @@ const KEEP_ALIVE_MS = 20_000;
|
|
|
15
15
|
// The ACK must be received in this period
|
|
16
16
|
const KEEP_ALIVE_LIFETIME_MS = 30_000;
|
|
17
17
|
export const DEFAULT_REMOTE_LOGGER = Logger.get('PowerSyncRemote');
|
|
18
|
+
/**
|
|
19
|
+
* Class wrapper for providing a fetch implementation.
|
|
20
|
+
* The class wrapper is used to distinguish the fetchImplementation
|
|
21
|
+
* option in [AbstractRemoteOptions] from the general fetch method
|
|
22
|
+
* which is typeof "function"
|
|
23
|
+
*/
|
|
24
|
+
export class FetchImplementationProvider {
|
|
25
|
+
getFetch() {
|
|
26
|
+
return fetch.bind(globalThis);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
18
29
|
export const DEFAULT_REMOTE_OPTIONS = {
|
|
19
30
|
socketUrlTransformer: (url) => url.replace(/^https?:\/\//, function (match) {
|
|
20
31
|
return match === 'https://' ? 'wss://' : 'ws://';
|
|
21
32
|
}),
|
|
22
|
-
fetchImplementation:
|
|
33
|
+
fetchImplementation: new FetchImplementationProvider()
|
|
23
34
|
};
|
|
24
35
|
export class AbstractRemote {
|
|
25
36
|
connector;
|
|
@@ -34,8 +45,15 @@ export class AbstractRemote {
|
|
|
34
45
|
...(options ?? {})
|
|
35
46
|
};
|
|
36
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* @returns a fetch implementation (function)
|
|
50
|
+
* which can be called to perform fetch requests
|
|
51
|
+
*/
|
|
37
52
|
get fetch() {
|
|
38
|
-
|
|
53
|
+
const { fetchImplementation } = this.options;
|
|
54
|
+
return fetchImplementation instanceof FetchImplementationProvider
|
|
55
|
+
? fetchImplementation.getFetch()
|
|
56
|
+
: fetchImplementation;
|
|
39
57
|
}
|
|
40
58
|
async getCredentials() {
|
|
41
59
|
const { expiresAt } = this.credentials ?? {};
|
|
@@ -116,13 +116,16 @@ export class AbstractStreamingSyncImplementation extends BaseObserver {
|
|
|
116
116
|
}
|
|
117
117
|
catch (ex) {
|
|
118
118
|
this.updateSyncStatus({
|
|
119
|
-
connected: false,
|
|
120
119
|
dataFlow: {
|
|
121
120
|
uploading: false
|
|
122
121
|
}
|
|
123
122
|
});
|
|
124
123
|
await this.delayRetry();
|
|
125
|
-
|
|
124
|
+
if (!this.isConnected) {
|
|
125
|
+
// Exit the upload loop if the sync stream is no longer connected
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
this.logger.debug(`Caught exception when uploading. Upload will retry after a delay. Exception: ${ex.message}`);
|
|
126
129
|
}
|
|
127
130
|
finally {
|
|
128
131
|
this.updateSyncStatus({
|