@powersync/web 0.0.0-dev-20240506092851
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 +55 -0
- package/lib/src/db/PowerSyncDatabase.d.ts +43 -0
- package/lib/src/db/PowerSyncDatabase.js +91 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.d.ts +23 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +56 -0
- package/lib/src/db/adapters/SSRDBAdapter.d.ts +29 -0
- package/lib/src/db/adapters/SSRDBAdapter.js +85 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +56 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +196 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.d.ts +6 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +11 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.d.ts +8 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +13 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +47 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +187 -0
- package/lib/src/db/sync/WebRemote.d.ts +6 -0
- package/lib/src/db/sync/WebRemote.js +133 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +11 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.js +15 -0
- package/lib/src/index.d.ts +8 -0
- package/lib/src/index.js +8 -0
- package/lib/src/worker/db/SharedWASQLiteDB.worker.d.ts +1 -0
- package/lib/src/worker/db/SharedWASQLiteDB.worker.js +57 -0
- package/lib/src/worker/db/WASQLiteDB.worker.d.ts +1 -0
- package/lib/src/worker/db/WASQLiteDB.worker.js +12 -0
- package/lib/src/worker/db/open-db.d.ts +20 -0
- package/lib/src/worker/db/open-db.js +192 -0
- package/lib/src/worker/db/open-worker-database.d.ts +11 -0
- package/lib/src/worker/db/open-worker-database.js +30 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +17 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js +5 -0
- package/lib/src/worker/sync/BroadcastLogger.d.ts +37 -0
- package/lib/src/worker/sync/BroadcastLogger.js +107 -0
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +88 -0
- package/lib/src/worker/sync/SharedSyncImplementation.js +247 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.d.ts +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js +22 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { BaseObserver } from '@powersync/common';
|
|
11
|
+
import * as Comlink from 'comlink';
|
|
12
|
+
import Logger from 'js-logger';
|
|
13
|
+
import { getWorkerDatabaseOpener } from '../../../worker/db/open-worker-database';
|
|
14
|
+
/**
|
|
15
|
+
* Adapter for WA-SQLite
|
|
16
|
+
*/
|
|
17
|
+
export class WASQLiteDBAdapter extends BaseObserver {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
super();
|
|
20
|
+
this.options = options;
|
|
21
|
+
/**
|
|
22
|
+
* Wraps the worker execute function, awaiting for it to be available
|
|
23
|
+
*/
|
|
24
|
+
this._execute = (sql, bindings) => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
yield this.initialized;
|
|
26
|
+
const result = yield this.workerMethods.execute(sql, bindings);
|
|
27
|
+
return Object.assign(Object.assign({}, result), { rows: Object.assign(Object.assign({}, result.rows), { item: (idx) => result.rows._array[idx] }) });
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Wraps the worker executeBatch function, awaiting for it to be available
|
|
31
|
+
*/
|
|
32
|
+
this._executeBatch = (query, params) => __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
yield this.initialized;
|
|
34
|
+
const result = yield this.workerMethods.executeBatch(query, params);
|
|
35
|
+
return Object.assign(Object.assign({}, result), { rows: undefined });
|
|
36
|
+
});
|
|
37
|
+
this.logger = Logger.get('WASQLite');
|
|
38
|
+
this.dbGetHelpers = null;
|
|
39
|
+
this.workerMethods = null;
|
|
40
|
+
this.initialized = this.init();
|
|
41
|
+
this.dbGetHelpers = this.generateDBHelpers({ execute: this._execute.bind(this) });
|
|
42
|
+
}
|
|
43
|
+
get name() {
|
|
44
|
+
return this.options.dbFilename;
|
|
45
|
+
}
|
|
46
|
+
get flags() {
|
|
47
|
+
var _a;
|
|
48
|
+
return (_a = this.options.flags) !== null && _a !== void 0 ? _a : {};
|
|
49
|
+
}
|
|
50
|
+
getWorker() { }
|
|
51
|
+
init() {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const { enableMultiTabs } = this.flags;
|
|
54
|
+
if (!enableMultiTabs) {
|
|
55
|
+
this.logger.warn('Multiple tabs are not enabled in this browser');
|
|
56
|
+
}
|
|
57
|
+
const dbOpener = this.options.workerPort
|
|
58
|
+
? Comlink.wrap(this.options.workerPort)
|
|
59
|
+
: getWorkerDatabaseOpener(this.options.dbFilename, enableMultiTabs);
|
|
60
|
+
this.workerMethods = yield dbOpener(this.options.dbFilename);
|
|
61
|
+
this.workerMethods.registerOnTableChange(Comlink.proxy((opType, tableName, rowId) => {
|
|
62
|
+
this.iterateListeners((cb) => { var _a; return (_a = cb.tablesUpdated) === null || _a === void 0 ? void 0 : _a.call(cb, { opType, table: tableName, rowId }); });
|
|
63
|
+
}));
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
execute(query, params) {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
return this.writeLock((ctx) => ctx.execute(query, params));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
executeBatch(query, params) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
return this.writeLock((ctx) => this._executeBatch(query, params));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Attempts to close the connection.
|
|
78
|
+
* Shared workers might not actually close the connection if other
|
|
79
|
+
* tabs are still using it.
|
|
80
|
+
*/
|
|
81
|
+
close() {
|
|
82
|
+
var _a, _b;
|
|
83
|
+
(_b = (_a = this.workerMethods) === null || _a === void 0 ? void 0 : _a.close) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
84
|
+
}
|
|
85
|
+
getAll(sql, parameters) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
yield this.initialized;
|
|
88
|
+
return this.dbGetHelpers.getAll(sql, parameters);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
getOptional(sql, parameters) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
yield this.initialized;
|
|
94
|
+
return this.dbGetHelpers.getOptional(sql, parameters);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
get(sql, parameters) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
yield this.initialized;
|
|
100
|
+
return this.dbGetHelpers.get(sql, parameters);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
readLock(fn, options) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
yield this.initialized;
|
|
106
|
+
return this.acquireLock(() => __awaiter(this, void 0, void 0, function* () { return fn(this.generateDBHelpers({ execute: this._execute })); }));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
writeLock(fn, options) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
yield this.initialized;
|
|
112
|
+
return this.acquireLock(() => __awaiter(this, void 0, void 0, function* () { return fn(this.generateDBHelpers({ execute: this._execute })); }));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
acquireLock(callback) {
|
|
116
|
+
return navigator.locks.request(`db-lock-${this.options.dbFilename}`, callback);
|
|
117
|
+
}
|
|
118
|
+
readTransaction(fn, options) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
return this.readLock(this.wrapTransaction(fn));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
writeTransaction(fn, options) {
|
|
124
|
+
return this.writeLock(this.wrapTransaction(fn));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Wraps a lock context into a transaction context
|
|
128
|
+
*/
|
|
129
|
+
wrapTransaction(cb) {
|
|
130
|
+
return (tx) => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
yield this._execute('BEGIN TRANSACTION');
|
|
132
|
+
let finalized = false;
|
|
133
|
+
const commit = () => __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
if (finalized) {
|
|
135
|
+
return { rowsAffected: 0 };
|
|
136
|
+
}
|
|
137
|
+
finalized = true;
|
|
138
|
+
return this._execute('COMMIT');
|
|
139
|
+
});
|
|
140
|
+
const rollback = () => {
|
|
141
|
+
finalized = true;
|
|
142
|
+
return this._execute('ROLLBACK');
|
|
143
|
+
};
|
|
144
|
+
try {
|
|
145
|
+
const result = yield cb(Object.assign(Object.assign({}, tx), { commit,
|
|
146
|
+
rollback }));
|
|
147
|
+
if (!finalized) {
|
|
148
|
+
yield commit();
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
catch (ex) {
|
|
153
|
+
this.logger.debug('Caught ex in transaction', ex);
|
|
154
|
+
yield rollback();
|
|
155
|
+
throw ex;
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
generateDBHelpers(tx) {
|
|
160
|
+
return Object.assign(Object.assign({}, tx), {
|
|
161
|
+
/**
|
|
162
|
+
* Execute a read-only query and return results
|
|
163
|
+
*/
|
|
164
|
+
getAll(sql, parameters) {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
var _a, _b;
|
|
167
|
+
const res = yield tx.execute(sql, parameters);
|
|
168
|
+
return (_b = (_a = res.rows) === null || _a === void 0 ? void 0 : _a._array) !== null && _b !== void 0 ? _b : [];
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
/**
|
|
172
|
+
* Execute a read-only query and return the first result, or null if the ResultSet is empty.
|
|
173
|
+
*/
|
|
174
|
+
getOptional(sql, parameters) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
var _a, _b;
|
|
177
|
+
const res = yield tx.execute(sql, parameters);
|
|
178
|
+
return (_b = (_a = res.rows) === null || _a === void 0 ? void 0 : _a.item(0)) !== null && _b !== void 0 ? _b : null;
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
/**
|
|
182
|
+
* Execute a read-only query and return the first result, error if the ResultSet is empty.
|
|
183
|
+
*/
|
|
184
|
+
get(sql, parameters) {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
var _a;
|
|
187
|
+
const res = yield tx.execute(sql, parameters);
|
|
188
|
+
const first = (_a = res.rows) === null || _a === void 0 ? void 0 : _a.item(0);
|
|
189
|
+
if (!first) {
|
|
190
|
+
throw new Error('Result set is empty');
|
|
191
|
+
}
|
|
192
|
+
return first;
|
|
193
|
+
});
|
|
194
|
+
} });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, DBAdapter, PowerSyncDatabaseOptions } from '@powersync/common';
|
|
2
|
+
import { AbstractWebPowerSyncDatabaseOpenFactory } from '../AbstractWebPowerSyncDatabaseOpenFactory';
|
|
3
|
+
export declare class WASQLitePowerSyncDatabaseOpenFactory extends AbstractWebPowerSyncDatabaseOpenFactory {
|
|
4
|
+
protected openDB(): DBAdapter;
|
|
5
|
+
generateInstance(options: PowerSyncDatabaseOptions): AbstractPowerSyncDatabase;
|
|
6
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PowerSyncDatabase } from '../../../db/PowerSyncDatabase';
|
|
2
|
+
import { WASQLiteDBAdapter } from './WASQLiteDBAdapter';
|
|
3
|
+
import { AbstractWebPowerSyncDatabaseOpenFactory } from '../AbstractWebPowerSyncDatabaseOpenFactory';
|
|
4
|
+
export class WASQLitePowerSyncDatabaseOpenFactory extends AbstractWebPowerSyncDatabaseOpenFactory {
|
|
5
|
+
openDB() {
|
|
6
|
+
return new WASQLiteDBAdapter(Object.assign(Object.assign({}, this.options), { flags: this.resolveDBFlags() }));
|
|
7
|
+
}
|
|
8
|
+
generateInstance(options) {
|
|
9
|
+
return new PowerSyncDatabase(options);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AbstractStreamingSyncImplementation, AbstractStreamingSyncImplementationOptions, LockOptions } from '@powersync/common';
|
|
2
|
+
import { Mutex } from 'async-mutex';
|
|
3
|
+
export declare class SSRStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
|
|
4
|
+
syncMutex: Mutex;
|
|
5
|
+
crudMutex: Mutex;
|
|
6
|
+
constructor(options: AbstractStreamingSyncImplementationOptions);
|
|
7
|
+
obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AbstractStreamingSyncImplementation, LockType } from '@powersync/common';
|
|
2
|
+
import { Mutex } from 'async-mutex';
|
|
3
|
+
export class SSRStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super(options);
|
|
6
|
+
this.syncMutex = new Mutex();
|
|
7
|
+
this.crudMutex = new Mutex();
|
|
8
|
+
}
|
|
9
|
+
obtainLock(lockOptions) {
|
|
10
|
+
const mutex = lockOptions.type == LockType.CRUD ? this.crudMutex : this.syncMutex;
|
|
11
|
+
return mutex.runExclusive(lockOptions.callback);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { WebStreamingSyncImplementation, WebStreamingSyncImplementationOptions } from './WebStreamingSyncImplementation';
|
|
3
|
+
import { SharedSyncImplementation } from '../../worker/sync/SharedSyncImplementation';
|
|
4
|
+
import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider';
|
|
5
|
+
import { PowerSyncCredentials, SyncStatusOptions } from '@powersync/common';
|
|
6
|
+
/**
|
|
7
|
+
* The shared worker will trigger methods on this side of the message port
|
|
8
|
+
* via this client provider.
|
|
9
|
+
*/
|
|
10
|
+
declare class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
|
|
11
|
+
protected options: WebStreamingSyncImplementationOptions;
|
|
12
|
+
statusChanged: (status: SyncStatusOptions) => void;
|
|
13
|
+
constructor(options: WebStreamingSyncImplementationOptions, statusChanged: (status: SyncStatusOptions) => void);
|
|
14
|
+
fetchCredentials(): Promise<PowerSyncCredentials | null>;
|
|
15
|
+
uploadCrud(): Promise<void>;
|
|
16
|
+
get logger(): import("js-logger").ILogger | undefined;
|
|
17
|
+
trace(...x: any[]): void;
|
|
18
|
+
debug(...x: any[]): void;
|
|
19
|
+
info(...x: any[]): void;
|
|
20
|
+
log(...x: any[]): void;
|
|
21
|
+
warn(...x: any[]): void;
|
|
22
|
+
error(...x: any[]): void;
|
|
23
|
+
time(label: string): void;
|
|
24
|
+
timeEnd(label: string): void;
|
|
25
|
+
}
|
|
26
|
+
export declare class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplementation {
|
|
27
|
+
protected syncManager: Comlink.Remote<SharedSyncImplementation>;
|
|
28
|
+
protected clientProvider: SharedSyncClientProvider;
|
|
29
|
+
protected messagePort: MessagePort;
|
|
30
|
+
protected isInitialized: Promise<void>;
|
|
31
|
+
constructor(options: WebStreamingSyncImplementationOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Starts the sync process, this effectively acts as a call to
|
|
34
|
+
* `connect` if not yet connected.
|
|
35
|
+
*/
|
|
36
|
+
connect(): Promise<void>;
|
|
37
|
+
disconnect(): Promise<void>;
|
|
38
|
+
getWriteCheckpoint(): Promise<string>;
|
|
39
|
+
hasCompletedSync(): Promise<boolean>;
|
|
40
|
+
dispose(): Promise<void>;
|
|
41
|
+
waitForReady(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Used in tests to force a connection states
|
|
44
|
+
*/
|
|
45
|
+
private _testUpdateStatus;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import * as Comlink from 'comlink';
|
|
11
|
+
import { WebStreamingSyncImplementation } from './WebStreamingSyncImplementation';
|
|
12
|
+
import { SharedSyncClientEvent } from '../../worker/sync/SharedSyncImplementation';
|
|
13
|
+
import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider';
|
|
14
|
+
import { openWorkerDatabasePort } from '../../worker/db/open-worker-database';
|
|
15
|
+
/**
|
|
16
|
+
* The shared worker will trigger methods on this side of the message port
|
|
17
|
+
* via this client provider.
|
|
18
|
+
*/
|
|
19
|
+
class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
|
|
20
|
+
constructor(options, statusChanged) {
|
|
21
|
+
super();
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.statusChanged = statusChanged;
|
|
24
|
+
}
|
|
25
|
+
fetchCredentials() {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
const credentials = yield this.options.remote.getCredentials();
|
|
28
|
+
if (credentials == null) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The credentials need to be serializable.
|
|
33
|
+
* Users might extend [PowerSyncCredentials] to contain
|
|
34
|
+
* items which are not serializable.
|
|
35
|
+
* This returns only the essential fields.
|
|
36
|
+
*/
|
|
37
|
+
return {
|
|
38
|
+
endpoint: credentials.endpoint,
|
|
39
|
+
token: credentials.token,
|
|
40
|
+
expiresAt: credentials.expiresAt
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
uploadCrud() {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
/**
|
|
47
|
+
* Don't return anything here, just incase something which is not
|
|
48
|
+
* serializable is returned from the `uploadCrud` function.
|
|
49
|
+
*/
|
|
50
|
+
yield this.options.uploadCrud();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
get logger() {
|
|
54
|
+
return this.options.logger;
|
|
55
|
+
}
|
|
56
|
+
trace(...x) {
|
|
57
|
+
var _a;
|
|
58
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.trace(...x);
|
|
59
|
+
}
|
|
60
|
+
debug(...x) {
|
|
61
|
+
var _a;
|
|
62
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug(...x);
|
|
63
|
+
}
|
|
64
|
+
info(...x) {
|
|
65
|
+
var _a;
|
|
66
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info(...x);
|
|
67
|
+
}
|
|
68
|
+
log(...x) {
|
|
69
|
+
var _a;
|
|
70
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.log(...x);
|
|
71
|
+
}
|
|
72
|
+
warn(...x) {
|
|
73
|
+
var _a;
|
|
74
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.warn(...x);
|
|
75
|
+
}
|
|
76
|
+
error(...x) {
|
|
77
|
+
var _a;
|
|
78
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.error(...x);
|
|
79
|
+
}
|
|
80
|
+
time(label) {
|
|
81
|
+
var _a;
|
|
82
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.time(label);
|
|
83
|
+
}
|
|
84
|
+
timeEnd(label) {
|
|
85
|
+
var _a;
|
|
86
|
+
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.timeEnd(label);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplementation {
|
|
90
|
+
constructor(options) {
|
|
91
|
+
super(options);
|
|
92
|
+
/**
|
|
93
|
+
* Configure or connect to the shared sync worker.
|
|
94
|
+
* This worker will manage all syncing operations remotely.
|
|
95
|
+
*/
|
|
96
|
+
const syncWorker = new SharedWorker(new URL('../../worker/sync/SharedSyncImplementation.worker.js', import.meta.url), {
|
|
97
|
+
/* @vite-ignore */
|
|
98
|
+
name: `shared-sync-${this.webOptions.identifier}`,
|
|
99
|
+
type: 'module'
|
|
100
|
+
});
|
|
101
|
+
this.messagePort = syncWorker.port;
|
|
102
|
+
this.syncManager = Comlink.wrap(this.messagePort);
|
|
103
|
+
this.triggerCrudUpload = this.syncManager.triggerCrudUpload;
|
|
104
|
+
/**
|
|
105
|
+
* Opens MessagePort to the existing shared DB worker.
|
|
106
|
+
* The sync worker cannot initiate connections directly to the
|
|
107
|
+
* DB worker, but a port to the DB worker can be transferred to the
|
|
108
|
+
* sync worker.
|
|
109
|
+
*/
|
|
110
|
+
const { crudUploadThrottleMs, identifier, retryDelayMs } = this.options;
|
|
111
|
+
const dbOpenerPort = openWorkerDatabasePort(this.options.identifier, true);
|
|
112
|
+
this.isInitialized = this.syncManager.init(Comlink.transfer(dbOpenerPort, [dbOpenerPort]), {
|
|
113
|
+
dbName: this.options.identifier,
|
|
114
|
+
streamOptions: {
|
|
115
|
+
crudUploadThrottleMs,
|
|
116
|
+
identifier,
|
|
117
|
+
retryDelayMs,
|
|
118
|
+
flags: this.webOptions.flags
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
/**
|
|
122
|
+
* Pass along any sync status updates to this listener
|
|
123
|
+
*/
|
|
124
|
+
this.clientProvider = new SharedSyncClientProvider(this.webOptions, (status) => {
|
|
125
|
+
this.iterateListeners((l) => this.updateSyncStatus(status));
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* The sync worker will call this client provider when it needs
|
|
129
|
+
* to fetch credentials or upload data.
|
|
130
|
+
* This performs bi-directional method calling.
|
|
131
|
+
*/
|
|
132
|
+
Comlink.expose(this.clientProvider, this.messagePort);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Starts the sync process, this effectively acts as a call to
|
|
136
|
+
* `connect` if not yet connected.
|
|
137
|
+
*/
|
|
138
|
+
connect() {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
yield this.waitForReady();
|
|
141
|
+
return this.syncManager.connect();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
disconnect() {
|
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
+
yield this.waitForReady();
|
|
147
|
+
return this.syncManager.disconnect();
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
getWriteCheckpoint() {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
yield this.waitForReady();
|
|
153
|
+
return this.syncManager.getWriteCheckpoint();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
hasCompletedSync() {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
return this.syncManager.hasCompletedSync();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
dispose() {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
yield this.waitForReady();
|
|
164
|
+
// Signal the shared worker that this client is closing its connection to the worker
|
|
165
|
+
const closeMessagePayload = {
|
|
166
|
+
event: SharedSyncClientEvent.CLOSE_CLIENT,
|
|
167
|
+
data: {}
|
|
168
|
+
};
|
|
169
|
+
this.messagePort.postMessage(closeMessagePayload);
|
|
170
|
+
// Release the proxy
|
|
171
|
+
this.syncManager[Comlink.releaseProxy]();
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
waitForReady() {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
return this.isInitialized;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Used in tests to force a connection states
|
|
181
|
+
*/
|
|
182
|
+
_testUpdateStatus(status) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
return this.syncManager['_testUpdateAllStatuses'](status.toJSON());
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AbstractRemote } from '@powersync/common';
|
|
2
|
+
export declare class WebRemote extends AbstractRemote {
|
|
3
|
+
post(path: string, data: any, headers?: Record<string, string>): Promise<any>;
|
|
4
|
+
get(path: string, headers?: Record<string, string>): Promise<any>;
|
|
5
|
+
postStreaming(path: string, data: any, headers?: Record<string, string>, signal?: AbortSignal): Promise<any>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { AbortOperation, AbstractRemote } from '@powersync/common';
|
|
11
|
+
export class WebRemote extends AbstractRemote {
|
|
12
|
+
post(path_1, data_1) {
|
|
13
|
+
return __awaiter(this, arguments, void 0, function* (path, data, headers = {}) {
|
|
14
|
+
const request = yield this.buildRequest(path);
|
|
15
|
+
const res = yield fetch(request.url, {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
headers: Object.assign(Object.assign({}, headers), request.headers),
|
|
18
|
+
body: JSON.stringify(data)
|
|
19
|
+
});
|
|
20
|
+
if (!res.ok) {
|
|
21
|
+
throw new Error(`Received ${res.status} - ${res.statusText} when posting to ${path}: ${yield res.text()}}`);
|
|
22
|
+
}
|
|
23
|
+
return res.json();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
get(path, headers) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const request = yield this.buildRequest(path);
|
|
29
|
+
const res = yield fetch(request.url, {
|
|
30
|
+
method: 'GET',
|
|
31
|
+
headers: Object.assign(Object.assign({}, headers), request.headers)
|
|
32
|
+
});
|
|
33
|
+
if (!res.ok) {
|
|
34
|
+
throw new Error(`Received ${res.status} - ${res.statusText} when getting from ${path}: ${yield res.text()}}`);
|
|
35
|
+
}
|
|
36
|
+
return res.json();
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
postStreaming(path_1, data_1) {
|
|
40
|
+
return __awaiter(this, arguments, void 0, function* (path, data, headers = {}, signal) {
|
|
41
|
+
const request = yield this.buildRequest(path);
|
|
42
|
+
/**
|
|
43
|
+
* This abort controller will abort pending fetch requests.
|
|
44
|
+
* If the request has resolved, it will be used to close the readable stream.
|
|
45
|
+
* Which will cancel the network request.
|
|
46
|
+
*
|
|
47
|
+
* This nested controller is required since:
|
|
48
|
+
* Aborting the active fetch request while it is being consumed seems to throw
|
|
49
|
+
* an unhandled exception on the window level.
|
|
50
|
+
*/
|
|
51
|
+
const controller = new AbortController();
|
|
52
|
+
let requestResolved = false;
|
|
53
|
+
signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', () => {
|
|
54
|
+
var _a;
|
|
55
|
+
if (!requestResolved) {
|
|
56
|
+
// Only abort via the abort controller if the request has not resolved yet
|
|
57
|
+
controller.abort((_a = signal.reason) !== null && _a !== void 0 ? _a : new AbortOperation('Cancelling network request before it resolves. Abort signal has been received.'));
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const res = yield fetch(request.url, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: Object.assign(Object.assign({}, headers), request.headers),
|
|
63
|
+
body: JSON.stringify(data),
|
|
64
|
+
signal: controller.signal,
|
|
65
|
+
cache: 'no-store'
|
|
66
|
+
}).catch((ex) => {
|
|
67
|
+
if (ex.name == 'AbortError') {
|
|
68
|
+
throw new AbortOperation(`Pending fetch request to ${request.url} has been aborted.`);
|
|
69
|
+
}
|
|
70
|
+
throw ex;
|
|
71
|
+
});
|
|
72
|
+
if (!res) {
|
|
73
|
+
throw new Error('Fetch request was aborted');
|
|
74
|
+
}
|
|
75
|
+
requestResolved = true;
|
|
76
|
+
if (!res.ok || !res.body) {
|
|
77
|
+
const text = yield res.text();
|
|
78
|
+
this.logger.error(`Could not POST streaming to ${path} - ${res.status} - ${res.statusText}: ${text}`);
|
|
79
|
+
const error = new Error(`HTTP ${res.statusText}: ${text}`);
|
|
80
|
+
error.status = res.status;
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The can-ndjson-stream does not handle aborted streams well.
|
|
85
|
+
* This will intercept the readable stream and close the stream if
|
|
86
|
+
* aborted.
|
|
87
|
+
*/
|
|
88
|
+
const reader = res.body.getReader();
|
|
89
|
+
// This will close the network request and read stream
|
|
90
|
+
const closeReader = () => __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
try {
|
|
92
|
+
yield reader.cancel();
|
|
93
|
+
}
|
|
94
|
+
catch (ex) {
|
|
95
|
+
// an error will throw if the reader hasn't been used yet
|
|
96
|
+
}
|
|
97
|
+
reader.releaseLock();
|
|
98
|
+
});
|
|
99
|
+
signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', () => {
|
|
100
|
+
closeReader();
|
|
101
|
+
});
|
|
102
|
+
const outputStream = new ReadableStream({
|
|
103
|
+
start: (controller) => {
|
|
104
|
+
const processStream = () => __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
while (!(signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
106
|
+
try {
|
|
107
|
+
const { done, value } = yield reader.read();
|
|
108
|
+
// When no more data needs to be consumed, close the stream
|
|
109
|
+
if (done) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
// Enqueue the next data chunk into our target stream
|
|
113
|
+
controller.enqueue(value);
|
|
114
|
+
}
|
|
115
|
+
catch (ex) {
|
|
116
|
+
this.logger.error('Caught exception when reading sync stream', ex);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!(signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
121
|
+
// Close the downstream readable stream
|
|
122
|
+
yield closeReader();
|
|
123
|
+
}
|
|
124
|
+
controller.close();
|
|
125
|
+
});
|
|
126
|
+
processStream();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
// Create a new response out of the intercepted stream
|
|
130
|
+
return new Response(outputStream).body;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AbstractStreamingSyncImplementation, AbstractStreamingSyncImplementationOptions, LockOptions } from '@powersync/common';
|
|
2
|
+
export interface WebStreamingSyncImplementationOptions extends AbstractStreamingSyncImplementationOptions {
|
|
3
|
+
flags?: {
|
|
4
|
+
broadcastLogs?: boolean;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export declare class WebStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
|
|
8
|
+
constructor(options: WebStreamingSyncImplementationOptions);
|
|
9
|
+
get webOptions(): WebStreamingSyncImplementationOptions;
|
|
10
|
+
obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AbstractStreamingSyncImplementation, LockType } from '@powersync/common';
|
|
2
|
+
export class WebStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
// Super will store and provide default values for options
|
|
5
|
+
super(options);
|
|
6
|
+
}
|
|
7
|
+
get webOptions() {
|
|
8
|
+
return this.options;
|
|
9
|
+
}
|
|
10
|
+
obtainLock(lockOptions) {
|
|
11
|
+
const identifier = `streaming-sync-${lockOptions.type}-${this.webOptions.identifier}`;
|
|
12
|
+
lockOptions.type == LockType.SYNC && console.debug('requesting lock for ', identifier);
|
|
13
|
+
return navigator.locks.request(identifier, { signal: lockOptions.signal }, lockOptions.callback);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from '@powersync/common';
|
|
2
|
+
export * from './db/PowerSyncDatabase';
|
|
3
|
+
export * from './db/sync/WebRemote';
|
|
4
|
+
export * from './db/sync/WebStreamingSyncImplementation';
|
|
5
|
+
export * from './db/sync/SharedWebStreamingSyncImplementation';
|
|
6
|
+
export * from './db/adapters/wa-sqlite/WASQLiteDBAdapter';
|
|
7
|
+
export * from './db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory';
|
|
8
|
+
export * from './db/adapters/AbstractWebPowerSyncDatabaseOpenFactory';
|
package/lib/src/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from '@powersync/common';
|
|
2
|
+
export * from './db/PowerSyncDatabase';
|
|
3
|
+
export * from './db/sync/WebRemote';
|
|
4
|
+
export * from './db/sync/WebStreamingSyncImplementation';
|
|
5
|
+
export * from './db/sync/SharedWebStreamingSyncImplementation';
|
|
6
|
+
export * from './db/adapters/wa-sqlite/WASQLiteDBAdapter';
|
|
7
|
+
export * from './db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory';
|
|
8
|
+
export * from './db/adapters/AbstractWebPowerSyncDatabaseOpenFactory';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@journeyapps/wa-sqlite';
|