@dxos/network-manager 0.1.56-main.167f01e → 0.1.56-main.1a3e779

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 (52) hide show
  1. package/dist/lib/browser/{chunk-5R7TDXUU.mjs → chunk-YADPADXG.mjs} +697 -273
  2. package/dist/lib/browser/chunk-YADPADXG.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 +695 -271
  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 +643 -268
  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.map +1 -1
  17. package/dist/types/src/swarm/connection.d.ts +2 -1
  18. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  19. package/dist/types/src/swarm/peer.d.ts +1 -1
  20. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  21. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts +4 -3
  23. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  24. package/dist/types/src/testing/test-wire-protocol.d.ts +13 -2
  25. package/dist/types/src/testing/test-wire-protocol.d.ts.map +1 -1
  26. package/dist/types/src/topology/fully-connected-topology.d.ts.map +1 -1
  27. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  28. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  29. package/dist/types/src/transport/webrtc-transport-proxy.d.ts +1 -1
  30. package/dist/types/src/transport/webrtc-transport-proxy.d.ts.map +1 -1
  31. package/dist/types/src/transport/webrtc-transport-service.d.ts.map +1 -1
  32. package/dist/types/src/transport/webrtc-transport.d.ts +3 -3
  33. package/dist/types/src/transport/webrtc-transport.d.ts.map +1 -1
  34. package/package.json +18 -18
  35. package/src/connection-log.ts +2 -0
  36. package/src/network-manager.ts +1 -2
  37. package/src/signal/swarm-messenger.ts +3 -4
  38. package/src/swarm/connection-limiter.ts +2 -1
  39. package/src/swarm/connection.ts +25 -4
  40. package/src/swarm/peer.ts +32 -8
  41. package/src/swarm/swarm.ts +6 -7
  42. package/src/testing/test-builder.ts +9 -5
  43. package/src/testing/test-wire-protocol.ts +22 -2
  44. package/src/tests/memory-transport.test.ts +1 -1
  45. package/src/topology/fully-connected-topology.ts +1 -2
  46. package/src/topology/mmst-topology.ts +1 -1
  47. package/src/topology/star-topology.ts +1 -2
  48. package/src/transport/memory-transport.ts +1 -1
  49. package/src/transport/webrtc-transport-proxy.ts +18 -16
  50. package/src/transport/webrtc-transport-service.ts +31 -8
  51. package/src/transport/webrtc-transport.ts +21 -7
  52. package/dist/lib/browser/chunk-5R7TDXUU.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: 96,
96
+ S: this,
97
+ C: (f, a) => f(...a)
98
+ });
87
99
  }
