@dxos/network-manager 0.1.56-main.c19b7cd → 0.1.56-main.c277f68

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 (42) hide show
  1. package/dist/lib/browser/{chunk-FC6CWDES.mjs → chunk-WQ3YXGWP.mjs} +132 -141
  2. package/dist/lib/browser/chunk-WQ3YXGWP.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 +23 -16
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/index.cjs +130 -139
  8. package/dist/lib/node/index.cjs.map +3 -3
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +152 -154
  11. package/dist/lib/node/testing/index.cjs.map +3 -3
  12. package/dist/types/src/connection-log.d.ts.map +1 -1
  13. package/dist/types/src/signal/swarm-messenger.d.ts +1 -1
  14. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  15. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  16. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  17. package/dist/types/src/swarm/connection.d.ts +2 -1
  18. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  19. package/dist/types/src/swarm/peer.d.ts +1 -1
  20. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  21. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts +4 -3
  23. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  24. package/dist/types/src/testing/test-wire-protocol.d.ts +13 -2
  25. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  26. package/dist/types/src/transport/webrtc-transport-proxy.d.ts +1 -1
  27. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  28. package/dist/types/src/transport/webrtc-transport.d.ts +3 -3
  29. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  30. package/package.json +18 -18
  31. package/src/connection-log.ts +2 -0
  32. package/src/signal/swarm-messenger.ts +2 -2
  33. package/src/swarm/connection-limiter.ts +1 -1
  34. package/src/swarm/connection.ts +12 -3
  35. package/src/swarm/peer.ts +8 -6
  36. package/src/swarm/swarm.ts +5 -5
  37. package/src/testing/test-builder.ts +9 -5
  38. package/src/testing/test-wire-protocol.ts +22 -2
  39. package/src/tests/memory-transport.test.ts +1 -1
  40. package/src/transport/webrtc-transport-proxy.ts +4 -4
  41. package/src/transport/webrtc-transport.ts +20 -6
  42. package/dist/lib/browser/chunk-FC6CWDES.mjs.map +0 -7
@@ -145,7 +145,7 @@ var Connection = class {
145
145
  initiator: this.initiator
146
146
  }, {
147
147
  F: __dxlog_file,
148
- L: 95,
148
+ L: 96,
149
149
  S: this,
150
150
  C: (f, a) => f(...a)
151
151
  });
