violetics 7.0.13-alpha → 7.0.15-alpha

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.
@@ -6,6 +6,11 @@ export class WebSocketClient extends AbstractSocketClient {
6
6
  super(...arguments);
7
7
  this.socket = null;
8
8
  this.socketListeners = new Map();
9
+ this.reconnectAttempts = 0;
10
+ this.maxReconnectAttempts = this.config.maxReconnectAttempts || 5;
11
+ this.reconnectDelay = this.config.reconnectDelay || 1000;
12
+ this.maxReconnectDelay = this.config.maxReconnectDelay || 30000;
13
+ this.reconnecting = false;
9
14
  }
10
15
  get isOpen() {
11
16
  return this.socket?.readyState === WebSocket.OPEN;
@@ -37,8 +42,45 @@ export class WebSocketClient extends AbstractSocketClient {
37
42
  this.socketListeners.set(event, handler);
38
43
  this.socket?.on(event, handler);
39
44
  }
45
+
46
+ this.socket.on('close', (code, reason) => {
47
+ this.emit('close', code, reason);
48
+ this.attemptReconnect(code, reason);
49
+ });
50
+ this.socketListeners.set('close-internal', (...args) => this.socket?.emit('close', ...args));
40
51
  }
52
+
53
+ async attemptReconnect(code, reason) {
54
+ if (this.reconnecting || code === 1000 || this.reconnectAttempts >= this.maxReconnectAttempts) {
55
+ return;
56
+ }
57
+
58
+ this.reconnecting = true;
59
+ const delay = Math.min(
60
+ this.reconnectDelay * Math.pow(2, this.reconnectAttempts),
61
+ this.maxReconnectDelay
62
+ );
63
+
64
+ this.emit('reconnecting', {
65
+ attempt: this.reconnectAttempts + 1,
66
+ maxAttempts: this.maxReconnectAttempts,
67
+ delay,
68
+ code,
69
+ reason: reason?.toString() || 'Connection closed'
70
+ });
71
+
72
+ await new Promise(resolve => setTimeout(resolve, delay));
73
+
74
+ this.reconnectAttempts++;
75
+ this.reconnecting = false;
76
+
77
+ this.connect();
78
+ }
79
+
41
80
  async close() {
81
+ this.reconnecting = false;
82
+ this.reconnectAttempts = 0;
83
+
42
84
  if (!this.socket) {
43
85
  return;
44
86
  }
@@ -49,13 +91,33 @@ export class WebSocketClient extends AbstractSocketClient {
49
91
  const closePromise = new Promise(resolve => {
50
92
  this.socket?.once('close', resolve);
51
93
  });
52
- this.socket.close();
53
- await closePromise;
94
+ this.socket.close(1000, 'Intentional close');
95
+
96
+ try {
97
+ await Promise.race([
98
+ closePromise,
99
+ new Promise((_, reject) => setTimeout(() => reject(new Error('close timeout')), 5000))
100
+ ]);
101
+ }
102
+ catch { }
103
+
54
104
  this.socket = null;
55
105
  }
106
+
56
107
  send(str, cb) {
57
108
  this.socket?.send(str, cb);
58
109
  return Boolean(this.socket);
59
110
  }
111
+
112
+ get connectionStats() {
113
+ return {
114
+ isOpen: this.isOpen,
115
+ isClosed: this.isClosed,
116
+ isConnecting: this.isConnecting,
117
+ reconnectAttempts: this.reconnectAttempts,
118
+ maxReconnectAttempts: this.maxReconnectAttempts,
119
+ isReconnecting: this.reconnecting
120
+ };
121
+ }
60
122
  }
61
123
  //# sourceMappingURL=websocket.js.map
@@ -33,6 +33,7 @@ export const makeEventBuffer = (logger, config = {}) => {
33
33
  let activeBufferedTimeouts = new Set();
34
34
  const MAX_HISTORY_CACHE_SIZE = config.maxHistoryCacheSize || 10000;
35
35
  const BUFFER_TIMEOUT_MS = config.bufferTimeoutMs || 30000;
36
+ const MAX_BUFFER_COUNT = config.maxBufferCount || 10000;
36
37
  // take the generic event and fire it as a baileys event
37
38
  ev.on('event', (map) => {
38
39
  for (const event in map) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "violetics",
3
3
  "type": "module",
4
- "version": "7.0.13-alpha",
4
+ "version": "7.0.15-alpha",
5
5
  "description": "A WebSockets library for interacting with WhatsApp Web",
6
6
  "keywords": [
7
7
  "whatsapp",