@powersync/shared-internals 0.0.0-dev-20260827080125
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/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 +156 -0
- package/lib/client/BasePowerSyncDatabase.js +654 -0
- package/lib/client/BasePowerSyncDatabase.js.map +1 -0
- package/lib/client/ConnectionManager.d.ts +120 -0
- package/lib/client/ConnectionManager.js +324 -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/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 +58 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +47 -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 +32 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +160 -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 +17 -0
- package/lib/client/sync/options.js.map +1 -0
- package/lib/client/sync/stream/AbstractRemote.d.ts +131 -0
- package/lib/client/sync/stream/AbstractRemote.js +270 -0
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +156 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +740 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -0
- package/lib/client/sync/stream/CheckpointState.d.ts +44 -0
- package/lib/client/sync/stream/CheckpointState.js +106 -0
- package/lib/client/sync/stream/CheckpointState.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 +81 -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 +58 -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 +42 -0
- package/lib/utils/stream_transform.js +227 -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 +68 -0
- package/src/client/BasePowerSyncDatabase.ts +898 -0
- package/src/client/ConnectionManager.ts +441 -0
- package/src/client/CustomQuery.ts +60 -0
- package/src/client/sync/CheckpointRequestImpl.ts +54 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +85 -0
- package/src/client/sync/bucket/CrudEntry.ts +116 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +203 -0
- package/src/client/sync/options.ts +25 -0
- package/src/client/sync/stream/AbstractRemote.ts +388 -0
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +948 -0
- package/src/client/sync/stream/CheckpointState.ts +136 -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 +85 -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 +81 -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 +276 -0
- package/src/version.ts +3 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BaseListener, BaseObserverInterface, Disposable, CrudBatch, CrudEntry, Transaction, SqliteValue } from '@powersync/common';
|
|
2
|
+
export declare enum PSInternalTable {
|
|
3
|
+
DATA = "ps_data",
|
|
4
|
+
CRUD = "ps_crud",
|
|
5
|
+
BUCKETS = "ps_buckets",
|
|
6
|
+
OPLOG = "ps_oplog",
|
|
7
|
+
UNTYPED = "ps_untyped"
|
|
8
|
+
}
|
|
9
|
+
export declare enum PowerSyncControlCommand {
|
|
10
|
+
PROCESS_TEXT_LINE = "line_text",
|
|
11
|
+
PROCESS_BSON_LINE = "line_binary",
|
|
12
|
+
STOP = "stop",
|
|
13
|
+
START = "start",
|
|
14
|
+
NOTIFY_TOKEN_REFRESHED = "refreshed_token",
|
|
15
|
+
NOTIFY_CRUD_UPLOAD_COMPLETED = "completed_upload",
|
|
16
|
+
UPDATE_SUBSCRIPTIONS = "update_subscriptions",
|
|
17
|
+
/**
|
|
18
|
+
* An `established` or `end` event for response streams.
|
|
19
|
+
*/
|
|
20
|
+
CONNECTION_STATE = "connection"
|
|
21
|
+
}
|
|
22
|
+
export interface BucketStorageListener extends BaseListener {
|
|
23
|
+
crudUpdate: () => void;
|
|
24
|
+
}
|
|
25
|
+
export interface BucketStorageAdapter extends BaseObserverInterface<BucketStorageListener>, Disposable {
|
|
26
|
+
hasMigratedSubkeys(): Promise<boolean>;
|
|
27
|
+
migrateToFixedSubkeys(): Promise<void>;
|
|
28
|
+
nextCrudItem(): Promise<CrudEntry | undefined>;
|
|
29
|
+
hasCrud(): Promise<boolean>;
|
|
30
|
+
getCrudBatch(limit?: number): Promise<CrudBatch | null>;
|
|
31
|
+
updateLocalTarget(cb: () => Promise<string>): Promise<boolean>;
|
|
32
|
+
handleCrudCheckpoint(lastClientId: number, writeCheckpoint?: string): Promise<void>;
|
|
33
|
+
readCheckpointRequestId(variant: 'next' | 'current' | 'seed', payload?: string): Promise<string>;
|
|
34
|
+
/**
|
|
35
|
+
* Get an unique client id.
|
|
36
|
+
*/
|
|
37
|
+
getClientId(): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Invokes the `powersync_control` function for the sync client.
|
|
40
|
+
*/
|
|
41
|
+
control(op: PowerSyncControlCommand, payload: string | Uint8Array | null): Promise<string>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Invokes `powersync_control` from the core extension, casting the result to text.
|
|
45
|
+
*/
|
|
46
|
+
export declare function rawPowerSyncControl(tx: Transaction, op: string, payload: SqliteValue): Promise<string | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Reads the current target checkpoint request id, or updates it when the update parameter is set.
|
|
49
|
+
*
|
|
50
|
+
* After requesting a checkpoint, the service needs to include that checkpoint id (or a subsequent one) in a
|
|
51
|
+
* `checkpoint_complete` message before we apply any subsequent sync lines. This guards against uploaded changes that
|
|
52
|
+
* have not yet been synced to flicker if we apply an intermediate checkpoint.
|
|
53
|
+
*
|
|
54
|
+
* @param update An optional value to update the stored target checkpoint request. {@link MAX_OP_ID} can be used as a
|
|
55
|
+
* sentinel for pending changes that have been uploaded, but for which no checkpoint request has been created yet.
|
|
56
|
+
* @returns The previous checkpoint request.
|
|
57
|
+
*/
|
|
58
|
+
export declare function targetCheckpointRequestId(tx: Transaction, update?: string | null): Promise<string | null>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export var PSInternalTable;
|
|
2
|
+
(function (PSInternalTable) {
|
|
3
|
+
PSInternalTable["DATA"] = "ps_data";
|
|
4
|
+
PSInternalTable["CRUD"] = "ps_crud";
|
|
5
|
+
PSInternalTable["BUCKETS"] = "ps_buckets";
|
|
6
|
+
PSInternalTable["OPLOG"] = "ps_oplog";
|
|
7
|
+
PSInternalTable["UNTYPED"] = "ps_untyped";
|
|
8
|
+
})(PSInternalTable || (PSInternalTable = {}));
|
|
9
|
+
export var PowerSyncControlCommand;
|
|
10
|
+
(function (PowerSyncControlCommand) {
|
|
11
|
+
PowerSyncControlCommand["PROCESS_TEXT_LINE"] = "line_text";
|
|
12
|
+
PowerSyncControlCommand["PROCESS_BSON_LINE"] = "line_binary";
|
|
13
|
+
PowerSyncControlCommand["STOP"] = "stop";
|
|
14
|
+
PowerSyncControlCommand["START"] = "start";
|
|
15
|
+
PowerSyncControlCommand["NOTIFY_TOKEN_REFRESHED"] = "refreshed_token";
|
|
16
|
+
PowerSyncControlCommand["NOTIFY_CRUD_UPLOAD_COMPLETED"] = "completed_upload";
|
|
17
|
+
PowerSyncControlCommand["UPDATE_SUBSCRIPTIONS"] = "update_subscriptions";
|
|
18
|
+
/**
|
|
19
|
+
* An `established` or `end` event for response streams.
|
|
20
|
+
*/
|
|
21
|
+
PowerSyncControlCommand["CONNECTION_STATE"] = "connection";
|
|
22
|
+
})(PowerSyncControlCommand || (PowerSyncControlCommand = {}));
|
|
23
|
+
/**
|
|
24
|
+
* Invokes `powersync_control` from the core extension, casting the result to text.
|
|
25
|
+
*/
|
|
26
|
+
export async function rawPowerSyncControl(tx, op, payload) {
|
|
27
|
+
// For some calls, notably on target_checkpoint_request_id, powersync_control returns a 64-bit integer we want to
|
|
28
|
+
// represent as text. Instead of dealing with bigints which may or may not be supported across different sqlite
|
|
29
|
+
// libraries, play it safe and cast to text before mapping to JavaScript.
|
|
30
|
+
const { rawRows } = await tx.executeRaw('SELECT CAST(powersync_control(?, ?) AS TEXT)', [op, payload]);
|
|
31
|
+
return rawRows[0][0];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Reads the current target checkpoint request id, or updates it when the update parameter is set.
|
|
35
|
+
*
|
|
36
|
+
* After requesting a checkpoint, the service needs to include that checkpoint id (or a subsequent one) in a
|
|
37
|
+
* `checkpoint_complete` message before we apply any subsequent sync lines. This guards against uploaded changes that
|
|
38
|
+
* have not yet been synced to flicker if we apply an intermediate checkpoint.
|
|
39
|
+
*
|
|
40
|
+
* @param update An optional value to update the stored target checkpoint request. {@link MAX_OP_ID} can be used as a
|
|
41
|
+
* sentinel for pending changes that have been uploaded, but for which no checkpoint request has been created yet.
|
|
42
|
+
* @returns The previous checkpoint request.
|
|
43
|
+
*/
|
|
44
|
+
export function targetCheckpointRequestId(tx, update = null) {
|
|
45
|
+
return rawPowerSyncControl(tx, 'target_checkpoint_request_id', update);
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=BucketStorageAdapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BucketStorageAdapter.js","sourceRoot":"","sources":["../../../../src/client/sync/bucket/BucketStorageAdapter.ts"],"names":[],"mappings":"AAWA,MAAM,CAAN,IAAY,eAMX;AAND,WAAY,eAAe;IACzB,mCAAgB,CAAA;IAChB,mCAAgB,CAAA;IAChB,yCAAsB,CAAA;IACtB,qCAAkB,CAAA;IAClB,yCAAsB,CAAA;AACxB,CAAC,EANW,eAAe,KAAf,eAAe,QAM1B;AAED,MAAM,CAAN,IAAY,uBAYX;AAZD,WAAY,uBAAuB;IACjC,0DAA+B,CAAA;IAC/B,4DAAiC,CAAA;IACjC,wCAAa,CAAA;IACb,0CAAe,CAAA;IACf,qEAA0C,CAAA;IAC1C,4EAAiD,CAAA;IACjD,wEAA6C,CAAA;IAC7C;;OAEG;IACH,0DAA+B,CAAA;AACjC,CAAC,EAZW,uBAAuB,KAAvB,uBAAuB,QAYlC;AA6BD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,EAAe,EAAE,EAAU,EAAE,OAAoB;IACzF,iHAAiH;IACjH,+GAA+G;IAC/G,yEAAyE;IACzE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,8CAA8C,EAAE,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;IACvG,OAAO,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAkB,CAAC;AACxC,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,yBAAyB,CAAC,EAAe,EAAE,SAAwB,IAAI;IACrF,OAAO,mBAAmB,CAAC,EAAE,EAAE,8BAA8B,EAAE,MAAM,CAAC,CAAC;AACzE,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { CrudEntry, UpdateType } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export type CrudEntryJSON = {
|
|
6
|
+
id: string;
|
|
7
|
+
data: string;
|
|
8
|
+
tx_id?: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The output JSON seems to be a third type of JSON, not the same as the input JSON.
|
|
12
|
+
*/
|
|
13
|
+
type CrudEntryOutputJSON = {
|
|
14
|
+
op_id: number;
|
|
15
|
+
op: UpdateType;
|
|
16
|
+
type: string;
|
|
17
|
+
id: string;
|
|
18
|
+
tx_id?: number;
|
|
19
|
+
data?: Record<string, any>;
|
|
20
|
+
old?: Record<string, any>;
|
|
21
|
+
metadata?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* A single client-side change.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export declare class CrudEntryImpl implements CrudEntry {
|
|
29
|
+
clientId: number;
|
|
30
|
+
id: string;
|
|
31
|
+
op: UpdateType;
|
|
32
|
+
opData?: Record<string, any>;
|
|
33
|
+
previousValues?: Record<string, any>;
|
|
34
|
+
table: string;
|
|
35
|
+
transactionId?: number;
|
|
36
|
+
metadata?: string;
|
|
37
|
+
static fromRow(dbRow: CrudEntryJSON): CrudEntry;
|
|
38
|
+
constructor(clientId: number, op: UpdateType, table: string, id: string, transactionId?: number, opData?: Record<string, any>, previousValues?: Record<string, any>, metadata?: string);
|
|
39
|
+
toJSON(): CrudEntryOutputJSON;
|
|
40
|
+
equals(entry: CrudEntry): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Generates an array for use in deep comparison operations
|
|
43
|
+
*/
|
|
44
|
+
toComparisonArray(): unknown[];
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single client-side change.
|
|
3
|
+
*
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export class CrudEntryImpl {
|
|
7
|
+
clientId;
|
|
8
|
+
id;
|
|
9
|
+
op;
|
|
10
|
+
opData;
|
|
11
|
+
previousValues;
|
|
12
|
+
table;
|
|
13
|
+
transactionId;
|
|
14
|
+
metadata;
|
|
15
|
+
static fromRow(dbRow) {
|
|
16
|
+
const data = JSON.parse(dbRow.data);
|
|
17
|
+
return new CrudEntryImpl(parseInt(dbRow.id), data.op, data.type, data.id, dbRow.tx_id, data.data, data.old, data.metadata);
|
|
18
|
+
}
|
|
19
|
+
constructor(clientId, op, table, id, transactionId, opData, previousValues, metadata) {
|
|
20
|
+
this.clientId = clientId;
|
|
21
|
+
this.id = id;
|
|
22
|
+
this.op = op;
|
|
23
|
+
this.opData = opData;
|
|
24
|
+
this.table = table;
|
|
25
|
+
this.transactionId = transactionId;
|
|
26
|
+
this.previousValues = previousValues;
|
|
27
|
+
this.metadata = metadata;
|
|
28
|
+
}
|
|
29
|
+
toJSON() {
|
|
30
|
+
return {
|
|
31
|
+
op_id: this.clientId,
|
|
32
|
+
op: this.op,
|
|
33
|
+
type: this.table,
|
|
34
|
+
id: this.id,
|
|
35
|
+
tx_id: this.transactionId,
|
|
36
|
+
data: this.opData,
|
|
37
|
+
old: this.previousValues,
|
|
38
|
+
metadata: this.metadata
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
equals(entry) {
|
|
42
|
+
return JSON.stringify(this.toComparisonArray()) == JSON.stringify(entry.toComparisonArray());
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Generates an array for use in deep comparison operations
|
|
46
|
+
*/
|
|
47
|
+
toComparisonArray() {
|
|
48
|
+
return [
|
|
49
|
+
this.transactionId,
|
|
50
|
+
this.clientId,
|
|
51
|
+
this.op,
|
|
52
|
+
this.table,
|
|
53
|
+
this.id,
|
|
54
|
+
this.opData,
|
|
55
|
+
this.previousValues,
|
|
56
|
+
this.metadata
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=CrudEntry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CrudEntry.js","sourceRoot":"","sources":["../../../../src/client/sync/bucket/CrudEntry.ts"],"names":[],"mappings":"AAkCA;;;;GAIG;AACH,MAAM,OAAO,aAAa;IACxB,QAAQ,CAAS;IACjB,EAAE,CAAS;IACX,EAAE,CAAa;IACf,MAAM,CAAuB;IAC7B,cAAc,CAAuB;IACrC,KAAK,CAAS;IACd,aAAa,CAAU;IACvB,QAAQ,CAAU;IAElB,MAAM,CAAC,OAAO,CAAC,KAAoB;QACjC,MAAM,IAAI,GAAsB,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACvD,OAAO,IAAI,aAAa,CACtB,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,EAClB,IAAI,CAAC,EAAE,EACP,IAAI,CAAC,IAAI,EACT,IAAI,CAAC,EAAE,EACP,KAAK,CAAC,KAAK,EACX,IAAI,CAAC,IAAI,EACT,IAAI,CAAC,GAAG,EACR,IAAI,CAAC,QAAQ,CACd,CAAC;IACJ,CAAC;IAED,YACE,QAAgB,EAChB,EAAc,EACd,KAAa,EACb,EAAU,EACV,aAAsB,EACtB,MAA4B,EAC5B,cAAoC,EACpC,QAAiB;QAEjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,QAAQ;YACpB,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,IAAI,EAAE,IAAI,CAAC,KAAK;YAChB,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,KAAK,EAAE,IAAI,CAAC,aAAa;YACzB,IAAI,EAAE,IAAI,CAAC,MAAM;YACjB,GAAG,EAAE,IAAI,CAAC,cAAc;YACxB,QAAQ,EAAE,IAAI,CAAC,QAAQ;SACxB,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,KAAgB;QACrB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAC/F,CAAC;IAED;;OAEG;IACH,iBAAiB;QACf,OAAO;YACL,IAAI,CAAC,aAAa;YAClB,IAAI,CAAC,QAAQ;YACb,IAAI,CAAC,EAAE;YACP,IAAI,CAAC,KAAK;YACV,IAAI,CAAC,EAAE;YACP,IAAI,CAAC,MAAM;YACX,IAAI,CAAC,cAAc;YACnB,IAAI,CAAC,QAAQ;SACd,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BaseObserver, PowerSyncLogger, DBAdapter, Transaction, CrudEntry, CrudBatch } from '@powersync/common';
|
|
2
|
+
import { BucketStorageAdapter, BucketStorageListener, PowerSyncControlCommand } from './BucketStorageAdapter.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export declare class SqliteBucketStorage extends BaseObserver<BucketStorageListener> implements BucketStorageAdapter {
|
|
7
|
+
private db;
|
|
8
|
+
private logger;
|
|
9
|
+
private updateListener;
|
|
10
|
+
private _clientId?;
|
|
11
|
+
constructor(db: DBAdapter, logger: PowerSyncLogger);
|
|
12
|
+
dispose(): Promise<void>;
|
|
13
|
+
_getClientId(): Promise<string>;
|
|
14
|
+
getClientId(): Promise<string>;
|
|
15
|
+
updateLocalTarget(cb: () => Promise<string>): Promise<boolean>;
|
|
16
|
+
readCheckpointRequestId(variant: 'next' | 'current' | 'seed', payload?: string | null): Promise<string>;
|
|
17
|
+
nextCrudItem(): Promise<CrudEntry | undefined>;
|
|
18
|
+
hasCrud(): Promise<boolean>;
|
|
19
|
+
/**
|
|
20
|
+
* Get a batch of objects to send to the server.
|
|
21
|
+
* When the objects are successfully sent to the server, call .complete()
|
|
22
|
+
*/
|
|
23
|
+
getCrudBatch(limit?: number): Promise<CrudBatch | null>;
|
|
24
|
+
handleCrudCheckpoint(lastClientId: number, writeCheckpoint?: string): Promise<void>;
|
|
25
|
+
writeTransaction<T>(callback: (tx: Transaction) => Promise<T>, options?: {
|
|
26
|
+
timeoutMs: number;
|
|
27
|
+
}): Promise<T>;
|
|
28
|
+
control(op: PowerSyncControlCommand, payload: string | Uint8Array | ArrayBuffer | null): Promise<string>;
|
|
29
|
+
hasMigratedSubkeys(): Promise<boolean>;
|
|
30
|
+
migrateToFixedSubkeys(): Promise<void>;
|
|
31
|
+
static _subkeyMigrationKey: string;
|
|
32
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { BaseObserver, LogLevels } from '@powersync/common';
|
|
2
|
+
import { PSInternalTable, rawPowerSyncControl, targetCheckpointRequestId } from './BucketStorageAdapter.js';
|
|
3
|
+
import { CrudEntryImpl } from './CrudEntry.js';
|
|
4
|
+
import { MAX_OP_ID } from '../../../constants.js';
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export class SqliteBucketStorage extends BaseObserver {
|
|
9
|
+
db;
|
|
10
|
+
logger;
|
|
11
|
+
updateListener;
|
|
12
|
+
_clientId;
|
|
13
|
+
constructor(db, logger) {
|
|
14
|
+
super();
|
|
15
|
+
this.db = db;
|
|
16
|
+
this.logger = logger;
|
|
17
|
+
this.updateListener = db.registerListener({
|
|
18
|
+
tablesUpdated: ({ tables }) => {
|
|
19
|
+
if (tables.includes(PSInternalTable.CRUD)) {
|
|
20
|
+
this.iterateListeners((l) => l.crudUpdate?.());
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async dispose() {
|
|
26
|
+
this.updateListener?.();
|
|
27
|
+
}
|
|
28
|
+
async _getClientId() {
|
|
29
|
+
const row = await this.db.get('SELECT powersync_client_id() as client_id');
|
|
30
|
+
return row['client_id'];
|
|
31
|
+
}
|
|
32
|
+
getClientId() {
|
|
33
|
+
if (this._clientId == null) {
|
|
34
|
+
this._clientId = this._getClientId();
|
|
35
|
+
}
|
|
36
|
+
return this._clientId;
|
|
37
|
+
}
|
|
38
|
+
async updateLocalTarget(cb) {
|
|
39
|
+
const sequenceBefore = await this.db.readTransaction(async (tx) => {
|
|
40
|
+
const currentCheckpoint = await targetCheckpointRequestId(tx);
|
|
41
|
+
if (currentCheckpoint != MAX_OP_ID)
|
|
42
|
+
return;
|
|
43
|
+
const rs = await tx.getOptional("SELECT seq FROM main.sqlite_sequence WHERE name = 'ps_crud'");
|
|
44
|
+
return rs?.seq;
|
|
45
|
+
});
|
|
46
|
+
if (sequenceBefore == null) {
|
|
47
|
+
// Nothing to update
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const opId = await cb();
|
|
51
|
+
return this.writeTransaction(async (tx) => {
|
|
52
|
+
const anyData = await tx.execute('SELECT 1 FROM ps_crud LIMIT 1');
|
|
53
|
+
if (anyData.rows?.length) {
|
|
54
|
+
// if isNotEmpty
|
|
55
|
+
this.logger.log({
|
|
56
|
+
level: LogLevels.debug,
|
|
57
|
+
message: `New data uploaded since write checkpoint ${opId} - need new write checkpoint`
|
|
58
|
+
});
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
const { seq: seqAfter } = await tx.get("SELECT seq FROM main.sqlite_sequence WHERE name = 'ps_crud'");
|
|
62
|
+
if (seqAfter != sequenceBefore) {
|
|
63
|
+
this.logger.log({
|
|
64
|
+
level: LogLevels.debug,
|
|
65
|
+
message: `New data uploaded since write checpoint ${opId} - need new write checkpoint (sequence updated)`
|
|
66
|
+
});
|
|
67
|
+
// New crud data may have been uploaded since we got the checkpoint. Abort.
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
this.logger.log({
|
|
71
|
+
level: LogLevels.debug,
|
|
72
|
+
message: `Updating target write checkpoint to ${opId}`
|
|
73
|
+
});
|
|
74
|
+
await targetCheckpointRequestId(tx, opId);
|
|
75
|
+
return true;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
readCheckpointRequestId(variant, payload = null) {
|
|
79
|
+
// This needs to run in a write transaction because some checkpoint request reads interact with sync client state
|
|
80
|
+
// bound to the write connection.
|
|
81
|
+
return this.db.writeTransaction(async (tx) => {
|
|
82
|
+
return (await rawPowerSyncControl(tx, `${variant}_checkpoint_request_id`, payload));
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
async nextCrudItem() {
|
|
86
|
+
const next = await this.db.getOptional('SELECT * FROM ps_crud ORDER BY id ASC LIMIT 1');
|
|
87
|
+
if (!next) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
return CrudEntryImpl.fromRow(next);
|
|
91
|
+
}
|
|
92
|
+
async hasCrud() {
|
|
93
|
+
const anyData = await this.db.getOptional('SELECT 1 FROM ps_crud LIMIT 1');
|
|
94
|
+
return !!anyData;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Get a batch of objects to send to the server.
|
|
98
|
+
* When the objects are successfully sent to the server, call .complete()
|
|
99
|
+
*/
|
|
100
|
+
async getCrudBatch(limit = 100) {
|
|
101
|
+
if (!(await this.hasCrud())) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
const crudResult = await this.db.getAll('SELECT * FROM ps_crud ORDER BY id ASC LIMIT ?', [limit]);
|
|
105
|
+
const all = [];
|
|
106
|
+
for (const row of crudResult) {
|
|
107
|
+
all.push(CrudEntryImpl.fromRow(row));
|
|
108
|
+
}
|
|
109
|
+
if (all.length === 0) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const last = all[all.length - 1];
|
|
113
|
+
return {
|
|
114
|
+
crud: all,
|
|
115
|
+
haveMore: true,
|
|
116
|
+
complete: async (writeCheckpoint) => {
|
|
117
|
+
return this.handleCrudCheckpoint(last.clientId, writeCheckpoint);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
handleCrudCheckpoint(lastClientId, writeCheckpoint) {
|
|
122
|
+
return this.writeTransaction(async (tx) => {
|
|
123
|
+
await tx.execute('DELETE FROM ps_crud WHERE id <= ?', [lastClientId]);
|
|
124
|
+
if (writeCheckpoint) {
|
|
125
|
+
const crudResult = await tx.execute('SELECT 1 FROM ps_crud LIMIT 1');
|
|
126
|
+
if (crudResult.rows?.length) {
|
|
127
|
+
await targetCheckpointRequestId(tx, writeCheckpoint);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
await targetCheckpointRequestId(tx, MAX_OP_ID);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async writeTransaction(callback, options) {
|
|
136
|
+
return this.db.writeTransaction(callback, options);
|
|
137
|
+
}
|
|
138
|
+
async control(op, payload) {
|
|
139
|
+
return await this.writeTransaction(async (tx) => {
|
|
140
|
+
return (await rawPowerSyncControl(tx, op, payload));
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
async hasMigratedSubkeys() {
|
|
144
|
+
const { r } = await this.db.get('SELECT EXISTS(SELECT * FROM ps_kv WHERE key = ?) as r', [
|
|
145
|
+
SqliteBucketStorage._subkeyMigrationKey
|
|
146
|
+
]);
|
|
147
|
+
return r != 0;
|
|
148
|
+
}
|
|
149
|
+
async migrateToFixedSubkeys() {
|
|
150
|
+
await this.writeTransaction(async (tx) => {
|
|
151
|
+
await tx.execute('UPDATE ps_oplog SET key = powersync_remove_duplicate_key_encoding(key);');
|
|
152
|
+
await tx.execute('INSERT OR REPLACE INTO ps_kv (key, value) VALUES (?, ?);', [
|
|
153
|
+
SqliteBucketStorage._subkeyMigrationKey,
|
|
154
|
+
'1'
|
|
155
|
+
]);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
static _subkeyMigrationKey = 'powersync_js_migrated_subkeys';
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=SqliteBucketStorage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SqliteBucketStorage.js","sourceRoot":"","sources":["../../../../src/client/sync/bucket/SqliteBucketStorage.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,SAAS,EAOV,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAIL,eAAe,EACf,mBAAmB,EACnB,yBAAyB,EAC1B,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,aAAa,EAAiB,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,mBAAoB,SAAQ,YAAmC;IAKhE;IACA;IALF,cAAc,CAAa;IAC3B,SAAS,CAAmB;IAEpC,YACU,EAAa,EACb,MAAuB;QAE/B,KAAK,EAAE,CAAC;QAHA,OAAE,GAAF,EAAE,CAAW;QACb,WAAM,GAAN,MAAM,CAAiB;QAG/B,IAAI,CAAC,cAAc,GAAG,EAAE,CAAC,gBAAgB,CAAC;YACxC,aAAa,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE;gBAC5B,IAAI,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC1C,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,E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3E,mBAAmB,CAAC,mBAAmB;gBACvC,GAAG;aACJ,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED,MAAM,CAAC,mBAAmB,GAAG,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SyncOptions, SyncStreamConnectionMethod } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export type ResolvedSyncOptions = Required<SyncOptions>;
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare function resolveSyncOptions(options: SyncOptions, defaultConnectionMethod: SyncStreamConnectionMethod): ResolvedSyncOptions;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FetchStrategy } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
export function resolveSyncOptions(options, defaultConnectionMethod) {
|
|
6
|
+
return {
|
|
7
|
+
appMetadata: options.appMetadata ?? {},
|
|
8
|
+
connectionMethod: options.connectionMethod ?? defaultConnectionMethod,
|
|
9
|
+
fetchStrategy: options.fetchStrategy ?? FetchStrategy.Buffered,
|
|
10
|
+
params: options.params ?? {},
|
|
11
|
+
includeDefaultStreams: options.includeDefaultStreams ?? true,
|
|
12
|
+
retryDelayMs: options.retryDelayMs ?? 5000,
|
|
13
|
+
crudUploadThrottleMs: options.crudUploadThrottleMs ?? 1000,
|
|
14
|
+
checkpointMode: options.checkpointMode ?? 'legacy'
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../../../src/client/sync/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAA2C,MAAM,mBAAmB,CAAC;AAO3F;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAoB,EACpB,uBAAmD;IAEnD,OAAO;QACL,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,EAAE;QACtC,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,uBAAuB;QACrE,aAAa,EAAE,OAAO,CAAC,aAAa,IAAI,aAAa,CAAC,QAAQ;QAC9D,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;QAC5B,qBAAqB,EAAE,OAAO,CAAC,qBAAqB,IAAI,IAAI;QAC5D,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,IAAI;QAC1C,oBAAoB,EAAE,OAAO,CAAC,oBAAoB,IAAI,IAAI;QAC1D,cAAc,EAAE,OAAO,CAAC,cAAc,IAAI,QAAQ;KACnD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { type fetch } from 'cross-fetch';
|
|
2
|
+
import { FetchStrategy, PowerSyncCredentials, PowerSyncLogger } from '@powersync/common';
|
|
3
|
+
import { SimpleAsyncIterator } from '../../../utils/stream_transform.js';
|
|
4
|
+
import { WebSocketSupport, WebSocketSyncStreamPlatform } from './WebSocketSupport.js';
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
export type RemoteConnector = {
|
|
9
|
+
fetchCredentials: () => Promise<PowerSyncCredentials | null>;
|
|
10
|
+
invalidateCredentials?: () => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export type SyncStreamOptions = {
|
|
16
|
+
path: string;
|
|
17
|
+
data: unknown;
|
|
18
|
+
abortSignal: AbortSignal;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export type FetchImplementation = typeof fetch;
|
|
24
|
+
export declare function lazyFetchImplementation(getFetch: () => FetchImplementation): FetchImplementation;
|
|
25
|
+
export interface FetchOptions {
|
|
26
|
+
resource: string;
|
|
27
|
+
request: RequestInit;
|
|
28
|
+
expectStreamingResponse: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export interface SocketSyncStreamOptions {
|
|
34
|
+
path: string;
|
|
35
|
+
fetchStrategy: FetchStrategy;
|
|
36
|
+
abortSignal: AbortSignal;
|
|
37
|
+
data: unknown;
|
|
38
|
+
}
|
|
39
|
+
export interface PreparedRequest {
|
|
40
|
+
url: string;
|
|
41
|
+
headers: Record<string, string>;
|
|
42
|
+
userAgent: string;
|
|
43
|
+
path: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
export declare abstract class AbstractRemote {
|
|
49
|
+
protected connector: RemoteConnector;
|
|
50
|
+
readonly logger: PowerSyncLogger;
|
|
51
|
+
protected credentials: PowerSyncCredentials | null;
|
|
52
|
+
constructor(connector: RemoteConnector, logger: PowerSyncLogger);
|
|
53
|
+
/**
|
|
54
|
+
* Sends a request using a suitable `fetch` implementation for the current platform.
|
|
55
|
+
*/
|
|
56
|
+
protected abstract fetch(options: FetchOptions): Promise<Response>;
|
|
57
|
+
/**
|
|
58
|
+
* Get credentials currently cached, or fetch new credentials if none are
|
|
59
|
+
* available.
|
|
60
|
+
*
|
|
61
|
+
* These credentials may have expired already.
|
|
62
|
+
*/
|
|
63
|
+
getCredentials(): Promise<PowerSyncCredentials | null>;
|
|
64
|
+
/**
|
|
65
|
+
* Fetch a new set of credentials and cache it.
|
|
66
|
+
*
|
|
67
|
+
* Until this call succeeds, `getCredentials` will still return the
|
|
68
|
+
* old credentials.
|
|
69
|
+
*
|
|
70
|
+
* This may be called before the current credentials have expired.
|
|
71
|
+
*/
|
|
72
|
+
prefetchCredentials(): Promise<PowerSyncCredentials | null>;
|
|
73
|
+
/**
|
|
74
|
+
* Get credentials for PowerSync.
|
|
75
|
+
*
|
|
76
|
+
* This should always fetch a fresh set of credentials - don't use cached
|
|
77
|
+
* values.
|
|
78
|
+
*/
|
|
79
|
+
fetchCredentials(): Promise<PowerSyncCredentials | null>;
|
|
80
|
+
/***
|
|
81
|
+
* Immediately invalidate credentials.
|
|
82
|
+
*
|
|
83
|
+
* This may be called when the current credentials have expired.
|
|
84
|
+
*/
|
|
85
|
+
invalidateCredentials(): void;
|
|
86
|
+
getUserAgent(): string;
|
|
87
|
+
protected buildRequest(path: string): Promise<PreparedRequest>;
|
|
88
|
+
fetchAndDecodeJson({ path, method, headers, body, signal }: {
|
|
89
|
+
path: string;
|
|
90
|
+
method?: string;
|
|
91
|
+
headers?: Record<string, string>;
|
|
92
|
+
body?: string;
|
|
93
|
+
signal?: AbortSignal;
|
|
94
|
+
}): Promise<any>;
|
|
95
|
+
/**
|
|
96
|
+
* @returns A text decoder decoding UTF-8. This is a method to allow patching it for Hermes which doesn't support the
|
|
97
|
+
* builtin, without forcing us to bundle a polyfill with `@powersync/common`.
|
|
98
|
+
*/
|
|
99
|
+
createTextDecoder(): TextDecoder;
|
|
100
|
+
createSocket(url: string): WebSocket;
|
|
101
|
+
/**
|
|
102
|
+
* Loads `@powersync/shared-internals/websockets`.
|
|
103
|
+
*
|
|
104
|
+
* We prefer to load that as a lazy module with a dynamic `import()` expressions on most platforms. An exception is
|
|
105
|
+
* React Native, where WebSocket support is preferred and we want to load this directly.
|
|
106
|
+
*/
|
|
107
|
+
protected abstract loadWebSocketSupport(platform: WebSocketSyncStreamPlatform): Promise<WebSocketSupport>;
|
|
108
|
+
/**
|
|
109
|
+
* Returns a data stream of sync line data, fetched via RSocket-over-WebSocket.
|
|
110
|
+
*
|
|
111
|
+
* The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}.
|
|
112
|
+
*/
|
|
113
|
+
socketStreamRaw(options: SocketSyncStreamOptions): Promise<SimpleAsyncIterator<Uint8Array>>;
|
|
114
|
+
/**
|
|
115
|
+
* Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an
|
|
116
|
+
* async iterator of byte blobs.
|
|
117
|
+
*
|
|
118
|
+
* To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method.
|
|
119
|
+
*/
|
|
120
|
+
protected fetchStreamRaw(options: SyncStreamOptions): Promise<{
|
|
121
|
+
isBson: boolean;
|
|
122
|
+
stream: SimpleAsyncIterator<Uint8Array>;
|
|
123
|
+
}>;
|
|
124
|
+
/**
|
|
125
|
+
* Posts a `/sync/stream` request.
|
|
126
|
+
*
|
|
127
|
+
* Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as
|
|
128
|
+
* `Uint8Array`s.
|
|
129
|
+
*/
|
|
130
|
+
fetchStream(options: SyncStreamOptions): Promise<SimpleAsyncIterator<Uint8Array | string>>;
|
|
131
|
+
}
|