@dxos/network-manager 0.1.56-main.c787da3 → 0.1.56-main.cf6bb41

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 (53) hide show
  1. package/dist/lib/browser/{chunk-MIYEP22D.mjs → chunk-5TRIVLI6.mjs} +850 -346
  2. package/dist/lib/browser/chunk-5TRIVLI6.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +5 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +6 -9
  6. package/dist/lib/browser/testing/index.mjs.map +2 -2
  7. package/dist/lib/node/index.cjs +869 -363
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +803 -360
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/network-manager.d.ts +1 -1
  13. package/dist/types/src/network-manager.d.ts.map +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 +28 -0
  16. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -0
  17. package/dist/types/src/swarm/connection-limiter.test.d.ts +2 -0
  18. package/dist/types/src/swarm/connection-limiter.test.d.ts.map +1 -0
  19. package/dist/types/src/swarm/connection.d.ts +1 -1
  20. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  21. package/dist/types/src/swarm/connection.test.d.ts +2 -0
  22. package/dist/types/src/swarm/connection.test.d.ts.map +1 -0
  23. package/dist/types/src/swarm/index.d.ts +1 -0
  24. package/dist/types/src/swarm/index.d.ts.map +1 -1
  25. package/dist/types/src/swarm/peer.d.ts +3 -1
  26. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  27. package/dist/types/src/swarm/swarm.d.ts +3 -1
  28. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  29. package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -1
  30. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  31. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  32. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  33. package/dist/types/src/transport/webrtc-transport-service.d.ts.map +1 -1
  34. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  35. package/package.json +18 -18
  36. package/src/network-manager.ts +19 -6
  37. package/src/signal/swarm-messenger.ts +1 -2
  38. package/src/swarm/connection-limiter.test.ts +44 -0
  39. package/src/swarm/connection-limiter.ts +72 -0
  40. package/src/swarm/connection.test.ts +72 -0
  41. package/src/swarm/connection.ts +18 -5
  42. package/src/swarm/index.ts +1 -0
  43. package/src/swarm/peer.ts +46 -9
  44. package/src/swarm/swarm.test.ts +140 -90
  45. package/src/swarm/swarm.ts +4 -3
  46. package/src/topology/fully-connected-topology.ts +1 -2
  47. package/src/topology/mmst-topology.ts +1 -1
  48. package/src/topology/star-topology.ts +1 -2
  49. package/src/transport/memory-transport.ts +1 -1
  50. package/src/transport/webrtc-transport-proxy.ts +14 -12
  51. package/src/transport/webrtc-transport-service.ts +31 -8
  52. package/src/transport/webrtc-transport.ts +1 -1
  53. package/dist/lib/browser/chunk-MIYEP22D.mjs.map +0 -7
@@ -31,10 +31,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
33
  Connection: () => Connection,
34
+ ConnectionLimiter: () => ConnectionLimiter,
34
35
  ConnectionLog: () => ConnectionLog,
35
36
  ConnectionState: () => ConnectionState,
36
37
  EventType: () => EventType,
37
38
  FullyConnectedTopology: () => FullyConnectedTopology,
39
+ MAX_CONCURRENT_INITIATING_CONNECTIONS: () => MAX_CONCURRENT_INITIATING_CONNECTIONS,
38
40
  MMSTTopology: () => MMSTTopology,
39
41
  MemoryTransport: () => MemoryTransport,
40
42
  MemoryTransportFactory: () => MemoryTransportFactory,
@@ -53,17 +55,17 @@ __export(src_exports, {
53
55
  module.exports = __toCommonJS(src_exports);
54
56
 
55
57
  // packages/core/mesh/network-manager/src/connection-log.ts
56
- var import_async5 = require("@dxos/async");
58
+ var import_async6 = require("@dxos/async");
57
59
  var import_debug3 = require("@dxos/debug");
58
- var import_keys6 = require("@dxos/keys");
59
- var import_util4 = require("@dxos/util");
60
+ var import_keys7 = require("@dxos/keys");
61
+ var import_util5 = require("@dxos/util");
60
62
 
61
63
  // packages/core/mesh/network-manager/src/swarm/connection.ts
62
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
63
64
  var import_async = require("@dxos/async");
64
65
  var import_context = require("@dxos/context");
65
66
  var import_debug = require("@dxos/debug");
66
67
  var import_errors = require("@dxos/errors");
68
+ var import_invariant = require("@dxos/invariant");
67
69
  var import_keys = require("@dxos/keys");
68
70
  var import_log = require("@dxos/log");
69
71
  var import_protocols = require("@dxos/protocols");
@@ -135,6 +137,18 @@ var Connection = class {
135
137
  await this._flushSignalBuffer();
136
138
  });
137
139
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
140
+ import_log.log.trace("dxos.mesh.connection.construct", {
141
+ sessionId: this.sessionId,
142
+ topic: this.topic,
143
+ localPeerId: this.ownId,
144
+ remotePeerId: this.remoteId,
145
+ initiator: this.initiator
146
+ }, {
147
+ F: __dxlog_file,
148
+ L: 95,
149
+ S: this,
150
+ C: (f, a) => f(...a)
151
+ });
138
152
  }
139
153
  get state() {
140
154
  return this._state;
@@ -148,14 +162,33 @@ var Connection = class {
148
162
  /**
149
163
  * Create an underlying transport and prepares it for the connection.
150
164
  */
151
- // TODO(burdon): Make async?
152
- openConnection() {
153
- (0, import_tiny_invariant.default)(this._state === ConnectionState.INITIAL, "Invalid state.");
165
+ async openConnection() {
166
+ (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
167
+ F: __dxlog_file,
168
+ L: 120,
169
+ S: this,
170
+ A: [
171
+ "this._state === ConnectionState.INITIAL",
172
+ "'Invalid state.'"
173
+ ]
174
+ });
154
175
  import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
155
176
  id: this._instanceId
156
177
  }), {
157
178
  F: __dxlog_file,
158
- L: 115,
179
+ L: 121,
180
+ S: this,
181
+ C: (f, a) => f(...a)
182
+ });
183
+ import_log.log.trace("dxos.mesh.connection.open", {
184
+ sessionId: this.sessionId,
185
+ topic: this.topic,
186
+ localPeerId: this.ownId,
187
+ remotePeerId: this.remoteId,
188
+ initiator: this.initiator
189
+ }, {
190
+ F: __dxlog_file,
191
+ L: 122,
159
192
  S: this,
160
193
  C: (f, a) => f(...a)
161
194
  });
@@ -166,13 +199,21 @@ var Connection = class {
166
199
  this._protocol.stream.on("close", () => {
167
200
  (0, import_log.log)("protocol stream closed", void 0, {
168
201
  F: __dxlog_file,
169
- L: 126,
202
+ L: 139,
170
203
  S: this,
171
204
  C: (f, a) => f(...a)
172
205
  });
173
206
  this.close().catch((err) => this.errors.raise(err));
174
207
  });
175
- (0, import_tiny_invariant.default)(!this._transport);
208
+ (0, import_invariant.invariant)(!this._transport, void 0, {
209
+ F: __dxlog_file,
210
+ L: 143,
211
+ S: this,
212
+ A: [
213
+ "!this._transport",
214
+ ""
215
+ ]
216
+ });
176
217
  this._transport = this._transportFactory.createTransport({
177
218
  initiator: this.initiator,
178
219
  stream: this._protocol.stream,
@@ -198,13 +239,12 @@ var Connection = class {
198
239
  id: this._instanceId
199
240
  }), {
200
241
  F: __dxlog_file,
201
- L: 159,
242
+ L: 172,
202
243
  S: this,
203
244
  C: (f, a) => f(...a)
204
245
  });
205
246
  }
206
247
  async close() {
207
- var _a;
208
248
  if (this._state === ConnectionState.CLOSED) {
209
249
  return;
210
250
  }
@@ -214,7 +254,7 @@ var Connection = class {
214
254
  peerId: this.ownId
215
255
  }, {
216
256
  F: __dxlog_file,
217
- L: 171,
257
+ L: 184,
218
258
  S: this,
219
259
  C: (f, a) => f(...a)
220
260
  });
@@ -223,17 +263,17 @@ var Connection = class {
223
263
  } catch (err) {
224
264
  import_log.log.catch(err, void 0, {
225
265
  F: __dxlog_file,
226
- L: 177,
266
+ L: 190,
227
267
  S: this,
228
268
  C: (f, a) => f(...a)
229
269
  });
230
270
  }
231
271
  try {
232
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
272
+ await this._transport?.destroy();
233
273
  } catch (err) {
234
274
  import_log.log.catch(err, void 0, {
235
275
  F: __dxlog_file,
236
- L: 184,
276
+ L: 197,
237
277
  S: this,
238
278
  C: (f, a) => f(...a)
239
279
  });
@@ -242,7 +282,7 @@ var Connection = class {
242
282
  peerId: this.ownId
243
283
  }, {
244
284
  F: __dxlog_file,
245
- L: 187,
285
+ L: 200,
246
286
  S: this,
247
287
  C: (f, a) => f(...a)
248
288
  });
@@ -282,7 +322,7 @@ var Connection = class {
282
322
  err
283
323
  }, {
284
324
  F: __dxlog_file,
285
- L: 221,
325
+ L: 234,
286
326
  S: this,
287
327
  C: (f, a) => f(...a)
288
328
  });
@@ -293,21 +333,52 @@ var Connection = class {
293
333
  * Receive a signal from the remote peer.
294
334
  */
295
335
  async signal(msg) {
296
- var _a, _b, _c;
297
- (0, import_tiny_invariant.default)(msg.sessionId);
336
+ (0, import_invariant.invariant)(msg.sessionId, void 0, {
337
+ F: __dxlog_file,
338
+ L: 243,
339
+ S: this,
340
+ A: [
341
+ "msg.sessionId",
342
+ ""
343
+ ]
344
+ });
298
345
  if (!msg.sessionId.equals(this.sessionId)) {
299
346
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
300
347
  F: __dxlog_file,
301
- L: 232,
348
+ L: 245,
302
349
  S: this,
303
350
  C: (f, a) => f(...a)
304
351
  });
305
352
  return;
306
353
  }
307
- (0, import_tiny_invariant.default)(msg.data.signal || msg.data.signalBatch);
308
- (0, import_tiny_invariant.default)((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
309
- (0, import_tiny_invariant.default)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
310
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
354
+ (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
355
+ F: __dxlog_file,
356
+ L: 248,
357
+ S: this,
358
+ A: [
359
+ "msg.data.signal || msg.data.signalBatch",
360
+ ""
361
+ ]
362
+ });
363
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
364
+ F: __dxlog_file,
365
+ L: 249,
366
+ S: this,
367
+ A: [
368
+ "msg.author?.equals(this.remoteId)",
369
+ ""
370
+ ]
371
+ });
372
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
373
+ F: __dxlog_file,
374
+ L: 250,
375
+ S: this,
376
+ A: [
377
+ "msg.recipient?.equals(this.ownId)",
378
+ ""
379
+ ]
380
+ });
381
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
311
382
  msg.data.signal
312
383
  ];
313
384
  for (const signal of signals) {
@@ -321,20 +392,28 @@ var Connection = class {
321
392
  msg: msg.data
322
393
  }, {
323
394
  F: __dxlog_file,
324
- L: 246,
395
+ L: 259,
325
396
  S: this,
326
397
  C: (f, a) => f(...a)
327
398
  });
328
399
  this._incomingSignalBuffer.push(signal);
329
400
  } else {
330
- (0, import_tiny_invariant.default)(this._transport, "Connection not ready to accept signals.");
401
+ (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
402
+ F: __dxlog_file,
403
+ L: 262,
404
+ S: this,
405
+ A: [
406
+ "this._transport",
407
+ "'Connection not ready to accept signals.'"
408
+ ]
409
+ });
331
410
  (0, import_log.log)("received signal", {
332
411
  peerId: this.ownId,
333
412
  remoteId: this.remoteId,
334
413
  msg: msg.data
335
414
  }, {
336
415
  F: __dxlog_file,
337
- L: 250,
416
+ L: 263,
338
417
  S: this,
339
418
  C: (f, a) => f(...a)
340
419
  });
