@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
@@ -4,15 +4,15 @@ 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
11
- import invariant from "tiny-invariant";
12
11
  import { DeferredTask, Event, sleep, synchronized } from "@dxos/async";
13
12
  import { Context, cancelWithContext } from "@dxos/context";
14
13
  import { ErrorStream } from "@dxos/debug";
15
14
  import { CancelledError } from "@dxos/errors";
15
+ import { invariant } from "@dxos/invariant";
16
16
  import { PublicKey } from "@dxos/keys";
17
17
  import { log } from "@dxos/log";
18
18
  import { trace } from "@dxos/protocols";
@@ -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: 95,
96
+ S: this,
97
+ C: (f, a) => f(...a)
98
+ });
87
99
  }
88
100
  get state() {
89
101
  return this._state;
@@ -97,14 +109,33 @@ var Connection = class {
97
109
  /**
98
110
  * Create an underlying transport and prepares it for the connection.
99
111
  */
100
- // TODO(burdon): Make async?
101
- openConnection() {
102
- invariant(this._state === ConnectionState.INITIAL, "Invalid state.");
112
+ async openConnection() {
113
+ invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
114
+ F: __dxlog_file,
115
+ L: 120,
116
+ S: this,
117
+ A: [
118
+ "this._state === ConnectionState.INITIAL",
119
+ "'Invalid state.'"
120
+ ]
121
+ });
103
122
  log.trace("dxos.mesh.connection.open-connection", trace.begin({
104
123
  id: this._instanceId
105
124
  }), {
106
125
  F: __dxlog_file,
107
- L: 115,
126
+ L: 121,
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: 122,
108
139
  S: this,
109
140
  C: (f, a) => f(...a)
110
141
  });
@@ -115,13 +146,21 @@ var Connection = class {
115
146
  this._protocol.stream.on("close", () => {
116
147
  log("protocol stream closed", void 0, {
117
148
  F: __dxlog_file,
118
- L: 126,
149
+ L: 139,
119
150
  S: this,
120
151
  C: (f, a) => f(...a)
121
152
  });
122
153
  this.close().catch((err) => this.errors.raise(err));
123
154
  });
124
- invariant(!this._transport);
155
+ invariant(!this._transport, void 0, {
156
+ F: __dxlog_file,
157
+ L: 143,
158
+ S: this,
159
+ A: [
160
+ "!this._transport",
161
+ ""
162
+ ]
163
+ });
125
164
  this._transport = this._transportFactory.createTransport({
126
165
  initiator: this.initiator,
127
166
  stream: this._protocol.stream,
@@ -147,13 +186,12 @@ var Connection = class {
147
186
  id: this._instanceId
148
187
  }), {
149
188
  F: __dxlog_file,
150
- L: 159,
189
+ L: 172,
151
190
  S: this,
152
191
  C: (f, a) => f(...a)
153
192
  });
154
193
  }
155
194
  async close() {
156
- var _a;
157
195
  if (this._state === ConnectionState.CLOSED) {
158
196
  return;
159
197
  }
@@ -163,7 +201,7 @@ var Connection = class {
163
201
  peerId: this.ownId
164
202
  }, {
165
203
  F: __dxlog_file,
166
- L: 171,
204
+ L: 184,
167
205
  S: this,
168
206
  C: (f, a) => f(...a)
169
207
  });
@@ -172,17 +210,17 @@ var Connection = class {
172
210
  } catch (err) {
173
211
  log.catch(err, void 0, {
174
212
  F: __dxlog_file,
175
- L: 177,
213
+ L: 190,
176
214
  S: this,
177
215
  C: (f, a) => f(...a)
178
216
  });
179
217
  }
180
218
  try {
181
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
219
+ await this._transport?.destroy();
182
220
  } catch (err) {
183
221
  log.catch(err, void 0, {
184
222
  F: __dxlog_file,
185
- L: 184,
223
+ L: 197,
186
224
  S: this,
187
225
  C: (f, a) => f(...a)
188
226
  });
@@ -191,7 +229,7 @@ var Connection = class {
191
229
  peerId: this.ownId
192
230
  }, {
193
231
  F: __dxlog_file,
194
- L: 187,
232
+ L: 200,
195
233
  S: this,
196
234
  C: (f, a) => f(...a)
197
235
  });
@@ -231,7 +269,7 @@ var Connection = class {
231
269
  err
232
270
  }, {
233
271
  F: __dxlog_file,
234
- L: 221,
272
+ L: 234,
235
273
  S: this,
236
274
  C: (f, a) => f(...a)
237
275
  });
@@ -242,21 +280,52 @@ var Connection = class {
242
280
  * Receive a signal from the remote peer.
243
281
  */
244
282
  async signal(msg) {
245
- var _a, _b, _c;
246
- invariant(msg.sessionId);
283
+ invariant(msg.sessionId, void 0, {
284
+ F: __dxlog_file,
285
+ L: 243,
286
+ S: this,
287
+ A: [
288
+ "msg.sessionId",
289
+ ""
290
+ ]
291
+ });
247
292
  if (!msg.sessionId.equals(this.sessionId)) {
248
293
  log("dropping signal for incorrect session id", void 0, {
249
294
  F: __dxlog_file,
250
- L: 232,
295
+ L: 245,
251
296
  S: this,
252
297
  C: (f, a) => f(...a)
253
298
  });
254
299
  return;
255
300
  }
256
- invariant(msg.data.signal || msg.data.signalBatch);
257
- invariant((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
258
- invariant((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
259
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
301
+ invariant(msg.data.signal || msg.data.signalBatch, void 0, {
302
+ F: __dxlog_file,
303
+ L: 248,
304
+ S: this,
305
+ A: [
306
+ "msg.data.signal || msg.data.signalBatch",
307
+ ""
308
+ ]
309
+ });
310
+ invariant(msg.author?.equals(this.remoteId), void 0, {
311
+ F: __dxlog_file,
312
+ L: 249,
313
+ S: this,
314
+ A: [
315
+ "msg.author?.equals(this.remoteId)",
316
+ ""
317
+ ]
318
+ });
319
+ invariant(msg.recipient?.equals(this.ownId), void 0, {
320
+ F: __dxlog_file,
321
+ L: 250,
322
+ S: this,
323
+ A: [
324
+ "msg.recipient?.equals(this.ownId)",
325
+ ""
326
+ ]
327
+ });
328
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
260
329
  msg.data.signal
261
330
  ];
262
331
  for (const signal of signals) {
@@ -270,20 +339,28 @@ var Connection = class {
270
339
  msg: msg.data
271
340
  }, {
272
341
  F: __dxlog_file,
273
- L: 246,
342
+ L: 259,
274
343
  S: this,
275
344
  C: (f, a) => f(...a)
276
345
  });
277
346
  this._incomingSignalBuffer.push(signal);
278
347
  } else {
279
- invariant(this._transport, "Connection not ready to accept signals.");
348
+ invariant(this._transport, "Connection not ready to accept signals.", {
349
+ F: __dxlog_file,
350
+ L: 262,
351
+ S: this,
352
+ A: [
353
+ "this._transport",
354
+ "'Connection not ready to accept signals.'"
355
+ ]
356
+ });
280
357
  log("received signal", {
281
358
  peerId: this.ownId,
282
359
  remoteId: this.remoteId,
283
360
  msg: msg.data
284
361
  }, {
285
362
  F: __dxlog_file,
286
- L: 250,
363
+ L: 263,
287
364
  S: this,
288
365
  C: (f, a) => f(...a)
289
366
  });
@@ -298,11 +375,19 @@ var Connection = class {
298
375
  peerId: this.ownId
299
376
  }, {
300
377
  F: __dxlog_file,
301
- L: 257,
378
+ L: 270,
302
379
  S: this,
303
380
  C: (f, a) => f(...a)
304
381
  });
305
- invariant(state !== this._state, "Already in this state.");
382
+ invariant(state !== this._state, "Already in this state.", {
383
+ F: __dxlog_file,
384
+ L: 271,
385
+ S: this,
386
+ A: [
387
+ "state !== this._state",
388
+ "'Already in this state.'"
389
+ ]
390
+ });
306
391
  this._state = state;
307
392
  this.stateChanged.emit(state);
308
393
  }
@@ -312,8 +397,8 @@ _ts_decorate([
312
397
  ], Connection.prototype, "close", null);
313
398
 
314
399
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
315
- import invariant2 from "tiny-invariant";
316
400
  import { Context as Context2 } from "@dxos/context";
401
+ import { invariant as invariant2 } from "@dxos/invariant";
317
402
  import { PublicKey as PublicKey2 } from "@dxos/keys";
318
403
  import { log as log2 } from "@dxos/log";
319
404
  import { schema } from "@dxos/protocols";
@@ -330,7 +415,6 @@ var SwarmMessenger = class {
330
415
  this._topic = topic;
331
416
  }
332
417
  async receiveMessage({ author, recipient, payload }) {
333
- var _a, _b, _c, _d;
334
418
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
335
419
  return;
336
420
  }
@@ -344,25 +428,25 @@ var SwarmMessenger = class {
344
428
  msg: message
345
429
  }, {
346
430
  F: __dxlog_file2,
347
- L: 70,
431
+ L: 69,
348
432
  S: this,
349
433
  C: (f, a) => f(...a)
350
434
  });
351
- if ((_a = message.data) == null ? void 0 : _a.offer) {
435
+ if (message.data?.offer) {
352
436
  await this._handleOffer({
353
437
  author,
354
438
  recipient,
355
439
  message
356
440
  });
357
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
441
+ } else if (message.data?.answer) {
358
442
  await this._resolveAnswers(message);
359
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
443
+ } else if (message.data?.signal) {
360
444
  await this._handleSignal({
361
445
  author,
362
446
  recipient,
363
447
  message
364
448
  });
365
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
449
+ } else if (message.data?.signalBatch) {
366
450
  await this._handleSignal({
367
451
  author,
368
452
  recipient,
@@ -373,15 +457,22 @@ var SwarmMessenger = class {
373
457
  message
374
458
  }, {
375
459
  F: __dxlog_file2,
376
- L: 81,
460
+ L: 80,
377
461
  S: this,
378
462
  C: (f, a) => f(...a)
379
463
  });
380
464
  }
381
465
  }
382
466
  async signal(message) {
383
- var _a, _b;
384
- invariant2(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
467
+ invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
468
+ F: __dxlog_file2,
469
+ L: 85,
470
+ S: this,
471
+ A: [
472
+ "message.data?.signal || message.data?.signalBatch",
473
+ "'Invalid message'"
474
+ ]
475
+ });
385
476
  await this._sendReliableMessage({
386
477
  author: message.author,
387
478
  recipient: message.recipient,
@@ -405,11 +496,10 @@ var SwarmMessenger = class {
405
496
  });
406
497
  }
407
498
  async _sendReliableMessage({ author, recipient, message }) {
408
- var _a;
409
499
  const networkMessage = {
410
500
  ...message,
411
501
  // Setting unique message_id if it not specified yet.
412
- messageId: (_a = message.messageId) != null ? _a : PublicKey2.random()
502
+ messageId: message.messageId ?? PublicKey2.random()
413
503
  };
414
504
  log2("sending", {
415
505
  from: author,
@@ -417,7 +507,7 @@ var SwarmMessenger = class {
417
507
  msg: networkMessage
418
508
  }, {
419
509
  F: __dxlog_file2,
420
- L: 124,
510
+ L: 123,
421
511
  S: this,
422
512
  C: (f, a) => f(...a)
423
513
  });
@@ -431,17 +521,32 @@ var SwarmMessenger = class {
431
521
  });
432
522
  }
433
523
  async _resolveAnswers(message) {
434
- var _a, _b, _c;
435
- invariant2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
524
+ invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
525
+ F: __dxlog_file2,
526
+ L: 135,
527
+ S: this,
528
+ A: [
529
+ "message.data?.answer?.offerMessageId",
530
+ "'No offerMessageId'"
531
+ ]
532
+ });
436
533
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
437
534
  if (offerRecord) {
438
535
  this._offerRecords.delete(message.data.answer.offerMessageId);
439
- invariant2((_c = message.data) == null ? void 0 : _c.answer, "No answer");
536
+ invariant2(message.data?.answer, "No answer", {
537
+ F: __dxlog_file2,
538
+ L: 139,
539
+ S: this,
540
+ A: [
541
+ "message.data?.answer",
542
+ "'No answer'"
543
+ ]
544
+ });
440
545
  log2("resolving", {
441
546
  answer: message.data.answer
442
547
  }, {
443
548
  F: __dxlog_file2,
444
- L: 141,
549
+ L: 140,
445
550
  S: this,
446
551
  C: (f, a) => f(...a)
447
552
  });
@@ -449,7 +554,15 @@ var SwarmMessenger = class {
449
554
  }
450
555
  }
451
556
  async _handleOffer({ author, recipient, message }) {
452
- invariant2(message.data.offer, "No offer");
557
+ invariant2(message.data.offer, "No offer", {
558
+ F: __dxlog_file2,
559
+ L: 154,
560
+ S: this,
561
+ A: [
562
+ "message.data.offer",
563
+ "'No offer'"
564
+ ]
565
+ });
453
566
  const offerMessage = {
454
567
  author,
455
568
  recipient,
@@ -477,15 +590,31 @@ var SwarmMessenger = class {
477
590
  err
478
591
  }, {
479
592
  F: __dxlog_file2,
480
- L: 175,
593
+ L: 174,
481
594
  S: this,
482
595
  C: (f, a) => f(...a)
483
596
  });
484
597
  }
485
598
  }
486
599
  async _handleSignal({ author, recipient, message }) {
487
- invariant2(message.messageId);
488
- invariant2(message.data.signal || message.data.signalBatch, "Invalid message");
600
+ invariant2(message.messageId, void 0, {
601
+ F: __dxlog_file2,
602
+ L: 187,
603
+ S: this,
604
+ A: [
605
+ "message.messageId",
606
+ ""
607
+ ]
608
+ });
609
+ invariant2(message.data.signal || message.data.signalBatch, "Invalid message", {
610
+ F: __dxlog_file2,
611
+ L: 188,
612
+ S: this,
613
+ A: [
614
+ "message.data.signal || message.data.signalBatch",
615
+ "'Invalid message'"
616
+ ]
617
+ });
489
618
  const signalMessage = {
490
619
  author,
491
620
  recipient,
@@ -500,19 +629,20 @@ var SwarmMessenger = class {
500
629
  };
501
630
 
502
631
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
503
- import invariant4 from "tiny-invariant";
504
632
  import { Event as Event2, scheduleTask as scheduleTask2, sleep as sleep2, synchronized as synchronized3 } from "@dxos/async";
505
633
  import { Context as Context4 } from "@dxos/context";
506
634
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
635
+ import { invariant as invariant4 } from "@dxos/invariant";
507
636
  import { PublicKey as PublicKey4 } from "@dxos/keys";
508
637
  import { log as log4, logInfo } from "@dxos/log";
509
638
  import { trace as trace2 } from "@dxos/protocols";
510
639
  import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
511
640
 
512
641
  // packages/core/mesh/network-manager/src/swarm/peer.ts
513
- import invariant3 from "tiny-invariant";
514
642
  import { scheduleTask, synchronized as synchronized2 } from "@dxos/async";
515
643
  import { Context as Context3 } from "@dxos/context";
644
+ import { CancelledError as CancelledError2 } from "@dxos/errors";
645
+ import { invariant as invariant3 } from "@dxos/invariant";
516
646
  import { PublicKey as PublicKey3 } from "@dxos/keys";
517
647
  import { log as log3 } from "@dxos/log";
518
648
  function _ts_decorate2(decorators, target, key, desc) {
@@ -528,13 +658,14 @@ function _ts_decorate2(decorators, target, key, desc) {
528
658
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
529
659
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
530
660
  var Peer = class {
531
- constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _callbacks) {
661
+ constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
532
662
  this.id = id;
533
663
  this.topic = topic;
534
664
  this.localPeerId = localPeerId;
535
665
  this._signalMessaging = _signalMessaging;
536
666
  this._protocolProvider = _protocolProvider;
537
667
  this._transportFactory = _transportFactory;
668
+ this._connectionLimiter = _connectionLimiter;
538
669
  this._callbacks = _callbacks;
539
670
  this._availableAfter = 0;
540
671
  this.availableToConnect = true;
@@ -549,13 +680,14 @@ var Peer = class {
549
680
  const remoteId = message.author;
550
681
  if (this.connection || this.initiating) {
551
682
  if (remoteId.toHex() < this.localPeerId.toHex()) {
552
- log3("closing local connection and accepting remote peer's offer", {
553
- id: this.id,
683
+ log3("close local connection", {
684
+ localPeerId: this.id,
554
685
  topic: this.topic,
555
- peerId: this.localPeerId
686
+ remotePeerId: this.localPeerId,
687
+ sessionId: this.connection?.sessionId
556
688
  }, {
557
689
  F: __dxlog_file3,
558
- L: 105,
690
+ L: 106,
559
691
  S: this,
560
692
  C: (f, a) => f(...a)
561
693
  });
@@ -570,22 +702,33 @@ var Peer = class {
570
702
  }
571
703
  if (await this._callbacks.onOffer(remoteId)) {
572
704
  if (!this.connection) {
573
- invariant3(message.sessionId);
705
+ invariant3(message.sessionId, void 0, {
706
+ F: __dxlog_file3,
707
+ L: 126,
708
+ S: this,
709
+ A: [
710
+ "message.sessionId",
711
+ ""
712
+ ]
713
+ });
574
714
  const connection = this._createConnection(false, message.sessionId);
575
715
  try {
576
- connection.openConnection();
716
+ await this._connectionLimiter.connecting(message.sessionId);
717
+ await connection.openConnection();
577
718
  } catch (err) {
578
- log3.warn("connection error", {
579
- topic: this.topic,
580
- peerId: this.localPeerId,
581
- remoteId: this.id,
582
- err
583
- }, {
584
- F: __dxlog_file3,
585
- L: 129,
586
- S: this,
587
- C: (f, a) => f(...a)
588
- });
719
+ if (!(err instanceof CancelledError2)) {
720
+ log3.warn("connection error", {
721
+ topic: this.topic,
722
+ peerId: this.localPeerId,
723
+ remoteId: this.id,
724
+ err
725
+ }, {
726
+ F: __dxlog_file3,
727
+ L: 134,
728
+ S: this,
729
+ C: (f, a) => f(...a)
730
+ });
731
+ }
589
732
  await this.closeConnection();
590
733
  }
591
734
  return {
@@ -601,8 +744,24 @@ var Peer = class {
601
744
  * Initiate a connection to the remote peer.
602
745
  */
603
746
  async initiateConnection() {
604
- invariant3(!this.initiating, "Initiation in progress.");
605
- invariant3(!this.connection, "Already connected.");
747
+ invariant3(!this.initiating, "Initiation in progress.", {
748
+ F: __dxlog_file3,
749
+ L: 151,
750
+ S: this,
751
+ A: [
752
+ "!this.initiating",
753
+ "'Initiation in progress.'"
754
+ ]
755
+ });
756
+ invariant3(!this.connection, "Already connected.", {
757
+ F: __dxlog_file3,
758
+ L: 152,
759
+ S: this,
760
+ A: [
761
+ "!this.connection",
762
+ "'Already connected.'"
763
+ ]
764
+ });
606
765
  const sessionId = PublicKey3.random();
607
766
  log3("initiating...", {
608
767
  id: this.id,
@@ -611,13 +770,14 @@ var Peer = class {
611
770
  sessionId
612
771
  }, {
613
772
  F: __dxlog_file3,
614
- L: 147,
773
+ L: 154,
615
774
  S: this,
616
775
  C: (f, a) => f(...a)
617
776
  });
618
777
  const connection = this._createConnection(true, sessionId);
619
778
  this.initiating = true;
620
779
  try {
780
+ await this._connectionLimiter.connecting(sessionId);
621
781
  const answer = await this._signalMessaging.offer({
622
782
  author: this.localPeerId,
623
783
  recipient: this.id,
@@ -634,14 +794,14 @@ var Peer = class {
634
794
  remoteId: this.id
635
795
  }, {
636
796
  F: __dxlog_file3,
637
- L: 159,
797
+ L: 169,
638
798
  S: this,
639
799
  C: (f, a) => f(...a)
640
800
  });
641
801
  if (connection.state !== ConnectionState.INITIAL) {
642
802
  log3("ignoring response", void 0, {
643
803
  F: __dxlog_file3,
644
- L: 161,
804
+ L: 171,
645
805
  S: this,
646
806
  C: (f, a) => f(...a)
647
807
  });
@@ -651,7 +811,7 @@ var Peer = class {
651
811
  this._callbacks.onRejected();
652
812
  return;
653
813
  }
654
- connection.openConnection();
814
+ await connection.openConnection();
655
815
  this._callbacks.onAccepted();
656
816
  } catch (err) {
657
817
  log3("initiation error", {
@@ -661,7 +821,7 @@ var Peer = class {
661
821
  remoteId: this.id
662
822
  }, {
663
823
  F: __dxlog_file3,
664
- L: 172,
824
+ L: 182,
665
825
  S: this,
666
826
  C: (f, a) => f(...a)
667
827
  });
@@ -676,7 +836,6 @@ var Peer = class {
676
836
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
677
837
  */
678
838
  _createConnection(initiator, sessionId) {
679
- var _a;
680
839
  log3("creating connection", {
681
840
  topic: this.topic,
682
841
  peerId: this.localPeerId,
@@ -685,11 +844,19 @@ var Peer = class {
685
844
  sessionId
686
845
  }, {
687
846
  F: __dxlog_file3,
688
- L: 186,
847
+ L: 196,
689
848
  S: this,
690
849
  C: (f, a) => f(...a)
691
850
  });
692
- invariant3(!this.connection, "Already connected.");
851
+ invariant3(!this.connection, "Already connected.", {
852
+ F: __dxlog_file3,
853
+ L: 203,
854
+ S: this,
855
+ A: [
856
+ "!this.connection",
857
+ "'Already connected.'"
858
+ ]
859
+ });
693
860
  const connection = new Connection(
694
861
  this.topic,
695
862
  this.localPeerId,
@@ -707,7 +874,7 @@ var Peer = class {
707
874
  this._transportFactory
708
875
  );
709
876
  this._callbacks.onInitiated(connection);
710
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
877
+ void this._connectionCtx?.dispose();
711
878
  this._connectionCtx = this._ctx.derive();
712
879
  connection.stateChanged.on((state) => {
713
880
  switch (state) {
@@ -715,6 +882,19 @@ var Peer = class {
715
882
  this.availableToConnect = true;
716
883
  this._lastConnectionTime = Date.now();
717
884
  this._callbacks.onConnected();
885
+ this._connectionLimiter.doneConnecting(sessionId);
886
+ log3.trace("dxos.mesh.connection.connected", {
887
+ topic: this.topic,
888
+ localPeerId: this.localPeerId,
889
+ remotePeerId: this.id,
890
+ sessionId,
891
+ initiator
892
+ }, {
893
+ F: __dxlog_file3,
894
+ L: 229,
895
+ S: this,
896
+ C: (f, a) => f(...a)
897
+ });
718
898
  break;
719
899
  }
720
900
  case ConnectionState.CLOSED: {
@@ -725,11 +905,31 @@ var Peer = class {
725
905
  initiator
726
906
  }, {
727
907
  F: __dxlog_file3,
728
- L: 221,
908
+ L: 240,
909
+ S: this,
910
+ C: (f, a) => f(...a)
911
+ });
912
+ invariant3(this.connection === connection, "Connection mismatch (race condition).", {
913
+ F: __dxlog_file3,
914
+ L: 241,
915
+ S: this,
916
+ A: [
917
+ "this.connection === connection",
918
+ "'Connection mismatch (race condition).'"
919
+ ]
920
+ });
921
+ log3.trace("dxos.mesh.connection.closed", {
922
+ topic: this.topic,
923
+ localPeerId: this.localPeerId,
924
+ remotePeerId: this.id,
925
+ sessionId,
926
+ initiator
927
+ }, {
928
+ F: __dxlog_file3,
929
+ L: 243,
729
930
  S: this,
730
931
  C: (f, a) => f(...a)
731
932
  });
732
- invariant3(this.connection === connection, "Connection mismatch (race condition).");
733
933
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
734
934
  this._availableAfter = 0;
735
935
  } else {
@@ -738,6 +938,7 @@ var Peer = class {
738
938
  }
739
939
  this.connection = void 0;
740
940
  this._callbacks.onDisconnected();
941
+ this._connectionLimiter.doneConnecting(sessionId);
741
942
  scheduleTask(this._connectionCtx, () => {
742
943
  this.availableToConnect = true;
743
944
  this._callbacks.onPeerAvailable();
@@ -755,7 +956,20 @@ var Peer = class {
755
956
  err
756
957
  }, {
757
958
  F: __dxlog_file3,
758
- L: 249,
959
+ L: 277,
960
+ S: this,
961
+ C: (f, a) => f(...a)
962
+ });
963
+ log3.trace("dxos.mesh.connection.error", {
964
+ topic: this.topic,
965
+ localPeerId: this.localPeerId,
966
+ remotePeerId: this.id,
967
+ sessionId,
968
+ initiator,
969
+ err
970
+ }, {
971
+ F: __dxlog_file3,
972
+ L: 278,
759
973
  S: this,
760
974
  C: (f, a) => f(...a)
761
975
  });
@@ -774,7 +988,7 @@ var Peer = class {
774
988
  sessionId: connection.sessionId
775
989
  }, {
776
990
  F: __dxlog_file3,
777
- L: 265,
991
+ L: 301,
778
992
  S: this,
779
993
  C: (f, a) => f(...a)
780
994
  });
@@ -784,7 +998,7 @@ var Peer = class {
784
998
  sessionId: connection.sessionId
785
999
  }, {
786
1000
  F: __dxlog_file3,
787
- L: 271,
1001
+ L: 307,
788
1002
  S: this,
789
1003
  C: (f, a) => f(...a)
790
1004
  });
@@ -795,7 +1009,7 @@ var Peer = class {
795
1009
  message
796
1010
  }, {
797
1011
  F: __dxlog_file3,
798
- L: 276,
1012
+ L: 312,
799
1013
  S: this,
800
1014
  C: (f, a) => f(...a)
801
1015
  });
@@ -804,18 +1018,17 @@ var Peer = class {
804
1018
  await this.connection.signal(message);
805
1019
  }
806
1020
  async destroy() {
807
- var _a;
808
1021
  await this._ctx.dispose();
809
1022
  log3("Destroying peer", {
810
1023
  peerId: this.id,
811
1024
  topic: this.topic
812
1025
  }, {
813
1026
  F: __dxlog_file3,
814
- L: 285,
1027
+ L: 322,
815
1028
  S: this,
816
1029
  C: (f, a) => f(...a)
817
1030
  });
818
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1031
+ await this?.connection?.close();
819
1032
  }
820
1033
  };
821
1034
  _ts_decorate2([
@@ -851,7 +1064,7 @@ var getClassName = (obj) => Object.getPrototypeOf(obj).constructor.name;
851
1064
  var Swarm = class {
852
1065
  // TODO(burdon): Swarm => Peer.create/destroy =< Connection.open/close
853
1066
  // TODO(burdon): Split up properties.
854
- constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label) {
1067
+ constructor(_topic, _ownPeerId, _topology, _protocolProvider, _messenger, _transportFactory, _label, _connectionLimiter) {
855
1068
  this._topic = _topic;
856
1069
  this._ownPeerId = _ownPeerId;
857
1070
  this._topology = _topology;
@@ -859,6 +1072,7 @@ var Swarm = class {
859
1072
  this._messenger = _messenger;
860
1073
  this._transportFactory = _transportFactory;
861
1074
  this._label = _label;
1075
+ this._connectionLimiter = _connectionLimiter;
862
1076
  this._peers = new ComplexMap2(PublicKey4.hash);
863
1077
  this._ctx = new Context4();
864
1078
  this._listeningHandle = void 0;
@@ -875,7 +1089,7 @@ var Swarm = class {
875
1089
  }
876
1090
  }), {
877
1091
  F: __dxlog_file4,
878
- L: 86,
1092
+ L: 87,
879
1093
  S: this,
880
1094
  C: (f, a) => f(...a)
881
1095
  });
@@ -883,7 +1097,7 @@ var Swarm = class {
883
1097
  peerId: _ownPeerId
884
1098
  }, {
885
1099
  F: __dxlog_file4,
886
- L: 90,
1100
+ L: 91,
887
1101
  S: this,
888
1102
  C: (f, a) => f(...a)
889
1103
  });
@@ -898,7 +1112,7 @@ var Swarm = class {
898
1112
  id: this._instanceId
899
1113
  }), {
900
1114
  F: __dxlog_file4,
901
- L: 99,
1115
+ L: 100,
902
1116
  S: this,
903
1117
  C: (f, a) => f(...a)
904
1118
  });
@@ -919,7 +1133,15 @@ var Swarm = class {
919
1133
  return this._topic;
920
1134
  }
921
1135
  async open() {
922
- invariant4(!this._listeningHandle);
1136
+ invariant4(!this._listeningHandle, void 0, {
1137
+ F: __dxlog_file4,
1138
+ L: 127,
1139
+ S: this,
1140
+ A: [
1141
+ "!this._listeningHandle",
1142
+ ""
1143
+ ]
1144
+ });
923
1145
  this._listeningHandle = await this._messenger.listen({
924
1146
  peerId: this._ownPeerId,
925
1147
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -928,7 +1150,7 @@ var Swarm = class {
928
1150
  err
929
1151
  }, {
930
1152
  F: __dxlog_file4,
931
- L: 133,
1153
+ L: 134,
932
1154
  S: this,
933
1155
  C: (f, a) => f(...a)
934
1156
  }));
@@ -936,27 +1158,34 @@ var Swarm = class {
936
1158
  });
937
1159
  }
938
1160
  async destroy() {
939
- var _a;
940
1161
  log4("destroying...", void 0, {
941
1162
  F: __dxlog_file4,
942
- L: 139,
1163
+ L: 140,
943
1164
  S: this,
944
1165
  C: (f, a) => f(...a)
945
1166
  });
946
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1167
+ await this._listeningHandle?.unsubscribe();
947
1168
  this._listeningHandle = void 0;
948
1169
  await this._ctx.dispose();
949
1170
  await this._topology.destroy();
950
1171
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
951
1172
  log4("destroyed", void 0, {
952
1173
  F: __dxlog_file4,
953
- L: 146,
1174
+ L: 147,
954
1175
  S: this,
955
1176
  C: (f, a) => f(...a)
956
1177
  });
957
1178
  }
958
1179
  async setTopology(topology) {
959
- invariant4(!this._ctx.disposed, "Swarm is offline");
1180
+ invariant4(!this._ctx.disposed, "Swarm is offline", {
1181
+ F: __dxlog_file4,
1182
+ L: 151,
1183
+ S: this,
1184
+ A: [
1185
+ "!this._ctx.disposed",
1186
+ "'Swarm is offline'"
1187
+ ]
1188
+ });
960
1189
  if (topology === this._topology) {
961
1190
  return;
962
1191
  }
@@ -965,7 +1194,7 @@ var Swarm = class {
965
1194
  topology: getClassName(topology)
966
1195
  }, {
967
1196
  F: __dxlog_file4,
968
- L: 154,
1197
+ L: 155,
969
1198
  S: this,
970
1199
  C: (f, a) => f(...a)
971
1200
  });
@@ -975,19 +1204,18 @@ var Swarm = class {
975
1204
  this._topology.update();
976
1205
  }
977
1206
  onSwarmEvent(swarmEvent) {
978
- var _a;
979
1207
  log4("swarm event", {
980
1208
  swarmEvent
981
1209
  }, {
982
1210
  F: __dxlog_file4,
983
- L: 167,
1211
+ L: 168,
984
1212
  S: this,
985
1213
  C: (f, a) => f(...a)
986
1214
  });
987
1215
  if (this._ctx.disposed) {
988
1216
  log4("swarm event ignored for disposed swarm", void 0, {
989
1217
  F: __dxlog_file4,
990
- L: 170,
1218
+ L: 171,
991
1219
  S: this,
992
1220
  C: (f, a) => f(...a)
993
1221
  });
@@ -999,7 +1227,7 @@ var Swarm = class {
999
1227
  peerId
1000
1228
  }, {
1001
1229
  F: __dxlog_file4,
1002
- L: 176,
1230
+ L: 177,
1003
1231
  S: this,
1004
1232
  C: (f, a) => f(...a)
1005
1233
  });
@@ -1011,10 +1239,10 @@ var Swarm = class {
1011
1239
  const peer = this._peers.get(PublicKey4.from(swarmEvent.peerLeft.peer));
1012
1240
  if (peer) {
1013
1241
  peer.advertizing = false;
1014
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1242
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1015
1243
  void this._destroyPeer(peer.id).catch((err) => log4.catch(err, void 0, {
1016
1244
  F: __dxlog_file4,
1017
- L: 187,
1245
+ L: 188,
1018
1246
  S: this,
1019
1247
  C: (f, a) => f(...a)
1020
1248
  }));
@@ -1024,19 +1252,18 @@ var Swarm = class {
1024
1252
  this._topology.update();
1025
1253
  }
1026
1254
  async onOffer(message) {
1027
- var _a, _b;
1028
1255
  log4("offer", {
1029
1256
  message
1030
1257
  }, {
1031
1258
  F: __dxlog_file4,
1032
- L: 197,
1259
+ L: 198,
1033
1260
  S: this,
1034
1261
  C: (f, a) => f(...a)
1035
1262
  });
1036
1263
  if (this._ctx.disposed) {
1037
1264
  log4("ignored for disposed swarm", void 0, {
1038
1265
  F: __dxlog_file4,
1039
- L: 199,
1266
+ L: 200,
1040
1267
  S: this,
1041
1268
  C: (f, a) => f(...a)
1042
1269
  });
@@ -1044,13 +1271,21 @@ var Swarm = class {
1044
1271
  accept: false
1045
1272
  };
1046
1273
  }
1047
- invariant4(message.author);
1048
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1274
+ invariant4(message.author, void 0, {
1275
+ F: __dxlog_file4,
1276
+ L: 205,
1277
+ S: this,
1278
+ A: [
1279
+ "message.author",
1280
+ ""
1281
+ ]
1282
+ });
1283
+ if (!message.recipient?.equals(this._ownPeerId)) {
1049
1284
  log4("rejecting offer with incorrect peerId", {
1050
1285
  message
1051
1286
  }, {
1052
1287
  F: __dxlog_file4,
1053
- L: 206,
1288
+ L: 207,
1054
1289
  S: this,
1055
1290
  C: (f, a) => f(...a)
1056
1291
  });
@@ -1058,12 +1293,12 @@ var Swarm = class {
1058
1293
  accept: false
1059
1294
  };
1060
1295
  }
1061
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1296
+ if (!message.topic?.equals(this._topic)) {
1062
1297
  log4("rejecting offer with incorrect topic", {
1063
1298
  message
1064
1299
  }, {
1065
1300
  F: __dxlog_file4,
1066
- L: 210,
1301
+ L: 211,
1067
1302
  S: this,
1068
1303
  C: (f, a) => f(...a)
1069
1304
  });
@@ -1077,7 +1312,6 @@ var Swarm = class {
1077
1312
  return answer;
1078
1313
  }
1079
1314
  async onSignal(message) {
1080
- var _a, _b;
1081
1315
  log4("signal", {
1082
1316
  message
1083
1317
  }, {
@@ -1095,9 +1329,33 @@ var Swarm = class {
1095
1329
  });
1096
1330
  return;
1097
1331
  }
1098
- invariant4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1099
- invariant4((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1100
- invariant4(message.author);
1332
+ invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1333
+ F: __dxlog_file4,
1334
+ L: 227,
1335
+ S: this,
1336
+ A: [
1337
+ "message.recipient?.equals(this._ownPeerId)",
1338
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1339
+ ]
1340
+ });
1341
+ invariant4(message.topic?.equals(this._topic), void 0, {
1342
+ F: __dxlog_file4,
1343
+ L: 231,
1344
+ S: this,
1345
+ A: [
1346
+ "message.topic?.equals(this._topic)",
1347
+ ""
1348
+ ]
1349
+ });
1350
+ invariant4(message.author, void 0, {
1351
+ F: __dxlog_file4,
1352
+ L: 232,
1353
+ S: this,
1354
+ A: [
1355
+ "message.author",
1356
+ ""
1357
+ ]
1358
+ });
1101
1359
  const peer = this._getOrCreatePeer(message.author);
1102
1360
  await peer.onSignal(message);
1103
1361
  }
@@ -1115,7 +1373,7 @@ var Swarm = class {
1115
1373
  _getOrCreatePeer(peerId) {
1116
1374
  let peer = this._peers.get(peerId);
1117
1375
  if (!peer) {
1118
- peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, {
1376
+ peer = new Peer(peerId, this._topic, this._ownPeerId, this._swarmMessenger, this._protocolProvider, this._transportFactory, this._connectionLimiter, {
1119
1377
  onInitiated: (connection) => {
1120
1378
  this.connectionAdded.emit(connection);
1121
1379
  },
@@ -1149,7 +1407,15 @@ var Swarm = class {
1149
1407
  return peer;
1150
1408
  }
1151
1409
  async _destroyPeer(peerId) {
1152
- invariant4(this._peers.has(peerId));
1410
+ invariant4(this._peers.has(peerId), void 0, {
1411
+ F: __dxlog_file4,
1412
+ L: 302,
1413
+ S: this,
1414
+ A: [
1415
+ "this._peers.has(peerId)",
1416
+ ""
1417
+ ]
1418
+ });
1153
1419
  await this._peers.get(peerId).destroy();
1154
1420
  this._peers.delete(peerId);
1155
1421
  }
@@ -1170,7 +1436,7 @@ var Swarm = class {
1170
1436
  } catch (err) {
1171
1437
  log4("initiation error", err, {
1172
1438
  F: __dxlog_file4,
1173
- L: 327,
1439
+ L: 328,
1174
1440
  S: this,
1175
1441
  C: (f, a) => f(...a)
1176
1442
  });
@@ -1198,7 +1464,7 @@ var Swarm = class {
1198
1464
  remoteId
1199
1465
  }, {
1200
1466
  F: __dxlog_file4,
1201
- L: 354,
1467
+ L: 355,
1202
1468
  S: this,
1203
1469
  C: (f, a) => f(...a)
1204
1470
  });
@@ -1215,7 +1481,7 @@ var Swarm = class {
1215
1481
  remoteId
1216
1482
  }, {
1217
1483
  F: __dxlog_file4,
1218
- L: 368,
1484
+ L: 369,
1219
1485
  S: this,
1220
1486
  C: (f, a) => f(...a)
1221
1487
  });
@@ -1225,7 +1491,7 @@ var Swarm = class {
1225
1491
  remoteId
1226
1492
  }, {
1227
1493
  F: __dxlog_file4,
1228
- L: 371,
1494
+ L: 372,
1229
1495
  S: this,
1230
1496
  C: (f, a) => f(...a)
1231
1497
  });
@@ -1253,9 +1519,6 @@ _ts_decorate3([
1253
1519
  _ts_decorate3([
1254
1520
  synchronized3
1255
1521
  ], Swarm.prototype, "onOffer", null);
1256
- _ts_decorate3([
1257
- synchronized3
1258
- ], Swarm.prototype, "onSignal", null);
1259
1522
  _ts_decorate3([
1260
1523
  synchronized3
1261
1524
  ], Swarm.prototype, "goOffline", null);
@@ -1287,8 +1550,7 @@ var SwarmMapper = class {
1287
1550
  }));
1288
1551
  }));
1289
1552
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1290
- var _a;
1291
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1553
+ this._connectionSubscriptions.get(peerId)?.();
1292
1554
  this._connectionSubscriptions.delete(peerId);
1293
1555
  this._update();
1294
1556
  }));
@@ -1334,11 +1596,93 @@ var SwarmMapper = class {
1334
1596
  }
1335
1597
  };
1336
1598
 
1599
+ // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
1600
+ import { DeferredTask as DeferredTask2 } from "@dxos/async";
1601
+ import { Context as Context5 } from "@dxos/context";
1602
+ import { CancelledError as CancelledError3 } from "@dxos/errors";
1603
+ import { invariant as invariant5 } from "@dxos/invariant";
1604
+ import { PublicKey as PublicKey6 } from "@dxos/keys";
1605
+ import { log as log6 } from "@dxos/log";
1606
+ import { ComplexMap as ComplexMap4 } from "@dxos/util";
1607
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1608
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1609
+ var ConnectionLimiter = class {
1610
+ constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1611
+ this._ctx = new Context5();
1612
+ /**
1613
+ * Queue of promises to resolve when initiating connections amount is below the limit.
1614
+ */
1615
+ this._waitingPromises = new ComplexMap4(PublicKey6.hash);
1616
+ this.resolveWaitingPromises = new DeferredTask2(this._ctx, async () => {
1617
+ Array.from(this._waitingPromises.values()).slice(0, this._maxConcurrentInitConnections).forEach(({ resolve }) => {
1618
+ resolve();
1619
+ });
1620
+ });
1621
+ this._maxConcurrentInitConnections = maxConcurrentInitConnections;
1622
+ }
1623
+ /**
1624
+ * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
1625
+ */
1626
+ async connecting(sessionId) {
1627
+ invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1628
+ F: __dxlog_file6,
1629
+ L: 48,
1630
+ S: this,
1631
+ A: [
1632
+ "!this._waitingPromises.has(sessionId)",
1633
+ "'Peer is already waiting for connection'"
1634
+ ]
1635
+ });
1636
+ log6("waiting", {
1637
+ sessionId
1638
+ }, {
1639
+ F: __dxlog_file6,
1640
+ L: 49,
1641
+ S: this,
1642
+ C: (f, a) => f(...a)
1643
+ });
1644
+ await new Promise((resolve, reject) => {
1645
+ this._waitingPromises.set(sessionId, {
1646
+ resolve,
1647
+ reject
1648
+ });
1649
+ this.resolveWaitingPromises.schedule();
1650
+ });
1651
+ log6("allow", {
1652
+ sessionId
1653
+ }, {
1654
+ F: __dxlog_file6,
1655
+ L: 57,
1656
+ S: this,
1657
+ C: (f, a) => f(...a)
1658
+ });
1659
+ }
1660
+ /**
1661
+ * Rejects promise returned by `connecting` method.
1662
+ */
1663
+ doneConnecting(sessionId) {
1664
+ log6("done", {
1665
+ sessionId
1666
+ }, {
1667
+ F: __dxlog_file6,
1668
+ L: 64,
1669
+ S: this,
1670
+ C: (f, a) => f(...a)
1671
+ });
1672
+ if (!this._waitingPromises.has(sessionId)) {
1673
+ return;
1674
+ }
1675
+ this._waitingPromises.get(sessionId).reject(new CancelledError3());
1676
+ this._waitingPromises.delete(sessionId);
1677
+ this.resolveWaitingPromises.schedule();
1678
+ }
1679
+ };
1680
+
1337
1681
  // packages/core/mesh/network-manager/src/connection-log.ts
1338
1682
  import { Event as Event4 } from "@dxos/async";
1339
1683
  import { raise } from "@dxos/debug";
1340
- import { PublicKey as PublicKey6 } from "@dxos/keys";
1341
- import { ComplexMap as ComplexMap4 } from "@dxos/util";
1684
+ import { PublicKey as PublicKey7 } from "@dxos/keys";
1685
+ import { ComplexMap as ComplexMap5 } from "@dxos/util";
1342
1686
  var EventType;
1343
1687
  (function(EventType2) {
1344
1688
  EventType2["CONNECTION_STATE_CHANGED"] = "CONNECTION_STATE_CHANGED";
@@ -1352,28 +1696,26 @@ var ConnectionLog = class {
1352
1696
  /**
1353
1697
  * SwarmId => info
1354
1698
  */
1355
- this._swarms = new ComplexMap4(PublicKey6.hash);
1699
+ this._swarms = new ComplexMap5(PublicKey7.hash);
1356
1700
  this.update = new Event4();
1357
1701
  }
1358
1702
  getSwarmInfo(swarmId) {
1359
- var _a;
1360
- return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
1703
+ return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
1361
1704
  }
1362
1705
  get swarms() {
1363
1706
  return Array.from(this._swarms.values());
1364
1707
  }
1365
1708
  joinedSwarm(swarm) {
1366
1709
  const info = {
1367
- id: PublicKey6.from(swarm._instanceId),
1710
+ id: PublicKey7.from(swarm._instanceId),
1368
1711
  topic: swarm.topic,
1369
1712
  isActive: true,
1370
1713
  label: swarm.label,
1371
1714
  connections: []
1372
1715
  };
1373
- this._swarms.set(PublicKey6.from(swarm._instanceId), info);
1716
+ this._swarms.set(PublicKey7.from(swarm._instanceId), info);
1374
1717
  this.update.emit();
1375
1718
  swarm.connectionAdded.on((connection) => {
1376
- var _a, _b;
1377
1719
  const connectionInfo = {
1378
1720
  state: ConnectionState.INITIAL,
1379
1721
  remotePeerId: connection.remoteId,
@@ -1392,42 +1734,42 @@ var ConnectionLog = class {
1392
1734
  });
1393
1735
  this.update.emit();
1394
1736
  });
1395
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1737
+ connection.protocol?.stats?.on((stats) => {
1396
1738
  connectionInfo.streams = stats.channels;
1397
1739
  this.update.emit();
1398
1740
  });
1399
1741
  });
1400
1742
  }
1401
1743
  leftSwarm(swarm) {
1402
- this.getSwarmInfo(PublicKey6.from(swarm._instanceId)).isActive = false;
1744
+ this.getSwarmInfo(PublicKey7.from(swarm._instanceId)).isActive = false;
1403
1745
  this.update.emit();
1404
1746
  }
1405
1747
  };
1406
1748
 
1407
1749
  // packages/core/mesh/network-manager/src/network-manager.ts
1408
- import invariant5 from "tiny-invariant";
1409
1750
  import { Event as Event5 } from "@dxos/async";
1410
- import { PublicKey as PublicKey7 } from "@dxos/keys";
1411
- import { log as log6 } from "@dxos/log";
1751
+ import { invariant as invariant6 } from "@dxos/invariant";
1752
+ import { PublicKey as PublicKey8 } from "@dxos/keys";
1753
+ import { log as log7 } from "@dxos/log";
1412
1754
  import { Messenger } from "@dxos/messaging";
1413
1755
  import { trace as trace3 } from "@dxos/protocols";
1414
1756
  import { ConnectionState as ConnectionState2 } from "@dxos/protocols/proto/dxos/client/services";
1415
- import { ComplexMap as ComplexMap5 } from "@dxos/util";
1416
- var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1757
+ import { ComplexMap as ComplexMap6 } from "@dxos/util";
1758
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/network-manager.ts";
1417
1759
  var NetworkManager = class {
1418
1760
  constructor({ transportFactory, signalManager, log: log1 }) {
1419
- this._swarms = new ComplexMap5(PublicKey7.hash);
1420
- this._mappers = new ComplexMap5(PublicKey7.hash);
1761
+ /**
1762
+ * @internal
1763
+ */
1764
+ this._swarms = new ComplexMap6(PublicKey8.hash);
1765
+ this._mappers = new ComplexMap6(PublicKey8.hash);
1421
1766
  this._connectionState = ConnectionState2.ONLINE;
1422
1767
  this.connectionStateChanged = new Event5();
1423
1768
  this.topicsUpdated = new Event5();
1424
- this._instanceId = PublicKey7.random().toHex();
1769
+ this._instanceId = PublicKey8.random().toHex();
1425
1770
  this._transportFactory = transportFactory;
1426
1771
  this._signalManager = signalManager;
1427
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1428
- var _a;
1429
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1430
- });
1772
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1431
1773
  this._messenger = new Messenger({
1432
1774
  signalManager: this._signalManager
1433
1775
  });
@@ -1435,6 +1777,7 @@ var NetworkManager = class {
1435
1777
  join: (opts) => this._signalManager.join(opts),
1436
1778
  leave: (opts) => this._signalManager.leave(opts)
1437
1779
  };
1780
+ this._connectionLimiter = new ConnectionLimiter();
1438
1781
  if (log1) {
1439
1782
  this._connectionLog = new ConnectionLog();
1440
1783
  }
@@ -1457,21 +1800,21 @@ var NetworkManager = class {
1457
1800
  return this._swarms.get(topic);
1458
1801
  }
1459
1802
  async open() {
1460
- log6.trace("dxos.mesh.network-manager.open", trace3.begin({
1803
+ log7.trace("dxos.mesh.network-manager.open", trace3.begin({
1461
1804
  id: this._instanceId
1462
1805
  }), {
1463
- F: __dxlog_file6,
1464
- L: 127,
1806
+ F: __dxlog_file7,
1807
+ L: 130,
1465
1808
  S: this,
1466
1809
  C: (f, a) => f(...a)
1467
1810
  });
1468
1811
  await this._messenger.open();
1469
1812
  await this._signalManager.open();
1470
- log6.trace("dxos.mesh.network-manager.open", trace3.end({
1813
+ log7.trace("dxos.mesh.network-manager.open", trace3.end({
1471
1814
  id: this._instanceId
1472
1815
  }), {
1473
- F: __dxlog_file6,
1474
- L: 130,
1816
+ F: __dxlog_file7,
1817
+ L: 133,
1475
1818
  S: this,
1476
1819
  C: (f, a) => f(...a)
1477
1820
  });
@@ -1479,9 +1822,9 @@ var NetworkManager = class {
1479
1822
  async close() {
1480
1823
  for (const topic of this._swarms.keys()) {
1481
1824
  await this.leaveSwarm(topic).catch((err) => {
1482
- log6(err, void 0, {
1483
- F: __dxlog_file6,
1484
- L: 136,
1825
+ log7(err, void 0, {
1826
+ F: __dxlog_file7,
1827
+ L: 139,
1485
1828
  S: this,
1486
1829
  C: (f, a) => f(...a)
1487
1830
  });
@@ -1494,31 +1837,62 @@ var NetworkManager = class {
1494
1837
  * Join the swarm.
1495
1838
  */
1496
1839
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1497
- var _a;
1498
- invariant5(PublicKey7.isPublicKey(topic));
1499
- invariant5(PublicKey7.isPublicKey(peerId));
1500
- invariant5(topology);
1501
- invariant5(typeof protocol === "function");
1840
+ invariant6(PublicKey8.isPublicKey(topic), void 0, {
1841
+ F: __dxlog_file7,
1842
+ L: 157,
1843
+ S: this,
1844
+ A: [
1845
+ "PublicKey.isPublicKey(topic)",
1846
+ ""
1847
+ ]
1848
+ });
1849
+ invariant6(PublicKey8.isPublicKey(peerId), void 0, {
1850
+ F: __dxlog_file7,
1851
+ L: 158,
1852
+ S: this,
1853
+ A: [
1854
+ "PublicKey.isPublicKey(peerId)",
1855
+ ""
1856
+ ]
1857
+ });
1858
+ invariant6(topology, void 0, {
1859
+ F: __dxlog_file7,
1860
+ L: 159,
1861
+ S: this,
1862
+ A: [
1863
+ "topology",
1864
+ ""
1865
+ ]
1866
+ });
1867
+ invariant6(typeof protocol === "function", void 0, {
1868
+ F: __dxlog_file7,
1869
+ L: 160,
1870
+ S: this,
1871
+ A: [
1872
+ "typeof protocol === 'function'",
1873
+ ""
1874
+ ]
1875
+ });
1502
1876
  if (this._swarms.has(topic)) {
1503
- throw new Error(`Already connected to swarm: ${PublicKey7.from(topic)}`);
1877
+ throw new Error(`Already connected to swarm: ${PublicKey8.from(topic)}`);
1504
1878
  }
1505
- log6("joining", {
1506
- topic: PublicKey7.from(topic),
1879
+ log7("joining", {
1880
+ topic: PublicKey8.from(topic),
1507
1881
  peerId,
1508
1882
  topology: topology.toString()
1509
1883
  }, {
1510
- F: __dxlog_file6,
1511
- L: 162,
1884
+ F: __dxlog_file7,
1885
+ L: 165,
1512
1886
  S: this,
1513
1887
  C: (f, a) => f(...a)
1514
1888
  });
1515
- const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label);
1889
+ const swarm = new Swarm(topic, peerId, topology, protocol, this._messenger, this._transportFactory, label, this._connectionLimiter);
1516
1890
  swarm.errors.handle((error) => {
1517
- log6("swarm error", {
1891
+ log7("swarm error", {
1518
1892
  error
1519
1893
  }, {
1520
- F: __dxlog_file6,
1521
- L: 165,
1894
+ F: __dxlog_file7,
1895
+ L: 178,
1522
1896
  S: this,
1523
1897
  C: (f, a) => f(...a)
1524
1898
  });
@@ -1529,20 +1903,20 @@ var NetworkManager = class {
1529
1903
  this._signalConnection.join({
1530
1904
  topic,
1531
1905
  peerId
1532
- }).catch((error) => log6.catch(error, void 0, {
1533
- F: __dxlog_file6,
1534
- L: 174,
1906
+ }).catch((error) => log7.catch(error, void 0, {
1907
+ F: __dxlog_file7,
1908
+ L: 187,
1535
1909
  S: this,
1536
1910
  C: (f, a) => f(...a)
1537
1911
  }));
1538
1912
  this.topicsUpdated.emit();
1539
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1540
- log6("joined", {
1541
- topic: PublicKey7.from(topic),
1913
+ this._connectionLog?.joinedSwarm(swarm);
1914
+ log7("joined", {
1915
+ topic: PublicKey8.from(topic),
1542
1916
  count: this._swarms.size
1543
1917
  }, {
1544
- F: __dxlog_file6,
1545
- L: 178,
1918
+ F: __dxlog_file7,
1919
+ L: 191,
1546
1920
  S: this,
1547
1921
  C: (f, a) => f(...a)
1548
1922
  });
@@ -1554,15 +1928,14 @@ var NetworkManager = class {
1554
1928
  * Close the connection.
1555
1929
  */
1556
1930
  async leaveSwarm(topic) {
1557
- var _a;
1558
1931
  if (!this._swarms.has(topic)) {
1559
1932
  return;
1560
1933
  }
1561
- log6("leaving", {
1562
- topic: PublicKey7.from(topic)
1934
+ log7("leaving", {
1935
+ topic: PublicKey8.from(topic)
1563
1936
  }, {
1564
- F: __dxlog_file6,
1565
- L: 194,
1937
+ F: __dxlog_file7,
1938
+ L: 207,
1566
1939
  S: this,
1567
1940
  C: (f, a) => f(...a)
1568
1941
  });
@@ -1574,16 +1947,16 @@ var NetworkManager = class {
1574
1947
  const map = this._mappers.get(topic);
1575
1948
  map.destroy();
1576
1949
  this._mappers.delete(topic);
1577
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1950
+ this._connectionLog?.leftSwarm(swarm);
1578
1951
  await swarm.destroy();
1579
1952
  this._swarms.delete(topic);
1580
1953
  await this.topicsUpdated.emit();
1581
- log6("left", {
1582
- topic: PublicKey7.from(topic),
1954
+ log7("left", {
1955
+ topic: PublicKey8.from(topic),
1583
1956
  count: this._swarms.size
1584
1957
  }, {
1585
- F: __dxlog_file6,
1586
- L: 208,
1958
+ F: __dxlog_file7,
1959
+ L: 221,
1587
1960
  S: this,
1588
1961
  C: (f, a) => f(...a)
1589
1962
  });
@@ -1617,17 +1990,34 @@ var NetworkManager = class {
1617
1990
  };
1618
1991
 
1619
1992
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1620
- import invariant6 from "tiny-invariant";
1993
+ import { invariant as invariant7 } from "@dxos/invariant";
1994
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1621
1995
  var FullyConnectedTopology = class {
1622
1996
  toString() {
1623
1997
  return "FullyConnectedTopology";
1624
1998
  }
1625
1999
  init(controller) {
1626
- invariant6(!this._controller, "Already initialized");
2000
+ invariant7(!this._controller, "Already initialized", {
2001
+ F: __dxlog_file8,
2002
+ L: 18,
2003
+ S: this,
2004
+ A: [
2005
+ "!this._controller",
2006
+ "'Already initialized'"
2007
+ ]
2008
+ });
1627
2009
  this._controller = controller;
1628
2010
  }
1629
2011
  update() {
1630
- invariant6(this._controller, "Not initialized");
2012
+ invariant7(this._controller, "Not initialized", {
2013
+ F: __dxlog_file8,
2014
+ L: 23,
2015
+ S: this,
2016
+ A: [
2017
+ "this._controller",
2018
+ "'Not initialized'"
2019
+ ]
2020
+ });
1631
2021
  const { candidates: discovered } = this._controller.getState();
1632
2022
  for (const peer of discovered) {
1633
2023
  this._controller.connect(peer);
@@ -1641,10 +2031,10 @@ var FullyConnectedTopology = class {
1641
2031
  };
1642
2032
 
1643
2033
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1644
- import invariant7 from "tiny-invariant";
1645
2034
  import distance from "xor-distance";
1646
- import { log as log7 } from "@dxos/log";
1647
- var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2035
+ import { invariant as invariant8 } from "@dxos/invariant";
2036
+ import { log as log8 } from "@dxos/log";
2037
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1648
2038
  var MMSTTopology = class {
1649
2039
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
1650
2040
  this._sampleCollected = false;
@@ -1653,15 +2043,31 @@ var MMSTTopology = class {
1653
2043
  this._sampleSize = sampleSize;
1654
2044
  }
1655
2045
  init(controller) {
1656
- invariant7(!this._controller, "Already initialized");
2046
+ invariant8(!this._controller, "Already initialized", {
2047
+ F: __dxlog_file9,
2048
+ L: 46,
2049
+ S: this,
2050
+ A: [
2051
+ "!this._controller",
2052
+ "'Already initialized'"
2053
+ ]
2054
+ });
1657
2055
  this._controller = controller;
1658
2056
  }
1659
2057
  update() {
1660
- invariant7(this._controller, "Not initialized");
2058
+ invariant8(this._controller, "Not initialized", {
2059
+ F: __dxlog_file9,
2060
+ L: 51,
2061
+ S: this,
2062
+ A: [
2063
+ "this._controller",
2064
+ "'Not initialized'"
2065
+ ]
2066
+ });
1661
2067
  const { connected, candidates } = this._controller.getState();
1662
2068
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
1663
- log7("Running the algorithm.", void 0, {
1664
- F: __dxlog_file7,
2069
+ log8("Running the algorithm.", void 0, {
2070
+ F: __dxlog_file9,
1665
2071
  L: 55,
1666
2072
  S: this,
1667
2073
  C: (f, a) => f(...a)
@@ -1671,11 +2077,19 @@ var MMSTTopology = class {
1671
2077
  }
1672
2078
  }
1673
2079
  async onOffer(peer) {
1674
- invariant7(this._controller, "Not initialized");
2080
+ invariant8(this._controller, "Not initialized", {
2081
+ F: __dxlog_file9,
2082
+ L: 62,
2083
+ S: this,
2084
+ A: [
2085
+ "this._controller",
2086
+ "'Not initialized'"
2087
+ ]
2088
+ });
1675
2089
  const { connected } = this._controller.getState();
1676
2090
  const accept = connected.length < this._maxPeers;
1677
- log7(`Offer ${peer} accept=${accept}`, void 0, {
1678
- F: __dxlog_file7,
2091
+ log8(`Offer ${peer} accept=${accept}`, void 0, {
2092
+ F: __dxlog_file9,
1679
2093
  L: 65,
1680
2094
  S: this,
1681
2095
  C: (f, a) => f(...a)
@@ -1685,13 +2099,21 @@ var MMSTTopology = class {
1685
2099
  async destroy() {
1686
2100
  }
1687
2101
  _runAlgorithm() {
1688
- invariant7(this._controller, "Not initialized");
2102
+ invariant8(this._controller, "Not initialized", {
2103
+ F: __dxlog_file9,
2104
+ L: 74,
2105
+ S: this,
2106
+ A: [
2107
+ "this._controller",
2108
+ "'Not initialized'"
2109
+ ]
2110
+ });
1689
2111
  const { connected, candidates, ownPeerId } = this._controller.getState();
1690
2112
  if (connected.length > this._maxPeers) {
1691
2113
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
1692
2114
  for (const peer of sorted) {
1693
- log7(`Disconnect ${peer}.`, void 0, {
1694
- F: __dxlog_file7,
2115
+ log8(`Disconnect ${peer}.`, void 0, {
2116
+ F: __dxlog_file9,
1695
2117
  L: 83,
1696
2118
  S: this,
1697
2119
  C: (f, a) => f(...a)
@@ -1702,8 +2124,8 @@ var MMSTTopology = class {
1702
2124
  const sample = candidates.sort(() => Math.random() - 0.5).slice(0, this._sampleSize);
1703
2125
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
1704
2126
  for (const peer of sorted) {
1705
- log7(`Connect ${peer}.`, void 0, {
1706
- F: __dxlog_file7,
2127
+ log8(`Connect ${peer}.`, void 0, {
2128
+ F: __dxlog_file9,
1707
2129
  L: 91,
1708
2130
  S: this,
1709
2131
  C: (f, a) => f(...a)
@@ -1719,9 +2141,9 @@ var MMSTTopology = class {
1719
2141
  var sortByXorDistance = (keys, reference) => keys.sort((a, b) => distance.gt(distance(a.asBuffer(), reference.asBuffer()), distance(b.asBuffer(), reference.asBuffer())));
1720
2142
 
1721
2143
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1722
- import invariant8 from "tiny-invariant";
1723
- import { log as log8 } from "@dxos/log";
1724
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2144
+ import { invariant as invariant9 } from "@dxos/invariant";
2145
+ import { log as log9 } from "@dxos/log";
2146
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1725
2147
  var StarTopology = class {
1726
2148
  // prettier-ignore
1727
2149
  constructor(_centralPeer) {
@@ -1731,26 +2153,42 @@ var StarTopology = class {
1731
2153
  return `StarTopology(${this._centralPeer.truncate()})`;
1732
2154
  }
1733
2155
  init(controller) {
1734
- invariant8(!this._controller, "Already initialized.");
2156
+ invariant9(!this._controller, "Already initialized.", {
2157
+ F: __dxlog_file10,
2158
+ L: 24,
2159
+ S: this,
2160
+ A: [
2161
+ "!this._controller",
2162
+ "'Already initialized.'"
2163
+ ]
2164
+ });
1735
2165
  this._controller = controller;
1736
2166
  }
1737
2167
  update() {
1738
- invariant8(this._controller, "Not initialized.");
2168
+ invariant9(this._controller, "Not initialized.", {
2169
+ F: __dxlog_file10,
2170
+ L: 29,
2171
+ S: this,
2172
+ A: [
2173
+ "this._controller",
2174
+ "'Not initialized.'"
2175
+ ]
2176
+ });
1739
2177
  const { candidates, connected, ownPeerId } = this._controller.getState();
1740
2178
  if (!ownPeerId.equals(this._centralPeer)) {
1741
- log8("leaf peer dropping all connections apart from central peer.", void 0, {
1742
- F: __dxlog_file8,
1743
- L: 33,
2179
+ log9("leaf peer dropping all connections apart from central peer.", void 0, {
2180
+ F: __dxlog_file10,
2181
+ L: 32,
1744
2182
  S: this,
1745
2183
  C: (f, a) => f(...a)
1746
2184
  });
1747
2185
  for (const peer of connected) {
1748
2186
  if (!peer.equals(this._centralPeer)) {
1749
- log8("dropping connection", {
2187
+ log9("dropping connection", {
1750
2188
  peer
1751
2189
  }, {
1752
- F: __dxlog_file8,
1753
- L: 38,
2190
+ F: __dxlog_file10,
2191
+ L: 37,
1754
2192
  S: this,
1755
2193
  C: (f, a) => f(...a)
1756
2194
  });
@@ -1760,11 +2198,11 @@ var StarTopology = class {
1760
2198
  }
1761
2199
  for (const peer of candidates) {
1762
2200
  if (peer.equals(this._centralPeer) || ownPeerId.equals(this._centralPeer)) {
1763
- log8("connecting to peer", {
2201
+ log9("connecting to peer", {
1764
2202
  peer
1765
2203
  }, {
1766
- F: __dxlog_file8,
1767
- L: 47,
2204
+ F: __dxlog_file10,
2205
+ L: 46,
1768
2206
  S: this,
1769
2207
  C: (f, a) => f(...a)
1770
2208
  });
@@ -1773,15 +2211,23 @@ var StarTopology = class {
1773
2211
  }
1774
2212
  }
1775
2213
  async onOffer(peer) {
1776
- invariant8(this._controller, "Not initialized.");
2214
+ invariant9(this._controller, "Not initialized.", {
2215
+ F: __dxlog_file10,
2216
+ L: 53,
2217
+ S: this,
2218
+ A: [
2219
+ "this._controller",
2220
+ "'Not initialized.'"
2221
+ ]
2222
+ });
1777
2223
  const { ownPeerId } = this._controller.getState();
1778
- log8("offer", {
2224
+ log9("offer", {
1779
2225
  peer,
1780
2226
  isCentral: peer.equals(this._centralPeer),
1781
2227
  isSelfCentral: ownPeerId.equals(this._centralPeer)
1782
2228
  }, {
1783
- F: __dxlog_file8,
1784
- L: 56,
2229
+ F: __dxlog_file10,
2230
+ L: 55,
1785
2231
  S: this,
1786
2232
  C: (f, a) => f(...a)
1787
2233
  });
@@ -1793,12 +2239,12 @@ var StarTopology = class {
1793
2239
 
1794
2240
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1795
2241
  import { Transform } from "@dxos/node-std/stream";
1796
- import invariant9 from "tiny-invariant";
1797
2242
  import { Event as Event6, Trigger } from "@dxos/async";
1798
2243
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
1799
- import { PublicKey as PublicKey8 } from "@dxos/keys";
1800
- import { log as log9, logInfo as logInfo2 } from "@dxos/log";
1801
- import { ComplexMap as ComplexMap6 } from "@dxos/util";
2244
+ import { invariant as invariant10 } from "@dxos/invariant";
2245
+ import { PublicKey as PublicKey9 } from "@dxos/keys";
2246
+ import { log as log10, logInfo as logInfo2 } from "@dxos/log";
2247
+ import { ComplexMap as ComplexMap7 } from "@dxos/util";
1802
2248
  function _ts_decorate4(decorators, target, key, desc) {
1803
2249
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1804
2250
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -1809,7 +2255,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1809
2255
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1810
2256
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1811
2257
  }
1812
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2258
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1813
2259
  var MEMORY_TRANSPORT_DELAY = 1;
1814
2260
  var createStreamDelay = (delay) => {
1815
2261
  return new Transform({
@@ -1822,30 +2268,41 @@ var createStreamDelay = (delay) => {
1822
2268
  var MemoryTransportFactory = {
1823
2269
  createTransport: (params) => new MemoryTransport(params)
1824
2270
  };
1825
- var _MemoryTransport = class {
2271
+ var MemoryTransport = class _MemoryTransport {
2272
+ static {
2273
+ // TODO(burdon): Remove static properties (inject context into constructor).
2274
+ this._connections = new ComplexMap7(PublicKey9.hash);
2275
+ }
1826
2276
  constructor(options) {
1827
- var _a;
1828
2277
  this.options = options;
1829
2278
  this.closed = new Event6();
1830
2279
  this.connected = new Event6();
1831
2280
  this.errors = new ErrorStream3();
1832
- this._instanceId = PublicKey8.random();
2281
+ this._instanceId = PublicKey9.random();
1833
2282
  this._remote = new Trigger();
1834
2283
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1835
2284
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1836
2285
  this._destroyed = false;
1837
- log9("creating", void 0, {
1838
- F: __dxlog_file9,
2286
+ log10("creating", void 0, {
2287
+ F: __dxlog_file11,
1839
2288
  L: 64,
1840
2289
  S: this,
1841
2290
  C: (f, a) => f(...a)
1842
2291
  });
1843
- invariant9(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2292
+ invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2293
+ F: __dxlog_file11,
2294
+ L: 66,
2295
+ S: this,
2296
+ A: [
2297
+ "!MemoryTransport._connections.has(this._instanceId)",
2298
+ "'Duplicate memory connection'"
2299
+ ]
2300
+ });
1844
2301
  _MemoryTransport._connections.set(this._instanceId, this);
1845
2302
  if (this.options.initiator) {
1846
2303
  setTimeout(async () => {
1847
- log9("sending signal", void 0, {
1848
- F: __dxlog_file9,
2304
+ log10("sending signal", void 0, {
2305
+ F: __dxlog_file11,
1849
2306
  L: 73,
1850
2307
  S: this,
1851
2308
  C: (f, a) => f(...a)
@@ -1862,7 +2319,7 @@ var _MemoryTransport = class {
1862
2319
  });
1863
2320
  } else {
1864
2321
  this._remote.wait({
1865
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2322
+ timeout: this.options.timeout ?? 1e3
1866
2323
  }).then((remoteId) => {
1867
2324
  if (this._destroyed) {
1868
2325
  return;
@@ -1874,11 +2331,19 @@ var _MemoryTransport = class {
1874
2331
  this.closed.emit();
1875
2332
  return;
1876
2333
  }
1877
- invariant9(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2334
+ invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2335
+ F: __dxlog_file11,
2336
+ L: 99,
2337
+ S: this,
2338
+ A: [
2339
+ "!this._remoteConnection._remoteConnection",
2340
+ "`Remote already connected: ${this._remoteInstanceId}`"
2341
+ ]
2342
+ });
1878
2343
  this._remoteConnection._remoteConnection = this;
1879
2344
  this._remoteConnection._remoteInstanceId = this._instanceId;
1880
- log9("connected", void 0, {
1881
- F: __dxlog_file9,
2345
+ log10("connected", void 0, {
2346
+ F: __dxlog_file11,
1882
2347
  L: 103,
1883
2348
  S: this,
1884
2349
  C: (f, a) => f(...a)
@@ -1895,8 +2360,8 @@ var _MemoryTransport = class {
1895
2360
  }
1896
2361
  }
1897
2362
  async destroy() {
1898
- log9("closing", void 0, {
1899
- F: __dxlog_file9,
2363
+ log10("closing", void 0, {
2364
+ F: __dxlog_file11,
1900
2365
  L: 124,
1901
2366
  S: this,
1902
2367
  C: (f, a) => f(...a)
@@ -1904,8 +2369,8 @@ var _MemoryTransport = class {
1904
2369
  this._destroyed = true;
1905
2370
  _MemoryTransport._connections.delete(this._instanceId);
1906
2371
  if (this._remoteConnection) {
1907
- log9("closing", void 0, {
1908
- F: __dxlog_file9,
2372
+ log10("closing", void 0, {
2373
+ F: __dxlog_file11,
1909
2374
  L: 129,
1910
2375
  S: this,
1911
2376
  C: (f, a) => f(...a)
@@ -1917,43 +2382,40 @@ var _MemoryTransport = class {
1917
2382
  this._remoteConnection.closed.emit();
1918
2383
  this._remoteConnection._remoteConnection = void 0;
1919
2384
  this._remoteConnection = void 0;
1920
- log9("closed", void 0, {
1921
- F: __dxlog_file9,
2385
+ log10("closed", void 0, {
2386
+ F: __dxlog_file11,
1922
2387
  L: 147,
1923
2388
  S: this,
1924
2389
  C: (f, a) => f(...a)
1925
2390
  });
1926
2391
  }
1927
2392
  this.closed.emit();
1928
- log9("closed", void 0, {
1929
- F: __dxlog_file9,
2393
+ log10("closed", void 0, {
2394
+ F: __dxlog_file11,
1930
2395
  L: 151,
1931
2396
  S: this,
1932
2397
  C: (f, a) => f(...a)
1933
2398
  });
1934
2399
  }
1935
2400
  signal({ payload }) {
1936
- log9("received signal", {
2401
+ log10("received signal", {
1937
2402
  payload
1938
2403
  }, {
1939
- F: __dxlog_file9,
2404
+ F: __dxlog_file11,
1940
2405
  L: 155,
1941
2406
  S: this,
1942
2407
  C: (f, a) => f(...a)
1943
2408
  });
1944
- if (!(payload == null ? void 0 : payload.transportId)) {
2409
+ if (!payload?.transportId) {
1945
2410
  return;
1946
2411
  }
1947
2412
  const transportId = payload.transportId;
1948
2413
  if (transportId) {
1949
- const remoteId = PublicKey8.fromHex(transportId);
2414
+ const remoteId = PublicKey9.fromHex(transportId);
1950
2415
  this._remote.wake(remoteId);
1951
2416
  }
1952
2417
  }
1953
2418
  };
1954
- var MemoryTransport = _MemoryTransport;
1955
- // TODO(burdon): Remove static properties (inject context into constructor).
1956
- MemoryTransport._connections = new ComplexMap6(PublicKey8.hash);
1957
2419
  _ts_decorate4([
1958
2420
  logInfo2
1959
2421
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -1963,53 +2425,52 @@ _ts_decorate4([
1963
2425
 
1964
2426
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
1965
2427
  import SimplePeerConstructor from "simple-peer";
1966
- import invariant10 from "tiny-invariant";
1967
2428
  import { Event as Event7 } from "@dxos/async";
1968
2429
  import { ErrorStream as ErrorStream4, raise as raise2 } from "@dxos/debug";
1969
- import { PublicKey as PublicKey9 } from "@dxos/keys";
1970
- import { log as log10 } from "@dxos/log";
2430
+ import { invariant as invariant11 } from "@dxos/invariant";
2431
+ import { PublicKey as PublicKey10 } from "@dxos/keys";
2432
+ import { log as log11 } from "@dxos/log";
1971
2433
  import { trace as trace4 } from "@dxos/protocols";
1972
2434
 
1973
2435
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
1974
2436
  var wrtc = null;
1975
2437
  try {
1976
2438
  wrtc = __require("@koush/wrtc");
1977
- } catch (e) {
2439
+ } catch {
1978
2440
  }
1979
2441
 
1980
2442
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
1981
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2443
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
1982
2444
  var WebRTCTransport = class {
1983
2445
  constructor(params) {
1984
- var _a;
1985
2446
  this.params = params;
1986
2447
  this._closed = false;
1987
2448
  this.closed = new Event7();
1988
2449
  this.connected = new Event7();
1989
2450
  this.errors = new ErrorStream4();
1990
- this._instanceId = PublicKey9.random().toHex();
1991
- log10.trace("dxos.mesh.webrtc-transport.constructor", trace4.begin({
2451
+ this._instanceId = PublicKey10.random().toHex();
2452
+ log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.begin({
1992
2453
  id: this._instanceId
1993
2454
  }), {
1994
- F: __dxlog_file10,
2455
+ F: __dxlog_file12,
1995
2456
  L: 39,
1996
2457
  S: this,
1997
2458
  C: (f, a) => f(...a)
1998
2459
  });
1999
- log10("created connection", params, {
2000
- F: __dxlog_file10,
2460
+ log11("created connection", params, {
2461
+ F: __dxlog_file12,
2001
2462
  L: 40,
2002
2463
  S: this,
2003
2464
  C: (f, a) => f(...a)
2004
2465
  });
2005
2466
  this._peer = new SimplePeerConstructor({
2006
2467
  initiator: this.params.initiator,
2007
- wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
2468
+ wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available")),
2008
2469
  config: this.params.webrtcConfig
2009
2470
  });
2010
2471
  this._peer.on("signal", async (data) => {
2011
- log10("signal", data, {
2012
- F: __dxlog_file10,
2472
+ log11("signal", data, {
2473
+ F: __dxlog_file12,
2013
2474
  L: 48,
2014
2475
  S: this,
2015
2476
  C: (f, a) => f(...a)
@@ -2021,8 +2482,8 @@ var WebRTCTransport = class {
2021
2482
  });
2022
2483
  });
2023
2484
  this._peer.on("connect", () => {
2024
- log10("connected", void 0, {
2025
- F: __dxlog_file10,
2485
+ log11("connected", void 0, {
2486
+ F: __dxlog_file12,
2026
2487
  L: 53,
2027
2488
  S: this,
2028
2489
  C: (f, a) => f(...a)
@@ -2031,8 +2492,8 @@ var WebRTCTransport = class {
2031
2492
  this.connected.emit();
2032
2493
  });
2033
2494
  this._peer.on("close", async () => {
2034
- log10("closed", void 0, {
2035
- F: __dxlog_file10,
2495
+ log11("closed", void 0, {
2496
+ F: __dxlog_file12,
2036
2497
  L: 59,
2037
2498
  S: this,
2038
2499
  C: (f, a) => f(...a)
@@ -2041,38 +2502,37 @@ var WebRTCTransport = class {
2041
2502
  this.closed.emit();
2042
2503
  });
2043
2504
  this._peer.on("error", async (err) => {
2044
- var _a2;
2045
2505
  this.errors.raise(err);
2046
2506
  try {
2047
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2507
+ if (typeof this._peer?._pc.getStats === "function") {
2048
2508
  this._peer._pc.getStats().then((stats) => {
2049
- log10.warn("report after webrtc error", {
2509
+ log11.warn("report after webrtc error", {
2050
2510
  config: this.params.webrtcConfig,
2051
2511
  stats
2052
2512
  }, {
2053
- F: __dxlog_file10,
2513
+ F: __dxlog_file12,
2054
2514
  L: 71,
2055
2515
  S: this,
2056
2516
  C: (f, a) => f(...a)
2057
2517
  });
2058
2518
  });
2059
2519
  }
2060
- } catch (e) {
2520
+ } catch {
2061
2521
  }
2062
2522
  await this.destroy();
2063
2523
  });
2064
- log10.trace("dxos.mesh.webrtc-transport.constructor", trace4.end({
2524
+ log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.end({
2065
2525
  id: this._instanceId
2066
2526
  }), {
2067
- F: __dxlog_file10,
2527
+ F: __dxlog_file12,
2068
2528
  L: 81,
2069
2529
  S: this,
2070
2530
  C: (f, a) => f(...a)
2071
2531
  });
2072
2532
  }
2073
2533
  async destroy() {
2074
- log10("closing...", void 0, {
2075
- F: __dxlog_file10,
2534
+ log11("closing...", void 0, {
2535
+ F: __dxlog_file12,
2076
2536
  L: 85,
2077
2537
  S: this,
2078
2538
  C: (f, a) => f(...a)
@@ -2081,8 +2541,8 @@ var WebRTCTransport = class {
2081
2541
  await this._disconnectStreams();
2082
2542
  this._peer.destroy();
2083
2543
  this.closed.emit();
2084
- log10("closed", void 0, {
2085
- F: __dxlog_file10,
2544
+ log11("closed", void 0, {
2545
+ F: __dxlog_file12,
2086
2546
  L: 90,
2087
2547
  S: this,
2088
2548
  C: (f, a) => f(...a)
@@ -2092,12 +2552,19 @@ var WebRTCTransport = class {
2092
2552
  if (this._closed) {
2093
2553
  return;
2094
2554
  }
2095
- invariant10(signal.payload.data, "Signal message must contain signal data.");
2555
+ invariant11(signal.payload.data, "Signal message must contain signal data.", {
2556
+ F: __dxlog_file12,
2557
+ L: 98,
2558
+ S: this,
2559
+ A: [
2560
+ "signal.payload.data",
2561
+ "'Signal message must contain signal data.'"
2562
+ ]
2563
+ });
2096
2564
  this._peer.signal(signal.payload.data);
2097
2565
  }
2098
2566
  async _disconnectStreams() {
2099
- var _a, _b, _c, _d;
2100
- (_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);
2567
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2101
2568
  }
2102
2569
  };
2103
2570
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2109,23 +2576,30 @@ var createWebRTCTransportFactory = (webrtcConfig) => ({
2109
2576
 
2110
2577
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2111
2578
  import { Duplex } from "@dxos/node-std/stream";
2112
- import invariant11 from "tiny-invariant";
2113
2579
  import { Stream } from "@dxos/codec-protobuf";
2114
- import { PublicKey as PublicKey10 } from "@dxos/keys";
2115
- import { log as log11 } from "@dxos/log";
2580
+ import { invariant as invariant12 } from "@dxos/invariant";
2581
+ import { PublicKey as PublicKey11 } from "@dxos/keys";
2582
+ import { log as log12 } from "@dxos/log";
2116
2583
  import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2117
- import { ComplexMap as ComplexMap7 } from "@dxos/util";
2118
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2584
+ import { ComplexMap as ComplexMap8 } from "@dxos/util";
2585
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2119
2586
  var WebRTCTransportService = class {
2120
2587
  // prettier-ignore
2121
2588
  constructor(_webrtcConfig) {
2122
2589
  this._webrtcConfig = _webrtcConfig;
2123
- this.transports = new ComplexMap7(PublicKey10.hash);
2590
+ this.transports = new ComplexMap8(PublicKey11.hash);
2124
2591
  }
2125
2592
  open(request) {
2126
2593
  const rpcStream = new Stream(({ ready, next, close }) => {
2127
2594
  const duplex = new Duplex({
2128
2595
  read: () => {
2596
+ const callbacks = [
2597
+ ...transportState.writeCallbacks
2598
+ ];
2599
+ transportState.writeCallbacks.length = 0;
2600
+ for (const cb of callbacks) {
2601
+ cb();
2602
+ }
2129
2603
  },
2130
2604
  write: function(chunk, _, callback) {
2131
2605
  next({
@@ -2177,30 +2651,53 @@ var WebRTCTransportService = class {
2177
2651
  });
2178
2652
  close();
2179
2653
  });
2180
- ready();
2181
- this.transports.set(request.proxyId, {
2654
+ const transportState = {
2182
2655
  transport,
2183
- stream: duplex
2184
- });
2656
+ stream: duplex,
2657
+ writeCallbacks: []
2658
+ };
2659
+ ready();
2660
+ this.transports.set(request.proxyId, transportState);
2185
2661
  });
2186
2662
  return rpcStream;
2187
2663
  }
2188
2664
  async sendSignal({ proxyId, signal }) {
2189
- invariant11(this.transports.has(proxyId));
2665
+ invariant12(this.transports.has(proxyId), void 0, {
2666
+ F: __dxlog_file13,
2667
+ L: 113,
2668
+ S: this,
2669
+ A: [
2670
+ "this.transports.has(proxyId)",
2671
+ ""
2672
+ ]
2673
+ });
2190
2674
  await this.transports.get(proxyId).transport.signal(signal);
2191
2675
  }
2192
2676
  async sendData({ proxyId, payload }) {
2193
- invariant11(this.transports.has(proxyId));
2194
- await this.transports.get(proxyId).stream.push(payload);
2677
+ invariant12(this.transports.has(proxyId), void 0, {
2678
+ F: __dxlog_file13,
2679
+ L: 118,
2680
+ S: this,
2681
+ A: [
2682
+ "this.transports.has(proxyId)",
2683
+ ""
2684
+ ]
2685
+ });
2686
+ const state = this.transports.get(proxyId);
2687
+ const bufferHasSpace = state.stream.push(payload);
2688
+ if (!bufferHasSpace) {
2689
+ await new Promise((resolve) => {
2690
+ state.writeCallbacks.push(resolve);
2691
+ });
2692
+ }
2195
2693
  }
2196
2694
  async close({ proxyId }) {
2197
- var _a, _b;
2198
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2199
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2695
+ await this.transports.get(proxyId)?.transport.destroy();
2696
+ await this.transports.get(proxyId)?.stream.end();
2200
2697
  this.transports.delete(proxyId);
2201
- log11("Closed.", void 0, {
2202
- F: __dxlog_file11,
2203
- L: 109,
2698
+ log12("Closed.", void 0, {
2699
+ F: __dxlog_file13,
2700
+ L: 132,
2204
2701
  S: this,
2205
2702
  C: (f, a) => f(...a)
2206
2703
  });
@@ -2208,21 +2705,22 @@ var WebRTCTransportService = class {
2208
2705
  };
2209
2706
 
2210
2707
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2211
- import invariant12 from "tiny-invariant";
2708
+ import { Writable } from "@dxos/node-std/stream";
2212
2709
  import { Event as Event8 } from "@dxos/async";
2213
- import { Context as Context5 } from "@dxos/context";
2710
+ import { Context as Context6 } from "@dxos/context";
2214
2711
  import { ErrorStream as ErrorStream5 } from "@dxos/debug";
2215
- import { PublicKey as PublicKey11 } from "@dxos/keys";
2216
- import { log as log12 } from "@dxos/log";
2712
+ import { invariant as invariant13 } from "@dxos/invariant";
2713
+ import { PublicKey as PublicKey12 } from "@dxos/keys";
2714
+ import { log as log13 } from "@dxos/log";
2217
2715
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2218
2716
  import { arrayToBuffer } from "@dxos/util";
2219
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2717
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2220
2718
  var WebRTCTransportProxy = class {
2221
2719
  // prettier-ignore
2222
2720
  constructor(_params) {
2223
2721
  this._params = _params;
2224
- this._proxyId = PublicKey11.random();
2225
- this._ctx = new Context5();
2722
+ this._proxyId = PublicKey12.random();
2723
+ this._ctx = new Context6();
2226
2724
  this.closed = new Event8();
2227
2725
  this._closed = false;
2228
2726
  this.connected = new Event8();
@@ -2233,9 +2731,9 @@ var WebRTCTransportProxy = class {
2233
2731
  });
2234
2732
  this._serviceStream.waitUntilReady().then(() => {
2235
2733
  this._serviceStream.subscribe(async (event) => {
2236
- log12("WebRTCTransportProxy: event", event, {
2237
- F: __dxlog_file12,
2238
- L: 50,
2734
+ log13("WebRTCTransportProxy: event", event, {
2735
+ F: __dxlog_file14,
2736
+ L: 51,
2239
2737
  S: this,
2240
2738
  C: (f, a) => f(...a)
2241
2739
  });
@@ -2247,28 +2745,24 @@ var WebRTCTransportProxy = class {
2247
2745
  await this._handleSignal(event.signal);
2248
2746
  }
2249
2747
  });
2250
- const dataListener = async (data) => {
2251
- try {
2252
- await this._params.bridgeService.sendData({
2748
+ this._params.stream.pipe(new Writable({
2749
+ write: (chunk, _, callback) => {
2750
+ this._params.bridgeService.sendData({
2253
2751
  proxyId: this._proxyId,
2254
- payload: data
2255
- });
2256
- } catch (err) {
2257
- log12.catch(err, void 0, {
2258
- F: __dxlog_file12,
2259
- L: 67,
2260
- S: this,
2261
- C: (f, a) => f(...a)
2752
+ payload: chunk
2753
+ }).then(() => callback(), (err) => {
2754
+ log13.catch(err, void 0, {
2755
+ F: __dxlog_file14,
2756
+ L: 69,
2757
+ S: this,
2758
+ C: (f, a) => f(...a)
2759
+ });
2262
2760
  });
2263
2761
  }
2264
- };
2265
- this._params.stream.on("data", dataListener);
2266
- this._ctx.onDispose(() => {
2267
- this._params.stream.off("data", dataListener);
2268
- });
2269
- }, (error) => log12.catch(error, void 0, {
2270
- F: __dxlog_file12,
2271
- L: 73,
2762
+ }));
2763
+ }, (error) => log13.catch(error, void 0, {
2764
+ F: __dxlog_file14,
2765
+ L: 75,
2272
2766
  S: this,
2273
2767
  C: (f, a) => f(...a)
2274
2768
  }));
@@ -2312,9 +2806,9 @@ var WebRTCTransportProxy = class {
2312
2806
  proxyId: this._proxyId
2313
2807
  });
2314
2808
  } catch (err) {
2315
- log12.catch(err, void 0, {
2316
- F: __dxlog_file12,
2317
- L: 124,
2809
+ log13.catch(err, void 0, {
2810
+ F: __dxlog_file14,
2811
+ L: 126,
2318
2812
  S: this,
2319
2813
  C: (f, a) => f(...a)
2320
2814
  });
@@ -2348,7 +2842,15 @@ var WebRTCTransportProxyFactory = class {
2348
2842
  return this;
2349
2843
  }
2350
2844
  createTransport(options) {
2351
- invariant12(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2845
+ invariant13(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2846
+ F: __dxlog_file14,
2847
+ L: 163,
2848
+ S: this,
2849
+ A: [
2850
+ "this._bridgeService",
2851
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2852
+ ]
2853
+ });
2352
2854
  const transport = new WebRTCTransportProxy({
2353
2855
  ...options,
2354
2856
  bridgeService: this._bridgeService
@@ -2383,6 +2885,8 @@ export {
2383
2885
  SwarmMessenger,
2384
2886
  Swarm,
2385
2887
  SwarmMapper,
2888
+ MAX_CONCURRENT_INITIATING_CONNECTIONS,
2889
+ ConnectionLimiter,
2386
2890
  EventType,
2387
2891
  ConnectionLog,
2388
2892
  NetworkManager,
@@ -2398,4 +2902,4 @@ export {
2398
2902
  WebRTCTransportProxyFactory,
2399
2903
  createTeleportProtocolFactory
2400
2904
  };
2401
- //# sourceMappingURL=chunk-MIYEP22D.mjs.map
2905
+ //# sourceMappingURL=chunk-5TRIVLI6.mjs.map