@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.
Files changed (130) hide show
  1. package/README.md +2 -2
  2. package/dist/common/LSError.d.ts +13 -0
  3. package/dist/common/LSError.js +19 -1
  4. package/dist/diagnostics/local.d.ts +7 -2
  5. package/dist/interfaces/DatabaseFileAccess.d.ts +20 -4
  6. package/dist/interfaces/FileHandler.d.ts +67 -1
  7. package/dist/interfaces/FileHandler.js +16 -0
  8. package/dist/interfaces/StorageAccess.d.ts +8 -2
  9. package/dist/interfaces/StorageEventManager.d.ts +7 -0
  10. package/dist/managers/ChunkFetcher.d.ts +16 -0
  11. package/dist/managers/ChunkFetcher.js +190 -112
  12. package/dist/managers/EntryManager/EntryManager.d.ts +7 -0
  13. package/dist/managers/EntryManager/EntryManager.js +6 -0
  14. package/dist/managers/EntryManager/EntryManagerImpls.d.ts +9 -5
  15. package/dist/managers/EntryManager/EntryManagerImpls.js +103 -76
  16. package/dist/managers/LayeredChunkManager/ArrivalWaitLayer.d.ts +7 -3
  17. package/dist/managers/LayeredChunkManager/ArrivalWaitLayer.js +83 -16
  18. package/dist/managers/LayeredChunkManager/DatabaseReadLayer.d.ts +3 -2
  19. package/dist/managers/LayeredChunkManager/DatabaseReadLayer.js +38 -36
  20. package/dist/managers/LayeredChunkManager/types.d.ts +15 -1
  21. package/dist/managers/LayeredChunkManager.d.ts +4 -0
  22. package/dist/managers/LayeredChunkManager.js +19 -6
  23. package/dist/managers/LiveSyncManagers.d.ts +2 -0
  24. package/dist/managers/LiveSyncManagers.js +6 -0
  25. package/dist/managers/StorageEventManager.d.ts +22 -0
  26. package/dist/managers/StorageEventManager.js +48 -1
  27. package/dist/platform/FilePathIndex.d.ts +110 -0
  28. package/dist/platform/FilePathIndex.js +146 -0
  29. package/dist/pouchdb/LiveSyncLocalDB.d.ts +32 -0
  30. package/dist/pouchdb/LiveSyncLocalDB.js +72 -18
  31. package/dist/pouchdb/LocalDatabaseReadScope.d.ts +24 -0
  32. package/dist/pouchdb/LocalDatabaseReadScope.js +62 -0
  33. package/dist/pouchdb/pathIdentityCompatibility.d.ts +6 -2
  34. package/dist/pouchdb/pathIdentityCompatibility.js +15 -10
  35. package/dist/replication/contentRead.d.ts +3 -0
  36. package/dist/replication/contentRead.js +4 -0
  37. package/dist/replication/couchdb/LiveSyncReplicator.d.ts +8 -1
  38. package/dist/replication/couchdb/LiveSyncReplicator.js +187 -119
  39. package/dist/replication/index.d.ts +5 -1
  40. package/dist/replication/index.js +18 -1
  41. package/dist/replication/trystero/LiveSyncTrysteroReplicator.d.ts +3 -3
  42. package/dist/replication/trystero/P2PAutomationCoordinator.d.ts +1 -1
  43. package/dist/replication/trystero/P2PReplicationWire.d.ts +1 -1
  44. package/dist/replication/trystero/TrysteroReplicator/changeRelay.d.ts +133 -0
  45. package/dist/replication/trystero/TrysteroReplicator/changeRelay.js +176 -0
  46. package/dist/replication/trystero/TrysteroReplicator/configurationExchange.d.ts +55 -0
  47. package/dist/replication/trystero/TrysteroReplicator/configurationExchange.js +99 -0
  48. package/dist/replication/trystero/TrysteroReplicator/configuredTargets.d.ts +83 -0
  49. package/dist/replication/trystero/TrysteroReplicator/configuredTargets.js +121 -0
  50. package/dist/replication/trystero/TrysteroReplicator/peerAgreement.d.ts +75 -0
  51. package/dist/replication/trystero/TrysteroReplicator/peerAgreement.js +131 -0
  52. package/dist/replication/trystero/TrysteroReplicator/progress.d.ts +57 -0
  53. package/dist/replication/trystero/TrysteroReplicator/progress.js +84 -0
  54. package/dist/replication/trystero/TrysteroReplicator/relayControl.d.ts +20 -0
  55. package/dist/replication/trystero/TrysteroReplicator/relayControl.js +25 -0
  56. package/dist/replication/trystero/TrysteroReplicator/transfers.d.ts +125 -0
  57. package/dist/replication/trystero/TrysteroReplicator/transfers.js +278 -0
  58. package/dist/replication/trystero/TrysteroReplicator.d.ts +65 -483
  59. package/dist/replication/trystero/TrysteroReplicator.js +240 -673
  60. package/dist/replication/trystero/TrysteroReplicatorP2PClient.d.ts +19 -6
  61. package/dist/replication/trystero/TrysteroReplicatorP2PClient.js +29 -25
  62. package/dist/replication/trystero/TrysteroReplicatorP2PServer/peerAdmission.d.ts +66 -0
  63. package/dist/replication/trystero/TrysteroReplicatorP2PServer/peerAdmission.js +142 -0
  64. package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +19 -16
  65. package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +33 -93
  66. package/dist/replication/trystero/p2pReplicationDiagnostics.d.ts +1 -1
  67. package/dist/replication/trystero/p2pReplicationProgress.d.ts +2 -11
  68. package/dist/replication/trystero/replicationTypes.d.ts +88 -0
  69. package/dist/replication/trystero/replicationTypes.js +0 -0
  70. package/dist/replication/trystero/rpcSessionProvider.d.ts +13 -0
  71. package/dist/replication/trystero/rpcSessionProvider.js +0 -0
  72. package/dist/rpc/RpcRoom/pendingInvocations.d.ts +141 -0
  73. package/dist/rpc/RpcRoom/pendingInvocations.js +225 -0
  74. package/dist/rpc/RpcRoom.d.ts +9 -4
  75. package/dist/rpc/RpcRoom.js +73 -118
  76. package/dist/rpc/RpcSession.d.ts +6 -0
  77. package/dist/rpc/RpcSession.js +11 -0
  78. package/dist/rpc/types.d.ts +2 -0
  79. package/dist/serviceFeatures/offlineScanner/files.d.ts +1 -1
  80. package/dist/serviceFeatures/offlineScanner/files.js +27 -7
  81. package/dist/serviceFeatures/offlineScanner/scan.d.ts +5 -1
  82. package/dist/serviceFeatures/offlineScanner/scan.js +2 -1
  83. package/dist/serviceFeatures/offlineScanner.d.ts +1 -1
  84. package/dist/serviceFeatures/offlineScanner.js +10 -2
  85. package/dist/serviceFeatures/targetFilter.d.ts +1 -1
  86. package/dist/serviceFeatures/targetFilter.js +8 -38
  87. package/dist/serviceModules/FileAccessBase.d.ts +4 -0
  88. package/dist/serviceModules/FileAccessBase.js +10 -0
  89. package/dist/serviceModules/Rebuilder.js +1 -1
  90. package/dist/serviceModules/ServiceDatabaseFileAccessBase.d.ts +13 -0
  91. package/dist/serviceModules/ServiceDatabaseFileAccessBase.js +190 -123
  92. package/dist/serviceModules/ServiceFileAccessBase/fileNames.d.ts +54 -0
  93. package/dist/serviceModules/ServiceFileAccessBase/fileNames.js +57 -0
  94. package/dist/serviceModules/ServiceFileAccessBase.d.ts +5 -1
  95. package/dist/serviceModules/ServiceFileAccessBase.js +18 -3
  96. package/dist/serviceModules/ServiceFileHandlerBase/deletion.js +17 -7
  97. package/dist/serviceModules/ServiceFileHandlerBase/events.d.ts +3 -2
  98. package/dist/serviceModules/ServiceFileHandlerBase/events.js +9 -9
  99. package/dist/serviceModules/ServiceFileHandlerBase/protection.js +24 -9
  100. package/dist/serviceModules/ServiceFileHandlerBase/provenance.d.ts +10 -0
  101. package/dist/serviceModules/ServiceFileHandlerBase/provenance.js +28 -4
  102. package/dist/serviceModules/ServiceFileHandlerBase/reflection.d.ts +11 -5
  103. package/dist/serviceModules/ServiceFileHandlerBase/reflection.js +144 -58
  104. package/dist/serviceModules/ServiceFileHandlerBase/saving.js +16 -2
  105. package/dist/serviceModules/ServiceFileHandlerBase/scanning.js +1 -0
  106. package/dist/serviceModules/ServiceFileHandlerBase.d.ts +7 -2
  107. package/dist/serviceModules/ServiceFileHandlerBase.js +13 -4
  108. package/dist/serviceModules/adapters/IFileSystemAdapter.d.ts +3 -0
  109. package/dist/services/base/ControlService.js +1 -5
  110. package/dist/services/base/DatabaseEventService.d.ts +3 -0
  111. package/dist/services/base/DatabaseEventService.js +3 -0
  112. package/dist/services/base/DatabaseService.d.ts +15 -0
  113. package/dist/services/base/DatabaseService.js +85 -4
  114. package/dist/services/base/IService.d.ts +18 -1
  115. package/dist/services/base/KeyValueDBService.d.ts +5 -4
  116. package/dist/services/base/KeyValueDBService.js +46 -48
  117. package/dist/services/base/ReplicationService.d.ts +3 -1
  118. package/dist/services/base/ReplicationService.js +3 -1
  119. package/dist/services/base/VaultScanResult.d.ts +5 -0
  120. package/dist/services/lib/HandlerUtils.d.ts +40 -5
  121. package/dist/services/lib/HandlerUtils.js +52 -4
  122. package/dist/storage.d.ts +2 -0
  123. package/dist/storage.js +2 -0
  124. package/docs/development.md +111 -0
  125. package/docs/offline-scanner.md +46 -0
  126. package/docs/p2p-transport-lifecycle.md +24 -0
  127. package/docs/platform-storage.md +22 -0
  128. package/docs/releasing.md +48 -0
  129. package/docs/settings-lifecycle.md +4 -2
  130. package/package.json +1 -1
