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