jagproject 26.3.22 → 26.3.25

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 (206) hide show
  1. package/WAProto/GenerateStatics.sh +3 -4
  2. package/WAProto/WAProto.proto +1215 -511
  3. package/WAProto/fix-imports.js +73 -0
  4. package/WAProto/index.d.ts +14017 -0
  5. package/WAProto/index.js +64857 -145167
  6. package/engine-requirements.js +4 -7
  7. package/lib/Defaults/index.d.ts +74 -0
  8. package/lib/Defaults/index.js +51 -33
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Defaults/wileys-version.json +2 -2
  11. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  12. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  13. package/lib/Signal/Group/group-session-builder.js +5 -3
  14. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  15. package/lib/Signal/Group/group_cipher.js +35 -46
  16. package/lib/Signal/Group/index.d.ts +12 -0
  17. package/lib/Signal/Group/index.js +21 -21
  18. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  19. package/lib/Signal/Group/keyhelper.js +2 -2
  20. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  21. package/lib/Signal/Group/sender-chain-key.js +5 -10
  22. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  23. package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
  24. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  25. package/lib/Signal/Group/sender-key-message.js +8 -8
  26. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  27. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  28. package/lib/Signal/Group/sender-key-record.js +7 -16
  29. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  30. package/lib/Signal/Group/sender-key-state.js +25 -37
  31. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  32. package/lib/Signal/Group/sender-message-key.js +2 -2
  33. package/lib/Signal/libsignal.d.ts +5 -0
  34. package/lib/Signal/libsignal.js +358 -54
  35. package/lib/Signal/lid-mapping.d.ts +19 -0
  36. package/lib/Signal/lid-mapping.js +274 -0
  37. package/lib/Socket/Client/index.d.ts +3 -0
  38. package/lib/Socket/Client/index.js +2 -2
  39. package/lib/Socket/Client/types.d.ts +16 -0
  40. package/lib/Socket/Client/types.js +1 -0
  41. package/lib/Socket/Client/websocket.d.ts +13 -0
  42. package/lib/Socket/Client/websocket.js +18 -30
  43. package/lib/Socket/business.d.ts +202 -0
  44. package/lib/Socket/business.js +160 -38
  45. package/lib/Socket/chats.d.ts +111 -0
  46. package/lib/Socket/chats.js +497 -314
  47. package/lib/Socket/communities.d.ts +258 -0
  48. package/lib/Socket/communities.js +438 -0
  49. package/lib/Socket/community.js +333 -0
  50. package/lib/Socket/groups.d.ts +150 -0
  51. package/lib/Socket/groups.js +229 -91
  52. package/lib/Socket/index.d.ts +245 -0
  53. package/lib/Socket/index.js +9 -6
  54. package/lib/Socket/messages-recv.d.ts +187 -0
  55. package/lib/Socket/messages-recv.js +1105 -501
  56. package/lib/Socket/messages-send.d.ts +183 -0
  57. package/lib/Socket/messages-send.js +1184 -501
  58. package/lib/Socket/mex.d.ts +3 -0
  59. package/lib/Socket/mex.js +45 -0
  60. package/lib/Socket/newsletter.d.ts +160 -0
  61. package/lib/Socket/newsletter.js +227 -200
  62. package/lib/Socket/socket.d.ts +55 -0
  63. package/lib/Socket/socket.js +507 -206
  64. package/lib/Socket/usync.js +6 -6
  65. package/lib/Store/index.js +17 -5
  66. package/lib/Store/make-cache-manager-store.js +83 -0
  67. package/lib/Store/make-in-memory-store.js +48 -89
  68. package/lib/Store/make-ordered-dictionary.js +1 -1
  69. package/lib/Types/Auth.d.ts +116 -0
  70. package/lib/Types/Bussines.d.ts +25 -0
  71. package/lib/Types/Bussines.js +2 -0
  72. package/lib/Types/Call.d.ts +15 -0
  73. package/lib/Types/Chat.d.ts +123 -0
  74. package/lib/Types/Chat.js +7 -1
  75. package/lib/Types/Contact.d.ts +24 -0
  76. package/lib/Types/Events.d.ts +237 -0
  77. package/lib/Types/Events.js +1 -0
  78. package/lib/Types/GroupMetadata.d.ts +67 -0
  79. package/lib/Types/Label.d.ts +47 -0
  80. package/lib/Types/Label.js +1 -3
  81. package/lib/Types/LabelAssociation.d.ts +30 -0
  82. package/lib/Types/LabelAssociation.js +1 -3
  83. package/lib/Types/Message.d.ts +305 -0
  84. package/lib/Types/Message.js +9 -5
  85. package/lib/Types/MexUpdates.js +11 -0
  86. package/lib/Types/Newsletter.d.ts +135 -0
  87. package/lib/Types/Newsletter.js +36 -11
  88. package/lib/Types/Product.d.ts +79 -0
  89. package/lib/Types/Signal.d.ts +76 -0
  90. package/lib/Types/Signal.js +1 -0
  91. package/lib/Types/Socket.d.ts +133 -0
  92. package/lib/Types/Socket.js +1 -0
  93. package/lib/Types/State.d.ts +39 -0
  94. package/lib/Types/State.js +12 -0
  95. package/lib/Types/USync.d.ts +26 -0
  96. package/lib/Types/USync.js +1 -0
  97. package/lib/Types/index.d.ts +65 -0
  98. package/lib/Types/index.js +14 -14
  99. package/lib/Utils/audioToBuffer.js +31 -0
  100. package/lib/Utils/auth-utils.d.ts +19 -0
  101. package/lib/Utils/auth-utils.js +222 -123
  102. package/lib/Utils/baileys-event-stream.js +60 -0
  103. package/lib/Utils/browser-utils.d.ts +4 -0
  104. package/lib/Utils/browser-utils.js +38 -29
  105. package/lib/Utils/business.d.ts +23 -0
  106. package/lib/Utils/business.js +54 -48
  107. package/lib/Utils/chat-utils.d.ts +70 -0
  108. package/lib/Utils/chat-utils.js +284 -189
  109. package/lib/Utils/crypto.d.ts +37 -0
  110. package/lib/Utils/crypto.js +16 -41
  111. package/lib/Utils/decode-wa-message.d.ts +48 -0
  112. package/lib/Utils/decode-wa-message.js +128 -48
  113. package/lib/Utils/event-buffer.d.ts +34 -0
  114. package/lib/Utils/event-buffer.js +124 -62
  115. package/lib/Utils/generics.d.ts +91 -0
  116. package/lib/Utils/generics.js +154 -138
  117. package/lib/Utils/history.d.ts +22 -0
  118. package/lib/Utils/history.js +77 -34
  119. package/lib/Utils/identity-change-handler.d.ts +37 -0
  120. package/lib/Utils/identity-change-handler.js +54 -0
  121. package/lib/Utils/index.d.ts +22 -0
  122. package/lib/Utils/index.js +32 -19
  123. package/lib/Utils/link-preview.d.ts +21 -0
  124. package/lib/Utils/link-preview.js +12 -17
  125. package/lib/Utils/logger.d.ts +13 -0
  126. package/lib/Utils/lt-hash.d.ts +8 -0
  127. package/lib/Utils/lt-hash.js +2 -43
  128. package/lib/Utils/make-mutex.d.ts +9 -0
  129. package/lib/Utils/make-mutex.js +21 -27
  130. package/lib/Utils/message-retry-manager.d.ts +110 -0
  131. package/lib/Utils/message-retry-manager.js +143 -45
  132. package/lib/Utils/messages-media.d.ts +130 -0
  133. package/lib/Utils/messages-media.js +429 -502
  134. package/lib/Utils/messages-newsletter.d.ts +84 -0
  135. package/lib/Utils/messages-newsletter.js +295 -0
  136. package/lib/Utils/messages.d.ts +92 -0
  137. package/lib/Utils/messages.js +1099 -400
  138. package/lib/Utils/noise-handler.d.ts +20 -0
  139. package/lib/Utils/noise-handler.js +145 -91
  140. package/lib/Utils/pre-key-manager.d.ts +28 -0
  141. package/lib/Utils/pre-key-manager.js +112 -0
  142. package/lib/Utils/process-message.d.ts +60 -0
  143. package/lib/Utils/process-message.js +316 -184
  144. package/lib/Utils/reporting-utils.d.ts +11 -0
  145. package/lib/Utils/reporting-utils.js +262 -0
  146. package/lib/Utils/resolve-jid.d.ts +43 -0
  147. package/lib/Utils/resolve-jid.js +95 -0
  148. package/lib/Utils/serial-task-queue.js +29 -0
  149. package/lib/Utils/signal.d.ts +34 -0
  150. package/lib/Utils/signal.js +56 -39
  151. package/lib/Utils/streamToBuffer.js +17 -0
  152. package/lib/Utils/sync-action-utils.d.ts +19 -0
  153. package/lib/Utils/sync-action-utils.js +52 -0
  154. package/lib/Utils/tc-token-utils.d.ts +12 -0
  155. package/lib/Utils/tc-token-utils.js +20 -0
  156. package/lib/Utils/use-mongo-file-auth-state.js +71 -0
  157. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  158. package/lib/Utils/use-multi-file-auth-state.js +11 -12
  159. package/lib/Utils/use-single-file-auth-state.js +73 -0
  160. package/lib/Utils/validate-connection.d.ts +11 -0
  161. package/lib/Utils/validate-connection.js +59 -82
  162. package/lib/Utils/wileys-event-stream.js +1 -61
  163. package/lib/WABinary/constants.d.ts +28 -0
  164. package/lib/WABinary/decode.d.ts +7 -0
  165. package/lib/WABinary/decode.js +39 -4
  166. package/lib/WABinary/encode.d.ts +3 -0
  167. package/lib/WABinary/encode.js +17 -11
  168. package/lib/WABinary/generic-utils.d.ts +15 -0
  169. package/lib/WABinary/generic-utils.js +46 -18
  170. package/lib/WABinary/index.d.ts +6 -0
  171. package/lib/WABinary/index.js +9 -5
  172. package/lib/WABinary/jid-utils.d.ts +48 -0
  173. package/lib/WABinary/jid-utils.js +67 -37
  174. package/lib/WABinary/types.d.ts +19 -0
  175. package/lib/WABinary/types.js +34 -0
  176. package/lib/WAM/BinaryInfo.d.ts +9 -0
  177. package/lib/WAM/constants.d.ts +40 -0
  178. package/lib/WAM/constants.js +19183 -11678
  179. package/lib/WAM/encode.d.ts +3 -0
  180. package/lib/WAM/encode.js +15 -17
  181. package/lib/WAM/index.d.ts +4 -0
  182. package/lib/WAM/index.js +3 -3
  183. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  184. package/lib/WAUSync/Protocols/USyncContactProtocol.js +6 -6
  185. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  186. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +9 -9
  187. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  188. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +6 -6
  189. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  190. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +7 -8
  191. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  192. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +18 -17
  193. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  194. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +11 -3
  195. package/lib/WAUSync/Protocols/index.d.ts +5 -0
  196. package/lib/WAUSync/Protocols/index.js +6 -4
  197. package/lib/WAUSync/USyncQuery.d.ts +29 -0
  198. package/lib/WAUSync/USyncQuery.js +38 -30
  199. package/lib/WAUSync/USyncUser.d.ts +13 -0
  200. package/lib/WAUSync/index.d.ts +4 -0
  201. package/lib/WAUSync/index.js +3 -3
  202. package/lib/index.d.ts +12 -0
  203. package/lib/index.js +3 -5
  204. package/package.json +10 -6
  205. package/readme.md +97 -0
  206. package/LICENSE +0 -21