@@ -349,11 +428,19 @@ var Connection = class {
349
428
  peerId: this.ownId
350
429
  }, {
351
430
  F: __dxlog_file,
352
- L: 257,
431
+ L: 270,
353
432
  S: this,
354
433
  C: (f, a) => f(...a)
355
434
  });
356
- (0, import_tiny_invariant.default)(state !== this._state, "Already in this state.");
435
+ (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
436
+ F: __dxlog_file,
437
+ L: 271,
438
+ S: this,
439
+ A: [
440
+ "state !== this._state",
441
+ "'Already in this state.'"
442
+ ]
443
+ });
357
444
  this._state = state;
358
445
  this.stateChanged.emit(state);
359
446
  }
@@ -363,18 +450,18 @@ _ts_decorate([
363
450
  ], Connection.prototype, "close", null);
364
451
 
365
452
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
366
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
367
453
  var import_async3 = require("@dxos/async");
368
454
  var import_context4 = require("@dxos/context");
369
455
  var import_debug2 = require("@dxos/debug");
456
+ var import_invariant4 = require("@dxos/invariant");
370
457
  var import_keys4 = require("@dxos/keys");
371
458
  var import_log4 = require("@dxos/log");
372
459
  var import_protocols3 = require("@dxos/protocols");
373
460
  var import_util2 = require("@dxos/util");
374
461
 
375
462
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
376
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
377
463
  var import_context2 = require("@dxos/context");
464
+ var import_invariant2 = require("@dxos/invariant");
378
465
  var import_keys2 = require("@dxos/keys");
379
466
  var import_log2 = require("@dxos/log");
380
467
  var import_protocols2 = require("@dxos/protocols");
@@ -391,7 +478,6 @@ var SwarmMessenger = class {
391
478
  this._topic = topic;
392
479
  }
393
480
  async receiveMessage({ author, recipient, payload }) {
394
- var _a, _b, _c, _d;
395
481
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
396
482
  return;
397
483
  }
@@ -405,25 +491,25 @@ var SwarmMessenger = class {
405
491
  msg: message
406
492
  }, {
407
493
  F: __dxlog_file2,
408
- L: 70,
494
+ L: 69,
409
495
  S: this,
410
496
  C: (f, a) => f(...a)
411
497
  });
412
- if ((_a = message.data) == null ? void 0 : _a.offer) {
498
+ if (message.data?.offer) {
413
499
  await this._handleOffer({
414
500
  author,
415
501
  recipient,
416
502
  message
417
503
  });
418
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
504
+ } else if (message.data?.answer) {
419
505
  await this._resolveAnswers(message);
420
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
506
+ } else if (message.data?.signal) {
421
507
  await this._handleSignal({
422
508
  author,
423
509
  recipient,
424
510
  message
425
511
  });
426
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
512
+ } else if (message.data?.signalBatch) {
427
513
  await this._handleSignal({
428
514
  author,
429
515
  recipient,
@@ -434,15 +520,22 @@ var SwarmMessenger = class {
434
520
  message
435
521
  }, {
436
522
  F: __dxlog_file2,
437
- L: 81,
523
+ L: 80,
438
524
  S: this,
439
525
  C: (f, a) => f(...a)
440
526
  });
441
527
  }
442
528
  }
443
529
  async signal(message) {
444
- var _a, _b;
445
- (0, import_tiny_invariant2.default)(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
530
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
531
+ F: __dxlog_file2,
532
+ L: 85,
533
+ S: this,
534
+ A: [
535
+ "message.data?.signal || message.data?.signalBatch",
536
+ "'Invalid message'"
537
+ ]
538
+ });
446
539
  await this._sendReliableMessage({
447
540
  author: message.author,
448
541
  recipient: message.recipient,
@@ -466,11 +559,10 @@ var SwarmMessenger = class {
466
559
  });
467
560
  }
468
561
  async _sendReliableMessage({ author, recipient, message }) {
469
- var _a;
470
562
  const networkMessage = {
471
563
  ...message,
472
564
  // Setting unique message_id if it not specified yet.
473
- messageId: (_a = message.messageId) != null ? _a : import_keys2.PublicKey.random()
565
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
474
566
  };
475
567
  (0, import_log2.log)("sending", {
476
568
  from: author,
@@ -478,7 +570,7 @@ var SwarmMessenger = class {
478
570
  msg: networkMessage
479
571
  }, {
480
572
  F: __dxlog_file2,
481
- L: 124,
573
+ L: 123,
482
574
  S: this,
483
575
  C: (f, a) => f(...a)
484
576
  });
@@ -492,17 +584,32 @@ var SwarmMessenger = class {
492
584
  });
493
585
  }
494
586
  async _resolveAnswers(message) {
495
- var _a, _b, _c;
496
- (0, import_tiny_invariant2.default)((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
587
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
588
+ F: __dxlog_file2,
589
+ L: 135,
590
+ S: this,
591
+ A: [
592
+ "message.data?.answer?.offerMessageId",
593
+ "'No offerMessageId'"
594
+ ]
595
+ });
497
596
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
498
597
  if (offerRecord) {
499
598
  this._offerRecords.delete(message.data.answer.offerMessageId);
500
- (0, import_tiny_invariant2.default)((_c = message.data) == null ? void 0 : _c.answer, "No answer");
599
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
600
+ F: __dxlog_file2,
601
+ L: 139,
602
+ S: this,
603
+ A: [
604
+ "message.data?.answer",
605
+ "'No answer'"
606
+ ]
607
+ });
501
608
  (0, import_log2.log)("resolving", {
502
609
  answer: message.data.answer
503
610
  }, {
504
611
  F: __dxlog_file2,
505
- L: 141,
612
+ L: 140,
506
613
  S: this,
507
614
  C: (f, a) => f(...a)
508
615
  });
@@ -510,7 +617,15 @@ var SwarmMessenger = class {
510
617
  }
511
618
  }
512
619
  async _handleOffer({ author, recipient, message }) {
513
- (0, import_tiny_invariant2.default)(message.data.offer, "No offer");
620
+ (0, import_invariant2.invariant)(message.data.offer, "No offer", {
621
+ F: __dxlog_file2,
622
+ L: 154,
623
+ S: this,
624
+ A: [
625
+ "message.data.offer",
626
+ "'No offer'"
627
+ ]
628
+ });
514
629
  const offerMessage = {
515
630
  author,
516
631
  recipient,
@@ -538,15 +653,31 @@ var SwarmMessenger = class {
538
653
  err
539
654
  }, {
540
655
  F: __dxlog_file2,
541
- L: 175,
656
+ L: 174,
542
657
  S: this,
543
658
  C: (f, a) => f(...a)
544
659
  });
545
660
  }
546
661
  }
