@dxos/network-manager 0.3.9-main.dd8d283 → 0.3.9-main.e6215fc

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.
Files changed (55) hide show
  1. package/dist/lib/browser/{chunk-QJNZBY3Q.mjs → chunk-HJ6JLRZM.mjs} +293 -78
  2. package/dist/lib/browser/chunk-HJ6JLRZM.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +1 -1
  6. package/dist/lib/node/{chunk-XE5JOIGD.cjs → chunk-JDKM4UPJ.cjs} +21 -13
  7. package/dist/lib/node/chunk-JDKM4UPJ.cjs.map +7 -0
  8. package/dist/lib/node/{chunk-RCWLBTUN.cjs → chunk-XZF4MAFB.cjs} +295 -70
  9. package/dist/lib/node/chunk-XZF4MAFB.cjs.map +7 -0
  10. package/dist/lib/node/{datachannel-53JZPGFA.cjs → datachannel-3VTTIMR5.cjs} +9 -9
  11. package/dist/lib/node/{datachannel-53JZPGFA.cjs.map → datachannel-3VTTIMR5.cjs.map} +2 -2
  12. package/dist/lib/node/index.cjs +32 -32
  13. package/dist/lib/node/index.cjs.map +1 -1
  14. package/dist/lib/node/meta.json +1 -1
  15. package/dist/lib/node/testing/index.cjs +19 -19
  16. package/dist/types/src/connection-log.d.ts.map +1 -1
  17. package/dist/types/src/swarm/connection.d.ts +4 -1
  18. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  19. package/dist/types/src/transport/datachannel/rtc-peer-connection.d.ts +7 -0
  20. package/dist/types/src/transport/datachannel/rtc-peer-connection.d.ts.map +1 -1
  21. package/dist/types/src/transport/libdatachannel-transport.d.ts +3 -1
  22. package/dist/types/src/transport/libdatachannel-transport.d.ts.map +1 -1
  23. package/dist/types/src/transport/memory-transport.d.ts +3 -1
  24. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  25. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts +3 -1
  26. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  27. package/dist/types/src/transport/simplepeer-transport-service.d.ts +3 -1
  28. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  29. package/dist/types/src/transport/simplepeer-transport.d.ts +7 -1
  30. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  31. package/dist/types/src/transport/tcp-transport.browser.d.ts +3 -1
  32. package/dist/types/src/transport/tcp-transport.browser.d.ts.map +1 -1
  33. package/dist/types/src/transport/tcp-transport.d.ts +3 -1
  34. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -1
  35. package/dist/types/src/transport/transport.d.ts +17 -0
  36. package/dist/types/src/transport/transport.d.ts.map +1 -1
  37. package/dist/types/src/wire-protocol.d.ts +1 -1
  38. package/dist/types/src/wire-protocol.d.ts.map +1 -1
  39. package/package.json +17 -17
  40. package/src/connection-log.ts +14 -1
  41. package/src/swarm/connection.ts +40 -6
  42. package/src/transport/datachannel/rtc-peer-connection.ts +20 -12
  43. package/src/transport/libdatachannel-transport.test.ts +0 -4
  44. package/src/transport/libdatachannel-transport.ts +37 -1
  45. package/src/transport/memory-transport.ts +14 -1
  46. package/src/transport/simplepeer-transport-proxy.ts +9 -1
  47. package/src/transport/simplepeer-transport-service.ts +14 -0
  48. package/src/transport/simplepeer-transport.ts +70 -3
  49. package/src/transport/tcp-transport.browser.ts +9 -1
  50. package/src/transport/tcp-transport.ts +19 -1
  51. package/src/transport/transport.ts +19 -0
  52. package/src/wire-protocol.ts +3 -3
  53. package/dist/lib/browser/chunk-QJNZBY3Q.mjs.map +0 -7
  54. package/dist/lib/node/chunk-RCWLBTUN.cjs.map +0 -7
  55. package/dist/lib/node/chunk-XE5JOIGD.cjs.map +0 -7
@@ -4,13 +4,13 @@ import {
4
4
  } from "./chunk-YERMWDRB.mjs";
5
5
 
6
6
  // packages/core/mesh/network-manager/src/swarm/connection.ts
7
- import { DeferredTask, Event, sleep, scheduleTask, synchronized } from "@dxos/async";
7
+ import { DeferredTask, Event, sleep, scheduleTask, scheduleTaskInterval, synchronized } from "@dxos/async";
8
8
  import { Context, cancelWithContext } from "@dxos/context";
9
9
  import { ErrorStream } from "@dxos/debug";
10
10
  import { invariant } from "@dxos/invariant";
11
11
  import { PublicKey } from "@dxos/keys";
