@dxos/network-manager 0.1.56-main.e47dfd1 → 0.1.56-main.e79d64a

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-5R7TDXUU.mjs → chunk-WQ3YXGWP.mjs} +702 -276
  2. package/dist/lib/browser/chunk-WQ3YXGWP.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +1 -1
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/browser/testing/index.mjs +23 -16
  6. package/dist/lib/browser/testing/index.mjs.map +3 -3
  7. package/dist/lib/node/index.cjs +700 -274
  8. package/dist/lib/node/index.cjs.map +3 -3
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +648 -271
  11. package/dist/lib/node/testing/index.cjs.map +3 -3
  12. package/dist/types/src/connection-log.d.ts.map +1 -1
  13. package/dist/types/src/network-manager.d.ts.map +1 -1
  14. package/dist/types/src/signal/swarm-messenger.d.ts +1 -1
  15. package/dist/types/src/signal/swarm-messenger.d.ts.map +1 -1
  16. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  17. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  18. package/dist/types/src/swarm/connection.d.ts +2 -1
  19. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  20. package/dist/types/src/swarm/peer.d.ts +1 -1
  21. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  22. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  23. package/dist/types/src/testing/test-builder.d.ts +4 -3
  24. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  25. package/dist/types/src/testing/test-wire-protocol.d.ts +13 -2
  26. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  27. package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -1
  28. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  29. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  30. package/dist/types/src/transport/webrtc-transport-proxy.d.ts +1 -1
  31. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  32. package/dist/types/src/transport/webrtc-transport-service.d.ts.map +1 -1
  33. package/dist/types/src/transport/webrtc-transport.d.ts +3 -3
  34. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  35. package/package.json +18 -18
  36. package/src/connection-log.ts +2 -0
  37. package/src/network-manager.ts +1 -2
  38. package/src/signal/swarm-messenger.ts +3 -4
  39. package/src/swarm/connection-limiter.ts +3 -2
  40. package/src/swarm/connection.ts +29 -6
  41. package/src/swarm/peer.ts +32 -8
  42. package/src/swarm/swarm.ts +6 -7
  43. package/src/testing/test-builder.ts +9 -5
  44. package/src/testing/test-wire-protocol.ts +22 -2
  45. package/src/tests/memory-transport.test.ts +1 -1
  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 +18 -16
  51. package/src/transport/webrtc-transport-service.ts +31 -8
  52. package/src/transport/webrtc-transport.ts +21 -7
  53. package/dist/lib/browser/chunk-5R7TDXUU.mjs.map +0 -7
@@ -46,8 +46,8 @@ var import_rpc = require("@dxos/rpc");
46
46
  var import_util11 = require("@dxos/util");
47
47
 
48
48
  // packages/core/mesh/network-manager/src/network-manager.ts
49
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
50
49
  var import_async7 = require("@dxos/async");
50
+ var import_invariant6 = require("@dxos/invariant");
51
51
  var import_keys8 = require("@dxos/keys");
52
52
  var import_log7 = require("@dxos/log");
53
53
  var import_messaging = require("@dxos/messaging");
@@ -62,11 +62,11 @@ var import_keys7 = require("@dxos/keys");
62
62
  var import_util5 = require("@dxos/util");
63
63
 
64
64
  // packages/core/mesh/network-manager/src/swarm/connection.ts
65
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
66
65
  var import_async = require("@dxos/async");
67
66
  var import_context = require("@dxos/context");
68
67
  var import_debug = require("@dxos/debug");
69
68
  var import_errors = require("@dxos/errors");
69
+ var import_invariant = require("@dxos/invariant");
70
70
  var import_keys = require("@dxos/keys");
71
71
  var import_log = require("@dxos/log");
72
72
  var import_protocols = require("@dxos/protocols");
@@ -138,6 +138,18 @@ var Connection = class {
138
138
  await this._flushSignalBuffer();
139
139
  });
140
140
  this._signallingDelay = STARTING_SIGNALLING_DELAY;
141
+ import_log.log.trace("dxos.mesh.connection.construct", {
142
+ sessionId: this.sessionId,
143
+ topic: this.topic,
144
+ localPeerId: this.ownId,
145
+ remotePeerId: this.remoteId,
146
+ initiator: this.initiator
147
+ }, {
148
+ F: __dxlog_file,
149
+ L: 96,
150
+ S: this,
151
+ C: (f, a) => f(...a)
152
+ });
141
153
  }
