egos-transfer 0.2.4 → 0.2.5

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.
package/dist/index.js CHANGED
@@ -96,7 +96,7 @@ class PeerTransfer {
96
96
  clearTimeout(timer);
97
97
  resolve(p);
98
98
  };
99
- peer.on('open', (id) => {
99
+ peer.once('open', (id) => {
100
100
  console.log('peer@open');
101
101
  this.reconnectAttempt = 0; // 连接成功,重置退避计数
102
102
  done(peer);
@@ -390,6 +390,7 @@ class PeerTransfer {
390
390
  if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
391
391
  yield this.reconnect();
392
392
  }
393
+ // 重试前检查:对方可能已经建连成功(duplicate 场景)
393
394
  const cur = this.connectPool.get(peerId);
394
395
  if (this.isValidConnection(cur)) {
395
396
  clearTimeout(guardTimer);
@@ -407,6 +408,12 @@ class PeerTransfer {
407
408
  clearTimeout(guardTimer);
408
409
  return resolve(connection);
409
410
  }
411
+ // duplicate 场景:onConnect 返回 undefined 但连接池可能已有对方建的连接
412
+ const pooled = this.connectPool.get(peerId);
413
+ if (this.isValidConnection(pooled)) {
414
+ clearTimeout(guardTimer);
415
+ return resolve(pooled);
416
+ }
410
417
  }
411
418
  clearTimeout(guardTimer);
412
419
  resolve(undefined);
@@ -513,7 +520,13 @@ class PeerTransfer {
513
520
  }
514
521
  }, constant_1.CONNECTION_TIMEOUT);
515
522
  this.bindConnectionData(conn);
516
- conn.on('error', () => {
523
+ conn.on('error', (err) => {
524
+ // duplicate 场景:连接池可能已有对方建的连接,不应删除
525
+ const pooled = this.connectPool.get(conn.peer);
526
+ if (this.isValidConnection(pooled)) {
527
+ done(pooled);
528
+ return;
529
+ }
517
530
  this.removeConnection(conn.peer, conn.connectionId);
518
531
  done(null);
519
532
  });
@@ -742,8 +755,9 @@ class PeerTransfer {
742
755
  }
743
756
  onRequest(conn, message) {
744
757
  return __awaiter(this, void 0, void 0, function* () {
758
+ var _a;
745
759
  try {
746
- console.log('onRequest@@', conn.peer, message.route);
760
+ console.log('onRequest@@', (_a = conn.peer) === null || _a === void 0 ? void 0 : _a.substring(0, 4), message.route);
747
761
  yield this.router.run(conn, message);
748
762
  }
749
763
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
package/src/index.ts CHANGED
@@ -140,7 +140,7 @@ export abstract class PeerTransfer {
140
140
  clearTimeout(timer);
141
141
  resolve(p);
142
142
  };
143
- peer.on('open', (id) => {
143
+ peer.once('open', (id) => {
144
144
  console.log('peer@open');
145
145
  this.reconnectAttempt = 0; // 连接成功,重置退避计数
146
146
  done(peer);
@@ -447,6 +447,7 @@ export abstract class PeerTransfer {
447
447
  await this.reconnect();
448
448
  }
449
449
 
450
+ // 重试前检查:对方可能已经建连成功(duplicate 场景)
450
451
  const cur = this.connectPool.get(peerId);
451
452
  if (this.isValidConnection(cur)) {
452
453
  clearTimeout(guardTimer);
@@ -465,6 +466,13 @@ export abstract class PeerTransfer {
465
466
  clearTimeout(guardTimer);
466
467
  return resolve(connection);
467
468
  }
469
+
470
+ // duplicate 场景:onConnect 返回 undefined 但连接池可能已有对方建的连接
471
+ const pooled = this.connectPool.get(peerId);
472
+ if (this.isValidConnection(pooled)) {
473
+ clearTimeout(guardTimer);
474
+ return resolve(pooled);
475
+ }
468
476
  }
469
477
  clearTimeout(guardTimer);
470
478
  resolve(undefined);
@@ -572,7 +580,13 @@ export abstract class PeerTransfer {
572
580
 
573
581
  this.bindConnectionData(conn);
574
582
 
575
- conn.on('error', () => {
583
+ conn.on('error', (err) => {
584
+ // duplicate 场景:连接池可能已有对方建的连接,不应删除
585
+ const pooled = this.connectPool.get(conn.peer);
586
+ if (this.isValidConnection(pooled)) {
587
+ done(pooled);
588
+ return;
589
+ }
576
590
  this.removeConnection(conn.peer, conn.connectionId);
577
591
  done(null);
578
592
  });
@@ -862,7 +876,7 @@ export abstract class PeerTransfer {
862
876
 
863
877
  async onRequest(conn: DataConnection, message: PeerMessage) {
864
878
  try {
865
- console.log('onRequest@@', conn.peer, message.route);
879
+ console.log('onRequest@@', conn.peer?.substring(0, 4), message.route);
866
880
  await this.router.run(conn, message);
867
881
  } catch (err: any) {
868
882
  this.responseFail(conn, message, { message: err.message, code: err.code });