@pontalabs/baileys 1.0.10 → 1.1.0
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 +96 -85
- package/lib/Defaults/connection.js +39 -0
- package/lib/Defaults/constants.js +50 -0
- package/lib/Defaults/history.js +13 -0
- package/lib/Defaults/index.js +36 -151
- package/lib/Defaults/media.js +43 -0
- package/lib/Defaults/phonenumber-mcc.json +221 -221
- package/lib/Defaults/prefix.js +12 -0
- package/lib/Defaults/vialeys-version.json +7 -0
- package/lib/Signal/Group/ciphertext-message.js +10 -15
- package/lib/Signal/Group/group-session-builder.js +34 -60
- package/lib/Signal/Group/group_cipher.js +69 -76
- package/lib/Signal/Group/index.js +41 -60
- package/lib/Signal/Group/keyhelper.js +12 -57
- package/lib/Signal/Group/sender-chain-key.js +20 -33
- package/lib/Signal/Group/sender-key-distribution-message.js +43 -48
- package/lib/Signal/Group/sender-key-message.js +58 -53
- package/lib/Signal/Group/sender-key-name.js +21 -31
- package/lib/Signal/Group/sender-key-record.js +21 -34
- package/lib/Signal/Group/sender-key-state.js +59 -106
- package/lib/Signal/Group/sender-message-key.js +16 -21
- package/lib/Signal/libsignal.js +282 -203
- package/lib/Signal/lid-mapping.js +164 -184
- package/lib/Socket/Client/index.js +31 -22
- package/lib/Socket/Client/types.js +9 -15
- package/lib/Socket/Client/websocket.js +42 -37
- package/lib/Socket/business.js +212 -336
- package/lib/Socket/chats.js +696 -940
- package/lib/Socket/community.js +454 -369
- package/lib/Socket/groups.js +278 -280
- package/lib/Socket/index.js +9 -21
- package/lib/Socket/messages-recv.js +1027 -1570
- package/lib/Socket/messages-send.js +1180 -1136
- package/lib/Socket/mex.js +59 -0
- package/lib/Socket/newsletter.js +193 -251
- package/lib/Socket/socket.js +758 -898
- package/lib/Store/index.js +35 -24
- package/lib/Store/make-cache-manager-store.js +53 -66
- package/lib/Store/make-in-memory-store.js +319 -325
- package/lib/Store/make-ordered-dictionary.js +49 -57
- package/lib/Store/object-repository.js +10 -15
- package/lib/Types/Auth.js +2 -3
- package/lib/Types/Bussines.js +2 -3
- package/lib/Types/Call.js +2 -3
- package/lib/Types/Chat.js +10 -9
- package/lib/Types/Contact.js +2 -3
- package/lib/Types/Events.js +2 -3
- package/lib/Types/GroupMetadata.js +2 -3
- package/lib/Types/Label.js +6 -12
- package/lib/Types/LabelAssociation.js +4 -13
- package/lib/Types/Message.js +10 -18
- package/lib/Types/MexUpdates.js +15 -18
- package/lib/Types/Newsletter.js +26 -34
- package/lib/Types/Product.js +2 -3
- package/lib/Types/Signal.js +2 -3
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +8 -13
- package/lib/Types/USync.js +2 -3
- package/lib/Types/index.js +49 -40
- package/lib/Utils/auth-utils.js +190 -495
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/business.js +177 -199
- package/lib/Utils/chat-utils.js +711 -624
- package/lib/Utils/crypto.js +116 -161
- package/lib/Utils/decode-wa-message.js +225 -321
- package/lib/Utils/event-buffer.js +363 -405
- package/lib/Utils/generics.js +318 -472
- package/lib/Utils/history.js +72 -76
- package/lib/Utils/index.js +49 -41
- package/lib/Utils/link-preview.js +59 -103
- package/lib/Utils/logger.js +4 -7
- package/lib/Utils/lt-hash.js +29 -42
- package/lib/Utils/make-mutex.js +26 -42
- package/lib/Utils/message-retry-manager.js +134 -160
- package/lib/Utils/messages-media.js +665 -731
- package/lib/Utils/messages.js +518 -1
- package/lib/Utils/noise-handler.js +127 -125
- package/lib/Utils/pre-key-manager.js +86 -0
- package/lib/Utils/process-message.js +739 -351
- package/lib/Utils/signal.js +146 -129
- package/lib/Utils/use-multi-file-auth-state.js +68 -135
- package/lib/Utils/validate-connection.js +183 -142
- package/lib/Utils/vialeys-event-stream.js +41 -0
- package/lib/WABinary/constants.js +1281 -1289
- package/lib/WABinary/decode.js +145 -200
- package/lib/WABinary/encode.js +149 -202
- package/lib/WABinary/generic-utils.js +79 -97
- package/lib/WABinary/index.js +36 -25
- package/lib/WABinary/jid-utils.js +91 -123
- package/lib/WABinary/types.js +2 -3
- package/lib/WAM/BinaryInfo.js +8 -13
- package/lib/WAM/constants.js +16584 -11464
- package/lib/WAM/encode.js +117 -133
- package/lib/WAM/index.js +34 -23
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -69
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +11 -25
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +32 -44
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +13 -23
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -38
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +17 -31
- package/lib/WAUSync/Protocols/index.js +37 -26
- package/lib/WAUSync/USyncQuery.js +66 -66
- package/lib/WAUSync/USyncUser.js +13 -18
- package/lib/WAUSync/index.js +34 -23
- package/lib/index.js +95 -28
- package/package.json +4 -3
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Socket/usync.js +0 -83
- package/lib/Utils/baileys-event-stream.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.js +0 -70
- package/lib/Utils/signal-housekeeping.d.ts +0 -19
- package/lib/Utils/signal-housekeeping.js +0 -73
- package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
- package/lib/Utils/use-mongo-file-auth-state.js +0 -84
- package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.js +0 -80
|
@@ -1,160 +1,134 @@
|
|
|
1
|
-
"use strict"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
addRecentMessage(to, id, message) {
|
|
46
|
-
const key = { to, id }
|
|
47
|
-
const keyStr = this.keyToString(key)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.logger.debug(`Added message to retry cache: ${to}/${id}`)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
this.retryCounters.
|
|
98
|
-
this.
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
this.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
136
|
-
delete this.pendingPhoneRequests[messageId]
|
|
137
|
-
this.statistics.phoneRequests++
|
|
138
|
-
callback()
|
|
139
|
-
}, delay)
|
|
140
|
-
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`)
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Cancel pending phone request
|
|
144
|
-
*/
|
|
145
|
-
cancelPendingPhoneRequest(messageId) {
|
|
146
|
-
const timeout = this.pendingPhoneRequests[messageId]
|
|
147
|
-
if (timeout) {
|
|
148
|
-
clearTimeout(timeout)
|
|
149
|
-
delete this.pendingPhoneRequests[messageId]
|
|
150
|
-
this.logger.debug(`Cancelled pending phone request for message ${messageId}`)
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
keyToString(key) {
|
|
154
|
-
return `${key.to}:${key.id}`
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
module.exports = {
|
|
159
|
-
MessageRetryManager
|
|
160
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { LRUCache: LRUCache } = require("lru-cache");
|
|
4
|
+
const RECENT_MESSAGES_SIZE = 512;
|
|
5
|
+
const MESSAGE_KEY_SEPARATOR = "\0";
|
|
6
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1e3;
|
|
7
|
+
const PHONE_REQUEST_DELAY = 3e3;
|
|
8
|
+
class MessageRetryManager {
|
|
9
|
+
constructor(logger, maxMsgRetryCount) {
|
|
10
|
+
this.logger = logger;
|
|
11
|
+
this.recentMessagesMap = new LRUCache({
|
|
12
|
+
max: RECENT_MESSAGES_SIZE,
|
|
13
|
+
ttl: 5 * 60 * 1e3,
|
|
14
|
+
ttlAutopurge: true,
|
|
15
|
+
dispose: (_value, key) => {
|
|
16
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
17
|
+
if (separatorIndex > -1) {
|
|
18
|
+
const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
|
|
19
|
+
this.messageKeyIndex.delete(messageId);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
this.messageKeyIndex = new Map();
|
|
24
|
+
this.sessionRecreateHistory = new LRUCache({
|
|
25
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
26
|
+
ttlAutopurge: true,
|
|
27
|
+
});
|
|
28
|
+
this.retryCounters = new LRUCache({
|
|
29
|
+
ttl: 15 * 60 * 1e3,
|
|
30
|
+
ttlAutopurge: true,
|
|
31
|
+
updateAgeOnGet: true,
|
|
32
|
+
});
|
|
33
|
+
this.pendingPhoneRequests = {};
|
|
34
|
+
this.maxMsgRetryCount = 5;
|
|
35
|
+
this.statistics = {
|
|
36
|
+
totalRetries: 0,
|
|
37
|
+
successfulRetries: 0,
|
|
38
|
+
failedRetries: 0,
|
|
39
|
+
mediaRetries: 0,
|
|
40
|
+
sessionRecreations: 0,
|
|
41
|
+
phoneRequests: 0,
|
|
42
|
+
};
|
|
43
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
44
|
+
}
|
|
45
|
+
addRecentMessage(to, id, message) {
|
|
46
|
+
const key = { to: to, id: id };
|
|
47
|
+
const keyStr = this.keyToString(key);
|
|
48
|
+
this.recentMessagesMap.set(keyStr, {
|
|
49
|
+
message: message,
|
|
50
|
+
timestamp: Date.now(),
|
|
51
|
+
});
|
|
52
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
53
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
54
|
+
}
|
|
55
|
+
getRecentMessage(to, id) {
|
|
56
|
+
const key = { to: to, id: id };
|
|
57
|
+
const keyStr = this.keyToString(key);
|
|
58
|
+
return this.recentMessagesMap.get(keyStr);
|
|
59
|
+
}
|
|
60
|
+
shouldRecreateSession(jid, retryCount, hasSession) {
|
|
61
|
+
if (!hasSession) {
|
|
62
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
63
|
+
this.statistics.sessionRecreations++;
|
|
64
|
+
return {
|
|
65
|
+
reason: "we don't have a Signal session with them",
|
|
66
|
+
recreate: true,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (retryCount < 2) {
|
|
70
|
+
return { reason: "", recreate: false };
|
|
71
|
+
}
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
74
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
75
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
76
|
+
this.statistics.sessionRecreations++;
|
|
77
|
+
return {
|
|
78
|
+
reason: "retry count > 1 and over an hour since last recreation",
|
|
79
|
+
recreate: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return { reason: "", recreate: false };
|
|
83
|
+
}
|
|
84
|
+
incrementRetryCount(messageId) {
|
|
85
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
86
|
+
this.statistics.totalRetries++;
|
|
87
|
+
return this.retryCounters.get(messageId);
|
|
88
|
+
}
|
|
89
|
+
getRetryCount(messageId) {
|
|
90
|
+
return this.retryCounters.get(messageId) || 0;
|
|
91
|
+
}
|
|
92
|
+
hasExceededMaxRetries(messageId) {
|
|
93
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
94
|
+
}
|
|
95
|
+
markRetrySuccess(messageId) {
|
|
96
|
+
this.statistics.successfulRetries++;
|
|
97
|
+
this.retryCounters.delete(messageId);
|
|
98
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
99
|
+
this.removeRecentMessage(messageId);
|
|
100
|
+
}
|
|
101
|
+
markRetryFailed(messageId) {
|
|
102
|
+
this.statistics.failedRetries++;
|
|
103
|
+
this.retryCounters.delete(messageId);
|
|
104
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
105
|
+
this.removeRecentMessage(messageId);
|
|
106
|
+
}
|
|
107
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
108
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
109
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
110
|
+
delete this.pendingPhoneRequests[messageId];
|
|
111
|
+
this.statistics.phoneRequests++;
|
|
112
|
+
callback();
|
|
113
|
+
}, delay);
|
|
114
|
+
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
|
|
115
|
+
}
|
|
116
|
+
cancelPendingPhoneRequest(messageId) {
|
|
117
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
118
|
+
if (timeout) {
|
|
119
|
+
clearTimeout(timeout);
|
|
120
|
+
delete this.pendingPhoneRequests[messageId];
|
|
121
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
keyToString(key) {
|
|
125
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
126
|
+
}
|
|
127
|
+
removeRecentMessage(messageId) {
|
|
128
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
129
|
+
if (!keyStr) return;
|
|
130
|
+
this.recentMessagesMap.delete(keyStr);
|
|
131
|
+
this.messageKeyIndex.delete(messageId);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
module.exports = { MessageRetryManager: MessageRetryManager };
|