547
662
  async _handleSignal({ author, recipient, message }) {
548
- (0, import_tiny_invariant2.default)(message.messageId);
549
- (0, import_tiny_invariant2.default)(message.data.signal || message.data.signalBatch, "Invalid message");
663
+ (0, import_invariant2.invariant)(message.messageId, void 0, {
664
+ F: __dxlog_file2,
665
+ L: 187,
666
+ S: this,
667
+ A: [
668
+ "message.messageId",
669
+ ""
670
+ ]
671
+ });
672
+ (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
673
+ F: __dxlog_file2,
674
+ L: 188,
675
+ S: this,
676
+ A: [
677
+ "message.data.signal || message.data.signalBatch",
678
+ "'Invalid message'"
679
+ ]
680
+ });
550
681
  const signalMessage = {
551
682
  author,
552
683
  recipient,
@@ -561,9 +692,10 @@ var SwarmMessenger = class {
561
692
  };
562
693
 
563
694
  // packages/core/mesh/network-manager/src/swarm/peer.ts
564
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
565
695
  var import_async2 = require("@dxos/async");
566
696
  var import_context3 = require("@dxos/context");
697
+ var import_errors2 = require("@dxos/errors");
698
+ var import_invariant3 = require("@dxos/invariant");
567
699
  var import_keys3 = require("@dxos/keys");
568
700
  var import_log3 = require("@dxos/log");
569
701
  function _ts_decorate2(decorators, target, key, desc) {
@@ -579,13 +711,14 @@ function _ts_decorate2(decorators, target, key, desc) {
579
711
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
580
712
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
581
713
  var Peer = class {
582
- constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
714
+ constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
583
715
  this.id = id;
584
716
  this.topic = topic;
585
717
  this.localPeerId = localPeerId;
586
718
  this._signalMessaging = _signalMessaging;
587
719
  this._protocolProvider = _protocolProvider;
588
720
  this._transportFactory = _transportFactory;
721
+ this._connectionLimiter = _connectionLimiter;
589
722
  this._callbacks = _callbacks;
590
723
  this._availableAfter = 0;
591
724
  this.availableToConnect = true;
@@ -600,13 +733,14 @@ var Peer = class {
600
733
  const remoteId = message.author;
601
734
  if (this.connection || this.initiating) {
602
735
  if (remoteId.toHex() < this.localPeerId.toHex()) {
603
- (0, import_log3.log)("closing local connection and accepting remote peer's offer", {
604
- id: this.id,
736
+ (0, import_log3.log)("close local connection", {
737
+ localPeerId: this.id,
605
738
  topic: this.topic,
606
- peerId: this.localPeerId
739
+ remotePeerId: this.localPeerId,
740
+ sessionId: this.connection?.sessionId
607
741
  }, {
608
742
  F: __dxlog_file3,
609
- L: 105,
743
+ L: 106,
610
744
  S: this,
611
745
  C: (f, a) => f(...a)
612
746
  });
@@ -621,22 +755,33 @@ var Peer = class {
621
755
  }
622
756
  if (await this._callbacks.onOffer(remoteId)) {
623
757
  if (!this.connection) {
624
- (0, import_tiny_invariant3.default)(message.sessionId);
758
+ (0, import_invariant3.invariant)(message.sessionId, void 0, {
759
+ F: __dxlog_file3,
760
+ L: 126,
761
+ S: this,
762
+ A: [
763
+ "message.sessionId",
764
+ ""
765
+ ]
766
+ });
625
767
  const connection = this._createConnection(false, message.sessionId);
626
768
  try {
627
- connection.openConnection();
769
+ await this._connectionLimiter.connecting(message.sessionId);
770
+ await connection.openConnection();
628
771
  } catch (err) {
629
- import_log3.log.warn("connection error", {
630
- topic: this.topic,
631
- peerId: this.localPeerId,
632
- remoteId: this.id,
633
- err
634
- }, {
635
- F: __dxlog_file3,
636
- L: 129,
637
- S: this,
638
- C: (f, a) => f(...a)
639
- });
772
+ if (!(err instanceof import_errors2.CancelledError)) {
773
+ import_log3.log.warn("connection error", {
774
+ topic: this.topic,
775
+ peerId: this.localPeerId,
776
+ remoteId: this.id,
777
+ err
778
+ }, {
779
+ F: __dxlog_file3,
780
+ L: 134,
781
+ S: this,
782
+ C: (f, a) => f(...a)
783
+ });
784
+ }
640
785
  await this.closeConnection();
641
786
  }
642
787
  return {
@@ -652,8 +797,24 @@ var Peer = class {
652
797
  * Initiate a connection to the remote peer.
653
798
  */
654
799
  async initiateConnection() {
655
- (0, import_tiny_invariant3.default)(!this.initiating, "Initiation in progress.");
656
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
800
+ (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
801
+ F: __dxlog_file3,
802
+ L: 151,
803
+ S: this,
804
+ A: [
805
+ "!this.initiating",
806
+ "'Initiation in progress.'"
807
+ ]
808
+ });
809
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
810
+ F: __dxlog_file3,
811
+ L: 152,
812
+ S: this,
813
+ A: [
814
+ "!this.connection",
815
+ "'Already connected.'"
816
+ ]
817
+ });
657
818
  const sessionId = import_keys3.PublicKey.random();
658
819
  (0, import_log3.log)("initiating...", {
659
820
  id: this.id,
@@ -662,13 +823,14 @@ var Peer = class {
662
823
  sessionId
663
824
  }, {
664
825
  F: __dxlog_file3,
665
- L: 147,
826
+ L: 154,
666
827
  S: this,
667
828
  C: (f, a) => f(...a)
668
829
  });
669
830
  const connection = this._createConnection(true, sessionId);
670
831
  this.initiating = true;
671
832
  try {
833
+ await this._connectionLimiter.connecting(sessionId);
672
834
  const answer = await this._signalMessaging.offer({
673
835
  author: this.localPeerId,
674
836
  recipient: this.id,
@@ -685,14 +847,14 @@ var Peer = class {
685
847
  remoteId: this.id
686
848
  }, {
687
849
  F: __dxlog_file3,
688
- L: 159,
850
+ L: 169,
689
851
  S: this,
690
852
  C: (f, a) => f(...a)
691
853
  });
692
854
  if (connection.state !== ConnectionState.INITIAL) {
693
855
  (0, import_log3.log)("ignoring response", void 0, {
694
856
  F: __dxlog_file3,
695
- L: 161,
857
+ L: 171,
696
858
  S: this,
697
859
  C: (f, a) => f(...a)
698
860
  });
@@ -702,7 +864,7 @@ var Peer = class {
702
864
  this._callbacks.onRejected();
703
865
  return;
704
866
  }
705
- connection.openConnection();
867
+ await connection.openConnection();
706
868
  this._callbacks.onAccepted();
707
869
  } catch (err) {
708
870
  (0, import_log3.log)("initiation error", {
@@ -712,7 +874,7 @@ var Peer = class {
712
874
  remoteId: this.id
713
875
  }, {
714
876
  F: __dxlog_file3,
715
- L: 172,
877
+ L: 182,
716
878
  S: this,
717
879
  C: (f, a) => f(...a)
718
880
  });
@@ -727,7 +889,6 @@ var Peer = class {
727
889
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
728
890
  */
729
891
  _createConnection(initiator, sessionId) {
730
- var _a;
731
892
  (0, import_log3.log)("creating connection", {
732
893
  topic: this.topic,
733
894
  peerId: this.localPeerId,
@@ -736,11 +897,19 @@ var Peer = class {
736
897
  sessionId
737
898
  }, {
738
899
  F: __dxlog_file3,
739
- L: 186,
900
+ L: 196,
740
901
  S: this,
741
902
  C: (f, a) => f(...a)
742
903
  });
743
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
904
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
905
+ F: __dxlog_file3,
906
+ L: 203,
907
+ S: this,
908
+ A: [
909
+ "!this.connection",
910
+ "'Already connected.'"
911
+ ]
912
+ });
744
913
  const connection = new Connection(
745
914
  this.topic,
746
915
  this.localPeerId,
@@ -758,7 +927,7 @@ var Peer = class {
758
927
  this._transportFactory
759
928
  );
760
929
  this._callbacks.onInitiated(connection);
761
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
930
+ void this._connectionCtx?.dispose();
762
931
  this._connectionCtx = this._ctx.derive();
763
932
  connection.stateChanged.on((state) => {
764
933
  switch (state) {
@@ -766,6 +935,19 @@ var Peer = class {
766
935
  this.availableToConnect = true;
767
936
  this._lastConnectionTime = Date.now();
768
937
  this._callbacks.onConnected();
938
+ this._connectionLimiter.doneConnecting(sessionId);
939
+ import_log3.log.trace("dxos.mesh.connection.connected", {
940
+ topic: this.topic,
941
+ localPeerId: this.localPeerId,
942
+ remotePeerId: this.id,
943
+ sessionId,
944
+ initiator
945
+ }, {
946
+ F: __dxlog_file3,
947
+ L: 229,
948
+ S: this,
949
+ C: (f, a) => f(...a)
950
+ });
769
951
  break;
770
952
  }
771
953
  case ConnectionState.CLOSED: {
@@ -776,11 +958,31 @@ var Peer = class {
776
958
  initiator
777
959
  }, {
778
960
  F: __dxlog_file3,
779
- L: 221,
961
+ L: 240,
962
+ S: this,
963
+ C: (f, a) => f(...a)
964
+ });
965
+ (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
966
+ F: __dxlog_file3,
967
+ L: 241,
968
+ S: this,
969
+ A: [
970
+ "this.connection === connection",
971
+ "'Connection mismatch (race condition).'"
972
+ ]
973
+ });
974
+ import_log3.log.trace("dxos.mesh.connection.closed", {
975
+ topic: this.topic,
976
+ localPeerId: this.localPeerId,
977
+ remotePeerId: this.id,
978
+ sessionId,
979
+ initiator
980
+ }, {
981
+ F: __dxlog_file3,
982
+ L: 243,
780
983
  S: this,
781
984
  C: (f, a) => f(...a)
782
985
  });
783
- (0, import_tiny_invariant3.default)(this.connection === connection, "Connection mismatch (race condition).");
784
986
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
785
987
  this._availableAfter = 0;
786
988
  } else {
@@ -789,6 +991,7 @@ var Peer = class {
789
991
  }
790
992
  this.connection = void 0;
791
993
  this._callbacks.onDisconnected();
994
+ this._connectionLimiter.doneConnecting(sessionId);
792
995
  (0, import_async2.scheduleTask)(this._connectionCtx, () => {
793
996
  this.availableToConnect = true;
794
997
  this._callbacks.onPeerAvailable();
@@ -806,7 +1009,20 @@ var Peer = class {
806
1009
  err
807
1010
  }, {
808
1011
  F: __dxlog_file3,
809
- L: 249,
1012
+ L: 277,
1013
+ S: this,
1014
+ C: (f, a) => f(...a)
1015
+ });
1016
+ import_log3.log.trace("dxos.mesh.connection.error", {
1017
+ topic: this.topic,
1018
+ localPeerId: this.localPeerId,
1019
+ remotePeerId: this.id,
1020
+ sessionId,
1021
+ initiator,
1022
+ err
1023
+ }, {
1024
+ F: __dxlog_file3,
1025
+ L: 278,
810
1026
  S: this,
811
1027
  C: (f, a) => f(...a)
812
1028
  });
@@ -825,7 +1041,7 @@ var Peer = class {
825
1041
  sessionId: connection.sessionId
826
1042
  }, {
827
1043
  F: __dxlog_file3,
828
- L: 265,
1044
+ L: 301,
829
1045
  S: this,
830
1046
  C: (f, a) => f(...a)
831
1047
  });
@@ -835,7 +1051,7 @@ var Peer = class {
835
1051
  sessionId: connection.sessionId
836
1052
  }, {
837
1053
  F: __dxlog_file3,
838
- L: 271,
1054
+ L: 307,
839
1055
  S: this,
840
1056
  C: (f, a) => f(...a)
841
1057
  });
@@ -846,7 +1062,7 @@ var Peer = class {
846
1062
  message
847
1063
  }, {
848
1064
  F: __dxlog_file3,
849
- L: 276,
1065
+ L: 312,
850
1066
  S: this,
851
1067
  C: (f, a) => f(...a)
852
1068
  });
@@ -855,18 +1071,17 @@ var Peer = class {
855
1071
  await this.connection.signal(message);
856
1072
  }
857
1073
  async destroy() {
858
- var _a;
859
1074
  await this._ctx.dispose();
860
1075
  (0, import_log3.log)("Destroying peer", {
861
1076
  peerId: this.id,
862
1077
  topic: this.topic
863
1078
  }, {
864
1079
  F: __dxlog_file3,
865
- L: 285,
1080
+ L: 322,
866
1081
  S: this,
867
1082
  C: (f, a) => f(...a)
868
1083
  });
869
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1084
+ await this?.connection?.close();
870
1085
  }
871
1086
  };
872
1087
  _ts_decorate2([
@@ -902,7 +1117,7 @@ var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
902
1117
  var Swarm = class {
903
1118
  // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
904
1119
  // TODO(burdon): Split up properties.
905
- constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
1120
+ constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter) {
906
1121
  this._topic = _topic;
907
1122
  this._ownPeerId = _ownPeerId;
908
1123
  this._topology = _topology;
@@ -910,6 +1125,7 @@ var Swarm = class {
910
1125
  this._messenger = _messenger;
911
1126
  this._transportFactory = _transportFactory;
912
1127
  this._label = _label;
1128
+ this._connectionLimiter = _connectionLimiter;
913
1129
  this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
914
1130
  this._ctx = new import_context4.Context();
915
1131
  this._listeningHandle = void 0;
@@ -926,7 +1142,7 @@ var Swarm = class {
926
1142
  }
927
1143
  }), {
928
1144
  F: __dxlog_file4,
929
- L: 86,
1145
+ L: 87,
930
1146
  S: this,
931
1147
  C: (f, a) => f(...a)
932
1148
  });
@@ -934,7 +1150,7 @@ var Swarm = class {
934
1150
  peerId: _ownPeerId
935
1151
  }, {
936
1152
  F: __dxlog_file4,
937
- L: 90,
1153
+ L: 91,
938
1154
  S: this,
939
1155
  C: (f, a) => f(...a)
940
1156
  });
@@ -949,7 +1165,7 @@ var Swarm = class {
949
1165
  id: this._instanceId
950
1166
  }), {
951
1167
  F: __dxlog_file4,
952
- L: 99,
1168
+ L: 100,
953
1169
  S: this,
954
1170
  C: (f, a) => f(...a)
955
1171
  });
@@ -970,7 +1186,15 @@ var Swarm = class {
970
1186
  return this._topic;
971
1187
  }
972
1188
  async open() {
973
- (0, import_tiny_invariant4.default)(!this._listeningHandle);
1189
+ (0, import_invariant4.invariant)(!this._listeningHandle, void 0, {
1190
+ F: __dxlog_file4,
1191
+ L: 127,
1192
+ S: this,
1193
+ A: [
1194
+ "!this._listeningHandle",
1195
+ ""
1196
+ ]
1197
+ });
974
1198
  this._listeningHandle = await this._messenger.listen({
975
1199
  peerId: this._ownPeerId,
976
1200
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -979,7 +1203,7 @@ var Swarm = class {
979
1203
  err
980
1204
  }, {
981
1205
  F: __dxlog_file4,
982
- L: 133,
1206
+ L: 134,
983
1207
  S: this,
984
1208
  C: (f, a) => f(...a)
985
1209
  }));
@@ -987,27 +1211,34 @@ var Swarm = class {
987
1211
  });
988
1212
  }
989
1213
  async destroy() {
990
- var _a;
991
1214
  (0, import_log4.log)("destroying...", void 0, {
992
1215
  F: __dxlog_file4,
993
- L: 139,
1216
+ L: 140,
994
1217
  S: this,
995
1218
  C: (f, a) => f(...a)
996
1219
  });
997
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1220
+ await this._listeningHandle?.unsubscribe();
998
1221
  this._listeningHandle = void 0;
999
1222
  await this._ctx.dispose();
1000
1223
  await this._topology.destroy();
1001
1224
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1002
1225
  (0, import_log4.log)("destroyed", void 0, {
1003
1226
  F: __dxlog_file4,
1004
- L: 146,
1227
+ L: 147,
1005
1228
  S: this,
1006
1229
  C: (f, a) => f(...a)
1007
1230
  });
1008
1231
  }
1009
1232
  async setTopology(topology) {
1010
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Swarm is offline");
1233
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Swarm is offline", {
1234
+ F: __dxlog_file4,
1235
+ L: 151,
1236
+ S: this,
1237
+ A: [
1238
+ "!this._ctx.disposed",
1239
+ "'Swarm is offline'"
1240
+ ]
1241
+ });
1011
1242
  if (topology === this._topology) {
1012
1243
  return;
1013
1244
  }
@@ -1016,7 +1247,7 @@ var Swarm = class {
1016
1247
  topology: getClassName(topology)
1017
1248
  }, {
1018
1249
  F: __dxlog_file4,
1019
- L: 154,
1250
+ L: 155,
1020
1251
  S: this,
1021
1252
  C: (f, a) => f(...a)
1022
1253
  });
@@ -1026,19 +1257,18 @@ var Swarm = class {
1026
1257
  this._topology.update();
1027
1258
  }
1028
1259
  onSwarmEvent(swarmEvent) {
1029
- var _a;
1030
1260
  (0, import_log4.log)("swarm event", {
1031
1261
  swarmEvent
1032
1262
  }, {
1033
1263
  F: __dxlog_file4,
1034
- L: 167,
1264
+ L: 168,
1035
1265
  S: this,
1036
1266
  C: (f, a) => f(...a)
1037
1267
  });
1038
1268
  if (this._ctx.disposed) {
1039
1269
  (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1040
1270
  F: __dxlog_file4,
1041
- L: 170,
1271
+ L: 171,
1042
1272
  S: this,
1043
1273
  C: (f, a) => f(...a)
1044
1274
  });
@@ -1050,7 +1280,7 @@ var Swarm = class {
1050
1280
  peerId
1051
1281
  }, {
1052
1282
  F: __dxlog_file4,
1053
- L: 176,
1283
+ L: 177,
1054
1284
  S: this,
1055
1285
  C: (f, a) => f(...a)
1056
1286
  });
@@ -1062,10 +1292,10 @@ var Swarm = class {
1062
1292
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1063
1293
  if (peer) {
1064
1294
  peer.advertizing = false;
1065
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1295
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1066
1296
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1067
1297
  F: __dxlog_file4,
1068
- L: 187,
1298
+ L: 188,
1069
1299
  S: this,
1070
1300
  C: (f, a) => f(...a)
1071
1301
  }));
@@ -1075,19 +1305,18 @@ var Swarm = class {
1075
1305
  this._topology.update();
1076
1306
  }
1077
1307
  async onOffer(message) {
1078
- var _a, _b;
1079
1308
  (0, import_log4.log)("offer", {
1080
1309
  message
1081
1310
  }, {
1082
1311
  F: __dxlog_file4,
1083
- L: 197,
1312
+ L: 198,
1084
1313
  S: this,
1085
1314
  C: (f, a) => f(...a)
1086
1315
  });
1087
1316
  if (this._ctx.disposed) {
1088
1317
  (0, import_log4.log)("ignored for disposed swarm", void 0, {
1089
1318
  F: __dxlog_file4,
1090
- L: 199,
1319
+ L: 200,
1091
1320
  S: this,
1092
1321
  C: (f, a) => f(...a)
1093
1322
  });
@@ -1095,13 +1324,21 @@ var Swarm = class {
1095
1324
  accept: false
1096
1325
  };
1097
1326
  }
1098
- (0, import_tiny_invariant4.default)(message.author);
1099
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1327
+ (0, import_invariant4.invariant)(message.author, void 0, {
1328
+ F: __dxlog_file4,
1329
+ L: 205,
1330
+ S: this,
1331
+ A: [
1332
+ "message.author",
1333
+ ""
1334
+ ]
1335
+ });
1336
+ if (!message.recipient?.equals(this._ownPeerId)) {
1100
1337
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1101
1338
  message
1102
1339
  }, {
1103
1340
  F: __dxlog_file4,
1104
- L: 206,
1341
+ L: 207,
1105
1342
  S: this,
1106
1343
  C: (f, a) => f(...a)
1107
1344
  });
@@ -1109,12 +1346,12 @@ var Swarm = class {
1109
1346
  accept: false
1110
1347
  };
1111
1348
  }
1112
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1349
+ if (!message.topic?.equals(this._topic)) {
1113
1350
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1114
1351
  message
1115
1352
  }, {
1116
1353
  F: __dxlog_file4,
1117
- L: 210,
1354
+ L: 211,
1118
1355
  S: this,
1119
1356
  C: (f, a) => f(...a)
1120
1357
  });
@@ -1128,7 +1365,6 @@ var Swarm = class {
1128
1365
  return answer;
1129
1366
  }
1130
1367
  async onSignal(message) {
1131
- var _a, _b;
1132
1368
  (0, import_log4.log)("signal", {
1133
1369
  message
1134
1370
  }, {
@@ -1146,9 +1382,33 @@ var Swarm = class {
1146
1382
  });
1147
1383
  return;
1148
1384
  }
1149
- (0, import_tiny_invariant4.default)((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1150
- (0, import_tiny_invariant4.default)((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1151
- (0, import_tiny_invariant4.default)(message.author);
1385
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1386
+ F: __dxlog_file4,
1387
+ L: 227,
1388
+ S: this,
1389
+ A: [
1390
+ "message.recipient?.equals(this._ownPeerId)",
1391
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1392
+ ]
1393
+ });
1394
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1395
+ F: __dxlog_file4,
1396
+ L: 231,
1397
+ S: this,
1398
+ A: [
1399
+ "message.topic?.equals(this._topic)",
1400
+ ""
1401
+ ]
1402
+ });
1403
+ (0, import_invariant4.invariant)(message.author, void 0, {
1404
+ F: __dxlog_file4,
1405
+ L: 232,
1406
+ S: this,
1407
+ A: [
1408
+ "message.author",
1409
+ ""
1410
+ ]
1411
+ });
1152
1412
  const peer = this._getOrCreatePeer(message.author);
1153
1413
  await peer.onSignal(message);
1154
1414
  }
@@ -1166,7 +1426,7 @@ var Swarm = class {
1166
1426
  _getOrCreatePeer(peerId) {
1167
1427
  let peer = this._peers.get(peerId);
1168
1428
  if (!peer) {
1169
- peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, {
1429
+ peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, this._connectionLimiter, {
1170
1430
  onInitiated: (connection) => {
1171
1431
  this.connectionAdded.emit(connection);
1172
1432
  },
@@ -1200,7 +1460,15 @@ var Swarm = class {
1200
1460
  return peer;
1201
1461
  }
1202
1462
  async _destroyPeer(peerId) {
1203
- (0, import_tiny_invariant4.default)(this._peers.has(peerId));
1463
+ (0, import_invariant4.invariant)(this._peers.has(peerId), void 0, {
1464
+ F: __dxlog_file4,
1465
+ L: 302,
1466
+ S: this,
1467
+ A: [
1468
+ "this._peers.has(peerId)",
1469
+ ""
1470
+ ]
1471
+ });
1204
1472
  await this._peers.get(peerId).destroy();
1205
1473
  this._peers.delete(peerId);
1206
1474
  }
@@ -1221,7 +1489,7 @@ var Swarm = class {
1221
1489
  } catch (err) {
1222
1490
  (0, import_log4.log)("initiation error", err, {
1223
1491
  F: __dxlog_file4,
1224
- L: 327,
1492
+ L: 328,
1225
1493
  S: this,
1226
1494
  C: (f, a) => f(...a)
1227
1495
  });
@@ -1249,7 +1517,7 @@ var Swarm = class {
1249
1517
  remoteId
1250
1518
  }, {
1251
1519
  F: __dxlog_file4,
1252
- L: 354,
1520
+ L: 355,
1253
1521
  S: this,
1254
1522
  C: (f, a) => f(...a)
1255
1523
  });
@@ -1266,7 +1534,7 @@ var Swarm = class {
1266
1534
  remoteId
1267
1535
  }, {
1268
1536
  F: __dxlog_file4,
1269
- L: 368,
1537
+ L: 369,
1270
1538
  S: this,
1271
1539
  C: (f, a) => f(...a)
1272
1540
  });
@@ -1276,7 +1544,7 @@ var Swarm = class {
1276
1544
  remoteId
1277
1545
  }, {
1278
1546
  F: __dxlog_file4,
1279
- L: 371,
1547
+ L: 372,
1280
1548
  S: this,
1281
1549
  C: (f, a) => f(...a)
1282
1550
  });
@@ -1304,9 +1572,6 @@ _ts_decorate3([
1304
1572
  _ts_decorate3([
1305
1573
  import_async3.synchronized
1306
1574
  ], Swarm.prototype, "onOffer", null);
1307
- _ts_decorate3([
1308
- import_async3.synchronized
1309
- ], Swarm.prototype, "onSignal", null);
1310
1575
  _ts_decorate3([
1311
1576
  import_async3.synchronized
1312
1577
  ], Swarm.prototype, "goOffline", null);
@@ -1338,8 +1603,7 @@ var SwarmMapper = class {
1338
1603
  }));
1339
1604
  }));
1340
1605
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1341
- var _a;
1342
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1606
+ this._connectionSubscriptions.get(peerId)?.();
1343
1607
  this._connectionSubscriptions.delete(peerId);
1344
1608
  this._update();
1345
1609
  }));
@@ -1385,6 +1649,88 @@ var SwarmMapper = class {
1385
1649
  }
1386
1650
  };
1387
1651
 
1652
+ // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
1653
+ var import_async5 = require("@dxos/async");
1654
+ var import_context5 = require("@dxos/context");
1655
+ var import_errors3 = require("@dxos/errors");
1656
+ var import_invariant5 = require("@dxos/invariant");
1657
+ var import_keys6 = require("@dxos/keys");
1658
+ var import_log6 = require("@dxos/log");
1659
+ var import_util4 = require("@dxos/util");
1660
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1661
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1662
+ var ConnectionLimiter = class {
1663
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1664
+ this._ctx = new import_context5.Context();
1665
+ /**
1666
+ * Queue of promises to resolve when initiating connections amount is below the limit.
1667
+ */
1668
+ this._waitingPromises = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
1669
+ this.resolveWaitingPromises = new import_async5.DeferredTask(this._ctx, async () => {
1670
+ Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
1671
+ resolve();
1672
+ });
1673
+ });
1674
+ this._maxConcurrentInitConnections = maxConcurrentInitConnections;
1675
+ }
1676
+ /**
1677
+ * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
1678
+ */
1679
+ async connecting(sessionId) {
1680
+ (0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1681
+ F: __dxlog_file6,
1682
+ L: 48,
1683
+ S: this,
1684
+ A: [
1685
+ "!this._waitingPromises.has(sessionId)",
1686
+ "'Peer is already waiting for connection'"
1687
+ ]
1688
+ });
1689
+ (0, import_log6.log)("waiting", {
1690
+ sessionId
1691
+ }, {
1692
+ F: __dxlog_file6,
1693
+ L: 49,
1694
+ S: this,
1695
+ C: (f, a) => f(...a)
1696
+ });
1697
+ await new Promise((resolve, reject) => {
1698
+ this._waitingPromises.set(sessionId, {
1699
+ resolve,
1700
+ reject
1701
+ });
1702
+ this.resolveWaitingPromises.schedule();
1703
+ });
1704
+ (0, import_log6.log)("allow", {
1705
+ sessionId
1706
+ }, {
1707
+ F: __dxlog_file6,
1708
+ L: 57,
1709
+ S: this,
1710
+ C: (f, a) => f(...a)
1711
+ });
1712
+ }
1713
+ /**
1714
+ * Rejects promise returned by `connecting` method.
1715
+ */
1716
+ doneConnecting(sessionId) {
1717
+ (0, import_log6.log)("done", {
1718
+ sessionId
1719
+ }, {
1720
+ F: __dxlog_file6,
1721
+ L: 64,
1722
+ S: this,
1723
+ C: (f, a) => f(...a)
1724
+ });
1725
+ if (!this._waitingPromises.has(sessionId)) {
1726
+ return;
1727
+ }
1728
+ this._waitingPromises.get(sessionId).reject(new import_errors3.CancelledError());
1729
+ this._waitingPromises.delete(sessionId);
1730
+ this.resolveWaitingPromises.schedule();
1731
+ }
1732
+ };
1733
+
1388
1734
  // packages/core/mesh/network-manager/src/connection-log.ts
1389
1735
  var EventType;
1390
1736
  (function(EventType2) {
@@ -1399,28 +1745,26 @@ var ConnectionLog = class {
1399
1745
  /**
1400
1746
  * SwarmId => info
1401
1747
  */
1402
- this._swarms = new import_util4.ComplexMap(import_keys6.PublicKey.hash);
1403
- this.update = new import_async5.Event();
1748
+ this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1749
+ this.update = new import_async6.Event();
1404
1750
  }
1405
1751
  getSwarmInfo(swarmId) {
1406
- var _a;
1407
- return (_a = this._swarms.get(swarmId)) != null ? _a : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1752
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1408
1753
  }
1409
1754
  get swarms() {
1410
1755
  return Array.from(this._swarms.values());
1411
1756
  }
1412
1757
  joinedSwarm(swarm) {
1413
1758
  const info = {
1414
- id: import_keys6.PublicKey.from(swarm._instanceId),
1759
+ id: import_keys7.PublicKey.from(swarm._instanceId),
1415
1760
  topic: swarm.topic,
1416
1761
  isActive: true,
1417
1762
  label: swarm.label,
1418
1763
  connections: []
1419
1764
  };
1420
- this._swarms.set(import_keys6.PublicKey.from(swarm._instanceId), info);
1765
+ this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1421
1766
  this.update.emit();
1422
1767
  swarm.connectionAdded.on((connection) => {
1423
- var _a, _b;
1424
1768
  const connectionInfo = {
1425
1769
  state: ConnectionState.INITIAL,
1426
1770
  remotePeerId: connection.remoteId,
@@ -1439,42 +1783,42 @@ var ConnectionLog = class {
1439
1783
  });
1440
1784
  this.update.emit();
1441
1785
  });
1442
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1786
+ connection.protocol?.stats?.on((stats) => {
1443
1787
  connectionInfo.streams = stats.channels;
1444
1788
  this.update.emit();
1445
1789
  });
1446
1790
  });
1447
1791
  }
1448
1792
  leftSwarm(swarm) {
1449
- this.getSwarmInfo(import_keys6.PublicKey.from(swarm._instanceId)).isActive = false;
1793
+ this.getSwarmInfo(import_keys7.PublicKey.from(swarm._instanceId)).isActive = false;
1450
1794
  this.update.emit();
1451
1795
  }
1452
1796
  };
