xmd-baileys 1.0.21 → 1.0.23
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/lib/Defaults/index.js
CHANGED
|
@@ -82,8 +82,8 @@ exports.DEFAULT_CONNECTION_CONFIG = {
|
|
|
82
82
|
emitOwnEvents: !0,
|
|
83
83
|
defaultQueryTimeoutMs: 6E4,
|
|
84
84
|
customUploadHosts: [],
|
|
85
|
-
retryRequestDelayMs:
|
|
86
|
-
maxMsgRetryCount:
|
|
85
|
+
retryRequestDelayMs: 50,
|
|
86
|
+
maxMsgRetryCount: 2,
|
|
87
87
|
fireInitQueries: !0,
|
|
88
88
|
auth: void 0,
|
|
89
89
|
markOnlineOnConnect: !0,
|
|
@@ -92,8 +92,10 @@ exports.DEFAULT_CONNECTION_CONFIG = {
|
|
|
92
92
|
shouldSyncHistoryMessage: () => !0,
|
|
93
93
|
shouldIgnoreJid: () => !1,
|
|
94
94
|
linkPreviewImageThumbnailWidth: 192,
|
|
95
|
-
transactionOpts: { maxCommitRetries:
|
|
95
|
+
transactionOpts: { maxCommitRetries: 3, delayBetweenTriesMs: 500 },
|
|
96
96
|
generateHighQualityLinkPreview: !1,
|
|
97
|
+
enableAutoSessionRecreation: !0,
|
|
98
|
+
enableRecentMessageCache: !0,
|
|
97
99
|
options: {},
|
|
98
100
|
appStateMacVerification: { patch: !1, snapshot: !1 },
|
|
99
101
|
countryCode: "US",
|
package/lib/Utils/index.js
CHANGED
|
@@ -28,6 +28,7 @@ __exportStar(require("./lt-hash"), exports);
|
|
|
28
28
|
__exportStar(require("./auth-utils"), exports);
|
|
29
29
|
__exportStar(require("./baileys-event-stream"), exports);
|
|
30
30
|
__exportStar(require("./use-multi-file-auth-state"), exports);
|
|
31
|
+
__exportStar(require("./message-retry-manager"), exports);
|
|
31
32
|
__exportStar(require("./link-preview"), exports);
|
|
32
33
|
__exportStar(require("./event-buffer"), exports);
|
|
33
34
|
__exportStar(require("./process-message"), exports);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageRetryManager = void 0;
|
|
4
|
+
const NodeCache = require("node-cache");
|
|
5
|
+
const RECENT_MESSAGES_SIZE = 512;
|
|
6
|
+
const MESSAGE_KEY_SEPARATOR = '\u0000';
|
|
7
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000;
|
|
8
|
+
const PHONE_REQUEST_DELAY = 3000;
|
|
9
|
+
class MessageRetryManager {
|
|
10
|
+
constructor(logger, maxMsgRetryCount) {
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
this.recentMessagesMap = new NodeCache({
|
|
13
|
+
maxKeys: RECENT_MESSAGES_SIZE,
|
|
14
|
+
stdTTL: 5 * 60,
|
|
15
|
+
useClones: false
|
|
16
|
+
});
|
|
17
|
+
this.messageKeyIndex = new Map();
|
|
18
|
+
this.sessionRecreateHistory = new NodeCache({
|
|
19
|
+
stdTTL: RECREATE_SESSION_TIMEOUT * 2 / 1000,
|
|
20
|
+
useClones: false
|
|
21
|
+
});
|
|
22
|
+
this.retryCounters = new NodeCache({
|
|
23
|
+
stdTTL: 15 * 60,
|
|
24
|
+
useClones: false
|
|
25
|
+
});
|
|
26
|
+
this.pendingPhoneRequests = {};
|
|
27
|
+
this.maxMsgRetryCount = maxMsgRetryCount || 5;
|
|
28
|
+
this.statistics = {
|
|
29
|
+
totalRetries: 0,
|
|
30
|
+
successfulRetries: 0,
|
|
31
|
+
failedRetries: 0,
|
|
32
|
+
mediaRetries: 0,
|
|
33
|
+
sessionRecreations: 0,
|
|
34
|
+
phoneRequests: 0
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
addRecentMessage(to, id, message) {
|
|
38
|
+
const key = { to, id };
|
|
39
|
+
const keyStr = this.keyToString(key);
|
|
40
|
+
this.recentMessagesMap.set(keyStr, {
|
|
41
|
+
message,
|
|
42
|
+
timestamp: Date.now()
|
|
43
|
+
});
|
|
44
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
45
|
+
}
|
|
46
|
+
getRecentMessage(to, id) {
|
|
47
|
+
const key = { to, id };
|
|
48
|
+
const keyStr = this.keyToString(key);
|
|
49
|
+
return this.recentMessagesMap.get(keyStr);
|
|
50
|
+
}
|
|
51
|
+
shouldRecreateSession(jid, retryCount, hasSession) {
|
|
52
|
+
if (!hasSession) {
|
|
53
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
54
|
+
this.statistics.sessionRecreations++;
|
|
55
|
+
return {
|
|
56
|
+
reason: "we don't have a Signal session with them",
|
|
57
|
+
recreate: true
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (retryCount < 2) {
|
|
61
|
+
return { reason: '', recreate: false };
|
|
62
|
+
}
|
|
63
|
+
const now = Date.now();
|
|
64
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
65
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
66
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
67
|
+
this.statistics.sessionRecreations++;
|
|
68
|
+
return {
|
|
69
|
+
reason: 'retry count > 1 and over an hour since last recreation',
|
|
70
|
+
recreate: true
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return { reason: '', recreate: false };
|
|
74
|
+
}
|
|
75
|
+
incrementRetryCount(messageId) {
|
|
76
|
+
const current = this.retryCounters.get(messageId) || 0;
|
|
77
|
+
this.retryCounters.set(messageId, current + 1);
|
|
78
|
+
this.statistics.totalRetries++;
|
|
79
|
+
return current + 1;
|
|
80
|
+
}
|
|
81
|
+
getRetryCount(messageId) {
|
|
82
|
+
return this.retryCounters.get(messageId) || 0;
|
|
83
|
+
}
|
|
84
|
+
hasExceededMaxRetries(messageId) {
|
|
85
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
86
|
+
}
|
|
87
|
+
markRetrySuccess(messageId) {
|
|
88
|
+
this.statistics.successfulRetries++;
|
|
89
|
+
this.retryCounters.del(messageId);
|
|
90
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
91
|
+
this.removeRecentMessage(messageId);
|
|
92
|
+
}
|
|
93
|
+
markRetryFailed(messageId) {
|
|
94
|
+
this.statistics.failedRetries++;
|
|
95
|
+
this.retryCounters.del(messageId);
|
|
96
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
97
|
+
this.removeRecentMessage(messageId);
|
|
98
|
+
}
|
|
99
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
100
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
101
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
102
|
+
delete this.pendingPhoneRequests[messageId];
|
|
103
|
+
this.statistics.phoneRequests++;
|
|
104
|
+
callback();
|
|
105
|
+
}, delay);
|
|
106
|
+
}
|
|
107
|
+
cancelPendingPhoneRequest(messageId) {
|
|
108
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
109
|
+
if (timeout) {
|
|
110
|
+
clearTimeout(timeout);
|
|
111
|
+
delete this.pendingPhoneRequests[messageId];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
keyToString(key) {
|
|
115
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
116
|
+
}
|
|
117
|
+
removeRecentMessage(messageId) {
|
|
118
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
119
|
+
if (!keyStr) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.recentMessagesMap.del(keyStr);
|
|
123
|
+
this.messageKeyIndex.delete(messageId);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.MessageRetryManager = MessageRetryManager;
|