@mrlegendbot/baileys 1.2.3 → 1.2.4
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.upstream.md +1312 -0
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +6275 -0
- package/WAProto/compiler.js +550 -0
- package/WAProto/index.js +2 -97691
- package/lib/Defaults/index.js +89 -92
- package/lib/Signal/Group/ciphertext-message.js +9 -10
- package/lib/Signal/Group/group-session-builder.js +21 -22
- package/lib/Signal/Group/group_cipher.js +75 -69
- package/lib/Signal/Group/index.js +0 -1
- package/lib/Signal/Group/keyhelper.js +9 -10
- package/lib/Signal/Group/sender-chain-key.js +21 -22
- package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
- package/lib/Signal/Group/sender-key-message.js +56 -62
- package/lib/Signal/Group/sender-key-name.js +38 -40
- package/lib/Signal/Group/sender-key-record.js +33 -34
- package/lib/Signal/Group/sender-key-state.js +72 -76
- package/lib/Signal/Group/sender-message-key.js +22 -23
- package/lib/Signal/libsignal.js +506 -405
- package/lib/Signal/lid-mapping.js +276 -258
- package/lib/Socket/Client/index.js +0 -1
- package/lib/Socket/Client/types.js +6 -7
- package/lib/Socket/Client/websocket.js +48 -49
- package/lib/Socket/business.js +349 -369
- package/lib/Socket/chats.js +1172 -1148
- package/lib/Socket/communities.js +473 -413
- package/lib/Socket/groups.js +387 -331
- package/lib/Socket/index.js +6 -8
- package/lib/Socket/messages-recv.js +1782 -1719
- package/lib/Socket/messages-send.js +1503 -2450
- package/lib/Socket/mex.js +40 -35
- package/lib/Socket/newsletter.js +239 -191
- package/lib/Socket/socket.js +992 -923
- package/lib/Store/index.js +8 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +70 -0
- package/lib/Store/make-in-memory-store.js +417 -0
- package/lib/Store/make-ordered-dictionary.js +69 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +0 -1
- package/lib/Types/Bussines.js +0 -1
- package/lib/Types/Call.js +0 -1
- package/lib/Types/Chat.js +1 -8
- package/lib/Types/Contact.js +0 -1
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +0 -1
- package/lib/Types/Label.js +20 -22
- package/lib/Types/LabelAssociation.js +2 -4
- package/lib/Types/Message.js +3 -5
- package/lib/Types/Mex.js +26 -31
- package/lib/Types/Product.js +0 -1
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +4 -52
- package/lib/Types/USync.js +0 -1
- package/lib/Types/index.js +10 -11
- package/lib/Utils/auth-utils.js +240 -277
- package/lib/Utils/browser-utils.js +20 -23
- package/lib/Utils/business.js +200 -204
- package/lib/Utils/chat-utils.js +836 -810
- package/lib/Utils/companion-reg-client-utils.js +23 -25
- package/lib/Utils/crypto.js +70 -86
- package/lib/Utils/decode-wa-message.js +277 -284
- package/lib/Utils/event-buffer.js +592 -595
- package/lib/Utils/generics.js +312 -335
- package/lib/Utils/history.js +123 -115
- package/lib/Utils/index.js +1 -2
- package/lib/Utils/link-preview.js +74 -73
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +27 -27
- package/lib/Utils/message-retry-manager.js +213 -254
- package/lib/Utils/messages-media.js +741 -715
- package/lib/Utils/messages.js +791 -827
- package/lib/Utils/noise-handler.js +215 -187
- package/lib/Utils/pre-key-manager.js +69 -98
- package/lib/Utils/process-message.js +617 -586
- package/lib/Utils/reporting-utils.js +553 -234
- package/lib/Utils/signal.js +217 -170
- package/lib/Utils/stanza-ack.js +27 -35
- package/lib/Utils/sync-action-utils.js +37 -41
- package/lib/Utils/tc-token-utils.js +120 -141
- package/lib/Utils/use-multi-file-auth-state.js +87 -105
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/Utils/validate-connection.js +190 -188
- package/lib/WABinary/constants.js +28 -1294
- package/lib/WABinary/decode.js +245 -249
- package/lib/WABinary/encode.js +201 -206
- package/lib/WABinary/generic-utils.js +86 -89
- package/lib/WABinary/index.js +0 -1
- package/lib/WABinary/jid-utils.js +68 -77
- package/lib/WABinary/types.js +0 -1
- package/lib/WAM/BinaryInfo.js +7 -8
- package/lib/WAM/constants.js +19892 -22853
- package/lib/WAM/encode.js +121 -132
- package/lib/WAM/index.js +0 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
- package/lib/WAUSync/Protocols/index.js +0 -1
- package/lib/WAUSync/USyncQuery.js +79 -88
- package/lib/WAUSync/USyncUser.js +28 -29
- package/lib/WAUSync/index.js +0 -1
- package/lib/index.js +2 -2
- package/package.json +22 -86
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/fix-imports.js +0 -85
- package/WAProto/index.d.ts +0 -14017
- package/lib/Defaults/index.d.ts +0 -75
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
- package/lib/Signal/Group/group-session-builder.d.ts +0 -15
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -12
- package/lib/Signal/Group/keyhelper.d.ts +0 -11
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -18
- package/lib/Signal/Group/sender-key-record.d.ts +0 -31
- package/lib/Signal/Group/sender-key-state.d.ts +0 -39
- package/lib/Signal/Group/sender-message-key.d.ts +0 -12
- package/lib/Signal/libsignal.d.ts +0 -5
- package/lib/Signal/lid-mapping.d.ts +0 -23
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -217
- package/lib/Socket/chats.d.ts +0 -125
- package/lib/Socket/communities.d.ts +0 -273
- package/lib/Socket/groups.d.ts +0 -161
- package/lib/Socket/index.d.ts +0 -260
- package/lib/Socket/messages-recv.d.ts +0 -203
- package/lib/Socket/messages-send.d.ts +0 -199
- package/lib/Socket/mex.d.ts +0 -3
- package/lib/Socket/newsletter.d.ts +0 -170
- package/lib/Socket/socket.d.ts +0 -59
- package/lib/Types/Auth.d.ts +0 -117
- package/lib/Types/Bussines.d.ts +0 -25
- package/lib/Types/Call.d.ts +0 -15
- package/lib/Types/Chat.d.ts +0 -124
- package/lib/Types/Contact.d.ts +0 -26
- package/lib/Types/Events.d.ts +0 -256
- package/lib/Types/GroupMetadata.d.ts +0 -71
- package/lib/Types/Label.d.ts +0 -47
- package/lib/Types/LabelAssociation.d.ts +0 -30
- package/lib/Types/Message.d.ts +0 -320
- package/lib/Types/Mex.d.ts +0 -141
- package/lib/Types/Product.d.ts +0 -79
- package/lib/Types/Signal.d.ts +0 -87
- package/lib/Types/Socket.d.ts +0 -136
- package/lib/Types/State.d.ts +0 -97
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -66
- package/lib/Utils/auth-utils.d.ts +0 -24
- package/lib/Utils/browser-utils.d.ts +0 -4
- package/lib/Utils/business.d.ts +0 -23
- package/lib/Utils/chat-utils.d.ts +0 -100
- package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
- package/lib/Utils/crypto.d.ts +0 -37
- package/lib/Utils/decode-wa-message.d.ts +0 -66
- package/lib/Utils/event-buffer.d.ts +0 -36
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -24
- package/lib/Utils/identity-change-handler.d.ts +0 -44
- package/lib/Utils/identity-change-handler.js +0 -50
- package/lib/Utils/index.d.ts +0 -22
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -12
- package/lib/Utils/lt-hash.d.ts +0 -8
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -115
- package/lib/Utils/messages-media.d.ts +0 -133
- package/lib/Utils/messages.d.ts +0 -91
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/offline-node-processor.d.ts +0 -17
- package/lib/Utils/offline-node-processor.js +0 -40
- package/lib/Utils/pre-key-manager.d.ts +0 -28
- package/lib/Utils/process-message.d.ts +0 -60
- package/lib/Utils/reporting-utils.d.ts +0 -11
- package/lib/Utils/signal.d.ts +0 -47
- package/lib/Utils/stanza-ack.d.ts +0 -11
- package/lib/Utils/sync-action-utils.d.ts +0 -19
- package/lib/Utils/tc-token-utils.d.ts +0 -37
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -28
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -6
- package/lib/WABinary/jid-utils.d.ts +0 -48
- package/lib/WABinary/types.d.ts +0 -19
- package/lib/WAM/BinaryInfo.d.ts +0 -9
- package/lib/WAM/constants.d.ts +0 -40
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -30
- package/lib/WAUSync/USyncUser.d.ts +0 -17
- package/lib/WAUSync/index.d.ts +0 -4
- package/lib/index.d.ts +0 -12
package/lib/WABinary/encode.js
CHANGED
|
@@ -1,220 +1,215 @@
|
|
|
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);
|
|
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
|
-
if (str === '') {
|
|
148
|
-
writeStringRaw(str);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const tokenIndex = TOKEN_MAP[str];
|
|
152
|
-
if (tokenIndex) {
|
|
153
|
-
if (typeof tokenIndex.dict === 'number') {
|
|
154
|
-
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
155
|
-
}
|
|
156
|
-
pushByte(tokenIndex.index);
|
|
157
|
-
}
|
|
158
|
-
else if (isNibble(str)) {
|
|
159
|
-
writePackedBytes(str, 'nibble');
|
|
160
|
-
}
|
|
161
|
-
else if (isHex(str)) {
|
|
162
|
-
writePackedBytes(str, 'hex');
|
|
163
|
-
}
|
|
164
|
-
else {
|
|
165
|
-
const decodedJid = jidDecode(str);
|
|
166
|
-
if (decodedJid) {
|
|
167
|
-
writeJid(decodedJid);
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
writeStringRaw(str);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
const writeListStart = (listSize) => {
|
|
175
|
-
if (listSize === 0) {
|
|
176
|
-
pushByte(TAGS.LIST_EMPTY);
|
|
177
|
-
}
|
|
178
|
-
else if (listSize < 256) {
|
|
179
|
-
pushBytes([TAGS.LIST_8, listSize]);
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
pushByte(TAGS.LIST_16);
|
|
183
|
-
pushInt16(listSize);
|
|
83
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
|
184
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;
|
|
185
117
|
};
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
|
|
190
|
-
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
|
|
191
|
-
writeString(tag);
|
|
192
|
-
for (const key of validAttributes) {
|
|
193
|
-
if (typeof attrs[key] === 'string') {
|
|
194
|
-
writeString(key);
|
|
195
|
-
writeString(attrs[key]);
|
|
196
|
-
}
|
|
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]));
|
|
197
121
|
}
|
|
198
|
-
if (
|
|
199
|
-
|
|
122
|
+
if (str.length % 2 !== 0) {
|
|
123
|
+
pushByte(packBytePair(str[str.length - 1], '\x00'));
|
|
200
124
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
125
|
+
};
|
|
126
|
+
const isNibble = str => {
|
|
127
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
128
|
+
return false;
|
|
204
129
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
+
if (str === '') {
|
|
156
|
+
writeStringRaw(str);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const tokenIndex = TOKEN_MAP[str];
|
|
160
|
+
if (tokenIndex) {
|
|
161
|
+
if (typeof tokenIndex.dict === 'number') {
|
|
162
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
163
|
+
}
|
|
164
|
+
pushByte(tokenIndex.index);
|
|
165
|
+
} else if (isNibble(str)) {
|
|
166
|
+
writePackedBytes(str, 'nibble');
|
|
167
|
+
} else if (isHex(str)) {
|
|
168
|
+
writePackedBytes(str, 'hex');
|
|
169
|
+
} else {
|
|
170
|
+
const decodedJid = jidDecode(str);
|
|
171
|
+
if (decodedJid) {
|
|
172
|
+
writeJid(decodedJid);
|
|
173
|
+
} else {
|
|
174
|
+
writeStringRaw(str);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
const writeListStart = listSize => {
|
|
179
|
+
if (listSize === 0) {
|
|
180
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
181
|
+
} else if (listSize < 256) {
|
|
182
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
|
183
|
+
} else {
|
|
184
|
+
pushByte(TAGS.LIST_16);
|
|
185
|
+
pushInt16(listSize);
|
|
211
186
|
}
|
|
212
|
-
|
|
213
|
-
|
|
187
|
+
};
|
|
188
|
+
if (!tag) {
|
|
189
|
+
throw new Error('Invalid node: tag cannot be undefined');
|
|
190
|
+
}
|
|
191
|
+
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null);
|
|
192
|
+
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
|
|
193
|
+
writeString(tag);
|
|
194
|
+
for (const key of validAttributes) {
|
|
195
|
+
if (typeof attrs[key] === 'string') {
|
|
196
|
+
writeString(key);
|
|
197
|
+
writeString(attrs[key]);
|
|
214
198
|
}
|
|
215
|
-
|
|
216
|
-
|
|
199
|
+
}
|
|
200
|
+
if (typeof content === 'string') {
|
|
201
|
+
writeString(content);
|
|
202
|
+
} else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
203
|
+
writeByteLength(content.length);
|
|
204
|
+
pushBytes(content);
|
|
205
|
+
} else if (Array.isArray(content)) {
|
|
206
|
+
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
|
|
207
|
+
writeListStart(validContent.length);
|
|
208
|
+
for (const item of validContent) {
|
|
209
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
|
217
210
|
}
|
|
218
|
-
|
|
211
|
+
} else if (typeof content === 'undefined') {} else {
|
|
212
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
|
|
213
|
+
}
|
|
214
|
+
return buffer;
|
|
219
215
|
};
|
|
220
|
-
//# sourceMappingURL=encode.js.map
|
|
@@ -1,113 +1,110 @@
|
|
|
1
1
|
import { Boom } from '@hapi/boom';
|
|
2
|
-
import { proto } from '../../WAProto/
|
|
3
|
-
import
|
|
4
|
-
// some extra useful utilities
|
|
2
|
+
import { proto } from '../../WAProto/compiler.js';
|
|
3
|
+
import './types.js';
|
|
5
4
|
const indexCache = new WeakMap();
|
|
6
5
|
export const getBinaryNodeChildren = (node, childTag) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (!arr)
|
|
16
|
-
index.set(child.tag, (arr = []));
|
|
17
|
-
arr.push(child);
|
|
18
|
-
}
|
|
19
|
-
indexCache.set(node, index);
|
|
6
|
+
if (!node || !Array.isArray(node.content)) return [];
|
|
7
|
+
let index = indexCache.get(node);
|
|
8
|
+
if (!index) {
|
|
9
|
+
index = new Map();
|
|
10
|
+
for (const child of node.content) {
|
|
11
|
+
let arr = index.get(child.tag);
|
|
12
|
+
if (!arr) index.set(child.tag, arr = []);
|
|
13
|
+
arr.push(child);
|
|
20
14
|
}
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
indexCache.set(node, index);
|
|
16
|
+
}
|
|
17
|
+
return index.get(childTag) || [];
|
|
23
18
|
};
|
|
24
19
|
export const getBinaryNodeChild = (node, childTag) => {
|
|
25
|
-
|
|
20
|
+
return getBinaryNodeChildren(node, childTag)[0];
|
|
26
21
|
};
|
|
27
|
-
export const getAllBinaryNodeChildren = ({
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
22
|
+
export const getAllBinaryNodeChildren = ({
|
|
23
|
+
content
|
|
24
|
+
}) => {
|
|
25
|
+
if (Array.isArray(content)) {
|
|
26
|
+
return content;
|
|
27
|
+
}
|
|
28
|
+
return [];
|
|
32
29
|
};
|
|
33
30
|
export const getBinaryNodeChildBuffer = (node, childTag) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
32
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
33
|
+
return child;
|
|
34
|
+
}
|
|
38
35
|
};
|
|
39
36
|
export const getBinaryNodeChildString = (node, childTag) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
37
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
38
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
39
|
+
return Buffer.from(child).toString('utf-8');
|
|
40
|
+
} else if (typeof child === 'string') {
|
|
41
|
+
return child;
|
|
42
|
+
}
|
|
47
43
|
};
|
|
48
44
|
export const getBinaryNodeChildUInt = (node, childTag, length) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
const buff = getBinaryNodeChildBuffer(node, childTag);
|
|
46
|
+
if (buff) {
|
|
47
|
+
return bufferToUInt(buff, length);
|
|
48
|
+
}
|
|
53
49
|
};
|
|
54
|
-
export const assertNodeErrorFree =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
export const assertNodeErrorFree = node => {
|
|
51
|
+
const errNode = getBinaryNodeChild(node, 'error');
|
|
52
|
+
if (errNode) {
|
|
53
|
+
throw new Boom(errNode.attrs.text || 'Unknown error', {
|
|
54
|
+
data: +errNode.attrs.code
|
|
55
|
+
});
|
|
56
|
+
}
|
|
59
57
|
};
|
|
60
58
|
export const reduceBinaryNodeToDictionary = (node, tag) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}, {});
|
|
59
|
+
const nodes = getBinaryNodeChildren(node, tag);
|
|
60
|
+
const dict = nodes.reduce((dict, {
|
|
61
|
+
attrs
|
|
62
|
+
}) => {
|
|
63
|
+
if (typeof attrs.name === 'string') {
|
|
64
|
+
dict[attrs.name] = attrs.value || attrs.config_value;
|
|
65
|
+
} else {
|
|
66
|
+
dict[attrs.config_code] = attrs.value || attrs.config_value;
|
|
67
|
+
}
|
|
71
68
|
return dict;
|
|
69
|
+
}, {});
|
|
70
|
+
return dict;
|
|
72
71
|
};
|
|
73
|
-
export const getBinaryNodeMessages = ({
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
export const getBinaryNodeMessages = ({
|
|
73
|
+
content
|
|
74
|
+
}) => {
|
|
75
|
+
const msgs = [];
|
|
76
|
+
if (Array.isArray(content)) {
|
|
77
|
+
for (const item of content) {
|
|
78
|
+
if (item.tag === 'message') {
|
|
79
|
+
msgs.push(proto.WebMessageInfo.decode(item.content).toJSON());
|
|
80
|
+
}
|
|
81
81
|
}
|
|
82
|
-
|
|
82
|
+
}
|
|
83
|
+
return msgs;
|
|
83
84
|
};
|
|
84
85
|
function bufferToUInt(e, t) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
let a = 0;
|
|
87
|
+
for (let i = 0; i < t; i++) {
|
|
88
|
+
a = 256 * a + e[i];
|
|
89
|
+
}
|
|
90
|
+
return a;
|
|
90
91
|
}
|
|
91
|
-
const tabs =
|
|
92
|
+
const tabs = n => '\t'.repeat(n);
|
|
92
93
|
export function binaryNodeToString(node, i = 0) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
.join(' ')}`;
|
|
110
|
-
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
|
|
111
|
-
return tag + content;
|
|
94
|
+
if (!node) {
|
|
95
|
+
return node;
|
|
96
|
+
}
|
|
97
|
+
if (typeof node === 'string') {
|
|
98
|
+
return tabs(i) + node;
|
|
99
|
+
}
|
|
100
|
+
if (node instanceof Uint8Array) {
|
|
101
|
+
return tabs(i) + Buffer.from(node).toString('hex');
|
|
102
|
+
}
|
|
103
|
+
if (Array.isArray(node)) {
|
|
104
|
+
return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n');
|
|
105
|
+
}
|
|
106
|
+
const children = binaryNodeToString(node.content, i + 1);
|
|
107
|
+
const tag = `<${node.tag} ${Object.entries(node.attrs || {}).filter(([, v]) => v !== undefined).map(([k, v]) => `${k}='${v}'`).join(' ')}`;
|
|
108
|
+
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>';
|
|
109
|
+
return tag + content;
|
|
112
110
|
}
|
|
113
|
-
//# sourceMappingURL=generic-utils.js.map
|
package/lib/WABinary/index.js
CHANGED