1453
1797
 
1454
1798
  // packages/core/mesh/network-manager/src/network-manager.ts
1455
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
1456
- var import_async6 = require("@dxos/async");
1457
- var import_keys7 = require("@dxos/keys");
1458
- var import_log6 = require("@dxos/log");
1799
+ var import_async7 = require("@dxos/async");
1800
+ var import_invariant6 = require("@dxos/invariant");
1801
+ var import_keys8 = require("@dxos/keys");
1802
+ var import_log7 = require("@dxos/log");
1459
1803
  var import_messaging = require("@dxos/messaging");
1460
1804
  var import_protocols4 = require("@dxos/protocols");
1461
1805
  var import_services = require("@dxos/protocols/proto/dxos/client/services");
1462
- var import_util5 = require("@dxos/util");
1463
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1806
+ var import_util6 = require("@dxos/util");
1807
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1464
1808
  var NetworkManager = class {
1465
1809
  constructor({ transportFactory, signalManager, log: log1 }) {
1466
- this._swarms = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1467
- this._mappers = new import_util5.ComplexMap(import_keys7.PublicKey.hash);
1810
+ /**
1811
+ * @internal
1812
+ */
1813
+ this._swarms = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1814
+ this._mappers = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
1468
1815
  this._connectionState = import_services.ConnectionState.ONLINE;
1469
- this.connectionStateChanged = new import_async6.Event();
1470
- this.topicsUpdated = new import_async6.Event();
1471
- this._instanceId = import_keys7.PublicKey.random().toHex();
1816
+ this.connectionStateChanged = new import_async7.Event();
1817
+ this.topicsUpdated = new import_async7.Event();
1818
+ this._instanceId = import_keys8.PublicKey.random().toHex();
1472
1819
  this._transportFactory = transportFactory;
1473
1820
  this._signalManager = signalManager;
1474
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1475
- var _a;
1476
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1477
- });
1821
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1478
1822
  this._messenger = new import_messaging.Messenger({
1479
1823
  signalManager: this._signalManager
1480
1824
  });
