@nexustechpro/baileys 1.0.1
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.md +1573 -0
- package/WAProto/fix-imports.js +29 -0
- package/WAProto/index.js +169659 -0
- package/lib/Defaults/index.js +115 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/libsignal.js +342 -0
- package/lib/Signal/lid-mapping.js +171 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +91 -0
- package/lib/Socket/business.js +376 -0
- package/lib/Socket/chats.js +963 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/groups.js +328 -0
- package/lib/Socket/index.js +19 -0
- package/lib/Socket/messages-recv.js +1240 -0
- package/lib/Socket/messages-send.js +1370 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.js +202 -0
- package/lib/Socket/nexus-handler.js +667 -0
- package/lib/Socket/socket.js +871 -0
- package/lib/Store/index.js +4 -0
- package/lib/Store/make-cache-manager-store.js +81 -0
- package/lib/Store/make-in-memory-store.js +416 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Newsletter.js +31 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +3 -0
- package/lib/Types/State.js +13 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +26 -0
- package/lib/Utils/auth-utils.js +257 -0
- package/lib/Utils/baileys-event-stream.js +56 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/chat-utils.js +763 -0
- package/lib/Utils/crypto.js +142 -0
- package/lib/Utils/decode-wa-message.js +279 -0
- package/lib/Utils/event-buffer.js +548 -0
- package/lib/Utils/generics.js +381 -0
- package/lib/Utils/history.js +84 -0
- package/lib/Utils/index.js +20 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/lt-hash.js +48 -0
- package/lib/Utils/make-mutex.js +40 -0
- package/lib/Utils/message-retry-manager.js +149 -0
- package/lib/Utils/messages-media.js +685 -0
- package/lib/Utils/messages.js +820 -0
- package/lib/Utils/noise-handler.js +147 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/process-message.js +413 -0
- package/lib/Utils/signal.js +159 -0
- package/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/lib/Utils/validate-connection.js +195 -0
- package/lib/WABinary/constants.js +1301 -0
- package/lib/WABinary/decode.js +238 -0
- package/lib/WABinary/encode.js +216 -0
- package/lib/WABinary/generic-utils.js +111 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +96 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/index.js +5 -0
- package/lib/WAUSync/USyncQuery.js +94 -0
- package/lib/WAUSync/USyncUser.js +23 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/index.js +24 -0
- package/package.json +113 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
|
+
/** Number of sent messages to cache in memory for handling retry receipts */
|
|
3
|
+
const RECENT_MESSAGES_SIZE = 512;
|
|
4
|
+
/** Timeout for session recreation - 1 hour */
|
|
5
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
6
|
+
const PHONE_REQUEST_DELAY = 3000;
|
|
7
|
+
export class MessageRetryManager {
|
|
8
|
+
constructor(logger, maxMsgRetryCount) {
|
|
9
|
+
this.logger = logger;
|
|
10
|
+
this.recentMessagesMap = new LRUCache({
|
|
11
|
+
max: RECENT_MESSAGES_SIZE
|
|
12
|
+
});
|
|
13
|
+
this.sessionRecreateHistory = new LRUCache({
|
|
14
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
15
|
+
ttlAutopurge: true
|
|
16
|
+
});
|
|
17
|
+
this.retryCounters = new LRUCache({
|
|
18
|
+
ttl: 15 * 60 * 1000,
|
|
19
|
+
ttlAutopurge: true,
|
|
20
|
+
updateAgeOnGet: true
|
|
21
|
+
}); // 15 minutes TTL
|
|
22
|
+
this.pendingPhoneRequests = {};
|
|
23
|
+
this.maxMsgRetryCount = 5;
|
|
24
|
+
this.statistics = {
|
|
25
|
+
totalRetries: 0,
|
|
26
|
+
successfulRetries: 0,
|
|
27
|
+
failedRetries: 0,
|
|
28
|
+
mediaRetries: 0,
|
|
29
|
+
sessionRecreations: 0,
|
|
30
|
+
phoneRequests: 0
|
|
31
|
+
};
|
|
32
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Add a recent message to the cache for retry handling
|
|
36
|
+
*/
|
|
37
|
+
addRecentMessage(to, id, message) {
|
|
38
|
+
const key = { to, id };
|
|
39
|
+
const keyStr = this.keyToString(key);
|
|
40
|
+
// Add new message
|
|
41
|
+
this.recentMessagesMap.set(keyStr, {
|
|
42
|
+
message,
|
|
43
|
+
timestamp: Date.now()
|
|
44
|
+
});
|
|
45
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get a recent message from the cache
|
|
49
|
+
*/
|
|
50
|
+
getRecentMessage(to, id) {
|
|
51
|
+
const key = { to, id };
|
|
52
|
+
const keyStr = this.keyToString(key);
|
|
53
|
+
return this.recentMessagesMap.get(keyStr);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if a session should be recreated based on retry count and history
|
|
57
|
+
*/
|
|
58
|
+
shouldRecreateSession(jid, retryCount, hasSession) {
|
|
59
|
+
// If we don't have a session, always recreate
|
|
60
|
+
if (!hasSession) {
|
|
61
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
62
|
+
this.statistics.sessionRecreations++;
|
|
63
|
+
return {
|
|
64
|
+
reason: "we don't have a Signal session with them",
|
|
65
|
+
recreate: true
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// Only consider recreation if retry count > 1
|
|
69
|
+
if (retryCount < 2) {
|
|
70
|
+
return { reason: '', recreate: false };
|
|
71
|
+
}
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
74
|
+
// If no previous recreation or it's been more than an hour
|
|
75
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
76
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
77
|
+
this.statistics.sessionRecreations++;
|
|
78
|
+
return {
|
|
79
|
+
reason: 'retry count > 1 and over an hour since last recreation',
|
|
80
|
+
recreate: true
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return { reason: '', recreate: false };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Increment retry counter for a message
|
|
87
|
+
*/
|
|
88
|
+
incrementRetryCount(messageId) {
|
|
89
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
90
|
+
this.statistics.totalRetries++;
|
|
91
|
+
return this.retryCounters.get(messageId);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Get retry count for a message
|
|
95
|
+
*/
|
|
96
|
+
getRetryCount(messageId) {
|
|
97
|
+
return this.retryCounters.get(messageId) || 0;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Check if message has exceeded maximum retry attempts
|
|
101
|
+
*/
|
|
102
|
+
hasExceededMaxRetries(messageId) {
|
|
103
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Mark retry as successful
|
|
107
|
+
*/
|
|
108
|
+
markRetrySuccess(messageId) {
|
|
109
|
+
this.statistics.successfulRetries++;
|
|
110
|
+
// Clean up retry counter for successful message
|
|
111
|
+
this.retryCounters.delete(messageId);
|
|
112
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Mark retry as failed
|
|
116
|
+
*/
|
|
117
|
+
markRetryFailed(messageId) {
|
|
118
|
+
this.statistics.failedRetries++;
|
|
119
|
+
this.retryCounters.delete(messageId);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Schedule a phone request with delay
|
|
123
|
+
*/
|
|
124
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
125
|
+
// Cancel any existing request for this message
|
|
126
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
127
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
128
|
+
delete this.pendingPhoneRequests[messageId];
|
|
129
|
+
this.statistics.phoneRequests++;
|
|
130
|
+
callback();
|
|
131
|
+
}, delay);
|
|
132
|
+
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Cancel pending phone request
|
|
136
|
+
*/
|
|
137
|
+
cancelPendingPhoneRequest(messageId) {
|
|
138
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
139
|
+
if (timeout) {
|
|
140
|
+
clearTimeout(timeout);
|
|
141
|
+
delete this.pendingPhoneRequests[messageId];
|
|
142
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
keyToString(key) {
|
|
146
|
+
return `${key.to}:${key.id}`;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=message-retry-manager.js.map
|