egos-transfer 0.2.12 → 0.2.14

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.
@@ -15,8 +15,6 @@ export interface ConnectionManagerDeps {
15
15
  */
16
16
  export declare class ConnectionManager {
17
17
  private pool;
18
- /** peerId → 连接入池时间戳 */
19
- private createdAt;
20
18
  private healthCheckTimer;
21
19
  private pingRequests;
22
20
  private destroyed;
@@ -32,9 +30,13 @@ export declare class ConnectionManager {
32
30
  * @param conn - 已打开的 DataConnection
33
31
  * @param onData - 数据事件回调(由 PeerTransfer 提供,路由消息)
34
32
  */
35
- setup(conn: DataConnection, onData: (message: any) => void): DataConnection;
33
+ setup(conn: DataConnection & {
34
+ timestamp?: number;
35
+ }, onData: (message: any) => void): DataConnection;
36
36
  remove(peerId: string, connectionId?: string): void;
37
- isValid(conn?: DataConnection): boolean;
37
+ isValid(conn?: DataConnection & {
38
+ timestamp?: number;
39
+ }): boolean;
38
40
  private handleConnClosed;
39
41
  startHealthCheck(): void;
40
42
  stopHealthCheck(): void;
@@ -22,8 +22,6 @@ const MAX_CONNECTION_AGE = 15 * 60 * 1000;
22
22
  class ConnectionManager {
23
23
  constructor(deps) {
24
24
  this.pool = new Map();
25
- /** peerId → 连接入池时间戳 */
26
- this.createdAt = new Map();
27
25
  this.healthCheckTimer = null;
28
26
  this.pingRequests = new Map();
29
27
  this.destroyed = false;
@@ -64,8 +62,8 @@ class ConnectionManager {
64
62
  * @param onData - 数据事件回调(由 PeerTransfer 提供,路由消息)
65
63
  */
66
64
  setup(conn, onData) {
65
+ conn.timestamp = Date.now();
67
66
  this.pool.set(conn.peer, conn);
68
- this.createdAt.set(conn.peer, Date.now());
69
67
  conn.on('data', onData);
70
68
  conn.on('close', () => this.handleConnClosed(conn));
71
69
  conn.on('error', () => this.handleConnClosed(conn));
@@ -92,7 +90,6 @@ class ConnectionManager {
92
90
  if (connectionId && conn.connectionId !== connectionId)
93
91
  return;
94
92
  this.pool.delete(peerId);
95
- this.createdAt.delete(peerId);
96
93
  // 先 close 触发 close 事件(handleConnClosed 会结算 in-flight 请求),
97
94
  // 再清理监听器与 PeerJS 内部引用,避免僵尸连接堆积导致无法建立新连接
98
95
  conn.close();
@@ -111,10 +108,13 @@ class ConnectionManager {
111
108
  }
112
109
  }
113
110
  isValid(conn) {
114
- if (!(conn === null || conn === void 0 ? void 0 : conn.open))
111
+ if (!(conn === null || conn === void 0 ? void 0 : conn.open) || !(conn === null || conn === void 0 ? void 0 : conn.peerConnection)) {
115
112
  return false;
116
- if (!(conn === null || conn === void 0 ? void 0 : conn.peerConnection))
113
+ }
114
+ // 检查连接是否过期
115
+ if (conn.timestamp && Date.now() - conn.timestamp > MAX_CONNECTION_AGE) {
117
116
  return false;
117
+ }
118
118
  return ['connected', 'completed'].includes(conn.peerConnection.connectionState);
119
119
  }
120
120
  handleConnClosed(conn) {
@@ -210,7 +210,6 @@ class ConnectionManager {
210
210
  this.remove(peerId);
211
211
  }
212
212
  this.pool.clear();
213
- this.createdAt.clear();
214
213
  this.pingRequests.clear();
215
214
  }
216
215
  }
@@ -511,7 +511,10 @@ class PeerTransfer {
511
511
  // ── Router integration ──────────────────────────────────────────
512
512
  onRequest(conn, message) {
513
513
  return __awaiter(this, void 0, void 0, function* () {
514
- console.info('onRequest===>', message.route);
514
+ var _a;
515
+ if (((_a = this.peerConfig) === null || _a === void 0 ? void 0 : _a.debug) >= 2) {
516
+ console.info('onRequest===>', message.route);
517
+ }
515
518
  // this.resetIdleTimer();
516
519
  try {
517
520
  yield this.router.run(conn, message);
@@ -523,7 +526,10 @@ class PeerTransfer {
523
526
  }
524
527
  onResponse(_conn, message) {
525
528
  return __awaiter(this, void 0, void 0, function* () {
526
- console.info('onResponse<====', message.route);
529
+ var _a;
530
+ if (((_a = this.peerConfig) === null || _a === void 0 ? void 0 : _a.debug) >= 2) {
531
+ console.info('onResponse<====', message.route);
532
+ }
527
533
  // this.resetIdleTimer();
528
534
  // 优先检查 health check ping 回调
529
535
  const cb = this.requests.get(message.requestId);
@@ -587,7 +593,6 @@ class PeerTransfer {
587
593
  this.abortCreate();
588
594
  this.clearIdleTimer();
589
595
  this.clearHeartbeatTimer();
590
- this.connectionManager.destroy();
591
596
  this.requests.clear();
592
597
  this.connRequests.clear();
593
598
  this.destroyPeer();
@@ -619,9 +624,9 @@ class PeerTransfer {
619
624
  this.lastPeerCreatedAt = 0; // 重置节流,createPeer 可立即再次创建
620
625
  }
621
626
  destroyPeer() {
622
- var _a, _b;
627
+ var _a;
628
+ // @todo 是否需要清流 peer?
623
629
  (_a = this.peer) === null || _a === void 0 ? void 0 : _a.destroy();
624
- (_b = this.peer) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
625
630
  this.peer = undefined;
626
631
  }
627
632
  askReload(peerId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
@@ -605,7 +605,10 @@ export abstract class PeerTransfer {
605
605
  // ── Router integration ──────────────────────────────────────────
606
606
 
607
607
  async onRequest(conn: DataConnection, message: PeerMessage) {
608
- console.info('onRequest===>', message.route);
608
+ if (this.peerConfig?.debug >= 2) {
609
+ console.info('onRequest===>', message.route);
610
+ }
611
+
609
612
  // this.resetIdleTimer();
610
613
  try {
611
614
  await this.router.run(conn, message);
@@ -615,7 +618,10 @@ export abstract class PeerTransfer {
615
618
  }
616
619
 
617
620
  async onResponse(_conn: DataConnection, message: PeerMessage) {
618
- console.info('onResponse<====', message.route);
621
+ if (this.peerConfig?.debug >= 2) {
622
+ console.info('onResponse<====', message.route);
623
+ }
624
+
619
625
  // this.resetIdleTimer();
620
626
  // 优先检查 health check ping 回调
621
627
  const cb = this.requests.get(message.requestId);
@@ -688,9 +694,6 @@ export abstract class PeerTransfer {
688
694
  this.abortCreate();
689
695
  this.clearIdleTimer();
690
696
  this.clearHeartbeatTimer();
691
-
692
- this.connectionManager.destroy();
693
-
694
697
  this.requests.clear();
695
698
  this.connRequests.clear();
696
699
  this.destroyPeer();
@@ -726,8 +729,8 @@ export abstract class PeerTransfer {
726
729
  }
727
730
 
728
731
  private destroyPeer() {
732
+ // @todo 是否需要清流 peer?
729
733
  this.peer?.destroy();
730
- this.peer?.removeAllListeners();
731
734
  this.peer = undefined;
732
735
  }
733
736