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