142
154
  get state() {
143
155
  return this._state;
@@ -152,12 +164,32 @@ var Connection = class {
152
164
  * Create an underlying transport and prepares it for the connection.
153
165
  */
154
166
  async openConnection() {
155
- (0, import_tiny_invariant.default)(this._state === ConnectionState.INITIAL, "Invalid state.");
167
+ (0, import_invariant.invariant)(this._state === ConnectionState.INITIAL, "Invalid state.", {
168
+ F: __dxlog_file,
169
+ L: 121,
170
+ S: this,
171
+ A: [
172
+ "this._state === ConnectionState.INITIAL",
173
+ "'Invalid state.'"
174
+ ]
175
+ });
156
176
  import_log.log.trace("dxos.mesh.connection.open-connection", import_protocols.trace.begin({
157
177
  id: this._instanceId
158
178
  }), {
159
179
  F: __dxlog_file,
160
- L: 114,
180
+ L: 122,
181
+ S: this,
182
+ C: (f, a) => f(...a)
183
+ });
184
+ import_log.log.trace("dxos.mesh.connection.open", {
185
+ sessionId: this.sessionId,
186
+ topic: this.topic,
187
+ localPeerId: this.ownId,
188
+ remotePeerId: this.remoteId,
189
+ initiator: this.initiator
190
+ }, {
191
+ F: __dxlog_file,
192
+ L: 123,
161
193
  S: this,
162
194
  C: (f, a) => f(...a)
163
195
  });
@@ -168,13 +200,21 @@ var Connection = class {
168
200
  this._protocol.stream.on("close", () => {
169
201
  (0, import_log.log)("protocol stream closed", void 0, {
170
202
  F: __dxlog_file,
171
- L: 125,
203
+ L: 140,
172
204
  S: this,
173
205
  C: (f, a) => f(...a)
174
206
  });
175
207
  this.close().catch((err) => this.errors.raise(err));
176
208
  });
177
- (0, import_tiny_invariant.default)(!this._transport);
209
+ (0, import_invariant.invariant)(!this._transport, void 0, {
210
+ F: __dxlog_file,
211
+ L: 144,
212
+ S: this,
213
+ A: [
214
+ "!this._transport",
215
+ ""
216
+ ]
217
+ });
178
218
  this._transport = this._transportFactory.createTransport({
179
219
  initiator: this.initiator,
180
220
  stream: this._protocol.stream,
@@ -188,6 +228,9 @@ var Connection = class {
188
228
  this.close().catch((err) => this.errors.raise(err));
189
229
  });
190
230
  this._transport.errors.handle((err) => {
231
+ if (!this.closeReason) {
232
+ this.closeReason = err?.message;
233
+ }
191
234
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
192
235
  this.errors.raise(err);
193
236
  }
@@ -200,13 +243,15 @@ var Connection = class {
200
243
  id: this._instanceId
201
244
  }), {
202
245
  F: __dxlog_file,
203
- L: 158,
246
+ L: 176,
204
247
  S: this,
205
248
  C: (f, a) => f(...a)
206
249
  });
207
250
  }
208
- async close() {
209
- var _a2;
251
+ async close(err) {
252
+ if (!this.closeReason) {
253
+ this.closeReason = err?.message;
254
+ }
210
255
  if (this._state === ConnectionState.CLOSED) {
211
256
  return;
212
257
  }
@@ -216,26 +261,26 @@ var Connection = class {
216
261
  peerId: this.ownId
217
262
  }, {
218
263
  F: __dxlog_file,
219
- L: 170,
264
+ L: 191,
220
265
  S: this,
221
266
  C: (f, a) => f(...a)
222
267
  });
223
268
  try {
224
269
  await this._protocol.destroy();
225
- } catch (err) {
226
- import_log.log.catch(err, void 0, {
270
+ } catch (err2) {
271
+ import_log.log.catch(err2, void 0, {
227
272
  F: __dxlog_file,
228
- L: 176,
273
+ L: 197,
229
274
  S: this,
230
275
  C: (f, a) => f(...a)
231
276
  });
232
277
  }
233
278
  try {
234
- await ((_a2 = this._transport) == null ? void 0 : _a2.destroy());
235
- } catch (err) {
236
- import_log.log.catch(err, void 0, {
279
+ await this._transport?.destroy();
280
+ } catch (err2) {
281
+ import_log.log.catch(err2, void 0, {
237
282
  F: __dxlog_file,
238
- L: 183,
283
+ L: 204,
239
284
  S: this,
240
285
  C: (f, a) => f(...a)
241
286
  });
@@ -244,7 +289,7 @@ var Connection = class {
244
289
  peerId: this.ownId
245
290
  }, {
246
291
  F: __dxlog_file,
247
- L: 186,
292
+ L: 207,
248
293
  S: this,
249
294
  C: (f, a) => f(...a)
250
295
  });
@@ -259,8 +304,10 @@ var Connection = class {
259
304
  return;
260
305
  }
261
306
  try {
262
- await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
263
- this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
307
+ if (process.env.NODE_ENV !== "test") {
308
+ await (0, import_context.cancelWithContext)(this._ctx, (0, import_async.sleep)(this._signallingDelay));
309
+ this._signallingDelay = Math.min(this._signallingDelay * 2, MAX_SIGNALLING_DELAY);
310
+ }
264
311
  const signals = [
265
312
  ...this._outgoingSignalBuffer
266
313
  ];
@@ -284,7 +331,7 @@ var Connection = class {
284
331
  err
285
332
  }, {
286
333
  F: __dxlog_file,
287
- L: 220,
334
+ L: 243,
288
335
  S: this,
289
336
  C: (f, a) => f(...a)
290
337
  });
@@ -295,21 +342,52 @@ var Connection = class {
295
342
  * Receive a signal from the remote peer.
296
343
  */
297
344
  async signal(msg) {
298
- var _a2, _b, _c;
299
- (0, import_tiny_invariant.default)(msg.sessionId);
345
+ (0, import_invariant.invariant)(msg.sessionId, void 0, {
346
+ F: __dxlog_file,
347
+ L: 252,
348
+ S: this,
349
+ A: [
350
+ "msg.sessionId",
351
+ ""
352
+ ]
353
+ });
300
354
  if (!msg.sessionId.equals(this.sessionId)) {
301
355
  (0, import_log.log)("dropping signal for incorrect session id", void 0, {
302
356
  F: __dxlog_file,
303
- L: 231,
357
+ L: 254,
304
358
  S: this,
305
359
  C: (f, a) => f(...a)
306
360
  });
307
361
  return;
308
362
  }
309
- (0, import_tiny_invariant.default)(msg.data.signal || msg.data.signalBatch);
310
- (0, import_tiny_invariant.default)((_a2 = msg.author) == null ? void 0 : _a2.equals(this.remoteId));
311
- (0, import_tiny_invariant.default)((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
312
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
363
+ (0, import_invariant.invariant)(msg.data.signal || msg.data.signalBatch, void 0, {
364
+ F: __dxlog_file,
365
+ L: 257,
366
+ S: this,
367
+ A: [
368
+ "msg.data.signal || msg.data.signalBatch",
369
+ ""
370
+ ]
371
+ });
372
+ (0, import_invariant.invariant)(msg.author?.equals(this.remoteId), void 0, {
373
+ F: __dxlog_file,
374
+ L: 258,
375
+ S: this,
376
+ A: [
377
+ "msg.author?.equals(this.remoteId)",
378
+ ""
379
+ ]
380
+ });
381
+ (0, import_invariant.invariant)(msg.recipient?.equals(this.ownId), void 0, {
382
+ F: __dxlog_file,
383
+ L: 259,
384
+ S: this,
385
+ A: [
386
+ "msg.recipient?.equals(this.ownId)",
387
+ ""
388
+ ]
389
+ });
390
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
313
391
  msg.data.signal
314
392
  ];
315
393
  for (const signal of signals) {
@@ -323,20 +401,28 @@ var Connection = class {
323
401
  msg: msg.data
324
402
  }, {
325
403
  F: __dxlog_file,
326
- L: 245,
404
+ L: 268,
327
405
  S: this,
328
406
  C: (f, a) => f(...a)
329
407
  });
330
408
  this._incomingSignalBuffer.push(signal);
331
409
  } else {
332
- (0, import_tiny_invariant.default)(this._transport, "Connection not ready to accept signals.");
410
+ (0, import_invariant.invariant)(this._transport, "Connection not ready to accept signals.", {
411
+ F: __dxlog_file,
412
+ L: 271,
413
+ S: this,
414
+ A: [
415
+ "this._transport",
416
+ "'Connection not ready to accept signals.'"
417
+ ]
418
+ });
333
419
  (0, import_log.log)("received signal", {
334
420
  peerId: this.ownId,
335
421
  remoteId: this.remoteId,
336
422
  msg: msg.data
337
423
  }, {
338
424
  F: __dxlog_file,
339
- L: 249,
425
+ L: 272,
340
426
  S: this,
341
427
  C: (f, a) => f(...a)
342
428
  });
@@ -351,11 +437,19 @@ var Connection = class {
351
437
  peerId: this.ownId
352
438
  }, {
353
439
  F: __dxlog_file,
354
- L: 256,
440
+ L: 279,
355
441
  S: this,
356
442
  C: (f, a) => f(...a)
357
443
  });
358
- (0, import_tiny_invariant.default)(state !== this._state, "Already in this state.");
444
+ (0, import_invariant.invariant)(state !== this._state, "Already in this state.", {
445
+ F: __dxlog_file,
446
+ L: 280,
447
+ S: this,
448
+ A: [
449
+ "state !== this._state",
450
+ "'Already in this state.'"
451
+ ]
452
+ });
359
453
  this._state = state;
360
454
  this.stateChanged.emit(state);
361
455
  }
@@ -365,18 +459,18 @@ _ts_decorate([
365
459
  ], Connection.prototype, "close", null);
366
460
 
367
461
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
368
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
369
462
  var import_async3 = require("@dxos/async");
370
463
  var import_context4 = require("@dxos/context");
371
464
  var import_debug2 = require("@dxos/debug");
465
+ var import_invariant4 = require("@dxos/invariant");
372
466
  var import_keys4 = require("@dxos/keys");
373
467
  var import_log4 = require("@dxos/log");
374
468
  var import_protocols3 = require("@dxos/protocols");
375
469
  var import_util2 = require("@dxos/util");
376
470
 
377
471
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
378
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
379
472
  var import_context2 = require("@dxos/context");
473
+ var import_invariant2 = require("@dxos/invariant");
380
474
  var import_keys2 = require("@dxos/keys");
381
475
  var import_log2 = require("@dxos/log");
382
476
  var import_protocols2 = require("@dxos/protocols");
@@ -393,7 +487,6 @@ var SwarmMessenger = class {
393
487
  this._topic = topic;
394
488
  }
395
489
  async receiveMessage({ author, recipient, payload }) {
396
- var _a2, _b, _c, _d;
397
490
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
398
491
  return;
399
492
  }
@@ -407,25 +500,25 @@ var SwarmMessenger = class {
407
500
  msg: message
408
501
  }, {
409
502
  F: __dxlog_file2,
410
- L: 70,
503
+ L: 69,
411
504
  S: this,
412
505
  C: (f, a) => f(...a)
413
506
  });
414
- if ((_a2 = message.data) == null ? void 0 : _a2.offer) {
507
+ if (message.data?.offer) {
415
508
  await this._handleOffer({
416
509
  author,
417
510
  recipient,
418
511
  message
419
512
  });
420
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
513
+ } else if (message.data?.answer) {
421
514
  await this._resolveAnswers(message);
422
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
515
+ } else if (message.data?.signal) {
423
516
  await this._handleSignal({
424
517
  author,
425
518
  recipient,
426
519
  message
427
520
  });
428
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
521
+ } else if (message.data?.signalBatch) {
429
522
  await this._handleSignal({
430
523
  author,
431
524
  recipient,
@@ -436,15 +529,22 @@ var SwarmMessenger = class {
436
529
  message
437
530
  }, {
438
531
  F: __dxlog_file2,
439
- L: 81,
532
+ L: 80,
440
533
  S: this,
441
534
  C: (f, a) => f(...a)
442
535
  });
443
536
  }
444
537
  }
445
538
  async signal(message) {
446
- var _a2, _b;
447
- (0, import_tiny_invariant2.default)(((_a2 = message.data) == null ? void 0 : _a2.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
539
+ (0, import_invariant2.invariant)(message.data?.signal || message.data?.signalBatch, "Invalid message", {
540
+ F: __dxlog_file2,
541
+ L: 85,
542
+ S: this,
543
+ A: [
544
+ "message.data?.signal || message.data?.signalBatch",
545
+ "'Invalid message'"
546
+ ]
547
+ });
448
548
  await this._sendReliableMessage({
449
549
  author: message.author,
450
550
  recipient: message.recipient,
@@ -468,11 +568,10 @@ var SwarmMessenger = class {
468
568
  });
469
569
  }
470
570
  async _sendReliableMessage({ author, recipient, message }) {
471
- var _a2;
472
571
  const networkMessage = {
473
572
  ...message,
474
573
  // Setting unique message_id if it not specified yet.
475
- messageId: (_a2 = message.messageId) != null ? _a2 : import_keys2.PublicKey.random()
574
+ messageId: message.messageId ?? import_keys2.PublicKey.random()
476
575
  };
477
576
  (0, import_log2.log)("sending", {
478
577
  from: author,
@@ -480,7 +579,7 @@ var SwarmMessenger = class {
480
579
  msg: networkMessage
481
580
  }, {
482
581
  F: __dxlog_file2,
483
- L: 124,
582
+ L: 123,
484
583
  S: this,
485
584
  C: (f, a) => f(...a)
486
585
  });
@@ -494,17 +593,32 @@ var SwarmMessenger = class {
494
593
  });
495
594
  }
496
595
  async _resolveAnswers(message) {
497
- var _a2, _b, _c;
498
- (0, import_tiny_invariant2.default)((_b = (_a2 = message.data) == null ? void 0 : _a2.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
596
+ (0, import_invariant2.invariant)(message.data?.answer?.offerMessageId, "No offerMessageId", {
597
+ F: __dxlog_file2,
598
+ L: 135,
599
+ S: this,
600
+ A: [
601
+ "message.data?.answer?.offerMessageId",
602
+ "'No offerMessageId'"
603
+ ]
604
+ });
499
605
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
500
606
  if (offerRecord) {
501
607
  this._offerRecords.delete(message.data.answer.offerMessageId);
502
- (0, import_tiny_invariant2.default)((_c = message.data) == null ? void 0 : _c.answer, "No answer");
608
+ (0, import_invariant2.invariant)(message.data?.answer, "No answer", {
609
+ F: __dxlog_file2,
610
+ L: 139,
611
+ S: this,
612
+ A: [
613
+ "message.data?.answer",
614
+ "'No answer'"
615
+ ]
616
+ });
503
617
  (0, import_log2.log)("resolving", {
504
618
  answer: message.data.answer
505
619
  }, {
506
620
  F: __dxlog_file2,
507
- L: 141,
621
+ L: 140,
508
622
  S: this,
509
623
  C: (f, a) => f(...a)
510
624
  });
@@ -512,7 +626,15 @@ var SwarmMessenger = class {
512
626
  }
513
627
  }
514
628
  async _handleOffer({ author, recipient, message }) {
515
- (0, import_tiny_invariant2.default)(message.data.offer, "No offer");
629
+ (0, import_invariant2.invariant)(message.data.offer, "No offer", {
630
+ F: __dxlog_file2,
631
+ L: 154,
632
+ S: this,
633
+ A: [
634
+ "message.data.offer",
635
+ "'No offer'"
636
+ ]
637
+ });
516
638
  const offerMessage = {
517
639
  author,
518
640
  recipient,
@@ -540,15 +662,31 @@ var SwarmMessenger = class {
540
662
  err
541
663
  }, {
542
664
  F: __dxlog_file2,
543
- L: 175,
665
+ L: 174,
544
666
  S: this,
545
667
  C: (f, a) => f(...a)
546
668
  });
547
669
  }
548
670
  }
549
671
  async _handleSignal({ author, recipient, message }) {
550
- (0, import_tiny_invariant2.default)(message.messageId);
551
- (0, import_tiny_invariant2.default)(message.data.signal || message.data.signalBatch, "Invalid message");
672
+ (0, import_invariant2.invariant)(message.messageId, void 0, {
673
+ F: __dxlog_file2,
674
+ L: 187,
675
+ S: this,
676
+ A: [
677
+ "message.messageId",
678
+ ""
679
+ ]
680
+ });
681
+ (0, import_invariant2.invariant)(message.data.signal || message.data.signalBatch, "Invalid message", {
682
+ F: __dxlog_file2,
683
+ L: 188,
684
+ S: this,
685
+ A: [
686
+ "message.data.signal || message.data.signalBatch",
687
+ "'Invalid message'"
688
+ ]
689
+ });
552
690
  const signalMessage = {
553
691
  author,
554
692
  recipient,
@@ -563,10 +701,10 @@ var SwarmMessenger = class {
563
701
  };
564
702
 
565
703
  // packages/core/mesh/network-manager/src/swarm/peer.ts
566
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
567
704
  var import_async2 = require("@dxos/async");
568
705
  var import_context3 = require("@dxos/context");
569
706
  var import_errors2 = require("@dxos/errors");
707
+ var import_invariant3 = require("@dxos/invariant");
570
708
  var import_keys3 = require("@dxos/keys");
571
709
  var import_log3 = require("@dxos/log");
572
710
  function _ts_decorate2(decorators, target, key, desc) {
@@ -582,6 +720,7 @@ function _ts_decorate2(decorators, target, key, desc) {
582
720
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
583
721
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
584
722
  var Peer = class {
723
+ // TODO(burdon): Convert to map?
585
724
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
586
725
  this.id = id;
587
726
  this.topic = topic;
@@ -601,7 +740,6 @@ var Peer = class {
601
740
  * Respond to remote offer.
602
741
  */
603
742
  async onOffer(message) {
604
- var _a2;
605
743
  const remoteId = message.author;
606
744
  if (this.connection || this.initiating) {
607
745
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -609,15 +747,15 @@ var Peer = class {
609
747
  localPeerId: this.id,
610
748
  topic: this.topic,
611
749
  remotePeerId: this.localPeerId,
612
- sessionId: (_a2 = this.connection) == null ? void 0 : _a2.sessionId
750
+ sessionId: this.connection?.sessionId
613
751
  }, {
614
752
  F: __dxlog_file3,
615
- L: 107,
753
+ L: 108,
616
754
  S: this,
617
755
  C: (f, a) => f(...a)
618
756
  });
619
757
  if (this.connection) {
620
- await this.closeConnection();
758
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
621
759
  }
622
760
  } else {
623
761
  return {
@@ -627,7 +765,15 @@ var Peer = class {
627
765
  }
628
766
  if (await this._callbacks.onOffer(remoteId)) {
629
767
  if (!this.connection) {
630
- (0, import_tiny_invariant3.default)(message.sessionId);
768
+ (0, import_invariant3.invariant)(message.sessionId, void 0, {
769
+ F: __dxlog_file3,
770
+ L: 128,
771
+ S: this,
772
+ A: [
773
+ "message.sessionId",
774
+ ""
775
+ ]
776
+ });
631
777
  const connection = this._createConnection(false, message.sessionId);
632
778
  try {
633
779
  await this._connectionLimiter.connecting(message.sessionId);
@@ -641,12 +787,12 @@ var Peer = class {
641
787
  err
642
788
  }, {
643
789
  F: __dxlog_file3,
644
- L: 135,
790
+ L: 136,
645
791
  S: this,
646
792
  C: (f, a) => f(...a)
647
793
  });
648
794
  }
649
- await this.closeConnection();
795
+ await this.closeConnection(err);
650
796
  }
651
797
  return {
652
798
  accept: true
@@ -661,8 +807,24 @@ var Peer = class {
661
807
  * Initiate a connection to the remote peer.
662
808
  */
663
809
  async initiateConnection() {
664
- (0, import_tiny_invariant3.default)(!this.initiating, "Initiation in progress.");
665
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
810
+ (0, import_invariant3.invariant)(!this.initiating, "Initiation in progress.", {
811
+ F: __dxlog_file3,
812
+ L: 153,
813
+ S: this,
814
+ A: [
815
+ "!this.initiating",
816
+ "'Initiation in progress.'"
817
+ ]
818
+ });
819
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
820
+ F: __dxlog_file3,
821
+ L: 154,
822
+ S: this,
823
+ A: [
824
+ "!this.connection",
825
+ "'Already connected.'"
826
+ ]
827
+ });
666
828
  const sessionId = import_keys3.PublicKey.random();
667
829
  (0, import_log3.log)("initiating...", {
668
830
  id: this.id,
@@ -671,7 +833,7 @@ var Peer = class {
671
833
  sessionId
672
834
  }, {
673
835
  F: __dxlog_file3,
674
- L: 155,
836
+ L: 156,
675
837
  S: this,
676
838
  C: (f, a) => f(...a)
677
839
  });
@@ -695,14 +857,14 @@ var Peer = class {
695
857
  remoteId: this.id
696
858
  }, {
697
859
  F: __dxlog_file3,
698
- L: 170,
860
+ L: 171,
699
861
  S: this,
700
862
  C: (f, a) => f(...a)
701
863
  });
702
864
  if (connection.state !== ConnectionState.INITIAL) {
703
865
  (0, import_log3.log)("ignoring response", void 0, {
704
866
  F: __dxlog_file3,
705
- L: 172,
867
+ L: 173,
706
868
  S: this,
707
869
  C: (f, a) => f(...a)
708
870
  });
@@ -722,11 +884,11 @@ var Peer = class {
722
884
  remoteId: this.id
723
885
  }, {
724
886
  F: __dxlog_file3,
725
- L: 183,
887
+ L: 184,
726
888
  S: this,
727
889
  C: (f, a) => f(...a)
728
890
  });
729
- await this.closeConnection();
891
+ await this.closeConnection(err);
730
892
  throw err;
731
893
  } finally {
732
894
  this.initiating = false;
@@ -737,7 +899,6 @@ var Peer = class {
737
899
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
738
900
  */
739
901
  _createConnection(initiator, sessionId) {
740
- var _a2;
741
902
  (0, import_log3.log)("creating connection", {
742
903
  topic: this.topic,
743
904
  peerId: this.localPeerId,
@@ -746,11 +907,19 @@ var Peer = class {
746
907
  sessionId
747
908
  }, {
748
909
  F: __dxlog_file3,
749
- L: 197,
910
+ L: 198,
750
911
  S: this,
751
912
  C: (f, a) => f(...a)
752
913
  });
753
- (0, import_tiny_invariant3.default)(!this.connection, "Already connected.");
914
+ (0, import_invariant3.invariant)(!this.connection, "Already connected.", {
915
+ F: __dxlog_file3,
916
+ L: 205,
917
+ S: this,
918
+ A: [
919
+ "!this.connection",
920
+ "'Already connected.'"
921
+ ]
922
+ });
754
923
  const connection = new Connection(
755
924
  this.topic,
756
925
  this.localPeerId,
@@ -768,7 +937,7 @@ var Peer = class {
768
937
  this._transportFactory
769
938
  );
770
939
  this._callbacks.onInitiated(connection);
771
- void ((_a2 = this._connectionCtx) == null ? void 0 : _a2.dispose());
940
+ void this._connectionCtx?.dispose();
772
941
  this._connectionCtx = this._ctx.derive();
773
942
  connection.stateChanged.on((state) => {
774
943
  switch (state) {
@@ -777,6 +946,18 @@ var Peer = class {
777
946
  this._lastConnectionTime = Date.now();
778
947
  this._callbacks.onConnected();
779
948
  this._connectionLimiter.doneConnecting(sessionId);
949
+ import_log3.log.trace("dxos.mesh.connection.connected", {
950
+ topic: this.topic,
951
+ localPeerId: this.localPeerId,
952
+ remotePeerId: this.id,
953
+ sessionId,
954
+ initiator
955
+ }, {
956
+ F: __dxlog_file3,
957
+ L: 231,
958
+ S: this,
959
+ C: (f, a) => f(...a)
960
+ });
780
961
  break;
781
962
  }
782
963
  case ConnectionState.CLOSED: {
@@ -787,11 +968,31 @@ var Peer = class {
787
968
  initiator
788
969
  }, {
789
970
  F: __dxlog_file3,
790
- L: 234,
971
+ L: 242,
972
+ S: this,
973
+ C: (f, a) => f(...a)
974
+ });
975
+ (0, import_invariant3.invariant)(this.connection === connection, "Connection mismatch (race condition).", {
976
+ F: __dxlog_file3,
977
+ L: 243,
978
+ S: this,
979
+ A: [
980
+ "this.connection === connection",
981
+ "'Connection mismatch (race condition).'"
982
+ ]
983
+ });
984
+ import_log3.log.trace("dxos.mesh.connection.closed", {
985
+ topic: this.topic,
986
+ localPeerId: this.localPeerId,
987
+ remotePeerId: this.id,
988
+ sessionId,
989
+ initiator
990
+ }, {
991
+ F: __dxlog_file3,
992
+ L: 245,
791
993
  S: this,
792
994
  C: (f, a) => f(...a)
793
995
  });
794
- (0, import_tiny_invariant3.default)(this.connection === connection, "Connection mismatch (race condition).");
795
996
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
796
997
  this._availableAfter = 0;
797
998
  } else {
@@ -818,16 +1019,29 @@ var Peer = class {
818
1019
  err
819
1020
  }, {
820
1021
  F: __dxlog_file3,
821
- L: 263,
1022
+ L: 279,
1023
+ S: this,
1024
+ C: (f, a) => f(...a)
1025
+ });
1026
+ import_log3.log.trace("dxos.mesh.connection.error", {
1027
+ topic: this.topic,
1028
+ localPeerId: this.localPeerId,
1029
+ remotePeerId: this.id,
1030
+ sessionId,
1031
+ initiator,
1032
+ err
1033
+ }, {
1034
+ F: __dxlog_file3,
1035
+ L: 280,
822
1036
  S: this,
823
1037
  C: (f, a) => f(...a)
824
1038
  });
825
- void this.closeConnection();
1039
+ void this.closeConnection(err);
826
1040
  });
827
1041
  this.connection = connection;
828
1042
  return connection;
829
1043
  }
830
- async closeConnection() {
1044
+ async closeConnection(err) {
831
1045
  if (!this.connection) {
832
1046
  return;
833
1047
  }
@@ -837,17 +1051,17 @@ var Peer = class {
837
1051
  sessionId: connection.sessionId
838
1052
  }, {
839
1053
  F: __dxlog_file3,
840
- L: 279,
1054
+ L: 303,
841
1055
  S: this,
842
1056
  C: (f, a) => f(...a)
843
1057
  });
844
- await connection.close();
1058
+ await connection.close(err);
845
1059
  (0, import_log3.log)("closed", {
846
1060
  peerId: this.id,
847
1061
  sessionId: connection.sessionId
848
1062
  }, {
849
1063
  F: __dxlog_file3,
850
- L: 285,
1064
+ L: 309,
851
1065
  S: this,
852
1066
  C: (f, a) => f(...a)
853
1067
  });
@@ -858,7 +1072,7 @@ var Peer = class {
858
1072
  message
859
1073
  }, {
860
1074
  F: __dxlog_file3,
861
- L: 290,
1075
+ L: 314,
862
1076
  S: this,
863
1077
  C: (f, a) => f(...a)
864
1078
  });
@@ -867,18 +1081,17 @@ var Peer = class {
867
1081
  await this.connection.signal(message);
868
1082
  }
869
1083
  async destroy() {
870
- var _a2;
871
1084
  await this._ctx.dispose();
872
1085
  (0, import_log3.log)("Destroying peer", {
873
1086
  peerId: this.id,
874
1087
  topic: this.topic
875
1088
  }, {
876
1089
  F: __dxlog_file3,
877
- L: 300,
1090
+ L: 324,
878
1091
  S: this,
879
1092
  C: (f, a) => f(...a)
880
1093
  });
881
- await ((_a2 = this == null ? void 0 : this.connection) == null ? void 0 : _a2.close());
1094
+ await this?.connection?.close();
882
1095
  }
883
1096
  };
884
1097
  _ts_decorate2([
@@ -923,9 +1136,9 @@ var Swarm = class {
923
1136
  this._transportFactory = _transportFactory;
924
1137
  this._label = _label;
925
1138
  this._connectionLimiter = _connectionLimiter;
926
- this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
927
1139
  this._ctx = new import_context4.Context();
928
1140
  this._listeningHandle = void 0;
1141
+ this._peers = new import_util2.ComplexMap(import_keys4.PublicKey.hash);
929
1142
  this._instanceId = import_keys4.PublicKey.random().toHex();
930
1143
  this.connectionAdded = new import_async3.Event();
931
1144
  this.disconnected = new import_async3.Event();
@@ -939,7 +1152,7 @@ var Swarm = class {
939
1152
  }
940
1153
  }), {
941
1154
  F: __dxlog_file4,
942
- L: 88,
1155
+ L: 87,
943
1156
  S: this,
944
1157
  C: (f, a) => f(...a)
945
1158
  });
@@ -947,7 +1160,7 @@ var Swarm = class {
947
1160
  peerId: _ownPeerId
948
1161
  }, {
949
1162
  F: __dxlog_file4,
950
- L: 92,
1163
+ L: 91,
951
1164
  S: this,
952
1165
  C: (f, a) => f(...a)
953
1166
  });
@@ -962,7 +1175,7 @@ var Swarm = class {
962
1175
  id: this._instanceId
963
1176
  }), {
964
1177
  F: __dxlog_file4,
965
- L: 101,
1178
+ L: 100,
966
1179
  S: this,
967
1180
  C: (f, a) => f(...a)
968
1181
  });
@@ -983,7 +1196,15 @@ var Swarm = class {
983
1196
  return this._topic;
984
1197
  }
985
1198
  async open() {
986
- (0, import_tiny_invariant4.default)(!this._listeningHandle);
1199
+ (0, import_invariant4.invariant)(!this._listeningHandle, void 0, {
1200
+ F: __dxlog_file4,
1201
+ L: 127,
1202
+ S: this,
1203
+ A: [
1204
+ "!this._listeningHandle",
1205
+ ""
1206
+ ]
1207
+ });
987
1208
  this._listeningHandle = await this._messenger.listen({
988
1209
  peerId: this._ownPeerId,
989
1210
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -992,7 +1213,7 @@ var Swarm = class {
992
1213
  err
993
1214
  }, {
994
1215
  F: __dxlog_file4,
995
- L: 135,
1216
+ L: 134,
996
1217
  S: this,
997
1218
  C: (f, a) => f(...a)
998
1219
  }));
@@ -1000,27 +1221,34 @@ var Swarm = class {
1000
1221
  });
1001
1222
  }
1002
1223
  async destroy() {
1003
- var _a2;
1004
1224
  (0, import_log4.log)("destroying...", void 0, {
1005
1225
  F: __dxlog_file4,
1006
- L: 141,
1226
+ L: 140,
1007
1227
  S: this,
1008
1228
  C: (f, a) => f(...a)
1009
1229
  });
1010
- await ((_a2 = this._listeningHandle) == null ? void 0 : _a2.unsubscribe());
1230
+ await this._listeningHandle?.unsubscribe();
1011
1231
  this._listeningHandle = void 0;
1012
1232
  await this._ctx.dispose();
1013
1233
  await this._topology.destroy();
1014
1234
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
1015
1235
  (0, import_log4.log)("destroyed", void 0, {
1016
1236
  F: __dxlog_file4,
1017
- L: 148,
1237
+ L: 147,
1018
1238
  S: this,
1019
1239
  C: (f, a) => f(...a)
1020
1240
  });
1021
1241
  }
1022
1242
  async setTopology(topology) {
1023
- (0, import_tiny_invariant4.default)(!this._ctx.disposed, "Swarm is offline");
1243
+ (0, import_invariant4.invariant)(!this._ctx.disposed, "Swarm is offline", {
1244
+ F: __dxlog_file4,
1245
+ L: 151,
1246
+ S: this,
1247
+ A: [
1248
+ "!this._ctx.disposed",
1249
+ "'Swarm is offline'"
1250
+ ]
1251
+ });
1024
1252
  if (topology === this._topology) {
1025
1253
  return;
1026
1254
  }
@@ -1029,7 +1257,7 @@ var Swarm = class {
1029
1257
  topology: getClassName(topology)
1030
1258
  }, {
1031
1259
  F: __dxlog_file4,
1032
- L: 156,
1260
+ L: 155,
1033
1261
  S: this,
1034
1262
  C: (f, a) => f(...a)
1035
1263
  });
@@ -1039,19 +1267,18 @@ var Swarm = class {
1039
1267
  this._topology.update();
1040
1268
  }
1041
1269
  onSwarmEvent(swarmEvent) {
1042
- var _a2;
1043
1270
  (0, import_log4.log)("swarm event", {
1044
1271
  swarmEvent
1045
1272
  }, {
1046
1273
  F: __dxlog_file4,
1047
- L: 169,
1274
+ L: 168,
1048
1275
  S: this,
1049
1276
  C: (f, a) => f(...a)
1050
1277
  });
1051
1278
  if (this._ctx.disposed) {
1052
1279
  (0, import_log4.log)("swarm event ignored for disposed swarm", void 0, {
1053
1280
  F: __dxlog_file4,
1054
- L: 172,
1281
+ L: 171,
1055
1282
  S: this,
1056
1283
  C: (f, a) => f(...a)
1057
1284
  });
@@ -1063,7 +1290,7 @@ var Swarm = class {
1063
1290
  peerId
1064
1291
  }, {
1065
1292
  F: __dxlog_file4,
1066
- L: 178,
1293
+ L: 177,
1067
1294
  S: this,
1068
1295
  C: (f, a) => f(...a)
1069
1296
  });
@@ -1075,10 +1302,10 @@ var Swarm = class {
1075
1302
  const peer = this._peers.get(import_keys4.PublicKey.from(swarmEvent.peerLeft.peer));
1076
1303
  if (peer) {
1077
1304
  peer.advertizing = false;
1078
- if (((_a2 = peer.connection) == null ? void 0 : _a2.state) !== ConnectionState.CONNECTED) {
1305
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1079
1306
  void this._destroyPeer(peer.id).catch((err) => import_log4.log.catch(err, void 0, {
1080
1307
  F: __dxlog_file4,
1081
- L: 189,
1308
+ L: 188,
1082
1309
  S: this,
1083
1310
  C: (f, a) => f(...a)
1084
1311
  }));
@@ -1088,19 +1315,18 @@ var Swarm = class {
1088
1315
  this._topology.update();
1089
1316
  }
1090
1317
  async onOffer(message) {
1091
- var _a2, _b;
1092
1318
  (0, import_log4.log)("offer", {
1093
1319
  message
1094
1320
  }, {
1095
1321
  F: __dxlog_file4,
1096
- L: 199,
1322
+ L: 198,
1097
1323
  S: this,
1098
1324
  C: (f, a) => f(...a)
1099
1325
  });
1100
1326
  if (this._ctx.disposed) {
1101
1327
  (0, import_log4.log)("ignored for disposed swarm", void 0, {
1102
1328
  F: __dxlog_file4,
1103
- L: 201,
1329
+ L: 200,
1104
1330
  S: this,
1105
1331
  C: (f, a) => f(...a)
1106
1332
  });
@@ -1108,13 +1334,21 @@ var Swarm = class {
1108
1334
  accept: false
1109
1335
  };
1110
1336
  }
1111
- (0, import_tiny_invariant4.default)(message.author);
1112
- if (!((_a2 = message.recipient) == null ? void 0 : _a2.equals(this._ownPeerId))) {
1337
+ (0, import_invariant4.invariant)(message.author, void 0, {
1338
+ F: __dxlog_file4,
1339
+ L: 205,
1340
+ S: this,
1341
+ A: [
1342
+ "message.author",
1343
+ ""
1344
+ ]
1345
+ });
1346
+ if (!message.recipient?.equals(this._ownPeerId)) {
1113
1347
  (0, import_log4.log)("rejecting offer with incorrect peerId", {
1114
1348
  message
1115
1349
  }, {
1116
1350
  F: __dxlog_file4,
1117
- L: 208,
1351
+ L: 207,
1118
1352
  S: this,
1119
1353
  C: (f, a) => f(...a)
1120
1354
  });
@@ -1122,12 +1356,12 @@ var Swarm = class {
1122
1356
  accept: false
1123
1357
  };
1124
1358
  }
1125
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1359
+ if (!message.topic?.equals(this._topic)) {
1126
1360
  (0, import_log4.log)("rejecting offer with incorrect topic", {
1127
1361
  message
1128
1362
  }, {
1129
1363
  F: __dxlog_file4,
1130
- L: 212,
1364
+ L: 211,
1131
1365
  S: this,
1132
1366
  C: (f, a) => f(...a)
1133
1367
  });
@@ -1141,27 +1375,50 @@ var Swarm = class {
1141
1375
  return answer;
1142
1376
  }
1143
1377
  async onSignal(message) {
1144
- var _a2, _b;
1145
1378
  (0, import_log4.log)("signal", {
1146
1379
  message
1147
1380
  }, {
1148
1381
  F: __dxlog_file4,
1149
- L: 223,
1382
+ L: 222,
1150
1383
  S: this,
1151
1384
  C: (f, a) => f(...a)
1152
1385
  });
1153
1386
  if (this._ctx.disposed) {
1154
1387
  import_log4.log.info("ignored for offline swarm", void 0, {
1155
1388
  F: __dxlog_file4,
1156
- L: 225,
1389
+ L: 224,
1157
1390
  S: this,
1158
1391
  C: (f, a) => f(...a)
1159
1392
  });
1160
1393
  return;
1161
1394
  }
1162
- (0, import_tiny_invariant4.default)((_a2 = message.recipient) == null ? void 0 : _a2.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1163
- (0, import_tiny_invariant4.default)((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1164
- (0, import_tiny_invariant4.default)(message.author);
1395
+ (0, import_invariant4.invariant)(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1396
+ F: __dxlog_file4,
1397
+ L: 227,
1398
+ S: this,
1399
+ A: [
1400
+ "message.recipient?.equals(this._ownPeerId)",
1401
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1402
+ ]
1403
+ });
1404
+ (0, import_invariant4.invariant)(message.topic?.equals(this._topic), void 0, {
1405
+ F: __dxlog_file4,
1406
+ L: 231,
1407
+ S: this,
1408
+ A: [
1409
+ "message.topic?.equals(this._topic)",
1410
+ ""
1411
+ ]
1412
+ });
1413
+ (0, import_invariant4.invariant)(message.author, void 0, {
1414
+ F: __dxlog_file4,
1415
+ L: 232,
1416
+ S: this,
1417
+ A: [
1418
+ "message.author",
1419
+ ""
1420
+ ]
1421
+ });
1165
1422
  const peer = this._getOrCreatePeer(message.author);
1166
1423
  await peer.onSignal(message);
1167
1424
  }
@@ -1213,7 +1470,15 @@ var Swarm = class {
1213
1470
  return peer;
1214
1471
  }
1215
1472
  async _destroyPeer(peerId) {
1216
- (0, import_tiny_invariant4.default)(this._peers.has(peerId));
1473
+ (0, import_invariant4.invariant)(this._peers.has(peerId), void 0, {
1474
+ F: __dxlog_file4,
1475
+ L: 302,
1476
+ S: this,
1477
+ A: [
1478
+ "this._peers.has(peerId)",
1479
+ ""
1480
+ ]
1481
+ });
1217
1482
  await this._peers.get(peerId).destroy();
1218
1483
  this._peers.delete(peerId);
1219
1484
  }
@@ -1234,7 +1499,7 @@ var Swarm = class {
1234
1499
  } catch (err) {
1235
1500
  (0, import_log4.log)("initiation error", err, {
1236
1501
  F: __dxlog_file4,
1237
- L: 329,
1502
+ L: 328,
1238
1503
  S: this,
1239
1504
  C: (f, a) => f(...a)
1240
1505
  });
@@ -1262,7 +1527,7 @@ var Swarm = class {
1262
1527
  remoteId
1263
1528
  }, {
1264
1529
  F: __dxlog_file4,
1265
- L: 356,
1530
+ L: 355,
1266
1531
  S: this,
1267
1532
  C: (f, a) => f(...a)
1268
1533
  });
@@ -1279,7 +1544,7 @@ var Swarm = class {
1279
1544
  remoteId
1280
1545
  }, {
1281
1546
  F: __dxlog_file4,
1282
- L: 370,
1547
+ L: 369,
1283
1548
  S: this,
1284
1549
  C: (f, a) => f(...a)
1285
1550
  });
@@ -1289,7 +1554,7 @@ var Swarm = class {
1289
1554
  remoteId
1290
1555
  }, {
1291
1556
  F: __dxlog_file4,
1292
- L: 373,
1557
+ L: 372,
1293
1558
  S: this,
1294
1559
  C: (f, a) => f(...a)
1295
1560
  });
@@ -1348,8 +1613,7 @@ var SwarmMapper = class {
1348
1613
  }));
1349
1614
  }));
1350
1615
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1351
- var _a2;
1352
- (_a2 = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a2();
1616
+ this._connectionSubscriptions.get(peerId)?.();
1353
1617
  this._connectionSubscriptions.delete(peerId);
1354
1618
  this._update();
1355
1619
  }));
@@ -1399,11 +1663,12 @@ var SwarmMapper = class {
1399
1663
  var import_async5 = require("@dxos/async");
1400
1664
  var import_context5 = require("@dxos/context");
1401
1665
  var import_errors3 = require("@dxos/errors");
1666
+ var import_invariant5 = require("@dxos/invariant");
1402
1667
  var import_keys6 = require("@dxos/keys");
1403
1668
  var import_log6 = require("@dxos/log");
1404
1669
  var import_util4 = require("@dxos/util");
1405
1670
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1406
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
1671
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
1407
1672
  var ConnectionLimiter = class {
1408
1673
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1409
1674
  this._ctx = new import_context5.Context();
@@ -1422,9 +1687,9 @@ var ConnectionLimiter = class {
1422
1687
  * @returns Promise that resolves in queue when connections amount with 'CONNECTING' state is below the limit.
1423
1688
  */
1424
1689
  async connecting(sessionId) {
1425
- (0, import_log6.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1690
+ (0, import_invariant5.invariant)(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1426
1691
  F: __dxlog_file6,
1427
- L: 47,
1692
+ L: 48,
1428
1693
  S: this,
1429
1694
  A: [
1430
1695
  "!this._waitingPromises.has(sessionId)",
@@ -1435,7 +1700,7 @@ var ConnectionLimiter = class {
1435
1700
  sessionId
1436
1701
  }, {
1437
1702
  F: __dxlog_file6,
1438
- L: 48,
1703
+ L: 49,
1439
1704
  S: this,
1440
1705
  C: (f, a) => f(...a)
1441
1706
  });
@@ -1450,7 +1715,7 @@ var ConnectionLimiter = class {
1450
1715
  sessionId
1451
1716
  }, {
1452
1717
  F: __dxlog_file6,
1453
- L: 56,
1718
+ L: 57,
1454
1719
  S: this,
1455
1720
  C: (f, a) => f(...a)
1456
1721
  });
@@ -1463,7 +1728,7 @@ var ConnectionLimiter = class {
1463
1728
  sessionId
1464
1729
  }, {
1465
1730
  F: __dxlog_file6,
1466
- L: 63,
1731
+ L: 64,
1467
1732
  S: this,
1468
1733
  C: (f, a) => f(...a)
1469
1734
  });
@@ -1494,8 +1759,7 @@ var ConnectionLog = class {
1494
1759
  this.update = new import_async6.Event();
1495
1760
  }
1496
1761
  getSwarmInfo(swarmId) {
1497
- var _a2;
1498
- return (_a2 = this._swarms.get(swarmId)) != null ? _a2 : (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1762
+ return this._swarms.get(swarmId) ?? (0, import_debug3.raise)(new Error(`Swarm not found: ${swarmId}`));
1499
1763
  }
1500
1764
  get swarms() {
1501
1765
  return Array.from(this._swarms.values());
@@ -1511,9 +1775,9 @@ var ConnectionLog = class {
1511
1775
  this._swarms.set(import_keys7.PublicKey.from(swarm._instanceId), info);
1512
1776
  this.update.emit();
1513
1777
  swarm.connectionAdded.on((connection) => {
1514
- var _a2, _b;
1515
1778
  const connectionInfo = {
1516
1779
  state: ConnectionState.INITIAL,
1780
+ closeReason: connection.closeReason,
1517
1781
  remotePeerId: connection.remoteId,
1518
1782
  sessionId: connection.sessionId,
1519
1783
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1524,13 +1788,14 @@ var ConnectionLog = class {
1524
1788
  this.update.emit();
1525
1789
  connection.stateChanged.on((state) => {
1526
1790
  connectionInfo.state = state;
1791
+ connectionInfo.closeReason = connection.closeReason;
1527
1792
  connectionInfo.events.push({
1528
1793
  type: EventType.CONNECTION_STATE_CHANGED,
1529
1794
  newState: state
1530
1795
  });
1531
1796
  this.update.emit();
1532
1797
  });
1533
- (_b = (_a2 = connection.protocol) == null ? void 0 : _a2.stats) == null ? void 0 : _b.on((stats) => {
1798
+ connection.protocol?.stats?.on((stats) => {
1534
1799
  connectionInfo.streams = stats.channels;
1535
1800
  this.update.emit();
1536
1801
  });
@@ -1557,10 +1822,7 @@ var NetworkManager = class {
1557
1822
  this._instanceId = import_keys8.PublicKey.random().toHex();
1558
1823
  this._transportFactory = transportFactory;
1559
1824
  this._signalManager = signalManager;
1560
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1561
- var _a2;
1562
- return (_a2 = this._swarms.get(topic)) == null ? void 0 : _a2.onSwarmEvent(event);
1563
- });
1825
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1564
1826
  this._messenger = new import_messaging.Messenger({
1565
1827
  signalManager: this._signalManager
1566
1828
  });
@@ -1595,7 +1857,7 @@ var NetworkManager = class {
1595
1857
  id: this._instanceId
1596
1858
  }), {
1597
1859
  F: __dxlog_file7,
1598
- L: 131,
1860
+ L: 130,
1599
1861
  S: this,
1600
1862
  C: (f, a) => f(...a)
1601
1863
  });
@@ -1605,7 +1867,7 @@ var NetworkManager = class {
1605
1867
  id: this._instanceId
1606
1868
  }), {
1607
1869
  F: __dxlog_file7,
1608
- L: 134,
1870
+ L: 133,
1609
1871
  S: this,
1610
1872
  C: (f, a) => f(...a)
1611
1873
  });
@@ -1615,7 +1877,7 @@ var NetworkManager = class {
1615
1877
  await this.leaveSwarm(topic).catch((err) => {
1616
1878
  (0, import_log7.log)(err, void 0, {
1617
1879
  F: __dxlog_file7,
1618
- L: 140,
1880
+ L: 139,
1619
1881
  S: this,
1620
1882
  C: (f, a) => f(...a)
1621
1883
  });
@@ -1628,11 +1890,42 @@ var NetworkManager = class {
1628
1890
  * Join the swarm.
1629
1891
  */
1630
1892
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1631
- var _a2;
1632
- (0, import_tiny_invariant5.default)(import_keys8.PublicKey.isPublicKey(topic));
1633
- (0, import_tiny_invariant5.default)(import_keys8.PublicKey.isPublicKey(peerId));
1634
- (0, import_tiny_invariant5.default)(topology);
1635
- (0, import_tiny_invariant5.default)(typeof protocol === "function");
1893
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(topic), void 0, {
1894
+ F: __dxlog_file7,
1895
+ L: 157,
1896
+ S: this,
1897
+ A: [
1898
+ "PublicKey.isPublicKey(topic)",
1899
+ ""
1900
+ ]
1901
+ });
1902
+ (0, import_invariant6.invariant)(import_keys8.PublicKey.isPublicKey(peerId), void 0, {
1903
+ F: __dxlog_file7,
1904
+ L: 158,
1905
+ S: this,
1906
+ A: [
1907
+ "PublicKey.isPublicKey(peerId)",
1908
+ ""
1909
+ ]
1910
+ });
1911
+ (0, import_invariant6.invariant)(topology, void 0, {
1912
+ F: __dxlog_file7,
1913
+ L: 159,
1914
+ S: this,
1915
+ A: [
1916
+ "topology",
1917
+ ""
1918
+ ]
1919
+ });
1920
+ (0, import_invariant6.invariant)(typeof protocol === "function", void 0, {
1921
+ F: __dxlog_file7,
1922
+ L: 160,
1923
+ S: this,
1924
+ A: [
1925
+ "typeof protocol === 'function'",
1926
+ ""
1927
+ ]
1928
+ });
1636
1929
  if (this._swarms.has(topic)) {
1637
1930
  throw new Error(`Already connected to swarm: ${import_keys8.PublicKey.from(topic)}`);
1638
1931
  }
@@ -1642,7 +1935,7 @@ var NetworkManager = class {
1642
1935
  topology: topology.toString()
1643
1936
  }, {
1644
1937
  F: __dxlog_file7,
1645
- L: 166,
1938
+ L: 165,
1646
1939
  S: this,
1647
1940
  C: (f, a) => f(...a)
1648
1941
  });
@@ -1652,7 +1945,7 @@ var NetworkManager = class {
1652
1945
  error
1653
1946
  }, {
1654
1947
  F: __dxlog_file7,
1655
- L: 179,
1948
+ L: 178,
1656
1949
  S: this,
1657
1950
  C: (f, a) => f(...a)
1658
1951
  });
@@ -1665,18 +1958,18 @@ var NetworkManager = class {
1665
1958
  peerId
1666
1959
  }).catch((error) => import_log7.log.catch(error, void 0, {
1667
1960
  F: __dxlog_file7,
1668
- L: 188,
1961
+ L: 187,
1669
1962
  S: this,
1670
1963
  C: (f, a) => f(...a)
1671
1964
  }));
1672
1965
  this.topicsUpdated.emit();
1673
- (_a2 = this._connectionLog) == null ? void 0 : _a2.joinedSwarm(swarm);
1966
+ this._connectionLog?.joinedSwarm(swarm);
1674
1967
  (0, import_log7.log)("joined", {
1675
1968
  topic: import_keys8.PublicKey.from(topic),
1676
1969
  count: this._swarms.size
1677
1970
  }, {
1678
1971
  F: __dxlog_file7,
1679
- L: 192,
1972
+ L: 191,
1680
1973
  S: this,
1681
1974
  C: (f, a) => f(...a)
1682
1975
  });
@@ -1688,7 +1981,6 @@ var NetworkManager = class {
1688
1981
  * Close the connection.
1689
1982
  */
1690
1983
  async leaveSwarm(topic) {
1691
- var _a2;
1692
1984
  if (!this._swarms.has(topic)) {
1693
1985
  return;
1694
1986
  }
@@ -1696,7 +1988,7 @@ var NetworkManager = class {
1696
1988
  topic: import_keys8.PublicKey.from(topic)
1697
1989
  }, {
1698
1990
  F: __dxlog_file7,
1699
- L: 208,
1991
+ L: 207,
1700
1992
  S: this,
1701
1993
  C: (f, a) => f(...a)
1702
1994
  });
@@ -1708,7 +2000,7 @@ var NetworkManager = class {
1708
2000
  const map = this._mappers.get(topic);
1709
2001
  map.destroy();
1710
2002
  this._mappers.delete(topic);
1711
- (_a2 = this._connectionLog) == null ? void 0 : _a2.leftSwarm(swarm);
2003
+ this._connectionLog?.leftSwarm(swarm);
1712
2004
  await swarm.destroy();
1713
2005
  this._swarms.delete(topic);
1714
2006
  await this.topicsUpdated.emit();
@@ -1717,7 +2009,7 @@ var NetworkManager = class {
1717
2009
  count: this._swarms.size
1718
2010
  }, {
1719
2011
  F: __dxlog_file7,
1720
- L: 222,
2012
+ L: 221,
1721
2013
  S: this,
1722
2014
  C: (f, a) => f(...a)
1723
2015
  });
@@ -1751,17 +2043,34 @@ var NetworkManager = class {
1751
2043
  };
1752
2044
 
1753
2045
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1754
- var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
2046
+ var import_invariant7 = require("@dxos/invariant");
2047
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1755
2048
  var FullyConnectedTopology = class {
1756
2049
  toString() {
1757
2050
  return "FullyConnectedTopology";
1758
2051
  }
1759
2052
  init(controller) {
1760
- (0, import_tiny_invariant6.default)(!this._controller, "Already initialized");
2053
+ (0, import_invariant7.invariant)(!this._controller, "Already initialized", {
2054
+ F: __dxlog_file8,
2055
+ L: 18,
2056
+ S: this,
2057
+ A: [
2058
+ "!this._controller",
2059
+ "'Already initialized'"
2060
+ ]
2061
+ });
1761
2062
  this._controller = controller;
1762
2063
  }
1763
2064
  update() {
1764
- (0, import_tiny_invariant6.default)(this._controller, "Not initialized");
2065
+ (0, import_invariant7.invariant)(this._controller, "Not initialized", {
2066
+ F: __dxlog_file8,
2067
+ L: 23,
2068
+ S: this,
2069
+ A: [
2070
+ "this._controller",
2071
+ "'Not initialized'"
2072
+ ]
2073
+ });
1765
2074
  const { candidates: discovered } = this._controller.getState();
1766
2075
  for (const peer of discovered) {
1767
2076
  this._controller.connect(peer);
@@ -1775,19 +2084,19 @@ var FullyConnectedTopology = class {
1775
2084
  };
1776
2085
 
1777
2086
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1778
- var import_tiny_invariant7 = __toESM(require("tiny-invariant"));
1779
2087
  var import_xor_distance = __toESM(require("xor-distance"));
2088
+ var import_invariant8 = require("@dxos/invariant");
1780
2089
  var import_log8 = require("@dxos/log");
1781
2090
 
1782
2091
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1783
- var import_tiny_invariant8 = __toESM(require("tiny-invariant"));
2092
+ var import_invariant9 = require("@dxos/invariant");
1784
2093
  var import_log9 = require("@dxos/log");
1785
2094
 
1786
2095
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1787
2096
  var import_node_stream = require("node:stream");
1788
- var import_tiny_invariant9 = __toESM(require("tiny-invariant"));
1789
2097
  var import_async8 = require("@dxos/async");
1790
2098
  var import_debug4 = require("@dxos/debug");
2099
+ var import_invariant10 = require("@dxos/invariant");
1791
2100
  var import_keys9 = require("@dxos/keys");
1792
2101
  var import_log10 = require("@dxos/log");
1793
2102
  var import_util7 = require("@dxos/util");
@@ -1801,7 +2110,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1801
2110
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1802
2111
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1803
2112
  }
1804
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2113
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1805
2114
  var MEMORY_TRANSPORT_DELAY = 1;
1806
2115
  var createStreamDelay = (delay) => {
1807
2116
  return new import_node_stream.Transform({
@@ -1814,9 +2123,12 @@ var createStreamDelay = (delay) => {
1814
2123
  var MemoryTransportFactory = {
1815
2124
  createTransport: (params) => new MemoryTransport(params)
1816
2125
  };
1817
- var _MemoryTransport = class {
2126
+ var MemoryTransport = class _MemoryTransport {
2127
+ static {
2128
+ // TODO(burdon): Remove static properties (inject context into constructor).
2129
+ this._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
2130
+ }
1818
2131
  constructor(options) {
1819
- var _a2;
1820
2132
  this.options = options;
1821
2133
  this.closed = new import_async8.Event();
1822
2134
  this.connected = new import_async8.Event();
@@ -1827,17 +2139,25 @@ var _MemoryTransport = class {
1827
2139
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1828
2140
  this._destroyed = false;
1829
2141
  (0, import_log10.log)("creating", void 0, {
1830
- F: __dxlog_file8,
2142
+ F: __dxlog_file9,
1831
2143
  L: 64,
1832
2144
  S: this,
1833
2145
  C: (f, a) => f(...a)
1834
2146
  });
1835
- (0, import_tiny_invariant9.default)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2147
+ (0, import_invariant10.invariant)(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2148
+ F: __dxlog_file9,
2149
+ L: 66,
2150
+ S: this,
2151
+ A: [
2152
+ "!MemoryTransport._connections.has(this._instanceId)",
2153
+ "'Duplicate memory connection'"
2154
+ ]
2155
+ });
1836
2156
  _MemoryTransport._connections.set(this._instanceId, this);
1837
2157
  if (this.options.initiator) {
1838
2158
  setTimeout(async () => {
1839
2159
  (0, import_log10.log)("sending signal", void 0, {
1840
- F: __dxlog_file8,
2160
+ F: __dxlog_file9,
1841
2161
  L: 73,
1842
2162
  S: this,
1843
2163
  C: (f, a) => f(...a)
@@ -1854,7 +2174,7 @@ var _MemoryTransport = class {
1854
2174
  });
1855
2175
  } else {
1856
2176
  this._remote.wait({
1857
- timeout: (_a2 = this.options.timeout) != null ? _a2 : 1e3
2177
+ timeout: this.options.timeout ?? 1e3
1858
2178
  }).then((remoteId) => {
1859
2179
  if (this._destroyed) {
1860
2180
  return;
@@ -1866,11 +2186,19 @@ var _MemoryTransport = class {
1866
2186
  this.closed.emit();
1867
2187
  return;
1868
2188
  }
1869
- (0, import_tiny_invariant9.default)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2189
+ (0, import_invariant10.invariant)(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2190
+ F: __dxlog_file9,
2191
+ L: 99,
2192
+ S: this,
2193
+ A: [
2194
+ "!this._remoteConnection._remoteConnection",
2195
+ "`Remote already connected: ${this._remoteInstanceId}`"
2196
+ ]
2197
+ });
1870
2198
  this._remoteConnection._remoteConnection = this;
1871
2199
  this._remoteConnection._remoteInstanceId = this._instanceId;
1872
2200
  (0, import_log10.log)("connected", void 0, {
1873
- F: __dxlog_file8,
2201
+ F: __dxlog_file9,
1874
2202
  L: 103,
1875
2203
  S: this,
1876
2204
  C: (f, a) => f(...a)
@@ -1888,7 +2216,7 @@ var _MemoryTransport = class {
1888
2216
  }
1889
2217
  async destroy() {
1890
2218
  (0, import_log10.log)("closing", void 0, {
1891
- F: __dxlog_file8,
2219
+ F: __dxlog_file9,
1892
2220
  L: 124,
1893
2221
  S: this,
1894
2222
  C: (f, a) => f(...a)
@@ -1897,7 +2225,7 @@ var _MemoryTransport = class {
1897
2225
  _MemoryTransport._connections.delete(this._instanceId);
1898
2226
  if (this._remoteConnection) {
1899
2227
  (0, import_log10.log)("closing", void 0, {
1900
- F: __dxlog_file8,
2228
+ F: __dxlog_file9,
1901
2229
  L: 129,
1902
2230
  S: this,
1903
2231
  C: (f, a) => f(...a)
@@ -1910,7 +2238,7 @@ var _MemoryTransport = class {
1910
2238
  this._remoteConnection._remoteConnection = void 0;
1911
2239
  this._remoteConnection = void 0;
1912
2240
  (0, import_log10.log)("closed", void 0, {
1913
- F: __dxlog_file8,
2241
+ F: __dxlog_file9,
1914
2242
  L: 147,
1915
2243
  S: this,
1916
2244
  C: (f, a) => f(...a)
@@ -1918,7 +2246,7 @@ var _MemoryTransport = class {
1918
2246
  }
1919
2247
  this.closed.emit();
1920
2248
  (0, import_log10.log)("closed", void 0, {
1921
- F: __dxlog_file8,
2249
+ F: __dxlog_file9,
1922
2250
  L: 151,
1923
2251
  S: this,
1924
2252
  C: (f, a) => f(...a)
@@ -1928,12 +2256,12 @@ var _MemoryTransport = class {
1928
2256
  (0, import_log10.log)("received signal", {
1929
2257
  payload
1930
2258
  }, {
1931
- F: __dxlog_file8,
2259
+ F: __dxlog_file9,
1932
2260
  L: 155,
1933
2261
  S: this,
1934
2262
  C: (f, a) => f(...a)
1935
2263
  });
1936
- if (!(payload == null ? void 0 : payload.transportId)) {
2264
+ if (!payload?.transportId) {
1937
2265
  return;
1938
2266
  }
1939
2267
  const transportId = payload.transportId;
@@ -1943,9 +2271,6 @@ var _MemoryTransport = class {
1943
2271
  }
1944
2272
  }
1945
2273
  };
1946
- var MemoryTransport = _MemoryTransport;
1947
- // TODO(burdon): Remove static properties (inject context into constructor).
1948
- MemoryTransport._connections = new import_util7.ComplexMap(import_keys9.PublicKey.hash);
1949
2274
  _ts_decorate4([
1950
2275
  import_log10.logInfo
1951
2276
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -1955,9 +2280,9 @@ _ts_decorate4([
1955
2280
 
1956
2281
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
1957
2282
  var import_simple_peer = __toESM(require("simple-peer"));
1958
- var import_tiny_invariant10 = __toESM(require("tiny-invariant"));
1959
2283
  var import_async9 = require("@dxos/async");
1960
2284
  var import_debug5 = require("@dxos/debug");
2285
+ var import_invariant11 = require("@dxos/invariant");
1961
2286
  var import_keys10 = require("@dxos/keys");
1962
2287
  var import_log11 = require("@dxos/log");
1963
2288
  var import_protocols5 = require("@dxos/protocols");
@@ -1966,43 +2291,47 @@ var import_protocols5 = require("@dxos/protocols");
1966
2291
  var wrtc = null;
1967
2292
  try {
1968
2293
  wrtc = require("@koush/wrtc");
1969
- } catch (e) {
2294
+ } catch {
1970
2295
  }
1971
2296
 
1972
2297
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
1973
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2298
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
1974
2299
  var WebRTCTransport = class {
1975
2300
  constructor(params) {
1976
- var _a2;
1977
2301
  this.params = params;
2302
+ this._instanceId = import_keys10.PublicKey.random().toHex();
1978
2303
  this._closed = false;
1979
2304
  this.closed = new import_async9.Event();
1980
2305
  this.connected = new import_async9.Event();
1981
2306
  this.errors = new import_debug5.ErrorStream();
1982
- this._instanceId = import_keys10.PublicKey.random().toHex();
1983
2307
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.begin({
1984
2308
  id: this._instanceId
1985
2309
  }), {
1986
- F: __dxlog_file9,
1987
- L: 39,
2310
+ F: __dxlog_file10,
2311
+ L: 44,
1988
2312
  S: this,
1989
2313
  C: (f, a) => f(...a)
1990
2314
  });
1991
2315
  (0, import_log11.log)("created connection", params, {
1992
- F: __dxlog_file9,
1993
- L: 40,
2316
+ F: __dxlog_file10,
2317
+ L: 45,
1994
2318
  S: this,
1995
2319
  C: (f, a) => f(...a)
1996
2320
  });
1997
2321
  this._peer = new import_simple_peer.default({
1998
2322
  initiator: this.params.initiator,
1999
- wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : (_a2 = wrtc) != null ? _a2 : (0, import_debug5.raise)(new Error("wrtc not available")),
2000
- config: this.params.webrtcConfig
2323
+ config: this.params.webrtcConfig,
2324
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2325
+ channelConfig: {
2326
+ protocol: "dxos.mesh"
2327
+ },
2328
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2329
+ wrtc: import_simple_peer.default.WEBRTC_SUPPORT ? void 0 : wrtc ?? (0, import_debug5.raise)(new Error("wrtc not available"))
2001
2330
  });
2002
2331
  this._peer.on("signal", async (data) => {
2003
2332
  (0, import_log11.log)("signal", data, {
2004
- F: __dxlog_file9,
2005
- L: 48,
2333
+ F: __dxlog_file10,
2334
+ L: 62,
2006
2335
  S: this,
2007
2336
  C: (f, a) => f(...a)
2008
2337
  });
@@ -2014,8 +2343,8 @@ var WebRTCTransport = class {
2014
2343
  });
2015
2344
  this._peer.on("connect", () => {
2016
2345
  (0, import_log11.log)("connected", void 0, {
2017
- F: __dxlog_file9,
2018
- L: 53,
2346
+ F: __dxlog_file10,
2347
+ L: 67,
2019
2348
  S: this,
2020
2349
  C: (f, a) => f(...a)
2021
2350
  });
@@ -2024,8 +2353,8 @@ var WebRTCTransport = class {
2024
2353
  });
2025
2354
  this._peer.on("close", async () => {
2026
2355
  (0, import_log11.log)("closed", void 0, {
2027
- F: __dxlog_file9,
2028
- L: 59,
2356
+ F: __dxlog_file10,
2357
+ L: 73,
2029
2358
  S: this,
2030
2359
  C: (f, a) => f(...a)
2031
2360
  });
@@ -2033,39 +2362,38 @@ var WebRTCTransport = class {
2033
2362
  this.closed.emit();
2034
2363
  });
2035
2364
  this._peer.on("error", async (err) => {
2036
- var _a3;
2037
2365
  this.errors.raise(err);
2038
2366
  try {
2039
- if (typeof ((_a3 = this._peer) == null ? void 0 : _a3._pc.getStats) === "function") {
2367
+ if (typeof this._peer?._pc.getStats === "function") {
2040
2368
  this._peer._pc.getStats().then((stats) => {
2041
2369
  import_log11.log.warn("report after webrtc error", {
2042
2370
  config: this.params.webrtcConfig,
2043
2371
  stats
2044
2372
  }, {
2045
- F: __dxlog_file9,
2046
- L: 71,
2373
+ F: __dxlog_file10,
2374
+ L: 85,
2047
2375
  S: this,
2048
2376
  C: (f, a) => f(...a)
2049
2377
  });
2050
2378
  });
2051
2379
  }
2052
- } catch (e) {
2380
+ } catch {
2053
2381
  }
2054
2382
  await this.destroy();
2055
2383
  });
2056
2384
  import_log11.log.trace("dxos.mesh.webrtc-transport.constructor", import_protocols5.trace.end({
2057
2385
  id: this._instanceId
2058
2386
  }), {
2059
- F: __dxlog_file9,
2060
- L: 81,
2387
+ F: __dxlog_file10,
2388
+ L: 95,
2061
2389
  S: this,
2062
2390
  C: (f, a) => f(...a)
2063
2391
  });
2064
2392
  }
2065
2393
  async destroy() {
2066
2394
  (0, import_log11.log)("closing...", void 0, {
2067
- F: __dxlog_file9,
2068
- L: 85,
2395
+ F: __dxlog_file10,
2396
+ L: 99,
2069
2397
  S: this,
2070
2398
  C: (f, a) => f(...a)
2071
2399
  });
@@ -2074,8 +2402,8 @@ var WebRTCTransport = class {
2074
2402
  this._peer.destroy();
2075
2403
  this.closed.emit();
2076
2404
  (0, import_log11.log)("closed", void 0, {
2077
- F: __dxlog_file9,
2078
- L: 90,
2405
+ F: __dxlog_file10,
2406
+ L: 104,
2079
2407
  S: this,
2080
2408
  C: (f, a) => f(...a)
2081
2409
  });
@@ -2084,24 +2412,31 @@ var WebRTCTransport = class {
2084
2412
  if (this._closed) {
2085
2413
  return;
2086
2414
  }
2087
- (0, import_tiny_invariant10.default)(signal.payload.data, "Signal message must contain signal data.");
2415
+ (0, import_invariant11.invariant)(signal.payload.data, "Signal message must contain signal data.", {
2416
+ F: __dxlog_file10,
2417
+ L: 112,
2418
+ S: this,
2419
+ A: [
2420
+ "signal.payload.data",
2421
+ "'Signal message must contain signal data.'"
2422
+ ]
2423
+ });
2088
2424
  this._peer.signal(signal.payload.data);
2089
2425
  }
2090
2426
  async _disconnectStreams() {
2091
- var _a2, _b, _c, _d;
2092
- (_d = (_c = (_b = (_a2 = this.params.stream).unpipe) == null ? void 0 : _b.call(_a2, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2427
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2093
2428
  }
2094
2429
  };
2095
2430
 
2096
2431
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2097
2432
  var import_node_stream2 = require("node:stream");
2098
- var import_tiny_invariant11 = __toESM(require("tiny-invariant"));
2099
2433
  var import_codec_protobuf = require("@dxos/codec-protobuf");
2434
+ var import_invariant12 = require("@dxos/invariant");
2100
2435
  var import_keys11 = require("@dxos/keys");
2101
2436
  var import_log12 = require("@dxos/log");
2102
2437
  var import_bridge = require("@dxos/protocols/proto/dxos/mesh/bridge");
2103
2438
  var import_util8 = require("@dxos/util");
2104
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2439
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2105
2440
  var WebRTCTransportService = class {
2106
2441
  // prettier-ignore
2107
2442
  constructor(_webrtcConfig) {
@@ -2112,6 +2447,13 @@ var WebRTCTransportService = class {
2112
2447
  const rpcStream = new import_codec_protobuf.Stream(({ ready, next, close }) => {
2113
2448
  const duplex = new import_node_stream2.Duplex({
2114
2449
  read: () => {
2450
+ const callbacks = [
2451
+ ...transportState.writeCallbacks
2452
+ ];
2453
+ transportState.writeCallbacks.length = 0;
2454
+ for (const cb of callbacks) {
2455
+ cb();
2456
+ }
2115
2457
  },
2116
2458
  write: function(chunk, _, callback) {
2117
2459
  next({
@@ -2163,30 +2505,53 @@ var WebRTCTransportService = class {
2163
2505
  });
2164
2506
  close();
2165
2507
  });
2166
- ready();
2167
- this.transports.set(request.proxyId, {
2508
+ const transportState = {
2168
2509
  transport,
2169
- stream: duplex
2170
- });
2510
+ stream: duplex,
2511
+ writeCallbacks: []
2512
+ };
2513
+ ready();
2514
+ this.transports.set(request.proxyId, transportState);
2171
2515
  });
2172
2516
  return rpcStream;
2173
2517
  }
2174
2518
  async sendSignal({ proxyId, signal }) {
2175
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2519
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2520
+ F: __dxlog_file11,
2521
+ L: 113,
2522
+ S: this,
2523
+ A: [
2524
+ "this.transports.has(proxyId)",
2525
+ ""
2526
+ ]
2527
+ });
2176
2528
  await this.transports.get(proxyId).transport.signal(signal);
2177
2529
  }
2178
2530
  async sendData({ proxyId, payload }) {
2179
- (0, import_tiny_invariant11.default)(this.transports.has(proxyId));
2180
- await this.transports.get(proxyId).stream.push(payload);
2531
+ (0, import_invariant12.invariant)(this.transports.has(proxyId), void 0, {
2532
+ F: __dxlog_file11,
2533
+ L: 118,
2534
+ S: this,
2535
+ A: [
2536
+ "this.transports.has(proxyId)",
2537
+ ""
2538
+ ]
2539
+ });
2540
+ const state = this.transports.get(proxyId);
2541
+ const bufferHasSpace = state.stream.push(payload);
2542
+ if (!bufferHasSpace) {
2543
+ await new Promise((resolve) => {
2544
+ state.writeCallbacks.push(resolve);
2545
+ });
2546
+ }
2181
2547
  }
2182
2548
  async close({ proxyId }) {
2183
- var _a2, _b;
2184
- await ((_a2 = this.transports.get(proxyId)) == null ? void 0 : _a2.transport.destroy());
2185
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2549
+ await this.transports.get(proxyId)?.transport.destroy();
2550
+ await this.transports.get(proxyId)?.stream.end();
2186
2551
  this.transports.delete(proxyId);
2187
2552
  (0, import_log12.log)("Closed.", void 0, {
2188
- F: __dxlog_file10,
2189
- L: 109,
2553
+ F: __dxlog_file11,
2554
+ L: 132,
2190
2555
  S: this,
2191
2556
  C: (f, a) => f(...a)
2192
2557
  });
@@ -2194,15 +2559,16 @@ var WebRTCTransportService = class {
2194
2559
  };
2195
2560
 
2196
2561
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2197
- var import_tiny_invariant12 = __toESM(require("tiny-invariant"));
2562
+ var import_node_stream3 = require("node:stream");
2198
2563
  var import_async10 = require("@dxos/async");
2199
2564
  var import_context6 = require("@dxos/context");
2200
2565
  var import_debug6 = require("@dxos/debug");
2566
+ var import_invariant13 = require("@dxos/invariant");
2201
2567
  var import_keys12 = require("@dxos/keys");
2202
2568
  var import_log13 = require("@dxos/log");
2203
2569
  var import_bridge2 = require("@dxos/protocols/proto/dxos/mesh/bridge");
2204
2570
  var import_util9 = require("@dxos/util");
2205
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2571
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2206
2572
  var WebRTCTransportProxy = class {
2207
2573
  // prettier-ignore
2208
2574
  constructor(_params) {
@@ -2210,9 +2576,9 @@ var WebRTCTransportProxy = class {
2210
2576
  this._proxyId = import_keys12.PublicKey.random();
2211
2577
  this._ctx = new import_context6.Context();
2212
2578
  this.closed = new import_async10.Event();
2213
- this._closed = false;
2214
2579
  this.connected = new import_async10.Event();
2215
2580
  this.errors = new import_debug6.ErrorStream();
2581
+ this._closed = false;
2216
2582
  this._serviceStream = this._params.bridgeService.open({
2217
2583
  proxyId: this._proxyId,
2218
2584
  initiator: this._params.initiator
@@ -2220,8 +2586,8 @@ var WebRTCTransportProxy = class {
2220
2586
  this._serviceStream.waitUntilReady().then(() => {
2221
2587
  this._serviceStream.subscribe(async (event) => {
2222
2588
  (0, import_log13.log)("WebRTCTransportProxy: event", event, {
2223
- F: __dxlog_file11,
2224
- L: 50,
2589
+ F: __dxlog_file12,
2590
+ L: 51,
2225
2591
  S: this,
2226
2592
  C: (f, a) => f(...a)
2227
2593
  });
@@ -2233,28 +2599,24 @@ var WebRTCTransportProxy = class {
2233
2599
  await this._handleSignal(event.signal);
2234
2600
  }
2235
2601
  });
2236
- const dataListener = async (data) => {
2237
- try {
2238
- await this._params.bridgeService.sendData({
2602
+ this._params.stream.pipe(new import_node_stream3.Writable({
2603
+ write: (chunk, _, callback) => {
2604
+ this._params.bridgeService.sendData({
2239
2605
  proxyId: this._proxyId,
2240
- payload: data
2241
- });
2242
- } catch (err) {
2243
- import_log13.log.catch(err, void 0, {
2244
- F: __dxlog_file11,
2245
- L: 67,
2246
- S: this,
2247
- C: (f, a) => f(...a)
2606
+ payload: chunk
2607
+ }).then(() => callback(), (err) => {
2608
+ import_log13.log.catch(err, void 0, {
2609
+ F: __dxlog_file12,
2610
+ L: 69,
2611
+ S: this,
2612
+ C: (f, a) => f(...a)
2613
+ });
2248
2614
  });
2249
2615
  }
2250
- };
2251
- this._params.stream.on("data", dataListener);
2252
- this._ctx.onDispose(() => {
2253
- this._params.stream.off("data", dataListener);
2254
- });
2616
+ }));
2255
2617
  }, (error) => import_log13.log.catch(error, void 0, {
2256
- F: __dxlog_file11,
2257
- L: 73,
2618
+ F: __dxlog_file12,
2619
+ L: 75,
2258
2620
  S: this,
2259
2621
  C: (f, a) => f(...a)
2260
2622
  }));
@@ -2292,15 +2654,15 @@ var WebRTCTransportProxy = class {
2292
2654
  if (this._closed) {
2293
2655
  return;
2294
2656
  }
2295
- this._serviceStream.close();
2657
+ await this._serviceStream.close();
2296
2658
  try {
2297
2659
  await this._params.bridgeService.close({
2298
2660
  proxyId: this._proxyId
2299
2661
  });
2300
2662
  } catch (err) {
2301
2663
  import_log13.log.catch(err, void 0, {
2302
- F: __dxlog_file11,
2303
- L: 124,
2664
+ F: __dxlog_file12,
2665
+ L: 126,
2304
2666
  S: this,
2305
2667
  C: (f, a) => f(...a)
2306
2668
  });
@@ -2313,7 +2675,7 @@ var WebRTCTransportProxy = class {
2313
2675
  */
2314
2676
  // TODO(burdon): Option on close method.
2315
2677
  forceClose() {
2316
- this._serviceStream.close();
2678
+ void this._serviceStream.close();
2317
2679
  this.closed.emit();
2318
2680
  this._closed = true;
2319
2681
  }
@@ -2334,7 +2696,15 @@ var WebRTCTransportProxyFactory = class {
2334
2696
  return this;
2335
2697
  }
2336
2698
  createTransport(options) {
2337
- (0, import_tiny_invariant12.default)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2699
+ (0, import_invariant13.invariant)(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2700
+ F: __dxlog_file12,
2701
+ L: 163,
2702
+ S: this,
2703
+ A: [
2704
+ "this._bridgeService",
2705
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2706
+ ]
2707
+ });
2338
2708
  const transport = new WebRTCTransportProxy({
2339
2709
  ...options,
2340
2710
  bridgeService: this._bridgeService
@@ -2371,20 +2741,22 @@ var createTeleportProtocolFactory = (onConnection) => {
2371
2741
  };
2372
2742
 
2373
2743
  // packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts
2374
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
2744
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/testing/test-wire-protocol.ts";
2375
2745
  var TestWireProtocol = class {
2376
- constructor(peerId) {
2746
+ constructor(peerId, _extensionFactory = () => []) {
2377
2747
  this.peerId = peerId;
2748
+ this._extensionFactory = _extensionFactory;
2378
2749
  this.connections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2379
2750
  this.streamConnections = new import_util10.ComplexMap(import_keys13.PublicKey.hash);
2380
2751
  this.connected = new import_async11.Event();
2381
2752
  this.disconnected = new import_async11.Event();
2753
+ this.otherConnections = new import_util10.ComplexMap(({ remotePeerId, extension }) => remotePeerId.toHex() + extension);
2382
2754
  this.factory = createTeleportProtocolFactory(async (teleport) => {
2383
2755
  (0, import_log14.log)("create", {
2384
2756
  remotePeerId: teleport.remotePeerId
2385
2757
  }, {
2386
- F: __dxlog_file12,
2387
- L: 23,
2758
+ F: __dxlog_file13,
2759
+ L: 38,
2388
2760
  S: this,
2389
2761
  C: (f, a) => f(...a)
2390
2762
  });
@@ -2404,6 +2776,13 @@ var TestWireProtocol = class {
2404
2776
  });
2405
2777
  this.streamConnections.set(teleport.remotePeerId, streamExtension);
2406
2778
  teleport.addExtension("test-stream", streamExtension);
2779
+ for (const { name, extension: extension2 } of this._extensionFactory()) {
2780
+ this.otherConnections.set({
2781
+ remotePeerId: teleport.remotePeerId,
2782
+ extension: name
2783
+ }, extension2);
2784
+ teleport.addExtension(name, extension2);
2785
+ }
2407
2786
  });
2408
2787
  }
2409
2788
  async waitForConnection(peerId) {
@@ -2413,8 +2792,8 @@ var TestWireProtocol = class {
2413
2792
  (0, import_log14.log)("waitForConnection", {
2414
2793
  peerId
2415
2794
  }, {
2416
- F: __dxlog_file12,
2417
- L: 47,
2795
+ F: __dxlog_file13,
2796
+ L: 67,
2418
2797
  S: this,
2419
2798
  C: (f, a) => f(...a)
2420
2799
  });
@@ -2446,8 +2825,7 @@ var TestWireProtocol = class {
2446
2825
  };
2447
2826
 
2448
2827
  // packages/core/mesh/network-manager/src/testing/test-builder.ts
2449
- var _a;
2450
- var port = (_a = process.env.SIGNAL_PORT) != null ? _a : 4e3;
2828
+ var port = process.env.SIGNAL_PORT ?? 4e3;
2451
2829
  var TEST_SIGNAL_HOSTS = [
2452
2830
  {
2453
2831
  server: `ws://localhost:${port}/.well-known/dx/signal`
@@ -2518,17 +2896,15 @@ var TestPeer = class {
2518
2896
  });
2519
2897
  }
2520
2898
  async open() {
2521
- var _a2, _b;
2522
2899
  await this._networkManager.open();
2523
- await ((_a2 = this._proxy) == null ? void 0 : _a2.open());
2524
- await ((_b = this._service) == null ? void 0 : _b.open());
2900
+ await this._proxy?.open();
2901
+ await this._service?.open();
2525
2902
  }
2526
2903
  async close() {
2527
- var _a2, _b;
2528
2904
  await Promise.all(Array.from(this._swarms.values()).map((swarm) => swarm.leave()));
2529
2905
  this._swarms.clear();
2530
- await ((_a2 = this._proxy) == null ? void 0 : _a2.close());
2531
- await ((_b = this._service) == null ? void 0 : _b.close());
2906
+ await this._proxy?.close();
2907
+ await this._service?.close();
2532
2908
  await this._networkManager.close();
2533
2909
  }
2534
2910
  getSwarm(topic) {
@@ -2538,8 +2914,8 @@ var TestPeer = class {
2538
2914
  }
2539
2915
  return swarm;
2540
2916
  }
2541
- createSwarm(topic) {
2542
- const swarm = new TestSwarmConnection(this, topic);
2917
+ createSwarm(topic, extensionFactory = () => []) {
2918
+ const swarm = new TestSwarmConnection(this, topic, extensionFactory);
2543
2919
  this._swarms.set(topic, swarm);
2544
2920
  return swarm;
2545
2921
  }
@@ -2551,10 +2927,11 @@ var TestPeer = class {
2551
2927
  }
2552
2928
  };
2553
2929
  var TestSwarmConnection = class {
2554
- constructor(peer, topic) {
2930
+ constructor(peer, topic, extensionFactory) {
2555
2931
  this.peer = peer;
2556
2932
  this.topic = topic;
2557
- this.protocol = new TestWireProtocol(this.peer.peerId);
2933
+ this.extensionFactory = extensionFactory;
2934
+ this.protocol = new TestWireProtocol(this.peer.peerId, this.extensionFactory);
2558
2935
  }
2559
2936
  // TODO(burdon): Need to create new plugin instance per swarm?
2560
2937
  // If so, then perhaps joinSwarm should return swarm object with access to plugins.