@bellaxchuu/flowred 0.8.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 (142) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +214 -0
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +6902 -0
  5. package/WAProto/fix-imports.js +85 -0
  6. package/WAProto/index.js +242946 -0
  7. package/engine-requirements.js +10 -0
  8. package/lib/Defaults/index.js +190 -0
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Signal/Group/ciphertext-message.js +15 -0
  11. package/lib/Signal/Group/group-session-builder.js +92 -0
  12. package/lib/Signal/Group/group_cipher.js +89 -0
  13. package/lib/Signal/Group/index.js +136 -0
  14. package/lib/Signal/Group/keyhelper.js +73 -0
  15. package/lib/Signal/Group/sender-chain-key.js +32 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  17. package/lib/Signal/Group/sender-key-message.js +69 -0
  18. package/lib/Signal/Group/sender-key-name.js +50 -0
  19. package/lib/Signal/Group/sender-key-record.js +44 -0
  20. package/lib/Signal/Group/sender-key-state.js +97 -0
  21. package/lib/Signal/Group/sender-message-key.js +30 -0
  22. package/lib/Signal/libsignal.js +490 -0
  23. package/lib/Signal/lid-mapping.js +262 -0
  24. package/lib/Socket/Client/index.js +30 -0
  25. package/lib/Socket/Client/types.js +13 -0
  26. package/lib/Socket/Client/websocket.js +62 -0
  27. package/lib/Socket/aigroups.js +240 -0
  28. package/lib/Socket/business.js +422 -0
  29. package/lib/Socket/chats.js +1816 -0
  30. package/lib/Socket/communities.js +549 -0
  31. package/lib/Socket/graphql.js +656 -0
  32. package/lib/Socket/groups.js +764 -0
  33. package/lib/Socket/index.js +39 -0
  34. package/lib/Socket/interactive-handler.js +522 -0
  35. package/lib/Socket/interop.js +549 -0
  36. package/lib/Socket/luxu.js +387 -0
  37. package/lib/Socket/managed-account.js +183 -0
  38. package/lib/Socket/messages-recv.js +2440 -0
  39. package/lib/Socket/messages-send.js +1853 -0
  40. package/lib/Socket/mex.js +60 -0
  41. package/lib/Socket/newsletter.js +820 -0
  42. package/lib/Socket/privacy.js +449 -0
  43. package/lib/Socket/registration.js +427 -0
  44. package/lib/Socket/socket.js +1092 -0
  45. package/lib/Socket/text-router.js +83 -0
  46. package/lib/Socket/username.js +243 -0
  47. package/lib/Store/index.js +36 -0
  48. package/lib/Store/keyed-db.js +108 -0
  49. package/lib/Store/make-cache-manager-store.js +90 -0
  50. package/lib/Store/make-in-memory-store.js +604 -0
  51. package/lib/Store/make-ordered-dictionary.js +81 -0
  52. package/lib/Store/object-repository.js +29 -0
  53. package/lib/Types/Auth.js +38 -0
  54. package/lib/Types/Bussines.js +2 -0
  55. package/lib/Types/Call.js +2 -0
  56. package/lib/Types/Chat.js +4 -0
  57. package/lib/Types/Contact.js +2 -0
  58. package/lib/Types/Events.js +2 -0
  59. package/lib/Types/GroupMetadata.js +2 -0
  60. package/lib/Types/Label.js +27 -0
  61. package/lib/Types/LabelAssociation.js +9 -0
  62. package/lib/Types/Message.js +95 -0
  63. package/lib/Types/Mex.js +112 -0
  64. package/lib/Types/Newsletter.js +121 -0
  65. package/lib/Types/Product.js +2 -0
  66. package/lib/Types/Signal.js +2 -0
  67. package/lib/Types/Socket.js +2 -0
  68. package/lib/Types/State.js +70 -0
  69. package/lib/Types/USync.js +2 -0
  70. package/lib/Types/index.js +55 -0
  71. package/lib/Utils/auth-utils.js +306 -0
  72. package/lib/Utils/browser-utils.js +114 -0
  73. package/lib/Utils/business.js +247 -0
  74. package/lib/Utils/chat-utils.js +1208 -0
  75. package/lib/Utils/command-loader.js +100 -0
  76. package/lib/Utils/companion-reg-client-utils.js +42 -0
  77. package/lib/Utils/consumer-application.js +107 -0
  78. package/lib/Utils/crypto.js +155 -0
  79. package/lib/Utils/curve25519-js.js +275 -0
  80. package/lib/Utils/decode-wa-message.js +560 -0
  81. package/lib/Utils/event-buffer.js +607 -0
  82. package/lib/Utils/generics.js +563 -0
  83. package/lib/Utils/group-history.js +60 -0
  84. package/lib/Utils/history.js +244 -0
  85. package/lib/Utils/identity-change-handler.js +52 -0
  86. package/lib/Utils/index.js +57 -0
  87. package/lib/Utils/jid-display-normalization.js +218 -0
  88. package/lib/Utils/link-preview.js +143 -0
  89. package/lib/Utils/logger.js +9 -0
  90. package/lib/Utils/lt-hash.js +39 -0
  91. package/lib/Utils/make-mutex.js +36 -0
  92. package/lib/Utils/message-composer.js +479 -0
  93. package/lib/Utils/message-retry-manager.js +270 -0
  94. package/lib/Utils/messages-media.js +896 -0
  95. package/lib/Utils/messages.js +2904 -0
  96. package/lib/Utils/meta-ai-msmsg.js +262 -0
  97. package/lib/Utils/native-bridge.js +82 -0
  98. package/lib/Utils/noise-handler.js +196 -0
  99. package/lib/Utils/offline-node-processor.js +42 -0
  100. package/lib/Utils/pre-key-manager.js +107 -0
  101. package/lib/Utils/process-message.js +1048 -0
  102. package/lib/Utils/reporting-utils.js +262 -0
  103. package/lib/Utils/session-pool.js +94 -0
  104. package/lib/Utils/signal.js +224 -0
  105. package/lib/Utils/stanza-ack.js +40 -0
  106. package/lib/Utils/sticker.js +145 -0
  107. package/lib/Utils/sync-action-utils.js +54 -0
  108. package/lib/Utils/tc-token-utils.js +161 -0
  109. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  110. package/lib/Utils/validate-connection.js +219 -0
  111. package/lib/Utils/view-once-cache.js +79 -0
  112. package/lib/Utils/voip-rekey.js +22 -0
  113. package/lib/WABinary/constants.js +1304 -0
  114. package/lib/WABinary/decode.js +343 -0
  115. package/lib/WABinary/encode.js +228 -0
  116. package/lib/WABinary/generic-utils.js +240 -0
  117. package/lib/WABinary/index.js +33 -0
  118. package/lib/WABinary/jid-utils.js +361 -0
  119. package/lib/WABinary/types.js +2 -0
  120. package/lib/WAM/BinaryInfo.js +13 -0
  121. package/lib/WAM/constants.js +39486 -0
  122. package/lib/WAM/encode.js +142 -0
  123. package/lib/WAM/index.js +31 -0
  124. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +55 -0
  125. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
  126. package/lib/WAUSync/Protocols/USyncContactProtocol.js +54 -0
  127. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  128. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  129. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +59 -0
  130. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +31 -0
  131. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
  132. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  133. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
  134. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  135. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +27 -0
  136. package/lib/WAUSync/Protocols/index.js +39 -0
  137. package/lib/WAUSync/USyncQuery.js +133 -0
  138. package/lib/WAUSync/USyncUser.js +50 -0
  139. package/lib/WAUSync/index.js +31 -0
  140. package/lib/antiban.js +4637 -0
  141. package/lib/index.js +45 -0
  142. package/package.json +107 -0
