egos-transfer 0.2.11 → 0.2.13

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.
@@ -30,9 +30,13 @@ export declare class ConnectionManager {
30
30
  * @param conn - 已打开的 DataConnection
31
31
  * @param onData - 数据事件回调(由 PeerTransfer 提供,路由消息)
32
32
  */
33
- setup(conn: DataConnection, onData: (message: any) => void): DataConnection;
33
+ setup(conn: DataConnection & {
34
+ timestamp?: number;
35
+ }, onData: (message: any) => void): DataConnection;
34
36
  remove(peerId: string, connectionId?: string): void;
35
- isValid(conn?: DataConnection): boolean;
37
+ isValid(conn?: DataConnection & {
38
+ timestamp?: number;
39
+ }): boolean;
36
40
  private handleConnClosed;
37
41
  startHealthCheck(): void;
38
42
  stopHealthCheck(): void;
@@ -11,6 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ConnectionManager = void 0;
13
13
  const HEALTH_CHECK_INTERVAL = 15000;
14
+ /** 连接最大存活时间:超过 15 分钟的连接在 get 时自动丢弃,强制重建 */
15
+ const MAX_CONNECTION_AGE = 15 * 60 * 1000;
14
16
  /**
15
17
  * DataConnection 连接池管理器
16
18
  * - 管理 DataConnection 的生命周期(注册/移除/有效性检查)
@@ -60,6 +62,7 @@ class ConnectionManager {
60
62
  * @param onData - 数据事件回调(由 PeerTransfer 提供,路由消息)
61
63
  */
62
64
  setup(conn, onData) {
65
+ conn.timestamp = Date.now();
63
66
  this.pool.set(conn.peer, conn);
64
67
  conn.on('data', onData);
65
68
  conn.on('close', () => this.handleConnClosed(conn));
@@ -105,10 +108,13 @@ class ConnectionManager {
105
108
  }
106
109
  }
107
110
  isValid(conn) {
108
- 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)) {
109
112
  return false;
110
- if (!(conn === null || conn === void 0 ? void 0 : conn.peerConnection))
113
+ }
114
+ // 检查连接是否过期
115
+ if (conn.timestamp && Date.now() - conn.timestamp > MAX_CONNECTION_AGE) {
111
116
  return false;
117
+ }
112
118
  return ['connected', 'completed'].includes(conn.peerConnection.connectionState);
113
119
  }
114
120
  handleConnClosed(conn) {
@@ -587,7 +587,6 @@ class PeerTransfer {
587
587
  this.abortCreate();
588
588
  this.clearIdleTimer();
589
589
  this.clearHeartbeatTimer();
590
- this.connectionManager.destroy();
591
590
  this.requests.clear();
592
591
  this.connRequests.clear();
593
592
  this.destroyPeer();
@@ -619,9 +618,9 @@ class PeerTransfer {
619
618
  this.lastPeerCreatedAt = 0; // 重置节流,createPeer 可立即再次创建
620
619
  }
621
620
  destroyPeer() {
622
- var _a, _b;
621
+ var _a;
622
+ // @todo 是否需要清流 peer?
623
623
  (_a = this.peer) === null || _a === void 0 ? void 0 : _a.destroy();
624
- (_b = this.peer) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
625
624
  this.peer = undefined;
626
625
  }
627
626
  askReload(peerId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
@@ -1,11 +1,10 @@
1
1
  import { DataConnection, Peer } from 'peerjs';
2
- import { PeerAction, PeerRoutes, PeerScope } from './entity';
3
-
4
- import { PING_TIMEOUT } from './constant';
5
2
 
6
3
  type ResponseCb = (args: any) => void;
7
4
 
8
5
  const HEALTH_CHECK_INTERVAL = 15000;
6
+ /** 连接最大存活时间:超过 15 分钟的连接在 get 时自动丢弃,强制重建 */
7
+ const MAX_CONNECTION_AGE = 15 * 60 * 1000;
9
8
 
10
9
  export interface ConnectionManagerDeps {
11
10
  /** 获取当前 Peer 实例(PeerTransfer 可能异步重建) */
@@ -74,7 +73,11 @@ export class ConnectionManager {
74
73
  * @param conn - 已打开的 DataConnection
75
74
  * @param onData - 数据事件回调(由 PeerTransfer 提供,路由消息)
76
75
  */
77
- setup(conn: DataConnection, onData: (message: any) => void): DataConnection {
76
+ setup(
77
+ conn: DataConnection & { timestamp?: number },
78
+ onData: (message: any) => void,
79
+ ): DataConnection {
80
+ conn.timestamp = Date.now();
78
81
  this.pool.set(conn.peer, conn);
79
82
  conn.on('data', onData);
80
83
  conn.on('close', () => this.handleConnClosed(conn));
@@ -120,9 +123,14 @@ export class ConnectionManager {
120
123
  }
121
124
  }
122
125
 
123
- isValid(conn?: DataConnection): boolean {
124
- if (!conn?.open) return false;
125
- if (!conn?.peerConnection) return false;
126
+ isValid(conn?: DataConnection & { timestamp?: number }): boolean {
127
+ if (!conn?.open || !conn?.peerConnection) {
128
+ return false;
129
+ }
130
+ // 检查连接是否过期
131
+ if (conn.timestamp && Date.now() - conn.timestamp > MAX_CONNECTION_AGE) {
132
+ return false;
133
+ }
126
134
  return ['connected', 'completed'].includes(conn.peerConnection.connectionState);
127
135
  }
128
136
 
@@ -688,9 +688,6 @@ export abstract class PeerTransfer {
688
688
  this.abortCreate();
689
689
  this.clearIdleTimer();
690
690
  this.clearHeartbeatTimer();
691
-
692
- this.connectionManager.destroy();
693
-
694
691
  this.requests.clear();
695
692
  this.connRequests.clear();
696
693
  this.destroyPeer();
@@ -726,8 +723,8 @@ export abstract class PeerTransfer {
726
723
  }
727
724
 
728
725
  private destroyPeer() {
726
+ // @todo 是否需要清流 peer?
729
727
  this.peer?.destroy();
730
- this.peer?.removeAllListeners();
731
728
  this.peer = undefined;
732
729
  }
733
730