@dxos/network-manager 0.1.56-main.bf228a7 → 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-FYPOLWVS.mjs → chunk-WQ3YXGWP.mjs} +188 -136
  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 +186 -134
  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 +208 -149
  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 +28 -4
  35. package/src/swarm/peer.ts +31 -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-FYPOLWVS.mjs.map +0 -7
@@ -4,7 +4,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
4
4
  }) : x)(function(x) {
5
5
  if (typeof require !== "undefined")
6
6
  return require.apply(this, arguments);
7
- throw new Error('Dynamic require of "' + x + '" is not supported');
7
+ throw Error('Dynamic require of "' + x + '" is not supported');
8
8
  });
9
9
 
10
10
  // packages/core/mesh/network-manager/src/swarm/connection.ts
@@ -84,6 +84,18 @@ var Connection = class {
84
84
  await this._flushSignalBuffer();
85
85
  });
86
86
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
87
+ log.trace("dxos.mesh.connection.construct", {
88
+ sessionId: this.sessionId,
89
+ topic: this.topic,
90
+ localPeerId: this.ownId,
91
+ remotePeerId: this.remoteId,
92
+ initiator: this.initiator
93
+ }, {
94
+ F: __dxlog_file,
95
+ L: 96,
96
+ S: this,
97
+ C: (f, a) => f(...a)
98
+ });
87
99
  }