@@ -1482,6 +1826,7 @@ var NetworkManager = class {
1482
1826
  join: (opts) => this._signalManager.join(opts),
1483
1827
  leave: (opts) => this._signalManager.leave(opts)
1484
1828
  };
1829
+ this._connectionLimiter = new ConnectionLimiter();
1485
1830
  if (log1) {
1486
1831
  this._connectionLog = new ConnectionLog();
1487
1832
  }
@@ -1504,21 +1849,21 @@ var NetworkManager = class {
1504
1849
  return this._swarms.get(topic);
1505
1850
  }
1506
1851
  async open() {
1507
- import_log6.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
1852
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.begin({
1508
1853
  id: this._instanceId
1509
1854
  }), {
1510
- F: __dxlog_file6,
1511
- L: 127,
1855
+ F: __dxlog_file7,
1856
+ L: 130,
1512
1857
  S: this,
1513
1858
  C: (f, a) => f(...a)
1514
1859
  });
1515
1860
  await this._messenger.open();
1516
1861
  await this._signalManager.open();
1517
- import_log6.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
1862
+ import_log7.log.trace("dxos.mesh.network-manager.open", import_protocols4.trace.end({
1518
1863
  id: this._instanceId
1519
1864
  }), {
1520
- F: __dxlog_file6,
1521
- L: 130,
1865
+ F: __dxlog_file7,
1866
+ L: 133,
1522
1867
  S: this,
1523
1868
  C: (f, a) => f(...a)
1524
1869
  });
