@dxos/network-manager 0.1.58-main.353d0d1 → 0.1.58-main.385b5e4

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-VP6DWC3F.mjs → chunk-JIGWYM5K.mjs} +228 -123
  2. package/dist/lib/browser/chunk-JIGWYM5K.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 +146 -13
  6. package/dist/lib/browser/testing/index.mjs.map +4 -4
  7. package/dist/lib/node/index.cjs +516 -411
  8. package/dist/lib/node/index.cjs.map +4 -4
  9. package/dist/lib/node/meta.json +1 -1
  10. package/dist/lib/node/testing/index.cjs +1108 -869
  11. package/dist/lib/node/testing/index.cjs.map +4 -4
  12. package/dist/types/src/swarm/connection-limiter.d.ts +1 -1
  13. package/dist/types/src/swarm/connection-limiter.d.ts.map +1 -1
  14. package/dist/types/src/swarm/connection.d.ts +3 -1
  15. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  16. package/dist/types/src/swarm/peer.d.ts +3 -3
  17. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  18. package/dist/types/src/swarm/swarm-mapper.d.ts.map +1 -1
  19. package/dist/types/src/swarm/swarm.d.ts +2 -2
  20. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  21. package/dist/types/src/testing/test-builder.d.ts +1 -1
  22. package/dist/types/src/testing/test-builder.d.ts.map +1 -1
  23. package/dist/types/src/tests/basic-test-suite.d.ts.map +1 -1
  24. package/dist/types/src/tests/tcp-transport.test.d.ts +2 -0
  25. package/dist/types/src/tests/tcp-transport.test.d.ts.map +1 -0
  26. package/dist/types/src/topology/star-topology.d.ts.map +1 -1
  27. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  28. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  29. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  30. package/dist/types/src/transport/tcp-transport.d.ts +23 -0
  31. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -0
  32. package/dist/types/src/transport/transport.d.ts +2 -1
  33. package/dist/types/src/transport/transport.d.ts.map +1 -1
  34. package/package.json +17 -18
  35. package/src/swarm/connection-limiter.ts +2 -2
  36. package/src/swarm/connection.test.ts +1 -1
  37. package/src/swarm/connection.ts +53 -27
  38. package/src/swarm/peer.ts +10 -3
  39. package/src/swarm/swarm-mapper.ts +4 -7
  40. package/src/swarm/swarm.test.ts +3 -3
  41. package/src/swarm/swarm.ts +3 -3
  42. package/src/testing/test-builder.ts +6 -2
  43. package/src/tests/basic-test-suite.ts +1 -1
  44. package/src/tests/memory-transport.test.ts +2 -2
  45. package/src/tests/tcp-transport.test.ts +67 -0
  46. package/src/tests/webrtc-transport.test.ts +1 -1
  47. package/src/topology/star-topology.ts +1 -4
  48. package/src/transport/simplepeer-transport-proxy.ts +48 -21
  49. package/src/transport/simplepeer-transport-service.ts +1 -4
  50. package/src/transport/simplepeer-transport.ts +38 -4
  51. package/src/transport/tcp-transport.ts +107 -0
  52. package/src/transport/transport.ts +1 -0
  53. package/dist/lib/browser/chunk-VP6DWC3F.mjs.map +0 -7
@@ -7,11 +7,10 @@ import {
7
7
  import { DeferredTask, Event, sleep, synchronized } from "@dxos/async";
8
8
  import { Context, cancelWithContext } from "@dxos/context";
9
9
  import { ErrorStream } from "@dxos/debug";
10
- import { CancelledError } from "@dxos/errors";
11
10
  import { invariant } from "@dxos/invariant";
12
11
  import { PublicKey } from "@dxos/keys";
13
12
  import { log } from "@dxos/log";
14
- import { trace } from "@dxos/protocols";
13
+ import { CancelledError, ProtocolError, ConnectionResetError, ConnectivityError, UnknownProtocolError, trace } from "@dxos/protocols";
15
14
  function _ts_decorate(decorators, target, key, desc) {
16
15
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
17
16
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -64,6 +63,8 @@ var ConnectionState;
64
63
  */
65
64
  "CLOSED"
66
65
  ] = "CLOSED";
66
+ ConnectionState5["ABORTING"] = "ABORTING";
67
+ ConnectionState5["ABORTED"] = "ABORTED";
67
68
  })(ConnectionState || (ConnectionState = {}));