88
100
  get state() {
89
101
  return this._state;
@@ -98,12 +110,32 @@ var Connection = class {
98
110
  * Create an underlying transport and prepares it for the connection.
99
111
  */
100
112
  async openConnection() {
101
- invariant(this._state === ConnectionState.INITIAL, "Invalid state.");
113
+ invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
114
+ F: __dxlog_file,
115
+ L: 121,
116
+ S: this,
117
+ A: [
118
+ "this._state === ConnectionState.INITIAL",
119
+ "'Invalid state.'"
120
+ ]
121
+ });
102
122
  log.trace("dxos.mesh.connection.open-connection", trace.begin({
103
123
  id: this._instanceId
104
124
  }), {
105
125
  F: __dxlog_file,
106
- L: 114,
126
+ L: 122,
127
+ S: this,
128
+ C: (f, a) => f(...a)
129
+ });
130
+ log.trace("dxos.mesh.connection.open", {
131
+ sessionId: this.sessionId,
132
+ topic: this.topic,
133
+ localPeerId: this.ownId,
134
+ remotePeerId: this.remoteId,
135
+ initiator: this.initiator
136
+ }, {
137
+ F: __dxlog_file,
138
+ L: 123,
107
139
  S: this,
108
140
  C: (f, a) => f(...a)
109
141
  });
@@ -114,13 +146,21 @@ var Connection = class {
114
146
  this._protocol.stream.on("close", () => {
115
147
  log("protocol stream closed", void 0, {
116
148
  F: __dxlog_file,
117
- L: 125,
149
+ L: 140,
118
150
  S: this,
119
151
  C: (f, a) => f(...a)
120
152
  });
121
153
  this.close().catch((err) => this.errors.raise(err));
122
154
  });
123
- invariant(!this._transport);
155
+ invariant(!this._transport, void 0, {
156
+ F: __dxlog_file,
157
+ L: 144,
158
+ S: this,
159
+ A: [
160
+ "!this._transport",
161
+ ""
162
+ ]
163
+ });
124
164
  this._transport = this._transportFactory.createTransport({
125
165
  initiator: this.initiator,
126
166
  stream: this._protocol.stream,
@@ -134,6 +174,9 @@ var Connection = class {
134
174
  this.close().catch((err) => this.errors.raise(err));
135
175
  });
136
176
  this._transport.errors.handle((err) => {
177
+ if (!this.closeReason) {
178
+ this.closeReason = err?.message;
179
+ }
137
180
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
138
181
  this.errors.raise(err);
139
182
  }
@@ -146,13 +189,15 @@ var Connection = class {
146
189
  id: this._instanceId
147
190
  }), {
148
191
  F: __dxlog_file,
149
- L: 158,
192
+ L: 176,
150
193
  S: this,
151
194
  C: (f, a) => f(...a)
152
195
  });
153
196
  }
154
- async close() {
155
- var _a;
197
+ async close(err) {
198
+ if (!this.closeReason) {
199
+ this.closeReason = err?.message;
200
+ }
156
201
  if (this._state === ConnectionState.CLOSED) {
157
202
  return;
158
203
  }
@@ -162,26 +207,26 @@ var Connection = class {
162
207
  peerId: this.ownId
163
208
  }, {
164
209
  F: __dxlog_file,
165
- L: 170,
210
+ L: 191,
166
211
  S: this,
167
212
  C: (f, a) => f(...a)
168
213
  });
169
214
  try {
170
215
  await this._protocol.destroy();
171
- } catch (err) {
172
- log.catch(err, void 0, {
216
+ } catch (err2) {
217
+ log.catch(err2, void 0, {
173
218
  F: __dxlog_file,
174
- L: 176,
219
+ L: 197,
175
220
  S: this,
176
221
  C: (f, a) => f(...a)
177
222
  });
178
223
  }
179
224
  try {
180
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
181
- } catch (err) {
182
- log.catch(err, void 0, {
225
+ await this._transport?.destroy();
226
+ } catch (err2) {
227
+ log.catch(err2, void 0, {
183
228
  F: __dxlog_file,
184
- L: 183,
229
+ L: 204,
185
230
  S: this,
186
231
  C: (f, a) => f(...a)
187
232
  });
@@ -190,7 +235,7 @@ var Connection = class {
190
235
  peerId: this.ownId
191
236
  }, {
192
237
  F: __dxlog_file,
193
- L: 186,
238
+ L: 207,
194
239
  S: this,
195
240
  C: (f, a) => f(...a)
196
241
  });
@@ -230,7 +275,7 @@ var Connection = class {
230
275
  err
231
276
  }, {
232
277
  F: __dxlog_file,
233
- L: 220,
278
+ L: 241,
234
279
  S: this,
235
280
  C: (f, a) => f(...a)
236
281
  });
@@ -241,21 +286,52 @@ var Connection = class {
241
286
  * Receive a signal from the remote peer.
242
287
  */
243
288
  async signal(msg) {
244
- var _a, _b, _c;
245
- invariant(msg.sessionId);
289
+ invariant(msg.sessionId, void 0, {
290
+ F: __dxlog_file,
291
+ L: 250,
292
+ S: this,
293
+ A: [
294
+ "msg.sessionId",
295
+ ""
296
+ ]
297
+ });
246
298
  if (!msg.sessionId.equals(this.sessionId)) {
247
299
  log("dropping signal for incorrect session id", void 0, {
248
300
  F: __dxlog_file,
249
- L: 231,
301
+ L: 252,
250
302
  S: this,
251
303
  C: (f, a) => f(...a)
252
304
  });
253
305
  return;
254
306
  }
255
- invariant(msg.data.signal || msg.data.signalBatch);
256
- invariant((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId));
257
- invariant((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId));
258
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
307
+ invariant(msg.data.signal || msg.data.signalBatch, void 0, {
308
+ F: __dxlog_file,
309
+ L: 255,
310
+ S: this,
311
+ A: [
312
+ "msg.data.signal || msg.data.signalBatch",
313
+ ""
314
+ ]
315
+ });
316
+ invariant(msg.author?.equals(this.remoteId), void 0, {
317
+ F: __dxlog_file,
318
+ L: 256,
319
+ S: this,
320
+ A: [
321
+ "msg.author?.equals(this.remoteId)",
322
+ ""
323
+ ]
324
+ });
325
+ invariant(msg.recipient?.equals(this.ownId), void 0, {
326
+ F: __dxlog_file,
327
+ L: 257,
328
+ S: this,
329
+ A: [
330
+ "msg.recipient?.equals(this.ownId)",
331
+ ""
332
+ ]
333
+ });
334
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
259
335
  msg.data.signal
260
336
  ];
261
337
  for (const signal of signals) {
@@ -269,20 +345,28 @@ var Connection = class {
269
345
  msg: msg.data
270
346
  }, {
271
347
  F: __dxlog_file,
272
- L: 245,
348
+ L: 266,
273
349
  S: this,
274
350
  C: (f, a) => f(...a)
275
351
  });
276
352
  this._incomingSignalBuffer.push(signal);
277
353
  } else {
278
- invariant(this._transport, "Connection not ready to accept signals.");
354
+ invariant(this._transport, "Connection not ready to accept signals.", {
355
+ F: __dxlog_file,
356
+ L: 269,
357
+ S: this,
358
+ A: [
359
+ "this._transport",
360
+ "'Connection not ready to accept signals.'"
361
+ ]
362
+ });
279
363
  log("received signal", {
280
364
  peerId: this.ownId,
281
365
  remoteId: this.remoteId,
282
366
  msg: msg.data
283
367
  }, {
284
368
  F: __dxlog_file,
285
- L: 249,
369
+ L: 270,
286
370
  S: this,
287
371
  C: (f, a) => f(...a)
288
372
  });
@@ -297,11 +381,19 @@ var Connection = class {
297
381
  peerId: this.ownId
298
382
  }, {
299
383
  F: __dxlog_file,
300
- L: 256,
384
+ L: 277,
301
385
  S: this,
302
386
  C: (f, a) => f(...a)
303
387
  });
304
- invariant(state !== this._state, "Already in this state.");
388
+ invariant(state !== this._state, "Already in this state.", {
389
+ F: __dxlog_file,
390
+ L: 278,
391
+ S: this,
392
+ A: [
393
+ "state !== this._state",
394
+ "'Already in this state.'"
395
+ ]
396
+ });
305
397
  this._state = state;
306
398
  this.stateChanged.emit(state);
307
399
  }
@@ -311,8 +403,8 @@ _ts_decorate([
311
403
  ], Connection.prototype, "close", null);
312
404
 
313
405
  // packages/core/mesh/network-manager/src/signal/swarm-messenger.ts
314
- import invariant2 from "tiny-invariant";
315
406
  import { Context as Context2 } from "@dxos/context";
407
+ import { invariant as invariant2 } from "@dxos/invariant";
316
408
  import { PublicKey as PublicKey2 } from "@dxos/keys";
317
409
  import { log as log2 } from "@dxos/log";
318
410
  import { schema } from "@dxos/protocols";
@@ -329,7 +421,6 @@ var SwarmMessenger = class {
329
421
  this._topic = topic;
330
422
  }
331
423
  async receiveMessage({ author, recipient, payload }) {
332
- var _a, _b, _c, _d;
333
424
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
334
425
  return;
335
426
  }
@@ -343,25 +434,25 @@ var SwarmMessenger = class {
343
434
  msg: message
344
435
  }, {
345
436
  F: __dxlog_file2,
346
- L: 70,
437
+ L: 69,
347
438
  S: this,
348
439
  C: (f, a) => f(...a)
349
440
  });
350
- if ((_a = message.data) == null ? void 0 : _a.offer) {
441
+ if (message.data?.offer) {
351
442
  await this._handleOffer({
352
443
  author,
353
444
  recipient,
354
445
  message
355
446
  });
356
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
447
+ } else if (message.data?.answer) {
357
448
  await this._resolveAnswers(message);
358
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
449
+ } else if (message.data?.signal) {
359
450
  await this._handleSignal({
360
451
  author,
361
452
  recipient,
362
453
  message
363
454
  });
364
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
455
+ } else if (message.data?.signalBatch) {
365
456
  await this._handleSignal({
366
457
  author,
367
458
  recipient,
@@ -372,15 +463,22 @@ var SwarmMessenger = class {
372
463
  message
373
464
  }, {
374
465
  F: __dxlog_file2,
375
- L: 81,
466
+ L: 80,
376
467
  S: this,
377
468
  C: (f, a) => f(...a)
378
469
  });
379
470
  }
380
471
  }
381
472
  async signal(message) {
382
- var _a, _b;
383
- invariant2(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message");
473
+ invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
474
+ F: __dxlog_file2,
475
+ L: 85,
476
+ S: this,
477
+ A: [
478
+ "message.data?.signal || message.data?.signalBatch",
479
+ "'Invalid message'"
480
+ ]
481
+ });
384
482
  await this._sendReliableMessage({
385
483
  author: message.author,
386
484
  recipient: message.recipient,
@@ -404,11 +502,10 @@ var SwarmMessenger = class {
404
502
  });
405
503
  }
406
504
  async _sendReliableMessage({ author, recipient, message }) {
407
- var _a;
408
505
  const networkMessage = {
409
506
  ...message,
410
507
  // Setting unique message_id if it not specified yet.
411
- messageId: (_a = message.messageId) != null ? _a : PublicKey2.random()
508
+ messageId: message.messageId ?? PublicKey2.random()
412
509
  };
413
510
  log2("sending", {
414
511
  from: author,
@@ -416,7 +513,7 @@ var SwarmMessenger = class {
416
513
  msg: networkMessage
417
514
  }, {
418
515
  F: __dxlog_file2,
419
- L: 124,
516
+ L: 123,
420
517
  S: this,
421
518
  C: (f, a) => f(...a)
422
519
  });
@@ -430,17 +527,32 @@ var SwarmMessenger = class {
430
527
  });
431
528
  }
432
529
  async _resolveAnswers(message) {
433
- var _a, _b, _c;
434
- invariant2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId");
530
+ invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
531
+ F: __dxlog_file2,
532
+ L: 135,
533
+ S: this,
534
+ A: [
535
+ "message.data?.answer?.offerMessageId",
536
+ "'No offerMessageId'"
537
+ ]
538
+ });
435
539
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
436
540
  if (offerRecord) {
437
541
  this._offerRecords.delete(message.data.answer.offerMessageId);
438
- invariant2((_c = message.data) == null ? void 0 : _c.answer, "No answer");
542
+ invariant2(message.data?.answer, "No answer", {
543
+ F: __dxlog_file2,
544
+ L: 139,
545
+ S: this,
546
+ A: [
547
+ "message.data?.answer",
548
+ "'No answer'"
549
+ ]
550
+ });
439
551
  log2("resolving", {
440
552
  answer: message.data.answer
441
553
  }, {
442
554
  F: __dxlog_file2,
443
- L: 141,
555
+ L: 140,
444
556
  S: this,
445
557
  C: (f, a) => f(...a)
446
558
  });
@@ -448,7 +560,15 @@ var SwarmMessenger = class {
448
560
  }
449
561
  }
450
562
  async _handleOffer({ author, recipient, message }) {
451
- invariant2(message.data.offer, "No offer");
563
+ invariant2(message.data.offer, "No offer", {
564
+ F: __dxlog_file2,
565
+ L: 154,
566
+ S: this,
567
+ A: [
568
+ "message.data.offer",
569
+ "'No offer'"
570
+ ]
571
+ });
452
572
  const offerMessage = {
453
573
  author,
454
574
  recipient,
@@ -476,15 +596,31 @@ var SwarmMessenger = class {
476
596
  err
477
597
  }, {
478
598
  F: __dxlog_file2,
479
- L: 175,
599
+ L: 174,
480
600
  S: this,
481
601
  C: (f, a) => f(...a)
482
602
  });
483
603
  }
484
604
  }
485
605
  async _handleSignal({ author, recipient, message }) {
486
- invariant2(message.messageId);
487
- invariant2(message.data.signal || message.data.signalBatch, "Invalid message");
606
+ invariant2(message.messageId, void 0, {
607
+ F: __dxlog_file2,
608
+ L: 187,
609
+ S: this,
610
+ A: [
611
+ "message.messageId",
612
+ ""
613
+ ]
614
+ });
615
+ invariant2(message.data.signal || message.data.signalBatch, "Invalid message", {
616
+ F: __dxlog_file2,
617
+ L: 188,
618
+ S: this,
619
+ A: [
620
+ "message.data.signal || message.data.signalBatch",
621
+ "'Invalid message'"
622
+ ]
623
+ });
488
624
  const signalMessage = {
489
625
  author,
490
626
  recipient,
@@ -499,20 +635,20 @@ var SwarmMessenger = class {
499
635
  };
500
636
 
501
637
  // packages/core/mesh/network-manager/src/swarm/swarm.ts
502
- import invariant4 from "tiny-invariant";
503
638
  import { Event as Event2, scheduleTask as scheduleTask2, sleep as sleep2, synchronized as synchronized3 } from "@dxos/async";
504
639
  import { Context as Context4 } from "@dxos/context";
505
640
  import { ErrorStream as ErrorStream2 } from "@dxos/debug";
641
+ import { invariant as invariant4 } from "@dxos/invariant";
506
642
  import { PublicKey as PublicKey4 } from "@dxos/keys";
507
643
  import { log as log4, logInfo } from "@dxos/log";
508
644
  import { trace as trace2 } from "@dxos/protocols";
509
645
  import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
510
646
 
511
647
  // packages/core/mesh/network-manager/src/swarm/peer.ts
512
- import invariant3 from "tiny-invariant";
513
648
  import { scheduleTask, synchronized as synchronized2 } from "@dxos/async";
514
649
  import { Context as Context3 } from "@dxos/context";
515
650
  import { CancelledError as CancelledError2 } from "@dxos/errors";
651
+ import { invariant as invariant3 } from "@dxos/invariant";
516
652
  import { PublicKey as PublicKey3 } from "@dxos/keys";
517
653
  import { log as log3 } from "@dxos/log";
518
654
  function _ts_decorate2(decorators, target, key, desc) {
@@ -528,6 +664,7 @@ function _ts_decorate2(decorators, target, key, desc) {
528
664
  var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/peer.ts";
529
665
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
530
666
  var Peer = class {
667
+ // TODO(burdon): Convert to map?
531
668
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
532
669
  this.id = id;
533
670
  this.topic = topic;
@@ -547,7 +684,6 @@ var Peer = class {
547
684
  * Respond to remote offer.
548
685
  */
549
686
  async onOffer(message) {
550
- var _a;
551
687
  const remoteId = message.author;
552
688
  if (this.connection || this.initiating) {
553
689
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -555,15 +691,15 @@ var Peer = class {
555
691
  localPeerId: this.id,
556
692
  topic: this.topic,
557
693
  remotePeerId: this.localPeerId,
558
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
694
+ sessionId: this.connection?.sessionId
559
695
  }, {
560
696
  F: __dxlog_file3,
561
- L: 107,
697
+ L: 108,
562
698
  S: this,
563
699
  C: (f, a) => f(...a)
564
700
  });
565
701
  if (this.connection) {
566
- await this.closeConnection();
702
+ await this.closeConnection(new Error("Connection displaced by remote initiator."));
567
703
  }
568
704
  } else {
569
705
  return {
@@ -573,7 +709,15 @@ var Peer = class {
573
709
  }
574
710
  if (await this._callbacks.onOffer(remoteId)) {
575
711
  if (!this.connection) {
576
- invariant3(message.sessionId);
712
+ invariant3(message.sessionId, void 0, {
713
+ F: __dxlog_file3,
714
+ L: 128,
715
+ S: this,
716
+ A: [
717
+ "message.sessionId",
718
+ ""
719
+ ]
720
+ });
577
721
  const connection = this._createConnection(false, message.sessionId);
578
722
  try {
579
723
  await this._connectionLimiter.connecting(message.sessionId);
@@ -587,12 +731,12 @@ var Peer = class {
587
731
  err
588
732
  }, {
589
733
  F: __dxlog_file3,
590
- L: 135,
734
+ L: 136,
591
735
  S: this,
592
736
  C: (f, a) => f(...a)
593
737
  });
594
738
  }
595
- await this.closeConnection();
739
+ await this.closeConnection(err);
596
740
  }
597
741
  return {
598
742
  accept: true
@@ -607,8 +751,24 @@ var Peer = class {
607
751
  * Initiate a connection to the remote peer.
608
752
  */
609
753
  async initiateConnection() {
610
- invariant3(!this.initiating, "Initiation in progress.");
611
- invariant3(!this.connection, "Already connected.");
754
+ invariant3(!this.initiating, "Initiation in progress.", {
755
+ F: __dxlog_file3,
756
+ L: 153,
757
+ S: this,
758
+ A: [
759
+ "!this.initiating",
760
+ "'Initiation in progress.'"
761
+ ]
762
+ });
763
+ invariant3(!this.connection, "Already connected.", {
764
+ F: __dxlog_file3,
765
+ L: 154,
766
+ S: this,
767
+ A: [
768
+ "!this.connection",
769
+ "'Already connected.'"
770
+ ]
771
+ });
612
772
  const sessionId = PublicKey3.random();
613
773
  log3("initiating...", {
614
774
  id: this.id,
@@ -617,7 +777,7 @@ var Peer = class {
617
777
  sessionId
618
778
  }, {
619
779
  F: __dxlog_file3,
620
- L: 155,
780
+ L: 156,
621
781
  S: this,
622
782
  C: (f, a) => f(...a)
623
783
  });
@@ -641,14 +801,14 @@ var Peer = class {
641
801
  remoteId: this.id
642
802
  }, {
643
803
  F: __dxlog_file3,
644
- L: 170,
804
+ L: 171,
645
805
  S: this,
646
806
  C: (f, a) => f(...a)
647
807
  });
648
808
  if (connection.state !== ConnectionState.INITIAL) {
649
809
  log3("ignoring response", void 0, {
650
810
  F: __dxlog_file3,
651
- L: 172,
811
+ L: 173,
652
812
  S: this,
653
813
  C: (f, a) => f(...a)
654
814
  });
@@ -668,11 +828,11 @@ var Peer = class {
668
828
  remoteId: this.id
669
829
  }, {
670
830
  F: __dxlog_file3,
671
- L: 183,
831
+ L: 184,
672
832
  S: this,
673
833
  C: (f, a) => f(...a)
674
834
  });
675
- await this.closeConnection();
835
+ await this.closeConnection(err);
676
836
  throw err;
677
837
  } finally {
678
838
  this.initiating = false;
@@ -683,7 +843,6 @@ var Peer = class {
683
843
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
684
844
  */
685
845
  _createConnection(initiator, sessionId) {
686
- var _a;
687
846
  log3("creating connection", {
688
847
  topic: this.topic,
689
848
  peerId: this.localPeerId,
@@ -692,11 +851,19 @@ var Peer = class {
692
851
  sessionId
693
852
  }, {
694
853
  F: __dxlog_file3,
695
- L: 197,
854
+ L: 198,
696
855
  S: this,
697
856
  C: (f, a) => f(...a)
698
857
  });
699
- invariant3(!this.connection, "Already connected.");
858
+ invariant3(!this.connection, "Already connected.", {
859
+ F: __dxlog_file3,
860
+ L: 205,
861
+ S: this,
862
+ A: [
863
+ "!this.connection",
864
+ "'Already connected.'"
865
+ ]
866
+ });
700
867
  const connection = new Connection(
701
868
  this.topic,
702
869
  this.localPeerId,
@@ -714,7 +881,7 @@ var Peer = class {
714
881
  this._transportFactory
715
882
  );
716
883
  this._callbacks.onInitiated(connection);
717
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
884
+ void this._connectionCtx?.dispose();
718
885
  this._connectionCtx = this._ctx.derive();
719
886
  connection.stateChanged.on((state) => {
720
887
  switch (state) {
@@ -723,6 +890,18 @@ var Peer = class {
723
890
  this._lastConnectionTime = Date.now();
724
891
  this._callbacks.onConnected();
725
892
  this._connectionLimiter.doneConnecting(sessionId);
893
+ log3.trace("dxos.mesh.connection.connected", {
894
+ topic: this.topic,
895
+ localPeerId: this.localPeerId,
896
+ remotePeerId: this.id,
897
+ sessionId,
898
+ initiator
899
+ }, {
900
+ F: __dxlog_file3,
901
+ L: 231,
902
+ S: this,
903
+ C: (f, a) => f(...a)
904
+ });
726
905
  break;
727
906
  }
728
907
  case ConnectionState.CLOSED: {
@@ -733,11 +912,31 @@ var Peer = class {
733
912
  initiator
734
913
  }, {
735
914
  F: __dxlog_file3,
736
- L: 234,
915
+ L: 242,
916
+ S: this,
917
+ C: (f, a) => f(...a)
918
+ });
919
+ invariant3(this.connection === connection, "Connection mismatch (race condition).", {
920
+ F: __dxlog_file3,
921
+ L: 243,
922
+ S: this,
923
+ A: [
924
+ "this.connection === connection",
925
+ "'Connection mismatch (race condition).'"
926
+ ]
927
+ });
928
+ log3.trace("dxos.mesh.connection.closed", {
929
+ topic: this.topic,
930
+ localPeerId: this.localPeerId,
931
+ remotePeerId: this.id,
932
+ sessionId,
933
+ initiator
934
+ }, {
935
+ F: __dxlog_file3,
936
+ L: 245,
737
937
  S: this,
738
938
  C: (f, a) => f(...a)
739
939
  });
740
- invariant3(this.connection === connection, "Connection mismatch (race condition).");
741
940
  if (this._lastConnectionTime && this._lastConnectionTime + CONNECTION_COUNTS_STABLE_AFTER < Date.now()) {
742
941
  this._availableAfter = 0;
743
942
  } else {
@@ -764,16 +963,29 @@ var Peer = class {
764
963
  err
765
964
  }, {
766
965
  F: __dxlog_file3,
767
- L: 263,
966
+ L: 279,
768
967
  S: this,
769
968
  C: (f, a) => f(...a)
770
969
  });
771
- void this.closeConnection();
970
+ log3.trace("dxos.mesh.connection.error", {
971
+ topic: this.topic,
972
+ localPeerId: this.localPeerId,
973
+ remotePeerId: this.id,
974
+ sessionId,
975
+ initiator,
976
+ err
977
+ }, {
978
+ F: __dxlog_file3,
979
+ L: 280,
980
+ S: this,
981
+ C: (f, a) => f(...a)
982
+ });
983
+ void this.closeConnection(err);
772
984
  });
773
985
  this.connection = connection;
774
986
  return connection;
775
987
  }
776
- async closeConnection() {
988
+ async closeConnection(err) {
777
989
  if (!this.connection) {
778
990
  return;
779
991
  }
@@ -783,17 +995,17 @@ var Peer = class {
783
995
  sessionId: connection.sessionId
784
996
  }, {
785
997
  F: __dxlog_file3,
786
- L: 279,
998
+ L: 303,
787
999
  S: this,
788
1000
  C: (f, a) => f(...a)
789
1001
  });
790
- await connection.close();
1002
+ await connection.close(err);
791
1003
  log3("closed", {
792
1004
  peerId: this.id,
793
1005
  sessionId: connection.sessionId
794
1006
  }, {
795
1007
  F: __dxlog_file3,
796
- L: 285,
1008
+ L: 309,
797
1009
  S: this,
798
1010
  C: (f, a) => f(...a)
799
1011
  });
@@ -804,7 +1016,7 @@ var Peer = class {
804
1016
  message
805
1017
  }, {
806
1018
  F: __dxlog_file3,
807
- L: 290,
1019
+ L: 314,
808
1020
  S: this,
809
1021
  C: (f, a) => f(...a)
810
1022
  });
@@ -813,18 +1025,17 @@ var Peer = class {
813
1025
  await this.connection.signal(message);
814
1026
  }
815
1027
  async destroy() {
816
- var _a;
817
1028
  await this._ctx.dispose();
818
1029
  log3("Destroying peer", {
819
1030
  peerId: this.id,
820
1031
  topic: this.topic
821
1032
  }, {
822
1033
  F: __dxlog_file3,
823
- L: 300,
1034
+ L: 324,
824
1035
  S: this,
825
1036
  C: (f, a) => f(...a)
826
1037
  });
827
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1038
+ await this?.connection?.close();
828
1039
  }
829
1040
  };
830
1041
  _ts_decorate2([
@@ -869,9 +1080,9 @@ var Swarm = class {
869
1080
  this._transportFactory = _transportFactory;
870
1081
  this._label = _label;
871
1082
  this._connectionLimiter = _connectionLimiter;
872
- this._peers = new ComplexMap2(PublicKey4.hash);
873
1083
  this._ctx = new Context4();
874
1084
  this._listeningHandle = void 0;
1085
+ this._peers = new ComplexMap2(PublicKey4.hash);
875
1086
  this._instanceId = PublicKey4.random().toHex();
876
1087
  this.connectionAdded = new Event2();
877
1088
  this.disconnected = new Event2();
@@ -885,7 +1096,7 @@ var Swarm = class {
885
1096
  }
886
1097
  }), {
887
1098
  F: __dxlog_file4,
888
- L: 88,
1099
+ L: 87,
889
1100
  S: this,
890
1101
  C: (f, a) => f(...a)
891
1102
  });
@@ -893,7 +1104,7 @@ var Swarm = class {
893
1104
  peerId: _ownPeerId
894
1105
  }, {
895
1106
  F: __dxlog_file4,
896
- L: 92,
1107
+ L: 91,
897
1108
  S: this,
898
1109
  C: (f, a) => f(...a)
899
1110
  });
@@ -908,7 +1119,7 @@ var Swarm = class {
908
1119
  id: this._instanceId
909
1120
  }), {
910
1121
  F: __dxlog_file4,
911
- L: 101,
1122
+ L: 100,
912
1123
  S: this,
913
1124
  C: (f, a) => f(...a)
914
1125
  });
@@ -929,7 +1140,15 @@ var Swarm = class {
929
1140
  return this._topic;
930
1141
  }
931
1142
  async open() {
932
- invariant4(!this._listeningHandle);
1143
+ invariant4(!this._listeningHandle, void 0, {
1144
+ F: __dxlog_file4,
1145
+ L: 127,
1146
+ S: this,
1147
+ A: [
1148
+ "!this._listeningHandle",
1149
+ ""
1150
+ ]
1151
+ });
933
1152
  this._listeningHandle = await this._messenger.listen({
934
1153
  peerId: this._ownPeerId,
935
1154
  payloadType: "dxos.mesh.swarm.SwarmMessage",
@@ -938,7 +1157,7 @@ var Swarm = class {
938
1157
  err
939
1158
  }, {
940
1159
  F: __dxlog_file4,
941
- L: 135,
1160
+ L: 134,
942
1161
  S: this,
943
1162
  C: (f, a) => f(...a)
944
1163
  }));
@@ -946,27 +1165,34 @@ var Swarm = class {
946
1165
  });
947
1166
  }
948
1167
  async destroy() {
949
- var _a;
950
1168
  log4("destroying...", void 0, {
951
1169
  F: __dxlog_file4,
952
- L: 141,
1170
+ L: 140,
953
1171
  S: this,
954
1172
  C: (f, a) => f(...a)
955
1173
  });
956
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1174
+ await this._listeningHandle?.unsubscribe();
957
1175
  this._listeningHandle = void 0;
958
1176
  await this._ctx.dispose();
959
1177
  await this._topology.destroy();
960
1178
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key)));
961
1179
  log4("destroyed", void 0, {
962
1180
  F: __dxlog_file4,
963
- L: 148,
1181
+ L: 147,
964
1182
  S: this,
965
1183
  C: (f, a) => f(...a)
966
1184
  });
967
1185
  }
968
1186
  async setTopology(topology) {
969
- invariant4(!this._ctx.disposed, "Swarm is offline");
1187
+ invariant4(!this._ctx.disposed, "Swarm is offline", {
1188
+ F: __dxlog_file4,
1189
+ L: 151,
1190
+ S: this,
1191
+ A: [
1192
+ "!this._ctx.disposed",
1193
+ "'Swarm is offline'"
1194
+ ]
1195
+ });
970
1196
  if (topology === this._topology) {
971
1197
  return;
972
1198
  }
@@ -975,7 +1201,7 @@ var Swarm = class {
975
1201
  topology: getClassName(topology)
976
1202
  }, {
977
1203
  F: __dxlog_file4,
978
- L: 156,
1204
+ L: 155,
979
1205
  S: this,
980
1206
  C: (f, a) => f(...a)
981
1207
  });
@@ -985,19 +1211,18 @@ var Swarm = class {
985
1211
  this._topology.update();
986
1212
  }
987
1213
  onSwarmEvent(swarmEvent) {
988
- var _a;
989
1214
  log4("swarm event", {
990
1215
  swarmEvent
991
1216
  }, {
992
1217
  F: __dxlog_file4,
993
- L: 169,
1218
+ L: 168,
994
1219
  S: this,
995
1220
  C: (f, a) => f(...a)
996
1221
  });
997
1222
  if (this._ctx.disposed) {
998
1223
  log4("swarm event ignored for disposed swarm", void 0, {
999
1224
  F: __dxlog_file4,
1000
- L: 172,
1225
+ L: 171,
1001
1226
  S: this,
1002
1227
  C: (f, a) => f(...a)
1003
1228
  });
@@ -1009,7 +1234,7 @@ var Swarm = class {
1009
1234
  peerId
1010
1235
  }, {
1011
1236
  F: __dxlog_file4,
1012
- L: 178,
1237
+ L: 177,
1013
1238
  S: this,
1014
1239
  C: (f, a) => f(...a)
1015
1240
  });
@@ -1021,10 +1246,10 @@ var Swarm = class {
1021
1246
  const peer = this._peers.get(PublicKey4.from(swarmEvent.peerLeft.peer));
1022
1247
  if (peer) {
1023
1248
  peer.advertizing = false;
1024
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1249
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1025
1250
  void this._destroyPeer(peer.id).catch((err) => log4.catch(err, void 0, {
1026
1251
  F: __dxlog_file4,
1027
- L: 189,
1252
+ L: 188,
1028
1253
  S: this,
1029
1254
  C: (f, a) => f(...a)
1030
1255
  }));
@@ -1034,19 +1259,18 @@ var Swarm = class {
1034
1259
  this._topology.update();
1035
1260
  }
1036
1261
  async onOffer(message) {
1037
- var _a, _b;
1038
1262
  log4("offer", {
1039
1263
  message
1040
1264
  }, {
1041
1265
  F: __dxlog_file4,
1042
- L: 199,
1266
+ L: 198,
1043
1267
  S: this,
1044
1268
  C: (f, a) => f(...a)
1045
1269
  });
1046
1270
  if (this._ctx.disposed) {
1047
1271
  log4("ignored for disposed swarm", void 0, {
1048
1272
  F: __dxlog_file4,
1049
- L: 201,
1273
+ L: 200,
1050
1274
  S: this,
1051
1275
  C: (f, a) => f(...a)
1052
1276
  });
@@ -1054,13 +1278,21 @@ var Swarm = class {
1054
1278
  accept: false
1055
1279
  };
1056
1280
  }
1057
- invariant4(message.author);
1058
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1281
+ invariant4(message.author, void 0, {
1282
+ F: __dxlog_file4,
1283
+ L: 205,
1284
+ S: this,
1285
+ A: [
1286
+ "message.author",
1287
+ ""
1288
+ ]
1289
+ });
1290
+ if (!message.recipient?.equals(this._ownPeerId)) {
1059
1291
  log4("rejecting offer with incorrect peerId", {
1060
1292
  message
1061
1293
  }, {
1062
1294
  F: __dxlog_file4,
1063
- L: 208,
1295
+ L: 207,
1064
1296
  S: this,
1065
1297
  C: (f, a) => f(...a)
1066
1298
  });
@@ -1068,12 +1300,12 @@ var Swarm = class {
1068
1300
  accept: false
1069
1301
  };
1070
1302
  }
1071
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1303
+ if (!message.topic?.equals(this._topic)) {
1072
1304
  log4("rejecting offer with incorrect topic", {
1073
1305
  message
1074
1306
  }, {
1075
1307
  F: __dxlog_file4,
1076
- L: 212,
1308
+ L: 211,
1077
1309
  S: this,
1078
1310
  C: (f, a) => f(...a)
1079
1311
  });
@@ -1087,27 +1319,50 @@ var Swarm = class {
1087
1319
  return answer;
1088
1320
  }
1089
1321
  async onSignal(message) {
1090
- var _a, _b;
1091
1322
  log4("signal", {
1092
1323
  message
1093
1324
  }, {
1094
1325
  F: __dxlog_file4,
1095
- L: 223,
1326
+ L: 222,
1096
1327
  S: this,
1097
1328
  C: (f, a) => f(...a)
1098
1329
  });
1099
1330
  if (this._ctx.disposed) {
1100
1331
  log4.info("ignored for offline swarm", void 0, {
1101
1332
  F: __dxlog_file4,
1102
- L: 225,
1333
+ L: 224,
1103
1334
  S: this,
1104
1335
  C: (f, a) => f(...a)
1105
1336
  });
1106
1337
  return;
1107
1338
  }
1108
- invariant4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`);
1109
- invariant4((_b = message.topic) == null ? void 0 : _b.equals(this._topic));
1110
- invariant4(message.author);
1339
+ invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1340
+ F: __dxlog_file4,
1341
+ L: 227,
1342
+ S: this,
1343
+ A: [
1344
+ "message.recipient?.equals(this._ownPeerId)",
1345
+ "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1346
+ ]
1347
+ });
1348
+ invariant4(message.topic?.equals(this._topic), void 0, {
1349
+ F: __dxlog_file4,
1350
+ L: 231,
1351
+ S: this,
1352
+ A: [
1353
+ "message.topic?.equals(this._topic)",
1354
+ ""
1355
+ ]
1356
+ });
1357
+ invariant4(message.author, void 0, {
1358
+ F: __dxlog_file4,
1359
+ L: 232,
1360
+ S: this,
1361
+ A: [
1362
+ "message.author",
1363
+ ""
1364
+ ]
1365
+ });
1111
1366
  const peer = this._getOrCreatePeer(message.author);
1112
1367
  await peer.onSignal(message);
1113
1368
  }
@@ -1159,7 +1414,15 @@ var Swarm = class {
1159
1414
  return peer;
1160
1415
  }
1161
1416
  async _destroyPeer(peerId) {
1162
- invariant4(this._peers.has(peerId));
1417
+ invariant4(this._peers.has(peerId), void 0, {
1418
+ F: __dxlog_file4,
1419
+ L: 302,
1420
+ S: this,
1421
+ A: [
1422
+ "this._peers.has(peerId)",
1423
+ ""
1424
+ ]
1425
+ });
1163
1426
  await this._peers.get(peerId).destroy();
1164
1427
  this._peers.delete(peerId);
1165
1428
  }
@@ -1180,7 +1443,7 @@ var Swarm = class {
1180
1443
  } catch (err) {
1181
1444
  log4("initiation error", err, {
1182
1445
  F: __dxlog_file4,
1183
- L: 329,
1446
+ L: 328,
1184
1447
  S: this,
1185
1448
  C: (f, a) => f(...a)
1186
1449
  });
@@ -1208,7 +1471,7 @@ var Swarm = class {
1208
1471
  remoteId
1209
1472
  }, {
1210
1473
  F: __dxlog_file4,
1211
- L: 356,
1474
+ L: 355,
1212
1475
  S: this,
1213
1476
  C: (f, a) => f(...a)
1214
1477
  });
@@ -1225,7 +1488,7 @@ var Swarm = class {
1225
1488
  remoteId
1226
1489
  }, {
1227
1490
  F: __dxlog_file4,
1228
- L: 370,
1491
+ L: 369,
1229
1492
  S: this,
1230
1493
  C: (f, a) => f(...a)
1231
1494
  });
@@ -1235,7 +1498,7 @@ var Swarm = class {
1235
1498
  remoteId
1236
1499
  }, {
1237
1500
  F: __dxlog_file4,
1238
- L: 373,
1501
+ L: 372,
1239
1502
  S: this,
1240
1503
  C: (f, a) => f(...a)
1241
1504
  });
@@ -1294,8 +1557,7 @@ var SwarmMapper = class {
1294
1557
  }));
1295
1558
  }));
1296
1559
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1297
- var _a;
1298
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1560
+ this._connectionSubscriptions.get(peerId)?.();
1299
1561
  this._connectionSubscriptions.delete(peerId);
1300
1562
  this._update();
1301
1563
  }));
@@ -1345,8 +1607,9 @@ var SwarmMapper = class {
1345
1607
  import { DeferredTask as DeferredTask2 } from "@dxos/async";
1346
1608
  import { Context as Context5 } from "@dxos/context";
1347
1609
  import { CancelledError as CancelledError3 } from "@dxos/errors";
1610
+ import { invariant as invariant5 } from "@dxos/invariant";
1348
1611
  import { PublicKey as PublicKey6 } from "@dxos/keys";
1349
- import { invariant as invariant5, log as log6 } from "@dxos/log";
1612
+ import { log as log6 } from "@dxos/log";
1350
1613
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
1351
1614
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1352
1615
  var MAX_CONCURRENT_INITIATING_CONNECTIONS = 3;
@@ -1370,7 +1633,7 @@ var ConnectionLimiter = class {
1370
1633
  async connecting(sessionId) {
1371
1634
  invariant5(!this._waitingPromises.has(sessionId), "Peer is already waiting for connection", {
1372
1635
  F: __dxlog_file6,
1373
- L: 47,
1636
+ L: 48,
1374
1637
  S: this,
1375
1638
  A: [
1376
1639
  "!this._waitingPromises.has(sessionId)",
@@ -1381,7 +1644,7 @@ var ConnectionLimiter = class {
1381
1644
  sessionId
1382
1645
  }, {
1383
1646
  F: __dxlog_file6,
1384
- L: 48,
1647
+ L: 49,
1385
1648
  S: this,
1386
1649
  C: (f, a) => f(...a)
1387
1650
  });
@@ -1396,7 +1659,7 @@ var ConnectionLimiter = class {
1396
1659
  sessionId
1397
1660
  }, {
1398
1661
  F: __dxlog_file6,
1399
- L: 56,
1662
+ L: 57,
1400
1663
  S: this,
1401
1664
  C: (f, a) => f(...a)
1402
1665
  });
@@ -1409,7 +1672,7 @@ var ConnectionLimiter = class {
1409
1672
  sessionId
1410
1673
  }, {
1411
1674
  F: __dxlog_file6,
1412
- L: 63,
1675
+ L: 64,
1413
1676
  S: this,
1414
1677
  C: (f, a) => f(...a)
1415
1678
  });
@@ -1444,8 +1707,7 @@ var ConnectionLog = class {
1444
1707
  this.update = new Event4();
1445
1708
  }
1446
1709
  getSwarmInfo(swarmId) {
1447
- var _a;
1448
- return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
1710
+ return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
1449
1711
  }
1450
1712
  get swarms() {
1451
1713
  return Array.from(this._swarms.values());
@@ -1461,9 +1723,9 @@ var ConnectionLog = class {
1461
1723
  this._swarms.set(PublicKey7.from(swarm._instanceId), info);
1462
1724
  this.update.emit();
1463
1725
  swarm.connectionAdded.on((connection) => {
1464
- var _a, _b;
1465
1726
  const connectionInfo = {
1466
1727
  state: ConnectionState.INITIAL,
1728
+ closeReason: connection.closeReason,
1467
1729
  remotePeerId: connection.remoteId,
1468
1730
  sessionId: connection.sessionId,
1469
1731
  transport: connection.transport && Object.getPrototypeOf(connection.transport).constructor.name,
@@ -1474,13 +1736,14 @@ var ConnectionLog = class {
1474
1736
  this.update.emit();
1475
1737
  connection.stateChanged.on((state) => {
1476
1738
  connectionInfo.state = state;
1739
+ connectionInfo.closeReason = connection.closeReason;
1477
1740
  connectionInfo.events.push({
1478
1741
  type: EventType.CONNECTION_STATE_CHANGED,
1479
1742
  newState: state
1480
1743
  });
1481
1744
  this.update.emit();
1482
1745
  });
1483
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1746
+ connection.protocol?.stats?.on((stats) => {
1484
1747
  connectionInfo.streams = stats.channels;
1485
1748
  this.update.emit();
1486
1749
  });
@@ -1493,8 +1756,8 @@ var ConnectionLog = class {
1493
1756
  };
1494
1757
 
1495
1758
  // packages/core/mesh/network-manager/src/network-manager.ts
1496
- import invariant6 from "tiny-invariant";
1497
1759
  import { Event as Event5 } from "@dxos/async";
1760
+ import { invariant as invariant6 } from "@dxos/invariant";
1498
1761
  import { PublicKey as PublicKey8 } from "@dxos/keys";
1499
1762
  import { log as log7 } from "@dxos/log";
1500
1763
  import { Messenger } from "@dxos/messaging";
@@ -1515,10 +1778,7 @@ var NetworkManager = class {
1515
1778
  this._instanceId = PublicKey8.random().toHex();
1516
1779
  this._transportFactory = transportFactory;
1517
1780
  this._signalManager = signalManager;
1518
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1519
- var _a;
1520
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1521
- });
1781
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1522
1782
  this._messenger = new Messenger({
1523
1783
  signalManager: this._signalManager
1524
1784
  });
@@ -1553,7 +1813,7 @@ var NetworkManager = class {
1553
1813
  id: this._instanceId
1554
1814
  }), {
1555
1815
  F: __dxlog_file7,
1556
- L: 131,
1816
+ L: 130,
1557
1817
  S: this,
1558
1818
  C: (f, a) => f(...a)
1559
1819
  });
@@ -1563,7 +1823,7 @@ var NetworkManager = class {
1563
1823
  id: this._instanceId
1564
1824
  }), {
1565
1825
  F: __dxlog_file7,
1566
- L: 134,
1826
+ L: 133,
1567
1827
  S: this,
1568
1828
  C: (f, a) => f(...a)
1569
1829
  });
@@ -1573,7 +1833,7 @@ var NetworkManager = class {
1573
1833
  await this.leaveSwarm(topic).catch((err) => {
1574
1834
  log7(err, void 0, {
1575
1835
  F: __dxlog_file7,
1576
- L: 140,
1836
+ L: 139,
1577
1837
  S: this,
1578
1838
  C: (f, a) => f(...a)
1579
1839
  });
@@ -1586,11 +1846,42 @@ var NetworkManager = class {
1586
1846
  * Join the swarm.
1587
1847
  */
1588
1848
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1589
- var _a;
1590
- invariant6(PublicKey8.isPublicKey(topic));
1591
- invariant6(PublicKey8.isPublicKey(peerId));
1592
- invariant6(topology);
1593
- invariant6(typeof protocol === "function");
1849
+ invariant6(PublicKey8.isPublicKey(topic), void 0, {
1850
+ F: __dxlog_file7,
1851
+ L: 157,
1852
+ S: this,
1853
+ A: [
1854
+ "PublicKey.isPublicKey(topic)",
1855
+ ""
1856
+ ]
1857
+ });
1858
+ invariant6(PublicKey8.isPublicKey(peerId), void 0, {
1859
+ F: __dxlog_file7,
1860
+ L: 158,
1861
+ S: this,
1862
+ A: [
1863
+ "PublicKey.isPublicKey(peerId)",
1864
+ ""
1865
+ ]
1866
+ });
1867
+ invariant6(topology, void 0, {
1868
+ F: __dxlog_file7,
1869
+ L: 159,
1870
+ S: this,
1871
+ A: [
1872
+ "topology",
1873
+ ""
1874
+ ]
1875
+ });
1876
+ invariant6(typeof protocol === "function", void 0, {
1877
+ F: __dxlog_file7,
1878
+ L: 160,
1879
+ S: this,
1880
+ A: [
1881
+ "typeof protocol === 'function'",
1882
+ ""
1883
+ ]
1884
+ });
1594
1885
  if (this._swarms.has(topic)) {
1595
1886
  throw new Error(`Already connected to swarm: ${PublicKey8.from(topic)}`);
1596
1887
  }
@@ -1600,7 +1891,7 @@ var NetworkManager = class {
1600
1891
  topology: topology.toString()
1601
1892
  }, {
1602
1893
  F: __dxlog_file7,
1603
- L: 166,
1894
+ L: 165,
1604
1895
  S: this,
1605
1896
  C: (f, a) => f(...a)
1606
1897
  });
@@ -1610,7 +1901,7 @@ var NetworkManager = class {
1610
1901
  error
1611
1902
  }, {
1612
1903
  F: __dxlog_file7,
1613
- L: 179,
1904
+ L: 178,
1614
1905
  S: this,
1615
1906
  C: (f, a) => f(...a)
1616
1907
  });
@@ -1623,18 +1914,18 @@ var NetworkManager = class {
1623
1914
  peerId
1624
1915
  }).catch((error) => log7.catch(error, void 0, {
1625
1916
  F: __dxlog_file7,
1626
- L: 188,
1917
+ L: 187,
1627
1918
  S: this,
1628
1919
  C: (f, a) => f(...a)
1629
1920
  }));
1630
1921
  this.topicsUpdated.emit();
1631
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1922
+ this._connectionLog?.joinedSwarm(swarm);
1632
1923
  log7("joined", {
1633
1924
  topic: PublicKey8.from(topic),
1634
1925
  count: this._swarms.size
1635
1926
  }, {
1636
1927
  F: __dxlog_file7,
1637
- L: 192,
1928
+ L: 191,
1638
1929
  S: this,
1639
1930
  C: (f, a) => f(...a)
1640
1931
  });
@@ -1646,7 +1937,6 @@ var NetworkManager = class {
1646
1937
  * Close the connection.
1647
1938
  */
1648
1939
  async leaveSwarm(topic) {
1649
- var _a;
1650
1940
  if (!this._swarms.has(topic)) {
1651
1941
  return;
1652
1942
  }
@@ -1654,7 +1944,7 @@ var NetworkManager = class {
1654
1944
  topic: PublicKey8.from(topic)
1655
1945
  }, {
1656
1946
  F: __dxlog_file7,
1657
- L: 208,
1947
+ L: 207,
1658
1948
  S: this,
1659
1949
  C: (f, a) => f(...a)
1660
1950
  });
@@ -1666,7 +1956,7 @@ var NetworkManager = class {
1666
1956
  const map = this._mappers.get(topic);
1667
1957
  map.destroy();
1668
1958
  this._mappers.delete(topic);
1669
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1959
+ this._connectionLog?.leftSwarm(swarm);
1670
1960
  await swarm.destroy();
1671
1961
  this._swarms.delete(topic);
1672
1962
  await this.topicsUpdated.emit();
@@ -1675,7 +1965,7 @@ var NetworkManager = class {
1675
1965
  count: this._swarms.size
1676
1966
  }, {
1677
1967
  F: __dxlog_file7,
1678
- L: 222,
1968
+ L: 221,
1679
1969
  S: this,
1680
1970
  C: (f, a) => f(...a)
1681
1971
  });
@@ -1709,17 +1999,34 @@ var NetworkManager = class {
1709
1999
  };
1710
2000
 
1711
2001
  // packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts
1712
- import invariant7 from "tiny-invariant";
2002
+ import { invariant as invariant7 } from "@dxos/invariant";
2003
+ var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/fully-connected-topology.ts";
1713
2004
  var FullyConnectedTopology = class {
1714
2005
  toString() {
1715
2006
  return "FullyConnectedTopology";
1716
2007
  }
1717
2008
  init(controller) {
1718
- invariant7(!this._controller, "Already initialized");
2009
+ invariant7(!this._controller, "Already initialized", {
2010
+ F: __dxlog_file8,
2011
+ L: 18,
2012
+ S: this,
2013
+ A: [
2014
+ "!this._controller",
2015
+ "'Already initialized'"
2016
+ ]
2017
+ });
1719
2018
  this._controller = controller;
1720
2019
  }
1721
2020
  update() {
1722
- invariant7(this._controller, "Not initialized");
2021
+ invariant7(this._controller, "Not initialized", {
2022
+ F: __dxlog_file8,
2023
+ L: 23,
2024
+ S: this,
2025
+ A: [
2026
+ "this._controller",
2027
+ "'Not initialized'"
2028
+ ]
2029
+ });
1723
2030
  const { candidates: discovered } = this._controller.getState();
1724
2031
  for (const peer of discovered) {
1725
2032
  this._controller.connect(peer);
@@ -1733,10 +2040,10 @@ var FullyConnectedTopology = class {
1733
2040
  };
1734
2041
 
1735
2042
  // packages/core/mesh/network-manager/src/topology/mmst-topology.ts
1736
- import invariant8 from "tiny-invariant";
1737
2043
  import distance from "xor-distance";
2044
+ import { invariant as invariant8 } from "@dxos/invariant";
1738
2045
  import { log as log8 } from "@dxos/log";
1739
- var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
2046
+ var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/mmst-topology.ts";
1740
2047
  var MMSTTopology = class {
1741
2048
  constructor({ originateConnections = 2, maxPeers = 4, sampleSize = 10 } = {}) {
1742
2049
  this._sampleCollected = false;
@@ -1745,15 +2052,31 @@ var MMSTTopology = class {
1745
2052
  this._sampleSize = sampleSize;
1746
2053
  }
1747
2054
  init(controller) {
1748
- invariant8(!this._controller, "Already initialized");
2055
+ invariant8(!this._controller, "Already initialized", {
2056
+ F: __dxlog_file9,
2057
+ L: 46,
2058
+ S: this,
2059
+ A: [
2060
+ "!this._controller",
2061
+ "'Already initialized'"
2062
+ ]
2063
+ });
1749
2064
  this._controller = controller;
1750
2065
  }
1751
2066
  update() {
1752
- invariant8(this._controller, "Not initialized");
2067
+ invariant8(this._controller, "Not initialized", {
2068
+ F: __dxlog_file9,
2069
+ L: 51,
2070
+ S: this,
2071
+ A: [
2072
+ "this._controller",
2073
+ "'Not initialized'"
2074
+ ]
2075
+ });
1753
2076
  const { connected, candidates } = this._controller.getState();
1754
2077
  if (this._sampleCollected || connected.length > this._maxPeers || candidates.length > 0) {
1755
2078
  log8("Running the algorithm.", void 0, {
1756
- F: __dxlog_file8,
2079
+ F: __dxlog_file9,
1757
2080
  L: 55,
1758
2081
  S: this,
1759
2082
  C: (f, a) => f(...a)
@@ -1763,11 +2086,19 @@ var MMSTTopology = class {
1763
2086
  }
1764
2087
  }
1765
2088
  async onOffer(peer) {
1766
- invariant8(this._controller, "Not initialized");
2089
+ invariant8(this._controller, "Not initialized", {
2090
+ F: __dxlog_file9,
2091
+ L: 62,
2092
+ S: this,
2093
+ A: [
2094
+ "this._controller",
2095
+ "'Not initialized'"
2096
+ ]
2097
+ });
1767
2098
  const { connected } = this._controller.getState();
1768
2099
  const accept = connected.length < this._maxPeers;
1769
2100
  log8(`Offer ${peer} accept=${accept}`, void 0, {
1770
- F: __dxlog_file8,
2101
+ F: __dxlog_file9,
1771
2102
  L: 65,
1772
2103
  S: this,
1773
2104
  C: (f, a) => f(...a)
@@ -1777,13 +2108,21 @@ var MMSTTopology = class {
1777
2108
  async destroy() {
1778
2109
  }
1779
2110
  _runAlgorithm() {
1780
- invariant8(this._controller, "Not initialized");
2111
+ invariant8(this._controller, "Not initialized", {
2112
+ F: __dxlog_file9,
2113
+ L: 74,
2114
+ S: this,
2115
+ A: [
2116
+ "this._controller",
2117
+ "'Not initialized'"
2118
+ ]
2119
+ });
1781
2120
  const { connected, candidates, ownPeerId } = this._controller.getState();
1782
2121
  if (connected.length > this._maxPeers) {
1783
2122
  const sorted = sortByXorDistance(connected, ownPeerId).reverse().slice(0, this._maxPeers - connected.length);
1784
2123
  for (const peer of sorted) {
1785
2124
  log8(`Disconnect ${peer}.`, void 0, {
1786
- F: __dxlog_file8,
2125
+ F: __dxlog_file9,
1787
2126
  L: 83,
1788
2127
  S: this,
1789
2128
  C: (f, a) => f(...a)
@@ -1795,7 +2134,7 @@ var MMSTTopology = class {
1795
2134
  const sorted = sortByXorDistance(sample, ownPeerId).slice(0, this._originateConnections - connected.length);
1796
2135
  for (const peer of sorted) {
1797
2136
  log8(`Connect ${peer}.`, void 0, {
1798
- F: __dxlog_file8,
2137
+ F: __dxlog_file9,
1799
2138
  L: 91,
1800
2139
  S: this,
1801
2140
  C: (f, a) => f(...a)
@@ -1811,9 +2150,9 @@ var MMSTTopology = class {
1811
2150
  var sortByXorDistance = (keys, reference) => keys.sort((a, b) => distance.gt(distance(a.asBuffer(), reference.asBuffer()), distance(b.asBuffer(), reference.asBuffer())));
1812
2151
 
1813
2152
  // packages/core/mesh/network-manager/src/topology/star-topology.ts
1814
- import invariant9 from "tiny-invariant";
2153
+ import { invariant as invariant9 } from "@dxos/invariant";
1815
2154
  import { log as log9 } from "@dxos/log";
1816
- var __dxlog_file9 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2155
+ var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
1817
2156
  var StarTopology = class {
1818
2157
  // prettier-ignore
1819
2158
  constructor(_centralPeer) {
@@ -1823,16 +2162,32 @@ var StarTopology = class {
1823
2162
  return `StarTopology(${this._centralPeer.truncate()})`;
1824
2163
  }
1825
2164
  init(controller) {
1826
- invariant9(!this._controller, "Already initialized.");
2165
+ invariant9(!this._controller, "Already initialized.", {
2166
+ F: __dxlog_file10,
2167
+ L: 24,
2168
+ S: this,
2169
+ A: [
2170
+ "!this._controller",
2171
+ "'Already initialized.'"
2172
+ ]
2173
+ });
1827
2174
  this._controller = controller;
1828
2175
  }
1829
2176
  update() {
1830
- invariant9(this._controller, "Not initialized.");
2177
+ invariant9(this._controller, "Not initialized.", {
2178
+ F: __dxlog_file10,
2179
+ L: 29,
2180
+ S: this,
2181
+ A: [
2182
+ "this._controller",
2183
+ "'Not initialized.'"
2184
+ ]
2185
+ });
1831
2186
  const { candidates, connected, ownPeerId } = this._controller.getState();
1832
2187
  if (!ownPeerId.equals(this._centralPeer)) {
1833
2188
  log9("leaf peer dropping all connections apart from central peer.", void 0, {
1834
- F: __dxlog_file9,
1835
- L: 33,
2189
+ F: __dxlog_file10,
2190
+ L: 32,
1836
2191
  S: this,
1837
2192
  C: (f, a) => f(...a)
1838
2193
  });
@@ -1841,8 +2196,8 @@ var StarTopology = class {
1841
2196
  log9("dropping connection", {
1842
2197
  peer
1843
2198
  }, {
1844
- F: __dxlog_file9,
1845
- L: 38,
2199
+ F: __dxlog_file10,
2200
+ L: 37,
1846
2201
  S: this,
1847
2202
  C: (f, a) => f(...a)
1848
2203
  });
@@ -1855,8 +2210,8 @@ var StarTopology = class {
1855
2210
  log9("connecting to peer", {
1856
2211
  peer
1857
2212
  }, {
1858
- F: __dxlog_file9,
1859
- L: 47,
2213
+ F: __dxlog_file10,
2214
+ L: 46,
1860
2215
  S: this,
1861
2216
  C: (f, a) => f(...a)
1862
2217
  });
@@ -1865,15 +2220,23 @@ var StarTopology = class {
1865
2220
  }
1866
2221
  }
1867
2222
  async onOffer(peer) {
1868
- invariant9(this._controller, "Not initialized.");
2223
+ invariant9(this._controller, "Not initialized.", {
2224
+ F: __dxlog_file10,
2225
+ L: 53,
2226
+ S: this,
2227
+ A: [
2228
+ "this._controller",
2229
+ "'Not initialized.'"
2230
+ ]
2231
+ });
1869
2232
  const { ownPeerId } = this._controller.getState();
1870
2233
  log9("offer", {
1871
2234
  peer,
1872
2235
  isCentral: peer.equals(this._centralPeer),
1873
2236
  isSelfCentral: ownPeerId.equals(this._centralPeer)
1874
2237
  }, {
1875
- F: __dxlog_file9,
1876
- L: 56,
2238
+ F: __dxlog_file10,
2239
+ L: 55,
1877
2240
  S: this,
1878
2241
  C: (f, a) => f(...a)
1879
2242
  });
@@ -1885,9 +2248,9 @@ var StarTopology = class {
1885
2248
 
1886
2249
  // packages/core/mesh/network-manager/src/transport/memory-transport.ts
1887
2250
  import { Transform } from "@dxos/node-std/stream";
1888
- import invariant10 from "tiny-invariant";
1889
2251
  import { Event as Event6, Trigger } from "@dxos/async";
1890
2252
  import { ErrorStream as ErrorStream3 } from "@dxos/debug";
2253
+ import { invariant as invariant10 } from "@dxos/invariant";
1891
2254
  import { PublicKey as PublicKey9 } from "@dxos/keys";
1892
2255
  import { log as log10, logInfo as logInfo2 } from "@dxos/log";
1893
2256
  import { ComplexMap as ComplexMap7 } from "@dxos/util";
@@ -1901,7 +2264,7 @@ function _ts_decorate4(decorators, target, key, desc) {
1901
2264
  r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1902
2265
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1903
2266
  }
1904
- var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
2267
+ var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/memory-transport.ts";
1905
2268
  var MEMORY_TRANSPORT_DELAY = 1;
1906
2269
  var createStreamDelay = (delay) => {
1907
2270
  return new Transform({
@@ -1914,9 +2277,12 @@ var createStreamDelay = (delay) => {
1914
2277
  var MemoryTransportFactory = {
1915
2278
  createTransport: (params) => new MemoryTransport(params)
1916
2279
  };
1917
- var _MemoryTransport = class {
2280
+ var MemoryTransport = class _MemoryTransport {
2281
+ static {
2282
+ // TODO(burdon): Remove static properties (inject context into constructor).
2283
+ this._connections = new ComplexMap7(PublicKey9.hash);
2284
+ }
1918
2285
  constructor(options) {
1919
- var _a;
1920
2286
  this.options = options;
1921
2287
  this.closed = new Event6();
1922
2288
  this.connected = new Event6();
@@ -1927,17 +2293,25 @@ var _MemoryTransport = class {
1927
2293
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
1928
2294
  this._destroyed = false;
1929
2295
  log10("creating", void 0, {
1930
- F: __dxlog_file10,
2296
+ F: __dxlog_file11,
1931
2297
  L: 64,
1932
2298
  S: this,
1933
2299
  C: (f, a) => f(...a)
1934
2300
  });
1935
- invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection");
2301
+ invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2302
+ F: __dxlog_file11,
2303
+ L: 66,
2304
+ S: this,
2305
+ A: [
2306
+ "!MemoryTransport._connections.has(this._instanceId)",
2307
+ "'Duplicate memory connection'"
2308
+ ]
2309
+ });
1936
2310
  _MemoryTransport._connections.set(this._instanceId, this);
1937
2311
  if (this.options.initiator) {
1938
2312
  setTimeout(async () => {
1939
2313
  log10("sending signal", void 0, {
1940
- F: __dxlog_file10,
2314
+ F: __dxlog_file11,
1941
2315
  L: 73,
1942
2316
  S: this,
1943
2317
  C: (f, a) => f(...a)
@@ -1954,7 +2328,7 @@ var _MemoryTransport = class {
1954
2328
  });
1955
2329
  } else {
1956
2330
  this._remote.wait({
1957
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2331
+ timeout: this.options.timeout ?? 1e3
1958
2332
  }).then((remoteId) => {
1959
2333
  if (this._destroyed) {
1960
2334
  return;
@@ -1966,11 +2340,19 @@ var _MemoryTransport = class {
1966
2340
  this.closed.emit();
1967
2341
  return;
1968
2342
  }
1969
- invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`);
2343
+ invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2344
+ F: __dxlog_file11,
2345
+ L: 99,
2346
+ S: this,
2347
+ A: [
2348
+ "!this._remoteConnection._remoteConnection",
2349
+ "`Remote already connected: ${this._remoteInstanceId}`"
2350
+ ]
2351
+ });
1970
2352
  this._remoteConnection._remoteConnection = this;
1971
2353
  this._remoteConnection._remoteInstanceId = this._instanceId;
1972
2354
  log10("connected", void 0, {
1973
- F: __dxlog_file10,
2355
+ F: __dxlog_file11,
1974
2356
  L: 103,
1975
2357
  S: this,
1976
2358
  C: (f, a) => f(...a)
@@ -1988,7 +2370,7 @@ var _MemoryTransport = class {
1988
2370
  }
1989
2371
  async destroy() {
1990
2372
  log10("closing", void 0, {
1991
- F: __dxlog_file10,
2373
+ F: __dxlog_file11,
1992
2374
  L: 124,
1993
2375
  S: this,
1994
2376
  C: (f, a) => f(...a)
@@ -1997,7 +2379,7 @@ var _MemoryTransport = class {
1997
2379
  _MemoryTransport._connections.delete(this._instanceId);
1998
2380
  if (this._remoteConnection) {
1999
2381
  log10("closing", void 0, {
2000
- F: __dxlog_file10,
2382
+ F: __dxlog_file11,
2001
2383
  L: 129,
2002
2384
  S: this,
2003
2385
  C: (f, a) => f(...a)
@@ -2010,7 +2392,7 @@ var _MemoryTransport = class {
2010
2392
  this._remoteConnection._remoteConnection = void 0;
2011
2393
  this._remoteConnection = void 0;
2012
2394
  log10("closed", void 0, {
2013
- F: __dxlog_file10,
2395
+ F: __dxlog_file11,
2014
2396
  L: 147,
2015
2397
  S: this,
2016
2398
  C: (f, a) => f(...a)
@@ -2018,7 +2400,7 @@ var _MemoryTransport = class {
2018
2400
  }
2019
2401
  this.closed.emit();
2020
2402
  log10("closed", void 0, {
2021
- F: __dxlog_file10,
2403
+ F: __dxlog_file11,
2022
2404
  L: 151,
2023
2405
  S: this,
2024
2406
  C: (f, a) => f(...a)
@@ -2028,12 +2410,12 @@ var _MemoryTransport = class {
2028
2410
  log10("received signal", {
2029
2411
  payload
2030
2412
  }, {
2031
- F: __dxlog_file10,
2413
+ F: __dxlog_file11,
2032
2414
  L: 155,
2033
2415
  S: this,
2034
2416
  C: (f, a) => f(...a)
2035
2417
  });
2036
- if (!(payload == null ? void 0 : payload.transportId)) {
2418
+ if (!payload?.transportId) {
2037
2419
  return;
2038
2420
  }
2039
2421
  const transportId = payload.transportId;
@@ -2043,9 +2425,6 @@ var _MemoryTransport = class {
2043
2425
  }
2044
2426
  }
2045
2427
  };
2046
- var MemoryTransport = _MemoryTransport;
2047
- // TODO(burdon): Remove static properties (inject context into constructor).
2048
- MemoryTransport._connections = new ComplexMap7(PublicKey9.hash);
2049
2428
  _ts_decorate4([
2050
2429
  logInfo2
2051
2430
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2055,9 +2434,9 @@ _ts_decorate4([
2055
2434
 
2056
2435
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2057
2436
  import SimplePeerConstructor from "simple-peer";
2058
- import invariant11 from "tiny-invariant";
2059
2437
  import { Event as Event7 } from "@dxos/async";
2060
2438
  import { ErrorStream as ErrorStream4, raise as raise2 } from "@dxos/debug";
2439
+ import { invariant as invariant11 } from "@dxos/invariant";
2061
2440
  import { PublicKey as PublicKey10 } from "@dxos/keys";
2062
2441
  import { log as log11 } from "@dxos/log";
2063
2442
  import { trace as trace4 } from "@dxos/protocols";
@@ -2066,43 +2445,47 @@ import { trace as trace4 } from "@dxos/protocols";
2066
2445
  var wrtc = null;
2067
2446
  try {
2068
2447
  wrtc = __require("@koush/wrtc");
2069
- } catch (e) {
2448
+ } catch {
2070
2449
  }
2071
2450
 
2072
2451
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2073
- var __dxlog_file11 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2452
+ var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2074
2453
  var WebRTCTransport = class {
2075
2454
  constructor(params) {
2076
- var _a;
2077
2455
  this.params = params;
2456
+ this._instanceId = PublicKey10.random().toHex();
2078
2457
  this._closed = false;
2079
2458
  this.closed = new Event7();
2080
2459
  this.connected = new Event7();
2081
2460
  this.errors = new ErrorStream4();
2082
- this._instanceId = PublicKey10.random().toHex();
2083
2461
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.begin({
2084
2462
  id: this._instanceId
2085
2463
  }), {
2086
- F: __dxlog_file11,
2087
- L: 39,
2464
+ F: __dxlog_file12,
2465
+ L: 44,
2088
2466
  S: this,
2089
2467
  C: (f, a) => f(...a)
2090
2468
  });
2091
2469
  log11("created connection", params, {
2092
- F: __dxlog_file11,
2093
- L: 40,
2470
+ F: __dxlog_file12,
2471
+ L: 45,
2094
2472
  S: this,
2095
2473
  C: (f, a) => f(...a)
2096
2474
  });
2097
2475
  this._peer = new SimplePeerConstructor({
2098
2476
  initiator: this.params.initiator,
2099
- wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
2100
- config: this.params.webrtcConfig
2477
+ config: this.params.webrtcConfig,
2478
+ // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel
2479
+ channelConfig: {
2480
+ protocol: "dxos.mesh"
2481
+ },
2482
+ // Custom WebRTC implementation (for Node); otherwise get-browser-rtc.
2483
+ wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available"))
2101
2484
  });
2102
2485
  this._peer.on("signal", async (data) => {
2103
2486
  log11("signal", data, {
2104
- F: __dxlog_file11,
2105
- L: 48,
2487
+ F: __dxlog_file12,
2488
+ L: 62,
2106
2489
  S: this,
2107
2490
  C: (f, a) => f(...a)
2108
2491
  });
@@ -2114,8 +2497,8 @@ var WebRTCTransport = class {
2114
2497
  });
2115
2498
  this._peer.on("connect", () => {
2116
2499
  log11("connected", void 0, {
2117
- F: __dxlog_file11,
2118
- L: 53,
2500
+ F: __dxlog_file12,
2501
+ L: 67,
2119
2502
  S: this,
2120
2503
  C: (f, a) => f(...a)
2121
2504
  });
@@ -2124,8 +2507,8 @@ var WebRTCTransport = class {
2124
2507
  });
2125
2508
  this._peer.on("close", async () => {
2126
2509
  log11("closed", void 0, {
2127
- F: __dxlog_file11,
2128
- L: 59,
2510
+ F: __dxlog_file12,
2511
+ L: 73,
2129
2512
  S: this,
2130
2513
  C: (f, a) => f(...a)
2131
2514
  });
@@ -2133,39 +2516,38 @@ var WebRTCTransport = class {
2133
2516
  this.closed.emit();
2134
2517
  });
2135
2518
  this._peer.on("error", async (err) => {
2136
- var _a2;
2137
2519
  this.errors.raise(err);
2138
2520
  try {
2139
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2521
+ if (typeof this._peer?._pc.getStats === "function") {
2140
2522
  this._peer._pc.getStats().then((stats) => {
2141
2523
  log11.warn("report after webrtc error", {
2142
2524
  config: this.params.webrtcConfig,
2143
2525
  stats
2144
2526
  }, {
2145
- F: __dxlog_file11,
2146
- L: 71,
2527
+ F: __dxlog_file12,
2528
+ L: 85,
2147
2529
  S: this,
2148
2530
  C: (f, a) => f(...a)
2149
2531
  });
2150
2532
  });
2151
2533
  }
2152
- } catch (e) {
2534
+ } catch {
2153
2535
  }
2154
2536
  await this.destroy();
2155
2537
  });
2156
2538
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.end({
2157
2539
  id: this._instanceId
2158
2540
  }), {
2159
- F: __dxlog_file11,
2160
- L: 81,
2541
+ F: __dxlog_file12,
2542
+ L: 95,
2161
2543
  S: this,
2162
2544
  C: (f, a) => f(...a)
2163
2545
  });
2164
2546
  }
2165
2547
  async destroy() {
2166
2548
  log11("closing...", void 0, {
2167
- F: __dxlog_file11,
2168
- L: 85,
2549
+ F: __dxlog_file12,
2550
+ L: 99,
2169
2551
  S: this,
2170
2552
  C: (f, a) => f(...a)
2171
2553
  });
@@ -2174,8 +2556,8 @@ var WebRTCTransport = class {
2174
2556
  this._peer.destroy();
2175
2557
  this.closed.emit();
2176
2558
  log11("closed", void 0, {
2177
- F: __dxlog_file11,
2178
- L: 90,
2559
+ F: __dxlog_file12,
2560
+ L: 104,
2179
2561
  S: this,
2180
2562
  C: (f, a) => f(...a)
2181
2563
  });
@@ -2184,12 +2566,19 @@ var WebRTCTransport = class {
2184
2566
  if (this._closed) {
2185
2567
  return;
2186
2568
  }
2187
- invariant11(signal.payload.data, "Signal message must contain signal data.");
2569
+ invariant11(signal.payload.data, "Signal message must contain signal data.", {
2570
+ F: __dxlog_file12,
2571
+ L: 112,
2572
+ S: this,
2573
+ A: [
2574
+ "signal.payload.data",
2575
+ "'Signal message must contain signal data.'"
2576
+ ]
2577
+ });
2188
2578
  this._peer.signal(signal.payload.data);
2189
2579
  }
2190
2580
  async _disconnectStreams() {
2191
- var _a, _b, _c, _d;
2192
- (_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);
2581
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2193
2582
  }
2194
2583
  };
2195
2584
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2201,13 +2590,13 @@ var createWebRTCTransportFactory = (webrtcConfig) => ({
2201
2590
 
2202
2591
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts
2203
2592
  import { Duplex } from "@dxos/node-std/stream";
2204
- import invariant12 from "tiny-invariant";
2205
2593
  import { Stream } from "@dxos/codec-protobuf";
2594
+ import { invariant as invariant12 } from "@dxos/invariant";
2206
2595
  import { PublicKey as PublicKey11 } from "@dxos/keys";
2207
2596
  import { log as log12 } from "@dxos/log";
2208
2597
  import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2209
2598
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
2210
- var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2599
+ var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-service.ts";
2211
2600
  var WebRTCTransportService = class {
2212
2601
  // prettier-ignore
2213
2602
  constructor(_webrtcConfig) {
@@ -2218,6 +2607,13 @@ var WebRTCTransportService = class {
2218
2607
  const rpcStream = new Stream(({ ready, next, close }) => {
2219
2608
  const duplex = new Duplex({
2220
2609
  read: () => {
2610
+ const callbacks = [
2611
+ ...transportState.writeCallbacks
2612
+ ];
2613
+ transportState.writeCallbacks.length = 0;
2614
+ for (const cb of callbacks) {
2615
+ cb();
2616
+ }
2221
2617
  },
2222
2618
  write: function(chunk, _, callback) {
2223
2619
  next({
@@ -2269,30 +2665,53 @@ var WebRTCTransportService = class {
2269
2665
  });
2270
2666
  close();
2271
2667
  });
2272
- ready();
2273
- this.transports.set(request.proxyId, {
2668
+ const transportState = {
2274
2669
  transport,
2275
- stream: duplex
2276
- });
2670
+ stream: duplex,
2671
+ writeCallbacks: []
2672
+ };
2673
+ ready();
2674
+ this.transports.set(request.proxyId, transportState);
2277
2675
  });
2278
2676
  return rpcStream;
2279
2677
  }
2280
2678
  async sendSignal({ proxyId, signal }) {
2281
- invariant12(this.transports.has(proxyId));
2679
+ invariant12(this.transports.has(proxyId), void 0, {
2680
+ F: __dxlog_file13,
2681
+ L: 113,
2682
+ S: this,
2683
+ A: [
2684
+ "this.transports.has(proxyId)",
2685
+ ""
2686
+ ]
2687
+ });
2282
2688
  await this.transports.get(proxyId).transport.signal(signal);
2283
2689
  }
2284
2690
  async sendData({ proxyId, payload }) {
2285
- invariant12(this.transports.has(proxyId));
2286
- await this.transports.get(proxyId).stream.push(payload);
2691
+ invariant12(this.transports.has(proxyId), void 0, {
2692
+ F: __dxlog_file13,
2693
+ L: 118,
2694
+ S: this,
2695
+ A: [
2696
+ "this.transports.has(proxyId)",
2697
+ ""
2698
+ ]
2699
+ });
2700
+ const state = this.transports.get(proxyId);
2701
+ const bufferHasSpace = state.stream.push(payload);
2702
+ if (!bufferHasSpace) {
2703
+ await new Promise((resolve) => {
2704
+ state.writeCallbacks.push(resolve);
2705
+ });
2706
+ }
2287
2707
  }
2288
2708
  async close({ proxyId }) {
2289
- var _a, _b;
2290
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2291
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2709
+ await this.transports.get(proxyId)?.transport.destroy();
2710
+ await this.transports.get(proxyId)?.stream.end();
2292
2711
  this.transports.delete(proxyId);
2293
2712
  log12("Closed.", void 0, {
2294
- F: __dxlog_file12,
2295
- L: 109,
2713
+ F: __dxlog_file13,
2714
+ L: 132,
2296
2715
  S: this,
2297
2716
  C: (f, a) => f(...a)
2298
2717
  });
@@ -2300,15 +2719,16 @@ var WebRTCTransportService = class {
2300
2719
  };
2301
2720
 
2302
2721
  // packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts
2303
- import invariant13 from "tiny-invariant";
2722
+ import { Writable } from "@dxos/node-std/stream";
2304
2723
  import { Event as Event8 } from "@dxos/async";
2305
2724
  import { Context as Context6 } from "@dxos/context";
2306
2725
  import { ErrorStream as ErrorStream5 } from "@dxos/debug";
2726
+ import { invariant as invariant13 } from "@dxos/invariant";
2307
2727
  import { PublicKey as PublicKey12 } from "@dxos/keys";
2308
2728
  import { log as log13 } from "@dxos/log";
2309
2729
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2310
2730
  import { arrayToBuffer } from "@dxos/util";
2311
- var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2731
+ var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport-proxy.ts";
2312
2732
  var WebRTCTransportProxy = class {
2313
2733
  // prettier-ignore
2314
2734
  constructor(_params) {
@@ -2316,9 +2736,9 @@ var WebRTCTransportProxy = class {
2316
2736
  this._proxyId = PublicKey12.random();
2317
2737
  this._ctx = new Context6();
2318
2738
  this.closed = new Event8();
2319
- this._closed = false;
2320
2739
  this.connected = new Event8();
2321
2740
  this.errors = new ErrorStream5();
2741
+ this._closed = false;
2322
2742
  this._serviceStream = this._params.bridgeService.open({
2323
2743
  proxyId: this._proxyId,
2324
2744
  initiator: this._params.initiator
@@ -2326,8 +2746,8 @@ var WebRTCTransportProxy = class {
2326
2746
  this._serviceStream.waitUntilReady().then(() => {
2327
2747
  this._serviceStream.subscribe(async (event) => {
2328
2748
  log13("WebRTCTransportProxy: event", event, {
2329
- F: __dxlog_file13,
2330
- L: 50,
2749
+ F: __dxlog_file14,
2750
+ L: 51,
2331
2751
  S: this,
2332
2752
  C: (f, a) => f(...a)
2333
2753
  });
@@ -2339,28 +2759,24 @@ var WebRTCTransportProxy = class {
2339
2759
  await this._handleSignal(event.signal);
2340
2760
  }
2341
2761
  });
2342
- const dataListener = async (data) => {
2343
- try {
2344
- await this._params.bridgeService.sendData({
2762
+ this._params.stream.pipe(new Writable({
2763
+ write: (chunk, _, callback) => {
2764
+ this._params.bridgeService.sendData({
2345
2765
  proxyId: this._proxyId,
2346
- payload: data
2347
- });
2348
- } catch (err) {
2349
- log13.catch(err, void 0, {
2350
- F: __dxlog_file13,
2351
- L: 67,
2352
- S: this,
2353
- C: (f, a) => f(...a)
2766
+ payload: chunk
2767
+ }).then(() => callback(), (err) => {
2768
+ log13.catch(err, void 0, {
2769
+ F: __dxlog_file14,
2770
+ L: 69,
2771
+ S: this,
2772
+ C: (f, a) => f(...a)
2773
+ });
2354
2774
  });
2355
2775
  }
2356
- };
2357
- this._params.stream.on("data", dataListener);
2358
- this._ctx.onDispose(() => {
2359
- this._params.stream.off("data", dataListener);
2360
- });
2776
+ }));
2361
2777
  }, (error) => log13.catch(error, void 0, {
2362
- F: __dxlog_file13,
2363
- L: 73,
2778
+ F: __dxlog_file14,
2779
+ L: 75,
2364
2780
  S: this,
2365
2781
  C: (f, a) => f(...a)
2366
2782
  }));
@@ -2398,15 +2814,15 @@ var WebRTCTransportProxy = class {
2398
2814
  if (this._closed) {
2399
2815
  return;
2400
2816
  }
2401
- this._serviceStream.close();
2817
+ await this._serviceStream.close();
2402
2818
  try {
2403
2819
  await this._params.bridgeService.close({
2404
2820
  proxyId: this._proxyId
2405
2821
  });
2406
2822
  } catch (err) {
2407
2823
  log13.catch(err, void 0, {
2408
- F: __dxlog_file13,
2409
- L: 124,
2824
+ F: __dxlog_file14,
2825
+ L: 126,
2410
2826
  S: this,
2411
2827
  C: (f, a) => f(...a)
2412
2828
  });
@@ -2419,7 +2835,7 @@ var WebRTCTransportProxy = class {
2419
2835
  */
2420
2836
  // TODO(burdon): Option on close method.
2421
2837
  forceClose() {
2422
- this._serviceStream.close();
2838
+ void this._serviceStream.close();
2423
2839
  this.closed.emit();
2424
2840
  this._closed = true;
2425
2841
  }
@@ -2440,7 +2856,15 @@ var WebRTCTransportProxyFactory = class {
2440
2856
  return this;
2441
2857
  }
2442
2858
  createTransport(options) {
2443
- invariant13(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections");
2859
+ invariant13(this._bridgeService, "WebRTCTransportProxyFactory is not ready to open connections", {
2860
+ F: __dxlog_file14,
2861
+ L: 163,
2862
+ S: this,
2863
+ A: [
2864
+ "this._bridgeService",
2865
+ "'WebRTCTransportProxyFactory is not ready to open connections'"
2866
+ ]
2867
+ });
2444
2868
  const transport = new WebRTCTransportProxy({
2445
2869
  ...options,
2446
2870
  bridgeService: this._bridgeService
@@ -2492,4 +2916,4 @@ export {
2492
2916
  WebRTCTransportProxyFactory,
2493
2917
  createTeleportProtocolFactory
2494
2918
  };
2495
- //# sourceMappingURL=chunk-5R7TDXUU.mjs.map
2919
+ //# sourceMappingURL=chunk-YADPADXG.mjs.map