@@ -1526,9 +1871,9 @@ var NetworkManager = class {
1526
1871
  async close() {
1527
1872
  for (const topic of this._swarms.keys()) {
1528
1873
  await this.leaveSwarm(topic).catch((err) => {
1529
- (0, import_log6.log)(err, void 0, {
1530
- F: __dxlog_file6,
1531
- L: 136,
1874
+ (0, import_log7.log)(err, void 0, {
1875
+ F: __dxlog_file7,
1876
+ L: 139,
1532
1877
  S: this,
1533
1878
  C: (f, a) => f(...a)
1534
1879
  });
@@ -1541,31 +1886,62 @@ var NetworkManager = class {
1541
1886
  * Join the swarm.
1542
1887
  */
1543
1888
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1544
- var _a;
1545
- (0, import_tiny_invariant5.default)(import_keys7.PublicKey.isPublicKey(topic));
1546
- (0, import_tiny_invariant5.default)(import_keys7.PublicKey.isPublicKey(peerId));
1547
- (0, import_tiny_invariant5.default)(topology);
1548
- (0, import_tiny_invariant5.default)(typeof protocol === "function");
1889
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1890
+ F: __dxlog_file7,
1891
+ L: 157,
1892
+ S: this,
1893
+ A: [
1894
+ "PublicKey.isPublicKey(topic)",
1895
+ ""
1896
+ ]
1897
+ });
1898
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
1899
+ F: __dxlog_file7,
1900
+ L: 158,
1901
+ S: this,
1902
+ A: [
1903
+ "PublicKey.isPublicKey(peerId)",
1904
+ ""
1905
+ ]
1906
+ });
1907
+ (0, import_invariant6.invariant)(topology, void 0, {
1908
+ F: __dxlog_file7,
1909
+ L: 159,
1910
+ S: this,
1911
+ A: [
1912
+ "topology",
1913
+ ""
1914
+ ]
1915
+ });
1916
+ (0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
1917
+ F: __dxlog_file7,
1918
+ L: 160,
1919
+ S: this,
1920
+ A: [
1921
+ "typeof protocol === 'function'",
1922
+ ""
1923
+ ]
1924
+ });
1549
1925
  if (this._swarms.has(topic)) {
1550
- throw new Error(`Already connected to swarm: ${import_keys7.PublicKey.from(topic)}`);
1926
+ throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
1551
1927
  }
1552
- (0, import_log6.log)("joining", {
1553
- topic: import_keys7.PublicKey.from(topic),
1928
+ (0, import_log7.log)("joining", {
1929
+ topic: import_keys8.PublicKey.from(topic),
1554
1930
  peerId,
1555
1931
  topology: topology.toString()
1556
1932
  }, {
1557
- F: __dxlog_file6,
1558
- L: 162,
1933
+ F: __dxlog_file7,
1934
+ L: 165,
1559
1935
  S: this,
1560
1936
  C: (f, a) => f(...a)
1561
1937
  });
1562
- const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label);
1938
+ const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
1563
1939
  swarm.errors.handle((error) => {
1564
- (0, import_log6.log)("swarm error", {
1940
+ (0, import_log7.log)("swarm error", {
1565
1941
  error
1566
1942
  }, {
1567
- F: __dxlog_file6,
1568
- L: 165,
1943
+ F: __dxlog_file7,
1944
+ L: 178,
1569
1945
  S: this,
1570
1946
  C: (f, a) => f(...a)
1571
1947
  });
@@ -1576,20 +1952,20 @@ var NetworkManager = class {
1576
1952
  this._signalConnection.join({
1577
1953
  topic,
1578
1954
  peerId
1579
- }).catch((error) => import_log6.log.catch(error, void 0, {
1580
- F: __dxlog_file6,
1581
- L: 174,
1955
+ }).catch((error) => import_log7.log.catch(error, void 0, {
1956
+ F: __dxlog_file7,
1957
+ L: 187,
1582
1958
  S: this,
1583
1959
  C: (f, a) => f(...a)
1584
1960
  }));
1585
1961
  this.topicsUpdated.emit();
1586
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1587
- (0, import_log6.log)("joined", {
1588
- topic: import_keys7.PublicKey.from(topic),
1962
+ this._connectionLog?.joinedSwarm(swarm);
1963
+ (0, import_log7.log)("joined", {
1964
+ topic: import_keys8.PublicKey.from(topic),
1589
1965
  count: this._swarms.size
1590
1966
  }, {
1591
- F: __dxlog_file6,
1592
- L: 178,
1967
+ F: __dxlog_file7,
1968
+ L: 191,
1593
1969
  S: this,
1594
1970
  C: (f, a) => f(...a)
1595
1971
  });
@@ -1601,15 +1977,14 @@ var NetworkManager = class {
1601
1977
  * Close the connection.
1602
1978
  */
1603
1979
  async leaveSwarm(topic) {
1604
- var _a;
1605
1980
  if (!this._swarms.has(topic)) {
1606
1981
  return;
1607
1982
  }
1608
- (0, import_log6.log)("leaving", {
1609
- topic: import_keys7.PublicKey.from(topic)
1983
+ (0, import_log7.log)("leaving", {
1984
+ topic: import_keys8.PublicKey.from(topic)
1610
1985
  }, {
1611
- F: __dxlog_file6,
1612
- L: 194,
1986
+ F: __dxlog_file7,
1987
+ L: 207,
1613
1988
  S: this,
1614
1989
  C: (f, a) => f(...a)
1615
1990
  });
@@ -1621,16 +1996,16 @@ var NetworkManager = class {
1621
1996
  const map = this._mappers.get(topic);
1622
1997
  map.destroy();
1623
1998
  this._mappers.delete(topic);
1624
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1999
+ this._connectionLog?.leftSwarm(swarm);
1625
2000
  await swarm.destroy();
1626
2001
  this._swarms.delete(topic);
1627
2002
  await this.topicsUpdated.emit();
1628
- (0, import_log6.log)("left", {
1629
- topic: import_keys7.PublicKey.from(topic),
2003
+ (0, import_log7.log)("left", {
2004
+ topic: import_keys8.PublicKey.from(topic),
1630
2005
  count: this._swarms.size
1631
2006
  }, {
1632
- F: __dxlog_file6,
1633
- L: 208,
2007
+ F: __dxlog_file7,
2008
+ L: 221,
1634
2009
  S: this,
1635
2010
  C: (f, a) => f(...a)
1636
2011
  });
@@ -1664,17 +2039,34 @@ var NetworkManager = class {
1664
2039
  };
1665
2040
 
1666
2041
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1667
- var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
2042
+ var import_invariant7 = require("@dxos/invariant");
2043
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1668
2044
  var FullyConnectedTopology = class {
1669
2045
  toString() {
1670
2046
  return "FullyConnectedTopology";
1671
2047
  }
1672
2048
  init(controller) {
1673
- (0, import_tiny_invariant6.default)(!this._controller, "Already initialized");
2049
+ (0, import_invariant7.invariant)(!this._controller, "Already initialized", {
2050
+ F: __dxlog_file8,
2051
+ L: 18,
2052
+ S: this,
2053
+ A: [
2054
+ "!this._controller",
2055
+ "'Already initialized'"
2056
+ ]
2057
+ });
1674
2058
  this._controller = controller;
1675
2059
  }
1676
2060
  update() {
1677
- (0, import_tiny_invariant6.default)(this._controller, "Not initialized");
2061
+ (0, import_invariant7.invariant)(this._controller, "Not initialized", {
2062
+ F: __dxlog_file8,
2063
+ L: 23,
2064
+ S: this,
2065
+ A: [
2066
+ "this._controller",
2067
+ "'Not initialized'"
2068
+ ]
2069
+ });
1678
2070
  const { candidates: discovered } = this._controller.getState();
1679
2071
  for (const peer of discovered) {
1680
2072
  this._controller.connect(peer);
@@ -1688,10 +2080,10 @@ var FullyConnectedTopology = class {
1688
2080
  };
1689
2081
 
1690
2082
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1691
- var import_tiny_invariant7 = __toESM(require("tiny-invariant"));
1692
2083
  var import_xor_distance = __toESM(require("xor-distance"));
1693
- var import_log7 = require("@dxos/log");
1694
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2084
+ var import_invariant8 = require("@dxos/invariant");
2085
+ var import_log8 = require("@dxos/log");
2086
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1695
2087
  var MMSTTopology = class {
1696
2088
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
1697
2089
  this._sampleCollected = false;
@@ -1700,15 +2092,31 @@ var MMSTTopology = class {
1700
2092
  this._sampleSize = sampleSize;
1701
2093
  }
1702
2094
  init(controller) {
1703
- (0, import_tiny_invariant7.default)(!this._controller, "Already initialized");
2095
+ (0, import_invariant8.invariant)(!this._controller, "Already initialized", {
2096
+ F: __dxlog_file9,
2097
+ L: 46,
2098
+ S: this,
2099
+ A: [
2100
+ "!this._controller",
2101
+ "'Already initialized'"
2102
+ ]
2103
+ });
1704
2104
  this._controller = controller;
1705
2105
  }
1706
2106
  update() {
1707
- (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
2107
+ (0, import_invariant8.invariant)(this._controller, "Not initialized", {
2108
+ F: __dxlog_file9,
2109
+ L: 51,
2110
+ S: this,
2111
+ A: [
2112
+ "this._controller",
2113
+ "'Not initialized'"
2114
+ ]
2115
+ });
1708
2116
  const { connected, candidates } = this._controller.getState();
1709
2117
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
1710
- (0, import_log7.log)("Running the algorithm.", void 0, {
1711
- F: __dxlog_file7,
2118
+ (0, import_log8.log)("Running the algorithm.", void 0, {
2119
+ F: __dxlog_file9,
1712
2120
  L: 55,
1713
2121
  S: this,
1714
2122
  C: (f, a) => f(...a)
@@ -1718,11 +2126,19 @@ var MMSTTopology = class {
1718
2126
  }
1719
2127
  }
1720
2128
  async onOffer(peer) {
1721
- (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
2129
+ (0, import_invariant8.invariant)(this._controller, "Not initialized", {
2130
+ F: __dxlog_file9,
2131
+ L: 62,
2132
+ S: this,
2133
+ A: [
2134
+ "this._controller",
2135
+ "'Not initialized'"
2136
+ ]
2137
+ });
1722
2138
  const { connected } = this._controller.getState();
1723
2139
  const accept = connected.length < this._maxPeers;
1724
- (0, import_log7.log)(`Offer ${peer} accept=${accept}`, void 0, {
1725
- F: __dxlog_file7,
2140
+ (0, import_log8.log)(`Offer ${peer} accept=${accept}`, void 0, {
2141
+ F: __dxlog_file9,
1726
2142
  L: 65,
1727
2143
  S: this,
1728
2144
  C: (f, a) => f(...a)
@@ -1732,13 +2148,21 @@ var MMSTTopology = class {
1732
2148
  async destroy() {
1733
2149
  }
1734
2150
  _runAlgorithm() {
1735
- (0, import_tiny_invariant7.default)(this._controller, "Not initialized");
2151
+ (0, import_invariant8.invariant)(this._controller, "Not initialized", {
2152
+ F: __dxlog_file9,
2153
+ L: 74,
2154
+ S: this,
2155
+ A: [
2156
+ "this._controller",
2157
+ "'Not initialized'"
2158
+ ]
2159
+ });
1736
2160
  const { connected, candidates, ownPeerId } = this._controller.getState();
1737
2161
  if (connected.length > this._maxPeers) {
1738
2162
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
1739
2163
  for (const peer of sorted) {
1740
- (0, import_log7.log)(`Disconnect ${peer}.`, void 0, {
1741
- F: __dxlog_file7,
2164
+ (0, import_log8.log)(`Disconnect ${peer}.`, void 0, {
2165
+ F: __dxlog_file9,
1742
2166
  L: 83,
1743
2167
  S: this,
1744
2168
  C: (f, a) => f(...a)
@@ -1749,8 +2173,8 @@ var MMSTTopology = class {
1749
2173
  const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
1750
2174
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
1751
2175
  for (const peer of sorted) {
1752
- (0, import_log7.log)(`Connect ${peer}.`, void 0, {
1753
- F: __dxlog_file7,
2176
+ (0, import_log8.log)(`Connect ${peer}.`, void 0, {
2177
+ F: __dxlog_file9,
1754
2178
  L: 91,
1755
2179
  S: this,
1756
2180
  C: (f, a) => f(...a)
@@ -1766,9 +2190,9 @@ var MMSTTopology = class {
1766
2190
  var sortByXorDistance = (keys, reference) => keys.sort((a, b) => import_xor_distance.default.gt((0, import_xor_distance.default)(a.asBuffer(), reference.asBuffer()), (0, import_xor_distance.default)(b.asBuffer(), reference.asBuffer())));
1767
2191
 
1768
2192
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1769
- var import_tiny_invariant8 = __toESM(require("tiny-invariant"));
1770
- var import_log8 = require("@dxos/log");
1771
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2193
+ var import_invariant9 = require("@dxos/invariant");
2194
+ var import_log9 = require("@dxos/log");
2195
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1772
2196
  var StarTopology = class {
1773
2197
  // prettier-ignore
1774
2198
  constructor(_centralPeer) {
@@ -1778,26 +2202,42 @@ var StarTopology = class {
1778
2202
  return `StarTopology(${this._centralPeer.truncate()})`;
1779
2203
  }
1780
2204
  init(controller) {
1781
- (0, import_tiny_invariant8.default)(!this._controller, "Already initialized.");
2205
+ (0, import_invariant9.invariant)(!this._controller, "Already initialized.", {
2206
+ F: __dxlog_file10,
2207
+ L: 24,
2208
+ S: this,
2209
+ A: [
2210
+ "!this._controller",
2211
+ "'Already initialized.'"
2212
+ ]
2213
+ });
1782
2214
  this._controller = controller;
1783
2215
  }
1784
2216
  update() {
1785
- (0, import_tiny_invariant8.default)(this._controller, "Not initialized.");
2217
+ (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2218
+ F: __dxlog_file10,
2219
+ L: 29,
2220
+ S: this,
2221
+ A: [
2222
+ "this._controller",
2223
+ "'Not initialized.'"
2224
+ ]
2225
+ });
1786
2226
  const { candidates, connected, ownPeerId } = this._controller.getState();
1787
2227
  if (!ownPeerId.equals(this._centralPeer)) {
1788
- (0, import_log8.log)("leaf peer dropping all connections apart from central peer.", void 0, {
1789
- F: __dxlog_file8,
1790
- L: 33,
2228
+ (0, import_log9.log)("leaf peer dropping all connections apart from central peer.", void 0, {
2229
+ F: __dxlog_file10,
2230
+ L: 32,
1791
2231
  S: this,
1792
2232
  C: (f, a) => f(...a)
1793
2233
  });
1794
2234
  for (const peer of connected) {
1795
2235
  if (!peer.equals(this._centralPeer)) {
1796
- (0, import_log8.log)("dropping connection", {
2236
+ (0, import_log9.log)("dropping connection", {
1797
2237
  peer
1798
2238
  }, {
1799
- F: __dxlog_file8,
1800
- L: 38,
2239
+ F: __dxlog_file10,
2240
+ L: 37,
1801
2241
  S: this,
1802
2242
  C: (f, a) => f(...a)
1803
2243
  });
@@ -1807,11 +2247,11 @@ var StarTopology = class {
1807
2247
  }
1808
2248
  for (const peer of candidates) {
1809
2249
  if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
1810
- (0, import_log8.log)("connecting to peer", {
2250
+ (0, import_log9.log)("connecting to peer", {
1811
2251
  peer
1812
2252
  }, {
1813
- F: __dxlog_file8,
1814
- L: 47,
2253
+ F: __dxlog_file10,
2254
+ L: 46,
1815
2255
  S: this,
1816
2256
  C: (f, a) => f(...a)
1817
2257
  });
@@ -1820,15 +2260,23 @@ var StarTopology = class {
1820
2260
  }
1821
2261
  }
1822
2262
  async onOffer(peer) {
1823
- (0, import_tiny_invariant8.default)(this._controller, "Not initialized.");
2263
+ (0, import_invariant9.invariant)(this._controller, "Not initialized.", {
2264
+ F: __dxlog_file10,
2265
+ L: 53,
2266
+ S: this,
2267
+ A: [
2268
+ "this._controller",
2269
+ "'Not initialized.'"
2270
+ ]
2271
+ });
1824
2272
  const { ownPeerId } = this._controller.getState();
1825
- (0, import_log8.log)("offer", {
2273
+ (0, import_log9.log)("offer", {
1826
2274
  peer,
1827
2275
  isCentral: peer.equals(this._centralPeer),
1828
2276
  isSelfCentral: ownPeerId.equals(this._centralPeer)
1829
2277
  }, {
1830
- F: __dxlog_file8,
1831
- L: 56,
2278
+ F: __dxlog_file10,
2279
+ L: 55,
1832
2280
  S: this,
1833
2281
  C: (f, a) => f(...a)
1834
2282
  });
@@ -1840,12 +2288,12 @@ var StarTopology = class {
1840
2288
 
1841
2289
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1842
2290
  var import_node_stream = require("node:stream");
1843
- var import_tiny_invariant9 = __toESM(require("tiny-invariant"));
1844
- var import_async7 = require("@dxos/async");
2291
+ var import_async8 = require("@dxos/async");
1845
2292
  var import_debug4 = require("@dxos/debug");
1846
- var import_keys8 = require("@dxos/keys");
1847
- var import_log9 = require("@dxos/log");
1848
- var import_util6 = require("@dxos/util");
2293
+ var import_invariant10 = require("@dxos/invariant");
2294
+ var import_keys9 = require("@dxos/keys");
2295
+ var import_log10 = require("@dxos/log");
2296
+ var import_util7 = require("@dxos/util");
1849
2297
  function _ts_decorate4(decorators, target, key, desc) {
1850
2298
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1851
2299
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1856,7 +2304,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1856
2304
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1857
2305
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1858
2306
  }
1859
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2307
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1860
2308
  var MEMORY_TRANSPORT_DELAY = 1;
1861
2309
  var createStreamDelay = (delay) => {
1862
2310
  return new import_node_stream.Transform({
@@ -1869,30 +2317,41 @@ var createStreamDelay = (delay) => {
1869
2317
  var MemoryTransportFactory = {
1870
2318
  createTransport: (params) => new MemoryTransport(params)
1871
2319
  };
1872
- var _MemoryTransport = class {
2320
+ var MemoryTransport = class _MemoryTransport {
2321
+ static {
2322
+ // TODO(burdon): Remove static properties (inject context into constructor).
2323
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2324
+ }
1873
2325
  constructor(options) {
1874
- var _a;
1875
2326
  this.options = options;
1876
- this.closed = new import_async7.Event();
1877
- this.connected = new import_async7.Event();
2327
+ this.closed = new import_async8.Event();
2328
+ this.connected = new import_async8.Event();
1878
2329
  this.errors = new import_debug4.ErrorStream();
1879
- this._instanceId = import_keys8.PublicKey.random();
1880
- this._remote = new import_async7.Trigger();
2330
+ this._instanceId = import_keys9.PublicKey.random();
2331
+ this._remote = new import_async8.Trigger();
1881
2332
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1882
2333
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1883
2334
  this._destroyed = false;
1884
- (0, import_log9.log)("creating", void 0, {
1885
- F: __dxlog_file9,
2335
+ (0, import_log10.log)("creating", void 0, {
2336
+ F: __dxlog_file11,
1886
2337
  L: 64,
1887
2338
  S: this,
1888
2339
  C: (f, a) => f(...a)
1889
2340
  });
1890
- (0, import_tiny_invariant9.default)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2341
+ (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2342
+ F: __dxlog_file11,
2343
+ L: 66,
2344
+ S: this,
2345
+ A: [
2346
+ "!MemoryTransport._connections.has(this._instanceId)",
2347
+ "'Duplicate memory connection'"
2348
+ ]
2349
+ });
1891
2350
  _MemoryTransport._connections.set(this._instanceId, this);
1892
2351
  if (this.options.initiator) {
1893
2352
  setTimeout(async () => {
1894
- (0, import_log9.log)("sending signal", void 0, {
1895
- F: __dxlog_file9,
2353
+ (0, import_log10.log)("sending signal", void 0, {
2354
+ F: __dxlog_file11,
1896
2355
  L: 73,
1897
2356
  S: this,
1898
2357
  C: (f, a) => f(...a)
@@ -1909,7 +2368,7 @@ var _MemoryTransport = class {
1909
2368
  });
1910
2369
  } else {
1911
2370
  this._remote.wait({
1912
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2371
+ timeout: this.options.timeout ?? 1e3
1913
2372
  }).then((remoteId) => {
1914
2373
  if (this._destroyed) {
1915
2374
  return;
@@ -1921,11 +2380,19 @@ var _MemoryTransport = class {
1921
2380
  this.closed.emit();
1922
2381
  return;
1923
2382
  }
1924
- (0, import_tiny_invariant9.default)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2383
+ (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2384
+ F: __dxlog_file11,
2385
+ L: 99,
2386
+ S: this,
2387
+ A: [
2388
+ "!this._remoteConnection._remoteConnection",
2389
+ "`Remote already connected: ${this._remoteInstanceId}`"
2390
+ ]
2391
+ });
1925
2392
  this._remoteConnection._remoteConnection = this;
1926
2393
  this._remoteConnection._remoteInstanceId = this._instanceId;
1927
- (0, import_log9.log)("connected", void 0, {
1928
- F: __dxlog_file9,
2394
+ (0, import_log10.log)("connected", void 0, {
2395
+ F: __dxlog_file11,
1929
2396
  L: 103,
1930
2397
  S: this,
1931
2398
  C: (f, a) => f(...a)
@@ -1942,8 +2409,8 @@ var _MemoryTransport = class {
1942
2409
  }
1943
2410
  }
1944
2411
  async destroy() {
1945
- (0, import_log9.log)("closing", void 0, {
1946
- F: __dxlog_file9,
2412
+ (0, import_log10.log)("closing", void 0, {
2413
+ F: __dxlog_file11,
1947
2414
  L: 124,
1948
2415
  S: this,
1949
2416
  C: (f, a) => f(...a)
@@ -1951,8 +2418,8 @@ var _MemoryTransport = class {
1951
2418
  this._destroyed = true;
1952
2419
  _MemoryTransport._connections.delete(this._instanceId);
1953
2420
  if (this._remoteConnection) {
1954
- (0, import_log9.log)("closing", void 0, {
1955
- F: __dxlog_file9,
2421
+ (0, import_log10.log)("closing", void 0, {
2422
+ F: __dxlog_file11,
1956
2423
  L: 129,
1957
2424
  S: this,
1958
2425
  C: (f, a) => f(...a)
@@ -1964,99 +2431,95 @@ var _MemoryTransport = class {
1964
2431
  this._remoteConnection.closed.emit();
1965
2432
  this._remoteConnection._remoteConnection = void 0;
1966
2433
  this._remoteConnection = void 0;
1967
- (0, import_log9.log)("closed", void 0, {
1968
- F: __dxlog_file9,
2434
+ (0, import_log10.log)("closed", void 0, {
2435
+ F: __dxlog_file11,
1969
2436
  L: 147,
1970
2437
  S: this,
1971
2438
  C: (f, a) => f(...a)
1972
2439
  });
1973
2440
  }
1974
2441
  this.closed.emit();
1975
- (0, import_log9.log)("closed", void 0, {
1976
- F: __dxlog_file9,
2442
+ (0, import_log10.log)("closed", void 0, {
2443
+ F: __dxlog_file11,
1977
2444
  L: 151,
1978
2445
  S: this,
1979
2446
  C: (f, a) => f(...a)
1980
2447
  });
1981
2448
  }
1982
2449
  signal({ payload }) {
1983
- (0, import_log9.log)("received signal", {
2450
+ (0, import_log10.log)("received signal", {
1984
2451
  payload
1985
2452
  }, {
1986
- F: __dxlog_file9,
2453
+ F: __dxlog_file11,
1987
2454
  L: 155,
1988
2455
  S: this,
1989
2456
  C: (f, a) => f(...a)
1990
2457
  });
1991
- if (!(payload == null ? void 0 : payload.transportId)) {
2458
+ if (!payload?.transportId) {
1992
2459
  return;
1993
2460
  }
1994
2461
  const transportId = payload.transportId;
1995
2462
  if (transportId) {
1996
- const remoteId = import_keys8.PublicKey.fromHex(transportId);
2463
+ const remoteId = import_keys9.PublicKey.fromHex(transportId);
1997
2464
  this._remote.wake(remoteId);
1998
2465
  }
1999
2466
  }
2000
2467
  };
2001
- var MemoryTransport = _MemoryTransport;
2002
- // TODO(burdon): Remove static properties (inject context into constructor).
2003
- MemoryTransport._connections = new import_util6.ComplexMap(import_keys8.PublicKey.hash);
2004
2468
  _ts_decorate4([
2005
- import_log9.logInfo
2469
+ import_log10.logInfo
2006
2470
  ], MemoryTransport.prototype, "_instanceId", void 0);
2007
2471
  _ts_decorate4([
2008
- import_log9.logInfo
2472
+ import_log10.logInfo
2009
2473
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2010
2474
 
2011
2475
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2012
2476
  var import_simple_peer = __toESM(require("simple-peer"));
2013
- var import_tiny_invariant10 = __toESM(require("tiny-invariant"));
2014
- var import_async8 = require("@dxos/async");
2477
+ var import_async9 = require("@dxos/async");
2015
2478
  var import_debug5 = require("@dxos/debug");
2016
- var import_keys9 = require("@dxos/keys");
2017
- var import_log10 = require("@dxos/log");
2479
+ var import_invariant11 = require("@dxos/invariant");
2480
+ var import_keys10 = require("@dxos/keys");
2481
+ var import_log11 = require("@dxos/log");
2018
2482
  var import_protocols5 = require("@dxos/protocols");
2019
2483
 
2020
2484
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
2021
2485
  var wrtc = null;
2022
2486
  try {
2023
2487
  wrtc = require("@koush/wrtc");
2024
- } catch (e) {
2488
+ } catch {
2025
2489
  }
2026
2490
 
2027
2491
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2028
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2492
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2029
2493
  var WebRTCTransport = class {
2030
2494
  constructor(params) {
2031
- var _a;
2032
2495
  this.params = params;
2033
2496
  this._closed = false;
2034
- this.closed = new import_async8.Event();
2035
- this.connected = new import_async8.Event();
2497
+ this.closed = new import_async9.Event();
2498
+ this.connected = new import_async9.Event();
2036
2499
  this.errors = new import_debug5.ErrorStream();
2037
- this._instanceId = import_keys9.PublicKey.random().toHex();
2038
- import_log10.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2500
+ this._instanceId = import_keys10.PublicKey.random().toHex();
2501
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
2039
2502
  id: this._instanceId
2040
2503
  }), {
2041
- F: __dxlog_file10,
2504
+ F: __dxlog_file12,
2042
2505
  L: 39,
2043
2506
  S: this,
2044
2507
  C: (f, a) => f(...a)
2045
2508
  });
2046
- (0, import_log10.log)("created connection", params, {
2047
- F: __dxlog_file10,
2509
+ (0, import_log11.log)("created connection", params, {
2510
+ F: __dxlog_file12,
2048
2511
  L: 40,
2049
2512
  S: this,
2050
2513
  C: (f, a) => f(...a)
2051
2514
  });
2052
2515
  this._peer = new import_simple_peer.default({
2053
2516
  initiator: this.params.initiator,
2054
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : (0, import_debug5.raise)(new Error("wrtc not available")),
2517
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available")),
2055
2518
  config: this.params.webrtcConfig
2056
2519
  });
2057
2520
  this._peer.on("signal", async (data) => {
2058
- (0, import_log10.log)("signal", data, {
2059
- F: __dxlog_file10,
2521
+ (0, import_log11.log)("signal", data, {
2522
+ F: __dxlog_file12,
2060
2523
  L: 48,
2061
2524
  S: this,
2062
2525
  C: (f, a) => f(...a)
@@ -2068,8 +2531,8 @@ var WebRTCTransport = class {
2068
2531
  });
2069
2532
  });
2070
2533
  this._peer.on("connect", () => {
2071
- (0, import_log10.log)("connected", void 0, {
2072
- F: __dxlog_file10,
2534
+ (0, import_log11.log)("connected", void 0, {
2535
+ F: __dxlog_file12,
2073
2536
  L: 53,
2074
2537
  S: this,
2075
2538
  C: (f, a) => f(...a)
@@ -2078,8 +2541,8 @@ var WebRTCTransport = class {
2078
2541
  this.connected.emit();
2079
2542
  });
2080
2543
  this._peer.on("close", async () => {
2081
- (0, import_log10.log)("closed", void 0, {
2082
- F: __dxlog_file10,
2544
+ (0, import_log11.log)("closed", void 0, {
2545
+ F: __dxlog_file12,
2083
2546
  L: 59,
2084
2547
  S: this,
2085
2548
  C: (f, a) => f(...a)
@@ -2088,38 +2551,37 @@ var WebRTCTransport = class {
2088
2551
  this.closed.emit();
2089
2552
  });
2090
2553
  this._peer.on("error", async (err) => {
2091
- var _a2;
2092
2554
  this.errors.raise(err);
2093
2555
  try {
2094
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2556
+ if (typeof this._peer?._pc.getStats === "function") {
2095
2557
  this._peer._pc.getStats().then((stats) => {
2096
- import_log10.log.warn("report after webrtc error", {
2558
+ import_log11.log.warn("report after webrtc error", {
2097
2559
  config: this.params.webrtcConfig,
2098
2560
  stats
2099
2561
  }, {
2100
- F: __dxlog_file10,
2562
+ F: __dxlog_file12,
2101
2563
  L: 71,
2102
2564
  S: this,
2103
2565
  C: (f, a) => f(...a)
2104
2566
  });
2105
2567
  });
2106
2568
  }
2107
- } catch (e) {
2569
+ } catch {
2108
2570
  }
2109
2571
  await this.destroy();
2110
2572
  });
2111
- import_log10.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2573
+ import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2112
2574
  id: this._instanceId
2113
2575
  }), {
2114
- F: __dxlog_file10,
2576
+ F: __dxlog_file12,
2115
2577
  L: 81,
2116
2578
  S: this,
2117
2579
  C: (f, a) => f(...a)
2118
2580
  });
2119
2581
  }
2120
2582
  async destroy() {
2121
- (0, import_log10.log)("closing...", void 0, {
2122
- F: __dxlog_file10,
2583
+ (0, import_log11.log)("closing...", void 0, {
2584
+ F: __dxlog_file12,
2123
2585
  L: 85,
2124
2586
  S: this,
2125
2587
  C: (f, a) => f(...a)
@@ -2128,8 +2590,8 @@ var WebRTCTransport = class {
2128
2590
  await this._disconnectStreams();
2129
2591
  this._peer.destroy();
2130
2592
  this.closed.emit();
2131
- (0, import_log10.log)("closed", void 0, {
2132
- F: __dxlog_file10,
2593
+ (0, import_log11.log)("closed", void 0, {
2594
+ F: __dxlog_file12,
2133
2595
  L: 90,
2134
2596
  S: this,
2135
2597
  C: (f, a) => f(...a)
@@ -2139,12 +2601,19 @@ var WebRTCTransport = class {
2139
2601
  if (this._closed) {
2140
2602
  return;
2141
2603
  }
2142
- (0, import_tiny_invariant10.default)(signal.payload.data, "Signal message must contain signal data.");
2604
+ (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2605
+ F: __dxlog_file12,
2606
+ L: 98,
2607
+ S: this,
2608
+ A: [
2609
+ "signal.payload.data",
2610
+ "'Signal message must contain signal data.'"
2611
+ ]
2612
+ });
2143
2613
  this._peer.signal(signal.payload.data);
2144
2614
  }
2145
2615
  async _disconnectStreams() {
2146
- var _a, _b, _c, _d;
2147
- (_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);
2616
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2148
2617
  }
2149
2618
  };
2150
2619
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2156,23 +2625,30 @@ var createWebRTCTransportFactory = (webrtcConfig) => ({
2156
2625
 
2157
2626
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2158
2627
  var import_node_stream2 = require("node:stream");
2159
- var import_tiny_invariant11 = __toESM(require("tiny-invariant"));
2160
2628
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2161
- var import_keys10 = require("@dxos/keys");
2162
- var import_log11 = require("@dxos/log");
2629
+ var import_invariant12 = require("@dxos/invariant");
2630
+ var import_keys11 = require("@dxos/keys");
2631
+ var import_log12 = require("@dxos/log");
2163
2632
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2164
- var import_util7 = require("@dxos/util");
2165
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2633
+ var import_util8 = require("@dxos/util");
2634
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2166
2635
  var WebRTCTransportService = class {
2167
2636
  // prettier-ignore
2168
2637
  constructor(_webrtcConfig) {
2169
2638
  this._webrtcConfig = _webrtcConfig;
2170
- this.transports = new import_util7.ComplexMap(import_keys10.PublicKey.hash);
2639
+ this.transports = new import_util8.ComplexMap(import_keys11.PublicKey.hash);
2171
2640
  }
2172
2641
  open(request) {
2173
2642
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
2174
2643
  const duplex = new import_node_stream2.Duplex({
2175
2644
  read: () => {
2645
+ const callbacks = [
2646
+ ...transportState.writeCallbacks
2647
+ ];
2648
+ transportState.writeCallbacks.length = 0;
2649
+ for (const cb of callbacks) {
2650
+ cb();
2651
+ }
2176
2652
  },
2177
2653
  write: function(chunk, _, callback) {
2178
2654
  next({
@@ -2224,30 +2700,53 @@ var WebRTCTransportService = class {
2224
2700
  });
2225
2701
  close();
2226
2702
  });
2227
- ready();
2228
- this.transports.set(request.proxyId, {
2703
+ const transportState = {
2229
2704
  transport,
2230
- stream: duplex
2231
- });
2705
+ stream: duplex,
2706
+ writeCallbacks: []
2707
+ };
2708
+ ready();
2709
+ this.transports.set(request.proxyId, transportState);
2232
2710
  });
2233
2711
  return rpcStream;
2234
2712
  }
2235
2713
  async sendSignal({ proxyId, signal }) {
2236
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2714
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2715
+ F: __dxlog_file13,
2716
+ L: 113,
2717
+ S: this,
2718
+ A: [
2719
+ "this.transports.has(proxyId)",
2720
+ ""
2721
+ ]
2722
+ });
2237
2723
  await this.transports.get(proxyId).transport.signal(signal);
2238
2724
  }
2239
2725
  async sendData({ proxyId, payload }) {
2240
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2241
- await this.transports.get(proxyId).stream.push(payload);
2726
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2727
+ F: __dxlog_file13,
2728
+ L: 118,
2729
+ S: this,
2730
+ A: [
2731
+ "this.transports.has(proxyId)",
2732
+ ""
2733
+ ]
2734
+ });
2735
+ const state = this.transports.get(proxyId);
2736
+ const bufferHasSpace = state.stream.push(payload);
2737
+ if (!bufferHasSpace) {
2738
+ await new Promise((resolve) => {
2739
+ state.writeCallbacks.push(resolve);
2740
+ });
2741
+ }
2242
2742
  }
2243
2743
  async close({ proxyId }) {
2244
- var _a, _b;
2245
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2246
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2744
+ await this.transports.get(proxyId)?.transport.destroy();
2745
+ await this.transports.get(proxyId)?.stream.end();
2247
2746
  this.transports.delete(proxyId);
2248
- (0, import_log11.log)("Closed.", void 0, {
2249
- F: __dxlog_file11,
2250
- L: 109,
2747
+ (0, import_log12.log)("Closed.", void 0, {
2748
+ F: __dxlog_file13,
2749
+ L: 132,
2251
2750
  S: this,
2252
2751
  C: (f, a) => f(...a)
2253
2752
  });
@@ -2255,24 +2754,25 @@ var WebRTCTransportService = class {
2255
2754
  };
2256
2755
 
2257
2756
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2258
- var import_tiny_invariant12 = __toESM(require("tiny-invariant"));
2259
- var import_async9 = require("@dxos/async");
2260
- var import_context5 = require("@dxos/context");
2757
+ var import_node_stream3 = require("node:stream");
2758
+ var import_async10 = require("@dxos/async");
2759
+ var import_context6 = require("@dxos/context");
2261
2760
  var import_debug6 = require("@dxos/debug");
2262
- var import_keys11 = require("@dxos/keys");
2263
- var import_log12 = require("@dxos/log");
2761
+ var import_invariant13 = require("@dxos/invariant");
2762
+ var import_keys12 = require("@dxos/keys");
2763
+ var import_log13 = require("@dxos/log");
2264
2764
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2265
- var import_util8 = require("@dxos/util");
2266
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2765
+ var import_util9 = require("@dxos/util");
2766
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2267
2767
  var WebRTCTransportProxy = class {
2268
2768
  // prettier-ignore
2269
2769
  constructor(_params) {
2270
2770
  this._params = _params;
2271
- this._proxyId = import_keys11.PublicKey.random();
2272
- this._ctx = new import_context5.Context();
2273
- this.closed = new import_async9.Event();
2771
+ this._proxyId = import_keys12.PublicKey.random();
2772
+ this._ctx = new import_context6.Context();
2773
+ this.closed = new import_async10.Event();
2274
2774
  this._closed = false;
2275
- this.connected = new import_async9.Event();
2775
+ this.connected = new import_async10.Event();
2276
2776
  this.errors = new import_debug6.ErrorStream();
2277
2777
  this._serviceStream = this._params.bridgeService.open({
2278
2778
  proxyId: this._proxyId,
@@ -2280,9 +2780,9 @@ var WebRTCTransportProxy = class {
2280
2780
  });
2281
2781
  this._serviceStream.waitUntilReady().then(() => {
2282
2782
  this._serviceStream.subscribe(async (event) => {
2283
- (0, import_log12.log)("WebRTCTransportProxy: event", event, {
2284
- F: __dxlog_file12,
2285
- L: 50,
2783
+ (0, import_log13.log)("WebRTCTransportProxy: event", event, {
2784
+ F: __dxlog_file14,
2785
+ L: 51,
2286
2786
  S: this,
2287
2787
  C: (f, a) => f(...a)
2288
2788
  });
@@ -2294,28 +2794,24 @@ var WebRTCTransportProxy = class {
2294
2794
  await this._handleSignal(event.signal);
2295
2795
  }
2296
2796
  });
2297
- const dataListener = async (data) => {
2298
- try {
2299
- await this._params.bridgeService.sendData({
2797
+ this._params.stream.pipe(new import_node_stream3.Writable({
2798
+ write: (chunk, _, callback) => {
2799
+ this._params.bridgeService.sendData({
2300
2800
  proxyId: this._proxyId,
2301
- payload: data
2302
- });
2303
- } catch (err) {
2304
- import_log12.log.catch(err, void 0, {
2305
- F: __dxlog_file12,
2306
- L: 67,
2307
- S: this,
2308
- C: (f, a) => f(...a)
2801
+ payload: chunk
2802
+ }).then(() => callback(), (err) => {
2803
+ import_log13.log.catch(err, void 0, {
2804
+ F: __dxlog_file14,
2805
+ L: 69,
2806
+ S: this,
2807
+ C: (f, a) => f(...a)
2808
+ });
2309
2809
  });
2310
2810
  }
2311
- };
2312
- this._params.stream.on("data", dataListener);
2313
- this._ctx.onDispose(() => {
2314
- this._params.stream.off("data", dataListener);
2315
- });
2316
- }, (error) => import_log12.log.catch(error, void 0, {
2317
- F: __dxlog_file12,
2318
- L: 73,
2811
+ }));
2812
+ }, (error) => import_log13.log.catch(error, void 0, {
2813
+ F: __dxlog_file14,
2814
+ L: 75,
2319
2815
  S: this,
2320
2816
  C: (f, a) => f(...a)
2321
2817
  }));
@@ -2336,7 +2832,7 @@ var WebRTCTransportProxy = class {
2336
2832
  }
2337
2833
  }
2338
2834
  _handleData(dataEvent) {
2339
- this._params.stream.write((0, import_util8.arrayToBuffer)(dataEvent.payload));
2835
+ this._params.stream.write((0, import_util9.arrayToBuffer)(dataEvent.payload));
2340
2836
  }
2341
2837
  async _handleSignal(signalEvent) {
2342
2838
  await this._params.sendSignal(signalEvent.payload);
@@ -2359,9 +2855,9 @@ var WebRTCTransportProxy = class {
2359
2855
  proxyId: this._proxyId
2360
2856
  });
2361
2857
  } catch (err) {
2362
- import_log12.log.catch(err, void 0, {
2363
- F: __dxlog_file12,
2364
- L: 124,
2858
+ import_log13.log.catch(err, void 0, {
2859
+ F: __dxlog_file14,
2860
+ L: 126,
2365
2861
  S: this,
2366
2862
  C: (f, a) => f(...a)
2367
2863
  });
@@ -2395,7 +2891,15 @@ var WebRTCTransportProxyFactory = class {
2395
2891
  return this;
2396
2892
  }
2397
2893
  createTransport(options) {
2398
- (0, import_tiny_invariant12.default)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2894
+ (0, import_invariant13.invariant)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2895
+ F: __dxlog_file14,
2896
+ L: 163,
2897
+ S: this,
2898
+ A: [
2899
+ "this._bridgeService",
2900
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2901
+ ]
2902
+ });
2399
2903
  const transport = new WebRTCTransportProxy({
2400
2904
  ...options,
2401
2905
  bridgeService: this._bridgeService
@@ -2426,10 +2930,12 @@ var createTeleportProtocolFactory = (onConnection) => {
2426
2930
  // Annotate the CommonJS export names for ESM import in node:
2427
2931
  0 && (module.exports = {
2428
2932
  Connection,
2933
+ ConnectionLimiter,
2429
2934
  ConnectionLog,
2430
2935
  ConnectionState,
2431
2936
  EventType,
2432
2937
  FullyConnectedTopology,
2938
+ MAX_CONCURRENT_INITIATING_CONNECTIONS,
2433
2939
  MMSTTopology,
2434
2940
  MemoryTransport,
2435
2941
  MemoryTransportFactory,