@dxos/network-manager 0.4.10-main.fa5a270 → 0.4.10-main.fd8ea31

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 (47) hide show
  1. package/dist/lib/browser/{chunk-KEN4UCZF.mjs → chunk-SOVZDG44.mjs} +503 -453
  2. package/dist/lib/browser/chunk-SOVZDG44.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 +1 -1
  6. package/dist/lib/node/{chunk-5D3NBEP2.cjs → chunk-BMZFS4HT.cjs} +519 -468
  7. package/dist/lib/node/chunk-BMZFS4HT.cjs.map +7 -0
  8. package/dist/lib/node/index.cjs +27 -27
  9. package/dist/lib/node/index.cjs.map +1 -1
  10. package/dist/lib/node/meta.json +1 -1
  11. package/dist/lib/node/testing/index.cjs +18 -18
  12. package/dist/types/src/swarm/connection.d.ts.map +1 -1
  13. package/dist/types/src/swarm/peer.d.ts.map +1 -1
  14. package/dist/types/src/swarm/swarm.d.ts.map +1 -1
  15. package/dist/types/src/transport/libdatachannel-transport.d.ts +20 -19
  16. package/dist/types/src/transport/libdatachannel-transport.d.ts.map +1 -1
  17. package/dist/types/src/transport/memory-transport.d.ts +17 -10
  18. package/dist/types/src/transport/memory-transport.d.ts.map +1 -1
  19. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts +7 -9
  20. package/dist/types/src/transport/simplepeer-transport-proxy.d.ts.map +1 -1
  21. package/dist/types/src/transport/simplepeer-transport-service.d.ts.map +1 -1
  22. package/dist/types/src/transport/simplepeer-transport.d.ts +9 -11
  23. package/dist/types/src/transport/simplepeer-transport.d.ts.map +1 -1
  24. package/dist/types/src/transport/tcp-transport.browser.d.ts +7 -2
  25. package/dist/types/src/transport/tcp-transport.browser.d.ts.map +1 -1
  26. package/dist/types/src/transport/tcp-transport.d.ts +6 -4
  27. package/dist/types/src/transport/tcp-transport.d.ts.map +1 -1
  28. package/dist/types/src/transport/transport.d.ts +18 -12
  29. package/dist/types/src/transport/transport.d.ts.map +1 -1
  30. package/package.json +17 -17
  31. package/src/swarm/connection.ts +7 -5
  32. package/src/swarm/peer.ts +3 -4
  33. package/src/swarm/swarm.ts +1 -0
  34. package/src/transport/libdatachannel-transport.test.ts +14 -9
  35. package/src/transport/libdatachannel-transport.ts +226 -204
  36. package/src/transport/memory-transport.test.ts +10 -7
  37. package/src/transport/memory-transport.ts +49 -42
  38. package/src/transport/simplepeer-transport-proxy-test.ts +13 -8
  39. package/src/transport/simplepeer-transport-proxy.ts +57 -50
  40. package/src/transport/simplepeer-transport-service.ts +5 -2
  41. package/src/transport/simplepeer-transport.test.ts +9 -5
  42. package/src/transport/simplepeer-transport.ts +25 -25
  43. package/src/transport/tcp-transport.browser.ts +10 -5
  44. package/src/transport/tcp-transport.ts +31 -22
  45. package/src/transport/transport.ts +25 -14
  46. package/dist/lib/browser/chunk-KEN4UCZF.mjs.map +0 -7
  47. package/dist/lib/node/chunk-5D3NBEP2.cjs.map +0 -7
@@ -167,6 +167,7 @@ var Connection = class {
167
167
  sendSignal: async (signal) => this._sendSignal(signal),
168
168
  sessionId: this.sessionId
169
169
  });
170
+ await this._transport.open();
170
171
  this._transport.connected.once(async () => {
171
172
  this._changeState("CONNECTED");
172
173
  await this.connectedTimeoutContext.dispose();
@@ -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: 216,
181
+ L: 218,
181
182
  S: this,
182
183
  C: (f, a) => f(...a)
183
184
  });
@@ -188,7 +189,7 @@ var Connection = class {
188
189
  err
189
190
  }, {
190
191
  F: __dxlog_file,
191
- L: 221,
192
+ L: 223,
192
193
  S: this,
193
194
  C: (f, a) => f(...a)
194
195
  });
@@ -198,7 +199,7 @@ var Connection = class {
198
199
  if (err instanceof ConnectionResetError) {
199
200
  log.info("aborting due to transport ConnectionResetError", void 0, {
200
201
  F: __dxlog_file,
201
- L: 228,
202
+ L: 230,
202
203
  S: this,
203
204
  C: (f, a) => f(...a)
204
205
  });
@@ -206,7 +207,7 @@ var Connection = class {
206
207
  } else if (err instanceof ConnectivityError) {
207
208
  log.info("aborting due to transport ConnectivityError", void 0, {
208
209
  F: __dxlog_file,
209
- L: 231,
210
+ L: 233,
210
211
  S: this,
211
212
  C: (f, a) => f(...a)
212
213
  });
@@ -216,7 +217,7 @@ var Connection = class {
216
217
  err
217
218
  }, {
218
219
  F: __dxlog_file,
219
- L: 234,
220
+ L: 236,
220
221
  S: this,
221
222
  C: (f, a) => f(...a)
222
223
  });
@@ -227,14 +228,14 @@ var Connection = class {
227
228
  }
228
229
  });
229
230
  for (const signal of this._incomingSignalBuffer) {
230
- void this._transport.signal(signal);
231
+ void this._transport.onSignal(signal);
231
232
  }
232
233
  this._incomingSignalBuffer = [];
233
234
  log.trace("dxos.mesh.connection.open-connection", trace.end({
234
235
  id: this._instanceId
235
236
  }), {
236
237
  F: __dxlog_file,
237
- L: 250,
238
+ L: 252,
238
239
  S: this,
239
240
  C: (f, a) => f(...a)
240
241
  });
@@ -244,14 +245,14 @@ var Connection = class {
244
245
  err
245
246
  }, {
246
247
  F: __dxlog_file,
247
- L: 257,
248
+ L: 259,
248
249
  S: this,
249
250
  C: (f, a) => f(...a)
250
251
  });
