@clonegod/ttd-bsc-common 1.0.53 → 1.0.55
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.
|
@@ -3,18 +3,15 @@ export declare class WebSocketManager {
|
|
|
3
3
|
private static instance;
|
|
4
4
|
private wsProvider;
|
|
5
5
|
private wsConnections;
|
|
6
|
-
private reconnectAttempts;
|
|
7
|
-
private readonly MAX_RECONNECT_ATTEMPTS;
|
|
8
|
-
private readonly RECONNECT_DELAY;
|
|
9
|
-
private reconnecting;
|
|
10
6
|
private eventListeners;
|
|
11
7
|
private constructor();
|
|
8
|
+
private formatConn;
|
|
9
|
+
private formatErr;
|
|
12
10
|
static getInstance(): WebSocketManager;
|
|
13
11
|
getWsProvider(wsEndpoint: string, network: {
|
|
14
12
|
name: string;
|
|
15
13
|
chainId: number;
|
|
16
14
|
}): ethers.providers.WebSocketProvider | null;
|
|
17
|
-
private handleWsError;
|
|
18
15
|
private handleWsDisconnection;
|
|
19
16
|
incrementConnections(): void;
|
|
20
17
|
decrementConnections(): void;
|
|
@@ -7,12 +7,22 @@ class WebSocketManager {
|
|
|
7
7
|
constructor() {
|
|
8
8
|
this.wsProvider = null;
|
|
9
9
|
this.wsConnections = 0;
|
|
10
|
-
this.reconnectAttempts = 0;
|
|
11
|
-
this.MAX_RECONNECT_ATTEMPTS = 5;
|
|
12
|
-
this.RECONNECT_DELAY = 2000;
|
|
13
|
-
this.reconnecting = false;
|
|
14
10
|
this.eventListeners = new Map();
|
|
15
11
|
}
|
|
12
|
+
formatConn(wsEndpoint, network) {
|
|
13
|
+
return { wsEndpoint, network: network === null || network === void 0 ? void 0 : network.name, chainId: network === null || network === void 0 ? void 0 : network.chainId };
|
|
14
|
+
}
|
|
15
|
+
formatErr(err) {
|
|
16
|
+
var _a, _b;
|
|
17
|
+
if (!err)
|
|
18
|
+
return {};
|
|
19
|
+
const anyErr = err;
|
|
20
|
+
return {
|
|
21
|
+
name: anyErr === null || anyErr === void 0 ? void 0 : anyErr.name,
|
|
22
|
+
code: (_a = anyErr === null || anyErr === void 0 ? void 0 : anyErr.code) !== null && _a !== void 0 ? _a : (_b = anyErr === null || anyErr === void 0 ? void 0 : anyErr.error) === null || _b === void 0 ? void 0 : _b.code,
|
|
23
|
+
message: anyErr === null || anyErr === void 0 ? void 0 : anyErr.message
|
|
24
|
+
};
|
|
25
|
+
}
|
|
16
26
|
static getInstance() {
|
|
17
27
|
if (!WebSocketManager.instance) {
|
|
18
28
|
WebSocketManager.instance = new WebSocketManager();
|
|
@@ -20,76 +30,75 @@ class WebSocketManager {
|
|
|
20
30
|
return WebSocketManager.instance;
|
|
21
31
|
}
|
|
22
32
|
getWsProvider(wsEndpoint, network) {
|
|
33
|
+
var _a;
|
|
23
34
|
if (!wsEndpoint) {
|
|
24
35
|
(0, dist_1.log_warn)('WebSocket endpoint not configured');
|
|
25
36
|
return null;
|
|
26
37
|
}
|
|
27
|
-
if (!this.wsProvider
|
|
38
|
+
if (!this.wsProvider) {
|
|
28
39
|
(0, dist_1.log_info)('Creating new WebSocket provider');
|
|
29
40
|
try {
|
|
30
41
|
this.wsProvider = new ethers_1.ethers.providers.WebSocketProvider(wsEndpoint, network);
|
|
31
42
|
this.wsProvider.on('error', (error) => {
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
this.wsProvider._websocket.on('close', () => {
|
|
35
|
-
(0, dist_1.log_warn)('WebSocket connection closed');
|
|
43
|
+
(0, dist_1.log_error)('[WS] provider error', Object.assign(Object.assign({}, this.formatConn(wsEndpoint, network)), this.formatErr(error)));
|
|
36
44
|
this.handleWsDisconnection(wsEndpoint, network);
|
|
37
45
|
});
|
|
38
|
-
this.
|
|
46
|
+
const rawWs = (_a = this.wsProvider) === null || _a === void 0 ? void 0 : _a._websocket;
|
|
47
|
+
if (rawWs === null || rawWs === void 0 ? void 0 : rawWs.on) {
|
|
48
|
+
rawWs.on('close', (code, reason) => {
|
|
49
|
+
(0, dist_1.log_warn)('[WS] socket close', Object.assign(Object.assign({}, this.formatConn(wsEndpoint, network)), { code, reason }));
|
|
50
|
+
this.handleWsDisconnection(wsEndpoint, network);
|
|
51
|
+
});
|
|
52
|
+
rawWs.on('error', (err) => {
|
|
53
|
+
(0, dist_1.log_error)('[WS] socket error', Object.assign(Object.assign({}, this.formatConn(wsEndpoint, network)), this.formatErr(err)));
|
|
54
|
+
this.handleWsDisconnection(wsEndpoint, network);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
39
57
|
}
|
|
40
58
|
catch (error) {
|
|
41
|
-
(0, dist_1.log_error)('
|
|
59
|
+
(0, dist_1.log_error)('[WS] create provider failed', Object.assign(Object.assign({}, this.formatConn(wsEndpoint, network)), this.formatErr(error)));
|
|
42
60
|
this.wsProvider = null;
|
|
43
61
|
}
|
|
44
62
|
}
|
|
45
63
|
return this.wsProvider;
|
|
46
64
|
}
|
|
47
|
-
handleWsError(error, wsEndpoint, network) {
|
|
48
|
-
(0, dist_1.log_error)('WebSocket provider error', error);
|
|
49
|
-
this.handleWsDisconnection(wsEndpoint, network);
|
|
50
|
-
}
|
|
51
65
|
handleWsDisconnection(wsEndpoint, network) {
|
|
52
|
-
|
|
53
|
-
return;
|
|
66
|
+
var _a, _b, _c, _d;
|
|
54
67
|
if (this.wsProvider) {
|
|
55
|
-
|
|
68
|
+
try {
|
|
69
|
+
this.wsProvider.removeAllListeners();
|
|
70
|
+
const rawWs = (_a = this.wsProvider) === null || _a === void 0 ? void 0 : _a._websocket;
|
|
71
|
+
(_b = rawWs === null || rawWs === void 0 ? void 0 : rawWs.terminate) === null || _b === void 0 ? void 0 : _b.call(rawWs);
|
|
72
|
+
(_d = (_c = this.wsProvider) === null || _c === void 0 ? void 0 : _c.destroy) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
73
|
+
}
|
|
74
|
+
catch (_e) { }
|
|
56
75
|
this.wsProvider = null;
|
|
57
76
|
}
|
|
58
|
-
|
|
59
|
-
this.reconnectAttempts < this.MAX_RECONNECT_ATTEMPTS) {
|
|
60
|
-
this.reconnecting = true;
|
|
61
|
-
this.reconnectAttempts++;
|
|
62
|
-
(0, dist_1.log_info)(`Attempting to reconnect WebSocket (${this.reconnectAttempts}/${this.MAX_RECONNECT_ATTEMPTS})`);
|
|
63
|
-
setTimeout(() => {
|
|
64
|
-
this.reconnecting = false;
|
|
65
|
-
this.getWsProvider(wsEndpoint, network);
|
|
66
|
-
}, this.RECONNECT_DELAY * this.reconnectAttempts);
|
|
67
|
-
}
|
|
68
|
-
else if (this.reconnectAttempts >= this.MAX_RECONNECT_ATTEMPTS) {
|
|
69
|
-
(0, dist_1.log_error)(`Maximum WebSocket reconnection attempts (${this.MAX_RECONNECT_ATTEMPTS}) reached`, new Error('Max reconnect attempts'));
|
|
70
|
-
this.wsConnections = 0;
|
|
71
|
-
this.reconnectAttempts = 0;
|
|
72
|
-
this.reconnecting = false;
|
|
73
|
-
}
|
|
77
|
+
this.wsConnections = 0;
|
|
74
78
|
}
|
|
75
79
|
incrementConnections() {
|
|
76
80
|
this.wsConnections++;
|
|
77
81
|
(0, dist_1.log_info)(`WebSocket active connections: ${this.wsConnections}`);
|
|
78
82
|
}
|
|
79
83
|
decrementConnections() {
|
|
84
|
+
var _a, _b, _c, _d;
|
|
80
85
|
if (this.wsConnections > 0) {
|
|
81
86
|
this.wsConnections--;
|
|
82
87
|
}
|
|
83
88
|
(0, dist_1.log_info)(`WebSocket active connections: ${this.wsConnections}`);
|
|
84
89
|
if (this.wsConnections === 0 && this.wsProvider) {
|
|
85
90
|
(0, dist_1.log_info)('No active connections, closing WebSocket');
|
|
86
|
-
|
|
91
|
+
try {
|
|
92
|
+
this.wsProvider.removeAllListeners();
|
|
93
|
+
const rawWs = (_a = this.wsProvider) === null || _a === void 0 ? void 0 : _a._websocket;
|
|
94
|
+
(_b = rawWs === null || rawWs === void 0 ? void 0 : rawWs.terminate) === null || _b === void 0 ? void 0 : _b.call(rawWs);
|
|
95
|
+
(_d = (_c = this.wsProvider) === null || _c === void 0 ? void 0 : _c.destroy) === null || _d === void 0 ? void 0 : _d.call(_c);
|
|
96
|
+
}
|
|
97
|
+
catch (_e) { }
|
|
87
98
|
this.wsProvider = null;
|
|
88
|
-
this.reconnectAttempts = 0;
|
|
89
99
|
}
|
|
90
100
|
}
|
|
91
101
|
listenToEvent(eventName, handler) {
|
|
92
|
-
var _a;
|
|
93
102
|
if (!this.wsProvider) {
|
|
94
103
|
(0, dist_1.log_warn)('WebSocket provider not available for event listener');
|
|
95
104
|
return;
|
|
@@ -97,22 +106,42 @@ class WebSocketManager {
|
|
|
97
106
|
if (!this.eventListeners.has(eventName)) {
|
|
98
107
|
this.eventListeners.set(eventName, new Set());
|
|
99
108
|
}
|
|
100
|
-
|
|
101
|
-
|
|
109
|
+
const wrapped = (...args) => {
|
|
110
|
+
try {
|
|
111
|
+
handler(...args);
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
this.decrementConnections();
|
|
115
|
+
const set = this.eventListeners.get(eventName);
|
|
116
|
+
if (set) {
|
|
117
|
+
set.delete(wrapped);
|
|
118
|
+
if (set.size === 0)
|
|
119
|
+
this.eventListeners.delete(eventName);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
wrapped.original = handler;
|
|
124
|
+
this.eventListeners.get(eventName).add(wrapped);
|
|
125
|
+
this.wsProvider.once(eventName, wrapped);
|
|
102
126
|
this.incrementConnections();
|
|
103
127
|
}
|
|
104
128
|
removeEventListener(eventName, handler) {
|
|
105
129
|
if (!this.wsProvider)
|
|
106
130
|
return;
|
|
107
|
-
this.wsProvider.removeListener(eventName, handler);
|
|
108
131
|
const handlers = this.eventListeners.get(eventName);
|
|
109
132
|
if (handlers) {
|
|
110
|
-
|
|
133
|
+
for (const h of Array.from(handlers)) {
|
|
134
|
+
const orig = h.original;
|
|
135
|
+
if (h === handler || orig === handler) {
|
|
136
|
+
this.wsProvider.removeListener(eventName, h);
|
|
137
|
+
handlers.delete(h);
|
|
138
|
+
this.decrementConnections();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
111
141
|
if (handlers.size === 0) {
|
|
112
142
|
this.eventListeners.delete(eventName);
|
|
113
143
|
}
|
|
114
144
|
}
|
|
115
|
-
this.decrementConnections();
|
|
116
145
|
}
|
|
117
146
|
}
|
|
118
147
|
exports.WebSocketManager = WebSocketManager;
|