@baileys-md/baileys 12.0.0 → 12.2.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/LICENSE +21 -13
- package/WAProto/WAProto.proto +769 -233
- package/WAProto/index.js +65813 -141372
- package/lib/Defaults/index.js +128 -113
- package/lib/KeyDB/BinarySearch.js +30 -0
- package/lib/KeyDB/KeyedDB.js +178 -0
- package/lib/KeyDB/index.js +14 -0
- package/lib/Signal/Group/ciphertext-message.js +22 -14
- package/lib/Signal/Group/group-session-builder.js +21 -42
- package/lib/Signal/Group/group_cipher.js +85 -87
- package/lib/Signal/Group/index.js +23 -57
- package/lib/Signal/Group/keyhelper.js +28 -52
- package/lib/Signal/Group/sender-chain-key.js +37 -33
- package/lib/Signal/Group/sender-key-distribution-message.js +73 -63
- package/lib/Signal/Group/sender-key-message.js +75 -66
- package/lib/Signal/Group/sender-key-name.js +55 -44
- package/lib/Signal/Group/sender-key-record.js +49 -49
- package/lib/Signal/Group/sender-key-state.js +90 -93
- package/lib/Signal/Group/sender-message-key.js +37 -28
- package/lib/Signal/libsignal.js +324 -163
- package/lib/Signal/lid-mapping.js +166 -0
- package/lib/Socket/Client/index.js +14 -18
- package/lib/Socket/Client/types.js +12 -12
- package/lib/Socket/Client/websocket.js +60 -109
- package/lib/Socket/business.js +359 -242
- package/lib/Socket/chats.js +857 -898
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +304 -324
- package/lib/Socket/index.js +25 -9
- package/lib/Socket/messages-recv.js +1109 -1000
- package/lib/Socket/messages-send.js +615 -387
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +231 -227
- package/lib/Socket/socket.js +803 -628
- package/lib/Store/index.js +18 -8
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +286 -435
- package/lib/Store/make-ordered-dictionary.js +77 -79
- package/lib/Store/object-repository.js +24 -26
- package/lib/Types/Auth.js +13 -2
- package/lib/Types/Bussines.js +13 -0
- package/lib/Types/Call.js +13 -2
- package/lib/Types/Chat.js +19 -4
- package/lib/Types/Contact.js +13 -2
- package/lib/Types/Events.js +13 -2
- package/lib/Types/GroupMetadata.js +13 -2
- package/lib/Types/Label.js +43 -26
- package/lib/Types/LabelAssociation.js +16 -8
- package/lib/Types/Message.js +22 -7
- package/lib/Types/Newsletter.js +42 -17
- package/lib/Types/Product.js +13 -2
- package/lib/Types/Signal.js +13 -2
- package/lib/Types/Socket.js +14 -2
- package/lib/Types/State.js +21 -2
- package/lib/Types/USync.js +13 -2
- package/lib/Types/index.js +37 -41
- package/lib/Utils/auth-utils.js +219 -189
- package/lib/Utils/baileys-event-stream.js +54 -0
- package/lib/Utils/browser-utils.js +21 -31
- package/lib/Utils/business.js +213 -214
- package/lib/Utils/chat-utils.js +711 -689
- package/lib/Utils/crypto.js +112 -175
- package/lib/Utils/decode-wa-message.js +254 -194
- package/lib/Utils/event-buffer.js +510 -500
- package/lib/Utils/generics.js +318 -430
- package/lib/Utils/history.js +83 -90
- package/lib/Utils/index.js +31 -35
- package/lib/Utils/link-preview.js +71 -116
- package/lib/Utils/logger.js +5 -7
- package/lib/Utils/lt-hash.js +40 -46
- package/lib/Utils/make-mutex.js +34 -41
- package/lib/Utils/message-retry-manager.js +33 -48
- package/lib/Utils/messages-media.js +516 -784
- package/lib/Utils/messages.js +347 -489
- package/lib/Utils/noise-handler.js +138 -144
- package/lib/Utils/pre-key-manager.js +95 -0
- package/lib/Utils/process-message.js +331 -384
- package/lib/Utils/signal.js +157 -139
- package/lib/Utils/use-multi-file-auth-state.js +119 -91
- package/lib/Utils/validate-connection.js +184 -203
- package/lib/WABinary/constants.js +1308 -1298
- package/lib/WABinary/decode.js +241 -256
- package/lib/WABinary/encode.js +217 -239
- package/lib/WABinary/generic-utils.js +131 -40
- package/lib/WABinary/index.js +17 -21
- package/lib/WABinary/jid-utils.js +97 -79
- package/lib/WABinary/types.js +13 -2
- package/lib/WAM/BinaryInfo.js +20 -12
- package/lib/WAM/constants.js +22863 -15348
- package/lib/WAM/encode.js +145 -136
- package/lib/WAM/index.js +15 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +39 -31
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +61 -54
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +39 -29
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +48 -40
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -51
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +38 -21
- package/lib/WAUSync/Protocols/index.js +17 -20
- package/lib/WAUSync/USyncQuery.js +98 -86
- package/lib/WAUSync/USyncUser.js +35 -26
- package/lib/WAUSync/index.js +16 -19
- package/lib/index.js +23 -41
- package/package.json +46 -56
- package/README.md +0 -113
- package/WAProto/GenerateStatics.sh +0 -4
- package/lib/Defaults/wileys-version.json +0 -3
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/usync.js +0 -70
- package/lib/Utils/wileys-event-stream.js +0 -63
package/lib/WABinary/encode.js
CHANGED
|
@@ -1,250 +1,228 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.encodeBinaryNode = void 0;
|
|
37
|
-
const constants = __importStar(require("./constants"));
|
|
38
|
-
const jid_utils_1 = require("./jid-utils");
|
|
39
|
-
const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
40
|
-
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
|
41
|
-
return Buffer.from(encoded);
|
|
1
|
+
//========================================//
|
|
2
|
+
/**
|
|
3
|
+
* @project @sixcore/baileys
|
|
4
|
+
* @author Sixx.js </>🌿
|
|
5
|
+
* @version 1.0.3
|
|
6
|
+
* @license MIT
|
|
7
|
+
* @country Mozambique 🇲🇿♥️
|
|
8
|
+
* @chamadas || sms +258860817689
|
|
9
|
+
* @whatsApp +55889616-2417 || +258851907875
|
|
10
|
+
* @description Biblioteca Node.js para integração e automação no WhatsApp usando Baileys
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as constants from "./constants.js";
|
|
14
|
+
import { jidDecode } from "./jid-utils.js";
|
|
15
|
+
|
|
16
|
+
export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
17
|
+
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
|
18
|
+
return Buffer.from(encoded);
|
|
42
19
|
};
|
|
43
|
-
|
|
20
|
+
|
|
44
21
|
const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const
|
|
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
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
if (str.length % 2 !== 0) {
|
|
148
|
-
pushByte(packBytePair(str[str.length - 1], '\x00'));
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
const isNibble = (str) => {
|
|
152
|
-
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
for (let i = 0; i < str.length; i++) {
|
|
156
|
-
const char = str[i];
|
|
157
|
-
const isInNibbleRange = char >= '0' && char <= '9';
|
|
158
|
-
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return true;
|
|
163
|
-
};
|
|
164
|
-
const isHex = (str) => {
|
|
165
|
-
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
for (let i = 0; i < str.length; i++) {
|
|
169
|
-
const char = str[i];
|
|
170
|
-
const isInNibbleRange = char >= '0' && char <= '9';
|
|
171
|
-
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
|
172
|
-
return false;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return true;
|
|
176
|
-
};
|
|
177
|
-
const writeString = (str) => {
|
|
178
|
-
if (str === undefined || str === null) {
|
|
179
|
-
pushByte(TAGS.LIST_EMPTY);
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
const tokenIndex = TOKEN_MAP[str];
|
|
183
|
-
if (tokenIndex) {
|
|
184
|
-
if (typeof tokenIndex.dict === 'number') {
|
|
185
|
-
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
186
|
-
}
|
|
187
|
-
pushByte(tokenIndex.index);
|
|
188
|
-
}
|
|
189
|
-
else if (isNibble(str)) {
|
|
190
|
-
writePackedBytes(str, 'nibble');
|
|
191
|
-
}
|
|
192
|
-
else if (isHex(str)) {
|
|
193
|
-
writePackedBytes(str, 'hex');
|
|
194
|
-
}
|
|
195
|
-
else if (str) {
|
|
196
|
-
const decodedJid = (0, jid_utils_1.jidDecode)(str);
|
|
197
|
-
if (decodedJid) {
|
|
198
|
-
writeJid(decodedJid);
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
writeStringRaw(str);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
const writeListStart = (listSize) => {
|
|
206
|
-
if (listSize === 0) {
|
|
207
|
-
pushByte(TAGS.LIST_EMPTY);
|
|
208
|
-
}
|
|
209
|
-
else if (listSize < 256) {
|
|
210
|
-
pushBytes([TAGS.LIST_8, listSize]);
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
pushByte(TAGS.LIST_16);
|
|
214
|
-
pushInt16(listSize);
|
|
215
|
-
}
|
|
22
|
+
const { TAGS, TOKEN_MAP } = opts;
|
|
23
|
+
const pushByte = (value) => buffer.push(value & 0xff);
|
|
24
|
+
const pushInt = (value, n, littleEndian = false) => {
|
|
25
|
+
for (let i = 0; i < n; i++) {
|
|
26
|
+
const curShift = littleEndian ? i : n - 1 - i;
|
|
27
|
+
buffer.push((value >> (curShift * 8)) & 0xff);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const pushBytes = (bytes) => {
|
|
31
|
+
for (const b of bytes) {
|
|
32
|
+
buffer.push(b);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const pushInt16 = (value) => {
|
|
36
|
+
pushBytes([(value >> 8) & 0xff, value & 0xff]);
|
|
37
|
+
};
|
|
38
|
+
const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
|
|
39
|
+
const writeByteLength = (length) => {
|
|
40
|
+
if (length >= 4294967296) {
|
|
41
|
+
throw new Error("string muito grande para codificar: " + length);
|
|
42
|
+
}
|
|
43
|
+
if (length >= 1 << 20) {
|
|
44
|
+
pushByte(TAGS.BINARY_32);
|
|
45
|
+
pushInt(length, 4);
|
|
46
|
+
}
|
|
47
|
+
else if (length >= 256) {
|
|
48
|
+
pushByte(TAGS.BINARY_20);
|
|
49
|
+
pushInt20(length);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
pushByte(TAGS.BINARY_8);
|
|
53
|
+
pushByte(length);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const writeStringRaw = (str) => {
|
|
57
|
+
const bytes = Buffer.from(str, "utf-8");
|
|
58
|
+
writeByteLength(bytes.length);
|
|
59
|
+
pushBytes(bytes);
|
|
60
|
+
};
|
|
61
|
+
const writeJid = ({ domainType, device, user, server }) => {
|
|
62
|
+
if (typeof device !== "undefined") {
|
|
63
|
+
pushByte(TAGS.AD_JID);
|
|
64
|
+
pushByte(domainType || 0);
|
|
65
|
+
pushByte(device || 0);
|
|
66
|
+
writeString(user);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
pushByte(TAGS.JID_PAIR);
|
|
70
|
+
if (user.length) {
|
|
71
|
+
writeString(user);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
75
|
+
}
|
|
76
|
+
writeString(server);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const packNibble = (char) => {
|
|
80
|
+
switch (char) {
|
|
81
|
+
case "-":
|
|
82
|
+
return 10;
|
|
83
|
+
case ".":
|
|
84
|
+
return 11;
|
|
85
|
+
case "\0":
|
|
86
|
+
return 15;
|
|
87
|
+
default:
|
|
88
|
+
if (char >= "0" && char <= "9") {
|
|
89
|
+
return char.charCodeAt(0) - "0".charCodeAt(0);
|
|
90
|
+
}
|
|
91
|
+
throw new Error(`byte inválido para mordidela "${char}"`);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const packHex = (char) => {
|
|
95
|
+
if (char >= "0" && char <= "9") {
|
|
96
|
+
return char.charCodeAt(0) - "0".charCodeAt(0);
|
|
97
|
+
}
|
|
98
|
+
if (char >= "A" && char <= "F") {
|
|
99
|
+
return 10 + char.charCodeAt(0) - "A".charCodeAt(0);
|
|
100
|
+
}
|
|
101
|
+
if (char >= "a" && char <= "f") {
|
|
102
|
+
return 10 + char.charCodeAt(0) - "a".charCodeAt(0);
|
|
103
|
+
}
|
|
104
|
+
if (char === "\0") {
|
|
105
|
+
return 15;
|
|
106
|
+
}
|
|
107
|
+
throw new Error(`Caractere hexadecimal inválido "${char}"`);
|
|
108
|
+
};
|
|
109
|
+
const writePackedBytes = (str, type) => {
|
|
110
|
+
if (str.length > TAGS.PACKED_MAX) {
|
|
111
|
+
throw new Error("Muitos bytes para empacotar");
|
|
112
|
+
}
|
|
113
|
+
pushByte(type === "nibble" ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
|
114
|
+
let roundedLength = Math.ceil(str.length / 2.0);
|
|
115
|
+
if (str.length % 2 !== 0) {
|
|
116
|
+
roundedLength |= 128;
|
|
117
|
+
}
|
|
118
|
+
pushByte(roundedLength);
|
|
119
|
+
const packFunction = type === "nibble" ? packNibble : packHex;
|
|
120
|
+
const packBytePair = (v1, v2) => {
|
|
121
|
+
const result = (packFunction(v1) << 4) | packFunction(v2);
|
|
122
|
+
return result;
|
|
216
123
|
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
const validAttributes = Object.keys(attrs || {}).filter(k => (typeof attrs[k] !== 'undefined' && attrs[k] !== null));
|
|
221
|
-
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0));
|
|
222
|
-
writeString(tag);
|
|
223
|
-
for (const key of validAttributes) {
|
|
224
|
-
if (typeof attrs[key] === 'string') {
|
|
225
|
-
writeString(key);
|
|
226
|
-
writeString(attrs[key]);
|
|
227
|
-
}
|
|
124
|
+
const strLengthHalf = Math.floor(str.length / 2);
|
|
125
|
+
for (let i = 0; i < strLengthHalf; i++) {
|
|
126
|
+
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
|
228
127
|
}
|
|
229
|
-
if (
|
|
230
|
-
|
|
128
|
+
if (str.length % 2 !== 0) {
|
|
129
|
+
pushByte(packBytePair(str[str.length - 1], "\x00"));
|
|
231
130
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
131
|
+
};
|
|
132
|
+
const isNibble = (str) => {
|
|
133
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
134
|
+
return false;
|
|
235
135
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
136
|
+
for (const char of str) {
|
|
137
|
+
const isInNibbleRange = char >= "0" && char <= "9";
|
|
138
|
+
if (!isInNibbleRange && char !== "-" && char !== ".") {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
};
|
|
144
|
+
const isHex = (str) => {
|
|
145
|
+
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
for (const char of str) {
|
|
149
|
+
const isInNibbleRange = char >= "0" && char <= "9";
|
|
150
|
+
if (!isInNibbleRange && !(char >= "A" && char <= "F")) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
242
153
|
}
|
|
243
|
-
|
|
244
|
-
|
|
154
|
+
return true;
|
|
155
|
+
};
|
|
156
|
+
const writeString = (str) => {
|
|
157
|
+
if (str === undefined || str === null) {
|
|
158
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const tokenIndex = TOKEN_MAP[str];
|
|
162
|
+
if (tokenIndex) {
|
|
163
|
+
if (typeof tokenIndex.dict === "number") {
|
|
164
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
165
|
+
}
|
|
166
|
+
pushByte(tokenIndex.index);
|
|
167
|
+
}
|
|
168
|
+
else if (isNibble(str)) {
|
|
169
|
+
writePackedBytes(str, "nibble");
|
|
170
|
+
}
|
|
171
|
+
else if (isHex(str)) {
|
|
172
|
+
writePackedBytes(str, "hex");
|
|
173
|
+
}
|
|
174
|
+
else if (str) {
|
|
175
|
+
const decodedJid = jidDecode(str);
|
|
176
|
+
if (decodedJid) {
|
|
177
|
+
writeJid(decodedJid);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
writeStringRaw(str);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const writeListStart = (listSize) => {
|
|
185
|
+
if (listSize === 0) {
|
|
186
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
187
|
+
}
|
|
188
|
+
else if (listSize < 256) {
|
|
189
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
|
245
190
|
}
|
|
246
191
|
else {
|
|
247
|
-
|
|
192
|
+
pushByte(TAGS.LIST_16);
|
|
193
|
+
pushInt16(listSize);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
if (!tag) {
|
|
197
|
+
throw new Error("Nó inválido: a tag não pode ser indefinida");
|
|
198
|
+
}
|
|
199
|
+
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== "undefined" && attrs[k] !== null);
|
|
200
|
+
writeListStart(2 * validAttributes.length + 1 + (typeof content !== "undefined" ? 1 : 0));
|
|
201
|
+
writeString(tag);
|
|
202
|
+
for (const key of validAttributes) {
|
|
203
|
+
if (typeof attrs[key] === "string") {
|
|
204
|
+
writeString(key);
|
|
205
|
+
writeString(attrs[key]);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (typeof content === "string") {
|
|
209
|
+
writeString(content);
|
|
210
|
+
}
|
|
211
|
+
else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
212
|
+
writeByteLength(content.length);
|
|
213
|
+
pushBytes(content);
|
|
214
|
+
}
|
|
215
|
+
else if (Array.isArray(content)) {
|
|
216
|
+
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === "string"));
|
|
217
|
+
writeListStart(validContent.length);
|
|
218
|
+
for (const item of validContent) {
|
|
219
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
|
248
220
|
}
|
|
249
|
-
|
|
221
|
+
}
|
|
222
|
+
else if (typeof content === "undefined") {
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
throw new Error(`filhos inválidos para cabeçalho "${tag}": ${content} (${typeof content})`);
|
|
226
|
+
}
|
|
227
|
+
return buffer;
|
|
250
228
|
};
|