@powersync/web 1.36.0 → 1.37.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.
Files changed (91) hide show
  1. package/dist/index.umd.js +1127 -1235
  2. package/dist/index.umd.js.map +1 -1
  3. package/dist/worker/SharedSyncImplementation.umd.js +550 -3089
  4. package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
  5. package/dist/worker/WASQLiteDB.umd.js +797 -854
  6. package/dist/worker/WASQLiteDB.umd.js.map +1 -1
  7. package/lib/package.json +2 -3
  8. package/lib/src/db/PowerSyncDatabase.d.ts +1 -2
  9. package/lib/src/db/PowerSyncDatabase.js +3 -4
  10. package/lib/src/db/adapters/AsyncWebAdapter.d.ts +40 -0
  11. package/lib/src/db/adapters/AsyncWebAdapter.js +69 -0
  12. package/lib/src/db/adapters/SSRDBAdapter.d.ts +1 -2
  13. package/lib/src/db/adapters/SSRDBAdapter.js +5 -6
  14. package/lib/src/db/adapters/wa-sqlite/ConcurrentConnection.d.ts +56 -0
  15. package/lib/src/db/adapters/wa-sqlite/ConcurrentConnection.js +121 -0
  16. package/lib/src/db/adapters/wa-sqlite/DatabaseClient.d.ts +54 -0
  17. package/lib/src/db/adapters/wa-sqlite/DatabaseClient.js +227 -0
  18. package/lib/src/db/adapters/wa-sqlite/DatabaseServer.d.ts +47 -0
  19. package/lib/src/db/adapters/wa-sqlite/DatabaseServer.js +146 -0
  20. package/lib/src/db/adapters/wa-sqlite/RawSqliteConnection.d.ts +46 -0
  21. package/lib/src/db/adapters/wa-sqlite/RawSqliteConnection.js +147 -0
  22. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +14 -6
  23. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +66 -39
  24. package/lib/src/db/adapters/wa-sqlite/vfs.d.ts +61 -0
  25. package/lib/src/db/adapters/wa-sqlite/vfs.js +91 -0
  26. package/lib/src/db/adapters/web-sql-flags.d.ts +5 -0
  27. package/lib/src/db/sync/SSRWebStreamingSyncImplementation.d.ts +1 -2
  28. package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +2 -3
  29. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +4 -19
  30. package/lib/src/index.d.ts +1 -4
  31. package/lib/src/index.js +1 -4
  32. package/lib/src/shared/tab_close_signal.d.ts +11 -0
  33. package/lib/src/shared/tab_close_signal.js +26 -0
  34. package/lib/src/worker/db/MultiDatabaseServer.d.ts +17 -0
  35. package/lib/src/worker/db/MultiDatabaseServer.js +86 -0
  36. package/lib/src/worker/db/WASQLiteDB.worker.js +9 -48
  37. package/lib/src/worker/db/open-worker-database.d.ts +3 -3
  38. package/lib/src/worker/db/open-worker-database.js +1 -1
  39. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +5 -6
  40. package/lib/src/worker/sync/SharedSyncImplementation.js +92 -54
  41. package/lib/tsconfig.tsbuildinfo +1 -1
  42. package/package.json +3 -4
  43. package/src/db/PowerSyncDatabase.ts +3 -3
  44. package/src/db/adapters/AsyncWebAdapter.ts +91 -0
  45. package/src/db/adapters/SSRDBAdapter.ts +7 -7
  46. package/src/db/adapters/wa-sqlite/ConcurrentConnection.ts +137 -0
  47. package/src/db/adapters/wa-sqlite/DatabaseClient.ts +325 -0
  48. package/src/db/adapters/wa-sqlite/DatabaseServer.ts +201 -0
  49. package/src/db/adapters/wa-sqlite/RawSqliteConnection.ts +191 -0
  50. package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +87 -43
  51. package/src/db/adapters/wa-sqlite/vfs.ts +112 -0
  52. package/src/db/adapters/web-sql-flags.ts +6 -0
  53. package/src/db/sync/SSRWebStreamingSyncImplementation.ts +2 -3
  54. package/src/db/sync/SharedWebStreamingSyncImplementation.ts +4 -20
  55. package/src/index.ts +1 -4
  56. package/src/shared/tab_close_signal.ts +28 -0
  57. package/src/worker/db/MultiDatabaseServer.ts +104 -0
  58. package/src/worker/db/WASQLiteDB.worker.ts +10 -57
  59. package/src/worker/db/open-worker-database.ts +3 -3
  60. package/src/worker/sync/SharedSyncImplementation.ts +118 -58
  61. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js +0 -1881
  62. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js.map +0 -1
  63. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-97ebe9.index.umd.js +0 -555
  64. package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-97ebe9.index.umd.js.map +0 -1
  65. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +0 -17
  66. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +0 -33
  67. package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +0 -49
  68. package/lib/src/db/adapters/AsyncDatabaseConnection.js +0 -1
  69. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +0 -109
  70. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +0 -404
  71. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +0 -59
  72. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +0 -147
  73. package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.d.ts +0 -12
  74. package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.js +0 -19
  75. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +0 -155
  76. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +0 -401
  77. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +0 -32
  78. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +0 -49
  79. package/lib/src/worker/db/SharedWASQLiteConnection.d.ts +0 -42
  80. package/lib/src/worker/db/SharedWASQLiteConnection.js +0 -90
  81. package/lib/src/worker/db/WorkerWASQLiteConnection.d.ts +0 -9
  82. package/lib/src/worker/db/WorkerWASQLiteConnection.js +0 -12
  83. package/src/db/adapters/AbstractWebSQLOpenFactory.ts +0 -48
  84. package/src/db/adapters/AsyncDatabaseConnection.ts +0 -55
  85. package/src/db/adapters/LockedAsyncDatabaseAdapter.ts +0 -489
  86. package/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.ts +0 -201
  87. package/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.ts +0 -23
  88. package/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +0 -497
  89. package/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts +0 -86
  90. package/src/worker/db/SharedWASQLiteConnection.ts +0 -131
  91. package/src/worker/db/WorkerWASQLiteConnection.ts +0 -14