@@ -165,7 +165,7 @@ var Connection = class {
165
165
  async openConnection() {
166
166
  (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
167
167
  F: __dxlog_file,
168
- L: 120,
168
+ L: 121,
169
169
  S: this,
170
170
  A: [
171
171
  "this._state === ConnectionState.INITIAL",
@@ -176,7 +176,7 @@ var Connection = class {
176
176
  id: this._instanceId
177
177
  }), {
178
178
  F: __dxlog_file,
179
- L: 121,
179
+ L: 122,
180
180
  S: this,
181
181
  C: (f, a) => f(...a)
182
182
  });
@@ -188,7 +188,7 @@ var Connection = class {
188
188
  initiator: this.initiator
189
189
  }, {
190
190
  F: __dxlog_file,
191
- L: 122,
191
+ L: 123,
192
192
  S: this,
193
193
  C: (f, a) => f(...a)
194
194
  });
@@ -199,7 +199,7 @@ var Connection = class {
199
199
  this._protocol.stream.on("close", () => {
200
200
  (0, import_log.log)("protocol stream closed", void 0, {
201
201
  F: __dxlog_file,
202
- L: 139,
202
+ L: 140,
203
203
  S: this,
204
204
  C: (f, a) => f(...a)
205
205
  });
@@ -207,7 +207,7 @@ var Connection = class {
207
207
  });
208
208
  (0, import_invariant.invariant)(!this._transport, void 0, {
209
209
  F: __dxlog_file,
210
- L: 143,
210
+ L: 144,
211
211
  S: this,
212
212
  A: [
213
213
  "!this._transport",
@@ -227,6 +227,9 @@ var Connection = class {
227
227
  this.close().catch((err) => this.errors.raise(err));
228
228
  });
229
229
  this._transport.errors.handle((err) => {
230
+ if (!this.closeReason) {
231
+ this.closeReason = err?.message;
232
+ }
230
233
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
231
234
  this.errors.raise(err);
232
235
  }
@@ -239,13 +242,15 @@ var Connection = class {
239
242
  id: this._instanceId
240
243
  }), {
241
244
  F: __dxlog_file,
242
- L: 172,
245
+ L: 176,
243
246
  S: this,
244
247
  C: (f, a) => f(...a)
245
248
  });
246
249
  }
247
- async close() {
248
- var _a;
250
+ async close(err) {
251
+ if (!this.closeReason) {
252
+ this.closeReason = err?.message;
253
+ }
249
254
  if (this._state === ConnectionState.CLOSED) {
250
255
  return;
251
256
  }
@@ -255,26 +260,26 @@ var Connection = class {
255
260
  peerId: this.ownId
256
261
  }, {
257
262
  F: __dxlog_file,
258
- L: 184,
263
+ L: 191,
259
264
  S: this,
260
265
  C: (f, a) => f(...a)
261
266
  });
262
267
  try {
263
268
  await this._protocol.destroy();
264
- } catch (err) {
265
- import_log.log.catch(err, void 0, {
269
+ } catch (err2) {
270
+ import_log.log.catch(err2, void 0, {
266
271
  F: __dxlog_file,
267
- L: 190,
272
+ L: 197,
268
273
  S: this,
269
274
  C: (f, a) => f(...a)
270
275
  });
271
276
  }
272
277
  try {
273
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
274
- } catch (err) {
275
- import_log.log.catch(err, void 0, {
278
+ await this._transport?.destroy();
279
+ } catch (err2) {
280
+ import_log.log.catch(err2, void 0, {
276
281
  F: __dxlog_file,
277
- L: 197,
282
+ L: 204,
278
283
  S: this,
279
284
  C: (f, a) => f(...a)
280
285
  });
@@ -283,7 +288,7 @@ var Connection = class {
283
288
  peerId: this.ownId
284
289
  }, {
285
290
  F: __dxlog_file,
286
- L: 200,
291
+ L: 207,
287
292
  S: this,
288
293
  C: (f, a) => f(...a)
289
294
  });
@@ -298,8 +303,10 @@ var Connection = class {
298
303
  return;
299
304
  }
300
305
  try {
301
- await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
302
- this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
306
+ if (process.env.NODE_ENV !== "test") {
307
+ await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
308
+ this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
309
+ }
303
310
  const signals = [
304
311
  ...this._outgoingSignalBuffer
305
312
  ];
@@ -323,7 +330,7 @@ var Connection = class {
323
330
  err
324
331
  }, {
325
332
  F: __dxlog_file,
326
- L: 234,
333
+ L: 243,
327
334
  S: this,
328
335
  C: (f, a) => f(...a)
329
336
  });
@@ -334,10 +341,9 @@ var Connection = class {
334
341
  * Receive a signal from the remote peer.
335
342
  */
336
343
  async signal(msg) {
337
- var _a, _b, _c;
338
344
  (0, import_invariant.invariant)(msg.sessionId, void 0, {
339
345
  F: __dxlog_file,
340
- L: 243,
346
+ L: 252,
341
347
  S: this,
342
348
  A: [
343
349
  "msg.sessionId",
@@ -347,7 +353,7 @@ var Connection = class {
347
353
  if (!msg.sessionId.equals(this.sessionId)) {
348
354
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
349
355
  F: __dxlog_file,
350
- L: 245,
356
+ L: 254,
351
357
  S: this,
352
358
  C: (f, a) => f(...a)
353
359
  });
@@ -355,32 +361,32 @@ var Connection = class {
355
361
  }
356
362
  (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
357
363
  F: __dxlog_file,
358
- L: 248,
364
+ L: 257,
359
365
  S: this,
360
366
  A: [
361
367
  "msg.data.signal || msg.data.signalBatch",
362
368
  ""
363
369
  ]
364
370
  });
365
- (0, import_invariant.invariant)((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId), void 0, {
371
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
366
372
  F: __dxlog_file,
367
- L: 249,
373
+ L: 258,
368
374
  S: this,
369
375
  A: [
370
376
  "msg.author?.equals(this.remoteId)",
371
377
  ""
372
378
  ]
373
379
  });
374
- (0, import_invariant.invariant)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId), void 0, {
380
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
375
381
  F: __dxlog_file,
376
- L: 250,
382
+ L: 259,
377
383
  S: this,
378
384
  A: [
379
385
  "msg.recipient?.equals(this.ownId)",
380
386
  ""
381
387
  ]
382
388
  });
383
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
389
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
384
390
  msg.data.signal
385
391
  ];
386
392
  for (const signal of signals) {
@@ -394,7 +400,7 @@ var Connection = class {
394
400
  msg: msg.data
395
401
  }, {
396
402
  F: __dxlog_file,
397
- L: 259,
403
+ L: 268,
398
404
  S: this,
399
405
  C: (f, a) => f(...a)
400
406
  });
@@ -402,7 +408,7 @@ var Connection = class {
402
408
  } else {
403
409
  (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
404
410
  F: __dxlog_file,
405
- L: 262,
411
+ L: 271,
406
412
  S: this,
407
413
  A: [
408
414
  "this._transport",
@@ -415,7 +421,7 @@ var Connection = class {
415
421
  msg: msg.data
416
422
  }, {
417
423
  F: __dxlog_file,
418
- L: 263,
424
+ L: 272,
419
425
  S: this,
420
426
  C: (f, a) => f(...a)
421
427
  });
@@ -430,13 +436,13 @@ var Connection = class {
430
436
  peerId: this.ownId
431
437
  }, {
432
438
  F: __dxlog_file,
433
- L: 270,
439
+ L: 279,
434
440
  S: this,
435
441
  C: (f, a) => f(...a)
436
442
  });
437
443
  (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
438
444
  F: __dxlog_file,
439
- L: 271,
445
+ L: 280,
440
446
  S: this,
441
447
  A: [
442
448
  "state !== this._state",
@@ -480,7 +486,6 @@ var SwarmMessenger = class {
480
486
  this._topic = topic;
481
487
  }
482
488
  async receiveMessage({ author, recipient, payload }) {
483
- var _a, _b, _c, _d;
484
489
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
485
490
  return;
486
491
  }
@@ -498,21 +503,21 @@ var SwarmMessenger = class {
498
503
  S: this,
499
504
  C: (f, a) => f(...a)
500
505
  });
501
- if ((_a = message.data) == null ? void 0 : _a.offer) {
506
+ if (message.data?.offer) {
502
507
  await this._handleOffer({
503
508
  author,
504
509
  recipient,
505
510
  message
506
511
  });
507
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
512
+ } else if (message.data?.answer) {
508
513
  await this._resolveAnswers(message);
509
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
514
+ } else if (message.data?.signal) {
510
515
  await this._handleSignal({
511
516
  author,
512
517
  recipient,
513
518
  message
514
519
  });
515
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
520
+ } else if (message.data?.signalBatch) {
516
521
  await this._handleSignal({
517
522
  author,
518
523
  recipient,
@@ -530,8 +535,7 @@ var SwarmMessenger = class {
530
535
  }
531
536
  }
532
537
  async signal(message) {
533
- var _a, _b;
534
- (0, import_invariant2.invariant)(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message", {
538
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
535
539
  F: __dxlog_file2,
536
540
  L: 85,
537
541
  S: this,
@@ -563,11 +567,10 @@ var SwarmMessenger = class {
563
567
  });
564
568
  }
565
569
  async _sendReliableMessage({ author, recipient, message }) {
566
- var _a;
567
570
  const networkMessage = {
568
571
  ...message,
569
572
  // Setting unique message_id if it not specified yet.
570
- messageId: (_a = message.messageId) != null ? _a : import_keys2.PublicKey.random()
573
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
571
574
  };
572
575
  (0, import_log2.log)("sending", {
573
576
  from: author,
@@ -589,8 +592,7 @@ var SwarmMessenger = class {
589
592
  });
590
593
  }
591
594
  async _resolveAnswers(message) {
592
- var _a, _b, _c;
593
- (0, import_invariant2.invariant)((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId", {
595
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
594
596
  F: __dxlog_file2,
595
597
  L: 135,
596
598
  S: this,
@@ -602,7 +604,7 @@ var SwarmMessenger = class {
602
604
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
603
605
  if (offerRecord) {
604
606
  this._offerRecords.delete(message.data.answer.offerMessageId);
605
- (0, import_invariant2.invariant)((_c = message.data) == null ? void 0 : _c.answer, "No answer", {
607
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
606
608
  F: __dxlog_file2,
607
609
  L: 139,
608
610
  S: this,
@@ -717,6 +719,7 @@ function _ts_decorate2(decorators, target, key, desc) {
717
719
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
718
720
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
719
721
  var Peer = class {
722
+ // TODO(burdon): Convert to map?
720
723
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
721
724
  this.id = id;
722
725
  this.topic = topic;
@@ -736,7 +739,6 @@ var Peer = class {
736
739
  * Respond to remote offer.
737
740
  */
738
741
  async onOffer(message) {
739
- var _a;
740
742
  const remoteId = message.author;
741
743
  if (this.connection || this.initiating) {
742
744
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -744,15 +746,15 @@ var Peer = class {
744
746
  localPeerId: this.id,
745
747
  topic: this.topic,
746
748
  remotePeerId: this.localPeerId,
747
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
749
+ sessionId: this.connection?.sessionId
748
750
  }, {
749
751
  F: __dxlog_file3,
750
- L: 106,
752
+ L: 108,
751
753
  S: this,
752
754
  C: (f, a) => f(...a)
753
755
  });
754
756
  if (this.connection) {
755
- await this.closeConnection();
757
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
756
758
  }
757
759
  } else {
758
760
  return {
@@ -764,7 +766,7 @@ var Peer = class {
764
766
  if (!this.connection) {
765
767
  (0, import_invariant3.invariant)(message.sessionId, void 0, {
766
768
  F: __dxlog_file3,
767
- L: 126,
769
+ L: 128,
768
770
  S: this,
769
771
  A: [
770
772
  "message.sessionId",
@@ -784,12 +786,12 @@ var Peer = class {
784
786
  err
785
787
  }, {
786
788
  F: __dxlog_file3,
787
- L: 134,
789
+ L: 136,
788
790
  S: this,
789
791
  C: (f, a) => f(...a)
790
792
  });
791
793
  }
792
- await this.closeConnection();
794
+ await this.closeConnection(err);
793
795
  }
794
796
  return {
795
797
  accept: true
@@ -806,7 +808,7 @@ var Peer = class {
806
808
  async initiateConnection() {
807
809
  (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
808
810
  F: __dxlog_file3,
809
- L: 151,
811
+ L: 153,
810
812
  S: this,
811
813
  A: [
812
814
  "!this.initiating",
@@ -815,7 +817,7 @@ var Peer = class {
815
817
  });
816
818
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
817
819
  F: __dxlog_file3,
818
- L: 152,
820
+ L: 154,
819
821
  S: this,
820
822
  A: [
821
823
  "!this.connection",
@@ -830,7 +832,7 @@ var Peer = class {
830
832
  sessionId
831
833
  }, {
832
834
  F: __dxlog_file3,
833
- L: 154,
835
+ L: 156,
834
836
  S: this,
835
837
  C: (f, a) => f(...a)
836
838
  });
@@ -854,14 +856,14 @@ var Peer = class {
854
856
  remoteId: this.id
855
857
  }, {
856
858
  F: __dxlog_file3,
857
- L: 169,
859
+ L: 171,
858
860
  S: this,
859
861
  C: (f, a) => f(...a)
860
862
  });
861
863
  if (connection.state !== ConnectionState.INITIAL) {
862
864
  (0, import_log3.log)("ignoring response", void 0, {
863
865
  F: __dxlog_file3,
864
- L: 171,
866
+ L: 173,
865
867
  S: this,
866
868
  C: (f, a) => f(...a)
867
869
  });
@@ -881,11 +883,11 @@ var Peer = class {
881
883
  remoteId: this.id
882
884
  }, {
883
885
  F: __dxlog_file3,
884
- L: 182,
886
+ L: 184,
885
887
  S: this,
886
888
  C: (f, a) => f(...a)
887
889
  });
888
- await this.closeConnection();
890
+ await this.closeConnection(err);
889
891
  throw err;
890
892
  } finally {
891
893
  this.initiating = false;
@@ -896,7 +898,6 @@ var Peer = class {
896
898
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
897
899
  */
898
900
  _createConnection(initiator, sessionId) {
899
- var _a;
900
901
  (0, import_log3.log)("creating connection", {
901
902
  topic: this.topic,
902
903
  peerId: this.localPeerId,
@@ -905,13 +906,13 @@ var Peer = class {
905
906
  sessionId
906
907
  }, {
907
908
  F: __dxlog_file3,
908
- L: 196,
909
+ L: 198,
909
910
  S: this,
910
911
  C: (f, a) => f(...a)
911
912
  });
912
913
  (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
913
914
  F: __dxlog_file3,
914
- L: 203,
915
+ L: 205,
915
916
  S: this,
916
917
  A: [
917
918
  "!this.connection",
@@ -935,7 +936,7 @@ var Peer = class {
935
936
  this._transportFactory
936
937
  );
937
938
  this._callbacks.onInitiated(connection);
938
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
939
+ void this._connectionCtx?.dispose();
939
940
  this._connectionCtx = this._ctx.derive();
940
941
  connection.stateChanged.on((state) => {
941
942
  switch (state) {
@@ -952,7 +953,7 @@ var Peer = class {
952
953
  initiator
953
954
  }, {
954
955
  F: __dxlog_file3,
955
- L: 229,
956
+ L: 231,
956
957
  S: this,
957
958
  C: (f, a) => f(...a)
958
959
  });
@@ -966,13 +967,13 @@ var Peer = class {
966
967
  initiator
967
968
  }, {
968
969
  F: __dxlog_file3,
969
- L: 240,
970
+ L: 242,
970
971
  S: this,
971
972
  C: (f, a) => f(...a)
972
973
  });
973
974
  (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
974
975
  F: __dxlog_file3,
975
- L: 241,
976
+ L: 243,
976
977
  S: this,
977
978
  A: [
978
979
  "this.connection === connection",
@@ -987,7 +988,7 @@ var Peer = class {
987
988
  initiator
988
989
  }, {
989
990
  F: __dxlog_file3,
990
- L: 243,
991
+ L: 245,
991
992
  S: this,
992
993
  C: (f, a) => f(...a)
993
994
  });
@@ -1017,7 +1018,7 @@ var Peer = class {
1017
1018
  err
1018
1019
  }, {
1019
1020
  F: __dxlog_file3,
1020
- L: 277,
1021
+ L: 279,
1021
1022
  S: this,
1022
1023
  C: (f, a) => f(...a)
1023
1024
  });
@@ -1030,16 +1031,16 @@ var Peer = class {
1030
1031
  err
1031
1032
  }, {
1032
1033
  F: __dxlog_file3,
1033
- L: 278,
1034
+ L: 280,
1034
1035
  S: this,
1035
1036
  C: (f, a) => f(...a)
1036
1037
  });
1037
- void this.closeConnection();
1038
+ void this.closeConnection(err);
1038
1039
  });
1039
1040
  this.connection = connection;
1040
1041
  return connection;
1041
1042
  }
1042
- async closeConnection() {
1043
+ async closeConnection(err) {
1043
1044
  if (!this.connection) {
1044
1045
  return;
1045
1046
  }
@@ -1049,17 +1050,17 @@ var Peer = class {
1049
1050
  sessionId: connection.sessionId
1050
1051
  }, {
1051
1052
  F: __dxlog_file3,
1052
- L: 301,
1053
+ L: 303,
1053
1054
  S: this,
1054
1055
  C: (f, a) => f(...a)
1055
1056
  });
1056
- await connection.close();
1057
+ await connection.close(err);
1057
1058
  (0, import_log3.log)("closed", {
1058
1059
  peerId: this.id,
1059
1060
  sessionId: connection.sessionId
1060
1061
  }, {
1061
1062
  F: __dxlog_file3,
1062
- L: 307,
1063
+ L: 309,
1063
1064
  S: this,
1064
1065
  C: (f, a) => f(...a)
1065
1066
  });
@@ -1070,7 +1071,7 @@ var Peer = class {
1070
1071
  message
1071
1072
  }, {
1072
1073
  F: __dxlog_file3,
1073
- L: 312,
1074
+ L: 314,
1074
1075
  S: this,
1075
1076
  C: (f, a) => f(...a)
1076
1077
  });
@@ -1079,18 +1080,17 @@ var Peer = class {
1079
1080
  await this.connection.signal(message);
1080
1081
  }
1081
1082
  async destroy() {
1082
- var _a;
1083
1083
  await this._ctx.dispose();
1084
1084
  (0, import_log3.log)("Destroying peer", {
1085
1085
  peerId: this.id,
1086
1086
  topic: this.topic
1087
1087
  }, {
1088
1088
  F: __dxlog_file3,
1089
- L: 322,
1089
+ L: 324,
1090
1090
  S: this,
1091
1091
  C: (f, a) => f(...a)
1092
1092
  });
1093
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1093
+ await this?.connection?.close();
1094
1094
  }
1095
1095
  };
1096
1096
  _ts_decorate2([
@@ -1135,9 +1135,9 @@ var Swarm = class {
1135
1135
  this._transportFactory = _transportFactory;
1136
1136
  this._label = _label;
1137
1137
  this._connectionLimiter = _connectionLimiter;
1138
- this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1139
1138
  this._ctx = new import_context4.Context();
1140
1139
  this._listeningHandle = void 0;
1140
+ this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
1141
1141
  this._instanceId = import_keys4.PublicKey.random().toHex();
1142
1142
  this.connectionAdded = new import_async3.Event();
1143
1143
  this.disconnected = new import_async3.Event();
@@ -1220,14 +1220,13 @@ var Swarm = class {
1220
1220
  });
1221
1221
  }
1222
1222
  async destroy() {
1223
- var _a;
1224
1223
  (0, import_log4.log)("destroying...", void 0, {
1225
1224
  F: __dxlog_file4,
1226
1225
  L: 140,
1227
1226
  S: this,
1228
1227
  C: (f, a) => f(...a)
1229
1228
  });
1230
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1229
+ await this._listeningHandle?.unsubscribe();
1231
1230
  this._listeningHandle = void 0;
1232
1231
  await this._ctx.dispose();
1233
1232
  await this._topology.destroy();
@@ -1267,7 +1266,6 @@ var Swarm = class {
1267
1266
  this._topology.update();
1268
1267
  }
1269
1268
  onSwarmEvent(swarmEvent) {
1270
- var _a;
1271
1269
  (0, import_log4.log)("swarm event", {
1272
1270
  swarmEvent
1273
1271
  }, {
@@ -1303,7 +1301,7 @@ var Swarm = class {
1303
1301
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1304
1302
  if (peer) {
1305
1303
  peer.advertizing = false;
1306
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1304
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1307
1305
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1308
1306
  F: __dxlog_file4,
1309
1307
  L: 188,
@@ -1316,7 +1314,6 @@ var Swarm = class {
1316
1314
  this._topology.update();
1317
1315
  }
1318
1316
  async onOffer(message) {
1319
- var _a, _b;
1320
1317
  (0, import_log4.log)("offer", {
1321
1318
  message
1322
1319
  }, {
@@ -1345,7 +1342,7 @@ var Swarm = class {
1345
1342
  ""
1346
1343
  ]
1347
1344
  });
1348
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1345
+ if (!message.recipient?.equals(this._ownPeerId)) {
1349
1346
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1350
1347
  message
1351
1348
  }, {
@@ -1358,7 +1355,7 @@ var Swarm = class {
1358
1355
  accept: false
1359
1356
  };
1360
1357
  }
1361
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1358
+ if (!message.topic?.equals(this._topic)) {
1362
1359
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1363
1360
  message
1364
1361
  }, {
@@ -1377,7 +1374,6 @@ var Swarm = class {
1377
1374
  return answer;
1378
1375
  }
1379
1376
  async onSignal(message) {
1380
- var _a, _b;
1381
1377
  (0, import_log4.log)("signal", {
1382
1378
  message
1383
1379
  }, {
@@ -1395,7 +1391,7 @@ var Swarm = class {
1395
1391
  });
1396
1392
  return;
1397
1393
  }
1398
- (0, import_invariant4.invariant)((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1394
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1399
1395
  F: __dxlog_file4,
1400
1396
  L: 227,
1401
1397
  S: this,
@@ -1404,7 +1400,7 @@ var Swarm = class {
1404
1400
  "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1405
1401
  ]
1406
1402
  });
1407
- (0, import_invariant4.invariant)((_b = message.topic) == null ? void 0 : _b.equals(this._topic), void 0, {
1403
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1408
1404
  F: __dxlog_file4,
1409
1405
  L: 231,
1410
1406
  S: this,
@@ -1616,8 +1612,7 @@ var SwarmMapper = class {
1616
1612
  }));
1617
1613
  }));
1618
1614
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1619
- var _a;
1620
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1615
+ this._connectionSubscriptions.get(peerId)?.();
1621
1616
  this._connectionSubscriptions.delete(peerId);
1622
1617
  this._update();
1623
1618
  }));
@@ -1672,7 +1667,7 @@ var import_keys6 = require("@dxos/keys");
1672
1667
  var import_log6 = require("@dxos/log");
1673
1668
  var import_util4 = require("@dxos/util");
1674
1669
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1675
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1670
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
1676
1671
  var ConnectionLimiter = class {
1677
1672
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1678
1673
  this._ctx = new import_context5.Context();
@@ -1763,8 +1758,7 @@ var ConnectionLog = class {
1763
1758
  this.update = new import_async6.Event();
1764
1759
  }
1765
1760
  getSwarmInfo(swarmId) {
1766
- var _a;
1767
- return (_a = this._swarms.get(swarmId)) != null ? _a : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1761
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1768
1762
  }
1769
1763
  get swarms() {
1770
1764
  return Array.from(this._swarms.values());
@@ -1780,9 +1774,9 @@ var ConnectionLog = class {
1780
1774
  this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1781
1775
  this.update.emit();
1782
1776
  swarm.connectionAdded.on((connection) => {
1783
- var _a, _b;
1784
1777
  const connectionInfo = {
1785
1778
  state: ConnectionState.INITIAL,
1779
+ closeReason: connection.closeReason,
1786
1780
  remotePeerId: connection.remoteId,
1787
1781
  sessionId: connection.sessionId,
1788
1782
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1793,13 +1787,14 @@ var ConnectionLog = class {
1793
1787
  this.update.emit();
1794
1788
  connection.stateChanged.on((state) => {
1795
1789
  connectionInfo.state = state;
1790
+ connectionInfo.closeReason = connection.closeReason;
1796
1791
  connectionInfo.events.push({
1797
1792
  type: EventType.CONNECTION_STATE_CHANGED,
1798
1793
  newState: state
1799
1794
  });
1800
1795
  this.update.emit();
1801
1796
  });
1802
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1797
+ connection.protocol?.stats?.on((stats) => {
1803
1798
  connectionInfo.streams = stats.channels;
1804
1799
  this.update.emit();
1805
1800
  });
@@ -1834,10 +1829,7 @@ var NetworkManager = class {
1834
1829
  this._instanceId = import_keys8.PublicKey.random().toHex();
1835
1830
  this._transportFactory = transportFactory;
1836
1831
  this._signalManager = signalManager;
1837
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1838
- var _a;
1839
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1840
- });
1832
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1841
1833
  this._messenger = new import_messaging.Messenger({
1842
1834
  signalManager: this._signalManager
1843
1835
  });
@@ -1905,7 +1897,6 @@ var NetworkManager = class {
1905
1897
  * Join the swarm.
1906
1898
  */
1907
1899
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1908
- var _a;
1909
1900
  (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1910
1901
  F: __dxlog_file7,
1911
1902
  L: 157,
@@ -1979,7 +1970,7 @@ var NetworkManager = class {
1979
1970
  C: (f, a) => f(...a)
1980
1971
  }));
1981
1972
  this.topicsUpdated.emit();
1982
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1973
+ this._connectionLog?.joinedSwarm(swarm);
1983
1974
  (0, import_log7.log)("joined", {
1984
1975
  topic: import_keys8.PublicKey.from(topic),
1985
1976
  count: this._swarms.size
@@ -1997,7 +1988,6 @@ var NetworkManager = class {
1997
1988
  * Close the connection.
1998
1989
  */
1999
1990
  async leaveSwarm(topic) {
2000
- var _a;
2001
1991
  if (!this._swarms.has(topic)) {
2002
1992
  return;
2003
1993
  }
@@ -2017,7 +2007,7 @@ var NetworkManager = class {
2017
2007
  const map = this._mappers.get(topic);
2018
2008
  map.destroy();
2019
2009
  this._mappers.delete(topic);
2020
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
2010
+ this._connectionLog?.leftSwarm(swarm);
2021
2011
  await swarm.destroy();
2022
2012
  this._swarms.delete(topic);
2023
2013
  await this.topicsUpdated.emit();
@@ -2338,9 +2328,12 @@ var createStreamDelay = (delay) => {
2338
2328
  var MemoryTransportFactory = {
2339
2329
  createTransport: (params) => new MemoryTransport(params)
2340
2330
  };
2341
- var _MemoryTransport = class {
2331
+ var MemoryTransport = class _MemoryTransport {
2332
+ static {
2333
+ // TODO(burdon): Remove static properties (inject context into constructor).
2334
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2335
+ }
2342
2336
  constructor(options) {
2343
- var _a;
2344
2337
  this.options = options;
2345
2338
  this.closed = new import_async8.Event();
2346
2339
  this.connected = new import_async8.Event();
@@ -2386,7 +2379,7 @@ var _MemoryTransport = class {
2386
2379
  });
2387
2380
  } else {
2388
2381
  this._remote.wait({
2389
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2382
+ timeout: this.options.timeout ?? 1e3
2390
2383
  }).then((remoteId) => {
2391
2384
  if (this._destroyed) {
2392
2385
  return;
@@ -2473,7 +2466,7 @@ var _MemoryTransport = class {
2473
2466
  S: this,
2474
2467
  C: (f, a) => f(...a)
2475
2468
  });
2476
- if (!(payload == null ? void 0 : payload.transportId)) {
2469
+ if (!payload?.transportId) {
2477
2470
  return;
2478
2471
  }
2479
2472
  const transportId = payload.transportId;
@@ -2483,9 +2476,6 @@ var _MemoryTransport = class {
2483
2476
  }
2484
2477
  }
2485
2478
  };
2486
- var MemoryTransport = _MemoryTransport;
2487
- // TODO(burdon): Remove static properties (inject context into constructor).
2488
- MemoryTransport._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2489
2479
  _ts_decorate4([
2490
2480
  import_log10.logInfo
2491
2481
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2506,43 +2496,47 @@ var import_protocols5 = require("@dxos/protocols");
2506
2496
  var wrtc = null;
2507
2497
  try {
2508
2498
  wrtc = require("@koush/wrtc");
2509
- } catch (e) {
2499
+ } catch {
2510
2500
  }
2511
2501
 
2512
2502
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2513
2503
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2514
2504
  var WebRTCTransport = class {
2515
2505
  constructor(params) {
2516
- var _a;
2517
2506
  this.params = params;
2507
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2518
2508
  this._closed = false;
2519
2509
  this.closed = new import_async9.Event();
2520
2510
  this.connected = new import_async9.Event();
2521
2511
  this.errors = new import_debug5.ErrorStream();
2522
- this._instanceId = import_keys10.PublicKey.random().toHex();
2523
2512
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2524
2513
  id: this._instanceId
2525
2514
  }), {
2526
2515
  F: __dxlog_file12,
2527
- L: 39,
2516
+ L: 44,
2528
2517
  S: this,
2529
2518
  C: (f, a) => f(...a)
2530
2519
  });
2531
2520
  (0, import_log11.log)("created connection", params, {
2532
2521
  F: __dxlog_file12,
2533
- L: 40,
2522
+ L: 45,
2534
2523
  S: this,
2535
2524
  C: (f, a) => f(...a)
2536
2525
  });
2537
2526
  this._peer = new import_simple_peer.default({
2538
2527
  initiator: this.params.initiator,
2539
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : (0, import_debug5.raise)(new Error("wrtc not available")),
2540
- config: this.params.webrtcConfig
2528
+ config: this.params.webrtcConfig,
2529
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2530
+ channelConfig: {
2531
+ protocol: "dxos.mesh"
2532
+ },
2533
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2534
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available"))
2541
2535
  });
2542
2536
  this._peer.on("signal", async (data) => {
2543
2537
  (0, import_log11.log)("signal", data, {
2544
2538
  F: __dxlog_file12,
2545
- L: 48,
2539
+ L: 62,
2546
2540
  S: this,
2547
2541
  C: (f, a) => f(...a)
2548
2542
  });
@@ -2555,7 +2549,7 @@ var WebRTCTransport = class {
2555
2549
  this._peer.on("connect", () => {
2556
2550
  (0, import_log11.log)("connected", void 0, {
2557
2551
  F: __dxlog_file12,
2558
- L: 53,
2552
+ L: 67,
2559
2553
  S: this,
2560
2554
  C: (f, a) => f(...a)
2561
2555
  });
@@ -2565,7 +2559,7 @@ var WebRTCTransport = class {
2565
2559
  this._peer.on("close", async () => {
2566
2560
  (0, import_log11.log)("closed", void 0, {
2567
2561
  F: __dxlog_file12,
2568
- L: 59,
2562
+ L: 73,
2569
2563
  S: this,
2570
2564
  C: (f, a) => f(...a)
2571
2565
  });
@@ -2573,23 +2567,22 @@ var WebRTCTransport = class {
2573
2567
  this.closed.emit();
2574
2568
  });
2575
2569
  this._peer.on("error", async (err) => {
2576
- var _a2;
2577
2570
  this.errors.raise(err);
2578
2571
  try {
2579
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2572
+ if (typeof this._peer?._pc.getStats === "function") {
2580
2573
  this._peer._pc.getStats().then((stats) => {
2581
2574
  import_log11.log.warn("report after webrtc error", {
2582
2575
  config: this.params.webrtcConfig,
2583
2576
  stats
2584
2577
  }, {
2585
2578
  F: __dxlog_file12,
2586
- L: 71,
2579
+ L: 85,
2587
2580
  S: this,
2588
2581
  C: (f, a) => f(...a)
2589
2582
  });
2590
2583
  });
2591
2584
  }
2592
- } catch (e) {
2585
+ } catch {
2593
2586
  }
2594
2587
  await this.destroy();
2595
2588
  });
@@ -2597,7 +2590,7 @@ var WebRTCTransport = class {
2597
2590
  id: this._instanceId
2598
2591
  }), {
2599
2592
  F: __dxlog_file12,
2600
- L: 81,
2593
+ L: 95,
2601
2594
  S: this,
2602
2595
  C: (f, a) => f(...a)
2603
2596
  });
@@ -2605,7 +2598,7 @@ var WebRTCTransport = class {
2605
2598
  async destroy() {
2606
2599
  (0, import_log11.log)("closing...", void 0, {
2607
2600
  F: __dxlog_file12,
2608
- L: 85,
2601
+ L: 99,
2609
2602
  S: this,
2610
2603
  C: (f, a) => f(...a)
2611
2604
  });
@@ -2615,7 +2608,7 @@ var WebRTCTransport = class {
2615
2608
  this.closed.emit();
2616
2609
  (0, import_log11.log)("closed", void 0, {
2617
2610
  F: __dxlog_file12,
2618
- L: 90,
2611
+ L: 104,
2619
2612
  S: this,
2620
2613
  C: (f, a) => f(...a)
2621
2614
  });
@@ -2626,7 +2619,7 @@ var WebRTCTransport = class {
2626
2619
  }
2627
2620
  (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2628
2621
  F: __dxlog_file12,
2629
- L: 98,
2622
+ L: 112,
2630
2623
  S: this,
2631
2624
  A: [
2632
2625
  "signal.payload.data",
@@ -2636,8 +2629,7 @@ var WebRTCTransport = class {
2636
2629
  this._peer.signal(signal.payload.data);
2637
2630
  }
2638
2631
  async _disconnectStreams() {
2639
- var _a, _b, _c, _d;
2640
- (_d = (_c = (_b = (_a = this.params.stream).unpipe) == null ? void 0 : _b.call(_a, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2632
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2641
2633
  }
2642
2634
  };
2643
2635
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2765,9 +2757,8 @@ var WebRTCTransportService = class {
2765
2757
  }
2766
2758
  }
2767
2759
  async close({ proxyId }) {
2768
- var _a, _b;
2769
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2770
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2760
+ await this.transports.get(proxyId)?.transport.destroy();
2761
+ await this.transports.get(proxyId)?.stream.end();
2771
2762
  this.transports.delete(proxyId);
2772
2763
  (0, import_log12.log)("Closed.", void 0, {
2773
2764
  F: __dxlog_file13,
@@ -2796,9 +2787,9 @@ var WebRTCTransportProxy = class {
2796
2787
  this._proxyId = import_keys12.PublicKey.random();
2797
2788
  this._ctx = new import_context6.Context();
2798
2789
  this.closed = new import_async10.Event();
2799
- this._closed = false;
2800
2790
  this.connected = new import_async10.Event();
2801
2791
  this.errors = new import_debug6.ErrorStream();
2792
+ this._closed = false;
2802
2793
  this._serviceStream = this._params.bridgeService.open({
2803
2794
  proxyId: this._proxyId,
2804
2795
  initiator: this._params.initiator
@@ -2874,7 +2865,7 @@ var WebRTCTransportProxy = class {
2874
2865
  if (this._closed) {
2875
2866
  return;
2876
2867
  }
2877
- this._serviceStream.close();
2868
+ await this._serviceStream.close();
2878
2869
  try {
2879
2870
  await this._params.bridgeService.close({
2880
2871
  proxyId: this._proxyId
@@ -2895,7 +2886,7 @@ var WebRTCTransportProxy = class {
2895
2886
  */
2896
2887
  // TODO(burdon): Option on close method.
2897
2888
  forceClose() {
2898
- this._serviceStream.close();
2889
+ void this._serviceStream.close();
2899
2890
  this.closed.emit();
2900
2891
  this._closed = true;
2901
2892
  }