Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/generic-utils.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildString = exports.getBinaryNodeChildBuffer = exports.getBinaryNodeChild = exports.getAllBinaryNodeChildren = exports.getBinaryNodeChildren = void 0;
4
const boom_1 = require("@hapi/boom");
5
const WAProto_1 = require("../../WAProto");
6
// some extra useful utilities
7
const getBinaryNodeChildren = (node, childTag) => {
8
if (Array.isArray(node === null || node === void 0 ? void 0 : node.content)) {
9
return node.content.filter(item => item.tag === childTag);
10
}
11
return [];
12
};
13
exports.getBinaryNodeChildren = getBinaryNodeChildren;
14
const getAllBinaryNodeChildren = ({ content }) => {
15
if (Array.isArray(content)) {
16
return content;
17
}
18
return [];
19
};
20
exports.getAllBinaryNodeChildren = getAllBinaryNodeChildren;
21
const getBinaryNodeChild = (node, childTag) => {
22
if (Array.isArray(node === null || node === void 0 ? void 0 : node.content)) {
23
return node === null || node === void 0 ? void 0 : node.content.find(item => item.tag === childTag);
24
}
25
};
26
exports.getBinaryNodeChild = getBinaryNodeChild;
27
const getBinaryNodeChildBuffer = (node, childTag) => {
28
var _a;
29
const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;
30
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
31
return child;
32
}
33
};
34
exports.getBinaryNodeChildBuffer = getBinaryNodeChildBuffer;
35
const getBinaryNodeChildString = (node, childTag) => {
36
var _a;
37
const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;
38
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
39
return Buffer.from(child).toString('utf-8');
40
}
41
else if (typeof child === 'string') {
42
return child;
43
}
44
};
45
exports.getBinaryNodeChildString = getBinaryNodeChildString;
46
const getBinaryNodeChildUInt = (node, childTag, length) => {
47
const buff = (0, exports.getBinaryNodeChildBuffer)(node, childTag);
48
if (buff) {
49
return bufferToUInt(buff, length);
50
}
51
};
52
exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;
53
const assertNodeErrorFree = (node) => {
54
const errNode = (0, exports.getBinaryNodeChild)(node, 'error');
55
if (errNode) {
56
throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
57
}
58
};
59
exports.assertNodeErrorFree = assertNodeErrorFree;
60
const reduceBinaryNodeToDictionary = (node, tag) => {
61
const nodes = (0, exports.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
exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;
69
const getBinaryNodeMessages = ({ content }) => {
70
const msgs = [];
71
if (Array.isArray(content)) {
72
for (const item of content) {
73
if (item.tag === 'message') {
74
msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));
75
}
76
}
77
}
78
return msgs;
79
};
80
exports.getBinaryNodeMessages = getBinaryNodeMessages;
81
function bufferToUInt(e, t) {
82
let a = 0;
83
for (let i = 0; i < t; i++) {
84
a = 256 * a + e[i];
85
}
86
return a;
87
}
88
89