@dxos/network-manager 0.1.41 → 0.1.45-next.45667d4
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/lib/browser/{chunk-JAQBUAX3.mjs → chunk-UTCOIPSK.mjs} +101 -136
- package/dist/lib/browser/chunk-UTCOIPSK.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +3 -2
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/index.cjs +100 -135
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +102 -136
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/types/src/network-manager.d.ts +4 -6
- package/dist/types/src/network-manager.d.ts.map +1 -1
- package/dist/types/src/signal/message-router.d.ts +1 -1
- package/dist/types/src/signal/message-router.d.ts.map +1 -1
- package/dist/types/src/swarm/connection.d.ts +0 -4
- package/dist/types/src/swarm/connection.d.ts.map +1 -1
- package/dist/types/src/swarm/peer.d.ts +0 -1
- package/dist/types/src/swarm/peer.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm-mapper.d.ts +1 -1
- package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
- package/dist/types/src/swarm/swarm.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts +1 -1
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/transport/transport.d.ts +1 -1
- package/dist/types/src/transport/transport.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc-transport-proxy.d.ts +1 -1
- package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
- package/dist/types/src/transport/webrtc-transport.d.ts +1 -2
- package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +1 -1
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/wire-protocol.d.ts +2 -2
- package/dist/types/src/wire-protocol.d.ts.map +1 -1
- package/package.json +17 -17
- package/src/connection-log.ts +4 -4
- package/src/network-manager.ts +6 -14
- package/src/signal/integration.test.ts +19 -19
- package/src/signal/message-router.test.ts +30 -30
- package/src/signal/message-router.ts +15 -15
- package/src/swarm/connection.ts +10 -17
- package/src/swarm/peer.ts +6 -9
- package/src/swarm/swarm-mapper.ts +3 -3
- package/src/swarm/swarm.test.ts +14 -14
- package/src/swarm/swarm.ts +14 -15
- package/src/testing/test-builder.ts +23 -12
- package/src/testing/test-wire-protocol.ts +2 -2
- package/src/tests/basic-test-suite.ts +8 -8
- package/src/tests/memory-transport.test.ts +4 -4
- package/src/tests/property-test-suite.ts +16 -16
- package/src/tests/utils.ts +2 -2
- package/src/tests/webrtc-transport.test.ts +16 -2
- package/src/topology/mmst-topology.ts +1 -1
- package/src/topology/star-topology.ts +1 -1
- package/src/transport/memory-transport.test.ts +4 -4
- package/src/transport/memory-transport.ts +3 -3
- package/src/transport/webrtc-simple-peer.test.ts +1 -1
- package/src/transport/webrtc-transport-proxy.test.ts +18 -18
- package/src/transport/webrtc-transport-proxy.ts +2 -2
- package/src/transport/webrtc-transport-service.ts +13 -13
- package/src/transport/webrtc-transport.test.ts +3 -3
- package/src/transport/webrtc-transport.ts +6 -13
- package/src/wire-protocol.ts +2 -2
- package/dist/lib/browser/chunk-JAQBUAX3.mjs.map +0 -7
|
@@ -11,7 +11,7 @@ import { Awaited } from '@dxos/async';
|
|
|
11
11
|
import { PublicKey } from '@dxos/keys';
|
|
12
12
|
import { Messenger, WebsocketSignalManager } from '@dxos/messaging';
|
|
13
13
|
import { Answer } from '@dxos/protocols/proto/dxos/mesh/swarm';
|
|
14
|
-
import {
|
|
14
|
+
import { runTestSignalServer } from '@dxos/signal';
|
|
15
15
|
import { afterAll, beforeAll, describe, test, afterTest } from '@dxos/test';
|
|
16
16
|
|
|
17
17
|
import { MessageRouter } from './message-router';
|
|
@@ -20,10 +20,10 @@ import { OfferMessage, SignalMessage } from './signal-messenger';
|
|
|
20
20
|
describe('MessageRouter', () => {
|
|
21
21
|
let topic: PublicKey;
|
|
22
22
|
|
|
23
|
-
let broker1: Awaited<ReturnType<typeof
|
|
23
|
+
let broker1: Awaited<ReturnType<typeof runTestSignalServer>>;
|
|
24
24
|
|
|
25
25
|
beforeAll(async () => {
|
|
26
|
-
broker1 = await
|
|
26
|
+
broker1 = await runTestSignalServer();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
beforeEach(() => {
|
|
@@ -38,7 +38,7 @@ describe('MessageRouter', () => {
|
|
|
38
38
|
signalApiUrl,
|
|
39
39
|
onSignal = (async () => {}) as any,
|
|
40
40
|
onOffer = async () => ({ accept: true }),
|
|
41
|
-
topic
|
|
41
|
+
topic,
|
|
42
42
|
}: {
|
|
43
43
|
signalApiUrl: string;
|
|
44
44
|
onSignal?: (msg: SignalMessage) => Promise<void>;
|
|
@@ -54,7 +54,7 @@ describe('MessageRouter', () => {
|
|
|
54
54
|
await messenger.listen({
|
|
55
55
|
peerId,
|
|
56
56
|
payloadType: 'dxos.mesh.swarm.SwarmMessage',
|
|
57
|
-
onMessage: async (message) => await router.receiveMessage(message)
|
|
57
|
+
onMessage: async (message) => await router.receiveMessage(message),
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
const router: MessageRouter = new MessageRouter({
|
|
@@ -62,13 +62,13 @@ describe('MessageRouter', () => {
|
|
|
62
62
|
sendMessage: async (message) => await messenger.sendMessage(message),
|
|
63
63
|
onSignal,
|
|
64
64
|
onOffer,
|
|
65
|
-
topic
|
|
65
|
+
topic,
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
return {
|
|
69
69
|
peerId,
|
|
70
70
|
signalManager,
|
|
71
|
-
router
|
|
71
|
+
router,
|
|
72
72
|
};
|
|
73
73
|
};
|
|
74
74
|
|
|
@@ -80,15 +80,15 @@ describe('MessageRouter', () => {
|
|
|
80
80
|
const { signalManager: signalManager1, peerId: peer1 } = await createSignalClientAndMessageRouter({
|
|
81
81
|
signalApiUrl: broker1.url(),
|
|
82
82
|
onSignal: signalMock1,
|
|
83
|
-
topic
|
|
83
|
+
topic,
|
|
84
84
|
});
|
|
85
85
|
const {
|
|
86
86
|
signalManager: signalManager2,
|
|
87
87
|
router: router2,
|
|
88
|
-
peerId: peer2
|
|
88
|
+
peerId: peer2,
|
|
89
89
|
} = await createSignalClientAndMessageRouter({
|
|
90
90
|
signalApiUrl: broker1.url(),
|
|
91
|
-
topic
|
|
91
|
+
topic,
|
|
92
92
|
});
|
|
93
93
|
|
|
94
94
|
await signalManager1.join({ topic, peerId: peer1 });
|
|
@@ -99,7 +99,7 @@ describe('MessageRouter', () => {
|
|
|
99
99
|
recipient: peer1,
|
|
100
100
|
sessionId: PublicKey.random(),
|
|
101
101
|
topic,
|
|
102
|
-
data: { signal: { payload: { msg: 'Some info' } } }
|
|
102
|
+
data: { signal: { payload: { msg: 'Some info' } } },
|
|
103
103
|
};
|
|
104
104
|
await router2.signal(msg);
|
|
105
105
|
|
|
@@ -112,18 +112,18 @@ describe('MessageRouter', () => {
|
|
|
112
112
|
const {
|
|
113
113
|
signalManager: signalManager1,
|
|
114
114
|
router: router1,
|
|
115
|
-
peerId: peer1
|
|
115
|
+
peerId: peer1,
|
|
116
116
|
} = await createSignalClientAndMessageRouter({
|
|
117
117
|
signalApiUrl: broker1.url(),
|
|
118
118
|
onSignal: (async () => {}) as any,
|
|
119
119
|
onOffer: async () => ({ accept: true }),
|
|
120
|
-
topic
|
|
120
|
+
topic,
|
|
121
121
|
});
|
|
122
122
|
const { signalManager: signalManager2, peerId: peer2 } = await createSignalClientAndMessageRouter({
|
|
123
123
|
signalApiUrl: broker1.url(),
|
|
124
124
|
onSignal: (async () => {}) as any,
|
|
125
125
|
onOffer: async () => ({ accept: true }),
|
|
126
|
-
topic
|
|
126
|
+
topic,
|
|
127
127
|
});
|
|
128
128
|
|
|
129
129
|
await signalManager1.join({ topic, peerId: peer1 });
|
|
@@ -133,7 +133,7 @@ describe('MessageRouter', () => {
|
|
|
133
133
|
recipient: peer2,
|
|
134
134
|
sessionId: PublicKey.random(),
|
|
135
135
|
topic,
|
|
136
|
-
data: { offer: {} }
|
|
136
|
+
data: { offer: {} },
|
|
137
137
|
});
|
|
138
138
|
expect(answer.accept).toEqual(true);
|
|
139
139
|
}).timeout(5_000);
|
|
@@ -146,12 +146,12 @@ describe('MessageRouter', () => {
|
|
|
146
146
|
const {
|
|
147
147
|
signalManager: signalManager1,
|
|
148
148
|
router: router1,
|
|
149
|
-
peerId: peer1
|
|
149
|
+
peerId: peer1,
|
|
150
150
|
} = await createSignalClientAndMessageRouter({
|
|
151
151
|
signalApiUrl: broker1.url(),
|
|
152
152
|
onSignal: signalMock1,
|
|
153
153
|
onOffer: async () => ({ accept: true }),
|
|
154
|
-
topic
|
|
154
|
+
topic,
|
|
155
155
|
});
|
|
156
156
|
const received2: SignalMessage[] = [];
|
|
157
157
|
const signalMock2 = async (msg: SignalMessage) => {
|
|
@@ -160,12 +160,12 @@ describe('MessageRouter', () => {
|
|
|
160
160
|
const {
|
|
161
161
|
signalManager: signalManager2,
|
|
162
162
|
router: router2,
|
|
163
|
-
peerId: peer2
|
|
163
|
+
peerId: peer2,
|
|
164
164
|
} = await createSignalClientAndMessageRouter({
|
|
165
165
|
signalApiUrl: broker1.url(),
|
|
166
166
|
onSignal: signalMock2,
|
|
167
167
|
onOffer: async () => ({ accept: true }),
|
|
168
|
-
topic
|
|
168
|
+
topic,
|
|
169
169
|
});
|
|
170
170
|
const received3: SignalMessage[] = [];
|
|
171
171
|
const signalMock3 = async (msg: SignalMessage) => {
|
|
@@ -174,12 +174,12 @@ describe('MessageRouter', () => {
|
|
|
174
174
|
const {
|
|
175
175
|
signalManager: signalManager3,
|
|
176
176
|
router: router3,
|
|
177
|
-
peerId: peer3
|
|
177
|
+
peerId: peer3,
|
|
178
178
|
} = await createSignalClientAndMessageRouter({
|
|
179
179
|
signalApiUrl: broker1.url(),
|
|
180
180
|
onSignal: signalMock3,
|
|
181
181
|
onOffer: async () => ({ accept: true }),
|
|
182
|
-
topic
|
|
182
|
+
topic,
|
|
183
183
|
});
|
|
184
184
|
|
|
185
185
|
await signalManager1.join({ topic, peerId: peer1 });
|
|
@@ -192,7 +192,7 @@ describe('MessageRouter', () => {
|
|
|
192
192
|
recipient: peer3,
|
|
193
193
|
sessionId: PublicKey.random(),
|
|
194
194
|
topic,
|
|
195
|
-
data: { signal: { payload: { msg: '1to3' } } }
|
|
195
|
+
data: { signal: { payload: { msg: '1to3' } } },
|
|
196
196
|
};
|
|
197
197
|
await router1.signal(msg1to3);
|
|
198
198
|
await waitForExpect(() => {
|
|
@@ -205,7 +205,7 @@ describe('MessageRouter', () => {
|
|
|
205
205
|
recipient: peer3,
|
|
206
206
|
sessionId: PublicKey.random(),
|
|
207
207
|
topic,
|
|
208
|
-
data: { signal: { payload: { msg: '2to3' } } }
|
|
208
|
+
data: { signal: { payload: { msg: '2to3' } } },
|
|
209
209
|
};
|
|
210
210
|
await router2.signal(msg2to3);
|
|
211
211
|
await waitForExpect(() => {
|
|
@@ -218,7 +218,7 @@ describe('MessageRouter', () => {
|
|
|
218
218
|
recipient: peer1,
|
|
219
219
|
sessionId: PublicKey.random(),
|
|
220
220
|
topic,
|
|
221
|
-
data: { signal: { payload: { msg: '3to1' } } }
|
|
221
|
+
data: { signal: { payload: { msg: '3to1' } } },
|
|
222
222
|
};
|
|
223
223
|
await router3.signal(msg3to1);
|
|
224
224
|
await waitForExpect(() => {
|
|
@@ -230,22 +230,22 @@ describe('MessageRouter', () => {
|
|
|
230
230
|
const {
|
|
231
231
|
signalManager: signalManager1,
|
|
232
232
|
router: router1,
|
|
233
|
-
peerId: peer1
|
|
233
|
+
peerId: peer1,
|
|
234
234
|
} = await createSignalClientAndMessageRouter({
|
|
235
235
|
signalApiUrl: broker1.url(),
|
|
236
236
|
onSignal: (async () => {}) as any,
|
|
237
237
|
onOffer: async () => ({ accept: true }),
|
|
238
|
-
topic
|
|
238
|
+
topic,
|
|
239
239
|
});
|
|
240
240
|
const {
|
|
241
241
|
signalManager: signalManager2,
|
|
242
242
|
router: router2,
|
|
243
|
-
peerId: peer2
|
|
243
|
+
peerId: peer2,
|
|
244
244
|
} = await createSignalClientAndMessageRouter({
|
|
245
245
|
signalApiUrl: broker1.url(),
|
|
246
246
|
onSignal: (async () => {}) as any,
|
|
247
247
|
onOffer: async () => ({ accept: true }),
|
|
248
|
-
topic
|
|
248
|
+
topic,
|
|
249
249
|
});
|
|
250
250
|
|
|
251
251
|
await signalManager1.join({ topic, peerId: peer1 });
|
|
@@ -257,7 +257,7 @@ describe('MessageRouter', () => {
|
|
|
257
257
|
recipient: peer2,
|
|
258
258
|
sessionId: PublicKey.random(),
|
|
259
259
|
topic,
|
|
260
|
-
data: { offer: {} }
|
|
260
|
+
data: { offer: {} },
|
|
261
261
|
});
|
|
262
262
|
expect(answer1.accept).toEqual(true);
|
|
263
263
|
|
|
@@ -267,7 +267,7 @@ describe('MessageRouter', () => {
|
|
|
267
267
|
recipient: peer1,
|
|
268
268
|
sessionId: PublicKey.random(),
|
|
269
269
|
topic,
|
|
270
|
-
data: { offer: {} }
|
|
270
|
+
data: { offer: {} },
|
|
271
271
|
});
|
|
272
272
|
expect(answer2.accept).toEqual(true);
|
|
273
273
|
}).timeout(5_000);
|
|
@@ -47,7 +47,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
47
47
|
async receiveMessage({
|
|
48
48
|
author,
|
|
49
49
|
recipient,
|
|
50
|
-
payload
|
|
50
|
+
payload,
|
|
51
51
|
}: {
|
|
52
52
|
author: PublicKey;
|
|
53
53
|
recipient: PublicKey;
|
|
@@ -80,21 +80,21 @@ export class MessageRouter implements SignalMessenger {
|
|
|
80
80
|
await this._sendReliableMessage({
|
|
81
81
|
author: message.author,
|
|
82
82
|
recipient: message.recipient,
|
|
83
|
-
message
|
|
83
|
+
message,
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
async offer(message: OfferMessage): Promise<Answer> {
|
|
88
88
|
const networkMessage: SwarmMessage = {
|
|
89
89
|
...message,
|
|
90
|
-
messageId: PublicKey.random()
|
|
90
|
+
messageId: PublicKey.random(),
|
|
91
91
|
};
|
|
92
92
|
return new Promise<Answer>((resolve, reject) => {
|
|
93
93
|
this._offerRecords.set(networkMessage.messageId!, { resolve, reject });
|
|
94
94
|
return this._sendReliableMessage({
|
|
95
95
|
author: message.author,
|
|
96
96
|
recipient: message.recipient,
|
|
97
|
-
message: networkMessage
|
|
97
|
+
message: networkMessage,
|
|
98
98
|
});
|
|
99
99
|
});
|
|
100
100
|
}
|
|
@@ -102,7 +102,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
102
102
|
private async _sendReliableMessage({
|
|
103
103
|
author,
|
|
104
104
|
recipient,
|
|
105
|
-
message
|
|
105
|
+
message,
|
|
106
106
|
}: {
|
|
107
107
|
author: PublicKey;
|
|
108
108
|
recipient: PublicKey;
|
|
@@ -111,7 +111,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
111
111
|
const networkMessage: SwarmMessage = {
|
|
112
112
|
...message,
|
|
113
113
|
// Setting unique message_id if it not specified yet.
|
|
114
|
-
messageId: message.messageId ?? PublicKey.random()
|
|
114
|
+
messageId: message.messageId ?? PublicKey.random(),
|
|
115
115
|
};
|
|
116
116
|
|
|
117
117
|
log('sending', { from: author, to: recipient, msg: networkMessage });
|
|
@@ -121,7 +121,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
121
121
|
private async _encodeAndSend({
|
|
122
122
|
author,
|
|
123
123
|
recipient,
|
|
124
|
-
message
|
|
124
|
+
message,
|
|
125
125
|
}: {
|
|
126
126
|
author: PublicKey;
|
|
127
127
|
recipient: PublicKey;
|
|
@@ -132,8 +132,8 @@ export class MessageRouter implements SignalMessenger {
|
|
|
132
132
|
recipient,
|
|
133
133
|
payload: {
|
|
134
134
|
type_url: 'dxos.mesh.swarm.SwarmMessage',
|
|
135
|
-
value: schema.getCodecForType('dxos.mesh.swarm.SwarmMessage').encode(message)
|
|
136
|
-
}
|
|
135
|
+
value: schema.getCodecForType('dxos.mesh.swarm.SwarmMessage').encode(message),
|
|
136
|
+
},
|
|
137
137
|
});
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -151,7 +151,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
151
151
|
private async _handleOffer({
|
|
152
152
|
author,
|
|
153
153
|
recipient,
|
|
154
|
-
message
|
|
154
|
+
message,
|
|
155
155
|
}: {
|
|
156
156
|
author: PublicKey;
|
|
157
157
|
recipient: PublicKey;
|
|
@@ -162,7 +162,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
162
162
|
author,
|
|
163
163
|
recipient,
|
|
164
164
|
...message,
|
|
165
|
-
data: { offer: message.data.offer }
|
|
165
|
+
data: { offer: message.data.offer },
|
|
166
166
|
};
|
|
167
167
|
const answer = await this._onOffer(offerMessage);
|
|
168
168
|
answer.offerMessageId = message.messageId;
|
|
@@ -172,15 +172,15 @@ export class MessageRouter implements SignalMessenger {
|
|
|
172
172
|
message: {
|
|
173
173
|
topic: message.topic,
|
|
174
174
|
sessionId: message.sessionId,
|
|
175
|
-
data: { answer }
|
|
176
|
-
}
|
|
175
|
+
data: { answer },
|
|
176
|
+
},
|
|
177
177
|
});
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
private async _handleSignal({
|
|
181
181
|
author,
|
|
182
182
|
recipient,
|
|
183
|
-
message
|
|
183
|
+
message,
|
|
184
184
|
}: {
|
|
185
185
|
author: PublicKey;
|
|
186
186
|
recipient: PublicKey;
|
|
@@ -192,7 +192,7 @@ export class MessageRouter implements SignalMessenger {
|
|
|
192
192
|
author,
|
|
193
193
|
recipient,
|
|
194
194
|
...message,
|
|
195
|
-
data: { signal: message.data.signal }
|
|
195
|
+
data: { signal: message.data.signal },
|
|
196
196
|
};
|
|
197
197
|
|
|
198
198
|
await this._onSignal(signalMessage);
|
package/src/swarm/connection.ts
CHANGED
|
@@ -43,7 +43,7 @@ export enum ConnectionState {
|
|
|
43
43
|
/**
|
|
44
44
|
* Connection closed.
|
|
45
45
|
*/
|
|
46
|
-
CLOSED = 'CLOSED'
|
|
46
|
+
CLOSED = 'CLOSED',
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -59,10 +59,6 @@ export class Connection {
|
|
|
59
59
|
readonly errors = new ErrorStream();
|
|
60
60
|
|
|
61
61
|
public _instanceId = PublicKey.random().toHex();
|
|
62
|
-
public _traceParent?: string;
|
|
63
|
-
public _performance = {
|
|
64
|
-
signalsSent: 0
|
|
65
|
-
};
|
|
66
62
|
|
|
67
63
|
constructor(
|
|
68
64
|
public readonly topic: PublicKey,
|
|
@@ -72,7 +68,7 @@ export class Connection {
|
|
|
72
68
|
public readonly initiator: boolean,
|
|
73
69
|
private readonly _signalMessaging: SignalMessenger,
|
|
74
70
|
private readonly _protocol: WireProtocol,
|
|
75
|
-
private readonly _transportFactory: TransportFactory
|
|
71
|
+
private readonly _transportFactory: TransportFactory,
|
|
76
72
|
) {}
|
|
77
73
|
|
|
78
74
|
get state() {
|
|
@@ -93,8 +89,9 @@ export class Connection {
|
|
|
93
89
|
// TODO(burdon): Make async?
|
|
94
90
|
openConnection() {
|
|
95
91
|
assert(this._state === ConnectionState.INITIAL, 'Invalid state.');
|
|
92
|
+
log.trace('dxos.mesh.connection.open-connection', trace.begin({ id: this._instanceId }));
|
|
93
|
+
|
|
96
94
|
this._changeState(ConnectionState.CONNECTING);
|
|
97
|
-
log.trace('dxos.mesh.connection', trace.begin({ id: this._instanceId, parentId: this._traceParent }));
|
|
98
95
|
|
|
99
96
|
// TODO(dmaretskyi): Initialize only after the transport has established connection.
|
|
100
97
|
this._protocol.initialize().catch((err) => {
|
|
@@ -111,16 +108,14 @@ export class Connection {
|
|
|
111
108
|
this._transport = this._transportFactory.createTransport({
|
|
112
109
|
initiator: this.initiator,
|
|
113
110
|
stream: this._protocol.stream,
|
|
114
|
-
sendSignal: async (signal) =>
|
|
115
|
-
this.
|
|
116
|
-
await this._signalMessaging.signal({
|
|
111
|
+
sendSignal: async (signal) =>
|
|
112
|
+
this._signalMessaging.signal({
|
|
117
113
|
author: this.ownId,
|
|
118
114
|
recipient: this.remoteId,
|
|
119
115
|
sessionId: this.sessionId,
|
|
120
116
|
topic: this.topic,
|
|
121
|
-
data: { signal }
|
|
122
|
-
})
|
|
123
|
-
}
|
|
117
|
+
data: { signal },
|
|
118
|
+
}),
|
|
124
119
|
});
|
|
125
120
|
|
|
126
121
|
this._transport.connected.once(() => {
|
|
@@ -144,6 +139,8 @@ export class Connection {
|
|
|
144
139
|
}
|
|
145
140
|
|
|
146
141
|
this._bufferedSignals = [];
|
|
142
|
+
|
|
143
|
+
log.trace('dxos.mesh.connection.open-connection', trace.end({ id: this._instanceId }));
|
|
147
144
|
}
|
|
148
145
|
|
|
149
146
|
@synchronized
|
|
@@ -171,10 +168,6 @@ export class Connection {
|
|
|
171
168
|
|
|
172
169
|
log('closed', { peerId: this.ownId });
|
|
173
170
|
this._changeState(ConnectionState.CLOSED);
|
|
174
|
-
log.trace(
|
|
175
|
-
'dxos.mesh.connection',
|
|
176
|
-
trace.end({ id: this._instanceId, status: 'ok', data: { performance: this._performance } })
|
|
177
|
-
);
|
|
178
171
|
}
|
|
179
172
|
|
|
180
173
|
async signal(msg: SignalMessage) {
|
package/src/swarm/peer.ts
CHANGED
|
@@ -81,8 +81,6 @@ export class Peer {
|
|
|
81
81
|
|
|
82
82
|
public initiating = false;
|
|
83
83
|
|
|
84
|
-
public _traceParent?: string;
|
|
85
|
-
|
|
86
84
|
constructor(
|
|
87
85
|
public readonly id: PublicKey,
|
|
88
86
|
public readonly topic: PublicKey,
|
|
@@ -90,7 +88,7 @@ export class Peer {
|
|
|
90
88
|
private readonly _signalMessaging: SignalMessenger,
|
|
91
89
|
private readonly _protocolProvider: WireProtocolProvider,
|
|
92
90
|
private readonly _transportFactory: TransportFactory,
|
|
93
|
-
private readonly _callbacks: PeerCallbacks
|
|
91
|
+
private readonly _callbacks: PeerCallbacks,
|
|
94
92
|
) {}
|
|
95
93
|
|
|
96
94
|
/**
|
|
@@ -107,7 +105,7 @@ export class Peer {
|
|
|
107
105
|
log("closing local connection and accepting remote peer's offer", {
|
|
108
106
|
id: this.id,
|
|
109
107
|
topic: this.topic,
|
|
110
|
-
peerId: this.localPeerId
|
|
108
|
+
peerId: this.localPeerId,
|
|
111
109
|
});
|
|
112
110
|
|
|
113
111
|
if (this.connection) {
|
|
@@ -156,7 +154,7 @@ export class Peer {
|
|
|
156
154
|
recipient: this.id,
|
|
157
155
|
sessionId,
|
|
158
156
|
topic: this.topic,
|
|
159
|
-
data: { offer: {} }
|
|
157
|
+
data: { offer: {} },
|
|
160
158
|
});
|
|
161
159
|
log('received', { answer, topic: this.topic, ownId: this.localPeerId, remoteId: this.id });
|
|
162
160
|
if (connection.state !== ConnectionState.INITIAL) {
|
|
@@ -190,7 +188,7 @@ export class Peer {
|
|
|
190
188
|
peerId: this.localPeerId,
|
|
191
189
|
remoteId: this.id,
|
|
192
190
|
initiator,
|
|
193
|
-
sessionId
|
|
191
|
+
sessionId,
|
|
194
192
|
});
|
|
195
193
|
assert(!this.connection, 'Already connected.');
|
|
196
194
|
|
|
@@ -203,9 +201,8 @@ export class Peer {
|
|
|
203
201
|
this._signalMessaging,
|
|
204
202
|
// TODO(dmaretskyi): Init only when connection is established.
|
|
205
203
|
this._protocolProvider({ initiator, localPeerId: this.localPeerId, remotePeerId: this.id, topic: this.topic }),
|
|
206
|
-
this._transportFactory
|
|
204
|
+
this._transportFactory,
|
|
207
205
|
);
|
|
208
|
-
connection._traceParent = this._traceParent;
|
|
209
206
|
this._callbacks.onInitiated(connection);
|
|
210
207
|
|
|
211
208
|
void this._connectionCtx?.dispose();
|
|
@@ -241,7 +238,7 @@ export class Peer {
|
|
|
241
238
|
this.availableToConnect = true;
|
|
242
239
|
this._callbacks.onPeerAvailable();
|
|
243
240
|
},
|
|
244
|
-
this._availableAfter
|
|
241
|
+
this._availableAfter,
|
|
245
242
|
);
|
|
246
243
|
|
|
247
244
|
break;
|
|
@@ -78,14 +78,14 @@ export class SwarmMapper {
|
|
|
78
78
|
this._peers.set(this._swarm.ownPeerId, {
|
|
79
79
|
id: this._swarm.ownPeerId,
|
|
80
80
|
state: 'ME', // TODO(burdon): Enum (rename "local").
|
|
81
|
-
connections: []
|
|
81
|
+
connections: [],
|
|
82
82
|
});
|
|
83
83
|
|
|
84
84
|
for (const connection of this._swarm.connections) {
|
|
85
85
|
this._peers.set(connection.remoteId, {
|
|
86
86
|
id: connection.remoteId,
|
|
87
87
|
state: connection.state,
|
|
88
|
-
connections: [this._swarm.ownPeerId]
|
|
88
|
+
connections: [this._swarm.ownPeerId],
|
|
89
89
|
});
|
|
90
90
|
}
|
|
91
91
|
|
|
@@ -115,7 +115,7 @@ export class SwarmMapper {
|
|
|
115
115
|
|
|
116
116
|
log('graph changed', {
|
|
117
117
|
directConnections: this._swarm.connections.length,
|
|
118
|
-
totalPeersInSwarm: this._peers.size
|
|
118
|
+
totalPeersInSwarm: this._peers.size,
|
|
119
119
|
});
|
|
120
120
|
|
|
121
121
|
this.mapUpdated.emit(Array.from(this._peers.values()));
|
package/src/swarm/swarm.test.ts
CHANGED
|
@@ -35,7 +35,7 @@ describe('Swarm', () => {
|
|
|
35
35
|
protocol.factory,
|
|
36
36
|
new Messenger({ signalManager }),
|
|
37
37
|
createWebRTCTransportFactory(),
|
|
38
|
-
undefined
|
|
38
|
+
undefined,
|
|
39
39
|
);
|
|
40
40
|
|
|
41
41
|
afterTest(async () => {
|
|
@@ -59,22 +59,22 @@ describe('Swarm', () => {
|
|
|
59
59
|
|
|
60
60
|
const promise = Promise.all([
|
|
61
61
|
asyncTimeout(swarm1.connected.waitForCount(1), 3000, new Error('Swarm1 connect timeout.')),
|
|
62
|
-
asyncTimeout(swarm2.connected.waitForCount(1), 3000, new Error('Swarm2 connect timeout.'))
|
|
62
|
+
asyncTimeout(swarm2.connected.waitForCount(1), 3000, new Error('Swarm2 connect timeout.')),
|
|
63
63
|
]);
|
|
64
64
|
|
|
65
65
|
// Behavior of the Signal Server.
|
|
66
66
|
swarm1.onSwarmEvent({
|
|
67
67
|
peerAvailable: {
|
|
68
68
|
peer: peerId1.asUint8Array(),
|
|
69
|
-
since: new Date()
|
|
70
|
-
}
|
|
69
|
+
since: new Date(),
|
|
70
|
+
},
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
swarm1.onSwarmEvent({
|
|
74
74
|
peerAvailable: {
|
|
75
75
|
peer: peerId2.asUint8Array(),
|
|
76
|
-
since: new Date()
|
|
77
|
-
}
|
|
76
|
+
since: new Date(),
|
|
77
|
+
},
|
|
78
78
|
});
|
|
79
79
|
|
|
80
80
|
log('Candidates changed');
|
|
@@ -100,15 +100,15 @@ describe('Swarm', () => {
|
|
|
100
100
|
swarm1.onSwarmEvent({
|
|
101
101
|
peerAvailable: {
|
|
102
102
|
peer: peerId2.asUint8Array(),
|
|
103
|
-
since: new Date()
|
|
104
|
-
}
|
|
103
|
+
since: new Date(),
|
|
104
|
+
},
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
swarm2.onSwarmEvent({
|
|
108
108
|
peerAvailable: {
|
|
109
109
|
peer: peerId1.asUint8Array(),
|
|
110
|
-
since: new Date()
|
|
111
|
-
}
|
|
110
|
+
since: new Date(),
|
|
111
|
+
},
|
|
112
112
|
});
|
|
113
113
|
|
|
114
114
|
await connectPromises;
|
|
@@ -131,15 +131,15 @@ describe('Swarm', () => {
|
|
|
131
131
|
swarm1.onSwarmEvent({
|
|
132
132
|
peerAvailable: {
|
|
133
133
|
peer: peerId2.asUint8Array(),
|
|
134
|
-
since: new Date()
|
|
135
|
-
}
|
|
134
|
+
since: new Date(),
|
|
135
|
+
},
|
|
136
136
|
});
|
|
137
137
|
await sleep(15);
|
|
138
138
|
swarm2.onSwarmEvent({
|
|
139
139
|
peerAvailable: {
|
|
140
140
|
peer: peerId1.asUint8Array(),
|
|
141
|
-
since: new Date()
|
|
142
|
-
}
|
|
141
|
+
since: new Date(),
|
|
142
|
+
},
|
|
143
143
|
});
|
|
144
144
|
|
|
145
145
|
await connectPromises;
|
package/src/swarm/swarm.ts
CHANGED
|
@@ -79,8 +79,12 @@ export class Swarm {
|
|
|
79
79
|
private readonly _protocolProvider: WireProtocolProvider,
|
|
80
80
|
private readonly _messenger: Messenger,
|
|
81
81
|
private readonly _transportFactory: TransportFactory,
|
|
82
|
-
private readonly _label: string | undefined
|
|
82
|
+
private readonly _label: string | undefined,
|
|
83
83
|
) {
|
|
84
|
+
log.trace(
|
|
85
|
+
'dxos.mesh.swarm.constructor',
|
|
86
|
+
trace.begin({ id: this._instanceId, data: { topic: this._topic.toHex(), peerId: this._ownPeerId.toHex() } }),
|
|
87
|
+
);
|
|
84
88
|
log('creating swarm', { peerId: _ownPeerId });
|
|
85
89
|
_topology.init(this._getSwarmController());
|
|
86
90
|
|
|
@@ -88,21 +92,18 @@ export class Swarm {
|
|
|
88
92
|
sendMessage: async (msg) => await this._messenger.sendMessage(msg),
|
|
89
93
|
onSignal: async (msg) => await this.onSignal(msg),
|
|
90
94
|
onOffer: async (msg) => await this.onOffer(msg),
|
|
91
|
-
topic: this._topic
|
|
95
|
+
topic: this._topic,
|
|
92
96
|
});
|
|
93
97
|
|
|
94
98
|
this._messenger
|
|
95
99
|
.listen({
|
|
96
100
|
peerId: this._ownPeerId,
|
|
97
101
|
payloadType: 'dxos.mesh.swarm.SwarmMessage',
|
|
98
|
-
onMessage: async (message) => await this._swarmMessenger.receiveMessage(message)
|
|
102
|
+
onMessage: async (message) => await this._swarmMessenger.receiveMessage(message),
|
|
99
103
|
})
|
|
100
104
|
.catch((error) => log.catch(error));
|
|
101
105
|
|
|
102
|
-
log.trace(
|
|
103
|
-
'dxos.mesh.swarm',
|
|
104
|
-
trace.begin({ id: this._instanceId, data: { topic: this._topic.toHex(), peerId: this._ownPeerId.toHex() } })
|
|
105
|
-
);
|
|
106
|
+
log.trace('dxos.mesh.swarm.constructor', trace.end({ id: this._instanceId }));
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
get connections() {
|
|
@@ -135,7 +136,6 @@ export class Swarm {
|
|
|
135
136
|
await this._topology.destroy();
|
|
136
137
|
await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
|
|
137
138
|
log('destroyed');
|
|
138
|
-
log.trace('dxos.mesh.swarm', trace.end({ id: this._instanceId }));
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
async setTopology(topology: Topology) {
|
|
@@ -145,7 +145,7 @@ export class Swarm {
|
|
|
145
145
|
}
|
|
146
146
|
log('setting topology', {
|
|
147
147
|
previous: getClassName(this._topology),
|
|
148
|
-
topology: getClassName(topology)
|
|
148
|
+
topology: getClassName(topology),
|
|
149
149
|
});
|
|
150
150
|
|
|
151
151
|
await this._topology.destroy();
|
|
@@ -218,7 +218,7 @@ export class Swarm {
|
|
|
218
218
|
}
|
|
219
219
|
assert(
|
|
220
220
|
message.recipient?.equals(this._ownPeerId),
|
|
221
|
-
`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}
|
|
221
|
+
`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`,
|
|
222
222
|
);
|
|
223
223
|
assert(message.topic?.equals(this._topic));
|
|
224
224
|
assert(message.author);
|
|
@@ -280,10 +280,9 @@ export class Swarm {
|
|
|
280
280
|
},
|
|
281
281
|
onPeerAvailable: () => {
|
|
282
282
|
this._topology.update();
|
|
283
|
-
}
|
|
284
|
-
}
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
285
|
);
|
|
286
|
-
peer._traceParent = this._instanceId;
|
|
287
286
|
this._peers.set(peerId, peer);
|
|
288
287
|
}
|
|
289
288
|
|
|
@@ -305,7 +304,7 @@ export class Swarm {
|
|
|
305
304
|
.map((peer) => peer.id),
|
|
306
305
|
candidates: Array.from(this._peers.values())
|
|
307
306
|
.filter((peer) => !peer.connection && peer.advertizing && peer.availableToConnect)
|
|
308
|
-
.map((peer) => peer.id)
|
|
307
|
+
.map((peer) => peer.id),
|
|
309
308
|
}),
|
|
310
309
|
connect: (peer) => {
|
|
311
310
|
if (this._ctx.disposed) {
|
|
@@ -331,7 +330,7 @@ export class Swarm {
|
|
|
331
330
|
await this._closeConnection(peer);
|
|
332
331
|
this._topology.update();
|
|
333
332
|
});
|
|
334
|
-
}
|
|
333
|
+
},
|
|
335
334
|
};
|
|
336
335
|
}
|
|
337
336
|
|