@nexustechpro/baileys 2.0.2 → 2.0.6

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.
Files changed (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/WAProto/index.js +22 -18
  4. package/lib/Defaults/baileys-version.json +6 -2
  5. package/lib/Defaults/index.js +173 -172
  6. package/lib/Signal/libsignal.js +395 -292
  7. package/lib/Signal/lid-mapping.js +264 -171
  8. package/lib/Socket/Client/index.js +2 -2
  9. package/lib/Socket/Client/types.js +10 -10
  10. package/lib/Socket/Client/websocket.js +45 -310
  11. package/lib/Socket/business.js +375 -375
  12. package/lib/Socket/chats.js +916 -963
  13. package/lib/Socket/communities.js +430 -430
  14. package/lib/Socket/groups.js +342 -342
  15. package/lib/Socket/index.js +21 -22
  16. package/lib/Socket/messages-recv.js +963 -743
  17. package/lib/Socket/messages-send.js +273 -321
  18. package/lib/Socket/mex.js +50 -50
  19. package/lib/Socket/newsletter.js +148 -148
  20. package/lib/Socket/nexus-handler.js +296 -247
  21. package/lib/Socket/registration.js +50 -33
  22. package/lib/Socket/socket.js +872 -1201
  23. package/lib/Store/index.js +5 -5
  24. package/lib/Store/make-cache-manager-store.js +81 -81
  25. package/lib/Store/make-in-memory-store.js +416 -416
  26. package/lib/Store/make-ordered-dictionary.js +81 -81
  27. package/lib/Store/object-repository.js +30 -30
  28. package/lib/Types/Auth.js +1 -1
  29. package/lib/Types/Bussines.js +1 -1
  30. package/lib/Types/Call.js +1 -1
  31. package/lib/Types/Chat.js +7 -7
  32. package/lib/Types/Contact.js +1 -1
  33. package/lib/Types/Events.js +1 -1
  34. package/lib/Types/GroupMetadata.js +1 -1
  35. package/lib/Types/Label.js +24 -24
  36. package/lib/Types/LabelAssociation.js +6 -6
  37. package/lib/Types/Message.js +10 -10
  38. package/lib/Types/Newsletter.js +37 -29
  39. package/lib/Types/Product.js +1 -1
  40. package/lib/Types/Signal.js +1 -1
  41. package/lib/Types/Socket.js +2 -2
  42. package/lib/Types/State.js +55 -12
  43. package/lib/Types/USync.js +1 -1
  44. package/lib/Types/index.js +25 -25
  45. package/lib/Utils/auth-utils.js +264 -256
  46. package/lib/Utils/baileys-event-stream.js +55 -55
  47. package/lib/Utils/browser-utils.js +27 -27
  48. package/lib/Utils/business.js +228 -230
  49. package/lib/Utils/chat-utils.js +726 -764
  50. package/lib/Utils/companion-reg-client-utils.js +34 -0
  51. package/lib/Utils/crypto.js +109 -135
  52. package/lib/Utils/decode-wa-message.js +342 -314
  53. package/lib/Utils/event-buffer.js +547 -547
  54. package/lib/Utils/generics.js +295 -297
  55. package/lib/Utils/history.js +91 -83
  56. package/lib/Utils/index.js +25 -20
  57. package/lib/Utils/key-store.js +17 -0
  58. package/lib/Utils/link-preview.js +107 -98
  59. package/lib/Utils/logger.js +2 -2
  60. package/lib/Utils/lt-hash.js +47 -47
  61. package/lib/Utils/make-mutex.js +39 -39
  62. package/lib/Utils/message-retry-manager.js +148 -148
  63. package/lib/Utils/messages-media.js +579 -535
  64. package/lib/Utils/messages.js +821 -706
  65. package/lib/Utils/noise-handler.js +255 -255
  66. package/lib/Utils/pre-key-manager.js +105 -105
  67. package/lib/Utils/process-message.js +430 -412
  68. package/lib/Utils/reporting-utils.js +155 -0
  69. package/lib/Utils/signal.js +191 -159
  70. package/lib/Utils/sync-action-utils.js +33 -0
  71. package/lib/Utils/tc-token-utils.js +162 -0
  72. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  73. package/lib/Utils/validate-connection.js +194 -194
  74. package/lib/WABinary/constants.js +1306 -1300
  75. package/lib/WABinary/decode.js +237 -237
  76. package/lib/WABinary/encode.js +232 -232
  77. package/lib/WABinary/generic-utils.js +252 -211
  78. package/lib/WABinary/index.js +6 -5
  79. package/lib/WABinary/jid-utils.js +279 -95
  80. package/lib/WABinary/types.js +1 -1
  81. package/lib/WAM/BinaryInfo.js +9 -9
  82. package/lib/WAM/constants.js +22852 -22852
  83. package/lib/WAM/encode.js +149 -149
  84. package/lib/WAM/index.js +3 -3
  85. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  86. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  87. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  88. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  89. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  90. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  91. package/lib/WAUSync/Protocols/index.js +4 -4
  92. package/lib/WAUSync/USyncQuery.js +93 -93
  93. package/lib/WAUSync/USyncUser.js +22 -22
  94. package/lib/WAUSync/index.js +3 -3
  95. package/lib/index.js +65 -66
  96. package/package.json +172 -143
  97. package/lib/Signal/Group/ciphertext-message.js +0 -12
  98. package/lib/Signal/Group/group-session-builder.js +0 -30
  99. package/lib/Signal/Group/group_cipher.js +0 -100
  100. package/lib/Signal/Group/index.js +0 -12
  101. package/lib/Signal/Group/keyhelper.js +0 -18
  102. package/lib/Signal/Group/sender-chain-key.js +0 -26
  103. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  104. package/lib/Signal/Group/sender-key-message.js +0 -66
  105. package/lib/Signal/Group/sender-key-name.js +0 -48
  106. package/lib/Signal/Group/sender-key-record.js +0 -41
  107. package/lib/Signal/Group/sender-key-state.js +0 -84
  108. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,311 +1,46 @@
1
- import WebSocket from 'ws'
2
- import { DEFAULT_ORIGIN } from '../../Defaults/index.js'
3
- import { AbstractSocketClient } from './types.js'
4
-
5
- // ==================== CONSTANTS ====================
6
- const CONSTANTS = {
7
- MIN_SEND_INTERVAL_MS: 50,
8
- MAX_RECONNECT_ATTEMPTS: 5,
9
- INITIAL_RECONNECT_DELAY: 1000,
10
- MAX_RECONNECT_DELAY: 30000,
11
- RECONNECT_BACKOFF_MULTIPLIER: 2
12
- }
13
-
14
- // ==================== WEBSOCKET CLIENT ====================
15
- export class WebSocketClient extends AbstractSocketClient {
16
- constructor() {
17
- super(...arguments)
18
-
19
- // Core socket
20
- this.socket = null
21
-
22
- // Message queue
23
- this._queue = []
24
- this._isDispatching = false
25
- this._lastDispatch = 0
26
- this._minSendIntervalMs = CONSTANTS.MIN_SEND_INTERVAL_MS
27
-
28
- // Reconnection state
29
- this._reconnectTimeout = null
30
- this._reconnectAttempts = 0
31
- this._maxReconnectAttempts = CONSTANTS.MAX_RECONNECT_ATTEMPTS
32
- this._reconnectDelay = CONSTANTS.INITIAL_RECONNECT_DELAY
33
- this._shouldReconnect = true
34
- this._isManualClose = false
35
- this._isReconnecting = false
36
- }
37
-
38
- // ==================== LOGGER ====================
39
- get logger() {
40
- return this.config?.logger || console
41
- }
42
-
43
- // ==================== CONNECTION STATE ====================
44
- get isOpen() {
45
- return this.socket?.readyState === WebSocket.OPEN
46
- }
47
-
48
- get isClosed() {
49
- return !this.socket || this.socket.readyState === WebSocket.CLOSED
50
- }
51
-
52
- get isClosing() {
53
- return !this.socket || this.socket.readyState === WebSocket.CLOSING
54
- }
55
-
56
- get isConnecting() {
57
- return this.socket?.readyState === WebSocket.CONNECTING
58
- }
59
-
60
- // ==================== CONNECTION MANAGEMENT ====================
61
- async connect() {
62
- if (this.socket && !this.isClosed) {
63
- this.logger.debug({ state: this.socket.readyState }, 'already connected or connecting')
64
- return
65
- }
66
-
67
- try {
68
- this.logger.debug('establishing websocket connection')
69
-
70
- this.socket = new WebSocket(this.url, {
71
- origin: DEFAULT_ORIGIN,
72
- headers: this.config.options?.headers,
73
- handshakeTimeout: this.config.connectTimeoutMs,
74
- timeout: this.config.connectTimeoutMs,
75
- agent: this.config.agent
76
- })
77
-
78
- if (!this.socket) {
79
- throw new Error('WebSocket creation failed')
80
- }
81
-
82
- this.socket.setMaxListeners(0)
83
-
84
- // Forward all WebSocket events
85
- const events = ['error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']
86
- events.forEach(e => {
87
- this.socket?.on(e, (...args) => this.emit(e, ...args))
88
- })
89
-
90
- // Handle close with auto-reconnect
91
- this.socket.on('close', (...args) => {
92
- this.emit('close', ...args)
93
- if (this._shouldReconnect && !this._isManualClose) {
94
- this._attemptReconnect()
95
- }
96
- })
97
-
98
- // Handle successful connection
99
- this.socket.on('open', () => {
100
- this.logger.info('websocket connection established')
101
- this._reconnectAttempts = 0
102
- this._reconnectDelay = CONSTANTS.INITIAL_RECONNECT_DELAY
103
- this._isReconnecting = false
104
-
105
- // Process any queued messages
106
- if (this._queue.length > 0) {
107
- this.logger.debug({ queueLength: this._queue.length }, 'processing queued messages')
108
- this._dispatch()
109
- }
110
- })
111
-
112
- } catch (error) {
113
- this.logger.error({ error: error.message }, 'websocket connection failed')
114
- this.socket = null
115
- throw error
116
- }
117
- }
118
-
119
- async close() {
120
- this.logger.debug('closing websocket connection (manual)')
121
-
122
- this._isManualClose = true
123
- this._shouldReconnect = false
124
- this._isReconnecting = false
125
-
126
- if (this._reconnectTimeout) {
127
- clearTimeout(this._reconnectTimeout)
128
- this._reconnectTimeout = null
129
- }
130
-
131
- this.socket?.close?.()
132
- this.socket = null
133
- this._queue = []
134
- }
135
-
136
- async restart() {
137
- this.logger.info('restarting websocket connection')
138
-
139
- this._isManualClose = true
140
- this._isReconnecting = false
141
-
142
- // Force close existing connection
143
- if (this.socket) {
144
- await new Promise(resolve => {
145
- this.socket.once('close', resolve)
146
- this.socket.terminate()
147
- })
148
- this.socket = null
149
- }
150
-
151
- // Clear queue and reset state
152
- this._queue = []
153
- this._reconnectDelay = CONSTANTS.INITIAL_RECONNECT_DELAY
154
- this._isManualClose = false
155
- this._shouldReconnect = true
156
-
157
- // Reconnect
158
- await this.connect()
159
- }
160
-
161
- // ==================== RECONNECTION LOGIC ====================
162
- _attemptReconnect() {
163
- if (this._isReconnecting) {
164
- this.logger.trace('reconnection already in progress')
165
- return
166
- }
167
-
168
- if (this._reconnectAttempts >= this._maxReconnectAttempts) {
169
- this.logger.error(
170
- { attempts: this._reconnectAttempts, max: this._maxReconnectAttempts },
171
- 'max reconnect attempts reached'
172
- )
173
- this.emit('reconnect-failed')
174
- return
175
- }
176
-
177
- if (this._reconnectTimeout) {
178
- clearTimeout(this._reconnectTimeout)
179
- }
180
-
181
- this._isReconnecting = true
182
- this._reconnectAttempts++
183
-
184
- this.logger.info(
185
- {
186
- attempt: this._reconnectAttempts,
187
- maxAttempts: this._maxReconnectAttempts,
188
- delay: this._reconnectDelay
189
- },
190
- 'attempting websocket reconnection'
191
- )
192
-
193
- this._reconnectTimeout = setTimeout(async () => {
194
- try {
195
- await this.connect()
196
- } catch (error) {
197
- this.logger.warn({ error: error.message }, 'reconnection attempt failed')
198
- this._isReconnecting = false
199
-
200
- if (this._reconnectAttempts < this._maxReconnectAttempts) {
201
- this._attemptReconnect()
202
- } else {
203
- this.emit('reconnect-failed')
204
- }
205
- }
206
- }, this._reconnectDelay)
207
-
208
- // Exponential backoff with cap
209
- this._reconnectDelay = Math.min(
210
- this._reconnectDelay * CONSTANTS.RECONNECT_BACKOFF_MULTIPLIER,
211
- CONSTANTS.MAX_RECONNECT_DELAY
212
- )
213
- }
214
-
215
- // ==================== MESSAGE SENDING ====================
216
- send(str, cb) {
217
- const doSend = () => {
218
- // Handle closed/closing socket
219
- if (this.isClosed || this.isClosing) {
220
- this.logger.warn('socket closed, attempting reconnection')
221
-
222
- this._isManualClose = false
223
- this._shouldReconnect = true
224
- this._attemptReconnect()
225
- this._queue.unshift(doSend) // Re-queue at front
226
-
227
- cb?.(new Error('Socket closed, reconnecting...'))
228
- return false
229
- }
230
-
231
- // Check if socket is ready
232
- if (!this.socket || !this.isOpen) {
233
- cb?.(new Error('Socket not open'))
234
- return false
235
- }
236
-
237
- // Send the message
238
- try {
239
- this.socket.send(str, cb)
240
- return true
241
- } catch (error) {
242
- this.logger.error({ error: error.message }, 'failed to send message')
243
- cb?.(error)
244
- return false
245
- }
246
- }
247
-
248
- this._queue.push(doSend)
249
- this._dispatch()
250
- return true
251
- }
252
-
253
- // ==================== MESSAGE QUEUE DISPATCH ====================
254
- _dispatch() {
255
- // Don't dispatch if already dispatching or socket not ready
256
- if (this._isDispatching || (!this.isOpen && !this.isConnecting)) {
257
- return
258
- }
259
-
260
- const now = Date.now()
261
- const elapsed = now - this._lastDispatch
262
-
263
- // Check if enough time has passed and queue has items
264
- if (this._queue.length && elapsed >= this._minSendIntervalMs) {
265
- this._isDispatching = true
266
-
267
- const sendFn = this._queue.shift()
268
- sendFn?.()
269
-
270
- this._lastDispatch = Date.now()
271
- this._isDispatching = false
272
-
273
- // Schedule next dispatch if queue not empty
274
- if (this._queue.length) {
275
- const nextDelay = Math.max(0, this._minSendIntervalMs - (Date.now() - this._lastDispatch))
276
- setTimeout(() => this._dispatch(), nextDelay)
277
- }
278
- } else if (this._queue.length) {
279
- // Schedule dispatch after required interval
280
- const delay = Math.max(0, this._minSendIntervalMs - elapsed)
281
- setTimeout(() => this._dispatch(), delay)
282
- }
283
- }
284
-
285
- // ==================== PUBLIC CONTROL METHODS ====================
286
- disableAutoReconnect() {
287
- this.logger.info('auto-reconnect disabled')
288
- this._shouldReconnect = false
289
- }
290
-
291
- enableAutoReconnect() {
292
- this.logger.info('auto-reconnect enabled')
293
- this._shouldReconnect = true
294
- this._isManualClose = false
295
- }
296
-
297
- // Getters for external monitoring
298
- get reconnectAttempts() {
299
- return this._reconnectAttempts
300
- }
301
-
302
- get queueLength() {
303
- return this._queue.length
304
- }
305
-
306
- get isReconnecting() {
307
- return this._isReconnecting
308
- }
309
- }
310
-
1
+ import WebSocket from 'ws'
2
+ import { DEFAULT_ORIGIN } from '../../Defaults/index.js'
3
+ import { AbstractSocketClient } from './types.js'
4
+
5
+ export class WebSocketClient extends AbstractSocketClient {
6
+ constructor() {
7
+ super(...arguments)
8
+ this.socket = null
9
+ }
10
+
11
+ get isOpen() { return this.socket?.readyState === WebSocket.OPEN }
12
+ get isClosed() { return this.socket === null || this.socket?.readyState === WebSocket.CLOSED }
13
+ get isClosing() { return this.socket === null || this.socket?.readyState === WebSocket.CLOSING }
14
+ get isConnecting() { return this.socket?.readyState === WebSocket.CONNECTING }
15
+
16
+ connect() {
17
+ if (this.socket) return
18
+ this.socket = new WebSocket(this.url, {
19
+ origin: DEFAULT_ORIGIN,
20
+ headers: this.config.options?.headers,
21
+ handshakeTimeout: this.config.connectTimeoutMs,
22
+ timeout: this.config.connectTimeoutMs,
23
+ agent: this.config.agent
24
+ })
25
+ this.socket.setMaxListeners(0)
26
+ const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']
27
+ for (const event of events) { this.socket?.on(event, (...args) => this.emit(event, ...args)) }
28
+ }
29
+
30
+ async close() {
31
+ if (!this.socket) return
32
+ const closePromise = new Promise(resolve => {
33
+ this.socket?.once('close', resolve)
34
+ })
35
+ this.socket.close()
36
+ await closePromise
37
+ this.socket = null
38
+ }
39
+
40
+ send(str, cb) {
41
+ this.socket?.send(str, cb)
42
+ return Boolean(this.socket)
43
+ }
44
+ }
45
+
311
46
  export default WebSocketClient