gifted-baileys 1.5.6 → 1.5.7

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 (252) hide show
  1. package/README.md +9 -0
  2. package/WAProto/WAProto.proto +969 -88
  3. package/WAProto/index.d.ts +13199 -1260
  4. package/WAProto/index.js +124901 -74525
  5. package/lib/Defaults/baileys-version.json +3 -0
  6. package/lib/Defaults/index.d.ts +284 -0
  7. package/{src → lib}/Defaults/index.js +7 -14
  8. package/lib/Signal/libsignal.d.ts +3 -0
  9. package/lib/Signal/libsignal.js +161 -0
  10. package/lib/Socket/Client/abstract-socket-client.d.ts +15 -0
  11. package/lib/Socket/Client/index.d.ts +2 -0
  12. package/{src → lib}/Socket/Client/index.js +2 -3
  13. package/lib/Socket/Client/mobile-socket-client.d.ts +12 -0
  14. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  15. package/lib/Socket/Client/types.d.ts +17 -0
  16. package/lib/Socket/Client/types.js +13 -0
  17. package/lib/Socket/Client/websocket.d.ts +12 -0
  18. package/lib/Socket/Client/websocket.js +62 -0
  19. package/lib/Socket/business.d.ts +170 -0
  20. package/{src → lib}/Socket/business.js +28 -33
  21. package/lib/Socket/chats.d.ts +81 -0
  22. package/{src → lib}/Socket/chats.js +174 -176
  23. package/lib/Socket/groups.d.ts +115 -0
  24. package/{src → lib}/Socket/groups.js +80 -68
  25. package/lib/Socket/index.d.ts +172 -0
  26. package/{src → lib}/Socket/index.js +4 -1
  27. package/lib/Socket/messages-recv.d.ts +158 -0
  28. package/{src → lib}/Socket/messages-recv.js +378 -211
  29. package/lib/Socket/messages-send.d.ts +155 -0
  30. package/{src → lib}/Socket/messages-send.js +452 -177
  31. package/lib/Socket/newsletter.d.ts +132 -0
  32. package/{src → lib}/Socket/newsletter.js +107 -98
  33. package/lib/Socket/registration.d.ts +264 -0
  34. package/{src → lib}/Socket/registration.js +56 -48
  35. package/lib/Socket/socket.d.ts +44 -0
  36. package/{src → lib}/Socket/socket.js +77 -77
  37. package/lib/Socket/usync.d.ts +37 -0
  38. package/lib/Socket/usync.js +70 -0
  39. package/lib/Store/index.d.ts +3 -0
  40. package/lib/Store/make-cache-manager-store.d.ts +14 -0
  41. package/{src → lib}/Store/make-cache-manager-store.js +25 -34
  42. package/lib/Store/make-in-memory-store.d.ts +118 -0
  43. package/{src → lib}/Store/make-in-memory-store.js +36 -32
  44. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  45. package/lib/Store/object-repository.d.ts +10 -0
  46. package/{src → lib}/Store/object-repository.js +1 -1
  47. package/lib/Types/Auth.d.ts +109 -0
  48. package/lib/Types/Call.d.ts +13 -0
  49. package/lib/Types/Chat.d.ts +107 -0
  50. package/{src/Types/Contact.ts → lib/Types/Contact.d.ts} +8 -9
  51. package/lib/Types/Events.d.ts +172 -0
  52. package/lib/Types/GroupMetadata.d.ts +56 -0
  53. package/lib/Types/Label.d.ts +46 -0
  54. package/{src/Types/LabelAssociation.ts → lib/Types/LabelAssociation.d.ts} +16 -22
  55. package/lib/Types/Message.d.ts +433 -0
  56. package/lib/Types/Newsletter.d.ts +92 -0
  57. package/lib/Types/Product.d.ts +78 -0
  58. package/lib/Types/Signal.d.ts +57 -0
  59. package/{src/Types/Socket.ts → lib/Types/Socket.d.ts} +61 -68
  60. package/lib/Types/State.d.ts +27 -0
  61. package/lib/Types/USync.d.ts +25 -0
  62. package/lib/Types/index.d.ts +66 -0
  63. package/lib/Utils/auth-utils.d.ts +18 -0
  64. package/{src → lib}/Utils/auth-utils.js +73 -90
  65. package/lib/Utils/baileys-event-stream.d.ts +16 -0
  66. package/lib/Utils/baileys-event-stream.js +63 -0
  67. package/lib/Utils/business.d.ts +22 -0
  68. package/{src → lib}/Utils/business.js +15 -43
  69. package/lib/Utils/chat-utils.d.ts +70 -0
  70. package/{src → lib}/Utils/chat-utils.js +87 -94
  71. package/lib/Utils/crypto.d.ts +40 -0
  72. package/{src → lib}/Utils/crypto.js +4 -2
  73. package/lib/Utils/decode-wa-message.d.ts +36 -0
  74. package/lib/Utils/decode-wa-message.js +226 -0
  75. package/lib/Utils/event-buffer.d.ts +35 -0
  76. package/{src → lib}/Utils/event-buffer.js +4 -13
  77. package/lib/Utils/generics.d.ts +88 -0
  78. package/{src → lib}/Utils/generics.js +67 -86
  79. package/lib/Utils/history.d.ts +19 -0
  80. package/{src → lib}/Utils/history.js +13 -39
  81. package/lib/Utils/index.d.ts +17 -0
  82. package/lib/Utils/link-preview.d.ts +21 -0
  83. package/{src → lib}/Utils/link-preview.js +17 -54
  84. package/lib/Utils/logger.d.ts +2 -0
  85. package/lib/Utils/lt-hash.d.ts +12 -0
  86. package/lib/Utils/make-mutex.d.ts +7 -0
  87. package/{src → lib}/Utils/make-mutex.js +4 -13
  88. package/lib/Utils/messages-media.d.ts +113 -0
  89. package/{src → lib}/Utils/messages-media.js +193 -255
  90. package/lib/Utils/messages.d.ts +77 -0
  91. package/{src → lib}/Utils/messages.js +588 -118
  92. package/lib/Utils/noise-handler.d.ts +20 -0
  93. package/lib/Utils/process-message.d.ts +41 -0
  94. package/{src → lib}/Utils/process-message.js +27 -30
  95. package/lib/Utils/signal.d.ts +33 -0
  96. package/{src → lib}/Utils/signal.js +25 -42
  97. package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
  98. package/{src → lib}/Utils/use-multi-file-auth-state.js +27 -28
  99. package/lib/Utils/validate-connection.d.ts +11 -0
  100. package/{src → lib}/Utils/validate-connection.js +40 -9
  101. package/lib/WABinary/constants.d.ts +27 -0
  102. package/lib/WABinary/decode.d.ts +6 -0
  103. package/lib/WABinary/encode.d.ts +2 -0
  104. package/{src → lib}/WABinary/encode.js +16 -10
  105. package/lib/WABinary/generic-utils.d.ts +14 -0
  106. package/lib/WABinary/index.d.ts +5 -0
  107. package/lib/WABinary/jid-utils.d.ts +31 -0
  108. package/lib/WABinary/types.d.ts +18 -0
  109. package/lib/WABinary/types.js +2 -0
  110. package/lib/WAM/BinaryInfo.d.ts +8 -0
  111. package/lib/WAM/constants.d.ts +38 -0
  112. package/lib/WAM/encode.d.ts +2 -0
  113. package/lib/WAM/index.d.ts +3 -0
  114. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  115. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  116. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  117. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  118. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  119. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  120. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  121. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  122. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  123. package/lib/WAUSync/Protocols/index.js +20 -0
  124. package/lib/WAUSync/USyncQuery.d.ts +26 -0
  125. package/lib/WAUSync/USyncQuery.js +79 -0
  126. package/lib/WAUSync/USyncUser.d.ts +10 -0
  127. package/lib/WAUSync/USyncUser.js +22 -0
  128. package/lib/WAUSync/index.d.ts +3 -0
  129. package/lib/WAUSync/index.js +19 -0
  130. package/{src → lib}/index.js +1 -0
  131. package/package.json +26 -8
  132. package/LICENSE +0 -21
  133. package/src/Defaults/baileys-version.json +0 -3
  134. package/src/Defaults/index.ts +0 -131
  135. package/src/README.md +0 -1
  136. package/src/Signal/libsignal.js +0 -180
  137. package/src/Signal/libsignal.ts +0 -141
  138. package/src/Socket/Client/abstract-socket-client.ts +0 -19
  139. package/src/Socket/Client/index.ts +0 -3
  140. package/src/Socket/Client/mobile-socket-client.js +0 -78
  141. package/src/Socket/Client/mobile-socket-client.ts +0 -66
  142. package/src/Socket/Client/web-socket-client.js +0 -75
  143. package/src/Socket/Client/web-socket-client.ts +0 -57
  144. package/src/Socket/business.ts +0 -281
  145. package/src/Socket/chats.ts +0 -1030
  146. package/src/Socket/groups.ts +0 -356
  147. package/src/Socket/index.ts +0 -13
  148. package/src/Socket/messages-recv.ts +0 -985
  149. package/src/Socket/messages-send.ts +0 -871
  150. package/src/Socket/newsletter.ts +0 -282
  151. package/src/Socket/registration.ts +0 -250
  152. package/src/Socket/socket.ts +0 -777
  153. package/src/Store/index.ts +0 -3
  154. package/src/Store/make-cache-manager-store.ts +0 -100
  155. package/src/Store/make-in-memory-store.ts +0 -475
  156. package/src/Store/make-ordered-dictionary.ts +0 -86
  157. package/src/Store/object-repository.ts +0 -32
  158. package/src/Tests/test.app-state-sync.js +0 -204
  159. package/src/Tests/test.app-state-sync.ts +0 -207
  160. package/src/Tests/test.event-buffer.js +0 -270
  161. package/src/Tests/test.event-buffer.ts +0 -319
  162. package/src/Tests/test.key-store.js +0 -76
  163. package/src/Tests/test.key-store.ts +0 -92
  164. package/src/Tests/test.libsignal.js +0 -141
  165. package/src/Tests/test.libsignal.ts +0 -186
  166. package/src/Tests/test.media-download.js +0 -93
  167. package/src/Tests/test.media-download.ts +0 -76
  168. package/src/Tests/test.messages.js +0 -33
  169. package/src/Tests/test.messages.ts +0 -37
  170. package/src/Tests/utils.js +0 -34
  171. package/src/Tests/utils.ts +0 -36
  172. package/src/Types/Auth.ts +0 -113
  173. package/src/Types/Call.ts +0 -15
  174. package/src/Types/Chat.ts +0 -106
  175. package/src/Types/Events.ts +0 -93
  176. package/src/Types/GroupMetadata.ts +0 -53
  177. package/src/Types/Label.ts +0 -36
  178. package/src/Types/Message.ts +0 -288
  179. package/src/Types/Newsletter.ts +0 -98
  180. package/src/Types/Product.ts +0 -85
  181. package/src/Types/Signal.ts +0 -68
  182. package/src/Types/State.ts +0 -29
  183. package/src/Types/index.ts +0 -59
  184. package/src/Utils/auth-utils.ts +0 -222
  185. package/src/Utils/baileys-event-stream.js +0 -92
  186. package/src/Utils/baileys-event-stream.ts +0 -66
  187. package/src/Utils/business.ts +0 -275
  188. package/src/Utils/chat-utils.ts +0 -860
  189. package/src/Utils/crypto.ts +0 -131
  190. package/src/Utils/decode-wa-message.js +0 -211
  191. package/src/Utils/decode-wa-message.ts +0 -228
  192. package/src/Utils/event-buffer.ts +0 -613
  193. package/src/Utils/generics.ts +0 -434
  194. package/src/Utils/history.ts +0 -112
  195. package/src/Utils/index.ts +0 -17
  196. package/src/Utils/link-preview.ts +0 -122
  197. package/src/Utils/logger.ts +0 -3
  198. package/src/Utils/lt-hash.ts +0 -61
  199. package/src/Utils/make-mutex.ts +0 -44
  200. package/src/Utils/messages-media.ts +0 -847
  201. package/src/Utils/messages.ts +0 -956
  202. package/src/Utils/noise-handler.ts +0 -197
  203. package/src/Utils/process-message.ts +0 -414
  204. package/src/Utils/signal.ts +0 -177
  205. package/src/Utils/use-multi-file-auth-state.ts +0 -90
  206. package/src/Utils/validate-connection.ts +0 -238
  207. package/src/WABinary/constants.ts +0 -42
  208. package/src/WABinary/decode.ts +0 -265
  209. package/src/WABinary/encode.ts +0 -236
  210. package/src/WABinary/generic-utils.ts +0 -121
  211. package/src/WABinary/index.ts +0 -5
  212. package/src/WABinary/jid-utils.ts +0 -68
  213. package/src/WABinary/types.ts +0 -17
  214. package/src/WAM/BinaryInfo.ts +0 -12
  215. package/src/WAM/constants.ts +0 -15382
  216. package/src/WAM/encode.ts +0 -174
  217. package/src/WAM/index.ts +0 -3
  218. package/src/gifted +0 -1
  219. package/src/index.ts +0 -13
  220. /package/{src → lib}/Defaults/phonenumber-mcc.json +0 -0
  221. /package/{src → lib}/Socket/Client/abstract-socket-client.js +0 -0
  222. /package/{src → lib}/Store/index.js +0 -0
  223. /package/{src → lib}/Store/make-ordered-dictionary.js +0 -0
  224. /package/{src → lib}/Types/Auth.js +0 -0
  225. /package/{src → lib}/Types/Call.js +0 -0
  226. /package/{src → lib}/Types/Chat.js +0 -0
  227. /package/{src → lib}/Types/Contact.js +0 -0
  228. /package/{src → lib}/Types/Events.js +0 -0
  229. /package/{src → lib}/Types/GroupMetadata.js +0 -0
  230. /package/{src → lib}/Types/Label.js +0 -0
  231. /package/{src → lib}/Types/LabelAssociation.js +0 -0
  232. /package/{src → lib}/Types/Message.js +0 -0
  233. /package/{src → lib}/Types/Newsletter.js +0 -0
  234. /package/{src → lib}/Types/Product.js +0 -0
  235. /package/{src → lib}/Types/Signal.js +0 -0
  236. /package/{src → lib}/Types/Socket.js +0 -0
  237. /package/{src → lib}/Types/State.js +0 -0
  238. /package/{src/WABinary/types.js → lib/Types/USync.js} +0 -0
  239. /package/{src → lib}/Types/index.js +0 -0
  240. /package/{src → lib}/Utils/index.js +0 -0
  241. /package/{src → lib}/Utils/logger.js +0 -0
  242. /package/{src → lib}/Utils/lt-hash.js +0 -0
  243. /package/{src → lib}/Utils/noise-handler.js +0 -0
  244. /package/{src → lib}/WABinary/constants.js +0 -0
  245. /package/{src → lib}/WABinary/decode.js +0 -0
  246. /package/{src → lib}/WABinary/generic-utils.js +0 -0
  247. /package/{src → lib}/WABinary/index.js +0 -0
  248. /package/{src → lib}/WABinary/jid-utils.js +0 -0
  249. /package/{src → lib}/WAM/BinaryInfo.js +0 -0
  250. /package/{src → lib}/WAM/constants.js +0 -0
  251. /package/{src → lib}/WAM/encode.js +0 -0
  252. /package/{src → lib}/WAM/index.js +0 -0
