@mrlegendbot/baileys 1.2.3 → 1.2.4
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.
- package/README.upstream.md +1312 -0
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +6275 -0
- package/WAProto/compiler.js +550 -0
- package/WAProto/index.js +2 -97691
- package/lib/Defaults/index.js +89 -92
- package/lib/Signal/Group/ciphertext-message.js +9 -10
- package/lib/Signal/Group/group-session-builder.js +21 -22
- package/lib/Signal/Group/group_cipher.js +75 -69
- package/lib/Signal/Group/index.js +0 -1
- package/lib/Signal/Group/keyhelper.js +9 -10
- package/lib/Signal/Group/sender-chain-key.js +21 -22
- package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
- package/lib/Signal/Group/sender-key-message.js +56 -62
- package/lib/Signal/Group/sender-key-name.js +38 -40
- package/lib/Signal/Group/sender-key-record.js +33 -34
- package/lib/Signal/Group/sender-key-state.js +72 -76
- package/lib/Signal/Group/sender-message-key.js +22 -23
- package/lib/Signal/libsignal.js +506 -405
- package/lib/Signal/lid-mapping.js +276 -258
- package/lib/Socket/Client/index.js +0 -1
- package/lib/Socket/Client/types.js +6 -7
- package/lib/Socket/Client/websocket.js +48 -49
- package/lib/Socket/business.js +349 -369
- package/lib/Socket/chats.js +1172 -1148
- package/lib/Socket/communities.js +473 -413
- package/lib/Socket/groups.js +387 -331
- package/lib/Socket/index.js +6 -8
- package/lib/Socket/messages-recv.js +1782 -1719
- package/lib/Socket/messages-send.js +1503 -2450
- package/lib/Socket/mex.js +40 -35
- package/lib/Socket/newsletter.js +239 -191
- package/lib/Socket/socket.js +992 -923
- package/lib/Store/index.js +8 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +70 -0
- package/lib/Store/make-in-memory-store.js +417 -0
- package/lib/Store/make-ordered-dictionary.js +69 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +0 -1
- package/lib/Types/Bussines.js +0 -1
- package/lib/Types/Call.js +0 -1
- package/lib/Types/Chat.js +1 -8
- package/lib/Types/Contact.js +0 -1
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +0 -1
- package/lib/Types/Label.js +20 -22
- package/lib/Types/LabelAssociation.js +2 -4
- package/lib/Types/Message.js +3 -5
- package/lib/Types/Mex.js +26 -31
- package/lib/Types/Product.js +0 -1
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +4 -52
- package/lib/Types/USync.js +0 -1
- package/lib/Types/index.js +10 -11
- package/lib/Utils/auth-utils.js +240 -277
- package/lib/Utils/browser-utils.js +20 -23
- package/lib/Utils/business.js +200 -204
- package/lib/Utils/chat-utils.js +836 -810
- package/lib/Utils/companion-reg-client-utils.js +23 -25
- package/lib/Utils/crypto.js +70 -86
- package/lib/Utils/decode-wa-message.js +277 -284
- package/lib/Utils/event-buffer.js +592 -595
- package/lib/Utils/generics.js +312 -335
- package/lib/Utils/history.js +123 -115
- package/lib/Utils/index.js +1 -2
- package/lib/Utils/link-preview.js +74 -73
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +27 -27
- package/lib/Utils/message-retry-manager.js +213 -254
- package/lib/Utils/messages-media.js +741 -715
- package/lib/Utils/messages.js +791 -827
- package/lib/Utils/noise-handler.js +215 -187
- package/lib/Utils/pre-key-manager.js +69 -98
- package/lib/Utils/process-message.js +617 -586
- package/lib/Utils/reporting-utils.js +553 -234
- package/lib/Utils/signal.js +217 -170
- package/lib/Utils/stanza-ack.js +27 -35
- package/lib/Utils/sync-action-utils.js +37 -41
- package/lib/Utils/tc-token-utils.js +120 -141
- package/lib/Utils/use-multi-file-auth-state.js +87 -105
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/Utils/validate-connection.js +190 -188
- package/lib/WABinary/constants.js +28 -1294
- package/lib/WABinary/decode.js +245 -249
- package/lib/WABinary/encode.js +201 -206
- package/lib/WABinary/generic-utils.js +86 -89
- package/lib/WABinary/index.js +0 -1
- package/lib/WABinary/jid-utils.js +68 -77
- package/lib/WABinary/types.js +0 -1
- package/lib/WAM/BinaryInfo.js +7 -8
- package/lib/WAM/constants.js +19892 -22853
- package/lib/WAM/encode.js +121 -132
- package/lib/WAM/index.js +0 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
- package/lib/WAUSync/Protocols/index.js +0 -1
- package/lib/WAUSync/USyncQuery.js +79 -88
- package/lib/WAUSync/USyncUser.js +28 -29
- package/lib/WAUSync/index.js +0 -1
- package/lib/index.js +2 -2
- package/package.json +22 -86
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/fix-imports.js +0 -85
- package/WAProto/index.d.ts +0 -14017
- package/lib/Defaults/index.d.ts +0 -75
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
- package/lib/Signal/Group/group-session-builder.d.ts +0 -15
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -12
- package/lib/Signal/Group/keyhelper.d.ts +0 -11
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -18
- package/lib/Signal/Group/sender-key-record.d.ts +0 -31
- package/lib/Signal/Group/sender-key-state.d.ts +0 -39
- package/lib/Signal/Group/sender-message-key.d.ts +0 -12
- package/lib/Signal/libsignal.d.ts +0 -5
- package/lib/Signal/lid-mapping.d.ts +0 -23
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -217
- package/lib/Socket/chats.d.ts +0 -125
- package/lib/Socket/communities.d.ts +0 -273
- package/lib/Socket/groups.d.ts +0 -161
- package/lib/Socket/index.d.ts +0 -260
- package/lib/Socket/messages-recv.d.ts +0 -203
- package/lib/Socket/messages-send.d.ts +0 -199
- package/lib/Socket/mex.d.ts +0 -3
- package/lib/Socket/newsletter.d.ts +0 -170
- package/lib/Socket/socket.d.ts +0 -59
- package/lib/Types/Auth.d.ts +0 -117
- package/lib/Types/Bussines.d.ts +0 -25
- package/lib/Types/Call.d.ts +0 -15
- package/lib/Types/Chat.d.ts +0 -124
- package/lib/Types/Contact.d.ts +0 -26
- package/lib/Types/Events.d.ts +0 -256
- package/lib/Types/GroupMetadata.d.ts +0 -71
- package/lib/Types/Label.d.ts +0 -47
- package/lib/Types/LabelAssociation.d.ts +0 -30
- package/lib/Types/Message.d.ts +0 -320
- package/lib/Types/Mex.d.ts +0 -141
- package/lib/Types/Product.d.ts +0 -79
- package/lib/Types/Signal.d.ts +0 -87
- package/lib/Types/Socket.d.ts +0 -136
- package/lib/Types/State.d.ts +0 -97
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -66
- package/lib/Utils/auth-utils.d.ts +0 -24
- package/lib/Utils/browser-utils.d.ts +0 -4
- package/lib/Utils/business.d.ts +0 -23
- package/lib/Utils/chat-utils.d.ts +0 -100
- package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
- package/lib/Utils/crypto.d.ts +0 -37
- package/lib/Utils/decode-wa-message.d.ts +0 -66
- package/lib/Utils/event-buffer.d.ts +0 -36
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -24
- package/lib/Utils/identity-change-handler.d.ts +0 -44
- package/lib/Utils/identity-change-handler.js +0 -50
- package/lib/Utils/index.d.ts +0 -22
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -12
- package/lib/Utils/lt-hash.d.ts +0 -8
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -115
- package/lib/Utils/messages-media.d.ts +0 -133
- package/lib/Utils/messages.d.ts +0 -91
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/offline-node-processor.d.ts +0 -17
- package/lib/Utils/offline-node-processor.js +0 -40
- package/lib/Utils/pre-key-manager.d.ts +0 -28
- package/lib/Utils/process-message.d.ts +0 -60
- package/lib/Utils/reporting-utils.d.ts +0 -11
- package/lib/Utils/signal.d.ts +0 -47
- package/lib/Utils/stanza-ack.d.ts +0 -11
- package/lib/Utils/sync-action-utils.d.ts +0 -19
- package/lib/Utils/tc-token-utils.d.ts +0 -37
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -28
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -6
- package/lib/WABinary/jid-utils.d.ts +0 -48
- package/lib/WABinary/types.d.ts +0 -19
- package/lib/WAM/BinaryInfo.d.ts +0 -9
- package/lib/WAM/constants.d.ts +0 -40
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -30
- package/lib/WAUSync/USyncUser.d.ts +0 -17
- package/lib/WAUSync/index.d.ts +0 -4
- package/lib/index.d.ts +0 -12
|
@@ -1,265 +1,224 @@
|
|
|
1
1
|
import { LRUCache } from 'lru-cache';
|
|
2
|
-
/** Number of sent messages to cache in memory for handling retry receipts */
|
|
3
2
|
const RECENT_MESSAGES_SIZE = 512;
|
|
4
3
|
const MESSAGE_KEY_SEPARATOR = '\u0000';
|
|
5
|
-
|
|
6
|
-
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
4
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000;
|
|
7
5
|
const PHONE_REQUEST_DELAY = 3000;
|
|
8
|
-
// Retry reason codes matching WhatsApp Web's Signal error codes.
|
|
9
6
|
export var RetryReason;
|
|
10
7
|
(function (RetryReason) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
|
|
26
|
-
RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
|
|
8
|
+
RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
|
|
9
|
+
RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
|
|
10
|
+
RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
|
|
11
|
+
RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
|
|
12
|
+
RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
|
|
13
|
+
RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
|
|
14
|
+
RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
|
|
15
|
+
RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
|
|
16
|
+
RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
|
|
17
|
+
RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
|
|
18
|
+
RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
|
|
19
|
+
RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
|
|
20
|
+
RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
|
|
21
|
+
RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
|
|
27
22
|
})(RetryReason || (RetryReason = {}));
|
|
28
|
-
/** Error codes that indicate a MAC failure and require immediate session recreation */
|
|
29
23
|
const MAC_ERROR_CODES = new Set([RetryReason.SignalErrorInvalidMessage, RetryReason.SignalErrorBadMac]);
|
|
30
24
|
export class MessageRetryManager {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
this.messageKeyIndex = new Map();
|
|
46
|
-
this.sessionRecreateHistory = new LRUCache({
|
|
47
|
-
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
48
|
-
ttlAutopurge: true
|
|
49
|
-
});
|
|
50
|
-
this.retryCounters = new LRUCache({
|
|
51
|
-
ttl: 15 * 60 * 1000,
|
|
52
|
-
ttlAutopurge: true,
|
|
53
|
-
updateAgeOnGet: true
|
|
54
|
-
}); // 15 minutes TTL
|
|
55
|
-
this.baseKeys = new LRUCache({
|
|
56
|
-
max: 1024,
|
|
57
|
-
ttl: 15 * 60 * 1000,
|
|
58
|
-
ttlAutopurge: true
|
|
59
|
-
});
|
|
60
|
-
this.pendingPhoneRequests = {};
|
|
61
|
-
this.maxMsgRetryCount = 5;
|
|
62
|
-
this.statistics = {
|
|
63
|
-
totalRetries: 0,
|
|
64
|
-
successfulRetries: 0,
|
|
65
|
-
failedRetries: 0,
|
|
66
|
-
mediaRetries: 0,
|
|
67
|
-
sessionRecreations: 0,
|
|
68
|
-
phoneRequests: 0
|
|
69
|
-
};
|
|
70
|
-
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Add a recent message to the cache for retry handling
|
|
74
|
-
*/
|
|
75
|
-
addRecentMessage(to, id, message) {
|
|
76
|
-
const key = { to, id };
|
|
77
|
-
const keyStr = this.keyToString(key);
|
|
78
|
-
// Add new message
|
|
79
|
-
this.recentMessagesMap.set(keyStr, {
|
|
80
|
-
message,
|
|
81
|
-
timestamp: Date.now()
|
|
82
|
-
});
|
|
83
|
-
this.messageKeyIndex.set(id, keyStr);
|
|
84
|
-
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Get a recent message from the cache
|
|
88
|
-
*/
|
|
89
|
-
getRecentMessage(to, id) {
|
|
90
|
-
const key = { to, id };
|
|
91
|
-
const keyStr = this.keyToString(key);
|
|
92
|
-
return this.recentMessagesMap.get(keyStr);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Check if a session should be recreated based on retry count, history, and error code.
|
|
96
|
-
* MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
|
|
97
|
-
*/
|
|
98
|
-
shouldRecreateSession(jid, hasSession, errorCode) {
|
|
99
|
-
// If we don't have a session, always recreate
|
|
100
|
-
if (!hasSession) {
|
|
101
|
-
this.sessionRecreateHistory.set(jid, Date.now());
|
|
102
|
-
this.statistics.sessionRecreations++;
|
|
103
|
-
return {
|
|
104
|
-
reason: "we don't have a Signal session with them",
|
|
105
|
-
recreate: true
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
// IMMEDIATE recreation for MAC errors - session is definitely out of sync
|
|
109
|
-
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
110
|
-
this.sessionRecreateHistory.set(jid, Date.now());
|
|
111
|
-
this.statistics.sessionRecreations++;
|
|
112
|
-
this.logger.warn({ jid, errorCode: RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
|
|
113
|
-
return {
|
|
114
|
-
reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
|
|
115
|
-
recreate: true
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
const now = Date.now();
|
|
119
|
-
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
120
|
-
// If no previous recreation or it's been more than an hour
|
|
121
|
-
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
122
|
-
this.sessionRecreateHistory.set(jid, now);
|
|
123
|
-
this.statistics.sessionRecreations++;
|
|
124
|
-
return {
|
|
125
|
-
reason: 'retry count > 1 and over an hour since last recreation',
|
|
126
|
-
recreate: true
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
return { reason: '', recreate: false };
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Parse error code from retry receipt's retry node.
|
|
133
|
-
* Returns undefined if no error code is present.
|
|
134
|
-
*/
|
|
135
|
-
parseRetryErrorCode(errorAttr) {
|
|
136
|
-
if (errorAttr === undefined || errorAttr === '') {
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
|
-
const code = parseInt(errorAttr, 10);
|
|
140
|
-
if (Number.isNaN(code)) {
|
|
141
|
-
return undefined;
|
|
25
|
+
constructor(logger, maxMsgRetryCount) {
|
|
26
|
+
this.logger = logger;
|
|
27
|
+
this.recentMessagesMap = new LRUCache({
|
|
28
|
+
max: RECENT_MESSAGES_SIZE,
|
|
29
|
+
ttl: 5 * 60 * 1000,
|
|
30
|
+
ttlAutopurge: true,
|
|
31
|
+
dispose: (_value, key) => {
|
|
32
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
33
|
+
if (separatorIndex > -1) {
|
|
34
|
+
const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
|
|
35
|
+
this.messageKeyIndex.delete(messageId);
|
|
142
36
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
this.messageKeyIndex = new Map();
|
|
40
|
+
this.sessionRecreateHistory = new LRUCache({
|
|
41
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
42
|
+
ttlAutopurge: true
|
|
43
|
+
});
|
|
44
|
+
this.retryCounters = new LRUCache({
|
|
45
|
+
ttl: 15 * 60 * 1000,
|
|
46
|
+
ttlAutopurge: true,
|
|
47
|
+
updateAgeOnGet: true
|
|
48
|
+
});
|
|
49
|
+
this.baseKeys = new LRUCache({
|
|
50
|
+
max: 1024,
|
|
51
|
+
ttl: 15 * 60 * 1000,
|
|
52
|
+
ttlAutopurge: true
|
|
53
|
+
});
|
|
54
|
+
this.pendingPhoneRequests = {};
|
|
55
|
+
this.maxMsgRetryCount = 5;
|
|
56
|
+
this.statistics = {
|
|
57
|
+
totalRetries: 0,
|
|
58
|
+
successfulRetries: 0,
|
|
59
|
+
failedRetries: 0,
|
|
60
|
+
mediaRetries: 0,
|
|
61
|
+
sessionRecreations: 0,
|
|
62
|
+
phoneRequests: 0
|
|
63
|
+
};
|
|
64
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
65
|
+
}
|
|
66
|
+
addRecentMessage(to, id, message) {
|
|
67
|
+
const key = {
|
|
68
|
+
to,
|
|
69
|
+
id
|
|
70
|
+
};
|
|
71
|
+
const keyStr = this.keyToString(key);
|
|
72
|
+
this.recentMessagesMap.set(keyStr, {
|
|
73
|
+
message,
|
|
74
|
+
timestamp: Date.now()
|
|
75
|
+
});
|
|
76
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
77
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
78
|
+
}
|
|
79
|
+
getRecentMessage(to, id) {
|
|
80
|
+
const key = {
|
|
81
|
+
to,
|
|
82
|
+
id
|
|
83
|
+
};
|
|
84
|
+
const keyStr = this.keyToString(key);
|
|
85
|
+
return this.recentMessagesMap.get(keyStr);
|
|
86
|
+
}
|
|
87
|
+
shouldRecreateSession(jid, hasSession, errorCode) {
|
|
88
|
+
if (!hasSession) {
|
|
89
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
90
|
+
this.statistics.sessionRecreations++;
|
|
91
|
+
return {
|
|
92
|
+
reason: "we don't have a Signal session with them",
|
|
93
|
+
recreate: true
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
97
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
98
|
+
this.statistics.sessionRecreations++;
|
|
99
|
+
this.logger.warn({
|
|
100
|
+
jid,
|
|
101
|
+
errorCode: RetryReason[errorCode]
|
|
102
|
+
}, 'MAC error detected, forcing immediate session recreation');
|
|
103
|
+
return {
|
|
104
|
+
reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
|
|
105
|
+
recreate: true
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const now = Date.now();
|
|
109
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
110
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
111
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
112
|
+
this.statistics.sessionRecreations++;
|
|
113
|
+
return {
|
|
114
|
+
reason: 'retry count > 1 and over an hour since last recreation',
|
|
115
|
+
recreate: true
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
reason: '',
|
|
120
|
+
recreate: false
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
parseRetryErrorCode(errorAttr) {
|
|
124
|
+
if (errorAttr === undefined || errorAttr === '') {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
const code = parseInt(errorAttr, 10);
|
|
128
|
+
if (Number.isNaN(code)) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
if (code >= RetryReason.UnknownError && code <= RetryReason.StatusRevokeDelay) {
|
|
132
|
+
return code;
|
|
133
|
+
}
|
|
134
|
+
return RetryReason.UnknownError;
|
|
135
|
+
}
|
|
136
|
+
isMacError(errorCode) {
|
|
137
|
+
return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
|
|
138
|
+
}
|
|
139
|
+
incrementRetryCount(messageId) {
|
|
140
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
141
|
+
this.statistics.totalRetries++;
|
|
142
|
+
return this.retryCounters.get(messageId);
|
|
143
|
+
}
|
|
144
|
+
getRetryCount(messageId) {
|
|
145
|
+
return this.retryCounters.get(messageId) || 0;
|
|
146
|
+
}
|
|
147
|
+
hasExceededMaxRetries(messageId) {
|
|
148
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
149
|
+
}
|
|
150
|
+
markRetrySuccess(messageId) {
|
|
151
|
+
this.statistics.successfulRetries++;
|
|
152
|
+
this.retryCounters.delete(messageId);
|
|
153
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
154
|
+
this.removeRecentMessage(messageId);
|
|
155
|
+
}
|
|
156
|
+
markRetryFailed(messageId) {
|
|
157
|
+
this.statistics.failedRetries++;
|
|
158
|
+
this.retryCounters.delete(messageId);
|
|
159
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
160
|
+
this.removeRecentMessage(messageId);
|
|
161
|
+
}
|
|
162
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
163
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
164
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
165
|
+
delete this.pendingPhoneRequests[messageId];
|
|
166
|
+
this.statistics.phoneRequests++;
|
|
167
|
+
callback();
|
|
168
|
+
}, delay);
|
|
169
|
+
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
|
|
170
|
+
}
|
|
171
|
+
cancelPendingPhoneRequest(messageId) {
|
|
172
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
173
|
+
if (timeout) {
|
|
174
|
+
clearTimeout(timeout);
|
|
175
|
+
delete this.pendingPhoneRequests[messageId];
|
|
176
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
clear() {
|
|
180
|
+
this.recentMessagesMap.clear();
|
|
181
|
+
this.messageKeyIndex.clear();
|
|
182
|
+
this.sessionRecreateHistory.clear();
|
|
183
|
+
this.retryCounters.clear();
|
|
184
|
+
this.baseKeys.clear();
|
|
185
|
+
for (const messageId of Object.keys(this.pendingPhoneRequests)) {
|
|
186
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
187
|
+
}
|
|
188
|
+
this.statistics = {
|
|
189
|
+
totalRetries: 0,
|
|
190
|
+
successfulRetries: 0,
|
|
191
|
+
failedRetries: 0,
|
|
192
|
+
mediaRetries: 0,
|
|
193
|
+
sessionRecreations: 0,
|
|
194
|
+
phoneRequests: 0
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
saveBaseKey(addr, msgId, baseKey) {
|
|
198
|
+
this.baseKeys.set(`${addr}:${msgId}`, baseKey);
|
|
199
|
+
}
|
|
200
|
+
hasSameBaseKey(addr, msgId, baseKey) {
|
|
201
|
+
const stored = this.baseKeys.get(`${addr}:${msgId}`);
|
|
202
|
+
if (!stored || stored.length !== baseKey.length) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
for (let i = 0; i < stored.length; i++) {
|
|
206
|
+
if (stored[i] !== baseKey[i]) return false;
|
|
207
|
+
}
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
deleteBaseKey(addr, msgId) {
|
|
211
|
+
this.baseKeys.delete(`${addr}:${msgId}`);
|
|
212
|
+
}
|
|
213
|
+
keyToString(key) {
|
|
214
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
215
|
+
}
|
|
216
|
+
removeRecentMessage(messageId) {
|
|
217
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
218
|
+
if (!keyStr) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
this.recentMessagesMap.delete(keyStr);
|
|
222
|
+
this.messageKeyIndex.delete(messageId);
|
|
223
|
+
}
|
|
264
224
|
}
|
|
265
|
-
//# sourceMappingURL=message-retry-manager.js.map
|