12
- import { log } from "@dxos/log";
13
- import { CancelledError, ProtocolError, ConnectionResetError, ConnectivityError, UnknownProtocolError, trace } from "@dxos/protocols";
12
+ import { log, logInfo } from "@dxos/log";
13
+ import { CancelledError, ProtocolError, ConnectionResetError, ConnectivityError, TimeoutError, UnknownProtocolError, trace } from "@dxos/protocols";
14
14
  function _ts_decorate(decorators, target, key, desc) {
15
15
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16
16
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -24,6 +24,7 @@ function _ts_decorate(decorators, target, key, desc) {
24
24
  var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection.ts";
25
25
  var STARTING_SIGNALLING_DELAY = 10;
26
26
  var TRANSPORT_CONNECTION_TIMEOUT = 1e4;
27
+ var TRANSPORT_STATS_INTERVAL = 5e3;
27
28
  var MAX_SIGNALLING_DELAY = 300;
28
29
  var ConnectionState;
29
30
  (function(ConnectionState5) {
@@ -86,6 +87,7 @@ var Connection = class {
86
87
  this.stateChanged = new Event();
87
88
  this.errors = new ErrorStream();
88
89
  this._instanceId = PublicKey.random().toHex();
90
+ this.transportStats = new Event();
89
91
  this._signalSendTask = new DeferredTask(this._ctx, async () => {
90
92
  await this._flushSignalBuffer();
91
93
  });
@@ -98,11 +100,14 @@ var Connection = class {
98
100
  initiator: this.initiator
99
101
  }, {
100
102
  F: __dxlog_file,
101
- L: 129,
103
+ L: 134,
102
104
  S: this,
103
105
  C: (f, a) => f(...a)
104
106
  });
105
107
  }
108
+ get sessionIdString() {
109
+ return this.sessionId.truncate();
110
+ }
106
111
  get state() {
107
112
  return this._state;
108
113
  }
@@ -118,7 +123,7 @@ var Connection = class {
118
123
  async openConnection() {
119
124
  invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
120
125
  F: __dxlog_file,
121
- L: 154,
126
+ L: 164,
122
127
  S: this,
123
128
  A: [
124
129
  "this._state === ConnectionState.INITIAL",
@@ -129,7 +134,7 @@ var Connection = class {
129
134
  id: this._instanceId
130
135
  }), {
131
136
  F: __dxlog_file,
132
- L: 155,
137
+ L: 165,
133
138
  S: this,
134
139
  C: (f, a) => f(...a)
135
140
  });
@@ -141,35 +146,35 @@ var Connection = class {
141
146
  initiator: this.initiator
142
147
  }, {
143
148
  F: __dxlog_file,
144
- L: 156,
149
+ L: 166,
145
150
  S: this,
146
151
  C: (f, a) => f(...a)
147
152
  });
148
153
  this._changeState(ConnectionState.CONNECTING);
149
- this._protocol.open().catch((err) => {
154
+ this._protocol.open(this.sessionId).catch((err) => {
150
155
  this.errors.raise(err);
151
156
  });
152
157
  this._protocol.stream.on("close", () => {
153
158
  log("protocol stream closed", void 0, {
154
159
  F: __dxlog_file,
155
- L: 173,
160
+ L: 183,
156
161
  S: this,
157
162
  C: (f, a) => f(...a)
158
163
  });
159
164
  this.close(new ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
160
165
  });
161
166
  scheduleTask(this.connectedTimeoutContext, async () => {
162
- log.info("timeout waiting for transport to connect, aborting", void 0, {
167
+ log.info(`timeout waiting ${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect, aborting`, void 0, {
163
168
  F: __dxlog_file,
164
- L: 180,
169
+ L: 190,
165
170
  S: this,
166
171
  C: (f, a) => f(...a)
167
172
  });
168
- await this.abort().catch((err) => this.errors.raise(err));
173
+ await this.abort(new TimeoutError(`${TRANSPORT_CONNECTION_TIMEOUT / 1e3}s for transport to connect`)).catch((err) => this.errors.raise(err));
169
174
  }, TRANSPORT_CONNECTION_TIMEOUT);
170
175
  invariant(!this._transport, void 0, {
171
176
  F: __dxlog_file,
172
- L: 186,
177
+ L: 198,
173
178
  S: this,
174
179
  A: [
175
180
  "!this._transport",
@@ -179,18 +184,20 @@ var Connection = class {
179
184
  this._transport = this._transportFactory.createTransport({
180
185
  initiator: this.initiator,
181
186
  stream: this._protocol.stream,
182
- sendSignal: async (signal) => this._sendSignal(signal)
187
+ sendSignal: async (signal) => this._sendSignal(signal),
188
+ sessionId: this.sessionId
183
189
  });
184
190
  this._transport.connected.once(async () => {
185
191
  this._changeState(ConnectionState.CONNECTED);
186
192
  await this.connectedTimeoutContext.dispose();
187
193
  this._callbacks?.onConnected?.();
194
+ scheduleTaskInterval(this._ctx, async () => this._emitTransportStats(), TRANSPORT_STATS_INTERVAL);
188
195
  });
189
196
  this._transport.closed.once(() => {
190
197
  this._transport = void 0;
191
198
  log("abort triggered by transport close", void 0, {
192
199
  F: __dxlog_file,
193
- L: 201,
200
+ L: 216,
194
201
  S: this,
195
202
  C: (f, a) => f(...a)
196
203
  });
@@ -201,7 +208,7 @@ var Connection = class {
201
208
  err
202
209
  }, {
203
210
  F: __dxlog_file,
204
- L: 206,
211
+ L: 221,
205
212
  S: this,
206
213
  C: (f, a) => f(...a)
207
214
  });
@@ -211,7 +218,7 @@ var Connection = class {
211
218
  if (err instanceof ConnectionResetError) {
212
219
  log.info("aborting due to transport ConnectionResetError", void 0, {
213
220
  F: __dxlog_file,
214
- L: 213,
221
+ L: 228,
215
222
  S: this,
216
223
  C: (f, a) => f(...a)
217
224
  });
@@ -219,7 +226,7 @@ var Connection = class {
219
226
  } else if (err instanceof ConnectivityError) {
220
227
  log.info("aborting due to transport ConnectivityError", void 0, {
221
228
  F: __dxlog_file,
222
- L: 216,
229
+ L: 231,
223
230
  S: this,
224
231
  C: (f, a) => f(...a)
225
232
  });
@@ -229,7 +236,7 @@ var Connection = class {
229
236
  err
230
237
  }, {
231
238
  F: __dxlog_file,
232
- L: 219,
239
+ L: 234,
233
240
  S: this,
234
241
  C: (f, a) => f(...a)
235
242
  });
@@ -247,7 +254,7 @@ var Connection = class {
247
254
  id: this._instanceId
248
255
  }), {
249
256
  F: __dxlog_file,
250
- L: 235,
257
+ L: 250,
251
258
  S: this,
252
259
  C: (f, a) => f(...a)
253
260
  });
@@ -257,14 +264,14 @@ var Connection = class {
257
264
  err
258
265
  }, {
259
266
  F: __dxlog_file,
260
- L: 242,
267
+ L: 257,
261
268
  S: this,
262
269
  C: (f, a) => f(...a)
263
270
  });
264
271
  if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
265
272
  log(`abort ignored: already ${this._state}`, this.closeReason, {
266
273
  F: __dxlog_file,
267
- L: 244,
274
+ L: 259,
268
275
  S: this,
269
276
  C: (f, a) => f(...a)
270
277
  });
@@ -279,7 +286,7 @@ var Connection = class {
279
286
  try {
280
287
  log("aborting protocol... ", void 0, {
281
288
  F: __dxlog_file,
282
- L: 258,
289
+ L: 273,
283
290
  S: this,
284
291
  C: (f, a) => f(...a)
285
292
  });
@@ -287,7 +294,7 @@ var Connection = class {
287
294
  } catch (err2) {
288
295
  log.catch(err2, void 0, {
289
296
  F: __dxlog_file,
290
- L: 261,
297
+ L: 276,
291
298
  S: this,
292
299
  C: (f, a) => f(...a)
293
300
  });
@@ -297,7 +304,7 @@ var Connection = class {
297
304
  } catch (err2) {
298
305
  log.catch(err2, void 0, {
299
306
  F: __dxlog_file,
300
- L: 268,
307
+ L: 283,
301
308
  S: this,
302
309
  C: (f, a) => f(...a)
303
310
  });
@@ -307,7 +314,7 @@ var Connection = class {
307
314
  } catch (err2) {
308
315
  log.catch(err2, void 0, {
309
316
  F: __dxlog_file,
310
- L: 274,
317
+ L: 289,
311
318
  S: this,
312
319
  C: (f, a) => f(...a)
313
320
  });
@@ -331,7 +338,7 @@ var Connection = class {
331
338
  peerId: this.ownId
332
339
  }, {
333
340
  F: __dxlog_file,
334
- L: 299,
341
+ L: 314,
335
342
  S: this,
336
343
  C: (f, a) => f(...a)
337
344
  });
@@ -341,7 +348,7 @@ var Connection = class {
341
348
  } catch (err2) {
342
349
  log.catch(err2, void 0, {
343
350
  F: __dxlog_file,
344
- L: 306,
351
+ L: 321,
345
352
  S: this,
346
353
  C: (f, a) => f(...a)
347
354
  });
@@ -351,7 +358,34 @@ var Connection = class {
351
358
  } catch (err2) {
352
359
  log.catch(err2, void 0, {
353
360
  F: __dxlog_file,
354
- L: 313,
361
+ L: 328,
362
+ S: this,
363
+ C: (f, a) => f(...a)
364
+ });
365
+ }
366
+ } else {
367
+ log.info(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
368
+ F: __dxlog_file,
369
+ L: 331,
370
+ S: this,
371
+ C: (f, a) => f(...a)
372
+ });
373
+ try {
374
+ await this._protocol.abort();
375
+ } catch (err2) {
376
+ log.catch(err2, void 0, {
377
+ F: __dxlog_file,
378
+ L: 335,
379
+ S: this,
380
+ C: (f, a) => f(...a)
381
+ });
382
+ }
383
+ try {
384
+ await this._transport?.destroy();
385
+ } catch (err2) {
386
+ log.catch(err2, void 0, {
387
+ F: __dxlog_file,
388
+ L: 340,
355
389
  S: this,
356
390
  C: (f, a) => f(...a)
357
391
  });
@@ -361,7 +395,7 @@ var Connection = class {
361
395
  peerId: this.ownId
362
396
  }, {
363
397
  F: __dxlog_file,
364
- L: 317,
398
+ L: 344,
365
399
  S: this,
366
400
  C: (f, a) => f(...a)
367
401
  });
@@ -404,7 +438,7 @@ var Connection = class {
404
438
  err
405
439
  }, {
406
440
  F: __dxlog_file,
407
- L: 355,
441
+ L: 382,
408
442
  S: this,
409
443
  C: (f, a) => f(...a)
410
444
  });
@@ -417,7 +451,7 @@ var Connection = class {
417
451
  async signal(msg) {
418
452
  invariant(msg.sessionId, void 0, {
419
453
  F: __dxlog_file,
420
- L: 364,
454
+ L: 391,
421
455
  S: this,
422
456
  A: [
423
457
  "msg.sessionId",
@@ -427,7 +461,7 @@ var Connection = class {
427
461
  if (!msg.sessionId.equals(this.sessionId)) {
428
462
  log("dropping signal for incorrect session id", void 0, {
429
463
  F: __dxlog_file,
430
- L: 366,
464
+ L: 393,
431
465
  S: this,
432
466
  C: (f, a) => f(...a)
433
467
  });
@@ -435,7 +469,7 @@ var Connection = class {
435
469
  }
436
470
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
437
471
  F: __dxlog_file,
438
- L: 369,
472
+ L: 396,
439
473
  S: this,
440
474
  A: [
441
475
  "msg.data.signal || msg.data.signalBatch",
@@ -444,7 +478,7 @@ var Connection = class {
444
478
  });
445
479
  invariant(msg.author?.equals(this.remoteId), void 0, {
446
480
  F: __dxlog_file,
447
- L: 370,
481
+ L: 397,
448
482
  S: this,
449
483
  A: [
450
484
  "msg.author?.equals(this.remoteId)",
@@ -453,7 +487,7 @@ var Connection = class {
453
487
  });
454
488
  invariant(msg.recipient?.equals(this.ownId), void 0, {
455
489
  F: __dxlog_file,
456
- L: 371,
490
+ L: 398,
457
491
  S: this,
458
492
  A: [
459
493
  "msg.recipient?.equals(this.ownId)",
@@ -477,7 +511,7 @@ var Connection = class {
477
511
  msg: msg.data
478
512
  }, {
479
513
  F: __dxlog_file,
480
- L: 380,
514
+ L: 407,
481
515
  S: this,
482
516
  C: (f, a) => f(...a)
483
517
  });
@@ -485,7 +519,7 @@ var Connection = class {
485
519
  } else {
486
520
  invariant(this._transport, "Connection not ready to accept signals.", {
487
521
  F: __dxlog_file,
488
- L: 383,
522
+ L: 410,
489
523
  S: this,
490
524
  A: [
491
525
  "this._transport",
@@ -498,7 +532,7 @@ var Connection = class {
498
532
  msg: msg.data
499
533
  }, {
500
534
  F: __dxlog_file,
501
- L: 384,
535
+ L: 411,
502
536
  S: this,
503
537
  C: (f, a) => f(...a)
504
538
  });
@@ -516,13 +550,13 @@ var Connection = class {
516
550
  peerId: this.ownId
517
551
  }, {
518
552
  F: __dxlog_file,
519
- L: 395,
553
+ L: 422,
520
554
  S: this,
521
555
  C: (f, a) => f(...a)
522
556
  });
523
557
  invariant(state !== this._state, "Already in this state.", {
524
558
  F: __dxlog_file,
525
- L: 396,
559
+ L: 423,
526
560
  S: this,
527
561
  A: [
528
562
  "state !== this._state",
@@ -532,7 +566,16 @@ var Connection = class {
532
566
  this._state = state;
533
567
  this.stateChanged.emit(state);
534
568
  }
569
+ async _emitTransportStats() {
570
+ const stats = await this.transport?.getStats();
571
+ if (stats) {
572
+ this.transportStats.emit(stats);
573
+ }
574
+ }
535
575
  };
576
+ _ts_decorate([
577
+ logInfo
578
+ ], Connection.prototype, "sessionIdString", null);
536
579
  _ts_decorate([
537
580
  synchronized
538
581
  ], Connection.prototype, "abort", null);
@@ -545,7 +588,7 @@ import { Context as Context2 } from "@dxos/context";
545
588
  import { invariant as invariant2 } from "@dxos/invariant";
546
589
  import { PublicKey as PublicKey2 } from "@dxos/keys";
547
590
  import { log as log2 } from "@dxos/log";
548
- import { schema, TimeoutError } from "@dxos/protocols";
591
+ import { schema, TimeoutError as TimeoutError2 } from "@dxos/protocols";
549
592
  import { ComplexMap } from "@dxos/util";
550
593
  var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/signal/swarm-messenger.ts";
551
594
  var SwarmMessage = schema.getCodecForType("dxos.mesh.swarm.SwarmMessage");
@@ -730,7 +773,7 @@ var SwarmMessenger = class {
730
773
  }
731
774
  });
732
775
  } catch (err) {
733
- if (err instanceof TimeoutError) {
776
+ if (err instanceof TimeoutError2) {
734
777
  log2.info("timeout sending answer to offer", {
735
778
  err
736
779
  }, {
@@ -789,7 +832,7 @@ import { Context as Context4 } from "@dxos/context";
789
832
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
790
833
  import { invariant as invariant4 } from "@dxos/invariant";
791
834
  import { PublicKey as PublicKey4 } from "@dxos/keys";
792
- import { log as log4, logInfo } from "@dxos/log";
835
+ import { log as log4, logInfo as logInfo2 } from "@dxos/log";
793
836
  import { trace as trace2 } from "@dxos/protocols";
794
837
  import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
795
838
 
@@ -1753,13 +1796,13 @@ var Swarm = class {
1753
1796
  }
1754
1797
  };
1755
1798
  _ts_decorate3([
1756
- logInfo
1799
+ logInfo2
1757
1800
  ], Swarm.prototype, "_instanceId", void 0);
1758
1801
  _ts_decorate3([
1759
- logInfo
1802
+ logInfo2
1760
1803
  ], Swarm.prototype, "ownPeerId", null);
1761
1804
  _ts_decorate3([
1762
- logInfo
1805
+ logInfo2
1763
1806
  ], Swarm.prototype, "topic", null);
1764
1807
  _ts_decorate3([
1765
1808
  synchronized3
@@ -1976,7 +2019,7 @@ var ConnectionLog = class {
1976
2019
  };
1977
2020
  info.connections.push(connectionInfo);
1978
2021
  this.update.emit();
1979
- connection.stateChanged.on((state) => {
2022
+ connection.stateChanged.on(async (state) => {
1980
2023
  connectionInfo.state = state;
1981
2024
  connectionInfo.closeReason = connection.closeReason;
1982
2025
  connectionInfo.lastUpdate = /* @__PURE__ */ new Date();
@@ -1984,6 +2027,10 @@ var ConnectionLog = class {
1984
2027
  type: EventType.CONNECTION_STATE_CHANGED,
1985
2028
  newState: state
1986
2029
  });
2030
+ if (state === ConnectionState.CONNECTED) {
2031
+ const details = await connection.transport?.getDetails();
2032
+ connectionInfo.transportDetails = details;
2033
+ }
1987
2034
  this.update.emit();
1988
2035
  });
1989
2036
  connection.protocol?.stats?.on((stats) => {
@@ -1993,6 +2040,12 @@ var ConnectionLog = class {
1993
2040
  connectionInfo.lastUpdate = /* @__PURE__ */ new Date();
1994
2041
  this.update.emit();
1995
2042
  });
2043
+ connection.transportStats?.on((stats) => {
2044
+ connectionInfo.transportBytesSent = stats.bytesSent;
2045
+ connectionInfo.transportBytesReceived = stats.bytesReceived;
2046
+ connectionInfo.transportPacketsSent = stats.packetsSent;
2047
+ connectionInfo.transportPacketsReceived = stats.packetsReceived;
2048
+ });
1996
2049
  gcSwarm(info);
1997
2050
  });
1998
2051
  }
@@ -2503,7 +2556,7 @@ import { Event as Event7, Trigger } from "@dxos/async";
2503
2556
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
2504
2557
  import { invariant as invariant10 } from "@dxos/invariant";
2505
2558
  import { PublicKey as PublicKey9 } from "@dxos/keys";
2506
- import { log as log10, logInfo as logInfo2 } from "@dxos/log";
2559
+ import { log as log10, logInfo as logInfo3 } from "@dxos/log";
2507
2560
  import { ComplexMap as ComplexMap7 } from "@dxos/util";
2508
2561
  function _ts_decorate4(decorators, target, key, desc) {
2509
2562
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -2678,12 +2731,23 @@ var MemoryTransport = class _MemoryTransport {
2678
2731
  this._remote.wake(remoteId);
2679
2732
  }
2680
2733
  }
2734
+ async getDetails() {
2735
+ return this._instanceId.toHex();
2736
+ }
2737
+ async getStats() {
2738
+ return {
2739
+ bytesSent: 0,
2740
+ bytesReceived: 0,
2741
+ packetsSent: 0,
2742
+ packetsReceived: 0
2743
+ };
2744
+ }
2681
2745
  };
2682
2746
  _ts_decorate4([
2683
- logInfo2
2747
+ logInfo3
2684
2748
  ], MemoryTransport.prototype, "_instanceId", void 0);
2685
2749
  _ts_decorate4([
2686
- logInfo2
2750
+ logInfo3
2687
2751
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2688
2752
 
2689
2753
  // packages/core/mesh/network-manager/src/transport/transport.ts
@@ -2715,6 +2779,9 @@ try {
2715
2779
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2716
2780
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
2717
2781
  var SimplePeerTransport = class {
2782
+ /**
2783
+ * @params opts.config formatted as per https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection
2784
+ */
2718
2785
  constructor(params) {
2719
2786
  this.params = params;
2720
2787
  this._closed = false;
@@ -2727,13 +2794,13 @@ var SimplePeerTransport = class {
2727
2794
  id: this._instanceId
2728
2795
  }), {
2729
2796
  F: __dxlog_file12,
2730
- L: 41,
2797
+ L: 44,
2731
2798
  S: this,
2732
2799
  C: (f, a) => f(...a)
2733
2800
  });
2734
2801
  log11("created connection", params, {
2735
2802
  F: __dxlog_file12,
2736
- L: 42,
2803
+ L: 45,
2737
2804
  S: this,
2738
2805
  C: (f, a) => f(...a)
2739
2806
  });
@@ -2746,7 +2813,7 @@ var SimplePeerTransport = class {
2746
2813
  this._peer.on("signal", async (data) => {
2747
2814
  log11("signal", data, {
2748
2815
  F: __dxlog_file12,
2749
- L: 51,
2816
+ L: 54,
2750
2817
  S: this,
2751
2818
  C: (f, a) => f(...a)
2752
2819
  });
@@ -2759,7 +2826,7 @@ var SimplePeerTransport = class {
2759
2826
  this._peer.on("connect", () => {
2760
2827
  log11("connected", void 0, {
2761
2828
  F: __dxlog_file12,
2762
- L: 56,
2829
+ L: 59,
2763
2830
  S: this,
2764
2831
  C: (f, a) => f(...a)
2765
2832
  });
@@ -2770,7 +2837,7 @@ var SimplePeerTransport = class {
2770
2837
  this._peer.on("close", async () => {
2771
2838
  log11("closed", void 0, {
2772
2839
  F: __dxlog_file12,
2773
- L: 63,
2840
+ L: 66,
2774
2841
  S: this,
2775
2842
  C: (f, a) => f(...a)
2776
2843
  });
@@ -2781,12 +2848,20 @@ var SimplePeerTransport = class {
2781
2848
  if (err.errorDetail === "sctp-failure") {
2782
2849
  this.errors.raise(new ConnectionResetError2("sctp-failure from RTCError", err));
2783
2850
  } else {
2851
+ log11.info("unknown RTCError", {
2852
+ err
2853
+ }, {
2854
+ F: __dxlog_file12,
2855
+ L: 77,
2856
+ S: this,
2857
+ C: (f, a) => f(...a)
2858
+ });
2784
2859
  this.errors.raise(new UnknownProtocolError2("unknown RTCError", err));
2785
2860
  }
2786
2861
  } else if ("code" in err) {
2787
2862
  log11.info("simple-peer error", err, {
2788
2863
  F: __dxlog_file12,
2789
- L: 78,
2864
+ L: 82,
2790
2865
  S: this,
2791
2866
  C: (f, a) => f(...a)
2792
2867
  });
@@ -2794,10 +2869,12 @@ var SimplePeerTransport = class {
2794
2869
  case "ERR_WEBRTC_SUPPORT":
2795
2870
  this.errors.raise(new ProtocolError2("WebRTC not supported", err));
2796
2871
  break;
2872
+ case "ERR_SIGNALING":
2873
+ this.errors.raise(new ConnectivityError2("signaling failure", err));
2874
+ break;
2797
2875
  case "ERR_ICE_CONNECTION_FAILURE":
2798
2876
  case "ERR_DATA_CHANNEL":
2799
2877
  case "ERR_CONNECTION_FAILURE":
2800
- case "ERR_SIGNALING":
2801
2878
  this.errors.raise(new ConnectivityError2("unknown communication failure", err));
2802
2879
  break;
2803
2880
  case "ERR_CREATE_OFFER":
@@ -2814,7 +2891,7 @@ var SimplePeerTransport = class {
2814
2891
  } else {
2815
2892
  log11.info("unknown peer connection error", err, {
2816
2893
  F: __dxlog_file12,
2817
- L: 102,
2894
+ L: 108,
2818
2895
  S: this,
2819
2896
  C: (f, a) => f(...a)
2820
2897
  });
@@ -2825,10 +2902,10 @@ var SimplePeerTransport = class {
2825
2902
  this._peer._pc.getStats().then((stats) => {
2826
2903
  log11.info("report after webrtc error", {
2827
2904
  config: this.params.webrtcConfig,
2828
- stats
2905
+ stats: Object.fromEntries(stats.entries())
2829
2906
  }, {
2830
2907
  F: __dxlog_file12,
2831
- L: 110,
2908
+ L: 116,
2832
2909
  S: this,
2833
2910
  C: (f, a) => f(...a)
2834
2911
  });
@@ -2837,7 +2914,7 @@ var SimplePeerTransport = class {
2837
2914
  } catch (err2) {
2838
2915
  log11.catch(err2, void 0, {
2839
2916
  F: __dxlog_file12,
2840
- L: 117,
2917
+ L: 123,
2841
2918
  S: this,
2842
2919
  C: (f, a) => f(...a)
2843
2920
  });
@@ -2848,15 +2925,68 @@ var SimplePeerTransport = class {
2848
2925
  id: this._instanceId
2849
2926
  }), {
2850
2927
  F: __dxlog_file12,
2851
- L: 122,
2928
+ L: 129,
2852
2929
  S: this,
2853
2930
  C: (f, a) => f(...a)
2854
2931
  });
2855
2932
  }
2933
+ async getStats() {
2934
+ const stats = await this._getStats();
2935
+ if (!stats) {
2936
+ return {
2937
+ bytesSent: 0,
2938
+ bytesReceived: 0,
2939
+ packetsSent: 0,
2940
+ packetsReceived: 0,
2941
+ rawStats: {}
2942
+ };
2943
+ }
2944
+ return {
2945
+ bytesSent: stats.transport.bytesSent,
2946
+ bytesReceived: stats.transport.bytesReceived,
2947
+ packetsSent: stats.transport.messagesSent,
2948
+ packetsReceived: stats.transport.messagesReceived,
2949
+ rawStats: stats.raw
2950
+ };
2951
+ }
2952
+ async _getStats() {
2953
+ if (typeof this._peer?._pc?.getStats !== "function") {
2954
+ return null;
2955
+ }
2956
+ return await this._peer._pc.getStats().then((stats) => {
2957
+ const statsEntries = Array.from(stats.entries());
2958
+ const transport = statsEntries.filter((s) => s[1].type === "transport")[0][1];
2959
+ const candidatePair = statsEntries.filter((s) => s[0] === transport.selectedCandidatePairId);
2960
+ let selectedCandidatePair;
2961
+ let remoteCandidate;
2962
+ if (candidatePair.length > 0) {
2963
+ selectedCandidatePair = candidatePair[0][1];
2964
+ remoteCandidate = statsEntries.filter((s) => s[0] === selectedCandidatePair.remoteCandidateId)[0][1];
2965
+ }
2966
+ return {
2967
+ datachannel: statsEntries.filter((s) => s[1].type === "data-channel")[0][1],
2968
+ transport,
2969
+ selectedCandidatePair,
2970
+ remoteCandidate,
2971
+ raw: Object.fromEntries(stats.entries())
2972
+ };
2973
+ });
2974
+ }
2975
+ async getDetails() {
2976
+ const stats = await this._getStats();
2977
+ const rc = stats?.remoteCandidate;
2978
+ if (!rc) {
2979
+ return "unavailable";
2980
+ }
2981
+ if (rc.relay) {
2982
+ return `${rc.ip}:${rc.port}/${rc.protocol} relay for XXX ${rc.candidateType}`;
2983
+ }
2984
+ return `${rc.ip}:${rc.port}/${rc.protocol} ${rc.candidateType}`;
2985
+ }
2856
2986
  async destroy() {
2857
2987
  log11("closing...", void 0, {
2858
2988
  F: __dxlog_file12,
2859
- L: 126,
2989
+ L: 192,
2860
2990
  S: this,
2861
2991
  C: (f, a) => f(...a)
2862
2992
  });
@@ -2866,10 +2996,11 @@ var SimplePeerTransport = class {
2866
2996
  this._closed = true;
2867
2997
  this._disconnectStreams();
2868
2998
  this._peer.destroy();
2999
+ this._closed = true;
2869
3000
  this.closed.emit();
2870
3001
  log11("closed", void 0, {
2871
3002
  F: __dxlog_file12,
2872
- L: 134,
3003
+ L: 201,
2873
3004
  S: this,
2874
3005
  C: (f, a) => f(...a)
2875
3006
  });
@@ -2984,7 +3115,7 @@ var SimplePeerTransportService = class {
2984
3115
  async sendSignal({ proxyId, signal }) {
2985
3116
  invariant12(this.transports.has(proxyId), void 0, {
2986
3117
  F: __dxlog_file13,
2987
- L: 112,
3118
+ L: 116,
2988
3119
  S: this,
2989
3120
  A: [
2990
3121
  "this.transports.has(proxyId)",
@@ -2993,18 +3124,46 @@ var SimplePeerTransportService = class {
2993
3124
  });
2994
3125
  await this.transports.get(proxyId).transport.signal(signal);
2995
3126
  }
3127
+ async getDetails({ proxyId }) {
3128
+ invariant12(this.transports.has(proxyId), void 0, {
3129
+ F: __dxlog_file13,
3130
+ L: 121,
3131
+ S: this,
3132
+ A: [
3133
+ "this.transports.has(proxyId)",
3134
+ ""
3135
+ ]
3136
+ });
3137
+ return {
3138
+ details: await this.transports.get(proxyId).transport.getDetails()
3139
+ };
3140
+ }
3141
+ async getStats({ proxyId }) {
3142
+ invariant12(this.transports.has(proxyId), void 0, {
3143
+ F: __dxlog_file13,
3144
+ L: 126,
3145
+ S: this,
3146
+ A: [
3147
+ "this.transports.has(proxyId)",
3148
+ ""
3149
+ ]
3150
+ });
3151
+ return {
3152
+ stats: await this.transports.get(proxyId).transport.getStats()
3153
+ };
3154
+ }
2996
3155
  async sendData({ proxyId, payload }) {
2997
3156
  if (this.transports.get(proxyId)?.state !== "OPEN") {
2998
3157
  log12.debug("transport is closed", void 0, {
2999
3158
  F: __dxlog_file13,
3000
- L: 118,
3159
+ L: 132,
3001
3160
  S: this,
3002
3161
  C: (f, a) => f(...a)
3003
3162
  });
3004
3163
  }
3005
3164
  invariant12(this.transports.has(proxyId), void 0, {
3006
3165
  F: __dxlog_file13,
3007
- L: 120,
3166
+ L: 134,
3008
3167
  S: this,
3009
3168
  A: [
3010
3169
  "this.transports.has(proxyId)",
@@ -3027,7 +3186,7 @@ var SimplePeerTransportService = class {
3027
3186
  }
3028
3187
  log12("Closed.", void 0, {
3029
3188
  F: __dxlog_file13,
3030
- L: 136,
3189
+ L: 150,
3031
3190
  S: this,
3032
3191
  C: (f, a) => f(...a)
3033
3192
  });
@@ -3042,7 +3201,7 @@ import { ErrorStream as ErrorStream5 } from "@dxos/debug";
3042
3201
  import { invariant as invariant13 } from "@dxos/invariant";
3043
3202
  import { PublicKey as PublicKey12 } from "@dxos/keys";
3044
3203
  import { log as log13 } from "@dxos/log";
3045
- import { ConnectionResetError as ConnectionResetError3, TimeoutError as TimeoutError2, ProtocolError as ProtocolError3, ConnectivityError as ConnectivityError3, UnknownProtocolError as UnknownProtocolError3 } from "@dxos/protocols";
3204
+ import { ConnectionResetError as ConnectionResetError3, TimeoutError as TimeoutError3, ProtocolError as ProtocolError3, ConnectivityError as ConnectivityError3, UnknownProtocolError as UnknownProtocolError3 } from "@dxos/protocols";
3046
3205
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
3047
3206
  import { arrayToBuffer } from "@dxos/util";
3048
3207
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
@@ -3098,9 +3257,9 @@ var SimplePeerTransportProxy = class {
3098
3257
  }
3099
3258
  this._timeoutCount = 0;
3100
3259
  }, (err) => {
3101
- if (err instanceof TimeoutError2 || err.constructor.name === "TimeoutError") {
3260
+ if (err instanceof TimeoutError3 || err.constructor.name === "TimeoutError") {
3102
3261
  if (this._timeoutCount++ > TIMEOUT_THRESHOLD) {
3103
- throw new TimeoutError2(`too many timeoutes (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
3262
+ throw new TimeoutError3(`too many timeoutes (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
3104
3263
  } else {
3105
3264
  log13("timeout error, but still invoking callback", void 0, {
3106
3265
  F: __dxlog_file14,
@@ -3166,6 +3325,16 @@ var SimplePeerTransportProxy = class {
3166
3325
  signal
3167
3326
  }).catch((err) => this.errors.raise(decodeError(err)));
3168
3327
  }
3328
+ async getDetails() {
3329
+ return (await this._params.bridgeService.getDetails({
3330
+ proxyId: this._proxyId
3331
+ })).details;
3332
+ }
3333
+ async getStats() {
3334
+ return (await this._params.bridgeService.getStats({
3335
+ proxyId: this._proxyId
3336
+ })).stats;
3337
+ }
3169
3338
  // TODO(burdon): Move open from constructor.
3170
3339
  async destroy() {
3171
3340
  await this._ctx.dispose();
@@ -3180,7 +3349,7 @@ var SimplePeerTransportProxy = class {
3180
3349
  } catch (err) {
3181
3350
  log13.catch(err, void 0, {
3182
3351
  F: __dxlog_file14,
3183
- L: 160,
3352
+ L: 168,
3184
3353
  S: this,
3185
3354
  C: (f, a) => f(...a)
3186
3355
  });
@@ -3216,7 +3385,7 @@ var SimplePeerTransportProxyFactory = class {
3216
3385
  createTransport(options) {
3217
3386
  invariant13(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3218
3387
  F: __dxlog_file14,
3219
- L: 197,
3388
+ L: 205,
3220
3389
  S: this,
3221
3390
  A: [
3222
3391
  "this._bridgeService",
@@ -3237,7 +3406,7 @@ var decodeError = (err) => {
3237
3406
  if (message.includes("CONNECTION_RESET")) {
3238
3407
  return new ConnectionResetError3(message);
3239
3408
  } else if (message.includes("TIMEOUT")) {
3240
- return new TimeoutError2(message);
3409
+ return new TimeoutError3(message);
3241
3410
  } else if (message.includes("PROTOCOL_ERROR")) {
3242
3411
  return new ProtocolError3(message);
3243
3412
  } else if (message.includes("CONNECTIVITY_ERROR")) {
@@ -3575,6 +3744,46 @@ var LibDataChannelTransport = class {
3575
3744
  });
3576
3745
  });
3577
3746
  }
3747
+ async getDetails() {
3748
+ return this._peer.then(async (peer) => {
3749
+ const cp = await peer.getSelectedCandidatePair();
3750
+ if (!cp) {
3751
+ return "unavailable";
3752
+ }
3753
+ return `${cp.remote.address}:${cp.remote.port}/${cp.remote.transportType} ${cp.remote.type}`;
3754
+ }).catch((err) => {
3755
+ log14.catch(err, void 0, {
3756
+ F: __dxlog_file15,
3757
+ L: 250,
3758
+ S: this,
3759
+ C: (f, a) => f(...a)
3760
+ });
3761
+ return "unavailable";
3762
+ });
3763
+ }
3764
+ async getStats() {
3765
+ return this._peer.then((peer) => {
3766
+ return {
3767
+ bytesSent: peer.bytesSent(),
3768
+ bytesReceived: peer.bytesReceived(),
3769
+ packetsSent: 0,
3770
+ packetsReceived: 0
3771
+ };
3772
+ }).catch((err) => {
3773
+ log14.catch(err, void 0, {
3774
+ F: __dxlog_file15,
3775
+ L: 266,
3776
+ S: this,
3777
+ C: (f, a) => f(...a)
3778
+ });
3779
+ return {
3780
+ bytesSent: 0,
3781
+ bytesReceived: 0,
3782
+ packetsSent: 0,
3783
+ packetsReceived: 0
3784
+ };
3785
+ });
3786
+ }
3578
3787
  // TODO(nf): add classmethod to call node-datachannel.cleanup() when all instances have been destroyed?
3579
3788
  async destroy() {
3580
3789
  await this._close();
@@ -3611,6 +3820,12 @@ var TcpTransport = class {
3611
3820
  signal() {
3612
3821
  throw new Error("Method not implemented.");
3613
3822
  }
3823
+ async getStats() {
3824
+ throw new Error("Method not implemented.");
3825
+ }
3826
+ async getDetails() {
3827
+ throw new Error("Method not implemented.");
3828
+ }
3614
3829
  };
3615
3830
 
3616
3831
  // packages/core/mesh/network-manager/src/wire-protocol.ts
@@ -3620,8 +3835,8 @@ var createTeleportProtocolFactory = (onConnection) => {
3620
3835
  const teleport = new Teleport(params);
3621
3836
  return {
3622
3837
  stream: teleport.stream,
3623
- open: async () => {
3624
- await teleport.open();
3838
+ open: async (sessionId) => {
3839
+ await teleport.open(sessionId);
3625
3840
  await onConnection(teleport);
3626
3841
  },
3627
3842
  close: async () => {
@@ -3662,4 +3877,4 @@ export {
3662
3877
  TcpTransport,
3663
3878
  createTeleportProtocolFactory
3664
3879
  };
3665
- //# sourceMappingURL=chunk-QJNZBY3Q.mjs.map
3880
+ //# sourceMappingURL=chunk-HJ6JLRZM.mjs.map