violetics 7.0.0-alpha → 7.0.2-alpha
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 +3 -2
- package/README.md +1001 -232
- package/WAProto/index.js +75379 -142631
- package/engine-requirements.js +11 -8
- package/lib/Defaults/index.js +132 -144
- package/lib/Signal/Group/ciphertext-message.js +2 -6
- package/lib/Signal/Group/group-session-builder.js +7 -42
- package/lib/Signal/Group/group_cipher.js +37 -52
- package/lib/Signal/Group/index.js +11 -57
- package/lib/Signal/Group/keyhelper.js +7 -45
- package/lib/Signal/Group/sender-chain-key.js +7 -16
- package/lib/Signal/Group/sender-key-distribution-message.js +8 -12
- package/lib/Signal/Group/sender-key-message.js +9 -13
- package/lib/Signal/Group/sender-key-name.js +2 -6
- package/lib/Signal/Group/sender-key-record.js +9 -22
- package/lib/Signal/Group/sender-key-state.js +27 -43
- package/lib/Signal/Group/sender-message-key.js +4 -8
- package/lib/Signal/libsignal.js +319 -94
- package/lib/Signal/lid-mapping.js +224 -139
- package/lib/Socket/Client/index.js +2 -19
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +162 -44
- package/lib/Socket/chats.js +477 -442
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +110 -99
- package/lib/Socket/index.js +10 -10
- package/lib/Socket/messages-recv.js +878 -552
- package/lib/Socket/messages-send.js +859 -428
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +195 -390
- package/lib/Socket/socket.js +463 -289
- package/lib/Store/index.js +3 -10
- package/lib/Store/make-in-memory-store.js +73 -79
- package/lib/Store/make-ordered-dictionary.js +4 -7
- package/lib/Store/object-repository.js +2 -6
- package/lib/Types/Auth.js +1 -2
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -2
- package/lib/Types/Chat.js +7 -4
- package/lib/Types/Contact.js +1 -2
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +1 -2
- package/lib/Types/Label.js +2 -5
- package/lib/Types/LabelAssociation.js +2 -5
- package/lib/Types/Message.js +17 -9
- package/lib/Types/Newsletter.js +33 -38
- package/lib/Types/Product.js +1 -2
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -2
- package/lib/Types/State.js +12 -2
- package/lib/Types/USync.js +1 -2
- package/lib/Types/index.js +14 -31
- package/lib/Utils/auth-utils.js +228 -145
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +66 -70
- package/lib/Utils/chat-utils.js +331 -249
- package/lib/Utils/crypto.js +57 -91
- package/lib/Utils/decode-wa-message.js +168 -84
- package/lib/Utils/event-buffer.js +138 -80
- package/lib/Utils/generics.js +180 -297
- package/lib/Utils/history.js +83 -49
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +19 -33
- package/lib/Utils/link-preview.js +14 -23
- package/lib/Utils/logger.js +2 -7
- package/lib/Utils/lt-hash.js +2 -46
- package/lib/Utils/make-mutex.js +24 -35
- package/lib/Utils/message-retry-manager.js +224 -0
- package/lib/Utils/messages-media.js +501 -496
- package/lib/Utils/messages.js +1428 -362
- package/lib/Utils/noise-handler.js +145 -100
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +356 -150
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/signal.js +78 -73
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +32 -34
- package/lib/Utils/validate-connection.js +91 -107
- package/lib/WABinary/constants.js +1300 -1304
- package/lib/WABinary/decode.js +26 -48
- package/lib/WABinary/encode.js +109 -155
- package/lib/WABinary/generic-utils.js +161 -149
- package/lib/WABinary/index.js +5 -21
- package/lib/WABinary/jid-utils.js +73 -40
- package/lib/WABinary/types.js +1 -2
- package/lib/WAM/BinaryInfo.js +2 -6
- package/lib/WAM/constants.js +19070 -11568
- package/lib/WAM/encode.js +17 -23
- package/lib/WAM/index.js +3 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -12
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -15
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -14
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -23
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -9
- package/lib/WAUSync/Protocols/index.js +4 -20
- package/lib/WAUSync/USyncQuery.js +40 -36
- package/lib/WAUSync/USyncUser.js +2 -6
- package/lib/WAUSync/index.js +3 -19
- package/lib/index.js +11 -44
- package/package.json +75 -108
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Signal/Group/queue-job.js +0 -57
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -111
- package/lib/Socket/groupStatus.js +0 -637
- package/lib/Socket/registration.js +0 -166
- package/lib/Socket/usync.js +0 -70
- package/lib/Store/make-cache-manager-store.js +0 -83
- package/lib/Utils/baileys-event-stream.js +0 -63
package/lib/WABinary/decode.js
CHANGED
|
@@ -1,43 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.decodeBinaryNode = exports.decodeDecompressedBinaryNode = exports.decompressingIfRequired = void 0;
|
|
27
|
-
const zlib_1 = require("zlib");
|
|
28
|
-
const constants = __importStar(require("./constants"));
|
|
29
|
-
const jid_utils_1 = require("./jid-utils");
|
|
30
|
-
const decompressingIfRequired = (buffer) => {
|
|
1
|
+
import { promisify } from 'util';
|
|
2
|
+
import { inflate } from 'zlib';
|
|
3
|
+
import * as constants from './constants.js';
|
|
4
|
+
import { jidEncode, WAJIDDomains } from './jid-utils.js';
|
|
5
|
+
const inflatePromise = promisify(inflate);
|
|
6
|
+
export const decompressingIfRequired = async (buffer) => {
|
|
31
7
|
if (2 & buffer.readUInt8()) {
|
|
32
|
-
buffer =
|
|
8
|
+
buffer = await inflatePromise(buffer.slice(1));
|
|
33
9
|
}
|
|
34
|
-
else {
|
|
10
|
+
else {
|
|
11
|
+
// nodes with no compression have a 0x00 prefix, we remove that
|
|
35
12
|
buffer = buffer.slice(1);
|
|
36
13
|
}
|
|
37
14
|
return buffer;
|
|
38
15
|
};
|
|
39
|
-
|
|
40
|
-
const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
|
|
16
|
+
export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
|
|
41
17
|
const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts;
|
|
42
18
|
const checkEOS = (length) => {
|
|
43
19
|
if (indexRef.index + length > buffer.length) {
|
|
@@ -144,18 +120,21 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
144
120
|
throw new Error('invalid jid pair: ' + i + ', ' + j);
|
|
145
121
|
};
|
|
146
122
|
const readAdJid = () => {
|
|
147
|
-
const
|
|
123
|
+
const rawDomainType = readByte();
|
|
124
|
+
const domainType = Number(rawDomainType);
|
|
148
125
|
const device = readByte();
|
|
149
126
|
const user = readString(readByte());
|
|
150
|
-
let server = 's.whatsapp.net';
|
|
151
|
-
if (domainType ===
|
|
127
|
+
let server = 's.whatsapp.net'; // default whatsapp server
|
|
128
|
+
if (domainType === WAJIDDomains.LID) {
|
|
152
129
|
server = 'lid';
|
|
153
|
-
}
|
|
130
|
+
}
|
|
131
|
+
else if (domainType === WAJIDDomains.HOSTED) {
|
|
154
132
|
server = 'hosted';
|
|
155
|
-
}
|
|
133
|
+
}
|
|
134
|
+
else if (domainType === WAJIDDomains.HOSTED_LID) {
|
|
156
135
|
server = 'hosted.lid';
|
|
157
136
|
}
|
|
158
|
-
return
|
|
137
|
+
return jidEncode(user, server, device);
|
|
159
138
|
};
|
|
160
139
|
const readFbJid = () => {
|
|
161
140
|
const user = readString(readByte());
|
|
@@ -171,7 +150,8 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
171
150
|
const beforeServer = indexRef.index;
|
|
172
151
|
try {
|
|
173
152
|
server = readString(readByte());
|
|
174
|
-
}
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
175
155
|
indexRef.index = beforeServer;
|
|
176
156
|
}
|
|
177
157
|
return `${integrator}-${user}:${device}@${server}`;
|
|
@@ -213,7 +193,7 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
213
193
|
const items = [];
|
|
214
194
|
const size = readListSize(tag);
|
|
215
195
|
for (let i = 0; i < size; i++) {
|
|
216
|
-
items.push(
|
|
196
|
+
items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef));
|
|
217
197
|
}
|
|
218
198
|
return items;
|
|
219
199
|
};
|
|
@@ -275,9 +255,7 @@ const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) =>
|
|
|
275
255
|
content: data
|
|
276
256
|
};
|
|
277
257
|
};
|
|
278
|
-
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
};
|
|
283
|
-
exports.decodeBinaryNode = decodeBinaryNode;
|
|
258
|
+
export const decodeBinaryNode = async (buff) => {
|
|
259
|
+
const decompBuff = await decompressingIfRequired(buff);
|
|
260
|
+
return decodeDecompressedBinaryNode(decompBuff, constants);
|
|
261
|
+
};
|
package/lib/WABinary/encode.js
CHANGED
|
@@ -1,265 +1,219 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
34
|
-
const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
35
|
-
const encoded = encodeBinaryNodeInner(node, opts, buffer)
|
|
36
|
-
return Buffer.from(encoded)
|
|
37
|
-
}
|
|
38
|
-
|
|
1
|
+
import * as constants from './constants.js';
|
|
2
|
+
import { jidDecode } from './jid-utils.js';
|
|
3
|
+
export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
4
|
+
const encoded = encodeBinaryNodeInner(node, opts, buffer);
|
|
5
|
+
return Buffer.from(encoded);
|
|
6
|
+
};
|
|
39
7
|
const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
40
|
-
const { TAGS, TOKEN_MAP } = opts
|
|
41
|
-
const pushByte = (value) => buffer.push(value & 0xff)
|
|
8
|
+
const { TAGS, TOKEN_MAP } = opts;
|
|
9
|
+
const pushByte = (value) => buffer.push(value & 0xff);
|
|
42
10
|
const pushInt = (value, n, littleEndian = false) => {
|
|
43
11
|
for (let i = 0; i < n; i++) {
|
|
44
|
-
const curShift = littleEndian ? i : n - 1 - i
|
|
45
|
-
buffer.push((value >> (curShift * 8)) & 0xff)
|
|
12
|
+
const curShift = littleEndian ? i : n - 1 - i;
|
|
13
|
+
buffer.push((value >> (curShift * 8)) & 0xff);
|
|
46
14
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
15
|
+
};
|
|
49
16
|
const pushBytes = (bytes) => {
|
|
50
17
|
for (const b of bytes) {
|
|
51
|
-
buffer.push(b)
|
|
18
|
+
buffer.push(b);
|
|
52
19
|
}
|
|
53
|
-
}
|
|
54
|
-
|
|
20
|
+
};
|
|
55
21
|
const pushInt16 = (value) => {
|
|
56
|
-
pushBytes([(value >> 8) & 0xff, value & 0xff])
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const pushInt20 = (value) => (pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]))
|
|
22
|
+
pushBytes([(value >> 8) & 0xff, value & 0xff]);
|
|
23
|
+
};
|
|
24
|
+
const pushInt20 = (value) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff]);
|
|
60
25
|
const writeByteLength = (length) => {
|
|
61
26
|
if (length >= 4294967296) {
|
|
62
|
-
throw new Error('string too large to encode: ' + length)
|
|
27
|
+
throw new Error('string too large to encode: ' + length);
|
|
63
28
|
}
|
|
64
29
|
if (length >= 1 << 20) {
|
|
65
|
-
pushByte(TAGS.BINARY_32)
|
|
66
|
-
pushInt(length, 4) // 32 bit integer
|
|
30
|
+
pushByte(TAGS.BINARY_32);
|
|
31
|
+
pushInt(length, 4); // 32 bit integer
|
|
67
32
|
}
|
|
68
33
|
else if (length >= 256) {
|
|
69
|
-
pushByte(TAGS.BINARY_20)
|
|
70
|
-
pushInt20(length)
|
|
34
|
+
pushByte(TAGS.BINARY_20);
|
|
35
|
+
pushInt20(length);
|
|
71
36
|
}
|
|
72
37
|
else {
|
|
73
|
-
pushByte(TAGS.BINARY_8)
|
|
74
|
-
pushByte(length)
|
|
38
|
+
pushByte(TAGS.BINARY_8);
|
|
39
|
+
pushByte(length);
|
|
75
40
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
41
|
+
};
|
|
78
42
|
const writeStringRaw = (str) => {
|
|
79
|
-
const bytes = Buffer.from(str, 'utf-8')
|
|
80
|
-
writeByteLength(bytes.length)
|
|
81
|
-
pushBytes(bytes)
|
|
82
|
-
}
|
|
83
|
-
|
|
43
|
+
const bytes = Buffer.from(str, 'utf-8');
|
|
44
|
+
writeByteLength(bytes.length);
|
|
45
|
+
pushBytes(bytes);
|
|
46
|
+
};
|
|
84
47
|
const writeJid = ({ domainType, device, user, server }) => {
|
|
85
48
|
if (typeof device !== 'undefined') {
|
|
86
|
-
pushByte(TAGS.AD_JID)
|
|
87
|
-
pushByte(domainType || 0)
|
|
88
|
-
pushByte(device || 0)
|
|
89
|
-
writeString(user)
|
|
49
|
+
pushByte(TAGS.AD_JID);
|
|
50
|
+
pushByte(domainType || 0);
|
|
51
|
+
pushByte(device || 0);
|
|
52
|
+
writeString(user);
|
|
90
53
|
}
|
|
91
54
|
else {
|
|
92
|
-
pushByte(TAGS.JID_PAIR)
|
|
55
|
+
pushByte(TAGS.JID_PAIR);
|
|
93
56
|
if (user.length) {
|
|
94
|
-
writeString(user)
|
|
57
|
+
writeString(user);
|
|
95
58
|
}
|
|
96
59
|
else {
|
|
97
|
-
pushByte(TAGS.LIST_EMPTY)
|
|
60
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
98
61
|
}
|
|
99
|
-
writeString(server)
|
|
62
|
+
writeString(server);
|
|
100
63
|
}
|
|
101
|
-
}
|
|
102
|
-
|
|
64
|
+
};
|
|
103
65
|
const packNibble = (char) => {
|
|
104
66
|
switch (char) {
|
|
105
67
|
case '-':
|
|
106
|
-
return 10
|
|
68
|
+
return 10;
|
|
107
69
|
case '.':
|
|
108
|
-
return 11
|
|
70
|
+
return 11;
|
|
109
71
|
case '\0':
|
|
110
|
-
return 15
|
|
72
|
+
return 15;
|
|
111
73
|
default:
|
|
112
74
|
if (char >= '0' && char <= '9') {
|
|
113
|
-
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
|
75
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
|
114
76
|
}
|
|
115
|
-
throw new Error(`invalid byte for nibble "${char}"`)
|
|
77
|
+
throw new Error(`invalid byte for nibble "${char}"`);
|
|
116
78
|
}
|
|
117
|
-
}
|
|
118
|
-
|
|
79
|
+
};
|
|
119
80
|
const packHex = (char) => {
|
|
120
81
|
if (char >= '0' && char <= '9') {
|
|
121
|
-
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
|
82
|
+
return char.charCodeAt(0) - '0'.charCodeAt(0);
|
|
122
83
|
}
|
|
123
84
|
if (char >= 'A' && char <= 'F') {
|
|
124
|
-
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
|
|
85
|
+
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0);
|
|
125
86
|
}
|
|
126
87
|
if (char >= 'a' && char <= 'f') {
|
|
127
|
-
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
|
|
88
|
+
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0);
|
|
128
89
|
}
|
|
129
90
|
if (char === '\0') {
|
|
130
|
-
return 15
|
|
91
|
+
return 15;
|
|
131
92
|
}
|
|
132
|
-
throw new Error(`Invalid hex char "${char}"`)
|
|
133
|
-
}
|
|
134
|
-
|
|
93
|
+
throw new Error(`Invalid hex char "${char}"`);
|
|
94
|
+
};
|
|
135
95
|
const writePackedBytes = (str, type) => {
|
|
136
96
|
if (str.length > TAGS.PACKED_MAX) {
|
|
137
|
-
throw new Error('Too many bytes to pack')
|
|
97
|
+
throw new Error('Too many bytes to pack');
|
|
138
98
|
}
|
|
139
|
-
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
|
|
140
|
-
let roundedLength = Math.ceil(str.length / 2.0)
|
|
99
|
+
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8);
|
|
100
|
+
let roundedLength = Math.ceil(str.length / 2.0);
|
|
141
101
|
if (str.length % 2 !== 0) {
|
|
142
|
-
roundedLength |= 128
|
|
102
|
+
roundedLength |= 128;
|
|
143
103
|
}
|
|
144
|
-
pushByte(roundedLength)
|
|
145
|
-
const packFunction = type === 'nibble' ? packNibble : packHex
|
|
104
|
+
pushByte(roundedLength);
|
|
105
|
+
const packFunction = type === 'nibble' ? packNibble : packHex;
|
|
146
106
|
const packBytePair = (v1, v2) => {
|
|
147
|
-
const result = (packFunction(v1) << 4) | packFunction(v2)
|
|
148
|
-
return result
|
|
149
|
-
}
|
|
150
|
-
const strLengthHalf = Math.floor(str.length / 2)
|
|
107
|
+
const result = (packFunction(v1) << 4) | packFunction(v2);
|
|
108
|
+
return result;
|
|
109
|
+
};
|
|
110
|
+
const strLengthHalf = Math.floor(str.length / 2);
|
|
151
111
|
for (let i = 0; i < strLengthHalf; i++) {
|
|
152
|
-
pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
|
|
112
|
+
pushByte(packBytePair(str[2 * i], str[2 * i + 1]));
|
|
153
113
|
}
|
|
154
114
|
if (str.length % 2 !== 0) {
|
|
155
|
-
pushByte(packBytePair(str[str.length - 1], '\x00'))
|
|
115
|
+
pushByte(packBytePair(str[str.length - 1], '\x00'));
|
|
156
116
|
}
|
|
157
|
-
}
|
|
158
|
-
|
|
117
|
+
};
|
|
159
118
|
const isNibble = (str) => {
|
|
160
119
|
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
161
|
-
return false
|
|
120
|
+
return false;
|
|
162
121
|
}
|
|
163
122
|
for (const char of str) {
|
|
164
|
-
const isInNibbleRange = char >= '0' && char <= '9'
|
|
123
|
+
const isInNibbleRange = char >= '0' && char <= '9';
|
|
165
124
|
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
|
166
|
-
return false
|
|
125
|
+
return false;
|
|
167
126
|
}
|
|
168
127
|
}
|
|
169
|
-
return true
|
|
170
|
-
}
|
|
171
|
-
|
|
128
|
+
return true;
|
|
129
|
+
};
|
|
172
130
|
const isHex = (str) => {
|
|
173
131
|
if (!str || str.length > TAGS.PACKED_MAX) {
|
|
174
|
-
return false
|
|
132
|
+
return false;
|
|
175
133
|
}
|
|
176
134
|
for (const char of str) {
|
|
177
|
-
const isInNibbleRange = char >= '0' && char <= '9'
|
|
135
|
+
const isInNibbleRange = char >= '0' && char <= '9';
|
|
178
136
|
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
|
179
|
-
return false
|
|
137
|
+
return false;
|
|
180
138
|
}
|
|
181
139
|
}
|
|
182
|
-
return true
|
|
183
|
-
}
|
|
184
|
-
|
|
140
|
+
return true;
|
|
141
|
+
};
|
|
185
142
|
const writeString = (str) => {
|
|
186
|
-
|
|
187
|
-
pushByte(TAGS.LIST_EMPTY)
|
|
188
|
-
return
|
|
143
|
+
if (str === undefined || str === null) {
|
|
144
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
145
|
+
return;
|
|
189
146
|
}
|
|
190
|
-
|
|
147
|
+
if (str === '') {
|
|
148
|
+
writeStringRaw(str);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const tokenIndex = TOKEN_MAP[str];
|
|
191
152
|
if (tokenIndex) {
|
|
192
153
|
if (typeof tokenIndex.dict === 'number') {
|
|
193
|
-
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
|
|
154
|
+
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
|
|
194
155
|
}
|
|
195
|
-
pushByte(tokenIndex.index)
|
|
156
|
+
pushByte(tokenIndex.index);
|
|
196
157
|
}
|
|
197
158
|
else if (isNibble(str)) {
|
|
198
|
-
writePackedBytes(str, 'nibble')
|
|
159
|
+
writePackedBytes(str, 'nibble');
|
|
199
160
|
}
|
|
200
161
|
else if (isHex(str)) {
|
|
201
|
-
writePackedBytes(str, 'hex')
|
|
162
|
+
writePackedBytes(str, 'hex');
|
|
202
163
|
}
|
|
203
|
-
else
|
|
204
|
-
const decodedJid =
|
|
164
|
+
else {
|
|
165
|
+
const decodedJid = jidDecode(str);
|
|
205
166
|
if (decodedJid) {
|
|
206
|
-
writeJid(decodedJid)
|
|
167
|
+
writeJid(decodedJid);
|
|
207
168
|
}
|
|
208
169
|
else {
|
|
209
|
-
writeStringRaw(str)
|
|
170
|
+
writeStringRaw(str);
|
|
210
171
|
}
|
|
211
172
|
}
|
|
212
|
-
}
|
|
213
|
-
|
|
173
|
+
};
|
|
214
174
|
const writeListStart = (listSize) => {
|
|
215
175
|
if (listSize === 0) {
|
|
216
|
-
pushByte(TAGS.LIST_EMPTY)
|
|
176
|
+
pushByte(TAGS.LIST_EMPTY);
|
|
217
177
|
}
|
|
218
178
|
else if (listSize < 256) {
|
|
219
|
-
pushBytes([TAGS.LIST_8, listSize])
|
|
179
|
+
pushBytes([TAGS.LIST_8, listSize]);
|
|
220
180
|
}
|
|
221
181
|
else {
|
|
222
|
-
pushByte(TAGS.LIST_16)
|
|
223
|
-
pushInt16(listSize)
|
|
182
|
+
pushByte(TAGS.LIST_16);
|
|
183
|
+
pushInt16(listSize);
|
|
224
184
|
}
|
|
225
|
-
}
|
|
226
|
-
|
|
185
|
+
};
|
|
227
186
|
if (!tag) {
|
|
228
|
-
throw new Error('Invalid node: tag cannot be undefined')
|
|
187
|
+
throw new Error('Invalid node: tag cannot be undefined');
|
|
229
188
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
writeString(tag)
|
|
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);
|
|
234
192
|
for (const key of validAttributes) {
|
|
235
193
|
if (typeof attrs[key] === 'string') {
|
|
236
|
-
writeString(key)
|
|
237
|
-
writeString(attrs[key])
|
|
194
|
+
writeString(key);
|
|
195
|
+
writeString(attrs[key]);
|
|
238
196
|
}
|
|
239
197
|
}
|
|
240
198
|
if (typeof content === 'string') {
|
|
241
|
-
writeString(content)
|
|
199
|
+
writeString(content);
|
|
242
200
|
}
|
|
243
201
|
else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
244
|
-
writeByteLength(content.length)
|
|
245
|
-
pushBytes(content)
|
|
202
|
+
writeByteLength(content.length);
|
|
203
|
+
pushBytes(content);
|
|
246
204
|
}
|
|
247
205
|
else if (Array.isArray(content)) {
|
|
248
|
-
|
|
249
|
-
writeListStart(validContent.length)
|
|
206
|
+
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'));
|
|
207
|
+
writeListStart(validContent.length);
|
|
250
208
|
for (const item of validContent) {
|
|
251
|
-
encodeBinaryNodeInner(item, opts, buffer)
|
|
209
|
+
encodeBinaryNodeInner(item, opts, buffer);
|
|
252
210
|
}
|
|
253
211
|
}
|
|
254
212
|
else if (typeof content === 'undefined') {
|
|
255
213
|
// do nothing
|
|
256
214
|
}
|
|
257
215
|
else {
|
|
258
|
-
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
|
|
216
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`);
|
|
259
217
|
}
|
|
260
|
-
return buffer
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
module.exports = {
|
|
264
|
-
encodeBinaryNode
|
|
265
|
-
}
|
|
218
|
+
return buffer;
|
|
219
|
+
};
|