@@ -1,147 +0,0 @@
1
- import { BaseObserver, ConnectionClosedError } from '@powersync/common';
2
- import * as Comlink from 'comlink';
3
- /**
4
- * Wraps a provided instance of {@link AsyncDatabaseConnection}, providing necessary proxy
5
- * functions for worker listeners.
6
- */
7
- export class WorkerWrappedAsyncDatabaseConnection extends BaseObserver {
8
- options;
9
- lockAbortController = new AbortController();
10
- notifyRemoteClosed;
11
- constructor(options) {
12
- super();
13
- this.options = options;
14
- if (options.remoteCanCloseUnexpectedly) {
15
- this.notifyRemoteClosed = new AbortController();
16
- }
17
- }
18
- get baseConnection() {
19
- return this.options.baseConnection;
20
- }
21
- init() {
22
- return this.baseConnection.init();
23
- }
24
- /**
25
- * Marks the remote as closed.
26
- *
27
- * This can sometimes happen outside of our control, e.g. when a shared worker requests a connection from a tab. When
28
- * it happens, all methods on the {@link baseConnection} would never resolve. To avoid livelocks in this scenario, we
29
- * throw on all outstanding promises and forbid new calls.
30
- */
31
- markRemoteClosed() {
32
- // Can non-null assert here because this function is only supposed to be called when remoteCanCloseUnexpectedly was
33
- // set.
34
- this.notifyRemoteClosed.abort();
35
- }
36
- markHold() {
37
- return this.withRemote(() => this.baseConnection.markHold());
38
- }
39
- releaseHold(holdId) {
40
- return this.withRemote(() => this.baseConnection.releaseHold(holdId));
41
- }
42
- isAutoCommit() {
43
- return this.withRemote(() => this.baseConnection.isAutoCommit());
44
- }
45
- withRemote(workerPromise, fireActionOnAbort = false) {
46
- const controller = this.notifyRemoteClosed;
47
- if (controller) {
48
- return new Promise((resolve, reject) => {
49
- if (controller.signal.aborted) {
50
- reject(new ConnectionClosedError('Called operation on closed remote'));
51
- if (!fireActionOnAbort) {
52
- // Don't run the operation if we're going to reject
53
- // We might want to fire-and-forget the operation in some cases (like a close operation)
54
- return;
55
- }
56
- }
57
- function handleAbort() {
58
- reject(new ConnectionClosedError('Remote peer closed with request in flight'));
59
- }
60
- function completePromise(action) {
61
- controller.signal.removeEventListener('abort', handleAbort);
62
- action();
63
- }
64
- controller.signal.addEventListener('abort', handleAbort);
65
- workerPromise()
66
- .then((data) => completePromise(() => resolve(data)))
67
- .catch((e) => completePromise(() => reject(e)));
68
- });
69
- }
70
- else {
71
- // Can't close, so just return the inner worker promise unguarded.
72
- return workerPromise();
73
- }
74
- }
75
- /**
76
- * Get a MessagePort which can be used to share the internals of this connection.
77
- */
78
- async shareConnection() {
79
- const { identifier, remote } = this.options;
80
- /**
81
- * Hold a navigator lock in order to avoid features such as Chrome's frozen tabs,
82
- * or Edge's sleeping tabs from pausing the thread for this connection.
83
- * This promise resolves once a lock is obtained.
84
- * This lock will be held as long as this connection is open.
85
- * The `shareConnection` method should not be called on multiple tabs concurrently.
86
- */
87
- await new Promise((resolve, reject) => navigator.locks
88
- .request(`shared-connection-${this.options.identifier}-${Date.now()}-${Math.round(Math.random() * 10000)}`, {
89
- signal: this.lockAbortController.signal
90
- }, async () => {
91
- resolve();
92
- // Free the lock when the connection is already closed.
93
- if (this.lockAbortController.signal.aborted) {
94
- return;
95
- }
96
- // Hold the lock while the shared connection is in use.
97
- await new Promise((releaseLock) => {
98
- this.lockAbortController.signal.addEventListener('abort', () => {
99
- releaseLock();
100
- });
101
- });
102
- })
103
- // We aren't concerned with abort errors here
104
- .catch((ex) => {
105
- if (ex.name == 'AbortError') {
106
- resolve();
107
- }
108
- else {
109
- reject(ex);
110
- }
111
- }));
112
- const newPort = await remote[Comlink.createEndpoint]();
113
- return { port: newPort, identifier };
114
- }
115
- /**
116
- * Registers a table change notification callback with the base database.
117
- * This can be extended by custom implementations in order to handle proxy events.
118
- */
119
- async registerOnTableChange(callback) {
120
- return this.baseConnection.registerOnTableChange(Comlink.proxy(callback));
121
- }
122
- async close() {
123
- // Abort any pending lock requests.
124
- this.lockAbortController.abort();
125
- try {
126
- // fire and forget the close operation
127
- await this.withRemote(() => this.baseConnection.close(), true);
128
- }
129
- finally {
130
- this.options.remote[Comlink.releaseProxy]();
131
- this.options.onClose?.();
132
- this.iterateListeners((l) => l.closing?.());
133
- }
134
- }
135
- execute(sql, params) {
136
- return this.withRemote(() => this.baseConnection.execute(sql, params));
137
- }
138
- executeRaw(sql, params) {
139
- return this.withRemote(() => this.baseConnection.executeRaw(sql, params));
140
- }
141
- executeBatch(sql, params) {
142
- return this.withRemote(() => this.baseConnection.executeBatch(sql, params));
143
- }
144
- getConfig() {
145
- return this.withRemote(() => this.baseConnection.getConfig());
146
- }
147
- }
@@ -1,12 +0,0 @@
1
- import { LockedAsyncDatabaseAdapter } from '../LockedAsyncDatabaseAdapter.js';
2
- import { WebDBAdapterConfiguration } from '../WebDBAdapter.js';
3
- /**
4
- * @internal
5
- * An intermediary implementation of WASQLiteDBAdapter, which takes the same
6
- * constructor arguments as {@link LockedAsyncDatabaseAdapter}, but provides some
7
- * basic WA-SQLite specific functionality.
8
- * This base class is used to avoid requiring overloading the constructor of {@link WASQLiteDBAdapter}
9
- */
10
- export declare class InternalWASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
11
- getConfiguration(): WebDBAdapterConfiguration;
12
- }
@@ -1,19 +0,0 @@
1
- import { LockedAsyncDatabaseAdapter } from '../LockedAsyncDatabaseAdapter.js';
2
- import { WASQLiteVFS } from './WASQLiteConnection.js';
3
- /**
4
- * @internal
5
- * An intermediary implementation of WASQLiteDBAdapter, which takes the same
6
- * constructor arguments as {@link LockedAsyncDatabaseAdapter}, but provides some
7
- * basic WA-SQLite specific functionality.
8
- * This base class is used to avoid requiring overloading the constructor of {@link WASQLiteDBAdapter}
9
- */
10
- export class InternalWASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
11
- getConfiguration() {
12
- // This is valid since we only handle WASQLite connections
13
- const baseConfig = super.getConfiguration();
14
- return {
15
- ...super.getConfiguration(),
16
- requiresPersistentTriggers: baseConfig.vfs == WASQLiteVFS.OPFSCoopSyncVFS || baseConfig.vfs == WASQLiteVFS.AccessHandlePoolVFS
17
- };
18
- }
19
- }
@@ -1,155 +0,0 @@
1
- import * as SQLite from '@journeyapps/wa-sqlite';
2
- import { BaseObserver, BatchedUpdateNotification } from '@powersync/common';
3
- import { Mutex } from 'async-mutex';
4
- import { AsyncDatabaseConnection, OnTableChangeCallback, ProxiedQueryResult } from '../AsyncDatabaseConnection.js';
5
- import { ResolvedWASQLiteOpenFactoryOptions } from './WASQLiteOpenFactory.js';
6
- /**
7
- * List of currently tested virtual filesystems
8
- */
9
- export declare enum WASQLiteVFS {
10
- IDBBatchAtomicVFS = "IDBBatchAtomicVFS",
11
- OPFSCoopSyncVFS = "OPFSCoopSyncVFS",
12
- AccessHandlePoolVFS = "AccessHandlePoolVFS"
13
- }
14
- /**
15
- * @internal
16
- */
17
- export type WASQLiteBroadCastTableUpdateEvent = {
18
- changedTables: Set<string>;
19
- connectionId: number;
20
- };
21
- /**
22
- * @internal
23
- */
24
- export type WASQLiteConnectionListener = {
25
- tablesUpdated: (event: BatchedUpdateNotification) => void;
26
- /**
27
- * Triggered when an active hold is overwritten by a new hold.
28
- * This is most likely to happen when a shared connection has been closed
29
- * without releasing the hold.
30
- * This listener can be used to cleanup any resources associated with the previous hold.
31
- * @param holdId - The id of the hold that has been overwritten.
32
- */
33
- holdOverwritten: (holdId: string) => Promise<void>;
34
- };
35
- /**
36
- * @internal
37
- */
38
- export type SQLiteModule = Parameters<typeof SQLite.Factory>[0];
39
- /**
40
- * @internal
41
- */
42
- export type WASQLiteModuleFactoryOptions = {
43
- dbFileName: string;
44
- encryptionKey?: string;
45
- };
46
- /**
47
- * @internal
48
- */
49
- export type WASQLiteModuleFactory = (options: WASQLiteModuleFactoryOptions) => Promise<{
50
- module: SQLiteModule;
51
- vfs: SQLiteVFS;
52
- }>;
53
- /**
54
- * @internal
55
- */
56
- export declare const AsyncWASQLiteModuleFactory: () => Promise<any>;
57
- /**
58
- * @internal
59
- */
60
- export declare const MultiCipherAsyncWASQLiteModuleFactory: () => Promise<any>;
61
- /**
62
- * @internal
63
- */
64
- export declare const SyncWASQLiteModuleFactory: () => Promise<any>;
65
- /**
66
- * @internal
67
- */
68
- export declare const MultiCipherSyncWASQLiteModuleFactory: () => Promise<any>;
69
- /**
70
- * @internal
71
- */
72
- export declare const DEFAULT_MODULE_FACTORIES: {
73
- IDBBatchAtomicVFS: (options: WASQLiteModuleFactoryOptions) => Promise<{
74
- module: any;
75
- vfs: any;
76
- }>;
77
- AccessHandlePoolVFS: (options: WASQLiteModuleFactoryOptions) => Promise<{
78
- module: any;
79
- vfs: any;
80
- }>;
81
- OPFSCoopSyncVFS: (options: WASQLiteModuleFactoryOptions) => Promise<{
82
- module: any;
83
- vfs: any;
84
- }>;
85
- };
86
- /**
87
- * @internal
88
- * WA-SQLite connection which directly interfaces with WA-SQLite.
89
- * This is usually instantiated inside a worker.
90
- */
91
- export declare class WASqliteConnection extends BaseObserver<WASQLiteConnectionListener> implements AsyncDatabaseConnection<ResolvedWASQLiteOpenFactoryOptions> {
92
- protected options: ResolvedWASQLiteOpenFactoryOptions;
93
- private _sqliteAPI;
94
- private _dbP;
95
- private _moduleFactory;
96
- protected updatedTables: Set<string>;
97
- protected updateTimer: ReturnType<typeof setTimeout> | null;
98
- protected statementMutex: Mutex;
99
- protected broadcastChannel: BroadcastChannel | null;
100
- /**
101
- * Unique id for this specific connection. This is used to prevent broadcast table change
102
- * notification loops.
103
- */
104
- protected connectionId: number;
105
- protected _holdCounter: number;
106
- protected _holdId: string | null;
107
- constructor(options: ResolvedWASQLiteOpenFactoryOptions);
108
- /**
109
- * Gets the id for the current hold.
110
- * This can be used to check for invalid states.
111
- */
112
- get currentHoldId(): string | null;
113
- protected get sqliteAPI(): SQLiteAPI;
114
- protected get dbP(): number;
115
- /**
116
- * Checks if the database connection is in autocommit mode.
117
- * @returns true if in autocommit mode, false if in a transaction
118
- */
119
- isAutoCommit(): Promise<boolean>;
120
- markHold(): Promise<string>;
121
- releaseHold(holdId: string): Promise<void>;
122
- protected openDB(): Promise<number>;
123
- protected executeEncryptionPragma(): Promise<void>;
124
- protected openSQLiteAPI(): Promise<SQLiteAPI>;
125
- protected registerBroadcastListeners(): void;
126
- protected queueTableUpdate(tableNames: Set<string>, shouldBroadcast?: boolean): void;
127
- init(): Promise<void>;
128
- getConfig(): Promise<ResolvedWASQLiteOpenFactoryOptions>;
129
- fireUpdates(shouldBroadcast?: boolean): void;
130
- /**
131
- * This executes SQL statements in a batch.
132
- */
133
- executeBatch(sql: string, bindings?: any[][]): Promise<ProxiedQueryResult>;
134
- /**
135
- * This executes single SQL statements inside a requested lock.
136
- */
137
- execute(sql: string | TemplateStringsArray, bindings?: any[]): Promise<ProxiedQueryResult>;
138
- executeRaw(sql: string | TemplateStringsArray, bindings?: any[]): Promise<any[][]>;
139
- close(): Promise<void>;
140
- registerOnTableChange(callback: OnTableChangeCallback): Promise<() => void>;
141
- /**
142
- * This requests a lock for executing statements.
143
- * Should only be used internally.
144
- */
145
- protected acquireExecuteLock: <T>(callback: () => Promise<T>) => Promise<T>;
146
- /**
147
- * This executes a single statement using SQLite3.
148
- */
149
- protected executeSingleStatement(sql: string | TemplateStringsArray, bindings?: any[]): Promise<ProxiedQueryResult>;
150
- /**
151
- * This executes a single statement using SQLite3 and returns the results as an array of arrays.
152
- */
153
- protected executeSingleStatementRaw(sql: string | TemplateStringsArray, bindings?: any[]): Promise<any[][]>;
154
- private _execute;
155
- }