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