@nexustechpro/baileys 1.0.3-rc.1 → 1.0.3

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.
@@ -36,7 +36,7 @@ export const PROCESSABLE_HISTORY_TYPES = [
36
36
  ];
37
37
  export const DEFAULT_CONNECTION_CONFIG = {
38
38
  version: version,
39
- browser: Browsers.macOS('Chrome'),
39
+ browser: Browsers.macOS('Safari'),
40
40
  waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
41
41
  connectTimeoutMs: 20000,
42
42
  keepAliveIntervalMs: 30000,
@@ -1,91 +1,122 @@
1
- import WebSocket from 'ws';
2
- import { DEFAULT_ORIGIN } from '../../Defaults/index.js';
3
- import { AbstractSocketClient } from './types.js';
1
+ import WebSocket from 'ws'
2
+ import { DEFAULT_ORIGIN } from '../../Defaults/index.js'
3
+ import { AbstractSocketClient } from './types.js'
4
+
4
5
  export class WebSocketClient extends AbstractSocketClient {
5
- constructor() {
6
- super(...arguments);
7
- this.socket = null;
8
- // queue & dispatch variables for throttling outgoing messages to avoid rate limits
9
- this._queue = [];
10
- this._isDispatching = false;
11
- this._lastDispatch = 0;
12
- this._minSendIntervalMs = 50; // 50ms minimum interval between sends
6
+ constructor() {
7
+ super(...arguments)
8
+ this.socket = null
9
+ this._queue = []
10
+ this._isDispatching = false
11
+ this._lastDispatch = 0
12
+ this._minSendIntervalMs = 50
13
+ this._reconnectTimeout = null
14
+ }
15
+
16
+ get isOpen() {
17
+ return this.socket?.readyState === WebSocket.OPEN
18
+ }
19
+
20
+ get isClosed() {
21
+ return this.socket === null || this.socket?.readyState === WebSocket.CLOSED
22
+ }
23
+
24
+ get isClosing() {
25
+ return this.socket === null || this.socket?.readyState === WebSocket.CLOSING
26
+ }
27
+
28
+ get isConnecting() {
29
+ return this.socket?.readyState === WebSocket.CONNECTING
30
+ }
31
+
32
+ async connect() {
33
+ if (this.socket) return
34
+
35
+ this.socket = new WebSocket(this.url, {
36
+ origin: DEFAULT_ORIGIN,
37
+ headers: this.config.options?.headers,
38
+ handshakeTimeout: this.config.connectTimeoutMs,
39
+ timeout: this.config.connectTimeoutMs,
40
+ agent: this.config.agent
41
+ })
42
+
43
+ this.socket.setMaxListeners(0)
44
+
45
+ const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']
46
+ for (const event of events) {
47
+ this.socket?.on(event, (...args) => this.emit(event, ...args))
13
48
  }
14
- get isOpen() {
15
- return this.socket?.readyState === WebSocket.OPEN;
49
+ }
50
+
51
+ async close() {
52
+ if (!this.socket) return
53
+
54
+ if (this._reconnectTimeout) {
55
+ clearTimeout(this._reconnectTimeout)
56
+ this._reconnectTimeout = null
16
57
  }
17
- get isClosed() {
18
- return this.socket === null || this.socket?.readyState === WebSocket.CLOSED;
58
+
59
+ this.socket.close()
60
+ this.socket = null
61
+ this._queue = []
62
+ }
63
+
64
+ async restart() {
65
+ if (this.socket) {
66
+ await new Promise(resolve => {
67
+ this.socket.once('close', resolve)
68
+ this.socket.terminate()
69
+ })
70
+ this.socket = null
19
71
  }
20
- get isClosing() {
21
- return this.socket === null || this.socket?.readyState === WebSocket.CLOSING;
72
+ this._queue = []
73
+ await this.connect()
74
+ }
75
+
76
+ send(str, cb) {
77
+ const doSend = () => {
78
+ if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
79
+ if (cb) cb(new Error('Socket not open'))
80
+ return false
81
+ }
82
+
83
+ try {
84
+ this.socket.send(str, (err) => {
85
+ if (err && cb) cb(err)
86
+ else if (cb) cb()
87
+ })
88
+ return true
89
+ } catch (error) {
90
+ if (cb) cb(error)
91
+ return false
92
+ }
22
93
  }
23
- get isConnecting() {
24
- return this.socket?.readyState === WebSocket.CONNECTING;
94
+
95
+ this._queue.push(doSend)
96
+ this._dispatch()
97
+ return true
98
+ }
99
+
100
+ _dispatch() {
101
+ if (this._isDispatching) return
102
+
103
+ const now = Date.now()
104
+ const timeSinceLastDispatch = now - this._lastDispatch
105
+
106
+ if (this._queue.length > 0 && timeSinceLastDispatch >= this._minSendIntervalMs) {
107
+ this._isDispatching = true
108
+ const fn = this._queue.shift()
109
+ const success = fn()
110
+ this._lastDispatch = Date.now()
111
+ this._isDispatching = false
112
+
113
+ if (this._queue.length > 0) {
114
+ const delay = Math.max(0, this._minSendIntervalMs - (Date.now() - this._lastDispatch))
115
+ setTimeout(() => this._dispatch(), delay)
116
+ }
117
+ } else if (this._queue.length > 0) {
118
+ const delay = Math.max(0, this._minSendIntervalMs - timeSinceLastDispatch)
119
+ setTimeout(() => this._dispatch(), delay)
25
120
  }
26
- async connect() {
27
- if (this.socket) {
28
- return;
29
- }
30
- this.socket = new WebSocket(this.url, {
31
- origin: DEFAULT_ORIGIN,
32
- headers: this.config.options?.headers,
33
- handshakeTimeout: this.config.connectTimeoutMs,
34
- timeout: this.config.connectTimeoutMs,
35
- agent: this.config.agent
36
- });
37
- this.socket.setMaxListeners(0);
38
- const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response'];
39
- for (const event of events) {
40
- this.socket?.on(event, (...args) => this.emit(event, ...args));
41
- }
42
- }
43
- async close() {
44
- if (!this.socket) {
45
- return;
46
- }
47
- this.socket.close();
48
- this.socket = null;
49
- }
50
- async restart() {
51
- if (this.socket) {
52
- await new Promise(resolve => {
53
- this.socket.once('close', resolve);
54
- this.socket.terminate();
55
- });
56
- this.socket = null;
57
- }
58
- await this.connect();
59
- }
60
- send(str, cb) {
61
- // throttle sends to reduce rate-limit likelihood
62
- const doSend = () => {
63
- this.socket?.send(str, cb);
64
- return Boolean(this.socket);
65
- };
66
- this._queue.push(doSend);
67
- this._dispatch();
68
- return true;
69
- }
70
- _dispatch() {
71
- if (this._isDispatching) {
72
- return;
73
- }
74
- const now = Date.now();
75
- if (this._queue.length > 0 && (now - this._lastDispatch) >= this._minSendIntervalMs) {
76
- this._isDispatching = true;
77
- const fn = this._queue.shift();
78
- fn();
79
- this._lastDispatch = Date.now();
80
- this._isDispatching = false;
81
- // continue dispatching if queue not empty
82
- if (this._queue.length > 0) {
83
- setTimeout(() => this._dispatch(), this._minSendIntervalMs - (Date.now() - this._lastDispatch));
84
- }
85
- }
86
- else if (this._queue.length > 0 && !this._isDispatching) {
87
- setTimeout(() => this._dispatch(), this._minSendIntervalMs - (now - this._lastDispatch));
88
- }
89
- }
90
- }
91
- //# sourceMappingURL=websocket.js.map
121
+ }
122
+ }