egos-transfer 0.2.12 → 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.
|
@@ -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
|
|
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
|
|
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
|
-
|
|
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
|
}
|
package/dist/peer-transfer.js
CHANGED
|
@@ -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
|
|
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
package/src/peer-transfer.ts
CHANGED
|
@@ -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
|
|