@@ -0,0 +1,270 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.MessageRetryManager = exports.RetryReason = void 0
4
+ const lru_cache_1 = require('lru-cache')
5
+ /** Number of sent messages to cache in memory for handling retry receipts */
6
+ const RECENT_MESSAGES_SIZE = 512
7
+ const MESSAGE_KEY_SEPARATOR = '\u0000'
8
+ /** Timeout for session recreation - 1 hour */
9
+ const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000 // 1 hour in milliseconds
10
+ const PHONE_REQUEST_DELAY = 3000
11
+ // Retry reason codes matching WhatsApp Web's Signal error codes.
12
+ var RetryReason
13
+ ;(function (RetryReason) {
14
+ RetryReason[(RetryReason['UnknownError'] = 0)] = 'UnknownError'
15
+ RetryReason[(RetryReason['SignalErrorNoSession'] = 1)] = 'SignalErrorNoSession'
16
+ RetryReason[(RetryReason['SignalErrorInvalidKey'] = 2)] = 'SignalErrorInvalidKey'
17
+ RetryReason[(RetryReason['SignalErrorInvalidKeyId'] = 3)] = 'SignalErrorInvalidKeyId'
18
+ /** MAC verification failed - most common cause of decryption failures */
19
+ RetryReason[(RetryReason['SignalErrorInvalidMessage'] = 4)] = 'SignalErrorInvalidMessage'
20
+ RetryReason[(RetryReason['SignalErrorInvalidSignature'] = 5)] = 'SignalErrorInvalidSignature'
21
+ RetryReason[(RetryReason['SignalErrorFutureMessage'] = 6)] = 'SignalErrorFutureMessage'
22
+ /** Explicit MAC failure - session is definitely out of sync */
23
+ RetryReason[(RetryReason['SignalErrorBadMac'] = 7)] = 'SignalErrorBadMac'
24
+ RetryReason[(RetryReason['SignalErrorInvalidSession'] = 8)] = 'SignalErrorInvalidSession'
25
+ RetryReason[(RetryReason['SignalErrorInvalidMsgKey'] = 9)] = 'SignalErrorInvalidMsgKey'
26
+ RetryReason[(RetryReason['BadBroadcastEphemeralSetting'] = 10)] = 'BadBroadcastEphemeralSetting'
27
+ RetryReason[(RetryReason['UnknownCompanionNoPrekey'] = 11)] = 'UnknownCompanionNoPrekey'
28
+ RetryReason[(RetryReason['AdvFailure'] = 12)] = 'AdvFailure'
29
+ RetryReason[(RetryReason['StatusRevokeDelay'] = 13)] = 'StatusRevokeDelay'
30
+ })(RetryReason || (exports.RetryReason = RetryReason = {}))
31
+ /** Error codes that indicate a MAC failure and require immediate session recreation */
32
+ const MAC_ERROR_CODES = new Set([RetryReason.SignalErrorInvalidMessage, RetryReason.SignalErrorBadMac])
33
+ class MessageRetryManager {
34
+ constructor(logger, maxMsgRetryCount) {
35
+ this.logger = logger
36
+ this.recentMessagesMap = new lru_cache_1.LRUCache({
37
+ max: RECENT_MESSAGES_SIZE,
38
+ ttl: 5 * 60 * 1000,
39
+ ttlAutopurge: true,
40
+ dispose: (_value, key) => {
41
+ const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR)
42
+ if (separatorIndex > -1) {
43
+ const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length)
44
+ this.messageKeyIndex.delete(messageId)
45
+ }
46
+ }
47
+ })
48
+ this.messageKeyIndex = new Map()
49
+ this.sessionRecreateHistory = new lru_cache_1.LRUCache({
50
+ ttl: RECREATE_SESSION_TIMEOUT * 2,
51
+ ttlAutopurge: true
52
+ })
53
+ this.retryCounters = new lru_cache_1.LRUCache({
54
+ ttl: 15 * 60 * 1000,
55
+ ttlAutopurge: true,
56
+ updateAgeOnGet: true
57
+ }) // 15 minutes TTL
58
+ this.baseKeys = new lru_cache_1.LRUCache({
59
+ max: 1024,
60
+ ttl: 15 * 60 * 1000,
61
+ ttlAutopurge: true
62
+ })
63
+ this.pendingPhoneRequests = {}
64
+ this.maxMsgRetryCount = 5
65
+ this.statistics = {
66
+ totalRetries: 0,
67
+ successfulRetries: 0,
68
+ failedRetries: 0,
69
+ mediaRetries: 0,
70
+ sessionRecreations: 0,
71
+ phoneRequests: 0
72
+ }
73
+ this.maxMsgRetryCount = maxMsgRetryCount
74
+ }
75
+ /**
76
+ * Add a recent message to the cache for retry handling
77
+ */
78
+ addRecentMessage(to, id, message) {
79
+ const key = { to, id }
80
+ const keyStr = this.keyToString(key)
81
+ // Add new message
82
+ this.recentMessagesMap.set(keyStr, {
83
+ message,
84
+ timestamp: Date.now()
85
+ })
86
+ this.messageKeyIndex.set(id, keyStr)
87
+ this.logger.debug(`Added message to retry cache: ${to}/${id}`)
88
+ }
89
+ /**
90
+ * Get a recent message from the cache
91
+ */
92
+ getRecentMessage(to, id) {
93
+ const key = { to, id }
94
+ const keyStr = this.keyToString(key)
95
+ return this.recentMessagesMap.get(keyStr)
96
+ }
97
+ /**
98
+ * Check if a session should be recreated based on retry count, history, and error code.
99
+ * MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
100
+ */
101
+ shouldRecreateSession(jid, hasSession, errorCode) {
102
+ // If we don't have a session, always recreate
103
+ if (!hasSession) {
104
+ this.sessionRecreateHistory.set(jid, Date.now())
105
+ this.statistics.sessionRecreations++
106
+ return {
107
+ reason: "we don't have a Signal session with them",
108
+ recreate: true
109
+ }
110
+ }
111
+ // IMMEDIATE recreation for MAC errors - session is definitely out of sync
112
+ if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
113
+ this.sessionRecreateHistory.set(jid, Date.now())
114
+ this.statistics.sessionRecreations++
115
+ this.logger.warn(
116
+ { jid, errorCode: RetryReason[errorCode] },
117
+ 'MAC error detected, forcing immediate session recreation'
118
+ )
119
+ return {
120
+ reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
121
+ recreate: true
122
+ }
123
+ }
124
+ const now = Date.now()
125
+ const prevTime = this.sessionRecreateHistory.get(jid)
126
+ // If no previous recreation or it's been more than an hour
127
+ if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
128
+ this.sessionRecreateHistory.set(jid, now)
129
+ this.statistics.sessionRecreations++
130
+ return {
131
+ reason: 'retry count > 1 and over an hour since last recreation',
132
+ recreate: true
133
+ }
134
+ }
135
+ return { reason: '', recreate: false }
136
+ }
137
+ /**
138
+ * Parse error code from retry receipt's retry node.
139
+ * Returns undefined if no error code is present.
140
+ */
141
+ parseRetryErrorCode(errorAttr) {
142
+ if (errorAttr === undefined || errorAttr === '') {
143
+ return undefined
144
+ }
145
+ const code = parseInt(errorAttr, 10)
146
+ if (Number.isNaN(code)) {
147
+ return undefined
148
+ }
149
+ // Validate it's a known RetryReason
150
+ if (code >= RetryReason.UnknownError && code <= RetryReason.StatusRevokeDelay) {
151
+ return code
152
+ }
153
+ return RetryReason.UnknownError
154
+ }
155
+ /**
156
+ * Check if an error code indicates a MAC failure
157
+ */
158
+ isMacError(errorCode) {
159
+ return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)
160
+ }
161
+ /**
162
+ * Increment retry counter for a message
163
+ */
164
+ incrementRetryCount(messageId) {
165
+ this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1)
166
+ this.statistics.totalRetries++
167
+ return this.retryCounters.get(messageId)
168
+ }
169
+ /**
170
+ * Get retry count for a message
171
+ */
172
+ getRetryCount(messageId) {
173
+ return this.retryCounters.get(messageId) || 0
174
+ }
175
+ /**
176
+ * Check if message has exceeded maximum retry attempts
177
+ */
178
+ hasExceededMaxRetries(messageId) {
179
+ return this.getRetryCount(messageId) >= this.maxMsgRetryCount
180
+ }
181
+ /**
182
+ * Mark retry as successful
183
+ */
184
+ markRetrySuccess(messageId) {
185
+ this.statistics.successfulRetries++
186
+ // Clean up retry counter for successful message
187
+ this.retryCounters.delete(messageId)
188
+ this.cancelPendingPhoneRequest(messageId)
189
+ this.removeRecentMessage(messageId)
190
+ }
191
+ /**
192
+ * Mark retry as failed
193
+ */
194
+ markRetryFailed(messageId) {
195
+ this.statistics.failedRetries++
196
+ this.retryCounters.delete(messageId)
197
+ this.cancelPendingPhoneRequest(messageId)
198
+ this.removeRecentMessage(messageId)
199
+ }
200
+ /**
201
+ * Schedule a phone request with delay
202
+ */
203
+ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
204
+ // Cancel any existing request for this message
205
+ this.cancelPendingPhoneRequest(messageId)
206
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
207
+ delete this.pendingPhoneRequests[messageId]
208
+ this.statistics.phoneRequests++
209
+ callback()
210
+ }, delay)
211
+ this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`)
212
+ }
213
+ /**
214
+ * Cancel pending phone request
215
+ */
216
+ cancelPendingPhoneRequest(messageId) {
217
+ const timeout = this.pendingPhoneRequests[messageId]
218
+ if (timeout) {
219
+ clearTimeout(timeout)
220
+ delete this.pendingPhoneRequests[messageId]
221
+ this.logger.debug(`Cancelled pending phone request for message ${messageId}`)
222
+ }
223
+ }
224
+ clear() {
225
+ this.recentMessagesMap.clear()
226
+ this.messageKeyIndex.clear()
227
+ this.sessionRecreateHistory.clear()
228
+ this.retryCounters.clear()
229
+ this.baseKeys.clear()
230
+ for (const messageId of Object.keys(this.pendingPhoneRequests)) {
231
+ this.cancelPendingPhoneRequest(messageId)
232
+ }
233
+ this.statistics = {
234
+ totalRetries: 0,
235
+ successfulRetries: 0,
236
+ failedRetries: 0,
237
+ mediaRetries: 0,
238
+ sessionRecreations: 0,
239
+ phoneRequests: 0
240
+ }
241
+ }
242
+ saveBaseKey(addr, msgId, baseKey) {
243
+ this.baseKeys.set(`${addr}:${msgId}`, baseKey)
244
+ }
245
+ hasSameBaseKey(addr, msgId, baseKey) {
246
+ const stored = this.baseKeys.get(`${addr}:${msgId}`)
247
+ if (!stored || stored.length !== baseKey.length) {
248
+ return false
249
+ }
250
+ for (let i = 0; i < stored.length; i++) {
251
+ if (stored[i] !== baseKey[i]) return false
252
+ }
253
+ return true
254
+ }
255
+ deleteBaseKey(addr, msgId) {
256
+ this.baseKeys.delete(`${addr}:${msgId}`)
257
+ }
258
+ keyToString(key) {
259
+ return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`
260
+ }
261
+ removeRecentMessage(messageId) {
262
+ const keyStr = this.messageKeyIndex.get(messageId)
263
+ if (!keyStr) {
264
+ return
265
+ }
266
+ this.recentMessagesMap.delete(keyStr)
267
+ this.messageKeyIndex.delete(messageId)
268
+ }
269
+ }
270
+ exports.MessageRetryManager = MessageRetryManager