@pontalabs/baileys 1.0.11 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -85
- package/lib/Defaults/connection.js +39 -0
- package/lib/Defaults/constants.js +50 -0
- package/lib/Defaults/history.js +13 -0
- package/lib/Defaults/index.js +36 -151
- package/lib/Defaults/media.js +43 -0
- package/lib/Defaults/phonenumber-mcc.json +221 -221
- package/lib/Defaults/prefix.js +12 -0
- package/lib/Defaults/vialeys-version.json +7 -0
- package/lib/Signal/Group/ciphertext-message.js +10 -15
- package/lib/Signal/Group/group-session-builder.js +34 -60
- package/lib/Signal/Group/group_cipher.js +69 -76
- package/lib/Signal/Group/index.js +41 -60
- package/lib/Signal/Group/keyhelper.js +12 -57
- package/lib/Signal/Group/sender-chain-key.js +20 -33
- package/lib/Signal/Group/sender-key-distribution-message.js +43 -48
- package/lib/Signal/Group/sender-key-message.js +58 -53
- package/lib/Signal/Group/sender-key-name.js +21 -31
- package/lib/Signal/Group/sender-key-record.js +21 -34
- package/lib/Signal/Group/sender-key-state.js +59 -106
- package/lib/Signal/Group/sender-message-key.js +16 -21
- package/lib/Signal/libsignal.js +282 -203
- package/lib/Signal/lid-mapping.js +164 -184
- package/lib/Socket/Client/index.js +31 -22
- package/lib/Socket/Client/types.js +9 -15
- package/lib/Socket/Client/websocket.js +42 -37
- package/lib/Socket/business.js +212 -336
- package/lib/Socket/chats.js +696 -940
- package/lib/Socket/community.js +454 -369
- package/lib/Socket/groups.js +278 -280
- package/lib/Socket/index.js +9 -21
- package/lib/Socket/messages-recv.js +1027 -1570
- package/lib/Socket/messages-send.js +1180 -1136
- package/lib/Socket/mex.js +59 -0
- package/lib/Socket/newsletter.js +193 -251
- package/lib/Socket/socket.js +758 -898
- package/lib/Store/index.js +35 -24
- package/lib/Store/make-cache-manager-store.js +53 -66
- package/lib/Store/make-in-memory-store.js +319 -325
- package/lib/Store/make-ordered-dictionary.js +49 -57
- package/lib/Store/object-repository.js +10 -15
- package/lib/Types/Auth.js +2 -3
- package/lib/Types/Bussines.js +2 -3
- package/lib/Types/Call.js +2 -3
- package/lib/Types/Chat.js +10 -9
- package/lib/Types/Contact.js +2 -3
- package/lib/Types/Events.js +2 -3
- package/lib/Types/GroupMetadata.js +2 -3
- package/lib/Types/Label.js +6 -12
- package/lib/Types/LabelAssociation.js +4 -13
- package/lib/Types/Message.js +10 -18
- package/lib/Types/MexUpdates.js +15 -18
- package/lib/Types/Newsletter.js +26 -34
- package/lib/Types/Product.js +2 -3
- package/lib/Types/Signal.js +2 -3
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +8 -13
- package/lib/Types/USync.js +2 -3
- package/lib/Types/index.js +49 -40
- package/lib/Utils/auth-utils.js +190 -495
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/business.js +177 -199
- package/lib/Utils/chat-utils.js +711 -624
- package/lib/Utils/crypto.js +116 -161
- package/lib/Utils/decode-wa-message.js +225 -321
- package/lib/Utils/event-buffer.js +363 -405
- package/lib/Utils/generics.js +318 -472
- package/lib/Utils/history.js +72 -76
- package/lib/Utils/index.js +49 -41
- package/lib/Utils/link-preview.js +59 -103
- package/lib/Utils/logger.js +4 -7
- package/lib/Utils/lt-hash.js +29 -42
- package/lib/Utils/make-mutex.js +26 -42
- package/lib/Utils/message-retry-manager.js +134 -160
- package/lib/Utils/messages-media.js +665 -731
- package/lib/Utils/messages.js +518 -1
- package/lib/Utils/noise-handler.js +127 -125
- package/lib/Utils/pre-key-manager.js +86 -0
- package/lib/Utils/process-message.js +739 -351
- package/lib/Utils/signal.js +146 -129
- package/lib/Utils/use-multi-file-auth-state.js +68 -135
- package/lib/Utils/validate-connection.js +183 -142
- package/lib/Utils/vialeys-event-stream.js +41 -0
- package/lib/WABinary/constants.js +1281 -1289
- package/lib/WABinary/decode.js +145 -200
- package/lib/WABinary/encode.js +149 -202
- package/lib/WABinary/generic-utils.js +79 -97
- package/lib/WABinary/index.js +36 -25
- package/lib/WABinary/jid-utils.js +91 -123
- package/lib/WABinary/types.js +2 -3
- package/lib/WAM/BinaryInfo.js +8 -13
- package/lib/WAM/constants.js +16584 -11464
- package/lib/WAM/encode.js +117 -133
- package/lib/WAM/index.js +34 -23
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -69
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +11 -25
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +32 -44
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +13 -23
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -38
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +17 -31
- package/lib/WAUSync/Protocols/index.js +37 -26
- package/lib/WAUSync/USyncQuery.js +66 -66
- package/lib/WAUSync/USyncUser.js +13 -18
- package/lib/WAUSync/index.js +34 -23
- package/lib/index.js +95 -28
- package/package.json +2 -1
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/usync.d.ts +0 -37
- package/lib/Socket/usync.js +0 -83
- package/lib/Utils/baileys-event-stream.d.ts +0 -18
- package/lib/Utils/baileys-event-stream.js +0 -70
- package/lib/Utils/signal-housekeeping.d.ts +0 -19
- package/lib/Utils/signal-housekeeping.js +0 -73
- package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
- package/lib/Utils/use-mongo-file-auth-state.js +0 -84
- package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
- package/lib/Utils/use-single-file-auth-state.js +0 -80
package/lib/WABinary/encode.js
CHANGED
|
@@ -1,265 +1,212 @@
|
|
|
1
|
-
"use strict"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var desc = Object.getOwnPropertyDescriptor(m, k)
|
|
6
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
-
desc = { enumerable: true, get: function() { return m[k] } }
|
|
8
|
-
}
|
|
9
|
-
Object.defineProperty(o, k2, desc)
|
|
10
|
-
}) : (function(o, m, k, k2) {
|
|
11
|
-
if (k2 === undefined) k2 = k
|
|
12
|
-
o[k2] = m[k]
|
|
13
|
-
}))
|
|
14
|
-
|
|
15
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v })
|
|
17
|
-
}) : function(o, v) {
|
|
18
|
-
o["default"] = v
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
-
if (mod && mod.__esModule) return mod
|
|
23
|
-
var result = {}
|
|
24
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
|
|
25
|
-
__setModuleDefault(result, mod)
|
|
26
|
-
return result
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true })
|
|
30
|
-
|
|
31
|
-
const constants = __importStar(require("./constants"))
|
|
32
|
-
const jid_utils_1 = require("./jid-utils")
|
|
33
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { jidDecode: jidDecode } = require("./jid-utils");
|
|
4
|
+
const constants = require("./constants");
|
|
34
5
|
const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
35
|
-
const encoded = encodeBinaryNodeInner(node, opts, buffer)
|
|
36
|
-
return Buffer.from(encoded)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const pushByte = (value) => buffer.push(value & 0xff)
|
|
6
|
+
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
|
7
|
+
return Buffer.from(encoded);
|
|
8
|
+
};
|
|
9
|
+
const encodeBinaryNodeInner = ({ tag: tag, attrs: attrs, content: content }, opts, buffer) => {
|
|
10
|
+
const { TAGS: TAGS, TOKEN_MAP: TOKEN_MAP } = opts;
|
|
11
|
+
const pushByte = (value) => buffer.push(value & 255);
|
|
42
12
|
const pushInt = (value, n, littleEndian = false) => {
|
|
43
13
|
for (let i = 0; i < n; i++) {
|
|
44
|
-
const curShift = littleEndian ? i : n - 1 - i
|
|
45
|
-
buffer.push((value >> (curShift * 8)) &
|
|
14
|
+
const curShift = littleEndian ? i : n - 1 - i;
|
|
15
|
+
buffer.push((value >> (curShift * 8)) & 255);
|
|
46
16
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
17
|
+
};
|
|
49
18
|
const pushBytes = (bytes) => {
|
|
50
19
|
for (const b of bytes) {
|
|
51
|
-
buffer.push(b)
|
|
20
|
+
buffer.push(b);
|
|
52
21
|
}
|
|
53
|
-
}
|
|
54
|
-
|
|
22
|
+
};
|
|
55
23
|
const pushInt16 = (value) => {
|
|
56
|
-
pushBytes([(value >> 8) &
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]))
|
|
24
|
+
pushBytes([(value >> 8) & 255, value & 255]);
|
|
25
|
+
};
|
|
26
|
+
const pushInt20 = (value) => pushBytes([(value >> 16) & 15, (value >> 8) & 255, value & 255]);
|
|
60
27
|
const writeByteLength = (length) => {
|
|
61
28
|
if (length >= 4294967296) {
|
|
62
|
-
throw new Error(
|
|
29
|
+
throw new Error("string too large to encode: " + length);
|
|
63
30
|
}
|
|
64
31
|
if (length >= 1 << 20) {
|
|
65
|
-
pushByte(TAGS.BINARY_32)
|
|
66
|
-
pushInt(length, 4)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
32
|
+
pushByte(TAGS.BINARY_32);
|
|
33
|
+
pushInt(length, 4);
|
|
34
|
+
} else if (length >= 256) {
|
|
35
|
+
pushByte(TAGS.BINARY_20);
|
|
36
|
+
pushInt20(length);
|
|
37
|
+
} else {
|
|
38
|
+
pushByte(TAGS.BINARY_8);
|
|
39
|
+
pushByte(length);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
78
42
|
const writeStringRaw = (str) => {
|
|
79
|
-
const bytes = Buffer.from(str,
|
|
80
|
-
writeByteLength(bytes.length)
|
|
81
|
-
pushBytes(bytes)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
pushByte(
|
|
87
|
-
pushByte(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
else {
|
|
92
|
-
pushByte(TAGS.JID_PAIR)
|
|
43
|
+
const bytes = Buffer.from(str, "utf-8");
|
|
44
|
+
writeByteLength(bytes.length);
|
|
45
|
+
pushBytes(bytes);
|
|
46
|
+
};
|
|
47
|
+
const writeJid = ({ domainType: domainType, device: device, user: user, server: server }) => {
|
|
48
|
+
if (typeof device !== "undefined") {
|
|
49
|
+
pushByte(TAGS.AD_JID);
|
|
50
|
+
pushByte(domainType || 0);
|
|
51
|
+
pushByte(device || 0);
|
|
52
|
+
writeString(user);
|
|
53
|
+
} else {
|
|
54
|
+
pushByte(TAGS.JID_PAIR);
|
|
93
55
|
if (user.length) {
|
|
94
|
-
writeString(user)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
pushByte(TAGS.LIST_EMPTY)
|
|
56
|
+
writeString(user);
|
|
57
|
+
} else {
|
|
58
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
98
59
|
}
|
|
99
|
-
writeString(server)
|
|
60
|
+
writeString(server);
|
|
100
61
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
62
|
+
};
|
|
103
63
|
const packNibble = (char) => {
|
|
104
64
|
switch (char) {
|
|
105
|
-
case
|
|
106
|
-
return 10
|
|
107
|
-
case
|
|
108
|
-
return 11
|
|
109
|
-
case
|
|
110
|
-
return 15
|
|
65
|
+
case "-":
|
|
66
|
+
return 10;
|
|
67
|
+
case ".":
|
|
68
|
+
return 11;
|
|
69
|
+
case "\0":
|
|
70
|
+
return 15;
|
|
111
71
|
default:
|
|
112
|
-
if (char >=
|
|
113
|
-
return char.charCodeAt(0) -
|
|
72
|
+
if (char >= "0" && char <= "9") {
|
|
73
|
+
return char.charCodeAt(0) - "0".charCodeAt(0);
|
|
114
74
|
}
|
|
115
|
-
throw new Error(`invalid byte for nibble "${char}"`)
|
|
75
|
+
throw new Error(`invalid byte for nibble "${char}"`);
|
|
116
76
|
}
|
|
117
|
-
}
|
|
118
|
-
|
|
77
|
+
};
|
|
119
78
|
const packHex = (char) => {
|
|
120
|
-
if (char >=
|
|
121
|
-
return char.charCodeAt(0) -
|
|
79
|
+
if (char >= "0" && char <= "9") {
|
|
80
|
+
return char.charCodeAt(0) - "0".charCodeAt(0);
|
|
122
81
|
}
|
|
123
|
-
if (char >=
|
|
124
|
-
return 10 + char.charCodeAt(0) -
|
|
82
|
+
if (char >= "A" && char <= "F") {
|
|
83
|
+
return 10 + char.charCodeAt(0) - "A".charCodeAt(0);
|
|
125
84
|
}
|
|
126
|
-
if (char >=
|
|
127
|
-
return 10 + char.charCodeAt(0) -
|
|
85
|
+
if (char >= "a" && char <= "f") {
|
|
86
|
+
return 10 + char.charCodeAt(0) - "a".charCodeAt(0);
|
|
128
87
|
}
|
|
129
|
-
if (char ===
|
|
130
|
-
return 15
|
|
88
|
+
if (char === "\0") {
|
|
89
|
+
return 15;
|
|
131
90
|
}
|
|
132
|
-
throw new Error(`Invalid hex char "${char}"`)
|
|
133
|
-
}
|
|
134
|
-
|
|
91
|
+
throw new Error(`Invalid hex char "${char}"`);
|
|
92
|
+
};
|
|
135
93
|
const writePackedBytes = (str, type) => {
|
|
136
94
|
if (str.length > TAGS.PACKED_MAX) {
|
|
137
|
-
throw new Error(
|
|
95
|
+
throw new Error("Too many bytes to pack");
|
|
138
96
|
}
|
|
139
|
-
pushByte(type ===
|
|
140
|
-
let roundedLength = Math.ceil(str.length / 2
|
|
97
|
+
pushByte(type === "nibble" ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
|
98
|
+
let roundedLength = Math.ceil(str.length / 2);
|
|
141
99
|
if (str.length % 2 !== 0) {
|
|
142
|
-
roundedLength |= 128
|
|
100
|
+
roundedLength |= 128;
|
|
143
101
|
}
|
|
144
|
-
pushByte(roundedLength)
|
|
145
|
-
const packFunction = type ===
|
|
102
|
+
pushByte(roundedLength);
|
|
103
|
+
const packFunction = type === "nibble" ? packNibble : packHex;
|
|
146
104
|
const packBytePair = (v1, v2) => {
|
|
147
|
-
const result = (packFunction(v1) << 4) | packFunction(v2)
|
|
148
|
-
return result
|
|
149
|
-
}
|
|
150
|
-
const strLengthHalf = Math.floor(str.length / 2)
|
|
105
|
+
const result = (packFunction(v1) << 4) | packFunction(v2);
|
|
106
|
+
return result;
|
|
107
|
+
};
|
|
108
|
+
const strLengthHalf = Math.floor(str.length / 2);
|
|
151
109
|
for (let i = 0; i < strLengthHalf; i++) {
|
|
152
|
-
pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
|
|
110
|
+
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
|
153
111
|
}
|
|
154
112
|
if (str.length % 2 !== 0) {
|
|
155
|
-
pushByte(packBytePair(str[str.length - 1],
|
|
113
|
+
pushByte(packBytePair(str[str.length - 1], "\0"));
|
|
156
114
|
}
|
|
157
|
-
}
|
|
158
|
-
|
|
115
|
+
};
|
|
159
116
|
const isNibble = (str) => {
|
|
160
117
|
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
161
|
-
return false
|
|
118
|
+
return false;
|
|
162
119
|
}
|
|
163
120
|
for (const char of str) {
|
|
164
|
-
const isInNibbleRange = char >=
|
|
165
|
-
if (!isInNibbleRange && char !==
|
|
166
|
-
return false
|
|
121
|
+
const isInNibbleRange = char >= "0" && char <= "9";
|
|
122
|
+
if (!isInNibbleRange && char !== "-" && char !== ".") {
|
|
123
|
+
return false;
|
|
167
124
|
}
|
|
168
125
|
}
|
|
169
|
-
return true
|
|
170
|
-
}
|
|
171
|
-
|
|
126
|
+
return true;
|
|
127
|
+
};
|
|
172
128
|
const isHex = (str) => {
|
|
173
129
|
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
174
|
-
return false
|
|
130
|
+
return false;
|
|
175
131
|
}
|
|
176
132
|
for (const char of str) {
|
|
177
|
-
const isInNibbleRange = char >=
|
|
178
|
-
if (!isInNibbleRange && !(char >=
|
|
179
|
-
return false
|
|
133
|
+
const isInNibbleRange = char >= "0" && char <= "9";
|
|
134
|
+
if (!isInNibbleRange && !(char >= "A" && char <= "F")) {
|
|
135
|
+
return false;
|
|
180
136
|
}
|
|
181
137
|
}
|
|
182
|
-
return true
|
|
183
|
-
}
|
|
184
|
-
|
|
138
|
+
return true;
|
|
139
|
+
};
|
|
185
140
|
const writeString = (str) => {
|
|
186
|
-
|
|
187
|
-
pushByte(TAGS.LIST_EMPTY)
|
|
188
|
-
return
|
|
141
|
+
if (str === undefined || str === null) {
|
|
142
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
143
|
+
return;
|
|
189
144
|
}
|
|
190
|
-
const tokenIndex = TOKEN_MAP[str]
|
|
145
|
+
const tokenIndex = TOKEN_MAP[str];
|
|
191
146
|
if (tokenIndex) {
|
|
192
|
-
if (typeof tokenIndex.dict ===
|
|
193
|
-
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
|
|
147
|
+
if (typeof tokenIndex.dict === "number") {
|
|
148
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
194
149
|
}
|
|
195
|
-
pushByte(tokenIndex.index)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
else if (
|
|
201
|
-
|
|
202
|
-
}
|
|
203
|
-
else if (str) {
|
|
204
|
-
const decodedJid = (0, jid_utils_1.jidDecode)(str)
|
|
150
|
+
pushByte(tokenIndex.index);
|
|
151
|
+
} else if (isNibble(str)) {
|
|
152
|
+
writePackedBytes(str, "nibble");
|
|
153
|
+
} else if (isHex(str)) {
|
|
154
|
+
writePackedBytes(str, "hex");
|
|
155
|
+
} else if (str) {
|
|
156
|
+
const decodedJid = jidDecode(str);
|
|
205
157
|
if (decodedJid) {
|
|
206
|
-
writeJid(decodedJid)
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
writeStringRaw(str)
|
|
158
|
+
writeJid(decodedJid);
|
|
159
|
+
} else {
|
|
160
|
+
writeStringRaw(str);
|
|
210
161
|
}
|
|
211
162
|
}
|
|
212
|
-
}
|
|
213
|
-
|
|
163
|
+
};
|
|
214
164
|
const writeListStart = (listSize) => {
|
|
215
165
|
if (listSize === 0) {
|
|
216
|
-
pushByte(TAGS.LIST_EMPTY)
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
166
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
167
|
+
} else if (listSize < 256) {
|
|
168
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
|
169
|
+
} else {
|
|
170
|
+
pushByte(TAGS.LIST_16);
|
|
171
|
+
pushInt16(listSize);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
227
174
|
if (!tag) {
|
|
228
|
-
throw new Error(
|
|
175
|
+
throw new Error("Invalid node: tag cannot be undefined");
|
|
229
176
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
177
|
+
const validAttributes = Object.keys(attrs || {}).filter(
|
|
178
|
+
(k) => typeof attrs[k] !== "undefined" && attrs[k] !== null
|
|
179
|
+
);
|
|
180
|
+
writeListStart(2 * validAttributes.length + 1 + (typeof content !== "undefined" ? 1 : 0));
|
|
181
|
+
writeString(tag);
|
|
234
182
|
for (const key of validAttributes) {
|
|
235
|
-
if (typeof attrs[key] ===
|
|
236
|
-
writeString(key)
|
|
237
|
-
writeString(attrs[key])
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
if (typeof content ===
|
|
241
|
-
writeString(content)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
183
|
+
if (typeof attrs[key] === "string") {
|
|
184
|
+
writeString(key);
|
|
185
|
+
writeString(attrs[key]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (typeof content === "string") {
|
|
189
|
+
writeString(content);
|
|
190
|
+
} else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
191
|
+
writeByteLength(content.length);
|
|
192
|
+
pushBytes(content);
|
|
193
|
+
} else if (Array.isArray(content)) {
|
|
194
|
+
const validContent = content.filter(
|
|
195
|
+
(item) =>
|
|
196
|
+
item &&
|
|
197
|
+
(item.tag ||
|
|
198
|
+
Buffer.isBuffer(item) ||
|
|
199
|
+
item instanceof Uint8Array ||
|
|
200
|
+
typeof item === "string")
|
|
201
|
+
);
|
|
202
|
+
writeListStart(validContent.length);
|
|
250
203
|
for (const item of validContent) {
|
|
251
|
-
encodeBinaryNodeInner(item, opts, buffer)
|
|
204
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
|
252
205
|
}
|
|
206
|
+
} else if (typeof content === "undefined") {
|
|
207
|
+
} else {
|
|
208
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
|
|
253
209
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
else {
|
|
258
|
-
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
|
|
259
|
-
}
|
|
260
|
-
return buffer
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
module.exports = {
|
|
264
|
-
encodeBinaryNode
|
|
265
|
-
}
|
|
210
|
+
return buffer;
|
|
211
|
+
};
|
|
212
|
+
module.exports = { encodeBinaryNode: encodeBinaryNode };
|
|
@@ -1,142 +1,124 @@
|
|
|
1
|
-
"use strict"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const boom_1 = require("@hapi/boom")
|
|
6
|
-
const WAProto_1 = require("../../WAProto")
|
|
7
|
-
|
|
8
|
-
// some extra useful utilities
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const { Boom: Boom } = require("@hapi/boom");
|
|
4
|
+
const { proto: proto } = require("../../WAProto");
|
|
9
5
|
const getBinaryNodeChildren = (node, childTag) => {
|
|
10
6
|
if (Array.isArray(node?.content)) {
|
|
11
|
-
return node.content.filter(item => item.tag === childTag)
|
|
7
|
+
return node.content.filter((item) => item.tag === childTag);
|
|
12
8
|
}
|
|
13
|
-
return []
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const getAllBinaryNodeChildren = ({ content }) => {
|
|
9
|
+
return [];
|
|
10
|
+
};
|
|
11
|
+
const getAllBinaryNodeChildren = ({ content: content }) => {
|
|
17
12
|
if (Array.isArray(content)) {
|
|
18
|
-
return content
|
|
13
|
+
return content;
|
|
19
14
|
}
|
|
20
|
-
return []
|
|
21
|
-
}
|
|
22
|
-
|
|
15
|
+
return [];
|
|
16
|
+
};
|
|
23
17
|
const getBinaryNodeChild = (node, childTag) => {
|
|
24
18
|
if (Array.isArray(node?.content)) {
|
|
25
|
-
return node?.content.find(item => item.tag === childTag)
|
|
19
|
+
return node?.content.find((item) => item.tag === childTag);
|
|
26
20
|
}
|
|
27
|
-
}
|
|
28
|
-
|
|
21
|
+
};
|
|
29
22
|
const getBinaryNodeChildBuffer = (node, childTag) => {
|
|
30
|
-
const child = getBinaryNodeChild(node, childTag)?.content
|
|
23
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
31
24
|
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
32
|
-
return child
|
|
25
|
+
return child;
|
|
33
26
|
}
|
|
34
|
-
}
|
|
35
|
-
|
|
27
|
+
};
|
|
36
28
|
const getBinaryNodeChildString = (node, childTag) => {
|
|
37
|
-
const child = getBinaryNodeChild(node, childTag)?.content
|
|
29
|
+
const child = getBinaryNodeChild(node, childTag)?.content;
|
|
38
30
|
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
|
39
|
-
return Buffer.from(child).toString(
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return child
|
|
31
|
+
return Buffer.from(child).toString("utf-8");
|
|
32
|
+
} else if (typeof child === "string") {
|
|
33
|
+
return child;
|
|
43
34
|
}
|
|
44
|
-
}
|
|
45
|
-
|
|
35
|
+
};
|
|
46
36
|
const getBinaryNodeChildUInt = (node, childTag, length) => {
|
|
47
|
-
const buff = getBinaryNodeChildBuffer(node, childTag)
|
|
37
|
+
const buff = getBinaryNodeChildBuffer(node, childTag);
|
|
48
38
|
if (buff) {
|
|
49
|
-
return bufferToUInt(buff, length)
|
|
39
|
+
return bufferToUInt(buff, length);
|
|
50
40
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
41
|
+
};
|
|
53
42
|
const assertNodeErrorFree = (node) => {
|
|
54
|
-
const errNode = getBinaryNodeChild(node,
|
|
43
|
+
const errNode = getBinaryNodeChild(node, "error");
|
|
55
44
|
if (errNode) {
|
|
56
|
-
throw new
|
|
45
|
+
throw new Boom(errNode.attrs.text || "Unknown error", {
|
|
46
|
+
data: +errNode.attrs.code,
|
|
47
|
+
});
|
|
57
48
|
}
|
|
58
|
-
}
|
|
59
|
-
|
|
49
|
+
};
|
|
60
50
|
const reduceBinaryNodeToDictionary = (node, tag) => {
|
|
61
|
-
const nodes = getBinaryNodeChildren(node, tag)
|
|
62
|
-
const dict = nodes.reduce((dict, { attrs }) => {
|
|
63
|
-
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value
|
|
64
|
-
return dict
|
|
65
|
-
}, {})
|
|
66
|
-
return dict
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
const msgs = []
|
|
51
|
+
const nodes = getBinaryNodeChildren(node, tag);
|
|
52
|
+
const dict = nodes.reduce((dict, { attrs: attrs }) => {
|
|
53
|
+
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;
|
|
54
|
+
return dict;
|
|
55
|
+
}, {});
|
|
56
|
+
return dict;
|
|
57
|
+
};
|
|
58
|
+
const getBinaryNodeMessages = ({ content: content }) => {
|
|
59
|
+
const msgs = [];
|
|
71
60
|
if (Array.isArray(content)) {
|
|
72
61
|
for (const item of content) {
|
|
73
|
-
if (item.tag ===
|
|
74
|
-
msgs.push(
|
|
62
|
+
if (item.tag === "message") {
|
|
63
|
+
msgs.push(proto.WebMessageInfo.decode(item.content));
|
|
75
64
|
}
|
|
76
65
|
}
|
|
77
66
|
}
|
|
78
|
-
return msgs
|
|
79
|
-
}
|
|
80
|
-
|
|
67
|
+
return msgs;
|
|
68
|
+
};
|
|
81
69
|
const getBinaryFilteredButtons = (nodeContent) => {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
)
|
|
89
|
-
}
|
|
90
|
-
|
|
70
|
+
if (!Array.isArray(nodeContent)) return false;
|
|
71
|
+
return nodeContent.some(
|
|
72
|
+
(a) =>
|
|
73
|
+
["native_flow"].includes(a?.content?.[0]?.content?.[0]?.tag) ||
|
|
74
|
+
["interactive", "buttons", "list"].includes(a?.content?.[0]?.tag) ||
|
|
75
|
+
["hsm", "biz"].includes(a?.tag)
|
|
76
|
+
);
|
|
77
|
+
};
|
|
91
78
|
const getBinaryFilteredBizBot = (nodeContent) => {
|
|
92
|
-
if (!Array.isArray(nodeContent)) return false
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
['bot'].includes(b?.tag) && b?.attrs?.biz_bot === '1'
|
|
96
|
-
)
|
|
97
|
-
}
|
|
98
|
-
|
|
79
|
+
if (!Array.isArray(nodeContent)) return false;
|
|
80
|
+
return nodeContent.some((b) => ["bot"].includes(b?.tag) && b?.attrs?.biz_bot === "1");
|
|
81
|
+
};
|
|
99
82
|
function bufferToUInt(e, t) {
|
|
100
|
-
let a = 0
|
|
83
|
+
let a = 0;
|
|
101
84
|
for (let i = 0; i < t; i++) {
|
|
102
|
-
a = 256 * a + e[i]
|
|
85
|
+
a = 256 * a + e[i];
|
|
103
86
|
}
|
|
104
|
-
return a
|
|
87
|
+
return a;
|
|
105
88
|
}
|
|
106
|
-
const tabs = (n) =>
|
|
89
|
+
const tabs = (n) => "\t".repeat(n);
|
|
107
90
|
function binaryNodeToString(node, i = 0) {
|
|
108
91
|
if (!node) {
|
|
109
|
-
return node
|
|
92
|
+
return node;
|
|
110
93
|
}
|
|
111
|
-
if (typeof node ===
|
|
112
|
-
return tabs(i) + node
|
|
94
|
+
if (typeof node === "string") {
|
|
95
|
+
return tabs(i) + node;
|
|
113
96
|
}
|
|
114
97
|
if (node instanceof Uint8Array) {
|
|
115
|
-
return tabs(i) + Buffer.from(node).toString(
|
|
98
|
+
return tabs(i) + Buffer.from(node).toString("hex");
|
|
116
99
|
}
|
|
117
100
|
if (Array.isArray(node)) {
|
|
118
|
-
return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join(
|
|
101
|
+
return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join("\n");
|
|
119
102
|
}
|
|
120
|
-
const children = binaryNodeToString(node.content, i + 1)
|
|
103
|
+
const children = binaryNodeToString(node.content, i + 1);
|
|
121
104
|
const tag = `<${node.tag} ${Object.entries(node.attrs || {})
|
|
122
105
|
.filter(([, v]) => v !== undefined)
|
|
123
106
|
.map(([k, v]) => `${k}='${v}'`)
|
|
124
|
-
.join(
|
|
125
|
-
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` :
|
|
126
|
-
return tag + content
|
|
107
|
+
.join(" ")}`;
|
|
108
|
+
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : "/>";
|
|
109
|
+
return tag + content;
|
|
127
110
|
}
|
|
128
|
-
|
|
129
111
|
module.exports = {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
112
|
+
getBinaryNodeChildren: getBinaryNodeChildren,
|
|
113
|
+
getAllBinaryNodeChildren: getAllBinaryNodeChildren,
|
|
114
|
+
getBinaryNodeChild: getBinaryNodeChild,
|
|
115
|
+
getBinaryNodeChildBuffer: getBinaryNodeChildBuffer,
|
|
116
|
+
getBinaryNodeChildString: getBinaryNodeChildString,
|
|
117
|
+
getBinaryNodeChildUInt: getBinaryNodeChildUInt,
|
|
118
|
+
assertNodeErrorFree: assertNodeErrorFree,
|
|
119
|
+
reduceBinaryNodeToDictionary: reduceBinaryNodeToDictionary,
|
|
120
|
+
getBinaryNodeMessages: getBinaryNodeMessages,
|
|
121
|
+
getBinaryFilteredButtons: getBinaryFilteredButtons,
|
|
122
|
+
getBinaryFilteredBizBot: getBinaryFilteredBizBot,
|
|
123
|
+
binaryNodeToString: binaryNodeToString,
|
|
124
|
+
};
|