Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/generic-utils.js
2593 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildBuffer = exports.getBinaryNodeChild = exports.getAllBinaryNodeChildren = exports.getBinaryNodeChildren = void 0;3const boom_1 = require("@hapi/boom");4const WAProto_1 = require("../../WAProto");5// some extra useful utilities6const getBinaryNodeChildren = ({ content }, childTag) => {7if (Array.isArray(content)) {8return content.filter(item => item.tag === childTag);9}10return [];11};12exports.getBinaryNodeChildren = getBinaryNodeChildren;13const getAllBinaryNodeChildren = ({ content }) => {14if (Array.isArray(content)) {15return content;16}17return [];18};19exports.getAllBinaryNodeChildren = getAllBinaryNodeChildren;20const getBinaryNodeChild = ({ content }, childTag) => {21if (Array.isArray(content)) {22return content.find(item => item.tag === childTag);23}24};25exports.getBinaryNodeChild = getBinaryNodeChild;26const getBinaryNodeChildBuffer = (node, childTag) => {27var _a;28const child = (_a = exports.getBinaryNodeChild(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;29if (Buffer.isBuffer(child) || child instanceof Uint8Array) {30return child;31}32};33exports.getBinaryNodeChildBuffer = getBinaryNodeChildBuffer;34const getBinaryNodeChildUInt = (node, childTag, length) => {35const buff = exports.getBinaryNodeChildBuffer(node, childTag);36if (buff) {37return bufferToUInt(buff, length);38}39};40exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;41const assertNodeErrorFree = (node) => {42const errNode = exports.getBinaryNodeChild(node, 'error');43if (errNode) {44throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });45}46};47exports.assertNodeErrorFree = assertNodeErrorFree;48const reduceBinaryNodeToDictionary = (node, tag) => {49const nodes = exports.getBinaryNodeChildren(node, tag);50const dict = nodes.reduce((dict, { attrs }) => {51dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;52return dict;53}, {});54return dict;55};56exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;57const getBinaryNodeMessages = ({ content }) => {58const msgs = [];59if (Array.isArray(content)) {60for (const item of content) {61if (item.tag === 'message') {62msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));63}64}65}66return msgs;67};68exports.getBinaryNodeMessages = getBinaryNodeMessages;69function bufferToUInt(e, t) {70let a = 0;71for (let i = 0; i < t; i++) {72a = 256 * a + e[i];73}74return a;75}767778