@alannxd/baileys 6.0.4 → 6.0.5
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/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +5479 -0
- package/WAProto/fix-imports.js +85 -0
- package/WAProto/index.d.ts +14017 -0
- package/WAProto/index.js +201 -160
- package/engine-requirements.js +1 -1
- package/lib/Defaults/index.d.ts +37 -15
- package/lib/Defaults/index.js +119 -136
- package/lib/Signal/Group/ciphertext-message.d.ts +1 -0
- package/lib/Signal/Group/ciphertext-message.js +2 -5
- package/lib/Signal/Group/group-session-builder.d.ts +4 -3
- package/lib/Signal/Group/group-session-builder.js +7 -41
- package/lib/Signal/Group/group_cipher.d.ts +4 -4
- package/lib/Signal/Group/group_cipher.js +37 -51
- package/lib/Signal/Group/index.d.ts +12 -11
- package/lib/Signal/Group/index.js +12 -57
- package/lib/Signal/Group/keyhelper.d.ts +2 -1
- package/lib/Signal/Group/keyhelper.js +7 -44
- package/lib/Signal/Group/sender-chain-key.d.ts +3 -2
- package/lib/Signal/Group/sender-chain-key.js +7 -15
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +2 -1
- package/lib/Signal/Group/sender-key-distribution-message.js +8 -11
- package/lib/Signal/Group/sender-key-message.d.ts +2 -1
- package/lib/Signal/Group/sender-key-message.js +9 -12
- package/lib/Signal/Group/sender-key-name.d.ts +1 -0
- package/lib/Signal/Group/sender-key-name.js +2 -5
- package/lib/Signal/Group/sender-key-record.d.ts +3 -2
- package/lib/Signal/Group/sender-key-record.js +9 -21
- package/lib/Signal/Group/sender-key-state.d.ts +7 -6
- package/lib/Signal/Group/sender-key-state.js +27 -42
- package/lib/Signal/Group/sender-message-key.d.ts +1 -0
- package/lib/Signal/Group/sender-message-key.js +4 -7
- package/lib/Signal/libsignal.d.ts +5 -3
- package/lib/Signal/libsignal.js +347 -90
- package/lib/Signal/lid-mapping.d.ts +23 -0
- package/lib/Signal/lid-mapping.js +277 -0
- package/lib/Socket/Client/index.d.ts +3 -3
- package/lib/Socket/Client/index.js +3 -19
- package/lib/Socket/Client/{abstract-socket-client.d.ts → types.d.ts} +4 -5
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/{web-socket-client.d.ts → websocket.d.ts} +3 -2
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/business.d.ts +154 -108
- package/lib/Socket/business.js +162 -43
- package/lib/Socket/chats.d.ts +96 -239
- package/lib/Socket/chats.js +627 -427
- package/lib/Socket/communities.d.ts +239 -146
- package/lib/Socket/communities.js +90 -80
- package/lib/Socket/groups.d.ts +104 -57
- package/lib/Socket/groups.js +154 -161
- package/lib/Socket/index.d.ts +202 -115
- package/lib/Socket/index.js +11 -10
- package/lib/Socket/luxu.d.ts +22 -266
- package/lib/Socket/luxu.js +422 -465
- package/lib/Socket/messages-recv.d.ts +136 -84
- package/lib/Socket/messages-recv.js +1421 -615
- package/lib/Socket/messages-send.d.ts +142 -126
- package/lib/Socket/messages-send.js +878 -671
- package/lib/Socket/mex.d.ts +3 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.d.ts +121 -85
- package/lib/Socket/newsletter.js +147 -272
- package/lib/Socket/socket.d.ts +34 -19
- package/lib/Socket/socket.js +544 -313
- package/lib/Store/index.d.ts +10 -3
- package/lib/Store/index.js +10 -10
- package/lib/Store/keyed-db.d.ts +22 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.d.ts +17 -11
- package/lib/Store/make-cache-manager-store.js +43 -41
- package/lib/Store/make-in-memory-store.d.ts +39 -118
- package/lib/Store/make-in-memory-store.js +112 -341
- package/lib/Store/make-ordered-dictionary.d.ts +11 -10
- package/lib/Store/make-ordered-dictionary.js +14 -20
- package/lib/Store/object-repository.d.ts +10 -9
- package/lib/Store/object-repository.js +11 -6
- package/lib/Types/Auth.d.ts +19 -12
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.d.ts +3 -1
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.d.ts +35 -13
- package/lib/Types/Chat.js +8 -4
- package/lib/Types/Contact.d.ts +8 -1
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.d.ts +116 -17
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.d.ts +21 -5
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.d.ts +12 -0
- package/lib/Types/Label.js +3 -5
- package/lib/Types/LabelAssociation.d.ts +1 -0
- package/lib/Types/LabelAssociation.js +3 -5
- package/lib/Types/Message.d.ts +105 -58
- package/lib/Types/Message.js +11 -9
- package/lib/Types/Mex.d.ts +141 -0
- package/lib/Types/Mex.js +37 -0
- package/lib/Types/Product.d.ts +2 -1
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.d.ts +32 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.d.ts +50 -25
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/State.d.ts +72 -2
- package/lib/Types/State.js +56 -2
- package/lib/Types/USync.d.ts +3 -2
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.d.ts +22 -14
- package/lib/Types/index.js +15 -31
- package/lib/Utils/auth-utils.d.ts +12 -6
- package/lib/Utils/auth-utils.js +239 -143
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.d.ts +3 -2
- package/lib/Utils/business.js +66 -69
- package/lib/Utils/chat-utils.d.ts +52 -23
- package/lib/Utils/chat-utils.js +396 -253
- package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/crypto.d.ts +18 -22
- package/lib/Utils/crypto.js +57 -90
- package/lib/Utils/decode-wa-message.d.ts +55 -8
- package/lib/Utils/decode-wa-message.js +203 -84
- package/lib/Utils/event-buffer.d.ts +9 -8
- package/lib/Utils/event-buffer.js +185 -77
- package/lib/Utils/generics.d.ts +28 -29
- package/lib/Utils/generics.js +180 -210
- package/lib/Utils/history.d.ts +18 -9
- package/lib/Utils/history.js +93 -55
- package/lib/Utils/identity-change-handler.d.ts +44 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/index.d.ts +22 -17
- package/lib/Utils/index.js +22 -33
- package/lib/Utils/link-preview.d.ts +5 -5
- package/lib/Utils/link-preview.js +16 -24
- package/lib/Utils/logger.d.ts +11 -3
- package/lib/Utils/logger.js +3 -7
- package/lib/Utils/lt-hash.d.ts +8 -12
- package/lib/Utils/lt-hash.js +3 -46
- package/lib/Utils/make-mutex.d.ts +4 -2
- package/lib/Utils/make-mutex.js +24 -34
- package/lib/Utils/message-retry-manager.d.ts +115 -0
- package/lib/Utils/message-retry-manager.js +265 -0
- package/lib/Utils/messages-media.d.ts +61 -44
- package/lib/Utils/messages-media.js +451 -482
- package/lib/Utils/messages.d.ts +32 -18
- package/lib/Utils/messages.js +458 -369
- package/lib/Utils/noise-handler.d.ts +13 -14
- package/lib/Utils/noise-handler.js +145 -99
- package/lib/Utils/offline-node-processor.d.ts +17 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/pre-key-manager.d.ts +28 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/process-message.d.ts +31 -12
- package/lib/Utils/process-message.js +459 -150
- package/lib/Utils/reporting-utils.d.ts +11 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/signal.d.ts +20 -5
- package/lib/Utils/signal.js +120 -72
- package/lib/Utils/stanza-ack.d.ts +11 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/sync-action-utils.d.ts +19 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/tc-token-utils.d.ts +37 -0
- package/lib/Utils/tc-token-utils.js +163 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +2 -2
- package/lib/Utils/use-multi-file-auth-state.js +29 -27
- package/lib/Utils/validate-connection.d.ts +7 -7
- package/lib/Utils/validate-connection.js +73 -99
- package/lib/WABinary/constants.d.ts +25 -27
- package/lib/WABinary/constants.js +1281 -20
- package/lib/WABinary/decode.d.ts +5 -5
- package/lib/WABinary/decode.js +52 -42
- package/lib/WABinary/encode.d.ts +3 -3
- package/lib/WABinary/encode.js +110 -155
- package/lib/WABinary/generic-utils.d.ts +8 -7
- package/lib/WABinary/generic-utils.js +48 -49
- package/lib/WABinary/index.d.ts +6 -5
- package/lib/WABinary/index.js +6 -21
- package/lib/WABinary/jid-utils.d.ts +25 -8
- package/lib/WABinary/jid-utils.js +74 -40
- package/lib/WABinary/types.d.ts +2 -1
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.d.ts +3 -11
- package/lib/WAM/BinaryInfo.js +2 -5
- package/lib/WAM/constants.d.ts +5 -3
- package/lib/WAM/constants.js +19071 -11568
- package/lib/WAM/encode.d.ts +3 -3
- package/lib/WAM/encode.js +17 -22
- package/lib/WAM/index.d.ts +4 -3
- package/lib/WAM/index.js +4 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +4 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +33 -13
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +3 -2
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +3 -2
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +3 -2
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +4 -3
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +5 -3
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -8
- package/lib/WAUSync/Protocols/index.d.ts +6 -4
- package/lib/WAUSync/Protocols/index.js +6 -20
- package/lib/WAUSync/USyncQuery.d.ts +6 -4
- package/lib/WAUSync/USyncQuery.js +44 -35
- package/lib/WAUSync/USyncUser.d.ts +10 -5
- package/lib/WAUSync/USyncUser.js +10 -5
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.js +4 -19
- package/lib/index.d.ts +10 -9
- package/lib/index.js +12 -34
- package/package.json +84 -51
- package/WAProto/fix-import.js +0 -29
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/registration.d.ts +0 -267
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/usync.d.ts +0 -36
- package/lib/Socket/usync.js +0 -70
- package/lib/Types/Newsletter.d.ts +0 -103
- package/lib/Types/Newsletter.js +0 -38
- package/lib/Utils/baileys-event-stream.d.ts +0 -16
- package/lib/Utils/baileys-event-stream.js +0 -63
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
exports.makeEventBuffer = void 0;
|
|
7
|
-
const events_1 = __importDefault(require("events"));
|
|
8
|
-
const Types_1 = require("../Types");
|
|
9
|
-
const generics_1 = require("./generics");
|
|
10
|
-
const messages_1 = require("./messages");
|
|
11
|
-
const process_message_1 = require("./process-message");
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { WAMessageStatus } from '../Types/index.js';
|
|
3
|
+
import { trimUndefined } from './generics.js';
|
|
4
|
+
import { updateMessageWithReaction, updateMessageWithReceipt } from './messages.js';
|
|
5
|
+
import { isRealMessage, shouldIncrementChatUnread } from './process-message.js';
|
|
12
6
|
const BUFFERABLE_EVENT = [
|
|
13
7
|
'messaging-history.set',
|
|
14
8
|
'chats.upsert',
|
|
@@ -21,19 +15,23 @@ const BUFFERABLE_EVENT = [
|
|
|
21
15
|
'messages.delete',
|
|
22
16
|
'messages.reaction',
|
|
23
17
|
'message-receipt.update',
|
|
24
|
-
'groups.update'
|
|
18
|
+
'groups.update'
|
|
25
19
|
];
|
|
26
20
|
const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
|
|
27
21
|
/**
|
|
28
22
|
* The event buffer logically consolidates different events into a single event
|
|
29
23
|
* making the data processing more efficient.
|
|
30
|
-
* @param ev the baileys event emitter
|
|
31
24
|
*/
|
|
32
|
-
const makeEventBuffer = (logger) => {
|
|
33
|
-
const ev = new
|
|
25
|
+
export const makeEventBuffer = (logger) => {
|
|
26
|
+
const ev = new EventEmitter();
|
|
34
27
|
const historyCache = new Set();
|
|
35
28
|
let data = makeBufferData();
|
|
36
|
-
let
|
|
29
|
+
let isBuffering = false;
|
|
30
|
+
let bufferTimeout = null;
|
|
31
|
+
let flushPendingTimeout = null; // Add a specific timer for the debounced flush to prevent leak
|
|
32
|
+
let bufferCount = 0;
|
|
33
|
+
const MAX_HISTORY_CACHE_SIZE = 10000; // Limit the history cache size to prevent memory bloat
|
|
34
|
+
const BUFFER_TIMEOUT_MS = 30000; // 30 seconds
|
|
37
35
|
// take the generic event and fire it as a baileys event
|
|
38
36
|
ev.on('event', (map) => {
|
|
39
37
|
for (const event in map) {
|
|
@@ -41,25 +39,46 @@ const makeEventBuffer = (logger) => {
|
|
|
41
39
|
}
|
|
42
40
|
});
|
|
43
41
|
function buffer() {
|
|
44
|
-
|
|
42
|
+
if (!isBuffering) {
|
|
43
|
+
logger.debug('Event buffer activated');
|
|
44
|
+
isBuffering = true;
|
|
45
|
+
bufferCount = 0;
|
|
46
|
+
if (bufferTimeout) {
|
|
47
|
+
clearTimeout(bufferTimeout);
|
|
48
|
+
}
|
|
49
|
+
bufferTimeout = setTimeout(() => {
|
|
50
|
+
if (isBuffering) {
|
|
51
|
+
logger.warn('Buffer timeout reached, auto-flushing');
|
|
52
|
+
flush();
|
|
53
|
+
}
|
|
54
|
+
}, BUFFER_TIMEOUT_MS);
|
|
55
|
+
}
|
|
56
|
+
// Always increment count when requested
|
|
57
|
+
bufferCount++;
|
|
45
58
|
}
|
|
46
|
-
function flush(
|
|
47
|
-
|
|
48
|
-
if (!buffersInProgress) {
|
|
59
|
+
function flush() {
|
|
60
|
+
if (!isBuffering) {
|
|
49
61
|
return false;
|
|
50
62
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
logger.debug({ bufferCount }, 'Flushing event buffer');
|
|
64
|
+
isBuffering = false;
|
|
65
|
+
bufferCount = 0;
|
|
66
|
+
// Clear timeout
|
|
67
|
+
if (bufferTimeout) {
|
|
68
|
+
clearTimeout(bufferTimeout);
|
|
69
|
+
bufferTimeout = null;
|
|
70
|
+
}
|
|
71
|
+
if (flushPendingTimeout) {
|
|
72
|
+
clearTimeout(flushPendingTimeout);
|
|
73
|
+
flushPendingTimeout = null;
|
|
74
|
+
}
|
|
75
|
+
// Clear history cache if it exceeds the max size
|
|
76
|
+
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
77
|
+
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache');
|
|
78
|
+
historyCache.clear();
|
|
59
79
|
}
|
|
60
80
|
const newData = makeBufferData();
|
|
61
81
|
const chatUpdates = Object.values(data.chatUpdates);
|
|
62
|
-
// gather the remaining conditional events so we re-queue them
|
|
63
82
|
let conditionalChatUpdatesLeft = 0;
|
|
64
83
|
for (const update of chatUpdates) {
|
|
65
84
|
if (update.conditional) {
|
|
@@ -78,8 +97,8 @@ const makeEventBuffer = (logger) => {
|
|
|
78
97
|
}
|
|
79
98
|
return {
|
|
80
99
|
process(handler) {
|
|
81
|
-
const listener = (map) => {
|
|
82
|
-
handler(map);
|
|
100
|
+
const listener = async (map) => {
|
|
101
|
+
await handler(map);
|
|
83
102
|
};
|
|
84
103
|
ev.on('event', listener);
|
|
85
104
|
return () => {
|
|
@@ -87,14 +106,35 @@ const makeEventBuffer = (logger) => {
|
|
|
87
106
|
};
|
|
88
107
|
},
|
|
89
108
|
emit(event, evData) {
|
|
90
|
-
if
|
|
109
|
+
// Check if this is a messages.upsert with a different type than what's buffered
|
|
110
|
+
// If so, flush the buffered messages first to avoid type overshadowing
|
|
111
|
+
if (event === 'messages.upsert') {
|
|
112
|
+
const { type } = evData;
|
|
113
|
+
const existingUpserts = Object.values(data.messageUpserts);
|
|
114
|
+
if (existingUpserts.length > 0) {
|
|
115
|
+
const bufferedType = existingUpserts[0].type;
|
|
116
|
+
if (bufferedType !== type) {
|
|
117
|
+
logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages');
|
|
118
|
+
// Emit the buffered messages with their correct type
|
|
119
|
+
ev.emit('event', {
|
|
120
|
+
'messages.upsert': {
|
|
121
|
+
messages: existingUpserts.map(m => m.message),
|
|
122
|
+
type: bufferedType
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
// Clear the message upserts from the buffer
|
|
126
|
+
data.messageUpserts = {};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
91
131
|
append(data, historyCache, event, evData, logger);
|
|
92
132
|
return true;
|
|
93
133
|
}
|
|
94
134
|
return ev.emit('event', { [event]: evData });
|
|
95
135
|
},
|
|
96
136
|
isBuffering() {
|
|
97
|
-
return
|
|
137
|
+
return isBuffering;
|
|
98
138
|
},
|
|
99
139
|
buffer,
|
|
100
140
|
flush,
|
|
@@ -103,19 +143,55 @@ const makeEventBuffer = (logger) => {
|
|
|
103
143
|
buffer();
|
|
104
144
|
try {
|
|
105
145
|
const result = await work(...args);
|
|
146
|
+
// If this is the only buffer, flush after a small delay
|
|
147
|
+
if (bufferCount === 1) {
|
|
148
|
+
setTimeout(() => {
|
|
149
|
+
if (isBuffering && bufferCount === 1) {
|
|
150
|
+
flush();
|
|
151
|
+
}
|
|
152
|
+
}, 100); // Small delay to allow nested buffers
|
|
153
|
+
}
|
|
106
154
|
return result;
|
|
107
155
|
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
108
159
|
finally {
|
|
109
|
-
|
|
160
|
+
bufferCount = Math.max(0, bufferCount - 1);
|
|
161
|
+
if (bufferCount === 0) {
|
|
162
|
+
// Only schedule ONE timeout, not 10,000
|
|
163
|
+
if (!flushPendingTimeout) {
|
|
164
|
+
flushPendingTimeout = setTimeout(flush, 100);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
110
167
|
}
|
|
111
168
|
};
|
|
112
169
|
},
|
|
113
170
|
on: (...args) => ev.on(...args),
|
|
114
171
|
off: (...args) => ev.off(...args),
|
|
115
172
|
removeAllListeners: (...args) => ev.removeAllListeners(...args),
|
|
173
|
+
destroy() {
|
|
174
|
+
// Clear buffer timeout
|
|
175
|
+
if (bufferTimeout) {
|
|
176
|
+
clearTimeout(bufferTimeout);
|
|
177
|
+
bufferTimeout = null;
|
|
178
|
+
}
|
|
179
|
+
if (flushPendingTimeout) {
|
|
180
|
+
clearTimeout(flushPendingTimeout);
|
|
181
|
+
flushPendingTimeout = null;
|
|
182
|
+
}
|
|
183
|
+
// Clear history cache
|
|
184
|
+
historyCache.clear();
|
|
185
|
+
// Reset buffer data
|
|
186
|
+
data = makeBufferData();
|
|
187
|
+
isBuffering = false;
|
|
188
|
+
bufferCount = 0;
|
|
189
|
+
// Remove all listeners
|
|
190
|
+
ev.removeAllListeners();
|
|
191
|
+
logger.debug('Event buffer destroyed');
|
|
192
|
+
}
|
|
116
193
|
};
|
|
117
194
|
};
|
|
118
|
-
exports.makeEventBuffer = makeEventBuffer;
|
|
119
195
|
const makeBufferData = () => {
|
|
120
196
|
return {
|
|
121
197
|
historySets: {
|
|
@@ -138,25 +214,27 @@ const makeBufferData = () => {
|
|
|
138
214
|
groupUpdates: {}
|
|
139
215
|
};
|
|
140
216
|
};
|
|
141
|
-
function append(data, historyCache, event,
|
|
142
|
-
|
|
217
|
+
function append(data, historyCache, event,
|
|
218
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
219
|
+
eventData, logger) {
|
|
143
220
|
switch (event) {
|
|
144
221
|
case 'messaging-history.set':
|
|
145
222
|
for (const chat of eventData.chats) {
|
|
146
|
-
const
|
|
223
|
+
const id = chat.id || '';
|
|
224
|
+
const existingChat = data.historySets.chats[id];
|
|
147
225
|
if (existingChat) {
|
|
148
226
|
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
|
|
149
227
|
}
|
|
150
|
-
if (!existingChat && !historyCache.has(
|
|
151
|
-
data.historySets.chats[
|
|
152
|
-
historyCache.add(
|
|
228
|
+
if (!existingChat && !historyCache.has(id)) {
|
|
229
|
+
data.historySets.chats[id] = chat;
|
|
230
|
+
historyCache.add(id);
|
|
153
231
|
absorbingChatUpdate(chat);
|
|
154
232
|
}
|
|
155
233
|
}
|
|
156
234
|
for (const contact of eventData.contacts) {
|
|
157
235
|
const existingContact = data.historySets.contacts[contact.id];
|
|
158
236
|
if (existingContact) {
|
|
159
|
-
Object.assign(existingContact,
|
|
237
|
+
Object.assign(existingContact, trimUndefined(contact));
|
|
160
238
|
}
|
|
161
239
|
else {
|
|
162
240
|
const historyContactId = `c:${contact.id}`;
|
|
@@ -176,15 +254,45 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
176
254
|
}
|
|
177
255
|
}
|
|
178
256
|
data.historySets.empty = false;
|
|
257
|
+
data.historySets.syncType = eventData.syncType;
|
|
258
|
+
if (eventData.pastParticipants?.length) {
|
|
259
|
+
const merged = new Map();
|
|
260
|
+
const sigOf = (p) => `${p.userJid || ''}:${p.leaveTs || ''}:${p.leaveReason || ''}`;
|
|
261
|
+
const ingest = (entry) => {
|
|
262
|
+
const key = entry.groupJid ?? JSON.stringify(entry);
|
|
263
|
+
const existing = merged.get(key);
|
|
264
|
+
if (!existing) {
|
|
265
|
+
merged.set(key, { ...entry, pastParticipants: [...(entry.pastParticipants || [])] });
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const seen = new Set((existing.pastParticipants || []).map(sigOf));
|
|
269
|
+
for (const p of entry.pastParticipants || []) {
|
|
270
|
+
const sig = sigOf(p);
|
|
271
|
+
if (!seen.has(sig)) {
|
|
272
|
+
existing.pastParticipants.push(p);
|
|
273
|
+
seen.add(sig);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
for (const entry of data.historySets.pastParticipants || [])
|
|
278
|
+
ingest(entry);
|
|
279
|
+
for (const entry of eventData.pastParticipants)
|
|
280
|
+
ingest(entry);
|
|
281
|
+
data.historySets.pastParticipants = [...merged.values()];
|
|
282
|
+
}
|
|
283
|
+
data.historySets.progress = eventData.progress;
|
|
284
|
+
data.historySets.chunkOrder = eventData.chunkOrder;
|
|
285
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
|
|
179
286
|
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
|
|
180
287
|
break;
|
|
181
288
|
case 'chats.upsert':
|
|
182
289
|
for (const chat of eventData) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
290
|
+
const id = chat.id || '';
|
|
291
|
+
let upsert = data.chatUpserts[id];
|
|
292
|
+
if (id && !upsert) {
|
|
293
|
+
upsert = data.historySets.chats[id];
|
|
186
294
|
if (upsert) {
|
|
187
|
-
logger.debug({ chatId:
|
|
295
|
+
logger.debug({ chatId: id }, 'absorbed chat upsert in chat set');
|
|
188
296
|
}
|
|
189
297
|
}
|
|
190
298
|
if (upsert) {
|
|
@@ -192,11 +300,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
192
300
|
}
|
|
193
301
|
else {
|
|
194
302
|
upsert = chat;
|
|
195
|
-
data.chatUpserts[
|
|
303
|
+
data.chatUpserts[id] = upsert;
|
|
196
304
|
}
|
|
197
305
|
absorbingChatUpdate(upsert);
|
|
198
|
-
if (data.chatDeletes.has(
|
|
199
|
-
data.chatDeletes.delete(
|
|
306
|
+
if (data.chatDeletes.has(id)) {
|
|
307
|
+
data.chatDeletes.delete(id);
|
|
200
308
|
}
|
|
201
309
|
}
|
|
202
310
|
break;
|
|
@@ -256,14 +364,14 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
256
364
|
}
|
|
257
365
|
}
|
|
258
366
|
if (upsert) {
|
|
259
|
-
upsert = Object.assign(upsert,
|
|
367
|
+
upsert = Object.assign(upsert, trimUndefined(contact));
|
|
260
368
|
}
|
|
261
369
|
else {
|
|
262
370
|
upsert = contact;
|
|
263
371
|
data.contactUpserts[contact.id] = upsert;
|
|
264
372
|
}
|
|
265
373
|
if (data.contactUpdates[contact.id]) {
|
|
266
|
-
upsert = Object.assign(data.contactUpdates[contact.id],
|
|
374
|
+
upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
|
|
267
375
|
delete data.contactUpdates[contact.id];
|
|
268
376
|
}
|
|
269
377
|
}
|
|
@@ -288,7 +396,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
288
396
|
const { messages, type } = eventData;
|
|
289
397
|
for (const message of messages) {
|
|
290
398
|
const key = stringifyMessageKey(message.key);
|
|
291
|
-
let existing =
|
|
399
|
+
let existing = data.messageUpserts[key]?.message;
|
|
292
400
|
if (!existing) {
|
|
293
401
|
existing = data.historySets.messages[key];
|
|
294
402
|
if (existing) {
|
|
@@ -309,9 +417,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
309
417
|
else {
|
|
310
418
|
data.messageUpserts[key] = {
|
|
311
419
|
message,
|
|
312
|
-
type: type === 'notify' ||
|
|
313
|
-
? 'notify'
|
|
314
|
-
: type
|
|
420
|
+
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
|
|
315
421
|
};
|
|
316
422
|
}
|
|
317
423
|
}
|
|
@@ -320,13 +426,13 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
320
426
|
const msgUpdates = eventData;
|
|
321
427
|
for (const { key, update } of msgUpdates) {
|
|
322
428
|
const keyStr = stringifyMessageKey(key);
|
|
323
|
-
const existing = data.historySets.messages[keyStr] ||
|
|
429
|
+
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
|
|
324
430
|
if (existing) {
|
|
325
431
|
Object.assign(existing, update);
|
|
326
432
|
// if the message was received & read by us
|
|
327
433
|
// the chat counter must have been incremented
|
|
328
434
|
// so we need to decrement it
|
|
329
|
-
if (update.status ===
|
|
435
|
+
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
330
436
|
decrementChatReadCounterIfMsgDidUnread(existing);
|
|
331
437
|
}
|
|
332
438
|
}
|
|
@@ -364,12 +470,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
364
470
|
const keyStr = stringifyMessageKey(key);
|
|
365
471
|
const existing = data.messageUpserts[keyStr];
|
|
366
472
|
if (existing) {
|
|
367
|
-
|
|
473
|
+
updateMessageWithReaction(existing.message, reaction);
|
|
368
474
|
}
|
|
369
475
|
else {
|
|
370
|
-
data.messageReactions[keyStr] = data.messageReactions[keyStr]
|
|
371
|
-
|
|
372
|
-
(0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
|
|
476
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
|
|
477
|
+
updateMessageWithReaction(data.messageReactions[keyStr], reaction);
|
|
373
478
|
}
|
|
374
479
|
}
|
|
375
480
|
break;
|
|
@@ -379,12 +484,11 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
379
484
|
const keyStr = stringifyMessageKey(key);
|
|
380
485
|
const existing = data.messageUpserts[keyStr];
|
|
381
486
|
if (existing) {
|
|
382
|
-
|
|
487
|
+
updateMessageWithReceipt(existing.message, receipt);
|
|
383
488
|
}
|
|
384
489
|
else {
|
|
385
|
-
data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
|
|
386
|
-
|
|
387
|
-
(0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
|
|
490
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
|
|
491
|
+
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
|
|
388
492
|
}
|
|
389
493
|
}
|
|
390
494
|
break;
|
|
@@ -402,7 +506,7 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
402
506
|
throw new Error(`"${event}" cannot be buffered`);
|
|
403
507
|
}
|
|
404
508
|
function absorbingChatUpdate(existing) {
|
|
405
|
-
const chatId = existing.id;
|
|
509
|
+
const chatId = existing.id || '';
|
|
406
510
|
const update = data.chatUpdates[chatId];
|
|
407
511
|
if (update) {
|
|
408
512
|
const conditionMatches = update.conditional ? update.conditional(data) : true;
|
|
@@ -423,10 +527,10 @@ function append(data, historyCache, event, eventData, logger) {
|
|
|
423
527
|
// if the message has already been marked read by us
|
|
424
528
|
const chatId = message.key.remoteJid;
|
|
425
529
|
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
|
|
426
|
-
if (
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
530
|
+
if (isRealMessage(message) &&
|
|
531
|
+
shouldIncrementChatUnread(message) &&
|
|
532
|
+
typeof chat?.unreadCount === 'number' &&
|
|
533
|
+
chat.unreadCount > 0) {
|
|
430
534
|
logger.debug({ chatId: chat.id }, 'decrementing chat counter');
|
|
431
535
|
chat.unreadCount -= 1;
|
|
432
536
|
if (chat.unreadCount === 0) {
|
|
@@ -442,7 +546,12 @@ function consolidateEvents(data) {
|
|
|
442
546
|
chats: Object.values(data.historySets.chats),
|
|
443
547
|
messages: Object.values(data.historySets.messages),
|
|
444
548
|
contacts: Object.values(data.historySets.contacts),
|
|
445
|
-
|
|
549
|
+
pastParticipants: data.historySets.pastParticipants,
|
|
550
|
+
syncType: data.historySets.syncType,
|
|
551
|
+
progress: data.historySets.progress,
|
|
552
|
+
isLatest: data.historySets.isLatest,
|
|
553
|
+
chunkOrder: data.historySets.chunkOrder,
|
|
554
|
+
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
|
446
555
|
};
|
|
447
556
|
}
|
|
448
557
|
const chatUpsertList = Object.values(data.chatUpserts);
|
|
@@ -496,12 +605,10 @@ function consolidateEvents(data) {
|
|
|
496
605
|
return map;
|
|
497
606
|
}
|
|
498
607
|
function concatChats(a, b) {
|
|
499
|
-
if (b.unreadCount === null
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
b.unreadCount = undefined;
|
|
504
|
-
}
|
|
608
|
+
if (b.unreadCount === null && // neutralize unread counter
|
|
609
|
+
a.unreadCount < 0) {
|
|
610
|
+
a.unreadCount = undefined;
|
|
611
|
+
b.unreadCount = undefined;
|
|
505
612
|
}
|
|
506
613
|
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
507
614
|
b = { ...b };
|
|
@@ -512,3 +619,4 @@ function concatChats(a, b) {
|
|
|
512
619
|
return Object.assign(a, b);
|
|
513
620
|
}
|
|
514
621
|
const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
|
|
622
|
+
//# sourceMappingURL=event-buffer.js.map
|
package/lib/Utils/generics.d.ts
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export declare const Browsers: {
|
|
8
|
-
ubuntu: (browser: any) => [string, string, string];
|
|
9
|
-
macOS: (browser: any) => [string, string, string];
|
|
10
|
-
baileys: (browser: any) => [string, string, string];
|
|
11
|
-
windows: (browser: any) => [string, string, string];
|
|
12
|
-
/** The appropriate browser based on your OS & release */
|
|
13
|
-
appropriate: (browser: any) => [string, string, string];
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import type { BaileysEventEmitter, BaileysEventMap, ConnectionState, WACallUpdateType, WAMessageKey, WAVersion } from '../Types/index.js';
|
|
3
|
+
import { type BinaryNode } from '../WABinary/index.js';
|
|
4
|
+
export declare const BufferJSON: {
|
|
5
|
+
replacer: (k: any, value: any) => any;
|
|
6
|
+
reviver: (_: any, value: any) => any;
|
|
14
7
|
};
|
|
15
|
-
export declare const
|
|
16
|
-
export declare const
|
|
17
|
-
export declare const writeRandomPadMax16: (msg: Uint8Array) => Buffer
|
|
18
|
-
export declare const unpadRandomMax16: (e: Uint8Array | Buffer) => Uint8Array
|
|
19
|
-
export declare const
|
|
8
|
+
export declare const getKeyAuthor: (key: WAMessageKey | undefined | null, meId?: string) => string;
|
|
9
|
+
export declare const isStringNullOrEmpty: (value: string | null | undefined) => value is null | undefined | "";
|
|
10
|
+
export declare const writeRandomPadMax16: (msg: Uint8Array) => Buffer<ArrayBuffer>;
|
|
11
|
+
export declare const unpadRandomMax16: (e: Uint8Array | Buffer) => Uint8Array<ArrayBuffer>;
|
|
12
|
+
export declare const generateParticipantHashV2: (participants: string[]) => string;
|
|
13
|
+
export declare const encodeWAMessage: (message: proto.IMessage) => Buffer<ArrayBuffer>;
|
|
20
14
|
export declare const generateRegistrationId: () => number;
|
|
21
|
-
export declare const encodeBigEndian: (e: number, t?: number) => Uint8Array
|
|
15
|
+
export declare const encodeBigEndian: (e: number, t?: number) => Uint8Array<ArrayBuffer>;
|
|
22
16
|
export declare const toNumber: (t: Long | number | null | undefined) => number;
|
|
23
17
|
/** unix timestamp of a date in seconds */
|
|
24
18
|
export declare const unixTimestampSeconds: (date?: Date) => number;
|
|
@@ -37,34 +31,33 @@ export declare const delayCancellable: (ms: number) => {
|
|
|
37
31
|
export declare function promiseTimeout<T>(ms: number | undefined, promise: (resolve: (v: T) => void, reject: (error: any) => void) => void): Promise<T>;
|
|
38
32
|
export declare const generateMessageIDV2: (userId?: string) => string;
|
|
39
33
|
export declare const generateMessageID: () => string;
|
|
40
|
-
export declare function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T): (check: (u: BaileysEventMap[T]) => boolean | undefined
|
|
41
|
-
export declare const bindWaitForConnectionUpdate: (ev: BaileysEventEmitter) => (check: (u: Partial<
|
|
42
|
-
export declare const printQRIfNecessaryListener: (ev: BaileysEventEmitter, logger: Logger) => void;
|
|
34
|
+
export declare function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T): (check: (u: BaileysEventMap[T]) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
|
|
35
|
+
export declare const bindWaitForConnectionUpdate: (ev: BaileysEventEmitter) => (check: (u: Partial<ConnectionState>) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
|
|
43
36
|
/**
|
|
44
37
|
* utility that fetches latest baileys version from the master branch.
|
|
45
38
|
* Use to ensure your WA connection is always on the latest version
|
|
46
39
|
*/
|
|
47
|
-
export declare const fetchLatestBaileysVersion: (options?:
|
|
40
|
+
export declare const fetchLatestBaileysVersion: (options?: RequestInit) => Promise<{
|
|
48
41
|
version: WAVersion;
|
|
49
42
|
isLatest: boolean;
|
|
50
43
|
error?: undefined;
|
|
51
44
|
} | {
|
|
52
45
|
version: WAVersion;
|
|
53
46
|
isLatest: boolean;
|
|
54
|
-
error:
|
|
47
|
+
error: unknown;
|
|
55
48
|
}>;
|
|
56
49
|
/**
|
|
57
50
|
* A utility that fetches the latest web version of whatsapp.
|
|
58
51
|
* Use to ensure your WA connection is always on the latest version
|
|
59
52
|
*/
|
|
60
|
-
export declare const fetchLatestWaWebVersion: (options
|
|
53
|
+
export declare const fetchLatestWaWebVersion: (options?: RequestInit) => Promise<{
|
|
61
54
|
version: WAVersion;
|
|
62
55
|
isLatest: boolean;
|
|
63
56
|
error?: undefined;
|
|
64
57
|
} | {
|
|
65
58
|
version: WAVersion;
|
|
66
59
|
isLatest: boolean;
|
|
67
|
-
error:
|
|
60
|
+
error: unknown;
|
|
68
61
|
}>;
|
|
69
62
|
/** unique message tag prefix for MD clients */
|
|
70
63
|
export declare const generateMdTagPrefix: () => string;
|
|
@@ -72,7 +65,7 @@ export declare const generateMdTagPrefix: () => string;
|
|
|
72
65
|
* Given a type of receipt, returns what the new status of the message should be
|
|
73
66
|
* @param type type from receipt
|
|
74
67
|
*/
|
|
75
|
-
export declare const getStatusFromReceiptType: (type: string | undefined) => proto.WebMessageInfo.Status;
|
|
68
|
+
export declare const getStatusFromReceiptType: (type: string | undefined) => proto.WebMessageInfo.Status | undefined;
|
|
76
69
|
/**
|
|
77
70
|
* Stream errors generally provide a reason, map that to a baileys DisconnectReason
|
|
78
71
|
* @param reason the string reason given, eg. "conflict"
|
|
@@ -87,6 +80,12 @@ export declare const getCodeFromWSError: (error: Error) => number;
|
|
|
87
80
|
* Is the given platform WA business
|
|
88
81
|
* @param platform AuthenticationCreds.platform
|
|
89
82
|
*/
|
|
90
|
-
export declare const isWABusinessPlatform: (platform: string) =>
|
|
91
|
-
export declare function trimUndefined(obj:
|
|
83
|
+
export declare const isWABusinessPlatform: (platform: string) => platform is "smba" | "smbi";
|
|
84
|
+
export declare function trimUndefined(obj: {
|
|
85
|
+
[_: string]: any;
|
|
86
|
+
}): {
|
|
87
|
+
[_: string]: any;
|
|
88
|
+
};
|
|
92
89
|
export declare function bytesToCrockford(buffer: Buffer): string;
|
|
90
|
+
export declare function encodeNewsletterMessage(message: proto.IMessage): Uint8Array;
|
|
91
|
+
//# sourceMappingURL=generics.d.ts.map
|