UNPKG

anju-xpro-baileys

Version:
126 lines (125 loc) 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.binaryNodeToString = exports.getBinaryNodeMessages = exports.reduceBinaryNodeToDictionary = exports.assertNodeErrorFree = exports.getBinaryNodeChildUInt = exports.getBinaryNodeChildString = exports.getBinaryNodeChildBuffer = exports.getBinaryNodeChild = exports.getAllBinaryNodeChildren = exports.getBinaryNodeChildren = void 0; const boom_1 = require("@hapi/boom"); const WAProto_1 = require("../../WAProto"); // some extra useful utilities const getBinaryNodeChildren = (node, childTag) => { if (!node || !Array.isArray(node.content)) { return []; } return node.content.filter((item) => item.tag === childTag); }; exports.getBinaryNodeChildren = getBinaryNodeChildren; const getAllBinaryNodeChildren = ({ content }) => { return Array.isArray(content) ? content : []; }; exports.getAllBinaryNodeChildren = getAllBinaryNodeChildren; const getBinaryNodeChild = (node, childTag) => { if (!node || !Array.isArray(node.content)) { return undefined; } return node.content.find(item => item.tag === childTag); }; exports.getBinaryNodeChild = getBinaryNodeChild; const getBinaryNodeChildBuffer = (node, childTag) => { var _a; const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content; if (Buffer.isBuffer(child) || child instanceof Uint8Array) { return child; } return undefined; }; exports.getBinaryNodeChildBuffer = getBinaryNodeChildBuffer; const getBinaryNodeChildString = (node, childTag) => { var _a; const child = (_a = (0, exports.getBinaryNodeChild)(node, childTag)) === null || _a === void 0 ? void 0 : _a.content; if (Buffer.isBuffer(child) || child instanceof Uint8Array) { return Buffer.from(child).toString('utf-8'); } else if (typeof child === 'string') { return child; } return undefined; }; exports.getBinaryNodeChildString = getBinaryNodeChildString; const getBinaryNodeChildUInt = (node, childTag, length) => { const buff = (0, exports.getBinaryNodeChildBuffer)(node, childTag); if (buff) { return bufferToUInt(buff, length); } return undefined; }; exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt; const assertNodeErrorFree = (node) => { var _a, _b; const errNode = (0, exports.getBinaryNodeChild)(node, 'error'); if (errNode) { throw new boom_1.Boom(((_a = errNode.attrs) === null || _a === void 0 ? void 0 : _a.text) || 'Unknown error', { data: Number((_b = errNode.attrs) === null || _b === void 0 ? void 0 : _b.code) || 0 }); } }; exports.assertNodeErrorFree = assertNodeErrorFree; const reduceBinaryNodeToDictionary = (node, tag) => { const nodes = (0, exports.getBinaryNodeChildren)(node, tag); return nodes.reduce((dict, { attrs }) => { const name = (attrs === null || attrs === void 0 ? void 0 : attrs.name) || (attrs === null || attrs === void 0 ? void 0 : attrs.config_code); const value = (attrs === null || attrs === void 0 ? void 0 : attrs.value) || (attrs === null || attrs === void 0 ? void 0 : attrs.config_value); if (name && value) { dict[name] = value; } return dict; }, {}); }; exports.reduceBinaryNodeToDictionary = reduceBinaryNodeToDictionary; const getBinaryNodeMessages = ({ content }) => { if (!Array.isArray(content)) { return []; } const msgs = []; for (const item of content) { if (item.tag === 'message' && item.content) { try { msgs.push(WAProto_1.proto.WebMessageInfo.decode(item.content)); } catch (error) { // Handle decode error if needed console.error('Failed to decode message:', error); } } } return msgs; }; exports.getBinaryNodeMessages = getBinaryNodeMessages; function bufferToUInt(e, t) { let a = 0; for (let i = 0; i < t; i++) { a = 256 * a + e[i]; } return a; } const tabs = (n) => '\t'.repeat(n); function binaryNodeToString(node, i = 0) { if (!node) { return undefined; } if (typeof node === 'string') { return tabs(i) + node; } if (node instanceof Uint8Array) { return tabs(i) + Buffer.from(node).toString('hex'); } if (Array.isArray(node)) { return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n'); } const children = binaryNodeToString(node.content, i + 1); const attrs = Object.entries(node.attrs || {}) .filter(([, v]) => v !== undefined) .map(([k, v]) => `${k}='${v}'`) .join(' '); const tag = `<${node.tag}${attrs ? ' ' + attrs : ''}`; const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'; return tag + content; } exports.binaryNodeToString = binaryNodeToString;