project: Move to ESM Modules

This commit is contained in:
Rajeh Taher
2025-07-17 13:54:17 +03:00
parent 19124426b2
commit 787aed88b8
69 changed files with 5143 additions and 4757 deletions
+17 -16
View File
@@ -57,7 +57,7 @@ export const decodeDecompressedBinaryNode = (
let val = 0
for (let i = 0; i < n; i++) {
const shift = littleEndian ? i : n - 1 - i
val |= next() << (shift * 8)
val |= next()! << (shift * 8)
}
return val
@@ -65,7 +65,7 @@ export const decodeDecompressedBinaryNode = (
const readInt20 = () => {
checkEOS(3)
return ((next() & 15) << 16) + (next() << 8) + next()
return ((next()! & 15) << 16) + (next()! << 8) + next()!
}
const unpackHex = (value: number) => {
@@ -104,11 +104,12 @@ export const decodeDecompressedBinaryNode = (
}
const readPacked8 = (tag: number) => {
const startByte = readByte()
const startByte = readByte()!
let value = ''
for (let i = 0; i < (startByte & 127); i++) {
const curByte = readByte()
const curByte = readByte()!
value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
}
@@ -138,8 +139,8 @@ export const decodeDecompressedBinaryNode = (
}
const readJidPair = () => {
const i = readString(readByte())
const j = readString(readByte())
const i = readString(readByte()!)
const j = readString(readByte()!)
if (j) {
return (i || '') + '@' + j
}
@@ -152,7 +153,7 @@ export const decodeDecompressedBinaryNode = (
const domainType = Number(rawDomainType)
const device = readByte()
const user = readString(readByte())
const user = readString(readByte()!)
return jidEncode(user, domainType === 0 || domainType === 128 ? 's.whatsapp.net' : 'lid', device)
}
@@ -167,11 +168,11 @@ export const decodeDecompressedBinaryNode = (
case TAGS.DICTIONARY_1:
case TAGS.DICTIONARY_2:
case TAGS.DICTIONARY_3:
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte()!)
case TAGS.LIST_EMPTY:
return ''
case TAGS.BINARY_8:
return readStringFromChars(readByte())
return readStringFromChars(readByte()!)
case TAGS.BINARY_20:
return readStringFromChars(readInt20())
case TAGS.BINARY_32:
@@ -190,7 +191,7 @@ export const decodeDecompressedBinaryNode = (
const readList = (tag: number) => {
const items: BinaryNode[] = []
const size = readListSize(tag)
const size = readListSize(tag)!
for (let i = 0; i < size; i++) {
items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
}
@@ -212,8 +213,8 @@ export const decodeDecompressedBinaryNode = (
return value
}
const listSize = readListSize(readByte())
const header = readString(readByte())
const listSize = readListSize(readByte()!)
const header = readString(readByte()!)
if (!listSize || !header.length) {
throw new Error('invalid node')
}
@@ -227,21 +228,21 @@ export const decodeDecompressedBinaryNode = (
// read the attributes in
const attributesLength = (listSize - 1) >> 1
for (let i = 0; i < attributesLength; i++) {
const key = readString(readByte())
const value = readString(readByte())
const key = readString(readByte()!)
const value = readString(readByte()!)
attrs[key] = value
}
if (listSize % 2 === 0) {
const tag = readByte()
const tag = readByte()!
if (isListTag(tag)) {
data = readList(tag)
} else {
let decoded: Buffer | string
switch (tag) {
case TAGS.BINARY_8:
decoded = readBytes(readByte())
decoded = readBytes(readByte()!)
break
case TAGS.BINARY_20:
decoded = readBytes(readInt20())
+3 -3
View File
@@ -1,5 +1,5 @@
import * as constants from './constants'
import { FullJid, jidDecode } from './jid-utils'
import { type FullJid, jidDecode } from './jid-utils'
import type { BinaryNode, BinaryNodeCodingOptions } from './types'
export const encodeBinaryNode = (
@@ -138,11 +138,11 @@ const encodeBinaryNodeInner = (
const strLengthHalf = Math.floor(str.length / 2)
for (let i = 0; i < strLengthHalf; i++) {
pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
pushByte(packBytePair(str[2 * i]!, str[2 * i + 1]!))
}
if (str.length % 2 !== 0) {
pushByte(packBytePair(str[str.length - 1], '\x00'))
pushByte(packBytePair(str[str.length - 1]!, '\x00'))
}
}
+11 -6
View File
@@ -1,6 +1,6 @@
import { Boom } from '@hapi/boom'
import { proto } from '../../WAProto'
import { BinaryNode } from './types'
import { type BinaryNode } from './types'
// some extra useful utilities
@@ -52,7 +52,7 @@ export const getBinaryNodeChildUInt = (node: BinaryNode, childTag: string, lengt
export const assertNodeErrorFree = (node: BinaryNode) => {
const errNode = getBinaryNodeChild(node, 'error')
if (errNode) {
throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code })
throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code! })
}
}
@@ -60,7 +60,12 @@ export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => {
const nodes = getBinaryNodeChildren(node, tag)
const dict = nodes.reduce(
(dict, { attrs }) => {
dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value
if (typeof attrs.name === 'string') {
dict[attrs.name] = attrs.value! || attrs.config_value!
} else {
dict[attrs.config_code!] = attrs.value! || attrs.config_value!
}
return dict
},
{} as { [_: string]: string }
@@ -84,7 +89,7 @@ export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
function bufferToUInt(e: Uint8Array | Buffer, t: number) {
let a = 0
for (let i = 0; i < t; i++) {
a = 256 * a + e[i]
a = 256 * a + e[i]!
}
return a
@@ -92,9 +97,9 @@ function bufferToUInt(e: Uint8Array | Buffer, t: number) {
const tabs = (n: number) => '\t'.repeat(n)
export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0) {
export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0): string {
if (!node) {
return node
return node!
}
if (typeof node === 'string') {
+2 -2
View File
@@ -31,7 +31,7 @@ export const jidDecode = (jid: string | undefined): FullJid | undefined => {
const userCombined = jid!.slice(0, sepIdx)
const [userAgent, device] = userCombined.split(':')
const user = userAgent.split('_')[0]
const user = userAgent!.split('_')[0]!
return {
server: server as JidServer,
@@ -61,7 +61,7 @@ export const isJidNewsletter = (jid: string | undefined) => jid?.endsWith('@news
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)