88
100
  get state() {
89
101
  return this._state;
@@ -100,7 +112,7 @@ var Connection = class {
100
112
  async openConnection() {
101
113
  invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
102
114
  F: __dxlog_file,
103
- L: 112,
115
+ L: 121,
104
116
  S: this,
105
117
  A: [
106
118
  "this._state === ConnectionState.INITIAL",
@@ -111,7 +123,19 @@ var Connection = class {
111
123
  id: this._instanceId
112
124
  }), {
113
125
  F: __dxlog_file,
114
- L: 113,
126
+ L: 122,
127
+ S: this,
128
+ C: (f, a) => f(...a)
129
+ });
130
+ log.trace("dxos.mesh.connection.open", {
131
+ sessionId: this.sessionId,
132
+ topic: this.topic,
133
+ localPeerId: this.ownId,
134
+ remotePeerId: this.remoteId,
135
+ initiator: this.initiator
136
+ }, {
137
+ F: __dxlog_file,
138
+ L: 123,
115
139
  S: this,
116
140
  C: (f, a) => f(...a)
117
141
  });
@@ -122,7 +146,7 @@ var Connection = class {
122
146
  this._protocol.stream.on("close", () => {
123
147
  log("protocol stream closed", void 0, {
124
148
  F: __dxlog_file,
125
- L: 124,
149
+ L: 140,
126
150
  S: this,
127
151
  C: (f, a) => f(...a)
128
152
  });
@@ -130,7 +154,7 @@ var Connection = class {
130
154
  });
131
155
  invariant(!this._transport, void 0, {
132
156
  F: __dxlog_file,
133
- L: 128,
157
+ L: 144,
134
158
  S: this,
135
159
  A: [
136
160
  "!this._transport",
@@ -150,6 +174,9 @@ var Connection = class {
150
174
  this.close().catch((err) => this.errors.raise(err));
151
175
  });
152
176
  this._transport.errors.handle((err) => {
177
+ if (!this.closeReason) {
178
+ this.closeReason = err?.message;
179
+ }
153
180
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
154
181
  this.errors.raise(err);
155
182
  }
@@ -162,13 +189,15 @@ var Connection = class {
162
189
  id: this._instanceId
163
190
  }), {
164
191
  F: __dxlog_file,
165
- L: 157,
192
+ L: 176,
166
193
  S: this,
167
194
  C: (f, a) => f(...a)
168
195
  });
169
196
  }
170
- async close() {
171
- var _a;
197
+ async close(err) {
198
+ if (!this.closeReason) {
199
+ this.closeReason = err?.message;
200
+ }
172
201
  if (this._state === ConnectionState.CLOSED) {
173
202
  return;
174
203
  }
@@ -178,26 +207,26 @@ var Connection = class {
178
207
  peerId: this.ownId
179
208
  }, {
180
209
  F: __dxlog_file,
181
- L: 169,
210
+ L: 191,
182
211
  S: this,
183
212
  C: (f, a) => f(...a)
184
213
  });
185
214
  try {
186
215
  await this._protocol.destroy();
187
- } catch (err) {
188
- log.catch(err, void 0, {
216
+ } catch (err2) {
217
+ log.catch(err2, void 0, {
189
218
  F: __dxlog_file,
190
- L: 175,
219
+ L: 197,
191
220
  S: this,
192
221
  C: (f, a) => f(...a)
193
222
  });
194
223
  }
195
224
  try {
196
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
197
- } catch (err) {
198
- log.catch(err, void 0, {
225
+ await this._transport?.destroy();
226
+ } catch (err2) {
227
+ log.catch(err2, void 0, {
199
228
  F: __dxlog_file,
200
- L: 182,
229
+ L: 204,
201
230
  S: this,
202
231
  C: (f, a) => f(...a)
203
232
  });
@@ -206,7 +235,7 @@ var Connection = class {
206
235
  peerId: this.ownId
207
236
  }, {
208
237
  F: __dxlog_file,
209
- L: 185,
238
+ L: 207,
210
239
  S: this,
211
240
  C: (f, a) => f(...a)
212
241
  });
@@ -221,8 +250,10 @@ var Connection = class {
221
250
  return;
222
251
  }
223
252
  try {
224
- await cancelWithContext(this._ctx, sleep(this._signallingDelay));
225
- this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
253
+ if (true) {
254
+ await cancelWithContext(this._ctx, sleep(this._signallingDelay));
255
+ this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
256
+ }
226
257
  const signals = [
227
258
  ...this._outgoingSignalBuffer
228
259
  ];
@@ -246,7 +277,7 @@ var Connection = class {
246
277
  err
247
278
  }, {
248
279
  F: __dxlog_file,
249
- L: 219,
280
+ L: 243,
250
281
  S: this,
251
282
  C: (f, a) => f(...a)
252
283
  });
@@ -257,10 +288,9 @@ var Connection = class {
257
288
  * Receive a signal from the remote peer.
258
289
  */
259
290
  async signal(msg) {
260
- var _a, _b, _c;
261
291
  invariant(msg.sessionId, void 0, {
262
292
  F: __dxlog_file,
263
- L: 228,
293
+ L: 252,
264
294
  S: this,
265
295
  A: [
266
296
  "msg.sessionId",
@@ -270,7 +300,7 @@ var Connection = class {
270
300
  if (!msg.sessionId.equals(this.sessionId)) {
271
301
  log("dropping signal for incorrect session id", void 0, {
272
302
  F: __dxlog_file,
273
- L: 230,
303
+ L: 254,
274
304
  S: this,
275
305
  C: (f, a) => f(...a)
276
306
  });
@@ -278,32 +308,32 @@ var Connection = class {
278
308
  }
279
309
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
280
310
  F: __dxlog_file,
281
- L: 233,
311
+ L: 257,
282
312
  S: this,
283
313
  A: [
284
314
  "msg.data.signal || msg.data.signalBatch",
285
315
  ""
286
316
  ]
287
317
  });
288
- invariant((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId), void 0, {
318
+ invariant(msg.author?.equals(this.remoteId), void 0, {
289
319
  F: __dxlog_file,
290
- L: 234,
320
+ L: 258,
291
321
  S: this,
292
322
  A: [
293
323
  "msg.author?.equals(this.remoteId)",
294
324
  ""
295
325
  ]
296
326
  });
297
- invariant((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId), void 0, {
327
+ invariant(msg.recipient?.equals(this.ownId), void 0, {
298
328
  F: __dxlog_file,
299
- L: 235,
329
+ L: 259,
300
330
  S: this,
301
331
  A: [
302
332
  "msg.recipient?.equals(this.ownId)",
303
333
  ""
304
334
  ]
305
335
  });
306
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
336
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
307
337
  msg.data.signal
308
338
  ];
309
339
  for (const signal of signals) {
@@ -317,7 +347,7 @@ var Connection = class {
317
347
  msg: msg.data
318
348
  }, {
319
349
  F: __dxlog_file,
320
- L: 244,
350
+ L: 268,
321
351
  S: this,
322
352
  C: (f, a) => f(...a)
323
353
  });
@@ -325,7 +355,7 @@ var Connection = class {
325
355
  } else {
326
356
  invariant(this._transport, "Connection not ready to accept signals.", {
327
357
  F: __dxlog_file,
328
- L: 247,
358
+ L: 271,
329
359
  S: this,
330
360
  A: [
331
361
  "this._transport",
@@ -338,7 +368,7 @@ var Connection = class {
338
368
  msg: msg.data
339
369
  }, {
340
370
  F: __dxlog_file,
341
- L: 248,
371
+ L: 272,
342
372
  S: this,
343
373
  C: (f, a) => f(...a)
344
374
  });
@@ -353,13 +383,13 @@ var Connection = class {
353
383
  peerId: this.ownId
354
384
  }, {
355
385
  F: __dxlog_file,
356
- L: 255,
386
+ L: 279,
357
387
  S: this,
358
388
  C: (f, a) => f(...a)
359
389
  });
360
390
  invariant(state !== this._state, "Already in this state.", {
361
391
  F: __dxlog_file,
362
- L: 256,
392
+ L: 280,
363
393
  S: this,
364
394
  A: [
365
395
  "state !== this._state",
@@ -393,7 +423,6 @@ var SwarmMessenger = class {
393
423
  this._topic = topic;
394
424
  }
395
425
  async receiveMessage({ author, recipient, payload }) {
396
- var _a, _b, _c, _d;
397
426
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
398
427
  return;
399
428
  }
@@ -411,21 +440,21 @@ var SwarmMessenger = class {
411
440
  S: this,
412
441
  C: (f, a) => f(...a)
413
442
  });
414
- if ((_a = message.data) == null ? void 0 : _a.offer) {
443
+ if (message.data?.offer) {
415
444
  await this._handleOffer({
416
445
  author,
417
446
  recipient,
418
447
  message
419
448
  });
420
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
449
+ } else if (message.data?.answer) {
421
450
  await this._resolveAnswers(message);
422
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
451
+ } else if (message.data?.signal) {
423
452
  await this._handleSignal({
424
453
  author,
425
454
  recipient,
426
455
  message
427
456
  });
428
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
457
+ } else if (message.data?.signalBatch) {
429
458
  await this._handleSignal({
430
459
  author,
431
460
  recipient,
@@ -443,8 +472,7 @@ var SwarmMessenger = class {
443
472
  }
444
473
  }
445
474
  async signal(message) {
446
- var _a, _b;
447
- invariant2(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message", {
475
+ invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
448
476
  F: __dxlog_file2,
449
477
  L: 85,
450
478
  S: this,
@@ -476,11 +504,10 @@ var SwarmMessenger = class {
476
504
  });
477
505
  }
478
506
  async _sendReliableMessage({ author, recipient, message }) {
479
- var _a;
480
507
  const networkMessage = {
481
508
  ...message,
482
509
  // Setting unique message_id if it not specified yet.
483
- messageId: (_a = message.messageId) != null ? _a : PublicKey2.random()
510
+ messageId: message.messageId ?? PublicKey2.random()
484
511
  };
485
512
  log2("sending", {
486
513
  from: author,
@@ -502,8 +529,7 @@ var SwarmMessenger = class {
502
529
  });
503
530
  }
504
531
  async _resolveAnswers(message) {
505
- var _a, _b, _c;
506
- invariant2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId", {
532
+ invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
507
533
  F: __dxlog_file2,
508
534
  L: 135,
509
535
  S: this,
@@ -515,7 +541,7 @@ var SwarmMessenger = class {
515
541
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
516
542
  if (offerRecord) {
517
543
  this._offerRecords.delete(message.data.answer.offerMessageId);
518
- invariant2((_c = message.data) == null ? void 0 : _c.answer, "No answer", {
544
+ invariant2(message.data?.answer, "No answer", {
519
545
  F: __dxlog_file2,
520
546
  L: 139,
521
547
  S: this,
@@ -640,6 +666,7 @@ function _ts_decorate2(decorators, target, key, desc) {
640
666
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
641
667
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
642
668
  var Peer = class {
669
+ // TODO(burdon): Convert to map?
643
670
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
644
671
  this.id = id;
645
672
  this.topic = topic;
@@ -659,7 +686,6 @@ var Peer = class {
659
686
  * Respond to remote offer.
660
687
  */
661
688
  async onOffer(message) {
662
- var _a;
663
689
  const remoteId = message.author;
664
690
  if (this.connection || this.initiating) {
665
691
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -667,15 +693,15 @@ var Peer = class {
667
693
  localPeerId: this.id,
668
694
  topic: this.topic,
669
695
  remotePeerId: this.localPeerId,
670
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
696
+ sessionId: this.connection?.sessionId
671
697
  }, {
672
698
  F: __dxlog_file3,
673
- L: 106,
699
+ L: 108,
674
700
  S: this,
675
701
  C: (f, a) => f(...a)
676
702
  });
677
703
  if (this.connection) {
678
- await this.closeConnection();
704
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
679
705
  }
680
706
  } else {
681
707
  return {
@@ -687,7 +713,7 @@ var Peer = class {
687
713
  if (!this.connection) {
688
714
  invariant3(message.sessionId, void 0, {
689
715
  F: __dxlog_file3,
690
- L: 126,
716
+ L: 128,
691
717
  S: this,
692
718
  A: [
693
719
  "message.sessionId",
@@ -707,12 +733,12 @@ var Peer = class {
707
733
  err
708
734
  }, {
709
735
  F: __dxlog_file3,
710
- L: 134,
736
+ L: 136,
711
737
  S: this,
712
738
  C: (f, a) => f(...a)
713
739
  });
714
740
  }
715
- await this.closeConnection();
741
+ await this.closeConnection(err);
716
742
  }
717
743
  return {
718
744
  accept: true
@@ -729,7 +755,7 @@ var Peer = class {
729
755
  async initiateConnection() {
730
756
  invariant3(!this.initiating, "Initiation in progress.", {
731
757
  F: __dxlog_file3,
732
- L: 151,
758
+ L: 153,
733
759
  S: this,
734
760
  A: [
735
761
  "!this.initiating",
@@ -738,7 +764,7 @@ var Peer = class {
738
764
  });
739
765
  invariant3(!this.connection, "Already connected.", {
740
766
  F: __dxlog_file3,
741
- L: 152,
767
+ L: 154,
742
768
  S: this,
743
769
  A: [
744
770
  "!this.connection",
@@ -753,7 +779,7 @@ var Peer = class {
753
779
  sessionId
754
780
  }, {
755
781
  F: __dxlog_file3,
756
- L: 154,
782
+ L: 156,
757
783
  S: this,
758
784
  C: (f, a) => f(...a)
759
785
  });
@@ -777,14 +803,14 @@ var Peer = class {
777
803
  remoteId: this.id
778
804
  }, {
779
805
  F: __dxlog_file3,
780
- L: 169,
806
+ L: 171,
781
807
  S: this,
782
808
  C: (f, a) => f(...a)
783
809
  });
784
810
  if (connection.state !== ConnectionState.INITIAL) {
785
811
  log3("ignoring response", void 0, {
786
812
  F: __dxlog_file3,
787
- L: 171,
813
+ L: 173,
788
814
  S: this,
789
815
  C: (f, a) => f(...a)
790
816
  });
@@ -804,11 +830,11 @@ var Peer = class {
804
830
  remoteId: this.id
805
831
  }, {
806
832
  F: __dxlog_file3,
807
- L: 182,
833
+ L: 184,
808
834
  S: this,
809
835
  C: (f, a) => f(...a)
810
836
  });
811
- await this.closeConnection();
837
+ await this.closeConnection(err);
812
838
  throw err;
813
839
  } finally {
814
840
  this.initiating = false;
@@ -819,7 +845,6 @@ var Peer = class {
819
845
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
820
846
  */
821
847
  _createConnection(initiator, sessionId) {
822
- var _a;
823
848
  log3("creating connection", {
824
849
  topic: this.topic,
825
850
  peerId: this.localPeerId,
@@ -828,13 +853,13 @@ var Peer = class {
828
853
  sessionId
829
854
  }, {
830
855
  F: __dxlog_file3,
831
- L: 196,
856
+ L: 198,
832
857
  S: this,
833
858
  C: (f, a) => f(...a)
834
859
  });
835
860
  invariant3(!this.connection, "Already connected.", {
836
861
  F: __dxlog_file3,
837
- L: 203,
862
+ L: 205,
838
863
  S: this,
839
864
  A: [
840
865
  "!this.connection",
@@ -858,7 +883,7 @@ var Peer = class {
858
883
  this._transportFactory
859
884
  );
860
885
  this._callbacks.onInitiated(connection);
861
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
886
+ void this._connectionCtx?.dispose();
862
887
  this._connectionCtx = this._ctx.derive();
863
888
  connection.stateChanged.on((state) => {
864
889
  switch (state) {
@@ -867,6 +892,18 @@ var Peer = class {
867
892
  this._lastConnectionTime = Date.now();
868
893
  this._callbacks.onConnected();
869
894
  this._connectionLimiter.doneConnecting(sessionId);
895
+ log3.trace("dxos.mesh.connection.connected", {
896
+ topic: this.topic,
897
+ localPeerId: this.localPeerId,
898
+ remotePeerId: this.id,
899
+ sessionId,
900
+ initiator
901
+ }, {
902
+ F: __dxlog_file3,
903
+ L: 231,
904
+ S: this,
905
+ C: (f, a) => f(...a)
906
+ });
870
907
  break;
871
908
  }
872
909
  case ConnectionState.CLOSED: {
@@ -877,19 +914,31 @@ var Peer = class {
877
914
  initiator
878
915
  }, {
879
916
  F: __dxlog_file3,
880
- L: 233,
917
+ L: 242,
881
918
  S: this,
882
919
  C: (f, a) => f(...a)
883
920
  });
884
921
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
885
922
  F: __dxlog_file3,
886
- L: 234,
923
+ L: 243,
887
924
  S: this,
888
925
  A: [
889
926
  "this.connection === connection",
890
927
  "'Connection mismatch (race condition).'"
891
928
  ]
892
929
  });
930
+ log3.trace("dxos.mesh.connection.closed", {
931
+ topic: this.topic,
932
+ localPeerId: this.localPeerId,
933
+ remotePeerId: this.id,
934
+ sessionId,
935
+ initiator
936
+ }, {
937
+ F: __dxlog_file3,
938
+ L: 245,
939
+ S: this,
940
+ C: (f, a) => f(...a)
941
+ });
893
942
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
894
943
  this._availableAfter = 0;
895
944
  } else {
@@ -916,16 +965,29 @@ var Peer = class {
916
965
  err
917
966
  }, {
918
967
  F: __dxlog_file3,
919
- L: 262,
968
+ L: 279,
920
969
  S: this,
921
970
  C: (f, a) => f(...a)
922
971
  });
923
- void this.closeConnection();
972
+ log3.trace("dxos.mesh.connection.error", {
973
+ topic: this.topic,
974
+ localPeerId: this.localPeerId,
975
+ remotePeerId: this.id,
976
+ sessionId,
977
+ initiator,
978
+ err
979
+ }, {
980
+ F: __dxlog_file3,
981
+ L: 280,
982
+ S: this,
983
+ C: (f, a) => f(...a)
984
+ });
985
+ void this.closeConnection(err);
924
986
  });
925
987
  this.connection = connection;
926
988
  return connection;
927
989
  }
928
- async closeConnection() {
990
+ async closeConnection(err) {
929
991
  if (!this.connection) {
930
992
  return;
931
993
  }
@@ -935,17 +997,17 @@ var Peer = class {
935
997
  sessionId: connection.sessionId
936
998
  }, {
937
999
  F: __dxlog_file3,
938
- L: 278,
1000
+ L: 303,
939
1001
  S: this,
940
1002
  C: (f, a) => f(...a)
941
1003
  });
942
- await connection.close();
1004
+ await connection.close(err);
943
1005
  log3("closed", {
944
1006
  peerId: this.id,
945
1007
  sessionId: connection.sessionId
946
1008
  }, {
947
1009
  F: __dxlog_file3,
948
- L: 284,
1010
+ L: 309,
949
1011
  S: this,
950
1012
  C: (f, a) => f(...a)
951
1013
  });
@@ -956,7 +1018,7 @@ var Peer = class {
956
1018
  message
957
1019
  }, {
958
1020
  F: __dxlog_file3,
959
- L: 289,
1021
+ L: 314,
960
1022
  S: this,
961
1023
  C: (f, a) => f(...a)
962
1024
  });
@@ -965,18 +1027,17 @@ var Peer = class {
965
1027
  await this.connection.signal(message);
966
1028
  }
967
1029
  async destroy() {
968
- var _a;
969
1030
  await this._ctx.dispose();
970
1031
  log3("Destroying peer", {
971
1032
  peerId: this.id,
972
1033
  topic: this.topic
973
1034
  }, {
974
1035
  F: __dxlog_file3,
975
- L: 299,
1036
+ L: 324,
976
1037
  S: this,
977
1038
  C: (f, a) => f(...a)
978
1039
  });
979
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1040
+ await this?.connection?.close();
980
1041
  }
981
1042
  };
982
1043
  _ts_decorate2([
@@ -1021,9 +1082,9 @@ var Swarm = class {
1021
1082
  this._transportFactory = _transportFactory;
1022
1083
  this._label = _label;
1023
1084
  this._connectionLimiter = _connectionLimiter;
1024
- this._peers = new ComplexMap2(PublicKey4.hash);
1025
1085
  this._ctx = new Context4();
1026
1086
  this._listeningHandle = void 0;
1087
+ this._peers = new ComplexMap2(PublicKey4.hash);
1027
1088
  this._instanceId = PublicKey4.random().toHex();
1028
1089
  this.connectionAdded = new Event2();
1029
1090
  this.disconnected = new Event2();
@@ -1106,14 +1167,13 @@ var Swarm = class {
1106
1167
  });
1107
1168
  }
1108
1169
  async destroy() {
1109
- var _a;
1110
1170
  log4("destroying...", void 0, {
1111
1171
  F: __dxlog_file4,
1112
1172
  L: 140,
1113
1173
  S: this,
1114
1174
  C: (f, a) => f(...a)
1115
1175
  });
1116
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1176
+ await this._listeningHandle?.unsubscribe();
1117
1177
  this._listeningHandle = void 0;
1118
1178
  await this._ctx.dispose();
1119
1179
  await this._topology.destroy();
@@ -1153,7 +1213,6 @@ var Swarm = class {
1153
1213
  this._topology.update();
1154
1214
  }
1155
1215
  onSwarmEvent(swarmEvent) {
1156
- var _a;
1157
1216
  log4("swarm event", {
1158
1217
  swarmEvent
1159
1218
  }, {
@@ -1189,7 +1248,7 @@ var Swarm = class {
1189
1248
  const peer = this._peers.get(PublicKey4.from(swarmEvent.peerLeft.peer));
1190
1249
  if (peer) {
1191
1250
  peer.advertizing = false;
1192
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1251
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1193
1252
  void this._destroyPeer(peer.id).catch((err) => log4.catch(err, void 0, {
1194
1253
  F: __dxlog_file4,
1195
1254
  L: 188,
@@ -1202,7 +1261,6 @@ var Swarm = class {
1202
1261
  this._topology.update();
1203
1262
  }
1204
1263
  async onOffer(message) {
1205
- var _a, _b;
1206
1264
  log4("offer", {
1207
1265
  message
1208
1266
  }, {
@@ -1231,7 +1289,7 @@ var Swarm = class {
1231
1289
  ""
1232
1290
  ]
1233
1291
  });
1234
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1292
+ if (!message.recipient?.equals(this._ownPeerId)) {
1235
1293
  log4("rejecting offer with incorrect peerId", {
1236
1294
  message
1237
1295
  }, {
@@ -1244,7 +1302,7 @@ var Swarm = class {
1244
1302
  accept: false
1245
1303
  };
1246
1304
  }
1247
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1305
+ if (!message.topic?.equals(this._topic)) {
1248
1306
  log4("rejecting offer with incorrect topic", {
1249
1307
  message
1250
1308
  }, {
@@ -1263,7 +1321,6 @@ var Swarm = class {
1263
1321
  return answer;
1264
1322
  }
1265
1323
  async onSignal(message) {
1266
- var _a, _b;
1267
1324
  log4("signal", {
1268
1325
  message
1269
1326
  }, {
@@ -1281,7 +1338,7 @@ var Swarm = class {
1281
1338
  });
1282
1339
  return;
1283
1340
  }
1284
- invariant4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1341
+ invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1285
1342
  F: __dxlog_file4,
1286
1343
  L: 227,
1287
1344
  S: this,
@@ -1290,7 +1347,7 @@ var Swarm = class {
1290
1347
  "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1291
1348
  ]
1292
1349
  });
1293
- invariant4((_b = message.topic) == null ? void 0 : _b.equals(this._topic), void 0, {
1350
+ invariant4(message.topic?.equals(this._topic), void 0, {
1294
1351
  F: __dxlog_file4,
1295
1352
  L: 231,
1296
1353
  S: this,
@@ -1502,8 +1559,7 @@ var SwarmMapper = class {
1502
1559
  }));
1503
1560
  }));
1504
1561
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1505
- var _a;
1506
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1562
+ this._connectionSubscriptions.get(peerId)?.();
1507
1563
  this._connectionSubscriptions.delete(peerId);
1508
1564
  this._update();
1509
1565
  }));
@@ -1558,7 +1614,7 @@ import { PublicKey as PublicKey6 } from "@dxos/keys";
1558
1614
  import { log as log6 } from "@dxos/log";
1559
1615
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
1560
1616
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1561
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1617
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
1562
1618
  var ConnectionLimiter = class {
1563
1619
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1564
1620
  this._ctx = new Context5();
@@ -1653,8 +1709,7 @@ var ConnectionLog = class {
1653
1709
  this.update = new Event4();
1654
1710
  }
1655
1711
  getSwarmInfo(swarmId) {
1656
- var _a;
1657
- return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
1712
+ return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
1658
1713
  }
1659
1714
  get swarms() {
1660
1715
  return Array.from(this._swarms.values());
@@ -1670,9 +1725,9 @@ var ConnectionLog = class {
1670
1725
  this._swarms.set(PublicKey7.from(swarm._instanceId), info);
1671
1726
  this.update.emit();
1672
1727
  swarm.connectionAdded.on((connection) => {
1673
- var _a, _b;
1674
1728
  const connectionInfo = {
1675
1729
  state: ConnectionState.INITIAL,
1730
+ closeReason: connection.closeReason,
1676
1731
  remotePeerId: connection.remoteId,
1677
1732
  sessionId: connection.sessionId,
1678
1733
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1683,13 +1738,14 @@ var ConnectionLog = class {
1683
1738
  this.update.emit();
1684
1739
  connection.stateChanged.on((state) => {
1685
1740
  connectionInfo.state = state;
1741
+ connectionInfo.closeReason = connection.closeReason;
1686
1742
  connectionInfo.events.push({
1687
1743
  type: EventType.CONNECTION_STATE_CHANGED,
1688
1744
  newState: state
1689
1745
  });
1690
1746
  this.update.emit();
1691
1747
  });
1692
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1748
+ connection.protocol?.stats?.on((stats) => {
1693
1749
  connectionInfo.streams = stats.channels;
1694
1750
  this.update.emit();
1695
1751
  });
@@ -1724,10 +1780,7 @@ var NetworkManager = class {
1724
1780
  this._instanceId = PublicKey8.random().toHex();
1725
1781
  this._transportFactory = transportFactory;
1726
1782
  this._signalManager = signalManager;
1727
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1728
- var _a;
1729
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1730
- });
1783
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1731
1784
  this._messenger = new Messenger({
1732
1785
  signalManager: this._signalManager
1733
1786
  });
@@ -1795,7 +1848,6 @@ var NetworkManager = class {
1795
1848
  * Join the swarm.
1796
1849
  */
1797
1850
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1798
- var _a;
1799
1851
  invariant6(PublicKey8.isPublicKey(topic), void 0, {
1800
1852
  F: __dxlog_file7,
1801
1853
  L: 157,
@@ -1869,7 +1921,7 @@ var NetworkManager = class {
1869
1921
  C: (f, a) => f(...a)
1870
1922
  }));
1871
1923
  this.topicsUpdated.emit();
1872
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1924
+ this._connectionLog?.joinedSwarm(swarm);
1873
1925
  log7("joined", {
1874
1926
  topic: PublicKey8.from(topic),
1875
1927
  count: this._swarms.size
@@ -1887,7 +1939,6 @@ var NetworkManager = class {
1887
1939
  * Close the connection.
1888
1940
  */
1889
1941
  async leaveSwarm(topic) {
1890
- var _a;
1891
1942
  if (!this._swarms.has(topic)) {
1892
1943
  return;
1893
1944
  }
@@ -1907,7 +1958,7 @@ var NetworkManager = class {
1907
1958
  const map = this._mappers.get(topic);
1908
1959
  map.destroy();
1909
1960
  this._mappers.delete(topic);
1910
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1961
+ this._connectionLog?.leftSwarm(swarm);
1911
1962
  await swarm.destroy();
1912
1963
  this._swarms.delete(topic);
1913
1964
  await this.topicsUpdated.emit();
@@ -2228,9 +2279,12 @@ var createStreamDelay = (delay) => {
2228
2279
  var MemoryTransportFactory = {
2229
2280
  createTransport: (params) => new MemoryTransport(params)
2230
2281
  };
2231
- var _MemoryTransport = class {
2282
+ var MemoryTransport = class _MemoryTransport {
2283
+ static {
2284
+ // TODO(burdon): Remove static properties (inject context into constructor).
2285
+ this._connections = new ComplexMap7(PublicKey9.hash);
2286
+ }
2232
2287
  constructor(options) {
2233
- var _a;
2234
2288
  this.options = options;
2235
2289
  this.closed = new Event6();
2236
2290
  this.connected = new Event6();
@@ -2276,7 +2330,7 @@ var _MemoryTransport = class {
2276
2330
  });
2277
2331
  } else {
2278
2332
  this._remote.wait({
2279
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2333
+ timeout: this.options.timeout ?? 1e3
2280
2334
  }).then((remoteId) => {
2281
2335
  if (this._destroyed) {
2282
2336
  return;
@@ -2363,7 +2417,7 @@ var _MemoryTransport = class {
2363
2417
  S: this,
2364
2418
  C: (f, a) => f(...a)
2365
2419
  });
2366
- if (!(payload == null ? void 0 : payload.transportId)) {
2420
+ if (!payload?.transportId) {
2367
2421
  return;
2368
2422
  }
2369
2423
  const transportId = payload.transportId;
@@ -2373,9 +2427,6 @@ var _MemoryTransport = class {
2373
2427
  }
2374
2428
  }
2375
2429
  };
2376
- var MemoryTransport = _MemoryTransport;
2377
- // TODO(burdon): Remove static properties (inject context into constructor).
2378
- MemoryTransport._connections = new ComplexMap7(PublicKey9.hash);
2379
2430
  _ts_decorate4([
2380
2431
  logInfo2
2381
2432
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2396,43 +2447,47 @@ import { trace as trace4 } from "@dxos/protocols";
2396
2447
  var wrtc = null;
2397
2448
  try {
2398
2449
  wrtc = __require("@koush/wrtc");
2399
- } catch (e) {
2450
+ } catch {
2400
2451
  }
2401
2452
 
2402
2453
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2403
2454
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2404
2455
  var WebRTCTransport = class {
2405
2456
  constructor(params) {
2406
- var _a;
2407
2457
  this.params = params;
2458
+ this._instanceId = PublicKey10.random().toHex();
2408
2459
  this._closed = false;
2409
2460
  this.closed = new Event7();
2410
2461
  this.connected = new Event7();
2411
2462
  this.errors = new ErrorStream4();
2412
- this._instanceId = PublicKey10.random().toHex();
2413
2463
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.begin({
2414
2464
  id: this._instanceId
2415
2465
  }), {
2416
2466
  F: __dxlog_file12,
2417
- L: 39,
2467
+ L: 44,
2418
2468
  S: this,
2419
2469
  C: (f, a) => f(...a)
2420
2470
  });
2421
2471
  log11("created connection", params, {
2422
2472
  F: __dxlog_file12,
2423
- L: 40,
2473
+ L: 45,
2424
2474
  S: this,
2425
2475
  C: (f, a) => f(...a)
2426
2476
  });
2427
2477
  this._peer = new SimplePeerConstructor({
2428
2478
  initiator: this.params.initiator,
2429
- wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
2430
- config: this.params.webrtcConfig
2479
+ config: this.params.webrtcConfig,
2480
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2481
+ channelConfig: {
2482
+ protocol: "dxos.mesh"
2483
+ },
2484
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2485
+ wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available"))
2431
2486
  });
2432
2487
  this._peer.on("signal", async (data) => {
2433
2488
  log11("signal", data, {
2434
2489
  F: __dxlog_file12,
2435
- L: 48,
2490
+ L: 62,
2436
2491
  S: this,
2437
2492
  C: (f, a) => f(...a)
2438
2493
  });
@@ -2445,7 +2500,7 @@ var WebRTCTransport = class {
2445
2500
  this._peer.on("connect", () => {
2446
2501
  log11("connected", void 0, {
2447
2502
  F: __dxlog_file12,
2448
- L: 53,
2503
+ L: 67,
2449
2504
  S: this,
2450
2505
  C: (f, a) => f(...a)
2451
2506
  });
@@ -2455,7 +2510,7 @@ var WebRTCTransport = class {
2455
2510
  this._peer.on("close", async () => {
2456
2511
  log11("closed", void 0, {
2457
2512
  F: __dxlog_file12,
2458
- L: 59,
2513
+ L: 73,
2459
2514
  S: this,
2460
2515
  C: (f, a) => f(...a)
2461
2516
  });
@@ -2463,23 +2518,22 @@ var WebRTCTransport = class {
2463
2518
  this.closed.emit();
2464
2519
  });
2465
2520
  this._peer.on("error", async (err) => {
2466
- var _a2;
2467
2521
  this.errors.raise(err);
2468
2522
  try {
2469
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2523
+ if (typeof this._peer?._pc.getStats === "function") {
2470
2524
  this._peer._pc.getStats().then((stats) => {
2471
2525
  log11.warn("report after webrtc error", {
2472
2526
  config: this.params.webrtcConfig,
2473
2527
  stats
2474
2528
  }, {
2475
2529
  F: __dxlog_file12,
2476
- L: 71,
2530
+ L: 85,
2477
2531
  S: this,
2478
2532
  C: (f, a) => f(...a)
2479
2533
  });
2480
2534
  });
2481
2535
  }
2482
- } catch (e) {
2536
+ } catch {
2483
2537
  }
2484
2538
  await this.destroy();
2485
2539
  });
@@ -2487,7 +2541,7 @@ var WebRTCTransport = class {
2487
2541
  id: this._instanceId
2488
2542
  }), {
2489
2543
  F: __dxlog_file12,
2490
- L: 81,
2544
+ L: 95,
2491
2545
  S: this,
2492
2546
  C: (f, a) => f(...a)
2493
2547
  });
@@ -2495,7 +2549,7 @@ var WebRTCTransport = class {
2495
2549
  async destroy() {
2496
2550
  log11("closing...", void 0, {
2497
2551
  F: __dxlog_file12,
2498
- L: 85,
2552
+ L: 99,
2499
2553
  S: this,
2500
2554
  C: (f, a) => f(...a)
2501
2555
  });
@@ -2505,7 +2559,7 @@ var WebRTCTransport = class {
2505
2559
  this.closed.emit();
2506
2560
  log11("closed", void 0, {
2507
2561
  F: __dxlog_file12,
2508
- L: 90,
2562
+ L: 104,
2509
2563
  S: this,
2510
2564
  C: (f, a) => f(...a)
2511
2565
  });
@@ -2516,7 +2570,7 @@ var WebRTCTransport = class {
2516
2570
  }
2517
2571
  invariant11(signal.payload.data, "Signal message must contain signal data.", {
2518
2572
  F: __dxlog_file12,
2519
- L: 98,
2573
+ L: 112,
2520
2574
  S: this,
2521
2575
  A: [
2522
2576
  "signal.payload.data",
@@ -2526,8 +2580,7 @@ var WebRTCTransport = class {
2526
2580
  this._peer.signal(signal.payload.data);
2527
2581
  }
2528
2582
  async _disconnectStreams() {
2529
- var _a, _b, _c, _d;
2530
- (_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);
2583
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2531
2584
  }
2532
2585
  };
2533
2586
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2655,9 +2708,8 @@ var WebRTCTransportService = class {
2655
2708
  }
2656
2709
  }
2657
2710
  async close({ proxyId }) {
2658
- var _a, _b;
2659
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2660
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2711
+ await this.transports.get(proxyId)?.transport.destroy();
2712
+ await this.transports.get(proxyId)?.stream.end();
2661
2713
  this.transports.delete(proxyId);
2662
2714
  log12("Closed.", void 0, {
2663
2715
  F: __dxlog_file13,
@@ -2686,9 +2738,9 @@ var WebRTCTransportProxy = class {
2686
2738
  this._proxyId = PublicKey12.random();
2687
2739
  this._ctx = new Context6();
2688
2740
  this.closed = new Event8();
2689
- this._closed = false;
2690
2741
  this.connected = new Event8();
2691
2742
  this.errors = new ErrorStream5();
2743
+ this._closed = false;
2692
2744
  this._serviceStream = this._params.bridgeService.open({
2693
2745
  proxyId: this._proxyId,
2694
2746
  initiator: this._params.initiator
@@ -2764,7 +2816,7 @@ var WebRTCTransportProxy = class {
2764
2816
  if (this._closed) {
2765
2817
  return;
2766
2818
  }
2767
- this._serviceStream.close();
2819
+ await this._serviceStream.close();
2768
2820
  try {
2769
2821
  await this._params.bridgeService.close({
2770
2822
  proxyId: this._proxyId
@@ -2785,7 +2837,7 @@ var WebRTCTransportProxy = class {
2785
2837
  */
2786
2838
  // TODO(burdon): Option on close method.
2787
2839
  forceClose() {
2788
- this._serviceStream.close();
2840
+ void this._serviceStream.close();
2789
2841
  this.closed.emit();
2790
2842
  this._closed = true;
2791
2843
  }
@@ -2866,4 +2918,4 @@ export {
2866
2918
  WebRTCTransportProxyFactory,
2867
2919
  createTeleportProtocolFactory
2868
2920
  };
2869
- //# sourceMappingURL=chunk-FYPOLWVS.mjs.map
2921
+ //# sourceMappingURL=chunk-WQ3YXGWP.mjs.map