@dxos/network-manager 0.1.23 → 0.1.24
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-2TROGP2W.mjs +2155 -0
- package/dist/lib/browser/chunk-2TROGP2W.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +21 -2133
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +27 -2009
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/index.cjs +7 -7
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +7 -7
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/types/src/connection-log.d.ts +2 -2
- package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
- package/dist/types/src/tests/utils.d.ts.map +1 -1
- package/package.json +16 -16
- package/src/connection-log.ts +2 -2
- package/src/network-manager.ts +3 -3
- package/src/swarm/swarm.ts +2 -2
- package/src/tests/basic-test-suite.ts +64 -55
- package/src/tests/utils.ts +2 -1
- package/src/transport/webrtc-transport-proxy.test.ts +5 -6
|
@@ -0,0 +1,2155 @@
|
|
|
1
|
+
import "@dxos/node-std/globals"
|
|
2
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
+
}) : x)(function(x) {
|
|
5
|
+
if (typeof require !== "undefined")
|
|
6
|
+
return require.apply(this, arguments);
|
|
7
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// packages/core/mesh/network-manager/src/swarm/connection.ts
|
|
11
|
+
import assert from "@dxos/node-std/assert";
|
|
12
|
+
import { Event, synchronized } from "@dxos/async";
|
|
13
|
+
import { ErrorStream } from "@dxos/debug";
|
|
14
|
+
import { log } from "@dxos/log";
|
|
15
|
+
var __decorate = function(decorators, target, key, desc) {
|
|
16
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
17
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
18
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
19
|
+
else
|
|
20
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
21
|
+
if (d = decorators[i])
|
|
22
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
23
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
24
|
+
};
|
|
25
|
+
var ConnectionState;
|
|
26
|
+
(function(ConnectionState5) {
|
|
27
|
+
ConnectionState5[
|
|
28
|
+
/**
|
|
29
|
+
* Initial state. Connection is registered but no attempt to connect to the remote peer has been performed.
|
|
30
|
+
* Might mean that we are waiting for the answer signal from the remote peer.
|
|
31
|
+
*/
|
|
32
|
+
"INITIAL"
|
|
33
|
+
] = "INITIAL";
|
|
34
|
+
ConnectionState5[
|
|
35
|
+
/**
|
|
36
|
+
* Trying to establish connection.
|
|
37
|
+
*/
|
|
38
|
+
"CONNECTING"
|
|
39
|
+
] = "CONNECTING";
|
|
40
|
+
ConnectionState5[
|
|
41
|
+
/**
|
|
42
|
+
* Connection is established.
|
|
43
|
+
*/
|
|
44
|
+
"CONNECTED"
|
|
45
|
+
] = "CONNECTED";
|
|
46
|
+
ConnectionState5[
|
|
47
|
+
/**
|
|
48
|
+
* Connection is being closed.
|
|
49
|
+
*/
|
|
50
|
+
"CLOSING"
|
|
51
|
+
] = "CLOSING";
|
|
52
|
+
ConnectionState5[
|
|
53
|
+
/**
|
|
54
|
+
* Connection closed.
|
|
55
|
+
*/
|
|
56
|
+
"CLOSED"
|
|
57
|
+
] = "CLOSED";
|
|
58
|
+
})(ConnectionState || (ConnectionState = {}));
|
|
59
|
+
var Connection = class {
|
|
60
|
+
constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory) {
|
|
61
|
+
this.topic = topic;
|
|
62
|
+
this.ownId = ownId;
|
|
63
|
+
this.remoteId = remoteId;
|
|
64
|
+
this.sessionId = sessionId;
|
|
65
|
+
this.initiator = initiator;
|
|
66
|
+
this._signalMessaging = _signalMessaging;
|
|
67
|
+
this._protocol = _protocol;
|
|
68
|
+
this._transportFactory = _transportFactory;
|
|
69
|
+
this._state = ConnectionState.INITIAL;
|
|
70
|
+
this._bufferedSignals = [];
|
|
71
|
+
this.stateChanged = new Event();
|
|
72
|
+
this.errors = new ErrorStream();
|
|
73
|
+
}
|
|
74
|
+
get state() {
|
|
75
|
+
return this._state;
|
|
76
|
+
}
|
|
77
|
+
get transport() {
|
|
78
|
+
return this._transport;
|
|
79
|
+
}
|
|
80
|
+
get protocol() {
|
|
81
|
+
return this._protocol;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create an underlying transport and prepares it for the connection.
|
|
85
|
+
*/
|
|
86
|
+
// TODO(burdon): Make async?
|
|
87
|
+
openConnection() {
|
|
88
|
+
assert(this._state === ConnectionState.INITIAL, "Invalid state.");
|
|
89
|
+
this._changeState(ConnectionState.CONNECTING);
|
|
90
|
+
this._protocol.initialize().catch((err) => {
|
|
91
|
+
this.errors.raise(err);
|
|
92
|
+
});
|
|
93
|
+
assert(!this._transport);
|
|
94
|
+
this._transport = this._transportFactory.createTransport({
|
|
95
|
+
initiator: this.initiator,
|
|
96
|
+
stream: this._protocol.stream,
|
|
97
|
+
sendSignal: async (signal) => {
|
|
98
|
+
await this._signalMessaging.signal({
|
|
99
|
+
author: this.ownId,
|
|
100
|
+
recipient: this.remoteId,
|
|
101
|
+
sessionId: this.sessionId,
|
|
102
|
+
topic: this.topic,
|
|
103
|
+
data: {
|
|
104
|
+
signal
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
this._transport.connected.once(() => {
|
|
110
|
+
this._changeState(ConnectionState.CONNECTED);
|
|
111
|
+
});
|
|
112
|
+
this._transport.closed.once(() => {
|
|
113
|
+
this._transport = void 0;
|
|
114
|
+
this.close().catch((err) => this.errors.raise(err));
|
|
115
|
+
});
|
|
116
|
+
this._transport.errors.handle((err) => {
|
|
117
|
+
if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
|
|
118
|
+
this.errors.raise(err);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
for (const signal of this._bufferedSignals) {
|
|
122
|
+
void this._transport.signal(signal);
|
|
123
|
+
}
|
|
124
|
+
this._bufferedSignals = [];
|
|
125
|
+
}
|
|
126
|
+
async close() {
|
|
127
|
+
var _a;
|
|
128
|
+
if (this._state === ConnectionState.CLOSED) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this._changeState(ConnectionState.CLOSING);
|
|
132
|
+
log("closing...", {
|
|
133
|
+
peerId: this.ownId
|
|
134
|
+
}, {
|
|
135
|
+
file: "connection.ts",
|
|
136
|
+
line: 142,
|
|
137
|
+
scope: this,
|
|
138
|
+
callSite: (f, a) => f(...a)
|
|
139
|
+
});
|
|
140
|
+
try {
|
|
141
|
+
await this._protocol.destroy();
|
|
142
|
+
} catch (err) {
|
|
143
|
+
log.catch(err, {}, {
|
|
144
|
+
file: "connection.ts",
|
|
145
|
+
line: 148,
|
|
146
|
+
scope: this,
|
|
147
|
+
callSite: (f, a) => f(...a)
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
await ((_a = this._transport) == null ? void 0 : _a.destroy());
|
|
152
|
+
} catch (err1) {
|
|
153
|
+
log.catch(err1, {}, {
|
|
154
|
+
file: "connection.ts",
|
|
155
|
+
line: 155,
|
|
156
|
+
scope: this,
|
|
157
|
+
callSite: (f, a) => f(...a)
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
log("closed", {
|
|
161
|
+
peerId: this.ownId
|
|
162
|
+
}, {
|
|
163
|
+
file: "connection.ts",
|
|
164
|
+
line: 158,
|
|
165
|
+
scope: this,
|
|
166
|
+
callSite: (f, a) => f(...a)
|
|
167
|
+
});
|
|
168
|
+
this._changeState(ConnectionState.CLOSED);
|
|
169
|
+
}
|
|
170
|
+
async signal(msg) {
|
|
171
|
+
var _a, _b;
|
|
172
|
+
assert(msg.sessionId);
|
|
173
|
+
if (!msg.sessionId.equals(this.sessionId)) {
|
|
174
|
+
log("dropping signal for incorrect session id", {}, {
|
|
175
|
+
file: "connection.ts",
|
|
176
|
+
line: 165,
|
|
177
|
+
scope: this,
|
|
178
|
+
callSite: (f, a) => f(...a)
|
|
179
|
+
});
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
assert(msg.data.signal);
|
|
183
|
+
assert((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
|
|
184
|
+
assert((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
|
|
185
|
+
if (this._state === ConnectionState.INITIAL) {
|
|
186
|
+
log("buffered signal", {
|
|
187
|
+
peerId: this.ownId,
|
|
188
|
+
remoteId: this.remoteId,
|
|
189
|
+
msg: msg.data
|
|
190
|
+
}, {
|
|
191
|
+
file: "connection.ts",
|
|
192
|
+
line: 173,
|
|
193
|
+
scope: this,
|
|
194
|
+
callSite: (f, a) => f(...a)
|
|
195
|
+
});
|
|
196
|
+
this._bufferedSignals.push(msg.data.signal);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
assert(this._transport, "Connection not ready to accept signals.");
|
|
200
|
+
log("received signal", {
|
|
201
|
+
peerId: this.ownId,
|
|
202
|
+
remoteId: this.remoteId,
|
|
203
|
+
msg: msg.data
|
|
204
|
+
}, {
|
|
205
|
+
file: "connection.ts",
|
|
206
|
+
line: 179,
|
|
207
|
+
scope: this,
|
|
208
|
+
callSite: (f, a) => f(...a)
|
|
209
|
+
});
|
|
210
|
+
await this._transport.signal(msg.data.signal);
|
|
211
|
+
}
|
|
212
|
+
_changeState(state) {
|
|
213
|
+
assert(state !== this._state, "Already in this state.");
|
|
214
|
+
this._state = state;
|
|
215
|
+
this.stateChanged.emit(state);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
__decorate([
|
|
219
|
+
synchronized
|
|
220
|
+
], Connection.prototype, "close", null);
|
|
221
|
+
|
|
222
|
+
// packages/core/mesh/network-manager/src/signal/message-router.ts
|
|
223
|
+
import assert2 from "@dxos/node-std/assert";
|
|
224
|
+
import { PublicKey } from "@dxos/keys";
|
|
225
|
+
import { log as log2 } from "@dxos/log";
|
|
226
|
+
import { schema } from "@dxos/protocols";
|
|
227
|
+
import { ComplexMap } from "@dxos/util";
|
|
228
|
+
var MessageRouter = class {
|
|
229
|
+
constructor({ sendMessage, onSignal, onOffer, topic }) {
|
|
230
|
+
this._offerRecords = new ComplexMap((key) => key.toHex());
|
|
231
|
+
this._sendMessage = sendMessage;
|
|
232
|
+
this._onSignal = onSignal;
|
|
233
|
+
this._onOffer = onOffer;
|
|
234
|
+
this._topic = topic;
|
|
235
|
+
}
|
|
236
|
+
async receiveMessage({ author, recipient, payload }) {
|
|
237
|
+
var _a, _b, _c;
|
|
238
|
+
if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const message = schema.getCodecForType("dxos.mesh.swarm.SwarmMessage").decode(payload.value);
|
|
242
|
+
if (!this._topic.equals(message.topic)) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
log2("received", {
|
|
246
|
+
from: author,
|
|
247
|
+
to: recipient,
|
|
248
|
+
msg: message
|
|
249
|
+
}, {
|
|
250
|
+
file: "message-router.ts",
|
|
251
|
+
line: 67,
|
|
252
|
+
scope: this,
|
|
253
|
+
callSite: (f, a) => f(...a)
|
|
254
|
+
});
|
|
255
|
+
if ((_a = message.data) == null ? void 0 : _a.offer) {
|
|
256
|
+
await this._handleOffer({
|
|
257
|
+
author,
|
|
258
|
+
recipient,
|
|
259
|
+
message
|
|
260
|
+
});
|
|
261
|
+
} else if ((_b = message.data) == null ? void 0 : _b.answer) {
|
|
262
|
+
await this._resolveAnswers(message);
|
|
263
|
+
} else if ((_c = message.data) == null ? void 0 : _c.signal) {
|
|
264
|
+
await this._handleSignal({
|
|
265
|
+
author,
|
|
266
|
+
recipient,
|
|
267
|
+
message
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
async signal(message) {
|
|
272
|
+
var _a;
|
|
273
|
+
assert2((_a = message.data) == null ? void 0 : _a.signal);
|
|
274
|
+
await this._sendReliableMessage({
|
|
275
|
+
author: message.author,
|
|
276
|
+
recipient: message.recipient,
|
|
277
|
+
message
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
async offer(message) {
|
|
281
|
+
const networkMessage = {
|
|
282
|
+
...message,
|
|
283
|
+
messageId: PublicKey.random()
|
|
284
|
+
};
|
|
285
|
+
return new Promise((resolve, reject) => {
|
|
286
|
+
this._offerRecords.set(networkMessage.messageId, {
|
|
287
|
+
resolve,
|
|
288
|
+
reject
|
|
289
|
+
});
|
|
290
|
+
return this._sendReliableMessage({
|
|
291
|
+
author: message.author,
|
|
292
|
+
recipient: message.recipient,
|
|
293
|
+
message: networkMessage
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
async _sendReliableMessage({ author, recipient, message }) {
|
|
298
|
+
var _a;
|
|
299
|
+
const networkMessage = {
|
|
300
|
+
...message,
|
|
301
|
+
// Setting unique message_id if it not specified yet.
|
|
302
|
+
messageId: (_a = message.messageId) != null ? _a : PublicKey.random()
|
|
303
|
+
};
|
|
304
|
+
log2("sending", {
|
|
305
|
+
from: author,
|
|
306
|
+
to: recipient,
|
|
307
|
+
msg: networkMessage
|
|
308
|
+
}, {
|
|
309
|
+
file: "message-router.ts",
|
|
310
|
+
line: 117,
|
|
311
|
+
scope: this,
|
|
312
|
+
callSite: (f, a) => f(...a)
|
|
313
|
+
});
|
|
314
|
+
await this._encodeAndSend({
|
|
315
|
+
author,
|
|
316
|
+
recipient,
|
|
317
|
+
message: networkMessage
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
async _encodeAndSend({ author, recipient, message }) {
|
|
321
|
+
await this._sendMessage({
|
|
322
|
+
author,
|
|
323
|
+
recipient,
|
|
324
|
+
payload: {
|
|
325
|
+
type_url: "dxos.mesh.swarm.SwarmMessage",
|
|
326
|
+
value: schema.getCodecForType("dxos.mesh.swarm.SwarmMessage").encode(message)
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
async _resolveAnswers(message) {
|
|
331
|
+
var _a, _b, _c;
|
|
332
|
+
assert2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
|
|
333
|
+
const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
|
|
334
|
+
if (offerRecord) {
|
|
335
|
+
this._offerRecords.delete(message.data.answer.offerMessageId);
|
|
336
|
+
assert2((_c = message.data) == null ? void 0 : _c.answer, "No answer");
|
|
337
|
+
log2("resolving", {
|
|
338
|
+
answer: message.data.answer
|
|
339
|
+
}, {
|
|
340
|
+
file: "message-router.ts",
|
|
341
|
+
line: 146,
|
|
342
|
+
scope: this,
|
|
343
|
+
callSite: (f, a) => f(...a)
|
|
344
|
+
});
|
|
345
|
+
offerRecord.resolve(message.data.answer);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
async _handleOffer({ author, recipient, message }) {
|
|
349
|
+
assert2(message.data.offer, "No offer");
|
|
350
|
+
const offerMessage = {
|
|
351
|
+
author,
|
|
352
|
+
recipient,
|
|
353
|
+
...message,
|
|
354
|
+
data: {
|
|
355
|
+
offer: message.data.offer
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
const answer = await this._onOffer(offerMessage);
|
|
359
|
+
answer.offerMessageId = message.messageId;
|
|
360
|
+
await this._sendReliableMessage({
|
|
361
|
+
author: recipient,
|
|
362
|
+
recipient: author,
|
|
363
|
+
message: {
|
|
364
|
+
topic: message.topic,
|
|
365
|
+
sessionId: message.sessionId,
|
|
366
|
+
data: {
|
|
367
|
+
answer
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
async _handleSignal({ author, recipient, message }) {
|
|
373
|
+
assert2(message.messageId);
|
|
374
|
+
assert2(message.data.signal, "No Signal");
|
|
375
|
+
const signalMessage = {
|
|
376
|
+
author,
|
|
377
|
+
recipient,
|
|
378
|
+
...message,
|
|
379
|
+
data: {
|
|
380
|
+
signal: message.data.signal
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
await this._onSignal(signalMessage);
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
// packages/core/mesh/network-manager/src/swarm/swarm.ts
|
|
388
|
+
import assert4 from "@dxos/node-std/assert";
|
|
389
|
+
import { Event as Event2, scheduleTask, sleep, synchronized as synchronized3 } from "@dxos/async";
|
|
390
|
+
import { Context } from "@dxos/context";
|
|
391
|
+
import { ErrorStream as ErrorStream2 } from "@dxos/debug";
|
|
392
|
+
import { PublicKey as PublicKey3 } from "@dxos/keys";
|
|
393
|
+
import { log as log4, logInfo } from "@dxos/log";
|
|
394
|
+
import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
|
|
395
|
+
|
|
396
|
+
// packages/core/mesh/network-manager/src/swarm/peer.ts
|
|
397
|
+
import assert3 from "@dxos/node-std/assert";
|
|
398
|
+
import { synchronized as synchronized2 } from "@dxos/async";
|
|
399
|
+
import { PublicKey as PublicKey2 } from "@dxos/keys";
|
|
400
|
+
import { log as log3 } from "@dxos/log";
|
|
401
|
+
var __decorate2 = function(decorators, target, key, desc) {
|
|
402
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
403
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
404
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
405
|
+
else
|
|
406
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
407
|
+
if (d = decorators[i])
|
|
408
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
409
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
410
|
+
};
|
|
411
|
+
var Peer = class {
|
|
412
|
+
constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
|
|
413
|
+
this.id = id;
|
|
414
|
+
this.topic = topic;
|
|
415
|
+
this.localPeerId = localPeerId;
|
|
416
|
+
this._signalMessaging = _signalMessaging;
|
|
417
|
+
this._protocolProvider = _protocolProvider;
|
|
418
|
+
this._transportFactory = _transportFactory;
|
|
419
|
+
this._callbacks = _callbacks;
|
|
420
|
+
this.advertizing = false;
|
|
421
|
+
this.initiating = false;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Respond to remote offer.
|
|
425
|
+
*/
|
|
426
|
+
async onOffer(message) {
|
|
427
|
+
const remoteId = message.author;
|
|
428
|
+
if (this.connection || this.initiating) {
|
|
429
|
+
if (remoteId.toHex() < this.localPeerId.toHex()) {
|
|
430
|
+
log3("closing local connection and accepting remote peer's offer", {
|
|
431
|
+
id: this.id,
|
|
432
|
+
topic: this.topic,
|
|
433
|
+
peerId: this.localPeerId
|
|
434
|
+
}, {
|
|
435
|
+
file: "peer.ts",
|
|
436
|
+
line: 84,
|
|
437
|
+
scope: this,
|
|
438
|
+
callSite: (f, a) => f(...a)
|
|
439
|
+
});
|
|
440
|
+
if (this.connection) {
|
|
441
|
+
await this.closeConnection();
|
|
442
|
+
}
|
|
443
|
+
} else {
|
|
444
|
+
return {
|
|
445
|
+
accept: false
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if (await this._callbacks.onOffer(remoteId)) {
|
|
450
|
+
if (!this.connection) {
|
|
451
|
+
assert3(message.sessionId);
|
|
452
|
+
const connection = this._createConnection(false, message.sessionId);
|
|
453
|
+
try {
|
|
454
|
+
connection.openConnection();
|
|
455
|
+
} catch (err) {
|
|
456
|
+
log3.warn("connection error", {
|
|
457
|
+
topic: this.topic,
|
|
458
|
+
peerId: this.localPeerId,
|
|
459
|
+
remoteId: this.id,
|
|
460
|
+
err
|
|
461
|
+
}, {
|
|
462
|
+
file: "peer.ts",
|
|
463
|
+
line: 108,
|
|
464
|
+
scope: this,
|
|
465
|
+
callSite: (f, a) => f(...a)
|
|
466
|
+
});
|
|
467
|
+
await this.closeConnection();
|
|
468
|
+
}
|
|
469
|
+
return {
|
|
470
|
+
accept: true
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return {
|
|
475
|
+
accept: false
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Initiate a connection to the remote peer.
|
|
480
|
+
*/
|
|
481
|
+
async initiateConnection() {
|
|
482
|
+
assert3(!this.initiating, "Initiation in progress.");
|
|
483
|
+
assert3(!this.connection, "Already connected.");
|
|
484
|
+
const sessionId = PublicKey2.random();
|
|
485
|
+
log3("initiating...", {
|
|
486
|
+
id: this.id,
|
|
487
|
+
topic: this.topic,
|
|
488
|
+
peerId: this.id,
|
|
489
|
+
sessionId
|
|
490
|
+
}, {
|
|
491
|
+
file: "peer.ts",
|
|
492
|
+
line: 126,
|
|
493
|
+
scope: this,
|
|
494
|
+
callSite: (f, a) => f(...a)
|
|
495
|
+
});
|
|
496
|
+
const connection = this._createConnection(true, sessionId);
|
|
497
|
+
this.initiating = true;
|
|
498
|
+
try {
|
|
499
|
+
const answer = await this._signalMessaging.offer({
|
|
500
|
+
author: this.localPeerId,
|
|
501
|
+
recipient: this.id,
|
|
502
|
+
sessionId,
|
|
503
|
+
topic: this.topic,
|
|
504
|
+
data: {
|
|
505
|
+
offer: {}
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
log3("received", {
|
|
509
|
+
answer,
|
|
510
|
+
topic: this.topic,
|
|
511
|
+
ownId: this.localPeerId,
|
|
512
|
+
remoteId: this.id
|
|
513
|
+
}, {
|
|
514
|
+
file: "peer.ts",
|
|
515
|
+
line: 138,
|
|
516
|
+
scope: this,
|
|
517
|
+
callSite: (f, a) => f(...a)
|
|
518
|
+
});
|
|
519
|
+
if (connection.state !== ConnectionState.INITIAL) {
|
|
520
|
+
log3("ignoring response", {}, {
|
|
521
|
+
file: "peer.ts",
|
|
522
|
+
line: 140,
|
|
523
|
+
scope: this,
|
|
524
|
+
callSite: (f, a) => f(...a)
|
|
525
|
+
});
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (!answer.accept) {
|
|
529
|
+
this._callbacks.onRejected();
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
connection.openConnection();
|
|
533
|
+
this._callbacks.onAccepted();
|
|
534
|
+
} catch (err) {
|
|
535
|
+
log3.warn("initiation error", {
|
|
536
|
+
topic: this.topic,
|
|
537
|
+
peerId: this.localPeerId,
|
|
538
|
+
remoteId: this.id,
|
|
539
|
+
err
|
|
540
|
+
}, {
|
|
541
|
+
file: "peer.ts",
|
|
542
|
+
line: 151,
|
|
543
|
+
scope: this,
|
|
544
|
+
callSite: (f, a) => f(...a)
|
|
545
|
+
});
|
|
546
|
+
await this.closeConnection();
|
|
547
|
+
throw err;
|
|
548
|
+
} finally {
|
|
549
|
+
this.initiating = false;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Create new connection.
|
|
554
|
+
* Either we're initiating a connection or creating one in response to an offer from the other peer.
|
|
555
|
+
*/
|
|
556
|
+
_createConnection(initiator, sessionId) {
|
|
557
|
+
log3("creating connection", {
|
|
558
|
+
topic: this.topic,
|
|
559
|
+
peerId: this.localPeerId,
|
|
560
|
+
remoteId: this.id,
|
|
561
|
+
initiator,
|
|
562
|
+
sessionId
|
|
563
|
+
}, {
|
|
564
|
+
file: "peer.ts",
|
|
565
|
+
line: 165,
|
|
566
|
+
scope: this,
|
|
567
|
+
callSite: (f, a) => f(...a)
|
|
568
|
+
});
|
|
569
|
+
assert3(!this.connection, "Already connected.");
|
|
570
|
+
const connection = new Connection(
|
|
571
|
+
this.topic,
|
|
572
|
+
this.localPeerId,
|
|
573
|
+
this.id,
|
|
574
|
+
sessionId,
|
|
575
|
+
initiator,
|
|
576
|
+
this._signalMessaging,
|
|
577
|
+
// TODO(dmaretskyi): Init only when connection is established.
|
|
578
|
+
this._protocolProvider({
|
|
579
|
+
initiator,
|
|
580
|
+
localPeerId: this.localPeerId,
|
|
581
|
+
remotePeerId: this.id,
|
|
582
|
+
topic: this.topic
|
|
583
|
+
}),
|
|
584
|
+
this._transportFactory
|
|
585
|
+
);
|
|
586
|
+
this._callbacks.onInitiated(connection);
|
|
587
|
+
connection.stateChanged.on((state) => {
|
|
588
|
+
switch (state) {
|
|
589
|
+
case ConnectionState.CONNECTED: {
|
|
590
|
+
this._callbacks.onConnected();
|
|
591
|
+
break;
|
|
592
|
+
}
|
|
593
|
+
case ConnectionState.CLOSED: {
|
|
594
|
+
log3("connection closed", {
|
|
595
|
+
topic: this.topic,
|
|
596
|
+
peerId: this.localPeerId,
|
|
597
|
+
remoteId: this.id,
|
|
598
|
+
initiator
|
|
599
|
+
}, {
|
|
600
|
+
file: "peer.ts",
|
|
601
|
+
line: 194,
|
|
602
|
+
scope: this,
|
|
603
|
+
callSite: (f, a) => f(...a)
|
|
604
|
+
});
|
|
605
|
+
assert3(this.connection === connection, "Connection mismatch (race condition).");
|
|
606
|
+
this.connection = void 0;
|
|
607
|
+
this._callbacks.onDisconnected();
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
connection.errors.handle((err) => {
|
|
613
|
+
log3.warn("connection error", {
|
|
614
|
+
topic: this.topic,
|
|
615
|
+
peerId: this.localPeerId,
|
|
616
|
+
remoteId: this.id,
|
|
617
|
+
initiator,
|
|
618
|
+
err
|
|
619
|
+
}, {
|
|
620
|
+
file: "peer.ts",
|
|
621
|
+
line: 204,
|
|
622
|
+
scope: this,
|
|
623
|
+
callSite: (f, a) => f(...a)
|
|
624
|
+
});
|
|
625
|
+
void this.closeConnection();
|
|
626
|
+
});
|
|
627
|
+
this.connection = connection;
|
|
628
|
+
return connection;
|
|
629
|
+
}
|
|
630
|
+
async closeConnection() {
|
|
631
|
+
if (!this.connection) {
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
const connection = this.connection;
|
|
635
|
+
log3("closing...", {
|
|
636
|
+
peerId: this.id,
|
|
637
|
+
sessionId: connection.sessionId
|
|
638
|
+
}, {
|
|
639
|
+
file: "peer.ts",
|
|
640
|
+
line: 220,
|
|
641
|
+
scope: this,
|
|
642
|
+
callSite: (f, a) => f(...a)
|
|
643
|
+
});
|
|
644
|
+
await connection.close();
|
|
645
|
+
log3("closed", {
|
|
646
|
+
peerId: this.id,
|
|
647
|
+
sessionId: connection.sessionId
|
|
648
|
+
}, {
|
|
649
|
+
file: "peer.ts",
|
|
650
|
+
line: 226,
|
|
651
|
+
scope: this,
|
|
652
|
+
callSite: (f, a) => f(...a)
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
async onSignal(message) {
|
|
656
|
+
if (!this.connection) {
|
|
657
|
+
log3("dropping signal message for non-existent connection", {
|
|
658
|
+
message
|
|
659
|
+
}, {
|
|
660
|
+
file: "peer.ts",
|
|
661
|
+
line: 231,
|
|
662
|
+
scope: this,
|
|
663
|
+
callSite: (f, a) => f(...a)
|
|
664
|
+
});
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
await this.connection.signal(message);
|
|
668
|
+
}
|
|
669
|
+
async destroy() {
|
|
670
|
+
var _a;
|
|
671
|
+
log3("Destroying peer", {
|
|
672
|
+
peerId: this.id,
|
|
673
|
+
topic: this.topic
|
|
674
|
+
}, {
|
|
675
|
+
file: "peer.ts",
|
|
676
|
+
line: 239,
|
|
677
|
+
scope: this,
|
|
678
|
+
callSite: (f, a) => f(...a)
|
|
679
|
+
});
|
|
680
|
+
await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
__decorate2([
|
|
684
|
+
synchronized2
|
|
685
|
+
], Peer.prototype, "destroy", null);
|
|
686
|
+
|
|
687
|
+
// packages/core/mesh/network-manager/src/swarm/swarm.ts
|
|
688
|
+
var __decorate3 = function(decorators, target, key, desc) {
|
|
689
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
690
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
691
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
692
|
+
else
|
|
693
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
694
|
+
if (d = decorators[i])
|
|
695
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
696
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
697
|
+
};
|
|
698
|
+
var INITIATION_DELAY = 100;
|
|
699
|
+
var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
|
|
700
|
+
var Swarm = class {
|
|
701
|
+
// TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
|
|
702
|
+
// TODO(burdon): Split up properties.
|
|
703
|
+
constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
|
|
704
|
+
this._topic = _topic;
|
|
705
|
+
this._ownPeerId = _ownPeerId;
|
|
706
|
+
this._topology = _topology;
|
|
707
|
+
this._protocolProvider = _protocolProvider;
|
|
708
|
+
this._messenger = _messenger;
|
|
709
|
+
this._transportFactory = _transportFactory;
|
|
710
|
+
this._label = _label;
|
|
711
|
+
this._peers = new ComplexMap2(PublicKey3.hash);
|
|
712
|
+
this._ctx = new Context();
|
|
713
|
+
this.connectionAdded = new Event2();
|
|
714
|
+
this.disconnected = new Event2();
|
|
715
|
+
this.connected = new Event2();
|
|
716
|
+
this.errors = new ErrorStream2();
|
|
717
|
+
this.instanceId = PublicKey3.random();
|
|
718
|
+
log4("creating swarm", {
|
|
719
|
+
peerId: _ownPeerId
|
|
720
|
+
}, {
|
|
721
|
+
file: "swarm.ts",
|
|
722
|
+
line: 83,
|
|
723
|
+
scope: this,
|
|
724
|
+
callSite: (f, a) => f(...a)
|
|
725
|
+
});
|
|
726
|
+
_topology.init(this._getSwarmController());
|
|
727
|
+
this._swarmMessenger = new MessageRouter({
|
|
728
|
+
sendMessage: async (msg) => await this._messenger.sendMessage(msg),
|
|
729
|
+
onSignal: async (msg) => await this.onSignal(msg),
|
|
730
|
+
onOffer: async (msg) => await this.onOffer(msg),
|
|
731
|
+
topic: this._topic
|
|
732
|
+
});
|
|
733
|
+
this._messenger.listen({
|
|
734
|
+
peerId: this._ownPeerId,
|
|
735
|
+
payloadType: "dxos.mesh.swarm.SwarmMessage",
|
|
736
|
+
onMessage: async (message) => await this._swarmMessenger.receiveMessage(message)
|
|
737
|
+
}).catch((error) => log4.catch(error, {}, {
|
|
738
|
+
file: "swarm.ts",
|
|
739
|
+
line: 99,
|
|
740
|
+
scope: this,
|
|
741
|
+
callSite: (f, a) => f(...a)
|
|
742
|
+
}));
|
|
743
|
+
}
|
|
744
|
+
get connections() {
|
|
745
|
+
return Array.from(this._peers.values()).map((peer) => peer.connection).filter(isNotNullOrUndefined);
|
|
746
|
+
}
|
|
747
|
+
get ownPeerId() {
|
|
748
|
+
return this._ownPeerId;
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Custom label assigned to this swarm. Used in devtools to display human-readable names for swarms.
|
|
752
|
+
*/
|
|
753
|
+
get label() {
|
|
754
|
+
return this._label;
|
|
755
|
+
}
|
|
756
|
+
get topic() {
|
|
757
|
+
return this._topic;
|
|
758
|
+
}
|
|
759
|
+
// TODO(burdon): async open?
|
|
760
|
+
async destroy() {
|
|
761
|
+
log4("destroying...", {}, {
|
|
762
|
+
file: "swarm.ts",
|
|
763
|
+
line: 127,
|
|
764
|
+
scope: this,
|
|
765
|
+
callSite: (f, a) => f(...a)
|
|
766
|
+
});
|
|
767
|
+
await this._ctx.dispose();
|
|
768
|
+
await this._topology.destroy();
|
|
769
|
+
await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
|
|
770
|
+
log4("destroyed", {}, {
|
|
771
|
+
file: "swarm.ts",
|
|
772
|
+
line: 131,
|
|
773
|
+
scope: this,
|
|
774
|
+
callSite: (f, a) => f(...a)
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
async setTopology(topology) {
|
|
778
|
+
assert4(!this._ctx.disposed, "Swarm is offline");
|
|
779
|
+
if (topology === this._topology) {
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
log4("setting topology", {
|
|
783
|
+
previous: getClassName(this._topology),
|
|
784
|
+
topology: getClassName(topology)
|
|
785
|
+
}, {
|
|
786
|
+
file: "swarm.ts",
|
|
787
|
+
line: 139,
|
|
788
|
+
scope: this,
|
|
789
|
+
callSite: (f, a) => f(...a)
|
|
790
|
+
});
|
|
791
|
+
await this._topology.destroy();
|
|
792
|
+
this._topology = topology;
|
|
793
|
+
this._topology.init(this._getSwarmController());
|
|
794
|
+
this._topology.update();
|
|
795
|
+
}
|
|
796
|
+
onSwarmEvent(swarmEvent) {
|
|
797
|
+
var _a;
|
|
798
|
+
log4("swarm event", {
|
|
799
|
+
swarmEvent
|
|
800
|
+
}, {
|
|
801
|
+
file: "swarm.ts",
|
|
802
|
+
line: 152,
|
|
803
|
+
scope: this,
|
|
804
|
+
callSite: (f, a) => f(...a)
|
|
805
|
+
});
|
|
806
|
+
if (this._ctx.disposed) {
|
|
807
|
+
log4("swarm event ignored for disposed swarm", {}, {
|
|
808
|
+
file: "swarm.ts",
|
|
809
|
+
line: 155,
|
|
810
|
+
scope: this,
|
|
811
|
+
callSite: (f, a) => f(...a)
|
|
812
|
+
});
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
if (swarmEvent.peerAvailable) {
|
|
816
|
+
const peerId = PublicKey3.from(swarmEvent.peerAvailable.peer);
|
|
817
|
+
log4("new peer", {
|
|
818
|
+
peerId
|
|
819
|
+
}, {
|
|
820
|
+
file: "swarm.ts",
|
|
821
|
+
line: 161,
|
|
822
|
+
scope: this,
|
|
823
|
+
callSite: (f, a) => f(...a)
|
|
824
|
+
});
|
|
825
|
+
if (!peerId.equals(this._ownPeerId)) {
|
|
826
|
+
const peer = this._getOrCreatePeer(peerId);
|
|
827
|
+
peer.advertizing = true;
|
|
828
|
+
}
|
|
829
|
+
} else if (swarmEvent.peerLeft) {
|
|
830
|
+
const peer1 = this._peers.get(PublicKey3.from(swarmEvent.peerLeft.peer));
|
|
831
|
+
if (peer1) {
|
|
832
|
+
peer1.advertizing = false;
|
|
833
|
+
if (((_a = peer1.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
|
|
834
|
+
void this._destroyPeer(peer1.id).catch((err) => log4.catch(err, {}, {
|
|
835
|
+
file: "swarm.ts",
|
|
836
|
+
line: 172,
|
|
837
|
+
scope: this,
|
|
838
|
+
callSite: (f, a) => f(...a)
|
|
839
|
+
}));
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
this._topology.update();
|
|
844
|
+
}
|
|
845
|
+
async onOffer(message) {
|
|
846
|
+
var _a, _b;
|
|
847
|
+
log4("offer", {
|
|
848
|
+
message
|
|
849
|
+
}, {
|
|
850
|
+
file: "swarm.ts",
|
|
851
|
+
line: 182,
|
|
852
|
+
scope: this,
|
|
853
|
+
callSite: (f, a) => f(...a)
|
|
854
|
+
});
|
|
855
|
+
if (this._ctx.disposed) {
|
|
856
|
+
log4("ignored for disposed swarm", {}, {
|
|
857
|
+
file: "swarm.ts",
|
|
858
|
+
line: 184,
|
|
859
|
+
scope: this,
|
|
860
|
+
callSite: (f, a) => f(...a)
|
|
861
|
+
});
|
|
862
|
+
return {
|
|
863
|
+
accept: false
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
assert4(message.author);
|
|
867
|
+
if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
|
|
868
|
+
log4("rejecting offer with incorrect peerId", {
|
|
869
|
+
message
|
|
870
|
+
}, {
|
|
871
|
+
file: "swarm.ts",
|
|
872
|
+
line: 191,
|
|
873
|
+
scope: this,
|
|
874
|
+
callSite: (f, a) => f(...a)
|
|
875
|
+
});
|
|
876
|
+
return {
|
|
877
|
+
accept: false
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
|
|
881
|
+
log4("rejecting offer with incorrect topic", {
|
|
882
|
+
message
|
|
883
|
+
}, {
|
|
884
|
+
file: "swarm.ts",
|
|
885
|
+
line: 195,
|
|
886
|
+
scope: this,
|
|
887
|
+
callSite: (f, a) => f(...a)
|
|
888
|
+
});
|
|
889
|
+
return {
|
|
890
|
+
accept: false
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
const peer = this._getOrCreatePeer(message.author);
|
|
894
|
+
const answer = await peer.onOffer(message);
|
|
895
|
+
this._topology.update();
|
|
896
|
+
return answer;
|
|
897
|
+
}
|
|
898
|
+
async onSignal(message) {
|
|
899
|
+
var _a, _b;
|
|
900
|
+
log4("signal", {
|
|
901
|
+
message
|
|
902
|
+
}, {
|
|
903
|
+
file: "swarm.ts",
|
|
904
|
+
line: 207,
|
|
905
|
+
scope: this,
|
|
906
|
+
callSite: (f, a) => f(...a)
|
|
907
|
+
});
|
|
908
|
+
if (this._ctx.disposed) {
|
|
909
|
+
log4.info("ignored for offline swarm", {}, {
|
|
910
|
+
file: "swarm.ts",
|
|
911
|
+
line: 209,
|
|
912
|
+
scope: this,
|
|
913
|
+
callSite: (f, a) => f(...a)
|
|
914
|
+
});
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
assert4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
|
|
918
|
+
assert4((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
|
|
919
|
+
assert4(message.author);
|
|
920
|
+
const peer = this._getOrCreatePeer(message.author);
|
|
921
|
+
await peer.onSignal(message);
|
|
922
|
+
}
|
|
923
|
+
// For debug purposes
|
|
924
|
+
async goOffline() {
|
|
925
|
+
await this._ctx.dispose();
|
|
926
|
+
await Promise.all([
|
|
927
|
+
...this._peers.keys()
|
|
928
|
+
].map((peerId) => this._destroyPeer(peerId)));
|
|
929
|
+
}
|
|
930
|
+
// For debug purposes
|
|
931
|
+
async goOnline() {
|
|
932
|
+
this._ctx = new Context();
|
|
933
|
+
}
|
|
934
|
+
_getOrCreatePeer(peerId) {
|
|
935
|
+
let peer = this._peers.get(peerId);
|
|
936
|
+
if (!peer) {
|
|
937
|
+
peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, {
|
|
938
|
+
onInitiated: (connection) => {
|
|
939
|
+
this.connectionAdded.emit(connection);
|
|
940
|
+
},
|
|
941
|
+
onConnected: () => {
|
|
942
|
+
this.connected.emit(peerId);
|
|
943
|
+
},
|
|
944
|
+
onDisconnected: async () => {
|
|
945
|
+
if (!peer.advertizing) {
|
|
946
|
+
await this._destroyPeer(peer.id);
|
|
947
|
+
}
|
|
948
|
+
this.disconnected.emit(peerId);
|
|
949
|
+
this._topology.update();
|
|
950
|
+
},
|
|
951
|
+
onRejected: () => {
|
|
952
|
+
if (this._peers.has(peerId)) {
|
|
953
|
+
void this._destroyPeer(peerId);
|
|
954
|
+
}
|
|
955
|
+
},
|
|
956
|
+
onAccepted: () => {
|
|
957
|
+
this._topology.update();
|
|
958
|
+
},
|
|
959
|
+
onOffer: (remoteId) => {
|
|
960
|
+
return this._topology.onOffer(remoteId);
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
this._peers.set(peerId, peer);
|
|
964
|
+
}
|
|
965
|
+
return peer;
|
|
966
|
+
}
|
|
967
|
+
async _destroyPeer(peerId) {
|
|
968
|
+
assert4(this._peers.has(peerId));
|
|
969
|
+
await this._peers.get(peerId).destroy();
|
|
970
|
+
this._peers.delete(peerId);
|
|
971
|
+
}
|
|
972
|
+
_getSwarmController() {
|
|
973
|
+
return {
|
|
974
|
+
getState: () => ({
|
|
975
|
+
ownPeerId: this._ownPeerId,
|
|
976
|
+
connected: Array.from(this._peers.values()).filter((peer) => peer.connection).map((peer) => peer.id),
|
|
977
|
+
candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing).map((peer) => peer.id)
|
|
978
|
+
}),
|
|
979
|
+
connect: (peer) => {
|
|
980
|
+
if (this._ctx.disposed) {
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
scheduleTask(this._ctx, async () => {
|
|
984
|
+
try {
|
|
985
|
+
await this._initiateConnection(peer);
|
|
986
|
+
} catch (err) {
|
|
987
|
+
log4.warn("initiation error", err, {
|
|
988
|
+
file: "swarm.ts",
|
|
989
|
+
line: 309,
|
|
990
|
+
scope: this,
|
|
991
|
+
callSite: (f, a) => f(...a)
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
},
|
|
996
|
+
disconnect: async (peer) => {
|
|
997
|
+
if (this._ctx.disposed) {
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
scheduleTask(this._ctx, async () => {
|
|
1001
|
+
await this._closeConnection(peer);
|
|
1002
|
+
this._topology.update();
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* Creates a connection then sends message over signal network.
|
|
1009
|
+
*/
|
|
1010
|
+
async _initiateConnection(remoteId) {
|
|
1011
|
+
const ctx = this._ctx;
|
|
1012
|
+
if (remoteId.toHex() < this._ownPeerId.toHex()) {
|
|
1013
|
+
log4("initiation delay", {
|
|
1014
|
+
remoteId
|
|
1015
|
+
}, {
|
|
1016
|
+
file: "swarm.ts",
|
|
1017
|
+
line: 336,
|
|
1018
|
+
scope: this,
|
|
1019
|
+
callSite: (f, a) => f(...a)
|
|
1020
|
+
});
|
|
1021
|
+
await sleep(INITIATION_DELAY);
|
|
1022
|
+
}
|
|
1023
|
+
if (ctx.disposed) {
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
const peer = this._getOrCreatePeer(remoteId);
|
|
1027
|
+
if (peer.connection) {
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
log4("initiating connection...", {
|
|
1031
|
+
remoteId
|
|
1032
|
+
}, {
|
|
1033
|
+
file: "swarm.ts",
|
|
1034
|
+
line: 350,
|
|
1035
|
+
scope: this,
|
|
1036
|
+
callSite: (f, a) => f(...a)
|
|
1037
|
+
});
|
|
1038
|
+
await peer.initiateConnection();
|
|
1039
|
+
this._topology.update();
|
|
1040
|
+
log4("initiated", {
|
|
1041
|
+
remoteId
|
|
1042
|
+
}, {
|
|
1043
|
+
file: "swarm.ts",
|
|
1044
|
+
line: 353,
|
|
1045
|
+
scope: this,
|
|
1046
|
+
callSite: (f, a) => f(...a)
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
async _closeConnection(peerId) {
|
|
1050
|
+
const peer = this._peers.get(peerId);
|
|
1051
|
+
if (!peer) {
|
|
1052
|
+
return;
|
|
1053
|
+
}
|
|
1054
|
+
await peer.closeConnection();
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
__decorate3([
|
|
1058
|
+
logInfo
|
|
1059
|
+
], Swarm.prototype, "instanceId", void 0);
|
|
1060
|
+
__decorate3([
|
|
1061
|
+
logInfo
|
|
1062
|
+
], Swarm.prototype, "ownPeerId", null);
|
|
1063
|
+
__decorate3([
|
|
1064
|
+
logInfo
|
|
1065
|
+
], Swarm.prototype, "topic", null);
|
|
1066
|
+
__decorate3([
|
|
1067
|
+
synchronized3
|
|
1068
|
+
], Swarm.prototype, "onSwarmEvent", null);
|
|
1069
|
+
__decorate3([
|
|
1070
|
+
synchronized3
|
|
1071
|
+
], Swarm.prototype, "onOffer", null);
|
|
1072
|
+
__decorate3([
|
|
1073
|
+
synchronized3
|
|
1074
|
+
], Swarm.prototype, "onSignal", null);
|
|
1075
|
+
__decorate3([
|
|
1076
|
+
synchronized3
|
|
1077
|
+
], Swarm.prototype, "goOffline", null);
|
|
1078
|
+
__decorate3([
|
|
1079
|
+
synchronized3
|
|
1080
|
+
], Swarm.prototype, "goOnline", null);
|
|
1081
|
+
|
|
1082
|
+
// packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts
|
|
1083
|
+
import { Event as Event3, EventSubscriptions } from "@dxos/async";
|
|
1084
|
+
import { PublicKey as PublicKey4 } from "@dxos/keys";
|
|
1085
|
+
import { log as log5 } from "@dxos/log";
|
|
1086
|
+
import { ComplexMap as ComplexMap3 } from "@dxos/util";
|
|
1087
|
+
var SwarmMapper = class {
|
|
1088
|
+
get peers() {
|
|
1089
|
+
return Array.from(this._peers.values());
|
|
1090
|
+
}
|
|
1091
|
+
// prettier-ignore
|
|
1092
|
+
constructor(_swarm) {
|
|
1093
|
+
this._swarm = _swarm;
|
|
1094
|
+
this._subscriptions = new EventSubscriptions();
|
|
1095
|
+
this._connectionSubscriptions = new ComplexMap3(PublicKey4.hash);
|
|
1096
|
+
this._peers = new ComplexMap3(PublicKey4.hash);
|
|
1097
|
+
this.mapUpdated = new Event3();
|
|
1098
|
+
this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
|
|
1099
|
+
this._update();
|
|
1100
|
+
this._connectionSubscriptions.set(connection.remoteId, connection.stateChanged.on(() => {
|
|
1101
|
+
this._update();
|
|
1102
|
+
}));
|
|
1103
|
+
}));
|
|
1104
|
+
this._subscriptions.add(_swarm.disconnected.on((peerId) => {
|
|
1105
|
+
var _a;
|
|
1106
|
+
(_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
|
|
1107
|
+
this._connectionSubscriptions.delete(peerId);
|
|
1108
|
+
this._update();
|
|
1109
|
+
}));
|
|
1110
|
+
this._update();
|
|
1111
|
+
}
|
|
1112
|
+
_update() {
|
|
1113
|
+
log5("updating swarm", {}, {
|
|
1114
|
+
file: "swarm-mapper.ts",
|
|
1115
|
+
line: 75,
|
|
1116
|
+
scope: this,
|
|
1117
|
+
callSite: (f, a) => f(...a)
|
|
1118
|
+
});
|
|
1119
|
+
this._peers.clear();
|
|
1120
|
+
this._peers.set(this._swarm.ownPeerId, {
|
|
1121
|
+
id: this._swarm.ownPeerId,
|
|
1122
|
+
state: "ME",
|
|
1123
|
+
connections: []
|
|
1124
|
+
});
|
|
1125
|
+
for (const connection of this._swarm.connections) {
|
|
1126
|
+
this._peers.set(connection.remoteId, {
|
|
1127
|
+
id: connection.remoteId,
|
|
1128
|
+
state: connection.state,
|
|
1129
|
+
connections: [
|
|
1130
|
+
this._swarm.ownPeerId
|
|
1131
|
+
]
|
|
1132
|
+
});
|
|
1133
|
+
}
|
|
1134
|
+
log5("graph changed", {
|
|
1135
|
+
directConnections: this._swarm.connections.length,
|
|
1136
|
+
totalPeersInSwarm: this._peers.size
|
|
1137
|
+
}, {
|
|
1138
|
+
file: "swarm-mapper.ts",
|
|
1139
|
+
line: 116,
|
|
1140
|
+
scope: this,
|
|
1141
|
+
callSite: (f, a) => f(...a)
|
|
1142
|
+
});
|
|
1143
|
+
this.mapUpdated.emit(Array.from(this._peers.values()));
|
|
1144
|
+
}
|
|
1145
|
+
// TODO(burdon): Async open/close.
|
|
1146
|
+
destroy() {
|
|
1147
|
+
Array.from(this._connectionSubscriptions.values()).forEach((cb) => cb());
|
|
1148
|
+
this._subscriptions.clear();
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
// packages/core/mesh/network-manager/src/connection-log.ts
|
|
1153
|
+
import { Event as Event4 } from "@dxos/async";
|
|
1154
|
+
import { raise } from "@dxos/debug";
|
|
1155
|
+
import { PublicKey as PublicKey5 } from "@dxos/keys";
|
|
1156
|
+
import { ComplexMap as ComplexMap4 } from "@dxos/util";
|
|
1157
|
+
var EventType;
|
|
1158
|
+
(function(EventType2) {
|
|
1159
|
+
EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
|
|
1160
|
+
EventType2["PROTOCOL_ERROR"] = "PROTOCOL_ERROR";
|
|
1161
|
+
EventType2["PROTOCOL_EXTENSIONS_INITIALIZED"] = "PROTOCOL_EXTENSIONS_INITIALIZED";
|
|
1162
|
+
EventType2["PROTOCOL_EXTENSIONS_HANDSHAKE"] = "PROTOCOL_EXTENSIONS_HANDSHAKE";
|
|
1163
|
+
EventType2["PROTOCOL_HANDSHAKE"] = "PROTOCOL_HANDSHAKE";
|
|
1164
|
+
})(EventType || (EventType = {}));
|
|
1165
|
+
var ConnectionLog = class {
|
|
1166
|
+
constructor() {
|
|
1167
|
+
/**
|
|
1168
|
+
* SwarmId => info
|
|
1169
|
+
*/
|
|
1170
|
+
this._swarms = new ComplexMap4(PublicKey5.hash);
|
|
1171
|
+
this.update = new Event4();
|
|
1172
|
+
}
|
|
1173
|
+
getSwarmInfo(swarmId) {
|
|
1174
|
+
var _a;
|
|
1175
|
+
return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
|
|
1176
|
+
}
|
|
1177
|
+
get swarms() {
|
|
1178
|
+
return Array.from(this._swarms.values());
|
|
1179
|
+
}
|
|
1180
|
+
joinedSwarm(swarm) {
|
|
1181
|
+
const info = {
|
|
1182
|
+
id: swarm.instanceId,
|
|
1183
|
+
topic: swarm.topic,
|
|
1184
|
+
isActive: true,
|
|
1185
|
+
label: swarm.label,
|
|
1186
|
+
connections: []
|
|
1187
|
+
};
|
|
1188
|
+
this._swarms.set(swarm.instanceId, info);
|
|
1189
|
+
this.update.emit();
|
|
1190
|
+
swarm.connectionAdded.on((connection) => {
|
|
1191
|
+
const connectionInfo = {
|
|
1192
|
+
state: ConnectionState.INITIAL,
|
|
1193
|
+
remotePeerId: connection.remoteId,
|
|
1194
|
+
sessionId: connection.sessionId,
|
|
1195
|
+
transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
|
|
1196
|
+
protocolExtensions: [],
|
|
1197
|
+
events: []
|
|
1198
|
+
};
|
|
1199
|
+
info.connections.push(connectionInfo);
|
|
1200
|
+
this.update.emit();
|
|
1201
|
+
connection.stateChanged.on((state) => {
|
|
1202
|
+
connectionInfo.state = state;
|
|
1203
|
+
connectionInfo.events.push({
|
|
1204
|
+
type: EventType.CONNECTION_STATE_CHANGED,
|
|
1205
|
+
newState: state
|
|
1206
|
+
});
|
|
1207
|
+
this.update.emit();
|
|
1208
|
+
});
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
leftSwarm(swarm) {
|
|
1212
|
+
this.getSwarmInfo(swarm.instanceId).isActive = false;
|
|
1213
|
+
this.update.emit();
|
|
1214
|
+
}
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
// packages/core/mesh/network-manager/src/network-manager.ts
|
|
1218
|
+
import assert5 from "@dxos/node-std/assert";
|
|
1219
|
+
import { Event as Event5 } from "@dxos/async";
|
|
1220
|
+
import { PublicKey as PublicKey6 } from "@dxos/keys";
|
|
1221
|
+
import { log as log6 } from "@dxos/log";
|
|
1222
|
+
import { Messenger } from "@dxos/messaging";
|
|
1223
|
+
import { ConnectionState as ConnectionState2 } from "@dxos/protocols/proto/dxos/client/services";
|
|
1224
|
+
import { ComplexMap as ComplexMap5 } from "@dxos/util";
|
|
1225
|
+
var NetworkManager = class {
|
|
1226
|
+
constructor({ transportFactory, signalManager, log: log13 }) {
|
|
1227
|
+
this._swarms = new ComplexMap5(PublicKey6.hash);
|
|
1228
|
+
this._mappers = new ComplexMap5(PublicKey6.hash);
|
|
1229
|
+
this._connectionState = ConnectionState2.ONLINE;
|
|
1230
|
+
this.connectionStateChanged = new Event5();
|
|
1231
|
+
this.topicsUpdated = new Event5();
|
|
1232
|
+
this._transportFactory = transportFactory;
|
|
1233
|
+
this._signalManager = signalManager;
|
|
1234
|
+
this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
|
|
1235
|
+
var _a;
|
|
1236
|
+
return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
|
|
1237
|
+
});
|
|
1238
|
+
this._messenger = new Messenger({
|
|
1239
|
+
signalManager: this._signalManager
|
|
1240
|
+
});
|
|
1241
|
+
this._signalConnection = {
|
|
1242
|
+
join: (opts) => this._signalManager.join(opts),
|
|
1243
|
+
leave: (opts) => this._signalManager.leave(opts)
|
|
1244
|
+
};
|
|
1245
|
+
if (log13) {
|
|
1246
|
+
this._connectionLog = new ConnectionLog();
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
// TODO(burdon): Remove access (Devtools only).
|
|
1250
|
+
get connectionLog() {
|
|
1251
|
+
return this._connectionLog;
|
|
1252
|
+
}
|
|
1253
|
+
// TODO(burdon): Remove access (Devtools only).
|
|
1254
|
+
get signalManager() {
|
|
1255
|
+
return this._signalManager;
|
|
1256
|
+
}
|
|
1257
|
+
get connectionState() {
|
|
1258
|
+
return this._connectionState;
|
|
1259
|
+
}
|
|
1260
|
+
// TODO(burdon): Reconcile with "discovery_key".
|
|
1261
|
+
get topics() {
|
|
1262
|
+
return Array.from(this._swarms.keys());
|
|
1263
|
+
}
|
|
1264
|
+
getSwarmMap(topic) {
|
|
1265
|
+
return this._mappers.get(topic);
|
|
1266
|
+
}
|
|
1267
|
+
getSwarm(topic) {
|
|
1268
|
+
return this._swarms.get(topic);
|
|
1269
|
+
}
|
|
1270
|
+
async open() {
|
|
1271
|
+
await this._messenger.open();
|
|
1272
|
+
await this._signalManager.open();
|
|
1273
|
+
}
|
|
1274
|
+
async close() {
|
|
1275
|
+
for (const topic of this._swarms.keys()) {
|
|
1276
|
+
await this.leaveSwarm(topic).catch((err) => {
|
|
1277
|
+
log6(err, {}, {
|
|
1278
|
+
file: "network-manager.ts",
|
|
1279
|
+
line: 136,
|
|
1280
|
+
scope: this,
|
|
1281
|
+
callSite: (f, a) => f(...a)
|
|
1282
|
+
});
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
await this._messenger.close();
|
|
1286
|
+
await this._signalManager.close();
|
|
1287
|
+
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Join the swarm.
|
|
1290
|
+
*/
|
|
1291
|
+
async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
|
|
1292
|
+
var _a;
|
|
1293
|
+
assert5(PublicKey6.isPublicKey(topic));
|
|
1294
|
+
assert5(PublicKey6.isPublicKey(peerId));
|
|
1295
|
+
assert5(topology);
|
|
1296
|
+
assert5(typeof protocol === "function");
|
|
1297
|
+
if (this._swarms.has(topic)) {
|
|
1298
|
+
throw new Error(`Already connected to swarm: ${PublicKey6.from(topic)}`);
|
|
1299
|
+
}
|
|
1300
|
+
log6("joining", {
|
|
1301
|
+
topic: PublicKey6.from(topic),
|
|
1302
|
+
peerId,
|
|
1303
|
+
topology: topology.toString()
|
|
1304
|
+
}, {
|
|
1305
|
+
file: "network-manager.ts",
|
|
1306
|
+
line: 162,
|
|
1307
|
+
scope: this,
|
|
1308
|
+
callSite: (f, a) => f(...a)
|
|
1309
|
+
});
|
|
1310
|
+
const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label);
|
|
1311
|
+
swarm.errors.handle((error) => {
|
|
1312
|
+
log6("swarm error", {
|
|
1313
|
+
error
|
|
1314
|
+
}, {
|
|
1315
|
+
file: "network-manager.ts",
|
|
1316
|
+
line: 165,
|
|
1317
|
+
scope: this,
|
|
1318
|
+
callSite: (f, a) => f(...a)
|
|
1319
|
+
});
|
|
1320
|
+
});
|
|
1321
|
+
this._swarms.set(topic, swarm);
|
|
1322
|
+
this._signalConnection.join({
|
|
1323
|
+
topic,
|
|
1324
|
+
peerId
|
|
1325
|
+
}).catch((error) => log6.catch(error, {}, {
|
|
1326
|
+
file: "network-manager.ts",
|
|
1327
|
+
line: 169,
|
|
1328
|
+
scope: this,
|
|
1329
|
+
callSite: (f, a) => f(...a)
|
|
1330
|
+
}));
|
|
1331
|
+
this._mappers.set(topic, new SwarmMapper(swarm));
|
|
1332
|
+
this.topicsUpdated.emit();
|
|
1333
|
+
(_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
|
|
1334
|
+
log6("joined", {
|
|
1335
|
+
topic: PublicKey6.from(topic),
|
|
1336
|
+
count: this._swarms.size
|
|
1337
|
+
}, {
|
|
1338
|
+
file: "network-manager.ts",
|
|
1339
|
+
line: 174,
|
|
1340
|
+
scope: this,
|
|
1341
|
+
callSite: (f, a) => f(...a)
|
|
1342
|
+
});
|
|
1343
|
+
return {
|
|
1344
|
+
close: () => this.leaveSwarm(topic)
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Close the connection.
|
|
1349
|
+
*/
|
|
1350
|
+
async leaveSwarm(topic) {
|
|
1351
|
+
var _a;
|
|
1352
|
+
if (!this._swarms.has(topic)) {
|
|
1353
|
+
log6.warn("swarm not open", {
|
|
1354
|
+
topic: PublicKey6.from(topic).truncate()
|
|
1355
|
+
}, {
|
|
1356
|
+
file: "network-manager.ts",
|
|
1357
|
+
line: 186,
|
|
1358
|
+
scope: this,
|
|
1359
|
+
callSite: (f, a) => f(...a)
|
|
1360
|
+
});
|
|
1361
|
+
return;
|
|
1362
|
+
}
|
|
1363
|
+
log6("leaving", {
|
|
1364
|
+
topic: PublicKey6.from(topic)
|
|
1365
|
+
}, {
|
|
1366
|
+
file: "network-manager.ts",
|
|
1367
|
+
line: 190,
|
|
1368
|
+
scope: this,
|
|
1369
|
+
callSite: (f, a) => f(...a)
|
|
1370
|
+
});
|
|
1371
|
+
const swarm = this._swarms.get(topic);
|
|
1372
|
+
await this._signalConnection.leave({
|
|
1373
|
+
topic,
|
|
1374
|
+
peerId: swarm.ownPeerId
|
|
1375
|
+
});
|
|
1376
|
+
const map = this._mappers.get(topic);
|
|
1377
|
+
map.destroy();
|
|
1378
|
+
this._mappers.delete(topic);
|
|
1379
|
+
(_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
|
|
1380
|
+
await swarm.destroy();
|
|
1381
|
+
this._swarms.delete(topic);
|
|
1382
|
+
await this.topicsUpdated.emit();
|
|
1383
|
+
log6("left", {
|
|
1384
|
+
topic: PublicKey6.from(topic),
|
|
1385
|
+
count: this._swarms.size
|
|
1386
|
+
}, {
|
|
1387
|
+
file: "network-manager.ts",
|
|
1388
|
+
line: 204,
|
|
1389
|
+
scope: this,
|
|
1390
|
+
callSite: (f, a) => f(...a)
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
async setConnectionState(state) {
|
|
1394
|
+
if (state === this._connectionState) {
|
|
1395
|
+
return;
|
|
1396
|
+
}
|
|
1397
|
+
switch (state) {
|
|
1398
|
+
case ConnectionState2.OFFLINE: {
|
|
1399
|
+
this._connectionState = state;
|
|
1400
|
+
await Promise.all([
|
|
1401
|
+
...this._swarms.values()
|
|
1402
|
+
].map((swarm) => swarm.goOffline()));
|
|
1403
|
+
await this._messenger.close();
|
|
1404
|
+
await this._signalManager.close();
|
|
1405
|
+
break;
|
|
1406
|
+
}
|
|
1407
|
+
case ConnectionState2.ONLINE: {
|
|
1408
|
+
this._connectionState = state;
|
|
1409
|
+
this._messenger.open();
|
|
1410
|
+
await Promise.all([
|
|
1411
|
+
...this._swarms.values()
|
|
1412
|
+
].map((swarm) => swarm.goOnline()));
|
|
1413
|
+
await this._signalManager.open();
|
|
1414
|
+
break;
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
this.connectionStateChanged.emit(this._connectionState);
|
|
1418
|
+
}
|
|
1419
|
+
};
|
|
1420
|
+
|
|
1421
|
+
// packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
|
|
1422
|
+
import assert6 from "@dxos/node-std/assert";
|
|
1423
|
+
var FullyConnectedTopology = class {
|
|
1424
|
+
toString() {
|
|
1425
|
+
return "FullyConnectedTopology";
|
|
1426
|
+
}
|
|
1427
|
+
init(controller) {
|
|
1428
|
+
assert6(!this._controller, "Already initialized");
|
|
1429
|
+
this._controller = controller;
|
|
1430
|
+
}
|
|
1431
|
+
update() {
|
|
1432
|
+
assert6(this._controller, "Not initialized");
|
|
1433
|
+
const { candidates: discovered } = this._controller.getState();
|
|
1434
|
+
for (const peer of discovered) {
|
|
1435
|
+
this._controller.connect(peer);
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
async onOffer(peer) {
|
|
1439
|
+
return true;
|
|
1440
|
+
}
|
|
1441
|
+
async destroy() {
|
|
1442
|
+
}
|
|
1443
|
+
};
|
|
1444
|
+
|
|
1445
|
+
// packages/core/mesh/network-manager/src/topology/mmst-topology.ts
|
|
1446
|
+
import assert7 from "@dxos/node-std/assert";
|
|
1447
|
+
import distance from "xor-distance";
|
|
1448
|
+
import { log as log7 } from "@dxos/log";
|
|
1449
|
+
var MMSTTopology = class {
|
|
1450
|
+
constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
|
|
1451
|
+
this._sampleCollected = false;
|
|
1452
|
+
this._originateConnections = originateConnections;
|
|
1453
|
+
this._maxPeers = maxPeers;
|
|
1454
|
+
this._sampleSize = sampleSize;
|
|
1455
|
+
}
|
|
1456
|
+
init(controller) {
|
|
1457
|
+
assert7(!this._controller, "Already initialized");
|
|
1458
|
+
this._controller = controller;
|
|
1459
|
+
}
|
|
1460
|
+
update() {
|
|
1461
|
+
assert7(this._controller, "Not initialized");
|
|
1462
|
+
const { connected, candidates } = this._controller.getState();
|
|
1463
|
+
if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
|
|
1464
|
+
log7("Running the algorithm.", {}, {
|
|
1465
|
+
file: "mmst-topology.ts",
|
|
1466
|
+
line: 55,
|
|
1467
|
+
scope: this,
|
|
1468
|
+
callSite: (f, a) => f(...a)
|
|
1469
|
+
});
|
|
1470
|
+
this._sampleCollected = true;
|
|
1471
|
+
this._runAlgorithm();
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
async onOffer(peer) {
|
|
1475
|
+
assert7(this._controller, "Not initialized");
|
|
1476
|
+
const { connected } = this._controller.getState();
|
|
1477
|
+
const accept = connected.length < this._maxPeers;
|
|
1478
|
+
log7(`Offer ${peer} accept=${accept}`, {}, {
|
|
1479
|
+
file: "mmst-topology.ts",
|
|
1480
|
+
line: 65,
|
|
1481
|
+
scope: this,
|
|
1482
|
+
callSite: (f, a) => f(...a)
|
|
1483
|
+
});
|
|
1484
|
+
return accept;
|
|
1485
|
+
}
|
|
1486
|
+
async destroy() {
|
|
1487
|
+
}
|
|
1488
|
+
_runAlgorithm() {
|
|
1489
|
+
assert7(this._controller, "Not initialized");
|
|
1490
|
+
const { connected, candidates, ownPeerId } = this._controller.getState();
|
|
1491
|
+
if (connected.length > this._maxPeers) {
|
|
1492
|
+
const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
|
|
1493
|
+
for (const peer of sorted) {
|
|
1494
|
+
log7(`Disconnect ${peer}.`, {}, {
|
|
1495
|
+
file: "mmst-topology.ts",
|
|
1496
|
+
line: 83,
|
|
1497
|
+
scope: this,
|
|
1498
|
+
callSite: (f, a) => f(...a)
|
|
1499
|
+
});
|
|
1500
|
+
this._controller.disconnect(peer);
|
|
1501
|
+
}
|
|
1502
|
+
} else if (connected.length < this._originateConnections) {
|
|
1503
|
+
const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
|
|
1504
|
+
const sorted1 = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
|
|
1505
|
+
for (const peer1 of sorted1) {
|
|
1506
|
+
log7(`Connect ${peer1}.`, {}, {
|
|
1507
|
+
file: "mmst-topology.ts",
|
|
1508
|
+
line: 91,
|
|
1509
|
+
scope: this,
|
|
1510
|
+
callSite: (f, a) => f(...a)
|
|
1511
|
+
});
|
|
1512
|
+
this._controller.connect(peer1);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
toString() {
|
|
1517
|
+
return "MMSTTopology";
|
|
1518
|
+
}
|
|
1519
|
+
};
|
|
1520
|
+
var sortByXorDistance = (keys, reference) => keys.sort((a, b) => distance.gt(distance(a.asBuffer(), reference.asBuffer()), distance(b.asBuffer(), reference.asBuffer())));
|
|
1521
|
+
|
|
1522
|
+
// packages/core/mesh/network-manager/src/topology/star-topology.ts
|
|
1523
|
+
import assert8 from "@dxos/node-std/assert";
|
|
1524
|
+
import { log as log8 } from "@dxos/log";
|
|
1525
|
+
var StarTopology = class {
|
|
1526
|
+
// prettier-ignore
|
|
1527
|
+
constructor(_centralPeer) {
|
|
1528
|
+
this._centralPeer = _centralPeer;
|
|
1529
|
+
}
|
|
1530
|
+
toString() {
|
|
1531
|
+
return `StarTopology(${this._centralPeer.truncate()})`;
|
|
1532
|
+
}
|
|
1533
|
+
init(controller) {
|
|
1534
|
+
assert8(!this._controller, "Already initialized.");
|
|
1535
|
+
this._controller = controller;
|
|
1536
|
+
}
|
|
1537
|
+
update() {
|
|
1538
|
+
assert8(this._controller, "Not initialized.");
|
|
1539
|
+
const { candidates, connected, ownPeerId } = this._controller.getState();
|
|
1540
|
+
if (!ownPeerId.equals(this._centralPeer)) {
|
|
1541
|
+
log8("leaf peer dropping all connections apart from central peer.", {}, {
|
|
1542
|
+
file: "star-topology.ts",
|
|
1543
|
+
line: 33,
|
|
1544
|
+
scope: this,
|
|
1545
|
+
callSite: (f, a) => f(...a)
|
|
1546
|
+
});
|
|
1547
|
+
for (const peer of connected) {
|
|
1548
|
+
if (!peer.equals(this._centralPeer)) {
|
|
1549
|
+
log8("dropping connection", {
|
|
1550
|
+
peer
|
|
1551
|
+
}, {
|
|
1552
|
+
file: "star-topology.ts",
|
|
1553
|
+
line: 38,
|
|
1554
|
+
scope: this,
|
|
1555
|
+
callSite: (f, a) => f(...a)
|
|
1556
|
+
});
|
|
1557
|
+
this._controller.disconnect(peer);
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
for (const peer1 of candidates) {
|
|
1562
|
+
if (peer1.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
|
|
1563
|
+
log8("connecting to peer", {
|
|
1564
|
+
peer: peer1
|
|
1565
|
+
}, {
|
|
1566
|
+
file: "star-topology.ts",
|
|
1567
|
+
line: 47,
|
|
1568
|
+
scope: this,
|
|
1569
|
+
callSite: (f, a) => f(...a)
|
|
1570
|
+
});
|
|
1571
|
+
this._controller.connect(peer1);
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
async onOffer(peer) {
|
|
1576
|
+
assert8(this._controller, "Not initialized.");
|
|
1577
|
+
const { ownPeerId } = this._controller.getState();
|
|
1578
|
+
log8("offer", {
|
|
1579
|
+
peer,
|
|
1580
|
+
isCentral: peer.equals(this._centralPeer),
|
|
1581
|
+
isSelfCentral: ownPeerId.equals(this._centralPeer)
|
|
1582
|
+
}, {
|
|
1583
|
+
file: "star-topology.ts",
|
|
1584
|
+
line: 56,
|
|
1585
|
+
scope: this,
|
|
1586
|
+
callSite: (f, a) => f(...a)
|
|
1587
|
+
});
|
|
1588
|
+
return ownPeerId.equals(this._centralPeer) || peer.equals(this._centralPeer);
|
|
1589
|
+
}
|
|
1590
|
+
async destroy() {
|
|
1591
|
+
}
|
|
1592
|
+
};
|
|
1593
|
+
|
|
1594
|
+
// packages/core/mesh/network-manager/src/transport/memory-transport.ts
|
|
1595
|
+
import assert9 from "@dxos/node-std/assert";
|
|
1596
|
+
import { Transform } from "@dxos/node-std/stream";
|
|
1597
|
+
import { Event as Event6, Trigger } from "@dxos/async";
|
|
1598
|
+
import { ErrorStream as ErrorStream3 } from "@dxos/debug";
|
|
1599
|
+
import { PublicKey as PublicKey7 } from "@dxos/keys";
|
|
1600
|
+
import { log as log9, logInfo as logInfo2 } from "@dxos/log";
|
|
1601
|
+
import { ComplexMap as ComplexMap6 } from "@dxos/util";
|
|
1602
|
+
var __decorate4 = function(decorators, target, key, desc) {
|
|
1603
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1604
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
1605
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
1606
|
+
else
|
|
1607
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
1608
|
+
if (d = decorators[i])
|
|
1609
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1610
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1611
|
+
};
|
|
1612
|
+
var MEMORY_TRANSPORT_DELAY = 1;
|
|
1613
|
+
var createStreamDelay = (delay) => {
|
|
1614
|
+
return new Transform({
|
|
1615
|
+
objectMode: true,
|
|
1616
|
+
transform: (chunk, _, cb) => {
|
|
1617
|
+
setTimeout(() => cb(null, chunk), delay);
|
|
1618
|
+
}
|
|
1619
|
+
});
|
|
1620
|
+
};
|
|
1621
|
+
var MemoryTransportFactory = {
|
|
1622
|
+
createTransport: (params) => new MemoryTransport(params)
|
|
1623
|
+
};
|
|
1624
|
+
var _MemoryTransport = class {
|
|
1625
|
+
constructor(options) {
|
|
1626
|
+
var _a;
|
|
1627
|
+
this.options = options;
|
|
1628
|
+
this.closed = new Event6();
|
|
1629
|
+
this.connected = new Event6();
|
|
1630
|
+
this.errors = new ErrorStream3();
|
|
1631
|
+
this._remote = new Trigger();
|
|
1632
|
+
this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
1633
|
+
this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
1634
|
+
this._destroyed = false;
|
|
1635
|
+
this._instanceId = PublicKey7.random();
|
|
1636
|
+
log9("creating", {}, {
|
|
1637
|
+
file: "memory-transport.ts",
|
|
1638
|
+
line: 64,
|
|
1639
|
+
scope: this,
|
|
1640
|
+
callSite: (f, a) => f(...a)
|
|
1641
|
+
});
|
|
1642
|
+
assert9(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
|
|
1643
|
+
_MemoryTransport._connections.set(this._instanceId, this);
|
|
1644
|
+
if (this.options.initiator) {
|
|
1645
|
+
setTimeout(async () => {
|
|
1646
|
+
log9("sending signal", {}, {
|
|
1647
|
+
file: "memory-transport.ts",
|
|
1648
|
+
line: 73,
|
|
1649
|
+
scope: this,
|
|
1650
|
+
callSite: (f, a) => f(...a)
|
|
1651
|
+
});
|
|
1652
|
+
void this.options.sendSignal({
|
|
1653
|
+
payload: {
|
|
1654
|
+
transportId: this._instanceId.toHex()
|
|
1655
|
+
}
|
|
1656
|
+
});
|
|
1657
|
+
});
|
|
1658
|
+
} else {
|
|
1659
|
+
this._remote.wait({
|
|
1660
|
+
timeout: (_a = this.options.timeout) != null ? _a : 1e3
|
|
1661
|
+
}).then((remoteId) => {
|
|
1662
|
+
if (this._destroyed) {
|
|
1663
|
+
return;
|
|
1664
|
+
}
|
|
1665
|
+
this._remoteInstanceId = remoteId;
|
|
1666
|
+
this._remoteConnection = _MemoryTransport._connections.get(this._remoteInstanceId);
|
|
1667
|
+
if (!this._remoteConnection) {
|
|
1668
|
+
this._destroyed = true;
|
|
1669
|
+
this.closed.emit();
|
|
1670
|
+
return;
|
|
1671
|
+
}
|
|
1672
|
+
assert9(!this._remoteConnection._remoteConnection, new Error(`Remote already connected: ${this._remoteInstanceId}`));
|
|
1673
|
+
this._remoteConnection._remoteConnection = this;
|
|
1674
|
+
this._remoteConnection._remoteInstanceId = this._instanceId;
|
|
1675
|
+
log9("connected", {}, {
|
|
1676
|
+
file: "memory-transport.ts",
|
|
1677
|
+
line: 102,
|
|
1678
|
+
scope: this,
|
|
1679
|
+
callSite: (f, a) => f(...a)
|
|
1680
|
+
});
|
|
1681
|
+
this.options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection.options.stream).pipe(this._incomingDelay).pipe(this.options.stream);
|
|
1682
|
+
this.connected.emit();
|
|
1683
|
+
this._remoteConnection.connected.emit();
|
|
1684
|
+
}).catch((err) => {
|
|
1685
|
+
if (this._destroyed) {
|
|
1686
|
+
return;
|
|
1687
|
+
}
|
|
1688
|
+
this.errors.raise(err);
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
async destroy() {
|
|
1693
|
+
log9("closing", {}, {
|
|
1694
|
+
file: "memory-transport.ts",
|
|
1695
|
+
line: 123,
|
|
1696
|
+
scope: this,
|
|
1697
|
+
callSite: (f, a) => f(...a)
|
|
1698
|
+
});
|
|
1699
|
+
this._destroyed = true;
|
|
1700
|
+
_MemoryTransport._connections.delete(this._instanceId);
|
|
1701
|
+
if (this._remoteConnection) {
|
|
1702
|
+
log9("closing", {}, {
|
|
1703
|
+
file: "memory-transport.ts",
|
|
1704
|
+
line: 128,
|
|
1705
|
+
scope: this,
|
|
1706
|
+
callSite: (f, a) => f(...a)
|
|
1707
|
+
});
|
|
1708
|
+
this._remoteConnection._destroyed = true;
|
|
1709
|
+
_MemoryTransport._connections.delete(this._remoteInstanceId);
|
|
1710
|
+
this._outgoingDelay.unpipe();
|
|
1711
|
+
this._incomingDelay.unpipe();
|
|
1712
|
+
this._remoteConnection.closed.emit();
|
|
1713
|
+
this._remoteConnection._remoteConnection = void 0;
|
|
1714
|
+
this._remoteConnection = void 0;
|
|
1715
|
+
log9("closed", {}, {
|
|
1716
|
+
file: "memory-transport.ts",
|
|
1717
|
+
line: 146,
|
|
1718
|
+
scope: this,
|
|
1719
|
+
callSite: (f, a) => f(...a)
|
|
1720
|
+
});
|
|
1721
|
+
}
|
|
1722
|
+
this.closed.emit();
|
|
1723
|
+
log9("closed", {}, {
|
|
1724
|
+
file: "memory-transport.ts",
|
|
1725
|
+
line: 150,
|
|
1726
|
+
scope: this,
|
|
1727
|
+
callSite: (f, a) => f(...a)
|
|
1728
|
+
});
|
|
1729
|
+
}
|
|
1730
|
+
signal({ payload }) {
|
|
1731
|
+
log9("received signal", {
|
|
1732
|
+
payload
|
|
1733
|
+
}, {
|
|
1734
|
+
file: "memory-transport.ts",
|
|
1735
|
+
line: 154,
|
|
1736
|
+
scope: this,
|
|
1737
|
+
callSite: (f, a) => f(...a)
|
|
1738
|
+
});
|
|
1739
|
+
if (!(payload == null ? void 0 : payload.transportId)) {
|
|
1740
|
+
return;
|
|
1741
|
+
}
|
|
1742
|
+
const transportId = payload.transportId;
|
|
1743
|
+
if (transportId) {
|
|
1744
|
+
const remoteId = PublicKey7.fromHex(transportId);
|
|
1745
|
+
this._remote.wake(remoteId);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
};
|
|
1749
|
+
var MemoryTransport = _MemoryTransport;
|
|
1750
|
+
// TODO(burdon): Remove static properties (inject context into constructor).
|
|
1751
|
+
MemoryTransport._connections = new ComplexMap6(PublicKey7.hash);
|
|
1752
|
+
__decorate4([
|
|
1753
|
+
logInfo2
|
|
1754
|
+
], MemoryTransport.prototype, "_instanceId", void 0);
|
|
1755
|
+
__decorate4([
|
|
1756
|
+
logInfo2
|
|
1757
|
+
], MemoryTransport.prototype, "_remoteInstanceId", void 0);
|
|
1758
|
+
|
|
1759
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
|
|
1760
|
+
import assert10 from "@dxos/node-std/assert";
|
|
1761
|
+
import SimplePeerConstructor from "simple-peer";
|
|
1762
|
+
import { Event as Event7 } from "@dxos/async";
|
|
1763
|
+
import { ErrorStream as ErrorStream4, raise as raise2 } from "@dxos/debug";
|
|
1764
|
+
import { log as log10 } from "@dxos/log";
|
|
1765
|
+
|
|
1766
|
+
// packages/core/mesh/network-manager/src/transport/webrtc.ts
|
|
1767
|
+
var wrtc = null;
|
|
1768
|
+
try {
|
|
1769
|
+
wrtc = __require("@koush/wrtc");
|
|
1770
|
+
} catch (e) {
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
|
|
1774
|
+
var WebRTCTransport = class {
|
|
1775
|
+
constructor(params) {
|
|
1776
|
+
var _a;
|
|
1777
|
+
this.params = params;
|
|
1778
|
+
this._closed = false;
|
|
1779
|
+
this.closed = new Event7();
|
|
1780
|
+
this.connected = new Event7();
|
|
1781
|
+
this.errors = new ErrorStream4();
|
|
1782
|
+
log10("created connection", params, {
|
|
1783
|
+
file: "webrtc-transport.ts",
|
|
1784
|
+
line: 35,
|
|
1785
|
+
scope: this,
|
|
1786
|
+
callSite: (f, a) => f(...a)
|
|
1787
|
+
});
|
|
1788
|
+
this._peer = new SimplePeerConstructor({
|
|
1789
|
+
initiator: this.params.initiator,
|
|
1790
|
+
wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
|
|
1791
|
+
config: this.params.webrtcConfig
|
|
1792
|
+
});
|
|
1793
|
+
this._peer.on("signal", async (data) => {
|
|
1794
|
+
log10("signal", data, {
|
|
1795
|
+
file: "webrtc-transport.ts",
|
|
1796
|
+
line: 43,
|
|
1797
|
+
scope: this,
|
|
1798
|
+
callSite: (f, a) => f(...a)
|
|
1799
|
+
});
|
|
1800
|
+
await this.params.sendSignal({
|
|
1801
|
+
payload: {
|
|
1802
|
+
data
|
|
1803
|
+
}
|
|
1804
|
+
});
|
|
1805
|
+
});
|
|
1806
|
+
this._peer.on("connect", () => {
|
|
1807
|
+
log10("connected", {}, {
|
|
1808
|
+
file: "webrtc-transport.ts",
|
|
1809
|
+
line: 48,
|
|
1810
|
+
scope: this,
|
|
1811
|
+
callSite: (f, a) => f(...a)
|
|
1812
|
+
});
|
|
1813
|
+
this.params.stream.pipe(this._peer).pipe(this.params.stream);
|
|
1814
|
+
this.connected.emit();
|
|
1815
|
+
});
|
|
1816
|
+
this._peer.on("close", async () => {
|
|
1817
|
+
log10("closed", {}, {
|
|
1818
|
+
file: "webrtc-transport.ts",
|
|
1819
|
+
line: 54,
|
|
1820
|
+
scope: this,
|
|
1821
|
+
callSite: (f, a) => f(...a)
|
|
1822
|
+
});
|
|
1823
|
+
await this._disconnectStreams();
|
|
1824
|
+
this.closed.emit();
|
|
1825
|
+
});
|
|
1826
|
+
this._peer.on("error", async (err) => {
|
|
1827
|
+
this.errors.raise(err);
|
|
1828
|
+
await this.destroy();
|
|
1829
|
+
});
|
|
1830
|
+
}
|
|
1831
|
+
async destroy() {
|
|
1832
|
+
log10("closing...", {}, {
|
|
1833
|
+
file: "webrtc-transport.ts",
|
|
1834
|
+
line: 66,
|
|
1835
|
+
scope: this,
|
|
1836
|
+
callSite: (f, a) => f(...a)
|
|
1837
|
+
});
|
|
1838
|
+
this._closed = true;
|
|
1839
|
+
await this._disconnectStreams();
|
|
1840
|
+
this._peer.destroy();
|
|
1841
|
+
this.closed.emit();
|
|
1842
|
+
log10("closed", {}, {
|
|
1843
|
+
file: "webrtc-transport.ts",
|
|
1844
|
+
line: 71,
|
|
1845
|
+
scope: this,
|
|
1846
|
+
callSite: (f, a) => f(...a)
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
signal(signal) {
|
|
1850
|
+
if (this._closed) {
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
assert10(signal.payload.data, "Signal message must contain signal data.");
|
|
1854
|
+
this._peer.signal(signal.payload.data);
|
|
1855
|
+
}
|
|
1856
|
+
async _disconnectStreams() {
|
|
1857
|
+
var _a, _b, _c, _d;
|
|
1858
|
+
(_d = (_c = (_b = (_a = this.params.stream).unpipe) == null ? void 0 : _b.call(_a, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
|
|
1859
|
+
}
|
|
1860
|
+
};
|
|
1861
|
+
var createWebRTCTransportFactory = (webrtcConfig) => ({
|
|
1862
|
+
createTransport: (params) => new WebRTCTransport({
|
|
1863
|
+
...params,
|
|
1864
|
+
webrtcConfig
|
|
1865
|
+
})
|
|
1866
|
+
});
|
|
1867
|
+
|
|
1868
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
|
|
1869
|
+
import assert11 from "@dxos/node-std/assert";
|
|
1870
|
+
import { Duplex } from "@dxos/node-std/stream";
|
|
1871
|
+
import { Stream } from "@dxos/codec-protobuf";
|
|
1872
|
+
import { PublicKey as PublicKey8 } from "@dxos/keys";
|
|
1873
|
+
import { log as log11 } from "@dxos/log";
|
|
1874
|
+
import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
|
|
1875
|
+
import { ComplexMap as ComplexMap7 } from "@dxos/util";
|
|
1876
|
+
var WebRTCTransportService = class {
|
|
1877
|
+
// prettier-ignore
|
|
1878
|
+
constructor(_webrtcConfig) {
|
|
1879
|
+
this._webrtcConfig = _webrtcConfig;
|
|
1880
|
+
this.transports = new ComplexMap7(PublicKey8.hash);
|
|
1881
|
+
}
|
|
1882
|
+
open(request) {
|
|
1883
|
+
const rpcStream = new Stream(({ ready, next, close }) => {
|
|
1884
|
+
const duplex = new Duplex({
|
|
1885
|
+
read: () => {
|
|
1886
|
+
},
|
|
1887
|
+
write: function(chunk, _, callback) {
|
|
1888
|
+
next({
|
|
1889
|
+
data: {
|
|
1890
|
+
payload: chunk
|
|
1891
|
+
}
|
|
1892
|
+
});
|
|
1893
|
+
callback();
|
|
1894
|
+
}
|
|
1895
|
+
});
|
|
1896
|
+
const transport = new WebRTCTransport({
|
|
1897
|
+
initiator: request.initiator,
|
|
1898
|
+
stream: duplex,
|
|
1899
|
+
sendSignal: async (signal) => {
|
|
1900
|
+
next({
|
|
1901
|
+
signal: {
|
|
1902
|
+
payload: signal
|
|
1903
|
+
}
|
|
1904
|
+
});
|
|
1905
|
+
}
|
|
1906
|
+
});
|
|
1907
|
+
next({
|
|
1908
|
+
connection: {
|
|
1909
|
+
state: ConnectionState3.CONNECTING
|
|
1910
|
+
}
|
|
1911
|
+
});
|
|
1912
|
+
transport.connected.on(() => {
|
|
1913
|
+
next({
|
|
1914
|
+
connection: {
|
|
1915
|
+
state: ConnectionState3.CONNECTED
|
|
1916
|
+
}
|
|
1917
|
+
});
|
|
1918
|
+
});
|
|
1919
|
+
transport.errors.handle((err) => {
|
|
1920
|
+
next({
|
|
1921
|
+
connection: {
|
|
1922
|
+
state: ConnectionState3.CLOSED,
|
|
1923
|
+
error: err.toString()
|
|
1924
|
+
}
|
|
1925
|
+
});
|
|
1926
|
+
close(err);
|
|
1927
|
+
});
|
|
1928
|
+
transport.closed.on(() => {
|
|
1929
|
+
next({
|
|
1930
|
+
connection: {
|
|
1931
|
+
state: ConnectionState3.CLOSED
|
|
1932
|
+
}
|
|
1933
|
+
});
|
|
1934
|
+
close();
|
|
1935
|
+
});
|
|
1936
|
+
ready();
|
|
1937
|
+
this.transports.set(request.proxyId, {
|
|
1938
|
+
transport,
|
|
1939
|
+
stream: duplex
|
|
1940
|
+
});
|
|
1941
|
+
});
|
|
1942
|
+
return rpcStream;
|
|
1943
|
+
}
|
|
1944
|
+
async sendSignal({ proxyId, signal }) {
|
|
1945
|
+
assert11(this.transports.has(proxyId));
|
|
1946
|
+
await this.transports.get(proxyId).transport.signal(signal);
|
|
1947
|
+
}
|
|
1948
|
+
async sendData({ proxyId, payload }) {
|
|
1949
|
+
assert11(this.transports.has(proxyId));
|
|
1950
|
+
await this.transports.get(proxyId).stream.push(payload);
|
|
1951
|
+
}
|
|
1952
|
+
async close({ proxyId }) {
|
|
1953
|
+
var _a, _b;
|
|
1954
|
+
await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
|
|
1955
|
+
await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
|
|
1956
|
+
this.transports.delete(proxyId);
|
|
1957
|
+
log11("Closed.", {}, {
|
|
1958
|
+
file: "webrtc-transport-service.ts",
|
|
1959
|
+
line: 108,
|
|
1960
|
+
scope: this,
|
|
1961
|
+
callSite: (f, a) => f(...a)
|
|
1962
|
+
});
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
|
|
1966
|
+
// packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
|
|
1967
|
+
import assert12 from "@dxos/node-std/assert";
|
|
1968
|
+
import { Event as Event8 } from "@dxos/async";
|
|
1969
|
+
import { Context as Context2 } from "@dxos/context";
|
|
1970
|
+
import { ErrorStream as ErrorStream5 } from "@dxos/debug";
|
|
1971
|
+
import { PublicKey as PublicKey9 } from "@dxos/keys";
|
|
1972
|
+
import { log as log12 } from "@dxos/log";
|
|
1973
|
+
import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
|
|
1974
|
+
var WebRTCTransportProxy = class {
|
|
1975
|
+
// prettier-ignore
|
|
1976
|
+
constructor(_params) {
|
|
1977
|
+
this._params = _params;
|
|
1978
|
+
this._proxyId = PublicKey9.random();
|
|
1979
|
+
this._ctx = new Context2();
|
|
1980
|
+
this.closed = new Event8();
|
|
1981
|
+
this._closed = false;
|
|
1982
|
+
this.connected = new Event8();
|
|
1983
|
+
this.errors = new ErrorStream5();
|
|
1984
|
+
this._serviceStream = this._params.bridgeService.open({
|
|
1985
|
+
proxyId: this._proxyId,
|
|
1986
|
+
initiator: this._params.initiator
|
|
1987
|
+
});
|
|
1988
|
+
this._serviceStream.waitUntilReady().then(() => {
|
|
1989
|
+
this._serviceStream.subscribe(async (event) => {
|
|
1990
|
+
log12("WebRTCTransportProxy: event", event, {
|
|
1991
|
+
file: "webrtc-transport-proxy.ts",
|
|
1992
|
+
line: 49,
|
|
1993
|
+
scope: this,
|
|
1994
|
+
callSite: (f, a) => f(...a)
|
|
1995
|
+
});
|
|
1996
|
+
if (event.connection) {
|
|
1997
|
+
await this._handleConnection(event.connection);
|
|
1998
|
+
} else if (event.data) {
|
|
1999
|
+
this._handleData(event.data);
|
|
2000
|
+
} else if (event.signal) {
|
|
2001
|
+
await this._handleSignal(event.signal);
|
|
2002
|
+
}
|
|
2003
|
+
});
|
|
2004
|
+
const dataListener = async (data) => {
|
|
2005
|
+
try {
|
|
2006
|
+
await this._params.bridgeService.sendData({
|
|
2007
|
+
proxyId: this._proxyId,
|
|
2008
|
+
payload: data
|
|
2009
|
+
});
|
|
2010
|
+
} catch (err) {
|
|
2011
|
+
log12.catch(err, {}, {
|
|
2012
|
+
file: "webrtc-transport-proxy.ts",
|
|
2013
|
+
line: 66,
|
|
2014
|
+
scope: this,
|
|
2015
|
+
callSite: (f, a) => f(...a)
|
|
2016
|
+
});
|
|
2017
|
+
}
|
|
2018
|
+
};
|
|
2019
|
+
this._params.stream.on("data", dataListener);
|
|
2020
|
+
this._ctx.onDispose(() => {
|
|
2021
|
+
this._params.stream.off("data", dataListener);
|
|
2022
|
+
});
|
|
2023
|
+
}, (error) => log12.catch(error, {}, {
|
|
2024
|
+
file: "webrtc-transport-proxy.ts",
|
|
2025
|
+
line: 72,
|
|
2026
|
+
scope: this,
|
|
2027
|
+
callSite: (f, a) => f(...a)
|
|
2028
|
+
}));
|
|
2029
|
+
}
|
|
2030
|
+
async _handleConnection(connectionEvent) {
|
|
2031
|
+
if (connectionEvent.error) {
|
|
2032
|
+
this.errors.raise(new Error(connectionEvent.error));
|
|
2033
|
+
}
|
|
2034
|
+
switch (connectionEvent.state) {
|
|
2035
|
+
case ConnectionState4.CONNECTED: {
|
|
2036
|
+
this.connected.emit();
|
|
2037
|
+
break;
|
|
2038
|
+
}
|
|
2039
|
+
case ConnectionState4.CLOSED: {
|
|
2040
|
+
await this.destroy();
|
|
2041
|
+
break;
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
_handleData(dataEvent) {
|
|
2046
|
+
this._params.stream.write(Buffer.from(dataEvent.payload));
|
|
2047
|
+
}
|
|
2048
|
+
async _handleSignal(signalEvent) {
|
|
2049
|
+
await this._params.sendSignal(signalEvent.payload);
|
|
2050
|
+
}
|
|
2051
|
+
signal(signal) {
|
|
2052
|
+
this._params.bridgeService.sendSignal({
|
|
2053
|
+
proxyId: this._proxyId,
|
|
2054
|
+
signal
|
|
2055
|
+
}).catch((err) => this.errors.raise(err));
|
|
2056
|
+
}
|
|
2057
|
+
// TODO(burdon): Move open from constructor.
|
|
2058
|
+
async destroy() {
|
|
2059
|
+
await this._ctx.dispose();
|
|
2060
|
+
if (this._closed) {
|
|
2061
|
+
return;
|
|
2062
|
+
}
|
|
2063
|
+
this._serviceStream.close();
|
|
2064
|
+
try {
|
|
2065
|
+
await this._params.bridgeService.close({
|
|
2066
|
+
proxyId: this._proxyId
|
|
2067
|
+
});
|
|
2068
|
+
} catch (err) {
|
|
2069
|
+
log12.catch(err, {}, {
|
|
2070
|
+
file: "webrtc-transport-proxy.ts",
|
|
2071
|
+
line: 123,
|
|
2072
|
+
scope: this,
|
|
2073
|
+
callSite: (f, a) => f(...a)
|
|
2074
|
+
});
|
|
2075
|
+
}
|
|
2076
|
+
this.closed.emit();
|
|
2077
|
+
this._closed = true;
|
|
2078
|
+
}
|
|
2079
|
+
/**
|
|
2080
|
+
* Called when underlying proxy service becomes unavailable.
|
|
2081
|
+
*/
|
|
2082
|
+
// TODO(burdon): Option on close method.
|
|
2083
|
+
forceClose() {
|
|
2084
|
+
this._serviceStream.close();
|
|
2085
|
+
this.closed.emit();
|
|
2086
|
+
this._closed = true;
|
|
2087
|
+
}
|
|
2088
|
+
};
|
|
2089
|
+
var WebRTCTransportProxyFactory = class {
|
|
2090
|
+
constructor() {
|
|
2091
|
+
this._connections = /* @__PURE__ */ new Set();
|
|
2092
|
+
}
|
|
2093
|
+
/**
|
|
2094
|
+
* Sets the current BridgeService to be used to open connections.
|
|
2095
|
+
* Calling this method will close any existing connections.
|
|
2096
|
+
*/
|
|
2097
|
+
setBridgeService(bridgeService) {
|
|
2098
|
+
this._bridgeService = bridgeService;
|
|
2099
|
+
for (const connection of this._connections) {
|
|
2100
|
+
connection.forceClose();
|
|
2101
|
+
}
|
|
2102
|
+
return this;
|
|
2103
|
+
}
|
|
2104
|
+
createTransport(options) {
|
|
2105
|
+
assert12(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
|
|
2106
|
+
const transport = new WebRTCTransportProxy({
|
|
2107
|
+
...options,
|
|
2108
|
+
bridgeService: this._bridgeService
|
|
2109
|
+
});
|
|
2110
|
+
this._connections.add(transport);
|
|
2111
|
+
transport.closed.on(() => this._connections.delete(transport));
|
|
2112
|
+
return transport;
|
|
2113
|
+
}
|
|
2114
|
+
};
|
|
2115
|
+
|
|
2116
|
+
// packages/core/mesh/network-manager/src/wire-protocol.ts
|
|
2117
|
+
import { Teleport } from "@dxos/teleport";
|
|
2118
|
+
var createTeleportProtocolFactory = (onConnection) => {
|
|
2119
|
+
return (params) => {
|
|
2120
|
+
const teleport = new Teleport(params);
|
|
2121
|
+
return {
|
|
2122
|
+
stream: teleport.stream,
|
|
2123
|
+
initialize: async () => {
|
|
2124
|
+
await teleport.open();
|
|
2125
|
+
await onConnection(teleport);
|
|
2126
|
+
},
|
|
2127
|
+
destroy: async () => {
|
|
2128
|
+
await teleport.close();
|
|
2129
|
+
}
|
|
2130
|
+
};
|
|
2131
|
+
};
|
|
2132
|
+
};
|
|
2133
|
+
|
|
2134
|
+
export {
|
|
2135
|
+
ConnectionState,
|
|
2136
|
+
Connection,
|
|
2137
|
+
MessageRouter,
|
|
2138
|
+
Swarm,
|
|
2139
|
+
SwarmMapper,
|
|
2140
|
+
EventType,
|
|
2141
|
+
ConnectionLog,
|
|
2142
|
+
NetworkManager,
|
|
2143
|
+
FullyConnectedTopology,
|
|
2144
|
+
MMSTTopology,
|
|
2145
|
+
StarTopology,
|
|
2146
|
+
MemoryTransportFactory,
|
|
2147
|
+
MemoryTransport,
|
|
2148
|
+
WebRTCTransport,
|
|
2149
|
+
createWebRTCTransportFactory,
|
|
2150
|
+
WebRTCTransportService,
|
|
2151
|
+
WebRTCTransportProxy,
|
|
2152
|
+
WebRTCTransportProxyFactory,
|
|
2153
|
+
createTeleportProtocolFactory
|
|
2154
|
+
};
|
|
2155
|
+
//# sourceMappingURL=chunk-2TROGP2W.mjs.map
|