@dxos/network-manager 0.4.10-main.fa5a270 → 0.4.10-main.fd8ea31

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 (47) hide show
  1. package/dist/lib/browser/{chunk-KEN4UCZF.mjs → chunk-SOVZDG44.mjs} +503 -453
  2. package/dist/lib/browser/chunk-SOVZDG44.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-5D3NBEP2.cjs → chunk-BMZFS4HT.cjs} +519 -468
  7. package/dist/lib/node/chunk-BMZFS4HT.cjs.map +7 -0
  8. package/dist/lib/node/index.cjs +27 -27
  9. package/dist/lib/node/index.cjs.map +1 -1
  10. package/dist/lib/node/meta.json +1 -1
  11. package/dist/lib/node/testing/index.cjs +18 -18
  12. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  13. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  14. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  15. package/dist/types/src/transport/libdatachannel-transport.d.ts +20 -19
  16. package/dist/types/src/transport/libdatachannel-transport.d.ts.map +1 -1
  17. package/dist/types/src/transport/memory-transport.d.ts +17 -10
  18. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  19. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts +7 -9
  20. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  21. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  22. package/dist/types/src/transport/simplepeer-transport.d.ts +9 -11
  23. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  24. package/dist/types/src/transport/tcp-transport.browser.d.ts +7 -2
  25. package/dist/types/src/transport/tcp-transport.browser.d.ts.map +1 -1
  26. package/dist/types/src/transport/tcp-transport.d.ts +6 -4
  27. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -1
  28. package/dist/types/src/transport/transport.d.ts +18 -12
  29. package/dist/types/src/transport/transport.d.ts.map +1 -1
  30. package/package.json +17 -17
  31. package/src/swarm/connection.ts +7 -5
  32. package/src/swarm/peer.ts +3 -4
  33. package/src/swarm/swarm.ts +1 -0
  34. package/src/transport/libdatachannel-transport.test.ts +14 -9
  35. package/src/transport/libdatachannel-transport.ts +226 -204
  36. package/src/transport/memory-transport.test.ts +10 -7
  37. package/src/transport/memory-transport.ts +49 -42
  38. package/src/transport/simplepeer-transport-proxy-test.ts +13 -8
  39. package/src/transport/simplepeer-transport-proxy.ts +57 -50
  40. package/src/transport/simplepeer-transport-service.ts +5 -2
  41. package/src/transport/simplepeer-transport.test.ts +9 -5
  42. package/src/transport/simplepeer-transport.ts +25 -25
  43. package/src/transport/tcp-transport.browser.ts +10 -5
  44. package/src/transport/tcp-transport.ts +31 -22
  45. package/src/transport/transport.ts +25 -14
  46. package/dist/lib/browser/chunk-KEN4UCZF.mjs.map +0 -7
  47. package/dist/lib/node/chunk-5D3NBEP2.cjs.map +0 -7
@@ -26,8 +26,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
26
  mod
