violetics 7.0.14-alpha → 7.0.16-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.
@@ -42,8 +42,45 @@ export class WebSocketClient extends AbstractSocketClient {
42
42
  this.socketListeners.set(event, handler);
43
43
  this.socket?.on(event, handler);
44
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));
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();
45
78
  }
79
+
46
80
  async close() {
81
+ this.reconnecting = false;
82
+ this.reconnectAttempts = 0;
83
+
47
84
  if (!this.socket) {
48
85
  return;
49
86
  }
@@ -54,13 +91,33 @@ export class WebSocketClient extends AbstractSocketClient {
54
91
  const closePromise = new Promise(resolve => {
55
92
  this.socket?.once('close', resolve);
56
93
  });
57
- this.socket.close();
58
- 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
+
59
104
  this.socket = null;
60
105
  }
106
+
61
107
  send(str, cb) {
62
108
  this.socket?.send(str, cb);
63
109
  return Boolean(this.socket);
64
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
+ }
65
122
  }
66
123
  //# sourceMappingURL=websocket.js.map
@@ -87,7 +87,7 @@ export const makeMessagesRecvSocket = (config) => {
87
87
  setTimeout(async () => {
88
88
  if (await placeholderResendCache.get(messageKey?.id)) {
89
89
  logger.debug({ messageKey }, 'PDO message without response after 8 seconds. Phone possibly offline');
90
- await placeholderResendCache.del(messageKey?.id);
90
+ await placeholderResendCache.delete(messageKey?.id);
91
91
  }
92
92
  }, 8000);
93
93
  return sendPeerDataOperationMessage(pdoMessage);
@@ -297,7 +297,7 @@ export const makeMessagesRecvSocket = (config) => {
297
297
  let retryCount = (await msgRetryCache.get(key)) || 0;
298
298
  if (retryCount >= maxMsgRetryCount) {
299
299
  logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');
300
- await msgRetryCache.del(key);
300
+ await msgRetryCache.delete(key);
301
301
  return;
302
302
  }
303
303
  retryCount += 1;
@@ -1509,7 +1509,7 @@ export const makeMessagesRecvSocket = (config) => {
1509
1509
  }
1510
1510
  // delete data once call has ended
1511
1511
  if (status === 'reject' || status === 'accept' || status === 'timeout' || status === 'terminate') {
1512
- await callOfferCache.del(call.id);
1512
+ await callOfferCache.delete(call.id);
1513
1513
  }
1514
1514
  ev.emit('call', [call]);
1515
1515
  await sendMessageAck(node);
@@ -238,7 +238,7 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
238
238
  const cachedData = msgId ? await placeholderResendCache?.get(msgId) : undefined;
239
239
  //eslint-disable-next-line max-depth
240
240
  if (msgId) {
241
- await placeholderResendCache?.del(msgId);
241
+ await placeholderResendCache?.delete(msgId);
242
242
  }
243
243
  let finalMsg;
244
244
  //eslint-disable-next-line max-depth
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "violetics",
3
3
  "type": "module",
4
- "version": "7.0.14-alpha",
4
+ "version": "7.0.16-alpha",
5
5
  "description": "A WebSockets library for interacting with WhatsApp Web",
6
6
  "keywords": [
7
7
  "whatsapp",