@vrtmrz/livesync-commonlib 0.1.0-rc.1 → 0.1.0-rc.3
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 +87 -19
- package/dist/common/configForDoc.js +6 -3
- package/dist/common/i18n.js +7 -7099
- package/dist/common/messages/combinedMessages.prod.d.ts +12 -0
- package/dist/common/messages/combinedMessages.prod.js +12 -0
- package/dist/common/messages/def.d.ts +4 -0
- package/dist/common/messagesJson/en.json +4 -0
- package/dist/common/models/setting.const.defaults.d.ts +19 -0
- package/dist/common/models/setting.const.defaults.js +21 -2
- package/dist/common/models/setting.lifecycle.d.ts +31 -0
- package/dist/common/models/setting.lifecycle.js +109 -0
- package/dist/common/models/setting.type.d.ts +4 -2
- package/dist/common/settingConstants.js +2 -2
- package/dist/common/types.d.ts +4 -2
- package/dist/common/types.js +16 -1
- package/dist/interfaces/Confirm.d.ts +10 -2
- package/dist/interfaces/DatabaseRebuilder.d.ts +12 -2
- package/dist/remoteConfigurations.d.ts +9 -0
- package/dist/remoteConfigurations.js +14 -0
- package/dist/replication/LiveSyncAbstractReplicator.d.ts +1 -2
- package/dist/replication/couchdb/LiveSyncReplicator.d.ts +0 -1
- package/dist/replication/couchdb/LiveSyncReplicator.js +0 -3
- package/dist/replication/journal/LiveSyncJournalReplicator.d.ts +0 -1
- package/dist/replication/journal/LiveSyncJournalReplicator.js +0 -3
- package/dist/replication/trystero/LiveSyncTrysteroReplicator.d.ts +5 -3
- package/dist/replication/trystero/LiveSyncTrysteroReplicator.js +42 -30
- package/dist/replication/trystero/P2PReplicatorCore.d.ts +2 -6
- package/dist/replication/trystero/P2PReplicatorCore.js +16 -86
- package/dist/replication/trystero/TrysteroReplicator.d.ts +2 -1
- package/dist/replication/trystero/TrysteroReplicator.js +10 -4
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.d.ts +4 -5
- package/dist/replication/trystero/TrysteroReplicatorP2PServer.js +22 -30
- package/dist/replication/trystero/UseP2PReplicatorResult.d.ts +10 -2
- package/dist/replication/trystero/addP2PEventHandlers.d.ts +4 -2
- package/dist/replication/trystero/addP2PEventHandlers.js +8 -6
- package/dist/replication/trystero/types.d.ts +2 -0
- package/dist/replication/trystero/useP2PReplicatorCommands.d.ts +1 -1
- package/dist/replication/trystero/useP2PReplicatorCommands.js +3 -1
- package/dist/replication/trystero/useP2PReplicatorFeature.js +1 -1
- package/dist/rpc/RpcRoom.d.ts +1 -1
- package/dist/rpc/RpcRoom.js +22 -16
- package/dist/rpc/transports/TrysteroTransport.d.ts +2 -0
- package/dist/rpc/transports/TrysteroTransport.js +24 -17
- package/dist/rpc/transports/trysteroRoomEvents.d.ts +14 -0
- package/dist/rpc/transports/trysteroRoomEvents.js +34 -0
- package/dist/serviceFeatures/checkRemoteSize.d.ts +5 -3
- package/dist/serviceFeatures/checkRemoteSize.js +51 -11
- package/dist/serviceFeatures/remoteConfig.d.ts +37 -1
- package/dist/serviceFeatures/remoteConfig.js +90 -0
- package/dist/serviceModules/Rebuilder.d.ts +3 -6
- package/dist/serviceModules/Rebuilder.js +23 -19
- package/dist/services/base/IService.d.ts +13 -6
- package/dist/services/base/ReplicationService.d.ts +4 -2
- package/dist/services/base/ReplicationService.js +11 -16
- package/dist/services/base/SettingService.d.ts +14 -9
- package/dist/services/base/SettingService.js +35 -17
- package/dist/services/implements/headless/HeadlessAPIService.d.ts +3 -3
- package/dist/services/implements/headless/HeadlessAPIService.js +2 -2
- package/dist/services/implements/injectable/InjectableTweakValueService.d.ts +3 -3
- package/dist/settings.d.ts +10 -0
- package/dist/settings.js +32 -0
- package/dist/worker/bgWorker.js +1 -1
- package/docs/development.md +83 -1
- package/docs/p2p-transport-lifecycle.md +52 -0
- package/docs/proven-in-use.md +57 -0
- package/docs/releasing.md +12 -6
- package/docs/remote-configurations.md +63 -0
- package/docs/settings-lifecycle.md +60 -0
- package/package.json +12 -2
|
@@ -18,6 +18,8 @@ import { delay } from "octagonal-wheels/promises";
|
|
|
18
18
|
class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
19
19
|
_p2pHost;
|
|
20
20
|
_replicator;
|
|
21
|
+
_lifecycleOperation = Promise.resolve();
|
|
22
|
+
_shouldBeOpen = false;
|
|
21
23
|
get openReplicationUI() {
|
|
22
24
|
return this.env.openReplicationUI;
|
|
23
25
|
}
|
|
@@ -27,9 +29,6 @@ class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
|
27
29
|
get rawHost() {
|
|
28
30
|
return this._p2pHost;
|
|
29
31
|
}
|
|
30
|
-
get isChunkSendingSupported() {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
32
|
getReplicationPBKDF2Salt(_setting, _refresh) {
|
|
34
33
|
return Promise.resolve(new Uint8Array(32));
|
|
35
34
|
}
|
|
@@ -59,6 +58,7 @@ class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
|
59
58
|
return services.API.confirm;
|
|
60
59
|
},
|
|
61
60
|
runFiniteReplicationActivity: (task, options) => services.replicator.runFiniteReplicationActivity(task, options),
|
|
61
|
+
canStartOrdinaryReplication: (showMessage = false) => services.replication.onCheckReplicationReady(showMessage),
|
|
62
62
|
processReplicatedDocs: async (docs) => {
|
|
63
63
|
const settings = services.setting.currentSettings();
|
|
64
64
|
if (settings.suspendParseReplicationResult) {
|
|
@@ -75,36 +75,48 @@ class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
|
75
75
|
}
|
|
76
76
|
};
|
|
77
77
|
}
|
|
78
|
+
_enqueueLifecycleOperation(operation) {
|
|
79
|
+
const queued = this._lifecycleOperation.catch(() => void 0).then(operation);
|
|
80
|
+
this._lifecycleOperation = queued.catch(() => void 0);
|
|
81
|
+
return queued;
|
|
82
|
+
}
|
|
78
83
|
async open() {
|
|
79
84
|
if (!this.env.services.setting.currentSettings().P2P_Enabled) {
|
|
80
85
|
Logger(this.translate("P2P.NotEnabled"), LOG_LEVEL_NOTICE);
|
|
81
86
|
return;
|
|
82
87
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
88
|
+
this._shouldBeOpen = true;
|
|
89
|
+
await this._enqueueLifecycleOperation(async () => {
|
|
90
|
+
if (!this._shouldBeOpen) return;
|
|
91
|
+
if (this._replicator && this._p2pHost?.isServing) {
|
|
92
|
+
Logger("P2P replicator is already open.");
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const env = this._buildEnv();
|
|
97
|
+
const host = new P2PHost(env);
|
|
98
|
+
const replicator = new TrysteroReplicator(env, host);
|
|
99
|
+
this._p2pHost = host;
|
|
100
|
+
this._replicator = replicator;
|
|
101
|
+
await replicator.open();
|
|
102
|
+
} catch (e) {
|
|
103
|
+
Logger(e instanceof Error ? e.message : "Error while opening P2P connection", LOG_LEVEL_NOTICE);
|
|
104
|
+
Logger(e, LOG_LEVEL_VERBOSE);
|
|
105
|
+
this._p2pHost = void 0;
|
|
106
|
+
this._replicator = void 0;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
100
109
|
}
|
|
101
110
|
async close() {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
this._shouldBeOpen = false;
|
|
112
|
+
await this._enqueueLifecycleOperation(async () => {
|
|
113
|
+
if (this._replicator) {
|
|
114
|
+
this._replicator.disableBroadcastChanges();
|
|
115
|
+
await this._replicator.close();
|
|
116
|
+
this._replicator = void 0;
|
|
117
|
+
}
|
|
118
|
+
this._p2pHost = void 0;
|
|
119
|
+
});
|
|
108
120
|
}
|
|
109
121
|
closeReplication() {
|
|
110
122
|
this._replicator?.disconnectFromServer();
|
|
@@ -138,11 +150,11 @@ class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
|
138
150
|
{ label: "replication" }
|
|
139
151
|
);
|
|
140
152
|
}
|
|
141
|
-
async replicateFrom(peerId, showNotice = false) {
|
|
153
|
+
async replicateFrom(peerId, showNotice = false, skipOrdinaryReplicationPolicy = false) {
|
|
142
154
|
const replicator = this._replicator;
|
|
143
155
|
if (!replicator) throw new Error("P2P replicator is not open");
|
|
144
156
|
return await this.env.services.replicator.runFiniteReplicationActivity(
|
|
145
|
-
() => replicator.replicateFrom(peerId, showNotice),
|
|
157
|
+
() => skipOrdinaryReplicationPolicy ? replicator.replicateFrom(peerId, showNotice, false, true) : replicator.replicateFrom(peerId, showNotice),
|
|
146
158
|
{ label: "replication" }
|
|
147
159
|
);
|
|
148
160
|
}
|
|
@@ -200,7 +212,7 @@ class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
|
200
212
|
tryConnectRemote(_setting, _showResult) {
|
|
201
213
|
return Promise.resolve(false);
|
|
202
214
|
}
|
|
203
|
-
replicateAllToServer(_setting, _showingNotice
|
|
215
|
+
replicateAllToServer(_setting, _showingNotice) {
|
|
204
216
|
return Promise.resolve(false);
|
|
205
217
|
}
|
|
206
218
|
async selectPeer(settingPeerName, r, logLevel) {
|
|
@@ -303,7 +315,7 @@ class LiveSyncTrysteroReplicator extends LiveSyncAbstractReplicator {
|
|
|
303
315
|
}
|
|
304
316
|
this.env.services.setting.currentSettings().P2P_RebuildFrom = "";
|
|
305
317
|
Logger("Fetching from peer " + peerId + ".", logLevel);
|
|
306
|
-
const rep = await r.replicateFrom(peerId, showingNotice);
|
|
318
|
+
const rep = await r.replicateFrom(peerId, showingNotice, false, true);
|
|
307
319
|
if (rep.ok) {
|
|
308
320
|
Logger("P2P Fetching has been succeed from " + peerId + ".", logLevel);
|
|
309
321
|
return true;
|
|
@@ -2,11 +2,7 @@ import type { NecessaryServices } from "../../interfaces/ServiceModule.js";
|
|
|
2
2
|
import type { P2PPaneParams } from "./UseP2PReplicatorResult.js";
|
|
3
3
|
export type P2PViewFactory = (leaf: unknown) => unknown;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* following the same middleware style as useOfflineScanner.
|
|
8
|
-
*
|
|
9
|
-
* @param viewTypeAndFactory Optional [viewType, factory] pair for registering the P2P pane view.
|
|
10
|
-
* When provided, also registers commands and ribbon icon via services.API.
|
|
5
|
+
* @deprecated Compose `useP2PReplicatorFeature` with host-specific commands
|
|
6
|
+
* and UI instead. This wrapper remains for source compatibility.
|
|
11
7
|
*/
|
|
12
8
|
export declare function useP2PReplicator(host: NecessaryServices<"API" | "appLifecycle" | "setting" | "vault" | "database" | "databaseEvents" | "keyValueDB" | "replication" | "config" | "UI" | "replicator" | "remote", never>, viewTypeAndFactory?: [viewType: string, factory: P2PViewFactory]): P2PPaneParams;
|
|
@@ -1,110 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { reactiveSource } from "octagonal-wheels/dataobject/
|
|
1
|
+
import { REMOTE_P2P } from "../../common/types.js";
|
|
2
|
+
import { reactiveSource } from "octagonal-wheels/dataobject/reactive_v2";
|
|
3
3
|
import { EVENT_REQUEST_OPEN_P2P } from "../../events/coreEvents.js";
|
|
4
|
-
import { LiveSyncTrysteroReplicator } from "./LiveSyncTrysteroReplicator.js";
|
|
5
|
-
import { Logger, LOG_LEVEL_NOTICE, LOG_LEVEL_VERBOSE } from "../../common/logger.js";
|
|
6
4
|
import { P2PLogCollector } from "./P2PLogCollector.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
5
|
+
import { useP2PReplicatorCommands } from "./useP2PReplicatorCommands.js";
|
|
6
|
+
import { useP2PReplicatorFeature } from "./useP2PReplicatorFeature.js";
|
|
9
7
|
function useP2PReplicator(host, viewTypeAndFactory) {
|
|
10
|
-
const
|
|
11
|
-
let replicator = new LiveSyncTrysteroReplicator(env);
|
|
12
|
-
const activeReplicator = {
|
|
13
|
-
get instance() {
|
|
14
|
-
return replicator;
|
|
15
|
-
}
|
|
16
|
-
};
|
|
8
|
+
const activeReplicator = useP2PReplicatorFeature(host);
|
|
17
9
|
const events = host.services.context.events;
|
|
18
|
-
addP2PEventHandlers(activeReplicator.instance, events);
|
|
19
10
|
const p2pLogCollector = new P2PLogCollector(events);
|
|
20
11
|
const storeP2PStatusLine = reactiveSource("");
|
|
21
12
|
p2pLogCollector.p2pReplicationLine.onChanged((line) => {
|
|
22
13
|
storeP2PStatusLine.value = line.value;
|
|
23
14
|
});
|
|
24
|
-
host.services.appLifecycle.onResumed.addHandler(() => {
|
|
25
|
-
const settings = host.services.setting.currentSettings();
|
|
26
|
-
if (settings.P2P_Enabled && settings.P2P_AutoStart) {
|
|
27
|
-
compatGlobal.setTimeout(() => void replicator.open(), 100);
|
|
28
|
-
}
|
|
29
|
-
return Promise.resolve(true);
|
|
30
|
-
});
|
|
31
|
-
host.services.appLifecycle.onUnload.addHandler(async () => {
|
|
32
|
-
await replicator.close();
|
|
33
|
-
return true;
|
|
34
|
-
});
|
|
35
|
-
host.services.appLifecycle.onSuspending.addHandler(async () => {
|
|
36
|
-
await replicator.close();
|
|
37
|
-
return true;
|
|
38
|
-
});
|
|
39
|
-
host.services.databaseEvents.onDatabaseInitialisation.addHandler(async () => {
|
|
40
|
-
await replicator.close();
|
|
41
|
-
return true;
|
|
42
|
-
});
|
|
43
|
-
host.services.setting.suspendExtraSync.addHandler(() => {
|
|
44
|
-
const s = host.services.setting.currentSettings();
|
|
45
|
-
if (s.remoteType !== REMOTE_P2P) {
|
|
46
|
-
s.P2P_Enabled = false;
|
|
47
|
-
}
|
|
48
|
-
s.P2P_AutoAccepting = AutoAccepting.NONE;
|
|
49
|
-
s.P2P_AutoBroadcast = false;
|
|
50
|
-
s.P2P_AutoStart = false;
|
|
51
|
-
s.P2P_AutoSyncPeers = "";
|
|
52
|
-
s.P2P_AutoWatchPeers = "";
|
|
53
|
-
return Promise.resolve(true);
|
|
54
|
-
});
|
|
55
|
-
host.services.replicator.getNewReplicator.addHandler(
|
|
56
|
-
async (settingOverride = {}) => {
|
|
57
|
-
const settings = { ...host.services.setting.currentSettings(), ...settingOverride };
|
|
58
|
-
if (settings.remoteType == REMOTE_P2P) {
|
|
59
|
-
try {
|
|
60
|
-
await replicator.close();
|
|
61
|
-
} catch (e) {
|
|
62
|
-
Logger(`Error closing existing p2p replicator`);
|
|
63
|
-
Logger(e, LOG_LEVEL_VERBOSE);
|
|
64
|
-
}
|
|
65
|
-
const newReplicator = new LiveSyncTrysteroReplicator({ services: host.services });
|
|
66
|
-
replicator = newReplicator;
|
|
67
|
-
return Promise.resolve(replicator);
|
|
68
|
-
}
|
|
69
|
-
return void 0;
|
|
70
|
-
}
|
|
71
|
-
);
|
|
72
15
|
if (viewTypeAndFactory) {
|
|
73
16
|
const [viewType, factory] = viewTypeAndFactory;
|
|
74
17
|
const openPane = () => host.services.API.showWindow(viewType);
|
|
18
|
+
useP2PReplicatorCommands(host, activeReplicator);
|
|
75
19
|
host.services.appLifecycle.onInitialise.addHandler(() => {
|
|
76
20
|
host.services.API.registerWindow(viewType, factory);
|
|
77
|
-
events.onEvent(EVENT_REQUEST_OPEN_P2P, () =>
|
|
78
|
-
void openPane();
|
|
79
|
-
});
|
|
21
|
+
events.onEvent(EVENT_REQUEST_OPEN_P2P, () => void openPane());
|
|
80
22
|
host.services.API.addCommand({
|
|
81
23
|
id: "open-p2p-replicator",
|
|
82
24
|
name: "P2P Sync : Open P2P Replicator",
|
|
83
|
-
callback: () =>
|
|
84
|
-
void openPane();
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
host.services.API.addCommand({
|
|
88
|
-
id: "p2p-establish-connection",
|
|
89
|
-
name: "P2P Sync : Connect to the Signalling Server",
|
|
90
|
-
checkCallback: (isChecking) => {
|
|
91
|
-
if (isChecking) return !(replicator.server?.isServing ?? false);
|
|
92
|
-
void replicator.open();
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
host.services.API.addCommand({
|
|
96
|
-
id: "p2p-close-connection",
|
|
97
|
-
name: "P2P Sync : Disconnect from the Signalling Server",
|
|
98
|
-
checkCallback: (isChecking) => {
|
|
99
|
-
if (isChecking) return replicator.server?.isServing ?? false;
|
|
100
|
-
Logger("Closing P2P Connection", LOG_LEVEL_NOTICE);
|
|
101
|
-
void replicator.close();
|
|
102
|
-
}
|
|
25
|
+
callback: () => void openPane()
|
|
103
26
|
});
|
|
104
27
|
host.services.API.addCommand({
|
|
105
28
|
id: "replicate-now-by-p2p",
|
|
106
29
|
name: "Replicate now by P2P",
|
|
107
30
|
checkCallback: (isChecking) => {
|
|
31
|
+
const replicator = activeReplicator.replicator;
|
|
108
32
|
const settings = host.services.setting.currentSettings();
|
|
109
33
|
if (isChecking) {
|
|
110
34
|
if (settings.remoteType == REMOTE_P2P) return false;
|
|
@@ -119,7 +43,13 @@ function useP2PReplicator(host, viewTypeAndFactory) {
|
|
|
119
43
|
return Promise.resolve(true);
|
|
120
44
|
});
|
|
121
45
|
}
|
|
122
|
-
return {
|
|
46
|
+
return {
|
|
47
|
+
get replicator() {
|
|
48
|
+
return activeReplicator.replicator;
|
|
49
|
+
},
|
|
50
|
+
p2pLogCollector,
|
|
51
|
+
storeP2PStatusLine
|
|
52
|
+
};
|
|
123
53
|
}
|
|
124
54
|
export {
|
|
125
55
|
useP2PReplicator
|
|
@@ -64,6 +64,7 @@ export declare class TrysteroReplicator {
|
|
|
64
64
|
get confirm(): Confirm;
|
|
65
65
|
get translate(): import("../../context.js").MessageTranslator;
|
|
66
66
|
private runFiniteReplicationActivity;
|
|
67
|
+
private canStartOrdinaryReplication;
|
|
67
68
|
constructor(env: ReplicatorHostEnv, server?: P2PHost);
|
|
68
69
|
close(): Promise<void>;
|
|
69
70
|
open(): Promise<void>;
|
|
@@ -123,7 +124,7 @@ export declare class TrysteroReplicator {
|
|
|
123
124
|
onReplicationProgress(peerId: string, info?: ProgressInfo): boolean;
|
|
124
125
|
onProgressAcknowledged(peerId: string, info?: ProgressInfo): boolean;
|
|
125
126
|
acknowledgeProgress(remotePeerId: string, info?: ProgressInfo): void;
|
|
126
|
-
replicateFrom(remotePeer: string, showNotice?: boolean, fromStart?: boolean): Promise<{
|
|
127
|
+
replicateFrom(remotePeer: string, showNotice?: boolean, fromStart?: boolean, skipOrdinaryReplicationPolicy?: boolean): Promise<{
|
|
127
128
|
error: any;
|
|
128
129
|
ok?: undefined;
|
|
129
130
|
} | {
|
|
@@ -51,6 +51,9 @@ class TrysteroReplicator {
|
|
|
51
51
|
}
|
|
52
52
|
return await task();
|
|
53
53
|
}
|
|
54
|
+
async canStartOrdinaryReplication(showMessage = false) {
|
|
55
|
+
return this._env.canStartOrdinaryReplication ? await this._env.canStartOrdinaryReplication(showMessage) : true;
|
|
56
|
+
}
|
|
54
57
|
constructor(env, server) {
|
|
55
58
|
this._env = env;
|
|
56
59
|
if (server) {
|
|
@@ -265,6 +268,9 @@ class TrysteroReplicator {
|
|
|
265
268
|
// }
|
|
266
269
|
lastSeq = "";
|
|
267
270
|
async requestSynchroniseToPeer(peerId) {
|
|
271
|
+
if (!await this.canStartOrdinaryReplication(false)) {
|
|
272
|
+
return { error: new Error("Replication is not ready") };
|
|
273
|
+
}
|
|
268
274
|
await delay(25);
|
|
269
275
|
if (!this.server) throw new Error("Server is not available");
|
|
270
276
|
const conn = this.server.getConnection(peerId);
|
|
@@ -440,7 +446,10 @@ class TrysteroReplicator {
|
|
|
440
446
|
Logger(ex, LOG_LEVEL_VERBOSE);
|
|
441
447
|
});
|
|
442
448
|
}
|
|
443
|
-
async replicateFrom(remotePeer, showNotice = false, fromStart = false) {
|
|
449
|
+
async replicateFrom(remotePeer, showNotice = false, fromStart = false, skipOrdinaryReplicationPolicy = false) {
|
|
450
|
+
if (!skipOrdinaryReplicationPolicy && !await this.canStartOrdinaryReplication(showNotice)) {
|
|
451
|
+
return { error: new Error("Replication is not ready") };
|
|
452
|
+
}
|
|
444
453
|
const logLevel = showNotice ? LOG_LEVEL_NOTICE : LOG_LEVEL_INFO;
|
|
445
454
|
Logger(`P2P Requesting Authentication to ${remotePeer}`, logLevel, "p2p-replicator");
|
|
446
455
|
if (await this.requestAuthenticate(remotePeer) !== true) {
|
|
@@ -664,9 +673,6 @@ ${info.lastSeq} / ${info.maxSeqInBatch})`,
|
|
|
664
673
|
pauseRelayReconnection();
|
|
665
674
|
sockets.forEach(([, s]) => {
|
|
666
675
|
s.close();
|
|
667
|
-
s.onclose = () => {
|
|
668
|
-
void this.server?.dispatchConnectionStatus();
|
|
669
|
-
};
|
|
670
676
|
});
|
|
671
677
|
void this.pauseServe();
|
|
672
678
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Room } from "@trystero-p2p/nostr";
|
|
2
2
|
import { type P2PSyncSetting } from "../../common/types.js";
|
|
3
3
|
import { type ReplicatorHostEnv, type FullFilledDeviceInfo, type Request, type Response, type Payload, type Advertisement, type BindableObject, type BindableFunction } from "./types.js";
|
|
4
4
|
import { StoredMapLike } from "../../dataobject/StoredMap.js";
|
|
@@ -56,7 +56,7 @@ export declare class TrysteroReplicatorP2PServer {
|
|
|
56
56
|
_room?: Room;
|
|
57
57
|
_serverPeerId: string;
|
|
58
58
|
_activeRoomId: string;
|
|
59
|
-
___send?:
|
|
59
|
+
___send?: (data: Payload, peerId?: string) => Promise<void>;
|
|
60
60
|
assignedFunctions: Map<string, BindableFunction>;
|
|
61
61
|
clients: Map<string, TrysteroReplicatorP2PClient>;
|
|
62
62
|
_bindingObjects: BindableObject[];
|
|
@@ -81,7 +81,7 @@ export declare class TrysteroReplicatorP2PServer {
|
|
|
81
81
|
get settings(): P2PSyncSetting;
|
|
82
82
|
get isEnabled(): boolean;
|
|
83
83
|
get deviceInfo(): FullFilledDeviceInfo;
|
|
84
|
-
_sendAdvertisement?:
|
|
84
|
+
_sendAdvertisement?: (data: Advertisement, peerId?: string) => Promise<void>;
|
|
85
85
|
sendAdvertisement(peerId?: string): void;
|
|
86
86
|
_knownAdvertisements: Map<string, Advertisement>;
|
|
87
87
|
get knownAdvertisements(): Advertisement[];
|
|
@@ -93,11 +93,10 @@ export declare class TrysteroReplicatorP2PServer {
|
|
|
93
93
|
_acceptablePeers: Computed<[settings: P2PSyncSetting], RegExp[]>;
|
|
94
94
|
_shouldDenyPeers: Computed<[settings: P2PSyncSetting], RegExp[]>;
|
|
95
95
|
isAcceptablePeer(peerId: string): Promise<boolean>;
|
|
96
|
-
__send(data: Payload, peerId: string): Promise<void
|
|
96
|
+
__send(data: Payload, peerId: string): Promise<void>;
|
|
97
97
|
processArrivedRPC(data: Payload, peerId: string): Promise<void>;
|
|
98
98
|
private _onPeerJoin;
|
|
99
99
|
private _onPeerLeave;
|
|
100
|
-
activePeer: Map<string, RTCPeerConnection>;
|
|
101
100
|
onAfterJoinRoom(): void;
|
|
102
101
|
startService(bindings?: BindableObject[]): Promise<void>;
|
|
103
102
|
start(bindings?: BindableObject[]): Promise<void>;
|
|
@@ -14,6 +14,7 @@ import { shareRunningResult } from "octagonal-wheels/concurrency/lock_v2";
|
|
|
14
14
|
import { Computed } from "octagonal-wheels/dataobject/Computed";
|
|
15
15
|
import { RpcRoom } from "../../rpc/index.js";
|
|
16
16
|
import { TRYSTERO_RPC_DEFAULTS } from "../../rpc/transports/TrysteroTransport.js";
|
|
17
|
+
import { subscribeTrysteroPeerEvents } from "../../rpc/transports/trysteroRoomEvents.js";
|
|
17
18
|
import { toRpcMethodName } from "./rpcCompat.js";
|
|
18
19
|
import { generateJoinRoomOptions } from "../../rpc/transports/trysteroUtils.js";
|
|
19
20
|
import { subscribeConnectionStatus, subscribeFailureDiagnosis } from "../../rpc/transports/DiagRTCPeerConnections.js";
|
|
@@ -333,52 +334,47 @@ You can chose as follows:
|
|
|
333
334
|
Logger(`Received peer join event from ${peerId}, but no active room. Ignoring.`, LOG_LEVEL_VERBOSE);
|
|
334
335
|
return;
|
|
335
336
|
}
|
|
336
|
-
const peers = this._room.getPeers();
|
|
337
|
-
const peer = peers[peerId];
|
|
338
337
|
Logger(`Peer joined: ${peerId}`, LOG_LEVEL_VERBOSE);
|
|
339
|
-
this.activePeer.set(peerId, peer);
|
|
340
338
|
this.sendAdvertisement(peerId);
|
|
341
339
|
}
|
|
342
340
|
_onPeerLeave(peerId) {
|
|
343
341
|
Logger(`Peer left: ${peerId}`, LOG_LEVEL_VERBOSE);
|
|
344
342
|
this._knownAdvertisements.delete(peerId);
|
|
345
|
-
const peerConn = this.activePeer.get(peerId);
|
|
346
|
-
if (peerConn) {
|
|
347
|
-
peerConn.close();
|
|
348
|
-
this.activePeer.delete(peerId);
|
|
349
|
-
}
|
|
350
343
|
void this._env.events.emitEvent(EVENT_DEVICE_LEAVED, peerId);
|
|
351
344
|
void this.dispatchConnectionStatus();
|
|
352
345
|
}
|
|
353
|
-
activePeer = /* @__PURE__ */ new Map();
|
|
354
346
|
onAfterJoinRoom() {
|
|
355
347
|
Logger(`Initializing...`, LOG_LEVEL_VERBOSE);
|
|
356
348
|
const room = this.room;
|
|
357
349
|
if (!room) throw new Error("This server has been already disconnected");
|
|
358
|
-
const
|
|
350
|
+
const rpcAction = room.makeAction("rpc2");
|
|
359
351
|
const transport = {
|
|
360
352
|
send: (message, peerId) => {
|
|
361
|
-
return
|
|
353
|
+
return rpcAction.send(message, { target: peerId });
|
|
362
354
|
},
|
|
363
355
|
onMessage: (handler) => {
|
|
364
|
-
|
|
356
|
+
rpcAction.onMessage = (data, { peerId }) => {
|
|
365
357
|
handler(data, peerId);
|
|
366
|
-
}
|
|
367
|
-
return () =>
|
|
358
|
+
};
|
|
359
|
+
return () => {
|
|
360
|
+
rpcAction.onMessage = null;
|
|
361
|
+
};
|
|
368
362
|
},
|
|
369
363
|
onPeerJoin: (handler) => {
|
|
370
|
-
room
|
|
371
|
-
|
|
372
|
-
|
|
364
|
+
return subscribeTrysteroPeerEvents(room, {
|
|
365
|
+
onJoin: (peerId) => {
|
|
366
|
+
this._onPeerJoin(peerId);
|
|
367
|
+
handler(peerId);
|
|
368
|
+
}
|
|
373
369
|
});
|
|
374
|
-
return () => void 0;
|
|
375
370
|
},
|
|
376
371
|
onPeerLeave: (handler) => {
|
|
377
|
-
room
|
|
378
|
-
|
|
379
|
-
|
|
372
|
+
return subscribeTrysteroPeerEvents(room, {
|
|
373
|
+
onLeave: (peerId) => {
|
|
374
|
+
this._onPeerLeave(peerId);
|
|
375
|
+
handler(peerId);
|
|
376
|
+
}
|
|
380
377
|
});
|
|
381
|
-
return () => void 0;
|
|
382
378
|
}
|
|
383
379
|
};
|
|
384
380
|
this._rpcRoom?.close();
|
|
@@ -393,11 +389,11 @@ You can chose as follows:
|
|
|
393
389
|
Logger(`RPC Protocol warning${peerId ? ` from ${peerId}` : ""}: ${message}`, LOG_LEVEL_VERBOSE);
|
|
394
390
|
}
|
|
395
391
|
});
|
|
396
|
-
const
|
|
397
|
-
this._sendAdvertisement =
|
|
398
|
-
|
|
392
|
+
const advertisementAction = room.makeAction("ad");
|
|
393
|
+
this._sendAdvertisement = (data, peerId) => advertisementAction.send(data, peerId ? { target: peerId } : void 0);
|
|
394
|
+
advertisementAction.onMessage = (data, { peerId }) => {
|
|
399
395
|
void this.onAdvertisement(data, peerId);
|
|
400
|
-
}
|
|
396
|
+
};
|
|
401
397
|
this._env.events.emitEvent(EVENT_P2P_CONNECTED);
|
|
402
398
|
void this.dispatchConnectionStatus();
|
|
403
399
|
}
|
|
@@ -494,14 +490,10 @@ You can chose as follows:
|
|
|
494
490
|
}
|
|
495
491
|
async close() {
|
|
496
492
|
this.assignedFunctions.clear();
|
|
497
|
-
const peers = this.room?.getPeers() ?? {};
|
|
498
493
|
this.clients.forEach((client) => client.close());
|
|
499
494
|
this.clients.clear();
|
|
500
495
|
this._rpcRoom?.close();
|
|
501
496
|
this._rpcRoom = void 0;
|
|
502
|
-
for (const [, peer] of Object.entries(peers)) {
|
|
503
|
-
peer.close();
|
|
504
|
-
}
|
|
505
497
|
await this.ensureLeaved();
|
|
506
498
|
this._activeRoomId = "";
|
|
507
499
|
this._knownAdvertisements.clear();
|
|
@@ -2,10 +2,18 @@ import type { ReactiveSource } from "octagonal-wheels/dataobject/reactive_v2";
|
|
|
2
2
|
import type { LiveSyncTrysteroReplicator } from "./LiveSyncTrysteroReplicator.js";
|
|
3
3
|
import type { P2PLogCollector } from "./P2PLogCollector.js";
|
|
4
4
|
export type UseP2PReplicatorResult = {
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The current replicator selected by the host lifecycle.
|
|
7
|
+
*
|
|
8
|
+
* Read this property at the point of use. Retaining a destructured value
|
|
9
|
+
* across settings or database lifecycle events can target a closed,
|
|
10
|
+
* replaced instance.
|
|
11
|
+
*/
|
|
12
|
+
readonly replicator: LiveSyncTrysteroReplicator;
|
|
6
13
|
};
|
|
7
14
|
export type P2PPaneParams = {
|
|
8
|
-
replicator
|
|
15
|
+
/** Current replicator; preserve the result object so its getter remains live. */
|
|
16
|
+
readonly replicator: LiveSyncTrysteroReplicator;
|
|
9
17
|
p2pLogCollector: P2PLogCollector;
|
|
10
18
|
storeP2PStatusLine: ReactiveSource<string>;
|
|
11
19
|
};
|
|
@@ -17,11 +17,13 @@ export interface P2PReplicatorLike {
|
|
|
17
17
|
isServing?: boolean;
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
+
/** Resolves the replicator which currently owns P2P state. */
|
|
21
|
+
export type P2PReplicatorProvider = () => P2PReplicatorLike;
|
|
20
22
|
/**
|
|
21
23
|
* Add event handlers for P2P replication related events.
|
|
22
|
-
* @param instance
|
|
24
|
+
* @param source A fixed compatibility instance or a provider for a replaceable replicator.
|
|
23
25
|
*/
|
|
24
|
-
export declare function addP2PEventHandlers(
|
|
26
|
+
export declare function addP2PEventHandlers(source: P2PReplicatorLike | P2PReplicatorProvider, events: LiveSyncEventHub): void;
|
|
25
27
|
/**
|
|
26
28
|
* open P2P replicator if not opened yet.
|
|
27
29
|
* @param instance
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { EVENT_DATABASE_REBUILT, EVENT_PLATFORM_UNLOADED, EVENT_SETTING_SAVED } from "../../events/coreEvents.js";
|
|
2
2
|
import { EVENT_ADVERTISEMENT_RECEIVED, EVENT_DEVICE_LEAVED, EVENT_REQUEST_STATUS } from "./TrysteroReplicatorP2PServer.js";
|
|
3
|
-
function addP2PEventHandlers(
|
|
3
|
+
function addP2PEventHandlers(source, events) {
|
|
4
|
+
const current = () => typeof source === "function" ? source() : source;
|
|
4
5
|
events.onEvent(EVENT_ADVERTISEMENT_RECEIVED, (peer) => {
|
|
5
|
-
void
|
|
6
|
+
void current().onNewPeer(peer);
|
|
6
7
|
});
|
|
7
8
|
events.onEvent(EVENT_DEVICE_LEAVED, (peerId) => {
|
|
8
|
-
|
|
9
|
+
current().onPeerLeaved(peerId);
|
|
9
10
|
});
|
|
10
11
|
events.onEvent(EVENT_REQUEST_STATUS, () => {
|
|
11
|
-
|
|
12
|
+
current().requestStatus();
|
|
12
13
|
});
|
|
13
14
|
events.onEvent(EVENT_DATABASE_REBUILT, async () => {
|
|
14
|
-
await
|
|
15
|
+
await current().open();
|
|
15
16
|
});
|
|
16
17
|
events.onEvent(EVENT_PLATFORM_UNLOADED, () => {
|
|
17
|
-
void
|
|
18
|
+
void current().close();
|
|
18
19
|
});
|
|
19
20
|
events.onEvent(EVENT_SETTING_SAVED, async (settings) => {
|
|
21
|
+
const instance = current();
|
|
20
22
|
const isOpen = instance.isServing ?? instance.server?.isServing ?? false;
|
|
21
23
|
if (settings.P2P_Enabled && settings.P2P_AutoStart) {
|
|
22
24
|
await instance.open();
|
|
@@ -85,6 +85,8 @@ export interface ReplicatorHostEnv extends ReplicatorHost {
|
|
|
85
85
|
db: PouchDB.Database<EntryDoc>;
|
|
86
86
|
simpleStore: SimpleStore<unknown>;
|
|
87
87
|
runFiniteReplicationActivity?: AsyncActivityRunner["run"];
|
|
88
|
+
/** Lightweight, repeatable host policy checked before ordinary P2P replication starts. */
|
|
89
|
+
canStartOrdinaryReplication?(showMessage?: boolean): Promise<boolean>;
|
|
88
90
|
processReplicatedDocs(docs: Array<PouchDB.Core.ExistingDocument<EntryDoc>>): void | Promise<void>;
|
|
89
91
|
}
|
|
90
92
|
export type Advertisement = {
|
|
@@ -4,4 +4,4 @@ import type { UseP2PReplicatorResult } from "./UseP2PReplicatorResult.js";
|
|
|
4
4
|
* ServiceFeature: Registers event handlers for P2P replication and manages the lifecycle of a LiveSyncTrysteroReplicator instance.
|
|
5
5
|
* @param host
|
|
6
6
|
*/
|
|
7
|
-
export declare function useP2PReplicatorCommands(host: NecessaryServices<"API" | "setting", never>,
|
|
7
|
+
export declare function useP2PReplicatorCommands(host: NecessaryServices<"API" | "setting", never>, result: UseP2PReplicatorResult): void;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Logger, LOG_LEVEL_NOTICE } from "octagonal-wheels/common/logger";
|
|
2
|
-
function useP2PReplicatorCommands(host,
|
|
2
|
+
function useP2PReplicatorCommands(host, result) {
|
|
3
3
|
host.services.API.addCommand({
|
|
4
4
|
id: "p2p-establish-connection",
|
|
5
5
|
name: "P2P Sync : Connect to the Signalling Server",
|
|
6
6
|
checkCallback: (isChecking) => {
|
|
7
|
+
const replicator = result.replicator;
|
|
7
8
|
if (!replicator) return false;
|
|
8
9
|
if (isChecking) return !(replicator.server?.isServing ?? false);
|
|
9
10
|
void replicator.open();
|
|
@@ -13,6 +14,7 @@ function useP2PReplicatorCommands(host, { replicator }) {
|
|
|
13
14
|
id: "p2p-close-connection",
|
|
14
15
|
name: "P2P Sync : Disconnect from the Signalling Server",
|
|
15
16
|
checkCallback: (isChecking) => {
|
|
17
|
+
const replicator = result.replicator;
|
|
16
18
|
if (!replicator) return false;
|
|
17
19
|
if (isChecking) return replicator.server?.isServing ?? false;
|
|
18
20
|
Logger("Closing P2P Connection", LOG_LEVEL_NOTICE);
|
|
@@ -19,7 +19,7 @@ function useP2PReplicatorFeature(host, openReplicationUIFactory, openRebuildUIFa
|
|
|
19
19
|
return replicator;
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
-
addP2PEventHandlers(activeReplicator.replicator, host.services.context.events);
|
|
22
|
+
addP2PEventHandlers(() => activeReplicator.replicator, host.services.context.events);
|
|
23
23
|
host.services.replicator.getNewReplicator.addHandler(
|
|
24
24
|
async (settingOverride = {}) => {
|
|
25
25
|
const settings = { ...host.services.setting.currentSettings(), ...settingOverride };
|
package/dist/rpc/RpcRoom.d.ts
CHANGED