@clonegod/ttd-bsc-common 1.0.54 → 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 = 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
+ }
16
26
  static getInstance() {
17
27
  if (!WebSocketManager.instance) {
18
28
  WebSocketManager.instance = new WebSocketManager();
@@ -20,75 +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 && !this.reconnecting) {
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
- this.handleWsError(error, wsEndpoint, network);
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.reconnectAttempts = 0;
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)('Failed to create WebSocket provider', 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
- if (this.reconnecting)
53
- return;
66
+ var _a, _b, _c, _d;
54
67
  if (this.wsProvider) {
55
- this.wsProvider.removeAllListeners();
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
- if (this.wsConnections > 0 && this.reconnectAttempts < this.MAX_RECONNECT_ATTEMPTS) {
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
- }
77
+ this.wsConnections = 0;
73
78
  }
74
79
  incrementConnections() {
75
80
  this.wsConnections++;
76
81
  (0, dist_1.log_info)(`WebSocket active connections: ${this.wsConnections}`);
77
82
  }
78
83
  decrementConnections() {
84
+ var _a, _b, _c, _d;
79
85
  if (this.wsConnections > 0) {
80
86
  this.wsConnections--;
81
87
  }
82
88
  (0, dist_1.log_info)(`WebSocket active connections: ${this.wsConnections}`);
83
89
  if (this.wsConnections === 0 && this.wsProvider) {
84
90
  (0, dist_1.log_info)('No active connections, closing WebSocket');
85
- this.wsProvider.removeAllListeners();
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) { }
86
98
  this.wsProvider = null;
87
- this.reconnectAttempts = 0;
88
99
  }
89
100
  }
90
101
  listenToEvent(eventName, handler) {
91
- var _a;
92
102
  if (!this.wsProvider) {
93
103
  (0, dist_1.log_warn)('WebSocket provider not available for event listener');
94
104
  return;
@@ -96,22 +106,42 @@ class WebSocketManager {
96
106
  if (!this.eventListeners.has(eventName)) {
97
107
  this.eventListeners.set(eventName, new Set());
98
108
  }
99
- (_a = this.eventListeners.get(eventName)) === null || _a === void 0 ? void 0 : _a.add(handler);
100
- this.wsProvider.once(eventName, handler);
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);
101
126
  this.incrementConnections();
102
127
  }
103
128
  removeEventListener(eventName, handler) {
104
129
  if (!this.wsProvider)
105
130
  return;
106
- this.wsProvider.removeListener(eventName, handler);
107
131
  const handlers = this.eventListeners.get(eventName);
108
132
  if (handlers) {
109
- handlers.delete(handler);
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
+ }
110
141
  if (handlers.size === 0) {
111
142
  this.eventListeners.delete(eventName);
112
143
  }
113
144
  }
114
- this.decrementConnections();
115
145
  }
116
146
  }
117
147
  exports.WebSocketManager = WebSocketManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-bsc-common",
3
- "version": "1.0.54",
3
+ "version": "1.0.55",
4
4
  "description": "BSC common library",
5
5
  "license": "UNLICENSED",
6
6
  "main": "dist/index.js",