@@ -0,0 +1,110 @@
1
+ import type { proto } from '../../WAProto/index.js';
2
+ import type { ILogger } from './logger.js';
3
+ export interface RecentMessageKey {
4
+ to: string;
5
+ id: string;
6
+ }
7
+ export interface RecentMessage {
8
+ message: proto.IMessage;
9
+ timestamp: number;
10
+ }
11
+ export interface SessionRecreateHistory {
12
+ [jid: string]: number;
13
+ }
14
+ export interface RetryCounter {
15
+ [messageId: string]: number;
16
+ }
17
+ export type PendingPhoneRequest = Record<string, ReturnType<typeof setTimeout>>;
18
+ export interface RetryStatistics {
19
+ totalRetries: number;
20
+ successfulRetries: number;
21
+ failedRetries: number;
22
+ mediaRetries: number;
23
+ sessionRecreations: number;
24
+ phoneRequests: number;
25
+ }
26
+ export declare enum RetryReason {
27
+ UnknownError = 0,
28
+ SignalErrorNoSession = 1,
29
+ SignalErrorInvalidKey = 2,
30
+ SignalErrorInvalidKeyId = 3,
31
+ /** MAC verification failed - most common cause of decryption failures */
32
+ SignalErrorInvalidMessage = 4,
33
+ SignalErrorInvalidSignature = 5,
34
+ SignalErrorFutureMessage = 6,
35
+ /** Explicit MAC failure - session is definitely out of sync */
36
+ SignalErrorBadMac = 7,
37
+ SignalErrorInvalidSession = 8,
38
+ SignalErrorInvalidMsgKey = 9,
39
+ BadBroadcastEphemeralSetting = 10,
40
+ UnknownCompanionNoPrekey = 11,
41
+ AdvFailure = 12,
42
+ StatusRevokeDelay = 13
43
+ }
44
+ export declare class MessageRetryManager {
45
+ private logger;
46
+ private recentMessagesMap;
47
+ private messageKeyIndex;
48
+ private sessionRecreateHistory;
49
+ private retryCounters;
50
+ private pendingPhoneRequests;
51
+ private readonly maxMsgRetryCount;
52
+ private statistics;
53
+ constructor(logger: ILogger, maxMsgRetryCount: number);
54
+ /**
55
+ * Add a recent message to the cache for retry handling
56
+ */
57
+ addRecentMessage(to: string, id: string, message: proto.IMessage): void;
58
+ /**
59
+ * Get a recent message from the cache
60
+ */
61
+ getRecentMessage(to: string, id: string): RecentMessage | undefined;
62
+ /**
63
+ * Check if a session should be recreated based on retry count, history, and error code.
64
+ * MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
65
+ */
66
+ shouldRecreateSession(jid: string, hasSession: boolean, errorCode?: RetryReason): {
67
+ reason: string;
68
+ recreate: boolean;
69
+ };
70
+ /**
71
+ * Parse error code from retry receipt's retry node.
72
+ * Returns undefined if no error code is present.
73
+ */
74
+ parseRetryErrorCode(errorAttr: string | undefined): RetryReason | undefined;
75
+ /**
76
+ * Check if an error code indicates a MAC failure
77
+ */
78
+ isMacError(errorCode: RetryReason | undefined): boolean;
79
+ /**
80
+ * Increment retry counter for a message
81
+ */
82
+ incrementRetryCount(messageId: string): number;
83
+ /**
84
+ * Get retry count for a message
85
+ */
86
+ getRetryCount(messageId: string): number;
87
+ /**
88
+ * Check if message has exceeded maximum retry attempts
89
+ */
90
+ hasExceededMaxRetries(messageId: string): boolean;
91
+ /**
92
+ * Mark retry as successful
93
+ */
94
+ markRetrySuccess(messageId: string): void;
95
+ /**
96
+ * Mark retry as failed
97
+ */
98
+ markRetryFailed(messageId: string): void;
99
+ /**
100
+ * Schedule a phone request with delay
101
+ */
102
+ schedulePhoneRequest(messageId: string, callback: () => void, delay?: number): void;
103
+ /**
104
+ * Cancel pending phone request
105
+ */
106
+ cancelPendingPhoneRequest(messageId: string): void;
107
+ private keyToString;
108
+ private removeRecentMessage;
109
+ }
110
+ //# sourceMappingURL=message-retry-manager.d.ts.map
@@ -1,32 +1,60 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MessageRetryManager = void 0;
4
-
3
+ exports.MessageRetryManager = exports.RetryReason = void 0;
5
4
  const lru_cache_1 = require("lru-cache");
