@skycodee/baileys 8.0.4 → 8.0.5

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.
Files changed (224) hide show
  1. package/LICENSE +1 -1
  2. package/WAProto/WAProto.proto +352 -520
  3. package/WAProto/index.js +558 -4154
  4. package/lib/Defaults/index.js +123 -0
  5. package/lib/Defaults/index.js.bak +123 -0
  6. package/lib/KeyDB/BinarySearch.js +20 -0
  7. package/lib/KeyDB/KeyedDB.js +167 -0
  8. package/lib/KeyDB/index.js +4 -0
  9. package/lib/Signal/Group/ciphertext-message.js +13 -0
  10. package/lib/Signal/Group/group-session-builder.js +32 -0
  11. package/lib/Signal/Group/group_cipher.js +84 -0
  12. package/lib/Signal/Group/index.js +13 -0
  13. package/lib/Signal/Group/keyhelper.js +20 -0
  14. package/lib/Signal/Group/sender-chain-key.js +28 -0
  15. package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
  16. package/lib/Signal/Group/sender-key-message.js +68 -0
  17. package/lib/Signal/Group/sender-key-name.js +52 -0
  18. package/lib/Signal/Group/sender-key-record.js +43 -0
  19. package/lib/Signal/Group/sender-key-state.js +86 -0
  20. package/lib/Signal/Group/sender-message-key.js +28 -0
  21. package/lib/Signal/libsignal.js +324 -0
  22. package/lib/Signal/lid-mapping.js +155 -0
  23. package/lib/Socket/Client/index.js +4 -0
  24. package/lib/Socket/Client/types.js +13 -0
  25. package/lib/Socket/Client/websocket.js +52 -0
  26. package/lib/Socket/Client/websocket.js.bak +53 -0
  27. package/lib/Socket/business.js +377 -0
  28. package/lib/Socket/chats.js +885 -0
  29. package/lib/Socket/communities.js +413 -0
  30. package/lib/Socket/groups.js +312 -0
  31. package/lib/Socket/index.js +16 -0
  32. package/lib/Socket/messages-recv.js +1163 -0
  33. package/lib/Socket/messages-send.js +1100 -0
  34. package/lib/Socket/mex.js +45 -0
  35. package/lib/Socket/newsletter.js +247 -0
  36. package/lib/Socket/socket.js +820 -0
  37. package/lib/Store/index.js +6 -0
  38. package/lib/Store/make-cache-manager-store.js +75 -0
  39. package/lib/Store/make-in-memory-store.js +290 -0
  40. package/lib/Store/make-ordered-dictionary.js +79 -0
  41. package/lib/Store/object-repository.js +25 -0
  42. package/lib/Types/Auth.js +3 -0
  43. package/lib/Types/Bussines.js +3 -0
  44. package/lib/Types/Call.js +3 -0
  45. package/lib/Types/Chat.js +9 -0
  46. package/lib/Types/Contact.js +3 -0
  47. package/lib/Types/Events.js +3 -0
  48. package/lib/Types/GroupMetadata.js +3 -0
  49. package/lib/Types/Label.js +25 -0
  50. package/lib/Types/LabelAssociation.js +7 -0
  51. package/lib/Types/Message.js +12 -0
  52. package/lib/Types/Newsletter.js +33 -0
  53. package/lib/Types/Newsletter.js.bak +33 -0
  54. package/lib/Types/Product.js +3 -0
  55. package/lib/Types/Signal.js +3 -0
  56. package/lib/Types/Socket.js +4 -0
  57. package/lib/Types/State.js +11 -0
  58. package/lib/Types/USync.js +3 -0
  59. package/lib/Types/index.js +28 -0
  60. package/lib/Utils/auth-utils.js +219 -0
  61. package/lib/Utils/baileys-event-stream.js +44 -0
  62. package/lib/Utils/browser-utils.js +25 -0
  63. package/lib/Utils/business.js +233 -0
  64. package/lib/Utils/chat-utils.js +752 -0
  65. package/lib/Utils/crypto.js +130 -0
  66. package/lib/Utils/decode-wa-message.js +267 -0
  67. package/lib/Utils/event-buffer.js +528 -0
  68. package/lib/Utils/generics.js +355 -0
  69. package/lib/Utils/history.js +87 -0
  70. package/lib/Utils/index.js +21 -0
  71. package/lib/Utils/link-preview.js +81 -0
  72. package/lib/Utils/logger.js +5 -0
  73. package/lib/Utils/lt-hash.js +45 -0
  74. package/lib/Utils/make-mutex.js +36 -0
  75. package/lib/Utils/message-retry-manager.js +113 -0
  76. package/lib/Utils/messages-media.js +627 -0
  77. package/lib/Utils/messages.js +909 -0
  78. package/lib/Utils/noise-handler.js +144 -0
  79. package/lib/Utils/pre-key-manager.js +85 -0
  80. package/lib/Utils/process-message.js +341 -0
  81. package/lib/Utils/signal.js +161 -0
  82. package/lib/Utils/use-multi-file-auth-state.js +111 -0
  83. package/lib/Utils/validate-connection.js +200 -0
  84. package/lib/WABinary/constants.js +1303 -0
  85. package/lib/WABinary/decode.js +240 -0
  86. package/lib/WABinary/encode.js +218 -0
  87. package/lib/WABinary/generic-utils.js +189 -0
  88. package/lib/WABinary/index.js +7 -0
  89. package/lib/WABinary/jid-utils.js +93 -0
  90. package/lib/WABinary/types.js +3 -0
  91. package/lib/WAM/BinaryInfo.js +11 -0
  92. package/lib/WAM/constants.js +22853 -0
  93. package/lib/WAM/encode.js +154 -0
  94. package/lib/WAM/index.js +5 -0
  95. package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -0
  96. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  97. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -0
  98. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
  99. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  100. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +30 -0
  101. package/lib/WAUSync/Protocols/index.js +6 -0
  102. package/lib/WAUSync/USyncQuery.js +90 -0
  103. package/lib/WAUSync/USyncUser.js +24 -0
  104. package/lib/WAUSync/index.js +5 -0
  105. package/lib/index.js +21 -0
  106. package/package.json +84 -57
  107. package/README.md +0 -1312
  108. package/WAProto/GenerateStatics.sh +0 -3
  109. package/WAProto/fix-imports.js +0 -81
  110. package/WAProto/index.d.ts +0 -14017
  111. package/engine-requirements.js +0 -10
  112. package/src/Defaults/baileys-version.json +0 -1
  113. package/src/Defaults/index.ts +0 -149
  114. package/src/Signal/Group/ciphertext-message.ts +0 -9
  115. package/src/Signal/Group/group-session-builder.ts +0 -56
  116. package/src/Signal/Group/group_cipher.ts +0 -117
  117. package/src/Signal/Group/index.ts +0 -11
  118. package/src/Signal/Group/keyhelper.ts +0 -28
  119. package/src/Signal/Group/sender-chain-key.ts +0 -34
  120. package/src/Signal/Group/sender-key-distribution-message.ts +0 -95
  121. package/src/Signal/Group/sender-key-message.ts +0 -96
  122. package/src/Signal/Group/sender-key-name.ts +0 -66
  123. package/src/Signal/Group/sender-key-record.ts +0 -69
  124. package/src/Signal/Group/sender-key-state.ts +0 -134
  125. package/src/Signal/Group/sender-message-key.ts +0 -36
  126. package/src/Signal/libsignal.ts +0 -521
  127. package/src/Signal/lid-mapping.ts +0 -328
  128. package/src/Socket/Client/index.ts +0 -2
  129. package/src/Socket/Client/types.ts +0 -22
  130. package/src/Socket/Client/websocket.ts +0 -63
  131. package/src/Socket/business.ts +0 -424
  132. package/src/Socket/chats.ts +0 -1272
  133. package/src/Socket/communities.ts +0 -477
  134. package/src/Socket/groups.ts +0 -361
  135. package/src/Socket/index.ts +0 -15
  136. package/src/Socket/messages-recv.ts +0 -1668
  137. package/src/Socket/messages-send.ts +0 -1296
  138. package/src/Socket/mex.ts +0 -58
  139. package/src/Socket/newsletter.ts +0 -229
  140. package/src/Socket/socket.ts +0 -1184
  141. package/src/Types/Auth.ts +0 -116
  142. package/src/Types/Bussines.ts +0 -20
  143. package/src/Types/Call.ts +0 -15
  144. package/src/Types/Chat.ts +0 -138
  145. package/src/Types/Contact.ts +0 -24
  146. package/src/Types/Events.ts +0 -159
  147. package/src/Types/GroupMetadata.ts +0 -70
  148. package/src/Types/Label.ts +0 -48
  149. package/src/Types/LabelAssociation.ts +0 -35
  150. package/src/Types/Message.ts +0 -380
  151. package/src/Types/Newsletter.ts +0 -98
  152. package/src/Types/Product.ts +0 -85
  153. package/src/Types/Signal.ts +0 -76
  154. package/src/Types/Socket.ts +0 -150
  155. package/src/Types/State.ts +0 -43
  156. package/src/Types/USync.ts +0 -27
  157. package/src/Types/globals.d.ts +0 -8
  158. package/src/Types/index.ts +0 -67
  159. package/src/Utils/auth-utils.ts +0 -368
  160. package/src/Utils/browser-utils.ts +0 -31
  161. package/src/Utils/business.ts +0 -286
  162. package/src/Utils/chat-utils.ts +0 -974
  163. package/src/Utils/crypto.ts +0 -147
  164. package/src/Utils/decode-wa-message.ts +0 -355
  165. package/src/Utils/event-buffer.ts +0 -692
  166. package/src/Utils/generics.ts +0 -474
  167. package/src/Utils/history.ts +0 -161
  168. package/src/Utils/identity-change-handler.ts +0 -83
  169. package/src/Utils/index.ts +0 -19
  170. package/src/Utils/link-preview.ts +0 -111
  171. package/src/Utils/logger.ts +0 -13
  172. package/src/Utils/lt-hash.ts +0 -8
  173. package/src/Utils/make-mutex.ts +0 -42
  174. package/src/Utils/message-retry-manager.ts +0 -295
  175. package/src/Utils/messages-media.ts +0 -984
  176. package/src/Utils/messages.ts +0 -1097
  177. package/src/Utils/noise-handler.ts +0 -268
  178. package/src/Utils/pre-key-manager.ts +0 -126
  179. package/src/Utils/process-message.ts +0 -670
  180. package/src/Utils/reporting-utils.ts +0 -344
  181. package/src/Utils/signal.ts +0 -214
  182. package/src/Utils/sync-action-utils.ts +0 -74
  183. package/src/Utils/tc-token-utils.ts +0 -34
  184. package/src/Utils/use-multi-file-auth-state.ts +0 -136
  185. package/src/Utils/validate-connection.ts +0 -253
  186. package/src/WABinary/constants.ts +0 -1305
  187. package/src/WABinary/decode.ts +0 -308
  188. package/src/WABinary/encode.ts +0 -258
  189. package/src/WABinary/generic-utils.ts +0 -141
  190. package/src/WABinary/index.ts +0 -5
  191. package/src/WABinary/jid-utils.ts +0 -128
  192. package/src/WABinary/types.ts +0 -17
  193. package/src/WAM/BinaryInfo.ts +0 -12
  194. package/src/WAM/constants.ts +0 -22889
  195. package/src/WAM/encode.ts +0 -169
  196. package/src/WAM/index.ts +0 -3
  197. package/src/WAUSync/Protocols/USyncContactProtocol.ts +0 -32
  198. package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +0 -78
  199. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +0 -35
  200. package/src/WAUSync/Protocols/USyncStatusProtocol.ts +0 -44
  201. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +0 -76
  202. package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +0 -33
  203. package/src/WAUSync/Protocols/index.ts +0 -4
  204. package/src/WAUSync/USyncQuery.ts +0 -133
  205. package/src/WAUSync/USyncUser.ts +0 -32
  206. package/src/WAUSync/index.ts +0 -3
  207. package/src/__tests__/Signal/Group/sender-key-state-regression.test.ts +0 -22
  208. package/src/__tests__/Signal/lid-mapping.test.ts +0 -47
  209. package/src/__tests__/Socket/identity-change-handling.test.ts +0 -207
  210. package/src/__tests__/TestUtils/session.ts +0 -47
  211. package/src/__tests__/Utils/generics.test.ts +0 -70
  212. package/src/__tests__/Utils/history.test.ts +0 -362
  213. package/src/__tests__/Utils/messages-media.test.ts +0 -405
  214. package/src/__tests__/Utils/noise-handler.test.ts +0 -339
  215. package/src/__tests__/Utils/process-message.test.ts +0 -138
  216. package/src/__tests__/Utils/process-sync-action.test.ts +0 -359
  217. package/src/__tests__/Utils/reporting-utils.test.ts +0 -160
  218. package/src/__tests__/Utils/signal.test.ts +0 -69
  219. package/src/__tests__/Utils/sync-action-utils.test.ts +0 -183
  220. package/src/__tests__/binary/connection-deadlock.test.ts +0 -54
  221. package/src/__tests__/e2e/fetch-wa-web-version.test-e2e.ts +0 -60
  222. package/src/__tests__/e2e/send-receive-message.test-e2e.ts +0 -644
  223. package/src/__tests__/proto-tojson-long.test.ts +0 -31
  224. package/src/index.ts +0 -13