27
27
  ));
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var chunk_5D3NBEP2_exports = {};
30
- __export(chunk_5D3NBEP2_exports, {
29
+ var chunk_BMZFS4HT_exports = {};
30
+ __export(chunk_BMZFS4HT_exports, {
31
31
  Connection: () => Connection,
32
32
  ConnectionLimiter: () => ConnectionLimiter,
33
33
  ConnectionLog: () => ConnectionLog,
@@ -55,7 +55,7 @@ __export(chunk_5D3NBEP2_exports, {
55
55
  createSimplePeerTransportFactory: () => createSimplePeerTransportFactory,
56
56
  createTeleportProtocolFactory: () => createTeleportProtocolFactory
57
57
  });
58
- module.exports = __toCommonJS(chunk_5D3NBEP2_exports);
58
+ module.exports = __toCommonJS(chunk_BMZFS4HT_exports);
59
59
  var import_async = require("@dxos/async");
60
60
  var import_context = require("@dxos/context");
61
61
  var import_debug = require("@dxos/debug");
@@ -146,6 +146,7 @@ var import_util9 = require("@dxos/util");
146
146
  var import_stream = require("stream");
147
147
  var import_async11 = require("@dxos/async");
148
148
  var import_debug7 = require("@dxos/debug");
149
+ var import_invariant13 = require("@dxos/invariant");
149
150
  var import_log14 = require("@dxos/log");
150
151
  var import_node_net = require("node:net");
151
152
  var import_async12 = require("@dxos/async");
@@ -304,6 +305,7 @@ var Connection = class {
304
305
  sendSignal: async (signal) => this._sendSignal(signal),
305
306
  sessionId: this.sessionId
306
307
  });
308
+ await this._transport.open();
307
309
  this._transport.connected.once(async () => {
308
310
  this._changeState("CONNECTED");
309
311
  await this.connectedTimeoutContext.dispose();
@@ -314,7 +316,7 @@ var Connection = class {
314
316
  this._transport = void 0;
315
317
  (0, import_log.log)("abort triggered by transport close", void 0, {
316
318
  F: __dxlog_file,
317
- L: 216,
319
+ L: 218,
318
320
  S: this,
319
321
  C: (f, a) => f(...a)
320
322
  });
@@ -325,7 +327,7 @@ var Connection = class {
325
327
  err
326
328
  }, {
327
329
  F: __dxlog_file,
328
- L: 221,
330
+ L: 223,
329
331
  S: this,
330
332
  C: (f, a) => f(...a)
331
333
  });
@@ -335,7 +337,7 @@ var Connection = class {
335
337
  if (err instanceof import_protocols.ConnectionResetError) {
336
338
  import_log.log.info("aborting due to transport ConnectionResetError", void 0, {
337
339
  F: __dxlog_file,
338
- L: 228,
340
+ L: 230,
339
341
  S: this,
340
342
  C: (f, a) => f(...a)
341
343
  });
@@ -343,7 +345,7 @@ var Connection = class {
343
345
  } else if (err instanceof import_protocols.ConnectivityError) {
344
346
  import_log.log.info("aborting due to transport ConnectivityError", void 0, {
345
347
  F: __dxlog_file,
346
- L: 231,
348
+ L: 233,
347
349
  S: this,
348
350
  C: (f, a) => f(...a)
349
351
  });
@@ -353,7 +355,7 @@ var Connection = class {
353
355
  err
354
356
  }, {
355
357
  F: __dxlog_file,
356
- L: 234,
358
+ L: 236,
357
359
  S: this,
358
360
  C: (f, a) => f(...a)
359
361
  });
@@ -364,14 +366,14 @@ var Connection = class {
364
366
  }
365
367
  });
366
368
  for (const signal of this._incomingSignalBuffer) {
367
- void this._transport.signal(signal);
369
+ void this._transport.onSignal(signal);
368
370
  }
369
371
  this._incomingSignalBuffer = [];
370
372
  import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.end({
371
373
  id: this._instanceId
372
374
  }), {
373
375
  F: __dxlog_file,
374
- L: 250,
376
+ L: 252,
375
377
  S: this,
376
378
  C: (f, a) => f(...a)
377
379
  });
@@ -381,14 +383,14 @@ var Connection = class {
381
383
  err
382
384
  }, {
383
385
  F: __dxlog_file,
384
- L: 257,
386
+ L: 259,
385
387
  S: this,
386
388
  C: (f, a) => f(...a)
387
389
  });
388
390
  if (this._state === "CLOSED" || this._state === "ABORTED") {
389
391
  (0, import_log.log)(`abort ignored: already ${this._state}`, this.closeReason, {
390
392
  F: __dxlog_file,
391
- L: 259,
393
+ L: 261,
392
394
  S: this,
393
395
  C: (f, a) => f(...a)
394
396
  });
@@ -403,7 +405,7 @@ var Connection = class {
403
405
  try {
404
406
  (0, import_log.log)("aborting protocol... ", void 0, {
405
407
  F: __dxlog_file,
406
- L: 273,
408
+ L: 275,
407
409
  S: this,
408
410
  C: (f, a) => f(...a)
409
411
  });
@@ -411,17 +413,17 @@ var Connection = class {
411
413
  } catch (err2) {
412
414
  import_log.log.catch(err2, void 0, {
413
415
  F: __dxlog_file,
414
- L: 276,
416
+ L: 278,
415
417
  S: this,
416
418
  C: (f, a) => f(...a)
417
419
  });
418
420
  }
419
421
  try {
420
- await this._transport?.destroy();
422
+ await this._transport?.close();
421
423
  } catch (err2) {
422
424
  import_log.log.catch(err2, void 0, {
423
425
  F: __dxlog_file,
424
- L: 283,
426
+ L: 285,
425
427
  S: this,
426
428
  C: (f, a) => f(...a)
427
429
  });
@@ -431,7 +433,7 @@ var Connection = class {
431
433
  } catch (err2) {
432
434
  import_log.log.catch(err2, void 0, {
433
435
  F: __dxlog_file,
434
- L: 289,
436
+ L: 291,
435
437
  S: this,
436
438
  C: (f, a) => f(...a)
437
439
  });
@@ -455,7 +457,7 @@ var Connection = class {
455
457
  peerId: this.ownId
456
458
  }, {
457
459
  F: __dxlog_file,
458
- L: 314,
460
+ L: 316,
459
461
  S: this,
460
462
  C: (f, a) => f(...a)
461
463
  });
@@ -465,17 +467,17 @@ var Connection = class {
465
467
  } catch (err2) {
466
468
  import_log.log.catch(err2, void 0, {
467
469
  F: __dxlog_file,
468
- L: 321,
470
+ L: 323,
469
471
  S: this,
470
472
  C: (f, a) => f(...a)
471
473
  });
472
474
  }
473
475
  try {
474
- await this._transport?.destroy();
476
+ await this._transport?.close();
475
477
  } catch (err2) {
476
478
  import_log.log.catch(err2, void 0, {
477
479
  F: __dxlog_file,
478
- L: 328,
480
+ L: 330,
479
481
  S: this,
480
482
  C: (f, a) => f(...a)
481
483
  });
@@ -483,7 +485,7 @@ var Connection = class {
483
485
  } else {
484
486
  import_log.log.info(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
485
487
  F: __dxlog_file,
486
- L: 331,
488
+ L: 333,
487
489
  S: this,
488
490
  C: (f, a) => f(...a)
489
491
  });
@@ -492,17 +494,17 @@ var Connection = class {
492
494
  } catch (err2) {
493
495
  import_log.log.catch(err2, void 0, {
494
496
  F: __dxlog_file,
495
- L: 335,
497
+ L: 337,
496
498
  S: this,
497
499
  C: (f, a) => f(...a)
498
500
  });
499
501
  }
500
502
  try {
501
- await this._transport?.destroy();
503
+ await this._transport?.close();
502
504
  } catch (err2) {
503
505
  import_log.log.catch(err2, void 0, {
504
506
  F: __dxlog_file,
505
- L: 340,
507
+ L: 342,
506
508
  S: this,
507
509
  C: (f, a) => f(...a)
508
510
  });
@@ -512,7 +514,7 @@ var Connection = class {
512
514
  peerId: this.ownId
513
515
  }, {
514
516
  F: __dxlog_file,
515
- L: 344,
517
+ L: 346,
516
518
  S: this,
517
519
  C: (f, a) => f(...a)
518
520
  });
@@ -555,7 +557,7 @@ var Connection = class {
555
557
  err
556
558
  }, {
557
559
  F: __dxlog_file,
558
- L: 382,
560
+ L: 384,
559
561
  S: this,
560
562
  C: (f, a) => f(...a)
561
563
  });
@@ -568,7 +570,7 @@ var Connection = class {
568
570
  async signal(msg) {
569
571
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
570
572
  F: __dxlog_file,
571
- L: 391,
573
+ L: 393,
572
574
  S: this,
573
575
  A: [
574
576
  "msg.sessionId",
@@ -578,7 +580,7 @@ var Connection = class {
578
580
  if (!msg.sessionId.equals(this.sessionId)) {
579
581
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
580
582
  F: __dxlog_file,
581
- L: 393,
583
+ L: 395,
582
584
  S: this,
583
585
  C: (f, a) => f(...a)
584
586
  });
@@ -586,7 +588,7 @@ var Connection = class {
586
588
  }
587
589
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
588
590
  F: __dxlog_file,
589
- L: 396,
591
+ L: 398,
590
592
  S: this,
591
593
  A: [
592
594
  "msg.data.signal || msg.data.signalBatch",
@@ -595,7 +597,7 @@ var Connection = class {
595
597
  });
596
598
  (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
597
599
  F: __dxlog_file,
598
- L: 397,
600
+ L: 399,
599
601
  S: this,
600
602
  A: [
601
603
  "msg.author?.equals(this.remoteId)",
@@ -604,7 +606,7 @@ var Connection = class {
604
606
  });
605
607
  (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
606
608
  F: __dxlog_file,
607
- L: 398,
609
+ L: 400,
608
610
  S: this,
609
611
  A: [
610
612
  "msg.recipient?.equals(this.ownId)",
@@ -628,7 +630,7 @@ var Connection = class {
628
630
  msg: msg.data
629
631
  }, {
630
632
  F: __dxlog_file,
631
- L: 407,
633
+ L: 409,
632
634
  S: this,
633
635
  C: (f, a) => f(...a)
634
636
  });
@@ -636,7 +638,7 @@ var Connection = class {
636
638
  } else {
637
639
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
638
640
  F: __dxlog_file,
639
- L: 410,
641
+ L: 412,
640
642
  S: this,
641
643
  A: [
642
644
  "this._transport",
@@ -649,11 +651,11 @@ var Connection = class {
649
651
  msg: msg.data
650
652
  }, {
651
653
  F: __dxlog_file,
652
- L: 411,
654
+ L: 413,
653
655
  S: this,
654
656
  C: (f, a) => f(...a)
655
657
  });
656
- await this._transport.signal(signal);
658
+ await this._transport.onSignal(signal);
657
659
  }
658
660
  }
659
661
  }
@@ -667,13 +669,13 @@ var Connection = class {
667
669
  peerId: this.ownId
668
670
  }, {
669
671
  F: __dxlog_file,
670
- L: 422,
672
+ L: 424,
671
673
  S: this,
672
674
  C: (f, a) => f(...a)
673
675
  });
674
676
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
675
677
  F: __dxlog_file,
676
- L: 423,
678
+ L: 425,
677
679
  S: this,
678
680
  A: [
679
681
  "state !== this._state",
@@ -952,7 +954,6 @@ var ConnectionDisplacedError = class extends import_protocols4.SystemError {
952
954
  };
953
955
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
954
956
  var Peer = class {
955
- // TODO(burdon): Convert to map?
956
957
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
957
958
  this.id = id;
958
959
  this.topic = topic;
@@ -981,7 +982,7 @@ var Peer = class {
981
982
  ].includes(this.connection.state)) {
982
983
  import_log4.log.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
983
984
  F: __dxlog_file3,
984
- L: 115,
985
+ L: 114,
985
986
  S: this,
986
987
  C: (f, a) => f(...a)
987
988
  });
@@ -998,7 +999,7 @@ var Peer = class {
998
999
  sessionId: this.connection?.sessionId
999
1000
  }, {
1000
1001
  F: __dxlog_file3,
1001
- L: 124,
1002
+ L: 123,
1002
1003
  S: this,
1003
1004
  C: (f, a) => f(...a)
1004
1005
  });
@@ -1015,7 +1016,7 @@ var Peer = class {
1015
1016
  if (!this.connection) {
1016
1017
  (0, import_invariant4.invariant)(message.sessionId, void 0, {
1017
1018
  F: __dxlog_file3,
1018
- L: 144,
1019
+ L: 143,
1019
1020
  S: this,
1020
1021
  A: [
1021
1022
  "message.sessionId",
@@ -1036,7 +1037,7 @@ var Peer = class {
1036
1037
  err
1037
1038
  }, {
1038
1039
  F: __dxlog_file3,
1039
- L: 154,
1040
+ L: 153,
1040
1041
  S: this,
1041
1042
  C: (f, a) => f(...a)
1042
1043
  });
@@ -1058,7 +1059,7 @@ var Peer = class {
1058
1059
  async initiateConnection() {
1059
1060
  (0, import_invariant4.invariant)(!this.initiating, "Initiation in progress.", {
1060
1061
  F: __dxlog_file3,
1061
- L: 171,
1062
+ L: 170,
1062
1063
  S: this,
1063
1064
  A: [
1064
1065
  "!this.initiating",
@@ -1067,7 +1068,7 @@ var Peer = class {
1067
1068
  });
1068
1069
  (0, import_invariant4.invariant)(!this.connection, "Already connected.", {
1069
1070
  F: __dxlog_file3,
1070
- L: 172,
1071
+ L: 171,
1071
1072
  S: this,
1072
1073
  A: [
1073
1074
  "!this.connection",
@@ -1082,7 +1083,7 @@ var Peer = class {
1082
1083
  sessionId
1083
1084
  }, {
1084
1085
  F: __dxlog_file3,
1085
- L: 174,
1086
+ L: 173,
1086
1087
  S: this,
1087
1088
  C: (f, a) => f(...a)
1088
1089
  });
@@ -1108,14 +1109,14 @@ var Peer = class {
1108
1109
  remoteId: this.id
1109
1110
  }, {
1110
1111
  F: __dxlog_file3,
1111
- L: 191,
1112
+ L: 190,
1112
1113
  S: this,
1113
1114
  C: (f, a) => f(...a)
1114
1115
  });
1115
1116
  if (connection.state !== ConnectionState.INITIAL) {
1116
1117
  (0, import_log4.log)("ignoring response", void 0, {
1117
1118
  F: __dxlog_file3,
1118
- L: 193,
1119
+ L: 192,
1119
1120
  S: this,
1120
1121
  C: (f, a) => f(...a)
1121
1122
  });
@@ -1129,7 +1130,7 @@ var Peer = class {
1129
1130
  remoteId: this.id
1130
1131
  }, {
1131
1132
  F: __dxlog_file3,
1132
- L: 197,
1133
+ L: 196,
1133
1134
  S: this,
1134
1135
  C: (f, a) => f(...a)
1135
1136
  });
@@ -1151,7 +1152,7 @@ var Peer = class {
1151
1152
  remoteId: this.id
1152
1153
  }, {
1153
1154
  F: __dxlog_file3,
1154
- L: 210,
1155
+ L: 209,
1155
1156
  S: this,
1156
1157
  C: (f, a) => f(...a)
1157
1158
  });
@@ -1163,7 +1164,7 @@ var Peer = class {
1163
1164
  try {
1164
1165
  (0, import_log4.log)("opening connection as initiator", void 0, {
1165
1166
  F: __dxlog_file3,
1166
- L: 223,
1167
+ L: 222,
1167
1168
  S: this,
1168
1169
  C: (f, a) => f(...a)
1169
1170
  });
@@ -1177,7 +1178,7 @@ var Peer = class {
1177
1178
  remoteId: this.id
1178
1179
  }, {
1179
1180
  F: __dxlog_file3,
1180
- L: 227,
1181
+ L: 226,
1181
1182
  S: this,
1182
1183
  C: (f, a) => f(...a)
1183
1184
  });
@@ -1185,7 +1186,7 @@ var Peer = class {
1185
1186
  err
1186
1187
  }, {
1187
1188
  F: __dxlog_file3,
1188
- L: 234,
1189
+ L: 233,
1189
1190
  S: this,
1190
1191
  C: (f, a) => f(...a)
1191
1192
  });
@@ -1208,13 +1209,13 @@ var Peer = class {
1208
1209
  sessionId
1209
1210
  }, {
1210
1211
  F: __dxlog_file3,
1211
- L: 248,
1212
+ L: 247,
1212
1213
  S: this,
1213
1214
  C: (f, a) => f(...a)
1214
1215
  });
1215
1216
  (0, import_invariant4.invariant)(!this.connection, "Already connected.", {
1216
1217
  F: __dxlog_file3,
1217
- L: 255,
1218
+ L: 254,
1218
1219
  S: this,
1219
1220
  A: [
1220
1221
  "!this.connection",
@@ -1250,7 +1251,7 @@ var Peer = class {
1250
1251
  initiator
1251
1252
  }, {
1252
1253
  F: __dxlog_file3,
1253
- L: 274,
1254
+ L: 273,
1254
1255
  S: this,
1255
1256
  C: (f, a) => f(...a)
1256
1257
  });
@@ -1263,14 +1264,14 @@ var Peer = class {
1263
1264
  initiator
1264
1265
  }, {
1265
1266
  F: __dxlog_file3,
1266
- L: 283,
1267
+ L: 282,
1267
1268
  S: this,
1268
1269
  C: (f, a) => f(...a)
1269
1270
  });
1270
1271
  this._connectionLimiter.doneConnecting(sessionId);
1271
1272
  (0, import_invariant4.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
1272
1273
  F: __dxlog_file3,
1273
- L: 288,
1274
+ L: 287,
1274
1275
  S: this,
1275
1276
  A: [
1276
1277
  "this.connection === connection",
@@ -1285,7 +1286,7 @@ var Peer = class {
1285
1286
  initiator
1286
1287
  }, {
1287
1288
  F: __dxlog_file3,
1288
- L: 290,
1289
+ L: 289,
1289
1290
  S: this,
1290
1291
  C: (f, a) => f(...a)
1291
1292
  });
@@ -1320,7 +1321,7 @@ var Peer = class {
1320
1321
  err
1321
1322
  }, {
1322
1323
  F: __dxlog_file3,
1323
- L: 330,
1324
+ L: 329,
1324
1325
  S: this,
1325
1326
  C: (f, a) => f(...a)
1326
1327
  });
@@ -1333,7 +1334,7 @@ var Peer = class {
1333
1334
  err
1334
1335
  }, {
1335
1336
  F: __dxlog_file3,
1336
- L: 337,
1337
+ L: 336,
1337
1338
  S: this,
1338
1339
  C: (f, a) => f(...a)
1339
1340
  });
@@ -1352,7 +1353,7 @@ var Peer = class {
1352
1353
  sessionId: connection.sessionId
1353
1354
  }, {
1354
1355
  F: __dxlog_file3,
1355
- L: 362,
1356
+ L: 361,
1356
1357
  S: this,
1357
1358
  C: (f, a) => f(...a)
1358
1359
  });
@@ -1362,7 +1363,7 @@ var Peer = class {
1362
1363
  sessionId: connection.sessionId
1363
1364
  }, {
1364
1365
  F: __dxlog_file3,
1365
- L: 368,
1366
+ L: 367,
1366
1367
  S: this,
1367
1368
  C: (f, a) => f(...a)
1368
1369
  });
@@ -1373,7 +1374,7 @@ var Peer = class {
1373
1374
  message
1374
1375
  }, {
1375
1376
  F: __dxlog_file3,
1376
- L: 373,
1377
+ L: 372,
1377
1378
  S: this,
1378
1379
  C: (f, a) => f(...a)
1379
1380
  });
@@ -1388,7 +1389,7 @@ var Peer = class {
1388
1389
  topic: this.topic
1389
1390
  }, {
1390
1391
  F: __dxlog_file3,
1391
- L: 383,
1392
+ L: 382,
1392
1393
  S: this,
1393
1394
  C: (f, a) => f(...a)
1394
1395
  });
@@ -1452,7 +1453,7 @@ var Swarm = class {
1452
1453
  }
1453
1454
  }), {
1454
1455
  F: __dxlog_file4,
1455
- L: 88,
1456
+ L: 89,
1456
1457
  S: this,
1457
1458
  C: (f, a) => f(...a)
1458
1459
  });
@@ -1460,7 +1461,7 @@ var Swarm = class {
1460
1461
  peerId: _ownPeerId
1461
1462
  }, {
1462
1463
  F: __dxlog_file4,
1463
- L: 92,
1464
+ L: 93,
1464
1465
  S: this,
1465
1466
  C: (f, a) => f(...a)
1466
1467
  });
@@ -1475,7 +1476,7 @@ var Swarm = class {
1475
1476
  id: this._instanceId
1476
1477
  }), {
1477
1478
  F: __dxlog_file4,
1478
- L: 101,
1479
+ L: 102,
1479
1480
  S: this,
1480
1481
  C: (f, a) => f(...a)
1481
1482
  });
@@ -1498,7 +1499,7 @@ var Swarm = class {
1498
1499
  async open() {
1499
1500
  (0, import_invariant3.invariant)(!this._listeningHandle, void 0, {
1500
1501
  F: __dxlog_file4,
1501
- L: 128,
1502
+ L: 129,
1502
1503
  S: this,
1503
1504
  A: [
1504
1505
  "!this._listeningHandle",
@@ -1513,7 +1514,7 @@ var Swarm = class {
1513
1514
  err
1514
1515
  }, {
1515
1516
  F: __dxlog_file4,
1516
- L: 136,
1517
+ L: 137,
1517
1518
  S: this,
1518
1519
  C: (f, a) => f(...a)
1519
1520
  }));
@@ -1523,7 +1524,7 @@ var Swarm = class {
1523
1524
  async destroy() {
1524
1525
  (0, import_log3.log)("destroying...", void 0, {
1525
1526
  F: __dxlog_file4,
1526
- L: 142,
1527
+ L: 143,
1527
1528
  S: this,
1528
1529
  C: (f, a) => f(...a)
1529
1530
  });
@@ -1534,7 +1535,7 @@ var Swarm = class {
1534
1535
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
1535
1536
  (0, import_log3.log)("destroyed", void 0, {
1536
1537
  F: __dxlog_file4,
1537
- L: 149,
1538
+ L: 150,
1538
1539
  S: this,
1539
1540
  C: (f, a) => f(...a)
1540
1541
  });
@@ -1542,7 +1543,7 @@ var Swarm = class {
1542
1543
  async setTopology(topology) {
1543
1544
  (0, import_invariant3.invariant)(!this._ctx.disposed, "Swarm is offline", {
1544
1545
  F: __dxlog_file4,
1545
- L: 153,
1546
+ L: 154,
1546
1547
  S: this,
1547
1548
  A: [
1548
1549
  "!this._ctx.disposed",
@@ -1557,7 +1558,7 @@ var Swarm = class {
1557
1558
  topology: getClassName(topology)
1558
1559
  }, {
1559
1560
  F: __dxlog_file4,
1560
- L: 157,
1561
+ L: 158,
1561
1562
  S: this,
1562
1563
  C: (f, a) => f(...a)
1563
1564
  });
@@ -1571,14 +1572,14 @@ var Swarm = class {
1571
1572
  swarmEvent
1572
1573
  }, {
1573
1574
  F: __dxlog_file4,
1574
- L: 170,
1575
+ L: 171,
1575
1576
  S: this,
1576
1577
  C: (f, a) => f(...a)
1577
1578
  });
1578
1579
  if (this._ctx.disposed) {
1579
1580
  (0, import_log3.log)("swarm event ignored for disposed swarm", void 0, {
1580
1581
  F: __dxlog_file4,
1581
- L: 173,
1582
+ L: 174,
1582
1583
  S: this,
1583
1584
  C: (f, a) => f(...a)
1584
1585
  });
@@ -1590,7 +1591,7 @@ var Swarm = class {
1590
1591
  peerId
1591
1592
  }, {
1592
1593
  F: __dxlog_file4,
1593
- L: 179,
1594
+ L: 180,
1594
1595
  S: this,
1595
1596
  C: (f, a) => f(...a)
1596
1597
  });
@@ -1605,7 +1606,7 @@ var Swarm = class {
1605
1606
  if (peer.connection?.state !== ConnectionState.CONNECTED) {
1606
1607
  void this._destroyPeer(peer.id, "peer left").catch((err) => import_log3.log.catch(err, void 0, {
1607
1608
  F: __dxlog_file4,
1608
- L: 190,
1609
+ L: 191,
1609
1610
  S: this,
1610
1611
  C: (f, a) => f(...a)
1611
1612
  }));
@@ -1615,7 +1616,7 @@ var Swarm = class {
1615
1616
  peer: swarmEvent.peerLeft.peer
1616
1617
  }, {
1617
1618
  F: __dxlog_file4,
1618
- L: 193,
1619
+ L: 194,
1619
1620
  S: this,
1620
1621
  C: (f, a) => f(...a)
1621
1622
  });
@@ -1628,14 +1629,14 @@ var Swarm = class {
1628
1629
  message
1629
1630
  }, {
1630
1631
  F: __dxlog_file4,
1631
- L: 202,
1632
+ L: 203,
1632
1633
  S: this,
1633
1634
  C: (f, a) => f(...a)
1634
1635
  });
1635
1636
  if (this._ctx.disposed) {
1636
1637
  (0, import_log3.log)("ignored for disposed swarm", void 0, {
1637
1638
  F: __dxlog_file4,
1638
- L: 204,
1639
+ L: 205,
1639
1640
  S: this,
1640
1641
  C: (f, a) => f(...a)
1641
1642
  });
@@ -1645,7 +1646,7 @@ var Swarm = class {
1645
1646
  }
1646
1647
  (0, import_invariant3.invariant)(message.author, void 0, {
1647
1648
  F: __dxlog_file4,
1648
- L: 209,
1649
+ L: 210,
1649
1650
  S: this,
1650
1651
  A: [
1651
1652
  "message.author",
@@ -1657,7 +1658,7 @@ var Swarm = class {
1657
1658
  message
1658
1659
  }, {
1659
1660
  F: __dxlog_file4,
1660
- L: 211,
1661
+ L: 212,
1661
1662
  S: this,
1662
1663
  C: (f, a) => f(...a)
1663
1664
  });
@@ -1670,7 +1671,7 @@ var Swarm = class {
1670
1671
  message
1671
1672
  }, {
1672
1673
  F: __dxlog_file4,
1673
- L: 215,
1674
+ L: 216,
1674
1675
  S: this,
1675
1676
  C: (f, a) => f(...a)
1676
1677
  });
@@ -1688,14 +1689,14 @@ var Swarm = class {
1688
1689
  message
1689
1690
  }, {
1690
1691
  F: __dxlog_file4,
1691
- L: 226,
1692
+ L: 227,
1692
1693
  S: this,
1693
1694
  C: (f, a) => f(...a)
1694
1695
  });
1695
1696
  if (this._ctx.disposed) {
1696
1697
  import_log3.log.info("ignored for offline swarm", void 0, {
1697
1698
  F: __dxlog_file4,
1698
- L: 228,
1699
+ L: 229,
1699
1700
  S: this,
1700
1701
  C: (f, a) => f(...a)
1701
1702
  });
@@ -1703,7 +1704,7 @@ var Swarm = class {
1703
1704
  }
1704
1705
  (0, import_invariant3.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1705
1706
  F: __dxlog_file4,
1706
- L: 231,
1707
+ L: 232,
1707
1708
  S: this,
1708
1709
  A: [
1709
1710
  "message.recipient?.equals(this._ownPeerId)",
@@ -1712,7 +1713,7 @@ var Swarm = class {
1712
1713
  });
1713
1714
  (0, import_invariant3.invariant)(message.topic?.equals(this._topic), void 0, {
1714
1715
  F: __dxlog_file4,
1715
- L: 235,
1716
+ L: 236,
1716
1717
  S: this,
1717
1718
  A: [
1718
1719
  "message.topic?.equals(this._topic)",
@@ -1721,7 +1722,7 @@ var Swarm = class {
1721
1722
  });
1722
1723
  (0, import_invariant3.invariant)(message.author, void 0, {
1723
1724
  F: __dxlog_file4,
1724
- L: 236,
1725
+ L: 237,
1725
1726
  S: this,
1726
1727
  A: [
1727
1728
  "message.author",
@@ -1765,7 +1766,7 @@ var Swarm = class {
1765
1766
  peerId
1766
1767
  }, {
1767
1768
  F: __dxlog_file4,
1768
- L: 285,
1769
+ L: 286,
1769
1770
  S: this,
1770
1771
  C: (f, a) => f(...a)
1771
1772
  });
@@ -1789,7 +1790,7 @@ var Swarm = class {
1789
1790
  async _destroyPeer(peerId, reason) {
1790
1791
  (0, import_invariant3.invariant)(this._peers.has(peerId), void 0, {
1791
1792
  F: __dxlog_file4,
1792
- L: 307,
1793
+ L: 308,
1793
1794
  S: this,
1794
1795
  A: [
1795
1796
  "this._peers.has(peerId)",
@@ -1816,7 +1817,7 @@ var Swarm = class {
1816
1817
  } catch (err) {
1817
1818
  (0, import_log3.log)("initiation error", err, {
1818
1819
  F: __dxlog_file4,
1819
- L: 333,
1820
+ L: 334,
1820
1821
  S: this,
1821
1822
  C: (f, a) => f(...a)
1822
1823
  });
@@ -1844,7 +1845,7 @@ var Swarm = class {
1844
1845
  remoteId
1845
1846
  }, {
1846
1847
  F: __dxlog_file4,
1847
- L: 360,
1848
+ L: 361,
1848
1849
  S: this,
1849
1850
  C: (f, a) => f(...a)
1850
1851
  });
@@ -1861,7 +1862,7 @@ var Swarm = class {
1861
1862
  remoteId
1862
1863
  }, {
1863
1864
  F: __dxlog_file4,
1864
- L: 374,
1865
+ L: 375,
1865
1866
  S: this,
1866
1867
  C: (f, a) => f(...a)
1867
1868
  });
@@ -1871,7 +1872,7 @@ var Swarm = class {
1871
1872
  remoteId
1872
1873
  }, {
1873
1874
  F: __dxlog_file4,
1874
- L: 377,
1875
+ L: 378,
1875
1876
  S: this,
1876
1877
  C: (f, a) => f(...a)
1877
1878
  });
@@ -2667,31 +2668,25 @@ var createStreamDelay = (delay) => {
2667
2668
  });
2668
2669
  };
2669
2670
  var MemoryTransportFactory = {
2670
- createTransport: (params) => new MemoryTransport(params)
2671
+ createTransport: (options) => new MemoryTransport(options)
2671
2672
  };
2672
2673
  var MemoryTransport = class _MemoryTransport {
2673
2674
  static {
2674
2675
  this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2675
2676
  }
2676
- constructor(options) {
2677
- this.options = options;
2678
- this.closed = new import_async8.Event();
2679
- this.connected = new import_async8.Event();
2680
- this.errors = new import_debug4.ErrorStream();
2677
+ constructor(_options) {
2678
+ this._options = _options;
2681
2679
  this._instanceId = import_keys9.PublicKey.random();
2682
2680
  this._remote = new import_async8.Trigger();
2683
2681
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2684
2682
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2685
- this._destroyed = false;
2686
- (0, import_log10.log)("creating", void 0, {
2687
- F: __dxlog_file11,
2688
- L: 64,
2689
- S: this,
2690
- C: (f, a) => f(...a)
2691
- });
2683
+ this._closed = false;
2684
+ this.closed = new import_async8.Event();
2685
+ this.connected = new import_async8.Event();
2686
+ this.errors = new import_debug4.ErrorStream();
2692
2687
  (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2693
2688
  F: __dxlog_file11,
2694
- L: 66,
2689
+ L: 64,
2695
2690
  S: this,
2696
2691
  A: [
2697
2692
  "!MemoryTransport._connections.has(this._instanceId)",
@@ -2699,41 +2694,52 @@ var MemoryTransport = class _MemoryTransport {
2699
2694
  ]
2700
2695
  });
2701
2696
  _MemoryTransport._connections.set(this._instanceId, this);
2702
- if (this.options.initiator) {
2703
- setTimeout(async () => {
2704
- (0, import_log10.log)("sending signal", void 0, {
2705
- F: __dxlog_file11,
2706
- L: 73,
2707
- S: this,
2708
- C: (f, a) => f(...a)
2709
- });
2710
- void this.options.sendSignal({
2697
+ }
2698
+ get isOpen() {
2699
+ return !this._closed;
2700
+ }
2701
+ async open() {
2702
+ (0, import_log10.log)("opening...", void 0, {
2703
+ F: __dxlog_file11,
2704
+ L: 74,
2705
+ S: this,
2706
+ C: (f, a) => f(...a)
2707
+ });
2708
+ if (this._options.initiator) {
2709
+ (0, import_log10.log)("sending signal", void 0, {
2710
+ F: __dxlog_file11,
2711
+ L: 78,
2712
+ S: this,
2713
+ C: (f, a) => f(...a)
2714
+ });
2715
+ try {
2716
+ await this._options.sendSignal({
2711
2717
  payload: {
2712
2718
  transportId: this._instanceId.toHex()
2713
2719
  }
2714
- }).catch((err) => {
2715
- if (!this._destroyed) {
2716
- this.errors.raise(err);
2717
- }
2718
2720
  });
2719
- });
2721
+ } catch (err) {
2722
+ if (!this._closed) {
2723
+ this.errors.raise(toError(err));
2724
+ }
2725
+ }
2720
2726
  } else {
2721
2727
  this._remote.wait({
2722
- timeout: this.options.timeout ?? 1e3
2728
+ timeout: this._options.timeout ?? 1e3
2723
2729
  }).then((remoteId) => {
2724
- if (this._destroyed) {
2730
+ if (this._closed) {
2725
2731
  return;
2726
2732
  }
2727
2733
  this._remoteInstanceId = remoteId;
2728
2734
  this._remoteConnection = _MemoryTransport._connections.get(this._remoteInstanceId);
2729
2735
  if (!this._remoteConnection) {
2730
- this._destroyed = true;
2736
+ this._closed = true;
2731
2737
  this.closed.emit();
2732
2738
  return;
2733
2739
  }
2734
2740
  (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2735
2741
  F: __dxlog_file11,
2736
- L: 99,
2742
+ L: 104,
2737
2743
  S: this,
2738
2744
  A: [
2739
2745
  "!this._remoteConnection._remoteConnection",
@@ -2744,68 +2750,56 @@ var MemoryTransport = class _MemoryTransport {
2744
2750
  this._remoteConnection._remoteInstanceId = this._instanceId;
2745
2751
  (0, import_log10.log)("connected", void 0, {
2746
2752
  F: __dxlog_file11,
2747
- L: 103,
2753
+ L: 108,
2748
2754
  S: this,
2749
2755
  C: (f, a) => f(...a)
2750
2756
  });
2751
- this.options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection.options.stream).pipe(this._incomingDelay).pipe(this.options.stream);
2757
+ this._options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection._options.stream).pipe(this._incomingDelay).pipe(this._options.stream);
2752
2758
  this.connected.emit();
2753
2759
  this._remoteConnection.connected.emit();
2754
2760
  }).catch((err) => {
2755
- if (this._destroyed) {
2761
+ if (this._closed) {
2756
2762
  return;
2757
2763
  }
2758
2764
  this.errors.raise(err);
2759
2765
  });
2760
2766
  }
2761
2767
  }
2762
- async destroy() {
2763
- (0, import_log10.log)("closing", void 0, {
2768
+ async close() {
2769
+ (0, import_log10.log)("closing...", void 0, {
2764
2770
  F: __dxlog_file11,
2765
- L: 124,
2771
+ L: 129,
2766
2772
  S: this,
2767
2773
  C: (f, a) => f(...a)
2768
2774
  });
2769
- this._destroyed = true;
2775
+ this._closed = true;
2770
2776
  _MemoryTransport._connections.delete(this._instanceId);
2771
2777
  if (this._remoteConnection) {
2772
- (0, import_log10.log)("closing", void 0, {
2773
- F: __dxlog_file11,
2774
- L: 129,
2775
- S: this,
2776
- C: (f, a) => f(...a)
2777
- });
2778
- this._remoteConnection._destroyed = true;
2778
+ this._remoteConnection._closed = true;
2779
2779
  _MemoryTransport._connections.delete(this._remoteInstanceId);
2780
- this.options.stream.unpipe(this._incomingDelay);
2781
- this._incomingDelay.unpipe(this._remoteConnection.options.stream);
2782
- this._remoteConnection.options.stream.unpipe(this._outgoingDelay);
2783
- this._outgoingDelay.unpipe(this.options.stream);
2784
- this.options.stream.unpipe(this._outgoingDelay);
2780
+ this._options.stream.unpipe(this._incomingDelay);
2781
+ this._incomingDelay.unpipe(this._remoteConnection._options.stream);
2782
+ this._remoteConnection._options.stream.unpipe(this._outgoingDelay);
2783
+ this._outgoingDelay.unpipe(this._options.stream);
2784
+ this._options.stream.unpipe(this._outgoingDelay);
2785
2785
  this._remoteConnection.closed.emit();
2786
2786
  this._remoteConnection._remoteConnection = void 0;
2787
2787
  this._remoteConnection = void 0;
2788
- (0, import_log10.log)("closed", void 0, {
2789
- F: __dxlog_file11,
2790
- L: 150,
2791
- S: this,
2792
- C: (f, a) => f(...a)
2793
- });
2794
2788
  }
2795
2789
  this.closed.emit();
2796
2790
  (0, import_log10.log)("closed", void 0, {
2797
2791
  F: __dxlog_file11,
2798
- L: 154,
2792
+ L: 157,
2799
2793
  S: this,
2800
2794
  C: (f, a) => f(...a)
2801
2795
  });
2802
2796
  }
2803
- signal({ payload }) {
2797
+ async onSignal({ payload }) {
2804
2798
  (0, import_log10.log)("received signal", {
2805
2799
  payload
2806
2800
  }, {
2807
2801
  F: __dxlog_file11,
2808
- L: 158,
2802
+ L: 161,
2809
2803
  S: this,
2810
2804
  C: (f, a) => f(...a)
2811
2805
  });
@@ -2836,11 +2830,12 @@ _ts_decorate4([
2836
2830
  _ts_decorate4([
2837
2831
  import_log10.logInfo
2838
2832
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2833
+ var toError = (err) => err instanceof Error ? err : new Error(String(err));
2839
2834
  var TransportKind;
2840
2835
  (function(TransportKind2) {
2841
2836
  TransportKind2["SIMPLE_PEER"] = "SIMPLE_PEER";
2842
- TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2843
2837
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2838
+ TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2844
2839
  TransportKind2["MEMORY"] = "MEMORY";
2845
2840
  TransportKind2["TCP"] = "TCP";
2846
2841
  })(TransportKind || (TransportKind = {}));
@@ -2850,12 +2845,21 @@ try {
2850
2845
  } catch {
2851
2846
  }
2852
2847
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
2848
+ var createSimplePeerTransportFactory = (webrtcConfig) => ({
2849
+ createTransport: (options) => new SimplePeerTransport({
2850
+ ...options,
2851
+ webrtcConfig
2852
+ })
2853
+ });
2853
2854
  var SimplePeerTransport = class {
2855
+ get isOpen() {
2856
+ return this._piped && !this._closed;
2857
+ }
2854
2858
  /**
2855
2859
  * @params opts.config formatted as per https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection
2856
2860
  */
2857
- constructor(params) {
2858
- this.params = params;
2861
+ constructor(_params) {
2862
+ this._params = _params;
2859
2863
  this._closed = false;
2860
2864
  this._piped = false;
2861
2865
  this.closed = new import_async9.Event();
@@ -2866,30 +2870,30 @@ var SimplePeerTransport = class {
2866
2870
  id: this._instanceId
2867
2871
  }), {
2868
2872
  F: __dxlog_file12,
2869
- L: 44,
2873
+ L: 50,
2870
2874
  S: this,
2871
2875
  C: (f, a) => f(...a)
2872
2876
  });
2873
- (0, import_log11.log)("created connection", params, {
2877
+ (0, import_log11.log)("created connection", _params, {
2874
2878
  F: __dxlog_file12,
2875
- L: 45,
2879
+ L: 51,
2876
2880
  S: this,
2877
2881
  C: (f, a) => f(...a)
2878
2882
  });
2879
2883
  this._peer = new import_simple_peer.default({
2880
2884
  channelName: "dxos.mesh.transport",
2881
- initiator: this.params.initiator,
2885
+ initiator: this._params.initiator,
2882
2886
  wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
2883
- config: this.params.webrtcConfig
2887
+ config: this._params.webrtcConfig
2884
2888
  });
2885
2889
  this._peer.on("signal", async (data) => {
2886
2890
  (0, import_log11.log)("signal", data, {
2887
2891
  F: __dxlog_file12,
2888
- L: 54,
2892
+ L: 60,
2889
2893
  S: this,
2890
2894
  C: (f, a) => f(...a)
2891
2895
  });
2892
- await this.params.sendSignal({
2896
+ await this._params.sendSignal({
2893
2897
  payload: {
2894
2898
  data
2895
2899
  }
@@ -2898,22 +2902,22 @@ var SimplePeerTransport = class {
2898
2902
  this._peer.on("connect", () => {
2899
2903
  (0, import_log11.log)("connected", void 0, {
2900
2904
  F: __dxlog_file12,
2901
- L: 59,
2905
+ L: 65,
2902
2906
  S: this,
2903
2907
  C: (f, a) => f(...a)
2904
2908
  });
2905
- this.params.stream.pipe(this._peer).pipe(this.params.stream);
2909
+ this._params.stream.pipe(this._peer).pipe(this._params.stream);
2906
2910
  this._piped = true;
2907
2911
  this.connected.emit();
2908
2912
  });
2909
2913
  this._peer.on("close", async () => {
2910
2914
  (0, import_log11.log)("closed", void 0, {
2911
2915
  F: __dxlog_file12,
2912
- L: 66,
2916
+ L: 72,
2913
2917
  S: this,
2914
2918
  C: (f, a) => f(...a)
2915
2919
  });
2916
- await this.destroy();
2920
+ await this.close();
2917
2921
  });
2918
2922
  this._peer.on("error", async (err) => {
2919
2923
  if (typeof RTCError !== "undefined" && err instanceof RTCError) {
@@ -2924,7 +2928,7 @@ var SimplePeerTransport = class {
2924
2928
  err
2925
2929
  }, {
2926
2930
  F: __dxlog_file12,
2927
- L: 77,
2931
+ L: 83,
2928
2932
  S: this,
2929
2933
  C: (f, a) => f(...a)
2930
2934
  });
@@ -2933,7 +2937,7 @@ var SimplePeerTransport = class {
2933
2937
  } else if ("code" in err) {
2934
2938
  import_log11.log.info("simple-peer error", err, {
2935
2939
  F: __dxlog_file12,
2936
- L: 82,
2940
+ L: 88,
2937
2941
  S: this,
2938
2942
  C: (f, a) => f(...a)
2939
2943
  });
@@ -2963,7 +2967,7 @@ var SimplePeerTransport = class {
2963
2967
  } else {
2964
2968
  import_log11.log.info("unknown peer connection error", err, {
2965
2969
  F: __dxlog_file12,
2966
- L: 108,
2970
+ L: 114,
2967
2971
  S: this,
2968
2972
  C: (f, a) => f(...a)
2969
2973
  });
@@ -2973,11 +2977,11 @@ var SimplePeerTransport = class {
2973
2977
  if (typeof this._peer?._pc?.getStats === "function") {
2974
2978
  this._peer._pc.getStats().then((stats) => {
2975
2979
  import_log11.log.info("report after webrtc error", {
2976
- config: this.params.webrtcConfig,
2980
+ config: this._params.webrtcConfig,
2977
2981
  stats: Object.fromEntries(stats.entries())
2978
2982
  }, {
2979
2983
  F: __dxlog_file12,
2980
- L: 116,
2984
+ L: 122,
2981
2985
  S: this,
2982
2986
  C: (f, a) => f(...a)
2983
2987
  });
@@ -2986,18 +2990,18 @@ var SimplePeerTransport = class {
2986
2990
  } catch (err2) {
2987
2991
  import_log11.log.catch(err2, void 0, {
2988
2992
  F: __dxlog_file12,
2989
- L: 123,
2993
+ L: 129,
2990
2994
  S: this,
2991
2995
  C: (f, a) => f(...a)
2992
2996
  });
2993
2997
  }
2994
- await this.destroy();
2998
+ await this.close();
2995
2999
  });
2996
3000
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols7.trace.end({
2997
3001
  id: this._instanceId
2998
3002
  }), {
2999
3003
  F: __dxlog_file12,
3000
- L: 129,
3004
+ L: 135,
3001
3005
  S: this,
3002
3006
  C: (f, a) => f(...a)
3003
3007
  });
@@ -3055,10 +3059,12 @@ var SimplePeerTransport = class {
3055
3059
  }
3056
3060
  return `${rc.ip}:${rc.port}/${rc.protocol} ${rc.candidateType}`;
3057
3061
  }
3058
- async destroy() {
3062
+ async open() {
3063
+ }
3064
+ async close() {
3059
3065
  (0, import_log11.log)("closing...", void 0, {
3060
3066
  F: __dxlog_file12,
3061
- L: 192,
3067
+ L: 200,
3062
3068
  S: this,
3063
3069
  C: (f, a) => f(...a)
3064
3070
  });
@@ -3071,12 +3077,12 @@ var SimplePeerTransport = class {
3071
3077
  this.closed.emit();
3072
3078
  (0, import_log11.log)("closed", void 0, {
3073
3079
  F: __dxlog_file12,
3074
- L: 200,
3080
+ L: 208,
3075
3081
  S: this,
3076
3082
  C: (f, a) => f(...a)
3077
3083
  });
3078
3084
  }
3079
- signal(signal) {
3085
+ async onSignal(signal) {
3080
3086
  if (this._closed) {
3081
3087
  return;
3082
3088
  }
@@ -3085,16 +3091,10 @@ var SimplePeerTransport = class {
3085
3091
  }
3086
3092
  _disconnectStreams() {
3087
3093
  if (this._piped) {
3088
- this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
3094
+ this._params.stream.unpipe?.(this._peer)?.unpipe?.(this._params.stream);
3089
3095
  }
3090
3096
  }
3091
3097
  };
3092
- var createSimplePeerTransportFactory = (webrtcConfig) => ({
3093
- createTransport: (params) => new SimplePeerTransport({
3094
- ...params,
3095
- webrtcConfig
3096
- })
3097
- });
3098
3098
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
3099
3099
  var SimplePeerTransportService = class {
3100
3100
  constructor(_webrtcConfig) {
@@ -3134,6 +3134,7 @@ var SimplePeerTransportService = class {
3134
3134
  });
3135
3135
  }
3136
3136
  });
3137
+ void transport.open();
3137
3138
  next({
3138
3139
  connection: {
3139
3140
  state: import_bridge.ConnectionState.CONNECTING
@@ -3177,19 +3178,19 @@ var SimplePeerTransportService = class {
3177
3178
  async sendSignal({ proxyId, signal }) {
3178
3179
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3179
3180
  F: __dxlog_file13,
3180
- L: 116,
3181
+ L: 119,
3181
3182
  S: this,
3182
3183
  A: [
3183
3184
  "this.transports.has(proxyId)",
3184
3185
  ""
3185
3186
  ]
3186
3187
  });
3187
- await this.transports.get(proxyId).transport.signal(signal);
3188
+ await this.transports.get(proxyId).transport.onSignal(signal);
3188
3189
  }
3189
3190
  async getDetails({ proxyId }) {
3190
3191
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3191
3192
  F: __dxlog_file13,
3192
- L: 121,
3193
+ L: 124,
3193
3194
  S: this,
3194
3195
  A: [
3195
3196
  "this.transports.has(proxyId)",
@@ -3203,7 +3204,7 @@ var SimplePeerTransportService = class {
3203
3204
  async getStats({ proxyId }) {
3204
3205
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3205
3206
  F: __dxlog_file13,
3206
- L: 126,
3207
+ L: 129,
3207
3208
  S: this,
3208
3209
  A: [
3209
3210
  "this.transports.has(proxyId)",
@@ -3218,14 +3219,14 @@ var SimplePeerTransportService = class {
3218
3219
  if (this.transports.get(proxyId)?.state !== "OPEN") {
3219
3220
  import_log12.log.debug("transport is closed", void 0, {
3220
3221
  F: __dxlog_file13,
3221
- L: 132,
3222
+ L: 135,
3222
3223
  S: this,
3223
3224
  C: (f, a) => f(...a)
3224
3225
  });
3225
3226
  }
3226
3227
  (0, import_invariant11.invariant)(this.transports.has(proxyId), void 0, {
3227
3228
  F: __dxlog_file13,
3228
- L: 134,
3229
+ L: 137,
3229
3230
  S: this,
3230
3231
  A: [
3231
3232
  "this.transports.has(proxyId)",
@@ -3241,25 +3242,26 @@ var SimplePeerTransportService = class {
3241
3242
  }
3242
3243
  }
3243
3244
  async close({ proxyId }) {
3244
- await this.transports.get(proxyId)?.transport.destroy();
3245
+ await this.transports.get(proxyId)?.transport.close();
3245
3246
  await this.transports.get(proxyId)?.stream.end();
3246
3247
  if (this.transports.get(proxyId)) {
3247
3248
  this.transports.get(proxyId).state = "CLOSED";
3248
3249
  }
3249
3250
  (0, import_log12.log)("Closed.", void 0, {
3250
3251
  F: __dxlog_file13,
3251
- L: 150,
3252
+ L: 153,
3252
3253
  S: this,
3253
3254
  C: (f, a) => f(...a)
3254
3255
  });
3255
3256
  }
3256
3257
  };
3257
3258
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3259
+ var RPC_TIMEOUT = 1e4;
3258
3260
  var RESP_MIN_THRESHOLD = 500;
3259
3261
  var TIMEOUT_THRESHOLD = 10;
3260
3262
  var SimplePeerTransportProxy = class {
3261
- constructor(_params) {
3262
- this._params = _params;
3263
+ constructor(_options) {
3264
+ this._options = _options;
3263
3265
  this._proxyId = import_keys12.PublicKey.random();
3264
3266
  this._ctx = new import_context6.Context();
3265
3267
  this._timeoutCount = 0;
@@ -3267,15 +3269,22 @@ var SimplePeerTransportProxy = class {
3267
3269
  this.connected = new import_async10.Event();
3268
3270
  this.errors = new import_debug6.ErrorStream();
3269
3271
  this._closed = false;
3270
- this._serviceStream = this._params.bridgeService.open({
3272
+ }
3273
+ get isOpen() {
3274
+ return !this._closed;
3275
+ }
3276
+ async open() {
3277
+ this._serviceStream = this._options.bridgeService.open({
3271
3278
  proxyId: this._proxyId,
3272
- initiator: this._params.initiator
3279
+ initiator: this._options.initiator
3280
+ }, {
3281
+ timeout: RPC_TIMEOUT
3273
3282
  });
3274
3283
  this._serviceStream.waitUntilReady().then(() => {
3275
3284
  this._serviceStream.subscribe(async (event) => {
3276
3285
  (0, import_log13.log)("SimplePeerTransportProxy: event", event, {
3277
3286
  F: __dxlog_file14,
3278
- L: 59,
3287
+ L: 66,
3279
3288
  S: this,
3280
3289
  C: (f, a) => f(...a)
3281
3290
  });
@@ -3290,16 +3299,16 @@ var SimplePeerTransportProxy = class {
3290
3299
  const proxyStream = new import_node_stream3.Writable({
3291
3300
  write: (chunk, _, callback) => {
3292
3301
  const then = performance.now();
3293
- this._params.bridgeService.sendData({
3302
+ this._options.bridgeService.sendData({
3294
3303
  proxyId: this._proxyId,
3295
3304
  payload: chunk
3296
3305
  }, {
3297
- timeout: 1e4
3306
+ timeout: RPC_TIMEOUT
3298
3307
  }).then(() => {
3299
3308
  if (performance.now() - then > RESP_MIN_THRESHOLD) {
3300
3309
  (0, import_log13.log)("slow response, delaying callback", void 0, {
3301
3310
  F: __dxlog_file14,
3302
- L: 83,
3311
+ L: 90,
3303
3312
  S: this,
3304
3313
  C: (f, a) => f(...a)
3305
3314
  });
@@ -3311,11 +3320,11 @@ var SimplePeerTransportProxy = class {
3311
3320
  }, (err) => {
3312
3321
  if (err instanceof import_protocols8.TimeoutError || err.constructor.name === "TimeoutError") {
3313
3322
  if (this._timeoutCount++ > TIMEOUT_THRESHOLD) {
3314
- throw new import_protocols8.TimeoutError(`too many timeoutes (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
3323
+ throw new import_protocols8.TimeoutError(`too many timeouts (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
3315
3324
  } else {
3316
3325
  (0, import_log13.log)("timeout error, but still invoking callback", void 0, {
3317
3326
  F: __dxlog_file14,
3318
- L: 95,
3327
+ L: 102,
3319
3328
  S: this,
3320
3329
  C: (f, a) => f(...a)
3321
3330
  });
@@ -3324,7 +3333,7 @@ var SimplePeerTransportProxy = class {
3324
3333
  } else {
3325
3334
  import_log13.log.catch(err, void 0, {
3326
3335
  F: __dxlog_file14,
3327
- L: 99,
3336
+ L: 106,
3328
3337
  S: this,
3329
3338
  C: (f, a) => f(...a)
3330
3339
  });
@@ -3337,19 +3346,50 @@ var SimplePeerTransportProxy = class {
3337
3346
  err
3338
3347
  }, {
3339
3348
  F: __dxlog_file14,
3340
- L: 107,
3349
+ L: 114,
3341
3350
  S: this,
3342
3351
  C: (f, a) => f(...a)
3343
3352
  });
3344
3353
  });
3345
- this._params.stream.pipe(proxyStream);
3354
+ this._options.stream.pipe(proxyStream);
3346
3355
  }, (error) => import_log13.log.catch(error, void 0, {
3347
3356
  F: __dxlog_file14,
3348
- L: 112,
3357
+ L: 119,
3349
3358
  S: this,
3350
3359
  C: (f, a) => f(...a)
3351
3360
  }));
3352
3361
  }
3362
+ async close() {
3363
+ await this._ctx.dispose();
3364
+ if (this._closed) {
3365
+ return;
3366
+ }
3367
+ await this._serviceStream.close();
3368
+ try {
3369
+ await this._options.bridgeService.close({
3370
+ proxyId: this._proxyId
3371
+ }, {
3372
+ timeout: RPC_TIMEOUT
3373
+ });
3374
+ } catch (err) {
3375
+ import_log13.log.catch(err, void 0, {
3376
+ F: __dxlog_file14,
3377
+ L: 134,
3378
+ S: this,
3379
+ C: (f, a) => f(...a)
3380
+ });
3381
+ }
3382
+ this.closed.emit();
3383
+ this._closed = true;
3384
+ }
3385
+ async onSignal(signal) {
3386
+ this._options.bridgeService.sendSignal({
3387
+ proxyId: this._proxyId,
3388
+ signal
3389
+ }, {
3390
+ timeout: RPC_TIMEOUT
3391
+ }).catch((err) => this.errors.raise(decodeError(err)));
3392
+ }
3353
3393
  async _handleConnection(connectionEvent) {
3354
3394
  if (connectionEvent.error) {
3355
3395
  this.errors.raise(decodeError(connectionEvent.error));
@@ -3360,55 +3400,31 @@ var SimplePeerTransportProxy = class {
3360
3400
  break;
3361
3401
  }
3362
3402
  case import_bridge2.ConnectionState.CLOSED: {
3363
- await this.destroy();
3403
+ await this.close();
3364
3404
  break;
3365
3405
  }
3366
3406
  }
3367
3407
  }
3368
3408
  _handleData(dataEvent) {
3369
- this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
3409
+ this._options.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
3370
3410
  }
3371
3411
  async _handleSignal(signalEvent) {
3372
- await this._params.sendSignal(signalEvent.payload);
3373
- }
3374
- signal(signal) {
3375
- this._params.bridgeService.sendSignal({
3376
- proxyId: this._proxyId,
3377
- signal
3378
- }).catch((err) => this.errors.raise(decodeError(err)));
3412
+ await this._options.sendSignal(signalEvent.payload);
3379
3413
  }
3380
3414
  async getDetails() {
3381
- return (await this._params.bridgeService.getDetails({
3415
+ return (await this._options.bridgeService.getDetails({
3382
3416
  proxyId: this._proxyId
3417
+ }, {
3418
+ timeout: RPC_TIMEOUT
3383
3419
  })).details;
3384
3420
  }
3385
3421
  async getStats() {
3386
- return (await this._params.bridgeService.getStats({
3422
+ return (await this._options.bridgeService.getStats({
3387
3423
  proxyId: this._proxyId
3424
+ }, {
3425
+ timeout: RPC_TIMEOUT
3388
3426
  })).stats;
3389
3427
  }
3390
- // TODO(burdon): Move open from constructor.
3391
- async destroy() {
3392
- await this._ctx.dispose();
3393
- if (this._closed) {
3394
- return;
3395
- }
3396
- await this._serviceStream.close();
3397
- try {
3398
- await this._params.bridgeService.close({
3399
- proxyId: this._proxyId
3400
- });
3401
- } catch (err) {
3402
- import_log13.log.catch(err, void 0, {
3403
- F: __dxlog_file14,
3404
- L: 171,
3405
- S: this,
3406
- C: (f, a) => f(...a)
3407
- });
3408
- }
3409
- this.closed.emit();
3410
- this._closed = true;
3411
- }
3412
3428
  /**
3413
3429
  * Called when underlying proxy service becomes unavailable.
3414
3430
  */
@@ -3437,7 +3453,7 @@ var SimplePeerTransportProxyFactory = class {
3437
3453
  createTransport(options) {
3438
3454
  (0, import_invariant12.invariant)(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3439
3455
  F: __dxlog_file14,
3440
- L: 208,
3456
+ L: 218,
3441
3457
  S: this,
3442
3458
  A: [
3443
3459
  "this._bridgeService",
@@ -3483,163 +3499,203 @@ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-man
3483
3499
  var DATACHANNEL_LABEL = "dxos.mesh.transport";
3484
3500
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3485
3501
  var MAX_MESSAGE_SIZE = 64 * 1024;
3502
+ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3503
+ createTransport: (options) => new LibDataChannelTransport({
3504
+ ...options,
3505
+ webrtcConfig
3506
+ })
3507
+ });
3486
3508
  var LibDataChannelTransport = class _LibDataChannelTransport {
3487
3509
  static {
3488
3510
  this._instanceCount = 0;
3489
3511
  }
3490
- constructor(params) {
3491
- this.params = params;
3512
+ constructor(_options) {
3513
+ this._options = _options;
3492
3514
  this._closed = false;
3493
- this.closed = new import_async11.Event();
3494
3515
  this._connected = false;
3516
+ this._writeCallback = null;
3517
+ this._readyForCandidates = new import_async11.Trigger();
3518
+ this.closed = new import_async11.Event();
3495
3519
  this.connected = new import_async11.Event();
3496
3520
  this.errors = new import_debug7.ErrorStream();
3497
- this._readyForCandidates = new import_async11.Trigger();
3498
- this._writeCallback = null;
3499
- this._peer = (async () => {
3500
- const { RTCPeerConnection } = (await importESM("node-datachannel/polyfill")).default;
3501
- if (this._closed) {
3502
- this.errors.raise(new Error("connection already closed"));
3503
- }
3504
- if (params.webrtcConfig) {
3505
- params.webrtcConfig.iceServers = params.webrtcConfig.iceServers ?? [];
3506
- } else {
3507
- params.webrtcConfig = {
3508
- iceServers: []
3509
- };
3521
+ }
3522
+ get isOpen() {
3523
+ return !!this._peer && !this._closed;
3524
+ }
3525
+ async open() {
3526
+ if (this._closed) {
3527
+ this.errors.raise(new Error("connection already closed"));
3528
+ }
3529
+ const { RTCPeerConnection } = (await importESM("node-datachannel/polyfill")).default;
3530
+ if (this._options.webrtcConfig) {
3531
+ this._options.webrtcConfig.iceServers = this._options.webrtcConfig.iceServers ?? [];
3532
+ } else {
3533
+ this._options.webrtcConfig = {
3534
+ iceServers: []
3535
+ };
3536
+ }
3537
+ this._peer = new RTCPeerConnection(this._options.webrtcConfig);
3538
+ this._peer.onicecandidateerror = (event) => {
3539
+ import_log14.log.error("peer.onicecandidateerror", {
3540
+ event
3541
+ }, {
3542
+ F: __dxlog_file15,
3543
+ L: 77,
3544
+ S: this,
3545
+ C: (f, a) => f(...a)
3546
+ });
3547
+ };
3548
+ this._peer.onconnectionstatechange = (event) => {
3549
+ import_log14.log.debug("peer.onconnectionstatechange", {
3550
+ event,
3551
+ peerConnectionState: this._peer?.connectionState,
3552
+ transportConnectionState: this._connected
3553
+ }, {
3554
+ F: __dxlog_file15,
3555
+ L: 81,
3556
+ S: this,
3557
+ C: (f, a) => f(...a)
3558
+ });
3559
+ };
3560
+ this._peer.onicecandidate = async (event) => {
3561
+ import_log14.log.debug("peer.onicecandidate", {
3562
+ event
3563
+ }, {
3564
+ F: __dxlog_file15,
3565
+ L: 91,
3566
+ S: this,
3567
+ C: (f, a) => f(...a)
3568
+ });
3569
+ if (event.candidate) {
3570
+ try {
3571
+ await this._options.sendSignal({
3572
+ payload: {
3573
+ data: {
3574
+ type: "candidate",
3575
+ candidate: {
3576
+ candidate: event.candidate.candidate,
3577
+ // These fields never seem to be not null, but connecting to Chrome doesn't work if they are.
3578
+ sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
3579
+ sdpMid: event.candidate.sdpMid ?? 0
3580
+ }
3581
+ }
3582
+ }
3583
+ });
3584
+ } catch (err) {
3585
+ import_log14.log.info("signaling error", {
3586
+ err
3587
+ }, {
3588
+ F: __dxlog_file15,
3589
+ L: 108,
3590
+ S: this,
3591
+ C: (f, a) => f(...a)
3592
+ });
3593
+ }
3510
3594
  }
3511
- const peer = new RTCPeerConnection(params.webrtcConfig);
3512
- _LibDataChannelTransport._instanceCount++;
3513
- peer.onicecandidateerror = (event) => {
3514
- import_log14.log.error("peer.onicecandidateerror", {
3515
- event
3595
+ };
3596
+ if (this._options.initiator) {
3597
+ (0, import_invariant13.invariant)(this._peer, "not open", {
3598
+ F: __dxlog_file15,
3599
+ L: 114,
3600
+ S: this,
3601
+ A: [
3602
+ "this._peer",
3603
+ "'not open'"
3604
+ ]
3605
+ });
3606
+ this._peer.createOffer().then(async (offer) => {
3607
+ if (this._closed) {
3608
+ return;
3609
+ }
3610
+ if (this._peer?.connectionState !== "connecting") {
3611
+ import_log14.log.error("peer not connecting", {
3612
+ peer: this._peer
3613
+ }, {
3614
+ F: __dxlog_file15,
3615
+ L: 125,
3616
+ S: this,
3617
+ C: (f, a) => f(...a)
3618
+ });
3619
+ this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3620
+ }
3621
+ import_log14.log.debug("creating offer", {
3622
+ peer: this._peer,
3623
+ offer
3516
3624
  }, {
3517
3625
  F: __dxlog_file15,
3518
- L: 63,
3626
+ L: 129,
3519
3627
  S: this,
3520
3628
  C: (f, a) => f(...a)
3521
3629
  });
3522
- };
3523
- peer.onconnectionstatechange = (event) => {
3524
- import_log14.log.debug("peer.onconnectionstatechange", {
3525
- event,
3526
- peerConnectionState: peer.connectionState,
3527
- transportConnectionState: this._connected
3528
- }, {
3529
- F: __dxlog_file15,
3530
- L: 67,
3531
- S: this,
3532
- C: (f, a) => f(...a)
3630
+ await this._peer.setLocalDescription(offer);
3631
+ await this._options.sendSignal({
3632
+ payload: {
3633
+ data: {
3634
+ type: offer.type,
3635
+ sdp: offer.sdp
3636
+ }
3637
+ }
3533
3638
  });
3639
+ }).catch((err) => {
3640
+ this.errors.raise(err);
3641
+ });
3642
+ this._handleChannel(this._peer.createDataChannel(DATACHANNEL_LABEL));
3643
+ import_log14.log.debug("created data channel", void 0, {
3644
+ F: __dxlog_file15,
3645
+ L: 139,
3646
+ S: this,
3647
+ C: (f, a) => f(...a)
3648
+ });
3649
+ this._peer.ondatachannel = () => {
3650
+ this.errors.raise(new Error("unexpected ondatachannel event for initiator"));
3534
3651
  };
3535
- peer.onicecandidate = async (event) => {
3536
- import_log14.log.debug("peer.onicecandidate", {
3652
+ } else {
3653
+ this._peer.ondatachannel = (event) => {
3654
+ import_log14.log.debug("peer.ondatachannel (non-initiator)", {
3537
3655
  event
3538
3656
  }, {
3539
3657
  F: __dxlog_file15,
3540
- L: 76,
3658
+ L: 145,
3541
3659
  S: this,
3542
3660
  C: (f, a) => f(...a)
3543
3661
  });
3544
- if (event.candidate) {
3545
- try {
3546
- await params.sendSignal({
3547
- payload: {
3548
- data: {
3549
- type: "candidate",
3550
- candidate: {
3551
- candidate: event.candidate.candidate,
3552
- // these fields never seem to be not null, but connecting to Chrome doesn't work if they are
3553
- sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
3554
- sdpMid: event.candidate.sdpMid ?? 0
3555
- }
3556
- }
3557
- }
3558
- });
3559
- } catch (err) {
3560
- import_log14.log.info("signaling errror", {
3561
- err
3562
- }, {
3563
- F: __dxlog_file15,
3564
- L: 93,
3565
- S: this,
3566
- C: (f, a) => f(...a)
3567
- });
3568
- }
3662
+ if (event.channel.label !== DATACHANNEL_LABEL) {
3663
+ this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
3569
3664
  }
3665
+ this._handleChannel(event.channel);
3570
3666
  };
3571
- if (params.initiator) {
3572
- peer.createOffer().then(async (offer) => {
3573
- if (this._closed) {
3574
- return;
3575
- }
3576
- if (peer.connectionState !== "connecting") {
3577
- import_log14.log.error("i am initiator but peer not in state connecting", {
3578
- peer
3579
- }, {
3580
- F: __dxlog_file15,
3581
- L: 106,
3582
- S: this,
3583
- C: (f, a) => f(...a)
3584
- });
3585
- this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3586
- }
3587
- import_log14.log.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3588
- offer
3589
- }, {
3590
- F: __dxlog_file15,
3591
- L: 109,
3592
- S: this,
3593
- C: (f, a) => f(...a)
3594
- });
3595
- await peer.setLocalDescription(offer);
3596
- await params.sendSignal({
3597
- payload: {
3598
- data: {
3599
- type: offer.type,
3600
- sdp: offer.sdp
3601
- }
3602
- }
3603
- });
3604
- return offer;
3605
- }).catch((err) => {
3606
- this.errors.raise(err);
3607
- });
3608
- this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
3609
- import_log14.log.debug("created data channel", void 0, {
3610
- F: __dxlog_file15,
3611
- L: 118,
3612
- S: this,
3613
- C: (f, a) => f(...a)
3614
- });
3615
- peer.ondatachannel = (event) => {
3616
- this.errors.raise(new Error("got ondatachannel when i am the initiator?"));
3617
- };
3618
- } else {
3619
- peer.ondatachannel = (event) => {
3620
- import_log14.log.debug("peer.ondatachannel (non-initiator)", {
3621
- event
3622
- }, {
3623
- F: __dxlog_file15,
3624
- L: 124,
3625
- S: this,
3626
- C: (f, a) => f(...a)
3627
- });
3628
- if (event.channel.label !== DATACHANNEL_LABEL) {
3629
- this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
3630
- }
3631
- this.handleChannel(event.channel);
3632
- };
3633
- }
3634
- return peer;
3635
- })();
3667
+ }
3668
+ _LibDataChannelTransport._instanceCount++;
3669
+ }
3670
+ async close() {
3671
+ await this._close();
3672
+ if (--_LibDataChannelTransport._instanceCount === 0) {
3673
+ (await importESM("node-datachannel")).cleanup();
3674
+ }
3675
+ }
3676
+ async _close() {
3677
+ if (this._closed) {
3678
+ return;
3679
+ }
3680
+ await this._disconnectStreams();
3681
+ try {
3682
+ this._peer?.close();
3683
+ } catch (err) {
3684
+ this.errors.raise(err);
3685
+ }
3686
+ this._peer = void 0;
3687
+ this._closed = true;
3688
+ this.closed.emit();
3636
3689
  }
3637
- handleChannel(dataChannel) {
3690
+ /**
3691
+ * Handle data channel events.
3692
+ */
3693
+ _handleChannel(dataChannel) {
3638
3694
  this._channel = dataChannel;
3639
3695
  this._channel.onopen = () => {
3640
- import_log14.log.debug("dataChannel.onopen", void 0, {
3696
+ import_log14.log.debug("channel.onopen", void 0, {
3641
3697
  F: __dxlog_file15,
3642
- L: 140,
3698
+ L: 190,
3643
3699
  S: this,
3644
3700
  C: (f, a) => f(...a)
3645
3701
  });
@@ -3658,9 +3714,9 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3658
3714
  }
3659
3715
  if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3660
3716
  if (this._writeCallback !== null) {
3661
- import_log14.log.error("consumer trying to write before we're ready for more data", void 0, {
3717
+ import_log14.log.error("consumer trying to write before we are ready for more data", void 0, {
3662
3718
  F: __dxlog_file15,
3663
- L: 157,
3719
+ L: 207,
3664
3720
  S: this,
3665
3721
  C: (f, a) => f(...a)
3666
3722
  });
@@ -3671,26 +3727,31 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3671
3727
  }
3672
3728
  }
3673
3729
  });
3674
- duplex.pipe(this.params.stream).pipe(duplex);
3730
+ duplex.pipe(this._options.stream).pipe(duplex);
3675
3731
  this._stream = duplex;
3676
3732
  this._connected = true;
3677
3733
  this.connected.emit();
3678
3734
  };
3679
- this._channel.onerror = async (err) => {
3680
- this.errors.raise(new Error("channel error: " + err.toString()));
3681
- await this._close();
3682
- };
3683
3735
  this._channel.onclose = async (err) => {
3684
- import_log14.log.info("channel onclose", {
3736
+ import_log14.log.info("channel.onclose", {
3685
3737
  err
3686
3738
  }, {
3687
3739
  F: __dxlog_file15,
3688
- L: 177,
3740
+ L: 223,
3689
3741
  S: this,
3690
3742
  C: (f, a) => f(...a)
3691
3743
  });
3692
3744
  await this._close();
3693
3745
  };
3746
+ this._channel.onerror = async (err) => {
3747
+ this.errors.raise(new Error("channel error: " + err.toString()));
3748
+ await this._close();
3749
+ };
3750
+ this._channel.onbufferedamountlow = () => {
3751
+ const cb = this._writeCallback;
3752
+ this._writeCallback = null;
3753
+ cb?.();
3754
+ };
3694
3755
  this._channel.onmessage = (event) => {
3695
3756
  let data = event.data;
3696
3757
  if (data instanceof ArrayBuffer) {
@@ -3698,37 +3759,27 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3698
3759
  }
3699
3760
  this._stream.push(data);
3700
3761
  };
3701
- this._channel.onbufferedamountlow = () => {
3702
- const cb = this._writeCallback;
3703
- this._writeCallback = null;
3704
- cb?.();
3705
- };
3706
3762
  }
3707
- async _close() {
3708
- if (this._closed) {
3709
- return;
3710
- }
3711
- await this._disconnectStreams();
3712
- const peer = await this._peer;
3763
+ async onSignal(signal) {
3764
+ (0, import_invariant13.invariant)(this._peer, "not open", {
3765
+ F: __dxlog_file15,
3766
+ L: 249,
3767
+ S: this,
3768
+ A: [
3769
+ "this._peer",
3770
+ "'not open'"
3771
+ ]
3772
+ });
3713
3773
  try {
3714
- peer.close();
3715
- } catch (err) {
3716
- this.errors.raise(err);
3717
- }
3718
- this._closed = true;
3719
- this.closed.emit();
3720
- }
3721
- signal(signal) {
3722
- this._peer.then(async (peer) => {
3723
3774
  const data = signal.payload.data;
3724
3775
  switch (data.type) {
3725
3776
  case "offer": {
3726
- if ((await this._peer).connectionState !== "new") {
3777
+ if (this._peer.connectionState !== "new") {
3727
3778
  import_log14.log.error("received offer but peer not in state new", {
3728
- peer
3779
+ peer: this._peer
3729
3780
  }, {
3730
3781
  F: __dxlog_file15,
3731
- L: 218,
3782
+ L: 256,
3732
3783
  S: this,
3733
3784
  C: (f, a) => f(...a)
3734
3785
  });
@@ -3736,13 +3787,13 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3736
3787
  break;
3737
3788
  }
3738
3789
  try {
3739
- await peer.setRemoteDescription({
3790
+ await this._peer.setRemoteDescription({
3740
3791
  type: data.type,
3741
3792
  sdp: data.sdp
3742
3793
  });
3743
- const answer = await peer.createAnswer();
3744
- await peer.setLocalDescription(answer);
3745
- await this.params.sendSignal({
3794
+ const answer = await this._peer.createAnswer();
3795
+ await this._peer.setLocalDescription(answer);
3796
+ await this._options.sendSignal({
3746
3797
  payload: {
3747
3798
  data: {
3748
3799
  type: answer.type,
@@ -3752,11 +3803,11 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3752
3803
  });
3753
3804
  this._readyForCandidates.wake();
3754
3805
  } catch (err) {
3755
- import_log14.log.error("can't handle offer from signalling server", {
3806
+ import_log14.log.error("cannot handle offer from signalling server", {
3756
3807
  err
3757
3808
  }, {
3758
3809
  F: __dxlog_file15,
3759
- L: 229,
3810
+ L: 268,
3760
3811
  S: this,
3761
3812
  C: (f, a) => f(...a)
3762
3813
  });
@@ -3766,17 +3817,17 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3766
3817
  }
3767
3818
  case "answer":
3768
3819
  try {
3769
- await peer.setRemoteDescription({
3820
+ await this._peer.setRemoteDescription({
3770
3821
  type: data.type,
3771
3822
  sdp: data.sdp
3772
3823
  });
3773
3824
  this._readyForCandidates.wake();
3774
3825
  } catch (err) {
3775
- import_log14.log.error("can't handle answer from signalling server", {
3826
+ import_log14.log.error("cannot handle answer from signalling server", {
3776
3827
  err
3777
3828
  }, {
3778
3829
  F: __dxlog_file15,
3779
- L: 240,
3830
+ L: 279,
3780
3831
  S: this,
3781
3832
  C: (f, a) => f(...a)
3782
3833
  });
@@ -3785,7 +3836,7 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3785
3836
  break;
3786
3837
  case "candidate":
3787
3838
  await this._readyForCandidates.wait();
3788
- await peer.addIceCandidate({
3839
+ await this._peer.addIceCandidate({
3789
3840
  candidate: data.candidate.candidate
3790
3841
  });
3791
3842
  break;
@@ -3795,20 +3846,20 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3795
3846
  signal
3796
3847
  }, {
3797
3848
  F: __dxlog_file15,
3798
- L: 251,
3849
+ L: 290,
3799
3850
  S: this,
3800
3851
  C: (f, a) => f(...a)
3801
3852
  });
3802
3853
  this.errors.raise(new Error(`unhandled signal type ${data.type}`));
3803
3854
  }
3804
- }).catch((err) => {
3855
+ } catch (err) {
3805
3856
  import_log14.log.catch(err, void 0, {
3806
3857
  F: __dxlog_file15,
3807
- L: 256,
3858
+ L: 294,
3808
3859
  S: this,
3809
3860
  C: (f, a) => f(...a)
3810
3861
  });
3811
- });
3862
+ }
3812
3863
  }
3813
3864
  async getDetails() {
3814
3865
  const stats = await this._getStats();
@@ -3821,26 +3872,6 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3821
3872
  }
3822
3873
  return `${rc.ip}:${rc.port} ${rc.candidateType}`;
3823
3874
  }
3824
- async _getStats() {
3825
- return this._peer.then(async (peer) => {
3826
- const stats = await peer.getStats();
3827
- const statsEntries = Array.from(stats.entries());
3828
- const transport = statsEntries.filter((s) => s[1].type === "transport")[0][1];
3829
- const candidatePair = statsEntries.filter((s) => s[0] === transport.selectedCandidatePairId);
3830
- let selectedCandidatePair;
3831
- let remoteCandidate;
3832
- if (candidatePair.length > 0) {
3833
- selectedCandidatePair = candidatePair[0][1];
3834
- remoteCandidate = statsEntries.filter((s) => s[0] === selectedCandidatePair.remoteCandidateId)[0][1];
3835
- }
3836
- return {
3837
- transport,
3838
- selectedCandidatePair,
3839
- remoteCandidate,
3840
- raw: Object.fromEntries(stats)
3841
- };
3842
- });
3843
- }
3844
3875
  async getStats() {
3845
3876
  const stats = await this._getStats();
3846
3877
  if (!stats) {
@@ -3860,43 +3891,63 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3860
3891
  rawStats: stats.raw
3861
3892
  };
3862
3893
  }
3863
- async destroy() {
3864
- await this._close();
3865
- if (--_LibDataChannelTransport._instanceCount === 0) {
3866
- (await importESM("node-datachannel")).cleanup();
3894
+ async _getStats() {
3895
+ (0, import_invariant13.invariant)(this._peer, "not open", {
3896
+ F: __dxlog_file15,
3897
+ L: 334,
3898
+ S: this,
3899
+ A: [
3900
+ "this._peer",
3901
+ "'not open'"
3902
+ ]
3903
+ });
3904
+ const stats = await this._peer.getStats();
3905
+ const statsEntries = Array.from(stats.entries());
3906
+ const transport = statsEntries.filter((s) => s[1].type === "transport")[0][1];
3907
+ const candidatePair = statsEntries.filter((s) => s[0] === transport.selectedCandidatePairId);
3908
+ let selectedCandidatePair;
3909
+ let remoteCandidate;
3910
+ if (candidatePair.length > 0) {
3911
+ selectedCandidatePair = candidatePair[0][1];
3912
+ remoteCandidate = statsEntries.filter((s) => s[0] === selectedCandidatePair.remoteCandidateId)[0][1];
3867
3913
  }
3914
+ return {
3915
+ transport,
3916
+ selectedCandidatePair,
3917
+ remoteCandidate,
3918
+ raw: Object.fromEntries(stats)
3919
+ };
3868
3920
  }
3869
3921
  async _disconnectStreams() {
3870
- this.params.stream.unpipe?.(this._stream)?.unpipe?.(this.params.stream);
3922
+ this._options.stream.unpipe?.(this._stream)?.unpipe?.(this._options.stream);
3871
3923
  }
3872
3924
  };
3873
3925
  _ts_decorate5([
3874
3926
  import_async11.synchronized
3875
3927
  ], LibDataChannelTransport.prototype, "_close", null);
3876
- var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3877
- createTransport: (params) => new LibDataChannelTransport({
3878
- ...params,
3879
- webrtcConfig
3880
- })
3881
- });
3882
3928
  var importESM = Function("path", "return import(path)");
3883
3929
  var __dxlog_file16 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/tcp-transport.ts";
3884
3930
  var TcpTransportFactory = {
3885
- createTransport: (params) => new TcpTransport(params)
3931
+ createTransport: (options) => new TcpTransport(options)
3886
3932
  };
3887
3933
  var TcpTransport = class {
3888
3934
  constructor(options) {
3889
3935
  this.options = options;
3890
3936
  this._server = void 0;
3891
3937
  this._socket = void 0;
3938
+ this._connected = false;
3939
+ this._closed = false;
3892
3940
  this.closed = new import_async12.Event();
3893
3941
  this.connected = new import_async12.Event();
3894
3942
  this.errors = new import_debug8.ErrorStream();
3895
- this._destroyed = false;
3896
- this._connected = false;
3897
- (0, import_log15.log)("creating", void 0, {
3943
+ }
3944
+ get isOpen() {
3945
+ return this._connected && !this._closed;
3946
+ }
3947
+ async open() {
3948
+ (0, import_log15.log)("opening", void 0, {
3898
3949
  F: __dxlog_file16,
3899
- L: 34,
3950
+ L: 39,
3900
3951
  S: this,
3901
3952
  C: (f, a) => f(...a)
3902
3953
  });
@@ -3906,7 +3957,7 @@ var TcpTransport = class {
3906
3957
  this._server = new Server((socket) => {
3907
3958
  (0, import_log15.log)("new connection", void 0, {
3908
3959
  F: __dxlog_file16,
3909
- L: 42,
3960
+ L: 47,
3910
3961
  S: this,
3911
3962
  C: (f, a) => f(...a)
3912
3963
  });
@@ -3921,7 +3972,7 @@ var TcpTransport = class {
3921
3972
  port
3922
3973
  }, {
3923
3974
  F: __dxlog_file16,
3924
- L: 51,
3975
+ L: 56,
3925
3976
  S: this,
3926
3977
  C: (f, a) => f(...a)
3927
3978
  });
@@ -3930,7 +3981,7 @@ var TcpTransport = class {
3930
3981
  port
3931
3982
  }
3932
3983
  }).catch((err) => {
3933
- if (!this._destroyed) {
3984
+ if (!this._closed) {
3934
3985
  this.errors.raise(err);
3935
3986
  }
3936
3987
  });
@@ -3942,23 +3993,23 @@ var TcpTransport = class {
3942
3993
  });
3943
3994
  }
3944
3995
  }
3945
- async destroy() {
3996
+ async close() {
3946
3997
  (0, import_log15.log)("closing", void 0, {
3947
3998
  F: __dxlog_file16,
3948
- L: 72,
3999
+ L: 78,
3949
4000
  S: this,
3950
4001
  C: (f, a) => f(...a)
3951
4002
  });
3952
4003
  this._socket?.destroy();
3953
4004
  this._server?.close();
3954
- this._destroyed = true;
4005
+ this._closed = true;
3955
4006
  }
3956
- signal({ payload }) {
4007
+ async onSignal({ payload }) {
3957
4008
  (0, import_log15.log)("received signal", {
3958
4009
  payload
3959
4010
  }, {
3960
4011
  F: __dxlog_file16,
3961
- L: 80,
4012
+ L: 85,
3962
4013
  S: this,
3963
4014
  C: (f, a) => f(...a)
3964
4015
  });
@@ -3994,19 +4045,17 @@ var TcpTransport = class {
3994
4045
  localPort: socket.localPort
3995
4046
  }, {
3996
4047
  F: __dxlog_file16,
3997
- L: 109,
4048
+ L: 115,
3998
4049
  S: this,
3999
4050
  C: (f, a) => f(...a)
4000
4051
  });
4001
4052
  this._socket = socket;
4002
- this.connected.emit();
4003
- this.options.stream.pipe(this._socket).pipe(this.options.stream);
4004
4053
  this._socket.on("connect", () => {
4005
4054
  (0, import_log15.log)("connected to", {
4006
4055
  port: this._socket?.remotePort
4007
4056
  }, {
4008
4057
  F: __dxlog_file16,
4009
- L: 115,
4058
+ L: 119,
4010
4059
  S: this,
4011
4060
  C: (f, a) => f(...a)
4012
4061
  });
@@ -4018,6 +4067,8 @@ var TcpTransport = class {
4018
4067
  this._socket.on("close", () => {
4019
4068
  this.closed.emit();
4020
4069
  });
4070
+ this.connected.emit();
4071
+ this.options.stream.pipe(this._socket).pipe(this.options.stream);
4021
4072
  }
4022
4073
  };
4023
4074
  var createTeleportProtocolFactory = (onConnection) => {
@@ -4067,4 +4118,4 @@ var createTeleportProtocolFactory = (onConnection) => {
4067
4118
  createSimplePeerTransportFactory,
4068
4119
  createTeleportProtocolFactory
4069
4120
  });
4070
- //# sourceMappingURL=chunk-5D3NBEP2.cjs.map
4121
+ //# sourceMappingURL=chunk-BMZFS4HT.cjs.map