6
-
5
+ /** Number of sent messages to cache in memory for handling retry receipts */
7
6
  const RECENT_MESSAGES_SIZE = 512;
8
-
9
- const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000;
10
-
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
11
10
  const PHONE_REQUEST_DELAY = 3000;
12
-
11
+ (function (RetryReason) {
12
+ RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
13
+ RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
14
+ RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
15
+ RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
16
+ /** MAC verification failed - most common cause of decryption failures */
17
+ RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
18
+ RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
19
+ RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
20
+ /** Explicit MAC failure - session is definitely out of sync */
21
+ RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
22
+ RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
23
+ RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
24
+ RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
25
+ RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
26
+ RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
27
+ RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
28
+ })(exports.RetryReason || (exports.RetryReason = {}));
29
+ /** Error codes that indicate a MAC failure and require immediate session recreation */
30
+ const MAC_ERROR_CODES = new Set([exports.RetryReason.SignalErrorInvalidMessage, exports.RetryReason.SignalErrorBadMac]);
13
31
  class MessageRetryManager {
14
32
  constructor(logger, maxMsgRetryCount) {
15
33
  this.logger = logger;
16
- this.maxMsgRetryCount = maxMsgRetryCount;
17
- this._recentMessagesMap = new lru_cache_1.LRUCache({
18
- max: RECENT_MESSAGES_SIZE
34
+ this.recentMessagesMap = new lru_cache_1.LRUCache({
35
+ max: RECENT_MESSAGES_SIZE,
36
+ ttl: 5 * 60 * 1000,
37
+ ttlAutopurge: true,
38
+ dispose: (_value, key) => {
39
+ const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
40
+ if (separatorIndex > -1) {
41
+ const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
42
+ this.messageKeyIndex.delete(messageId);
43
+ }
44
+ }
19
45
  });
20
- this._sessionRecreateHistory = new lru_cache_1.LRUCache({
46
+ this.messageKeyIndex = new Map();
47
+ this.sessionRecreateHistory = new lru_cache_1.LRUCache({
21
48
  ttl: RECREATE_SESSION_TIMEOUT * 2,
22
49
  ttlAutopurge: true
23
50
  });
24
- this._retryCounters = new lru_cache_1.LRUCache({
51
+ this.retryCounters = new lru_cache_1.LRUCache({
25
52
  ttl: 15 * 60 * 1000,
26
53
  ttlAutopurge: true,
27
54
  updateAgeOnGet: true
28
- });
29
- this._pendingPhoneRequests = {};
55
+ }); // 15 minutes TTL
56
+ this.pendingPhoneRequests = {};
57
+ this.maxMsgRetryCount = 5;
30
58
  this.statistics = {
31
59
  totalRetries: 0,
32
60
  successfulRetries: 0,
@@ -37,36 +65,57 @@ class MessageRetryManager {
37
65
  };
38
66
  this.maxMsgRetryCount = maxMsgRetryCount;
39
67
  }
68
+ /**
69
+ * Add a recent message to the cache for retry handling
70
+ */
40
71
  addRecentMessage(to, id, message) {
41
72
  const key = { to, id };
42
- const keyStr = this._keyToString(key);
43
- this._recentMessagesMap.set(keyStr, {
73
+ const keyStr = this.keyToString(key);
74
+ // Add new message
75
+ this.recentMessagesMap.set(keyStr, {
44
76
  message,
45
77
  timestamp: Date.now()
46
78
  });
79
+ this.messageKeyIndex.set(id, keyStr);
47
80
  this.logger.debug(`Added message to retry cache: ${to}/${id}`);
48
81
  }
82
+ /**
83
+ * Get a recent message from the cache
84
+ */
49
85
  getRecentMessage(to, id) {
50
86
  const key = { to, id };
51
- const keyStr = this._keyToString(key);
52
- return this._recentMessagesMap.get(keyStr);
87
+ const keyStr = this.keyToString(key);
88
+ return this.recentMessagesMap.get(keyStr);
53
89
  }
54
- shouldRecreateSession(jid, retryCount, hasSession) {
90
+ /**
91
+ * Check if a session should be recreated based on retry count, history, and error code.
92
+ * MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
93
+ */
94
+ shouldRecreateSession(jid, hasSession, errorCode) {
95
+ // If we don't have a session, always recreate
55
96
  if (!hasSession) {
56
- this._sessionRecreateHistory.set(jid, Date.now());
97
+ this.sessionRecreateHistory.set(jid, Date.now());
57
98
  this.statistics.sessionRecreations++;
58
99
  return {
59
100
  reason: "we don't have a Signal session with them",
60
101
  recreate: true
61
102
  };
62
103
  }
63
- if (retryCount < 2) {
64
- return { reason: '', recreate: false };
104
+ // IMMEDIATE recreation for MAC errors - session is definitely out of sync
105
+ if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
106
+ this.sessionRecreateHistory.set(jid, Date.now());
107
+ this.statistics.sessionRecreations++;
108
+ this.logger.warn({ jid, errorCode: exports.RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
109
+ return {
110
+ reason: `MAC error (code ${errorCode}: ${exports.RetryReason[errorCode]}), immediate session recreation`,
111
+ recreate: true
112
+ };
65
113
  }
66
114
  const now = Date.now();
67
- const prevTime = this._sessionRecreateHistory.get(jid);
115
+ const prevTime = this.sessionRecreateHistory.get(jid);
116
+ // If no previous recreation or it's been more than an hour
68
117
  if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
69
- this._sessionRecreateHistory.set(jid, now);
118
+ this.sessionRecreateHistory.set(jid, now);
70
119
  this.statistics.sessionRecreations++;
71
120
  return {
72
121
  reason: 'retry count > 1 and over an hour since last recreation',
@@ -75,54 +124,103 @@ class MessageRetryManager {
75
124
  }
76
125
  return { reason: '', recreate: false };
77
126
  }
127
+ /**
128
+ * Parse error code from retry receipt's retry node.
129
+ * Returns undefined if no error code is present.
130
+ */
131
+ parseRetryErrorCode(errorAttr) {
132
+ if (errorAttr === undefined || errorAttr === '') {
133
+ return undefined;
134
+ }
135
+ const code = parseInt(errorAttr, 10);
136
+ if (Number.isNaN(code)) {
137
+ return undefined;
138
+ }
139
+ // Validate it's a known RetryReason
140
+ if (code >= exports.RetryReason.UnknownError && code <= exports.RetryReason.StatusRevokeDelay) {
141
+ return code;
142
+ }
143
+ return exports.RetryReason.UnknownError;
144
+ }
145
+ /**
146
+ * Check if an error code indicates a MAC failure
147
+ */
148
+ isMacError(errorCode) {
149
+ return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
150
+ }
151
+ /**
152
+ * Increment retry counter for a message
153
+ */
78
154
  incrementRetryCount(messageId) {
79
- this._retryCounters.set(messageId, (this._retryCounters.get(messageId) || 0) + 1);
155
+ this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
80
156
  this.statistics.totalRetries++;
81
- return this._retryCounters.get(messageId);
157
+ return this.retryCounters.get(messageId);
82
158
  }
159
+ /**
160
+ * Get retry count for a message
161
+ */
83
162
  getRetryCount(messageId) {
84
- return this._retryCounters.get(messageId) || 0;
163
+ return this.retryCounters.get(messageId) || 0;
85
164
  }
165
+ /**
166
+ * Check if message has exceeded maximum retry attempts
167
+ */
86
168
  hasExceededMaxRetries(messageId) {
87
169
  return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
88
170
  }
171
+ /**
172
+ * Mark retry as successful
173
+ */
89
174
  markRetrySuccess(messageId) {
90
175
  this.statistics.successfulRetries++;
91
- this._retryCounters.delete(messageId);
92
- this._cancelPendingPhoneRequest(messageId);
176
+ // Clean up retry counter for successful message
177
+ this.retryCounters.delete(messageId);
178
+ this.cancelPendingPhoneRequest(messageId);
179
+ this.removeRecentMessage(messageId);
93
180
  }
181
+ /**
182
+ * Mark retry as failed
183
+ */
94
184
  markRetryFailed(messageId) {
95
185
  this.statistics.failedRetries++;
96
- this._retryCounters.delete(messageId);
186
+ this.retryCounters.delete(messageId);
187
+ this.cancelPendingPhoneRequest(messageId);
188
+ this.removeRecentMessage(messageId);
97
189
  }
190
+ /**
191
+ * Schedule a phone request with delay
192
+ */
98
193
  schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
99
- this._cancelPendingPhoneRequest(messageId);
100
- this._pendingPhoneRequests[messageId] = setTimeout(() => {
101
- delete this._pendingPhoneRequests[messageId];
194
+ // Cancel any existing request for this message
195
+ this.cancelPendingPhoneRequest(messageId);
196
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
197
+ delete this.pendingPhoneRequests[messageId];
102
198
  this.statistics.phoneRequests++;
103
199
  callback();
104
200
  }, delay);
105
201
  this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
106
202
  }
203
+ /**
204
+ * Cancel pending phone request
205
+ */
107
206
  cancelPendingPhoneRequest(messageId) {
108
- const timeout = this._pendingPhoneRequests[messageId];
207
+ const timeout = this.pendingPhoneRequests[messageId];
109
208
  if (timeout) {
110
209
  clearTimeout(timeout);
111
- delete this._pendingPhoneRequests[messageId];
210
+ delete this.pendingPhoneRequests[messageId];
112
211
  this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
113
212
  }
114
213
  }
115
- _keyToString(key) {
116
- return `${key.to}:${key.id}`;
214
+ keyToString(key) {
215
+ return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
117
216
  }
118
- _cancelPendingPhoneRequest(messageId) {
119
- const timeout = this._pendingPhoneRequests[messageId];
120
- if (timeout) {
121
- clearTimeout(timeout);
122
- delete this._pendingPhoneRequests[messageId];
123
- this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
217
+ removeRecentMessage(messageId) {
218
+ const keyStr = this.messageKeyIndex.get(messageId);
219
+ if (!keyStr) {
220
+ return;
124
221
  }
222
+ this.recentMessagesMap.delete(keyStr);
223
+ this.messageKeyIndex.delete(messageId);
125
224
  }
126
225
  }
127
-
128
- exports.MessageRetryManager = MessageRetryManager;
226
+ exports.MessageRetryManager = MessageRetryManager;
@@ -0,0 +1,130 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import type { Agent } from 'https';
3
+ import { Readable, Transform } from 'stream';
4
+ import { URL } from 'url';
5
+ import { proto } from '../../WAProto/index.js';
6
+ import { type MediaType } from '../Defaults/index.js';
7
+ import type { DownloadableMessage, MediaConnInfo, MediaDecryptionKeyInfo, SocketConfig, WAMediaUpload, WAMediaUploadFunction, WAMessageContent, WAMessageKey } from '../Types/index.js';
8
+ import { type BinaryNode } from '../WABinary/index.js';
9
+ import type { ILogger } from './logger.js';
10
+ export declare const hkdfInfoKey: (type: MediaType) => string;
11
+ export declare const getRawMediaUploadData: (media: WAMediaUpload, mediaType: MediaType, logger?: ILogger) => Promise<{
12
+ filePath: string;
13
+ fileSha256: NonSharedBuffer;
14
+ fileLength: number;
15
+ }>;
16
+ /** generates all the keys required to encrypt/decrypt & sign a media message */
17
+ export declare function getMediaKeys(buffer: Uint8Array | string | null | undefined, mediaType: MediaType): Promise<MediaDecryptionKeyInfo>;
18
+ export declare const extractImageThumb: (bufferOrFilePath: Readable | Buffer | string, width?: number) => Promise<{
19
+ buffer: any;
20
+ original: {
21
+ width: any;
22
+ height: any;
23
+ };
24
+ }>;
25
+ export declare const encodeBase64EncodedStringForUpload: (b64: string) => string;
26
+ export declare const generateProfilePicture: (mediaUpload: WAMediaUpload, dimensions?: {
27
+ width: number;
28
+ height: number;
29
+ }) => Promise<{
30
+ img: Buffer<ArrayBufferLike>;
31
+ }>;
32
+ /** gets the SHA256 of the given media message */
33
+ export declare const mediaMessageSHA256B64: (message: WAMessageContent) => string | null | undefined;
34
+ export declare function getAudioDuration(buffer: Buffer | string | Readable): Promise<number | undefined>;
35
+ /**
36
+ referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
37
+ */
38
+ export declare function getAudioWaveform(buffer: Buffer | string | Readable, logger?: ILogger): Promise<Uint8Array<ArrayBuffer> | undefined>;
39
+ export declare const toReadable: (buffer: Buffer) => Readable;
40
+ export declare const toBuffer: (stream: Readable) => Promise<Buffer<ArrayBuffer>>;
41
+ export declare const getStream: (item: WAMediaUpload, opts?: RequestInit & {
42
+ maxContentLength?: number;
43
+ }) => Promise<{
44
+ readonly stream: Readable;
45
+ readonly type: "buffer";
46
+ } | {
47
+ readonly stream: Readable;
48
+ readonly type: "readable";
49
+ } | {
50
+ readonly stream: Readable;
51
+ readonly type: "remote";
52
+ } | {
53
+ readonly stream: import("fs").ReadStream;
54
+ readonly type: "file";
55
+ }>;
56
+ /** generates a thumbnail for a given media, if required */
57
+ export declare function generateThumbnail(file: string, mediaType: 'video' | 'image', options: {
58
+ logger?: ILogger;
59
+ }): Promise<{
60
+ thumbnail: string | undefined;
61
+ originalImageDimensions: {
62
+ width: number;
63
+ height: number;
64
+ } | undefined;
65
+ }>;
66
+ export declare const getHttpStream: (url: string | URL, options?: RequestInit & {
67
+ isStream?: true;
68
+ }) => Promise<Readable>;
69
+ type EncryptedStreamOptions = {
70
+ saveOriginalFileIfRequired?: boolean;
71
+ logger?: ILogger;
72
+ opts?: RequestInit;
73
+ };
74
+ export declare const encryptedStream: (media: WAMediaUpload, mediaType: MediaType, { logger, saveOriginalFileIfRequired, opts }?: EncryptedStreamOptions) => Promise<{
75
+ mediaKey: NonSharedBuffer;
76
+ originalFilePath: string | undefined;
77
+ encFilePath: string;
78
+ mac: Buffer<ArrayBuffer>;
79
+ fileEncSha256: NonSharedBuffer;
80
+ fileSha256: NonSharedBuffer;
81
+ fileLength: number;
82
+ }>;
83
+ export type MediaDownloadOptions = {
84
+ startByte?: number;
85
+ endByte?: number;
86
+ options?: RequestInit;
87
+ };
88
+ export declare const getUrlFromDirectPath: (directPath: string) => string;
89
+ export declare const downloadContentFromMessage: ({ mediaKey, directPath, url }: DownloadableMessage, type: MediaType, opts?: MediaDownloadOptions) => Promise<Transform>;
90
+ /**
91
+ * Decrypts and downloads an AES256-CBC encrypted file given the keys.
92
+ * Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
93
+ * */
94
+ export declare const downloadEncryptedContent: (downloadUrl: string, { cipherKey, iv }: MediaDecryptionKeyInfo, { startByte, endByte, options }?: MediaDownloadOptions) => Promise<Transform>;
95
+ export declare function extensionForMediaMessage(message: WAMessageContent): string;
96
+ type MediaUploadResult = {
97
+ url?: string;
98
+ direct_path?: string;
99
+ meta_hmac?: string;
100
+ ts?: number;
101
+ fbid?: number;
102
+ };
103
+ export type UploadParams = {
104
+ url: string;
105
+ filePath: string;
106
+ headers: Record<string, string>;
107
+ timeoutMs?: number;
108
+ agent?: Agent;
109
+ };
110
+ export declare const uploadWithNodeHttp: ({ url, filePath, headers, timeoutMs, agent }: UploadParams, redirectCount?: number) => Promise<MediaUploadResult | undefined>;
111
+ export declare const getWAUploadToServer: ({ customUploadHosts, fetchAgent, logger, options }: SocketConfig, refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>) => WAMediaUploadFunction;
112
+ /**
113
+ * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
114
+ */
115
+ export declare const encryptMediaRetryRequest: (key: WAMessageKey, mediaKey: Buffer | Uint8Array, meId: string) => BinaryNode;
116
+ export declare const decodeMediaRetryNode: (node: BinaryNode) => {
117
+ key: WAMessageKey;
118
+ media?: {
119
+ ciphertext: Uint8Array;
120
+ iv: Uint8Array;
121
+ };
122
+ error?: Boom;
123
+ };
124
+ export declare const decryptMediaRetryData: ({ ciphertext, iv }: {
125
+ ciphertext: Uint8Array;
126
+ iv: Uint8Array;
127
+ }, mediaKey: Uint8Array, msgId: string) => proto.MediaRetryNotification;
128
+ export declare const getStatusCodeForMediaRetry: (code: number) => 200 | 412 | 404 | 418;
129
+ export {};
130
+ //# sourceMappingURL=messages-media.d.ts.map