@zervonex/baileys 0.0.0-stage → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3203 -2
- package/WAProto/WAProtoCompile.js +291 -0
- package/WAProto/fetcher.js +69 -0
- package/WAProto/index.js +88 -0
- package/WAProto/parser.js +313 -0
- package/engine-requirements.js +10 -0
- package/lib/Builders/AIRich.js +590 -0
- package/lib/Builders/base.js +130 -0
- package/lib/Builders/builders.js +448 -0
- package/lib/Builders/index.js +3 -0
- package/lib/Defaults/index.js +225 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/index.js +2 -0
- package/lib/Signal/libsignal.js +523 -0
- package/lib/Signal/lid-mapping.js +283 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +84 -0
- package/lib/Socket/business.js +152 -0
- package/lib/Socket/chats.js +855 -0
- package/lib/Socket/communities.js +147 -0
- package/lib/Socket/groups.js +294 -0
- package/lib/Socket/index.js +15 -0
- package/lib/Socket/messages-recv.js +904 -0
- package/lib/Socket/messages-send.js +693 -0
- package/lib/Socket/mex.js +34 -0
- package/lib/Socket/newsletter.js +752 -0
- package/lib/Socket/nexus-handler.js +55 -0
- package/lib/Socket/registration.js +155 -0
- package/lib/Socket/socket.js +798 -0
- package/lib/Socket/spam-report.js +65 -0
- package/lib/Socket/username.js +82 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +81 -0
- package/lib/Store/make-in-memory-store.js +425 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Store/reporting-info-store.js +38 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +15 -0
- package/lib/Types/Newsletter.js +100 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +26 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +30 -0
- package/lib/Utils/auth-utils.js +142 -0
- package/lib/Utils/baileys-event-stream.js +33 -0
- package/lib/Utils/bot-capabilities.js +40 -0
- package/lib/Utils/bot-planning-replay.js +44 -0
- package/lib/Utils/browser-utils.js +26 -0
- package/lib/Utils/business.js +75 -0
- package/lib/Utils/chat-utils.js +316 -0
- package/lib/Utils/companion-reg-client-utils.js +20 -0
- package/lib/Utils/consumer-application.js +38 -0
- package/lib/Utils/crypto.js +52 -0
- package/lib/Utils/decode-wa-message.js +197 -0
- package/lib/Utils/event-buffer.js +273 -0
- package/lib/Utils/extras.js +186 -0
- package/lib/Utils/generics.js +195 -0
- package/lib/Utils/history.js +51 -0
- package/lib/Utils/identity-change-handler.js +19 -0
- package/lib/Utils/index.js +39 -0
- package/lib/Utils/key-store.js +3 -0
- package/lib/Utils/link-preview.js +51 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +3 -0
- package/lib/Utils/make-mutex.js +16 -0
- package/lib/Utils/message-composer.js +59 -0
- package/lib/Utils/message-retry-manager.js +48 -0
- package/lib/Utils/messages-media.js +520 -0
- package/lib/Utils/messages.js +742 -0
- package/lib/Utils/meta-ai-msmsg.js +122 -0
- package/lib/Utils/meta-compositing.js +75 -0
- package/lib/Utils/modern-messages.js +235 -0
- package/lib/Utils/native-flow.js +55 -0
- package/lib/Utils/noise-handler.js +161 -0
- package/lib/Utils/pre-key-manager.js +76 -0
- package/lib/Utils/process-message.js +399 -0
- package/lib/Utils/reporting-utils.js +171 -0
- package/lib/Utils/signal.js +167 -0
- package/lib/Utils/spam-report-utils.js +142 -0
- package/lib/Utils/sync-action-utils.js +33 -0
- package/lib/Utils/tc-token-utils.js +102 -0
- package/lib/Utils/use-multi-file-auth-state.js +307 -0
- package/lib/Utils/validate-connection.js +181 -0
- package/lib/WABinary/constants.js +1321 -0
- package/lib/WABinary/decode.js +152 -0
- package/lib/WABinary/encode.js +140 -0
- package/lib/WABinary/generic-utils.js +179 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +235 -0
- package/lib/WABinary/types.js +4 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +100 -0
- package/lib/WAUSync/USyncUser.js +20 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +68 -0
- package/package.json +216 -5
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { promisify } from 'util'
|
|
2
|
+
import { inflate } from 'zlib'
|
|
3
|
+
import * as constants from './constants.js'
|
|
4
|
+
import { jidEncode, WAJIDDomains } from './jid-utils.js'
|
|
5
|
+
|
|
6
|
+
const inflatePromise = promisify(inflate)
|
|
7
|
+
|
|
8
|
+
export const decompressingIfRequired = async (buffer) => {
|
|
9
|
+
if (2 & buffer.readUInt8()) buffer = await inflatePromise(buffer.slice(1))
|
|
10
|
+
else buffer = buffer.slice(1)
|
|
11
|
+
return buffer
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const decodeBinaryNode = async (buff) =>
|
|
15
|
+
decodeDecompressedBinaryNode(await decompressingIfRequired(buff), constants)
|
|
16
|
+
|
|
17
|
+
export const decodeDecompressedBinaryNode = (buffer, opts, indexRef = { index: 0 }) => {
|
|
18
|
+
const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
|
|
19
|
+
|
|
20
|
+
const checkEOS = (n) => { if (indexRef.index + n > buffer.length) throw new Error('end of stream') }
|
|
21
|
+
const next = () => { const v = buffer[indexRef.index]; indexRef.index += 1; return v }
|
|
22
|
+
const readByte = () => { checkEOS(1); return next() }
|
|
23
|
+
const readBytes = (n) => { checkEOS(n); const v = buffer.slice(indexRef.index, indexRef.index + n); indexRef.index += n; return v }
|
|
24
|
+
const readStringFromChars = (n) => readBytes(n).toString('utf-8')
|
|
25
|
+
|
|
26
|
+
const readInt = (n, le = false) => {
|
|
27
|
+
checkEOS(n)
|
|
28
|
+
let val = 0
|
|
29
|
+
for (let i = 0; i < n; i++) val |= next() << ((le ? i : n - 1 - i) * 8)
|
|
30
|
+
return val
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const readInt20 = () => { checkEOS(3); return ((next() & 15) << 16) | (next() << 8) | next() }
|
|
34
|
+
|
|
35
|
+
const unpackHex = (v) => {
|
|
36
|
+
if (v >= 0 && v < 16) return v < 10 ? '0'.charCodeAt(0) + v : 'A'.charCodeAt(0) + v - 10
|
|
37
|
+
throw new Error('invalid hex: ' + v)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const unpackNibble = (v) => {
|
|
41
|
+
if (v >= 0 && v <= 9) return '0'.charCodeAt(0) + v
|
|
42
|
+
if (v === 10) return '-'.charCodeAt(0)
|
|
43
|
+
if (v === 11) return '.'.charCodeAt(0)
|
|
44
|
+
if (v === 15) return '\0'.charCodeAt(0)
|
|
45
|
+
throw new Error('invalid nibble: ' + v)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const unpackByte = (tag, v) => {
|
|
49
|
+
if (tag === TAGS.NIBBLE_8) return unpackNibble(v)
|
|
50
|
+
if (tag === TAGS.HEX_8) return unpackHex(v)
|
|
51
|
+
throw new Error('unknown tag: ' + tag)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const readPacked8 = (tag) => {
|
|
55
|
+
const startByte = readByte()
|
|
56
|
+
let value = ''
|
|
57
|
+
for (let i = 0; i < (startByte & 127); i++) {
|
|
58
|
+
const cur = readByte()
|
|
59
|
+
value += String.fromCharCode(unpackByte(tag, (cur & 0xf0) >> 4))
|
|
60
|
+
value += String.fromCharCode(unpackByte(tag, cur & 0x0f))
|
|
61
|
+
}
|
|
62
|
+
if (startByte >> 7 !== 0) value = value.slice(0, -1)
|
|
63
|
+
return value
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const isListTag = (tag) => tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
|
|
67
|
+
|
|
68
|
+
const readListSize = (tag) => {
|
|
69
|
+
switch (tag) {
|
|
70
|
+
case TAGS.LIST_EMPTY: return 0
|
|
71
|
+
case TAGS.LIST_8: return readByte()
|
|
72
|
+
case TAGS.LIST_16: return readInt(2)
|
|
73
|
+
default: throw new Error('invalid tag for list size: ' + tag)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const readJidPair = () => {
|
|
78
|
+
const i = readString(readByte())
|
|
79
|
+
const j = readString(readByte())
|
|
80
|
+
if (j) return (i || '') + '@' + j
|
|
81
|
+
throw new Error('invalid jid pair: ' + i + ', ' + j)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const readAdJid = () => {
|
|
85
|
+
const domainType = Number(readByte())
|
|
86
|
+
const device = readByte()
|
|
87
|
+
const user = readString(readByte())
|
|
88
|
+
const server =
|
|
89
|
+
domainType === WAJIDDomains.LID ? 'lid' :
|
|
90
|
+
domainType === WAJIDDomains.HOSTED ? 'hosted' :
|
|
91
|
+
domainType === WAJIDDomains.HOSTED_LID ? 'hosted.lid' :
|
|
92
|
+
's.whatsapp.net'
|
|
93
|
+
return jidEncode(user, server, device)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const getTokenDouble = (i1, i2) => {
|
|
97
|
+
const dict = DOUBLE_BYTE_TOKENS[i1]
|
|
98
|
+
if (!dict) throw new Error(`invalid double token dict (${i1})`)
|
|
99
|
+
const val = dict[i2]
|
|
100
|
+
if (val === undefined) throw new Error(`invalid double token (${i2})`)
|
|
101
|
+
return val
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const readString = (tag) => {
|
|
105
|
+
if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) return SINGLE_BYTE_TOKENS[tag] ?? ''
|
|
106
|
+
switch (tag) {
|
|
107
|
+
case TAGS.DICTIONARY_0:
|
|
108
|
+
case TAGS.DICTIONARY_1:
|
|
109
|
+
case TAGS.DICTIONARY_2:
|
|
110
|
+
case TAGS.DICTIONARY_3: return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
|
|
111
|
+
case TAGS.LIST_EMPTY: return ''
|
|
112
|
+
case TAGS.BINARY_8: return readStringFromChars(readByte())
|
|
113
|
+
case TAGS.BINARY_20: return readStringFromChars(readInt20())
|
|
114
|
+
case TAGS.BINARY_32: return readStringFromChars(readInt(4))
|
|
115
|
+
case TAGS.JID_PAIR: return readJidPair()
|
|
116
|
+
case TAGS.AD_JID: return readAdJid()
|
|
117
|
+
case TAGS.HEX_8:
|
|
118
|
+
case TAGS.NIBBLE_8: return readPacked8(tag)
|
|
119
|
+
default: throw new Error('invalid string with tag: ' + tag)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const readList = (tag) => {
|
|
124
|
+
const size = readListSize(tag)
|
|
125
|
+
const items = []
|
|
126
|
+
for (let i = 0; i < size; i++) items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
|
|
127
|
+
return items
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const listSize = readListSize(readByte())
|
|
131
|
+
const header = readString(readByte())
|
|
132
|
+
if (!listSize || !header.length) throw new Error('invalid node')
|
|
133
|
+
|
|
134
|
+
const attrs = {}
|
|
135
|
+
const attributesLength = (listSize - 1) >> 1
|
|
136
|
+
for (let i = 0; i < attributesLength; i++) attrs[readString(readByte())] = readString(readByte())
|
|
137
|
+
|
|
138
|
+
let data
|
|
139
|
+
if (listSize % 2 === 0) {
|
|
140
|
+
const tag = readByte()
|
|
141
|
+
data = isListTag(tag) ? readList(tag) : (() => {
|
|
142
|
+
switch (tag) {
|
|
143
|
+
case TAGS.BINARY_8: return readBytes(readByte())
|
|
144
|
+
case TAGS.BINARY_20: return readBytes(readInt20())
|
|
145
|
+
case TAGS.BINARY_32: return readBytes(readInt(4))
|
|
146
|
+
default: return readString(tag)
|
|
147
|
+
}
|
|
148
|
+
})()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return { tag: header, attrs, content: data }
|
|
152
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import * as constants from './constants.js'
|
|
2
|
+
import { jidDecode } from './jid-utils.js'
|
|
3
|
+
|
|
4
|
+
export const encodeBinaryNode = (node, opts = constants, buffer = [0]) => {
|
|
5
|
+
encodeBinaryNodeInner(node, opts, buffer)
|
|
6
|
+
return Buffer.from(buffer)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const encodeBinaryNodeInner = ({ tag, attrs, content }, opts, buffer) => {
|
|
10
|
+
const { TAGS, TOKEN_MAP } = opts
|
|
11
|
+
|
|
12
|
+
const pushByte = (v) => buffer.push(v & 0xff)
|
|
13
|
+
const pushBytes = (bytes) => {
|
|
14
|
+
if (bytes.length > 1000) { for (let i = 0; i < bytes.length; i++) buffer.push(bytes[i]) }
|
|
15
|
+
else buffer.push(...bytes)
|
|
16
|
+
}
|
|
17
|
+
const pushInt = (v, n, le = false) => {
|
|
18
|
+
for (let i = 0; i < n; i++) buffer.push((v >> ((le ? i : n - 1 - i) * 8)) & 0xff)
|
|
19
|
+
}
|
|
20
|
+
const pushInt16 = (v) => pushBytes([(v >> 8) & 0xff, v & 0xff])
|
|
21
|
+
const pushInt20 = (v) => pushBytes([(v >> 16) & 0x0f, (v >> 8) & 0xff, v & 0xff])
|
|
22
|
+
|
|
23
|
+
const writeByteLength = (length) => {
|
|
24
|
+
if (length >= 4294967296) throw new Error('string too large to encode: ' + length)
|
|
25
|
+
if (length >= 1 << 20) { pushByte(TAGS.BINARY_32); pushInt(length, 4) }
|
|
26
|
+
else if (length >= 256) { pushByte(TAGS.BINARY_20); pushInt20(length) }
|
|
27
|
+
else { pushByte(TAGS.BINARY_8); pushByte(length) }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const writeStringRaw = (str) => {
|
|
31
|
+
const bytes = Buffer.from(str, 'utf-8')
|
|
32
|
+
writeByteLength(bytes.length)
|
|
33
|
+
pushBytes(bytes)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const writeJid = ({ domainType, device, user, server }) => {
|
|
37
|
+
if (device !== undefined) {
|
|
38
|
+
pushByte(TAGS.AD_JID)
|
|
39
|
+
pushByte(domainType || 0)
|
|
40
|
+
pushByte(device || 0)
|
|
41
|
+
writeString(user)
|
|
42
|
+
} else {
|
|
43
|
+
pushByte(TAGS.JID_PAIR)
|
|
44
|
+
if (user.length) writeString(user)
|
|
45
|
+
else pushByte(TAGS.LIST_EMPTY)
|
|
46
|
+
writeString(server)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const packNibble = (char) => {
|
|
51
|
+
if (char >= '0' && char <= '9') return char.charCodeAt(0) - 48
|
|
52
|
+
if (char === '-') return 10
|
|
53
|
+
if (char === '.') return 11
|
|
54
|
+
if (char === '\0') return 15
|
|
55
|
+
throw new Error(`invalid byte for nibble "${char}"`)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const packHex = (char) => {
|
|
59
|
+
if (char >= '0' && char <= '9') return char.charCodeAt(0) - 48
|
|
60
|
+
if (char >= 'A' && char <= 'F') return 10 + char.charCodeAt(0) - 65
|
|
61
|
+
if (char >= 'a' && char <= 'f') return 10 + char.charCodeAt(0) - 97
|
|
62
|
+
if (char === '\0') return 15
|
|
63
|
+
throw new Error(`invalid hex char "${char}"`)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const writePackedBytes = (str, type) => {
|
|
67
|
+
if (str.length > TAGS.PACKED_MAX) throw new Error('too many bytes to pack')
|
|
68
|
+
pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
|
|
69
|
+
const odd = str.length % 2 !== 0
|
|
70
|
+
pushByte(odd ? (Math.ceil(str.length / 2) | 128) : Math.ceil(str.length / 2))
|
|
71
|
+
const pack = type === 'nibble' ? packNibble : packHex
|
|
72
|
+
for (let i = 0; i < Math.floor(str.length / 2); i++)
|
|
73
|
+
buffer.push((pack(str[2 * i]) << 4) | pack(str[2 * i + 1]))
|
|
74
|
+
if (odd) buffer.push((pack(str[str.length - 1]) << 4) | pack('\x00'))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const isNibble = (str) => {
|
|
78
|
+
if (!str || str.length > TAGS.PACKED_MAX) return false
|
|
79
|
+
for (const c of str) if (!(c >= '0' && c <= '9') && c !== '-' && c !== '.') return false
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const isHex = (str) => {
|
|
84
|
+
if (!str || str.length > TAGS.PACKED_MAX) return false
|
|
85
|
+
for (const c of str) if (!(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'F')) return false
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const writeString = (str) => {
|
|
90
|
+
if (str == null) { pushByte(TAGS.LIST_EMPTY); return }
|
|
91
|
+
const tokenIndex = TOKEN_MAP[str]
|
|
92
|
+
if (tokenIndex) {
|
|
93
|
+
if (typeof tokenIndex.dict === 'number') pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
|
|
94
|
+
pushByte(tokenIndex.index)
|
|
95
|
+
} else if (isNibble(str)) {
|
|
96
|
+
writePackedBytes(str, 'nibble')
|
|
97
|
+
} else if (isHex(str)) {
|
|
98
|
+
writePackedBytes(str, 'hex')
|
|
99
|
+
} else if (str) {
|
|
100
|
+
const decoded = jidDecode(str)
|
|
101
|
+
if (decoded) writeJid(decoded)
|
|
102
|
+
else writeStringRaw(str)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const writeListStart = (size) => {
|
|
107
|
+
if (size === 0) pushByte(TAGS.LIST_EMPTY)
|
|
108
|
+
else if (size < 256) pushBytes([TAGS.LIST_8, size])
|
|
109
|
+
else { pushByte(TAGS.LIST_16); pushInt16(size) }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!tag) throw new Error('invalid node: tag cannot be undefined')
|
|
113
|
+
|
|
114
|
+
const validAttrs = Object.keys(attrs || {}).filter(k => attrs[k] != null)
|
|
115
|
+
writeListStart(2 * validAttrs.length + 1 + (content !== undefined ? 1 : 0))
|
|
116
|
+
writeString(tag)
|
|
117
|
+
|
|
118
|
+
for (const key of validAttrs) {
|
|
119
|
+
if (typeof attrs[key] === 'string') { writeString(key); writeString(attrs[key]) }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (typeof content === 'string') {
|
|
123
|
+
writeString(content)
|
|
124
|
+
} else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
|
|
125
|
+
writeByteLength(content.length)
|
|
126
|
+
// large buffers overflow spread operator stack; push manually above threshold
|
|
127
|
+
if (content.length > 10_000_000) {
|
|
128
|
+
pushBytes([...content.slice(0, Math.min(1000, content.length))])
|
|
129
|
+
for (let i = 1000; i < content.length; i++) buffer.push(content[i])
|
|
130
|
+
} else {
|
|
131
|
+
pushBytes(content)
|
|
132
|
+
}
|
|
133
|
+
} else if (Array.isArray(content)) {
|
|
134
|
+
const valid = content.filter(item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string'))
|
|
135
|
+
writeListStart(valid.length)
|
|
136
|
+
for (const item of valid) encodeBinaryNodeInner(item, opts, buffer)
|
|
137
|
+
} else if (content !== undefined) {
|
|
138
|
+
throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom'
|
|
2
|
+
import { randomBytes } from 'crypto'
|
|
3
|
+
import { proto } from '../../WAProto/index.js'
|
|
4
|
+
|
|
5
|
+
// ─── Node Traversal ───────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
export const getBinaryNodeChildren = (node, childTag) =>
|
|
8
|
+
Array.isArray(node?.content) ? node.content.filter(item => item.tag === childTag) : []
|
|
9
|
+
|
|
10
|
+
export const getAllBinaryNodeChildren = ({ content }) =>
|
|
11
|
+
Array.isArray(content) ? content : []
|
|
12
|
+
|
|
13
|
+
export const getBinaryNodeChild = (node, childTag) =>
|
|
14
|
+
Array.isArray(node?.content) ? node.content.find(item => item.tag === childTag) : undefined
|
|
15
|
+
|
|
16
|
+
export const getBinaryNodeChildBuffer = (node, childTag) => {
|
|
17
|
+
const child = getBinaryNodeChild(node, childTag)?.content
|
|
18
|
+
return Buffer.isBuffer(child) || child instanceof Uint8Array ? child : undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const getBinaryNodeChildString = (node, childTag) => {
|
|
22
|
+
const child = getBinaryNodeChild(node, childTag)?.content
|
|
23
|
+
if (Buffer.isBuffer(child) || child instanceof Uint8Array) return Buffer.from(child).toString('utf-8')
|
|
24
|
+
if (typeof child === 'string') return child
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const getBinaryNodeChildUInt = (node, childTag, length) => {
|
|
28
|
+
const buff = getBinaryNodeChildBuffer(node, childTag)
|
|
29
|
+
return buff ? bufferToUInt(buff, length) : undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const getBinaryFilteredBizBot = nodeContent => {
|
|
33
|
+
if (!Array.isArray(nodeContent)) return false
|
|
34
|
+
return nodeContent.some(b => b?.tag === 'bot' && b?.attrs?.biz_bot === '1')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function bufferToUInt(e, t) {
|
|
38
|
+
let a = 0
|
|
39
|
+
for (let i = 0; i < t; i++) a = 256 * a + e[i]
|
|
40
|
+
return a
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ─── Node Validation ──────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
export const assertNodeErrorFree = (node) => {
|
|
46
|
+
const errNode = getBinaryNodeChild(node, 'error')
|
|
47
|
+
if (errNode) {
|
|
48
|
+
const errorCode = +errNode.attrs.code
|
|
49
|
+
if (errorCode === 429) {
|
|
50
|
+
const error = new Boom('Rate limit', { data: 429 })
|
|
51
|
+
error.isRateLimit = true
|
|
52
|
+
throw error
|
|
53
|
+
}
|
|
54
|
+
throw new Boom(errNode.attrs.text || 'Unknown error', { data: errorCode })
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const reduceBinaryNodeToDictionary = (node, tag) => {
|
|
59
|
+
const nodes = getBinaryNodeChildren(node, tag)
|
|
60
|
+
return nodes.reduce((dict, { attrs }) => {
|
|
61
|
+
if (typeof attrs.name === 'string') dict[attrs.name] = attrs.value || attrs.config_value
|
|
62
|
+
else dict[attrs.config_code] = attrs.value || attrs.config_value
|
|
63
|
+
return dict
|
|
64
|
+
}, {})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const getBinaryNodeMessages = ({ content }) => {
|
|
68
|
+
const msgs = []
|
|
69
|
+
if (Array.isArray(content)) {
|
|
70
|
+
for (const item of content) {
|
|
71
|
+
if (item.tag === 'message') msgs.push(proto.WebMessageInfo.decode(item.content).toJSON())
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return msgs
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ─── Button Type Detection ────────────────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
export const getBinaryFilteredButtons = (nodeContent) => {
|
|
80
|
+
if (!Array.isArray(nodeContent)) return false
|
|
81
|
+
return nodeContent.some(a =>
|
|
82
|
+
['native_flow'].includes(a?.content?.[0]?.content?.[0]?.tag) ||
|
|
83
|
+
['interactive', 'buttons', 'list'].includes(a?.content?.[0]?.tag) ||
|
|
84
|
+
['hsm', 'biz'].includes(a?.tag)
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const getButtonType = (message) => {
|
|
89
|
+
if (message.listMessage) return 'list'
|
|
90
|
+
if (message.buttonsMessage) return 'buttons'
|
|
91
|
+
const interactiveMsg = message.interactiveMessage || message.interactive
|
|
92
|
+
if (!interactiveMsg?.nativeFlowMessage) return null
|
|
93
|
+
const btn = interactiveMsg?.nativeFlowMessage?.buttons?.[0]?.name
|
|
94
|
+
if (['review_and_pay', 'review_order', 'payment_info', 'payment_status', 'payment_method'].includes(btn)) return btn
|
|
95
|
+
if (interactiveMsg?.nativeFlowMessage?.buttons?.length || interactiveMsg?.nativeFlowMessage) return 'interactive'
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ─── Biz Binary Node (canonical single implementation) ────────────────────────
|
|
100
|
+
|
|
101
|
+
const FLOWS_MAP = new Set(['mpm', 'catalog_message', 'send_location', 'call_permission_request', 'wa_payment_transaction_details', 'automated_greeting_message_view_catalog', 'card_message', 'order_status', 'track_order', 'reorder', 'cancel_order', 'clear_chat', 'navigateToScreen', 'payment_status', 'payment_method', 'flow', 'flow_action', 'voice_call', 'video_call_button', 'otp_button', 'authentication_button', 'cta_reminder', 'cta_cancel_reminder'])
|
|
102
|
+
|
|
103
|
+
const ORDER_RESPONSE_ALIAS = {
|
|
104
|
+
review_and_pay: 'order_details', review_order: 'order_status',
|
|
105
|
+
payment_info: 'payment_info', payment_status: 'payment_status',
|
|
106
|
+
payment_method: 'payment_method', order_details: 'order_details',
|
|
107
|
+
order_status: 'order_status', track_order: 'track_order',
|
|
108
|
+
reorder: 'reorder', cancel_order: 'cancel_order'
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const NATIVE_FLOW_ATTR = { type: 'native_flow', v: '1' }
|
|
112
|
+
const MIXED_NATIVE_FLOW = { tag: 'interactive', attrs: NATIVE_FLOW_ATTR, content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }] }
|
|
113
|
+
const LIST_TYPE_CONTENT = { tag: 'list', attrs: { v: '2', type: 'product_list' } }
|
|
114
|
+
const DECISION_SOURCE = [{ tag: 'decision_source', attrs: { value: 'df' } }]
|
|
115
|
+
|
|
116
|
+
export const getBizBinaryNode = (message) => {
|
|
117
|
+
const flowMsg = message.interactiveMessage?.nativeFlowMessage
|
|
118
|
+
const firstButtonName = flowMsg?.buttons?.[0]?.name
|
|
119
|
+
const bizAttrs = { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${Date.now() / 1000 | 0}` }
|
|
120
|
+
const qualityContent = {
|
|
121
|
+
tag: 'quality_control',
|
|
122
|
+
attrs: { decision_id: randomBytes(20).toString('hex'), source_type: 'third_party' },
|
|
123
|
+
content: DECISION_SOURCE
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (firstButtonName && ORDER_RESPONSE_ALIAS[firstButtonName]) {
|
|
127
|
+
bizAttrs.native_flow_name = ORDER_RESPONSE_ALIAS[firstButtonName]
|
|
128
|
+
return { tag: 'biz', attrs: bizAttrs, content: [qualityContent] }
|
|
129
|
+
}
|
|
130
|
+
if (firstButtonName && FLOWS_MAP.has(firstButtonName)) {
|
|
131
|
+
return {
|
|
132
|
+
tag: 'biz', attrs: bizAttrs,
|
|
133
|
+
content: [
|
|
134
|
+
{ tag: 'interactive', attrs: NATIVE_FLOW_ATTR, content: [{ tag: 'native_flow', attrs: { v: '2', name: firstButtonName } }] },
|
|
135
|
+
qualityContent
|
|
136
|
+
]
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (flowMsg || message.buttonsMessage || message.templateMessage) {
|
|
140
|
+
return { tag: 'biz', attrs: bizAttrs, content: [MIXED_NATIVE_FLOW, qualityContent] }
|
|
141
|
+
}
|
|
142
|
+
if (message.listMessage) {
|
|
143
|
+
return { tag: 'biz', attrs: bizAttrs, content: [LIST_TYPE_CONTENT, qualityContent] }
|
|
144
|
+
}
|
|
145
|
+
return { tag: 'biz', attrs: bizAttrs, content: [qualityContent] }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Legacy alias — kept for back-compat, delegates to canonical getBizBinaryNode
|
|
149
|
+
export const getButtonArgs = (message) => {
|
|
150
|
+
const msgContent = message.viewOnceMessage?.message || message
|
|
151
|
+
const interactiveMsg = msgContent.interactiveMessage || msgContent.interactive
|
|
152
|
+
const flowMsg = interactiveMsg?.nativeFlowMessage
|
|
153
|
+
const btnFirst = flowMsg?.buttons?.[0]?.name
|
|
154
|
+
const ts = Math.floor(Date.now() / 1000) - 77980457
|
|
155
|
+
const order_response_name = { review_and_pay: 'order_details', review_order: 'order_status', payment_info: 'payment_info', payment_status: 'payment_status', payment_method: 'payment_method' }
|
|
156
|
+
const flow_name = { cta_catalog: 'cta_catalog', mpm: 'mpm', call_permission_request: 'call_permission_request', call_request: 'call_permission_request', view_catalog: 'automated_greeting_message_view_catalog', automated_greeting_message_view_catalog: 'automated_greeting_message_view_catalog', wa_pay_detail: 'wa_payment_transaction_details', wa_payment_transaction_details: 'wa_payment_transaction_details', send_location: 'send_location', open_webview: 'open_webview', galaxy_message: 'galaxy_message' }
|
|
157
|
+
if (btnFirst && order_response_name[btnFirst]) return [{ tag: 'biz', attrs: { native_flow_name: order_response_name[btnFirst] }, content: [] }]
|
|
158
|
+
if (flowMsg || msgContent.buttonsMessage) {
|
|
159
|
+
const name = (btnFirst && flow_name[btnFirst]) ? flow_name[btnFirst] : 'mixed'
|
|
160
|
+
return [{ tag: 'biz', attrs: { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${ts}` }, content: [{ tag: 'engagement', attrs: { customer_service_state: 'open', conversation_state: 'open' } }, { tag: 'interactive', attrs: { type: 'native_flow', v: '1' }, content: [{ tag: 'native_flow', attrs: { v: '9', name }, content: [] }] }] }]
|
|
161
|
+
}
|
|
162
|
+
if (msgContent.listMessage) return [{ tag: 'biz', attrs: { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${ts}` }, content: [{ tag: 'engagement', attrs: { customer_service_state: 'open', conversation_state: 'open' } }] }]
|
|
163
|
+
return [{ tag: 'biz', attrs: { actual_actors: '2', host_storage: '2', privacy_mode_ts: `${ts}` }, content: [{ tag: 'engagement', attrs: { customer_service_state: 'open', conversation_state: 'open' } }] }]
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ─── Debug Utilities ──────────────────────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
const tabs = (n) => '\t'.repeat(n)
|
|
169
|
+
|
|
170
|
+
export const binaryNodeToString = (node, i = 0) => {
|
|
171
|
+
if (!node) return node
|
|
172
|
+
if (typeof node === 'string') return tabs(i) + node
|
|
173
|
+
if (node instanceof Uint8Array) return tabs(i) + Buffer.from(node).toString('hex')
|
|
174
|
+
if (Array.isArray(node)) return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
|
|
175
|
+
const children = binaryNodeToString(node.content, i + 1)
|
|
176
|
+
const tag = `<${node.tag} ${Object.entries(node.attrs || {}).filter(([, v]) => v !== undefined).map(([k, v]) => `${k}='${v}'`).join(' ')}`
|
|
177
|
+
const content = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
|
|
178
|
+
return tag + content
|
|
179
|
+
}
|