@@ -1,265 +0,0 @@
1
- import { inflateSync } from 'zlib'
2
- import * as constants from './constants'
3
- import { jidEncode } from './jid-utils'
4
- import type { BinaryNode, BinaryNodeCodingOptions } from './types'
5
-
6
- export const decompressingIfRequired = (buffer: Buffer) => {
7
- if(2 & buffer.readUInt8()) {
8
- buffer = inflateSync(buffer.slice(1))
9
- } else { // nodes with no compression have a 0x00 prefix, we remove that
10
- buffer = buffer.slice(1)
11
- }
12
-
13
- return buffer
14
- }
15
-
16
- export const decodeDecompressedBinaryNode = (
17
- buffer: Buffer,
18
- opts: Pick<BinaryNodeCodingOptions, 'DOUBLE_BYTE_TOKENS' | 'SINGLE_BYTE_TOKENS' | 'TAGS'>,
19
- indexRef: { index: number } = { index: 0 }
20
- ): BinaryNode => {
21
- const { DOUBLE_BYTE_TOKENS, SINGLE_BYTE_TOKENS, TAGS } = opts
22
-
23
- const checkEOS = (length: number) => {
24
- if(indexRef.index + length > buffer.length) {
25
- throw new Error('end of stream')
26
- }
27
- }
28
-
29
- const next = () => {
30
- const value = buffer[indexRef.index]
31
- indexRef.index += 1
32
- return value
33
- }
34
-
35
- const readByte = () => {
36
- checkEOS(1)
37
- return next()
38
- }
39
-
40
- const readBytes = (n: number) => {
41
- checkEOS(n)
42
- const value = buffer.slice(indexRef.index, indexRef.index + n)
43
- indexRef.index += n
44
- return value
45
- }
46
-
47
- const readStringFromChars = (length: number) => {
48
- return readBytes(length).toString('utf-8')
49
- }
50
-
51
- const readInt = (n: number, littleEndian = false) => {
52
- checkEOS(n)
53
- let val = 0
54
- for(let i = 0; i < n; i++) {
55
- const shift = littleEndian ? i : n - 1 - i
56
- val |= next() << (shift * 8)
57
- }
58
-
59
- return val
60
- }
61
-
62
- const readInt20 = () => {
63
- checkEOS(3)
64
- return ((next() & 15) << 16) + (next() << 8) + next()
65
- }
66
-
67
- const unpackHex = (value: number) => {
68
- if(value >= 0 && value < 16) {
69
- return value < 10 ? '0'.charCodeAt(0) + value : 'A'.charCodeAt(0) + value - 10
70
- }
71
-
72
- throw new Error('invalid hex: ' + value)
73
- }
74
-
75
- const unpackNibble = (value: number) => {
76
- if(value >= 0 && value <= 9) {
77
- return '0'.charCodeAt(0) + value
78
- }
79
-
80
- switch (value) {
81
- case 10:
82
- return '-'.charCodeAt(0)
83
- case 11:
84
- return '.'.charCodeAt(0)
85
- case 15:
86
- return '\0'.charCodeAt(0)
87
- default:
88
- throw new Error('invalid nibble: ' + value)
89
- }
90
- }
91
-
92
- const unpackByte = (tag: number, value: number) => {
93
- if(tag === TAGS.NIBBLE_8) {
94
- return unpackNibble(value)
95
- } else if(tag === TAGS.HEX_8) {
96
- return unpackHex(value)
97
- } else {
98
- throw new Error('unknown tag: ' + tag)
99
- }
100
- }
101
-
102
- const readPacked8 = (tag: number) => {
103
- const startByte = readByte()
104
- let value = ''
105
-
106
- for(let i = 0; i < (startByte & 127); i++) {
107
- const curByte = readByte()
108
- value += String.fromCharCode(unpackByte(tag, (curByte & 0xf0) >> 4))
109
- value += String.fromCharCode(unpackByte(tag, curByte & 0x0f))
110
- }
111
-
112
- if(startByte >> 7 !== 0) {
113
- value = value.slice(0, -1)
114
- }
115
-
116
- return value
117
- }
118
-
119
- const isListTag = (tag: number) => {
120
- return tag === TAGS.LIST_EMPTY || tag === TAGS.LIST_8 || tag === TAGS.LIST_16
121
- }
122
-
123
- const readListSize = (tag: number) => {
124
- switch (tag) {
125
- case TAGS.LIST_EMPTY:
126
- return 0
127
- case TAGS.LIST_8:
128
- return readByte()
129
- case TAGS.LIST_16:
130
- return readInt(2)
131
- default:
132
- throw new Error('invalid tag for list size: ' + tag)
133
- }
134
- }
135
-
136
- const readJidPair = () => {
137
- const i = readString(readByte())
138
- const j = readString(readByte())
139
- if(j) {
140
- return (i || '') + '@' + j
141
- }
142
-
143
- throw new Error('invalid jid pair: ' + i + ', ' + j)
144
- }
145
-
146
- const readAdJid = () => {
147
- const agent = readByte()
148
- const device = readByte()
149
- const user = readString(readByte())
150
-
151
- return jidEncode(user, agent === 0 ? 's.whatsapp.net' : 'lid', device)
152
- }
153
-
154
- const readString = (tag: number): string => {
155
- if(tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
156
- return SINGLE_BYTE_TOKENS[tag] || ''
157
- }
158
-
159
- switch (tag) {
160
- case TAGS.DICTIONARY_0:
161
- case TAGS.DICTIONARY_1:
162
- case TAGS.DICTIONARY_2:
163
- case TAGS.DICTIONARY_3:
164
- return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
165
- case TAGS.LIST_EMPTY:
166
- return ''
167
- case TAGS.BINARY_8:
168
- return readStringFromChars(readByte())
169
- case TAGS.BINARY_20:
170
- return readStringFromChars(readInt20())
171
- case TAGS.BINARY_32:
172
- return readStringFromChars(readInt(4))
173
- case TAGS.JID_PAIR:
174
- return readJidPair()
175
- case TAGS.AD_JID:
176
- return readAdJid()
177
- case TAGS.HEX_8:
178
- case TAGS.NIBBLE_8:
179
- return readPacked8(tag)
180
- default:
181
- throw new Error('invalid string with tag: ' + tag)
182
- }
183
- }
184
-
185
- const readList = (tag: number) => {
186
- const items: BinaryNode[] = []
187
- const size = readListSize(tag)
188
- for(let i = 0;i < size;i++) {
189
- items.push(decodeDecompressedBinaryNode(buffer, opts, indexRef))
190
- }
191
-
192
- return items
193
- }
194
-
195
- const getTokenDouble = (index1: number, index2: number) => {
196
- const dict = DOUBLE_BYTE_TOKENS[index1]
197
- if(!dict) {
198
- throw new Error(`Invalid double token dict (${index1})`)
199
- }
200
-
201
- const value = dict[index2]
202
- if(typeof value === 'undefined') {
203
- throw new Error(`Invalid double token (${index2})`)
204
- }
205
-
206
- return value
207
- }
208
-
209
- const listSize = readListSize(readByte())
210
- const header = readString(readByte())
211
- if(!listSize || !header.length) {
212
- throw new Error('invalid node')
213
- }
214
-
215
- const attrs: BinaryNode['attrs'] = { }
216
- let data: BinaryNode['content']
217
- if(listSize === 0 || !header) {
218
- throw new Error('invalid node')
219
- }
220
-
221
- // read the attributes in
222
- const attributesLength = (listSize - 1) >> 1
223
- for(let i = 0; i < attributesLength; i++) {
224
- const key = readString(readByte())
225
- const value = readString(readByte())
226
-
227
- attrs[key] = value
228
- }
229
-
230
- if(listSize % 2 === 0) {
231
- const tag = readByte()
232
- if(isListTag(tag)) {
233
- data = readList(tag)
234
- } else {
235
- let decoded: Buffer | string
236
- switch (tag) {
237
- case TAGS.BINARY_8:
238
- decoded = readBytes(readByte())
239
- break
240
- case TAGS.BINARY_20:
241
- decoded = readBytes(readInt20())
242
- break
243
- case TAGS.BINARY_32:
244
- decoded = readBytes(readInt(4))
245
- break
246
- default:
247
- decoded = readString(tag)
248
- break
249
- }
250
-
251
- data = decoded
252
- }
253
- }
254
-
255
- return {
256
- tag: header,
257
- attrs,
258
- content: data
259
- }
260
- }
261
-
262
- export const decodeBinaryNode = (buff: Buffer): BinaryNode => {
263
- const decompBuff = decompressingIfRequired(buff)
264
- return decodeDecompressedBinaryNode(decompBuff, constants)
265
- }
@@ -1,236 +0,0 @@
1
-
2
- import * as constants from './constants'
3
- import { FullJid, jidDecode } from './jid-utils'
4
- import type { BinaryNode, BinaryNodeCodingOptions } from './types'
5
-
6
- export const encodeBinaryNode = (
7
- { tag, attrs, content }: BinaryNode,
8
- opts: Pick<BinaryNodeCodingOptions, 'TAGS' | 'TOKEN_MAP'> = constants,
9
- buffer: number[] = [0]
10
- ) => {
11
- const { TAGS, TOKEN_MAP } = opts
12
-
13
- const pushByte = (value: number) => buffer.push(value & 0xff)
14
-
15
- const pushInt = (value: number, n: number, littleEndian = false) => {
16
- for(let i = 0; i < n; i++) {
17
- const curShift = littleEndian ? i : n - 1 - i
18
- buffer.push((value >> (curShift * 8)) & 0xff)
19
- }
20
- }
21
-
22
- const pushBytes = (bytes: Uint8Array | Buffer | number[]) => (
23
- bytes.forEach (b => buffer.push(b))
24
- )
25
- const pushInt16 = (value: number) => {
26
- pushBytes([(value >> 8) & 0xff, value & 0xff])
27
- }
28
-
29
- const pushInt20 = (value: number) => (
30
- pushBytes([(value >> 16) & 0x0f, (value >> 8) & 0xff, value & 0xff])
31
- )
32
- const writeByteLength = (length: number) => {
33
- if(length >= 4294967296) {
34
- throw new Error('string too large to encode: ' + length)
35
- }
36
-
37
- if(length >= 1 << 20) {
38
- pushByte(TAGS.BINARY_32)
39
- pushInt(length, 4) // 32 bit integer
40
- } else if(length >= 256) {
41
- pushByte(TAGS.BINARY_20)
42
- pushInt20(length)
43
- } else {
44
- pushByte(TAGS.BINARY_8)
45
- pushByte(length)
46
- }
47
- }
48
-
49
- const writeStringRaw = (str: string) => {
50
- const bytes = Buffer.from (str, 'utf-8')
51
- writeByteLength(bytes.length)
52
- pushBytes(bytes)
53
- }
54
-
55
- const writeJid = ({ domainType, device, user, server }: FullJid) => {
56
- if(typeof device !== 'undefined') {
57
- pushByte(TAGS.AD_JID)
58
- pushByte(domainType || 0)
59
- pushByte(device || 0)
60
- writeString(user)
61
- } else {
62
- pushByte(TAGS.JID_PAIR)
63
- if(user.length) {
64
- writeString(user)
65
- } else {
66
- pushByte(TAGS.LIST_EMPTY)
67
- }
68
-
69
- writeString(server)
70
- }
71
- }
72
-
73
- const packNibble = (char: string) => {
74
- switch (char) {
75
- case '-':
76
- return 10
77
- case '.':
78
- return 11
79
- case '\0':
80
- return 15
81
- default:
82
- if(char >= '0' && char <= '9') {
83
- return char.charCodeAt(0) - '0'.charCodeAt(0)
84
- }
85
-
86
- throw new Error(`invalid byte for nibble "${char}"`)
87
- }
88
- }
89
-
90
- const packHex = (char: string) => {
91
- if(char >= '0' && char <= '9') {
92
- return char.charCodeAt(0) - '0'.charCodeAt(0)
93
- }
94
-
95
- if(char >= 'A' && char <= 'F') {
96
- return 10 + char.charCodeAt(0) - 'A'.charCodeAt(0)
97
- }
98
-
99
- if(char >= 'a' && char <= 'f') {
100
- return 10 + char.charCodeAt(0) - 'a'.charCodeAt(0)
101
- }
102
-
103
- if(char === '\0') {
104
- return 15
105
- }
106
-
107
- throw new Error(`Invalid hex char "${char}"`)
108
- }
109
-
110
- const writePackedBytes = (str: string, type: 'nibble' | 'hex') => {
111
- if(str.length > TAGS.PACKED_MAX) {
112
- throw new Error('Too many bytes to pack')
113
- }
114
-
115
- pushByte(type === 'nibble' ? TAGS.NIBBLE_8 : TAGS.HEX_8)
116
-
117
- let roundedLength = Math.ceil(str.length / 2.0)
118
- if(str.length % 2 !== 0) {
119
- roundedLength |= 128
120
- }
121
-
122
- pushByte(roundedLength)
123
- const packFunction = type === 'nibble' ? packNibble : packHex
124
-
125
- const packBytePair = (v1: string, v2: string) => {
126
- const result = (packFunction(v1) << 4) | packFunction(v2)
127
- return result
128
- }
129
-
130
- const strLengthHalf = Math.floor(str.length / 2)
131
- for(let i = 0; i < strLengthHalf;i++) {
132
- pushByte(packBytePair(str[2 * i], str[2 * i + 1]))
133
- }
134
-
135
- if(str.length % 2 !== 0) {
136
- pushByte(packBytePair(str[str.length - 1], '\x00'))
137
- }
138
- }
139
-
140
- const isNibble = (str: string) => {
141
- if(str.length > TAGS.PACKED_MAX) {
142
- return false
143
- }
144
-
145
- for(let i = 0;i < str.length;i++) {
146
- const char = str[i]
147
- const isInNibbleRange = char >= '0' && char <= '9'
148
- if(!isInNibbleRange && char !== '-' && char !== '.') {
149
- return false
150
- }
151
- }
152
-
153
- return true
154
- }
155
-
156
- const isHex = (str: string) => {
157
- if(str.length > TAGS.PACKED_MAX) {
158
- return false
159
- }
160
-
161
- for(let i = 0;i < str.length;i++) {
162
- const char = str[i]
163
- const isInNibbleRange = char >= '0' && char <= '9'
164
- if(!isInNibbleRange && !(char >= 'A' && char <= 'F') && !(char >= 'a' && char <= 'f')) {
165
- return false
166
- }
167
- }
168
-
169
- return true
170
- }
171
-
172
- const writeString = (str: string) => {
173
- const tokenIndex = TOKEN_MAP[str]
174
- if(tokenIndex) {
175
- if(typeof tokenIndex.dict === 'number') {
176
- pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict)
177
- }
178
-
179
- pushByte(tokenIndex.index)
180
- } else if(isNibble(str)) {
181
- writePackedBytes(str, 'nibble')
182
- } else if(isHex(str)) {
183
- writePackedBytes(str, 'hex')
184
- } else if(str) {
185
- const decodedJid = jidDecode(str)
186
- if(decodedJid) {
187
- writeJid(decodedJid)
188
- } else {
189
- writeStringRaw(str)
190
- }
191
- }
192
- }
193
-
194
- const writeListStart = (listSize: number) => {
195
- if(listSize === 0) {
196
- pushByte(TAGS.LIST_EMPTY)
197
- } else if(listSize < 256) {
198
- pushBytes([TAGS.LIST_8, listSize])
199
- } else {
200
- pushByte(TAGS.LIST_16)
201
- pushInt16(listSize)
202
- }
203
- }
204
-
205
- const validAttributes = Object.keys(attrs).filter(k => (
206
- typeof attrs[k] !== 'undefined' && attrs[k] !== null
207
- ))
208
-
209
- writeListStart(2 * validAttributes.length + 1 + (typeof content !== 'undefined' ? 1 : 0))
210
- writeString(tag)
211
-
212
- for(const key of validAttributes) {
213
- if(typeof attrs[key] === 'string') {
214
- writeString(key)
215
- writeString(attrs[key])
216
- }
217
- }
218
-
219
- if(typeof content === 'string') {
220
- writeString(content)
221
- } else if(Buffer.isBuffer(content) || content instanceof Uint8Array) {
222
- writeByteLength(content.length)
223
- pushBytes(content)
224
- } else if(Array.isArray(content)) {
225
- writeListStart(content.length)
226
- for(const item of content) {
227
- encodeBinaryNode(item, opts, buffer)
228
- }
229
- } else if(typeof content === 'undefined') {
230
- // do nothing
231
- } else {
232
- throw new Error(`invalid children for header "${tag}": ${content} (${typeof content})`)
233
- }
234
-
235
- return Buffer.from(buffer)
236
- }
@@ -1,121 +0,0 @@
1
- import { Boom } from '@hapi/boom'
2
- import { proto } from '../../WAProto'
3
- import { BinaryNode } from './types'
4
-
5
- // some extra useful utilities
6
-
7
- export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
8
- if(Array.isArray(node?.content)) {
9
- return node!.content.filter(item => item.tag === childTag)
10
- }
11
-
12
- return []
13
- }
14
-
15
- export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
16
- if(Array.isArray(content)) {
17
- return content
18
- }
19
-
20
- return []
21
- }
22
-
23
- export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
24
- if(Array.isArray(node?.content)) {
25
- return node?.content.find(item => item.tag === childTag)
26
- }
27
- }
28
-
29
- export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
30
- const child = getBinaryNodeChild(node, childTag)?.content
31
- if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
32
- return child
33
- }
34
- }
35
-
36
- export const getBinaryNodeChildString = (node: BinaryNode | undefined, childTag: string) => {
37
- const child = getBinaryNodeChild(node, childTag)?.content
38
- if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
39
- return Buffer.from(child).toString('utf-8')
40
- } else if(typeof child === 'string') {
41
- return child
42
- }
43
- }
44
-
45
- export const getBinaryNodeChildUInt = (node: BinaryNode, childTag: string, length: number) => {
46
- const buff = getBinaryNodeChildBuffer(node, childTag)
47
- if(buff) {
48
- return bufferToUInt(buff, length)
49
- }
50
- }
51
-
52
- export const assertNodeErrorFree = (node: BinaryNode) => {
53
- const errNode = getBinaryNodeChild(node, 'error')
54
- if(errNode) {
55
- throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code })
56
- }
57
- }
58
-
59
- export const reduceBinaryNodeToDictionary = (node: BinaryNode, tag: string) => {
60
- const nodes = getBinaryNodeChildren(node, tag)
61
- const dict = nodes.reduce(
62
- (dict, { attrs }) => {
63
- dict[attrs.name || attrs.config_code] = attrs.value || attrs.config_value
64
- return dict
65
- }, { } as { [_: string]: string }
66
- )
67
- return dict
68
- }
69
-
70
- export const getBinaryNodeMessages = ({ content }: BinaryNode) => {
71
- const msgs: proto.WebMessageInfo[] = []
72
- if(Array.isArray(content)) {
73
- for(const item of content) {
74
- if(item.tag === 'message') {
75
- msgs.push(proto.WebMessageInfo.decode(item.content as Buffer))
76
- }
77
- }
78
- }
79
-
80
- return msgs
81
- }
82
-
83
- function bufferToUInt(e: Uint8Array | Buffer, t: number) {
84
- let a = 0
85
- for(let i = 0; i < t; i++) {
86
- a = 256 * a + e[i]
87
- }
88
-
89
- return a
90
- }
91
-
92
- const tabs = (n: number) => '\t'.repeat(n)
93
-
94
- export function binaryNodeToString(node: BinaryNode | BinaryNode['content'], i = 0) {
95
- if(!node) {
96
- return node
97
- }
98
-
99
- if(typeof node === 'string') {
100
- return tabs(i) + node
101
- }
102
-
103
- if(node instanceof Uint8Array) {
104
- return tabs(i) + Buffer.from(node).toString('hex')
105
- }
106
-
107
- if(Array.isArray(node)) {
108
- return node.map((x) => tabs(i + 1) + binaryNodeToString(x, i + 1)).join('\n')
109
- }
110
-
111
- const children = binaryNodeToString(node.content, i + 1)
112
-
113
- const tag = `<${node.tag} ${Object.entries(node.attrs || {})
114
- .filter(([, v]) => v !== undefined)
115
- .map(([k, v]) => `${k}='${v}'`)
116
- .join(' ')}`
117
-
118
- const content: string = children ? `>\n${children}\n${tabs(i)}</${node.tag}>` : '/>'
119
-
120
- return tag + content
121
- }
@@ -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'
@@ -1,68 +0,0 @@
1
- export const S_WHATSAPP_NET = '@s.whatsapp.net'
2
- export const OFFICIAL_BIZ_JID = '16505361212@c.us'
3
- export const SERVER_JID = 'server@c.us'
4
- export const PSA_WID = '0@c.us'
5
- export const STORIES_JID = 'status@broadcast'
6
-
7
- export type JidServer = 'c.us' | 'g.us' | 'broadcast' | 's.whatsapp.net' | 'call' | 'lid' | 'newsletter'
8
-
9
- export type JidWithDevice = {
10
- user: string
11
- device?: number
12
- }
13
-
14
- export type FullJid = JidWithDevice & {
15
- server: JidServer | string
16
- domainType?: number
17
- }
18
-
19
- export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
20
- return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
21
- }
22
-
23
- export const jidDecode = (jid: string | undefined): FullJid | undefined => {
24
- const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
25
- if(sepIdx < 0) {
26
- return undefined
27
- }
28
-
29
- const server = jid!.slice(sepIdx + 1)
30
- const userCombined = jid!.slice(0, sepIdx)
31
-
32
- const [userAgent, device] = userCombined.split(':')
33
- const user = userAgent.split('_')[0]
34
-
35
- return {
36
- server,
37
- user,
38
- domainType: server === 'lid' ? 1 : 0,
39
- device: device ? +device : undefined
40
- }
41
- }
42
-
43
- /** is the jid a user */
44
- export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) => (
45
- jidDecode(jid1)?.user === jidDecode(jid2)?.user
46
- )
47
- /** is the jid a user */
48
- export const isJidUser = (jid: string | undefined) => (jid?.endsWith('@s.whatsapp.net'))
49
- /** is the jid a group */
50
- export const isLidUser = (jid: string | undefined) => (jid?.endsWith('@lid'))
51
- /** is the jid a broadcast */
52
- export const isJidBroadcast = (jid: string | undefined) => (jid?.endsWith('@broadcast'))
53
- /** is the jid a group */
54
- export const isJidGroup = (jid: string | undefined) => (jid?.endsWith('@g.us'))
55
- /** is the jid the status broadcast */
56
- export const isJidStatusBroadcast = (jid: string) => jid === 'status@broadcast'
57
- /** is the jid the newsletter */
58
- export const isJidNewsLetter = (jid: string | undefined) => (jid?.endsWith('newsletter'))
59
-
60
- export const jidNormalizedUser = (jid: string | undefined) => {
61
- const result = jidDecode(jid)
62
- if(!result) {
63
- return ''
64
- }
65
-
66
- const { user, server } = result
67
- return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server as JidServer)
68
- }
@@ -1,17 +0,0 @@
1
- import * as constants from './constants'
2
- /**
3
- * the binary node WA uses internally for communication
4
- *
5
- * this is manipulated soley as an object and it does not have any functions.
6
- * This is done for easy serialization, to prevent running into issues with prototypes &
7
- * to maintain functional code structure
8
- * */
9
- export type BinaryNode = {
10
- tag: string
11
- attrs: { [key: string]: string }
12
- content?: BinaryNode[] | string | Uint8Array
13
- }
14
- export type BinaryNodeAttributes = BinaryNode['attrs']
15
- export type BinaryNodeData = BinaryNode['content']
16
-
17
- export type BinaryNodeCodingOptions = typeof constants