Path: blob/master/node_modules/@adiwajshing/baileys/lib/WABinary/generic-utils.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildString = 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 = (node, childTag) => {7if (Array.isArray(node === null || node === void 0 ? void 0 : node.content)) {8return node.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 = (node, childTag) => {21if (Array.isArray(node === null || node === void 0 ? void 0 : node.content)) {22return node === null || node === void 0 ? void 0 : node.content.find(item => item.tag === childTag);23}24};25exports.getBinaryNodeChild = getBinaryNodeChild;26const getBinaryNodeChildBuffer = (node, childTag) => {27var _a;28const child = (_a = (0, 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 getBinaryNodeChildString = (node, childTag) => {35var _a;36const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content;37if (Buffer.isBuffer(child) || child instanceof Uint8Array) {38return Buffer.from(child).toString('utf-8');39}40else if (typeof child === 'string') {41return child;42}43};44exports.getBinaryNodeChildString = getBinaryNodeChildString;45const getBinaryNodeChildUInt = (node, childTag, length) => {46const buff = (0, exports.getBinaryNodeChildBuffer)(node, childTag);47if (buff) {48return bufferToUInt(buff, length);49}50};51exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;52const assertNodeErrorFree = (node) => {53const errNode = (0, exports.getBinaryNodeChild)(node, 'error');54if (errNode) {55throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });56}57};58exports.assertNodeErrorFree = assertNodeErrorFree;59const reduceBinaryNodeToDictionary = (node, tag) => {60const nodes = (0, exports.getBinaryNodeChildren)(node, tag);61const dict = nodes.reduce((dict, { attrs }) => {62dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value;63return dict;64}, {});65return dict;66};67exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary;68const getBinaryNodeMessages = ({ content }) => {69const msgs = [];70if (Array.isArray(content)) {71for (const item of content) {72if (item.tag === 'message') {73msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content));74}75}76}77return msgs;78};79exports.getBinaryNodeMessages = getBinaryNodeMessages;80function bufferToUInt(e, t) {81let a = 0;82for (let i = 0; i < t; i++) {83a = 256 * a + e[i];84}85return a;86}878889