@vrtmrz/livesync-commonlib 0.5.0 → 0.6.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/dist/common/LSError.d.ts +13 -0
- package/dist/common/LSError.js +19 -1
- package/dist/diagnostics/local.d.ts +7 -2
- package/dist/interfaces/DatabaseFileAccess.d.ts +20 -4
- package/dist/interfaces/FileHandler.d.ts +67 -1
- package/dist/interfaces/FileHandler.js +16 -0
- package/dist/interfaces/StorageEventManager.d.ts +7 -0
- package/dist/managers/ChunkFetcher.d.ts +16 -0
- package/dist/managers/ChunkFetcher.js +190 -112
- package/dist/managers/EntryManager/EntryManager.d.ts +7 -0
- package/dist/managers/EntryManager/EntryManager.js +6 -0
- package/dist/managers/EntryManager/EntryManagerImpls.d.ts +9 -5
- package/dist/managers/EntryManager/EntryManagerImpls.js +103 -76
- package/dist/managers/LayeredChunkManager/ArrivalWaitLayer.d.ts +7 -3
- package/dist/managers/LayeredChunkManager/ArrivalWaitLayer.js +83 -16
- package/dist/managers/LayeredChunkManager/DatabaseReadLayer.d.ts +3 -2
- package/dist/managers/LayeredChunkManager/DatabaseReadLayer.js +38 -36
- package/dist/managers/LayeredChunkManager/types.d.ts +15 -1
- package/dist/managers/LayeredChunkManager.d.ts +4 -0
- package/dist/managers/LayeredChunkManager.js +19 -6
- package/dist/managers/LiveSyncManagers.d.ts +2 -0
- package/dist/managers/LiveSyncManagers.js +6 -0
- package/dist/managers/StorageEventManager.d.ts +22 -0
- package/dist/managers/StorageEventManager.js +48 -1
- package/dist/pouchdb/LiveSyncLocalDB.d.ts +32 -0
- package/dist/pouchdb/LiveSyncLocalDB.js +72 -18
- package/dist/pouchdb/LocalDatabaseReadScope.d.ts +24 -0
- package/dist/pouchdb/LocalDatabaseReadScope.js +62 -0
- package/dist/pouchdb/encryption.policy.js +2 -1
- package/dist/pouchdb/pathIdentityCompatibility.d.ts +6 -2
- package/dist/pouchdb/pathIdentityCompatibility.js +15 -10
- package/dist/replication/UnsupportedEncryptedDocumentFormatError.d.ts +13 -0
- package/dist/replication/UnsupportedEncryptedDocumentFormatError.js +28 -0
- package/dist/replication/contentRead.d.ts +3 -0
- package/dist/replication/contentRead.js +4 -0
- package/dist/replication/couchdb/LiveSyncReplicator.d.ts +11 -2
- package/dist/replication/couchdb/LiveSyncReplicator.js +207 -131
- package/dist/replication/index.d.ts +6 -1
- package/dist/replication/index.js +20 -1
- package/dist/serviceFeatures/offlineScanner/files.d.ts +1 -1
- package/dist/serviceFeatures/offlineScanner/files.js +27 -7
- package/dist/serviceFeatures/offlineScanner/scan.d.ts +5 -1
- package/dist/serviceFeatures/offlineScanner/scan.js +2 -1
- package/dist/serviceFeatures/offlineScanner.d.ts +1 -1
- package/dist/serviceFeatures/offlineScanner.js +10 -2
- package/dist/serviceModules/ServiceDatabaseFileAccessBase.d.ts +13 -0
- package/dist/serviceModules/ServiceDatabaseFileAccessBase.js +190 -123
- package/dist/serviceModules/ServiceFileHandlerBase/deletion.js +17 -7
- package/dist/serviceModules/ServiceFileHandlerBase/events.d.ts +3 -2
- package/dist/serviceModules/ServiceFileHandlerBase/events.js +9 -9
- package/dist/serviceModules/ServiceFileHandlerBase/protection.js +24 -9
- package/dist/serviceModules/ServiceFileHandlerBase/provenance.d.ts +10 -0
- package/dist/serviceModules/ServiceFileHandlerBase/provenance.js +28 -4
- package/dist/serviceModules/ServiceFileHandlerBase/reflection.d.ts +11 -5
- package/dist/serviceModules/ServiceFileHandlerBase/reflection.js +144 -58
- package/dist/serviceModules/ServiceFileHandlerBase/saving.js +16 -2
- package/dist/serviceModules/ServiceFileHandlerBase/scanning.js +1 -0
- package/dist/serviceModules/ServiceFileHandlerBase.d.ts +7 -2
- package/dist/serviceModules/ServiceFileHandlerBase.js +13 -4
- package/dist/services/base/ControlService.js +1 -5
- package/dist/services/base/DatabaseEventService.d.ts +3 -0
- package/dist/services/base/DatabaseEventService.js +3 -0
- package/dist/services/base/DatabaseService.d.ts +15 -0
- package/dist/services/base/DatabaseService.js +85 -4
- package/dist/services/base/IService.d.ts +18 -1
- package/dist/services/base/KeyValueDBService.d.ts +5 -4
- package/dist/services/base/KeyValueDBService.js +46 -48
- package/dist/services/base/ReplicationService.d.ts +4 -1
- package/dist/services/base/ReplicationService.js +8 -1
- package/dist/services/base/VaultScanResult.d.ts +5 -0
- package/dist/services/lib/HandlerUtils.d.ts +40 -5
- package/dist/services/lib/HandlerUtils.js +52 -4
- package/docs/development.md +8 -0
- package/docs/offline-scanner.md +57 -3
- package/docs/releasing.md +43 -0
- package/package.json +1 -1
|
@@ -4,10 +4,10 @@ import {
|
|
|
4
4
|
LOG_LEVEL_VERBOSE,
|
|
5
5
|
SuffixDatabaseName
|
|
6
6
|
} from "../common/types.js";
|
|
7
|
-
import { Logger } from "../common/logger.js";
|
|
8
7
|
import { isErrorOfMissingDoc } from "./utils_couchdb.js";
|
|
9
8
|
import { EVENT_CHUNK_FETCHED } from "../managers/ChunkFetcher.js";
|
|
10
9
|
import { FallbackWeakRef } from "octagonal-wheels/common/polyfill";
|
|
10
|
+
import { LocalDatabaseReadScope } from "./LocalDatabaseReadScope.js";
|
|
11
11
|
import { LiveSyncManagers } from "../managers/LiveSyncManagers.js";
|
|
12
12
|
import { createInstanceLogFunction } from "../services/lib/logUtils.js";
|
|
13
13
|
const REMOTE_CHUNK_FETCHED = "remote-chunk-fetched";
|
|
@@ -31,8 +31,22 @@ class LiveSyncLocalDB {
|
|
|
31
31
|
auth;
|
|
32
32
|
dbname;
|
|
33
33
|
settings;
|
|
34
|
+
/**
|
|
35
|
+
* The raw PouchDB handle for replication, changes feeds, and maintenance.
|
|
36
|
+
*
|
|
37
|
+
* Direct reads bypass this owner's caller-wait retirement. Use
|
|
38
|
+
* {@link readDBEntry} for file contents, {@link getRaw} or {@link allDocsRaw}
|
|
39
|
+
* for raw documents, and {@link readFromDatabase} for other read-only Pouch
|
|
40
|
+
* operations. These entry points end caller waits when the owner retires;
|
|
41
|
+
* they do not cancel native I/O or automatically retry a failed operation.
|
|
42
|
+
*
|
|
43
|
+
* Integrations which require this handle must own their operation's
|
|
44
|
+
* retirement and completion. Keep writes and mixed read/write workflows
|
|
45
|
+
* outside {@link readFromDatabase}; started writes must actually finish.
|
|
46
|
+
*/
|
|
34
47
|
localDatabase;
|
|
35
48
|
_log;
|
|
49
|
+
readScope = new LocalDatabaseReadScope();
|
|
36
50
|
_managers;
|
|
37
51
|
get managers() {
|
|
38
52
|
if (!this._managers) {
|
|
@@ -71,9 +85,27 @@ class LiveSyncLocalDB {
|
|
|
71
85
|
onunload() {
|
|
72
86
|
void this.notifyDatabaseUnload();
|
|
73
87
|
}
|
|
88
|
+
/** End caller read waits before lifecycle hooks drain their owners; native IO is not cancelled. */
|
|
89
|
+
stopReads() {
|
|
90
|
+
this.readScope.close();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Run a read on the captured Pouch and end its caller wait when this owner retires.
|
|
94
|
+
*
|
|
95
|
+
* Use {@link getRaw} or {@link allDocsRaw} for document reads with known IDs.
|
|
96
|
+
* This callback also supports other read-only operations, including inspection
|
|
97
|
+
* of unvalidated stored IDs. It can continue after the caller wait rejects,
|
|
98
|
+
* so it must not perform writes or change application state after a read.
|
|
99
|
+
* No connection-health conclusion, recovery request, or retry is implied.
|
|
100
|
+
*/
|
|
101
|
+
readFromDatabase(operation) {
|
|
102
|
+
const database = this.localDatabase;
|
|
103
|
+
return this.readScope.run(() => operation(database));
|
|
104
|
+
}
|
|
74
105
|
async close() {
|
|
75
106
|
this._log("Database closed (by close)");
|
|
76
107
|
this.isReady = false;
|
|
108
|
+
this.stopReads();
|
|
77
109
|
this.offRemoteChunkFetchedHandler?.();
|
|
78
110
|
this.offRemoteChunkFetchedHandler = void 0;
|
|
79
111
|
await this.closeActiveReplication();
|
|
@@ -122,6 +154,7 @@ class LiveSyncLocalDB {
|
|
|
122
154
|
}
|
|
123
155
|
async rollbackFailedInitialisation(database, managers) {
|
|
124
156
|
this.isReady = false;
|
|
157
|
+
if (this.localDatabase === database) this.stopReads();
|
|
125
158
|
this.offRemoteChunkFetchedHandler?.();
|
|
126
159
|
this.offRemoteChunkFetchedHandler = void 0;
|
|
127
160
|
const closeCleanup = this.databaseCloseCleanup?.database === database ? this.databaseCloseCleanup : void 0;
|
|
@@ -156,22 +189,25 @@ class LiveSyncLocalDB {
|
|
|
156
189
|
async initializeDatabase() {
|
|
157
190
|
await this._prepareHashFunctions();
|
|
158
191
|
if (this.localDatabase != null) {
|
|
192
|
+
this.stopReads();
|
|
159
193
|
this.localDatabase.removeAllListeners();
|
|
160
194
|
await this.localDatabase.close();
|
|
161
195
|
}
|
|
162
196
|
this.localDatabase = null;
|
|
163
|
-
this.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
);
|
|
197
|
+
this.readScope = new LocalDatabaseReadScope();
|
|
198
|
+
this.localDatabase = (this.runtimeOptions.createPouchDBInstance ?? this.env.services.database.createPouchDBInstance.bind(this.env.services.database))(this.dbname + SuffixDatabaseName, {
|
|
199
|
+
auto_compaction: false,
|
|
200
|
+
revs_limit: 100,
|
|
201
|
+
deterministic_revs: true
|
|
202
|
+
});
|
|
171
203
|
this.activeReplicationClose = void 0;
|
|
172
204
|
this.databaseUnloadNotification = void 0;
|
|
205
|
+
const openedDatabase = this.localDatabase;
|
|
206
|
+
const readScope = this.readScope;
|
|
207
|
+
this.databaseCloseCleanup = void 0;
|
|
173
208
|
const manager = new LiveSyncManagers({
|
|
174
209
|
database: this.localDatabase,
|
|
210
|
+
readScope,
|
|
175
211
|
chunkFetchSource: this.runtimeOptions?.createChunkFetchSource?.(this.localDatabase),
|
|
176
212
|
APIService: this.env.services.API,
|
|
177
213
|
pathService: this.env.services.path,
|
|
@@ -180,10 +216,9 @@ class LiveSyncLocalDB {
|
|
|
180
216
|
databaseService: this.env.services.database
|
|
181
217
|
});
|
|
182
218
|
this._managers = manager;
|
|
183
|
-
const openedDatabase = this.localDatabase;
|
|
184
|
-
this.databaseCloseCleanup = void 0;
|
|
185
219
|
try {
|
|
186
|
-
|
|
220
|
+
const initialisationAccepted = await this.env.services.databaseEvents.onDatabaseInitialisation(this);
|
|
221
|
+
if (!initialisationAccepted) {
|
|
187
222
|
await this.rollbackFailedInitialisation(openedDatabase, manager);
|
|
188
223
|
this._log("Database initialisation was rejected by a required module.", LOG_LEVEL_NOTICE);
|
|
189
224
|
return false;
|
|
@@ -193,7 +228,8 @@ class LiveSyncLocalDB {
|
|
|
193
228
|
this._log("Database info", LOG_LEVEL_VERBOSE);
|
|
194
229
|
this._log(JSON.stringify(await openedDatabase.info(), null, 2), LOG_LEVEL_VERBOSE);
|
|
195
230
|
await manager.initialise();
|
|
196
|
-
openedDatabase.on("
|
|
231
|
+
openedDatabase.on("closed", () => {
|
|
232
|
+
readScope.close();
|
|
197
233
|
this._log("Database closed.");
|
|
198
234
|
this.isReady = false;
|
|
199
235
|
openedDatabase.removeAllListeners();
|
|
@@ -318,11 +354,11 @@ class LiveSyncLocalDB {
|
|
|
318
354
|
this.isReady = false;
|
|
319
355
|
await this.closeActiveReplication();
|
|
320
356
|
if (!await this.env.services.databaseEvents.onResetDatabase(this)) {
|
|
321
|
-
|
|
357
|
+
this._log("Database reset has been prevented or failed on some modules.", LOG_LEVEL_NOTICE);
|
|
322
358
|
return false;
|
|
323
359
|
}
|
|
324
360
|
await this.managers.teardownManagers();
|
|
325
|
-
|
|
361
|
+
this._log("Database closed for reset Database.");
|
|
326
362
|
await this.localDatabase.destroy();
|
|
327
363
|
this.localDatabase = null;
|
|
328
364
|
if (!await this.initializeDatabase()) {
|
|
@@ -434,13 +470,14 @@ class LiveSyncLocalDB {
|
|
|
434
470
|
return true;
|
|
435
471
|
} catch (ex) {
|
|
436
472
|
if (isErrorOfMissingDoc(ex)) {
|
|
437
|
-
|
|
473
|
+
this._log(`Remove revision: Missing target revision, ${docId}-${revision}`, LOG_LEVEL_VERBOSE);
|
|
438
474
|
}
|
|
439
475
|
}
|
|
440
476
|
return false;
|
|
441
477
|
}
|
|
478
|
+
/** Read a raw document by its known ID, with caller-wait retirement owned by this database. */
|
|
442
479
|
getRaw(docId, options) {
|
|
443
|
-
return this.
|
|
480
|
+
return this.readFromDatabase((database) => database.get(docId, options || {}));
|
|
444
481
|
}
|
|
445
482
|
removeRaw(docId, revision, options) {
|
|
446
483
|
return this.localDatabase.remove(docId, revision, options || {});
|
|
@@ -448,8 +485,9 @@ class LiveSyncLocalDB {
|
|
|
448
485
|
putRaw(doc, options) {
|
|
449
486
|
return this.localDatabase.put(doc, options || {});
|
|
450
487
|
}
|
|
488
|
+
/** Read a raw document listing with caller-wait retirement owned by this database. */
|
|
451
489
|
allDocsRaw(options) {
|
|
452
|
-
return this.
|
|
490
|
+
return this.readFromDatabase((database) => database.allDocs(options));
|
|
453
491
|
}
|
|
454
492
|
bulkDocsRaw(docs, options) {
|
|
455
493
|
return this.localDatabase.bulkDocs(docs, options || {});
|
|
@@ -462,9 +500,25 @@ class LiveSyncLocalDB {
|
|
|
462
500
|
return await this.managers.entryManager.getDBEntryMeta(path, opt, includeDeleted);
|
|
463
501
|
}
|
|
464
502
|
async getDBEntry(path, opt, dump = false, waitForReady = true, includeDeleted = false, localOnly = false) {
|
|
503
|
+
try {
|
|
504
|
+
return await this.readDBEntry(path, opt, dump, waitForReady, includeDeleted, localOnly);
|
|
505
|
+
} catch (error) {
|
|
506
|
+
this._log(error, LOG_LEVEL_VERBOSE);
|
|
507
|
+
return false;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
async readDBEntry(path, opt, dump = false, waitForReady = true, includeDeleted = false, localOnly = false) {
|
|
465
511
|
return await this.managers.entryManager.getDBEntry(path, opt, dump, waitForReady, includeDeleted, localOnly);
|
|
466
512
|
}
|
|
467
513
|
async getDBEntryFromMeta(meta, dump = false, waitForReady = true, localOnly = false) {
|
|
514
|
+
try {
|
|
515
|
+
return await this.readDBEntryFromMeta(meta, dump, waitForReady, localOnly);
|
|
516
|
+
} catch (error) {
|
|
517
|
+
this._log(error, LOG_LEVEL_VERBOSE);
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
async readDBEntryFromMeta(meta, dump = false, waitForReady = true, localOnly = false) {
|
|
468
522
|
return await this.managers.entryManager.getDBEntryFromMeta(meta, dump, waitForReady, localOnly);
|
|
469
523
|
}
|
|
470
524
|
async deleteDBEntry(path, opt) {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Owns caller waits for reads tied to one local database lifetime.
|
|
3
|
+
*
|
|
4
|
+
* Closing the scope rejects pending caller waits and prevents new operations from
|
|
5
|
+
* starting. It does not cancel database I/O which has already started.
|
|
6
|
+
*/
|
|
7
|
+
export declare class LocalDatabaseReadScope {
|
|
8
|
+
private readonly pendingReads;
|
|
9
|
+
private closedError;
|
|
10
|
+
/** Whether this scope has stopped accepting reads. */
|
|
11
|
+
get isClosed(): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Starts a read while this scope is open and returns the caller's wait for its result.
|
|
14
|
+
*
|
|
15
|
+
* Closing the scope ends this wait without cancelling underlying database I/O.
|
|
16
|
+
*/
|
|
17
|
+
run<T>(operation: () => Promise<T>): Promise<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Stops new reads and rejects pending caller waits.
|
|
20
|
+
*
|
|
21
|
+
* Database I/O already started by those reads continues in the background.
|
|
22
|
+
*/
|
|
23
|
+
close(): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
class LocalDatabaseReadScope {
|
|
2
|
+
pendingReads = /* @__PURE__ */ new Set();
|
|
3
|
+
closedError;
|
|
4
|
+
/** Whether this scope has stopped accepting reads. */
|
|
5
|
+
get isClosed() {
|
|
6
|
+
return this.closedError !== void 0;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Starts a read while this scope is open and returns the caller's wait for its result.
|
|
10
|
+
*
|
|
11
|
+
* Closing the scope ends this wait without cancelling underlying database I/O.
|
|
12
|
+
*/
|
|
13
|
+
run(operation) {
|
|
14
|
+
const closedError = this.closedError;
|
|
15
|
+
if (closedError) {
|
|
16
|
+
return Promise.reject(closedError);
|
|
17
|
+
}
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const rejectPendingRead = (error) => {
|
|
20
|
+
reject(error);
|
|
21
|
+
};
|
|
22
|
+
this.pendingReads.add(rejectPendingRead);
|
|
23
|
+
let operationPromise;
|
|
24
|
+
try {
|
|
25
|
+
operationPromise = operation();
|
|
26
|
+
} catch (error) {
|
|
27
|
+
this.pendingReads.delete(rejectPendingRead);
|
|
28
|
+
reject(error);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
void Promise.resolve(operationPromise).then(
|
|
32
|
+
(value) => {
|
|
33
|
+
this.pendingReads.delete(rejectPendingRead);
|
|
34
|
+
resolve(value);
|
|
35
|
+
},
|
|
36
|
+
(error) => {
|
|
37
|
+
this.pendingReads.delete(rejectPendingRead);
|
|
38
|
+
reject(error);
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Stops new reads and rejects pending caller waits.
|
|
45
|
+
*
|
|
46
|
+
* Database I/O already started by those reads continues in the background.
|
|
47
|
+
*/
|
|
48
|
+
close() {
|
|
49
|
+
if (this.closedError) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const error = new Error("Local database reads have stopped.");
|
|
53
|
+
this.closedError = error;
|
|
54
|
+
for (const rejectPendingRead of this.pendingReads) {
|
|
55
|
+
rejectPendingRead(error);
|
|
56
|
+
}
|
|
57
|
+
this.pendingReads.clear();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
LocalDatabaseReadScope
|
|
62
|
+
};
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
IDPrefixes
|
|
10
10
|
} from "../common/types.js";
|
|
11
11
|
import { isEncryptedChunkEntry, isSyncInfoEntry, isObfuscatedEntry } from "../common/utils.js";
|
|
12
|
+
import { UnsupportedEncryptedDocumentFormatError } from "../replication/UnsupportedEncryptedDocumentFormatError.js";
|
|
12
13
|
import {
|
|
13
14
|
UnencryptedContentRejectedError
|
|
14
15
|
} from "../replication/UnencryptedContentRejectedError.js";
|
|
@@ -101,7 +102,7 @@ function assertUnencryptedContentPolicy(doc, algorithm, acceptUnencryptedContent
|
|
|
101
102
|
for (const contentKind of contentKinds) {
|
|
102
103
|
if (contentKind !== "eden" && record.e_ === true) {
|
|
103
104
|
if (forceV1DecryptsMarkedData || dataIsHandledByExistingDecryptor) continue;
|
|
104
|
-
throw new
|
|
105
|
+
throw new UnsupportedEncryptedDocumentFormatError();
|
|
105
106
|
}
|
|
106
107
|
if (contentKind === "chunk" && algorithm === E2EEAlgorithms.ForceV1 && !record.e_ && isEncryptedChunkEntry(doc) && hasForceV1CiphertextHeader) {
|
|
107
108
|
continue;
|
|
@@ -2,6 +2,7 @@ import { type EntryDoc } from "../common/types.js";
|
|
|
2
2
|
type PathIdentitySettings = {
|
|
3
3
|
pathNormalizationVersion?: number;
|
|
4
4
|
};
|
|
5
|
+
type DatabaseRead = <T>(operation: () => Promise<T>) => Promise<T>;
|
|
5
6
|
export type PathIdentityInspection = {
|
|
6
7
|
readonly status: "empty";
|
|
7
8
|
} | {
|
|
@@ -11,6 +12,9 @@ export type PathIdentityInspection = {
|
|
|
11
12
|
/** Read the existing declaration without changing a remote or declaring a new identity. */
|
|
12
13
|
export declare function inspectDatabasePathIdentity(database: PouchDB.Database<EntryDoc>): Promise<PathIdentityInspection>;
|
|
13
14
|
export declare function assertMatchingPathIdentityFeature(document: unknown, settings: PathIdentitySettings): void;
|
|
14
|
-
/**
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Refuse an implicit migration; a fresh database may declare its identity before its first Metadata write.
|
|
17
|
+
* The optional read observer owns IO failures, separately from compatibility rejection and declaration writes.
|
|
18
|
+
*/
|
|
19
|
+
export declare function assertDatabasePathIdentity(database: PouchDB.Database<EntryDoc>, settings: PathIdentitySettings, declare?: boolean, read?: DatabaseRead): Promise<void>;
|
|
16
20
|
export {};
|
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
} from "./remoteFeatureCompatibility.js";
|
|
9
9
|
import { isErrorOfMissingDoc } from "./utils_couchdb.js";
|
|
10
10
|
import { bumpRemoteVersion } from "./negotiation.js";
|
|
11
|
+
function readDatabase(operation) {
|
|
12
|
+
return operation();
|
|
13
|
+
}
|
|
11
14
|
async function inspectDatabasePathIdentity(database) {
|
|
12
15
|
let document;
|
|
13
16
|
try {
|
|
@@ -33,14 +36,16 @@ function assertMatchingPathIdentityFeature(document, settings) {
|
|
|
33
36
|
);
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
|
-
async function hasMetadata(database) {
|
|
39
|
+
async function hasMetadata(database, read) {
|
|
37
40
|
let startkey;
|
|
38
41
|
for (; ; ) {
|
|
39
|
-
const result = await
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const result = await read(
|
|
43
|
+
() => database.allDocs({
|
|
44
|
+
include_docs: true,
|
|
45
|
+
limit: 100,
|
|
46
|
+
...startkey ? { startkey, skip: 1 } : {}
|
|
47
|
+
})
|
|
48
|
+
);
|
|
44
49
|
for (const row of result.rows) {
|
|
45
50
|
if (row.doc?.type === "plain" || row.doc?.type === "newnote") return true;
|
|
46
51
|
}
|
|
@@ -48,10 +53,10 @@ async function hasMetadata(database) {
|
|
|
48
53
|
startkey = result.rows[result.rows.length - 1].id;
|
|
49
54
|
}
|
|
50
55
|
}
|
|
51
|
-
async function assertDatabasePathIdentity(database, settings, declare = false) {
|
|
56
|
+
async function assertDatabasePathIdentity(database, settings, declare = false, read = readDatabase) {
|
|
52
57
|
let document;
|
|
53
58
|
try {
|
|
54
|
-
document = await database.get(VERSIONING_DOCID);
|
|
59
|
+
document = await read(() => database.get(VERSIONING_DOCID));
|
|
55
60
|
} catch (error) {
|
|
56
61
|
if (!isErrorOfMissingDoc(error)) throw error;
|
|
57
62
|
}
|
|
@@ -62,9 +67,9 @@ async function assertDatabasePathIdentity(database, settings, declare = false) {
|
|
|
62
67
|
return;
|
|
63
68
|
}
|
|
64
69
|
if (declared) return;
|
|
65
|
-
if (await hasMetadata(database)) {
|
|
70
|
+
if (await hasMetadata(database, read)) {
|
|
66
71
|
try {
|
|
67
|
-
document = await database.get(VERSIONING_DOCID);
|
|
72
|
+
document = await read(() => database.get(VERSIONING_DOCID));
|
|
68
73
|
} catch (error) {
|
|
69
74
|
if (!isErrorOfMissingDoc(error)) throw error;
|
|
70
75
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Raised when the reader has no decoding route for a document marked as encrypted. */
|
|
2
|
+
export declare class UnsupportedEncryptedDocumentFormatError extends Error {
|
|
3
|
+
readonly name = "UnsupportedEncryptedDocumentFormatError";
|
|
4
|
+
constructor(options?: {
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
});
|
|
7
|
+
/**
|
|
8
|
+
* Finds the rejection in a cause chain. PouchDB copies the error's own
|
|
9
|
+
* properties into a different error type. Recover the class from its name
|
|
10
|
+
* and keep the reported exception as the cause for diagnostics.
|
|
11
|
+
*/
|
|
12
|
+
static findIn(error: unknown): UnsupportedEncryptedDocumentFormatError | undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const UNSUPPORTED_FORMAT_ERROR_NAME = "UnsupportedEncryptedDocumentFormatError";
|
|
2
|
+
class UnsupportedEncryptedDocumentFormatError extends Error {
|
|
3
|
+
name = UNSUPPORTED_FORMAT_ERROR_NAME;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super("Encrypted file content uses a representation which this E2EE reader cannot decrypt.", options);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Finds the rejection in a cause chain. PouchDB copies the error's own
|
|
9
|
+
* properties into a different error type. Recover the class from its name
|
|
10
|
+
* and keep the reported exception as the cause for diagnostics.
|
|
11
|
+
*/
|
|
12
|
+
static findIn(error) {
|
|
13
|
+
const seen = /* @__PURE__ */ new Set();
|
|
14
|
+
let current = error;
|
|
15
|
+
while (typeof current === "object" && current !== null && !seen.has(current)) {
|
|
16
|
+
if (current instanceof UnsupportedEncryptedDocumentFormatError) return current;
|
|
17
|
+
if (current instanceof Error && current.name === UNSUPPORTED_FORMAT_ERROR_NAME) {
|
|
18
|
+
return new UnsupportedEncryptedDocumentFormatError({ cause: current });
|
|
19
|
+
}
|
|
20
|
+
seen.add(current);
|
|
21
|
+
current = "cause" in current ? current.cause : void 0;
|
|
22
|
+
}
|
|
23
|
+
return void 0;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
UnsupportedEncryptedDocumentFormatError
|
|
28
|
+
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { LiveSyncLocalDB } from "../../pouchdb/LiveSyncLocalDB.js";
|
|
2
|
+
import { UnsupportedEncryptedDocumentFormatError } from "../UnsupportedEncryptedDocumentFormatError.js";
|
|
1
3
|
import type { ReplicationStatics } from "../../common/models/shared.definition.js";
|
|
2
4
|
import { type EntryDoc, type RemoteDBSettings, type EntryLeaf, type SyncParameters, type DatabaseEntry, type NodeData, type RemotePreferredTweakResult } from "../../common/types.js";
|
|
3
5
|
import type { InitialFetchFastForwardResource } from "../RemoteResource.js";
|
|
@@ -8,6 +10,7 @@ import { type CentralCompatibilityDecisionRecorder } from "../CentralCompatibili
|
|
|
8
10
|
import { type ReplicationOutcome } from "../ReplicatorProvider.js";
|
|
9
11
|
import { type ForgetJoinedDeviceResult, type JoinedDevicesSnapshot } from "../joinedDevices.js";
|
|
10
12
|
export type { OwnedCouchDBConnection, RemoteConnectionOpenOptions } from "../../services/base/RemoteConnection.js";
|
|
13
|
+
type SyncProcessingResult = "DONE" | "NEED_RETRY" | "NEED_RESURRECT" | "FAILED" | "CANCELLED" | UnsupportedEncryptedDocumentFormatError;
|
|
11
14
|
/**
|
|
12
15
|
* Compatibility constructor environment for the CouchDB Replicator facade.
|
|
13
16
|
*
|
|
@@ -53,6 +56,7 @@ export declare class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicato
|
|
|
53
56
|
private finishReplicationProgress;
|
|
54
57
|
isMobile(): boolean;
|
|
55
58
|
constructor(env: LiveSyncCouchDBReplicatorEnv);
|
|
59
|
+
private log;
|
|
56
60
|
/** Own the checkpoint handoff for one fresh, suspended Fast Fetch only. */
|
|
57
61
|
createInitialFetchFastForwardResource(setting: RemoteDBSettings): InitialFetchFastForwardResource;
|
|
58
62
|
getInitialSyncParameters(setting: RemoteDBSettings): Promise<SyncParameters>;
|
|
@@ -81,9 +85,13 @@ export declare class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicato
|
|
|
81
85
|
replicationChangeDetected(e: PouchDB.Replication.SyncResult<EntryDoc>, showResult: boolean, docSentOnStart: number, docArrivedOnStart: number): Promise<void>;
|
|
82
86
|
replicationCompleted(showResult: boolean): void;
|
|
83
87
|
replicationDenied(e: unknown): void;
|
|
84
|
-
|
|
88
|
+
/** Ask the current owner to inspect ambiguous managed-operation failures without joining its drain. */
|
|
89
|
+
private requestCurrentDatabaseInspection;
|
|
90
|
+
/** Report only the captured local read; recovery must not join the operation it drains. */
|
|
91
|
+
private readLocalDatabase;
|
|
92
|
+
replicationErrored(e: unknown, database?: LiveSyncLocalDB | null): void;
|
|
85
93
|
replicationPaused(): void;
|
|
86
|
-
processSync(syncHandler: PouchDB.Replication.Sync<EntryDoc> | PouchDB.Replication.Replication<EntryDoc>, showResult: boolean, docSentOnStart: number, docArrivedOnStart: number, syncMode: "sync" | "pullOnly" | "pushOnly", retrying: boolean, reportCancelledAsDone?: boolean): Promise<
|
|
94
|
+
processSync(syncHandler: PouchDB.Replication.Sync<EntryDoc> | PouchDB.Replication.Replication<EntryDoc>, showResult: boolean, docSentOnStart: number, docArrivedOnStart: number, syncMode: "sync" | "pullOnly" | "pushOnly", retrying: boolean, reportCancelledAsDone?: boolean): Promise<SyncProcessingResult>;
|
|
87
95
|
getEmptyMaxEntry(remoteID: number): {
|
|
88
96
|
_id: string;
|
|
89
97
|
maxSeq: number | string;
|
|
@@ -111,6 +119,7 @@ export declare class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicato
|
|
|
111
119
|
private runOneShotReplication;
|
|
112
120
|
private checkOneShotReplicationConnectivity;
|
|
113
121
|
private closeRemoteConnection;
|
|
122
|
+
private notifyFiniteReplicationCompleted;
|
|
114
123
|
private withRemoteConnection;
|
|
115
124
|
replicateAllToServer(setting: RemoteDBSettings, showingNotice?: boolean): Promise<boolean>;
|
|
116
125
|
replicateAllToServerWithOutcome(setting: RemoteDBSettings, showingNotice?: boolean): Promise<ReplicationOutcome>;
|