@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.
Files changed (152) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +5 -0
  3. package/dist/websockets.mjs +7558 -0
  4. package/dist/websockets.mjs.map +1 -0
  5. package/dist/websockets.node.mjs +5189 -0
  6. package/dist/websockets.node.mjs.map +1 -0
  7. package/legacy/sync_protocol.d.ts +103 -0
  8. package/lib/client/BasePowerSyncDatabase.d.ts +156 -0
  9. package/lib/client/BasePowerSyncDatabase.js +654 -0
  10. package/lib/client/BasePowerSyncDatabase.js.map +1 -0
  11. package/lib/client/ConnectionManager.d.ts +120 -0
  12. package/lib/client/ConnectionManager.js +324 -0
  13. package/lib/client/ConnectionManager.js.map +1 -0
  14. package/lib/client/CustomQuery.d.ts +21 -0
  15. package/lib/client/CustomQuery.js +43 -0
  16. package/lib/client/CustomQuery.js.map +1 -0
  17. package/lib/client/sync/CheckpointRequestImpl.d.ts +11 -0
  18. package/lib/client/sync/CheckpointRequestImpl.js +42 -0
  19. package/lib/client/sync/CheckpointRequestImpl.js.map +1 -0
  20. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +58 -0
  21. package/lib/client/sync/bucket/BucketStorageAdapter.js +47 -0
  22. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +1 -0
  23. package/lib/client/sync/bucket/CrudEntry.d.ts +46 -0
  24. package/lib/client/sync/bucket/CrudEntry.js +60 -0
  25. package/lib/client/sync/bucket/CrudEntry.js.map +1 -0
  26. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +32 -0
  27. package/lib/client/sync/bucket/SqliteBucketStorage.js +160 -0
  28. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -0
  29. package/lib/client/sync/options.d.ts +9 -0
  30. package/lib/client/sync/options.js +17 -0
  31. package/lib/client/sync/options.js.map +1 -0
  32. package/lib/client/sync/stream/AbstractRemote.d.ts +131 -0
  33. package/lib/client/sync/stream/AbstractRemote.js +270 -0
  34. package/lib/client/sync/stream/AbstractRemote.js.map +1 -0
  35. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +156 -0
  36. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +740 -0
  37. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -0
  38. package/lib/client/sync/stream/CheckpointState.d.ts +44 -0
  39. package/lib/client/sync/stream/CheckpointState.js +106 -0
  40. package/lib/client/sync/stream/CheckpointState.js.map +1 -0
  41. package/lib/client/sync/stream/WebSocketSupport.d.ts +26 -0
  42. package/lib/client/sync/stream/WebSocketSupport.js +198 -0
  43. package/lib/client/sync/stream/WebSocketSupport.js.map +1 -0
  44. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +15 -0
  45. package/lib/client/sync/stream/WebsocketClientTransport.js +62 -0
  46. package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -0
  47. package/lib/client/sync/stream/core-instruction.d.ts +81 -0
  48. package/lib/client/sync/stream/core-instruction.js +4 -0
  49. package/lib/client/sync/stream/core-instruction.js.map +1 -0
  50. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +6 -0
  51. package/lib/client/triggers/MemoryTriggerClaimManager.js +21 -0
  52. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +1 -0
  53. package/lib/client/triggers/TriggerManagerImpl.d.ts +68 -0
  54. package/lib/client/triggers/TriggerManagerImpl.js +415 -0
  55. package/lib/client/triggers/TriggerManagerImpl.js.map +1 -0
  56. package/lib/client/watched/AbstractQueryProcessor.d.ts +68 -0
  57. package/lib/client/watched/AbstractQueryProcessor.js +155 -0
  58. package/lib/client/watched/AbstractQueryProcessor.js.map +1 -0
  59. package/lib/client/watched/DifferentialQueryProcessor.d.ts +50 -0
  60. package/lib/client/watched/DifferentialQueryProcessor.js +174 -0
  61. package/lib/client/watched/DifferentialQueryProcessor.js.map +1 -0
  62. package/lib/client/watched/OnChangeQueryProcessor.d.ts +22 -0
  63. package/lib/client/watched/OnChangeQueryProcessor.js +80 -0
  64. package/lib/client/watched/OnChangeQueryProcessor.js.map +1 -0
  65. package/lib/client/watched/WatchedQuery.d.ts +9 -0
  66. package/lib/client/watched/WatchedQuery.js +12 -0
  67. package/lib/client/watched/WatchedQuery.js.map +1 -0
  68. package/lib/constants.d.ts +5 -0
  69. package/lib/constants.js +6 -0
  70. package/lib/constants.js.map +1 -0
  71. package/lib/db/ConnectionClosedError.d.ts +12 -0
  72. package/lib/db/ConnectionClosedError.js +23 -0
  73. package/lib/db/ConnectionClosedError.js.map +1 -0
  74. package/lib/db/crud/SyncProgress.d.ts +10 -0
  75. package/lib/db/crud/SyncProgress.js +33 -0
  76. package/lib/db/crud/SyncProgress.js.map +1 -0
  77. package/lib/db/crud/SyncStatus.d.ts +48 -0
  78. package/lib/db/crud/SyncStatus.js +178 -0
  79. package/lib/db/crud/SyncStatus.js.map +1 -0
  80. package/lib/db/openDatabase.d.ts +7 -0
  81. package/lib/db/openDatabase.js +22 -0
  82. package/lib/db/openDatabase.js.map +1 -0
  83. package/lib/index.d.ts +22 -0
  84. package/lib/index.js +22 -0
  85. package/lib/index.js.map +1 -0
  86. package/lib/utils/AbortOperation.d.ts +11 -0
  87. package/lib/utils/AbortOperation.js +21 -0
  88. package/lib/utils/AbortOperation.js.map +1 -0
  89. package/lib/utils/ControlledExecutor.d.ts +31 -0
  90. package/lib/utils/ControlledExecutor.js +58 -0
  91. package/lib/utils/ControlledExecutor.js.map +1 -0
  92. package/lib/utils/MetaBaseObserver.d.ts +11 -0
  93. package/lib/utils/MetaBaseObserver.js +51 -0
  94. package/lib/utils/MetaBaseObserver.js.map +1 -0
  95. package/lib/utils/async.d.ts +46 -0
  96. package/lib/utils/async.js +147 -0
  97. package/lib/utils/async.js.map +1 -0
  98. package/lib/utils/compatibility.d.ts +8 -0
  99. package/lib/utils/compatibility.js +9 -0
  100. package/lib/utils/compatibility.js.map +1 -0
  101. package/lib/utils/mutex.d.ts +42 -0
  102. package/lib/utils/mutex.js +150 -0
  103. package/lib/utils/mutex.js.map +1 -0
  104. package/lib/utils/parseQuery.d.ts +12 -0
  105. package/lib/utils/parseQuery.js +20 -0
  106. package/lib/utils/parseQuery.js.map +1 -0
  107. package/lib/utils/queue.d.ts +16 -0
  108. package/lib/utils/queue.js +42 -0
  109. package/lib/utils/queue.js.map +1 -0
  110. package/lib/utils/stream_transform.d.ts +42 -0
  111. package/lib/utils/stream_transform.js +227 -0
  112. package/lib/utils/stream_transform.js.map +1 -0
  113. package/lib/version.d.ts +1 -0
  114. package/lib/version.js +4 -0
  115. package/lib/version.js.map +1 -0
  116. package/package.json +68 -0
  117. package/src/client/BasePowerSyncDatabase.ts +898 -0
  118. package/src/client/ConnectionManager.ts +441 -0
  119. package/src/client/CustomQuery.ts +60 -0
  120. package/src/client/sync/CheckpointRequestImpl.ts +54 -0
  121. package/src/client/sync/bucket/BucketStorageAdapter.ts +85 -0
  122. package/src/client/sync/bucket/CrudEntry.ts +116 -0
  123. package/src/client/sync/bucket/SqliteBucketStorage.ts +203 -0
  124. package/src/client/sync/options.ts +25 -0
  125. package/src/client/sync/stream/AbstractRemote.ts +388 -0
  126. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +948 -0
  127. package/src/client/sync/stream/CheckpointState.ts +136 -0
  128. package/src/client/sync/stream/WebSocketSupport.ts +259 -0
  129. package/src/client/sync/stream/WebsocketClientTransport.ts +82 -0
  130. package/src/client/sync/stream/core-instruction.ts +85 -0
  131. package/src/client/triggers/MemoryTriggerClaimManager.ts +25 -0
  132. package/src/client/triggers/TriggerManagerImpl.ts +538 -0
  133. package/src/client/watched/AbstractQueryProcessor.ts +231 -0
  134. package/src/client/watched/DifferentialQueryProcessor.ts +226 -0
  135. package/src/client/watched/OnChangeQueryProcessor.ts +108 -0
  136. package/src/client/watched/WatchedQuery.ts +14 -0
  137. package/src/constants.ts +6 -0
  138. package/src/db/ConnectionClosedError.ts +25 -0
  139. package/src/db/crud/SyncProgress.ts +39 -0
  140. package/src/db/crud/SyncStatus.ts +234 -0
  141. package/src/db/openDatabase.ts +23 -0
  142. package/src/index.ts +24 -0
  143. package/src/utils/AbortOperation.ts +19 -0
  144. package/src/utils/ControlledExecutor.ts +81 -0
  145. package/src/utils/MetaBaseObserver.ts +65 -0
  146. package/src/utils/async.ts +185 -0
  147. package/src/utils/compatibility.ts +9 -0
  148. package/src/utils/mutex.ts +186 -0
  149. package/src/utils/parseQuery.ts +31 -0
  150. package/src/utils/queue.ts +48 -0
  151. package/src/utils/stream_transform.ts +276 -0
  152. package/src/version.ts +3 -0