@@ -0,0 +1,278 @@
1
+ import { getPathNormalizationVersion } from "../../../common/pathNormalization.js";
2
+ import { bindContext } from "octagonal-wheels/context";
3
+ import { scheduleOnceIfDuplicated } from "octagonal-wheels/concurrency/lock_v2";
4
+ import { delay } from "octagonal-wheels/promises";
5
+ import {
6
+ Logger,
7
+ LOG_LEVEL_INFO,
8
+ LOG_LEVEL_NOTICE,
9
+ LOG_LEVEL_VERBOSE
10
+ } from "octagonal-wheels/common/logger";
11
+ import { readLocalP2PPathIdentity, readRemoteP2PPathIdentity } from "../TrysteroReplicator.pathIdentity.js";
12
+ import { fromP2PReplicationWireResult } from "../P2PReplicationWire.js";
13
+ import { pullResultDiagnostic, pullFailureDiagnostic } from "../p2pReplicationDiagnostics.js";
14
+ const P2P_REPLICATION_COMPLETED = Object.freeze({ status: "completed", ok: true });
15
+ const P2P_REPLICATION_CANCELLED = Object.freeze({ status: "cancelled" });
16
+ function p2pReplicationFailed(error) {
17
+ return { status: "failed", error };
18
+ }
19
+ function isP2PReplicationCancelled(result) {
20
+ return result.status === "cancelled";
21
+ }
22
+ function clearActivePeers({ state }) {
23
+ state.replicateFromPeers.clear();
24
+ state.replicateToPeers.clear();
25
+ }
26
+ function createTransfers(deps) {
27
+ const state = {
28
+ availableReplicationPairs: /* @__PURE__ */ new Set(),
29
+ replicateToPeers: /* @__PURE__ */ new Set(),
30
+ replicateFromPeers: /* @__PURE__ */ new Set()
31
+ };
32
+ const agreement = bindContext({ deps }, checkPeerForReplication);
33
+ const transfer = bindContext({ deps }, transferFromPeer);
34
+ const pull = bindContext(
35
+ { deps, state, checkPeerForReplication: agreement, transferFromPeer: transfer },
36
+ pullFromWithinSession
37
+ );
38
+ return Object.assign(state, {
39
+ requestSynchroniseToPeer: bindContext({ deps }, requestSynchroniseToPeer),
40
+ requestSynchroniseToAllAvailablePeers: bindContext({ deps, state }, requestSynchroniseToAllAvailablePeers),
41
+ sync: bindContext({ deps }, sync),
42
+ replicateFrom: bindContext({ deps, pullFromWithinSession: pull }, replicateFrom),
43
+ clearActivePeers: bindContext({ state }, clearActivePeers)
44
+ });
45
+ }
46
+ async function requestSynchroniseToPeer(context, peerId, signal) {
47
+ const { deps } = context;
48
+ if (!await deps.canStartOrdinaryReplication(false)) {
49
+ return p2pReplicationFailed(new Error("Replication is not ready"));
50
+ }
51
+ if (signal?.aborted) {
52
+ return P2P_REPLICATION_CANCELLED;
53
+ }
54
+ await delay(25);
55
+ if (signal?.aborted) {
56
+ return P2P_REPLICATION_CANCELLED;
57
+ }
58
+ const host = deps.host();
59
+ if (!host) {
60
+ throw new Error("Server is not available");
61
+ }
62
+ const conn = host.connection(peerId);
63
+ try {
64
+ const result = await conn.requestSynchronisation(0, signal);
65
+ return fromP2PReplicationWireResult(result);
66
+ } catch (error) {
67
+ if (signal?.aborted) {
68
+ return P2P_REPLICATION_CANCELLED;
69
+ }
70
+ throw error;
71
+ }
72
+ }
73
+ async function requestSynchroniseToAllAvailablePeers(context) {
74
+ const { deps, state } = context;
75
+ await scheduleOnceIfDuplicated("requestSynchroniseToAllAvailablePeers", async () => {
76
+ await delay(25);
77
+ const replications = [...state.availableReplicationPairs].map((peerId) => {
78
+ return deps.requestSynchroniseToPeer(peerId);
79
+ });
80
+ await Promise.all(replications);
81
+ });
82
+ }
83
+ async function sync(context, remotePeer, showNotice, signal) {
84
+ const { deps } = context;
85
+ const from = await deps.replicateFrom(remotePeer, showNotice, false, false, signal);
86
+ if (isP2PReplicationCancelled(from)) {
87
+ return from;
88
+ }
89
+ if (from.error) {
90
+ Logger("Error while replicating from the remote", LOG_LEVEL_VERBOSE);
91
+ Logger(from.error, LOG_LEVEL_VERBOSE);
92
+ return from;
93
+ }
94
+ if (signal?.aborted) {
95
+ return P2P_REPLICATION_CANCELLED;
96
+ }
97
+ const logLevel = showNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO;
98
+ Logger(`P2P Replication has been requested to ${remotePeer}`, logLevel, "p2p-replicator");
99
+ const res = await deps.requestSynchroniseToPeer(remotePeer, signal);
100
+ if (res.ok) {
101
+ Logger("P2P Replication has been done", logLevel, "p2p-replicator");
102
+ }
103
+ if (res.error) {
104
+ Logger("Error while syncing from the remote", logLevel, "p2p-replicator");
105
+ Logger(res.error, LOG_LEVEL_VERBOSE);
106
+ }
107
+ return res;
108
+ }
109
+ async function replicateFrom(context, remotePeer, showNotice, fromStart, skipOrdinaryReplicationPolicy, signal) {
110
+ const { deps } = context;
111
+ deps.reportDiagnostic({ category: "sync", code: "pull-started", peerId: remotePeer });
112
+ try {
113
+ const result = await context.pullFromWithinSession(
114
+ remotePeer,
115
+ showNotice,
116
+ fromStart,
117
+ skipOrdinaryReplicationPolicy,
118
+ signal
119
+ );
120
+ deps.reportDiagnostic(pullResultDiagnostic(remotePeer, result.status));
121
+ return result;
122
+ } catch (error) {
123
+ deps.reportDiagnostic(pullFailureDiagnostic(remotePeer, signal?.aborted ?? false));
124
+ throw error;
125
+ }
126
+ }
127
+ async function pullFromWithinSession(context, remotePeer, showNotice, fromStart, skipOrdinaryReplicationPolicy, signal) {
128
+ const { deps, state } = context;
129
+ if (!skipOrdinaryReplicationPolicy && !await deps.canStartOrdinaryReplication(showNotice)) {
130
+ return p2pReplicationFailed(new Error("Replication is not ready"));
131
+ }
132
+ if (signal?.aborted) {
133
+ return P2P_REPLICATION_CANCELLED;
134
+ }
135
+ const logLevel = showNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO;
136
+ const rejection = await context.checkPeerForReplication(remotePeer, logLevel, signal);
137
+ if (rejection) {
138
+ return rejection;
139
+ }
140
+ Logger(`P2P Replicating from ${remotePeer}`, logLevel, "p2p-replicator");
141
+ if (state.replicateFromPeers.has(remotePeer)) {
142
+ Logger(`Replication from ${remotePeer} is already in progress`, logLevel, "p2p-replicator");
143
+ return p2pReplicationFailed(new Error("Replication from this peer is already in progress"));
144
+ }
145
+ state.replicateFromPeers.add(remotePeer);
146
+ deps.dispatchStatus();
147
+ const acknowledgement = deps.beginProgressAttempt();
148
+ let latestProgress = {
149
+ lastSeq: 0,
150
+ maxSeqInBatch: 0,
151
+ statistics: { documents: 0, checkedChanges: 0 }
152
+ };
153
+ deps.dispatchReplicationProgress(remotePeer, latestProgress);
154
+ deps.acknowledgeProgress(remotePeer, latestProgress, acknowledgement);
155
+ try {
156
+ return await context.transferFromPeer(remotePeer, fromStart, logLevel, signal, (info) => {
157
+ latestProgress = info;
158
+ deps.dispatchReplicationProgress(remotePeer, info);
159
+ deps.acknowledgeProgress(remotePeer, info, acknowledgement);
160
+ });
161
+ } catch (e) {
162
+ if (signal?.aborted) {
163
+ return P2P_REPLICATION_CANCELLED;
164
+ }
165
+ Logger("Error while P2P replicating", logLevel, "p2p-replicator");
166
+ Logger(e, LOG_LEVEL_VERBOSE);
167
+ return p2pReplicationFailed(e);
168
+ } finally {
169
+ deps.dispatchReplicationProgress(remotePeer);
170
+ deps.acknowledgeProgress(remotePeer, void 0, { ...acknowledgement, finalProgress: latestProgress });
171
+ state.replicateFromPeers.delete(remotePeer);
172
+ deps.dispatchStatus();
173
+ }
174
+ }
175
+ async function checkPeerForReplication(context, remotePeer, logLevel, signal) {
176
+ const { deps } = context;
177
+ Logger(`P2P Requesting Authentication to ${remotePeer}`, logLevel, "p2p-replicator");
178
+ const authenticated = signal ? await deps.requestAuthenticate(remotePeer, signal) : await deps.requestAuthenticate(remotePeer);
179
+ if (authenticated !== true) {
180
+ if (signal?.aborted) {
181
+ return P2P_REPLICATION_CANCELLED;
182
+ }
183
+ Logger("Peer rejected the connection", logLevel, "p2p-replicator");
184
+ return p2pReplicationFailed(new Error("Peer rejected the connection"));
185
+ }
186
+ let tweaksMatched;
187
+ try {
188
+ tweaksMatched = signal ? await deps.checkTweakValues(remotePeer, signal, logLevel) : await deps.checkTweakValues(remotePeer, void 0, logLevel);
189
+ } catch (error) {
190
+ if (signal?.aborted) {
191
+ return P2P_REPLICATION_CANCELLED;
192
+ }
193
+ throw error;
194
+ }
195
+ if (tweaksMatched !== true) {
196
+ if (signal?.aborted) {
197
+ return P2P_REPLICATION_CANCELLED;
198
+ }
199
+ Logger("Tweak values are not matched", logLevel, "p2p-replicator");
200
+ return p2pReplicationFailed(new Error("Tweak values are not matched"));
201
+ }
202
+ return void 0;
203
+ }
204
+ async function transferFromPeer(context, remotePeer, fromStart, logLevel, signal, onStatistics) {
205
+ const { deps } = context;
206
+ const host = deps.host();
207
+ if (!host) {
208
+ throw new Error("Server is not available");
209
+ }
210
+ const connection = host.connection(remotePeer);
211
+ const remoteDB = connection.getRemoteDB(signal);
212
+ const pathNormalizationVersion = getPathNormalizationVersion(deps.currentSettings());
213
+ const [localPathIdentity, remotePathIdentity] = await Promise.all([
214
+ readLocalP2PPathIdentity(deps.database(), pathNormalizationVersion),
215
+ readRemoteP2PPathIdentity(remoteDB, pathNormalizationVersion)
216
+ ]);
217
+ if (localPathIdentity !== pathNormalizationVersion || remotePathIdentity !== pathNormalizationVersion) {
218
+ const error = new Error("P2P database path identity does not match the configured path identity.");
219
+ Logger(error.message, logLevel, "p2p-replicator");
220
+ return p2pReplicationFailed(error);
221
+ }
222
+ Logger(`P2P replicateFrom preparing remote DB info for ${remotePeer}`, LOG_LEVEL_VERBOSE, "p2p-replicator");
223
+ const remoteDBInfo = await remoteDB.info();
224
+ Logger(
225
+ `P2P replicateFrom remote DB info for ${remotePeer}: ${remoteDBInfo.db_name} seq=${remoteDBInfo.update_seq}`,
226
+ LOG_LEVEL_VERBOSE,
227
+ "p2p-replicator"
228
+ );
229
+ const localDBInfo = await deps.database().info();
230
+ Logger(
231
+ `P2P replicateFrom local DB info for ${remotePeer}: ${localDBInfo.db_name} seq=${localDBInfo.update_seq}`,
232
+ LOG_LEVEL_VERBOSE,
233
+ "p2p-replicator"
234
+ );
235
+ Logger(`P2P replicateFrom entering replicateShim for ${remotePeer}`, LOG_LEVEL_VERBOSE, "p2p-replicator");
236
+ const outcome = await deps.replicate(
237
+ deps.database(),
238
+ remoteDB,
239
+ async (docs, info) => {
240
+ await deps.processReplicatedDocs(docs);
241
+ void deps.notifyProgress(remotePeer);
242
+ Logger(
243
+ `P2P Replication from ${remotePeer}
244
+ ${info.lastSeq} / ${info.maxSeqInBatch})`,
245
+ logLevel,
246
+ "p2p-replicator"
247
+ );
248
+ },
249
+ {
250
+ live: false,
251
+ rewind: fromStart,
252
+ signal,
253
+ onStatistics
254
+ }
255
+ );
256
+ Logger(`P2P replicateFrom replicateShim returned for ${remotePeer}`, LOG_LEVEL_VERBOSE, "p2p-replicator");
257
+ if (outcome.status === "cancelled") {
258
+ Logger(`P2P Replication from ${remotePeer} has been cancelled`, logLevel, "p2p-replicator");
259
+ return P2P_REPLICATION_CANCELLED;
260
+ }
261
+ Logger(`P2P Replication from ${remotePeer} has been completed`, logLevel, "p2p-replicator");
262
+ return P2P_REPLICATION_COMPLETED;
263
+ }
264
+ export {
265
+ P2P_REPLICATION_CANCELLED,
266
+ P2P_REPLICATION_COMPLETED,
267
+ checkPeerForReplication,
268
+ clearActivePeers,
269
+ createTransfers,
270
+ isP2PReplicationCancelled,
271
+ p2pReplicationFailed,
272
+ pullFromWithinSession,
273
+ replicateFrom,
274
+ requestSynchroniseToAllAvailablePeers,
275
+ requestSynchroniseToPeer,
276
+ sync,
277
+ transferFromPeer
278
+ };