@zervonexx/baileys 5.0.0 → 5.0.2
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 +22 -22
- package/README.md +4124 -4112
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +7160 -0
- package/WAProto/WAProtoCompile.js +264 -322
- package/WAProto/index.js +55 -95
- package/lib/Builders/AIRich.js +673 -673
- package/lib/Builders/auto.js +49 -49
- package/lib/Builders/base.js +144 -144
- package/lib/Builders/builders.js +588 -588
- package/lib/Builders/index.js +3 -3
- package/lib/Defaults/index.js +249 -248
- package/lib/Signal/index.js +2 -2
- package/lib/Signal/libsignal.js +526 -526
- package/lib/Signal/lid-mapping.js +282 -282
- package/lib/Socket/Client/index.js +1 -1
- package/lib/Socket/Client/types.js +9 -9
- package/lib/Socket/Client/websocket.js +83 -83
- package/lib/Socket/business.js +151 -151
- package/lib/Socket/chats.js +854 -854
- package/lib/Socket/communities.js +146 -146
- package/lib/Socket/groups.js +293 -293
- package/lib/Socket/index.js +17 -14
- package/lib/Socket/messages-recv.js +993 -993
- package/lib/Socket/messages-send.js +722 -722
- package/lib/Socket/mex.js +33 -33
- package/lib/Socket/newsletter.js +783 -783
- package/lib/Socket/registration.js +154 -154
- package/lib/Socket/socket.js +797 -797
- package/lib/Socket/spam-report.js +143 -143
- package/lib/Socket/username.js +81 -81
- package/lib/Socket/zervonex-handler.js +57 -57
- package/lib/Store/index.js +6 -6
- package/lib/Store/make-cache-manager-store.js +80 -80
- package/lib/Store/make-in-memory-store.js +426 -426
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Store/reporting-info-store.js +37 -37
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +14 -14
- package/lib/Types/Newsletter.js +99 -99
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +14 -14
- package/lib/Types/State.js +26 -26
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +30 -30
- package/lib/Utils/auth-utils.js +159 -159
- package/lib/Utils/baileys-event-stream.js +32 -32
- package/lib/Utils/bot-capabilities.js +40 -40
- package/lib/Utils/bot-planning-replay.js +43 -43
- package/lib/Utils/browser-utils.js +29 -29
- package/lib/Utils/business.js +74 -74
- package/lib/Utils/chat-utils.js +316 -316
- package/lib/Utils/companion-reg-client-utils.js +19 -19
- package/lib/Utils/consumer-application.js +37 -37
- package/lib/Utils/crypto.js +52 -52
- package/lib/Utils/cstoken.js +14 -14
- package/lib/Utils/decode-wa-message.js +199 -199
- package/lib/Utils/event-buffer.js +272 -272
- package/lib/Utils/extras.js +186 -186
- package/lib/Utils/generics.js +195 -194
- package/lib/Utils/history.js +50 -50
- package/lib/Utils/identity-change-handler.js +18 -18
- package/lib/Utils/index.js +40 -40
- package/lib/Utils/link-preview.js +50 -50
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +3 -3
- package/lib/Utils/make-mutex.js +15 -15
- package/lib/Utils/message-composer.js +61 -61
- package/lib/Utils/message-retry-manager.js +47 -47
- package/lib/Utils/messages-media.js +501 -501
- package/lib/Utils/messages.js +944 -942
- package/lib/Utils/meta-ai-msmsg.js +121 -121
- package/lib/Utils/meta-compositing.js +74 -74
- package/lib/Utils/modern-messages.js +234 -234
- package/lib/Utils/native-flow.js +80 -80
- package/lib/Utils/noise-handler.js +160 -160
- package/lib/Utils/pre-key-manager.js +75 -75
- package/lib/Utils/process-message.js +400 -400
- package/lib/Utils/reporting-utils.js +170 -170
- package/lib/Utils/signal.js +166 -166
- package/lib/Utils/spam-report-utils.js +165 -165
- package/lib/Utils/sync-action-utils.js +33 -33
- package/lib/Utils/tc-token-utils.js +153 -153
- package/lib/Utils/use-keyv-auth-state.js +199 -199
- package/lib/Utils/use-multi-file-auth-state.js +83 -83
- package/lib/Utils/validate-connection.js +171 -171
- package/lib/WABinary/constants.js +1320 -1320
- package/lib/WABinary/decode.js +151 -151
- package/lib/WABinary/encode.js +139 -139
- package/lib/WABinary/generic-utils.js +202 -202
- package/lib/WABinary/index.js +6 -6
- package/lib/WABinary/jid-utils.js +235 -235
- package/lib/WABinary/types.js +4 -4
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -30
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -48
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -15
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -24
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -9
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -21
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -19
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -22
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -16
- package/lib/WAUSync/Protocols/index.js +12 -12
- package/lib/WAUSync/USyncQuery.js +88 -88
- package/lib/WAUSync/USyncUser.js +20 -20
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +66 -66
- package/package.json +187 -192
- package/WAProto/fetcher.js +0 -68
- package/WAProto/parser.js +0 -334
package/WAProto/parser.js
DELETED
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'module'
|
|
2
|
-
import { writeFileSync, renameSync } from 'fs'
|
|
3
|
-
import { join, dirname } from 'path'
|
|
4
|
-
import { fileURLToPath } from 'url'
|
|
5
|
-
import logger from '../lib/Utils/logger.js'
|
|
6
|
-
|
|
7
|
-
const require = createRequire(import.meta.url)
|
|
8
|
-
const acorn = require('acorn')
|
|
9
|
-
const walk = require('acorn-walk')
|
|
10
|
-
|
|
11
|
-
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
12
|
-
const addPrefix = (lines, prefix) => lines.map(l => prefix + l)
|
|
13
|
-
|
|
14
|
-
function extractAllExpressions(node) {
|
|
15
|
-
const out = [node]
|
|
16
|
-
const exp = node.expression
|
|
17
|
-
if (exp) out.push(exp)
|
|
18
|
-
if (node?.expression?.arguments?.length)
|
|
19
|
-
for (const arg of node.expression.arguments)
|
|
20
|
-
if (arg?.body?.body?.length)
|
|
21
|
-
for (const e of arg.body.body) out.push(...extractAllExpressions(e))
|
|
22
|
-
if (node?.body?.body?.length)
|
|
23
|
-
for (const e of node.body.body)
|
|
24
|
-
if (e.expression) out.push(...extractAllExpressions(e.expression))
|
|
25
|
-
if (node.expression?.expressions?.length)
|
|
26
|
-
for (const e of node.expression.expressions) out.push(...extractAllExpressions(e))
|
|
27
|
-
return out
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function getNumericEnumValue(node) {
|
|
31
|
-
if (node?.type === 'Literal' && typeof node.value === 'number') return node.value
|
|
32
|
-
if (node?.type === 'UnaryExpression' && (node.operator === '-' || node.operator === '+') &&
|
|
33
|
-
node.argument?.type === 'Literal' && typeof node.argument.value === 'number')
|
|
34
|
-
return node.operator === '-' ? -node.argument.value : node.argument.value
|
|
35
|
-
return undefined
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function parseBundleSources(sources) {
|
|
39
|
-
const nodes = []
|
|
40
|
-
const opts = { ecmaVersion: 'latest', allowHashBang: true }
|
|
41
|
-
for (let i = 0; i < sources.length; i++) {
|
|
42
|
-
const patched = sources[i].replaceAll('LimitSharing$Trigger', 'LimitSharing$TriggerType')
|
|
43
|
-
sources[i] = null
|
|
44
|
-
try { nodes.push(...acorn.parse(patched, { ...opts, sourceType: 'script' }).body) }
|
|
45
|
-
catch {
|
|
46
|
-
try { nodes.push(...acorn.parse(patched, { ...opts, sourceType: 'module' }).body) }
|
|
47
|
-
catch (e) { logger.warn(`[WAProto] Skipping bundle ${i + 1}: ${e.message}`) }
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return nodes
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function extractProtoSources(bundle) {
|
|
54
|
-
const out = [], targets = new Set(['"$InternalEnum"', '"WAProtoConst"'])
|
|
55
|
-
let i = 0
|
|
56
|
-
while (i < bundle.length) {
|
|
57
|
-
const start = bundle.indexOf('__d(', i)
|
|
58
|
-
if (start === -1) break
|
|
59
|
-
const ns = bundle.indexOf('"', start + 4)
|
|
60
|
-
const name = bundle.slice(ns, bundle.indexOf('"', ns + 1) + 1)
|
|
61
|
-
if (targets.has(name) || name.endsWith('.pb"')) {
|
|
62
|
-
let depth = 0, j = start
|
|
63
|
-
while (j < bundle.length) {
|
|
64
|
-
if (bundle[j] === '(') depth++
|
|
65
|
-
else if (bundle[j] === ')' && --depth === 0) { out.push(bundle.slice(start, j + 1)); break }
|
|
66
|
-
j++
|
|
67
|
-
}
|
|
68
|
-
i = j + 1
|
|
69
|
-
} else i = start + 4
|
|
70
|
-
}
|
|
71
|
-
return out.length ? out : [bundle]
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function filterProtoModules(allNodes) {
|
|
75
|
-
return allNodes.filter(m => extractAllExpressions(m).find(e => e?.left?.property?.name === 'internalSpec'))
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function extractSchema(bundleSources) {
|
|
79
|
-
const allNodes = parseBundleSources(extractProtoSources(Array.isArray(bundleSources) ? bundleSources.join('') : bundleSources))
|
|
80
|
-
const modules = filterProtoModules(allNodes)
|
|
81
|
-
|
|
82
|
-
const unspecName = n => n.endsWith('Spec') ? n.slice(0, -4) : n
|
|
83
|
-
const getNesting = n => n.split('$').slice(0, -1).join('$')
|
|
84
|
-
const rename = n => unspecName(n)
|
|
85
|
-
|
|
86
|
-
const modulesInfo = {}
|
|
87
|
-
const moduleIndentationMap = {}
|
|
88
|
-
|
|
89
|
-
modules.forEach(module => {
|
|
90
|
-
const modName = module?.expression?.arguments?.[0]?.value ?? `__anon_${Math.random()}`
|
|
91
|
-
modulesInfo[modName] = { crossRefs: [] }
|
|
92
|
-
walk.simple(module, {
|
|
93
|
-
AssignmentExpression(node) {
|
|
94
|
-
if (node?.right?.type === 'CallExpression' &&
|
|
95
|
-
node?.right?.arguments?.length === 1 &&
|
|
96
|
-
node?.right?.arguments[0]?.type !== 'ObjectExpression')
|
|
97
|
-
modulesInfo[modName].crossRefs.push({ alias: node.left?.name, module: node.right.arguments[0].value })
|
|
98
|
-
},
|
|
99
|
-
})
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
for (const mod of modules) {
|
|
103
|
-
const modName = mod?.expression?.arguments?.[0]?.value
|
|
104
|
-
const modInfo = modulesInfo[modName]
|
|
105
|
-
const assignments = []
|
|
106
|
-
|
|
107
|
-
walk.simple(mod, {
|
|
108
|
-
AssignmentExpression(node) {
|
|
109
|
-
const left = node.left
|
|
110
|
-
if (left?.property?.name &&
|
|
111
|
-
left.property.name !== 'internalSpec' &&
|
|
112
|
-
left.property.name !== 'internalDefaults' &&
|
|
113
|
-
left.property.name !== 'name') assignments.push(left)
|
|
114
|
-
},
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
const makeBlankIdent = a => {
|
|
118
|
-
const key = rename(a?.property?.name)
|
|
119
|
-
const indentation = getNesting(key)
|
|
120
|
-
moduleIndentationMap[key] = moduleIndentationMap[key] || {}
|
|
121
|
-
moduleIndentationMap[key].indentation = indentation
|
|
122
|
-
if (indentation.length) {
|
|
123
|
-
moduleIndentationMap[indentation] = moduleIndentationMap[indentation] || {}
|
|
124
|
-
moduleIndentationMap[indentation].members = moduleIndentationMap[indentation].members || new Set()
|
|
125
|
-
moduleIndentationMap[indentation].members.add(key)
|
|
126
|
-
}
|
|
127
|
-
return [key, { name: key }]
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
modInfo.identifiers = Object.fromEntries(assignments.map(makeBlankIdent).reverse())
|
|
131
|
-
|
|
132
|
-
const enumAliases = {}
|
|
133
|
-
walk.ancestor(mod, {
|
|
134
|
-
Property(node, anc) {
|
|
135
|
-
const fatherNode = anc[anc.length - 3]
|
|
136
|
-
const fatherFather = anc[anc.length - 4]
|
|
137
|
-
if (fatherNode?.type === 'AssignmentExpression' &&
|
|
138
|
-
fatherNode?.left?.property?.name === 'internalSpec' &&
|
|
139
|
-
fatherNode?.right?.properties?.length) {
|
|
140
|
-
const values = fatherNode.right.properties.map(p => ({ name: p.key.name, id: p.value.value }))
|
|
141
|
-
enumAliases[fatherNode?.left?.name] = values
|
|
142
|
-
} else if (fatherNode?.type === 'VariableDeclarator' &&
|
|
143
|
-
fatherNode?.init?.type === 'ObjectExpression' &&
|
|
144
|
-
fatherNode.init.properties.length &&
|
|
145
|
-
fatherNode.init.properties.every(p => getNumericEnumValue(p.value) !== undefined)) {
|
|
146
|
-
const values = fatherNode.init.properties.map(p => ({ name: p.key.name || p.key.value, id: getNumericEnumValue(p.value) }))
|
|
147
|
-
enumAliases[fatherNode.id?.name] = values
|
|
148
|
-
} else if (node?.key?.name && fatherNode?.arguments?.length > 0) {
|
|
149
|
-
const values = fatherNode.arguments?.[0]?.properties?.map(p => ({ name: p.key.name, id: p.value.value }))
|
|
150
|
-
const nameAlias = fatherFather?.left?.name || fatherFather?.id?.name
|
|
151
|
-
if (nameAlias) enumAliases[nameAlias] = values
|
|
152
|
-
}
|
|
153
|
-
},
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
walk.simple(mod, {
|
|
157
|
-
AssignmentExpression(node) {
|
|
158
|
-
if (node.left?.type === 'MemberExpression' && modInfo.identifiers?.[rename(node.left.property?.name)]) {
|
|
159
|
-
const ident = modInfo.identifiers[rename(node.left.property.name)]
|
|
160
|
-
ident.alias = node.right?.name
|
|
161
|
-
ident.enumValues = enumAliases[ident.alias]
|
|
162
|
-
}
|
|
163
|
-
},
|
|
164
|
-
})
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const findByAlias = (identifiers, alias) => Object.values(identifiers).find(item => item.alias === alias)
|
|
168
|
-
|
|
169
|
-
for (const mod of modules) {
|
|
170
|
-
const modName = mod?.expression?.arguments?.[0]?.value
|
|
171
|
-
const modInfo = modulesInfo[modName]
|
|
172
|
-
|
|
173
|
-
walk.simple(mod, {
|
|
174
|
-
AssignmentExpression(node) {
|
|
175
|
-
if (node.left?.type !== 'MemberExpression' ||
|
|
176
|
-
node.left.property?.name !== 'internalSpec' ||
|
|
177
|
-
node.right?.type !== 'ObjectExpression') return
|
|
178
|
-
|
|
179
|
-
const targetIdent = Object.values(modInfo.identifiers).find(v => v.alias === node.left.object?.name)
|
|
180
|
-
if (!targetIdent) return
|
|
181
|
-
|
|
182
|
-
const constraints = [], rawMembers = []
|
|
183
|
-
for (const p of node.right.properties) {
|
|
184
|
-
p.key.name = p.key.type === 'Identifier' ? p.key.name : p.key.value
|
|
185
|
-
; (p.key.name.substring(0, 2) === '__' ? constraints : rawMembers).push(p)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
let members = rawMembers.map(({ key: { name }, value: { elements } }) => {
|
|
189
|
-
let type
|
|
190
|
-
const flags = []
|
|
191
|
-
const unwrapBinaryOr = n =>
|
|
192
|
-
n.type === 'BinaryExpression' && n.operator === '|'
|
|
193
|
-
? [].concat(unwrapBinaryOr(n.left), unwrapBinaryOr(n.right))
|
|
194
|
-
: [n]
|
|
195
|
-
|
|
196
|
-
unwrapBinaryOr(elements[1]).forEach(m => {
|
|
197
|
-
if (m.type !== 'MemberExpression' || m.object?.type !== 'MemberExpression') return
|
|
198
|
-
if (m.object.property?.name === 'TYPES') {
|
|
199
|
-
type = m.property.name.toLowerCase()
|
|
200
|
-
if (type === 'map' && elements[2]?.type === 'ArrayExpression') {
|
|
201
|
-
let typeStr = 'map<'
|
|
202
|
-
elements[2].elements.forEach((el, i) => {
|
|
203
|
-
typeStr += el?.property?.name ? el.property.name.toLowerCase()
|
|
204
|
-
: (findByAlias(modInfo.identifiers, el.name)?.name ?? el.name)
|
|
205
|
-
if (i < elements[2].elements.length - 1) typeStr += ', '
|
|
206
|
-
})
|
|
207
|
-
type = typeStr + '>'
|
|
208
|
-
}
|
|
209
|
-
} else if (m.object.property?.name === 'FLAGS') {
|
|
210
|
-
flags.push(m.property.name.toLowerCase())
|
|
211
|
-
}
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
if (type === 'message' || type === 'enum') {
|
|
215
|
-
if (elements[2]?.type === 'Identifier') {
|
|
216
|
-
const found = Object.values(modInfo.identifiers).find(v => v.alias === elements[2].name)
|
|
217
|
-
type = found?.name
|
|
218
|
-
} else if (elements[2]?.type === 'MemberExpression') {
|
|
219
|
-
const targetAlias = elements[2]?.object?.name || elements[2]?.object?.left?.name || elements[2]?.object?.callee?.name
|
|
220
|
-
const crossRef = modInfo.crossRefs.find(r => r.alias === targetAlias)
|
|
221
|
-
if (elements[1]?.property?.name === 'ENUM' && elements[2]?.property?.name?.includes('Type'))
|
|
222
|
-
type = rename(elements[2].property.name)
|
|
223
|
-
else if (elements[2]?.property?.name?.includes('Spec'))
|
|
224
|
-
type = rename(elements[2].property.name)
|
|
225
|
-
else if (crossRef && crossRef.module !== '$InternalEnum' && modulesInfo[crossRef.module]?.identifiers?.[rename(elements[2].property.name)])
|
|
226
|
-
type = rename(elements[2].property.name)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return { name, id: elements[0].value, type, flags }
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
constraints.forEach(c => {
|
|
234
|
-
if (c.key.name === '__oneofs__' && c.value.type === 'ObjectExpression') {
|
|
235
|
-
const oneofs = c.value.properties.map(p => ({
|
|
236
|
-
name: p.key.name, type: '__oneof__',
|
|
237
|
-
members: p.value.elements.map(e => {
|
|
238
|
-
const idx = members.findIndex(m => m.name === e.value)
|
|
239
|
-
const member = members[idx]
|
|
240
|
-
members.splice(idx, 1)
|
|
241
|
-
return member
|
|
242
|
-
}),
|
|
243
|
-
}))
|
|
244
|
-
members.push(...oneofs)
|
|
245
|
-
}
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
targetIdent.members = members
|
|
249
|
-
},
|
|
250
|
-
})
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return { modulesInfo, moduleIndentationMap }
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
export function generateProto3(modulesInfo, moduleIndentationMap, version) {
|
|
257
|
-
const indent = ' '
|
|
258
|
-
const unnest = n => n.split('$').slice(-1)[0]
|
|
259
|
-
|
|
260
|
-
const stringifyMember = (info, completeFlags, parentName) => {
|
|
261
|
-
if (info.type === '__oneof__')
|
|
262
|
-
return [`oneof ${info.name} {`, ...addPrefix([].concat(...info.members.map(m => stringifyMember(m, false))), indent), '}']
|
|
263
|
-
if (info.flags.includes('packed')) { info.flags.splice(info.flags.indexOf('packed'), 1); info.packed = ' [packed=true]' }
|
|
264
|
-
const reqIdx = info.flags.indexOf('required'); if (reqIdx !== -1) info.flags[reqIdx] = 'optional'
|
|
265
|
-
if (completeFlags && !info.flags.length && info.type && !info.type.includes('map')) info.flags.push('optional')
|
|
266
|
-
const indentation = moduleIndentationMap[info.type]?.indentation
|
|
267
|
-
let typeName = unnest(info.type || 'bytes')
|
|
268
|
-
if (indentation !== parentName && indentation) typeName = `${indentation.replaceAll('$', '.')}.${typeName}`
|
|
269
|
-
return [`${info.flags.join(' ')}${info.flags.length ? ' ' : ''}${typeName} ${info.name} = ${info.id}${info.packed || ''};`]
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const decodedProtoMap = {}
|
|
273
|
-
|
|
274
|
-
for (const [, modInfo] of Object.entries(modulesInfo)) {
|
|
275
|
-
if (!modInfo.identifiers) continue
|
|
276
|
-
|
|
277
|
-
const stringifyEnum = (ident, overrideName = null) => [
|
|
278
|
-
`enum ${overrideName || ident.displayName || ident.name} {`,
|
|
279
|
-
...addPrefix(ident.enumValues.map(v => `${v.name} = ${v.id};`), indent),
|
|
280
|
-
'}',
|
|
281
|
-
]
|
|
282
|
-
|
|
283
|
-
function stringifyMessage(ident) {
|
|
284
|
-
const nestedKeys = moduleIndentationMap[ident.name]?.members
|
|
285
|
-
const result = [
|
|
286
|
-
`message ${ident.displayName || ident.name} {`,
|
|
287
|
-
...addPrefix([].concat(...ident.members.map(m => stringifyMember(m, true, ident.name))), indent),
|
|
288
|
-
]
|
|
289
|
-
if (nestedKeys?.size) {
|
|
290
|
-
for (const memberName of Array.from(nestedKeys).sort()) {
|
|
291
|
-
let entity = modInfo.identifiers[memberName]
|
|
292
|
-
if (entity) {
|
|
293
|
-
entity = { ...entity, displayName: entity.name.slice(ident.name.length + 1) }
|
|
294
|
-
result.push(...addPrefix(getEntity(entity), indent))
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
result.push('}', '')
|
|
299
|
-
return result
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
const getEntity = v => {
|
|
303
|
-
if (v.members) return stringifyMessage(v)
|
|
304
|
-
if (v.enumValues?.length) return stringifyEnum(v)
|
|
305
|
-
return [`// Unknown entity ${v.name}`]
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
for (const v of Object.values(modInfo.identifiers))
|
|
309
|
-
if (!moduleIndentationMap[v.name]?.indentation?.length)
|
|
310
|
-
decodedProtoMap[v.name] = getEntity(v).join('\n')
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
const body = Object.keys(decodedProtoMap).sort().map(k => decodedProtoMap[k]).join('\n')
|
|
314
|
-
return `syntax = "proto3";\npackage proto;\n${version ? `\n/// WhatsApp Version: ${version}\n` : ''}\n${body}`
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
export function parseBundle(bundle, version) {
|
|
318
|
-
const sources = Array.isArray(bundle) ? bundle : [bundle]
|
|
319
|
-
const { modulesInfo, moduleIndentationMap } = extractSchema(sources)
|
|
320
|
-
return generateProto3(modulesInfo, moduleIndentationMap, version)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
export async function parseAndWriteProto(bundle, version, outPath = PROTO_FILE) {
|
|
324
|
-
const protoText = parseBundle(bundle, version)
|
|
325
|
-
const tmp = outPath + '.tmp'
|
|
326
|
-
writeFileSync(tmp, protoText, 'utf8')
|
|
327
|
-
renameSync(tmp, outPath)
|
|
328
|
-
const unknown = [...protoText.matchAll(/\/\/ Unknown entity (.+)/g)].map(m => m[1])
|
|
329
|
-
if (unknown.length) logger.warn(`[WAProto] ${unknown.length} unknown entities: ${unknown.join(', ')}`)
|
|
330
|
-
return {
|
|
331
|
-
messageCount: Object.values(modulesInfo).reduce((s, m) => s + Object.keys(m.identifiers || {}).length, 0),
|
|
332
|
-
unknownCount: unknown.length,
|
|
333
|
-
}
|
|
334
|
-
}
|