@dxos/network-manager 0.3.7 → 0.3.8-main.0ae9c21
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-THM7H3W2.mjs → chunk-UKYZHA5S.mjs} +16 -16
- package/dist/lib/browser/index.mjs +1 -1
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +3 -3
- package/dist/lib/node/chunk-PWHTYBOL.cjs +3761 -0
- package/dist/lib/node/chunk-PWHTYBOL.cjs.map +7 -0
- package/dist/lib/node/chunk-XE5JOIGD.cjs +462 -0
- package/dist/lib/node/chunk-XE5JOIGD.cjs.map +7 -0
- package/dist/lib/node/datachannel-53JZPGFA.cjs +35 -0
- package/dist/lib/node/datachannel-53JZPGFA.cjs.map +7 -0
- package/dist/lib/node/index.cjs +35 -4250
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +112 -4140
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/package.json +17 -17
- /package/dist/lib/browser/{chunk-THM7H3W2.mjs.map → chunk-UKYZHA5S.mjs.map} +0 -0
|
@@ -0,0 +1,3761 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var chunk_PWHTYBOL_exports = {};
|
|
30
|
+
__export(chunk_PWHTYBOL_exports, {
|
|
31
|
+
Connection: () => Connection,
|
|
32
|
+
ConnectionLimiter: () => ConnectionLimiter,
|
|
33
|
+
ConnectionLog: () => ConnectionLog,
|
|
34
|
+
ConnectionState: () => ConnectionState,
|
|
35
|
+
EventType: () => EventType,
|
|
36
|
+
FullyConnectedTopology: () => FullyConnectedTopology,
|
|
37
|
+
LibDataChannelTransport: () => LibDataChannelTransport,
|
|
38
|
+
MAX_CONCURRENT_INITIATING_CONNECTIONS: () => MAX_CONCURRENT_INITIATING_CONNECTIONS,
|
|
39
|
+
MMSTTopology: () => MMSTTopology,
|
|
40
|
+
MemoryTransport: () => MemoryTransport,
|
|
41
|
+
MemoryTransportFactory: () => MemoryTransportFactory,
|
|
42
|
+
NetworkManager: () => NetworkManager,
|
|
43
|
+
SimplePeerTransport: () => SimplePeerTransport,
|
|
44
|
+
SimplePeerTransportProxy: () => SimplePeerTransportProxy,
|
|
45
|
+
SimplePeerTransportProxyFactory: () => SimplePeerTransportProxyFactory,
|
|
46
|
+
SimplePeerTransportService: () => SimplePeerTransportService,
|
|
47
|
+
StarTopology: () => StarTopology,
|
|
48
|
+
Swarm: () => Swarm,
|
|
49
|
+
SwarmMapper: () => SwarmMapper,
|
|
50
|
+
SwarmMessenger: () => SwarmMessenger,
|
|
51
|
+
TcpTransport: () => TcpTransport,
|
|
52
|
+
TcpTransportFactory: () => TcpTransportFactory,
|
|
53
|
+
TransportKind: () => TransportKind,
|
|
54
|
+
createLibDataChannelTransportFactory: () => createLibDataChannelTransportFactory,
|
|
55
|
+
createSimplePeerTransportFactory: () => createSimplePeerTransportFactory,
|
|
56
|
+
createTeleportProtocolFactory: () => createTeleportProtocolFactory
|
|
57
|
+
});
|
|
58
|
+
module.exports = __toCommonJS(chunk_PWHTYBOL_exports);
|
|
59
|
+
var import_chunk_XE5JOIGD = require("./chunk-XE5JOIGD.cjs");
|
|
60
|
+
var import_async = require("@dxos/async");
|
|
61
|
+
var import_context = require("@dxos/context");
|
|
62
|
+
var import_debug = require("@dxos/debug");
|
|
63
|
+
var import_invariant = require("@dxos/invariant");
|
|
64
|
+
var import_keys = require("@dxos/keys");
|
|
65
|
+
var import_log = require("@dxos/log");
|
|
66
|
+
var import_protocols = require("@dxos/protocols");
|
|
67
|
+
var import_context2 = require("@dxos/context");
|
|
68
|
+
var import_invariant2 = require("@dxos/invariant");
|
|
69
|
+
var import_keys2 = require("@dxos/keys");
|
|
70
|
+
var import_log2 = require("@dxos/log");
|
|
71
|
+
var import_protocols2 = require("@dxos/protocols");
|
|
72
|
+
var import_util = require("@dxos/util");
|
|
73
|
+
var import_async2 = require("@dxos/async");
|
|
74
|
+
var import_context3 = require("@dxos/context");
|
|
75
|
+
var import_debug2 = require("@dxos/debug");
|
|
76
|
+
var import_invariant3 = require("@dxos/invariant");
|
|
77
|
+
var import_keys3 = require("@dxos/keys");
|
|
78
|
+
var import_log3 = require("@dxos/log");
|
|
79
|
+
var import_protocols3 = require("@dxos/protocols");
|
|
80
|
+
var import_util2 = require("@dxos/util");
|
|
81
|
+
var import_async3 = require("@dxos/async");
|
|
82
|
+
var import_context4 = require("@dxos/context");
|
|
83
|
+
var import_invariant4 = require("@dxos/invariant");
|
|
84
|
+
var import_keys4 = require("@dxos/keys");
|
|
85
|
+
var import_log4 = require("@dxos/log");
|
|
86
|
+
var import_protocols4 = require("@dxos/protocols");
|
|
87
|
+
var import_async4 = require("@dxos/async");
|
|
88
|
+
var import_keys5 = require("@dxos/keys");
|
|
89
|
+
var import_log5 = require("@dxos/log");
|
|
90
|
+
var import_util3 = require("@dxos/util");
|
|
91
|
+
var import_async5 = require("@dxos/async");
|
|
92
|
+
var import_context5 = require("@dxos/context");
|
|
93
|
+
var import_invariant5 = require("@dxos/invariant");
|
|
94
|
+
var import_keys6 = require("@dxos/keys");
|
|
95
|
+
var import_log6 = require("@dxos/log");
|
|
96
|
+
var import_protocols5 = require("@dxos/protocols");
|
|
97
|
+
var import_util4 = require("@dxos/util");
|
|
98
|
+
var import_async6 = require("@dxos/async");
|
|
99
|
+
var import_debug3 = require("@dxos/debug");
|
|
100
|
+
var import_keys7 = require("@dxos/keys");
|
|
101
|
+
var import_util5 = require("@dxos/util");
|
|
102
|
+
var import_async7 = require("@dxos/async");
|
|
103
|
+
var import_invariant6 = require("@dxos/invariant");
|
|
104
|
+
var import_keys8 = require("@dxos/keys");
|
|
105
|
+
var import_log7 = require("@dxos/log");
|
|
106
|
+
var import_messaging = require("@dxos/messaging");
|
|
107
|
+
var import_protocols6 = require("@dxos/protocols");
|
|
108
|
+
var import_services = require("@dxos/protocols/proto/dxos/client/services");
|
|
109
|
+
var import_util6 = require("@dxos/util");
|
|
110
|
+
var import_invariant7 = require("@dxos/invariant");
|
|
111
|
+
var import_xor_distance = __toESM(require("xor-distance"));
|
|
112
|
+
var import_invariant8 = require("@dxos/invariant");
|
|
113
|
+
var import_log8 = require("@dxos/log");
|
|
114
|
+
var import_invariant9 = require("@dxos/invariant");
|
|
115
|
+
var import_log9 = require("@dxos/log");
|
|
116
|
+
var import_node_stream = require("node:stream");
|
|
117
|
+
var import_async8 = require("@dxos/async");
|
|
118
|
+
var import_debug4 = require("@dxos/debug");
|
|
119
|
+
var import_invariant10 = require("@dxos/invariant");
|
|
120
|
+
var import_keys9 = require("@dxos/keys");
|
|
121
|
+
var import_log10 = require("@dxos/log");
|
|
122
|
+
var import_util7 = require("@dxos/util");
|
|
123
|
+
var import_simple_peer = __toESM(require("simple-peer"));
|
|
124
|
+
var import_tiny_invariant = __toESM(require("tiny-invariant"));
|
|
125
|
+
var import_async9 = require("@dxos/async");
|
|
126
|
+
var import_debug5 = require("@dxos/debug");
|
|
127
|
+
var import_keys10 = require("@dxos/keys");
|
|
128
|
+
var import_log11 = require("@dxos/log");
|
|
129
|
+
var import_protocols7 = require("@dxos/protocols");
|
|
130
|
+
var import_node_stream2 = require("node:stream");
|
|
131
|
+
var import_codec_protobuf = require("@dxos/codec-protobuf");
|
|
132
|
+
var import_invariant11 = require("@dxos/invariant");
|
|
133
|
+
var import_keys11 = require("@dxos/keys");
|
|
134
|
+
var import_log12 = require("@dxos/log");
|
|
135
|
+
var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
|
|
136
|
+
var import_util8 = require("@dxos/util");
|
|
137
|
+
var import_node_stream3 = require("node:stream");
|
|
138
|
+
var import_async10 = require("@dxos/async");
|
|
139
|
+
var import_context6 = require("@dxos/context");
|
|
140
|
+
var import_debug6 = require("@dxos/debug");
|
|
141
|
+
var import_invariant12 = require("@dxos/invariant");
|
|
142
|
+
var import_keys12 = require("@dxos/keys");
|
|
143
|
+
var import_log13 = require("@dxos/log");
|
|
144
|
+
var import_protocols8 = require("@dxos/protocols");
|
|
145
|
+
var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
|
|
146
|
+
var import_util9 = require("@dxos/util");
|
|
147
|
+
var import_stream = require("stream");
|
|
148
|
+
var import_async11 = require("@dxos/async");
|
|
149
|
+
var import_debug7 = require("@dxos/debug");
|
|
150
|
+
var import_log14 = require("@dxos/log");
|
|
151
|
+
var import_node_net = require("node:net");
|
|
152
|
+
var import_async12 = require("@dxos/async");
|
|
153
|
+
var import_debug8 = require("@dxos/debug");
|
|
154
|
+
var import_log15 = require("@dxos/log");
|
|
155
|
+
var import_teleport = require("@dxos/teleport");
|
|
156
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
157
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
158
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
159
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
160
|
+
else
|
|
161
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
162
|
+
if (d = decorators[i])
|
|
163
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
164
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
165
|
+
}
|
|
166
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
|
|
167
|
+
var STARTING_SIGNALLING_DELAY = 10;
|
|
168
|
+
var TRANSPORT_CONNECTION_TIMEOUT = 1e4;
|
|
169
|
+
var MAX_SIGNALLING_DELAY = 300;
|
|
170
|
+
var ConnectionState;
|
|
171
|
+
(function(ConnectionState5) {
|
|
172
|
+
ConnectionState5[
|
|
173
|
+
/**
|
|
174
|
+
* Connection is created, but not yet passed through the connection limiter.
|
|
175
|
+
*/
|
|
176
|
+
"CREATED"
|
|
177
|
+
] = "CREATED";
|
|
178
|
+
ConnectionState5[
|
|
179
|
+
/**
|
|
180
|
+
* Initial state. Connection is registered but no attempt to connect to the remote peer has been performed.
|
|
181
|
+
* Might mean that we are waiting for the answer signal from the remote peer.
|
|
182
|
+
*/
|
|
183
|
+
"INITIAL"
|
|
184
|
+
] = "INITIAL";
|
|
185
|
+
ConnectionState5[
|
|
186
|
+
/**
|
|
187
|
+
* Trying to establish connection.
|
|
188
|
+
*/
|
|
189
|
+
"CONNECTING"
|
|
190
|
+
] = "CONNECTING";
|
|
191
|
+
ConnectionState5[
|
|
192
|
+
/**
|
|
193
|
+
* Connection is established.
|
|
194
|
+
*/
|
|
195
|
+
"CONNECTED"
|
|
196
|
+
] = "CONNECTED";
|
|
197
|
+
ConnectionState5[
|
|
198
|
+
/**
|
|
199
|
+
* Connection is being closed.
|
|
200
|
+
*/
|
|
201
|
+
"CLOSING"
|
|
202
|
+
] = "CLOSING";
|
|
203
|
+
ConnectionState5[
|
|
204
|
+
/**
|
|
205
|
+
* Connection closed.
|
|
206
|
+
*/
|
|
207
|
+
"CLOSED"
|
|
208
|
+
] = "CLOSED";
|
|
209
|
+
ConnectionState5["ABORTING"] = "ABORTING";
|
|
210
|
+
ConnectionState5["ABORTED"] = "ABORTED";
|
|
211
|
+
})(ConnectionState || (ConnectionState = {}));
|
|
212
|
+
var Connection = class {
|
|
213
|
+
constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
|
|
214
|
+
this.topic = topic;
|
|
215
|
+
this.ownId = ownId;
|
|
216
|
+
this.remoteId = remoteId;
|
|
217
|
+
this.sessionId = sessionId;
|
|
218
|
+
this.initiator = initiator;
|
|
219
|
+
this._signalMessaging = _signalMessaging;
|
|
220
|
+
this._protocol = _protocol;
|
|
221
|
+
this._transportFactory = _transportFactory;
|
|
222
|
+
this._callbacks = _callbacks;
|
|
223
|
+
this._ctx = new import_context.Context();
|
|
224
|
+
this.connectedTimeoutContext = new import_context.Context();
|
|
225
|
+
this._state = ConnectionState.CREATED;
|
|
226
|
+
this._incomingSignalBuffer = [];
|
|
227
|
+
this._outgoingSignalBuffer = [];
|
|
228
|
+
this.stateChanged = new import_async.Event();
|
|
229
|
+
this.errors = new import_debug.ErrorStream();
|
|
230
|
+
this._instanceId = import_keys.PublicKey.random().toHex();
|
|
231
|
+
this._signalSendTask = new import_async.DeferredTask(this._ctx, async () => {
|
|
232
|
+
await this._flushSignalBuffer();
|
|
233
|
+
});
|
|
234
|
+
this._signallingDelay = STARTING_SIGNALLING_DELAY;
|
|
235
|
+
import_log.log.trace("dxos.mesh.connection.construct", {
|
|
236
|
+
sessionId: this.sessionId,
|
|
237
|
+
topic: this.topic,
|
|
238
|
+
localPeerId: this.ownId,
|
|
239
|
+
remotePeerId: this.remoteId,
|
|
240
|
+
initiator: this.initiator
|
|
241
|
+
}, {
|
|
242
|
+
F: __dxlog_file,
|
|
243
|
+
L: 129,
|
|
244
|
+
S: this,
|
|
245
|
+
C: (f, a) => f(...a)
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
get state() {
|
|
249
|
+
return this._state;
|
|
250
|
+
}
|
|
251
|
+
get transport() {
|
|
252
|
+
return this._transport;
|
|
253
|
+
}
|
|
254
|
+
get protocol() {
|
|
255
|
+
return this._protocol;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Create an underlying transport and prepares it for the connection.
|
|
259
|
+
*/
|
|
260
|
+
async openConnection() {
|
|
261
|
+
(0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
|
|
262
|
+
F: __dxlog_file,
|
|
263
|
+
L: 154,
|
|
264
|
+
S: this,
|
|
265
|
+
A: [
|
|
266
|
+
"this._state === ConnectionState.INITIAL",
|
|
267
|
+
"'Invalid state.'"
|
|
268
|
+
]
|
|
269
|
+
});
|
|
270
|
+
import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
|
|
271
|
+
id: this._instanceId
|
|
272
|
+
}), {
|
|
273
|
+
F: __dxlog_file,
|
|
274
|
+
L: 155,
|
|
275
|
+
S: this,
|
|
276
|
+
C: (f, a) => f(...a)
|
|
277
|
+
});
|
|
278
|
+
import_log.log.trace("dxos.mesh.connection.open", {
|
|
279
|
+
sessionId: this.sessionId,
|
|
280
|
+
topic: this.topic,
|
|
281
|
+
localPeerId: this.ownId,
|
|
282
|
+
remotePeerId: this.remoteId,
|
|
283
|
+
initiator: this.initiator
|
|
284
|
+
}, {
|
|
285
|
+
F: __dxlog_file,
|
|
286
|
+
L: 156,
|
|
287
|
+
S: this,
|
|
288
|
+
C: (f, a) => f(...a)
|
|
289
|
+
});
|
|
290
|
+
this._changeState(ConnectionState.CONNECTING);
|
|
291
|
+
this._protocol.open().catch((err) => {
|
|
292
|
+
this.errors.raise(err);
|
|
293
|
+
});
|
|
294
|
+
this._protocol.stream.on("close", () => {
|
|
295
|
+
(0, import_log.log)("protocol stream closed", void 0, {
|
|
296
|
+
F: __dxlog_file,
|
|
297
|
+
L: 173,
|
|
298
|
+
S: this,
|
|
299
|
+
C: (f, a) => f(...a)
|
|
300
|
+
});
|
|
301
|
+
this.close(new import_protocols.ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
|
|
302
|
+
});
|
|
303
|
+
(0, import_async.scheduleTask)(this.connectedTimeoutContext, async () => {
|
|
304
|
+
import_log.log.info("timeout waiting for transport to connect, aborting", void 0, {
|
|
305
|
+
F: __dxlog_file,
|
|
306
|
+
L: 180,
|
|
307
|
+
S: this,
|
|
308
|
+
C: (f, a) => f(...a)
|
|
309
|
+
});
|
|
310
|
+
await this.abort().catch((err) => this.errors.raise(err));
|
|
311
|
+
}, TRANSPORT_CONNECTION_TIMEOUT);
|
|
312
|
+
(0, import_invariant.invariant)(!this._transport, void 0, {
|
|
313
|
+
F: __dxlog_file,
|
|
314
|
+
L: 186,
|
|
315
|
+
S: this,
|
|
316
|
+
A: [
|
|
317
|
+
"!this._transport",
|
|
318
|
+
""
|
|
319
|
+
]
|
|
320
|
+
});
|
|
321
|
+
this._transport = this._transportFactory.createTransport({
|
|
322
|
+
initiator: this.initiator,
|
|
323
|
+
stream: this._protocol.stream,
|
|
324
|
+
sendSignal: async (signal) => this._sendSignal(signal)
|
|
325
|
+
});
|
|
326
|
+
this._transport.connected.once(async () => {
|
|
327
|
+
this._changeState(ConnectionState.CONNECTED);
|
|
328
|
+
await this.connectedTimeoutContext.dispose();
|
|
329
|
+
this._callbacks?.onConnected?.();
|
|
330
|
+
});
|
|
331
|
+
this._transport.closed.once(() => {
|
|
332
|
+
this._transport = void 0;
|
|
333
|
+
(0, import_log.log)("abort triggered by transport close", void 0, {
|
|
334
|
+
F: __dxlog_file,
|
|
335
|
+
L: 201,
|
|
336
|
+
S: this,
|
|
337
|
+
C: (f, a) => f(...a)
|
|
338
|
+
});
|
|
339
|
+
this.abort().catch((err) => this.errors.raise(err));
|
|
340
|
+
});
|
|
341
|
+
this._transport.errors.handle(async (err) => {
|
|
342
|
+
(0, import_log.log)("transport error:", {
|
|
343
|
+
err
|
|
344
|
+
}, {
|
|
345
|
+
F: __dxlog_file,
|
|
346
|
+
L: 206,
|
|
347
|
+
S: this,
|
|
348
|
+
C: (f, a) => f(...a)
|
|
349
|
+
});
|
|
350
|
+
if (!this.closeReason) {
|
|
351
|
+
this.closeReason = err?.message;
|
|
352
|
+
}
|
|
353
|
+
if (err instanceof import_protocols.ConnectionResetError) {
|
|
354
|
+
import_log.log.info("aborting due to transport ConnectionResetError", void 0, {
|
|
355
|
+
F: __dxlog_file,
|
|
356
|
+
L: 213,
|
|
357
|
+
S: this,
|
|
358
|
+
C: (f, a) => f(...a)
|
|
359
|
+
});
|
|
360
|
+
this.abort().catch((err2) => this.errors.raise(err2));
|
|
361
|
+
} else if (err instanceof import_protocols.ConnectivityError) {
|
|
362
|
+
import_log.log.info("aborting due to transport ConnectivityError", void 0, {
|
|
363
|
+
F: __dxlog_file,
|
|
364
|
+
L: 216,
|
|
365
|
+
S: this,
|
|
366
|
+
C: (f, a) => f(...a)
|
|
367
|
+
});
|
|
368
|
+
this.abort().catch((err2) => this.errors.raise(err2));
|
|
369
|
+
} else if (err instanceof import_protocols.UnknownProtocolError) {
|
|
370
|
+
import_log.log.warn("unsure what to do with UnknownProtocolError, will keep on truckin", {
|
|
371
|
+
err
|
|
372
|
+
}, {
|
|
373
|
+
F: __dxlog_file,
|
|
374
|
+
L: 219,
|
|
375
|
+
S: this,
|
|
376
|
+
C: (f, a) => f(...a)
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
|
|
380
|
+
await this.connectedTimeoutContext.dispose();
|
|
381
|
+
this.errors.raise(err);
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
for (const signal of this._incomingSignalBuffer) {
|
|
385
|
+
void this._transport.signal(signal);
|
|
386
|
+
}
|
|
387
|
+
this._incomingSignalBuffer = [];
|
|
388
|
+
import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.end({
|
|
389
|
+
id: this._instanceId
|
|
390
|
+
}), {
|
|
391
|
+
F: __dxlog_file,
|
|
392
|
+
L: 235,
|
|
393
|
+
S: this,
|
|
394
|
+
C: (f, a) => f(...a)
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
async abort(err) {
|
|
398
|
+
(0, import_log.log)("aborting...", {
|
|
399
|
+
err
|
|
400
|
+
}, {
|
|
401
|
+
F: __dxlog_file,
|
|
402
|
+
L: 242,
|
|
403
|
+
S: this,
|
|
404
|
+
C: (f, a) => f(...a)
|
|
405
|
+
});
|
|
406
|
+
if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
|
|
407
|
+
(0, import_log.log)(`abort ignored: already ${this._state}`, this.closeReason, {
|
|
408
|
+
F: __dxlog_file,
|
|
409
|
+
L: 244,
|
|
410
|
+
S: this,
|
|
411
|
+
C: (f, a) => f(...a)
|
|
412
|
+
});
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
await this.connectedTimeoutContext.dispose();
|
|
416
|
+
this._changeState(ConnectionState.ABORTING);
|
|
417
|
+
if (!this.closeReason) {
|
|
418
|
+
this.closeReason = err?.message;
|
|
419
|
+
}
|
|
420
|
+
await this._ctx.dispose();
|
|
421
|
+
try {
|
|
422
|
+
(0, import_log.log)("aborting protocol... ", void 0, {
|
|
423
|
+
F: __dxlog_file,
|
|
424
|
+
L: 258,
|
|
425
|
+
S: this,
|
|
426
|
+
C: (f, a) => f(...a)
|
|
427
|
+
});
|
|
428
|
+
await this._protocol.abort();
|
|
429
|
+
} catch (err2) {
|
|
430
|
+
import_log.log.catch(err2, void 0, {
|
|
431
|
+
F: __dxlog_file,
|
|
432
|
+
L: 261,
|
|
433
|
+
S: this,
|
|
434
|
+
C: (f, a) => f(...a)
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
try {
|
|
438
|
+
await this._transport?.destroy();
|
|
439
|
+
} catch (err2) {
|
|
440
|
+
import_log.log.catch(err2, void 0, {
|
|
441
|
+
F: __dxlog_file,
|
|
442
|
+
L: 268,
|
|
443
|
+
S: this,
|
|
444
|
+
C: (f, a) => f(...a)
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
try {
|
|
448
|
+
this._callbacks?.onClosed?.(err);
|
|
449
|
+
} catch (err2) {
|
|
450
|
+
import_log.log.catch(err2, void 0, {
|
|
451
|
+
F: __dxlog_file,
|
|
452
|
+
L: 274,
|
|
453
|
+
S: this,
|
|
454
|
+
C: (f, a) => f(...a)
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
this._changeState(ConnectionState.ABORTED);
|
|
458
|
+
}
|
|
459
|
+
async close(err) {
|
|
460
|
+
if (!this.closeReason) {
|
|
461
|
+
this.closeReason = err?.message;
|
|
462
|
+
} else {
|
|
463
|
+
this.closeReason += `; ${err?.message}`;
|
|
464
|
+
}
|
|
465
|
+
if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTING || this._state === ConnectionState.ABORTED) {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
const lastState = this._state;
|
|
469
|
+
this._changeState(ConnectionState.CLOSING);
|
|
470
|
+
await this.connectedTimeoutContext.dispose();
|
|
471
|
+
await this._ctx.dispose();
|
|
472
|
+
(0, import_log.log)("closing...", {
|
|
473
|
+
peerId: this.ownId
|
|
474
|
+
}, {
|
|
475
|
+
F: __dxlog_file,
|
|
476
|
+
L: 299,
|
|
477
|
+
S: this,
|
|
478
|
+
C: (f, a) => f(...a)
|
|
479
|
+
});
|
|
480
|
+
if (lastState === ConnectionState.CONNECTED) {
|
|
481
|
+
try {
|
|
482
|
+
await this._protocol.close();
|
|
483
|
+
} catch (err2) {
|
|
484
|
+
import_log.log.catch(err2, void 0, {
|
|
485
|
+
F: __dxlog_file,
|
|
486
|
+
L: 306,
|
|
487
|
+
S: this,
|
|
488
|
+
C: (f, a) => f(...a)
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
try {
|
|
492
|
+
await this._transport?.destroy();
|
|
493
|
+
} catch (err2) {
|
|
494
|
+
import_log.log.catch(err2, void 0, {
|
|
495
|
+
F: __dxlog_file,
|
|
496
|
+
L: 313,
|
|
497
|
+
S: this,
|
|
498
|
+
C: (f, a) => f(...a)
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
(0, import_log.log)("closed", {
|
|
503
|
+
peerId: this.ownId
|
|
504
|
+
}, {
|
|
505
|
+
F: __dxlog_file,
|
|
506
|
+
L: 317,
|
|
507
|
+
S: this,
|
|
508
|
+
C: (f, a) => f(...a)
|
|
509
|
+
});
|
|
510
|
+
this._changeState(ConnectionState.CLOSED);
|
|
511
|
+
this._callbacks?.onClosed?.(err);
|
|
512
|
+
}
|
|
513
|
+
_sendSignal(signal) {
|
|
514
|
+
this._outgoingSignalBuffer.push(signal);
|
|
515
|
+
this._signalSendTask.schedule();
|
|
516
|
+
}
|
|
517
|
+
async _flushSignalBuffer() {
|
|
518
|
+
if (this._outgoingSignalBuffer.length === 0) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
try {
|
|
522
|
+
if (process.env.NODE_ENV !== "test") {
|
|
523
|
+
await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
|
|
524
|
+
this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
|
|
525
|
+
}
|
|
526
|
+
const signals = [
|
|
527
|
+
...this._outgoingSignalBuffer
|
|
528
|
+
];
|
|
529
|
+
this._outgoingSignalBuffer.length = 0;
|
|
530
|
+
await this._signalMessaging.signal({
|
|
531
|
+
author: this.ownId,
|
|
532
|
+
recipient: this.remoteId,
|
|
533
|
+
sessionId: this.sessionId,
|
|
534
|
+
topic: this.topic,
|
|
535
|
+
data: {
|
|
536
|
+
signalBatch: {
|
|
537
|
+
signals
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
} catch (err) {
|
|
542
|
+
if (err instanceof import_protocols.CancelledError || err instanceof Error && err.message?.includes("CANCELLED")) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
import_log.log.info("signal message failed to deliver", {
|
|
546
|
+
err
|
|
547
|
+
}, {
|
|
548
|
+
F: __dxlog_file,
|
|
549
|
+
L: 355,
|
|
550
|
+
S: this,
|
|
551
|
+
C: (f, a) => f(...a)
|
|
552
|
+
});
|
|
553
|
+
await this.close(new import_protocols.ConnectivityError("signal message failed to deliver", err));
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Receive a signal from the remote peer.
|
|
558
|
+
*/
|
|
559
|
+
async signal(msg) {
|
|
560
|
+
(0, import_invariant.invariant)(msg.sessionId, void 0, {
|
|
561
|
+
F: __dxlog_file,
|
|
562
|
+
L: 364,
|
|
563
|
+
S: this,
|
|
564
|
+
A: [
|
|
565
|
+
"msg.sessionId",
|
|
566
|
+
""
|
|
567
|
+
]
|
|
568
|
+
});
|
|
569
|
+
if (!msg.sessionId.equals(this.sessionId)) {
|
|
570
|
+
(0, import_log.log)("dropping signal for incorrect session id", void 0, {
|
|
571
|
+
F: __dxlog_file,
|
|
572
|
+
L: 366,
|
|
573
|
+
S: this,
|
|
574
|
+
C: (f, a) => f(...a)
|
|
575
|
+
});
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
(0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
|
|
579
|
+
F: __dxlog_file,
|
|
580
|
+
L: 369,
|
|
581
|
+
S: this,
|
|
582
|
+
A: [
|
|
583
|
+
"msg.data.signal || msg.data.signalBatch",
|
|
584
|
+
""
|
|
585
|
+
]
|
|
586
|
+
});
|
|
587
|
+
(0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
|
|
588
|
+
F: __dxlog_file,
|
|
589
|
+
L: 370,
|
|
590
|
+
S: this,
|
|
591
|
+
A: [
|
|
592
|
+
"msg.author?.equals(this.remoteId)",
|
|
593
|
+
""
|
|
594
|
+
]
|
|
595
|
+
});
|
|
596
|
+
(0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
|
|
597
|
+
F: __dxlog_file,
|
|
598
|
+
L: 371,
|
|
599
|
+
S: this,
|
|
600
|
+
A: [
|
|
601
|
+
"msg.recipient?.equals(this.ownId)",
|
|
602
|
+
""
|
|
603
|
+
]
|
|
604
|
+
});
|
|
605
|
+
const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
|
|
606
|
+
msg.data.signal
|
|
607
|
+
];
|
|
608
|
+
for (const signal of signals) {
|
|
609
|
+
if (!signal) {
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if ([
|
|
613
|
+
ConnectionState.CREATED,
|
|
614
|
+
ConnectionState.INITIAL
|
|
615
|
+
].includes(this.state)) {
|
|
616
|
+
(0, import_log.log)("buffered signal", {
|
|
617
|
+
peerId: this.ownId,
|
|
618
|
+
remoteId: this.remoteId,
|
|
619
|
+
msg: msg.data
|
|
620
|
+
}, {
|
|
621
|
+
F: __dxlog_file,
|
|
622
|
+
L: 380,
|
|
623
|
+
S: this,
|
|
624
|
+
C: (f, a) => f(...a)
|
|
625
|
+
});
|
|
626
|
+
this._incomingSignalBuffer.push(signal);
|
|
627
|
+
} else {
|
|
628
|
+
(0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
|
|
629
|
+
F: __dxlog_file,
|
|
630
|
+
L: 383,
|
|
631
|
+
S: this,
|
|
632
|
+
A: [
|
|
633
|
+
"this._transport",
|
|
634
|
+
"'Connection not ready to accept signals.'"
|
|
635
|
+
]
|
|
636
|
+
});
|
|
637
|
+
(0, import_log.log)("received signal", {
|
|
638
|
+
peerId: this.ownId,
|
|
639
|
+
remoteId: this.remoteId,
|
|
640
|
+
msg: msg.data
|
|
641
|
+
}, {
|
|
642
|
+
F: __dxlog_file,
|
|
643
|
+
L: 384,
|
|
644
|
+
S: this,
|
|
645
|
+
C: (f, a) => f(...a)
|
|
646
|
+
});
|
|
647
|
+
await this._transport.signal(signal);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
initiate() {
|
|
652
|
+
this._changeState(ConnectionState.INITIAL);
|
|
653
|
+
}
|
|
654
|
+
_changeState(state) {
|
|
655
|
+
(0, import_log.log)("stateChanged", {
|
|
656
|
+
from: this._state,
|
|
657
|
+
to: state,
|
|
658
|
+
peerId: this.ownId
|
|
659
|
+
}, {
|
|
660
|
+
F: __dxlog_file,
|
|
661
|
+
L: 395,
|
|
662
|
+
S: this,
|
|
663
|
+
C: (f, a) => f(...a)
|
|
664
|
+
});
|
|
665
|
+
(0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
|
|
666
|
+
F: __dxlog_file,
|
|
667
|
+
L: 396,
|
|
668
|
+
S: this,
|
|
669
|
+
A: [
|
|
670
|
+
"state !== this._state",
|
|
671
|
+
"'Already in this state.'"
|
|
672
|
+
]
|
|
673
|
+
});
|
|
674
|
+
this._state = state;
|
|
675
|
+
this.stateChanged.emit(state);
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
_ts_decorate([
|
|
679
|
+
import_async.synchronized
|
|
680
|
+
], Connection.prototype, "abort", null);
|
|
681
|
+
_ts_decorate([
|
|
682
|
+
import_async.synchronized
|
|
683
|
+
], Connection.prototype, "close", null);
|
|
684
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
|
|
685
|
+
var SwarmMessage = import_protocols2.schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
|
|
686
|
+
var SwarmMessenger = class {
|
|
687
|
+
constructor({ sendMessage, onSignal, onOffer, topic }) {
|
|
688
|
+
this._ctx = new import_context2.Context();
|
|
689
|
+
this._offerRecords = new import_util.ComplexMap((key) => key.toHex());
|
|
690
|
+
this._sendMessage = sendMessage;
|
|
691
|
+
this._onSignal = onSignal;
|
|
692
|
+
this._onOffer = onOffer;
|
|
693
|
+
this._topic = topic;
|
|
694
|
+
}
|
|
695
|
+
async receiveMessage({ author, recipient, payload }) {
|
|
696
|
+
if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
const message = SwarmMessage.decode(payload.value);
|
|
700
|
+
if (!this._topic.equals(message.topic)) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
(0, import_log2.log)("received", {
|
|
704
|
+
from: author,
|
|
705
|
+
to: recipient,
|
|
706
|
+
msg: message
|
|
707
|
+
}, {
|
|
708
|
+
F: __dxlog_file2,
|
|
709
|
+
L: 69,
|
|
710
|
+
S: this,
|
|
711
|
+
C: (f, a) => f(...a)
|
|
712
|
+
});
|
|
713
|
+
if (message.data?.offer) {
|
|
714
|
+
await this._handleOffer({
|
|
715
|
+
author,
|
|
716
|
+
recipient,
|
|
717
|
+
message
|
|
718
|
+
});
|
|
719
|
+
} else if (message.data?.answer) {
|
|
720
|
+
await this._resolveAnswers(message);
|
|
721
|
+
} else if (message.data?.signal) {
|
|
722
|
+
await this._handleSignal({
|
|
723
|
+
author,
|
|
724
|
+
recipient,
|
|
725
|
+
message
|
|
726
|
+
});
|
|
727
|
+
} else if (message.data?.signalBatch) {
|
|
728
|
+
await this._handleSignal({
|
|
729
|
+
author,
|
|
730
|
+
recipient,
|
|
731
|
+
message
|
|
732
|
+
});
|
|
733
|
+
} else {
|
|
734
|
+
import_log2.log.warn("unknown message", {
|
|
735
|
+
message
|
|
736
|
+
}, {
|
|
737
|
+
F: __dxlog_file2,
|
|
738
|
+
L: 80,
|
|
739
|
+
S: this,
|
|
740
|
+
C: (f, a) => f(...a)
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
async signal(message) {
|
|
745
|
+
(0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
|
|
746
|
+
F: __dxlog_file2,
|
|
747
|
+
L: 85,
|
|
748
|
+
S: this,
|
|
749
|
+
A: [
|
|
750
|
+
"message.data?.signal || message.data?.signalBatch",
|
|
751
|
+
"'Invalid message'"
|
|
752
|
+
]
|
|
753
|
+
});
|
|
754
|
+
await this._sendReliableMessage({
|
|
755
|
+
author: message.author,
|
|
756
|
+
recipient: message.recipient,
|
|
757
|
+
message
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
async offer(message) {
|
|
761
|
+
const networkMessage = {
|
|
762
|
+
...message,
|
|
763
|
+
messageId: import_keys2.PublicKey.random()
|
|
764
|
+
};
|
|
765
|
+
return new Promise((resolve, reject) => {
|
|
766
|
+
this._offerRecords.set(networkMessage.messageId, {
|
|
767
|
+
resolve
|
|
768
|
+
});
|
|
769
|
+
this._sendReliableMessage({
|
|
770
|
+
author: message.author,
|
|
771
|
+
recipient: message.recipient,
|
|
772
|
+
message: networkMessage
|
|
773
|
+
}).catch((err) => reject(err));
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
async _sendReliableMessage({ author, recipient, message }) {
|
|
777
|
+
const networkMessage = {
|
|
778
|
+
...message,
|
|
779
|
+
// Setting unique message_id if it not specified yet.
|
|
780
|
+
messageId: message.messageId ?? import_keys2.PublicKey.random()
|
|
781
|
+
};
|
|
782
|
+
(0, import_log2.log)("sending", {
|
|
783
|
+
from: author,
|
|
784
|
+
to: recipient,
|
|
785
|
+
msg: networkMessage
|
|
786
|
+
}, {
|
|
787
|
+
F: __dxlog_file2,
|
|
788
|
+
L: 123,
|
|
789
|
+
S: this,
|
|
790
|
+
C: (f, a) => f(...a)
|
|
791
|
+
});
|
|
792
|
+
await this._sendMessage({
|
|
793
|
+
author,
|
|
794
|
+
recipient,
|
|
795
|
+
payload: {
|
|
796
|
+
type_url: "dxos.mesh.swarm.SwarmMessage",
|
|
797
|
+
value: SwarmMessage.encode(networkMessage)
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
async _resolveAnswers(message) {
|
|
802
|
+
(0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
|
|
803
|
+
F: __dxlog_file2,
|
|
804
|
+
L: 135,
|
|
805
|
+
S: this,
|
|
806
|
+
A: [
|
|
807
|
+
"message.data?.answer?.offerMessageId",
|
|
808
|
+
"'No offerMessageId'"
|
|
809
|
+
]
|
|
810
|
+
});
|
|
811
|
+
const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
|
|
812
|
+
if (offerRecord) {
|
|
813
|
+
this._offerRecords.delete(message.data.answer.offerMessageId);
|
|
814
|
+
(0, import_invariant2.invariant)(message.data?.answer, "No answer", {
|
|
815
|
+
F: __dxlog_file2,
|
|
816
|
+
L: 139,
|
|
817
|
+
S: this,
|
|
818
|
+
A: [
|
|
819
|
+
"message.data?.answer",
|
|
820
|
+
"'No answer'"
|
|
821
|
+
]
|
|
822
|
+
});
|
|
823
|
+
(0, import_log2.log)("resolving", {
|
|
824
|
+
answer: message.data.answer
|
|
825
|
+
}, {
|
|
826
|
+
F: __dxlog_file2,
|
|
827
|
+
L: 140,
|
|
828
|
+
S: this,
|
|
829
|
+
C: (f, a) => f(...a)
|
|
830
|
+
});
|
|
831
|
+
offerRecord.resolve(message.data.answer);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
async _handleOffer({ author, recipient, message }) {
|
|
835
|
+
(0, import_invariant2.invariant)(message.data.offer, "No offer", {
|
|
836
|
+
F: __dxlog_file2,
|
|
837
|
+
L: 154,
|
|
838
|
+
S: this,
|
|
839
|
+
A: [
|
|
840
|
+
"message.data.offer",
|
|
841
|
+
"'No offer'"
|
|
842
|
+
]
|
|
843
|
+
});
|
|
844
|
+
const offerMessage = {
|
|
845
|
+
author,
|
|
846
|
+
recipient,
|
|
847
|
+
...message,
|
|
848
|
+
data: {
|
|
849
|
+
offer: message.data.offer
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
const answer = await this._onOffer(offerMessage);
|
|
853
|
+
answer.offerMessageId = message.messageId;
|
|
854
|
+
try {
|
|
855
|
+
await this._sendReliableMessage({
|
|
856
|
+
author: recipient,
|
|
857
|
+
recipient: author,
|
|
858
|
+
message: {
|
|
859
|
+
topic: message.topic,
|
|
860
|
+
sessionId: message.sessionId,
|
|
861
|
+
data: {
|
|
862
|
+
answer
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
} catch (err) {
|
|
867
|
+
if (err instanceof import_protocols2.TimeoutError) {
|
|
868
|
+
import_log2.log.info("timeout sending answer to offer", {
|
|
869
|
+
err
|
|
870
|
+
}, {
|
|
871
|
+
F: __dxlog_file2,
|
|
872
|
+
L: 175,
|
|
873
|
+
S: this,
|
|
874
|
+
C: (f, a) => f(...a)
|
|
875
|
+
});
|
|
876
|
+
} else {
|
|
877
|
+
import_log2.log.info("error sending answer to offer", {
|
|
878
|
+
err
|
|
879
|
+
}, {
|
|
880
|
+
F: __dxlog_file2,
|
|
881
|
+
L: 177,
|
|
882
|
+
S: this,
|
|
883
|
+
C: (f, a) => f(...a)
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
async _handleSignal({ author, recipient, message }) {
|
|
889
|
+
(0, import_invariant2.invariant)(message.messageId, void 0, {
|
|
890
|
+
F: __dxlog_file2,
|
|
891
|
+
L: 191,
|
|
892
|
+
S: this,
|
|
893
|
+
A: [
|
|
894
|
+
"message.messageId",
|
|
895
|
+
""
|
|
896
|
+
]
|
|
897
|
+
});
|
|
898
|
+
(0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
|
|
899
|
+
F: __dxlog_file2,
|
|
900
|
+
L: 192,
|
|
901
|
+
S: this,
|
|
902
|
+
A: [
|
|
903
|
+
"message.data.signal || message.data.signalBatch",
|
|
904
|
+
"'Invalid message'"
|
|
905
|
+
]
|
|
906
|
+
});
|
|
907
|
+
const signalMessage = {
|
|
908
|
+
author,
|
|
909
|
+
recipient,
|
|
910
|
+
...message,
|
|
911
|
+
data: {
|
|
912
|
+
signal: message.data.signal,
|
|
913
|
+
signalBatch: message.data.signalBatch
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
await this._onSignal(signalMessage);
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
920
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
921
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
922
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
923
|
+
else
|
|
924
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
925
|
+
if (d = decorators[i])
|
|
926
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
927
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
928
|
+
}
|
|
929
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
|
|
930
|
+
var ConnectionDisplacedError = class extends import_protocols4.SystemError {
|
|
931
|
+
constructor() {
|
|
932
|
+
super("Connection displaced by remote initiator.");
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
|
|
936
|
+
var Peer = class {
|
|
937
|
+
// TODO(burdon): Convert to map?
|
|
938
|
+
constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
|
|
939
|
+
this.id = id;
|
|
940
|
+
this.topic = topic;
|
|
941
|
+
this.localPeerId = localPeerId;
|
|
942
|
+
this._signalMessaging = _signalMessaging;
|
|
943
|
+
this._protocolProvider = _protocolProvider;
|
|
944
|
+
this._transportFactory = _transportFactory;
|
|
945
|
+
this._connectionLimiter = _connectionLimiter;
|
|
946
|
+
this._callbacks = _callbacks;
|
|
947
|
+
this._availableAfter = 0;
|
|
948
|
+
this.availableToConnect = true;
|
|
949
|
+
this._ctx = new import_context4.Context();
|
|
950
|
+
this.advertizing = false;
|
|
951
|
+
this.initiating = false;
|
|
952
|
+
this.connectionDisplaced = new import_async3.Event();
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Respond to remote offer.
|
|
956
|
+
*/
|
|
957
|
+
async onOffer(message) {
|
|
958
|
+
const remoteId = message.author;
|
|
959
|
+
if (this.connection && ![
|
|
960
|
+
ConnectionState.CREATED,
|
|
961
|
+
ConnectionState.INITIAL,
|
|
962
|
+
ConnectionState.CONNECTING
|
|
963
|
+
].includes(this.connection.state)) {
|
|
964
|
+
import_log4.log.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
|
|
965
|
+
F: __dxlog_file3,
|
|
966
|
+
L: 115,
|
|
967
|
+
S: this,
|
|
968
|
+
C: (f, a) => f(...a)
|
|
969
|
+
});
|
|
970
|
+
return {
|
|
971
|
+
accept: false
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
if (this.connection || this.initiating) {
|
|
975
|
+
if (remoteId.toHex() < this.localPeerId.toHex()) {
|
|
976
|
+
(0, import_log4.log)("close local connection", {
|
|
977
|
+
localPeerId: this.id,
|
|
978
|
+
topic: this.topic,
|
|
979
|
+
remotePeerId: this.localPeerId,
|
|
980
|
+
sessionId: this.connection?.sessionId
|
|
981
|
+
}, {
|
|
982
|
+
F: __dxlog_file3,
|
|
983
|
+
L: 124,
|
|
984
|
+
S: this,
|
|
985
|
+
C: (f, a) => f(...a)
|
|
986
|
+
});
|
|
987
|
+
if (this.connection) {
|
|
988
|
+
await this.closeConnection(new ConnectionDisplacedError());
|
|
989
|
+
}
|
|
990
|
+
} else {
|
|
991
|
+
return {
|
|
992
|
+
accept: false
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
if (await this._callbacks.onOffer(remoteId)) {
|
|
997
|
+
if (!this.connection) {
|
|
998
|
+
(0, import_invariant4.invariant)(message.sessionId, void 0, {
|
|
999
|
+
F: __dxlog_file3,
|
|
1000
|
+
L: 144,
|
|
1001
|
+
S: this,
|
|
1002
|
+
A: [
|
|
1003
|
+
"message.sessionId",
|
|
1004
|
+
""
|
|
1005
|
+
]
|
|
1006
|
+
});
|
|
1007
|
+
const connection = this._createConnection(false, message.sessionId);
|
|
1008
|
+
try {
|
|
1009
|
+
await this._connectionLimiter.connecting(message.sessionId);
|
|
1010
|
+
connection.initiate();
|
|
1011
|
+
await connection.openConnection();
|
|
1012
|
+
} catch (err) {
|
|
1013
|
+
if (!(err instanceof import_protocols4.CancelledError)) {
|
|
1014
|
+
import_log4.log.info("connection error", {
|
|
1015
|
+
topic: this.topic,
|
|
1016
|
+
peerId: this.localPeerId,
|
|
1017
|
+
remoteId: this.id,
|
|
1018
|
+
err
|
|
1019
|
+
}, {
|
|
1020
|
+
F: __dxlog_file3,
|
|
1021
|
+
L: 154,
|
|
1022
|
+
S: this,
|
|
1023
|
+
C: (f, a) => f(...a)
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
await this.closeConnection(err);
|
|
1027
|
+
}
|
|
1028
|
+
return {
|
|
1029
|
+
accept: true
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return {
|
|
1034
|
+
accept: false
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Initiate a connection to the remote peer.
|
|
1039
|
+
*/
|
|
1040
|
+
async initiateConnection() {
|
|
1041
|
+
(0, import_invariant4.invariant)(!this.initiating, "Initiation in progress.", {
|
|
1042
|
+
F: __dxlog_file3,
|
|
1043
|
+
L: 171,
|
|
1044
|
+
S: this,
|
|
1045
|
+
A: [
|
|
1046
|
+
"!this.initiating",
|
|
1047
|
+
"'Initiation in progress.'"
|
|
1048
|
+
]
|
|
1049
|
+
});
|
|
1050
|
+
(0, import_invariant4.invariant)(!this.connection, "Already connected.", {
|
|
1051
|
+
F: __dxlog_file3,
|
|
1052
|
+
L: 172,
|
|
1053
|
+
S: this,
|
|
1054
|
+
A: [
|
|
1055
|
+
"!this.connection",
|
|
1056
|
+
"'Already connected.'"
|
|
1057
|
+
]
|
|
1058
|
+
});
|
|
1059
|
+
const sessionId = import_keys4.PublicKey.random();
|
|
1060
|
+
(0, import_log4.log)("initiating...", {
|
|
1061
|
+
id: this.id,
|
|
1062
|
+
topic: this.topic,
|
|
1063
|
+
peerId: this.id,
|
|
1064
|
+
sessionId
|
|
1065
|
+
}, {
|
|
1066
|
+
F: __dxlog_file3,
|
|
1067
|
+
L: 174,
|
|
1068
|
+
S: this,
|
|
1069
|
+
C: (f, a) => f(...a)
|
|
1070
|
+
});
|
|
1071
|
+
const connection = this._createConnection(true, sessionId);
|
|
1072
|
+
this.initiating = true;
|
|
1073
|
+
let answer;
|
|
1074
|
+
try {
|
|
1075
|
+
await this._connectionLimiter.connecting(sessionId);
|
|
1076
|
+
connection.initiate();
|
|
1077
|
+
answer = await this._signalMessaging.offer({
|
|
1078
|
+
author: this.localPeerId,
|
|
1079
|
+
recipient: this.id,
|
|
1080
|
+
sessionId,
|
|
1081
|
+
topic: this.topic,
|
|
1082
|
+
data: {
|
|
1083
|
+
offer: {}
|
|
1084
|
+
}
|
|
1085
|
+
});
|
|
1086
|
+
(0, import_log4.log)("received", {
|
|
1087
|
+
answer,
|
|
1088
|
+
topic: this.topic,
|
|
1089
|
+
ownId: this.localPeerId,
|
|
1090
|
+
remoteId: this.id
|
|
1091
|
+
}, {
|
|
1092
|
+
F: __dxlog_file3,
|
|
1093
|
+
L: 191,
|
|
1094
|
+
S: this,
|
|
1095
|
+
C: (f, a) => f(...a)
|
|
1096
|
+
});
|
|
1097
|
+
if (connection.state !== ConnectionState.INITIAL) {
|
|
1098
|
+
(0, import_log4.log)("ignoring response", void 0, {
|
|
1099
|
+
F: __dxlog_file3,
|
|
1100
|
+
L: 193,
|
|
1101
|
+
S: this,
|
|
1102
|
+
C: (f, a) => f(...a)
|
|
1103
|
+
});
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
} catch (err) {
|
|
1107
|
+
(0, import_log4.log)("initiation error: send offer", {
|
|
1108
|
+
err,
|
|
1109
|
+
topic: this.topic,
|
|
1110
|
+
peerId: this.localPeerId,
|
|
1111
|
+
remoteId: this.id
|
|
1112
|
+
}, {
|
|
1113
|
+
F: __dxlog_file3,
|
|
1114
|
+
L: 197,
|
|
1115
|
+
S: this,
|
|
1116
|
+
C: (f, a) => f(...a)
|
|
1117
|
+
});
|
|
1118
|
+
await connection.abort(err);
|
|
1119
|
+
throw err;
|
|
1120
|
+
} finally {
|
|
1121
|
+
this.initiating = false;
|
|
1122
|
+
}
|
|
1123
|
+
try {
|
|
1124
|
+
if (!answer.accept) {
|
|
1125
|
+
this._callbacks.onRejected();
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
} catch (err) {
|
|
1129
|
+
(0, import_log4.log)("initiation error: accept answer", {
|
|
1130
|
+
err,
|
|
1131
|
+
topic: this.topic,
|
|
1132
|
+
peerId: this.localPeerId,
|
|
1133
|
+
remoteId: this.id
|
|
1134
|
+
}, {
|
|
1135
|
+
F: __dxlog_file3,
|
|
1136
|
+
L: 210,
|
|
1137
|
+
S: this,
|
|
1138
|
+
C: (f, a) => f(...a)
|
|
1139
|
+
});
|
|
1140
|
+
await connection.abort(err);
|
|
1141
|
+
throw err;
|
|
1142
|
+
} finally {
|
|
1143
|
+
this.initiating = false;
|
|
1144
|
+
}
|
|
1145
|
+
try {
|
|
1146
|
+
(0, import_log4.log)("opening connection as initiator", void 0, {
|
|
1147
|
+
F: __dxlog_file3,
|
|
1148
|
+
L: 223,
|
|
1149
|
+
S: this,
|
|
1150
|
+
C: (f, a) => f(...a)
|
|
1151
|
+
});
|
|
1152
|
+
await connection.openConnection();
|
|
1153
|
+
this._callbacks.onAccepted();
|
|
1154
|
+
} catch (err) {
|
|
1155
|
+
(0, import_log4.log)("initiation error: open connection", {
|
|
1156
|
+
err,
|
|
1157
|
+
topic: this.topic,
|
|
1158
|
+
peerId: this.localPeerId,
|
|
1159
|
+
remoteId: this.id
|
|
1160
|
+
}, {
|
|
1161
|
+
F: __dxlog_file3,
|
|
1162
|
+
L: 227,
|
|
1163
|
+
S: this,
|
|
1164
|
+
C: (f, a) => f(...a)
|
|
1165
|
+
});
|
|
1166
|
+
import_log4.log.warn("closing connection due to unhandled error on openConnection", {
|
|
1167
|
+
err
|
|
1168
|
+
}, {
|
|
1169
|
+
F: __dxlog_file3,
|
|
1170
|
+
L: 234,
|
|
1171
|
+
S: this,
|
|
1172
|
+
C: (f, a) => f(...a)
|
|
1173
|
+
});
|
|
1174
|
+
await this.closeConnection(err);
|
|
1175
|
+
throw err;
|
|
1176
|
+
} finally {
|
|
1177
|
+
this.initiating = false;
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Create new connection.
|
|
1182
|
+
* Either we're initiating a connection or creating one in response to an offer from the other peer.
|
|
1183
|
+
*/
|
|
1184
|
+
_createConnection(initiator, sessionId) {
|
|
1185
|
+
(0, import_log4.log)("creating connection", {
|
|
1186
|
+
topic: this.topic,
|
|
1187
|
+
peerId: this.localPeerId,
|
|
1188
|
+
remoteId: this.id,
|
|
1189
|
+
initiator,
|
|
1190
|
+
sessionId
|
|
1191
|
+
}, {
|
|
1192
|
+
F: __dxlog_file3,
|
|
1193
|
+
L: 248,
|
|
1194
|
+
S: this,
|
|
1195
|
+
C: (f, a) => f(...a)
|
|
1196
|
+
});
|
|
1197
|
+
(0, import_invariant4.invariant)(!this.connection, "Already connected.", {
|
|
1198
|
+
F: __dxlog_file3,
|
|
1199
|
+
L: 255,
|
|
1200
|
+
S: this,
|
|
1201
|
+
A: [
|
|
1202
|
+
"!this.connection",
|
|
1203
|
+
"'Already connected.'"
|
|
1204
|
+
]
|
|
1205
|
+
});
|
|
1206
|
+
const connection = new Connection(
|
|
1207
|
+
this.topic,
|
|
1208
|
+
this.localPeerId,
|
|
1209
|
+
this.id,
|
|
1210
|
+
sessionId,
|
|
1211
|
+
initiator,
|
|
1212
|
+
this._signalMessaging,
|
|
1213
|
+
// TODO(dmaretskyi): Init only when connection is established.
|
|
1214
|
+
this._protocolProvider({
|
|
1215
|
+
initiator,
|
|
1216
|
+
localPeerId: this.localPeerId,
|
|
1217
|
+
remotePeerId: this.id,
|
|
1218
|
+
topic: this.topic
|
|
1219
|
+
}),
|
|
1220
|
+
this._transportFactory,
|
|
1221
|
+
{
|
|
1222
|
+
onConnected: () => {
|
|
1223
|
+
this.availableToConnect = true;
|
|
1224
|
+
this._lastConnectionTime = Date.now();
|
|
1225
|
+
this._callbacks.onConnected();
|
|
1226
|
+
this._connectionLimiter.doneConnecting(sessionId);
|
|
1227
|
+
import_log4.log.trace("dxos.mesh.connection.connected", {
|
|
1228
|
+
topic: this.topic,
|
|
1229
|
+
localPeerId: this.localPeerId,
|
|
1230
|
+
remotePeerId: this.id,
|
|
1231
|
+
sessionId,
|
|
1232
|
+
initiator
|
|
1233
|
+
}, {
|
|
1234
|
+
F: __dxlog_file3,
|
|
1235
|
+
L: 274,
|
|
1236
|
+
S: this,
|
|
1237
|
+
C: (f, a) => f(...a)
|
|
1238
|
+
});
|
|
1239
|
+
},
|
|
1240
|
+
onClosed: (err) => {
|
|
1241
|
+
(0, import_log4.log)("connection closed", {
|
|
1242
|
+
topic: this.topic,
|
|
1243
|
+
peerId: this.localPeerId,
|
|
1244
|
+
remoteId: this.id,
|
|
1245
|
+
initiator
|
|
1246
|
+
}, {
|
|
1247
|
+
F: __dxlog_file3,
|
|
1248
|
+
L: 283,
|
|
1249
|
+
S: this,
|
|
1250
|
+
C: (f, a) => f(...a)
|
|
1251
|
+
});
|
|
1252
|
+
this._connectionLimiter.doneConnecting(sessionId);
|
|
1253
|
+
(0, import_invariant4.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
|
|
1254
|
+
F: __dxlog_file3,
|
|
1255
|
+
L: 288,
|
|
1256
|
+
S: this,
|
|
1257
|
+
A: [
|
|
1258
|
+
"this.connection === connection",
|
|
1259
|
+
"'Connection mismatch (race condition).'"
|
|
1260
|
+
]
|
|
1261
|
+
});
|
|
1262
|
+
import_log4.log.trace("dxos.mesh.connection.closed", {
|
|
1263
|
+
topic: this.topic,
|
|
1264
|
+
localPeerId: this.localPeerId,
|
|
1265
|
+
remotePeerId: this.id,
|
|
1266
|
+
sessionId,
|
|
1267
|
+
initiator
|
|
1268
|
+
}, {
|
|
1269
|
+
F: __dxlog_file3,
|
|
1270
|
+
L: 290,
|
|
1271
|
+
S: this,
|
|
1272
|
+
C: (f, a) => f(...a)
|
|
1273
|
+
});
|
|
1274
|
+
if (err instanceof ConnectionDisplacedError) {
|
|
1275
|
+
this.connectionDisplaced.emit(this.connection);
|
|
1276
|
+
} else {
|
|
1277
|
+
if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
|
|
1278
|
+
this._availableAfter = 0;
|
|
1279
|
+
} else {
|
|
1280
|
+
this.availableToConnect = false;
|
|
1281
|
+
this._availableAfter = increaseInterval(this._availableAfter);
|
|
1282
|
+
}
|
|
1283
|
+
this._callbacks.onDisconnected();
|
|
1284
|
+
(0, import_async3.scheduleTask)(this._connectionCtx, () => {
|
|
1285
|
+
this.availableToConnect = true;
|
|
1286
|
+
this._callbacks.onPeerAvailable();
|
|
1287
|
+
}, this._availableAfter);
|
|
1288
|
+
}
|
|
1289
|
+
this.connection = void 0;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
);
|
|
1293
|
+
this._callbacks.onInitiated(connection);
|
|
1294
|
+
void this._connectionCtx?.dispose();
|
|
1295
|
+
this._connectionCtx = this._ctx.derive();
|
|
1296
|
+
connection.errors.handle((err) => {
|
|
1297
|
+
import_log4.log.info("connection error, closing", {
|
|
1298
|
+
topic: this.topic,
|
|
1299
|
+
peerId: this.localPeerId,
|
|
1300
|
+
remoteId: this.id,
|
|
1301
|
+
initiator,
|
|
1302
|
+
err
|
|
1303
|
+
}, {
|
|
1304
|
+
F: __dxlog_file3,
|
|
1305
|
+
L: 330,
|
|
1306
|
+
S: this,
|
|
1307
|
+
C: (f, a) => f(...a)
|
|
1308
|
+
});
|
|
1309
|
+
import_log4.log.trace("dxos.mesh.connection.error", {
|
|
1310
|
+
topic: this.topic,
|
|
1311
|
+
localPeerId: this.localPeerId,
|
|
1312
|
+
remotePeerId: this.id,
|
|
1313
|
+
sessionId,
|
|
1314
|
+
initiator,
|
|
1315
|
+
err
|
|
1316
|
+
}, {
|
|
1317
|
+
F: __dxlog_file3,
|
|
1318
|
+
L: 337,
|
|
1319
|
+
S: this,
|
|
1320
|
+
C: (f, a) => f(...a)
|
|
1321
|
+
});
|
|
1322
|
+
void this.closeConnection(err);
|
|
1323
|
+
});
|
|
1324
|
+
this.connection = connection;
|
|
1325
|
+
return connection;
|
|
1326
|
+
}
|
|
1327
|
+
async closeConnection(err) {
|
|
1328
|
+
if (!this.connection) {
|
|
1329
|
+
return;
|
|
1330
|
+
}
|
|
1331
|
+
const connection = this.connection;
|
|
1332
|
+
(0, import_log4.log)("closing...", {
|
|
1333
|
+
peerId: this.id,
|
|
1334
|
+
sessionId: connection.sessionId
|
|
1335
|
+
}, {
|
|
1336
|
+
F: __dxlog_file3,
|
|
1337
|
+
L: 362,
|
|
1338
|
+
S: this,
|
|
1339
|
+
C: (f, a) => f(...a)
|
|
1340
|
+
});
|
|
1341
|
+
await connection.close(err);
|
|
1342
|
+
(0, import_log4.log)("closed", {
|
|
1343
|
+
peerId: this.id,
|
|
1344
|
+
sessionId: connection.sessionId
|
|
1345
|
+
}, {
|
|
1346
|
+
F: __dxlog_file3,
|
|
1347
|
+
L: 368,
|
|
1348
|
+
S: this,
|
|
1349
|
+
C: (f, a) => f(...a)
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
async onSignal(message) {
|
|
1353
|
+
if (!this.connection) {
|
|
1354
|
+
(0, import_log4.log)("dropping signal message for non-existent connection", {
|
|
1355
|
+
message
|
|
1356
|
+
}, {
|
|
1357
|
+
F: __dxlog_file3,
|
|
1358
|
+
L: 373,
|
|
1359
|
+
S: this,
|
|
1360
|
+
C: (f, a) => f(...a)
|
|
1361
|
+
});
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
await this.connection.signal(message);
|
|
1365
|
+
}
|
|
1366
|
+
async destroy(reason) {
|
|
1367
|
+
await this._ctx.dispose();
|
|
1368
|
+
(0, import_log4.log)("Destroying peer", {
|
|
1369
|
+
peerId: this.id,
|
|
1370
|
+
topic: this.topic
|
|
1371
|
+
}, {
|
|
1372
|
+
F: __dxlog_file3,
|
|
1373
|
+
L: 383,
|
|
1374
|
+
S: this,
|
|
1375
|
+
C: (f, a) => f(...a)
|
|
1376
|
+
});
|
|
1377
|
+
await this?.connection?.close(reason);
|
|
1378
|
+
}
|
|
1379
|
+
};
|
|
1380
|
+
_ts_decorate2([
|
|
1381
|
+
import_async3.synchronized
|
|
1382
|
+
], Peer.prototype, "destroy", null);
|
|
1383
|
+
var increaseInterval = (interval) => {
|
|
1384
|
+
if (interval === 0) {
|
|
1385
|
+
return 50;
|
|
1386
|
+
} else if (interval < 500) {
|
|
1387
|
+
return 500;
|
|
1388
|
+
} else if (interval < 1e3) {
|
|
1389
|
+
return 1e3;
|
|
1390
|
+
} else if (interval < 5e3) {
|
|
1391
|
+
return 5e3;
|
|
1392
|
+
}
|
|
1393
|
+
return 1e4;
|
|
1394
|
+
};
|
|
1395
|
+
function _ts_decorate3(decorators, target, key, desc) {
|
|
1396
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1397
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
1398
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
1399
|
+
else
|
|
1400
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
1401
|
+
if (d = decorators[i])
|
|
1402
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1403
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1404
|
+
}
|
|
1405
|
+
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm.ts";
|
|
1406
|
+
var INITIATION_DELAY = 100;
|
|
1407
|
+
var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
|
|
1408
|
+
var Swarm = class {
|
|
1409
|
+
// TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
|
|
1410
|
+
// TODO(burdon): Split up properties.
|
|
1411
|
+
constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter, _initiationDelay = INITIATION_DELAY) {
|
|
1412
|
+
this._topic = _topic;
|
|
1413
|
+
this._ownPeerId = _ownPeerId;
|
|
1414
|
+
this._topology = _topology;
|
|
1415
|
+
this._protocolProvider = _protocolProvider;
|
|
1416
|
+
this._messenger = _messenger;
|
|
1417
|
+
this._transportFactory = _transportFactory;
|
|
1418
|
+
this._label = _label;
|
|
1419
|
+
this._connectionLimiter = _connectionLimiter;
|
|
1420
|
+
this._initiationDelay = _initiationDelay;
|
|
1421
|
+
this._ctx = new import_context3.Context();
|
|
1422
|
+
this._listeningHandle = void 0;
|
|
1423
|
+
this._peers = new import_util2.ComplexMap(import_keys3.PublicKey.hash);
|
|
1424
|
+
this._instanceId = import_keys3.PublicKey.random().toHex();
|
|
1425
|
+
this.connectionAdded = new import_async2.Event();
|
|
1426
|
+
this.disconnected = new import_async2.Event();
|
|
1427
|
+
this.connected = new import_async2.Event();
|
|
1428
|
+
this.errors = new import_debug2.ErrorStream();
|
|
1429
|
+
import_log3.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.begin({
|
|
1430
|
+
id: this._instanceId,
|
|
1431
|
+
data: {
|
|
1432
|
+
topic: this._topic.toHex(),
|
|
1433
|
+
peerId: this._ownPeerId.toHex()
|
|
1434
|
+
}
|
|
1435
|
+
}), {
|
|
1436
|
+
F: __dxlog_file4,
|
|
1437
|
+
L: 88,
|
|
1438
|
+
S: this,
|
|
1439
|
+
C: (f, a) => f(...a)
|
|
1440
|
+
});
|
|
1441
|
+
(0, import_log3.log)("creating swarm", {
|
|
1442
|
+
peerId: _ownPeerId
|
|
1443
|
+
}, {
|
|
1444
|
+
F: __dxlog_file4,
|
|
1445
|
+
L: 92,
|
|
1446
|
+
S: this,
|
|
1447
|
+
C: (f, a) => f(...a)
|
|
1448
|
+
});
|
|
1449
|
+
_topology.init(this._getSwarmController());
|
|
1450
|
+
this._swarmMessenger = new SwarmMessenger({
|
|
1451
|
+
sendMessage: async (msg) => await this._messenger.sendMessage(msg),
|
|
1452
|
+
onSignal: async (msg) => await this.onSignal(msg),
|
|
1453
|
+
onOffer: async (msg) => await this.onOffer(msg),
|
|
1454
|
+
topic: this._topic
|
|
1455
|
+
});
|
|
1456
|
+
import_log3.log.trace("dxos.mesh.swarm.constructor", import_protocols3.trace.end({
|
|
1457
|
+
id: this._instanceId
|
|
1458
|
+
}), {
|
|
1459
|
+
F: __dxlog_file4,
|
|
1460
|
+
L: 101,
|
|
1461
|
+
S: this,
|
|
1462
|
+
C: (f, a) => f(...a)
|
|
1463
|
+
});
|
|
1464
|
+
}
|
|
1465
|
+
get connections() {
|
|
1466
|
+
return Array.from(this._peers.values()).map((peer) => peer.connection).filter(import_util2.isNotNullOrUndefined);
|
|
1467
|
+
}
|
|
1468
|
+
get ownPeerId() {
|
|
1469
|
+
return this._ownPeerId;
|
|
1470
|
+
}
|
|
1471
|
+
/**
|
|
1472
|
+
* Custom label assigned to this swarm. Used in devtools to display human-readable names for swarms.
|
|
1473
|
+
*/
|
|
1474
|
+
get label() {
|
|
1475
|
+
return this._label;
|
|
1476
|
+
}
|
|
1477
|
+
get topic() {
|
|
1478
|
+
return this._topic;
|
|
1479
|
+
}
|
|
1480
|
+
async open() {
|
|
1481
|
+
(0, import_invariant3.invariant)(!this._listeningHandle, void 0, {
|
|
1482
|
+
F: __dxlog_file4,
|
|
1483
|
+
L: 128,
|
|
1484
|
+
S: this,
|
|
1485
|
+
A: [
|
|
1486
|
+
"!this._listeningHandle",
|
|
1487
|
+
""
|
|
1488
|
+
]
|
|
1489
|
+
});
|
|
1490
|
+
this._listeningHandle = await this._messenger.listen({
|
|
1491
|
+
peerId: this._ownPeerId,
|
|
1492
|
+
payloadType: "dxos.mesh.swarm.SwarmMessage",
|
|
1493
|
+
onMessage: async (message) => {
|
|
1494
|
+
await this._swarmMessenger.receiveMessage(message).catch((err) => import_log3.log.info("Error while receiving message", {
|
|
1495
|
+
err
|
|
1496
|
+
}, {
|
|
1497
|
+
F: __dxlog_file4,
|
|
1498
|
+
L: 136,
|
|
1499
|
+
S: this,
|
|
1500
|
+
C: (f, a) => f(...a)
|
|
1501
|
+
}));
|
|
1502
|
+
}
|
|
1503
|
+
});
|
|
1504
|
+
}
|
|
1505
|
+
async destroy() {
|
|
1506
|
+
(0, import_log3.log)("destroying...", void 0, {
|
|
1507
|
+
F: __dxlog_file4,
|
|
1508
|
+
L: 142,
|
|
1509
|
+
S: this,
|
|
1510
|
+
C: (f, a) => f(...a)
|
|
1511
|
+
});
|
|
1512
|
+
await this._listeningHandle?.unsubscribe();
|
|
1513
|
+
this._listeningHandle = void 0;
|
|
1514
|
+
await this._ctx.dispose();
|
|
1515
|
+
await this._topology.destroy();
|
|
1516
|
+
await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
|
|
1517
|
+
(0, import_log3.log)("destroyed", void 0, {
|
|
1518
|
+
F: __dxlog_file4,
|
|
1519
|
+
L: 149,
|
|
1520
|
+
S: this,
|
|
1521
|
+
C: (f, a) => f(...a)
|
|
1522
|
+
});
|
|
1523
|
+
}
|
|
1524
|
+
async setTopology(topology) {
|
|
1525
|
+
(0, import_invariant3.invariant)(!this._ctx.disposed, "Swarm is offline", {
|
|
1526
|
+
F: __dxlog_file4,
|
|
1527
|
+
L: 153,
|
|
1528
|
+
S: this,
|
|
1529
|
+
A: [
|
|
1530
|
+
"!this._ctx.disposed",
|
|
1531
|
+
"'Swarm is offline'"
|
|
1532
|
+
]
|
|
1533
|
+
});
|
|
1534
|
+
if (topology === this._topology) {
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1537
|
+
(0, import_log3.log)("setting topology", {
|
|
1538
|
+
previous: getClassName(this._topology),
|
|
1539
|
+
topology: getClassName(topology)
|
|
1540
|
+
}, {
|
|
1541
|
+
F: __dxlog_file4,
|
|
1542
|
+
L: 157,
|
|
1543
|
+
S: this,
|
|
1544
|
+
C: (f, a) => f(...a)
|
|
1545
|
+
});
|
|
1546
|
+
await this._topology.destroy();
|
|
1547
|
+
this._topology = topology;
|
|
1548
|
+
this._topology.init(this._getSwarmController());
|
|
1549
|
+
this._topology.update();
|
|
1550
|
+
}
|
|
1551
|
+
onSwarmEvent(swarmEvent) {
|
|
1552
|
+
(0, import_log3.log)("swarm event", {
|
|
1553
|
+
swarmEvent
|
|
1554
|
+
}, {
|
|
1555
|
+
F: __dxlog_file4,
|
|
1556
|
+
L: 170,
|
|
1557
|
+
S: this,
|
|
1558
|
+
C: (f, a) => f(...a)
|
|
1559
|
+
});
|
|
1560
|
+
if (this._ctx.disposed) {
|
|
1561
|
+
(0, import_log3.log)("swarm event ignored for disposed swarm", void 0, {
|
|
1562
|
+
F: __dxlog_file4,
|
|
1563
|
+
L: 173,
|
|
1564
|
+
S: this,
|
|
1565
|
+
C: (f, a) => f(...a)
|
|
1566
|
+
});
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
if (swarmEvent.peerAvailable) {
|
|
1570
|
+
const peerId = import_keys3.PublicKey.from(swarmEvent.peerAvailable.peer);
|
|
1571
|
+
(0, import_log3.log)("new peer", {
|
|
1572
|
+
peerId
|
|
1573
|
+
}, {
|
|
1574
|
+
F: __dxlog_file4,
|
|
1575
|
+
L: 179,
|
|
1576
|
+
S: this,
|
|
1577
|
+
C: (f, a) => f(...a)
|
|
1578
|
+
});
|
|
1579
|
+
if (!peerId.equals(this._ownPeerId)) {
|
|
1580
|
+
const peer = this._getOrCreatePeer(peerId);
|
|
1581
|
+
peer.advertizing = true;
|
|
1582
|
+
}
|
|
1583
|
+
} else if (swarmEvent.peerLeft) {
|
|
1584
|
+
const peer = this._peers.get(import_keys3.PublicKey.from(swarmEvent.peerLeft.peer));
|
|
1585
|
+
if (peer) {
|
|
1586
|
+
peer.advertizing = false;
|
|
1587
|
+
if (peer.connection?.state !== ConnectionState.CONNECTED) {
|
|
1588
|
+
void this._destroyPeer(peer.id, "peer left").catch((err) => import_log3.log.catch(err, void 0, {
|
|
1589
|
+
F: __dxlog_file4,
|
|
1590
|
+
L: 190,
|
|
1591
|
+
S: this,
|
|
1592
|
+
C: (f, a) => f(...a)
|
|
1593
|
+
}));
|
|
1594
|
+
}
|
|
1595
|
+
} else {
|
|
1596
|
+
(0, import_log3.log)("received peerLeft but no peer found", {
|
|
1597
|
+
peer: swarmEvent.peerLeft.peer
|
|
1598
|
+
}, {
|
|
1599
|
+
F: __dxlog_file4,
|
|
1600
|
+
L: 193,
|
|
1601
|
+
S: this,
|
|
1602
|
+
C: (f, a) => f(...a)
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
this._topology.update();
|
|
1607
|
+
}
|
|
1608
|
+
async onOffer(message) {
|
|
1609
|
+
(0, import_log3.log)("offer", {
|
|
1610
|
+
message
|
|
1611
|
+
}, {
|
|
1612
|
+
F: __dxlog_file4,
|
|
1613
|
+
L: 202,
|
|
1614
|
+
S: this,
|
|
1615
|
+
C: (f, a) => f(...a)
|
|
1616
|
+
});
|
|
1617
|
+
if (this._ctx.disposed) {
|
|
1618
|
+
(0, import_log3.log)("ignored for disposed swarm", void 0, {
|
|
1619
|
+
F: __dxlog_file4,
|
|
1620
|
+
L: 204,
|
|
1621
|
+
S: this,
|
|
1622
|
+
C: (f, a) => f(...a)
|
|
1623
|
+
});
|
|
1624
|
+
return {
|
|
1625
|
+
accept: false
|
|
1626
|
+
};
|
|
1627
|
+
}
|
|
1628
|
+
(0, import_invariant3.invariant)(message.author, void 0, {
|
|
1629
|
+
F: __dxlog_file4,
|
|
1630
|
+
L: 209,
|
|
1631
|
+
S: this,
|
|
1632
|
+
A: [
|
|
1633
|
+
"message.author",
|
|
1634
|
+
""
|
|
1635
|
+
]
|
|
1636
|
+
});
|
|
1637
|
+
if (!message.recipient?.equals(this._ownPeerId)) {
|
|
1638
|
+
(0, import_log3.log)("rejecting offer with incorrect peerId", {
|
|
1639
|
+
message
|
|
1640
|
+
}, {
|
|
1641
|
+
F: __dxlog_file4,
|
|
1642
|
+
L: 211,
|
|
1643
|
+
S: this,
|
|
1644
|
+
C: (f, a) => f(...a)
|
|
1645
|
+
});
|
|
1646
|
+
return {
|
|
1647
|
+
accept: false
|
|
1648
|
+
};
|
|
1649
|
+
}
|
|
1650
|
+
if (!message.topic?.equals(this._topic)) {
|
|
1651
|
+
(0, import_log3.log)("rejecting offer with incorrect topic", {
|
|
1652
|
+
message
|
|
1653
|
+
}, {
|
|
1654
|
+
F: __dxlog_file4,
|
|
1655
|
+
L: 215,
|
|
1656
|
+
S: this,
|
|
1657
|
+
C: (f, a) => f(...a)
|
|
1658
|
+
});
|
|
1659
|
+
return {
|
|
1660
|
+
accept: false
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
const peer = this._getOrCreatePeer(message.author);
|
|
1664
|
+
const answer = await peer.onOffer(message);
|
|
1665
|
+
this._topology.update();
|
|
1666
|
+
return answer;
|
|
1667
|
+
}
|
|
1668
|
+
async onSignal(message) {
|
|
1669
|
+
(0, import_log3.log)("signal", {
|
|
1670
|
+
message
|
|
1671
|
+
}, {
|
|
1672
|
+
F: __dxlog_file4,
|
|
1673
|
+
L: 226,
|
|
1674
|
+
S: this,
|
|
1675
|
+
C: (f, a) => f(...a)
|
|
1676
|
+
});
|
|
1677
|
+
if (this._ctx.disposed) {
|
|
1678
|
+
import_log3.log.info("ignored for offline swarm", void 0, {
|
|
1679
|
+
F: __dxlog_file4,
|
|
1680
|
+
L: 228,
|
|
1681
|
+
S: this,
|
|
1682
|
+
C: (f, a) => f(...a)
|
|
1683
|
+
});
|
|
1684
|
+
return;
|
|
1685
|
+
}
|
|
1686
|
+
(0, import_invariant3.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
|
|
1687
|
+
F: __dxlog_file4,
|
|
1688
|
+
L: 231,
|
|
1689
|
+
S: this,
|
|
1690
|
+
A: [
|
|
1691
|
+
"message.recipient?.equals(this._ownPeerId)",
|
|
1692
|
+
"`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
|
|
1693
|
+
]
|
|
1694
|
+
});
|
|
1695
|
+
(0, import_invariant3.invariant)(message.topic?.equals(this._topic), void 0, {
|
|
1696
|
+
F: __dxlog_file4,
|
|
1697
|
+
L: 235,
|
|
1698
|
+
S: this,
|
|
1699
|
+
A: [
|
|
1700
|
+
"message.topic?.equals(this._topic)",
|
|
1701
|
+
""
|
|
1702
|
+
]
|
|
1703
|
+
});
|
|
1704
|
+
(0, import_invariant3.invariant)(message.author, void 0, {
|
|
1705
|
+
F: __dxlog_file4,
|
|
1706
|
+
L: 236,
|
|
1707
|
+
S: this,
|
|
1708
|
+
A: [
|
|
1709
|
+
"message.author",
|
|
1710
|
+
""
|
|
1711
|
+
]
|
|
1712
|
+
});
|
|
1713
|
+
const peer = this._getOrCreatePeer(message.author);
|
|
1714
|
+
await peer.onSignal(message);
|
|
1715
|
+
}
|
|
1716
|
+
// For debug purposes
|
|
1717
|
+
async goOffline() {
|
|
1718
|
+
await this._ctx.dispose();
|
|
1719
|
+
await Promise.all([
|
|
1720
|
+
...this._peers.keys()
|
|
1721
|
+
].map((peerId) => this._destroyPeer(peerId, "goOffline")));
|
|
1722
|
+
}
|
|
1723
|
+
// For debug purposes
|
|
1724
|
+
async goOnline() {
|
|
1725
|
+
this._ctx = new import_context3.Context();
|
|
1726
|
+
}
|
|
1727
|
+
_getOrCreatePeer(peerId) {
|
|
1728
|
+
let peer = this._peers.get(peerId);
|
|
1729
|
+
if (!peer) {
|
|
1730
|
+
peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, this._connectionLimiter, {
|
|
1731
|
+
onInitiated: (connection) => {
|
|
1732
|
+
this.connectionAdded.emit(connection);
|
|
1733
|
+
},
|
|
1734
|
+
onConnected: () => {
|
|
1735
|
+
this.connected.emit(peerId);
|
|
1736
|
+
},
|
|
1737
|
+
onDisconnected: async () => {
|
|
1738
|
+
if (!peer.advertizing) {
|
|
1739
|
+
await this._destroyPeer(peer.id, "peer disconnected");
|
|
1740
|
+
}
|
|
1741
|
+
this.disconnected.emit(peerId);
|
|
1742
|
+
this._topology.update();
|
|
1743
|
+
},
|
|
1744
|
+
onRejected: () => {
|
|
1745
|
+
if (this._peers.has(peerId)) {
|
|
1746
|
+
(0, import_log3.log)("peer rejected connection", {
|
|
1747
|
+
peerId
|
|
1748
|
+
}, {
|
|
1749
|
+
F: __dxlog_file4,
|
|
1750
|
+
L: 285,
|
|
1751
|
+
S: this,
|
|
1752
|
+
C: (f, a) => f(...a)
|
|
1753
|
+
});
|
|
1754
|
+
void this._destroyPeer(peerId, "peer rejected connection");
|
|
1755
|
+
}
|
|
1756
|
+
},
|
|
1757
|
+
onAccepted: () => {
|
|
1758
|
+
this._topology.update();
|
|
1759
|
+
},
|
|
1760
|
+
onOffer: (remoteId) => {
|
|
1761
|
+
return this._topology.onOffer(remoteId);
|
|
1762
|
+
},
|
|
1763
|
+
onPeerAvailable: () => {
|
|
1764
|
+
this._topology.update();
|
|
1765
|
+
}
|
|
1766
|
+
});
|
|
1767
|
+
this._peers.set(peerId, peer);
|
|
1768
|
+
}
|
|
1769
|
+
return peer;
|
|
1770
|
+
}
|
|
1771
|
+
async _destroyPeer(peerId, reason) {
|
|
1772
|
+
(0, import_invariant3.invariant)(this._peers.has(peerId), void 0, {
|
|
1773
|
+
F: __dxlog_file4,
|
|
1774
|
+
L: 307,
|
|
1775
|
+
S: this,
|
|
1776
|
+
A: [
|
|
1777
|
+
"this._peers.has(peerId)",
|
|
1778
|
+
""
|
|
1779
|
+
]
|
|
1780
|
+
});
|
|
1781
|
+
await this._peers.get(peerId).destroy(new Error(reason));
|
|
1782
|
+
this._peers.delete(peerId);
|
|
1783
|
+
}
|
|
1784
|
+
_getSwarmController() {
|
|
1785
|
+
return {
|
|
1786
|
+
getState: () => ({
|
|
1787
|
+
ownPeerId: this._ownPeerId,
|
|
1788
|
+
connected: Array.from(this._peers.values()).filter((peer) => peer.connection).map((peer) => peer.id),
|
|
1789
|
+
candidates: Array.from(this._peers.values()).filter((peer) => !peer.connection && peer.advertizing && peer.availableToConnect).map((peer) => peer.id)
|
|
1790
|
+
}),
|
|
1791
|
+
connect: (peer) => {
|
|
1792
|
+
if (this._ctx.disposed) {
|
|
1793
|
+
return;
|
|
1794
|
+
}
|
|
1795
|
+
(0, import_async2.scheduleTask)(this._ctx, async () => {
|
|
1796
|
+
try {
|
|
1797
|
+
await this._initiateConnection(peer);
|
|
1798
|
+
} catch (err) {
|
|
1799
|
+
(0, import_log3.log)("initiation error", err, {
|
|
1800
|
+
F: __dxlog_file4,
|
|
1801
|
+
L: 333,
|
|
1802
|
+
S: this,
|
|
1803
|
+
C: (f, a) => f(...a)
|
|
1804
|
+
});
|
|
1805
|
+
}
|
|
1806
|
+
});
|
|
1807
|
+
},
|
|
1808
|
+
disconnect: async (peer) => {
|
|
1809
|
+
if (this._ctx.disposed) {
|
|
1810
|
+
return;
|
|
1811
|
+
}
|
|
1812
|
+
(0, import_async2.scheduleTask)(this._ctx, async () => {
|
|
1813
|
+
await this._closeConnection(peer);
|
|
1814
|
+
this._topology.update();
|
|
1815
|
+
});
|
|
1816
|
+
}
|
|
1817
|
+
};
|
|
1818
|
+
}
|
|
1819
|
+
/**
|
|
1820
|
+
* Creates a connection then sends message over signal network.
|
|
1821
|
+
*/
|
|
1822
|
+
async _initiateConnection(remoteId) {
|
|
1823
|
+
const ctx = this._ctx;
|
|
1824
|
+
if (remoteId.toHex() < this._ownPeerId.toHex()) {
|
|
1825
|
+
(0, import_log3.log)("initiation delay", {
|
|
1826
|
+
remoteId
|
|
1827
|
+
}, {
|
|
1828
|
+
F: __dxlog_file4,
|
|
1829
|
+
L: 360,
|
|
1830
|
+
S: this,
|
|
1831
|
+
C: (f, a) => f(...a)
|
|
1832
|
+
});
|
|
1833
|
+
await (0, import_async2.sleep)(this._initiationDelay);
|
|
1834
|
+
}
|
|
1835
|
+
if (ctx.disposed) {
|
|
1836
|
+
return;
|
|
1837
|
+
}
|
|
1838
|
+
const peer = this._getOrCreatePeer(remoteId);
|
|
1839
|
+
if (peer.connection) {
|
|
1840
|
+
return;
|
|
1841
|
+
}
|
|
1842
|
+
(0, import_log3.log)("initiating connection...", {
|
|
1843
|
+
remoteId
|
|
1844
|
+
}, {
|
|
1845
|
+
F: __dxlog_file4,
|
|
1846
|
+
L: 374,
|
|
1847
|
+
S: this,
|
|
1848
|
+
C: (f, a) => f(...a)
|
|
1849
|
+
});
|
|
1850
|
+
await peer.initiateConnection();
|
|
1851
|
+
this._topology.update();
|
|
1852
|
+
(0, import_log3.log)("initiated", {
|
|
1853
|
+
remoteId
|
|
1854
|
+
}, {
|
|
1855
|
+
F: __dxlog_file4,
|
|
1856
|
+
L: 377,
|
|
1857
|
+
S: this,
|
|
1858
|
+
C: (f, a) => f(...a)
|
|
1859
|
+
});
|
|
1860
|
+
}
|
|
1861
|
+
async _closeConnection(peerId) {
|
|
1862
|
+
const peer = this._peers.get(peerId);
|
|
1863
|
+
if (!peer) {
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
await peer.closeConnection();
|
|
1867
|
+
}
|
|
1868
|
+
};
|
|
1869
|
+
_ts_decorate3([
|
|
1870
|
+
import_log3.logInfo
|
|
1871
|
+
], Swarm.prototype, "_instanceId", void 0);
|
|
1872
|
+
_ts_decorate3([
|
|
1873
|
+
import_log3.logInfo
|
|
1874
|
+
], Swarm.prototype, "ownPeerId", null);
|
|
1875
|
+
_ts_decorate3([
|
|
1876
|
+
import_log3.logInfo
|
|
1877
|
+
], Swarm.prototype, "topic", null);
|
|
1878
|
+
_ts_decorate3([
|
|
1879
|
+
import_async2.synchronized
|
|
1880
|
+
], Swarm.prototype, "onSwarmEvent", null);
|
|
1881
|
+
_ts_decorate3([
|
|
1882
|
+
import_async2.synchronized
|
|
1883
|
+
], Swarm.prototype, "onOffer", null);
|
|
1884
|
+
_ts_decorate3([
|
|
1885
|
+
import_async2.synchronized
|
|
1886
|
+
], Swarm.prototype, "goOffline", null);
|
|
1887
|
+
_ts_decorate3([
|
|
1888
|
+
import_async2.synchronized
|
|
1889
|
+
], Swarm.prototype, "goOnline", null);
|
|
1890
|
+
var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/swarm-mapper.ts";
|
|
1891
|
+
var SwarmMapper = class {
|
|
1892
|
+
get peers() {
|
|
1893
|
+
return Array.from(this._peers.values());
|
|
1894
|
+
}
|
|
1895
|
+
constructor(_swarm) {
|
|
1896
|
+
this._swarm = _swarm;
|
|
1897
|
+
this._subscriptions = new import_async4.EventSubscriptions();
|
|
1898
|
+
this._connectionSubscriptions = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
|
|
1899
|
+
this._peers = new import_util3.ComplexMap(import_keys5.PublicKey.hash);
|
|
1900
|
+
this.mapUpdated = new import_async4.Event();
|
|
1901
|
+
this._subscriptions.add(_swarm.connectionAdded.on((connection) => {
|
|
1902
|
+
this._update();
|
|
1903
|
+
this._connectionSubscriptions.set(connection.remoteId, connection.stateChanged.on(() => {
|
|
1904
|
+
this._update();
|
|
1905
|
+
}));
|
|
1906
|
+
}));
|
|
1907
|
+
this._subscriptions.add(_swarm.disconnected.on((peerId) => {
|
|
1908
|
+
this._connectionSubscriptions.get(peerId)?.();
|
|
1909
|
+
this._connectionSubscriptions.delete(peerId);
|
|
1910
|
+
this._update();
|
|
1911
|
+
}));
|
|
1912
|
+
this._update();
|
|
1913
|
+
}
|
|
1914
|
+
_update() {
|
|
1915
|
+
(0, import_log5.log)("updating swarm", void 0, {
|
|
1916
|
+
F: __dxlog_file5,
|
|
1917
|
+
L: 72,
|
|
1918
|
+
S: this,
|
|
1919
|
+
C: (f, a) => f(...a)
|
|
1920
|
+
});
|
|
1921
|
+
this._peers.clear();
|
|
1922
|
+
this._peers.set(this._swarm.ownPeerId, {
|
|
1923
|
+
id: this._swarm.ownPeerId,
|
|
1924
|
+
state: "ME",
|
|
1925
|
+
connections: []
|
|
1926
|
+
});
|
|
1927
|
+
for (const connection of this._swarm.connections) {
|
|
1928
|
+
this._peers.set(connection.remoteId, {
|
|
1929
|
+
id: connection.remoteId,
|
|
1930
|
+
state: connection.state,
|
|
1931
|
+
connections: [
|
|
1932
|
+
this._swarm.ownPeerId
|
|
1933
|
+
]
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
(0, import_log5.log)("graph changed", {
|
|
1937
|
+
directConnections: this._swarm.connections.length,
|
|
1938
|
+
totalPeersInSwarm: this._peers.size
|
|
1939
|
+
}, {
|
|
1940
|
+
F: __dxlog_file5,
|
|
1941
|
+
L: 113,
|
|
1942
|
+
S: this,
|
|
1943
|
+
C: (f, a) => f(...a)
|
|
1944
|
+
});
|
|
1945
|
+
this.mapUpdated.emit(Array.from(this._peers.values()));
|
|
1946
|
+
}
|
|
1947
|
+
// TODO(burdon): Async open/close.
|
|
1948
|
+
destroy() {
|
|
1949
|
+
Array.from(this._connectionSubscriptions.values()).forEach((cb) => cb());
|
|
1950
|
+
this._subscriptions.clear();
|
|
1951
|
+
}
|
|
1952
|
+
};
|
|
1953
|
+
var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
|
|
1954
|
+
var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
|
|
1955
|
+
var ConnectionLimiter = class {
|
|
1956
|
+
constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
|
|
1957
|
+
this._ctx = new import_context5.Context();
|
|
1958
|
+
this._waitingPromises = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
|
|
1959
|
+
this.resolveWaitingPromises = new import_async5.DeferredTask(this._ctx, async () => {
|
|
1960
|
+
Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
|
|
1961
|
+
resolve();
|
|
1962
|
+
});
|
|
1963
|
+
});
|
|
1964
|
+
this._maxConcurrentInitConnections = maxConcurrentInitConnections;
|
|
1965
|
+
}
|
|
1966
|
+
/**
|
|
1967
|
+
* @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
|
|
1968
|
+
*/
|
|
1969
|
+
async connecting(sessionId) {
|
|
1970
|
+
(0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
|
|
1971
|
+
F: __dxlog_file6,
|
|
1972
|
+
L: 48,
|
|
1973
|
+
S: this,
|
|
1974
|
+
A: [
|
|
1975
|
+
"!this._waitingPromises.has(sessionId)",
|
|
1976
|
+
"'Peer is already waiting for connection'"
|
|
1977
|
+
]
|
|
1978
|
+
});
|
|
1979
|
+
(0, import_log6.log)("waiting", {
|
|
1980
|
+
sessionId
|
|
1981
|
+
}, {
|
|
1982
|
+
F: __dxlog_file6,
|
|
1983
|
+
L: 49,
|
|
1984
|
+
S: this,
|
|
1985
|
+
C: (f, a) => f(...a)
|
|
1986
|
+
});
|
|
1987
|
+
await new Promise((resolve, reject) => {
|
|
1988
|
+
this._waitingPromises.set(sessionId, {
|
|
1989
|
+
resolve,
|
|
1990
|
+
reject
|
|
1991
|
+
});
|
|
1992
|
+
this.resolveWaitingPromises.schedule();
|
|
1993
|
+
});
|
|
1994
|
+
(0, import_log6.log)("allow", {
|
|
1995
|
+
sessionId
|
|
1996
|
+
}, {
|
|
1997
|
+
F: __dxlog_file6,
|
|
1998
|
+
L: 57,
|
|
1999
|
+
S: this,
|
|
2000
|
+
C: (f, a) => f(...a)
|
|
2001
|
+
});
|
|
2002
|
+
}
|
|
2003
|
+
/**
|
|
2004
|
+
* Rejects promise returned by `connecting` method.
|
|
2005
|
+
*/
|
|
2006
|
+
doneConnecting(sessionId) {
|
|
2007
|
+
(0, import_log6.log)("done", {
|
|
2008
|
+
sessionId
|
|
2009
|
+
}, {
|
|
2010
|
+
F: __dxlog_file6,
|
|
2011
|
+
L: 64,
|
|
2012
|
+
S: this,
|
|
2013
|
+
C: (f, a) => f(...a)
|
|
2014
|
+
});
|
|
2015
|
+
if (!this._waitingPromises.has(sessionId)) {
|
|
2016
|
+
return;
|
|
2017
|
+
}
|
|
2018
|
+
this._waitingPromises.get(sessionId).reject(new import_protocols5.CancelledError());
|
|
2019
|
+
this._waitingPromises.delete(sessionId);
|
|
2020
|
+
this.resolveWaitingPromises.schedule();
|
|
2021
|
+
}
|
|
2022
|
+
};
|
|
2023
|
+
var EventType;
|
|
2024
|
+
(function(EventType2) {
|
|
2025
|
+
EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
|
|
2026
|
+
EventType2["PROTOCOL_ERROR"] = "PROTOCOL_ERROR";
|
|
2027
|
+
EventType2["PROTOCOL_EXTENSIONS_INITIALIZED"] = "PROTOCOL_EXTENSIONS_INITIALIZED";
|
|
2028
|
+
EventType2["PROTOCOL_EXTENSIONS_HANDSHAKE"] = "PROTOCOL_EXTENSIONS_HANDSHAKE";
|
|
2029
|
+
EventType2["PROTOCOL_HANDSHAKE"] = "PROTOCOL_HANDSHAKE";
|
|
2030
|
+
})(EventType || (EventType = {}));
|
|
2031
|
+
var ConnectionLog = class {
|
|
2032
|
+
constructor() {
|
|
2033
|
+
this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
|
|
2034
|
+
this.update = new import_async6.Event();
|
|
2035
|
+
}
|
|
2036
|
+
getSwarmInfo(swarmId) {
|
|
2037
|
+
return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
|
|
2038
|
+
}
|
|
2039
|
+
get swarms() {
|
|
2040
|
+
return Array.from(this._swarms.values());
|
|
2041
|
+
}
|
|
2042
|
+
joinedSwarm(swarm) {
|
|
2043
|
+
const info = {
|
|
2044
|
+
id: import_keys7.PublicKey.from(swarm._instanceId),
|
|
2045
|
+
topic: swarm.topic,
|
|
2046
|
+
isActive: true,
|
|
2047
|
+
label: swarm.label,
|
|
2048
|
+
connections: []
|
|
2049
|
+
};
|
|
2050
|
+
this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
|
|
2051
|
+
this.update.emit();
|
|
2052
|
+
swarm.connectionAdded.on((connection) => {
|
|
2053
|
+
const connectionInfo = {
|
|
2054
|
+
state: ConnectionState.CREATED,
|
|
2055
|
+
closeReason: connection.closeReason,
|
|
2056
|
+
remotePeerId: connection.remoteId,
|
|
2057
|
+
sessionId: connection.sessionId,
|
|
2058
|
+
transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
|
|
2059
|
+
protocolExtensions: [],
|
|
2060
|
+
events: []
|
|
2061
|
+
};
|
|
2062
|
+
info.connections.push(connectionInfo);
|
|
2063
|
+
this.update.emit();
|
|
2064
|
+
connection.stateChanged.on((state) => {
|
|
2065
|
+
connectionInfo.state = state;
|
|
2066
|
+
connectionInfo.closeReason = connection.closeReason;
|
|
2067
|
+
connectionInfo.events.push({
|
|
2068
|
+
type: EventType.CONNECTION_STATE_CHANGED,
|
|
2069
|
+
newState: state
|
|
2070
|
+
});
|
|
2071
|
+
this.update.emit();
|
|
2072
|
+
});
|
|
2073
|
+
connection.protocol?.stats?.on((stats) => {
|
|
2074
|
+
connectionInfo.readBufferSize = stats.readBufferSize;
|
|
2075
|
+
connectionInfo.writeBufferSize = stats.writeBufferSize;
|
|
2076
|
+
connectionInfo.streams = stats.channels;
|
|
2077
|
+
this.update.emit();
|
|
2078
|
+
});
|
|
2079
|
+
});
|
|
2080
|
+
}
|
|
2081
|
+
leftSwarm(swarm) {
|
|
2082
|
+
this.getSwarmInfo(import_keys7.PublicKey.from(swarm._instanceId)).isActive = false;
|
|
2083
|
+
this.update.emit();
|
|
2084
|
+
}
|
|
2085
|
+
};
|
|
2086
|
+
var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
|
|
2087
|
+
var NetworkManager = class {
|
|
2088
|
+
constructor({ transportFactory, signalManager, log: log1 }) {
|
|
2089
|
+
this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
|
|
2090
|
+
this._mappers = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
|
|
2091
|
+
this._connectionState = import_services.ConnectionState.ONLINE;
|
|
2092
|
+
this.connectionStateChanged = new import_async7.Event();
|
|
2093
|
+
this.topicsUpdated = new import_async7.Event();
|
|
2094
|
+
this._instanceId = import_keys8.PublicKey.random().toHex();
|
|
2095
|
+
this._transportFactory = transportFactory;
|
|
2096
|
+
this._signalManager = signalManager;
|
|
2097
|
+
this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
|
|
2098
|
+
this._messenger = new import_messaging.Messenger({
|
|
2099
|
+
signalManager: this._signalManager
|
|
2100
|
+
});
|
|
2101
|
+
this._signalConnection = {
|
|
2102
|
+
join: (opts) => this._signalManager.join(opts),
|
|
2103
|
+
leave: (opts) => this._signalManager.leave(opts)
|
|
2104
|
+
};
|
|
2105
|
+
this._connectionLimiter = new ConnectionLimiter();
|
|
2106
|
+
if (log1) {
|
|
2107
|
+
this._connectionLog = new ConnectionLog();
|
|
2108
|
+
}
|
|
2109
|
+
}
|
|
2110
|
+
// TODO(burdon): Remove access (Devtools only).
|
|
2111
|
+
get connectionLog() {
|
|
2112
|
+
return this._connectionLog;
|
|
2113
|
+
}
|
|
2114
|
+
get connectionState() {
|
|
2115
|
+
return this._connectionState;
|
|
2116
|
+
}
|
|
2117
|
+
// TODO(burdon): Reconcile with "discovery_key".
|
|
2118
|
+
get topics() {
|
|
2119
|
+
return Array.from(this._swarms.keys());
|
|
2120
|
+
}
|
|
2121
|
+
getSwarmMap(topic) {
|
|
2122
|
+
return this._mappers.get(topic);
|
|
2123
|
+
}
|
|
2124
|
+
getSwarm(topic) {
|
|
2125
|
+
return this._swarms.get(topic);
|
|
2126
|
+
}
|
|
2127
|
+
async open() {
|
|
2128
|
+
import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.begin({
|
|
2129
|
+
id: this._instanceId
|
|
2130
|
+
}), {
|
|
2131
|
+
F: __dxlog_file7,
|
|
2132
|
+
L: 130,
|
|
2133
|
+
S: this,
|
|
2134
|
+
C: (f, a) => f(...a)
|
|
2135
|
+
});
|
|
2136
|
+
await this._messenger.open();
|
|
2137
|
+
await this._signalManager.open();
|
|
2138
|
+
import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols6.trace.end({
|
|
2139
|
+
id: this._instanceId
|
|
2140
|
+
}), {
|
|
2141
|
+
F: __dxlog_file7,
|
|
2142
|
+
L: 133,
|
|
2143
|
+
S: this,
|
|
2144
|
+
C: (f, a) => f(...a)
|
|
2145
|
+
});
|
|
2146
|
+
}
|
|
2147
|
+
async close() {
|
|
2148
|
+
for (const topic of this._swarms.keys()) {
|
|
2149
|
+
await this.leaveSwarm(topic).catch((err) => {
|
|
2150
|
+
(0, import_log7.log)(err, void 0, {
|
|
2151
|
+
F: __dxlog_file7,
|
|
2152
|
+
L: 139,
|
|
2153
|
+
S: this,
|
|
2154
|
+
C: (f, a) => f(...a)
|
|
2155
|
+
});
|
|
2156
|
+
});
|
|
2157
|
+
}
|
|
2158
|
+
await this._messenger.close();
|
|
2159
|
+
await this._signalManager.close();
|
|
2160
|
+
}
|
|
2161
|
+
/**
|
|
2162
|
+
* Join the swarm.
|
|
2163
|
+
*/
|
|
2164
|
+
async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
|
|
2165
|
+
(0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
|
|
2166
|
+
F: __dxlog_file7,
|
|
2167
|
+
L: 157,
|
|
2168
|
+
S: this,
|
|
2169
|
+
A: [
|
|
2170
|
+
"PublicKey.isPublicKey(topic)",
|
|
2171
|
+
""
|
|
2172
|
+
]
|
|
2173
|
+
});
|
|
2174
|
+
(0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
|
|
2175
|
+
F: __dxlog_file7,
|
|
2176
|
+
L: 158,
|
|
2177
|
+
S: this,
|
|
2178
|
+
A: [
|
|
2179
|
+
"PublicKey.isPublicKey(peerId)",
|
|
2180
|
+
""
|
|
2181
|
+
]
|
|
2182
|
+
});
|
|
2183
|
+
(0, import_invariant6.invariant)(topology, void 0, {
|
|
2184
|
+
F: __dxlog_file7,
|
|
2185
|
+
L: 159,
|
|
2186
|
+
S: this,
|
|
2187
|
+
A: [
|
|
2188
|
+
"topology",
|
|
2189
|
+
""
|
|
2190
|
+
]
|
|
2191
|
+
});
|
|
2192
|
+
(0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
|
|
2193
|
+
F: __dxlog_file7,
|
|
2194
|
+
L: 160,
|
|
2195
|
+
S: this,
|
|
2196
|
+
A: [
|
|
2197
|
+
"typeof protocol === 'function'",
|
|
2198
|
+
""
|
|
2199
|
+
]
|
|
2200
|
+
});
|
|
2201
|
+
if (this._swarms.has(topic)) {
|
|
2202
|
+
throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
|
|
2203
|
+
}
|
|
2204
|
+
(0, import_log7.log)("joining", {
|
|
2205
|
+
topic: import_keys8.PublicKey.from(topic),
|
|
2206
|
+
peerId,
|
|
2207
|
+
topology: topology.toString()
|
|
2208
|
+
}, {
|
|
2209
|
+
F: __dxlog_file7,
|
|
2210
|
+
L: 165,
|
|
2211
|
+
S: this,
|
|
2212
|
+
C: (f, a) => f(...a)
|
|
2213
|
+
});
|
|
2214
|
+
const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
|
|
2215
|
+
swarm.errors.handle((error) => {
|
|
2216
|
+
(0, import_log7.log)("swarm error", {
|
|
2217
|
+
error
|
|
2218
|
+
}, {
|
|
2219
|
+
F: __dxlog_file7,
|
|
2220
|
+
L: 178,
|
|
2221
|
+
S: this,
|
|
2222
|
+
C: (f, a) => f(...a)
|
|
2223
|
+
});
|
|
2224
|
+
});
|
|
2225
|
+
this._swarms.set(topic, swarm);
|
|
2226
|
+
this._mappers.set(topic, new SwarmMapper(swarm));
|
|
2227
|
+
await swarm.open();
|
|
2228
|
+
this._signalConnection.join({
|
|
2229
|
+
topic,
|
|
2230
|
+
peerId
|
|
2231
|
+
}).catch((error) => import_log7.log.catch(error, void 0, {
|
|
2232
|
+
F: __dxlog_file7,
|
|
2233
|
+
L: 187,
|
|
2234
|
+
S: this,
|
|
2235
|
+
C: (f, a) => f(...a)
|
|
2236
|
+
}));
|
|
2237
|
+
this.topicsUpdated.emit();
|
|
2238
|
+
this._connectionLog?.joinedSwarm(swarm);
|
|
2239
|
+
(0, import_log7.log)("joined", {
|
|
2240
|
+
topic: import_keys8.PublicKey.from(topic),
|
|
2241
|
+
count: this._swarms.size
|
|
2242
|
+
}, {
|
|
2243
|
+
F: __dxlog_file7,
|
|
2244
|
+
L: 191,
|
|
2245
|
+
S: this,
|
|
2246
|
+
C: (f, a) => f(...a)
|
|
2247
|
+
});
|
|
2248
|
+
return {
|
|
2249
|
+
close: () => this.leaveSwarm(topic)
|
|
2250
|
+
};
|
|
2251
|
+
}
|
|
2252
|
+
/**
|
|
2253
|
+
* Close the connection.
|
|
2254
|
+
*/
|
|
2255
|
+
async leaveSwarm(topic) {
|
|
2256
|
+
if (!this._swarms.has(topic)) {
|
|
2257
|
+
return;
|
|
2258
|
+
}
|
|
2259
|
+
(0, import_log7.log)("leaving", {
|
|
2260
|
+
topic: import_keys8.PublicKey.from(topic)
|
|
2261
|
+
}, {
|
|
2262
|
+
F: __dxlog_file7,
|
|
2263
|
+
L: 207,
|
|
2264
|
+
S: this,
|
|
2265
|
+
C: (f, a) => f(...a)
|
|
2266
|
+
});
|
|
2267
|
+
const swarm = this._swarms.get(topic);
|
|
2268
|
+
await this._signalConnection.leave({
|
|
2269
|
+
topic,
|
|
2270
|
+
peerId: swarm.ownPeerId
|
|
2271
|
+
});
|
|
2272
|
+
const map = this._mappers.get(topic);
|
|
2273
|
+
map.destroy();
|
|
2274
|
+
this._mappers.delete(topic);
|
|
2275
|
+
this._connectionLog?.leftSwarm(swarm);
|
|
2276
|
+
await swarm.destroy();
|
|
2277
|
+
this._swarms.delete(topic);
|
|
2278
|
+
await this.topicsUpdated.emit();
|
|
2279
|
+
(0, import_log7.log)("left", {
|
|
2280
|
+
topic: import_keys8.PublicKey.from(topic),
|
|
2281
|
+
count: this._swarms.size
|
|
2282
|
+
}, {
|
|
2283
|
+
F: __dxlog_file7,
|
|
2284
|
+
L: 221,
|
|
2285
|
+
S: this,
|
|
2286
|
+
C: (f, a) => f(...a)
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
async setConnectionState(state) {
|
|
2290
|
+
if (state === this._connectionState) {
|
|
2291
|
+
return;
|
|
2292
|
+
}
|
|
2293
|
+
switch (state) {
|
|
2294
|
+
case import_services.ConnectionState.OFFLINE: {
|
|
2295
|
+
this._connectionState = state;
|
|
2296
|
+
await Promise.all([
|
|
2297
|
+
...this._swarms.values()
|
|
2298
|
+
].map((swarm) => swarm.goOffline()));
|
|
2299
|
+
await this._messenger.close();
|
|
2300
|
+
await this._signalManager.close();
|
|
2301
|
+
break;
|
|
2302
|
+
}
|
|
2303
|
+
case import_services.ConnectionState.ONLINE: {
|
|
2304
|
+
this._connectionState = state;
|
|
2305
|
+
this._messenger.open();
|
|
2306
|
+
await Promise.all([
|
|
2307
|
+
...this._swarms.values()
|
|
2308
|
+
].map((swarm) => swarm.goOnline()));
|
|
2309
|
+
await this._signalManager.open();
|
|
2310
|
+
break;
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
this.connectionStateChanged.emit(this._connectionState);
|
|
2314
|
+
}
|
|
2315
|
+
};
|
|
2316
|
+
var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
|
|
2317
|
+
var FullyConnectedTopology = class {
|
|
2318
|
+
toString() {
|
|
2319
|
+
return "FullyConnectedTopology";
|
|
2320
|
+
}
|
|
2321
|
+
init(controller) {
|
|
2322
|
+
(0, import_invariant7.invariant)(!this._controller, "Already initialized", {
|
|
2323
|
+
F: __dxlog_file8,
|
|
2324
|
+
L: 18,
|
|
2325
|
+
S: this,
|
|
2326
|
+
A: [
|
|
2327
|
+
"!this._controller",
|
|
2328
|
+
"'Already initialized'"
|
|
2329
|
+
]
|
|
2330
|
+
});
|
|
2331
|
+
this._controller = controller;
|
|
2332
|
+
}
|
|
2333
|
+
update() {
|
|
2334
|
+
(0, import_invariant7.invariant)(this._controller, "Not initialized", {
|
|
2335
|
+
F: __dxlog_file8,
|
|
2336
|
+
L: 23,
|
|
2337
|
+
S: this,
|
|
2338
|
+
A: [
|
|
2339
|
+
"this._controller",
|
|
2340
|
+
"'Not initialized'"
|
|
2341
|
+
]
|
|
2342
|
+
});
|
|
2343
|
+
const { candidates: discovered } = this._controller.getState();
|
|
2344
|
+
for (const peer of discovered) {
|
|
2345
|
+
this._controller.connect(peer);
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
async onOffer(peer) {
|
|
2349
|
+
return true;
|
|
2350
|
+
}
|
|
2351
|
+
async destroy() {
|
|
2352
|
+
}
|
|
2353
|
+
};
|
|
2354
|
+
var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
|
|
2355
|
+
var MMSTTopology = class {
|
|
2356
|
+
constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
|
|
2357
|
+
this._sampleCollected = false;
|
|
2358
|
+
this._originateConnections = originateConnections;
|
|
2359
|
+
this._maxPeers = maxPeers;
|
|
2360
|
+
this._sampleSize = sampleSize;
|
|
2361
|
+
}
|
|
2362
|
+
init(controller) {
|
|
2363
|
+
(0, import_invariant8.invariant)(!this._controller, "Already initialized", {
|
|
2364
|
+
F: __dxlog_file9,
|
|
2365
|
+
L: 46,
|
|
2366
|
+
S: this,
|
|
2367
|
+
A: [
|
|
2368
|
+
"!this._controller",
|
|
2369
|
+
"'Already initialized'"
|
|
2370
|
+
]
|
|
2371
|
+
});
|
|
2372
|
+
this._controller = controller;
|
|
2373
|
+
}
|
|
2374
|
+
update() {
|
|
2375
|
+
(0, import_invariant8.invariant)(this._controller, "Not initialized", {
|
|
2376
|
+
F: __dxlog_file9,
|
|
2377
|
+
L: 51,
|
|
2378
|
+
S: this,
|
|
2379
|
+
A: [
|
|
2380
|
+
"this._controller",
|
|
2381
|
+
"'Not initialized'"
|
|
2382
|
+
]
|
|
2383
|
+
});
|
|
2384
|
+
const { connected, candidates } = this._controller.getState();
|
|
2385
|
+
if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
|
|
2386
|
+
(0, import_log8.log)("Running the algorithm.", void 0, {
|
|
2387
|
+
F: __dxlog_file9,
|
|
2388
|
+
L: 55,
|
|
2389
|
+
S: this,
|
|
2390
|
+
C: (f, a) => f(...a)
|
|
2391
|
+
});
|
|
2392
|
+
this._sampleCollected = true;
|
|
2393
|
+
this._runAlgorithm();
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
async onOffer(peer) {
|
|
2397
|
+
(0, import_invariant8.invariant)(this._controller, "Not initialized", {
|
|
2398
|
+
F: __dxlog_file9,
|
|
2399
|
+
L: 62,
|
|
2400
|
+
S: this,
|
|
2401
|
+
A: [
|
|
2402
|
+
"this._controller",
|
|
2403
|
+
"'Not initialized'"
|
|
2404
|
+
]
|
|
2405
|
+
});
|
|
2406
|
+
const { connected } = this._controller.getState();
|
|
2407
|
+
const accept = connected.length < this._maxPeers;
|
|
2408
|
+
(0, import_log8.log)(`Offer ${peer} accept=${accept}`, void 0, {
|
|
2409
|
+
F: __dxlog_file9,
|
|
2410
|
+
L: 65,
|
|
2411
|
+
S: this,
|
|
2412
|
+
C: (f, a) => f(...a)
|
|
2413
|
+
});
|
|
2414
|
+
return accept;
|
|
2415
|
+
}
|
|
2416
|
+
async destroy() {
|
|
2417
|
+
}
|
|
2418
|
+
_runAlgorithm() {
|
|
2419
|
+
(0, import_invariant8.invariant)(this._controller, "Not initialized", {
|
|
2420
|
+
F: __dxlog_file9,
|
|
2421
|
+
L: 74,
|
|
2422
|
+
S: this,
|
|
2423
|
+
A: [
|
|
2424
|
+
"this._controller",
|
|
2425
|
+
"'Not initialized'"
|
|
2426
|
+
]
|
|
2427
|
+
});
|
|
2428
|
+
const { connected, candidates, ownPeerId } = this._controller.getState();
|
|
2429
|
+
if (connected.length > this._maxPeers) {
|
|
2430
|
+
const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
|
|
2431
|
+
for (const peer of sorted) {
|
|
2432
|
+
(0, import_log8.log)(`Disconnect ${peer}.`, void 0, {
|
|
2433
|
+
F: __dxlog_file9,
|
|
2434
|
+
L: 83,
|
|
2435
|
+
S: this,
|
|
2436
|
+
C: (f, a) => f(...a)
|
|
2437
|
+
});
|
|
2438
|
+
this._controller.disconnect(peer);
|
|
2439
|
+
}
|
|
2440
|
+
} else if (connected.length < this._originateConnections) {
|
|
2441
|
+
const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
|
|
2442
|
+
const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
|
|
2443
|
+
for (const peer of sorted) {
|
|
2444
|
+
(0, import_log8.log)(`Connect ${peer}.`, void 0, {
|
|
2445
|
+
F: __dxlog_file9,
|
|
2446
|
+
L: 91,
|
|
2447
|
+
S: this,
|
|
2448
|
+
C: (f, a) => f(...a)
|
|
2449
|
+
});
|
|
2450
|
+
this._controller.connect(peer);
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
toString() {
|
|
2455
|
+
return "MMSTTopology";
|
|
2456
|
+
}
|
|
2457
|
+
};
|
|
2458
|
+
var sortByXorDistance = (keys, reference) => keys.sort((a, b) => import_xor_distance.default.gt((0, import_xor_distance.default)(a.asBuffer(), reference.asBuffer()), (0, import_xor_distance.default)(b.asBuffer(), reference.asBuffer())));
|
|
2459
|
+
var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
|
|
2460
|
+
var StarTopology = class {
|
|
2461
|
+
constructor(_centralPeer) {
|
|
2462
|
+
this._centralPeer = _centralPeer;
|
|
2463
|
+
}
|
|
2464
|
+
toString() {
|
|
2465
|
+
return `StarTopology(${this._centralPeer.truncate()})`;
|
|
2466
|
+
}
|
|
2467
|
+
init(controller) {
|
|
2468
|
+
(0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
|
|
2469
|
+
F: __dxlog_file10,
|
|
2470
|
+
L: 21,
|
|
2471
|
+
S: this,
|
|
2472
|
+
A: [
|
|
2473
|
+
"!this._controller",
|
|
2474
|
+
"'Already initialized.'"
|
|
2475
|
+
]
|
|
2476
|
+
});
|
|
2477
|
+
this._controller = controller;
|
|
2478
|
+
}
|
|
2479
|
+
update() {
|
|
2480
|
+
(0, import_invariant9.invariant)(this._controller, "Not initialized.", {
|
|
2481
|
+
F: __dxlog_file10,
|
|
2482
|
+
L: 26,
|
|
2483
|
+
S: this,
|
|
2484
|
+
A: [
|
|
2485
|
+
"this._controller",
|
|
2486
|
+
"'Not initialized.'"
|
|
2487
|
+
]
|
|
2488
|
+
});
|
|
2489
|
+
const { candidates, connected, ownPeerId } = this._controller.getState();
|
|
2490
|
+
if (!ownPeerId.equals(this._centralPeer)) {
|
|
2491
|
+
(0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
|
|
2492
|
+
F: __dxlog_file10,
|
|
2493
|
+
L: 29,
|
|
2494
|
+
S: this,
|
|
2495
|
+
C: (f, a) => f(...a)
|
|
2496
|
+
});
|
|
2497
|
+
for (const peer of connected) {
|
|
2498
|
+
if (!peer.equals(this._centralPeer)) {
|
|
2499
|
+
(0, import_log9.log)("dropping connection", {
|
|
2500
|
+
peer
|
|
2501
|
+
}, {
|
|
2502
|
+
F: __dxlog_file10,
|
|
2503
|
+
L: 34,
|
|
2504
|
+
S: this,
|
|
2505
|
+
C: (f, a) => f(...a)
|
|
2506
|
+
});
|
|
2507
|
+
this._controller.disconnect(peer);
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
for (const peer of candidates) {
|
|
2512
|
+
if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
|
|
2513
|
+
(0, import_log9.log)("connecting to peer", {
|
|
2514
|
+
peer
|
|
2515
|
+
}, {
|
|
2516
|
+
F: __dxlog_file10,
|
|
2517
|
+
L: 43,
|
|
2518
|
+
S: this,
|
|
2519
|
+
C: (f, a) => f(...a)
|
|
2520
|
+
});
|
|
2521
|
+
this._controller.connect(peer);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
async onOffer(peer) {
|
|
2526
|
+
(0, import_invariant9.invariant)(this._controller, "Not initialized.", {
|
|
2527
|
+
F: __dxlog_file10,
|
|
2528
|
+
L: 50,
|
|
2529
|
+
S: this,
|
|
2530
|
+
A: [
|
|
2531
|
+
"this._controller",
|
|
2532
|
+
"'Not initialized.'"
|
|
2533
|
+
]
|
|
2534
|
+
});
|
|
2535
|
+
const { ownPeerId } = this._controller.getState();
|
|
2536
|
+
(0, import_log9.log)("offer", {
|
|
2537
|
+
peer,
|
|
2538
|
+
isCentral: peer.equals(this._centralPeer),
|
|
2539
|
+
isSelfCentral: ownPeerId.equals(this._centralPeer)
|
|
2540
|
+
}, {
|
|
2541
|
+
F: __dxlog_file10,
|
|
2542
|
+
L: 52,
|
|
2543
|
+
S: this,
|
|
2544
|
+
C: (f, a) => f(...a)
|
|
2545
|
+
});
|
|
2546
|
+
return ownPeerId.equals(this._centralPeer) || peer.equals(this._centralPeer);
|
|
2547
|
+
}
|
|
2548
|
+
async destroy() {
|
|
2549
|
+
}
|
|
2550
|
+
};
|
|
2551
|
+
function _ts_decorate4(decorators, target, key, desc) {
|
|
2552
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2553
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
2554
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
2555
|
+
else
|
|
2556
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
2557
|
+
if (d = decorators[i])
|
|
2558
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2559
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2560
|
+
}
|
|
2561
|
+
var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
|
|
2562
|
+
var MEMORY_TRANSPORT_DELAY = 1;
|
|
2563
|
+
var createStreamDelay = (delay) => {
|
|
2564
|
+
return new import_node_stream.Transform({
|
|
2565
|
+
objectMode: true,
|
|
2566
|
+
transform: (chunk, _, cb) => {
|
|
2567
|
+
setTimeout(() => cb(null, chunk), delay);
|
|
2568
|
+
}
|
|
2569
|
+
});
|
|
2570
|
+
};
|
|
2571
|
+
var MemoryTransportFactory = {
|
|
2572
|
+
createTransport: (params) => new MemoryTransport(params)
|
|
2573
|
+
};
|
|
2574
|
+
var MemoryTransport = class _MemoryTransport {
|
|
2575
|
+
static {
|
|
2576
|
+
this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
|
|
2577
|
+
}
|
|
2578
|
+
constructor(options) {
|
|
2579
|
+
this.options = options;
|
|
2580
|
+
this.closed = new import_async8.Event();
|
|
2581
|
+
this.connected = new import_async8.Event();
|
|
2582
|
+
this.errors = new import_debug4.ErrorStream();
|
|
2583
|
+
this._instanceId = import_keys9.PublicKey.random();
|
|
2584
|
+
this._remote = new import_async8.Trigger();
|
|
2585
|
+
this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
2586
|
+
this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
|
|
2587
|
+
this._destroyed = false;
|
|
2588
|
+
(0, import_log10.log)("creating", void 0, {
|
|
2589
|
+
F: __dxlog_file11,
|
|
2590
|
+
L: 64,
|
|
2591
|
+
S: this,
|
|
2592
|
+
C: (f, a) => f(...a)
|
|
2593
|
+
});
|
|
2594
|
+
(0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
|
|
2595
|
+
F: __dxlog_file11,
|
|
2596
|
+
L: 66,
|
|
2597
|
+
S: this,
|
|
2598
|
+
A: [
|
|
2599
|
+
"!MemoryTransport._connections.has(this._instanceId)",
|
|
2600
|
+
"'Duplicate memory connection'"
|
|
2601
|
+
]
|
|
2602
|
+
});
|
|
2603
|
+
_MemoryTransport._connections.set(this._instanceId, this);
|
|
2604
|
+
if (this.options.initiator) {
|
|
2605
|
+
setTimeout(async () => {
|
|
2606
|
+
(0, import_log10.log)("sending signal", void 0, {
|
|
2607
|
+
F: __dxlog_file11,
|
|
2608
|
+
L: 73,
|
|
2609
|
+
S: this,
|
|
2610
|
+
C: (f, a) => f(...a)
|
|
2611
|
+
});
|
|
2612
|
+
void this.options.sendSignal({
|
|
2613
|
+
payload: {
|
|
2614
|
+
transportId: this._instanceId.toHex()
|
|
2615
|
+
}
|
|
2616
|
+
}).catch((err) => {
|
|
2617
|
+
if (!this._destroyed) {
|
|
2618
|
+
this.errors.raise(err);
|
|
2619
|
+
}
|
|
2620
|
+
});
|
|
2621
|
+
});
|
|
2622
|
+
} else {
|
|
2623
|
+
this._remote.wait({
|
|
2624
|
+
timeout: this.options.timeout ?? 1e3
|
|
2625
|
+
}).then((remoteId) => {
|
|
2626
|
+
if (this._destroyed) {
|
|
2627
|
+
return;
|
|
2628
|
+
}
|
|
2629
|
+
this._remoteInstanceId = remoteId;
|
|
2630
|
+
this._remoteConnection = _MemoryTransport._connections.get(this._remoteInstanceId);
|
|
2631
|
+
if (!this._remoteConnection) {
|
|
2632
|
+
this._destroyed = true;
|
|
2633
|
+
this.closed.emit();
|
|
2634
|
+
return;
|
|
2635
|
+
}
|
|
2636
|
+
(0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
|
|
2637
|
+
F: __dxlog_file11,
|
|
2638
|
+
L: 99,
|
|
2639
|
+
S: this,
|
|
2640
|
+
A: [
|
|
2641
|
+
"!this._remoteConnection._remoteConnection",
|
|
2642
|
+
"`Remote already connected: ${this._remoteInstanceId}`"
|
|
2643
|
+
]
|
|
2644
|
+
});
|
|
2645
|
+
this._remoteConnection._remoteConnection = this;
|
|
2646
|
+
this._remoteConnection._remoteInstanceId = this._instanceId;
|
|
2647
|
+
(0, import_log10.log)("connected", void 0, {
|
|
2648
|
+
F: __dxlog_file11,
|
|
2649
|
+
L: 103,
|
|
2650
|
+
S: this,
|
|
2651
|
+
C: (f, a) => f(...a)
|
|
2652
|
+
});
|
|
2653
|
+
this.options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection.options.stream).pipe(this._incomingDelay).pipe(this.options.stream);
|
|
2654
|
+
this.connected.emit();
|
|
2655
|
+
this._remoteConnection.connected.emit();
|
|
2656
|
+
}).catch((err) => {
|
|
2657
|
+
if (this._destroyed) {
|
|
2658
|
+
return;
|
|
2659
|
+
}
|
|
2660
|
+
this.errors.raise(err);
|
|
2661
|
+
});
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
async destroy() {
|
|
2665
|
+
(0, import_log10.log)("closing", void 0, {
|
|
2666
|
+
F: __dxlog_file11,
|
|
2667
|
+
L: 124,
|
|
2668
|
+
S: this,
|
|
2669
|
+
C: (f, a) => f(...a)
|
|
2670
|
+
});
|
|
2671
|
+
this._destroyed = true;
|
|
2672
|
+
_MemoryTransport._connections.delete(this._instanceId);
|
|
2673
|
+
if (this._remoteConnection) {
|
|
2674
|
+
(0, import_log10.log)("closing", void 0, {
|
|
2675
|
+
F: __dxlog_file11,
|
|
2676
|
+
L: 129,
|
|
2677
|
+
S: this,
|
|
2678
|
+
C: (f, a) => f(...a)
|
|
2679
|
+
});
|
|
2680
|
+
this._remoteConnection._destroyed = true;
|
|
2681
|
+
_MemoryTransport._connections.delete(this._remoteInstanceId);
|
|
2682
|
+
this.options.stream.unpipe(this._incomingDelay);
|
|
2683
|
+
this._incomingDelay.unpipe(this._remoteConnection.options.stream);
|
|
2684
|
+
this._remoteConnection.options.stream.unpipe(this._outgoingDelay);
|
|
2685
|
+
this._outgoingDelay.unpipe(this.options.stream);
|
|
2686
|
+
this.options.stream.unpipe(this._outgoingDelay);
|
|
2687
|
+
this._remoteConnection.closed.emit();
|
|
2688
|
+
this._remoteConnection._remoteConnection = void 0;
|
|
2689
|
+
this._remoteConnection = void 0;
|
|
2690
|
+
(0, import_log10.log)("closed", void 0, {
|
|
2691
|
+
F: __dxlog_file11,
|
|
2692
|
+
L: 150,
|
|
2693
|
+
S: this,
|
|
2694
|
+
C: (f, a) => f(...a)
|
|
2695
|
+
});
|
|
2696
|
+
}
|
|
2697
|
+
this.closed.emit();
|
|
2698
|
+
(0, import_log10.log)("closed", void 0, {
|
|
2699
|
+
F: __dxlog_file11,
|
|
2700
|
+
L: 154,
|
|
2701
|
+
S: this,
|
|
2702
|
+
C: (f, a) => f(...a)
|
|
2703
|
+
});
|
|
2704
|
+
}
|
|
2705
|
+
signal({ payload }) {
|
|
2706
|
+
(0, import_log10.log)("received signal", {
|
|
2707
|
+
payload
|
|
2708
|
+
}, {
|
|
2709
|
+
F: __dxlog_file11,
|
|
2710
|
+
L: 158,
|
|
2711
|
+
S: this,
|
|
2712
|
+
C: (f, a) => f(...a)
|
|
2713
|
+
});
|
|
2714
|
+
if (!payload?.transportId) {
|
|
2715
|
+
return;
|
|
2716
|
+
}
|
|
2717
|
+
const transportId = payload.transportId;
|
|
2718
|
+
if (transportId) {
|
|
2719
|
+
const remoteId = import_keys9.PublicKey.fromHex(transportId);
|
|
2720
|
+
this._remote.wake(remoteId);
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
};
|
|
2724
|
+
_ts_decorate4([
|
|
2725
|
+
import_log10.logInfo
|
|
2726
|
+
], MemoryTransport.prototype, "_instanceId", void 0);
|
|
2727
|
+
_ts_decorate4([
|
|
2728
|
+
import_log10.logInfo
|
|
2729
|
+
], MemoryTransport.prototype, "_remoteInstanceId", void 0);
|
|
2730
|
+
var TransportKind;
|
|
2731
|
+
(function(TransportKind2) {
|
|
2732
|
+
TransportKind2["SIMPLE_PEER"] = "SIMPLE_PEER";
|
|
2733
|
+
TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
|
|
2734
|
+
TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
|
|
2735
|
+
TransportKind2["MEMORY"] = "MEMORY";
|
|
2736
|
+
TransportKind2["TCP"] = "TCP";
|
|
2737
|
+
})(TransportKind || (TransportKind = {}));
|
|
2738
|
+
var wrtc = null;
|
|
2739
|
+
try {
|
|
2740
|
+
wrtc = (0, import_chunk_XE5JOIGD.__require)("@koush/wrtc");
|
|
2741
|
+
} catch {
|
|
2742
|
+
}
|
|
2743
|
+
var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
|
|
2744
|
+
var SimplePeerTransport = class {
|
|
2745
|
+
constructor(params) {
|
|
2746
|
+
this.params = params;
|
|
2747
|
+
this._closed = false;
|
|
2748
|
+
this._piped = false;
|
|
2749
|
+
this.closed = new import_async9.Event();
|
|
2750
|
+
this.connected = new import_async9.Event();
|
|
2751
|
+
this.errors = new import_debug5.ErrorStream();
|
|
2752
|
+
this._instanceId = import_keys10.PublicKey.random().toHex();
|
|
2753
|
+
import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.begin({
|
|
2754
|
+
id: this._instanceId
|
|
2755
|
+
}), {
|
|
2756
|
+
F: __dxlog_file12,
|
|
2757
|
+
L: 41,
|
|
2758
|
+
S: this,
|
|
2759
|
+
C: (f, a) => f(...a)
|
|
2760
|
+
});
|
|
2761
|
+
(0, import_log11.log)("created connection", params, {
|
|
2762
|
+
F: __dxlog_file12,
|
|
2763
|
+
L: 42,
|
|
2764
|
+
S: this,
|
|
2765
|
+
C: (f, a) => f(...a)
|
|
2766
|
+
});
|
|
2767
|
+
this._peer = new import_simple_peer.default({
|
|
2768
|
+
channelName: "dxos.mesh.transport",
|
|
2769
|
+
initiator: this.params.initiator,
|
|
2770
|
+
wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
|
|
2771
|
+
config: this.params.webrtcConfig
|
|
2772
|
+
});
|
|
2773
|
+
this._peer.on("signal", async (data) => {
|
|
2774
|
+
(0, import_log11.log)("signal", data, {
|
|
2775
|
+
F: __dxlog_file12,
|
|
2776
|
+
L: 51,
|
|
2777
|
+
S: this,
|
|
2778
|
+
C: (f, a) => f(...a)
|
|
2779
|
+
});
|
|
2780
|
+
await this.params.sendSignal({
|
|
2781
|
+
payload: {
|
|
2782
|
+
data
|
|
2783
|
+
}
|
|
2784
|
+
});
|
|
2785
|
+
});
|
|
2786
|
+
this._peer.on("connect", () => {
|
|
2787
|
+
(0, import_log11.log)("connected", void 0, {
|
|
2788
|
+
F: __dxlog_file12,
|
|
2789
|
+
L: 56,
|
|
2790
|
+
S: this,
|
|
2791
|
+
C: (f, a) => f(...a)
|
|
2792
|
+
});
|
|
2793
|
+
this.params.stream.pipe(this._peer).pipe(this.params.stream);
|
|
2794
|
+
this._piped = true;
|
|
2795
|
+
this.connected.emit();
|
|
2796
|
+
});
|
|
2797
|
+
this._peer.on("close", async () => {
|
|
2798
|
+
(0, import_log11.log)("closed", void 0, {
|
|
2799
|
+
F: __dxlog_file12,
|
|
2800
|
+
L: 63,
|
|
2801
|
+
S: this,
|
|
2802
|
+
C: (f, a) => f(...a)
|
|
2803
|
+
});
|
|
2804
|
+
await this.destroy();
|
|
2805
|
+
});
|
|
2806
|
+
this._peer.on("error", async (err) => {
|
|
2807
|
+
if (typeof RTCError !== "undefined" && err instanceof RTCError) {
|
|
2808
|
+
if (err.errorDetail === "sctp-failure") {
|
|
2809
|
+
this.errors.raise(new import_protocols7.ConnectionResetError("sctp-failure from RTCError", err));
|
|
2810
|
+
} else {
|
|
2811
|
+
this.errors.raise(new import_protocols7.UnknownProtocolError("unknown RTCError", err));
|
|
2812
|
+
}
|
|
2813
|
+
} else if ("code" in err) {
|
|
2814
|
+
import_log11.log.info("simple-peer error", err, {
|
|
2815
|
+
F: __dxlog_file12,
|
|
2816
|
+
L: 78,
|
|
2817
|
+
S: this,
|
|
2818
|
+
C: (f, a) => f(...a)
|
|
2819
|
+
});
|
|
2820
|
+
switch (err.code) {
|
|
2821
|
+
case "ERR_WEBRTC_SUPPORT":
|
|
2822
|
+
this.errors.raise(new import_protocols7.ProtocolError("WebRTC not supported", err));
|
|
2823
|
+
break;
|
|
2824
|
+
case "ERR_ICE_CONNECTION_FAILURE":
|
|
2825
|
+
case "ERR_DATA_CHANNEL":
|
|
2826
|
+
case "ERR_CONNECTION_FAILURE":
|
|
2827
|
+
case "ERR_SIGNALING":
|
|
2828
|
+
this.errors.raise(new import_protocols7.ConnectivityError("unknown communication failure", err));
|
|
2829
|
+
break;
|
|
2830
|
+
case "ERR_CREATE_OFFER":
|
|
2831
|
+
case "ERR_CREATE_ANSWER":
|
|
2832
|
+
case "ERR_SET_LOCAL_DESCRIPTION":
|
|
2833
|
+
case "ERR_SET_REMOTE_DESCRIPTION":
|
|
2834
|
+
case "ERR_ADD_ICE_CANDIDATE":
|
|
2835
|
+
this.errors.raise(new import_protocols7.UnknownProtocolError("unknown simple-peer library failure", err));
|
|
2836
|
+
break;
|
|
2837
|
+
default:
|
|
2838
|
+
this.errors.raise(new Error("unknown simple-peer error"));
|
|
2839
|
+
break;
|
|
2840
|
+
}
|
|
2841
|
+
} else {
|
|
2842
|
+
import_log11.log.info("unknown peer connection error", err, {
|
|
2843
|
+
F: __dxlog_file12,
|
|
2844
|
+
L: 102,
|
|
2845
|
+
S: this,
|
|
2846
|
+
C: (f, a) => f(...a)
|
|
2847
|
+
});
|
|
2848
|
+
this.errors.raise(err);
|
|
2849
|
+
}
|
|
2850
|
+
try {
|
|
2851
|
+
if (typeof this._peer?._pc?.getStats === "function") {
|
|
2852
|
+
this._peer._pc.getStats().then((stats) => {
|
|
2853
|
+
import_log11.log.info("report after webrtc error", {
|
|
2854
|
+
config: this.params.webrtcConfig,
|
|
2855
|
+
stats
|
|
2856
|
+
}, {
|
|
2857
|
+
F: __dxlog_file12,
|
|
2858
|
+
L: 110,
|
|
2859
|
+
S: this,
|
|
2860
|
+
C: (f, a) => f(...a)
|
|
2861
|
+
});
|
|
2862
|
+
});
|
|
2863
|
+
}
|
|
2864
|
+
} catch (err2) {
|
|
2865
|
+
import_log11.log.catch(err2, void 0, {
|
|
2866
|
+
F: __dxlog_file12,
|
|
2867
|
+
L: 117,
|
|
2868
|
+
S: this,
|
|
2869
|
+
C: (f, a) => f(...a)
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
await this.destroy();
|
|
2873
|
+
});
|
|
2874
|
+
import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
|
|
2875
|
+
id: this._instanceId
|
|
2876
|
+
}), {
|
|
2877
|
+
F: __dxlog_file12,
|
|
2878
|
+
L: 122,
|
|
2879
|
+
S: this,
|
|
2880
|
+
C: (f, a) => f(...a)
|
|
2881
|
+
});
|
|
2882
|
+
}
|
|
2883
|
+
async destroy() {
|
|
2884
|
+
(0, import_log11.log)("closing...", void 0, {
|
|
2885
|
+
F: __dxlog_file12,
|
|
2886
|
+
L: 126,
|
|
2887
|
+
S: this,
|
|
2888
|
+
C: (f, a) => f(...a)
|
|
2889
|
+
});
|
|
2890
|
+
if (this._closed) {
|
|
2891
|
+
return;
|
|
2892
|
+
}
|
|
2893
|
+
this._closed = true;
|
|
2894
|
+
this._disconnectStreams();
|
|
2895
|
+
this._peer.destroy();
|
|
2896
|
+
this.closed.emit();
|
|
2897
|
+
(0, import_log11.log)("closed", void 0, {
|
|
2898
|
+
F: __dxlog_file12,
|
|
2899
|
+
L: 134,
|
|
2900
|
+
S: this,
|
|
2901
|
+
C: (f, a) => f(...a)
|
|
2902
|
+
});
|
|
2903
|
+
}
|
|
2904
|
+
signal(signal) {
|
|
2905
|
+
if (this._closed) {
|
|
2906
|
+
return;
|
|
2907
|
+
}
|
|
2908
|
+
(0, import_tiny_invariant.default)(signal.payload.data, "Signal message must contain signal data.");
|
|
2909
|
+
this._peer.signal(signal.payload.data);
|
|
2910
|
+
}
|
|
2911
|
+
_disconnectStreams() {
|
|
2912
|
+
if (this._piped) {
|
|
2913
|
+
this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
};
|
|
2917
|
+
var createSimplePeerTransportFactory = (webrtcConfig) => ({
|
|
2918
|
+
createTransport: (params) => new SimplePeerTransport({
|
|
2919
|
+
...params,
|
|
2920
|
+
webrtcConfig
|
|
2921
|
+
})
|
|
2922
|
+
});
|
|
2923
|
+
var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
|
|
2924
|
+
var SimplePeerTransportService = class {
|
|
2925
|
+
constructor(_webrtcConfig) {
|
|
2926
|
+
this._webrtcConfig = _webrtcConfig;
|
|
2927
|
+
this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
|
|
2928
|
+
}
|
|
2929
|
+
open(request) {
|
|
2930
|
+
const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
|
|
2931
|
+
const duplex = new import_node_stream2.Duplex({
|
|
2932
|
+
read: () => {
|
|
2933
|
+
const callbacks = [
|
|
2934
|
+
...transportState.writeCallbacks
|
|
2935
|
+
];
|
|
2936
|
+
transportState.writeCallbacks.length = 0;
|
|
2937
|
+
for (const cb of callbacks) {
|
|
2938
|
+
cb();
|
|
2939
|
+
}
|
|
2940
|
+
},
|
|
2941
|
+
write: function(chunk, _, callback) {
|
|
2942
|
+
next({
|
|
2943
|
+
data: {
|
|
2944
|
+
payload: chunk
|
|
2945
|
+
}
|
|
2946
|
+
});
|
|
2947
|
+
callback();
|
|
2948
|
+
}
|
|
2949
|
+
});
|
|
2950
|
+
const transport = new SimplePeerTransport({
|
|
2951
|
+
initiator: request.initiator,
|
|
2952
|
+
stream: duplex,
|
|
2953
|
+
webrtcConfig: this._webrtcConfig,
|
|
2954
|
+
sendSignal: async (signal) => {
|
|
2955
|
+
next({
|
|
2956
|
+
signal: {
|
|
2957
|
+
payload: signal
|
|
2958
|
+
}
|
|
2959
|
+
});
|
|
2960
|
+
}
|
|
2961
|
+
});
|
|
2962
|
+
next({
|
|
2963
|
+
connection: {
|
|
2964
|
+
state: import_bridge.ConnectionState.CONNECTING
|
|
2965
|
+
}
|
|
2966
|
+
});
|
|
2967
|
+
transport.connected.on(() => {
|
|
2968
|
+
next({
|
|
2969
|
+
connection: {
|
|
2970
|
+
state: import_bridge.ConnectionState.CONNECTED
|
|
2971
|
+
}
|
|
2972
|
+
});
|
|
2973
|
+
});
|
|
2974
|
+
transport.errors.handle((err) => {
|
|
2975
|
+
next({
|
|
2976
|
+
connection: {
|
|
2977
|
+
state: import_bridge.ConnectionState.CLOSED,
|
|
2978
|
+
error: err.toString()
|
|
2979
|
+
}
|
|
2980
|
+
});
|
|
2981
|
+
close(err);
|
|
2982
|
+
});
|
|
2983
|
+
transport.closed.on(() => {
|
|
2984
|
+
next({
|
|
2985
|
+
connection: {
|
|
2986
|
+
state: import_bridge.ConnectionState.CLOSED
|
|
2987
|
+
}
|
|
2988
|
+
});
|
|
2989
|
+
close();
|
|
2990
|
+
});
|
|
2991
|
+
const transportState = {
|
|
2992
|
+
transport,
|
|
2993
|
+
stream: duplex,
|
|
2994
|
+
writeCallbacks: [],
|
|
2995
|
+
state: "OPEN"
|
|
2996
|
+
};
|
|
2997
|
+
ready();
|
|
2998
|
+
this.transports.set(request.proxyId, transportState);
|
|
2999
|
+
});
|
|
3000
|
+
return rpcStream;
|
|
3001
|
+
}
|
|
3002
|
+
async sendSignal({ proxyId, signal }) {
|
|
3003
|
+
(0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
|
|
3004
|
+
F: __dxlog_file13,
|
|
3005
|
+
L: 112,
|
|
3006
|
+
S: this,
|
|
3007
|
+
A: [
|
|
3008
|
+
"this.transports.has(proxyId)",
|
|
3009
|
+
""
|
|
3010
|
+
]
|
|
3011
|
+
});
|
|
3012
|
+
await this.transports.get(proxyId).transport.signal(signal);
|
|
3013
|
+
}
|
|
3014
|
+
async sendData({ proxyId, payload }) {
|
|
3015
|
+
if (this.transports.get(proxyId)?.state !== "OPEN") {
|
|
3016
|
+
import_log12.log.debug("transport is closed", void 0, {
|
|
3017
|
+
F: __dxlog_file13,
|
|
3018
|
+
L: 118,
|
|
3019
|
+
S: this,
|
|
3020
|
+
C: (f, a) => f(...a)
|
|
3021
|
+
});
|
|
3022
|
+
}
|
|
3023
|
+
(0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
|
|
3024
|
+
F: __dxlog_file13,
|
|
3025
|
+
L: 120,
|
|
3026
|
+
S: this,
|
|
3027
|
+
A: [
|
|
3028
|
+
"this.transports.has(proxyId)",
|
|
3029
|
+
""
|
|
3030
|
+
]
|
|
3031
|
+
});
|
|
3032
|
+
const state = this.transports.get(proxyId);
|
|
3033
|
+
const bufferHasSpace = state.stream.push(payload);
|
|
3034
|
+
if (!bufferHasSpace) {
|
|
3035
|
+
await new Promise((resolve) => {
|
|
3036
|
+
state.writeCallbacks.push(resolve);
|
|
3037
|
+
});
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
async close({ proxyId }) {
|
|
3041
|
+
await this.transports.get(proxyId)?.transport.destroy();
|
|
3042
|
+
await this.transports.get(proxyId)?.stream.end();
|
|
3043
|
+
if (this.transports.get(proxyId)) {
|
|
3044
|
+
this.transports.get(proxyId).state = "CLOSED";
|
|
3045
|
+
}
|
|
3046
|
+
(0, import_log12.log)("Closed.", void 0, {
|
|
3047
|
+
F: __dxlog_file13,
|
|
3048
|
+
L: 136,
|
|
3049
|
+
S: this,
|
|
3050
|
+
C: (f, a) => f(...a)
|
|
3051
|
+
});
|
|
3052
|
+
}
|
|
3053
|
+
};
|
|
3054
|
+
var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
|
|
3055
|
+
var RESP_MIN_THRESHOLD = 500;
|
|
3056
|
+
var TIMEOUT_THRESHOLD = 10;
|
|
3057
|
+
var SimplePeerTransportProxy = class {
|
|
3058
|
+
constructor(_params) {
|
|
3059
|
+
this._params = _params;
|
|
3060
|
+
this._proxyId = import_keys12.PublicKey.random();
|
|
3061
|
+
this._ctx = new import_context6.Context();
|
|
3062
|
+
this._timeoutCount = 0;
|
|
3063
|
+
this.closed = new import_async10.Event();
|
|
3064
|
+
this.connected = new import_async10.Event();
|
|
3065
|
+
this.errors = new import_debug6.ErrorStream();
|
|
3066
|
+
this._closed = false;
|
|
3067
|
+
this._serviceStream = this._params.bridgeService.open({
|
|
3068
|
+
proxyId: this._proxyId,
|
|
3069
|
+
initiator: this._params.initiator
|
|
3070
|
+
});
|
|
3071
|
+
this._serviceStream.waitUntilReady().then(() => {
|
|
3072
|
+
this._serviceStream.subscribe(async (event) => {
|
|
3073
|
+
(0, import_log13.log)("SimplePeerTransportProxy: event", event, {
|
|
3074
|
+
F: __dxlog_file14,
|
|
3075
|
+
L: 59,
|
|
3076
|
+
S: this,
|
|
3077
|
+
C: (f, a) => f(...a)
|
|
3078
|
+
});
|
|
3079
|
+
if (event.connection) {
|
|
3080
|
+
await this._handleConnection(event.connection);
|
|
3081
|
+
} else if (event.data) {
|
|
3082
|
+
this._handleData(event.data);
|
|
3083
|
+
} else if (event.signal) {
|
|
3084
|
+
await this._handleSignal(event.signal);
|
|
3085
|
+
}
|
|
3086
|
+
});
|
|
3087
|
+
const proxyStream = new import_node_stream3.Writable({
|
|
3088
|
+
write: (chunk, _, callback) => {
|
|
3089
|
+
const then = performance.now();
|
|
3090
|
+
this._params.bridgeService.sendData({
|
|
3091
|
+
proxyId: this._proxyId,
|
|
3092
|
+
payload: chunk
|
|
3093
|
+
}).then(() => {
|
|
3094
|
+
if (performance.now() - then > RESP_MIN_THRESHOLD) {
|
|
3095
|
+
(0, import_log13.log)("slow response, delaying callback", void 0, {
|
|
3096
|
+
F: __dxlog_file14,
|
|
3097
|
+
L: 80,
|
|
3098
|
+
S: this,
|
|
3099
|
+
C: (f, a) => f(...a)
|
|
3100
|
+
});
|
|
3101
|
+
(0, import_async10.scheduleTask)(this._ctx, () => callback(), RESP_MIN_THRESHOLD);
|
|
3102
|
+
} else {
|
|
3103
|
+
callback();
|
|
3104
|
+
}
|
|
3105
|
+
this._timeoutCount = 0;
|
|
3106
|
+
}, (err) => {
|
|
3107
|
+
if (err instanceof import_protocols8.TimeoutError || err.constructor.name === "TimeoutError") {
|
|
3108
|
+
if (this._timeoutCount++ > TIMEOUT_THRESHOLD) {
|
|
3109
|
+
throw new import_protocols8.TimeoutError(`too many timeoutes (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
|
|
3110
|
+
} else {
|
|
3111
|
+
(0, import_log13.log)("timeout error, but still invoking callback", void 0, {
|
|
3112
|
+
F: __dxlog_file14,
|
|
3113
|
+
L: 92,
|
|
3114
|
+
S: this,
|
|
3115
|
+
C: (f, a) => f(...a)
|
|
3116
|
+
});
|
|
3117
|
+
callback();
|
|
3118
|
+
}
|
|
3119
|
+
} else {
|
|
3120
|
+
import_log13.log.catch(err, void 0, {
|
|
3121
|
+
F: __dxlog_file14,
|
|
3122
|
+
L: 96,
|
|
3123
|
+
S: this,
|
|
3124
|
+
C: (f, a) => f(...a)
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
});
|
|
3128
|
+
}
|
|
3129
|
+
});
|
|
3130
|
+
proxyStream.on("error", (err) => {
|
|
3131
|
+
(0, import_log13.log)("proxystream error", {
|
|
3132
|
+
err
|
|
3133
|
+
}, {
|
|
3134
|
+
F: __dxlog_file14,
|
|
3135
|
+
L: 104,
|
|
3136
|
+
S: this,
|
|
3137
|
+
C: (f, a) => f(...a)
|
|
3138
|
+
});
|
|
3139
|
+
});
|
|
3140
|
+
this._params.stream.pipe(proxyStream);
|
|
3141
|
+
}, (error) => import_log13.log.catch(error, void 0, {
|
|
3142
|
+
F: __dxlog_file14,
|
|
3143
|
+
L: 109,
|
|
3144
|
+
S: this,
|
|
3145
|
+
C: (f, a) => f(...a)
|
|
3146
|
+
}));
|
|
3147
|
+
}
|
|
3148
|
+
async _handleConnection(connectionEvent) {
|
|
3149
|
+
if (connectionEvent.error) {
|
|
3150
|
+
this.errors.raise(decodeError(connectionEvent.error));
|
|
3151
|
+
}
|
|
3152
|
+
switch (connectionEvent.state) {
|
|
3153
|
+
case import_bridge2.ConnectionState.CONNECTED: {
|
|
3154
|
+
this.connected.emit();
|
|
3155
|
+
break;
|
|
3156
|
+
}
|
|
3157
|
+
case import_bridge2.ConnectionState.CLOSED: {
|
|
3158
|
+
await this.destroy();
|
|
3159
|
+
break;
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
}
|
|
3163
|
+
_handleData(dataEvent) {
|
|
3164
|
+
this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
|
|
3165
|
+
}
|
|
3166
|
+
async _handleSignal(signalEvent) {
|
|
3167
|
+
await this._params.sendSignal(signalEvent.payload);
|
|
3168
|
+
}
|
|
3169
|
+
signal(signal) {
|
|
3170
|
+
this._params.bridgeService.sendSignal({
|
|
3171
|
+
proxyId: this._proxyId,
|
|
3172
|
+
signal
|
|
3173
|
+
}).catch((err) => this.errors.raise(decodeError(err)));
|
|
3174
|
+
}
|
|
3175
|
+
// TODO(burdon): Move open from constructor.
|
|
3176
|
+
async destroy() {
|
|
3177
|
+
await this._ctx.dispose();
|
|
3178
|
+
if (this._closed) {
|
|
3179
|
+
return;
|
|
3180
|
+
}
|
|
3181
|
+
await this._serviceStream.close();
|
|
3182
|
+
try {
|
|
3183
|
+
await this._params.bridgeService.close({
|
|
3184
|
+
proxyId: this._proxyId
|
|
3185
|
+
});
|
|
3186
|
+
} catch (err) {
|
|
3187
|
+
import_log13.log.catch(err, void 0, {
|
|
3188
|
+
F: __dxlog_file14,
|
|
3189
|
+
L: 160,
|
|
3190
|
+
S: this,
|
|
3191
|
+
C: (f, a) => f(...a)
|
|
3192
|
+
});
|
|
3193
|
+
}
|
|
3194
|
+
this.closed.emit();
|
|
3195
|
+
this._closed = true;
|
|
3196
|
+
}
|
|
3197
|
+
/**
|
|
3198
|
+
* Called when underlying proxy service becomes unavailable.
|
|
3199
|
+
*/
|
|
3200
|
+
// TODO(burdon): Option on close method.
|
|
3201
|
+
forceClose() {
|
|
3202
|
+
void this._serviceStream.close();
|
|
3203
|
+
this.closed.emit();
|
|
3204
|
+
this._closed = true;
|
|
3205
|
+
}
|
|
3206
|
+
};
|
|
3207
|
+
var SimplePeerTransportProxyFactory = class {
|
|
3208
|
+
constructor() {
|
|
3209
|
+
this._connections = /* @__PURE__ */ new Set();
|
|
3210
|
+
}
|
|
3211
|
+
/**
|
|
3212
|
+
* Sets the current BridgeService to be used to open connections.
|
|
3213
|
+
* Calling this method will close any existing connections.
|
|
3214
|
+
*/
|
|
3215
|
+
setBridgeService(bridgeService) {
|
|
3216
|
+
this._bridgeService = bridgeService;
|
|
3217
|
+
for (const connection of this._connections) {
|
|
3218
|
+
connection.forceClose();
|
|
3219
|
+
}
|
|
3220
|
+
return this;
|
|
3221
|
+
}
|
|
3222
|
+
createTransport(options) {
|
|
3223
|
+
(0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
|
|
3224
|
+
F: __dxlog_file14,
|
|
3225
|
+
L: 197,
|
|
3226
|
+
S: this,
|
|
3227
|
+
A: [
|
|
3228
|
+
"this._bridgeService",
|
|
3229
|
+
"'SimplePeerTransportProxyFactory is not ready to open connections'"
|
|
3230
|
+
]
|
|
3231
|
+
});
|
|
3232
|
+
const transport = new SimplePeerTransportProxy({
|
|
3233
|
+
...options,
|
|
3234
|
+
bridgeService: this._bridgeService
|
|
3235
|
+
});
|
|
3236
|
+
this._connections.add(transport);
|
|
3237
|
+
transport.closed.on(() => this._connections.delete(transport));
|
|
3238
|
+
return transport;
|
|
3239
|
+
}
|
|
3240
|
+
};
|
|
3241
|
+
var decodeError = (err) => {
|
|
3242
|
+
const message = typeof err === "string" ? err : err.message;
|
|
3243
|
+
if (message.includes("CONNECTION_RESET")) {
|
|
3244
|
+
return new import_protocols8.ConnectionResetError(message);
|
|
3245
|
+
} else if (message.includes("TIMEOUT")) {
|
|
3246
|
+
return new import_protocols8.TimeoutError(message);
|
|
3247
|
+
} else if (message.includes("PROTOCOL_ERROR")) {
|
|
3248
|
+
return new import_protocols8.ProtocolError(message);
|
|
3249
|
+
} else if (message.includes("CONNECTIVITY_ERROR")) {
|
|
3250
|
+
return new import_protocols8.ConnectivityError(message);
|
|
3251
|
+
} else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
|
|
3252
|
+
return new import_protocols8.UnknownProtocolError(message);
|
|
3253
|
+
} else {
|
|
3254
|
+
return typeof err === "string" ? new Error(err) : err;
|
|
3255
|
+
}
|
|
3256
|
+
};
|
|
3257
|
+
function _ts_decorate5(decorators, target, key, desc) {
|
|
3258
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3259
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
3260
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
3261
|
+
else
|
|
3262
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
3263
|
+
if (d = decorators[i])
|
|
3264
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
3265
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
3266
|
+
}
|
|
3267
|
+
var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts";
|
|
3268
|
+
var DATACHANNEL_LABEL = "dxos.mesh.transport";
|
|
3269
|
+
var MAX_BUFFERED_AMOUNT = 64 * 1024;
|
|
3270
|
+
var MAX_MESSAGE_SIZE = 64 * 1024;
|
|
3271
|
+
var LibDataChannelTransport = class {
|
|
3272
|
+
constructor(params) {
|
|
3273
|
+
this.params = params;
|
|
3274
|
+
this._closed = false;
|
|
3275
|
+
this.closed = new import_async11.Event();
|
|
3276
|
+
this._connected = false;
|
|
3277
|
+
this.connected = new import_async11.Event();
|
|
3278
|
+
this.errors = new import_debug7.ErrorStream();
|
|
3279
|
+
this._readyForCandidates = new import_async11.Trigger();
|
|
3280
|
+
this._writeCallback = null;
|
|
3281
|
+
this._peer = (async () => {
|
|
3282
|
+
const { RTCPeerConnection: PeerConnection } = await import("./datachannel-53JZPGFA.cjs");
|
|
3283
|
+
if (this._closed) {
|
|
3284
|
+
this.errors.raise(new Error("connection already closed"));
|
|
3285
|
+
}
|
|
3286
|
+
const peer = new PeerConnection(params.webrtcConfig);
|
|
3287
|
+
peer.onicecandidateerror = (event) => {
|
|
3288
|
+
import_log14.log.error("peer.onicecandidateerror", {
|
|
3289
|
+
event
|
|
3290
|
+
}, {
|
|
3291
|
+
F: __dxlog_file15,
|
|
3292
|
+
L: 53,
|
|
3293
|
+
S: this,
|
|
3294
|
+
C: (f, a) => f(...a)
|
|
3295
|
+
});
|
|
3296
|
+
};
|
|
3297
|
+
peer.onconnectionstatechange = (event) => {
|
|
3298
|
+
import_log14.log.debug("peer.onconnectionstatechange", {
|
|
3299
|
+
event,
|
|
3300
|
+
peerConnectionState: peer.connectionState,
|
|
3301
|
+
transportConnectionState: this._connected
|
|
3302
|
+
}, {
|
|
3303
|
+
F: __dxlog_file15,
|
|
3304
|
+
L: 57,
|
|
3305
|
+
S: this,
|
|
3306
|
+
C: (f, a) => f(...a)
|
|
3307
|
+
});
|
|
3308
|
+
};
|
|
3309
|
+
peer.onicecandidate = async (event) => {
|
|
3310
|
+
import_log14.log.debug("peer.onicecandidate", {
|
|
3311
|
+
event
|
|
3312
|
+
}, {
|
|
3313
|
+
F: __dxlog_file15,
|
|
3314
|
+
L: 66,
|
|
3315
|
+
S: this,
|
|
3316
|
+
C: (f, a) => f(...a)
|
|
3317
|
+
});
|
|
3318
|
+
if (event.candidate) {
|
|
3319
|
+
try {
|
|
3320
|
+
await params.sendSignal({
|
|
3321
|
+
payload: {
|
|
3322
|
+
data: {
|
|
3323
|
+
type: "candidate",
|
|
3324
|
+
candidate: {
|
|
3325
|
+
candidate: event.candidate.candidate,
|
|
3326
|
+
// these fields never seem to be not null, but connecting to Chrome doesn't work if they are
|
|
3327
|
+
sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
|
|
3328
|
+
sdpMid: event.candidate.sdpMid ?? 0
|
|
3329
|
+
}
|
|
3330
|
+
}
|
|
3331
|
+
}
|
|
3332
|
+
});
|
|
3333
|
+
} catch (err) {
|
|
3334
|
+
import_log14.log.info("signaling errror", {
|
|
3335
|
+
err
|
|
3336
|
+
}, {
|
|
3337
|
+
F: __dxlog_file15,
|
|
3338
|
+
L: 83,
|
|
3339
|
+
S: this,
|
|
3340
|
+
C: (f, a) => f(...a)
|
|
3341
|
+
});
|
|
3342
|
+
}
|
|
3343
|
+
}
|
|
3344
|
+
};
|
|
3345
|
+
if (params.initiator) {
|
|
3346
|
+
peer.createOffer().then(async (offer) => {
|
|
3347
|
+
if (peer.connectionState !== "connecting") {
|
|
3348
|
+
import_log14.log.error("i am initiator but peer not in state connecting", {
|
|
3349
|
+
peer
|
|
3350
|
+
}, {
|
|
3351
|
+
F: __dxlog_file15,
|
|
3352
|
+
L: 92,
|
|
3353
|
+
S: this,
|
|
3354
|
+
C: (f, a) => f(...a)
|
|
3355
|
+
});
|
|
3356
|
+
this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
|
|
3357
|
+
}
|
|
3358
|
+
import_log14.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
|
|
3359
|
+
offer
|
|
3360
|
+
}, {
|
|
3361
|
+
F: __dxlog_file15,
|
|
3362
|
+
L: 95,
|
|
3363
|
+
S: this,
|
|
3364
|
+
C: (f, a) => f(...a)
|
|
3365
|
+
});
|
|
3366
|
+
await peer.setLocalDescription(offer);
|
|
3367
|
+
await params.sendSignal({
|
|
3368
|
+
payload: {
|
|
3369
|
+
data: {
|
|
3370
|
+
type: offer.type,
|
|
3371
|
+
sdp: offer.sdp
|
|
3372
|
+
}
|
|
3373
|
+
}
|
|
3374
|
+
});
|
|
3375
|
+
return offer;
|
|
3376
|
+
}).catch((err) => {
|
|
3377
|
+
this.errors.raise(err);
|
|
3378
|
+
});
|
|
3379
|
+
this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
|
|
3380
|
+
import_log14.log.debug("created data channel", void 0, {
|
|
3381
|
+
F: __dxlog_file15,
|
|
3382
|
+
L: 104,
|
|
3383
|
+
S: this,
|
|
3384
|
+
C: (f, a) => f(...a)
|
|
3385
|
+
});
|
|
3386
|
+
peer.ondatachannel = (event) => {
|
|
3387
|
+
this.errors.raise(new Error("got ondatachannel when i am the initiator?"));
|
|
3388
|
+
};
|
|
3389
|
+
} else {
|
|
3390
|
+
peer.ondatachannel = (event) => {
|
|
3391
|
+
import_log14.log.debug("peer.ondatachannel (non-initiator)", {
|
|
3392
|
+
event
|
|
3393
|
+
}, {
|
|
3394
|
+
F: __dxlog_file15,
|
|
3395
|
+
L: 110,
|
|
3396
|
+
S: this,
|
|
3397
|
+
C: (f, a) => f(...a)
|
|
3398
|
+
});
|
|
3399
|
+
if (event.channel.label !== DATACHANNEL_LABEL) {
|
|
3400
|
+
this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
|
|
3401
|
+
}
|
|
3402
|
+
this.handleChannel(event.channel);
|
|
3403
|
+
};
|
|
3404
|
+
}
|
|
3405
|
+
return peer;
|
|
3406
|
+
})();
|
|
3407
|
+
}
|
|
3408
|
+
handleChannel(dataChannel) {
|
|
3409
|
+
this._channel = dataChannel;
|
|
3410
|
+
this._channel.onopen = () => {
|
|
3411
|
+
import_log14.log.debug("dataChannel.onopen", void 0, {
|
|
3412
|
+
F: __dxlog_file15,
|
|
3413
|
+
L: 126,
|
|
3414
|
+
S: this,
|
|
3415
|
+
C: (f, a) => f(...a)
|
|
3416
|
+
});
|
|
3417
|
+
const duplex = new import_stream.Duplex({
|
|
3418
|
+
read: () => {
|
|
3419
|
+
},
|
|
3420
|
+
write: (chunk, encoding, callback) => {
|
|
3421
|
+
if (chunk.length > MAX_MESSAGE_SIZE) {
|
|
3422
|
+
this.errors.raise(new Error(`message too large: ${chunk.length} > ${MAX_MESSAGE_SIZE}`));
|
|
3423
|
+
}
|
|
3424
|
+
dataChannel.send(chunk);
|
|
3425
|
+
if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
|
|
3426
|
+
if (this._writeCallback !== null) {
|
|
3427
|
+
import_log14.log.error("consumer trying to write before we're ready for more data", void 0, {
|
|
3428
|
+
F: __dxlog_file15,
|
|
3429
|
+
L: 139,
|
|
3430
|
+
S: this,
|
|
3431
|
+
C: (f, a) => f(...a)
|
|
3432
|
+
});
|
|
3433
|
+
}
|
|
3434
|
+
this._writeCallback = callback;
|
|
3435
|
+
} else {
|
|
3436
|
+
callback();
|
|
3437
|
+
}
|
|
3438
|
+
}
|
|
3439
|
+
});
|
|
3440
|
+
duplex.pipe(this.params.stream).pipe(duplex);
|
|
3441
|
+
this._stream = duplex;
|
|
3442
|
+
this._connected = true;
|
|
3443
|
+
this.connected.emit();
|
|
3444
|
+
};
|
|
3445
|
+
this._channel.onerror = async (err) => {
|
|
3446
|
+
this.errors.raise(new Error("channel error: " + err.toString()));
|
|
3447
|
+
await this._close();
|
|
3448
|
+
};
|
|
3449
|
+
this._channel.onclose = async (err) => {
|
|
3450
|
+
import_log14.log.info("channel onclose", {
|
|
3451
|
+
err
|
|
3452
|
+
}, {
|
|
3453
|
+
F: __dxlog_file15,
|
|
3454
|
+
L: 159,
|
|
3455
|
+
S: this,
|
|
3456
|
+
C: (f, a) => f(...a)
|
|
3457
|
+
});
|
|
3458
|
+
await this._close();
|
|
3459
|
+
};
|
|
3460
|
+
this._channel.onmessage = (event) => {
|
|
3461
|
+
let data = event.data;
|
|
3462
|
+
if (data instanceof ArrayBuffer) {
|
|
3463
|
+
data = Buffer.from(data);
|
|
3464
|
+
}
|
|
3465
|
+
this._stream.push(data);
|
|
3466
|
+
};
|
|
3467
|
+
this._channel.onbufferedamountlow = () => {
|
|
3468
|
+
const cb = this._writeCallback;
|
|
3469
|
+
this._writeCallback = null;
|
|
3470
|
+
cb?.();
|
|
3471
|
+
};
|
|
3472
|
+
}
|
|
3473
|
+
async _close() {
|
|
3474
|
+
if (this._closed) {
|
|
3475
|
+
return;
|
|
3476
|
+
}
|
|
3477
|
+
await this._disconnectStreams();
|
|
3478
|
+
this._closed = true;
|
|
3479
|
+
this.closed.emit();
|
|
3480
|
+
}
|
|
3481
|
+
signal(signal) {
|
|
3482
|
+
this._peer.then(async (peer) => {
|
|
3483
|
+
const data = signal.payload.data;
|
|
3484
|
+
switch (data.type) {
|
|
3485
|
+
case "offer": {
|
|
3486
|
+
if ((await this._peer).connectionState !== "new") {
|
|
3487
|
+
import_log14.log.error("received offer but peer not in state new", {
|
|
3488
|
+
peer
|
|
3489
|
+
}, {
|
|
3490
|
+
F: __dxlog_file15,
|
|
3491
|
+
L: 194,
|
|
3492
|
+
S: this,
|
|
3493
|
+
C: (f, a) => f(...a)
|
|
3494
|
+
});
|
|
3495
|
+
this.errors.raise(new Error("invalid signalling state: received offer when peer is not in state new"));
|
|
3496
|
+
break;
|
|
3497
|
+
}
|
|
3498
|
+
try {
|
|
3499
|
+
await peer.setRemoteDescription({
|
|
3500
|
+
type: data.type,
|
|
3501
|
+
sdp: data.sdp
|
|
3502
|
+
});
|
|
3503
|
+
const answer = await peer.createAnswer();
|
|
3504
|
+
await peer.setLocalDescription(answer);
|
|
3505
|
+
await this.params.sendSignal({
|
|
3506
|
+
payload: {
|
|
3507
|
+
data: {
|
|
3508
|
+
type: answer.type,
|
|
3509
|
+
sdp: answer.sdp
|
|
3510
|
+
}
|
|
3511
|
+
}
|
|
3512
|
+
});
|
|
3513
|
+
this._readyForCandidates.wake();
|
|
3514
|
+
} catch (err) {
|
|
3515
|
+
import_log14.log.error("can't handle offer from signalling server", {
|
|
3516
|
+
err
|
|
3517
|
+
}, {
|
|
3518
|
+
F: __dxlog_file15,
|
|
3519
|
+
L: 205,
|
|
3520
|
+
S: this,
|
|
3521
|
+
C: (f, a) => f(...a)
|
|
3522
|
+
});
|
|
3523
|
+
this.errors.raise(new Error("error handling offer"));
|
|
3524
|
+
}
|
|
3525
|
+
break;
|
|
3526
|
+
}
|
|
3527
|
+
case "answer":
|
|
3528
|
+
try {
|
|
3529
|
+
await peer.setRemoteDescription({
|
|
3530
|
+
type: data.type,
|
|
3531
|
+
sdp: data.sdp
|
|
3532
|
+
});
|
|
3533
|
+
this._readyForCandidates.wake();
|
|
3534
|
+
} catch (err) {
|
|
3535
|
+
import_log14.log.error("can't handle answer from signalling server", {
|
|
3536
|
+
err
|
|
3537
|
+
}, {
|
|
3538
|
+
F: __dxlog_file15,
|
|
3539
|
+
L: 216,
|
|
3540
|
+
S: this,
|
|
3541
|
+
C: (f, a) => f(...a)
|
|
3542
|
+
});
|
|
3543
|
+
this.errors.raise(new Error("error handling answer"));
|
|
3544
|
+
}
|
|
3545
|
+
break;
|
|
3546
|
+
case "candidate":
|
|
3547
|
+
await this._readyForCandidates.wait();
|
|
3548
|
+
await peer.addIceCandidate({
|
|
3549
|
+
candidate: data.candidate.candidate
|
|
3550
|
+
});
|
|
3551
|
+
break;
|
|
3552
|
+
default:
|
|
3553
|
+
import_log14.log.error("unhandled signal type", {
|
|
3554
|
+
type: data.type,
|
|
3555
|
+
signal
|
|
3556
|
+
}, {
|
|
3557
|
+
F: __dxlog_file15,
|
|
3558
|
+
L: 227,
|
|
3559
|
+
S: this,
|
|
3560
|
+
C: (f, a) => f(...a)
|
|
3561
|
+
});
|
|
3562
|
+
this.errors.raise(new Error(`unhandled signal type ${data.type}`));
|
|
3563
|
+
}
|
|
3564
|
+
}).catch((err) => {
|
|
3565
|
+
import_log14.log.catch(err, void 0, {
|
|
3566
|
+
F: __dxlog_file15,
|
|
3567
|
+
L: 232,
|
|
3568
|
+
S: this,
|
|
3569
|
+
C: (f, a) => f(...a)
|
|
3570
|
+
});
|
|
3571
|
+
});
|
|
3572
|
+
}
|
|
3573
|
+
// TODO(nf): add classmethod to call node-datachannel.cleanup() when all instances have been destroyed?
|
|
3574
|
+
async destroy() {
|
|
3575
|
+
await this._close();
|
|
3576
|
+
}
|
|
3577
|
+
async _disconnectStreams() {
|
|
3578
|
+
this.params.stream.unpipe?.(this._stream)?.unpipe?.(this.params.stream);
|
|
3579
|
+
}
|
|
3580
|
+
};
|
|
3581
|
+
_ts_decorate5([
|
|
3582
|
+
import_async11.synchronized
|
|
3583
|
+
], LibDataChannelTransport.prototype, "_close", null);
|
|
3584
|
+
var createLibDataChannelTransportFactory = (webrtcConfig) => ({
|
|
3585
|
+
createTransport: (params) => new LibDataChannelTransport({
|
|
3586
|
+
...params,
|
|
3587
|
+
webrtcConfig
|
|
3588
|
+
})
|
|
3589
|
+
});
|
|
3590
|
+
var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/tcp-transport.ts";
|
|
3591
|
+
var TcpTransportFactory = {
|
|
3592
|
+
createTransport: (params) => new TcpTransport(params)
|
|
3593
|
+
};
|
|
3594
|
+
var TcpTransport = class {
|
|
3595
|
+
constructor(options) {
|
|
3596
|
+
this.options = options;
|
|
3597
|
+
this._server = void 0;
|
|
3598
|
+
this._socket = void 0;
|
|
3599
|
+
this.closed = new import_async12.Event();
|
|
3600
|
+
this.connected = new import_async12.Event();
|
|
3601
|
+
this.errors = new import_debug8.ErrorStream();
|
|
3602
|
+
this._destroyed = false;
|
|
3603
|
+
this._connected = false;
|
|
3604
|
+
(0, import_log15.log)("creating", void 0, {
|
|
3605
|
+
F: __dxlog_file16,
|
|
3606
|
+
L: 34,
|
|
3607
|
+
S: this,
|
|
3608
|
+
C: (f, a) => f(...a)
|
|
3609
|
+
});
|
|
3610
|
+
if (this.options.initiator) {
|
|
3611
|
+
setTimeout(async () => {
|
|
3612
|
+
const { Server } = await import("node:net");
|
|
3613
|
+
this._server = new Server((socket) => {
|
|
3614
|
+
(0, import_log15.log)("new connection", void 0, {
|
|
3615
|
+
F: __dxlog_file16,
|
|
3616
|
+
L: 42,
|
|
3617
|
+
S: this,
|
|
3618
|
+
C: (f, a) => f(...a)
|
|
3619
|
+
});
|
|
3620
|
+
if (this._connected) {
|
|
3621
|
+
socket.destroy();
|
|
3622
|
+
}
|
|
3623
|
+
this._handleSocket(socket);
|
|
3624
|
+
});
|
|
3625
|
+
this._server.on("listening", () => {
|
|
3626
|
+
const { port } = this._server.address();
|
|
3627
|
+
(0, import_log15.log)("listening", {
|
|
3628
|
+
port
|
|
3629
|
+
}, {
|
|
3630
|
+
F: __dxlog_file16,
|
|
3631
|
+
L: 51,
|
|
3632
|
+
S: this,
|
|
3633
|
+
C: (f, a) => f(...a)
|
|
3634
|
+
});
|
|
3635
|
+
void this.options.sendSignal({
|
|
3636
|
+
payload: {
|
|
3637
|
+
port
|
|
3638
|
+
}
|
|
3639
|
+
}).catch((err) => {
|
|
3640
|
+
if (!this._destroyed) {
|
|
3641
|
+
this.errors.raise(err);
|
|
3642
|
+
}
|
|
3643
|
+
});
|
|
3644
|
+
});
|
|
3645
|
+
this._server.on("error", (err) => {
|
|
3646
|
+
this.errors.raise(err);
|
|
3647
|
+
});
|
|
3648
|
+
this._server.listen(0);
|
|
3649
|
+
});
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
async destroy() {
|
|
3653
|
+
(0, import_log15.log)("closing", void 0, {
|
|
3654
|
+
F: __dxlog_file16,
|
|
3655
|
+
L: 72,
|
|
3656
|
+
S: this,
|
|
3657
|
+
C: (f, a) => f(...a)
|
|
3658
|
+
});
|
|
3659
|
+
this._socket?.destroy();
|
|
3660
|
+
this._server?.close();
|
|
3661
|
+
this._destroyed = true;
|
|
3662
|
+
}
|
|
3663
|
+
signal({ payload }) {
|
|
3664
|
+
(0, import_log15.log)("received signal", {
|
|
3665
|
+
payload
|
|
3666
|
+
}, {
|
|
3667
|
+
F: __dxlog_file16,
|
|
3668
|
+
L: 80,
|
|
3669
|
+
S: this,
|
|
3670
|
+
C: (f, a) => f(...a)
|
|
3671
|
+
});
|
|
3672
|
+
if (this.options.initiator || this._connected) {
|
|
3673
|
+
return;
|
|
3674
|
+
}
|
|
3675
|
+
const socket = new import_node_net.Socket();
|
|
3676
|
+
this._handleSocket(socket);
|
|
3677
|
+
socket.connect({
|
|
3678
|
+
port: payload.port,
|
|
3679
|
+
host: "localhost"
|
|
3680
|
+
});
|
|
3681
|
+
}
|
|
3682
|
+
_handleSocket(socket) {
|
|
3683
|
+
(0, import_log15.log)("handling socket", {
|
|
3684
|
+
remotePort: socket.remotePort,
|
|
3685
|
+
localPort: socket.localPort
|
|
3686
|
+
}, {
|
|
3687
|
+
F: __dxlog_file16,
|
|
3688
|
+
L: 91,
|
|
3689
|
+
S: this,
|
|
3690
|
+
C: (f, a) => f(...a)
|
|
3691
|
+
});
|
|
3692
|
+
this._socket = socket;
|
|
3693
|
+
this.connected.emit();
|
|
3694
|
+
this.options.stream.pipe(this._socket).pipe(this.options.stream);
|
|
3695
|
+
this._socket.on("connect", () => {
|
|
3696
|
+
(0, import_log15.log)("connected to", {
|
|
3697
|
+
port: this._socket?.remotePort
|
|
3698
|
+
}, {
|
|
3699
|
+
F: __dxlog_file16,
|
|
3700
|
+
L: 97,
|
|
3701
|
+
S: this,
|
|
3702
|
+
C: (f, a) => f(...a)
|
|
3703
|
+
});
|
|
3704
|
+
this._connected = true;
|
|
3705
|
+
});
|
|
3706
|
+
this._socket.on("error", (err) => {
|
|
3707
|
+
this.errors.raise(err);
|
|
3708
|
+
});
|
|
3709
|
+
this._socket.on("close", () => {
|
|
3710
|
+
this.closed.emit();
|
|
3711
|
+
});
|
|
3712
|
+
}
|
|
3713
|
+
};
|
|
3714
|
+
var createTeleportProtocolFactory = (onConnection) => {
|
|
3715
|
+
return (params) => {
|
|
3716
|
+
const teleport = new import_teleport.Teleport(params);
|
|
3717
|
+
return {
|
|
3718
|
+
stream: teleport.stream,
|
|
3719
|
+
open: async () => {
|
|
3720
|
+
await teleport.open();
|
|
3721
|
+
await onConnection(teleport);
|
|
3722
|
+
},
|
|
3723
|
+
close: async () => {
|
|
3724
|
+
await teleport.close();
|
|
3725
|
+
},
|
|
3726
|
+
abort: async () => {
|
|
3727
|
+
await teleport.abort();
|
|
3728
|
+
}
|
|
3729
|
+
};
|
|
3730
|
+
};
|
|
3731
|
+
};
|
|
3732
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
3733
|
+
0 && (module.exports = {
|
|
3734
|
+
Connection,
|
|
3735
|
+
ConnectionLimiter,
|
|
3736
|
+
ConnectionLog,
|
|
3737
|
+
ConnectionState,
|
|
3738
|
+
EventType,
|
|
3739
|
+
FullyConnectedTopology,
|
|
3740
|
+
LibDataChannelTransport,
|
|
3741
|
+
MAX_CONCURRENT_INITIATING_CONNECTIONS,
|
|
3742
|
+
MMSTTopology,
|
|
3743
|
+
MemoryTransport,
|
|
3744
|
+
MemoryTransportFactory,
|
|
3745
|
+
NetworkManager,
|
|
3746
|
+
SimplePeerTransport,
|
|
3747
|
+
SimplePeerTransportProxy,
|
|
3748
|
+
SimplePeerTransportProxyFactory,
|
|
3749
|
+
SimplePeerTransportService,
|
|
3750
|
+
StarTopology,
|
|
3751
|
+
Swarm,
|
|
3752
|
+
SwarmMapper,
|
|
3753
|
+
SwarmMessenger,
|
|
3754
|
+
TcpTransport,
|
|
3755
|
+
TcpTransportFactory,
|
|
3756
|
+
TransportKind,
|
|
3757
|
+
createLibDataChannelTransportFactory,
|
|
3758
|
+
createSimplePeerTransportFactory,
|
|
3759
|
+
createTeleportProtocolFactory
|
|
3760
|
+
});
|
|
3761
|
+
//# sourceMappingURL=chunk-PWHTYBOL.cjs.map
|