@powersync/web 0.5.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.
- package/LICENSE +201 -0
- package/README.md +55 -0
- package/lib/src/db/PowerSyncDatabase.d.ts +43 -0
- package/lib/src/db/PowerSyncDatabase.js +92 -0
- package/lib/src/db/PowerSyncDatabase.js.map +1 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.d.ts +23 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +57 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js.map +1 -0
- package/lib/src/db/adapters/SSRDBAdapter.d.ts +29 -0
- package/lib/src/db/adapters/SSRDBAdapter.js +86 -0
- package/lib/src/db/adapters/SSRDBAdapter.js.map +1 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +56 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +197 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js.map +1 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.d.ts +6 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +12 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js.map +1 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.d.ts +8 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +14 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js.map +1 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +47 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +188 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js.map +1 -0
- package/lib/src/db/sync/WebRemote.d.ts +6 -0
- package/lib/src/db/sync/WebRemote.js +134 -0
- package/lib/src/db/sync/WebRemote.js.map +1 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +11 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.js +16 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.js.map +1 -0
- package/lib/src/index.d.ts +8 -0
- package/lib/src/index.js +9 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/worker/db/SharedWASQLiteDB.worker.d.ts +1 -0
- package/lib/src/worker/db/SharedWASQLiteDB.worker.js +58 -0
- package/lib/src/worker/db/SharedWASQLiteDB.worker.js.map +1 -0
- package/lib/src/worker/db/WASQLiteDB.worker.d.ts +1 -0
- package/lib/src/worker/db/WASQLiteDB.worker.js +13 -0
- package/lib/src/worker/db/WASQLiteDB.worker.js.map +1 -0
- package/lib/src/worker/db/open-db.d.ts +20 -0
- package/lib/src/worker/db/open-db.js +193 -0
- package/lib/src/worker/db/open-db.js.map +1 -0
- package/lib/src/worker/db/open-worker-database.d.ts +11 -0
- package/lib/src/worker/db/open-worker-database.js +31 -0
- package/lib/src/worker/db/open-worker-database.js.map +1 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +17 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js +6 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js.map +1 -0
- package/lib/src/worker/sync/BroadcastLogger.d.ts +37 -0
- package/lib/src/worker/sync/BroadcastLogger.js +108 -0
- package/lib/src/worker/sync/BroadcastLogger.js.map +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +88 -0
- package/lib/src/worker/sync/SharedSyncImplementation.js +248 -0
- package/lib/src/worker/sync/SharedSyncImplementation.js.map +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.d.ts +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js +23 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
}
|
|
134
|
+
//# sourceMappingURL=WebRemote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebRemote.js","sourceRoot":"","sources":["../../../../src/db/sync/WebRemote.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnE,MAAM,OAAO,SAAU,SAAQ,cAAc;IACrC,IAAI;6DAAC,IAAY,EAAE,IAAS,EAAE,UAAkC,EAAE;YACtE,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;YAE9C,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;gBACnC,MAAM,EAAE,MAAM;gBACd,OAAO,kCACF,OAAO,GACP,OAAO,CAAC,OAAO,CACnB;gBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;aAC3B,CAAC,CAAC;YAEH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,MAAM,IAAI,KAAK,CAAC,YAAY,GAAG,CAAC,MAAM,MAAM,GAAG,CAAC,UAAU,oBAAoB,IAAI,KAAK,MAAM,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;YAC9G,CAAC;YAED,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC;QACpB,CAAC;KAAA;IAEK,GAAG,CAAC,IAAY,EAAE,OAAgC;;YACtD,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;YAE9C,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;gBACnC,MAAM,EAAE,KAAK;gBACb,OAAO,kCACF,OAAO,GACP,OAAO,CAAC,OAAO,CACnB;aACF,CAA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B,MAAM,aAAa,GAAG,GAAS,EAAE;wBAC/B,OAAO,CAAC,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,CAAA,EAAE,CAAC;4BACxB,IAAI,CAAC;gCACH,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;gCAC5C,2DAA2D;gCAC3D,IAAI,IAAI,EAAE,CAAC;oCACT,MAAM;gCACR,CAAC;gCACD,qDAAqD;gCACrD,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;4BAC5B,CAAC;4BAAC,OAAO,EAAE,EAAE,CAAC;gCACZ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,2CAA2C,EAAE,EAAE,CAAC,CAAC;gCACnE,MAAM;4BACR,CAAC;wBACH,CAAC;wBACD,IAAI,CAAC,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,CAAA,EAAE,CAAC;4BACrB,uCAAuC;4BACvC,MAAM,WAAW,EAAE,CAAC;wBACtB,CAAC;wBACD,UAAU,CAAC,KAAK,EAAE,CAAC;oBACrB,CAAC,CAAA,CAAC;oBACF,aAAa,EAAE,CAAC;gBAClB,CAAC;aACF,CAAC,CAAC;YAEH,sDAAsD;YACtD,OAAO,IAAI,QAAQ,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC;QACzC,CAAC;KAAA;CACF"}
|
|
@@ -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,16 @@
|
|
|
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
|
+
}
|
|
16
|
+
//# sourceMappingURL=WebStreamingSyncImplementation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebStreamingSyncImplementation.js","sourceRoot":"","sources":["../../../../src/db/sync/WebStreamingSyncImplementation.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mCAAmC,EAGnC,QAAQ,EACT,MAAM,mBAAmB,CAAC;AAQ3B,MAAM,OAAO,8BAA+B,SAAQ,mCAAmC;IACrF,YAAY,OAA8C;QACxD,0DAA0D;QAC1D,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,OAAgD,CAAC;IAC/D,CAAC;IAED,UAAU,CAAI,WAA2B;QACvC,MAAM,UAAU,GAAG,kBAAkB,WAAW,CAAC,IAAI,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,EAAE,CAAC;QACtF,WAAW,CAAC,IAAI,IAAI,QAAQ,CAAC,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,sBAAsB,EAAE,UAAU,CAAC,CAAC;QACvF,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IACnG,CAAC;CACF"}
|
|
@@ -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,9 @@
|
|
|
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';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,0CAA0C,CAAC;AACzD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,2CAA2C,CAAC;AAC1D,cAAc,8DAA8D,CAAC;AAC7E,cAAc,uDAAuD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@journeyapps/wa-sqlite';
|
|
@@ -0,0 +1,58 @@
|
|
|
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 '@journeyapps/wa-sqlite';
|
|
11
|
+
import * as Comlink from 'comlink';
|
|
12
|
+
import { v4 as uuid } from 'uuid';
|
|
13
|
+
import { _openDB } from './open-db';
|
|
14
|
+
const _self = self;
|
|
15
|
+
const DBMap = new Map();
|
|
16
|
+
const OPEN_DB_LOCK = 'open-wasqlite-db';
|
|
17
|
+
const openDB = (dbFileName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
// Prevent multiple simultaneous opens from causing race conditions
|
|
19
|
+
return navigator.locks.request(OPEN_DB_LOCK, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const clientId = uuid();
|
|
21
|
+
if (!DBMap.has(dbFileName)) {
|
|
22
|
+
const clientIds = new Set();
|
|
23
|
+
const connection = yield _openDB(dbFileName);
|
|
24
|
+
DBMap.set(dbFileName, {
|
|
25
|
+
clientIds,
|
|
26
|
+
db: connection
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const dbEntry = DBMap.get(dbFileName);
|
|
30
|
+
dbEntry.clientIds.add(clientId);
|
|
31
|
+
const { db } = dbEntry;
|
|
32
|
+
const wrappedConnection = Object.assign(Object.assign({}, db), { close: Comlink.proxy(() => {
|
|
33
|
+
var _a;
|
|
34
|
+
const { clientIds } = dbEntry;
|
|
35
|
+
clientIds.delete(clientId);
|
|
36
|
+
if (clientIds.size == 0) {
|
|
37
|
+
console.debug(`Closing connection to ${dbFileName}.`);
|
|
38
|
+
DBMap.delete(dbFileName);
|
|
39
|
+
return (_a = db.close) === null || _a === void 0 ? void 0 : _a.call(db);
|
|
40
|
+
}
|
|
41
|
+
console.debug(`Connection to ${dbFileName} not closed yet due to active clients.`);
|
|
42
|
+
}) });
|
|
43
|
+
return Comlink.proxy(wrappedConnection);
|
|
44
|
+
}));
|
|
45
|
+
});
|
|
46
|
+
_self.onconnect = function (event) {
|
|
47
|
+
const port = event.ports[0];
|
|
48
|
+
console.debug('Exposing db on port', port);
|
|
49
|
+
Comlink.expose(openDB, port);
|
|
50
|
+
};
|
|
51
|
+
addEventListener('unload', () => {
|
|
52
|
+
Array.from(DBMap.values()).forEach((dbConnection) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
var _a;
|
|
54
|
+
const db = yield dbConnection.db;
|
|
55
|
+
(_a = db.close) === null || _a === void 0 ? void 0 : _a.call(db);
|
|
56
|
+
}));
|
|
57
|
+
});
|
|
58
|
+
//# sourceMappingURL=SharedWASQLiteDB.worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedWASQLiteDB.worker.js","sourceRoot":"","sources":["../../../../src/worker/db/SharedWASQLiteDB.worker.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,wBAAwB,CAAC;AAEhC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAqB,OAAO,EAAE,MAAM,WAAW,CAAC;AAWvD,MAAM,KAAK,GAA4B,IAAW,CAAC;AAEnD,MAAM,KAAK,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,OAAO,OAAO,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAC1C,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAA,CAAC;AAEF,KAAK,CAAC,SAAS,GAAG,UAAU,KAA2B;IACrD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,IAAI,CAAC,CAAC;IAC3C,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAC/B,CAAC,CAAC;AAEF,gBAAgB,CAAC,QAAQ,EAAE,GAAG,EAAE;IAC9B,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAO,YAAY,EAAE,EAAE;;QACxD,MAAM,EAAE,GAAG,MAAM,YAAY,CAAC,EAAE,CAAC;QACjC,MAAA,EAAE,CAAC,KAAK,kDAAI,CAAC;IACf,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
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 { _openDB } from './open-db';
|
|
12
|
+
Comlink.expose((dbFileName) => __awaiter(void 0, void 0, void 0, function* () { return Comlink.proxy(yield _openDB(dbFileName)); }));
|
|
13
|
+
//# sourceMappingURL=WASQLiteDB.worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WASQLiteDB.worker.js","sourceRoot":"","sources":["../../../../src/worker/db/WASQLiteDB.worker.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,CAAC,MAAM,CAAC,CAAO,UAAkB,EAAE,EAAE,kDAAC,OAAA,OAAO,CAAC,KAAK,CAAC,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC,CAAA,GAAA,CAAC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import '@journeyapps/wa-sqlite';
|
|
2
|
+
import { QueryResult } from '@powersync/common';
|
|
3
|
+
export type WASQLExecuteResult = Omit<QueryResult, 'rows'> & {
|
|
4
|
+
rows: {
|
|
5
|
+
_array: any[];
|
|
6
|
+
length: number;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export type DBWorkerInterface = {
|
|
10
|
+
close?: () => void;
|
|
11
|
+
execute: WASQLiteExecuteMethod;
|
|
12
|
+
executeBatch: WASQLiteExecuteBatchMethod;
|
|
13
|
+
registerOnTableChange: (callback: OnTableChangeCallback) => void;
|
|
14
|
+
};
|
|
15
|
+
export type WASQLiteExecuteMethod = (sql: string, params?: any[]) => Promise<WASQLExecuteResult>;
|
|
16
|
+
export type WASQLiteExecuteBatchMethod = (sql: string, params?: any[]) => Promise<WASQLExecuteResult>;
|
|
17
|
+
export type OnTableChangeCallback = (opType: number, tableName: string, rowId: number) => void;
|
|
18
|
+
export type OpenDB = (dbFileName: string) => DBWorkerInterface;
|
|
19
|
+
export type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];
|
|
20
|
+
export declare function _openDB(dbFileName: string): Promise<DBWorkerInterface>;
|
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
11
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
12
|
+
var m = o[Symbol.asyncIterator], i;
|
|
13
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
+
};
|
|
17
|
+
import * as SQLite from '@journeyapps/wa-sqlite';
|
|
18
|
+
import '@journeyapps/wa-sqlite';
|
|
19
|
+
import * as Comlink from 'comlink';
|
|
20
|
+
import { v4 as uuid } from 'uuid';
|
|
21
|
+
export function _openDB(dbFileName) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const { default: moduleFactory } = yield import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');
|
|
24
|
+
const module = yield moduleFactory();
|
|
25
|
+
const sqlite3 = SQLite.Factory(module);
|
|
26
|
+
const { IDBBatchAtomicVFS } = yield import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');
|
|
27
|
+
const vfs = new IDBBatchAtomicVFS(dbFileName);
|
|
28
|
+
sqlite3.vfs_register(vfs, true);
|
|
29
|
+
const db = yield sqlite3.open_v2(dbFileName);
|
|
30
|
+
/**
|
|
31
|
+
* Listeners are exclusive to the DB connection.
|
|
32
|
+
*/
|
|
33
|
+
const listeners = new Map();
|
|
34
|
+
sqlite3.register_table_onchange_hook(db, (opType, tableName, rowId) => {
|
|
35
|
+
Array.from(listeners.values()).forEach((l) => l(opType, tableName, rowId));
|
|
36
|
+
});
|
|
37
|
+
const registerOnTableChange = (callback) => {
|
|
38
|
+
const id = uuid();
|
|
39
|
+
listeners.set(id, callback);
|
|
40
|
+
return Comlink.proxy(() => {
|
|
41
|
+
listeners.delete(id);
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* This executes single SQL statements inside a requested lock.
|
|
46
|
+
*/
|
|
47
|
+
const execute = (sql, bindings) => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
// Running multiple statements on the same connection concurrently should not be allowed
|
|
49
|
+
return _acquireExecuteLock(() => __awaiter(this, void 0, void 0, function* () {
|
|
50
|
+
return executeSingleStatement(sql, bindings);
|
|
51
|
+
}));
|
|
52
|
+
});
|
|
53
|
+
/**
|
|
54
|
+
* This requests a lock for executing statements.
|
|
55
|
+
* Should only be used interanlly.
|
|
56
|
+
*/
|
|
57
|
+
const _acquireExecuteLock = (callback) => {
|
|
58
|
+
return navigator.locks.request(`db-execute-${dbFileName}`, callback);
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* This executes a single statement using SQLite3.
|
|
62
|
+
*/
|
|
63
|
+
const executeSingleStatement = (sql, bindings) => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
var _a, e_1, _b, _c;
|
|
65
|
+
const results = [];
|
|
66
|
+
try {
|
|
67
|
+
for (var _d = true, _e = __asyncValues(sqlite3.statements(db, sql)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
68
|
+
_c = _f.value;
|
|
69
|
+
_d = false;
|
|
70
|
+
const stmt = _c;
|
|
71
|
+
let columns;
|
|
72
|
+
const wrappedBindings = bindings ? [bindings] : [[]];
|
|
73
|
+
for (const binding of wrappedBindings) {
|
|
74
|
+
// TODO not sure why this is needed currently, but booleans break
|
|
75
|
+
binding.forEach((b, index, arr) => {
|
|
76
|
+
if (typeof b == 'boolean') {
|
|
77
|
+
arr[index] = b ? 1 : 0;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
sqlite3.reset(stmt);
|
|
81
|
+
if (bindings) {
|
|
82
|
+
sqlite3.bind_collection(stmt, binding);
|
|
83
|
+
}
|
|
84
|
+
const rows = [];
|
|
85
|
+
while ((yield sqlite3.step(stmt)) === SQLite.SQLITE_ROW) {
|
|
86
|
+
const row = sqlite3.row(stmt);
|
|
87
|
+
rows.push(row);
|
|
88
|
+
}
|
|
89
|
+
columns = columns !== null && columns !== void 0 ? columns : sqlite3.column_names(stmt);
|
|
90
|
+
if (columns.length) {
|
|
91
|
+
results.push({ columns, rows });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// When binding parameters, only a single statement is executed.
|
|
95
|
+
if (bindings) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
101
|
+
finally {
|
|
102
|
+
try {
|
|
103
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
104
|
+
}
|
|
105
|
+
finally { if (e_1) throw e_1.error; }
|
|
106
|
+
}
|
|
107
|
+
const rows = [];
|
|
108
|
+
for (const resultset of results) {
|
|
109
|
+
for (const row of resultset.rows) {
|
|
110
|
+
const outRow = {};
|
|
111
|
+
resultset.columns.forEach((key, index) => {
|
|
112
|
+
outRow[key] = row[index];
|
|
113
|
+
});
|
|
114
|
+
rows.push(outRow);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const result = {
|
|
118
|
+
insertId: sqlite3.last_insert_id(db),
|
|
119
|
+
rowsAffected: sqlite3.changes(db),
|
|
120
|
+
rows: {
|
|
121
|
+
_array: rows,
|
|
122
|
+
length: rows.length
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return result;
|
|
126
|
+
});
|
|
127
|
+
/**
|
|
128
|
+
* This executes SQL statements in a batch.
|
|
129
|
+
*/
|
|
130
|
+
const executeBatch = (sql, bindings) => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
return _acquireExecuteLock(() => __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
let affectedRows = 0;
|
|
133
|
+
const str = sqlite3.str_new(db, sql);
|
|
134
|
+
const query = sqlite3.str_value(str);
|
|
135
|
+
try {
|
|
136
|
+
yield executeSingleStatement('BEGIN TRANSACTION');
|
|
137
|
+
//Prepare statement once
|
|
138
|
+
const prepared = yield sqlite3.prepare_v2(db, query);
|
|
139
|
+
if (prepared === null) {
|
|
140
|
+
return {
|
|
141
|
+
rowsAffected: 0
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const wrappedBindings = bindings ? bindings : [];
|
|
145
|
+
for (const binding of wrappedBindings) {
|
|
146
|
+
// TODO not sure why this is needed currently, but booleans break
|
|
147
|
+
for (let i = 0; i < binding.length; i++) {
|
|
148
|
+
const b = binding[i];
|
|
149
|
+
if (typeof b == 'boolean') {
|
|
150
|
+
binding[i] = b ? 1 : 0;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
//Reset bindings
|
|
154
|
+
sqlite3.reset(prepared.stmt);
|
|
155
|
+
if (bindings) {
|
|
156
|
+
sqlite3.bind_collection(prepared.stmt, binding);
|
|
157
|
+
}
|
|
158
|
+
const result = yield sqlite3.step(prepared.stmt);
|
|
159
|
+
if (result === SQLite.SQLITE_DONE) {
|
|
160
|
+
//The value returned by sqlite3_changes() immediately after an INSERT, UPDATE or DELETE statement run on a view is always zero.
|
|
161
|
+
affectedRows += sqlite3.changes(db);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//Finalize prepared statement
|
|
165
|
+
yield sqlite3.finalize(prepared.stmt);
|
|
166
|
+
yield executeSingleStatement('COMMIT');
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
yield executeSingleStatement('ROLLBACK');
|
|
170
|
+
return {
|
|
171
|
+
rowsAffected: 0
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
finally {
|
|
175
|
+
sqlite3.str_finish(str);
|
|
176
|
+
}
|
|
177
|
+
const result = {
|
|
178
|
+
rowsAffected: affectedRows
|
|
179
|
+
};
|
|
180
|
+
return result;
|
|
181
|
+
}));
|
|
182
|
+
});
|
|
183
|
+
return {
|
|
184
|
+
execute: Comlink.proxy(execute),
|
|
185
|
+
executeBatch: Comlink.proxy(executeBatch),
|
|
186
|
+
registerOnTableChange: Comlink.proxy(registerOnTableChange),
|
|
187
|
+
close: Comlink.proxy(() => {
|
|
188
|
+
sqlite3.close(db);
|
|
189
|
+
})
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=open-db.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open-db.js","sourceRoot":"","sources":["../../../../src/worker/db/open-db.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,MAAM,wBAAwB,CAAC;AACjD,OAAO,wBAAwB,CAAC;AAChC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAyBlC,MAAM,UAAgB,OAAO,CAAC,UAAkB;;QAC9C,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,MAAM,MAAM,CAAC,iDAAiD,C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of database connections
|
|
5
|
+
*/
|
|
6
|
+
export declare function openWorkerDatabasePort(workerIdentifier: string, multipleTabs?: boolean): MessagePort | Worker;
|
|
7
|
+
/**
|
|
8
|
+
* @returns A function which allows for opening database connections inside
|
|
9
|
+
* a worker.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getWorkerDatabaseOpener(workerIdentifier: string, multipleTabs?: boolean): Comlink.Remote<OpenDB>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
/**
|
|
3
|
+
* Opens a shared or dedicated worker which exposes opening of database connections
|
|
4
|
+
*/
|
|
5
|
+
export function openWorkerDatabasePort(workerIdentifier, multipleTabs = true) {
|
|
6
|
+
/**
|
|
7
|
+
* Webpack V5 can bundle the worker automatically if the full Worker constructor syntax is used
|
|
8
|
+
* https://webpack.js.org/guides/web-workers/
|
|
9
|
+
* This enables multi tab support by default, but falls back if SharedWorker is not available
|
|
10
|
+
* (in the case of Android)
|
|
11
|
+
*/
|
|
12
|
+
return multipleTabs
|
|
13
|
+
? new SharedWorker(new URL('./SharedWASQLiteDB.worker.js', import.meta.url), {
|
|
14
|
+
/* @vite-ignore */
|
|
15
|
+
name: `shared-DB-worker-${workerIdentifier}`,
|
|
16
|
+
type: 'module'
|
|
17
|
+
}).port
|
|
18
|
+
: new Worker(new URL('./WASQLiteDB.worker.js', import.meta.url), {
|
|
19
|
+
/* @vite-ignore */
|
|
20
|
+
name: `DB-worker-${workerIdentifier}`,
|
|
21
|
+
type: 'module'
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @returns A function which allows for opening database connections inside
|
|
26
|
+
* a worker.
|
|
27
|
+
*/
|
|
28
|
+
export function getWorkerDatabaseOpener(workerIdentifier, multipleTabs = true) {
|
|
29
|
+
return Comlink.wrap(openWorkerDatabasePort(workerIdentifier, multipleTabs));
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=open-worker-database.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open-worker-database.js","sourceRoot":"","sources":["../../../../src/worker/db/open-worker-database.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAGnC;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,gBAAwB,EAAE,YAAY,GAAG,IAAI;IAClF;;;;;OAKG;IACH,OAAO,YAAY;QACjB,CAAC,CAAC,IAAI,YAAY,CAAC,IAAI,GAAG,CAAC,8BAA8B,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YACzE,kBAAkB;YAClB,IAAI,EAAE,oBAAoB,gBAAgB,EAAE;YAC5C,IAAI,EAAE,QAAQ;SACf,CAAC,CAAC,IAAI;QACT,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,GAAG,CAAC,wBAAwB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YAC7D,kBAAkB;YAClB,IAAI,EAAE,aAAa,gBAAgB,EAAE;YACrC,IAAI,EAAE,QAAQ;SACf,CAAC,CAAC;AACT,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,gBAAwB,EAAE,YAAY,GAAG,IAAI;IACnF,OAAO,OAAO,CAAC,IAAI,CAAS,sBAAsB,CAAC,gBAAgB,EAAE,YAAY,CAAC,CAAC,CAAC;AACtF,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PowerSyncCredentials, SyncStatusOptions } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* The client side port should provide these methods.
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class AbstractSharedSyncClientProvider {
|
|
6
|
+
abstract fetchCredentials(): Promise<PowerSyncCredentials | null>;
|
|
7
|
+
abstract uploadCrud(): Promise<void>;
|
|
8
|
+
abstract statusChanged(status: SyncStatusOptions): void;
|
|
9
|
+
abstract trace(...x: any[]): void;
|
|
10
|
+
abstract debug(...x: any[]): void;
|
|
11
|
+
abstract info(...x: any[]): void;
|
|
12
|
+
abstract log(...x: any[]): void;
|
|
13
|
+
abstract warn(...x: any[]): void;
|
|
14
|
+
abstract error(...x: any[]): void;
|
|
15
|
+
abstract time(label: string): void;
|
|
16
|
+
abstract timeEnd(label: string): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AbstractSharedSyncClientProvider.js","sourceRoot":"","sources":["../../../../src/worker/sync/AbstractSharedSyncClientProvider.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,OAAgB,gCAAgC;CAarD"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ILogLevel, ILogger } from 'js-logger';
|
|
2
|
+
import { type WrappedSyncPort } from './SharedSyncImplementation';
|
|
3
|
+
/**
|
|
4
|
+
* Broadcasts logs to all clients
|
|
5
|
+
*/
|
|
6
|
+
export declare class BroadcastLogger implements ILogger {
|
|
7
|
+
protected clients: WrappedSyncPort[];
|
|
8
|
+
TRACE: ILogLevel;
|
|
9
|
+
DEBUG: ILogLevel;
|
|
10
|
+
INFO: ILogLevel;
|
|
11
|
+
TIME: ILogLevel;
|
|
12
|
+
WARN: ILogLevel;
|
|
13
|
+
ERROR: ILogLevel;
|
|
14
|
+
OFF: ILogLevel;
|
|
15
|
+
constructor(clients: WrappedSyncPort[]);
|
|
16
|
+
trace(...x: any[]): void;
|
|
17
|
+
debug(...x: any[]): void;
|
|
18
|
+
info(...x: any[]): void;
|
|
19
|
+
log(...x: any[]): void;
|
|
20
|
+
warn(...x: any[]): void;
|
|
21
|
+
error(...x: any[]): void;
|
|
22
|
+
time(label: string): void;
|
|
23
|
+
timeEnd(label: string): void;
|
|
24
|
+
setLevel(level: ILogLevel): void;
|
|
25
|
+
getLevel(): ILogLevel;
|
|
26
|
+
enabledFor(level: ILogLevel): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Iterates all clients, catches individual client exceptions
|
|
29
|
+
* and proceeds to execute for all clients.
|
|
30
|
+
*/
|
|
31
|
+
protected iterateClients(callback: (client: WrappedSyncPort) => Promise<void>): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Guards against any logging errors.
|
|
34
|
+
* We don't want a logging exception to cause further issues upstream
|
|
35
|
+
*/
|
|
36
|
+
protected sanitizeArgs(x: any[]): any[];
|
|
37
|
+
}
|