@vinzsocket/baileys 2.8.10 → 2.8.20
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 +75 -73
- package/lib/Signal/Group/ciphertext-message.js +9 -9
- package/lib/Signal/Group/group-session-builder.js +21 -21
- package/lib/Signal/Group/group_cipher.js +79 -81
- package/lib/Signal/Group/index.js +2 -2
- package/lib/Signal/Group/keyhelper.js +9 -9
- package/lib/Signal/Group/queue-job.js +43 -46
- package/lib/Signal/Group/sender-chain-key.js +24 -25
- package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
- package/lib/Signal/Group/sender-key-message.js +55 -60
- package/lib/Signal/Group/sender-key-name.js +38 -39
- package/lib/Signal/Group/sender-key-record.js +39 -41
- package/lib/Signal/Group/sender-key-state.js +85 -89
- package/lib/Signal/Group/sender-message-key.js +22 -22
- package/lib/Signal/libsignal.js +387 -292
- package/lib/Signal/lid-mapping.js +98 -93
- package/lib/Socket/Client/types.js +6 -6
- package/lib/Socket/Client/websocket.js +44 -44
- package/lib/Socket/business.js +350 -365
- package/lib/Socket/chats.js +1019 -914
- package/lib/Socket/communities.js +454 -376
- package/lib/Socket/groups.js +378 -310
- package/lib/Socket/index.js +11 -11
- package/lib/Socket/messages-recv.js +1355 -1182
- package/lib/Socket/messages-send.js +1224 -1081
- package/lib/Socket/mex.js +40 -34
- package/lib/Socket/newsletter.js +212 -169
- package/lib/Socket/socket.js +896 -757
- package/lib/Store/index.js +1 -1
- package/lib/Store/make-cache-manager-store.js +75 -79
- package/lib/Store/make-in-memory-store.js +450 -389
- package/lib/Store/make-ordered-dictionary.js +73 -76
- package/lib/Store/object-repository.js +24 -22
- package/lib/Types/Chat.js +1 -7
- package/lib/Types/Label.js +20 -20
- package/lib/Types/LabelAssociation.js +2 -2
- package/lib/Types/Message.js +2 -2
- package/lib/Types/Newsletter.js +24 -24
- package/lib/Types/Socket.js +1 -1
- package/lib/Types/State.js +8 -8
- package/lib/Types/index.js +10 -10
- package/lib/Utils/auth-utils.js +408 -401
- package/lib/Utils/baileys-event-stream.js +44 -35
- package/lib/Utils/business.js +200 -197
- package/lib/Utils/chat-utils.js +775 -708
- package/lib/Utils/crypto.js +97 -85
- package/lib/Utils/decode-wa-message.js +240 -227
- package/lib/Utils/event-buffer.js +547 -518
- package/lib/Utils/extended-messages.js +250 -203
- package/lib/Utils/generics.js +300 -289
- package/lib/Utils/history.js +76 -69
- package/lib/Utils/lid-resolver.js +47 -42
- package/lib/Utils/link-preview.js +75 -65
- package/lib/Utils/logger.js +3 -1
- package/lib/Utils/lt-hash.js +38 -34
- package/lib/Utils/make-mutex.js +32 -34
- package/lib/Utils/mention.js +79 -86
- package/lib/Utils/message-retry-manager.js +148 -136
- package/lib/Utils/messages-media.js +652 -562
- package/lib/Utils/messages.js +753 -707
- package/lib/Utils/noise-handler.js +156 -135
- package/lib/Utils/process-message.js +404 -341
- package/lib/Utils/signal.js +180 -118
- package/lib/Utils/use-multi-file-auth-state.js +87 -91
- package/lib/Utils/validate-connection.js +167 -142
- package/lib/WABinary/constants.js +28 -1293
- package/lib/WABinary/decode.js +220 -218
- package/lib/WABinary/encode.js +199 -201
- package/lib/WABinary/generic-utils.js +80 -77
- package/lib/WABinary/jid-utils.js +41 -35
- package/lib/WAM/BinaryInfo.js +7 -7
- package/lib/WAM/constants.js +1 -15241
- package/lib/WAM/encode.js +124 -131
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
- package/lib/WAUSync/USyncQuery.js +82 -84
- package/lib/WAUSync/USyncUser.js +20 -20
- package/package.json +5 -2
package/lib/WABinary/encode.js
CHANGED
|
@@ -1,216 +1,214 @@
|
|
|
1
1
|
import * as constants from './constants.js';
|
|
2
2
|
import { jidDecode } from './jid-utils.js';
|
|
3
3
|
export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
|
5
|
+
return Buffer.from(encoded);
|
|
6
6
|
};
|
|
7
|
-
const encodeBinaryNodeInner = ({
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
7
|
+
const encodeBinaryNodeInner = ({
|
|
8
|
+
tag,
|
|
9
|
+
attrs,
|
|
10
|
+
content
|
|
11
|
+
}, opts, buffer) => {
|
|
12
|
+
const {
|
|
13
|
+
TAGS,
|
|
14
|
+
TOKEN_MAP
|
|
15
|
+
} = opts;
|
|
16
|
+
const pushByte = value => buffer.push(value & 0xff);
|
|
17
|
+
const pushInt = (value, n, littleEndian = false) => {
|
|
18
|
+
for (let i = 0; i < n; i++) {
|
|
19
|
+
const curShift = littleEndian ? i : n - 1 - i;
|
|
20
|
+
buffer.push(value >> curShift * 8 & 0xff);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const pushBytes = bytes => {
|
|
24
|
+
for (const b of bytes) {
|
|
25
|
+
buffer.push(b);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const pushInt16 = value => {
|
|
29
|
+
pushBytes([value >> 8 & 0xff, value & 0xff]);
|
|
30
|
+
};
|
|
31
|
+
const pushInt20 = value => pushBytes([value >> 16 & 0x0f, value >> 8 & 0xff, value & 0xff]);
|
|
32
|
+
const writeByteLength = length => {
|
|
33
|
+
if (length >= 4294967296) {
|
|
34
|
+
throw new Error('string too large to encode: ' + length);
|
|
35
|
+
}
|
|
36
|
+
if (length >= 1 << 20) {
|
|
37
|
+
pushByte(TAGS.BINARY_32);
|
|
38
|
+
pushInt(length, 4); // 32 bit integer
|
|
39
|
+
} else if (length >= 256) {
|
|
40
|
+
pushByte(TAGS.BINARY_20);
|
|
41
|
+
pushInt20(length);
|
|
42
|
+
} else {
|
|
43
|
+
pushByte(TAGS.BINARY_8);
|
|
44
|
+
pushByte(length);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const writeStringRaw = str => {
|
|
48
|
+
const bytes = Buffer.from(str, 'utf-8');
|
|
49
|
+
writeByteLength(bytes.length);
|
|
50
|
+
pushBytes(bytes);
|
|
51
|
+
};
|
|
52
|
+
const writeJid = ({
|
|
53
|
+
domainType,
|
|
54
|
+
device,
|
|
55
|
+
user,
|
|
56
|
+
server
|
|
57
|
+
}) => {
|
|
58
|
+
if (typeof device !== 'undefined') {
|
|
59
|
+
pushByte(TAGS.AD_JID);
|
|
60
|
+
pushByte(domainType || 0);
|
|
61
|
+
pushByte(device || 0);
|
|
62
|
+
writeString(user);
|
|
63
|
+
} else {
|
|
64
|
+
pushByte(TAGS.JID_PAIR);
|
|
65
|
+
if (user.length) {
|
|
66
|
+
writeString(user);
|
|
67
|
+
} else {
|
|
68
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
69
|
+
}
|
|
70
|
+
writeString(server);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const packNibble = char => {
|
|
74
|
+
switch (char) {
|
|
75
|
+
case '-':
|
|
76
|
+
return 10;
|
|
77
|
+
case '.':
|
|
78
|
+
return 11;
|
|
79
|
+
case '\0':
|
|
80
|
+
return 15;
|
|
81
|
+
default:
|
|
81
82
|
if (char >= '0' && char <= '9') {
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
if (char >= 'A' && char <= 'F') {
|
|
85
|
-
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
|
|
86
|
-
}
|
|
87
|
-
if (char >= 'a' && char <= 'f') {
|
|
88
|
-
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
|
|
89
|
-
}
|
|
90
|
-
if (char === '\0') {
|
|
91
|
-
return 15;
|
|
92
|
-
}
|
|
93
|
-
throw new Error(`Invalid hex char "${char}"`);
|
|
94
|
-
};
|
|
95
|
-
const writePackedBytes = (str, type) => {
|
|
96
|
-
if (str.length > TAGS.PACKED_MAX) {
|
|
97
|
-
throw new Error('Too many bytes to pack');
|
|
98
|
-
}
|
|
99
|
-
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
|
100
|
-
let roundedLength = Math.ceil(str.length / 2.0);
|
|
101
|
-
if (str.length % 2 !== 0) {
|
|
102
|
-
roundedLength |= 128;
|
|
103
|
-
}
|
|
104
|
-
pushByte(roundedLength);
|
|
105
|
-
const packFunction = type === 'nibble' ? packNibble : packHex;
|
|
106
|
-
const packBytePair = (v1, v2) => {
|
|
107
|
-
const result = (packFunction(v1) << 4) | packFunction(v2);
|
|
108
|
-
return result;
|
|
109
|
-
};
|
|
110
|
-
const strLengthHalf = Math.floor(str.length / 2);
|
|
111
|
-
for (let i = 0; i < strLengthHalf; i++) {
|
|
112
|
-
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
|
113
|
-
}
|
|
114
|
-
if (str.length % 2 !== 0) {
|
|
115
|
-
pushByte(packBytePair(str[str.length - 1], '\x00'));
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
const isNibble = (str) => {
|
|
119
|
-
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
for (const char of str) {
|
|
123
|
-
const isInNibbleRange = char >= '0' && char <= '9';
|
|
124
|
-
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return true;
|
|
129
|
-
};
|
|
130
|
-
const isHex = (str) => {
|
|
131
|
-
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
for (const char of str) {
|
|
135
|
-
const isInNibbleRange = char >= '0' && char <= '9';
|
|
136
|
-
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return true;
|
|
141
|
-
};
|
|
142
|
-
const writeString = (str) => {
|
|
143
|
-
if (str === undefined || str === null) {
|
|
144
|
-
pushByte(TAGS.LIST_EMPTY);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const tokenIndex = TOKEN_MAP[str];
|
|
148
|
-
if (tokenIndex) {
|
|
149
|
-
if (typeof tokenIndex.dict === 'number') {
|
|
150
|
-
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
151
|
-
}
|
|
152
|
-
pushByte(tokenIndex.index);
|
|
153
|
-
}
|
|
154
|
-
else if (isNibble(str)) {
|
|
155
|
-
writePackedBytes(str, 'nibble');
|
|
156
|
-
}
|
|
157
|
-
else if (isHex(str)) {
|
|
158
|
-
writePackedBytes(str, 'hex');
|
|
159
|
-
}
|
|
160
|
-
else if (str) {
|
|
161
|
-
const decodedJid = jidDecode(str);
|
|
162
|
-
if (decodedJid) {
|
|
163
|
-
writeJid(decodedJid);
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
writeStringRaw(str);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
const writeListStart = (listSize) => {
|
|
171
|
-
if (listSize === 0) {
|
|
172
|
-
pushByte(TAGS.LIST_EMPTY);
|
|
173
|
-
}
|
|
174
|
-
else if (listSize < 256) {
|
|
175
|
-
pushBytes([TAGS.LIST_8, listSize]);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
178
|
-
pushByte(TAGS.LIST_16);
|
|
179
|
-
pushInt16(listSize);
|
|
83
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
|
180
84
|
}
|
|
85
|
+
throw new Error(`invalid byte for nibble "${char}"`);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const packHex = char => {
|
|
89
|
+
if (char >= '0' && char <= '9') {
|
|
90
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
|
91
|
+
}
|
|
92
|
+
if (char >= 'A' && char <= 'F') {
|
|
93
|
+
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
|
|
94
|
+
}
|
|
95
|
+
if (char >= 'a' && char <= 'f') {
|
|
96
|
+
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
|
|
97
|
+
}
|
|
98
|
+
if (char === '\0') {
|
|
99
|
+
return 15;
|
|
100
|
+
}
|
|
101
|
+
throw new Error(`Invalid hex char "${char}"`);
|
|
102
|
+
};
|
|
103
|
+
const writePackedBytes = (str, type) => {
|
|
104
|
+
if (str.length > TAGS.PACKED_MAX) {
|
|
105
|
+
throw new Error('Too many bytes to pack');
|
|
106
|
+
}
|
|
107
|
+
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
|
108
|
+
let roundedLength = Math.ceil(str.length / 2.0);
|
|
109
|
+
if (str.length % 2 !== 0) {
|
|
110
|
+
roundedLength |= 128;
|
|
111
|
+
}
|
|
112
|
+
pushByte(roundedLength);
|
|
113
|
+
const packFunction = type === 'nibble' ? packNibble : packHex;
|
|
114
|
+
const packBytePair = (v1, v2) => {
|
|
115
|
+
const result = packFunction(v1) << 4 | packFunction(v2);
|
|
116
|
+
return result;
|
|
181
117
|
};
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
|
|
186
|
-
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
|
|
187
|
-
writeString(tag);
|
|
188
|
-
for (const key of validAttributes) {
|
|
189
|
-
if (typeof attrs[key] === 'string') {
|
|
190
|
-
writeString(key);
|
|
191
|
-
writeString(attrs[key]);
|
|
192
|
-
}
|
|
118
|
+
const strLengthHalf = Math.floor(str.length / 2);
|
|
119
|
+
for (let i = 0; i < strLengthHalf; i++) {
|
|
120
|
+
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
|
193
121
|
}
|
|
194
|
-
if (
|
|
195
|
-
|
|
122
|
+
if (str.length % 2 !== 0) {
|
|
123
|
+
pushByte(packBytePair(str[str.length - 1], '\x00'));
|
|
196
124
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
125
|
+
};
|
|
126
|
+
const isNibble = str => {
|
|
127
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
128
|
+
return false;
|
|
200
129
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
130
|
+
for (const char of str) {
|
|
131
|
+
const isInNibbleRange = char >= '0' && char <= '9';
|
|
132
|
+
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
};
|
|
138
|
+
const isHex = str => {
|
|
139
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
for (const char of str) {
|
|
143
|
+
const isInNibbleRange = char >= '0' && char <= '9';
|
|
144
|
+
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return true;
|
|
149
|
+
};
|
|
150
|
+
const writeString = str => {
|
|
151
|
+
if (str === undefined || str === null) {
|
|
152
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const tokenIndex = TOKEN_MAP[str];
|
|
156
|
+
if (tokenIndex) {
|
|
157
|
+
if (typeof tokenIndex.dict === 'number') {
|
|
158
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
159
|
+
}
|
|
160
|
+
pushByte(tokenIndex.index);
|
|
161
|
+
} else if (isNibble(str)) {
|
|
162
|
+
writePackedBytes(str, 'nibble');
|
|
163
|
+
} else if (isHex(str)) {
|
|
164
|
+
writePackedBytes(str, 'hex');
|
|
165
|
+
} else if (str) {
|
|
166
|
+
const decodedJid = jidDecode(str);
|
|
167
|
+
if (decodedJid) {
|
|
168
|
+
writeJid(decodedJid);
|
|
169
|
+
} else {
|
|
170
|
+
writeStringRaw(str);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const writeListStart = listSize => {
|
|
175
|
+
if (listSize === 0) {
|
|
176
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
177
|
+
} else if (listSize < 256) {
|
|
178
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
|
179
|
+
} else {
|
|
180
|
+
pushByte(TAGS.LIST_16);
|
|
181
|
+
pushInt16(listSize);
|
|
207
182
|
}
|
|
208
|
-
|
|
209
|
-
|
|
183
|
+
};
|
|
184
|
+
if (!tag) {
|
|
185
|
+
throw new Error('Invalid node: tag cannot be undefined');
|
|
186
|
+
}
|
|
187
|
+
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
|
|
188
|
+
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
|
|
189
|
+
writeString(tag);
|
|
190
|
+
for (const key of validAttributes) {
|
|
191
|
+
if (typeof attrs[key] === 'string') {
|
|
192
|
+
writeString(key);
|
|
193
|
+
writeString(attrs[key]);
|
|
210
194
|
}
|
|
211
|
-
|
|
212
|
-
|
|
195
|
+
}
|
|
196
|
+
if (typeof content === 'string') {
|
|
197
|
+
writeString(content);
|
|
198
|
+
} else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
199
|
+
writeByteLength(content.length);
|
|
200
|
+
pushBytes(content);
|
|
201
|
+
} else if (Array.isArray(content)) {
|
|
202
|
+
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
|
|
203
|
+
writeListStart(validContent.length);
|
|
204
|
+
for (const item of validContent) {
|
|
205
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
|
213
206
|
}
|
|
214
|
-
|
|
207
|
+
} else if (typeof content === 'undefined') {
|
|
208
|
+
// do nothing
|
|
209
|
+
} else {
|
|
210
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
|
|
211
|
+
}
|
|
212
|
+
return buffer;
|
|
215
213
|
};
|
|
216
214
|
//# sourceMappingURL=encode.js.map
|
|
@@ -1,102 +1,105 @@
|
|
|
1
1
|
import { Boom } from '@hapi/boom';
|
|
2
2
|
import { proto } from '../../WAProto/index.js';
|
|
3
|
-
import
|
|
3
|
+
import './types.js';
|
|
4
4
|
// some extra useful utilities
|
|
5
5
|
export const getBinaryNodeChildren = (node, childTag) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
if (Array.isArray(node?.content)) {
|
|
7
|
+
return node.content.filter(item => item.tag === childTag);
|
|
8
|
+
}
|
|
9
|
+
return [];
|
|
10
10
|
};
|
|
11
|
-
export const getAllBinaryNodeChildren = ({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return
|
|
11
|
+
export const getAllBinaryNodeChildren = ({
|
|
12
|
+
content
|
|
13
|
+
}) => {
|
|
14
|
+
if (Array.isArray(content)) {
|
|
15
|
+
return content;
|
|
16
|
+
}
|
|
17
|
+
return [];
|
|
16
18
|
};
|
|
17
19
|
export const getBinaryNodeChild = (node, childTag) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
if (Array.isArray(node?.content)) {
|
|
21
|
+
return node?.content.find(item => item.tag === childTag);
|
|
22
|
+
}
|
|
21
23
|
};
|
|
22
24
|
export const getBinaryNodeChildBuffer = (node, childTag) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
26
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
27
|
+
return child;
|
|
28
|
+
}
|
|
27
29
|
};
|
|
28
30
|
export const getBinaryNodeChildString = (node, childTag) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
31
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
32
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
33
|
+
return Buffer.from(child).toString('utf-8');
|
|
34
|
+
} else if (typeof child === 'string') {
|
|
35
|
+
return child;
|
|
36
|
+
}
|
|
36
37
|
};
|
|
37
38
|
export const getBinaryNodeChildUInt = (node, childTag, length) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
const buff = getBinaryNodeChildBuffer(node, childTag);
|
|
40
|
+
if (buff) {
|
|
41
|
+
return bufferToUInt(buff, length);
|
|
42
|
+
}
|
|
42
43
|
};
|
|
43
|
-
export const assertNodeErrorFree =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
export const assertNodeErrorFree = node => {
|
|
45
|
+
const errNode = getBinaryNodeChild(node, 'error');
|
|
46
|
+
if (errNode) {
|
|
47
|
+
throw new Boom(errNode.attrs.text || 'Unknown error', {
|
|
48
|
+
data: +errNode.attrs.code
|
|
49
|
+
});
|
|
50
|
+
}
|
|
48
51
|
};
|
|
49
52
|
export const reduceBinaryNodeToDictionary = (node, tag) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, {});
|
|
53
|
+
const nodes = getBinaryNodeChildren(node, tag);
|
|
54
|
+
const dict = nodes.reduce((dict, {
|
|
55
|
+
attrs
|
|
56
|
+
}) => {
|
|
57
|
+
if (typeof attrs.name === 'string') {
|
|
58
|
+
dict[attrs.name] = attrs.value || attrs.config_value;
|
|
59
|
+
} else {
|
|
60
|
+
dict[attrs.config_code] = attrs.value || attrs.config_value;
|
|
61
|
+
}
|
|
60
62
|
return dict;
|
|
63
|
+
}, {});
|
|
64
|
+
return dict;
|
|
61
65
|
};
|
|
62
|
-
export const getBinaryNodeMessages = ({
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
export const getBinaryNodeMessages = ({
|
|
67
|
+
content
|
|
68
|
+
}) => {
|
|
69
|
+
const msgs = [];
|
|
70
|
+
if (Array.isArray(content)) {
|
|
71
|
+
for (const item of content) {
|
|
72
|
+
if (item.tag === 'message') {
|
|
73
|
+
msgs.push(proto.WebMessageInfo.decode(item.content));
|
|
74
|
+
}
|
|
70
75
|
}
|
|
71
|
-
|
|
76
|
+
}
|
|
77
|
+
return msgs;
|
|
72
78
|
};
|
|
73
79
|
function bufferToUInt(e, t) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
let a = 0;
|
|
81
|
+
for (let i = 0; i < t; i++) {
|
|
82
|
+
a = 256 * a + e[i];
|
|
83
|
+
}
|
|
84
|
+
return a;
|
|
79
85
|
}
|
|
80
|
-
const tabs =
|
|
86
|
+
const tabs = n => '\t'.repeat(n);
|
|
81
87
|
export function binaryNodeToString(node, i = 0) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
.join(' ')}`;
|
|
99
|
-
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
|
|
100
|
-
return tag + content;
|
|
88
|
+
if (!node) {
|
|
89
|
+
return node;
|
|
90
|
+
}
|
|
91
|
+
if (typeof node === 'string') {
|
|
92
|
+
return tabs(i) + node;
|
|
93
|
+
}
|
|
94
|
+
if (node instanceof Uint8Array) {
|
|
95
|
+
return tabs(i) + Buffer.from(node).toString('hex');
|
|
96
|
+
}
|
|
97
|
+
if (Array.isArray(node)) {
|
|
98
|
+
return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
|
|
99
|
+
}
|
|
100
|
+
const children = binaryNodeToString(node.content, i + 1);
|
|
101
|
+
const tag = `<${node.tag} ${Object.entries(node.attrs || {}).filter(([, v]) => v !== undefined).map(([k, v]) => `${k}='${v}'`).join(' ')}`;
|
|
102
|
+
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
|
|
103
|
+
return tag + content;
|
|
101
104
|
}
|
|
102
105
|
//# sourceMappingURL=generic-utils.js.map
|