@@ -0,0 +1,103 @@
1
+ // TypeScript definitions for sync lines sent by the PowerSync service. These aren't used in the implementation of
2
+ // PowerSync SDKs, but we keep them around for:
3
+ //
4
+ // 1. Tests mocking the PowerSync service.
5
+ // 2. The diagnostic app tracking schema updates and bucket sizes by decoding protocol messages.
6
+ // 3. The nuxt SDK offering DevTools doing the same.
7
+ //
8
+ // Uses 2 and 3 should be replaced with the builtin diagnostic collection of the core extension eventually.
9
+
10
+ export type SyncDataBucketJSON = {
11
+ bucket: string;
12
+ has_more?: boolean;
13
+ after?: string;
14
+ next_after?: string;
15
+ data: OplogEntryJSON[];
16
+ };
17
+ export interface OplogEntryJSON {
18
+ checksum: number;
19
+ data?: string;
20
+ object_id?: string;
21
+ object_type?: string;
22
+ op_id: string;
23
+ op: OpTypeJSON;
24
+ subkey?: string;
25
+ }
26
+ export type OpTypeJSON = 'CLEAR' | 'MOVE' | 'PUT' | 'REMOVE';
27
+
28
+ /**
29
+ * 64-bit unsigned integer stored as a string in base-10.
30
+ *
31
+ * Not sortable as a string.
32
+ */
33
+ export type OpId = string;
34
+
35
+ export interface BucketChecksum {
36
+ bucket: string;
37
+ priority?: number;
38
+ /**
39
+ * 32-bit unsigned hash.
40
+ */
41
+ checksum: number;
42
+
43
+ /**
44
+ * Count of operations - informational only.
45
+ */
46
+ count?: number;
47
+ /**
48
+ * The JavaScript client does not use this field, which is why it's defined to be `any`. We rely on the structure of
49
+ * this interface to pass custom `BucketChecksum`s to the Rust client in unit tests, which so all fields need to be
50
+ * present.
51
+ */
52
+ subscriptions?: any;
53
+ }
54
+
55
+ export interface Checkpoint {
56
+ last_op_id: OpId;
57
+ buckets: BucketChecksum[];
58
+ write_checkpoint?: string;
59
+ streams?: any[];
60
+ }
61
+
62
+ export interface StreamingSyncCheckpoint {
63
+ checkpoint: Checkpoint;
64
+ }
65
+
66
+ export interface StreamingSyncCheckpointDiff {
67
+ checkpoint_diff: {
68
+ last_op_id: OpId;
69
+ updated_buckets: BucketChecksum[];
70
+ removed_buckets: string[];
71
+ write_checkpoint?: string;
72
+ };
73
+ }
74
+
75
+ export interface StreamingSyncDataJSON {
76
+ data: SyncDataBucketJSON;
77
+ }
78
+
79
+ export interface StreamingSyncCheckpointComplete {
80
+ checkpoint_complete: {
81
+ last_op_id: OpId;
82
+ };
83
+ }
84
+
85
+ export interface StreamingSyncCheckpointPartiallyComplete {
86
+ partial_checkpoint_complete: {
87
+ priority: number;
88
+ last_op_id: OpId;
89
+ };
90
+ }
91
+
92
+ export interface StreamingSyncKeepalive {
93
+ /** If specified, token expires in this many seconds. */
94
+ token_expires_in: number;
95
+ }
96
+
97
+ export type StreamingSyncLine =
98
+ | StreamingSyncDataJSON
99
+ | StreamingSyncCheckpoint
100
+ | StreamingSyncCheckpointDiff
101
+ | StreamingSyncCheckpointComplete
102
+ | StreamingSyncCheckpointPartiallyComplete
103
+ | StreamingSyncKeepalive;
@@ -0,0 +1,156 @@
1
+ import { ArrayQueryDefinition, BasePowerSyncDatabaseOptions, CommonPowerSyncDatabase, CrudBatch, CrudTransaction, DBAdapter, DisconnectAndClearOptions, LockContext, PowerSyncBackendConnector, PowerSyncCloseOptions, PowerSyncDBListener, PowerSyncLogger, Query, QueryResult, Schema, SQLOnChangeOptions, SQLWatchOptions, SyncOptions, SyncStatus, SyncStream, Transaction, TriggerManager, UploadQueueStats, WatchCompatibleQuery, WatchHandler, WatchOnChangeEvent, WatchOnChangeHandler, BaseObserver, SqliteRecord, SyncStreamConnectionMethod, CheckpointRequest } from '@powersync/common';
2
+ import { BucketStorageAdapter } from './sync/bucket/BucketStorageAdapter.js';
3
+ import { SyncStatusSnapshot } from '../db/crud/SyncStatus.js';
4
+ import { ConnectionManager, CreateSyncImplementationOptions } from './ConnectionManager.js';
5
+ import { Mutex } from '../utils/mutex.js';
6
+ import { TriggerManagerConfig, TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
7
+ import { StreamingSyncImplementation } from './sync/stream/AbstractStreamingSyncImplementation.js';
8
+ /**
9
+ * @internal
10
+ */
11
+ export declare const DEFAULT_POWERSYNC_CLOSE_OPTIONS: PowerSyncCloseOptions;
12
+ /**
13
+ * Requesting nested or recursive locks can block the application in some circumstances.
14
+ * This default lock timeout will act as a failsafe to throw an error if a lock cannot
15
+ * be obtained.
16
+ *
17
+ * @internal
18
+ */
19
+ export declare const DEFAULT_LOCK_TIMEOUT_MS = 120000;
20
+ export declare abstract class BasePowerSyncDatabase<Options extends BasePowerSyncDatabaseOptions = BasePowerSyncDatabaseOptions> extends BaseObserver<PowerSyncDBListener> implements CommonPowerSyncDatabase {
21
+ protected options: Options;
22
+ closed: boolean;
23
+ ready: boolean;
24
+ currentStatus: SyncStatusSnapshot;
25
+ sdkVersion: string;
26
+ protected bucketStorageAdapter: BucketStorageAdapter;
27
+ protected _isReadyPromise: Promise<void>;
28
+ connectionManager: ConnectionManager;
29
+ private subscriptions;
30
+ get syncStreamImplementation(): StreamingSyncImplementation | null;
31
+ /**
32
+ * The connector used to connect to the PowerSync service.
33
+ *
34
+ * @returns The connector used to connect to the PowerSync service or null if `connect()` has not been called.
35
+ */
36
+ get connector(): PowerSyncBackendConnector | null;
37
+ /**
38
+ * The resolved connection options used to connect to the PowerSync service.
39
+ *
40
+ * @returns The resolved connection options used to connect to the PowerSync service or null if `connect()` has not been called.
41
+ */
42
+ get connectionOptions(): Required<SyncOptions> | null;
43
+ protected _schema: Schema;
44
+ private _database;
45
+ protected runExclusiveMutex: Mutex;
46
+ /**
47
+ * @experimental
48
+ * Allows creating SQLite triggers which can be used to track various operations on SQLite tables.
49
+ */
50
+ readonly triggers: TriggerManager;
51
+ protected triggersImpl: TriggerManagerImpl;
52
+ logger: PowerSyncLogger;
53
+ constructor(options: Options);
54
+ /**
55
+ * The default connection method to use on this platform.
56
+ *
57
+ * This defaults to `HTTP` on most SDKs. On React Native, it defaults to RSocket if a streaming HTTP client is
58
+ * unavailable.
59
+ */
60
+ protected get defaultConnectionMethod(): SyncStreamConnectionMethod;
61
+ get schema(): Schema<{
62
+ [x: string]: import("@powersync/common").Table<any>;
63
+ }>;
64
+ /**
65
+ * The underlying database.
66
+ *
67
+ * For the most part, behavior is the same whether querying on the underlying database, or on {@link AbstractPowerSyncDatabase}.
68
+ */
69
+ get database(): DBAdapter;
70
+ /**
71
+ * Whether a connection to the PowerSync service is currently open.
72
+ */
73
+ get connected(): boolean;
74
+ get connecting(): boolean;
75
+ /**
76
+ * Opens the DBAdapter given open options using a default open factory
77
+ */
78
+ protected abstract openDBAdapter(): DBAdapter;
79
+ /**
80
+ * Generates a base configuration for {@link TriggerManagerImpl}.
81
+ * Implementations should override this if necessary.
82
+ */
83
+ protected generateTriggerManagerConfig(): TriggerManagerConfig;
84
+ protected abstract generateSyncStreamImplementation(connector: PowerSyncBackendConnector, options: CreateSyncImplementationOptions): StreamingSyncImplementation;
85
+ protected commonSyncOptions(connector: PowerSyncBackendConnector, options: CreateSyncImplementationOptions): {
86
+ adapter: BucketStorageAdapter;
87
+ uploadCrud: () => Promise<void>;
88
+ postCheckpointRequest: (clientId: string, requestId: string) => Promise<string | null> | null;
89
+ identifier: string;
90
+ logger: PowerSyncLogger;
91
+ subscriptions: import("./sync/stream/AbstractStreamingSyncImplementation.js").SubscribedStream[];
92
+ serializedSchema: any;
93
+ };
94
+ protected generateBucketStorageAdapter(): BucketStorageAdapter;
95
+ waitForReady(): Promise<void>;
96
+ waitForFirstSync(request?: AbortSignal | {
97
+ signal?: AbortSignal;
98
+ priority?: number;
99
+ }): Promise<void>;
100
+ waitForStatus(predicate: (status: SyncStatus) => any, signal?: AbortSignal): Promise<void>;
101
+ /**
102
+ * Allows for extended implementations to execute custom initialization
103
+ * logic as part of the total init process
104
+ */
105
+ protected abstract _initialize(): Promise<void>;
106
+ /**
107
+ * Entry point for executing initialization logic.
108
+ * This is to be automatically executed in the constructor.
109
+ */
110
+ protected initialize(): Promise<void>;
111
+ protected loadVersion(): Promise<void>;
112
+ protected resolveOfflineSyncStatus(): Promise<void>;
113
+ updateSchema(schema: Schema): Promise<void>;
114
+ init(): Promise<void>;
115
+ /**
116
+ * Locking mechanism for exclusively running critical portions of connect/disconnect operations.
117
+ * Locking here is mostly only important on web for multiple tab scenarios.
118
+ */
119
+ protected runExclusive<T>(callback: () => Promise<T>): Promise<T>;
120
+ connect(connector: PowerSyncBackendConnector, options?: SyncOptions): Promise<void>;
121
+ disconnect(): Promise<void>;
122
+ disconnectAndClear(options?: DisconnectAndClearOptions): Promise<void>;
123
+ syncStream(name: string, params?: Record<string, any>): SyncStream;
124
+ requestCheckpoint(): Promise<CheckpointRequest>;
125
+ close(options?: PowerSyncCloseOptions): Promise<void>;
126
+ getUploadQueueStats(includeSize?: boolean): Promise<UploadQueueStats>;
127
+ getCrudBatch(limit?: number): Promise<CrudBatch | null>;
128
+ getNextCrudTransaction(): Promise<CrudTransaction | null>;
129
+ getCrudTransactions(): AsyncIterable<CrudTransaction, null>;
130
+ getClientId(): Promise<string>;
131
+ execute<T = SqliteRecord>(sql: string, parameters?: any[]): Promise<QueryResult<T>>;
132
+ executeRaw(sql: string, parameters?: any[]): Promise<import("@powersync/common").RawQueryResult>;
133
+ executeBatch(sql: string, parameters?: any[][]): Promise<QueryResult<never>>;
134
+ getAll<T>(sql: string, parameters?: any[]): Promise<T[]>;
135
+ getOptional<T>(sql: string, parameters?: any[]): Promise<T | null>;
136
+ get<T>(sql: string, parameters?: any[]): Promise<T>;
137
+ readLock<T>(callback: (db: LockContext) => Promise<T>): Promise<T>;
138
+ writeLock<T>(callback: (db: LockContext) => Promise<T>): Promise<T>;
139
+ readTransaction<T>(callback: (tx: Transaction) => Promise<T>, lockTimeout?: number): Promise<T>;
140
+ writeTransaction<T>(callback: (tx: Transaction) => Promise<T>, lockTimeout?: number): Promise<T>;
141
+ watch(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult>;
142
+ watch(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
143
+ query<RowType>(query: ArrayQueryDefinition<RowType>): Query<RowType>;
144
+ customQuery<RowType>(query: WatchCompatibleQuery<RowType[]>): Query<RowType>;
145
+ watchWithCallback(sql: string, parameters?: any[], handler?: WatchHandler, options?: SQLWatchOptions): void;
146
+ watchWithAsyncGenerator(sql: string, parameters?: any[], options?: SQLWatchOptions): AsyncIterable<QueryResult>;
147
+ resolveTables(sql: string, parameters?: any[], options?: SQLWatchOptions): Promise<string[]>;
148
+ onChange(options?: SQLOnChangeOptions): AsyncIterable<WatchOnChangeEvent>;
149
+ onChange(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void;
150
+ onChangeWithCallback(handler?: WatchOnChangeHandler, options?: SQLOnChangeOptions): () => void;
151
+ onChangeWithAsyncGenerator(options?: SQLWatchOptions): AsyncIterable<WatchOnChangeEvent>;
152
+ createMutex(): Mutex;
153
+ private handleTableChanges;
154
+ private processTableUpdates;
155
+ private executeReadOnly;
156
+ }