68
69
  var Connection = class {
69
70
  constructor(topic, ownId, remoteId, sessionId, initiator, _signalMessaging, _protocol, _transportFactory, _callbacks) {
@@ -95,7 +96,7 @@ var Connection = class {
95
96
  initiator: this.initiator
96
97
  }, {
97
98
  F: __dxlog_file,
98
- L: 114,
99
+ L: 123,
99
100
  S: this,
100
101
  C: (f, a) => f(...a)
101
102
  });
@@ -115,7 +116,7 @@ var Connection = class {
115
116
  async openConnection() {
116
117
  invariant(this._state === ConnectionState.INITIAL, "Invalid state.", {
117
118
  F: __dxlog_file,
118
- L: 139,
119
+ L: 148,
119
120
  S: this,
120
121
  A: [
121
122
  "this._state === ConnectionState.INITIAL",
@@ -126,7 +127,7 @@ var Connection = class {
126
127
  id: this._instanceId
127
128
  }), {
128
129
  F: __dxlog_file,
129
- L: 140,
130
+ L: 149,
130
131
  S: this,
131
132
  C: (f, a) => f(...a)
132
133
  });
@@ -138,7 +139,7 @@ var Connection = class {
138
139
  initiator: this.initiator
139
140
  }, {
140
141
  F: __dxlog_file,
141
- L: 141,
142
+ L: 150,
142
143
  S: this,
143
144
  C: (f, a) => f(...a)
144
145
  });
@@ -149,15 +150,15 @@ var Connection = class {
149
150
  this._protocol.stream.on("close", () => {
150
151
  log("protocol stream closed", void 0, {
151
152
  F: __dxlog_file,
152
- L: 158,
153
+ L: 167,
153
154
  S: this,
154
155
  C: (f, a) => f(...a)
155
156
  });
156
- this.close().catch((err) => this.errors.raise(err));
157
+ this.close(new ProtocolError("protocol stream closed")).catch((err) => this.errors.raise(err));
157
158
  });
158
159
  invariant(!this._transport, void 0, {
159
160
  F: __dxlog_file,
160
- L: 162,
161
+ L: 171,
161
162
  S: this,
162
163
  A: [
163
164
  "!this._transport",
@@ -177,7 +178,7 @@ var Connection = class {
177
178
  this._transport = void 0;
178
179
  log("abort triggered by transport close", void 0, {
179
180
  F: __dxlog_file,
180
- L: 176,
181
+ L: 185,
181
182
  S: this,
182
183
  C: (f, a) => f(...a)
183
184
  });
@@ -188,21 +189,38 @@ var Connection = class {
188
189
  err
189
190
  }, {
190
191
  F: __dxlog_file,
191
- L: 181,
192
+ L: 190,
192
193
  S: this,
193
194
  C: (f, a) => f(...a)
194
195
  });
195
196
  if (!this.closeReason) {
196
197
  this.closeReason = err?.message;
197
198
  }
198
- if (err?.message.includes("OperationError")) {
199
- log("aborting due to transport OperationError", void 0, {
199
+ if (err instanceof ConnectionResetError) {
200
+ log("aborting due to transport ConnectionResetError", void 0, {
201
+ F: __dxlog_file,
202
+ L: 197,
203
+ S: this,
204
+ C: (f, a) => f(...a)
205
+ });
206
+ this.abort().catch((err2) => this.errors.raise(err2));
207
+ } else if (err instanceof ConnectivityError) {
208
+ log("aborting due to transport ConnectivityError", void 0, {
200
209
  F: __dxlog_file,
201
- L: 187,
210
+ L: 200,
202
211
  S: this,
203
212
  C: (f, a) => f(...a)
204
213
  });
205
214
  this.abort().catch((err2) => this.errors.raise(err2));
215
+ } else if (err instanceof UnknownProtocolError) {
216
+ log("unsure what to do for UnknownProtocolError", {
217
+ err
218
+ }, {
219
+ F: __dxlog_file,
220
+ L: 203,
221
+ S: this,
222
+ C: (f, a) => f(...a)
223
+ });
206
224
  }
207
225
  if (this._state !== ConnectionState.CLOSED && this._state !== ConnectionState.CLOSING) {
208
226
  this.errors.raise(err);
@@ -216,7 +234,7 @@ var Connection = class {
216
234
  id: this._instanceId
217
235
  }), {
218
236
  F: __dxlog_file,
219
- L: 203,
237
+ L: 218,
220
238
  S: this,
221
239
  C: (f, a) => f(...a)
222
240
  });
@@ -226,30 +244,30 @@ var Connection = class {
226
244
  err
227
245
  }, {
228
246
  F: __dxlog_file,
229
- L: 209,
247
+ L: 224,
230
248
  S: this,
231
249
  C: (f, a) => f(...a)
232
250
  });
233
- if (!this.closeReason) {
234
- this.closeReason = err?.message;
235
- }
236
- if (this._state === ConnectionState.CLOSED) {
237
- log("abort ignored: already closed", this.closeReason, {
251
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTED) {
252
+ log(`abort ignored: already ${this._state}`, this.closeReason, {
238
253
  F: __dxlog_file,
239
- L: 214,
254
+ L: 226,
240
255
  S: this,
241
256
  C: (f, a) => f(...a)
242
257
  });
243
258
  return;
244
259
  }
245
- this._changeState(ConnectionState.CLOSING);
260
+ this._changeState(ConnectionState.ABORTING);
261
+ if (!this.closeReason) {
262
+ this.closeReason = err?.message;
263
+ }
246
264
  await this._ctx.dispose();
247
265
  try {
248
266
  log("aborting protocol... ", {
249
267
  proto: this._protocol
250
268
  }, {
251
269
  F: __dxlog_file,
252
- L: 224,
270
+ L: 239,
253
271
  S: this,
254
272
  C: (f, a) => f(...a)
255
273
  });
@@ -257,7 +275,7 @@ var Connection = class {
257
275
  } catch (err2) {
258
276
  log.catch(err2, void 0, {
259
277
  F: __dxlog_file,
260
- L: 227,
278
+ L: 242,
261
279
  S: this,
262
280
  C: (f, a) => f(...a)
263
281
  });
@@ -267,56 +285,68 @@ var Connection = class {
267
285
  } catch (err2) {
268
286
  log.catch(err2, void 0, {
269
287
  F: __dxlog_file,
270
- L: 234,
288
+ L: 249,
271
289
  S: this,
272
290
  C: (f, a) => f(...a)
273
291
  });
274
292
  }
275
- this._changeState(ConnectionState.CLOSED);
276
- this._callbacks?.onClosed?.(err);
293
+ try {
294
+ this._callbacks?.onClosed?.(err);
295
+ } catch (err2) {
296
+ log.catch(err2, void 0, {
297
+ F: __dxlog_file,
298
+ L: 255,
299
+ S: this,
300
+ C: (f, a) => f(...a)
301
+ });
302
+ }
303
+ this._changeState(ConnectionState.ABORTED);
277
304
  }
278
305
  async close(err) {
279
306
  if (!this.closeReason) {
280
307
  this.closeReason = err?.message;
281
308
  }
282
- if (this._state === ConnectionState.CLOSED) {
309
+ if (this._state === ConnectionState.CLOSED || this._state === ConnectionState.ABORTING || this._state === ConnectionState.ABORTED) {
283
310
  return;
284
311
  }
312
+ const lastState = this._state;
285
313
  this._changeState(ConnectionState.CLOSING);
286
314
  await this._ctx.dispose();
287
315
  log("closing...", {
288
316
  peerId: this.ownId
289
317
  }, {
290
318
  F: __dxlog_file,
291
- L: 253,
319
+ L: 277,
292
320
  S: this,
293
321
  C: (f, a) => f(...a)
294
322
  });
295
- try {
296
- await this._protocol.close();
297
- } catch (err2) {
298
- log.catch(err2, void 0, {
299
- F: __dxlog_file,
300
- L: 259,
301
- S: this,
302
- C: (f, a) => f(...a)
303
- });
304
- }
305
- try {
306
- await this._transport?.destroy();
307
- } catch (err2) {
308
- log.catch(err2, void 0, {
309
- F: __dxlog_file,
310
- L: 266,
311
- S: this,
312
- C: (f, a) => f(...a)
313
- });
323
+ if (lastState === ConnectionState.CONNECTED) {
324
+ try {
325
+ await this._protocol.close();
326
+ } catch (err2) {
327
+ log.catch(err2, void 0, {
328
+ F: __dxlog_file,
329
+ L: 284,
330
+ S: this,
331
+ C: (f, a) => f(...a)
332
+ });
333
+ }
334
+ try {
335
+ await this._transport?.destroy();
336
+ } catch (err2) {
337
+ log.catch(err2, void 0, {
338
+ F: __dxlog_file,
339
+ L: 291,
340
+ S: this,
341
+ C: (f, a) => f(...a)
342
+ });
343
+ }
314
344
  }
315
345
  log("closed", {
316
346
  peerId: this.ownId
317
347
  }, {
318
348
  F: __dxlog_file,
319
- L: 269,
349
+ L: 295,
320
350
  S: this,
321
351
  C: (f, a) => f(...a)
322
352
  });
@@ -359,7 +389,7 @@ var Connection = class {
359
389
  err
360
390
  }, {
361
391
  F: __dxlog_file,
362
- L: 306,
392
+ L: 332,
363
393
  S: this,
364
394
  C: (f, a) => f(...a)
365
395
  });
@@ -372,7 +402,7 @@ var Connection = class {
372
402
  async signal(msg) {
373
403
  invariant(msg.sessionId, void 0, {
374
404
  F: __dxlog_file,
375
- L: 315,
405
+ L: 341,
376
406
  S: this,
377
407
  A: [
378
408
  "msg.sessionId",
@@ -382,7 +412,7 @@ var Connection = class {
382
412
  if (!msg.sessionId.equals(this.sessionId)) {
383
413
  log("dropping signal for incorrect session id", void 0, {
384
414
  F: __dxlog_file,
385
- L: 317,
415
+ L: 343,
386
416
  S: this,
387
417
  C: (f, a) => f(...a)
388
418
  });
@@ -390,7 +420,7 @@ var Connection = class {
390
420
  }
391
421
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
392
422
  F: __dxlog_file,
393
- L: 320,
423
+ L: 346,
394
424
  S: this,
395
425
  A: [
396
426
  "msg.data.signal || msg.data.signalBatch",
@@ -399,7 +429,7 @@ var Connection = class {
399
429
  });
400
430
  invariant(msg.author?.equals(this.remoteId), void 0, {
401
431
  F: __dxlog_file,
402
- L: 321,
432
+ L: 347,
403
433
  S: this,
404
434
  A: [
405
435
  "msg.author?.equals(this.remoteId)",
@@ -408,7 +438,7 @@ var Connection = class {
408
438
  });
409
439
  invariant(msg.recipient?.equals(this.ownId), void 0, {
410
440
  F: __dxlog_file,
411
- L: 322,
441
+ L: 348,
412
442
  S: this,
413
443
  A: [
414
444
  "msg.recipient?.equals(this.ownId)",
@@ -432,7 +462,7 @@ var Connection = class {
432
462
  msg: msg.data
433
463
  }, {
434
464
  F: __dxlog_file,
435
- L: 331,
465
+ L: 357,
436
466
  S: this,
437
467
  C: (f, a) => f(...a)
438
468
  });
@@ -440,7 +470,7 @@ var Connection = class {
440
470
  } else {
441
471
  invariant(this._transport, "Connection not ready to accept signals.", {
442
472
  F: __dxlog_file,
443
- L: 334,
473
+ L: 360,
444
474
  S: this,
445
475
  A: [
446
476
  "this._transport",
@@ -453,7 +483,7 @@ var Connection = class {
453
483
  msg: msg.data
454
484
  }, {
455
485
  F: __dxlog_file,
456
- L: 335,
486
+ L: 361,
457
487
  S: this,
458
488
  C: (f, a) => f(...a)
459
489
  });
@@ -467,17 +497,17 @@ var Connection = class {
467
497
  _changeState(state) {
468
498
  log("stateChanged", {
469
499
  from: this._state,
470
- too: state,
500
+ to: state,
471
501
  peerId: this.ownId
472
502
  }, {
473
503
  F: __dxlog_file,
474
- L: 346,
504
+ L: 372,
475
505
  S: this,
476
506
  C: (f, a) => f(...a)
477
507
  });
478
508
  invariant(state !== this._state, "Already in this state.", {
479
509
  F: __dxlog_file,
480
- L: 347,
510
+ L: 373,
481
511
  S: this,
482
512
  A: [
483
513
  "state !== this._state",
@@ -740,10 +770,10 @@ import { ComplexMap as ComplexMap2, isNotNullOrUndefined } from "@dxos/util";
740
770
  // packages/core/mesh/network-manager/src/swarm/peer.ts
741
771
  import { Event as Event2, scheduleTask, synchronized as synchronized2 } from "@dxos/async";
742
772
  import { Context as Context3 } from "@dxos/context";
743
- import { CancelledError as CancelledError2, SystemError } from "@dxos/errors";
744
773
  import { invariant as invariant3 } from "@dxos/invariant";
745
774
  import { PublicKey as PublicKey3 } from "@dxos/keys";
746
775
  import { log as log3 } from "@dxos/log";
776
+ import { CancelledError as CancelledError2, SystemError, TimeoutError } from "@dxos/protocols";
747
777
  function _ts_decorate2(decorators, target, key, desc) {
748
778
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
749
779
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -784,6 +814,20 @@ var Peer = class {
784
814
  */
785
815
  async onOffer(message) {
786
816
  const remoteId = message.author;
817
+ if (this.connection && ![
818
+ ConnectionState.CREATED,
819
+ ConnectionState.INITIAL
820
+ ].includes(this.connection.state)) {
821
+ log3.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
822
+ F: __dxlog_file3,
823
+ L: 112,
824
+ S: this,
825
+ C: (f, a) => f(...a)
826
+ });
827
+ return {
828
+ accept: false
829
+ };
830
+ }
787
831
  if (this.connection || this.initiating) {
788
832
  if (remoteId.toHex() < this.localPeerId.toHex()) {
789
833
  log3("close local connection", {
@@ -793,7 +837,7 @@ var Peer = class {
793
837
  sessionId: this.connection?.sessionId
794
838
  }, {
795
839
  F: __dxlog_file3,
796
- L: 117,
840
+ L: 121,
797
841
  S: this,
798
842
  C: (f, a) => f(...a)
799
843
  });
@@ -810,7 +854,7 @@ var Peer = class {
810
854
  if (!this.connection) {
811
855
  invariant3(message.sessionId, void 0, {
812
856
  F: __dxlog_file3,
813
- L: 137,
857
+ L: 141,
814
858
  S: this,
815
859
  A: [
816
860
  "message.sessionId",
@@ -831,7 +875,7 @@ var Peer = class {
831
875
  err
832
876
  }, {
833
877
  F: __dxlog_file3,
834
- L: 147,
878
+ L: 151,
835
879
  S: this,
836
880
  C: (f, a) => f(...a)
837
881
  });
@@ -853,7 +897,7 @@ var Peer = class {
853
897
  async initiateConnection() {
854
898
  invariant3(!this.initiating, "Initiation in progress.", {
855
899
  F: __dxlog_file3,
856
- L: 164,
900
+ L: 168,
857
901
  S: this,
858
902
  A: [
859
903
  "!this.initiating",
@@ -862,7 +906,7 @@ var Peer = class {
862
906
  });
863
907
  invariant3(!this.connection, "Already connected.", {
864
908
  F: __dxlog_file3,
865
- L: 165,
909
+ L: 169,
866
910
  S: this,
867
911
  A: [
868
912
  "!this.connection",
@@ -877,7 +921,7 @@ var Peer = class {
877
921
  sessionId
878
922
  }, {
879
923
  F: __dxlog_file3,
880
- L: 167,
924
+ L: 171,
881
925
  S: this,
882
926
  C: (f, a) => f(...a)
883
927
  });
@@ -902,14 +946,14 @@ var Peer = class {
902
946
  remoteId: this.id
903
947
  }, {
904
948
  F: __dxlog_file3,
905
- L: 183,
949
+ L: 187,
906
950
  S: this,
907
951
  C: (f, a) => f(...a)
908
952
  });
909
953
  if (connection.state !== ConnectionState.INITIAL) {
910
954
  log3("ignoring response", void 0, {
911
955
  F: __dxlog_file3,
912
- L: 185,
956
+ L: 189,
913
957
  S: this,
914
958
  C: (f, a) => f(...a)
915
959
  });
@@ -929,10 +973,13 @@ var Peer = class {
929
973
  remoteId: this.id
930
974
  }, {
931
975
  F: __dxlog_file3,
932
- L: 196,
976
+ L: 200,
933
977
  S: this,
934
978
  C: (f, a) => f(...a)
935
979
  });
980
+ if (err instanceof TimeoutError) {
981
+ await connection.abort(err);
982
+ }
936
983
  await this.closeConnection(err);
937
984
  throw err;
938
985
  } finally {
@@ -952,13 +999,13 @@ var Peer = class {
952
999
  sessionId
953
1000
  }, {
954
1001
  F: __dxlog_file3,
955
- L: 210,
1002
+ L: 217,
956
1003
  S: this,
957
1004
  C: (f, a) => f(...a)
958
1005
  });
959
1006
  invariant3(!this.connection, "Already connected.", {
960
1007
  F: __dxlog_file3,
961
- L: 217,
1008
+ L: 224,
962
1009
  S: this,
963
1010
  A: [
964
1011
  "!this.connection",
@@ -994,7 +1041,7 @@ var Peer = class {
994
1041
  initiator
995
1042
  }, {
996
1043
  F: __dxlog_file3,
997
- L: 236,
1044
+ L: 243,
998
1045
  S: this,
999
1046
  C: (f, a) => f(...a)
1000
1047
  });
@@ -1007,14 +1054,14 @@ var Peer = class {
1007
1054
  initiator
1008
1055
  }, {
1009
1056
  F: __dxlog_file3,
1010
- L: 245,
1057
+ L: 252,
1011
1058
  S: this,
1012
1059
  C: (f, a) => f(...a)
1013
1060
  });
1014
1061
  this._connectionLimiter.doneConnecting(sessionId);
1015
1062
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
1016
1063
  F: __dxlog_file3,
1017
- L: 250,
1064
+ L: 257,
1018
1065
  S: this,
1019
1066
  A: [
1020
1067
  "this.connection === connection",
@@ -1029,7 +1076,7 @@ var Peer = class {
1029
1076
  initiator
1030
1077
  }, {
1031
1078
  F: __dxlog_file3,
1032
- L: 252,
1079
+ L: 259,
1033
1080
  S: this,
1034
1081
  C: (f, a) => f(...a)
1035
1082
  });
@@ -1064,7 +1111,7 @@ var Peer = class {
1064
1111
  err
1065
1112
  }, {
1066
1113
  F: __dxlog_file3,
1067
- L: 292,
1114
+ L: 299,
1068
1115
  S: this,
1069
1116
  C: (f, a) => f(...a)
1070
1117
  });
@@ -1077,7 +1124,7 @@ var Peer = class {
1077
1124
  err
1078
1125
  }, {
1079
1126
  F: __dxlog_file3,
1080
- L: 293,
1127
+ L: 300,
1081
1128
  S: this,
1082
1129
  C: (f, a) => f(...a)
1083
1130
  });
@@ -1096,7 +1143,7 @@ var Peer = class {
1096
1143
  sessionId: connection.sessionId
1097
1144
  }, {
1098
1145
  F: __dxlog_file3,
1099
- L: 318,
1146
+ L: 325,
1100
1147
  S: this,
1101
1148
  C: (f, a) => f(...a)
1102
1149
  });
@@ -1106,7 +1153,7 @@ var Peer = class {
1106
1153
  sessionId: connection.sessionId
1107
1154
  }, {
1108
1155
  F: __dxlog_file3,
1109
- L: 324,
1156
+ L: 331,
1110
1157
  S: this,
1111
1158
  C: (f, a) => f(...a)
1112
1159
  });
@@ -1117,7 +1164,7 @@ var Peer = class {
1117
1164
  message
1118
1165
  }, {
1119
1166
  F: __dxlog_file3,
1120
- L: 329,
1167
+ L: 336,
1121
1168
  S: this,
1122
1169
  C: (f, a) => f(...a)
1123
1170
  });
@@ -1132,7 +1179,7 @@ var Peer = class {
1132
1179
  topic: this.topic
1133
1180
  }, {
1134
1181
  F: __dxlog_file3,
1135
- L: 339,
1182
+ L: 346,
1136
1183
  S: this,
1137
1184
  C: (f, a) => f(...a)
1138
1185
  });
@@ -1645,7 +1692,6 @@ var SwarmMapper = class {
1645
1692
  get peers() {
1646
1693
  return Array.from(this._peers.values());
1647
1694
  }
1648
- // prettier-ignore
1649
1695
  constructor(_swarm) {
1650
1696
  this._swarm = _swarm;
1651
1697
  this._subscriptions = new EventSubscriptions();
@@ -1668,7 +1714,7 @@ var SwarmMapper = class {
1668
1714
  _update() {
1669
1715
  log5("updating swarm", void 0, {
1670
1716
  F: __dxlog_file5,
1671
- L: 75,
1717
+ L: 72,
1672
1718
  S: this,
1673
1719
  C: (f, a) => f(...a)
1674
1720
  });
@@ -1692,7 +1738,7 @@ var SwarmMapper = class {
1692
1738
  totalPeersInSwarm: this._peers.size
1693
1739
  }, {
1694
1740
  F: __dxlog_file5,
1695
- L: 116,
1741
+ L: 113,
1696
1742
  S: this,
1697
1743
  C: (f, a) => f(...a)
1698
1744
  });
@@ -1708,13 +1754,13 @@ var SwarmMapper = class {
1708
1754
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
1709
1755
  import { DeferredTask as DeferredTask2 } from "@dxos/async";
1710
1756
  import { Context as Context5 } from "@dxos/context";
1711
- import { CancelledError as CancelledError3 } from "@dxos/errors";
1712
1757
  import { invariant as invariant5 } from "@dxos/invariant";
1713
1758
  import { PublicKey as PublicKey6 } from "@dxos/keys";
1714
1759
  import { log as log6 } from "@dxos/log";
1760
+ import { CancelledError as CancelledError3 } from "@dxos/protocols";
1715
1761
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
1716
1762
  var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/swarm/connection-limiter.ts";
1717
- var MAX_CONCURRENT_INITIATING_CONNECTIONS = 10;
1763
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
1718
1764
  var ConnectionLimiter = class {
1719
1765
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1720
1766
  this._ctx = new Context5();
@@ -2256,7 +2302,6 @@ import { invariant as invariant9 } from "@dxos/invariant";
2256
2302
  import { log as log9 } from "@dxos/log";
2257
2303
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2258
2304
  var StarTopology = class {
2259
- // prettier-ignore
2260
2305
  constructor(_centralPeer) {
2261
2306
  this._centralPeer = _centralPeer;
2262
2307
  }
@@ -2266,7 +2311,7 @@ var StarTopology = class {
2266
2311
  init(controller) {
2267
2312
  invariant9(!this._controller, "Already initialized.", {
2268
2313
  F: __dxlog_file10,
2269
- L: 24,
2314
+ L: 21,
2270
2315
  S: this,
2271
2316
  A: [
2272
2317
  "!this._controller",
@@ -2278,7 +2323,7 @@ var StarTopology = class {
2278
2323
  update() {
2279
2324
  invariant9(this._controller, "Not initialized.", {
2280
2325
  F: __dxlog_file10,
2281
- L: 29,
2326
+ L: 26,
2282
2327
  S: this,
2283
2328
  A: [
2284
2329
  "this._controller",
@@ -2289,7 +2334,7 @@ var StarTopology = class {
2289
2334
  if (!ownPeerId.equals(this._centralPeer)) {
2290
2335
  log9("leaf peer dropping all connections apart from central peer.", void 0, {
2291
2336
  F: __dxlog_file10,
2292
- L: 32,
2337
+ L: 29,
2293
2338
  S: this,
2294
2339
  C: (f, a) => f(...a)
2295
2340
  });
@@ -2299,7 +2344,7 @@ var StarTopology = class {
2299
2344
  peer
2300
2345
  }, {
2301
2346
  F: __dxlog_file10,
2302
- L: 37,
2347
+ L: 34,
2303
2348
  S: this,
2304
2349
  C: (f, a) => f(...a)
2305
2350
  });
@@ -2313,7 +2358,7 @@ var StarTopology = class {
2313
2358
  peer
2314
2359
  }, {
2315
2360
  F: __dxlog_file10,
2316
- L: 46,
2361
+ L: 43,
2317
2362
  S: this,
2318
2363
  C: (f, a) => f(...a)
2319
2364
  });
@@ -2324,7 +2369,7 @@ var StarTopology = class {
2324
2369
  async onOffer(peer) {
2325
2370
  invariant9(this._controller, "Not initialized.", {
2326
2371
  F: __dxlog_file10,
2327
- L: 53,
2372
+ L: 50,
2328
2373
  S: this,
2329
2374
  A: [
2330
2375
  "this._controller",
@@ -2338,7 +2383,7 @@ var StarTopology = class {
2338
2383
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2339
2384
  }, {
2340
2385
  F: __dxlog_file10,
2341
- L: 55,
2386
+ L: 52,
2342
2387
  S: this,
2343
2388
  C: (f, a) => f(...a)
2344
2389
  });
@@ -2541,6 +2586,7 @@ var TransportKind;
2541
2586
  TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2542
2587
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2543
2588
  TransportKind2["MEMORY"] = "MEMORY";
2589
+ TransportKind2["TCP"] = "TCP";
2544
2590
  })(TransportKind || (TransportKind = {}));
2545
2591
 
2546
2592
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
@@ -2550,7 +2596,7 @@ import { Event as Event8 } from "@dxos/async";
2550
2596
  import { ErrorStream as ErrorStream4, raise as raise2 } from "@dxos/debug";
2551
2597
  import { PublicKey as PublicKey10 } from "@dxos/keys";
2552
2598
  import { log as log11 } from "@dxos/log";
2553
- import { trace as trace4 } from "@dxos/protocols";
2599
+ import { ConnectionResetError as ConnectionResetError2, ConnectivityError as ConnectivityError2, ProtocolError as ProtocolError2, UnknownProtocolError as UnknownProtocolError2, trace as trace4 } from "@dxos/protocols";
2554
2600
 
2555
2601
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
2556
2602
  var wrtc = null;
@@ -2573,13 +2619,13 @@ var SimplePeerTransport = class {
2573
2619
  id: this._instanceId
2574
2620
  }), {
2575
2621
  F: __dxlog_file12,
2576
- L: 39,
2622
+ L: 40,
2577
2623
  S: this,
2578
2624
  C: (f, a) => f(...a)
2579
2625
  });
2580
2626
  log11("created connection", params, {
2581
2627
  F: __dxlog_file12,
2582
- L: 40,
2628
+ L: 41,
2583
2629
  S: this,
2584
2630
  C: (f, a) => f(...a)
2585
2631
  });
@@ -2592,7 +2638,7 @@ var SimplePeerTransport = class {
2592
2638
  this._peer.on("signal", async (data) => {
2593
2639
  log11("signal", data, {
2594
2640
  F: __dxlog_file12,
2595
- L: 49,
2641
+ L: 50,
2596
2642
  S: this,
2597
2643
  C: (f, a) => f(...a)
2598
2644
  });
@@ -2605,7 +2651,7 @@ var SimplePeerTransport = class {
2605
2651
  this._peer.on("connect", () => {
2606
2652
  log11("connected", void 0, {
2607
2653
  F: __dxlog_file12,
2608
- L: 54,
2654
+ L: 55,
2609
2655
  S: this,
2610
2656
  C: (f, a) => f(...a)
2611
2657
  });
@@ -2615,7 +2661,7 @@ var SimplePeerTransport = class {
2615
2661
  this._peer.on("close", async () => {
2616
2662
  log11("closed", void 0, {
2617
2663
  F: __dxlog_file12,
2618
- L: 60,
2664
+ L: 61,
2619
2665
  S: this,
2620
2666
  C: (f, a) => f(...a)
2621
2667
  });
@@ -2623,22 +2669,66 @@ var SimplePeerTransport = class {
2623
2669
  this.closed.emit();
2624
2670
  });
2625
2671
  this._peer.on("error", async (err) => {
2626
- this.errors.raise(err);
2672
+ if (typeof RTCError !== "undefined" && err instanceof RTCError) {
2673
+ if (err.errorDetail === "sctp-failure") {
2674
+ this.errors.raise(new ConnectionResetError2("sctp-failure from RTCError", err));
2675
+ } else {
2676
+ this.errors.raise(new UnknownProtocolError2("unknown RTCError", err));
2677
+ }
2678
+ } else if ("code" in err) {
2679
+ log11.info("simple-peer error", err, {
2680
+ F: __dxlog_file12,
2681
+ L: 77,
2682
+ S: this,
2683
+ C: (f, a) => f(...a)
2684
+ });
2685
+ switch (err.code) {
2686
+ case "ERR_WEBRTC_SUPPORT":
2687
+ this.errors.raise(new ProtocolError2("WebRTC not supported", err));
2688
+ case "ERR_ICE_CONNECTION_FAILURE":
2689
+ case "ERR_DATA_CHANNEL":
2690
+ case "ERR_CONNECTION_FAILURE":
2691
+ case "ERR_SIGNALING":
2692
+ this.errors.raise(new ConnectivityError2("unknown communication failure", err));
2693
+ case "ERR_CREATE_OFFER":
2694
+ case "ERR_CREATE_ANSWER":
2695
+ case "ERR_SET_LOCAL_DESCRIPTION":
2696
+ case "ERR_SET_REMOTE_DESCRIPTION":
2697
+ case "ERR_ADD_ICE_CANDIDATE":
2698
+ this.errors.raise(new UnknownProtocolError2("unknown simple-peer library failure", err));
2699
+ default:
2700
+ this.errors.raise(new Error("unknown simple-peer error"));
2701
+ }
2702
+ } else {
2703
+ log11.info("unknown peer connection error", err, {
2704
+ F: __dxlog_file12,
2705
+ L: 96,
2706
+ S: this,
2707
+ C: (f, a) => f(...a)
2708
+ });
2709
+ this.errors.raise(err);
2710
+ }
2627
2711
  try {
2628
- if (typeof this._peer?._pc.getStats === "function") {
2712
+ if (typeof this._peer?._pc?.getStats === "function") {
2629
2713
  this._peer._pc.getStats().then((stats) => {
2630
2714
  log11.warn("report after webrtc error", {
2631
2715
  config: this.params.webrtcConfig,
2632
2716
  stats
2633
2717
  }, {
2634
2718
  F: __dxlog_file12,
2635
- L: 72,
2719
+ L: 104,
2636
2720
  S: this,
2637
2721
  C: (f, a) => f(...a)
2638
2722
  });
2639
2723
  });
2640
2724
  }
2641
- } catch {
2725
+ } catch (err2) {
2726
+ log11.catch(err2, void 0, {
2727
+ F: __dxlog_file12,
2728
+ L: 111,
2729
+ S: this,
2730
+ C: (f, a) => f(...a)
2731
+ });
2642
2732
  }
2643
2733
  await this.destroy();
2644
2734
  });
@@ -2646,7 +2736,7 @@ var SimplePeerTransport = class {
2646
2736
  id: this._instanceId
2647
2737
  }), {
2648
2738
  F: __dxlog_file12,
2649
- L: 82,
2739
+ L: 116,
2650
2740
  S: this,
2651
2741
  C: (f, a) => f(...a)
2652
2742
  });
@@ -2654,7 +2744,7 @@ var SimplePeerTransport = class {
2654
2744
  async destroy() {
2655
2745
  log11("closing...", void 0, {
2656
2746
  F: __dxlog_file12,
2657
- L: 86,
2747
+ L: 120,
2658
2748
  S: this,
2659
2749
  C: (f, a) => f(...a)
2660
2750
  });
@@ -2664,7 +2754,7 @@ var SimplePeerTransport = class {
2664
2754
  this.closed.emit();
2665
2755
  log11("closed", void 0, {
2666
2756
  F: __dxlog_file12,
2667
- L: 91,
2757
+ L: 125,
2668
2758
  S: this,
2669
2759
  C: (f, a) => f(...a)
2670
2760
  });
@@ -2697,7 +2787,6 @@ import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/
2697
2787
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
2698
2788
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
2699
2789
  var SimplePeerTransportService = class {
2700
- // prettier-ignore
2701
2790
  constructor(_webrtcConfig) {
2702
2791
  this._webrtcConfig = _webrtcConfig;
2703
2792
  this.transports = new ComplexMap8(PublicKey11.hash);
@@ -2777,7 +2866,7 @@ var SimplePeerTransportService = class {
2777
2866
  async sendSignal({ proxyId, signal }) {
2778
2867
  invariant12(this.transports.has(proxyId), void 0, {
2779
2868
  F: __dxlog_file13,
2780
- L: 113,
2869
+ L: 110,
2781
2870
  S: this,
2782
2871
  A: [
2783
2872
  "this.transports.has(proxyId)",
@@ -2789,7 +2878,7 @@ var SimplePeerTransportService = class {
2789
2878
  async sendData({ proxyId, payload }) {
2790
2879
  invariant12(this.transports.has(proxyId), void 0, {
2791
2880
  F: __dxlog_file13,
2792
- L: 118,
2881
+ L: 115,
2793
2882
  S: this,
2794
2883
  A: [
2795
2884
  "this.transports.has(proxyId)",
@@ -2810,7 +2899,7 @@ var SimplePeerTransportService = class {
2810
2899
  this.transports.delete(proxyId);
2811
2900
  log12("Closed.", void 0, {
2812
2901
  F: __dxlog_file13,
2813
- L: 132,
2902
+ L: 129,
2814
2903
  S: this,
2815
2904
  C: (f, a) => f(...a)
2816
2905
  });
@@ -2825,11 +2914,11 @@ import { ErrorStream as ErrorStream5 } from "@dxos/debug";
2825
2914
  import { invariant as invariant13 } from "@dxos/invariant";
2826
2915
  import { PublicKey as PublicKey12 } from "@dxos/keys";
2827
2916
  import { log as log13 } from "@dxos/log";
2917
+ import { ConnectionResetError as ConnectionResetError3, TimeoutError as TimeoutError2, ProtocolError as ProtocolError3, ConnectivityError as ConnectivityError3, UnknownProtocolError as UnknownProtocolError3 } from "@dxos/protocols";
2828
2918
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2829
2919
  import { arrayToBuffer } from "@dxos/util";
2830
2920
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
2831
2921
  var SimplePeerTransportProxy = class {
2832
- // prettier-ignore
2833
2922
  constructor(_params) {
2834
2923
  this._params = _params;
2835
2924
  this._proxyId = PublicKey12.random();
@@ -2846,7 +2935,7 @@ var SimplePeerTransportProxy = class {
2846
2935
  this._serviceStream.subscribe(async (event) => {
2847
2936
  log13("SimplePeerTransportProxy: event", event, {
2848
2937
  F: __dxlog_file14,
2849
- L: 51,
2938
+ L: 55,
2850
2939
  S: this,
2851
2940
  C: (f, a) => f(...a)
2852
2941
  });
@@ -2866,7 +2955,7 @@ var SimplePeerTransportProxy = class {
2866
2955
  }).then(() => callback(), (err) => {
2867
2956
  log13.catch(err, void 0, {
2868
2957
  F: __dxlog_file14,
2869
- L: 69,
2958
+ L: 76,
2870
2959
  S: this,
2871
2960
  C: (f, a) => f(...a)
2872
2961
  });
@@ -2875,14 +2964,14 @@ var SimplePeerTransportProxy = class {
2875
2964
  }));
2876
2965
  }, (error) => log13.catch(error, void 0, {
2877
2966
  F: __dxlog_file14,
2878
- L: 75,
2967
+ L: 83,
2879
2968
  S: this,
2880
2969
  C: (f, a) => f(...a)
2881
2970
  }));
2882
2971
  }
2883
2972
  async _handleConnection(connectionEvent) {
2884
2973
  if (connectionEvent.error) {
2885
- this.errors.raise(new Error(connectionEvent.error));
2974
+ this.errors.raise(decodeError(connectionEvent.error));
2886
2975
  }
2887
2976
  switch (connectionEvent.state) {
2888
2977
  case ConnectionState4.CONNECTED: {
@@ -2905,7 +2994,7 @@ var SimplePeerTransportProxy = class {
2905
2994
  this._params.bridgeService.sendSignal({
2906
2995
  proxyId: this._proxyId,
2907
2996
  signal
2908
- }).catch((err) => this.errors.raise(err));
2997
+ }).catch((err) => this.errors.raise(decodeError(err)));
2909
2998
  }
2910
2999
  // TODO(burdon): Move open from constructor.
2911
3000
  async destroy() {
@@ -2921,7 +3010,7 @@ var SimplePeerTransportProxy = class {
2921
3010
  } catch (err) {
2922
3011
  log13.catch(err, void 0, {
2923
3012
  F: __dxlog_file14,
2924
- L: 126,
3013
+ L: 134,
2925
3014
  S: this,
2926
3015
  C: (f, a) => f(...a)
2927
3016
  });
@@ -2957,7 +3046,7 @@ var SimplePeerTransportProxyFactory = class {
2957
3046
  createTransport(options) {
2958
3047
  invariant13(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
2959
3048
  F: __dxlog_file14,
2960
- L: 163,
3049
+ L: 171,
2961
3050
  S: this,
2962
3051
  A: [
2963
3052
  "this._bridgeService",
@@ -2973,6 +3062,22 @@ var SimplePeerTransportProxyFactory = class {
2973
3062
  return transport;
2974
3063
  }
2975
3064
  };
3065
+ var decodeError = (err) => {
3066
+ const message = typeof err === "string" ? err : err.message;
3067
+ if (message.includes("CONNECTION_RESET")) {
3068
+ return new ConnectionResetError3(message);
3069
+ } else if (message.includes("TIMEOUT")) {
3070
+ return new TimeoutError2(message);
3071
+ } else if (message.includes("PROTOCOL_ERROR")) {
3072
+ return new ProtocolError3(message);
3073
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3074
+ return new ConnectivityError3(message);
3075
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3076
+ return new UnknownProtocolError3(message);
3077
+ } else {
3078
+ return typeof err === "string" ? new Error(err) : err;
3079
+ }
3080
+ };
2976
3081
 
2977
3082
  // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
2978
3083
  import { Duplex as Duplex2 } from "stream";
@@ -3360,4 +3465,4 @@ export {
3360
3465
  createLibDataChannelTransportFactory,
3361
3466
  createTeleportProtocolFactory
3362
3467
  };
3363
- //# sourceMappingURL=chunk-VP6DWC3F.mjs.map
3468
+ //# sourceMappingURL=chunk-JIGWYM5K.mjs.map