251
252
  if (this._state === "CLOSED" || this._state === "ABORTED") {
252
253
  log(`abort ignored: already ${this._state}`, this.closeReason, {
253
254
  F: __dxlog_file,
254
- L: 259,
255
+ L: 261,
255
256
  S: this,
256
257
  C: (f, a) => f(...a)
257
258
  });
@@ -266,7 +267,7 @@ var Connection = class {
266
267
  try {
267
268
  log("aborting protocol... ", void 0, {
268
269
  F: __dxlog_file,
269
- L: 273,
270
+ L: 275,
270
271
  S: this,
271
272
  C: (f, a) => f(...a)
272
273
  });
@@ -274,17 +275,17 @@ var Connection = class {
274
275
  } catch (err2) {
275
276
  log.catch(err2, void 0, {
276
277
  F: __dxlog_file,
277
- L: 276,
278
+ L: 278,
278
279
  S: this,
279
280
  C: (f, a) => f(...a)
280
281
  });
281
282
  }
282
283
  try {
283
- await this._transport?.destroy();
284
+ await this._transport?.close();
284
285
  } catch (err2) {
285
286
  log.catch(err2, void 0, {
286
287
  F: __dxlog_file,
287
- L: 283,
288
+ L: 285,
288
289
  S: this,
289
290
  C: (f, a) => f(...a)
290
291
  });
@@ -294,7 +295,7 @@ var Connection = class {
294
295
  } catch (err2) {
295
296
  log.catch(err2, void 0, {
296
297
  F: __dxlog_file,
297
- L: 289,
298
+ L: 291,
298
299
  S: this,
299
300
  C: (f, a) => f(...a)
300
301
  });
@@ -318,7 +319,7 @@ var Connection = class {
318
319
  peerId: this.ownId
319
320
  }, {
320
321
  F: __dxlog_file,
321
- L: 314,
322
+ L: 316,
322
323
  S: this,
323
324
  C: (f, a) => f(...a)
324
325
  });
@@ -328,17 +329,17 @@ var Connection = class {
328
329
  } catch (err2) {
329
330
  log.catch(err2, void 0, {
330
331
  F: __dxlog_file,
331
- L: 321,
332
+ L: 323,
332
333
  S: this,
333
334
  C: (f, a) => f(...a)
334
335
  });
335
336
  }
336
337
  try {
337
- await this._transport?.destroy();
338
+ await this._transport?.close();
338
339
  } catch (err2) {
339
340
  log.catch(err2, void 0, {
340
341
  F: __dxlog_file,
341
- L: 328,
342
+ L: 330,
342
343
  S: this,
343
344
  C: (f, a) => f(...a)
344
345
  });
@@ -346,7 +347,7 @@ var Connection = class {
346
347
  } else {
347
348
  log.info(`graceful close requested when we were in ${lastState} state? aborting`, void 0, {
348
349
  F: __dxlog_file,
349
- L: 331,
350
+ L: 333,
350
351
  S: this,
351
352
  C: (f, a) => f(...a)
352
353
  });
@@ -355,17 +356,17 @@ var Connection = class {
355
356
  } catch (err2) {
356
357
  log.catch(err2, void 0, {
357
358
  F: __dxlog_file,
358
- L: 335,
359
+ L: 337,
359
360
  S: this,
360
361
  C: (f, a) => f(...a)
361
362
  });
362
363
  }
363
364
  try {
364
- await this._transport?.destroy();
365
+ await this._transport?.close();
365
366
  } catch (err2) {
366
367
  log.catch(err2, void 0, {
367
368
  F: __dxlog_file,
368
- L: 340,
369
+ L: 342,
369
370
  S: this,
370
371
  C: (f, a) => f(...a)
371
372
  });
@@ -375,7 +376,7 @@ var Connection = class {
375
376
  peerId: this.ownId
376
377
  }, {
377
378
  F: __dxlog_file,
378
- L: 344,
379
+ L: 346,
379
380
  S: this,
380
381
  C: (f, a) => f(...a)
381
382
  });
@@ -418,7 +419,7 @@ var Connection = class {
418
419
  err
419
420
  }, {
420
421
  F: __dxlog_file,
421
- L: 382,
422
+ L: 384,
422
423
  S: this,
423
424
  C: (f, a) => f(...a)
424
425
  });
@@ -431,7 +432,7 @@ var Connection = class {
431
432
  async signal(msg) {
432
433
  invariant(msg.sessionId, void 0, {
433
434
  F: __dxlog_file,
434
- L: 391,
435
+ L: 393,
435
436
  S: this,
436
437
  A: [
437
438
  "msg.sessionId",
@@ -441,7 +442,7 @@ var Connection = class {
441
442
  if (!msg.sessionId.equals(this.sessionId)) {
442
443
  log("dropping signal for incorrect session id", void 0, {
443
444
  F: __dxlog_file,
444
- L: 393,
445
+ L: 395,
445
446
  S: this,
446
447
  C: (f, a) => f(...a)
447
448
  });
@@ -449,7 +450,7 @@ var Connection = class {
449
450
  }
450
451
  invariant(msg.data.signal || msg.data.signalBatch, void 0, {
451
452
  F: __dxlog_file,
452
- L: 396,
453
+ L: 398,
453
454
  S: this,
454
455
  A: [
455
456
  "msg.data.signal || msg.data.signalBatch",
@@ -458,7 +459,7 @@ var Connection = class {
458
459
  });
459
460
  invariant(msg.author?.equals(this.remoteId), void 0, {
460
461
  F: __dxlog_file,
461
- L: 397,
462
+ L: 399,
462
463
  S: this,
463
464
  A: [
464
465
  "msg.author?.equals(this.remoteId)",
@@ -467,7 +468,7 @@ var Connection = class {
467
468
  });
468
469
  invariant(msg.recipient?.equals(this.ownId), void 0, {
469
470
  F: __dxlog_file,
470
- L: 398,
471
+ L: 400,
471
472
  S: this,
472
473
  A: [
473
474
  "msg.recipient?.equals(this.ownId)",
@@ -491,7 +492,7 @@ var Connection = class {
491
492
  msg: msg.data
492
493
  }, {
493
494
  F: __dxlog_file,
494
- L: 407,
495
+ L: 409,
495
496
  S: this,
496
497
  C: (f, a) => f(...a)
497
498
  });
@@ -499,7 +500,7 @@ var Connection = class {
499
500
  } else {
500
501
  invariant(this._transport, "Connection not ready to accept signals.", {
501
502
  F: __dxlog_file,
502
- L: 410,
503
+ L: 412,
503
504
  S: this,
504
505
  A: [
505
506
  "this._transport",
@@ -512,11 +513,11 @@ var Connection = class {
512
513
  msg: msg.data
513
514
  }, {
514
515
  F: __dxlog_file,
515
- L: 411,
516
+ L: 413,
516
517
  S: this,
517
518
  C: (f, a) => f(...a)
518
519
  });
519
- await this._transport.signal(signal);
520
+ await this._transport.onSignal(signal);
520
521
  }
521
522
  }
522
523
  }
@@ -530,13 +531,13 @@ var Connection = class {
530
531
  peerId: this.ownId
531
532
  }, {
532
533
  F: __dxlog_file,
533
- L: 422,
534
+ L: 424,
534
535
  S: this,
535
536
  C: (f, a) => f(...a)
536
537
  });
537
538
  invariant(state !== this._state, "Already in this state.", {
538
539
  F: __dxlog_file,
539
- L: 423,
540
+ L: 425,
540
541
  S: this,
541
542
  A: [
542
543
  "state !== this._state",
@@ -841,7 +842,6 @@ var ConnectionDisplacedError = class extends SystemError {
841
842
  };
842
843
  var CONNECTION_COUNTS_STABLE_AFTER = 5e3;
843
844
  var Peer = class {
844
- // TODO(burdon): Convert to map?
845
845
  constructor(id, topic, localPeerId, _signalMessaging, _protocolProvider, _transportFactory, _connectionLimiter, _callbacks) {
846
846
  this.id = id;
847
847
  this.topic = topic;
@@ -870,7 +870,7 @@ var Peer = class {
870
870
  ].includes(this.connection.state)) {
871
871
  log3.info(`received offer when connection already in ${this.connection.state} state`, void 0, {
872
872
  F: __dxlog_file3,
873
- L: 115,
873
+ L: 114,
874
874
  S: this,
875
875
  C: (f, a) => f(...a)
876
876
  });
@@ -887,7 +887,7 @@ var Peer = class {
887
887
  sessionId: this.connection?.sessionId
888
888
  }, {
889
889
  F: __dxlog_file3,
890
- L: 124,
890
+ L: 123,
891
891
  S: this,
892
892
  C: (f, a) => f(...a)
893
893
  });
@@ -904,7 +904,7 @@ var Peer = class {
904
904
  if (!this.connection) {
905
905
  invariant3(message.sessionId, void 0, {
906
906
  F: __dxlog_file3,
907
- L: 144,
907
+ L: 143,
908
908
  S: this,
909
909
  A: [
910
910
  "message.sessionId",
@@ -925,7 +925,7 @@ var Peer = class {
925
925
  err
926
926
  }, {
927
927
  F: __dxlog_file3,
928
- L: 154,
928
+ L: 153,
929
929
  S: this,
930
930
  C: (f, a) => f(...a)
931
931
  });
@@ -947,7 +947,7 @@ var Peer = class {
947
947
  async initiateConnection() {
948
948
  invariant3(!this.initiating, "Initiation in progress.", {
949
949
  F: __dxlog_file3,
950
- L: 171,
950
+ L: 170,
951
951
  S: this,
952
952
  A: [
953
953
  "!this.initiating",
@@ -956,7 +956,7 @@ var Peer = class {
956
956
  });
957
957
  invariant3(!this.connection, "Already connected.", {
958
958
  F: __dxlog_file3,
959
- L: 172,
959
+ L: 171,
960
960
  S: this,
961
961
  A: [
962
962
  "!this.connection",
@@ -971,7 +971,7 @@ var Peer = class {
971
971
  sessionId
972
972
  }, {
973
973
  F: __dxlog_file3,
974
- L: 174,
974
+ L: 173,
975
975
  S: this,
976
976
  C: (f, a) => f(...a)
977
977
  });
@@ -997,14 +997,14 @@ var Peer = class {
997
997
  remoteId: this.id
998
998
  }, {
999
999
  F: __dxlog_file3,
1000
- L: 191,
1000
+ L: 190,
1001
1001
  S: this,
1002
1002
  C: (f, a) => f(...a)
1003
1003
  });
1004
1004
  if (connection.state !== ConnectionState.INITIAL) {
1005
1005
  log3("ignoring response", void 0, {
1006
1006
  F: __dxlog_file3,
1007
- L: 193,
1007
+ L: 192,
1008
1008
  S: this,
1009
1009
  C: (f, a) => f(...a)
1010
1010
  });
@@ -1018,7 +1018,7 @@ var Peer = class {
1018
1018
  remoteId: this.id
1019
1019
  }, {
1020
1020
  F: __dxlog_file3,
1021
- L: 197,
1021
+ L: 196,
1022
1022
  S: this,
1023
1023
  C: (f, a) => f(...a)
1024
1024
  });
@@ -1040,7 +1040,7 @@ var Peer = class {
1040
1040
  remoteId: this.id
1041
1041
  }, {
1042
1042
  F: __dxlog_file3,
1043
- L: 210,
1043
+ L: 209,
1044
1044
  S: this,
1045
1045
  C: (f, a) => f(...a)
1046
1046
  });
@@ -1052,7 +1052,7 @@ var Peer = class {
1052
1052
  try {
1053
1053
  log3("opening connection as initiator", void 0, {
1054
1054
  F: __dxlog_file3,
1055
- L: 223,
1055
+ L: 222,
1056
1056
  S: this,
1057
1057
  C: (f, a) => f(...a)
1058
1058
  });
@@ -1066,7 +1066,7 @@ var Peer = class {
1066
1066
  remoteId: this.id
1067
1067
  }, {
1068
1068
  F: __dxlog_file3,
1069
- L: 227,
1069
+ L: 226,
1070
1070
  S: this,
1071
1071
  C: (f, a) => f(...a)
1072
1072
  });
@@ -1074,7 +1074,7 @@ var Peer = class {
1074
1074
  err
1075
1075
  }, {
1076
1076
  F: __dxlog_file3,
1077
- L: 234,
1077
+ L: 233,
1078
1078
  S: this,
1079
1079
  C: (f, a) => f(...a)
1080
1080
  });
@@ -1097,13 +1097,13 @@ var Peer = class {
1097
1097
  sessionId
1098
1098
  }, {
1099
1099
  F: __dxlog_file3,
1100
- L: 248,
1100
+ L: 247,
1101
1101
  S: this,
1102
1102
  C: (f, a) => f(...a)
1103
1103
  });
1104
1104
  invariant3(!this.connection, "Already connected.", {
1105
1105
  F: __dxlog_file3,
1106
- L: 255,
1106
+ L: 254,
1107
1107
  S: this,
1108
1108
  A: [
1109
1109
  "!this.connection",
@@ -1139,7 +1139,7 @@ var Peer = class {
1139
1139
  initiator
1140
1140
  }, {
1141
1141
  F: __dxlog_file3,
1142
- L: 274,
1142
+ L: 273,
1143
1143
  S: this,
1144
1144
  C: (f, a) => f(...a)
1145
1145
  });
@@ -1152,14 +1152,14 @@ var Peer = class {
1152
1152
  initiator
1153
1153
  }, {
1154
1154
  F: __dxlog_file3,
1155
- L: 283,
1155
+ L: 282,
1156
1156
  S: this,
1157
1157
  C: (f, a) => f(...a)
1158
1158
  });
1159
1159
  this._connectionLimiter.doneConnecting(sessionId);
1160
1160
  invariant3(this.connection === connection, "Connection mismatch (race condition).", {
1161
1161
  F: __dxlog_file3,
1162
- L: 288,
1162
+ L: 287,
1163
1163
  S: this,
1164
1164
  A: [
1165
1165
  "this.connection === connection",
@@ -1174,7 +1174,7 @@ var Peer = class {
1174
1174
  initiator
1175
1175
  }, {
1176
1176
  F: __dxlog_file3,
1177
- L: 290,
1177
+ L: 289,
1178
1178
  S: this,
1179
1179
  C: (f, a) => f(...a)
1180
1180
  });
@@ -1209,7 +1209,7 @@ var Peer = class {
1209
1209
  err
1210
1210
  }, {
1211
1211
  F: __dxlog_file3,
1212
- L: 330,
1212
+ L: 329,
1213
1213
  S: this,
1214
1214
  C: (f, a) => f(...a)
1215
1215
  });
@@ -1222,7 +1222,7 @@ var Peer = class {
1222
1222
  err
1223
1223
  }, {
1224
1224
  F: __dxlog_file3,
1225
- L: 337,
1225
+ L: 336,
1226
1226
  S: this,
1227
1227
  C: (f, a) => f(...a)
1228
1228
  });
@@ -1241,7 +1241,7 @@ var Peer = class {
1241
1241
  sessionId: connection.sessionId
1242
1242
  }, {
1243
1243
  F: __dxlog_file3,
1244
- L: 362,
1244
+ L: 361,
1245
1245
  S: this,
1246
1246
  C: (f, a) => f(...a)
1247
1247
  });
@@ -1251,7 +1251,7 @@ var Peer = class {
1251
1251
  sessionId: connection.sessionId
1252
1252
  }, {
1253
1253
  F: __dxlog_file3,
1254
- L: 368,
1254
+ L: 367,
1255
1255
  S: this,
1256
1256
  C: (f, a) => f(...a)
1257
1257
  });
@@ -1262,7 +1262,7 @@ var Peer = class {
1262
1262
  message
1263
1263
  }, {
1264
1264
  F: __dxlog_file3,
1265
- L: 373,
1265
+ L: 372,
1266
1266
  S: this,
1267
1267
  C: (f, a) => f(...a)
1268
1268
  });
@@ -1277,7 +1277,7 @@ var Peer = class {
1277
1277
  topic: this.topic
1278
1278
  }, {
1279
1279
  F: __dxlog_file3,
1280
- L: 383,
1280
+ L: 382,
1281
1281
  S: this,
1282
1282
  C: (f, a) => f(...a)
1283
1283
  });
@@ -1343,7 +1343,7 @@ var Swarm = class {
1343
1343
  }
1344
1344
  }), {
1345
1345
  F: __dxlog_file4,
1346
- L: 88,
1346
+ L: 89,
1347
1347
  S: this,
1348
1348
  C: (f, a) => f(...a)
1349
1349
  });
@@ -1351,7 +1351,7 @@ var Swarm = class {
1351
1351
  peerId: _ownPeerId
1352
1352
  }, {
1353
1353
  F: __dxlog_file4,
1354
- L: 92,
1354
+ L: 93,
1355
1355
  S: this,
1356
1356
  C: (f, a) => f(...a)
1357
1357
  });
@@ -1366,7 +1366,7 @@ var Swarm = class {
1366
1366
  id: this._instanceId
1367
1367
  }), {
1368
1368
  F: __dxlog_file4,
1369
- L: 101,
1369
+ L: 102,
1370
1370
  S: this,
1371
1371
  C: (f, a) => f(...a)
1372
1372
  });
@@ -1389,7 +1389,7 @@ var Swarm = class {
1389
1389
  async open() {
1390
1390
  invariant4(!this._listeningHandle, void 0, {
1391
1391
  F: __dxlog_file4,
1392
- L: 128,
1392
+ L: 129,
1393
1393
  S: this,
1394
1394
  A: [
1395
1395
  "!this._listeningHandle",
@@ -1404,7 +1404,7 @@ var Swarm = class {
1404
1404
  err
1405
1405
  }, {
1406
1406
  F: __dxlog_file4,
1407
- L: 136,
1407
+ L: 137,
1408
1408
  S: this,
1409
1409
  C: (f, a) => f(...a)
1410
1410
  }));
@@ -1414,7 +1414,7 @@ var Swarm = class {
1414
1414
  async destroy() {
1415
1415
  log4("destroying...", void 0, {
1416
1416
  F: __dxlog_file4,
1417
- L: 142,
1417
+ L: 143,
1418
1418
  S: this,
1419
1419
  C: (f, a) => f(...a)
1420
1420
  });
@@ -1425,7 +1425,7 @@ var Swarm = class {
1425
1425
  await Promise.all(Array.from(this._peers.keys()).map((key) => this._destroyPeer(key, "swarm destroyed")));
1426
1426
  log4("destroyed", void 0, {
1427
1427
  F: __dxlog_file4,
1428
- L: 149,
1428
+ L: 150,
1429
1429
  S: this,
1430
1430
  C: (f, a) => f(...a)
1431
1431
  });
@@ -1433,7 +1433,7 @@ var Swarm = class {
1433
1433
  async setTopology(topology) {
1434
1434
  invariant4(!this._ctx.disposed, "Swarm is offline", {
1435
1435
  F: __dxlog_file4,
1436
- L: 153,
1436
+ L: 154,
1437
1437
  S: this,
1438
1438
  A: [
1439
1439
  "!this._ctx.disposed",
@@ -1448,7 +1448,7 @@ var Swarm = class {
1448
1448
  topology: getClassName(topology)
1449
1449
  }, {
1450
1450
  F: __dxlog_file4,
1451
- L: 157,
1451
+ L: 158,
1452
1452
  S: this,
1453
1453
  C: (f, a) => f(...a)
1454
1454
  });
@@ -1462,14 +1462,14 @@ var Swarm = class {
1462
1462
  swarmEvent
1463
1463
  }, {
1464
1464
  F: __dxlog_file4,
1465
- L: 170,
1465
+ L: 171,
1466
1466
  S: this,
1467
1467
  C: (f, a) => f(...a)
1468
1468
  });
1469
1469
  if (this._ctx.disposed) {
1470
1470
  log4("swarm event ignored for disposed swarm", void 0, {
1471
1471
  F: __dxlog_file4,
1472
- L: 173,
1472
+ L: 174,
1473
1473
  S: this,
1474
1474
  C: (f, a) => f(...a)
1475
1475
  });
@@ -1481,7 +1481,7 @@ var Swarm = class {
1481
1481
  peerId
1482
1482
  }, {
1483
1483
  F: __dxlog_file4,
1484
- L: 179,
1484
+ L: 180,
1485
1485
  S: this,
1486
1486
  C: (f, a) => f(...a)
1487
1487
  });
@@ -1496,7 +1496,7 @@ var Swarm = class {
1496
1496
  if (peer.connection?.state !== ConnectionState.CONNECTED) {
1497
1497
  void this._destroyPeer(peer.id, "peer left").catch((err) => log4.catch(err, void 0, {
1498
1498
  F: __dxlog_file4,
1499
- L: 190,
1499
+ L: 191,
1500
1500
  S: this,
1501
1501
  C: (f, a) => f(...a)
1502
1502
  }));
@@ -1506,7 +1506,7 @@ var Swarm = class {
1506
1506
  peer: swarmEvent.peerLeft.peer
1507
1507
  }, {
1508
1508
  F: __dxlog_file4,
1509
- L: 193,
1509
+ L: 194,
1510
1510
  S: this,
1511
1511
  C: (f, a) => f(...a)
1512
1512
  });
@@ -1519,14 +1519,14 @@ var Swarm = class {
1519
1519
  message
1520
1520
  }, {
1521
1521
  F: __dxlog_file4,
1522
- L: 202,
1522
+ L: 203,
1523
1523
  S: this,
1524
1524
  C: (f, a) => f(...a)
1525
1525
  });
1526
1526
  if (this._ctx.disposed) {
1527
1527
  log4("ignored for disposed swarm", void 0, {
1528
1528
  F: __dxlog_file4,
1529
- L: 204,
1529
+ L: 205,
1530
1530
  S: this,
1531
1531
  C: (f, a) => f(...a)
1532
1532
  });
@@ -1536,7 +1536,7 @@ var Swarm = class {
1536
1536
  }
1537
1537
  invariant4(message.author, void 0, {
1538
1538
  F: __dxlog_file4,
1539
- L: 209,
1539
+ L: 210,
1540
1540
  S: this,
1541
1541
  A: [
1542
1542
  "message.author",
@@ -1548,7 +1548,7 @@ var Swarm = class {
1548
1548
  message
1549
1549
  }, {
1550
1550
  F: __dxlog_file4,
1551
- L: 211,
1551
+ L: 212,
1552
1552
  S: this,
1553
1553
  C: (f, a) => f(...a)
1554
1554
  });
@@ -1561,7 +1561,7 @@ var Swarm = class {
1561
1561
  message
1562
1562
  }, {
1563
1563
  F: __dxlog_file4,
1564
- L: 215,
1564
+ L: 216,
1565
1565
  S: this,
1566
1566
  C: (f, a) => f(...a)
1567
1567
  });
@@ -1579,14 +1579,14 @@ var Swarm = class {
1579
1579
  message
1580
1580
  }, {
1581
1581
  F: __dxlog_file4,
1582
- L: 226,
1582
+ L: 227,
1583
1583
  S: this,
1584
1584
  C: (f, a) => f(...a)
1585
1585
  });
1586
1586
  if (this._ctx.disposed) {
1587
1587
  log4.info("ignored for offline swarm", void 0, {
1588
1588
  F: __dxlog_file4,
1589
- L: 228,
1589
+ L: 229,
1590
1590
  S: this,
1591
1591
  C: (f, a) => f(...a)
1592
1592
  });
@@ -1594,7 +1594,7 @@ var Swarm = class {
1594
1594
  }
1595
1595
  invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1596
1596
  F: __dxlog_file4,
1597
- L: 231,
1597
+ L: 232,
1598
1598
  S: this,
1599
1599
  A: [
1600
1600
  "message.recipient?.equals(this._ownPeerId)",
@@ -1603,7 +1603,7 @@ var Swarm = class {
1603
1603
  });
1604
1604
  invariant4(message.topic?.equals(this._topic), void 0, {
1605
1605
  F: __dxlog_file4,
1606
- L: 235,
1606
+ L: 236,
1607
1607
  S: this,
1608
1608
  A: [
1609
1609
  "message.topic?.equals(this._topic)",
@@ -1612,7 +1612,7 @@ var Swarm = class {
1612
1612
  });
1613
1613
  invariant4(message.author, void 0, {
1614
1614
  F: __dxlog_file4,
1615
- L: 236,
1615
+ L: 237,
1616
1616
  S: this,
1617
1617
  A: [
1618
1618
  "message.author",
@@ -1656,7 +1656,7 @@ var Swarm = class {
1656
1656
  peerId
1657
1657
  }, {
1658
1658
  F: __dxlog_file4,
1659
- L: 285,
1659
+ L: 286,
1660
1660
  S: this,
1661
1661
  C: (f, a) => f(...a)
1662
1662
  });
@@ -1680,7 +1680,7 @@ var Swarm = class {
1680
1680
  async _destroyPeer(peerId, reason) {
1681
1681
  invariant4(this._peers.has(peerId), void 0, {
1682
1682
  F: __dxlog_file4,
1683
- L: 307,
1683
+ L: 308,
1684
1684
  S: this,
1685
1685
  A: [
1686
1686
  "this._peers.has(peerId)",
@@ -1707,7 +1707,7 @@ var Swarm = class {
1707
1707
  } catch (err) {
1708
1708
  log4("initiation error", err, {
1709
1709
  F: __dxlog_file4,
1710
- L: 333,
1710
+ L: 334,
1711
1711
  S: this,
1712
1712
  C: (f, a) => f(...a)
1713
1713
  });
@@ -1735,7 +1735,7 @@ var Swarm = class {
1735
1735
  remoteId
1736
1736
  }, {
1737
1737
  F: __dxlog_file4,
1738
- L: 360,
1738
+ L: 361,
1739
1739
  S: this,
1740
1740
  C: (f, a) => f(...a)
1741
1741
  });
@@ -1752,7 +1752,7 @@ var Swarm = class {
1752
1752
  remoteId
1753
1753
  }, {
1754
1754
  F: __dxlog_file4,
1755
- L: 374,
1755
+ L: 375,
1756
1756
  S: this,
1757
1757
  C: (f, a) => f(...a)
1758
1758
  });
@@ -1762,7 +1762,7 @@ var Swarm = class {
1762
1762
  remoteId
1763
1763
  }, {
1764
1764
  F: __dxlog_file4,
1765
- L: 377,
1765
+ L: 378,
1766
1766
  S: this,
1767
1767
  C: (f, a) => f(...a)
1768
1768
  });
@@ -2619,32 +2619,26 @@ var createStreamDelay = (delay) => {
2619
2619
  });
2620
2620
  };
2621
2621
  var MemoryTransportFactory = {
2622
- createTransport: (params) => new MemoryTransport(params)
2622
+ createTransport: (options) => new MemoryTransport(options)
2623
2623
  };
2624
2624
  var MemoryTransport = class _MemoryTransport {
2625
2625
  static {
2626
2626
  // TODO(burdon): Remove static properties (inject context into constructor).
2627
2627
  this._connections = new ComplexMap7(PublicKey9.hash);
2628
2628
  }
2629
- constructor(options) {
2630
- this.options = options;
2631
- this.closed = new Event7();
2632
- this.connected = new Event7();
2633
- this.errors = new ErrorStream3();
2629
+ constructor(_options) {
2630
+ this._options = _options;
2634
2631
  this._instanceId = PublicKey9.random();
2635
2632
  this._remote = new Trigger();
2636
2633
  this._outgoingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2637
2634
  this._incomingDelay = createStreamDelay(MEMORY_TRANSPORT_DELAY);
2638
- this._destroyed = false;
2639
- log10("creating", void 0, {
2640
- F: __dxlog_file11,
2641
- L: 64,
2642
- S: this,
2643
- C: (f, a) => f(...a)
2644
- });
2635
+ this._closed = false;
2636
+ this.closed = new Event7();
2637
+ this.connected = new Event7();
2638
+ this.errors = new ErrorStream3();
2645
2639
  invariant10(!_MemoryTransport._connections.has(this._instanceId), "Duplicate memory connection", {
2646
2640
  F: __dxlog_file11,
2647
- L: 66,
2641
+ L: 64,
2648
2642
  S: this,
2649
2643
  A: [
2650
2644
  "!MemoryTransport._connections.has(this._instanceId)",
@@ -2652,41 +2646,52 @@ var MemoryTransport = class _MemoryTransport {
2652
2646
  ]
2653
2647
  });
2654
2648
  _MemoryTransport._connections.set(this._instanceId, this);
2655
- if (this.options.initiator) {
2656
- setTimeout(async () => {
2657
- log10("sending signal", void 0, {
2658
- F: __dxlog_file11,
2659
- L: 73,
2660
- S: this,
2661
- C: (f, a) => f(...a)
2662
- });
2663
- void this.options.sendSignal({
2649
+ }
2650
+ get isOpen() {
2651
+ return !this._closed;
2652
+ }
2653
+ async open() {
2654
+ log10("opening...", void 0, {
2655
+ F: __dxlog_file11,
2656
+ L: 74,
2657
+ S: this,
2658
+ C: (f, a) => f(...a)
2659
+ });
2660
+ if (this._options.initiator) {
2661
+ log10("sending signal", void 0, {
2662
+ F: __dxlog_file11,
2663
+ L: 78,
2664
+ S: this,
2665
+ C: (f, a) => f(...a)
2666
+ });
2667
+ try {
2668
+ await this._options.sendSignal({
2664
2669
  payload: {
2665
2670
  transportId: this._instanceId.toHex()
2666
2671
  }
2667
- }).catch((err) => {
2668
- if (!this._destroyed) {
2669
- this.errors.raise(err);
2670
- }
2671
2672
  });
2672
- });
2673
+ } catch (err) {
2674
+ if (!this._closed) {
2675
+ this.errors.raise(toError(err));
2676
+ }
2677
+ }
2673
2678
  } else {
2674
2679
  this._remote.wait({
2675
- timeout: this.options.timeout ?? 1e3
2680
+ timeout: this._options.timeout ?? 1e3
2676
2681
  }).then((remoteId) => {
2677
- if (this._destroyed) {
2682
+ if (this._closed) {
2678
2683
  return;
2679
2684
  }
2680
2685
  this._remoteInstanceId = remoteId;
2681
2686
  this._remoteConnection = _MemoryTransport._connections.get(this._remoteInstanceId);
2682
2687
  if (!this._remoteConnection) {
2683
- this._destroyed = true;
2688
+ this._closed = true;
2684
2689
  this.closed.emit();
2685
2690
  return;
2686
2691
  }
2687
2692
  invariant10(!this._remoteConnection._remoteConnection, `Remote already connected: ${this._remoteInstanceId}`, {
2688
2693
  F: __dxlog_file11,
2689
- L: 99,
2694
+ L: 104,
2690
2695
  S: this,
2691
2696
  A: [
2692
2697
  "!this._remoteConnection._remoteConnection",
@@ -2697,68 +2702,56 @@ var MemoryTransport = class _MemoryTransport {
2697
2702
  this._remoteConnection._remoteInstanceId = this._instanceId;
2698
2703
  log10("connected", void 0, {
2699
2704
  F: __dxlog_file11,
2700
- L: 103,
2705
+ L: 108,
2701
2706
  S: this,
2702
2707
  C: (f, a) => f(...a)
2703
2708
  });
2704
- this.options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection.options.stream).pipe(this._incomingDelay).pipe(this.options.stream);
2709
+ this._options.stream.pipe(this._outgoingDelay).pipe(this._remoteConnection._options.stream).pipe(this._incomingDelay).pipe(this._options.stream);
2705
2710
  this.connected.emit();
2706
2711
  this._remoteConnection.connected.emit();
2707
2712
  }).catch((err) => {
2708
- if (this._destroyed) {
2713
+ if (this._closed) {
2709
2714
  return;
2710
2715
  }
2711
2716
  this.errors.raise(err);
2712
2717
  });
2713
2718
  }
2714
2719
  }
2715
- async destroy() {
2716
- log10("closing", void 0, {
2720
+ async close() {
2721
+ log10("closing...", void 0, {
2717
2722
  F: __dxlog_file11,
2718
- L: 124,
2723
+ L: 129,
2719
2724
  S: this,
2720
2725
  C: (f, a) => f(...a)
2721
2726
  });
2722
- this._destroyed = true;
2727
+ this._closed = true;
2723
2728
  _MemoryTransport._connections.delete(this._instanceId);
2724
2729
  if (this._remoteConnection) {
2725
- log10("closing", void 0, {
2726
- F: __dxlog_file11,
2727
- L: 129,
2728
- S: this,
2729
- C: (f, a) => f(...a)
2730
- });
2731
- this._remoteConnection._destroyed = true;
2730
+ this._remoteConnection._closed = true;
2732
2731
  _MemoryTransport._connections.delete(this._remoteInstanceId);
2733
- this.options.stream.unpipe(this._incomingDelay);
2734
- this._incomingDelay.unpipe(this._remoteConnection.options.stream);
2735
- this._remoteConnection.options.stream.unpipe(this._outgoingDelay);
2736
- this._outgoingDelay.unpipe(this.options.stream);
2737
- this.options.stream.unpipe(this._outgoingDelay);
2732
+ this._options.stream.unpipe(this._incomingDelay);
2733
+ this._incomingDelay.unpipe(this._remoteConnection._options.stream);
2734
+ this._remoteConnection._options.stream.unpipe(this._outgoingDelay);
2735
+ this._outgoingDelay.unpipe(this._options.stream);
2736
+ this._options.stream.unpipe(this._outgoingDelay);
2738
2737
  this._remoteConnection.closed.emit();
2739
2738
  this._remoteConnection._remoteConnection = void 0;
2740
2739
  this._remoteConnection = void 0;
2741
- log10("closed", void 0, {
2742
- F: __dxlog_file11,
2743
- L: 150,
2744
- S: this,
2745
- C: (f, a) => f(...a)
2746
- });
2747
2740
  }
2748
2741
  this.closed.emit();
2749
2742
  log10("closed", void 0, {
2750
2743
  F: __dxlog_file11,
2751
- L: 154,
2744
+ L: 157,
2752
2745
  S: this,
2753
2746
  C: (f, a) => f(...a)
2754
2747
  });
2755
2748
  }
2756
- signal({ payload }) {
2749
+ async onSignal({ payload }) {
2757
2750
  log10("received signal", {
2758
2751
  payload
2759
2752
  }, {
2760
2753
  F: __dxlog_file11,
2761
- L: 158,
2754
+ L: 161,
2762
2755
  S: this,
2763
2756
  C: (f, a) => f(...a)
2764
2757
  });
@@ -2789,13 +2782,14 @@ _ts_decorate4([
2789
2782
  _ts_decorate4([
2790
2783
  logInfo3
2791
2784
  ], MemoryTransport.prototype, "_remoteInstanceId", void 0);
2785
+ var toError = (err) => err instanceof Error ? err : new Error(String(err));
2792
2786
 
2793
2787
  // packages/core/mesh/network-manager/src/transport/transport.ts
2794
2788
  var TransportKind;
2795
2789
  (function(TransportKind2) {
2796
2790
  TransportKind2["SIMPLE_PEER"] = "SIMPLE_PEER";
2797
- TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2798
2791
  TransportKind2["SIMPLE_PEER_PROXY"] = "SIMPLE_PEER_PROXY";
2792
+ TransportKind2["LIBDATACHANNEL"] = "LIBDATACHANNEL";
2799
2793
  TransportKind2["MEMORY"] = "MEMORY";
2800
2794
  TransportKind2["TCP"] = "TCP";
2801
2795
  })(TransportKind || (TransportKind = {}));
@@ -2818,12 +2812,21 @@ try {
2818
2812
 
2819
2813
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts
2820
2814
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport.ts";
2815
+ var createSimplePeerTransportFactory = (webrtcConfig) => ({
2816
+ createTransport: (options) => new SimplePeerTransport({
2817
+ ...options,
2818
+ webrtcConfig
2819
+ })
2820
+ });
2821
2821
  var SimplePeerTransport = class {
2822
+ get isOpen() {
2823
+ return this._piped && !this._closed;
2824
+ }
2822
2825
  /**
2823
2826
  * @params opts.config formatted as per https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection
2824
2827
  */
2825
- constructor(params) {
2826
- this.params = params;
2828
+ constructor(_params) {
2829
+ this._params = _params;
2827
2830
  this._closed = false;
2828
2831
  this._piped = false;
2829
2832
  this.closed = new Event8();
@@ -2834,30 +2837,30 @@ var SimplePeerTransport = class {
2834
2837
  id: this._instanceId
2835
2838
  }), {
2836
2839
  F: __dxlog_file12,
2837
- L: 44,
2840
+ L: 50,
2838
2841
  S: this,
2839
2842
  C: (f, a) => f(...a)
2840
2843
  });
2841
- log11("created connection", params, {
2844
+ log11("created connection", _params, {
2842
2845
  F: __dxlog_file12,
2843
- L: 45,
2846
+ L: 51,
2844
2847
  S: this,
2845
2848
  C: (f, a) => f(...a)
2846
2849
  });
2847
2850
  this._peer = new SimplePeerConstructor({
2848
2851
  channelName: "dxos.mesh.transport",
2849
- initiator: this.params.initiator,
2852
+ initiator: this._params.initiator,
2850
2853
  wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available")),
2851
- config: this.params.webrtcConfig
2854
+ config: this._params.webrtcConfig
2852
2855
  });
2853
2856
  this._peer.on("signal", async (data) => {
2854
2857
  log11("signal", data, {
2855
2858
  F: __dxlog_file12,
2856
- L: 54,
2859
+ L: 60,
2857
2860
  S: this,
2858
2861
  C: (f, a) => f(...a)
2859
2862
  });
2860
- await this.params.sendSignal({
2863
+ await this._params.sendSignal({
2861
2864
  payload: {
2862
2865
  data
2863
2866
  }
@@ -2866,22 +2869,22 @@ var SimplePeerTransport = class {
2866
2869
  this._peer.on("connect", () => {
2867
2870
  log11("connected", void 0, {
2868
2871
  F: __dxlog_file12,
2869
- L: 59,
2872
+ L: 65,
2870
2873
  S: this,
2871
2874
  C: (f, a) => f(...a)
2872
2875
  });
2873
- this.params.stream.pipe(this._peer).pipe(this.params.stream);
2876
+ this._params.stream.pipe(this._peer).pipe(this._params.stream);
2874
2877
  this._piped = true;
2875
2878
  this.connected.emit();
2876
2879
  });
2877
2880
  this._peer.on("close", async () => {
2878
2881
  log11("closed", void 0, {
2879
2882
  F: __dxlog_file12,
2880
- L: 66,
2883
+ L: 72,
2881
2884
  S: this,
2882
2885
  C: (f, a) => f(...a)
2883
2886
  });
2884
- await this.destroy();
2887
+ await this.close();
2885
2888
  });
2886
2889
  this._peer.on("error", async (err) => {
2887
2890
  if (typeof RTCError !== "undefined" && err instanceof RTCError) {
@@ -2892,7 +2895,7 @@ var SimplePeerTransport = class {
2892
2895
  err
2893
2896
  }, {
2894
2897
  F: __dxlog_file12,
2895
- L: 77,
2898
+ L: 83,
2896
2899
  S: this,
2897
2900
  C: (f, a) => f(...a)
2898
2901
  });
@@ -2901,7 +2904,7 @@ var SimplePeerTransport = class {
2901
2904
  } else if ("code" in err) {
2902
2905
  log11.info("simple-peer error", err, {
2903
2906
  F: __dxlog_file12,
2904
- L: 82,
2907
+ L: 88,
2905
2908
  S: this,
2906
2909
  C: (f, a) => f(...a)
2907
2910
  });
@@ -2931,7 +2934,7 @@ var SimplePeerTransport = class {
2931
2934
  } else {
2932
2935
  log11.info("unknown peer connection error", err, {
2933
2936
  F: __dxlog_file12,
2934
- L: 108,
2937
+ L: 114,
2935
2938
  S: this,
2936
2939
  C: (f, a) => f(...a)
2937
2940
  });
@@ -2941,11 +2944,11 @@ var SimplePeerTransport = class {
2941
2944
  if (typeof this._peer?._pc?.getStats === "function") {
2942
2945
  this._peer._pc.getStats().then((stats) => {
2943
2946
  log11.info("report after webrtc error", {
2944
- config: this.params.webrtcConfig,
2947
+ config: this._params.webrtcConfig,
2945
2948
  stats: Object.fromEntries(stats.entries())
2946
2949
  }, {
2947
2950
  F: __dxlog_file12,
2948
- L: 116,
2951
+ L: 122,
2949
2952
  S: this,
2950
2953
  C: (f, a) => f(...a)
2951
2954
  });
@@ -2954,18 +2957,18 @@ var SimplePeerTransport = class {
2954
2957
  } catch (err2) {
2955
2958
  log11.catch(err2, void 0, {
2956
2959
  F: __dxlog_file12,
2957
- L: 123,
2960
+ L: 129,
2958
2961
  S: this,
2959
2962
  C: (f, a) => f(...a)
2960
2963
  });
2961
2964
  }
2962
- await this.destroy();
2965
+ await this.close();
2963
2966
  });
2964
2967
  log11.trace("dxos.mesh.webrtc-transport.constructor", trace4.end({
2965
2968
  id: this._instanceId
2966
2969
  }), {
2967
2970
  F: __dxlog_file12,
2968
- L: 129,
2971
+ L: 135,
2969
2972
  S: this,
2970
2973
  C: (f, a) => f(...a)
2971
2974
  });
@@ -3023,10 +3026,12 @@ var SimplePeerTransport = class {
3023
3026
  }
3024
3027
  return `${rc.ip}:${rc.port}/${rc.protocol} ${rc.candidateType}`;
3025
3028
  }
3026
- async destroy() {
3029
+ async open() {
3030
+ }
3031
+ async close() {
3027
3032
  log11("closing...", void 0, {
3028
3033
  F: __dxlog_file12,
3029
- L: 192,
3034
+ L: 200,
3030
3035
  S: this,
3031
3036
  C: (f, a) => f(...a)
3032
3037
  });
@@ -3039,12 +3044,12 @@ var SimplePeerTransport = class {
3039
3044
  this.closed.emit();
3040
3045
  log11("closed", void 0, {
3041
3046
  F: __dxlog_file12,
3042
- L: 200,
3047
+ L: 208,
3043
3048
  S: this,
3044
3049
  C: (f, a) => f(...a)
3045
3050
  });
3046
3051
  }
3047
- signal(signal) {
3052
+ async onSignal(signal) {
3048
3053
  if (this._closed) {
3049
3054
  return;
3050
3055
  }
@@ -3053,16 +3058,10 @@ var SimplePeerTransport = class {
3053
3058
  }
3054
3059
  _disconnectStreams() {
3055
3060
  if (this._piped) {
3056
- this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
3061
+ this._params.stream.unpipe?.(this._peer)?.unpipe?.(this._params.stream);
3057
3062
  }
3058
3063
  }
3059
3064
  };
3060
- var createSimplePeerTransportFactory = (webrtcConfig) => ({
3061
- createTransport: (params) => new SimplePeerTransport({
3062
- ...params,
3063
- webrtcConfig
3064
- })
3065
- });
3066
3065
 
3067
3066
  // packages/core/mesh/network-manager/src/transport/simplepeer-transport-service.ts
3068
3067
  import { Duplex } from "@dxos/node-std/stream";
@@ -3111,6 +3110,7 @@ var SimplePeerTransportService = class {
3111
3110
  });
3112
3111
  }
3113
3112
  });
3113
+ void transport.open();
3114
3114
  next({
3115
3115
  connection: {
3116
3116
  state: ConnectionState3.CONNECTING
@@ -3154,19 +3154,19 @@ var SimplePeerTransportService = class {
3154
3154
  async sendSignal({ proxyId, signal }) {
3155
3155
  invariant12(this.transports.has(proxyId), void 0, {
3156
3156
  F: __dxlog_file13,
3157
- L: 116,
3157
+ L: 119,
3158
3158
  S: this,
3159
3159
  A: [
3160
3160
  "this.transports.has(proxyId)",
3161
3161
  ""
3162
3162
  ]
3163
3163
  });
3164
- await this.transports.get(proxyId).transport.signal(signal);
3164
+ await this.transports.get(proxyId).transport.onSignal(signal);
3165
3165
  }
3166
3166
  async getDetails({ proxyId }) {
3167
3167
  invariant12(this.transports.has(proxyId), void 0, {
3168
3168
  F: __dxlog_file13,
3169
- L: 121,
3169
+ L: 124,
3170
3170
  S: this,
3171
3171
  A: [
3172
3172
  "this.transports.has(proxyId)",
@@ -3180,7 +3180,7 @@ var SimplePeerTransportService = class {
3180
3180
  async getStats({ proxyId }) {
3181
3181
  invariant12(this.transports.has(proxyId), void 0, {
3182
3182
  F: __dxlog_file13,
3183
- L: 126,
3183
+ L: 129,
3184
3184
  S: this,
3185
3185
  A: [
3186
3186
  "this.transports.has(proxyId)",
@@ -3195,14 +3195,14 @@ var SimplePeerTransportService = class {
3195
3195
  if (this.transports.get(proxyId)?.state !== "OPEN") {
3196
3196
  log12.debug("transport is closed", void 0, {
3197
3197
  F: __dxlog_file13,
3198
- L: 132,
3198
+ L: 135,
3199
3199
  S: this,
3200
3200
  C: (f, a) => f(...a)
3201
3201
  });
3202
3202
  }
3203
3203
  invariant12(this.transports.has(proxyId), void 0, {
3204
3204
  F: __dxlog_file13,
3205
- L: 134,
3205
+ L: 137,
3206
3206
  S: this,
3207
3207
  A: [
3208
3208
  "this.transports.has(proxyId)",
@@ -3218,14 +3218,14 @@ var SimplePeerTransportService = class {
3218
3218
  }
3219
3219
  }
3220
3220
  async close({ proxyId }) {
3221
- await this.transports.get(proxyId)?.transport.destroy();
3221
+ await this.transports.get(proxyId)?.transport.close();
3222
3222
  await this.transports.get(proxyId)?.stream.end();
3223
3223
  if (this.transports.get(proxyId)) {
3224
3224
  this.transports.get(proxyId).state = "CLOSED";
3225
3225
  }
3226
3226
  log12("Closed.", void 0, {
3227
3227
  F: __dxlog_file13,
3228
- L: 150,
3228
+ L: 153,
3229
3229
  S: this,
3230
3230
  C: (f, a) => f(...a)
3231
3231
  });
@@ -3244,11 +3244,12 @@ import { ConnectionResetError as ConnectionResetError3, TimeoutError as TimeoutE
3244
3244
  import { ConnectionState as ConnectionState4 } from "@dxos/protocols/proto/dxos/mesh/bridge";
3245
3245
  import { arrayToBuffer } from "@dxos/util";
3246
3246
  var __dxlog_file14 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/simplepeer-transport-proxy.ts";
3247
+ var RPC_TIMEOUT = 1e4;
3247
3248
  var RESP_MIN_THRESHOLD = 500;
3248
3249
  var TIMEOUT_THRESHOLD = 10;
3249
3250
  var SimplePeerTransportProxy = class {
3250
- constructor(_params) {
3251
- this._params = _params;
3251
+ constructor(_options) {
3252
+ this._options = _options;
3252
3253
  this._proxyId = PublicKey12.random();
3253
3254
  this._ctx = new Context6();
3254
3255
  this._timeoutCount = 0;
@@ -3256,15 +3257,22 @@ var SimplePeerTransportProxy = class {
3256
3257
  this.connected = new Event9();
3257
3258
  this.errors = new ErrorStream5();
3258
3259
  this._closed = false;
3259
- this._serviceStream = this._params.bridgeService.open({
3260
+ }
3261
+ get isOpen() {
3262
+ return !this._closed;
3263
+ }
3264
+ async open() {
3265
+ this._serviceStream = this._options.bridgeService.open({
3260
3266
  proxyId: this._proxyId,
3261
- initiator: this._params.initiator
3267
+ initiator: this._options.initiator
3268
+ }, {
3269
+ timeout: RPC_TIMEOUT
3262
3270
  });
3263
3271
  this._serviceStream.waitUntilReady().then(() => {
3264
3272
  this._serviceStream.subscribe(async (event) => {
3265
3273
  log13("SimplePeerTransportProxy: event", event, {
3266
3274
  F: __dxlog_file14,
3267
- L: 59,
3275
+ L: 66,
3268
3276
  S: this,
3269
3277
  C: (f, a) => f(...a)
3270
3278
  });
@@ -3279,16 +3287,16 @@ var SimplePeerTransportProxy = class {
3279
3287
  const proxyStream = new Writable({
3280
3288
  write: (chunk, _, callback) => {
3281
3289
  const then = performance.now();
3282
- this._params.bridgeService.sendData({
3290
+ this._options.bridgeService.sendData({
3283
3291
  proxyId: this._proxyId,
3284
3292
  payload: chunk
3285
3293
  }, {
3286
- timeout: 1e4
3294
+ timeout: RPC_TIMEOUT
3287
3295
  }).then(() => {
3288
3296
  if (performance.now() - then > RESP_MIN_THRESHOLD) {
3289
3297
  log13("slow response, delaying callback", void 0, {
3290
3298
  F: __dxlog_file14,
3291
- L: 83,
3299
+ L: 90,
3292
3300
  S: this,
3293
3301
  C: (f, a) => f(...a)
3294
3302
  });
@@ -3300,11 +3308,11 @@ var SimplePeerTransportProxy = class {
3300
3308
  }, (err) => {
3301
3309
  if (err instanceof TimeoutError3 || err.constructor.name === "TimeoutError") {
3302
3310
  if (this._timeoutCount++ > TIMEOUT_THRESHOLD) {
3303
- throw new TimeoutError3(`too many timeoutes (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
3311
+ throw new TimeoutError3(`too many timeouts (${this._timeoutCount} > ${TIMEOUT_THRESHOLD}`);
3304
3312
  } else {
3305
3313
  log13("timeout error, but still invoking callback", void 0, {
3306
3314
  F: __dxlog_file14,
3307
- L: 95,
3315
+ L: 102,
3308
3316
  S: this,
3309
3317
  C: (f, a) => f(...a)
3310
3318
  });
@@ -3313,7 +3321,7 @@ var SimplePeerTransportProxy = class {
3313
3321
  } else {
3314
3322
  log13.catch(err, void 0, {
3315
3323
  F: __dxlog_file14,
3316
- L: 99,
3324
+ L: 106,
3317
3325
  S: this,
3318
3326
  C: (f, a) => f(...a)
3319
3327
  });
@@ -3326,19 +3334,50 @@ var SimplePeerTransportProxy = class {
3326
3334
  err
3327
3335
  }, {
3328
3336
  F: __dxlog_file14,
3329
- L: 107,
3337
+ L: 114,
3330
3338
  S: this,
3331
3339
  C: (f, a) => f(...a)
3332
3340
  });
3333
3341
  });
3334
- this._params.stream.pipe(proxyStream);
3342
+ this._options.stream.pipe(proxyStream);
3335
3343
  }, (error) => log13.catch(error, void 0, {
3336
3344
  F: __dxlog_file14,
3337
- L: 112,
3345
+ L: 119,
3338
3346
  S: this,
3339
3347
  C: (f, a) => f(...a)
3340
3348
  }));
3341
3349
  }
3350
+ async close() {
3351
+ await this._ctx.dispose();
3352
+ if (this._closed) {
3353
+ return;
3354
+ }
3355
+ await this._serviceStream.close();
3356
+ try {
3357
+ await this._options.bridgeService.close({
3358
+ proxyId: this._proxyId
3359
+ }, {
3360
+ timeout: RPC_TIMEOUT
3361
+ });
3362
+ } catch (err) {
3363
+ log13.catch(err, void 0, {
3364
+ F: __dxlog_file14,
3365
+ L: 134,
3366
+ S: this,
3367
+ C: (f, a) => f(...a)
3368
+ });
3369
+ }
3370
+ this.closed.emit();
3371
+ this._closed = true;
3372
+ }
3373
+ async onSignal(signal) {
3374
+ this._options.bridgeService.sendSignal({
3375
+ proxyId: this._proxyId,
3376
+ signal
3377
+ }, {
3378
+ timeout: RPC_TIMEOUT
3379
+ }).catch((err) => this.errors.raise(decodeError(err)));
3380
+ }
3342
3381
  async _handleConnection(connectionEvent) {
3343
3382
  if (connectionEvent.error) {
3344
3383
  this.errors.raise(decodeError(connectionEvent.error));
@@ -3349,55 +3388,31 @@ var SimplePeerTransportProxy = class {
3349
3388
  break;
3350
3389
  }
3351
3390
  case ConnectionState4.CLOSED: {
3352
- await this.destroy();
3391
+ await this.close();
3353
3392
  break;
3354
3393
  }
3355
3394
  }
3356
3395
  }
3357
3396
  _handleData(dataEvent) {
3358
- this._params.stream.write(arrayToBuffer(dataEvent.payload));
3397
+ this._options.stream.write(arrayToBuffer(dataEvent.payload));
3359
3398
  }
3360
3399
  async _handleSignal(signalEvent) {
3361
- await this._params.sendSignal(signalEvent.payload);
3362
- }
3363
- signal(signal) {
3364
- this._params.bridgeService.sendSignal({
3365
- proxyId: this._proxyId,
3366
- signal
3367
- }).catch((err) => this.errors.raise(decodeError(err)));
3400
+ await this._options.sendSignal(signalEvent.payload);
3368
3401
  }
3369
3402
  async getDetails() {
3370
- return (await this._params.bridgeService.getDetails({
3403
+ return (await this._options.bridgeService.getDetails({
3371
3404
  proxyId: this._proxyId
3405
+ }, {
3406
+ timeout: RPC_TIMEOUT
3372
3407
  })).details;
3373
3408
  }
3374
3409
  async getStats() {
3375
- return (await this._params.bridgeService.getStats({
3410
+ return (await this._options.bridgeService.getStats({
3376
3411
  proxyId: this._proxyId
3412
+ }, {
3413
+ timeout: RPC_TIMEOUT
3377
3414
  })).stats;
3378
3415
  }
3379
- // TODO(burdon): Move open from constructor.
3380
- async destroy() {
3381
- await this._ctx.dispose();
3382
- if (this._closed) {
3383
- return;
3384
- }
3385
- await this._serviceStream.close();
3386
- try {
3387
- await this._params.bridgeService.close({
3388
- proxyId: this._proxyId
3389
- });
3390
- } catch (err) {
3391
- log13.catch(err, void 0, {
3392
- F: __dxlog_file14,
3393
- L: 171,
3394
- S: this,
3395
- C: (f, a) => f(...a)
3396
- });
3397
- }
3398
- this.closed.emit();
3399
- this._closed = true;
3400
- }
3401
3416
  /**
3402
3417
  * Called when underlying proxy service becomes unavailable.
3403
3418
  */
@@ -3426,7 +3441,7 @@ var SimplePeerTransportProxyFactory = class {
3426
3441
  createTransport(options) {
3427
3442
  invariant13(this._bridgeService, "SimplePeerTransportProxyFactory is not ready to open connections", {
3428
3443
  F: __dxlog_file14,
3429
- L: 208,
3444
+ L: 218,
3430
3445
  S: this,
3431
3446
  A: [
3432
3447
  "this._bridgeService",
@@ -3463,6 +3478,7 @@ var decodeError = (err) => {
3463
3478
  import { Duplex as Duplex2 } from "stream";
3464
3479
  import { Event as Event10, Trigger as Trigger2, synchronized as synchronized4 } from "@dxos/async";
3465
3480
  import { ErrorStream as ErrorStream6 } from "@dxos/debug";
3481
+ import { invariant as invariant14 } from "@dxos/invariant";
3466
3482
  import { log as log14 } from "@dxos/log";
3467
3483
  function _ts_decorate5(decorators, target, key, desc) {
3468
3484
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -3478,163 +3494,203 @@ var __dxlog_file15 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-man
3478
3494
  var DATACHANNEL_LABEL = "dxos.mesh.transport";
3479
3495
  var MAX_BUFFERED_AMOUNT = 64 * 1024;
3480
3496
  var MAX_MESSAGE_SIZE = 64 * 1024;
3497
+ var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3498
+ createTransport: (options) => new LibDataChannelTransport({
3499
+ ...options,
3500
+ webrtcConfig
3501
+ })
3502
+ });
3481
3503
  var LibDataChannelTransport = class _LibDataChannelTransport {
3482
3504
  static {
3483
3505
  this._instanceCount = 0;
3484
3506
  }
3485
- constructor(params) {
3486
- this.params = params;
3507
+ constructor(_options) {
3508
+ this._options = _options;
3487
3509
  this._closed = false;
3488
- this.closed = new Event10();
3489
3510
  this._connected = false;
3511
+ this._writeCallback = null;
3512
+ this._readyForCandidates = new Trigger2();
3513
+ this.closed = new Event10();
3490
3514
  this.connected = new Event10();
3491
3515
  this.errors = new ErrorStream6();
3492
- this._readyForCandidates = new Trigger2();
3493
- this._writeCallback = null;
3494
- this._peer = (async () => {
3495
- const { RTCPeerConnection } = (await importESM("node-datachannel/polyfill")).default;
3496
- if (this._closed) {
3497
- this.errors.raise(new Error("connection already closed"));
3498
- }
3499
- if (params.webrtcConfig) {
3500
- params.webrtcConfig.iceServers = params.webrtcConfig.iceServers ?? [];
3501
- } else {
3502
- params.webrtcConfig = {
3503
- iceServers: []
3504
- };
3516
+ }
3517
+ get isOpen() {
3518
+ return !!this._peer && !this._closed;
3519
+ }
3520
+ async open() {
3521
+ if (this._closed) {
3522
+ this.errors.raise(new Error("connection already closed"));
3523
+ }
3524
+ const { RTCPeerConnection } = (await importESM("node-datachannel/polyfill")).default;
3525
+ if (this._options.webrtcConfig) {
3526
+ this._options.webrtcConfig.iceServers = this._options.webrtcConfig.iceServers ?? [];
3527
+ } else {
3528
+ this._options.webrtcConfig = {
3529
+ iceServers: []
3530
+ };
3531
+ }
3532
+ this._peer = new RTCPeerConnection(this._options.webrtcConfig);
3533
+ this._peer.onicecandidateerror = (event) => {
3534
+ log14.error("peer.onicecandidateerror", {
3535
+ event
3536
+ }, {
3537
+ F: __dxlog_file15,
3538
+ L: 77,
3539
+ S: this,
3540
+ C: (f, a) => f(...a)
3541
+ });
3542
+ };
3543
+ this._peer.onconnectionstatechange = (event) => {
3544
+ log14.debug("peer.onconnectionstatechange", {
3545
+ event,
3546
+ peerConnectionState: this._peer?.connectionState,
3547
+ transportConnectionState: this._connected
3548
+ }, {
3549
+ F: __dxlog_file15,
3550
+ L: 81,
3551
+ S: this,
3552
+ C: (f, a) => f(...a)
3553
+ });
3554
+ };
3555
+ this._peer.onicecandidate = async (event) => {
3556
+ log14.debug("peer.onicecandidate", {
3557
+ event
3558
+ }, {
3559
+ F: __dxlog_file15,
3560
+ L: 91,
3561
+ S: this,
3562
+ C: (f, a) => f(...a)
3563
+ });
3564
+ if (event.candidate) {
3565
+ try {
3566
+ await this._options.sendSignal({
3567
+ payload: {
3568
+ data: {
3569
+ type: "candidate",
3570
+ candidate: {
3571
+ candidate: event.candidate.candidate,
3572
+ // These fields never seem to be not null, but connecting to Chrome doesn't work if they are.
3573
+ sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
3574
+ sdpMid: event.candidate.sdpMid ?? 0
3575
+ }
3576
+ }
3577
+ }
3578
+ });
3579
+ } catch (err) {
3580
+ log14.info("signaling error", {
3581
+ err
3582
+ }, {
3583
+ F: __dxlog_file15,
3584
+ L: 108,
3585
+ S: this,
3586
+ C: (f, a) => f(...a)
3587
+ });
3588
+ }
3505
3589
  }
3506
- const peer = new RTCPeerConnection(params.webrtcConfig);
3507
- _LibDataChannelTransport._instanceCount++;
3508
- peer.onicecandidateerror = (event) => {
3509
- log14.error("peer.onicecandidateerror", {
3510
- event
3590
+ };
3591
+ if (this._options.initiator) {
3592
+ invariant14(this._peer, "not open", {
3593
+ F: __dxlog_file15,
3594
+ L: 114,
3595
+ S: this,
3596
+ A: [
3597
+ "this._peer",
3598
+ "'not open'"
3599
+ ]
3600
+ });
3601
+ this._peer.createOffer().then(async (offer) => {
3602
+ if (this._closed) {
3603
+ return;
3604
+ }
3605
+ if (this._peer?.connectionState !== "connecting") {
3606
+ log14.error("peer not connecting", {
3607
+ peer: this._peer
3608
+ }, {
3609
+ F: __dxlog_file15,
3610
+ L: 125,
3611
+ S: this,
3612
+ C: (f, a) => f(...a)
3613
+ });
3614
+ this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3615
+ }
3616
+ log14.debug("creating offer", {
3617
+ peer: this._peer,
3618
+ offer
3511
3619
  }, {
3512
3620
  F: __dxlog_file15,
3513
- L: 63,
3621
+ L: 129,
3514
3622
  S: this,
3515
3623
  C: (f, a) => f(...a)
3516
3624
  });
3517
- };
3518
- peer.onconnectionstatechange = (event) => {
3519
- log14.debug("peer.onconnectionstatechange", {
3520
- event,
3521
- peerConnectionState: peer.connectionState,
3522
- transportConnectionState: this._connected
3523
- }, {
3524
- F: __dxlog_file15,
3525
- L: 67,
3526
- S: this,
3527
- C: (f, a) => f(...a)
3625
+ await this._peer.setLocalDescription(offer);
3626
+ await this._options.sendSignal({
3627
+ payload: {
3628
+ data: {
3629
+ type: offer.type,
3630
+ sdp: offer.sdp
3631
+ }
3632
+ }
3528
3633
  });
3634
+ }).catch((err) => {
3635
+ this.errors.raise(err);
3636
+ });
3637
+ this._handleChannel(this._peer.createDataChannel(DATACHANNEL_LABEL));
3638
+ log14.debug("created data channel", void 0, {
3639
+ F: __dxlog_file15,
3640
+ L: 139,
3641
+ S: this,
3642
+ C: (f, a) => f(...a)
3643
+ });
3644
+ this._peer.ondatachannel = () => {
3645
+ this.errors.raise(new Error("unexpected ondatachannel event for initiator"));
3529
3646
  };
3530
- peer.onicecandidate = async (event) => {
3531
- log14.debug("peer.onicecandidate", {
3647
+ } else {
3648
+ this._peer.ondatachannel = (event) => {
3649
+ log14.debug("peer.ondatachannel (non-initiator)", {
3532
3650
  event
3533
3651
  }, {
3534
3652
  F: __dxlog_file15,
3535
- L: 76,
3653
+ L: 145,
3536
3654
  S: this,
3537
3655
  C: (f, a) => f(...a)
3538
3656
  });
3539
- if (event.candidate) {
3540
- try {
3541
- await params.sendSignal({
3542
- payload: {
3543
- data: {
3544
- type: "candidate",
3545
- candidate: {
3546
- candidate: event.candidate.candidate,
3547
- // these fields never seem to be not null, but connecting to Chrome doesn't work if they are
3548
- sdpMLineIndex: event.candidate.sdpMLineIndex ?? 0,
3549
- sdpMid: event.candidate.sdpMid ?? 0
3550
- }
3551
- }
3552
- }
3553
- });
3554
- } catch (err) {
3555
- log14.info("signaling errror", {
3556
- err
3557
- }, {
3558
- F: __dxlog_file15,
3559
- L: 93,
3560
- S: this,
3561
- C: (f, a) => f(...a)
3562
- });
3563
- }
3657
+ if (event.channel.label !== DATACHANNEL_LABEL) {
3658
+ this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
3564
3659
  }
3660
+ this._handleChannel(event.channel);
3565
3661
  };
3566
- if (params.initiator) {
3567
- peer.createOffer().then(async (offer) => {
3568
- if (this._closed) {
3569
- return;
3570
- }
3571
- if (peer.connectionState !== "connecting") {
3572
- log14.error("i am initiator but peer not in state connecting", {
3573
- peer
3574
- }, {
3575
- F: __dxlog_file15,
3576
- L: 106,
3577
- S: this,
3578
- C: (f, a) => f(...a)
3579
- });
3580
- this.errors.raise(new Error("invalid state: peer is initiator, but other peer not in state connecting"));
3581
- }
3582
- log14.debug(`im the initiator, creating offer, peer is in state ${peer.connectionState}`, {
3583
- offer
3584
- }, {
3585
- F: __dxlog_file15,
3586
- L: 109,
3587
- S: this,
3588
- C: (f, a) => f(...a)
3589
- });
3590
- await peer.setLocalDescription(offer);
3591
- await params.sendSignal({
3592
- payload: {
3593
- data: {
3594
- type: offer.type,
3595
- sdp: offer.sdp
3596
- }
3597
- }
3598
- });
3599
- return offer;
3600
- }).catch((err) => {
3601
- this.errors.raise(err);
3602
- });
3603
- this.handleChannel(peer.createDataChannel(DATACHANNEL_LABEL));
3604
- log14.debug("created data channel", void 0, {
3605
- F: __dxlog_file15,
3606
- L: 118,
3607
- S: this,
3608
- C: (f, a) => f(...a)
3609
- });
3610
- peer.ondatachannel = (event) => {
3611
- this.errors.raise(new Error("got ondatachannel when i am the initiator?"));
3612
- };
3613
- } else {
3614
- peer.ondatachannel = (event) => {
3615
- log14.debug("peer.ondatachannel (non-initiator)", {
3616
- event
3617
- }, {
3618
- F: __dxlog_file15,
3619
- L: 124,
3620
- S: this,
3621
- C: (f, a) => f(...a)
3622
- });
3623
- if (event.channel.label !== DATACHANNEL_LABEL) {
3624
- this.errors.raise(new Error(`unexpected channel label ${event.channel.label}`));
3625
- }
3626
- this.handleChannel(event.channel);
3627
- };
3628
- }
3629
- return peer;
3630
- })();
3662
+ }
3663
+ _LibDataChannelTransport._instanceCount++;
3631
3664
  }
3632
- handleChannel(dataChannel) {
3665
+ async close() {
3666
+ await this._close();
3667
+ if (--_LibDataChannelTransport._instanceCount === 0) {
3668
+ (await importESM("node-datachannel")).cleanup();
3669
+ }
3670
+ }
3671
+ async _close() {
3672
+ if (this._closed) {
3673
+ return;
3674
+ }
3675
+ await this._disconnectStreams();
3676
+ try {
3677
+ this._peer?.close();
3678
+ } catch (err) {
3679
+ this.errors.raise(err);
3680
+ }
3681
+ this._peer = void 0;
3682
+ this._closed = true;
3683
+ this.closed.emit();
3684
+ }
3685
+ /**
3686
+ * Handle data channel events.
3687
+ */
3688
+ _handleChannel(dataChannel) {
3633
3689
  this._channel = dataChannel;
3634
3690
  this._channel.onopen = () => {
3635
- log14.debug("dataChannel.onopen", void 0, {
3691
+ log14.debug("channel.onopen", void 0, {
3636
3692
  F: __dxlog_file15,
3637
- L: 140,
3693
+ L: 190,
3638
3694
  S: this,
3639
3695
  C: (f, a) => f(...a)
3640
3696
  });
@@ -3653,9 +3709,9 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3653
3709
  }
3654
3710
  if (this._channel.bufferedAmount > MAX_BUFFERED_AMOUNT) {
3655
3711
  if (this._writeCallback !== null) {
3656
- log14.error("consumer trying to write before we're ready for more data", void 0, {
3712
+ log14.error("consumer trying to write before we are ready for more data", void 0, {
3657
3713
  F: __dxlog_file15,
3658
- L: 157,
3714
+ L: 207,
3659
3715
  S: this,
3660
3716
  C: (f, a) => f(...a)
3661
3717
  });
@@ -3666,26 +3722,31 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3666
3722
  }
3667
3723
  }
3668
3724
  });
3669
- duplex.pipe(this.params.stream).pipe(duplex);
3725
+ duplex.pipe(this._options.stream).pipe(duplex);
3670
3726
  this._stream = duplex;
3671
3727
  this._connected = true;
3672
3728
  this.connected.emit();
3673
3729
  };
3674
- this._channel.onerror = async (err) => {
3675
- this.errors.raise(new Error("channel error: " + err.toString()));
3676
- await this._close();
3677
- };
3678
3730
  this._channel.onclose = async (err) => {
3679
- log14.info("channel onclose", {
3731
+ log14.info("channel.onclose", {
3680
3732
  err
3681
3733
  }, {
3682
3734
  F: __dxlog_file15,
3683
- L: 177,
3735
+ L: 223,
3684
3736
  S: this,
3685
3737
  C: (f, a) => f(...a)
3686
3738
  });
3687
3739
  await this._close();
3688
3740
  };
3741
+ this._channel.onerror = async (err) => {
3742
+ this.errors.raise(new Error("channel error: " + err.toString()));
3743
+ await this._close();
3744
+ };
3745
+ this._channel.onbufferedamountlow = () => {
3746
+ const cb = this._writeCallback;
3747
+ this._writeCallback = null;
3748
+ cb?.();
3749
+ };
3689
3750
  this._channel.onmessage = (event) => {
3690
3751
  let data = event.data;
3691
3752
  if (data instanceof ArrayBuffer) {
@@ -3693,37 +3754,27 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3693
3754
  }
3694
3755
  this._stream.push(data);
3695
3756
  };
3696
- this._channel.onbufferedamountlow = () => {
3697
- const cb = this._writeCallback;
3698
- this._writeCallback = null;
3699
- cb?.();
3700
- };
3701
3757
  }
3702
- async _close() {
3703
- if (this._closed) {
3704
- return;
3705
- }
3706
- await this._disconnectStreams();
3707
- const peer = await this._peer;
3758
+ async onSignal(signal) {
3759
+ invariant14(this._peer, "not open", {
3760
+ F: __dxlog_file15,
3761
+ L: 249,
3762
+ S: this,
3763
+ A: [
3764
+ "this._peer",
3765
+ "'not open'"
3766
+ ]
3767
+ });
3708
3768
  try {
3709
- peer.close();
3710
- } catch (err) {
3711
- this.errors.raise(err);
3712
- }
3713
- this._closed = true;
3714
- this.closed.emit();
3715
- }
3716
- signal(signal) {
3717
- this._peer.then(async (peer) => {
3718
3769
  const data = signal.payload.data;
3719
3770
  switch (data.type) {
3720
3771
  case "offer": {
3721
- if ((await this._peer).connectionState !== "new") {
3772
+ if (this._peer.connectionState !== "new") {
3722
3773
  log14.error("received offer but peer not in state new", {
3723
- peer
3774
+ peer: this._peer
3724
3775
  }, {
3725
3776
  F: __dxlog_file15,
3726
- L: 218,
3777
+ L: 256,
3727
3778
  S: this,
3728
3779
  C: (f, a) => f(...a)
3729
3780
  });
@@ -3731,13 +3782,13 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3731
3782
  break;
3732
3783
  }
3733
3784
  try {
3734
- await peer.setRemoteDescription({
3785
+ await this._peer.setRemoteDescription({
3735
3786
  type: data.type,
3736
3787
  sdp: data.sdp
3737
3788
  });
3738
- const answer = await peer.createAnswer();
3739
- await peer.setLocalDescription(answer);
3740
- await this.params.sendSignal({
3789
+ const answer = await this._peer.createAnswer();
3790
+ await this._peer.setLocalDescription(answer);
3791
+ await this._options.sendSignal({
3741
3792
  payload: {
3742
3793
  data: {
3743
3794
  type: answer.type,
@@ -3747,11 +3798,11 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3747
3798
  });
3748
3799
  this._readyForCandidates.wake();
3749
3800
  } catch (err) {
3750
- log14.error("can't handle offer from signalling server", {
3801
+ log14.error("cannot handle offer from signalling server", {
3751
3802
  err
3752
3803
  }, {
3753
3804
  F: __dxlog_file15,
3754
- L: 229,
3805
+ L: 268,
3755
3806
  S: this,
3756
3807
  C: (f, a) => f(...a)
3757
3808
  });
@@ -3761,17 +3812,17 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3761
3812
  }
3762
3813
  case "answer":
3763
3814
  try {
3764
- await peer.setRemoteDescription({
3815
+ await this._peer.setRemoteDescription({
3765
3816
  type: data.type,
3766
3817
  sdp: data.sdp
3767
3818
  });
3768
3819
  this._readyForCandidates.wake();
3769
3820
  } catch (err) {
3770
- log14.error("can't handle answer from signalling server", {
3821
+ log14.error("cannot handle answer from signalling server", {
3771
3822
  err
3772
3823
  }, {
3773
3824
  F: __dxlog_file15,
3774
- L: 240,
3825
+ L: 279,
3775
3826
  S: this,
3776
3827
  C: (f, a) => f(...a)
3777
3828
  });
@@ -3780,7 +3831,7 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3780
3831
  break;
3781
3832
  case "candidate":
3782
3833
  await this._readyForCandidates.wait();
3783
- await peer.addIceCandidate({
3834
+ await this._peer.addIceCandidate({
3784
3835
  candidate: data.candidate.candidate
3785
3836
  });
3786
3837
  break;
@@ -3790,20 +3841,20 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3790
3841
  signal
3791
3842
  }, {
3792
3843
  F: __dxlog_file15,
3793
- L: 251,
3844
+ L: 290,
3794
3845
  S: this,
3795
3846
  C: (f, a) => f(...a)
3796
3847
  });
3797
3848
  this.errors.raise(new Error(`unhandled signal type ${data.type}`));
3798
3849
  }
3799
- }).catch((err) => {
3850
+ } catch (err) {
3800
3851
  log14.catch(err, void 0, {
3801
3852
  F: __dxlog_file15,
3802
- L: 256,
3853
+ L: 294,
3803
3854
  S: this,
3804
3855
  C: (f, a) => f(...a)
3805
3856
  });
3806
- });
3857
+ }
3807
3858
  }
3808
3859
  async getDetails() {
3809
3860
  const stats = await this._getStats();
@@ -3816,26 +3867,6 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3816
3867
  }
3817
3868
  return `${rc.ip}:${rc.port} ${rc.candidateType}`;
3818
3869
  }
3819
- async _getStats() {
3820
- return this._peer.then(async (peer) => {
3821
- const stats = await peer.getStats();
3822
- const statsEntries = Array.from(stats.entries());
3823
- const transport = statsEntries.filter((s) => s[1].type === "transport")[0][1];
3824
- const candidatePair = statsEntries.filter((s) => s[0] === transport.selectedCandidatePairId);
3825
- let selectedCandidatePair;
3826
- let remoteCandidate;
3827
- if (candidatePair.length > 0) {
3828
- selectedCandidatePair = candidatePair[0][1];
3829
- remoteCandidate = statsEntries.filter((s) => s[0] === selectedCandidatePair.remoteCandidateId)[0][1];
3830
- }
3831
- return {
3832
- transport,
3833
- selectedCandidatePair,
3834
- remoteCandidate,
3835
- raw: Object.fromEntries(stats)
3836
- };
3837
- });
3838
- }
3839
3870
  async getStats() {
3840
3871
  const stats = await this._getStats();
3841
3872
  if (!stats) {
@@ -3855,25 +3886,40 @@ var LibDataChannelTransport = class _LibDataChannelTransport {
3855
3886
  rawStats: stats.raw
3856
3887
  };
3857
3888
  }
3858
- async destroy() {
3859
- await this._close();
3860
- if (--_LibDataChannelTransport._instanceCount === 0) {
3861
- (await importESM("node-datachannel")).cleanup();
3889
+ async _getStats() {
3890
+ invariant14(this._peer, "not open", {
3891
+ F: __dxlog_file15,
3892
+ L: 334,
3893
+ S: this,
3894
+ A: [
3895
+ "this._peer",
3896
+ "'not open'"
3897
+ ]
3898
+ });
3899
+ const stats = await this._peer.getStats();
3900
+ const statsEntries = Array.from(stats.entries());
3901
+ const transport = statsEntries.filter((s) => s[1].type === "transport")[0][1];
3902
+ const candidatePair = statsEntries.filter((s) => s[0] === transport.selectedCandidatePairId);
3903
+ let selectedCandidatePair;
3904
+ let remoteCandidate;
3905
+ if (candidatePair.length > 0) {
3906
+ selectedCandidatePair = candidatePair[0][1];
3907
+ remoteCandidate = statsEntries.filter((s) => s[0] === selectedCandidatePair.remoteCandidateId)[0][1];
3862
3908
  }
3909
+ return {
3910
+ transport,
3911
+ selectedCandidatePair,
3912
+ remoteCandidate,
3913
+ raw: Object.fromEntries(stats)
3914
+ };
3863
3915
  }
3864
3916
  async _disconnectStreams() {
3865
- this.params.stream.unpipe?.(this._stream)?.unpipe?.(this.params.stream);
3917
+ this._options.stream.unpipe?.(this._stream)?.unpipe?.(this._options.stream);
3866
3918
  }
3867
3919
  };
3868
3920
  _ts_decorate5([
3869
3921
  synchronized4
3870
3922
  ], LibDataChannelTransport.prototype, "_close", null);
3871
- var createLibDataChannelTransportFactory = (webrtcConfig) => ({
3872
- createTransport: (params) => new LibDataChannelTransport({
3873
- ...params,
3874
- webrtcConfig
3875
- })
3876
- });
3877
3923
  var importESM = Function("path", "return import(path)");
3878
3924
 
3879
3925
  // packages/core/mesh/network-manager/src/transport/tcp-transport.browser.ts
@@ -3888,10 +3934,14 @@ var TcpTransport = class {
3888
3934
  this.connected = new Event11();
3889
3935
  this.errors = new ErrorStream7();
3890
3936
  }
3891
- async destroy() {
3892
- throw new Error("Method not implemented.");
3937
+ get isOpen() {
3938
+ return true;
3939
+ }
3940
+ async open() {
3893
3941
  }
3894
- signal() {
3942
+ async close() {
3943
+ }
3944
+ async onSignal() {
3895
3945
  throw new Error("Method not implemented.");
3896
3946
  }
3897
3947
  async getStats() {
@@ -3941,15 +3991,15 @@ export {
3941
3991
  MemoryTransportFactory,
3942
3992
  MemoryTransport,
3943
3993
  TransportKind,
3944
- SimplePeerTransport,
3945
3994
  createSimplePeerTransportFactory,
3995
+ SimplePeerTransport,
3946
3996
  SimplePeerTransportService,
3947
3997
  SimplePeerTransportProxy,
3948
3998
  SimplePeerTransportProxyFactory,
3949
- LibDataChannelTransport,
3950
3999
  createLibDataChannelTransportFactory,
4000
+ LibDataChannelTransport,
3951
4001
  TcpTransportFactory,
3952
4002
  TcpTransport,
3953
4003
  createTeleportProtocolFactory
3954
4004
  };
3955
- //# sourceMappingURL=chunk-KEN4UCZF.mjs.map
4005
+ //# sourceMappingURL=chunk-SOVZDG44.mjs.map