chore: format everything
This commit is contained in:
+1273
-12
File diff suppressed because one or more lines are too long
+75
-78
@@ -6,10 +6,11 @@ import type { BinaryNode, BinaryNodeCodingOptions } from './types'
|
||||
|
||||
const inflatePromise = promisify(inflate)
|
||||
|
||||
export const decompressingIfRequired = async(buffer: Buffer) => {
|
||||
if(2 & buffer.readUInt8()) {
|
||||
export const decompressingIfRequired = async (buffer: Buffer) => {
|
||||
if (2 & buffer.readUInt8()) {
|
||||
buffer = await inflatePromise(buffer.slice(1))
|
||||
} else { // nodes with no compression have a 0x00 prefix, we remove that
|
||||
} else {
|
||||
// nodes with no compression have a 0x00 prefix, we remove that
|
||||
buffer = buffer.slice(1)
|
||||
}
|
||||
|
||||
@@ -24,7 +25,7 @@ export const decodeDecompressedBinaryNode = (
|
||||
const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
|
||||
|
||||
const checkEOS = (length: number) => {
|
||||
if(indexRef.index + length > buffer.length) {
|
||||
if (indexRef.index + length > buffer.length) {
|
||||
throw new Error('end of stream')
|
||||
}
|
||||
}
|
||||
@@ -54,7 +55,7 @@ export const decodeDecompressedBinaryNode = (
|
||||
const readInt = (n: number, littleEndian = false) => {
|
||||
checkEOS(n)
|
||||
let val = 0
|
||||
for(let i = 0; i < n; i++) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
const shift = littleEndian ? i : n - 1 - i
|
||||
val |= next() << (shift * 8)
|
||||
}
|
||||
@@ -68,7 +69,7 @@ export const decodeDecompressedBinaryNode = (
|
||||
}
|
||||
|
||||
const unpackHex = (value: number) => {
|
||||
if(value >= 0 && value < 16) {
|
||||
if (value >= 0 && value < 16) {
|
||||
return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
|
||||
}
|
||||
|
||||
@@ -76,26 +77,26 @@ export const decodeDecompressedBinaryNode = (
|
||||
}
|
||||
|
||||
const unpackNibble = (value: number) => {
|
||||
if(value >= 0 && value <= 9) {
|
||||
if (value >= 0 && value <= 9) {
|
||||
return '0'.charCodeAt(0) + value
|
||||
}
|
||||
|
||||
switch (value) {
|
||||
case 10:
|
||||
return '-'.charCodeAt(0)
|
||||
case 11:
|
||||
return '.'.charCodeAt(0)
|
||||
case 15:
|
||||
return '\0'.charCodeAt(0)
|
||||
default:
|
||||
throw new Error('invalid nibble: ' + value)
|
||||
case 10:
|
||||
return '-'.charCodeAt(0)
|
||||
case 11:
|
||||
return '.'.charCodeAt(0)
|
||||
case 15:
|
||||
return '\0'.charCodeAt(0)
|
||||
default:
|
||||
throw new Error('invalid nibble: ' + value)
|
||||
}
|
||||
}
|
||||
|
||||
const unpackByte = (tag: number, value: number) => {
|
||||
if(tag === TAGS.NIBBLE_8) {
|
||||
if (tag === TAGS.NIBBLE_8) {
|
||||
return unpackNibble(value)
|
||||
} else if(tag === TAGS.HEX_8) {
|
||||
} else if (tag === TAGS.HEX_8) {
|
||||
return unpackHex(value)
|
||||
} else {
|
||||
throw new Error('unknown tag: ' + tag)
|
||||
@@ -106,13 +107,13 @@ export const decodeDecompressedBinaryNode = (
|
||||
const startByte = readByte()
|
||||
let value = ''
|
||||
|
||||
for(let i = 0; i < (startByte & 127); i++) {
|
||||
for (let i = 0; i < (startByte & 127); i++) {
|
||||
const curByte = readByte()
|
||||
value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
|
||||
value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
|
||||
}
|
||||
|
||||
if(startByte >> 7 !== 0) {
|
||||
if (startByte >> 7 !== 0) {
|
||||
value = value.slice(0, -1)
|
||||
}
|
||||
|
||||
@@ -125,21 +126,21 @@ export const decodeDecompressedBinaryNode = (
|
||||
|
||||
const readListSize = (tag: number) => {
|
||||
switch (tag) {
|
||||
case TAGS.LIST_EMPTY:
|
||||
return 0
|
||||
case TAGS.LIST_8:
|
||||
return readByte()
|
||||
case TAGS.LIST_16:
|
||||
return readInt(2)
|
||||
default:
|
||||
throw new Error('invalid tag for list size: ' + tag)
|
||||
case TAGS.LIST_EMPTY:
|
||||
return 0
|
||||
case TAGS.LIST_8:
|
||||
return readByte()
|
||||
case TAGS.LIST_16:
|
||||
return readInt(2)
|
||||
default:
|
||||
throw new Error('invalid tag for list size: ' + tag)
|
||||
}
|
||||
}
|
||||
|
||||
const readJidPair = () => {
|
||||
const i = readString(readByte())
|
||||
const j = readString(readByte())
|
||||
if(j) {
|
||||
if (j) {
|
||||
return (i || '') + '@' + j
|
||||
}
|
||||
|
||||
@@ -153,48 +154,44 @@ export const decodeDecompressedBinaryNode = (
|
||||
const device = readByte()
|
||||
const user = readString(readByte())
|
||||
|
||||
return jidEncode(
|
||||
user,
|
||||
domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid',
|
||||
device
|
||||
)
|
||||
return jidEncode(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device)
|
||||
}
|
||||
|
||||
const readString = (tag: number): string => {
|
||||
if(tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
|
||||
if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
|
||||
return SINGLE_BYTE_TOKENS[tag] || ''
|
||||
}
|
||||
|
||||
switch (tag) {
|
||||
case TAGS.DICTIONARY_0:
|
||||
case TAGS.DICTIONARY_1:
|
||||
case TAGS.DICTIONARY_2:
|
||||
case TAGS.DICTIONARY_3:
|
||||
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
|
||||
case TAGS.LIST_EMPTY:
|
||||
return ''
|
||||
case TAGS.BINARY_8:
|
||||
return readStringFromChars(readByte())
|
||||
case TAGS.BINARY_20:
|
||||
return readStringFromChars(readInt20())
|
||||
case TAGS.BINARY_32:
|
||||
return readStringFromChars(readInt(4))
|
||||
case TAGS.JID_PAIR:
|
||||
return readJidPair()
|
||||
case TAGS.AD_JID:
|
||||
return readAdJid()
|
||||
case TAGS.HEX_8:
|
||||
case TAGS.NIBBLE_8:
|
||||
return readPacked8(tag)
|
||||
default:
|
||||
throw new Error('invalid string with tag: ' + tag)
|
||||
case TAGS.DICTIONARY_0:
|
||||
case TAGS.DICTIONARY_1:
|
||||
case TAGS.DICTIONARY_2:
|
||||
case TAGS.DICTIONARY_3:
|
||||
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
|
||||
case TAGS.LIST_EMPTY:
|
||||
return ''
|
||||
case TAGS.BINARY_8:
|
||||
return readStringFromChars(readByte())
|
||||
case TAGS.BINARY_20:
|
||||
return readStringFromChars(readInt20())
|
||||
case TAGS.BINARY_32:
|
||||
return readStringFromChars(readInt(4))
|
||||
case TAGS.JID_PAIR:
|
||||
return readJidPair()
|
||||
case TAGS.AD_JID:
|
||||
return readAdJid()
|
||||
case TAGS.HEX_8:
|
||||
case TAGS.NIBBLE_8:
|
||||
return readPacked8(tag)
|
||||
default:
|
||||
throw new Error('invalid string with tag: ' + tag)
|
||||
}
|
||||
}
|
||||
|
||||
const readList = (tag: number) => {
|
||||
const items: BinaryNode[] = []
|
||||
const size = readListSize(tag)
|
||||
for(let i = 0;i < size;i++) {
|
||||
for (let i = 0; i < size; i++) {
|
||||
items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
|
||||
}
|
||||
|
||||
@@ -203,12 +200,12 @@ export const decodeDecompressedBinaryNode = (
|
||||
|
||||
const getTokenDouble = (index1: number, index2: number) => {
|
||||
const dict = DOUBLE_BYTE_TOKENS[index1]
|
||||
if(!dict) {
|
||||
if (!dict) {
|
||||
throw new Error(`Invalid double token dict (${index1})`)
|
||||
}
|
||||
|
||||
const value = dict[index2]
|
||||
if(typeof value === 'undefined') {
|
||||
if (typeof value === 'undefined') {
|
||||
throw new Error(`Invalid double token (${index2})`)
|
||||
}
|
||||
|
||||
@@ -217,44 +214,44 @@ export const decodeDecompressedBinaryNode = (
|
||||
|
||||
const listSize = readListSize(readByte())
|
||||
const header = readString(readByte())
|
||||
if(!listSize || !header.length) {
|
||||
if (!listSize || !header.length) {
|
||||
throw new Error('invalid node')
|
||||
}
|
||||
|
||||
const attrs: BinaryNode['attrs'] = { }
|
||||
const attrs: BinaryNode['attrs'] = {}
|
||||
let data: BinaryNode['content']
|
||||
if(listSize === 0 || !header) {
|
||||
if (listSize === 0 || !header) {
|
||||
throw new Error('invalid node')
|
||||
}
|
||||
|
||||
// read the attributes in
|
||||
const attributesLength = (listSize - 1) >> 1
|
||||
for(let i = 0; i < attributesLength; i++) {
|
||||
for (let i = 0; i < attributesLength; i++) {
|
||||
const key = readString(readByte())
|
||||
const value = readString(readByte())
|
||||
|
||||
attrs[key] = value
|
||||
}
|
||||
|
||||
if(listSize % 2 === 0) {
|
||||
if (listSize % 2 === 0) {
|
||||
const tag = readByte()
|
||||
if(isListTag(tag)) {
|
||||
if (isListTag(tag)) {
|
||||
data = readList(tag)
|
||||
} else {
|
||||
let decoded: Buffer | string
|
||||
switch (tag) {
|
||||
case TAGS.BINARY_8:
|
||||
decoded = readBytes(readByte())
|
||||
break
|
||||
case TAGS.BINARY_20:
|
||||
decoded = readBytes(readInt20())
|
||||
break
|
||||
case TAGS.BINARY_32:
|
||||
decoded = readBytes(readInt(4))
|
||||
break
|
||||
default:
|
||||
decoded = readString(tag)
|
||||
break
|
||||
case TAGS.BINARY_8:
|
||||
decoded = readBytes(readByte())
|
||||
break
|
||||
case TAGS.BINARY_20:
|
||||
decoded = readBytes(readInt20())
|
||||
break
|
||||
case TAGS.BINARY_32:
|
||||
decoded = readBytes(readInt(4))
|
||||
break
|
||||
default:
|
||||
decoded = readString(tag)
|
||||
break
|
||||
}
|
||||
|
||||
data = decoded
|
||||
@@ -268,7 +265,7 @@ export const decodeDecompressedBinaryNode = (
|
||||
}
|
||||
}
|
||||
|
||||
export const decodeBinaryNode = async(buff: Buffer): Promise<BinaryNode> => {
|
||||
export const decodeBinaryNode = async (buff: Buffer): Promise<BinaryNode> => {
|
||||
const decompBuff = await decompressingIfRequired(buff)
|
||||
return decodeDecompressedBinaryNode(decompBuff, constants)
|
||||
}
|
||||
|
||||
+54
-58
@@ -1,4 +1,3 @@
|
||||
|
||||
import * as constants from './constants'
|
||||
import { FullJid, jidDecode } from './jid-utils'
|
||||
import type { BinaryNode, BinaryNodeCodingOptions } from './types'
|
||||
@@ -22,14 +21,14 @@ const encodeBinaryNodeInner = (
|
||||
const pushByte = (value: number) => buffer.push(value & 0xff)
|
||||
|
||||
const pushInt = (value: number, n: number, littleEndian = false) => {
|
||||
for(let i = 0; i < n; i++) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
const curShift = littleEndian ? i : n - 1 - i
|
||||
buffer.push((value >> (curShift * 8)) & 0xff)
|
||||
}
|
||||
}
|
||||
|
||||
const pushBytes = (bytes: Uint8Array | Buffer | number[]) => {
|
||||
for(const b of bytes) {
|
||||
for (const b of bytes) {
|
||||
buffer.push(b)
|
||||
}
|
||||
}
|
||||
@@ -38,18 +37,16 @@ const encodeBinaryNodeInner = (
|
||||
pushBytes([(value >> 8) & 0xff, value & 0xff])
|
||||
}
|
||||
|
||||
const pushInt20 = (value: number) => (
|
||||
pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
|
||||
)
|
||||
const pushInt20 = (value: number) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
|
||||
const writeByteLength = (length: number) => {
|
||||
if(length >= 4294967296) {
|
||||
if (length >= 4294967296) {
|
||||
throw new Error('string too large to encode: ' + length)
|
||||
}
|
||||
|
||||
if(length >= 1 << 20) {
|
||||
if (length >= 1 << 20) {
|
||||
pushByte(TAGS.BINARY_32)
|
||||
pushInt(length, 4) // 32 bit integer
|
||||
} else if(length >= 256) {
|
||||
} else if (length >= 256) {
|
||||
pushByte(TAGS.BINARY_20)
|
||||
pushInt20(length)
|
||||
} else {
|
||||
@@ -59,20 +56,20 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
|
||||
const writeStringRaw = (str: string) => {
|
||||
const bytes = Buffer.from (str, 'utf-8')
|
||||
const bytes = Buffer.from(str, 'utf-8')
|
||||
writeByteLength(bytes.length)
|
||||
pushBytes(bytes)
|
||||
}
|
||||
|
||||
const writeJid = ({ domainType, device, user, server }: FullJid) => {
|
||||
if(typeof device !== 'undefined') {
|
||||
if (typeof device !== 'undefined') {
|
||||
pushByte(TAGS.AD_JID)
|
||||
pushByte(domainType || 0)
|
||||
pushByte(device || 0)
|
||||
writeString(user)
|
||||
} else {
|
||||
pushByte(TAGS.JID_PAIR)
|
||||
if(user.length) {
|
||||
if (user.length) {
|
||||
writeString(user)
|
||||
} else {
|
||||
pushByte(TAGS.LIST_EMPTY)
|
||||
@@ -84,35 +81,35 @@ const encodeBinaryNodeInner = (
|
||||
|
||||
const packNibble = (char: string) => {
|
||||
switch (char) {
|
||||
case '-':
|
||||
return 10
|
||||
case '.':
|
||||
return 11
|
||||
case '\0':
|
||||
return 15
|
||||
default:
|
||||
if(char >= '0' && char <= '9') {
|
||||
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
||||
}
|
||||
case '-':
|
||||
return 10
|
||||
case '.':
|
||||
return 11
|
||||
case '\0':
|
||||
return 15
|
||||
default:
|
||||
if (char >= '0' && char <= '9') {
|
||||
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
||||
}
|
||||
|
||||
throw new Error(`invalid byte for nibble "${char}"`)
|
||||
throw new Error(`invalid byte for nibble "${char}"`)
|
||||
}
|
||||
}
|
||||
|
||||
const packHex = (char: string) => {
|
||||
if(char >= '0' && char <= '9') {
|
||||
if (char >= '0' && char <= '9') {
|
||||
return char.charCodeAt(0) - '0'.charCodeAt(0)
|
||||
}
|
||||
|
||||
if(char >= 'A' && char <= 'F') {
|
||||
if (char >= 'A' && char <= 'F') {
|
||||
return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
|
||||
}
|
||||
|
||||
if(char >= 'a' && char <= 'f') {
|
||||
if (char >= 'a' && char <= 'f') {
|
||||
return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
|
||||
}
|
||||
|
||||
if(char === '\0') {
|
||||
if (char === '\0') {
|
||||
return 15
|
||||
}
|
||||
|
||||
@@ -120,14 +117,14 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
|
||||
const writePackedBytes = (str: string, type: 'nibble' | 'hex') => {
|
||||
if(str.length > TAGS.PACKED_MAX) {
|
||||
if (str.length > TAGS.PACKED_MAX) {
|
||||
throw new Error('Too many bytes to pack')
|
||||
}
|
||||
|
||||
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
|
||||
|
||||
let roundedLength = Math.ceil(str.length / 2.0)
|
||||
if(str.length % 2 !== 0) {
|
||||
if (str.length % 2 !== 0) {
|
||||
roundedLength |= 128
|
||||
}
|
||||
|
||||
@@ -140,23 +137,23 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
|
||||
const strLengthHalf = Math.floor(str.length / 2)
|
||||
for(let i = 0; i < strLengthHalf;i++) {
|
||||
for (let i = 0; i < strLengthHalf; i++) {
|
||||
pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
|
||||
}
|
||||
|
||||
if(str.length % 2 !== 0) {
|
||||
if (str.length % 2 !== 0) {
|
||||
pushByte(packBytePair(str[str.length - 1], '\x00'))
|
||||
}
|
||||
}
|
||||
|
||||
const isNibble = (str?: string) => {
|
||||
if(!str || str.length > TAGS.PACKED_MAX) {
|
||||
if (!str || str.length > TAGS.PACKED_MAX) {
|
||||
return false
|
||||
}
|
||||
|
||||
for(const char of str) {
|
||||
for (const char of str) {
|
||||
const isInNibbleRange = char >= '0' && char <= '9'
|
||||
if(!isInNibbleRange && char !== '-' && char !== '.') {
|
||||
if (!isInNibbleRange && char !== '-' && char !== '.') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -165,13 +162,13 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
|
||||
const isHex = (str?: string) => {
|
||||
if(!str || str.length > TAGS.PACKED_MAX) {
|
||||
if (!str || str.length > TAGS.PACKED_MAX) {
|
||||
return false
|
||||
}
|
||||
|
||||
for(const char of str) {
|
||||
for (const char of str) {
|
||||
const isInNibbleRange = char >= '0' && char <= '9'
|
||||
if(!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
||||
if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -180,25 +177,25 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
|
||||
const writeString = (str?: string) => {
|
||||
if(str === undefined || str === null) {
|
||||
if (str === undefined || str === null) {
|
||||
pushByte(TAGS.LIST_EMPTY)
|
||||
return
|
||||
}
|
||||
|
||||
const tokenIndex = TOKEN_MAP[str]
|
||||
if(tokenIndex) {
|
||||
if(typeof tokenIndex.dict === 'number') {
|
||||
if (tokenIndex) {
|
||||
if (typeof tokenIndex.dict === 'number') {
|
||||
pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
|
||||
}
|
||||
|
||||
pushByte(tokenIndex.index)
|
||||
} else if(isNibble(str)) {
|
||||
} else if (isNibble(str)) {
|
||||
writePackedBytes(str, 'nibble')
|
||||
} else if(isHex(str)) {
|
||||
} else if (isHex(str)) {
|
||||
writePackedBytes(str, 'hex')
|
||||
} else if(str) {
|
||||
} else if (str) {
|
||||
const decodedJid = jidDecode(str)
|
||||
if(decodedJid) {
|
||||
if (decodedJid) {
|
||||
writeJid(decodedJid)
|
||||
} else {
|
||||
writeStringRaw(str)
|
||||
@@ -207,9 +204,9 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
|
||||
const writeListStart = (listSize: number) => {
|
||||
if(listSize === 0) {
|
||||
if (listSize === 0) {
|
||||
pushByte(TAGS.LIST_EMPTY)
|
||||
} else if(listSize < 256) {
|
||||
} else if (listSize < 256) {
|
||||
pushBytes([TAGS.LIST_8, listSize])
|
||||
} else {
|
||||
pushByte(TAGS.LIST_16)
|
||||
@@ -217,37 +214,36 @@ const encodeBinaryNodeInner = (
|
||||
}
|
||||
}
|
||||
|
||||
if(!tag) {
|
||||
if (!tag) {
|
||||
throw new Error('Invalid node: tag cannot be undefined')
|
||||
}
|
||||
|
||||
const validAttributes = Object.keys(attrs || {}).filter(k => (
|
||||
typeof attrs[k] !== 'undefined' && attrs[k] !== null
|
||||
))
|
||||
const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null)
|
||||
|
||||
writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0))
|
||||
writeString(tag)
|
||||
|
||||
for(const key of validAttributes) {
|
||||
if(typeof attrs[key] === 'string') {
|
||||
for (const key of validAttributes) {
|
||||
if (typeof attrs[key] === 'string') {
|
||||
writeString(key)
|
||||
writeString(attrs[key])
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof content === 'string') {
|
||||
if (typeof content === 'string') {
|
||||
writeString(content)
|
||||
} else if(Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
||||
} else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
||||
writeByteLength(content.length)
|
||||
pushBytes(content)
|
||||
} else if(Array.isArray(content)) {
|
||||
const validContent = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string')
|
||||
} else if (Array.isArray(content)) {
|
||||
const validContent = content.filter(
|
||||
item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string')
|
||||
)
|
||||
writeListStart(validContent.length)
|
||||
for(const item of validContent) {
|
||||
for (const item of validContent) {
|
||||
encodeBinaryNodeInner(item, opts, buffer)
|
||||
}
|
||||
} else if(typeof content === 'undefined') {
|
||||
} else if (typeof content === 'undefined') {
|
||||
// do nothing
|
||||
} else {
|
||||
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
|
||||
|
||||
@@ -5,7 +5,7 @@ import { BinaryNode } from './types'
|
||||
// some extra useful utilities
|
||||
|
||||
export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
|
||||
if(Array.isArray(node?.content)) {
|
||||
if (Array.isArray(node?.content)) {
|
||||
return node.content.filter(item => item.tag === childTag)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: st
|
||||
}
|
||||
|
||||
export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
|
||||
if(Array.isArray(content)) {
|
||||
if (Array.isArray(content)) {
|
||||
return content
|
||||
}
|
||||
|
||||
@@ -21,37 +21,37 @@ export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
|
||||
}
|
||||
|
||||
export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
|
||||
if(Array.isArray(node?.content)) {
|
||||
if (Array.isArray(node?.content)) {
|
||||
return node?.content.find(item => item.tag === childTag)
|
||||
}
|
||||
}
|
||||
|
||||
export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
|
||||
const child = getBinaryNodeChild(node, childTag)?.content
|
||||
if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
||||
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
||||
return child
|
||||
}
|
||||
}
|
||||
|
||||
export const getBinaryNodeChildString = (node: BinaryNode | undefined, childTag: string) => {
|
||||
const child = getBinaryNodeChild(node, childTag)?.content
|
||||
if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
||||
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
|
||||
return Buffer.from(child).toString('utf-8')
|
||||
} else if(typeof child === 'string') {
|
||||
} else if (typeof child === 'string') {
|
||||
return child
|
||||
}
|
||||
}
|
||||
|
||||
export const getBinaryNodeChildUInt = (node: BinaryNode, childTag: string, length: number) => {
|
||||
const buff = getBinaryNodeChildBuffer(node, childTag)
|
||||
if(buff) {
|
||||
if (buff) {
|
||||
return bufferToUInt(buff, length)
|
||||
}
|
||||
}
|
||||
|
||||
export const assertNodeErrorFree = (node: BinaryNode) => {
|
||||
const errNode = getBinaryNodeChild(node, 'error')
|
||||
if(errNode) {
|
||||
if (errNode) {
|
||||
throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code })
|
||||
}
|
||||
}
|
||||
@@ -62,16 +62,17 @@ export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => {
|
||||
(dict, { attrs }) => {
|
||||
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value
|
||||
return dict
|
||||
}, { } as { [_: string]: string }
|
||||
},
|
||||
{} as { [_: string]: string }
|
||||
)
|
||||
return dict
|
||||
}
|
||||
|
||||
export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
|
||||
const msgs: proto.WebMessageInfo[] = []
|
||||
if(Array.isArray(content)) {
|
||||
for(const item of content) {
|
||||
if(item.tag === 'message') {
|
||||
if (Array.isArray(content)) {
|
||||
for (const item of content) {
|
||||
if (item.tag === 'message') {
|
||||
msgs.push(proto.WebMessageInfo.decode(item.content as Buffer))
|
||||
}
|
||||
}
|
||||
@@ -82,7 +83,7 @@ export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
|
||||
|
||||
function bufferToUInt(e: Uint8Array | Buffer, t: number) {
|
||||
let a = 0
|
||||
for(let i = 0; i < t; i++) {
|
||||
for (let i = 0; i < t; i++) {
|
||||
a = 256 * a + e[i]
|
||||
}
|
||||
|
||||
@@ -92,20 +93,20 @@ function bufferToUInt(e: Uint8Array | Buffer, t: number) {
|
||||
const tabs = (n: number) => '\t'.repeat(n)
|
||||
|
||||
export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0) {
|
||||
if(!node) {
|
||||
if (!node) {
|
||||
return node
|
||||
}
|
||||
|
||||
if(typeof node === 'string') {
|
||||
if (typeof node === 'string') {
|
||||
return tabs(i) + node
|
||||
}
|
||||
|
||||
if(node instanceof Uint8Array) {
|
||||
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')
|
||||
if (Array.isArray(node)) {
|
||||
return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
|
||||
}
|
||||
|
||||
const children = binaryNodeToString(node.content, i + 1)
|
||||
@@ -118,4 +119,4 @@ export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i =
|
||||
const content: string = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
|
||||
|
||||
return tag + content
|
||||
}
|
||||
}
|
||||
|
||||
+13
-15
@@ -8,8 +8,8 @@ export const META_AI_JID = '13135550002@c.us'
|
||||
export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter' | 'bot'
|
||||
|
||||
export type JidWithDevice = {
|
||||
user: string
|
||||
device?: number
|
||||
user: string
|
||||
device?: number
|
||||
}
|
||||
|
||||
export type FullJid = JidWithDevice & {
|
||||
@@ -17,14 +17,13 @@ export type FullJid = JidWithDevice & {
|
||||
domainType?: number
|
||||
}
|
||||
|
||||
|
||||
export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
|
||||
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
|
||||
}
|
||||
|
||||
export const jidDecode = (jid: string | undefined): FullJid | undefined => {
|
||||
const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
|
||||
if(sepIdx < 0) {
|
||||
if (sepIdx < 0) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
@@ -43,34 +42,33 @@ export const jidDecode = (jid: string | undefined): FullJid | undefined => {
|
||||
}
|
||||
|
||||
/** is the jid a user */
|
||||
export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) => (
|
||||
export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) =>
|
||||
jidDecode(jid1)?.user === jidDecode(jid2)?.user
|
||||
)
|
||||
/** is the jid Meta IA */
|
||||
export const isJidMetaIa = (jid: string | undefined) => (jid?.endsWith('@bot'))
|
||||
export const isJidMetaIa = (jid: string | undefined) => jid?.endsWith('@bot')
|
||||
/** is the jid a user */
|
||||
export const isJidUser = (jid: string | undefined) => (jid?.endsWith('@s.whatsapp.net'))
|
||||
export const isJidUser = (jid: string | undefined) => jid?.endsWith('@s.whatsapp.net')
|
||||
/** is the jid a group */
|
||||
export const isLidUser = (jid: string | undefined) => (jid?.endsWith('@lid'))
|
||||
export const isLidUser = (jid: string | undefined) => jid?.endsWith('@lid')
|
||||
/** is the jid a broadcast */
|
||||
export const isJidBroadcast = (jid: string | undefined) => (jid?.endsWith('@broadcast'))
|
||||
export const isJidBroadcast = (jid: string | undefined) => jid?.endsWith('@broadcast')
|
||||
/** is the jid a group */
|
||||
export const isJidGroup = (jid: string | undefined) => (jid?.endsWith('@g.us'))
|
||||
export const isJidGroup = (jid: string | undefined) => jid?.endsWith('@g.us')
|
||||
/** is the jid the status broadcast */
|
||||
export const isJidStatusBroadcast = (jid: string) => jid === 'status@broadcast'
|
||||
/** is the jid a newsletter */
|
||||
export const isJidNewsletter = (jid: string | undefined) => (jid?.endsWith('@newsletter'))
|
||||
export const isJidNewsletter = (jid: string | undefined) => jid?.endsWith('@newsletter')
|
||||
|
||||
const botRegexp = /^1313555\d{4}$|^131655500\d{2}$/
|
||||
|
||||
export const isJidBot = (jid: string | undefined) => (jid && botRegexp.test(jid.split('@')[0]) && jid.endsWith('@c.us'))
|
||||
export const isJidBot = (jid: string | undefined) => jid && botRegexp.test(jid.split('@')[0]) && jid.endsWith('@c.us')
|
||||
|
||||
export const jidNormalizedUser = (jid: string | undefined) => {
|
||||
const result = jidDecode(jid)
|
||||
if(!result) {
|
||||
if (!result) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const { user, server } = result
|
||||
return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server as JidServer)
|
||||
return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : (server as JidServer))
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ import * as constants from './constants'
|
||||
* to maintain functional code structure
|
||||
* */
|
||||
export type BinaryNode = {
|
||||
tag: string
|
||||
attrs: { [key: string]: string }
|
||||
tag: string
|
||||
attrs: { [key: string]: string }
|
||||
content?: BinaryNode[] | string | Uint8Array
|
||||
}
|
||||
export type BinaryNodeAttributes = BinaryNode['attrs']
|
||||
export type BinaryNodeData = BinaryNode['content']
|
||||
|
||||
export type BinaryNodeCodingOptions = typeof constants
|
||||
export type BinaryNodeCodingOptions = typeof constants
|
||||
|
||||
Reference in New Issue
Block a user