@dxos/network-manager 0.1.58-main.75cbd70 → 0.1.58-main.7858df5

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-HKHUCTRB.mjs} +205 -114
  2. package/dist/lib/browser/chunk-HKHUCTRB.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 +493 -402
  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 +1085 -860
  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 +6 -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")
@@ -933,6 +963,9 @@ var Peer = class {
933
963
  S: this,
934
964
  C: (f, a) => f(...a)
935
965
  });
966
+ if (err instanceof TimeoutError) {
967
+ await connection.abort(err);
968
+ }
936
969
  await this.closeConnection(err);
937
970
  throw err;
938
971
  } finally {
@@ -952,13 +985,13 @@ var Peer = class {
952
985
  sessionId
953
986
  }, {
954
987
  F: __dxlog_file3,
955
- L: 210,
988
+ L: 213,
956
989
  S: this,
957
990
  C: (f, a) => f(...a)
958
991
  });
959
992
  invariant3(!this.connection, "Already connected.", {
960
993
  F: __dxlog_file3,
961
- L: 217,
994
+ L: 220,
962
995
  S: this,
963
996
  A: [
964
997
  "!this.connection",
@@ -994,7 +1027,7 @@ var Peer = class {
994
1027
  initiator
995
1028
  }, {
996
1029
  F: __dxlog_file3,
997
- L: 236,
1030
+ L: 239,
998
1031
  S: this,
999
1032
  C: (f, a) => f(...a)
1000
1033
  });
@@ -1007,14 +1040,14 @@ var Peer = class {
1007
1040
  initiator
1008
1041
  }, {
1009
1042
  F: __dxlog_file3,
1010
- L: 245,
1043
+ L: 248,
1011
1044
  S: this,
1012
1045
  C: (f, a) => f(...a)
1013
1046
  });
1014
1047
  this._connectionLimiter.doneConnecting(sessionId);
1015
1048
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
1016
1049
  F: __dxlog_file3,
1017
- L: 250,
1050
+ L: 253,
1018
1051
  S: this,
1019
1052
  A: [
1020
1053
  "this.connection === connection",
@@ -1029,7 +1062,7 @@ var Peer = class {
1029
1062
  initiator
1030
1063
  }, {
1031
1064
  F: __dxlog_file3,
1032
- L: 252,
1065
+ L: 255,
1033
1066
  S: this,
1034
1067
  C: (f, a) => f(...a)
1035
1068
  });
@@ -1064,7 +1097,7 @@ var Peer = class {
1064
1097
  err
1065
1098
  }, {
1066
1099
  F: __dxlog_file3,
1067
- L: 292,
1100
+ L: 295,
1068
1101
  S: this,
1069
1102
  C: (f, a) => f(...a)
1070
1103
  });
@@ -1077,7 +1110,7 @@ var Peer = class {
1077
1110
  err
1078
1111
  }, {
1079
1112
  F: __dxlog_file3,
1080
- L: 293,
1113
+ L: 296,
1081
1114
  S: this,
1082
1115
  C: (f, a) => f(...a)
1083
1116
  });
@@ -1096,7 +1129,7 @@ var Peer = class {
1096
1129
  sessionId: connection.sessionId
1097
1130
  }, {
1098
1131
  F: __dxlog_file3,
1099
- L: 318,
1132
+ L: 321,
1100
1133
  S: this,
1101
1134
  C: (f, a) => f(...a)
1102
1135
  });
@@ -1106,7 +1139,7 @@ var Peer = class {
1106
1139
  sessionId: connection.sessionId
1107
1140
  }, {
1108
1141
  F: __dxlog_file3,
1109
- L: 324,
1142
+ L: 327,
1110
1143
  S: this,
1111
1144
  C: (f, a) => f(...a)
1112
1145
  });
@@ -1117,7 +1150,7 @@ var Peer = class {
1117
1150
  message
1118
1151
  }, {
1119
1152
  F: __dxlog_file3,
1120
- L: 329,
1153
+ L: 332,
1121
1154
  S: this,
1122
1155
  C: (f, a) => f(...a)
1123
1156
  });
