Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/generic-utils.js
2593 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = 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 = ({ content }, childTag) => {
8
if (Array.isArray(content)) {
9
return 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 = ({ content }, childTag) => {
22
if (Array.isArray(content)) {
23
return content.find(item => item.tag === childTag);
24
}
25
};
26
exports.getBinaryNodeChild = getBinaryNodeChild;
27
const getBinaryNodeChildBuffer = (node, childTag) => {
28
var _a;
29
const child = (_a = 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 getBinaryNodeChildUInt = (node, childTag, length) => {
36
const buff = exports.getBinaryNodeChildBuffer(node, childTag);
37
if (buff) {
38
return bufferToUInt(buff, length);
39
}
40
};
41
exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;
42
const assertNodeErrorFree = (node) => {
43
const errNode = exports.getBinaryNodeChild(node, 'error');
44
if (errNode) {
45
throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
46
}
47
};
48
exports.assertNodeErrorFree = assertNodeErrorFree;
49
const reduceBinaryNodeToDictionary = (node, tag) => {
50
const nodes = exports.getBinaryNodeChildren(node, tag);
51
const dict = nodes.reduce((dict, { attrs }) => {
52
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;
53
return dict;
54
}, {});
55
return dict;
56
};
57
exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;
58
const getBinaryNodeMessages = ({ content }) => {
59
const msgs = [];
60
if (Array.isArray(content)) {
61
for (const item of content) {
62
if (item.tag === 'message') {
63
msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));
64
}
65
}
66
}
67
return msgs;
68
};
69
exports.getBinaryNodeMessages = getBinaryNodeMessages;
70
function bufferToUInt(e, t) {
71
let a = 0;
72
for (let i = 0; i < t; i++) {
73
a = 256 * a + e[i];
74
}
75
return a;
76
}
77
78