@vrtmrz/livesync-commonlib 0.4.3 → 0.6.0
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/README.md +2 -2
- 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/StorageAccess.d.ts +8 -2
- 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/platform/FilePathIndex.d.ts +110 -0
- package/dist/platform/FilePathIndex.js +146 -0
- 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/pathIdentityCompatibility.d.ts +6 -2
- package/dist/pouchdb/pathIdentityCompatibility.js +15 -10
- package/dist/replication/contentRead.d.ts +3 -0
- package/dist/replication/contentRead.js +4 -0
- package/dist/replication/couchdb/LiveSyncReplicator.d.ts +8 -1
- package/dist/replication/couchdb/LiveSyncReplicator.js +187 -119
- package/dist/replication/index.d.ts +5 -1
- package/dist/replication/index.js +18 -1
- package/dist/replication/trystero/LiveSyncTrysteroReplicator.d.ts +3 -3
- package/dist/replication/trystero/P2PAutomationCoordinator.d.ts +1 -1
- package/dist/replication/trystero/P2PReplicationWire.d.ts +1 -1
- package/dist/replication/trystero/TrysteroReplicator/changeRelay.d.ts +133 -0
- package/dist/replication/trystero/TrysteroReplicator/changeRelay.js +176 -0
- package/dist/replication/trystero/TrysteroReplicator/configurationExchange.d.ts +55 -0
- package/dist/replication/trystero/TrysteroReplicator/configurationExchange.js +99 -0
- package/dist/replication/trystero/TrysteroReplicator/configuredTargets.d.ts +83 -0
- package/dist/replication/trystero/TrysteroReplicator/configuredTargets.js +121 -0
- package/dist/replication/trystero/TrysteroReplicator/peerAgreement.d.ts +75 -0
- package/dist/replication/trystero/TrysteroReplicator/peerAgreement.js +131 -0
- package/dist/replication/trystero/TrysteroReplicator/progress.d.ts +57 -0
- package/dist/replication/trystero/TrysteroReplicator/progress.js +84 -0
- package/dist/replication/trystero/TrysteroReplicator/relayControl.d.ts +20 -0
- package/dist/replication/trystero/TrysteroReplicator/relayControl.js +25 -0
- package/dist/replication/trystero/TrysteroReplicator/transfers.d.ts +125 -0
- package/dist/replication/trystero/TrysteroReplicator/transfers.js +278 -0
- package/dist/replication/trystero/TrysteroReplicator.d.ts +65 -483
- package/dist/replication/trystero/TrysteroReplicator.js +240 -673
- package/dist/replication/trystero/TrysteroReplicatorP2PClient.d.ts +19 -6
- package/dist/replication/trystero/TrysteroReplicatorP2PClient.js +29 -25
- package/dist/replication/trystero/TrysteroReplicatorP2PServer/peerAdmission.d.ts +66 -0
- package/dist/replication/trystero/TrysteroReplicatorP2PServer/peerAdmission.js +142 -0
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +19 -16
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +33 -93
- package/dist/replication/trystero/p2pReplicationDiagnostics.d.ts +1 -1
- package/dist/replication/trystero/p2pReplicationProgress.d.ts +2 -11
- package/dist/replication/trystero/replicationTypes.d.ts +88 -0
- package/dist/replication/trystero/replicationTypes.js +0 -0
- package/dist/replication/trystero/rpcSessionProvider.d.ts +13 -0
- package/dist/replication/trystero/rpcSessionProvider.js +0 -0
- package/dist/rpc/RpcRoom/pendingInvocations.d.ts +141 -0
- package/dist/rpc/RpcRoom/pendingInvocations.js +225 -0
- package/dist/rpc/RpcRoom.d.ts +9 -4
- package/dist/rpc/RpcRoom.js +73 -118
- package/dist/rpc/RpcSession.d.ts +6 -0
- package/dist/rpc/RpcSession.js +11 -0
- package/dist/rpc/types.d.ts +2 -0
- 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/serviceFeatures/targetFilter.d.ts +1 -1
- package/dist/serviceFeatures/targetFilter.js +8 -38
- package/dist/serviceModules/FileAccessBase.d.ts +4 -0
- package/dist/serviceModules/FileAccessBase.js +10 -0
- package/dist/serviceModules/Rebuilder.js +1 -1
- package/dist/serviceModules/ServiceDatabaseFileAccessBase.d.ts +13 -0
- package/dist/serviceModules/ServiceDatabaseFileAccessBase.js +190 -123
- package/dist/serviceModules/ServiceFileAccessBase/fileNames.d.ts +54 -0
- package/dist/serviceModules/ServiceFileAccessBase/fileNames.js +57 -0
- package/dist/serviceModules/ServiceFileAccessBase.d.ts +5 -1
- package/dist/serviceModules/ServiceFileAccessBase.js +18 -3
- 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/serviceModules/adapters/IFileSystemAdapter.d.ts +3 -0
- 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 +3 -1
- package/dist/services/base/ReplicationService.js +3 -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/dist/storage.d.ts +2 -0
- package/dist/storage.js +2 -0
- package/docs/development.md +111 -0
- package/docs/offline-scanner.md +46 -0
- package/docs/p2p-transport-lifecycle.md +24 -0
- package/docs/platform-storage.md +22 -0
- package/docs/releasing.md +48 -0
- package/docs/settings-lifecycle.md +4 -2
- package/package.json +1 -1
|
@@ -36,6 +36,8 @@ export type GeneratedChunk = {
|
|
|
36
36
|
};
|
|
37
37
|
/** Optional host IO bound afresh to each prepared database instance. */
|
|
38
38
|
export interface LiveSyncLocalDBRuntimeOptions {
|
|
39
|
+
/** Physical database selection captured by the owning service before asynchronous retirement. */
|
|
40
|
+
createPouchDBInstance?: LiveSyncLocalDBEnv["services"]["database"]["createPouchDBInstance"];
|
|
39
41
|
createChunkFetchSource?: (database: PouchDB.Database<EntryDoc>) => ChunkFetchSource;
|
|
40
42
|
}
|
|
41
43
|
export declare class LiveSyncLocalDB {
|
|
@@ -43,8 +45,22 @@ export declare class LiveSyncLocalDB {
|
|
|
43
45
|
auth: Credential;
|
|
44
46
|
dbname: string;
|
|
45
47
|
settings: RemoteDBSettings;
|
|
48
|
+
/**
|
|
49
|
+
* The raw PouchDB handle for replication, changes feeds, and maintenance.
|
|
50
|
+
*
|
|
51
|
+
* Direct reads bypass this owner's caller-wait retirement. Use
|
|
52
|
+
* {@link readDBEntry} for file contents, {@link getRaw} or {@link allDocsRaw}
|
|
53
|
+
* for raw documents, and {@link readFromDatabase} for other read-only Pouch
|
|
54
|
+
* operations. These entry points end caller waits when the owner retires;
|
|
55
|
+
* they do not cancel native I/O or automatically retry a failed operation.
|
|
56
|
+
*
|
|
57
|
+
* Integrations which require this handle must own their operation's
|
|
58
|
+
* retirement and completion. Keep writes and mixed read/write workflows
|
|
59
|
+
* outside {@link readFromDatabase}; started writes must actually finish.
|
|
60
|
+
*/
|
|
46
61
|
localDatabase: PouchDB.Database<EntryDoc>;
|
|
47
62
|
_log: LogFunction;
|
|
63
|
+
private readScope;
|
|
48
64
|
private _managers?;
|
|
49
65
|
get managers(): LiveSyncManagers;
|
|
50
66
|
isReady: boolean;
|
|
@@ -62,6 +78,18 @@ export declare class LiveSyncLocalDB {
|
|
|
62
78
|
/** Notify dependent services at most once for each physical database lifecycle. */
|
|
63
79
|
private notifyDatabaseUnload;
|
|
64
80
|
onunload(): void;
|
|
81
|
+
/** End caller read waits before lifecycle hooks drain their owners; native IO is not cancelled. */
|
|
82
|
+
stopReads(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Run a read on the captured Pouch and end its caller wait when this owner retires.
|
|
85
|
+
*
|
|
86
|
+
* Use {@link getRaw} or {@link allDocsRaw} for document reads with known IDs.
|
|
87
|
+
* This callback also supports other read-only operations, including inspection
|
|
88
|
+
* of unvalidated stored IDs. It can continue after the caller wait rejects,
|
|
89
|
+
* so it must not perform writes or change application state after a read.
|
|
90
|
+
* No connection-health conclusion, recovery request, or retry is implied.
|
|
91
|
+
*/
|
|
92
|
+
readFromDatabase<T>(operation: (database: PouchDB.Database<EntryDoc>) => Promise<T>): Promise<T>;
|
|
65
93
|
close(): Promise<void>;
|
|
66
94
|
private closeActiveReplication;
|
|
67
95
|
private teardownDatabaseDependencies;
|
|
@@ -195,15 +223,19 @@ export declare class LiveSyncLocalDB {
|
|
|
195
223
|
datatype: import("../common/types.js").EntryTypeNotes;
|
|
196
224
|
} & PouchDB.Core.AllDocsMeta & PouchDB.Core.IdMeta & PouchDB.Core.RevisionIdMeta), void, unknown>;
|
|
197
225
|
removeRevision(docId: DocumentID, revision: string): Promise<boolean>;
|
|
226
|
+
/** Read a raw document by its known ID, with caller-wait retirement owned by this database. */
|
|
198
227
|
getRaw<T extends EntryDoc>(docId: DocumentID, options?: PouchDB.Core.GetOptions): Promise<T & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta>;
|
|
199
228
|
removeRaw(docId: DocumentID, revision: string, options?: PouchDB.Core.Options): Promise<PouchDB.Core.Response>;
|
|
200
229
|
putRaw<T extends EntryDoc>(doc: T, options?: PouchDB.Core.PutOptions): Promise<PouchDB.Core.Response>;
|
|
230
|
+
/** Read a raw document listing with caller-wait retirement owned by this database. */
|
|
201
231
|
allDocsRaw<T extends EntryDoc | DatabaseEntry>(options?: PouchDB.Core.AllDocsWithKeyOptions | PouchDB.Core.AllDocsWithKeysOptions | PouchDB.Core.AllDocsWithinRangeOptions | PouchDB.Core.AllDocsOptions): Promise<PouchDB.Core.AllDocsResponse<T>>;
|
|
202
232
|
bulkDocsRaw<T extends EntryDoc>(docs: Array<PouchDB.Core.PutDocument<T>>, options?: PouchDB.Core.BulkDocsOptions): Promise<Array<PouchDB.Core.Response | PouchDB.Core.Error>>;
|
|
203
233
|
isTargetFile(filenameSrc: string): boolean;
|
|
204
234
|
getDBEntryMeta(path: FilePathWithPrefix | FilePath, opt?: PouchDB.Core.GetOptions, includeDeleted?: boolean): Promise<false | LoadedEntry>;
|
|
205
235
|
getDBEntry(path: FilePathWithPrefix | FilePath, opt?: PouchDB.Core.GetOptions, dump?: boolean, waitForReady?: boolean, includeDeleted?: boolean, localOnly?: boolean): Promise<false | LoadedEntry>;
|
|
236
|
+
readDBEntry(path: FilePathWithPrefix | FilePath, opt?: PouchDB.Core.GetOptions, dump?: boolean, waitForReady?: boolean, includeDeleted?: boolean, localOnly?: boolean): Promise<false | LoadedEntry>;
|
|
206
237
|
getDBEntryFromMeta(meta: LoadedEntry | MetaEntry, dump?: boolean, waitForReady?: boolean, localOnly?: boolean): Promise<false | LoadedEntry>;
|
|
238
|
+
readDBEntryFromMeta(meta: LoadedEntry | MetaEntry, dump?: boolean, waitForReady?: boolean, localOnly?: boolean): Promise<false | LoadedEntry>;
|
|
207
239
|
deleteDBEntry(path: FilePathWithPrefix | FilePath, opt?: PouchDB.Core.GetOptions): Promise<boolean>;
|
|
208
240
|
storeDeletionAtRevision(path: FilePathWithPrefix | FilePath, baseRevision: string): Promise<false | PouchDB.Core.Response>;
|
|
209
241
|
putDBEntry(note: SavingEntry, onlyChunks?: boolean, conflictBaseRev?: string): Promise<false | PouchDB.Core.Response>;
|
|
@@ -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
|
+
};
|
|
@@ -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
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { LiveSyncLocalDB } from "../../pouchdb/LiveSyncLocalDB.js";
|
|
1
2
|
import type { ReplicationStatics } from "../../common/models/shared.definition.js";
|
|
2
3
|
import { type EntryDoc, type RemoteDBSettings, type EntryLeaf, type SyncParameters, type DatabaseEntry, type NodeData, type RemotePreferredTweakResult } from "../../common/types.js";
|
|
3
4
|
import type { InitialFetchFastForwardResource } from "../RemoteResource.js";
|
|
@@ -53,6 +54,7 @@ export declare class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicato
|
|
|
53
54
|
private finishReplicationProgress;
|
|
54
55
|
isMobile(): boolean;
|
|
55
56
|
constructor(env: LiveSyncCouchDBReplicatorEnv);
|
|
57
|
+
private log;
|
|
56
58
|
/** Own the checkpoint handoff for one fresh, suspended Fast Fetch only. */
|
|
57
59
|
createInitialFetchFastForwardResource(setting: RemoteDBSettings): InitialFetchFastForwardResource;
|
|
58
60
|
getInitialSyncParameters(setting: RemoteDBSettings): Promise<SyncParameters>;
|
|
@@ -81,7 +83,11 @@ export declare class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicato
|
|
|
81
83
|
replicationChangeDetected(e: PouchDB.Replication.SyncResult<EntryDoc>, showResult: boolean, docSentOnStart: number, docArrivedOnStart: number): Promise<void>;
|
|
82
84
|
replicationCompleted(showResult: boolean): void;
|
|
83
85
|
replicationDenied(e: unknown): void;
|
|
84
|
-
|
|
86
|
+
/** Ask the current owner to inspect ambiguous managed-operation failures without joining its drain. */
|
|
87
|
+
private requestCurrentDatabaseInspection;
|
|
88
|
+
/** Report only the captured local read; recovery must not join the operation it drains. */
|
|
89
|
+
private readLocalDatabase;
|
|
90
|
+
replicationErrored(e: unknown, database?: LiveSyncLocalDB | null): void;
|
|
85
91
|
replicationPaused(): void;
|
|
86
92
|
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<"DONE" | "NEED_RETRY" | "NEED_RESURRECT" | "FAILED" | "CANCELLED">;
|
|
87
93
|
getEmptyMaxEntry(remoteID: number): {
|
|
@@ -111,6 +117,7 @@ export declare class LiveSyncCouchDBReplicator extends LiveSyncAbstractReplicato
|
|
|
111
117
|
private runOneShotReplication;
|
|
112
118
|
private checkOneShotReplicationConnectivity;
|
|
113
119
|
private closeRemoteConnection;
|
|
120
|
+
private notifyFiniteReplicationCompleted;
|
|
114
121
|
private withRemoteConnection;
|
|
115
122
|
replicateAllToServer(setting: RemoteDBSettings, showingNotice?: boolean): Promise<boolean>;
|
|
116
123
|
replicateAllToServerWithOutcome(setting: RemoteDBSettings, showingNotice?: boolean): Promise<ReplicationOutcome>;
|