@@ -1,308 +0,0 @@
1
- import { promisify } from 'util'
2
- import { inflate } from 'zlib'
3
- import * as constants from './constants'
4
- import { jidEncode, type JidServer, WAJIDDomains } from './jid-utils'
5
- import type { BinaryNode, BinaryNodeCodingOptions } from './types'
6
-
7
- const inflatePromise = promisify(inflate)
8
-
9
- export const decompressingIfRequired = async (buffer: Buffer) => {
10
- if (2 & buffer.readUInt8()) {
11
- buffer = await inflatePromise(buffer.slice(1))
12
- } else {
13
- // nodes with no compression have a 0x00 prefix, we remove that
14
- buffer = buffer.slice(1)
15
- }
16
-
17
- return buffer
18
- }
19
-
20
- export const decodeDecompressedBinaryNode = (
21
- buffer: Buffer,
22
- opts: Pick<BinaryNodeCodingOptions, 'DOUBLE_BYTE_TOKENS' | 'SINGLE_BYTE_TOKENS' | 'TAGS'>,
23
- indexRef: { index: number } = { index: 0 }
24
- ): BinaryNode => {
25
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
26
-
27
- const checkEOS = (length: number) => {
28
- if (indexRef.index + length > buffer.length) {
29
- throw new Error('end of stream')
30
- }
31
- }
32
-
33
- const next = () => {
34
- const value = buffer[indexRef.index]
35
- indexRef.index += 1
36
- return value
37
- }
38
-
39
- const readByte = () => {
40
- checkEOS(1)
41
- return next()
42
- }
43
-
44
- const readBytes = (n: number) => {
45
- checkEOS(n)
46
- const value = buffer.slice(indexRef.index, indexRef.index + n)
47
- indexRef.index += n
48
- return value
49
- }
50
-
51
- const readStringFromChars = (length: number) => {
52
- return readBytes(length).toString('utf-8')
53
- }
54
-
55
- const readInt = (n: number, littleEndian = false) => {
56
- checkEOS(n)
57
- let val = 0
58
- for (let i = 0; i < n; i++) {
59
- const shift = littleEndian ? i : n - 1 - i
60
- val |= next()! << (shift * 8)
61
- }
62
-
63
- return val
64
- }
65
-
66
- const readInt20 = () => {
67
- checkEOS(3)
68
- return ((next()! & 15) << 16) + (next()! << 8) + next()!
69
- }
70
-
71
- const unpackHex = (value: number) => {
72
- if (value >= 0 && value < 16) {
73
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
74
- }
75
-
76
- throw new Error('invalid hex: ' + value)
77
- }
78
-
79
- const unpackNibble = (value: number) => {
80
- if (value >= 0 && value <= 9) {
81
- return '0'.charCodeAt(0) + value
82
- }
83
-
84
- switch (value) {
85
- case 10:
86
- return '-'.charCodeAt(0)
87
- case 11:
88
- return '.'.charCodeAt(0)
89
- case 15:
90
- return '\0'.charCodeAt(0)
91
- default:
92
- throw new Error('invalid nibble: ' + value)
93
- }
94
- }
95
-
96
- const unpackByte = (tag: number, value: number) => {
97
- if (tag === TAGS.NIBBLE_8) {
98
- return unpackNibble(value)
99
- } else if (tag === TAGS.HEX_8) {
100
- return unpackHex(value)
101
- } else {
102
- throw new Error('unknown tag: ' + tag)
103
- }
104
- }
105
-
106
- const readPacked8 = (tag: number) => {
107
- const startByte = readByte()!
108
- let value = ''
109
-
110
- for (let i = 0; i < (startByte & 127); i++) {
111
- const curByte = readByte()!
112
-
113
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
114
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
115
- }
116
-
117
- if (startByte >> 7 !== 0) {
118
- value = value.slice(0, -1)
119
- }
120
-
121
- return value
122
- }
123
-
124
- const isListTag = (tag: number) => {
125
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
126
- }
127
-
128
- const readListSize = (tag: number) => {
129
- switch (tag) {
130
- case TAGS.LIST_EMPTY:
131
- return 0
132
- case TAGS.LIST_8:
133
- return readByte()
134
- case TAGS.LIST_16:
135
- return readInt(2)
136
- default:
137
- throw new Error('invalid tag for list size: ' + tag)
138
- }
139
- }
140
-
141
- const readJidPair = () => {
142
- const i = readString(readByte()!)
143
- const j = readString(readByte()!)
144
- if (j) {
145
- return (i || '') + '@' + j
146
- }
147
-
148
- throw new Error('invalid jid pair: ' + i + ', ' + j)
149
- }
150
-
151
- const readAdJid = () => {
152
- const rawDomainType = readByte()
153
- const domainType = Number(rawDomainType)
154
-
155
- const device = readByte()
156
- const user = readString(readByte()!)
157
-
158
- let server: JidServer = 's.whatsapp.net' // default whatsapp server
159
- if (domainType === WAJIDDomains.LID) {
160
- server = 'lid'
161
- } else if (domainType === WAJIDDomains.HOSTED) {
162
- server = 'hosted'
163
- } else if (domainType === WAJIDDomains.HOSTED_LID) {
164
- server = 'hosted.lid'
165
- }
166
-
167
- return jidEncode(user, server, device)
168
- }
169
-
170
- const readFbJid = () => {
171
- const user = readString(readByte()!)
172
- const device = readInt(2)
173
- const server = readString(readByte()!)
174
- return `${user}:${device}@${server}`
175
- }
176
-
177
- const readInteropJid = () => {
178
- const user = readString(readByte()!)
179
- const device = readInt(2)
180
- const integrator = readInt(2)
181
-
182
- let server = 'interop'
183
- const beforeServer = indexRef.index
184
- try {
185
- server = readString(readByte()!)
186
- } catch (err) {
187
- indexRef.index = beforeServer
188
- }
189
-
190
- return `${integrator}-${user}:${device}@${server}`
191
- }
192
-
193
- const readString = (tag: number): string => {
194
- if (tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
195
- return SINGLE_BYTE_TOKENS[tag] || ''
196
- }
197
-
198
- switch (tag) {
199
- case TAGS.DICTIONARY_0:
200
- case TAGS.DICTIONARY_1:
201
- case TAGS.DICTIONARY_2:
202
- case TAGS.DICTIONARY_3:
203
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte()!)
204
- case TAGS.LIST_EMPTY:
205
- return ''
206
- case TAGS.BINARY_8:
207
- return readStringFromChars(readByte()!)
208
- case TAGS.BINARY_20:
209
- return readStringFromChars(readInt20())
210
- case TAGS.BINARY_32:
211
- return readStringFromChars(readInt(4))
212
- case TAGS.JID_PAIR:
213
- return readJidPair()
214
- case TAGS.FB_JID:
215
- return readFbJid()
216
- case TAGS.INTEROP_JID:
217
- return readInteropJid()
218
- case TAGS.AD_JID:
219
- return readAdJid()
220
- case TAGS.HEX_8:
221
- case TAGS.NIBBLE_8:
222
- return readPacked8(tag)
223
- default:
224
- throw new Error('invalid string with tag: ' + tag)
225
- }
226
- }
227
-
228
- const readList = (tag: number) => {
229
- const items: BinaryNode[] = []
230
- const size = readListSize(tag)!
231
- for (let i = 0; i < size; i++) {
232
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
233
- }
234
-
235
- return items
236
- }
237
-
238
- const getTokenDouble = (index1: number, index2: number) => {
239
- const dict = DOUBLE_BYTE_TOKENS[index1]
240
- if (!dict) {
241
- throw new Error(`Invalid double token dict (${index1})`)
242
- }
243
-
244
- const value = dict[index2]
245
- if (typeof value === 'undefined') {
246
- throw new Error(`Invalid double token (${index2})`)
247
- }
248
-
249
- return value
250
- }
251
-
252
- const listSize = readListSize(readByte()!)
253
- const header = readString(readByte()!)
254
- if (!listSize || !header.length) {
255
- throw new Error('invalid node')
256
- }
257
-
258
- const attrs: BinaryNode['attrs'] = {}
259
- let data: BinaryNode['content']
260
- if (listSize === 0 || !header) {
261
- throw new Error('invalid node')
262
- }
263
-
264
- // read the attributes in
265
- const attributesLength = (listSize - 1) >> 1
266
- for (let i = 0; i < attributesLength; i++) {
267
- const key = readString(readByte()!)
268
- const value = readString(readByte()!)
269
-
270
- attrs[key] = value
271
- }
272
-
273
- if (listSize % 2 === 0) {
274
- const tag = readByte()!
275
- if (isListTag(tag)) {
276
- data = readList(tag)
277
- } else {
278
- let decoded: Buffer | string
279
- switch (tag) {
280
- case TAGS.BINARY_8:
281
- decoded = readBytes(readByte()!)
282
- break
283
- case TAGS.BINARY_20:
284
- decoded = readBytes(readInt20())
285
- break
286
- case TAGS.BINARY_32:
287
- decoded = readBytes(readInt(4))
288
- break
289
- default:
290
- decoded = readString(tag)
291
- break
292
- }
293
-
294
- data = decoded
295
- }
296
- }
297
-
298
- return {
299
- tag: header,
300
- attrs,
301
- content: data
302
- }
303
- }
304
-
305
- export const decodeBinaryNode = async (buff: Buffer): Promise<BinaryNode> => {
306
- const decompBuff = await decompressingIfRequired(buff)
307
- return decodeDecompressedBinaryNode(decompBuff, constants)
308
- }
@@ -1,258 +0,0 @@
1
- import * as constants from './constants'
2
- import { type FullJid, jidDecode } from './jid-utils'
3
- import type { BinaryNode, BinaryNodeCodingOptions } from './types'
4
-
5
- export const encodeBinaryNode = (
6
- node: BinaryNode,
7
- opts: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'> = constants,
8
- buffer: number[] = [0]
9
- ): Buffer => {
10
- const encoded = encodeBinaryNodeInner(node, opts, buffer)
11
- return Buffer.from(encoded)
12
- }
13
-
14
- const encodeBinaryNodeInner = (
15
- { tag, attrs, content }: BinaryNode,
16
- opts: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'>,
17
- buffer: number[]
18
- ): number[] => {
19
- const { TAGS, TOKEN_MAP } = opts
20
-
21
- const pushByte = (value: number) => buffer.push(value & 0xff)
22
-
23
- const pushInt = (value: number, n: number, littleEndian = false) => {
24
- for (let i = 0; i < n; i++) {
25
- const curShift = littleEndian ? i : n - 1 - i
26
- buffer.push((value >> (curShift * 8)) & 0xff)
27
- }
28
- }
29
-
30
- const pushBytes = (bytes: Uint8Array | Buffer | number[]) => {
31
- for (const b of bytes) {
32
- buffer.push(b)
33
- }
34
- }
35
-
36
- const pushInt16 = (value: number) => {
37
- pushBytes([(value >> 8) & 0xff, value & 0xff])
38
- }
39
-
40
- const pushInt20 = (value: number) => pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
41
- const writeByteLength = (length: number) => {
42
- if (length >= 4294967296) {
43
- throw new Error('string too large to encode: ' + length)
44
- }
45
-
46
- if (length >= 1 << 20) {
47
- pushByte(TAGS.BINARY_32)
48
- pushInt(length, 4) // 32 bit integer
49
- } else if (length >= 256) {
50
- pushByte(TAGS.BINARY_20)
51
- pushInt20(length)
52
- } else {
53
- pushByte(TAGS.BINARY_8)
54
- pushByte(length)
55
- }
56
- }
57
-
58
- const writeStringRaw = (str: string) => {
59
- const bytes = Buffer.from(str, 'utf-8')
60
- writeByteLength(bytes.length)
61
- pushBytes(bytes)
62
- }
63
-
64
- const writeJid = ({ domainType, device, user, server }: FullJid) => {
65
- if (typeof device !== 'undefined') {
66
- pushByte(TAGS.AD_JID)
67
- pushByte(domainType || 0)
68
- pushByte(device || 0)
69
- writeString(user)
70
- } else {
71
- pushByte(TAGS.JID_PAIR)
72
- if (user.length) {
73
- writeString(user)
74
- } else {
75
- pushByte(TAGS.LIST_EMPTY)
76
- }
77
-
78
- writeString(server)
79
- }
80
- }
81
-
82
- const packNibble = (char: string) => {
83
- switch (char) {
84
- case '-':
85
- return 10
86
- case '.':
87
- return 11
88
- case '\0':
89
- return 15
90
- default:
91
- if (char >= '0' && char <= '9') {
92
- return char.charCodeAt(0) - '0'.charCodeAt(0)
93
- }
94
-
95
- throw new Error(`invalid byte for nibble "${char}"`)
96
- }
97
- }
98
-
99
- const packHex = (char: string) => {
100
- if (char >= '0' && char <= '9') {
101
- return char.charCodeAt(0) - '0'.charCodeAt(0)
102
- }
103
-
104
- if (char >= 'A' && char <= 'F') {
105
- return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
106
- }
107
-
108
- if (char >= 'a' && char <= 'f') {
109
- return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
110
- }
111
-
112
- if (char === '\0') {
113
- return 15
114
- }
115
-
116
- throw new Error(`Invalid hex char "${char}"`)
117
- }
118
-
119
- const writePackedBytes = (str: string, type: 'nibble' | 'hex') => {
120
- if (str.length > TAGS.PACKED_MAX) {
121
- throw new Error('Too many bytes to pack')
122
- }
123
-
124
- pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
125
-
126
- let roundedLength = Math.ceil(str.length / 2.0)
127
- if (str.length % 2 !== 0) {
128
- roundedLength |= 128
129
- }
130
-
131
- pushByte(roundedLength)
132
- const packFunction = type === 'nibble' ? packNibble : packHex
133
-
134
- const packBytePair = (v1: string, v2: string) => {
135
- const result = (packFunction(v1) << 4) | packFunction(v2)
136
- return result
137
- }
138
-
139
- const strLengthHalf = Math.floor(str.length / 2)
140
- for (let i = 0; i < strLengthHalf; i++) {
141
- pushByte(packBytePair(str[2 * i]!, str[2 * i + 1]!))
142
- }
143
-
144
- if (str.length % 2 !== 0) {
145
- pushByte(packBytePair(str[str.length - 1]!, '\x00'))
146
- }
147
- }
148
-
149
- const isNibble = (str?: string) => {
150
- if (!str || str.length > TAGS.PACKED_MAX) {
151
- return false
152
- }
153
-
154
- for (const char of str) {
155
- const isInNibbleRange = char >= '0' && char <= '9'
156
- if (!isInNibbleRange && char !== '-' && char !== '.') {
157
- return false
158
- }
159
- }
160
-
161
- return true
162
- }
163
-
164
- const isHex = (str?: string) => {
165
- if (!str || str.length > TAGS.PACKED_MAX) {
166
- return false
167
- }
168
-
169
- for (const char of str) {
170
- const isInNibbleRange = char >= '0' && char <= '9'
171
- if (!isInNibbleRange && !(char >= 'A' && char <= 'F')) {
172
- return false
173
- }
174
- }
175
-
176
- return true
177
- }
178
-
179
- const writeString = (str?: string) => {
180
- if (str === undefined || str === null) {
181
- pushByte(TAGS.LIST_EMPTY)
182
- return
183
- }
184
-
185
- if (str === '') {
186
- writeStringRaw(str)
187
- return
188
- }
189
-
190
- const tokenIndex = TOKEN_MAP[str]
191
- if (tokenIndex) {
192
- if (typeof tokenIndex.dict === 'number') {
193
- pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
194
- }
195
-
196
- pushByte(tokenIndex.index)
197
- } else if (isNibble(str)) {
198
- writePackedBytes(str, 'nibble')
199
- } else if (isHex(str)) {
200
- writePackedBytes(str, 'hex')
201
- } else {
202
- const decodedJid = jidDecode(str)
203
- if (decodedJid) {
204
- writeJid(decodedJid)
205
- } else {
206
- writeStringRaw(str)
207
- }
208
- }
209
- }
210
-
211
- const writeListStart = (listSize: number) => {
212
- if (listSize === 0) {
213
- pushByte(TAGS.LIST_EMPTY)
214
- } else if (listSize < 256) {
215
- pushBytes([TAGS.LIST_8, listSize])
216
- } else {
217
- pushByte(TAGS.LIST_16)
218
- pushInt16(listSize)
219
- }
220
- }
221
-
222
- if (!tag) {
223
- throw new Error('Invalid node: tag cannot be undefined')
224
- }
225
-
226
- const validAttributes = Object.keys(attrs || {}).filter(k => typeof attrs[k] !== 'undefined' && attrs[k] !== null)
227
-
228
- writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0))
229
- writeString(tag)
230
-
231
- for (const key of validAttributes) {
232
- if (typeof attrs[key] === 'string') {
233
- writeString(key)
234
- writeString(attrs[key])
235
- }
236
- }
237
-
238
- if (typeof content === 'string') {
239
- writeString(content)
240
- } else if (Buffer.isBuffer(content) || content instanceof Uint8Array) {
241
- writeByteLength(content.length)
242
- pushBytes(content)
243
- } else if (Array.isArray(content)) {
244
- const validContent = content.filter(
245
- item => item && (item.tag || Buffer.isBuffer(item) || item instanceof Uint8Array || typeof item === 'string')
246
- )
247
- writeListStart(validContent.length)
248
- for (const item of validContent) {
249
- encodeBinaryNodeInner(item, opts, buffer)
250
- }
251
- } else if (typeof content === 'undefined') {
252
- // do nothing
253
- } else {
254
- throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
255
- }
256
-
257
- return buffer
258
- }
@@ -1,141 +0,0 @@
1
- import { Boom } from '@hapi/boom'
2
- import { proto } from '../../WAProto/index.js'
3
- import { type BinaryNode } from './types'
4
-
5
- // some extra useful utilities
6
-
7
- const indexCache = new WeakMap<BinaryNode, Map<string, BinaryNode[]>>()
8
-
9
- export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
10
- if (!node || !Array.isArray(node.content)) return []
11
-
12
- let index = indexCache.get(node)
13
-
14
- // Build the index once per node
15
- if (!index) {
16
- index = new Map<string, BinaryNode[]>()
17
-
18
- for (const child of node.content) {
19
- let arr = index.get(child.tag)
20
- if (!arr) index.set(child.tag, (arr = []))
21
- arr.push(child)
22
- }
23
-
24
- indexCache.set(node, index)
25
- }
26
-
27
- // Return first matching child
28
- return index.get(childTag) || []
29
- }
30
-
31
- export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
32
- return getBinaryNodeChildren(node, childTag)[0]
33
- }
34
-
35
- export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
36
- if (Array.isArray(content)) {
37
- return content
38
- }
39
-
40
- return []
41
- }
42
-
43
- export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
44
- const child = getBinaryNodeChild(node, childTag)?.content
45
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
46
- return child
47
- }
48
- }
49
-
50
- export const getBinaryNodeChildString = (node: BinaryNode | undefined, childTag: string) => {
51
- const child = getBinaryNodeChild(node, childTag)?.content
52
- if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
53
- return Buffer.from(child).toString('utf-8')
54
- } else if (typeof child === 'string') {
55
- return child
56
- }
57
- }
58
-
59
- export const getBinaryNodeChildUInt = (node: BinaryNode, childTag: string, length: number) => {
60
- const buff = getBinaryNodeChildBuffer(node, childTag)
61
- if (buff) {
62
- return bufferToUInt(buff, length)
63
- }
64
- }
65
-
66
- export const assertNodeErrorFree = (node: BinaryNode) => {
67
- const errNode = getBinaryNodeChild(node, 'error')
68
- if (errNode) {
69
- throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code! })
70
- }
71
- }
72
-
73
- export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => {
74
- const nodes = getBinaryNodeChildren(node, tag)
75
- const dict = nodes.reduce(
76
- (dict, { attrs }) => {
77
- if (typeof attrs.name === 'string') {
78
- dict[attrs.name] = attrs.value! || attrs.config_value!
79
- } else {
80
- dict[attrs.config_code!] = attrs.value! || attrs.config_value!
81
- }
82
-
83
- return dict
84
- },
85
- {} as { [_: string]: string }
86
- )
87
- return dict
88
- }
89
-
90
- export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
91
- const msgs: proto.WebMessageInfo[] = []
92
- if (Array.isArray(content)) {
93
- for (const item of content) {
94
- if (item.tag === 'message') {
95
- msgs.push(proto.WebMessageInfo.decode(item.content as Buffer).toJSON() as proto.WebMessageInfo)
96
- }
97
- }
98
- }
99
-
100
- return msgs
101
- }
102
-
103
- function bufferToUInt(e: Uint8Array | Buffer, t: number) {
104
- let a = 0
105
- for (let i = 0; i < t; i++) {
106
- a = 256 * a + e[i]!
107
- }
108
-
109
- return a
110
- }
111
-
112
- const tabs = (n: number) => '\t'.repeat(n)
113
-
114
- export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0): string {
115
- if (!node) {
116
- return node!
117
- }
118
-
119
- if (typeof node === 'string') {
120
- return tabs(i) + node
121
- }
122
-
123
- if (node instanceof Uint8Array) {
124
- return tabs(i) + Buffer.from(node).toString('hex')
125
- }
126
-
127
- if (Array.isArray(node)) {
128
- return node.map(x => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
129
- }
130
-
131
- const children = binaryNodeToString(node.content, i + 1)
132
-
133
- const tag = `<${node.tag} ${Object.entries(node.attrs || {})
134
- .filter(([, v]) => v !== undefined)
135
- .map(([k, v]) => `${k}='${v}'`)
136
- .join(' ')}`
137
-
138
- const content: string = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
139
-
140
- return tag + content
141
- }
@@ -1,5 +0,0 @@
1
- export * from './encode'
2
- export * from './decode'
3
- export * from './generic-utils'
4
- export * from './jid-utils'
5
- export * from './types'