@@ -1132,7 +1165,7 @@ var Peer = class {
1132
1165
  topic: this.topic
1133
1166
  }, {
1134
1167
  F: __dxlog_file3,
1135
- L: 339,
1168
+ L: 342,
1136
1169
  S: this,
1137
1170
  C: (f, a) => f(...a)
1138
1171
  });
@@ -1645,7 +1678,6 @@ var SwarmMapper = class {
1645
1678
  get peers() {
1646
1679
  return Array.from(this._peers.values());
1647
1680
  }
1648
- // prettier-ignore
1649
1681
  constructor(_swarm) {
1650
1682
  this._swarm = _swarm;
1651
1683
  this._subscriptions = new EventSubscriptions();
@@ -1668,7 +1700,7 @@ var SwarmMapper = class {
1668
1700
  _update() {
1669
1701
  log5("updating swarm", void 0, {
1670
1702
  F: __dxlog_file5,
1671
- L: 75,
1703
+ L: 72,
1672
1704
  S: this,
1673
1705
  C: (f, a) => f(...a)
1674
1706
  });
@@ -1692,7 +1724,7 @@ var SwarmMapper = class {
1692
1724
  totalPeersInSwarm: this._peers.size
1693
1725
  }, {
1694
1726
  F: __dxlog_file5,
1695
- L: 116,
1727
+ L: 113,
1696
1728
  S: this,
1697
1729
  C: (f, a) => f(...a)
1698
1730
  });
@@ -1708,13 +1740,13 @@ var SwarmMapper = class {
1708
1740
  // packages/core/mesh/network-manager/src/swarm/connection-limiter.ts
1709
1741
  import { DeferredTask as DeferredTask2 } from "@dxos/async";
1710
1742
  import { Context as Context5 } from "@dxos/context";
1711
- import { CancelledError as CancelledError3 } from "@dxos/errors";
1712
1743
  import { invariant as invariant5 } from "@dxos/invariant";
1713
1744
  import { PublicKey as PublicKey6 } from "@dxos/keys";
1714
1745
  import { log as log6 } from "@dxos/log";
1746
+ import { CancelledError as CancelledError3 } from "@dxos/protocols";
1715
1747
  import { ComplexMap as ComplexMap4 } from "@dxos/util";
1716
1748
  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;
1749
+ var MAX_CONCURRENT_INITIATING_CONNECTIONS = 50;
1718
1750
  var ConnectionLimiter = class {
1719
1751
  constructor({ maxConcurrentInitConnections = MAX_CONCURRENT_INITIATING_CONNECTIONS } = {}) {
1720
1752
  this._ctx = new Context5();
@@ -2256,7 +2288,6 @@ import { invariant as invariant9 } from "@dxos/invariant";
2256
2288
  import { log as log9 } from "@dxos/log";
2257
2289
  var __dxlog_file10 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/topology/star-topology.ts";
2258
2290
  var StarTopology = class {
2259
- // prettier-ignore
2260
2291
  constructor(_centralPeer) {
2261
2292
  this._centralPeer = _centralPeer;
2262
2293
  }
@@ -2266,7 +2297,7 @@ var StarTopology = class {
2266
2297
  init(controller) {
2267
2298
  invariant9(!this._controller, "Already initialized.", {
2268
2299
  F: __dxlog_file10,
2269
- L: 24,
2300
+ L: 21,
2270
2301
  S: this,
2271
2302
  A: [
2272
2303
  "!this._controller",
@@ -2278,7 +2309,7 @@ var StarTopology = class {
2278
2309
  update() {
2279
2310
  invariant9(this._controller, "Not initialized.", {
2280
2311
  F: __dxlog_file10,
2281
- L: 29,
2312
+ L: 26,
2282
2313
  S: this,
2283
2314
  A: [
2284
2315
  "this._controller",
@@ -2289,7 +2320,7 @@ var StarTopology = class {
2289
2320
  if (!ownPeerId.equals(this._centralPeer)) {
2290
2321
  log9("leaf peer dropping all connections apart from central peer.", void 0, {
2291
2322
  F: __dxlog_file10,
2292
- L: 32,
2323
+ L: 29,
2293
2324
  S: this,
2294
2325
  C: (f, a) => f(...a)
2295
2326
  });
@@ -2299,7 +2330,7 @@ var StarTopology = class {
2299
2330
  peer
2300
2331
  }, {
2301
2332
  F: __dxlog_file10,
2302
- L: 37,
2333
+ L: 34,
2303
2334
  S: this,
2304
2335
  C: (f, a) => f(...a)
2305
2336
  });
@@ -2313,7 +2344,7 @@ var StarTopology = class {
2313
2344
  peer
2314
2345
  }, {
2315
2346
  F: __dxlog_file10,
2316
- L: 46,
2347
+ L: 43,
2317
2348
  S: this,
2318
2349
  C: (f, a) => f(...a)
2319
2350
  });
@@ -2324,7 +2355,7 @@ var StarTopology = class {
2324
2355
  async onOffer(peer) {
2325
2356
  invariant9(this._controller, "Not initialized.", {
2326
2357
  F: __dxlog_file10,
2327
- L: 53,
2358
+ L: 50,
2328
2359
  S: this,
2329
2360
  A: [
2330
2361
  "this._controller",
@@ -2338,7 +2369,7 @@ var StarTopology = class {
2338
2369
  isSelfCentral: ownPeerId.equals(this._centralPeer)
2339
2370
  }, {
2340
2371
  F: __dxlog_file10,
2341
- L: 55,
2372
+ L: 52,
2342
2373
  S: this,
2343
2374
  C: (f, a) => f(...a)
2344
2375
  });
@@ -2541,6 +2572,7 @@ var TransportKind;
2541
2572
  TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2542
2573
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2543
2574
  TransportKind2["MEMORY"] = "MEMORY";
2575
+ TransportKind2["TCP"] = "TCP";
2544
2576
  })(TransportKind || (TransportKind = {}));
2545
2577
 
2546
2578
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
@@ -2550,7 +2582,7 @@ import { Event as Event8 } from "@dxos/async";
2550
2582
  import { ErrorStream as ErrorStream4, raise as raise2 } from "@dxos/debug";
2551
2583
  import { PublicKey as PublicKey10 } from "@dxos/keys";
2552
2584
  import { log as log11 } from "@dxos/log";
2553
- import { trace as trace4 } from "@dxos/protocols";
2585
+ import { ConnectionResetError as ConnectionResetError2, ConnectivityError as ConnectivityError2, ProtocolError as ProtocolError2, UnknownProtocolError as UnknownProtocolError2, trace as trace4 } from "@dxos/protocols";
2554
2586
 
2555
2587
  // packages/core/mesh/network-manager/src/transport/webrtc.ts
2556
2588
  var wrtc = null;
@@ -2573,13 +2605,13 @@ var SimplePeerTransport = class {
2573
2605
  id: this._instanceId
2574
2606
  }), {
2575
2607
  F: __dxlog_file12,
2576
- L: 39,
2608
+ L: 40,
2577
2609
  S: this,
2578
2610
  C: (f, a) => f(...a)
2579
2611
  });
2580
2612
  log11("created connection", params, {
2581
2613
  F: __dxlog_file12,
2582
- L: 40,
2614
+ L: 41,
2583
2615
  S: this,
2584
2616
  C: (f, a) => f(...a)
2585
2617
  });
@@ -2592,7 +2624,7 @@ var SimplePeerTransport = class {
2592
2624
  this._peer.on("signal", async (data) => {
2593
2625
  log11("signal", data, {
2594
2626
  F: __dxlog_file12,
2595
- L: 49,
2627
+ L: 50,
2596
2628
  S: this,
2597
2629
  C: (f, a) => f(...a)
2598
2630
  });
@@ -2605,7 +2637,7 @@ var SimplePeerTransport = class {
2605
2637
  this._peer.on("connect", () => {
2606
2638
  log11("connected", void 0, {
2607
2639
  F: __dxlog_file12,
2608
- L: 54,
2640
+ L: 55,
2609
2641
  S: this,
2610
2642
  C: (f, a) => f(...a)
2611
2643
  });
@@ -2615,7 +2647,7 @@ var SimplePeerTransport = class {
2615
2647
  this._peer.on("close", async () => {
2616
2648
  log11("closed", void 0, {
2617
2649
  F: __dxlog_file12,
2618
- L: 60,
2650
+ L: 61,
2619
2651
  S: this,
2620
2652
  C: (f, a) => f(...a)
2621
2653
  });
@@ -2623,22 +2655,66 @@ var SimplePeerTransport = class {
2623
2655
  this.closed.emit();
2624
2656
  });
2625
2657
  this._peer.on("error", async (err) => {
2626
- this.errors.raise(err);
2658
+ if (typeof RTCError !== "undefined" && err instanceof RTCError) {
2659
+ if (err.errorDetail === "sctp-failure") {
2660
+ this.errors.raise(new ConnectionResetError2("sctp-failure from RTCError", err));
2661
+ } else {
2662
+ this.errors.raise(new UnknownProtocolError2("unknown RTCError", err));
2663
+ }
2664
+ } else if ("code" in err) {
2665
+ log11.info("simple-peer error", err, {
2666
+ F: __dxlog_file12,
2667
+ L: 77,
2668
+ S: this,
2669
+ C: (f, a) => f(...a)
2670
+ });
2671
+ switch (err.code) {
2672
+ case "ERR_WEBRTC_SUPPORT":
2673
+ this.errors.raise(new ProtocolError2("WebRTC not supported", err));
2674
+ case "ERR_ICE_CONNECTION_FAILURE":
2675
+ case "ERR_DATA_CHANNEL":
2676
+ case "ERR_CONNECTION_FAILURE":
2677
+ case "ERR_SIGNALING":
2678
+ this.errors.raise(new ConnectivityError2("unknown communication failure", err));
2679
+ case "ERR_CREATE_OFFER":
2680
+ case "ERR_CREATE_ANSWER":
2681
+ case "ERR_SET_LOCAL_DESCRIPTION":
2682
+ case "ERR_SET_REMOTE_DESCRIPTION":
2683
+ case "ERR_ADD_ICE_CANDIDATE":
2684
+ this.errors.raise(new UnknownProtocolError2("unknown simple-peer library failure", err));
2685
+ default:
2686
+ this.errors.raise(new Error("unknown simple-peer error"));
2687
+ }
2688
+ } else {
2689
+ log11.info("unknown peer connection error", err, {
2690
+ F: __dxlog_file12,
2691
+ L: 96,
2692
+ S: this,
2693
+ C: (f, a) => f(...a)
2694
+ });
2695
+ this.errors.raise(err);
2696
+ }
2627
2697
  try {
2628
- if (typeof this._peer?._pc.getStats === "function") {
2698
+ if (typeof this._peer?._pc?.getStats === "function") {
2629
2699
  this._peer._pc.getStats().then((stats) => {
2630
2700
  log11.warn("report after webrtc error", {
2631
2701
  config: this.params.webrtcConfig,
2632
2702
  stats
2633
2703
  }, {
2634
2704
  F: __dxlog_file12,
2635
- L: 72,
2705
+ L: 104,
2636
2706
  S: this,
2637
2707
  C: (f, a) => f(...a)
2638
2708
  });
2639
2709
  });
2640
2710
  }
2641
- } catch {
2711
+ } catch (err2) {
2712
+ log11.catch(err2, void 0, {
2713
+ F: __dxlog_file12,
2714
+ L: 111,
2715
+ S: this,
2716
+ C: (f, a) => f(...a)
2717
+ });
2642
2718
  }
2643
2719
  await this.destroy();
2644
2720
  });
@@ -2646,7 +2722,7 @@ var SimplePeerTransport = class {
2646
2722
  id: this._instanceId
2647
2723
  }), {
2648
2724
  F: __dxlog_file12,
2649
- L: 82,
2725
+ L: 116,
2650
2726
  S: this,
2651
2727
  C: (f, a) => f(...a)
2652
2728
  });
@@ -2654,7 +2730,7 @@ var SimplePeerTransport = class {
2654
2730
  async destroy() {
2655
2731
  log11("closing...", void 0, {
2656
2732
  F: __dxlog_file12,
2657
- L: 86,
2733
+ L: 120,
2658
2734
  S: this,
2659
2735
  C: (f, a) => f(...a)
2660
2736
  });
@@ -2664,7 +2740,7 @@ var SimplePeerTransport = class {
2664
2740
  this.closed.emit();
2665
2741
  log11("closed", void 0, {
2666
2742
  F: __dxlog_file12,
2667
- L: 91,
2743
+ L: 125,
2668
2744
  S: this,
2669
2745
  C: (f, a) => f(...a)
2670
2746
  });
@@ -2697,7 +2773,6 @@ import { ConnectionState as ConnectionState3 } from "@dxos/protocols/proto/dxos/
2697
2773
  import { ComplexMap as ComplexMap8 } from "@dxos/util";
2698
2774
  var __dxlog_file13 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts";
2699
2775
  var SimplePeerTransportService = class {
2700
- // prettier-ignore
2701
2776
  constructor(_webrtcConfig) {
2702
2777
  this._webrtcConfig = _webrtcConfig;
2703
2778
  this.transports = new ComplexMap8(PublicKey11.hash);
@@ -2777,7 +2852,7 @@ var SimplePeerTransportService = class {
2777
2852
  async sendSignal({ proxyId, signal }) {
2778
2853
  invariant12(this.transports.has(proxyId), void 0, {
2779
2854
  F: __dxlog_file13,
2780
- L: 113,
2855
+ L: 110,
2781
2856
  S: this,
2782
2857
  A: [
2783
2858
  "this.transports.has(proxyId)",
@@ -2789,7 +2864,7 @@ var SimplePeerTransportService = class {
2789
2864
  async sendData({ proxyId, payload }) {
2790
2865
  invariant12(this.transports.has(proxyId), void 0, {
2791
2866
  F: __dxlog_file13,
2792
- L: 118,
2867
+ L: 115,
2793
2868
  S: this,
2794
2869
  A: [
2795
2870
  "this.transports.has(proxyId)",
@@ -2810,7 +2885,7 @@ var SimplePeerTransportService = class {
2810
2885
  this.transports.delete(proxyId);
2811
2886
  log12("Closed.", void 0, {
2812
2887
  F: __dxlog_file13,
2813
- L: 132,
2888
+ L: 129,
2814
2889
  S: this,
2815
2890
  C: (f, a) => f(...a)
2816
2891
  });
@@ -2825,11 +2900,11 @@ import { ErrorStream as ErrorStream5 } from "@dxos/debug";
2825
2900
  import { invariant as invariant13 } from "@dxos/invariant";
2826
2901
  import { PublicKey as PublicKey12 } from "@dxos/keys";
2827
2902
  import { log as log13 } from "@dxos/log";
2903
+ import { ConnectionResetError as ConnectionResetError3, TimeoutError as TimeoutError2, ProtocolError as ProtocolError3, ConnectivityError as ConnectivityError3, UnknownProtocolError as UnknownProtocolError3 } from "@dxos/protocols";
2828
2904
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
2829
2905
  import { arrayToBuffer } from "@dxos/util";
2830
2906
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
2831
2907
  var SimplePeerTransportProxy = class {
2832
- // prettier-ignore
2833
2908
  constructor(_params) {
2834
2909
  this._params = _params;
2835
2910
  this._proxyId = PublicKey12.random();
@@ -2846,7 +2921,7 @@ var SimplePeerTransportProxy = class {
2846
2921
  this._serviceStream.subscribe(async (event) => {
2847
2922
  log13("SimplePeerTransportProxy: event", event, {
2848
2923
  F: __dxlog_file14,
2849
- L: 51,
2924
+ L: 55,
2850
2925
  S: this,
2851
2926
  C: (f, a) => f(...a)
2852
2927
  });
@@ -2866,7 +2941,7 @@ var SimplePeerTransportProxy = class {
2866
2941
  }).then(() => callback(), (err) => {
2867
2942
  log13.catch(err, void 0, {
2868
2943
  F: __dxlog_file14,
2869
- L: 69,
2944
+ L: 76,
2870
2945
  S: this,
2871
2946
  C: (f, a) => f(...a)
2872
2947
  });
@@ -2875,14 +2950,14 @@ var SimplePeerTransportProxy = class {
2875
2950
  }));
2876
2951
  }, (error) => log13.catch(error, void 0, {
2877
2952
  F: __dxlog_file14,
2878
- L: 75,
2953
+ L: 83,
2879
2954
  S: this,
2880
2955
  C: (f, a) => f(...a)
2881
2956
  }));
2882
2957
  }
2883
2958
  async _handleConnection(connectionEvent) {
2884
2959
  if (connectionEvent.error) {
2885
- this.errors.raise(new Error(connectionEvent.error));
2960
+ this.errors.raise(decodeError(connectionEvent.error));
2886
2961
  }
2887
2962
  switch (connectionEvent.state) {
2888
2963
  case ConnectionState4.CONNECTED: {
@@ -2905,7 +2980,7 @@ var SimplePeerTransportProxy = class {
2905
2980
  this._params.bridgeService.sendSignal({
2906
2981
  proxyId: this._proxyId,
2907
2982
  signal
2908
- }).catch((err) => this.errors.raise(err));
2983
+ }).catch((err) => this.errors.raise(decodeError(err)));
2909
2984
  }
2910
2985
  // TODO(burdon): Move open from constructor.
2911
2986
  async destroy() {
@@ -2921,7 +2996,7 @@ var SimplePeerTransportProxy = class {
2921
2996
  } catch (err) {
2922
2997
  log13.catch(err, void 0, {
2923
2998
  F: __dxlog_file14,
2924
- L: 126,
2999
+ L: 134,
2925
3000
  S: this,
2926
3001
  C: (f, a) => f(...a)
2927
3002
  });
@@ -2957,7 +3032,7 @@ var SimplePeerTransportProxyFactory = class {
2957
3032
  createTransport(options) {
2958
3033
  invariant13(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
2959
3034
  F: __dxlog_file14,
2960
- L: 163,
3035
+ L: 171,
2961
3036
  S: this,
2962
3037
  A: [
2963
3038
  "this._bridgeService",
@@ -2973,6 +3048,22 @@ var SimplePeerTransportProxyFactory = class {
2973
3048
  return transport;
2974
3049
  }
2975
3050
  };
3051
+ var decodeError = (err) => {
3052
+ const message = typeof err === "string" ? err : err.message;
3053
+ if (message.includes("CONNECTION_RESET")) {
3054
+ return new ConnectionResetError3(message);
3055
+ } else if (message.includes("TIMEOUT")) {
3056
+ return new TimeoutError2(message);
3057
+ } else if (message.includes("PROTOCOL_ERROR")) {
3058
+ return new ProtocolError3(message);
3059
+ } else if (message.includes("CONNECTIVITY_ERROR")) {
3060
+ return new ConnectivityError3(message);
3061
+ } else if (message.includes("UNKNOWN_PROTOCOL_ERROR")) {
3062
+ return new UnknownProtocolError3(message);
3063
+ } else {
3064
+ return typeof err === "string" ? new Error(err) : err;
3065
+ }
3066
+ };
2976
3067
 
2977
3068
  // packages/core/mesh/network-manager/src/transport/libdatachannel-transport.ts
2978
3069
  import { Duplex as Duplex2 } from "stream";
@@ -3360,4 +3451,4 @@ export {
3360
3451
  createLibDataChannelTransportFactory,
3361
3452
  createTeleportProtocolFactory
3362
3453
  };
3363
- //# sourceMappingURL=chunk-VP6DWC3F.mjs.map
3454
+ //# sourceMappingURL=chunk-HKHUCTRB.mjs.map