@dxos/network-manager 0.1.56-main.09ca29d → 0.1.56-main.0e9f69e

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.
@@ -4,7 +4,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
4
4
  }) : x)(function(x) {
5
5
  if (typeof require !== "undefined")
6
6
  return require.apply(this, arguments);
7
- throw new Error('Dynamic require of "' + x + '" is not supported');
7
+ throw Error('Dynamic require of "' + x + '" is not supported');
8
8
  });
9
9
 
10
10
  // packages/core/mesh/network-manager/src/swarm/connection.ts
@@ -192,7 +192,6 @@ var Connection = class {
192
192
  });
193
193
  }
194
194
  async close() {
195
- var _a;
196
195
  if (this._state === ConnectionState.CLOSED) {
197
196
  return;
198
197
  }
@@ -217,7 +216,7 @@ var Connection = class {
217
216
  });
218
217
  }
219
218
  try {
220
- await ((_a = this._transport) == null ? void 0 : _a.destroy());
219
+ await this._transport?.destroy();
221
220
  } catch (err) {
222
221
  log.catch(err, void 0, {
223
222
  F: __dxlog_file,
@@ -281,7 +280,6 @@ var Connection = class {
281
280
  * Receive a signal from the remote peer.
282
281
  */
283
282
  async signal(msg) {
284
- var _a, _b, _c;
285
283
  invariant(msg.sessionId, void 0, {
286
284
  F: __dxlog_file,
287
285
  L: 243,
@@ -309,7 +307,7 @@ var Connection = class {
309
307
  ""
310
308
  ]
311
309
  });
312
- invariant((_a = msg.author) == null ? void 0 : _a.equals(this.remoteId), void 0, {
310
+ invariant(msg.author?.equals(this.remoteId), void 0, {
313
311
  F: __dxlog_file,
314
312
  L: 249,
315
313
  S: this,
@@ -318,7 +316,7 @@ var Connection = class {
318
316
  ""
319
317
  ]
320
318
  });
321
- invariant((_b = msg.recipient) == null ? void 0 : _b.equals(this.ownId), void 0, {
319
+ invariant(msg.recipient?.equals(this.ownId), void 0, {
322
320
  F: __dxlog_file,
323
321
  L: 250,
324
322
  S: this,
@@ -327,7 +325,7 @@ var Connection = class {
327
325
  ""
328
326
  ]
329
327
  });
330
- const signals = msg.data.signalBatch ? (_c = msg.data.signalBatch.signals) != null ? _c : [] : [
328
+ const signals = msg.data.signalBatch ? msg.data.signalBatch.signals ?? [] : [
331
329
  msg.data.signal
332
330
  ];
333
331
  for (const signal of signals) {
@@ -417,7 +415,6 @@ var SwarmMessenger = class {
417
415
  this._topic = topic;
418
416
  }
419
417
  async receiveMessage({ author, recipient, payload }) {
420
- var _a, _b, _c, _d;
421
418
  if (payload.type_url !== "dxos.mesh.swarm.SwarmMessage") {
422
419
  return;
423
420
  }
@@ -435,21 +432,21 @@ var SwarmMessenger = class {
435
432
  S: this,
436
433
  C: (f, a) => f(...a)
437
434
  });
438
- if ((_a = message.data) == null ? void 0 : _a.offer) {
435
+ if (message.data?.offer) {
439
436
  await this._handleOffer({
440
437
  author,
441
438
  recipient,
442
439
  message
443
440
  });
444
- } else if ((_b = message.data) == null ? void 0 : _b.answer) {
441
+ } else if (message.data?.answer) {
445
442
  await this._resolveAnswers(message);
446
- } else if ((_c = message.data) == null ? void 0 : _c.signal) {
443
+ } else if (message.data?.signal) {
447
444
  await this._handleSignal({
448
445
  author,
449
446
  recipient,
450
447
  message
451
448
  });
452
- } else if ((_d = message.data) == null ? void 0 : _d.signalBatch) {
449
+ } else if (message.data?.signalBatch) {
453
450
  await this._handleSignal({
454
451
  author,
455
452
  recipient,
@@ -467,8 +464,7 @@ var SwarmMessenger = class {
467
464
  }
468
465
  }
469
466
  async signal(message) {
470
- var _a, _b;
471
- invariant2(((_a = message.data) == null ? void 0 : _a.signal) || ((_b = message.data) == null ? void 0 : _b.signalBatch), "Invalid message", {
467
+ invariant2(message.data?.signal || message.data?.signalBatch, "Invalid message", {
472
468
  F: __dxlog_file2,
473
469
  L: 85,
474
470
  S: this,
@@ -500,11 +496,10 @@ var SwarmMessenger = class {
500
496
  });
501
497
  }
502
498
  async _sendReliableMessage({ author, recipient, message }) {
503
- var _a;
504
499
  const networkMessage = {
505
500
  ...message,
506
501
  // Setting unique message_id if it not specified yet.
507
- messageId: (_a = message.messageId) != null ? _a : PublicKey2.random()
502
+ messageId: message.messageId ?? PublicKey2.random()
508
503
  };
509
504
  log2("sending", {
510
505
  from: author,
@@ -526,8 +521,7 @@ var SwarmMessenger = class {
526
521
  });
527
522
  }
528
523
  async _resolveAnswers(message) {
529
- var _a, _b, _c;
530
- invariant2((_b = (_a = message.data) == null ? void 0 : _a.answer) == null ? void 0 : _b.offerMessageId, "No offerMessageId", {
524
+ invariant2(message.data?.answer?.offerMessageId, "No offerMessageId", {
531
525
  F: __dxlog_file2,
532
526
  L: 135,
533
527
  S: this,
@@ -539,7 +533,7 @@ var SwarmMessenger = class {
539
533
  const offerRecord = this._offerRecords.get(message.data.answer.offerMessageId);
540
534
  if (offerRecord) {
541
535
  this._offerRecords.delete(message.data.answer.offerMessageId);
542
- invariant2((_c = message.data) == null ? void 0 : _c.answer, "No answer", {
536
+ invariant2(message.data?.answer, "No answer", {
543
537
  F: __dxlog_file2,
544
538
  L: 139,
545
539
  S: this,
@@ -683,7 +677,6 @@ var Peer = class {
683
677
  * Respond to remote offer.
684
678
  */
685
679
  async onOffer(message) {
686
- var _a;
687
680
  const remoteId = message.author;
688
681
  if (this.connection || this.initiating) {
689
682
  if (remoteId.toHex() < this.localPeerId.toHex()) {
@@ -691,7 +684,7 @@ var Peer = class {
691
684
  localPeerId: this.id,
692
685
  topic: this.topic,
693
686
  remotePeerId: this.localPeerId,
694
- sessionId: (_a = this.connection) == null ? void 0 : _a.sessionId
687
+ sessionId: this.connection?.sessionId
695
688
  }, {
696
689
  F: __dxlog_file3,
697
690
  L: 106,
@@ -843,7 +836,6 @@ var Peer = class {
843
836
  * Either we're initiating a connection or creating one in response to an offer from the other peer.
844
837
  */
845
838
  _createConnection(initiator, sessionId) {
846
- var _a;
847
839
  log3("creating connection", {
848
840
  topic: this.topic,
849
841
  peerId: this.localPeerId,
@@ -882,7 +874,7 @@ var Peer = class {
882
874
  this._transportFactory
883
875
  );
884
876
  this._callbacks.onInitiated(connection);
885
- void ((_a = this._connectionCtx) == null ? void 0 : _a.dispose());
877
+ void this._connectionCtx?.dispose();
886
878
  this._connectionCtx = this._ctx.derive();
887
879
  connection.stateChanged.on((state) => {
888
880
  switch (state) {
@@ -1026,7 +1018,6 @@ var Peer = class {
1026
1018
  await this.connection.signal(message);
1027
1019
  }
1028
1020
  async destroy() {
1029
- var _a;
1030
1021
  await this._ctx.dispose();
1031
1022
  log3("Destroying peer", {
1032
1023
  peerId: this.id,
@@ -1037,7 +1028,7 @@ var Peer = class {
1037
1028
  S: this,
1038
1029
  C: (f, a) => f(...a)
1039
1030
  });
1040
- await ((_a = this == null ? void 0 : this.connection) == null ? void 0 : _a.close());
1031
+ await this?.connection?.close();
1041
1032
  }
1042
1033
  };
1043
1034
  _ts_decorate2([
@@ -1167,14 +1158,13 @@ var Swarm = class {
1167
1158
  });
1168
1159
  }
1169
1160
  async destroy() {
1170
- var _a;
1171
1161
  log4("destroying...", void 0, {
1172
1162
  F: __dxlog_file4,
1173
1163
  L: 140,
1174
1164
  S: this,
1175
1165
  C: (f, a) => f(...a)
1176
1166
  });
1177
- await ((_a = this._listeningHandle) == null ? void 0 : _a.unsubscribe());
1167
+ await this._listeningHandle?.unsubscribe();
1178
1168
  this._listeningHandle = void 0;
1179
1169
  await this._ctx.dispose();
1180
1170
  await this._topology.destroy();
@@ -1214,7 +1204,6 @@ var Swarm = class {
1214
1204
  this._topology.update();
1215
1205
  }
1216
1206
  onSwarmEvent(swarmEvent) {
1217
- var _a;
1218
1207
  log4("swarm event", {
1219
1208
  swarmEvent
1220
1209
  }, {
@@ -1250,7 +1239,7 @@ var Swarm = class {
1250
1239
  const peer = this._peers.get(PublicKey4.from(swarmEvent.peerLeft.peer));
1251
1240
  if (peer) {
1252
1241
  peer.advertizing = false;
1253
- if (((_a = peer.connection) == null ? void 0 : _a.state) !== ConnectionState.CONNECTED) {
1242
+ if (peer.connection?.state !== ConnectionState.CONNECTED) {
1254
1243
  void this._destroyPeer(peer.id).catch((err) => log4.catch(err, void 0, {
1255
1244
  F: __dxlog_file4,
1256
1245
  L: 188,
@@ -1263,7 +1252,6 @@ var Swarm = class {
1263
1252
  this._topology.update();
1264
1253
  }
1265
1254
  async onOffer(message) {
1266
- var _a, _b;
1267
1255
  log4("offer", {
1268
1256
  message
1269
1257
  }, {
@@ -1292,7 +1280,7 @@ var Swarm = class {
1292
1280
  ""
1293
1281
  ]
1294
1282
  });
1295
- if (!((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId))) {
1283
+ if (!message.recipient?.equals(this._ownPeerId)) {
1296
1284
  log4("rejecting offer with incorrect peerId", {
1297
1285
  message
1298
1286
  }, {
@@ -1305,7 +1293,7 @@ var Swarm = class {
1305
1293
  accept: false
1306
1294
  };
1307
1295
  }
1308
- if (!((_b = message.topic) == null ? void 0 : _b.equals(this._topic))) {
1296
+ if (!message.topic?.equals(this._topic)) {
1309
1297
  log4("rejecting offer with incorrect topic", {
1310
1298
  message
1311
1299
  }, {
@@ -1324,7 +1312,6 @@ var Swarm = class {
1324
1312
  return answer;
1325
1313
  }
1326
1314
  async onSignal(message) {
1327
- var _a, _b;
1328
1315
  log4("signal", {
1329
1316
  message
1330
1317
  }, {
@@ -1342,7 +1329,7 @@ var Swarm = class {
1342
1329
  });
1343
1330
  return;
1344
1331
  }
1345
- invariant4((_a = message.recipient) == null ? void 0 : _a.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1332
+ invariant4(message.recipient?.equals(this._ownPeerId), `Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`, {
1346
1333
  F: __dxlog_file4,
1347
1334
  L: 227,
1348
1335
  S: this,
@@ -1351,7 +1338,7 @@ var Swarm = class {
1351
1338
  "`Invalid signal peer id expected=${this.ownPeerId}, actual=${message.recipient}`"
1352
1339
  ]
1353
1340
  });
1354
- invariant4((_b = message.topic) == null ? void 0 : _b.equals(this._topic), void 0, {
1341
+ invariant4(message.topic?.equals(this._topic), void 0, {
1355
1342
  F: __dxlog_file4,
1356
1343
  L: 231,
1357
1344
  S: this,
@@ -1563,8 +1550,7 @@ var SwarmMapper = class {
1563
1550
  }));
1564
1551
  }));
1565
1552
  this._subscriptions.add(_swarm.disconnected.on((peerId) => {
1566
- var _a;
1567
- (_a = this._connectionSubscriptions.get(peerId)) == null ? void 0 : _a();
1553
+ this._connectionSubscriptions.get(peerId)?.();
1568
1554
  this._connectionSubscriptions.delete(peerId);
1569
1555
  this._update();
1570
1556
  }));
@@ -1714,8 +1700,7 @@ var ConnectionLog = class {
1714
1700
  this.update = new Event4();
1715
1701
  }
1716
1702
  getSwarmInfo(swarmId) {
1717
- var _a;
1718
- return (_a = this._swarms.get(swarmId)) != null ? _a : raise(new Error(`Swarm not found: ${swarmId}`));
1703
+ return this._swarms.get(swarmId) ?? raise(new Error(`Swarm not found: ${swarmId}`));
1719
1704
  }
1720
1705
  get swarms() {
1721
1706
  return Array.from(this._swarms.values());
@@ -1731,7 +1716,6 @@ var ConnectionLog = class {
1731
1716
  this._swarms.set(PublicKey7.from(swarm._instanceId), info);
1732
1717
  this.update.emit();
1733
1718
  swarm.connectionAdded.on((connection) => {
1734
- var _a, _b;
1735
1719
  const connectionInfo = {
1736
1720
  state: ConnectionState.INITIAL,
1737
1721
  remotePeerId: connection.remoteId,
@@ -1750,7 +1734,7 @@ var ConnectionLog = class {
1750
1734
  });
1751
1735
  this.update.emit();
1752
1736
  });
1753
- (_b = (_a = connection.protocol) == null ? void 0 : _a.stats) == null ? void 0 : _b.on((stats) => {
1737
+ connection.protocol?.stats?.on((stats) => {
1754
1738
  connectionInfo.streams = stats.channels;
1755
1739
  this.update.emit();
1756
1740
  });
@@ -1785,10 +1769,7 @@ var NetworkManager = class {
1785
1769
  this._instanceId = PublicKey8.random().toHex();
1786
1770
  this._transportFactory = transportFactory;
1787
1771
  this._signalManager = signalManager;
1788
- this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => {
1789
- var _a;
1790
- return (_a = this._swarms.get(topic)) == null ? void 0 : _a.onSwarmEvent(event);
1791
- });
1772
+ this._signalManager.swarmEvent.on(({ topic, swarmEvent: event }) => this._swarms.get(topic)?.onSwarmEvent(event));
1792
1773
  this._messenger = new Messenger({
1793
1774
  signalManager: this._signalManager
1794
1775
  });
@@ -1856,7 +1837,6 @@ var NetworkManager = class {
1856
1837
  * Join the swarm.
1857
1838
  */
1858
1839
  async joinSwarm({ topic, peerId, topology, protocolProvider: protocol, label }) {
1859
- var _a;
1860
1840
  invariant6(PublicKey8.isPublicKey(topic), void 0, {
1861
1841
  F: __dxlog_file7,
1862
1842
  L: 157,
@@ -1930,7 +1910,7 @@ var NetworkManager = class {
1930
1910
  C: (f, a) => f(...a)
1931
1911
  }));
1932
1912
  this.topicsUpdated.emit();
1933
- (_a = this._connectionLog) == null ? void 0 : _a.joinedSwarm(swarm);
1913
+ this._connectionLog?.joinedSwarm(swarm);
1934
1914
  log7("joined", {
1935
1915
  topic: PublicKey8.from(topic),
1936
1916
  count: this._swarms.size
@@ -1948,7 +1928,6 @@ var NetworkManager = class {
1948
1928
  * Close the connection.
1949
1929
  */
1950
1930
  async leaveSwarm(topic) {
1951
- var _a;
1952
1931
  if (!this._swarms.has(topic)) {
1953
1932
  return;
1954
1933
  }
@@ -1968,7 +1947,7 @@ var NetworkManager = class {
1968
1947
  const map = this._mappers.get(topic);
1969
1948
  map.destroy();
1970
1949
  this._mappers.delete(topic);
1971
- (_a = this._connectionLog) == null ? void 0 : _a.leftSwarm(swarm);
1950
+ this._connectionLog?.leftSwarm(swarm);
1972
1951
  await swarm.destroy();
1973
1952
  this._swarms.delete(topic);
1974
1953
  await this.topicsUpdated.emit();
@@ -2289,9 +2268,12 @@ var createStreamDelay = (delay) => {
2289
2268
  var MemoryTransportFactory = {
2290
2269
  createTransport: (params) => new MemoryTransport(params)
2291
2270
  };
2292
- var _MemoryTransport = class {
2271
+ var MemoryTransport = class _MemoryTransport {
2272
+ static {
2273
+ // TODO(burdon): Remove static properties (inject context into constructor).
2274
+ this._connections = new ComplexMap7(PublicKey9.hash);
2275
+ }
2293
2276
  constructor(options) {
2294
- var _a;
2295
2277
  this.options = options;
2296
2278
  this.closed = new Event6();
2297
2279
  this.connected = new Event6();
@@ -2337,7 +2319,7 @@ var _MemoryTransport = class {
2337
2319
  });
2338
2320
  } else {
2339
2321
  this._remote.wait({
2340
- timeout: (_a = this.options.timeout) != null ? _a : 1e3
2322
+ timeout: this.options.timeout ?? 1e3
2341
2323
  }).then((remoteId) => {
2342
2324
  if (this._destroyed) {
2343
2325
  return;
@@ -2424,7 +2406,7 @@ var _MemoryTransport = class {
2424
2406
  S: this,
2425
2407
  C: (f, a) => f(...a)
2426
2408
  });
2427
- if (!(payload == null ? void 0 : payload.transportId)) {
2409
+ if (!payload?.transportId) {
2428
2410
  return;
2429
2411
  }
2430
2412
  const transportId = payload.transportId;
@@ -2434,9 +2416,6 @@ var _MemoryTransport = class {
2434
2416
  }
2435
2417
  }
2436
2418
  };
2437
- var MemoryTransport = _MemoryTransport;
2438
- // TODO(burdon): Remove static properties (inject context into constructor).
2439
- MemoryTransport._connections = new ComplexMap7(PublicKey9.hash);
2440
2419
  _ts_decorate4([
2441
2420
  logInfo2
2442
2421
  ], MemoryTransport.prototype, "_instanceId", void 0);
@@ -2457,14 +2436,13 @@ import { trace as trace4 } from "@dxos/protocols";
2457
2436
  var wrtc = null;
2458
2437
  try {
2459
2438
  wrtc = __require("@koush/wrtc");
2460
- } catch (e) {
2439
+ } catch {
2461
2440
  }
2462
2441
 
2463
2442
  // packages/core/mesh/network-manager/src/transport/webrtc-transport.ts
2464
2443
  var __dxlog_file12 = "/home/runner/work/dxos/dxos/packages/core/mesh/network-manager/src/transport/webrtc-transport.ts";
2465
2444
  var WebRTCTransport = class {
2466
2445
  constructor(params) {
2467
- var _a;
2468
2446
  this.params = params;
2469
2447
  this._closed = false;
2470
2448
  this.closed = new Event7();
@@ -2487,7 +2465,7 @@ var WebRTCTransport = class {
2487
2465
  });
2488
2466
  this._peer = new SimplePeerConstructor({
2489
2467
  initiator: this.params.initiator,
2490
- wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : (_a = wrtc) != null ? _a : raise2(new Error("wrtc not available")),
2468
+ wrtc: SimplePeerConstructor.WEBRTC_SUPPORT ? void 0 : wrtc ?? raise2(new Error("wrtc not available")),
2491
2469
  config: this.params.webrtcConfig
2492
2470
  });
2493
2471
  this._peer.on("signal", async (data) => {
@@ -2524,10 +2502,9 @@ var WebRTCTransport = class {
2524
2502
  this.closed.emit();
2525
2503
  });
2526
2504
  this._peer.on("error", async (err) => {
2527
- var _a2;
2528
2505
  this.errors.raise(err);
2529
2506
  try {
2530
- if (typeof ((_a2 = this._peer) == null ? void 0 : _a2._pc.getStats) === "function") {
2507
+ if (typeof this._peer?._pc.getStats === "function") {
2531
2508
  this._peer._pc.getStats().then((stats) => {
2532
2509
  log11.warn("report after webrtc error", {
2533
2510
  config: this.params.webrtcConfig,
@@ -2540,7 +2517,7 @@ var WebRTCTransport = class {
2540
2517
  });
2541
2518
  });
2542
2519
  }
2543
- } catch (e) {
2520
+ } catch {
2544
2521
  }
2545
2522
  await this.destroy();
2546
2523
  });
@@ -2587,8 +2564,7 @@ var WebRTCTransport = class {
2587
2564
  this._peer.signal(signal.payload.data);
2588
2565
  }
2589
2566
  async _disconnectStreams() {
2590
- var _a, _b, _c, _d;
2591
- (_d = (_c = (_b = (_a = this.params.stream).unpipe) == null ? void 0 : _b.call(_a, this._peer)) == null ? void 0 : _c.unpipe) == null ? void 0 : _d.call(_c, this.params.stream);
2567
+ this.params.stream.unpipe?.(this._peer)?.unpipe?.(this.params.stream);
2592
2568
  }
2593
2569
  };
2594
2570
  var createWebRTCTransportFactory = (webrtcConfig) => ({
@@ -2716,9 +2692,8 @@ var WebRTCTransportService = class {
2716
2692
  }
2717
2693
  }
2718
2694
  async close({ proxyId }) {
2719
- var _a, _b;
2720
- await ((_a = this.transports.get(proxyId)) == null ? void 0 : _a.transport.destroy());
2721
- await ((_b = this.transports.get(proxyId)) == null ? void 0 : _b.stream.end());
2695
+ await this.transports.get(proxyId)?.transport.destroy();
2696
+ await this.transports.get(proxyId)?.stream.end();
2722
2697
  this.transports.delete(proxyId);
2723
2698
  log12("Closed.", void 0, {
2724
2699
  F: __dxlog_file13,
@@ -2927,4 +2902,4 @@ export {
2927
2902
  WebRTCTransportProxyFactory,
2928
2903
  createTeleportProtocolFactory
2929
2904
  };
2930
- //# sourceMappingURL=chunk-FC6CWDES.mjs.map
2905
+ //# sourceMappingURL=chunk-5TRIVLI6.mjs.map