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