@neelegirl/baileys 1.5.3 → 1.5.4

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 (438) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +195 -195
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +478 -1153
  5. package/WAProto/fix-imports.js +29 -0
  6. package/WAProto/index.d.ts +2645 -51971
  7. package/WAProto/index.js +48649 -154035
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/index.d.ts +62 -78
  10. package/lib/Defaults/index.d.ts.map +1 -0
  11. package/lib/Defaults/index.js +115 -149
  12. package/lib/Defaults/index.js.map +1 -0
  13. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  14. package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  15. package/lib/Signal/Group/ciphertext-message.js +12 -0
  16. package/lib/Signal/Group/ciphertext-message.js.map +1 -0
  17. package/lib/Signal/{WASignalGroup → Group}/group-session-builder.d.ts +11 -13
  18. package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  19. package/lib/Signal/Group/group-session-builder.js +30 -0
  20. package/lib/Signal/Group/group-session-builder.js.map +1 -0
  21. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  22. package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  23. package/lib/Signal/{WASignalGroup → Group}/group_cipher.js +41 -70
  24. package/lib/Signal/Group/group_cipher.js.map +1 -0
  25. package/lib/Signal/Group/index.d.ts +12 -0
  26. package/lib/Signal/Group/index.d.ts.map +1 -0
  27. package/lib/Signal/Group/index.js +12 -0
  28. package/lib/Signal/Group/index.js.map +1 -0
  29. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  30. package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  31. package/lib/Signal/Group/keyhelper.js +18 -0
  32. package/lib/Signal/Group/keyhelper.js.map +1 -0
  33. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  34. package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  35. package/lib/Signal/Group/sender-chain-key.js +26 -0
  36. package/lib/Signal/Group/sender-chain-key.js.map +1 -0
  37. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  38. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  39. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  40. package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  41. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  42. package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  43. package/lib/Signal/Group/sender-key-message.js +66 -0
  44. package/lib/Signal/Group/sender-key-message.js.map +1 -0
  45. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  46. package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  47. package/lib/Signal/Group/sender-key-name.js +48 -0
  48. package/lib/Signal/Group/sender-key-name.js.map +1 -0
  49. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  50. package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  51. package/lib/Signal/Group/sender-key-record.js +41 -0
  52. package/lib/Signal/Group/sender-key-record.js.map +1 -0
  53. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  54. package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  55. package/lib/Signal/Group/sender-key-state.js +84 -0
  56. package/lib/Signal/Group/sender-key-state.js.map +1 -0
  57. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  58. package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  59. package/lib/Signal/{WASignalGroup/sender_message_key.js → Group/sender-message-key.js} +26 -39
  60. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  61. package/lib/Signal/libsignal.d.ts +5 -8
  62. package/lib/Signal/libsignal.d.ts.map +1 -0
  63. package/lib/Signal/libsignal.js +342 -391
  64. package/lib/Signal/libsignal.js.map +1 -0
  65. package/lib/Signal/lid-mapping.d.ts +23 -28
  66. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  67. package/lib/Signal/lid-mapping.js +171 -184
  68. package/lib/Signal/lid-mapping.js.map +1 -0
  69. package/lib/Socket/Client/index.d.ts +3 -2
  70. package/lib/Socket/Client/index.d.ts.map +1 -0
  71. package/lib/Socket/Client/index.js +3 -22
  72. package/lib/Socket/Client/index.js.map +1 -0
  73. package/lib/Socket/Client/types.d.ts +15 -15
  74. package/lib/Socket/Client/types.d.ts.map +1 -0
  75. package/lib/Socket/Client/types.js +8 -15
  76. package/lib/Socket/Client/types.js.map +1 -0
  77. package/lib/Socket/Client/websocket.d.ts +12 -12
  78. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  79. package/lib/Socket/Client/websocket.js +24 -36
  80. package/lib/Socket/Client/websocket.js.map +1 -0
  81. package/lib/Socket/business.d.ts +178 -177
  82. package/lib/Socket/business.d.ts.map +1 -0
  83. package/lib/Socket/business.js +81 -120
  84. package/lib/Socket/business.js.map +1 -0
  85. package/lib/Socket/chats.d.ts +93 -92
  86. package/lib/Socket/chats.d.ts.map +1 -0
  87. package/lib/Socket/chats.js +462 -618
  88. package/lib/Socket/chats.js.map +1 -0
  89. package/lib/Socket/communities.d.ts +244 -223
  90. package/lib/Socket/communities.d.ts.map +1 -0
  91. package/lib/Socket/communities.js +431 -433
  92. package/lib/Socket/communities.js.map +1 -0
  93. package/lib/Socket/groups.d.ts +118 -110
  94. package/lib/Socket/groups.d.ts.map +1 -0
  95. package/lib/Socket/groups.js +148 -181
  96. package/lib/Socket/groups.js.map +1 -0
  97. package/lib/Socket/index.d.ts +222 -182
  98. package/lib/Socket/index.d.ts.map +1 -0
  99. package/lib/Socket/index.js +12 -18
  100. package/lib/Socket/index.js.map +1 -0
  101. package/lib/Socket/messages-recv.d.ts +165 -166
  102. package/lib/Socket/messages-recv.d.ts.map +1 -0
  103. package/lib/Socket/messages-recv.js +753 -1031
  104. package/lib/Socket/messages-recv.js.map +1 -0
  105. package/lib/Socket/messages-send.d.ts +161 -157
  106. package/lib/Socket/messages-send.d.ts.map +1 -0
  107. package/lib/Socket/messages-send.js +564 -1396
  108. package/lib/Socket/messages-send.js.map +1 -0
  109. package/lib/Socket/mex.d.ts +3 -2
  110. package/lib/Socket/mex.d.ts.map +1 -0
  111. package/lib/Socket/mex.js +42 -47
  112. package/lib/Socket/mex.js.map +1 -0
  113. package/lib/Socket/newsletter.d.ts +139 -137
  114. package/lib/Socket/newsletter.d.ts.map +1 -0
  115. package/lib/Socket/newsletter.js +181 -295
  116. package/lib/Socket/newsletter.js.map +1 -0
  117. package/lib/Socket/socket.d.ts +42 -36
  118. package/lib/Socket/socket.d.ts.map +1 -0
  119. package/lib/Socket/socket.js +487 -557
  120. package/lib/Socket/socket.js.map +1 -0
  121. package/lib/Types/Auth.d.ts +87 -97
  122. package/lib/Types/Auth.d.ts.map +1 -0
  123. package/lib/Types/Auth.js +2 -3
  124. package/lib/Types/Auth.js.map +1 -0
  125. package/lib/Types/Bussines.d.ts +25 -0
  126. package/lib/Types/Bussines.d.ts.map +1 -0
  127. package/lib/Types/Bussines.js +2 -3
  128. package/lib/Types/Bussines.js.map +1 -0
  129. package/lib/Types/Call.d.ts +13 -13
  130. package/lib/Types/Call.d.ts.map +1 -0
  131. package/lib/Types/Call.js +2 -3
  132. package/lib/Types/Call.js.map +1 -0
  133. package/lib/Types/Chat.d.ts +77 -97
  134. package/lib/Types/Chat.d.ts.map +1 -0
  135. package/lib/Types/Chat.js +8 -9
  136. package/lib/Types/Chat.js.map +1 -0
  137. package/lib/Types/Contact.d.ts +10 -9
  138. package/lib/Types/Contact.d.ts.map +1 -0
  139. package/lib/Types/Contact.js +2 -3
  140. package/lib/Types/Contact.js.map +1 -0
  141. package/lib/Types/Events.d.ts +151 -175
  142. package/lib/Types/Events.d.ts.map +1 -0
  143. package/lib/Types/Events.js +2 -3
  144. package/lib/Types/Events.js.map +1 -0
  145. package/lib/Types/GroupMetadata.d.ts +49 -48
  146. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  147. package/lib/Types/GroupMetadata.js +2 -3
  148. package/lib/Types/GroupMetadata.js.map +1 -0
  149. package/lib/Types/Label.d.ts +13 -14
  150. package/lib/Types/Label.d.ts.map +1 -0
  151. package/lib/Types/Label.js +24 -30
  152. package/lib/Types/Label.js.map +1 -0
  153. package/lib/Types/LabelAssociation.d.ts +15 -20
  154. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  155. package/lib/Types/LabelAssociation.js +6 -12
  156. package/lib/Types/LabelAssociation.js.map +1 -0
  157. package/lib/Types/Message.d.ts +231 -412
  158. package/lib/Types/Message.d.ts.map +1 -0
  159. package/lib/Types/Message.js +11 -19
  160. package/lib/Types/Message.js.map +1 -0
  161. package/lib/Types/Newsletter.d.ts +130 -104
  162. package/lib/Types/Newsletter.d.ts.map +1 -0
  163. package/lib/Types/Newsletter.js +31 -40
  164. package/lib/Types/Newsletter.js.map +1 -0
  165. package/lib/Types/Product.d.ts +58 -71
  166. package/lib/Types/Product.d.ts.map +1 -0
  167. package/lib/Types/Product.js +2 -3
  168. package/lib/Types/Product.js.map +1 -0
  169. package/lib/Types/Signal.d.ts +60 -82
  170. package/lib/Types/Signal.d.ts.map +1 -0
  171. package/lib/Types/Signal.js +2 -3
  172. package/lib/Types/Signal.js.map +1 -0
  173. package/lib/Types/Socket.d.ts +73 -81
  174. package/lib/Types/Socket.d.ts.map +1 -0
  175. package/lib/Types/Socket.js +3 -3
  176. package/lib/Types/Socket.js.map +1 -0
  177. package/lib/Types/State.d.ts +17 -19
  178. package/lib/Types/State.d.ts.map +1 -0
  179. package/lib/Types/State.js +13 -14
  180. package/lib/Types/State.js.map +1 -0
  181. package/lib/Types/USync.d.ts +8 -8
  182. package/lib/Types/USync.d.ts.map +1 -0
  183. package/lib/Types/USync.js +2 -3
  184. package/lib/Types/USync.js.map +1 -0
  185. package/lib/Types/index.d.ts +47 -62
  186. package/lib/Types/index.d.ts.map +1 -0
  187. package/lib/Types/index.js +26 -50
  188. package/lib/Types/index.js.map +1 -0
  189. package/lib/Utils/auth-utils.d.ts +19 -21
  190. package/lib/Utils/auth-utils.d.ts.map +1 -0
  191. package/lib/Utils/auth-utils.js +257 -528
  192. package/lib/Utils/auth-utils.js.map +1 -0
  193. package/lib/Utils/baileys-event-stream.d.ts +17 -18
  194. package/lib/Utils/baileys-event-stream.d.ts.map +1 -0
  195. package/lib/Utils/baileys-event-stream.js +56 -70
  196. package/lib/Utils/baileys-event-stream.js.map +1 -0
  197. package/lib/Utils/browser-utils.d.ts +4 -0
  198. package/lib/Utils/browser-utils.d.ts.map +1 -0
  199. package/lib/Utils/browser-utils.js +28 -0
  200. package/lib/Utils/browser-utils.js.map +1 -0
  201. package/lib/Utils/business.d.ts +23 -29
  202. package/lib/Utils/business.d.ts.map +1 -0
  203. package/lib/Utils/business.js +231 -255
  204. package/lib/Utils/business.js.map +1 -0
  205. package/lib/Utils/chat-utils.d.ts +70 -82
  206. package/lib/Utils/chat-utils.d.ts.map +1 -0
  207. package/lib/Utils/chat-utils.js +763 -809
  208. package/lib/Utils/chat-utils.js.map +1 -0
  209. package/lib/Utils/crypto.d.ts +41 -56
  210. package/lib/Utils/crypto.d.ts.map +1 -0
  211. package/lib/Utils/crypto.js +142 -189
  212. package/lib/Utils/crypto.js.map +1 -0
  213. package/lib/Utils/decode-wa-message.d.ts +48 -53
  214. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  215. package/lib/Utils/decode-wa-message.js +279 -323
  216. package/lib/Utils/decode-wa-message.js.map +1 -0
  217. package/lib/Utils/event-buffer.d.ts +34 -39
  218. package/lib/Utils/event-buffer.d.ts.map +1 -0
  219. package/lib/Utils/event-buffer.js +548 -595
  220. package/lib/Utils/event-buffer.js.map +1 -0
  221. package/lib/Utils/generics.d.ts +90 -131
  222. package/lib/Utils/generics.d.ts.map +1 -0
  223. package/lib/Utils/generics.js +381 -630
  224. package/lib/Utils/generics.js.map +1 -0
  225. package/lib/Utils/history.d.ts +19 -23
  226. package/lib/Utils/history.d.ts.map +1 -0
  227. package/lib/Utils/history.js +84 -104
  228. package/lib/Utils/history.js.map +1 -0
  229. package/lib/Utils/index.d.ts +20 -20
  230. package/lib/Utils/index.d.ts.map +1 -0
  231. package/lib/Utils/index.js +19 -40
  232. package/lib/Utils/index.js.map +1 -0
  233. package/lib/Utils/link-preview.d.ts +21 -23
  234. package/lib/Utils/link-preview.d.ts.map +1 -0
  235. package/lib/Utils/link-preview.js +85 -120
  236. package/lib/Utils/link-preview.js.map +1 -0
  237. package/lib/Utils/logger.d.ts +12 -13
  238. package/lib/Utils/logger.d.ts.map +1 -0
  239. package/lib/Utils/logger.js +3 -7
  240. package/lib/Utils/logger.js.map +1 -0
  241. package/lib/Utils/lt-hash.d.ts +13 -14
  242. package/lib/Utils/lt-hash.d.ts.map +1 -0
  243. package/lib/Utils/lt-hash.js +48 -58
  244. package/lib/Utils/lt-hash.js.map +1 -0
  245. package/lib/Utils/make-mutex.d.ts +8 -9
  246. package/lib/Utils/make-mutex.d.ts.map +1 -0
  247. package/lib/Utils/make-mutex.js +40 -49
  248. package/lib/Utils/make-mutex.js.map +1 -0
  249. package/lib/Utils/message-retry-manager.d.ts +82 -88
  250. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  251. package/lib/Utils/message-retry-manager.js +149 -160
  252. package/lib/Utils/message-retry-manager.js.map +1 -0
  253. package/lib/Utils/messages-media.d.ts +114 -135
  254. package/lib/Utils/messages-media.d.ts.map +1 -0
  255. package/lib/Utils/messages-media.js +663 -869
  256. package/lib/Utils/messages-media.js.map +1 -0
  257. package/lib/Utils/messages.d.ts +76 -105
  258. package/lib/Utils/messages.d.ts.map +1 -0
  259. package/lib/Utils/messages.js +820 -1745
  260. package/lib/Utils/messages.js.map +1 -0
  261. package/lib/Utils/noise-handler.d.ts +20 -21
  262. package/lib/Utils/noise-handler.d.ts.map +1 -0
  263. package/lib/Utils/noise-handler.js +147 -165
  264. package/lib/Utils/noise-handler.js.map +1 -0
  265. package/lib/Utils/pre-key-manager.d.ts +28 -0
  266. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  267. package/lib/Utils/pre-key-manager.js +106 -0
  268. package/lib/Utils/pre-key-manager.js.map +1 -0
  269. package/lib/Utils/process-message.d.ts +42 -49
  270. package/lib/Utils/process-message.d.ts.map +1 -0
  271. package/lib/Utils/process-message.js +413 -427
  272. package/lib/Utils/process-message.js.map +1 -0
  273. package/lib/Utils/signal.d.ts +34 -42
  274. package/lib/Utils/signal.d.ts.map +1 -0
  275. package/lib/Utils/signal.js +159 -166
  276. package/lib/Utils/signal.js.map +1 -0
  277. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -18
  278. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  279. package/lib/Utils/use-multi-file-auth-state.js +121 -238
  280. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  281. package/lib/Utils/validate-connection.d.ts +11 -13
  282. package/lib/Utils/validate-connection.d.ts.map +1 -0
  283. package/lib/Utils/validate-connection.js +195 -220
  284. package/lib/Utils/validate-connection.js.map +1 -0
  285. package/lib/WABinary/constants.d.ts +28 -30
  286. package/lib/WABinary/constants.d.ts.map +1 -0
  287. package/lib/WABinary/constants.js +1301 -1316
  288. package/lib/WABinary/constants.js.map +1 -0
  289. package/lib/WABinary/decode.d.ts +7 -9
  290. package/lib/WABinary/decode.d.ts.map +1 -0
  291. package/lib/WABinary/decode.js +238 -288
  292. package/lib/WABinary/decode.js.map +1 -0
  293. package/lib/WABinary/encode.d.ts +3 -3
  294. package/lib/WABinary/encode.d.ts.map +1 -0
  295. package/lib/WABinary/encode.js +216 -265
  296. package/lib/WABinary/encode.js.map +1 -0
  297. package/lib/WABinary/generic-utils.d.ts +15 -28
  298. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  299. package/lib/WABinary/generic-utils.js +102 -142
  300. package/lib/WABinary/generic-utils.js.map +1 -0
  301. package/lib/WABinary/index.d.ts +6 -5
  302. package/lib/WABinary/index.d.ts.map +1 -0
  303. package/lib/WABinary/index.js +6 -25
  304. package/lib/WABinary/index.js.map +1 -0
  305. package/lib/WABinary/jid-utils.d.ts +48 -58
  306. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  307. package/lib/WABinary/jid-utils.js +96 -104
  308. package/lib/WABinary/jid-utils.js.map +1 -0
  309. package/lib/WABinary/types.d.ts +19 -22
  310. package/lib/WABinary/types.d.ts.map +1 -0
  311. package/lib/WABinary/types.js +2 -3
  312. package/lib/WABinary/types.js.map +1 -0
  313. package/lib/WAM/BinaryInfo.d.ts +9 -16
  314. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  315. package/lib/WAM/BinaryInfo.js +10 -17
  316. package/lib/WAM/BinaryInfo.js.map +1 -0
  317. package/lib/WAM/constants.d.ts +40 -47
  318. package/lib/WAM/constants.d.ts.map +1 -0
  319. package/lib/WAM/constants.js +22853 -15371
  320. package/lib/WAM/constants.js.map +1 -0
  321. package/lib/WAM/encode.d.ts +3 -3
  322. package/lib/WAM/encode.d.ts.map +1 -0
  323. package/lib/WAM/encode.js +150 -164
  324. package/lib/WAM/encode.js.map +1 -0
  325. package/lib/WAM/index.d.ts +4 -3
  326. package/lib/WAM/index.d.ts.map +1 -0
  327. package/lib/WAM/index.js +4 -23
  328. package/lib/WAM/index.js.map +1 -0
  329. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -9
  330. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  331. package/lib/WAUSync/Protocols/USyncContactProtocol.js +12 -19
  332. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  333. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +19 -22
  334. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  335. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +18 -26
  336. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  337. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +11 -12
  338. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  339. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +12 -20
  340. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  341. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +11 -12
  342. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  343. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +16 -24
  344. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  345. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  346. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  347. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  348. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  349. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  350. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  351. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  352. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  353. package/lib/WAUSync/Protocols/index.d.ts +5 -6
  354. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  355. package/lib/WAUSync/Protocols/index.js +5 -26
  356. package/lib/WAUSync/Protocols/index.js.map +1 -0
  357. package/lib/WAUSync/USyncQuery.d.ts +26 -28
  358. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  359. package/lib/WAUSync/USyncQuery.js +64 -62
  360. package/lib/WAUSync/USyncQuery.js.map +1 -0
  361. package/lib/WAUSync/USyncUser.d.ts +11 -10
  362. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  363. package/lib/WAUSync/USyncUser.js +12 -19
  364. package/lib/WAUSync/USyncUser.js.map +1 -0
  365. package/lib/WAUSync/index.d.ts +4 -3
  366. package/lib/WAUSync/index.d.ts.map +1 -0
  367. package/lib/WAUSync/index.js +4 -23
  368. package/lib/WAUSync/index.js.map +1 -0
  369. package/lib/index.d.ts +12 -13
  370. package/lib/index.d.ts.map +1 -0
  371. package/lib/index.js +11 -33
  372. package/lib/index.js.map +1 -0
  373. package/package.json +99 -96
  374. package/lib/Defaults/baileys-version.json +0 -3
  375. package/lib/Defaults/phonenumber-mcc.json +0 -223
  376. package/lib/Signal/WASignalGroup/GroupProtocol.js +0 -1909
  377. package/lib/Signal/WASignalGroup/ciphertext-message.d.ts +0 -9
  378. package/lib/Signal/WASignalGroup/ciphertext-message.js +0 -19
  379. package/lib/Signal/WASignalGroup/ciphertext_message.js +0 -16
  380. package/lib/Signal/WASignalGroup/generate-proto.sh +0 -1
  381. package/lib/Signal/WASignalGroup/group-session-builder.js +0 -72
  382. package/lib/Signal/WASignalGroup/group.proto +0 -42
  383. package/lib/Signal/WASignalGroup/group_cipher.d.ts +0 -19
  384. package/lib/Signal/WASignalGroup/group_session_builder.js +0 -46
  385. package/lib/Signal/WASignalGroup/index.d.ts +0 -11
  386. package/lib/Signal/WASignalGroup/index.js +0 -61
  387. package/lib/Signal/WASignalGroup/keyhelper.d.ts +0 -16
  388. package/lib/Signal/WASignalGroup/keyhelper.js +0 -66
  389. package/lib/Signal/WASignalGroup/protobufs.js +0 -3
  390. package/lib/Signal/WASignalGroup/queue_job.js +0 -69
  391. package/lib/Signal/WASignalGroup/readme.md +0 -6
  392. package/lib/Signal/WASignalGroup/sender-chain-key.d.ts +0 -14
  393. package/lib/Signal/WASignalGroup/sender-chain-key.js +0 -47
  394. package/lib/Signal/WASignalGroup/sender-key-distribution-message.d.ts +0 -17
  395. package/lib/Signal/WASignalGroup/sender-key-distribution-message.js +0 -71
  396. package/lib/Signal/WASignalGroup/sender-key-message.d.ts +0 -19
  397. package/lib/Signal/WASignalGroup/sender-key-message.js +0 -73
  398. package/lib/Signal/WASignalGroup/sender-key-name.d.ts +0 -19
  399. package/lib/Signal/WASignalGroup/sender-key-name.js +0 -59
  400. package/lib/Signal/WASignalGroup/sender-key-record.d.ts +0 -32
  401. package/lib/Signal/WASignalGroup/sender-key-record.js +0 -58
  402. package/lib/Signal/WASignalGroup/sender-key-state.d.ts +0 -44
  403. package/lib/Signal/WASignalGroup/sender-key-state.js +0 -147
  404. package/lib/Signal/WASignalGroup/sender-message-key.d.ts +0 -11
  405. package/lib/Signal/WASignalGroup/sender-message-key.js +0 -33
  406. package/lib/Signal/WASignalGroup/sender_chain_key.js +0 -50
  407. package/lib/Signal/WASignalGroup/sender_key_distribution_message.js +0 -78
  408. package/lib/Signal/WASignalGroup/sender_key_message.js +0 -92
  409. package/lib/Signal/WASignalGroup/sender_key_name.js +0 -70
  410. package/lib/Signal/WASignalGroup/sender_key_record.js +0 -56
  411. package/lib/Signal/WASignalGroup/sender_key_state.js +0 -129
  412. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -15
  413. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  414. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -12
  415. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  416. package/lib/Socket/usync.d.ts +0 -37
  417. package/lib/Socket/usync.js +0 -83
  418. package/lib/Store/index.d.ts +0 -4
  419. package/lib/Store/index.js +0 -24
  420. package/lib/Store/make-cache-manager-store.d.ts +0 -14
  421. package/lib/Store/make-cache-manager-store.js +0 -90
  422. package/lib/Store/make-in-memory-store.d.ts +0 -123
  423. package/lib/Store/make-in-memory-store.js +0 -429
  424. package/lib/Store/make-ordered-dictionary.d.ts +0 -12
  425. package/lib/Store/make-ordered-dictionary.js +0 -86
  426. package/lib/Store/object-repository.d.ts +0 -10
  427. package/lib/Store/object-repository.js +0 -31
  428. package/lib/Types/Bussiness.d.ts +0 -28
  429. package/lib/Types/MexUpdates.d.ts +0 -9
  430. package/lib/Types/MexUpdates.js +0 -18
  431. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  432. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  433. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  434. package/lib/Utils/use-single-file-auth-state.js +0 -80
  435. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
  436. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -69
  437. package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
  438. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -38
@@ -1,869 +1,663 @@
1
- "use strict"
2
-
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k
5
- var desc = Object.getOwnPropertyDescriptor(m, k)
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k] } }
8
- }
9
- Object.defineProperty(o, k2, desc)
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k
12
- o[k2] = m[k]
13
- }))
14
-
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v })
17
- }) : function(o, v) {
18
- o["default"] = v
19
- })
20
-
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod
23
- var result = {}
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k)
25
- __setModuleDefault(result, mod)
26
- return result
27
- }
28
-
29
- var __importDefault = (this && this.__importDefault) || function (mod) {
30
- return (mod && mod.__esModule) ? mod : { "default": mod }
31
- }
32
-
33
- Object.defineProperty(exports, "__esModule", { value: true })
34
-
35
- const boom_1 = require("@hapi/boom")
36
- const axios_1 = __importDefault(require("axios"))
37
- const child_process_1 = require("child_process")
38
- const Crypto = __importStar(require("crypto"))
39
- const events_1 = require("events")
40
- const fs_1 = require("fs")
41
- const os_1 = require("os")
42
- const path_1 = require("path")
43
- const stream_1 = require("stream")
44
- const WAProto_1 = require("../../WAProto")
45
- const Defaults_1 = require("../Defaults")
46
- const WABinary_1 = require("../WABinary")
47
- const crypto_1 = require("./crypto")
48
- const generics_1 = require("./generics")
49
-
50
- const getImageProcessingLibrary = async () => {
51
- const [_jimp, sharp] = await Promise.all([
52
- (async () => {
53
- const jimp = await (Promise.resolve().then(() => __importStar(require('jimp'))).catch(() => { }))
54
- return jimp
55
- })(),
56
- (async () => {
57
- const sharp = await (Promise.resolve().then(() => __importStar(require('sharp'))).catch(() => { }))
58
- return sharp
59
- })()
60
- ])
61
- if (sharp) {
62
- return { sharp }
63
- }
64
- const jimp = _jimp?.default || _jimp
65
- if (jimp) {
66
- return { jimp }
67
- }
68
- throw new boom_1.Boom('No image processing library available')
69
- }
70
-
71
- const hkdfInfoKey = (type) => {
72
- const hkdfInfo = Defaults_1.MEDIA_HKDF_KEY_MAPPING[type]
73
- return `WhatsApp ${hkdfInfo} Keys`
74
- }
75
-
76
- const getRawMediaUploadData = async (media, mediaType, logger) => {
77
- const { stream } = await getStream(media)
78
-
79
- logger?.debug('got stream for raw upload')
80
-
81
- const hasher = Crypto.createHash('sha256')
82
- const filePath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID())
83
- const fileWriteStream = fs_1.createWriteStream(filePath)
84
-
85
- let fileLength = 0
86
-
87
- try {
88
- for await (const data of stream) {
89
- fileLength += data.length
90
- hasher.update(data)
91
-
92
- if (!fileWriteStream.write(data)) {
93
- await events_1.once(fileWriteStream, 'drain')
94
- }
95
- }
96
-
97
- fileWriteStream.end()
98
- await events_1.once(fileWriteStream, 'finish')
99
- stream.destroy()
100
-
101
- const fileSha256 = hasher.digest()
102
-
103
- logger?.debug('hashed data for raw upload')
104
-
105
- return {
106
- filePath: filePath,
107
- fileSha256,
108
- fileLength
109
- }
110
- }
111
- catch (error) {
112
- fileWriteStream.destroy()
113
- stream.destroy()
114
-
115
- try {
116
- await fs_1.promises.unlink(filePath)
117
- }
118
- catch {
119
- //
120
- }
121
- throw error
122
- }
123
- }
124
-
125
- /** generates all the keys required to encrypt/decrypt & sign a media message */
126
- async function getMediaKeys(buffer, mediaType) {
127
- if (!buffer) {
128
- throw new boom_1.Boom('Cannot derive from empty media key')
129
- }
130
- if (typeof buffer === 'string') {
131
- buffer = Buffer.from(buffer.replace('data:base64,', ''), 'base64')
132
- }
133
- // expand using HKDF to 112 bytes, also pass in the relevant app info
134
- const expandedMediaKey = await crypto_1.hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) })
135
- return {
136
- iv: expandedMediaKey.slice(0, 16),
137
- cipherKey: expandedMediaKey.slice(16, 48),
138
- macKey: expandedMediaKey.slice(48, 80),
139
- }
140
- }
141
-
142
- /** Extracts video thumb using FFMPEG */
143
- const extractVideoThumb = (videoPath, time = '00:00:00', size = { width: 320 }) => {
144
- return new Promise((resolve, reject) => {
145
- const args = [
146
- '-ss', time,
147
- '-i', videoPath,
148
- '-y',
149
- '-vf', `scale=${size.width}:-1`,
150
- '-vframes', '1',
151
- '-f', 'image2',
152
- '-vcodec', 'mjpeg',
153
- 'pipe:1'
154
- ]
155
-
156
- const ffmpeg = child_process_1.spawn('ffmpeg', args)
157
- const chunks = []
158
- let errorOutput = ''
159
-
160
- ffmpeg.stdout.on('data', chunk => chunks.push(chunk))
161
- ffmpeg.stderr.on('data', data => {
162
- errorOutput += data.toString()
163
- })
164
- ffmpeg.on('error', reject)
165
- ffmpeg.on('close', code => {
166
- if (code === 0) return resolve(Buffer.concat(chunks))
167
- reject(new Error(`ffmpeg exited with code ${code}\n${errorOutput}`))
168
- })
169
- })
170
- }
171
-
172
- const extractImageThumb = async (bufferOrFilePath, width = 32, quality = 50) => {
173
- if (typeof bufferOrFilePath === "string" && bufferOrFilePath.startsWith("http")) {
174
- const response = await axios_1.default.get(bufferOrFilePath, { responseType: "arraybuffer" })
175
- bufferOrFilePath = Buffer.from(response.data)
176
- }
177
- if (bufferOrFilePath instanceof stream_1.Readable) {
178
- bufferOrFilePath = await toBuffer(bufferOrFilePath)
179
- }
180
- const lib = await getImageProcessingLibrary()
181
- if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
182
- const img = lib.sharp.default(bufferOrFilePath)
183
- const dimensions = await img.metadata()
184
- const buffer = await img
185
- .resize({
186
- width,
187
- height: width,
188
- fit: 'contain',
189
- background: { r: 255, g: 255, b: 255, alpha: 0 }
190
- })
191
- .jpeg({ quality })
192
- .toBuffer()
193
- return {
194
- buffer,
195
- original: {
196
- width: dimensions.width,
197
- height: dimensions.height,
198
- },
199
- }
200
- }
201
- else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
202
- const { read, MIME_JPEG, RESIZE_BEZIER, AUTO } = lib.jimp
203
- const jimp = await read(bufferOrFilePath)
204
- const dimensions = {
205
- width: jimp.getWidth(),
206
- height: jimp.getHeight()
207
- }
208
- const buffer = await jimp
209
- .quality(quality)
210
- .resize(width, AUTO, RESIZE_BEZIER)
211
- .getBufferAsync(MIME_JPEG)
212
- return {
213
- buffer,
214
- original: dimensions
215
- }
216
- }
217
- else {
218
- throw new boom_1.Boom('No image processing library available')
219
- }
220
- }
221
-
222
- const encodeBase64EncodedStringForUpload = (b64) => (encodeURIComponent(b64
223
- .replace(/\+/g, '-')
224
- .replace(/\//g, '_')
225
- .replace(/\=+$/, '')))
226
-
227
- const generateProfilePicture = async (mediaUpload) => {
228
- let bufferOrFilePath
229
- if (Buffer.isBuffer(mediaUpload)) {
230
- bufferOrFilePath = mediaUpload
231
- }
232
- else if ('url' in mediaUpload) {
233
- bufferOrFilePath = mediaUpload.url.toString()
234
- }
235
- else {
236
- bufferOrFilePath = await toBuffer(mediaUpload.stream)
237
- }
238
- const lib = await getImageProcessingLibrary()
239
- let img
240
- if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
241
- img = await lib.sharp.default(bufferOrFilePath)
242
- .resize(720, 720, {
243
- fit: 'inside',
244
- })
245
- .jpeg({ quality: 50 })
246
- .toBuffer()
247
- }
248
- else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
249
- const { read, MIME_JPEG } = lib.jimp
250
- const image = await read(bufferOrFilePath)
251
- const min = image.getWidth()
252
- const max = image.getHeight()
253
- const cropped = image.crop(0, 0, min, max)
254
- img = await cropped.scaleToFit(720, 720).getBufferAsync(MIME_JPEG)
255
- }
256
- else {
257
- throw new boom_1.Boom('No image processing library available')
258
- }
259
- return {
260
- img: await img,
261
- }
262
- }
263
-
264
-
265
- /** gets the SHA256 of the given media message */
266
- const mediaMessageSHA256B64 = (message) => {
267
- const media = Object.values(message)[0]
268
- return (media === null || media === void 0 ? void 0 : media.fileSha256) && Buffer.from(media.fileSha256).toString('base64')
269
- }
270
-
271
- async function getAudioDuration(buffer) {
272
- const musicMetadata = await Promise.resolve().then(() => __importStar(require('music-metadata')))
273
- const options = {
274
- duration: true
275
- }
276
-
277
- let metadata
278
-
279
- if (Buffer.isBuffer(buffer)) {
280
- metadata = await musicMetadata.parseBuffer(buffer, undefined, options)
281
- }
282
- else if (typeof buffer === 'string') {
283
- metadata = await musicMetadata.parseFile(buffer, options)
284
- }
285
- else {
286
- metadata = await musicMetadata.parseStream(buffer, undefined, options)
287
- }
288
- return metadata.format.duration
289
- }
290
-
291
- /**
292
- referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
293
- */
294
- async function getAudioWaveform(buffer, logger) {
295
- try {
296
- const { default: decoder } = await eval('import(\'audio-decode\')')
297
- let audioData
298
- if (Buffer.isBuffer(buffer)) {
299
- audioData = buffer
300
- }
301
- else if (typeof buffer === 'string') {
302
- const rStream = fs_1.createReadStream(buffer)
303
- audioData = await toBuffer(rStream)
304
- }
305
- else {
306
- audioData = await toBuffer(buffer)
307
- }
308
- const audioBuffer = await decoder(audioData)
309
- const rawData = audioBuffer.getChannelData(0) // We only need to work with one channel of data
310
- const samples = 64 // Number of samples we want to have in our final data set
311
- const blockSize = Math.floor(rawData.length / samples) // the number of samples in each subdivision
312
- const filteredData = []
313
- for (let i = 0; i < samples; i++) {
314
- const blockStart = blockSize * i // the location of the first sample in the block
315
- let sum = 0
316
- for (let j = 0; j < blockSize; j++) {
317
- sum = sum + Math.abs(rawData[blockStart + j]) // find the sum of all the samples in the block
318
- }
319
- filteredData.push(sum / blockSize) // divide the sum by the block size to get the average
320
- }
321
- // This guarantees that the largest data point will be set to 1, and the rest of the data will scale proportionally.
322
- const multiplier = Math.pow(Math.max(...filteredData), -1)
323
- const normalizedData = filteredData.map((n) => n * multiplier)
324
- // Generate waveform like WhatsApp
325
- const waveform = new Uint8Array(normalizedData.map((n) => Math.floor(100 * n)))
326
- return waveform
327
- }
328
- catch (e) {
329
- logger?.debug('Failed to generate waveform: ' + e)
330
- }
331
- }
332
-
333
- const toReadable = (buffer) => {
334
- const readable = new stream_1.Readable({ read: () => { } })
335
- readable.push(buffer)
336
- readable.push(null)
337
- return readable
338
- }
339
-
340
- const toBuffer = async (stream) => {
341
- const chunks = []
342
- for await (const chunk of stream) {
343
- chunks.push(chunk)
344
- }
345
- stream.destroy()
346
- return Buffer.concat(chunks)
347
- }
348
-
349
- const getStream = async (item, opts) => {
350
- if (Buffer.isBuffer(item)) {
351
- return { stream: toReadable(item), type: 'buffer' }
352
- }
353
-
354
- if ('stream' in item) {
355
- return { stream: item.stream, type: 'readable' }
356
- }
357
-
358
- const urlStr = item.url.toString()
359
-
360
- if (urlStr.startsWith('data:')) {
361
- const buffer = Buffer.from(urlStr.split(',')[1], 'base64')
362
- return { stream: await toReadable(buffer), type: 'buffer' }
363
- }
364
-
365
- if (urlStr.startsWith('http://') || urlStr.startsWith('https://')) {
366
- return { stream: await getHttpStream(item.url, opts), type: 'remote' }
367
- }
368
-
369
- return { stream: fs_1.createReadStream(item.url), type: 'file' }
370
- }
371
-
372
- /** generates a thumbnail for a given media, if required */
373
- async function generateThumbnail(file, mediaType, options) {
374
- let thumbnail
375
- let originalImageDimensions
376
- if (mediaType === 'image') {
377
- const { buffer, original } = await extractImageThumb(file, 256, 95)
378
- thumbnail = buffer.toString('base64')
379
- if (original.width && original.height) {
380
- originalImageDimensions = {
381
- width: original.width,
382
- height: original.height,
383
- }
384
- }
385
- }
386
- else if (mediaType === 'video') {
387
- try {
388
- const buff = await extractVideoThumb(file, '00:00:00', { width: 32, height: 32 })
389
- thumbnail = buff.toString('base64')
390
- }
391
- catch (err) {
392
- options?.logger?.debug('could not generate video thumb: ' + err)
393
- }
394
- }
395
- return {
396
- thumbnail,
397
- originalImageDimensions
398
- }
399
- }
400
-
401
- const getHttpStream = async (url, options = {}) => {
402
- const fetched = await axios_1.default.get(url.toString(), { ...options, responseType: 'stream' })
403
- return fetched.data
404
- }
405
-
406
- const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
407
- const { stream, type } = await getStream(media, opts)
408
- logger?.debug('fetched media stream')
409
-
410
- const encFilePath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID() + '-plain')
411
- const encFileWriteStream = fs_1.createWriteStream(encFilePath)
412
-
413
- let originalFilePath
414
- let originalFileStream
415
-
416
- if (type === 'file') {
417
- originalFilePath = media.url.toString()
418
- } else if (saveOriginalFileIfRequired) {
419
- originalFilePath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID() + '-original')
420
- originalFileStream = fs_1.createWriteStream(originalFilePath)
421
- }
422
-
423
- let fileLength = 0
424
- const sha256 = Crypto.createHash('sha256')
425
-
426
- try {
427
- for await (const data of stream) {
428
- fileLength += data.length
429
-
430
- if (type === 'remote'
431
- && opts?.maxContentLength
432
- && fileLength + data.length > opts.maxContentLength) {
433
- throw new boom_1.Boom(`content length exceeded when preparing "${type}"`, {
434
- data: { media, type }
435
- })
436
- }
437
-
438
- sha256.update(data)
439
- encFileWriteStream.write(data)
440
-
441
- if (originalFileStream && !originalFileStream.write(data)) {
442
- await events_1.once(originalFileStream, 'drain')
443
- }
444
- }
445
-
446
- const fileSha256 = sha256.digest()
447
- encFileWriteStream.end()
448
- originalFileStream?.end?.call(originalFileStream)
449
- stream.destroy()
450
-
451
- logger?.debug('prepared plain stream successfully')
452
-
453
- return {
454
- mediaKey: undefined,
455
- originalFilePath,
456
- encFilePath,
457
- mac: undefined,
458
- fileEncSha256: undefined,
459
- fileSha256,
460
- fileLength
461
- }
462
- }
463
- catch (error) {
464
- encFileWriteStream.destroy()
465
- originalFileStream?.destroy?.call(originalFileStream)
466
- sha256.destroy()
467
- stream.destroy()
468
- try {
469
- await fs_1.promises.unlink(encFilePath)
470
- if (originalFilePath && didSaveToTmpPath) {
471
- await fs_1.promises.unlink(originalFilePath)
472
- }
473
- } catch (err) {
474
- logger?.error({ err }, 'failed deleting tmp files')
475
- }
476
- throw error
477
- }
478
- }
479
-
480
- const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
481
- const { stream, type } = await getStream(media, opts)
482
- logger?.debug('fetched media stream')
483
- const mediaKey = Crypto.randomBytes(32)
484
- const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType)
485
- const encFilePath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID() + '-enc')
486
- const encFileWriteStream = fs_1.createWriteStream(encFilePath)
487
- let originalFileStream
488
- let originalFilePath
489
- if (saveOriginalFileIfRequired) {
490
- originalFilePath = path_1.join(os_1.tmpdir(), mediaType + generics_1.generateMessageID() + '-original')
491
- originalFileStream = fs_1.createWriteStream(originalFilePath)
492
- }
493
- let fileLength = 0
494
- const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv)
495
- const hmac = Crypto.createHmac('sha256', macKey).update(iv)
496
- const sha256Plain = Crypto.createHash('sha256')
497
- const sha256Enc = Crypto.createHash('sha256')
498
- const onChunk = (buff) => {
499
- sha256Enc.update(buff)
500
- hmac.update(buff)
501
- encFileWriteStream.write(buff)
502
- }
503
- try {
504
- for await (const data of stream) {
505
- fileLength += data.length
506
- if (type === 'remote'
507
- && opts?.maxContentLength
508
- && fileLength + data.length > opts.maxContentLength) {
509
- throw new boom_1.Boom(`content length exceeded when encrypting "${type}"`, {
510
- data: { media, type }
511
- })
512
- }
513
- if (originalFileStream) {
514
- if (!originalFileStream.write(data)) {
515
- await events_1.once(originalFileStream, 'drain')
516
- }
517
- }
518
- sha256Plain.update(data)
519
- onChunk(aes.update(data))
520
- }
521
- onChunk(aes.final())
522
- const mac = hmac.digest().slice(0, 10)
523
- sha256Enc.update(mac)
524
- const fileSha256 = sha256Plain.digest()
525
- const fileEncSha256 = sha256Enc.digest()
526
- encFileWriteStream.write(mac)
527
- encFileWriteStream.end()
528
- originalFileStream?.end?.call(originalFileStream)
529
- stream.destroy()
530
- logger?.debug('encrypted data successfully')
531
- return {
532
- mediaKey,
533
- originalFilePath,
534
- encFilePath,
535
- mac,
536
- fileEncSha256,
537
- fileSha256,
538
- fileLength
539
- }
540
- }
541
- catch (error) {
542
- // destroy all streams with error
543
- encFileWriteStream.destroy()
544
- originalFileStream?.destroy?.call(originalFileStream)
545
- aes.destroy()
546
- hmac.destroy()
547
- sha256Plain.destroy()
548
- sha256Enc.destroy()
549
- stream.destroy()
550
- try {
551
- await fs_1.promises.unlink(encFilePath)
552
- if (originalFilePath) {
553
- await fs_1.promises.unlink(originalFilePath)
554
- }
555
- }
556
- catch (err) {
557
- logger?.error({ err }, 'failed deleting tmp files')
558
- }
559
- throw error
560
- }
561
- }
562
-
563
- const DEF_HOST = 'mmg.whatsapp.net'
564
-
565
- const AES_CHUNK_SIZE = 16
566
-
567
- const toSmallestChunkSize = (num) => {
568
- return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE
569
- }
570
- const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`
571
-
572
- const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
573
- const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/')
574
- const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath)
575
-
576
- if (!downloadUrl) {
577
- throw new boom_1.Boom('No valid media URL or directPath present in message', { statusCode: 400 })
578
- }
579
-
580
- const keys = await getMediaKeys(mediaKey, type)
581
- return downloadEncryptedContent(downloadUrl, keys, opts)
582
- }
583
-
584
- /**
585
- * Decrypts and downloads an AES256-CBC encrypted file given the keys.
586
- * Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
587
- * */
588
- const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
589
- let bytesFetched = 0
590
- let startChunk = 0
591
- let firstBlockIsIV = false
592
- // if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
593
- if (startByte) {
594
- const chunk = toSmallestChunkSize(startByte || 0)
595
- if (chunk) {
596
- startChunk = chunk - AES_CHUNK_SIZE
597
- bytesFetched = chunk
598
- firstBlockIsIV = true
599
- }
600
- }
601
- const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined
602
- const headers = {
603
- ...(options?.headers) || {},
604
- Origin: Defaults_1.DEFAULT_ORIGIN,
605
- }
606
- if (startChunk || endChunk) {
607
- headers.Range = `bytes=${startChunk}-`
608
- if (endChunk) {
609
- headers.Range += endChunk
610
- }
611
- }
612
- // download the message
613
- const fetched = await getHttpStream(downloadUrl, {
614
- ...options || {},
615
- headers,
616
- maxBodyLength: Infinity,
617
- maxContentLength: Infinity,
618
- })
619
- let remainingBytes = Buffer.from([])
620
- let aes
621
- const pushBytes = (bytes, push) => {
622
- if (startByte || endByte) {
623
- const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0)
624
- const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0)
625
- push(bytes.slice(start, end))
626
- bytesFetched += bytes.length
627
- }
628
- else {
629
- push(bytes)
630
- }
631
- }
632
- const output = new stream_1.Transform({
633
- transform(chunk, _, callback) {
634
- let data = Buffer.concat([remainingBytes, chunk])
635
- const decryptLength = toSmallestChunkSize(data.length)
636
- remainingBytes = data.slice(decryptLength)
637
- data = data.slice(0, decryptLength)
638
- if (!aes) {
639
- let ivValue = iv
640
- if (firstBlockIsIV) {
641
- ivValue = data.slice(0, AES_CHUNK_SIZE)
642
- data = data.slice(AES_CHUNK_SIZE)
643
- }
644
- aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue)
645
- // if an end byte that is not EOF is specified
646
- // stop auto padding (PKCS7) -- otherwise throws an error for decryption
647
- if (endByte) {
648
- aes.setAutoPadding(false)
649
- }
650
- }
651
- try {
652
- pushBytes(aes.update(data), b => this.push(b))
653
- callback()
654
- }
655
- catch (error) {
656
- callback(error)
657
- }
658
- },
659
- final(callback) {
660
- try {
661
- pushBytes(aes.final(), b => this.push(b))
662
- callback()
663
- }
664
- catch (error) {
665
- callback(error)
666
- }
667
- },
668
- })
669
- return fetched.pipe(output, { end: true })
670
- }
671
-
672
- function extensionForMediaMessage(message) {
673
- const getExtension = (mimetype) => mimetype.split('')[0].split('/')[1]
674
- const type = Object.keys(message)[0]
675
- let extension
676
- if (type === 'locationMessage' ||
677
- type === 'liveLocationMessage' ||
678
- type === 'productMessage') {
679
- extension = '.jpeg'
680
- }
681
- else {
682
- const messageContent = message[type]
683
- extension = getExtension(messageContent.mimetype)
684
- }
685
- return extension
686
- }
687
-
688
- const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
689
- return async (filePath, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
690
- // send a query JSON to obtain the url & auth token to upload our media
691
- let uploadInfo = await refreshMediaConn(false)
692
- let urls
693
- let media = Defaults_1.MEDIA_PATH_MAP[mediaType]
694
- const hosts = [...customUploadHosts, ...uploadInfo.hosts]
695
- fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64)
696
- if (newsletter) {
697
- media = media?.replace('/mms/', '/newsletter/newsletter-')
698
- }
699
- for (const { hostname } of hosts) {
700
- logger.debug(`uploading to "${hostname}"`)
701
- const auth = encodeURIComponent(uploadInfo.auth) // the auth token
702
- const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
703
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
704
- let result
705
- try {
706
- const body = await axios_1.default.post(url, fs_1.createReadStream(filePath), {
707
- ...options,
708
- maxRedirects: 0,
709
- headers: {
710
- ...options.headers || {},
711
- 'Content-Type': 'application/octet-stream',
712
- 'Origin': Defaults_1.DEFAULT_ORIGIN
713
- },
714
- httpsAgent: fetchAgent,
715
- timeout: timeoutMs,
716
- responseType: 'json',
717
- maxBodyLength: Infinity,
718
- maxContentLength: Infinity,
719
- })
720
- result = body.data
721
- if (result?.url || result?.directPath) {
722
- urls = {
723
- mediaUrl: result.url,
724
- directPath: result.direct_path,
725
- meta_hmac: result.meta_hmac,
726
- fbid: result.fbid,
727
- ts: result.ts
728
- }
729
- break
730
- }
731
- else {
732
- uploadInfo = await refreshMediaConn(true)
733
- throw new Error(`upload failed, reason: ${JSON.stringify(result)}`)
734
- }
735
- }
736
- catch (error) {
737
- if (axios_1.default.isAxiosError(error)) {
738
- result = error.response?.data
739
- }
740
- const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname
741
- logger.warn({ trace: error.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`)
742
- }
743
- }
744
- if (!urls) {
745
- throw new boom_1.Boom('Media upload failed on all hosts', { statusCode: 500 })
746
- }
747
- return urls
748
- }
749
- }
750
-
751
- const getMediaRetryKey = (mediaKey) => {
752
- return crypto_1.hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' })
753
- }
754
- /**
755
- * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
756
- */
757
- const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
758
- const recp = { stanzaId: key.id }
759
- const recpBuffer = WAProto_1.proto.ServerErrorReceipt.encode(recp).finish()
760
- const iv = Crypto.randomBytes(12)
761
- const retryKey = await getMediaRetryKey(mediaKey)
762
- const ciphertext = crypto_1.aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id))
763
- const req = {
764
- tag: 'receipt',
765
- attrs: {
766
- id: key.id,
767
- to: WABinary_1.jidNormalizedUser(meId),
768
- type: 'server-error'
769
- },
770
- content: [
771
- // this encrypt node is actually pretty useless
772
- // the media is returned even without this node
773
- // keeping it here to maintain parity with WA Web
774
- {
775
- tag: 'encrypt',
776
- attrs: {},
777
- content: [
778
- { tag: 'enc_p', attrs: {}, content: ciphertext },
779
- { tag: 'enc_iv', attrs: {}, content: iv }
780
- ]
781
- },
782
- {
783
- tag: 'rmr',
784
- attrs: {
785
- jid: key.remoteJid,
786
- 'from_me': (!!key.fromMe).toString(),
787
- // @ts-ignore
788
- participant: key.participant || undefined
789
- }
790
- }
791
- ]
792
- }
793
- return req
794
- }
795
-
796
- const decodeMediaRetryNode = (node) => {
797
- const rmrNode = WABinary_1.getBinaryNodeChild(node, 'rmr')
798
- const event = {
799
- key: {
800
- id: node.attrs.id,
801
- remoteJid: rmrNode.attrs.jid,
802
- fromMe: rmrNode.attrs.from_me === 'true',
803
- participant: rmrNode.attrs.participant
804
- }
805
- }
806
- const errorNode = WABinary_1.getBinaryNodeChild(node, 'error')
807
- if (errorNode) {
808
- const errorCode = +errorNode.attrs.code
809
- event.error = new boom_1.Boom(`Failed to re-upload media (${errorCode})`, { data: errorNode.attrs, statusCode: getStatusCodeForMediaRetry(errorCode) })
810
- }
811
- else {
812
- const encryptedInfoNode = WABinary_1.getBinaryNodeChild(node, 'encrypt')
813
- const ciphertext = WABinary_1.getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p')
814
- const iv = WABinary_1.getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv')
815
- if (ciphertext && iv) {
816
- event.media = { ciphertext, iv }
817
- }
818
- else {
819
- event.error = new boom_1.Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 })
820
- }
821
- }
822
- return event
823
- }
824
-
825
- const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
826
- const retryKey = await getMediaRetryKey(mediaKey)
827
- const plaintext = crypto_1.aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId))
828
- return WAProto_1.proto.MediaRetryNotification.decode(plaintext)
829
- }
830
-
831
- const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code]
832
-
833
- const MEDIA_RETRY_STATUS_MAP = {
834
- [WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
835
- [WAProto_1.proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
836
- [WAProto_1.proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
837
- [WAProto_1.proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418,
838
- }
839
-
840
- module.exports = {
841
- hkdfInfoKey,
842
- getMediaKeys,
843
- extractVideoThumb,
844
- extractImageThumb,
845
- encodeBase64EncodedStringForUpload,
846
- generateProfilePicture,
847
- mediaMessageSHA256B64,
848
- getAudioDuration,
849
- getAudioWaveform,
850
- toReadable,
851
- toBuffer,
852
- getStream,
853
- generateThumbnail,
854
- getHttpStream,
855
- prepareStream,
856
- encryptedStream,
857
- getUrlFromDirectPath,
858
- downloadContentFromMessage,
859
- downloadEncryptedContent,
860
- extensionForMediaMessage,
861
- getRawMediaUploadData,
862
- getWAUploadToServer,
863
- getMediaRetryKey,
864
- encryptMediaRetryRequest,
865
- decodeMediaRetryNode,
866
- decryptMediaRetryData,
867
- getStatusCodeForMediaRetry,
868
- MEDIA_RETRY_STATUS_MAP
869
- }
1
+ import { Boom } from '@hapi/boom';
2
+ import { exec } from 'child_process';
3
+ import * as Crypto from 'crypto';
4
+ import { once } from 'events';
5
+ import { createReadStream, createWriteStream, promises as fs, WriteStream } from 'fs';
6
+ import { tmpdir } from 'os';
7
+ import { join } from 'path';
8
+ import { Readable, Transform } from 'stream';
9
+ import { URL } from 'url';
10
+ import { proto } from '../../WAProto/index.js';
11
+ import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP } from '../Defaults/index.js';
12
+ import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../WABinary/index.js';
13
+ import { aesDecryptGCM, aesEncryptGCM, hkdf } from './crypto.js';
14
+ import { generateMessageIDV2 } from './generics.js';
15
+ const getTmpFilesDirectory = () => tmpdir();
16
+ const getImageProcessingLibrary = async () => {
17
+ //@ts-ignore
18
+ const [jimp, sharp] = await Promise.all([import('jimp').catch(() => { }), import('sharp').catch(() => { })]);
19
+ if (sharp) {
20
+ return { sharp };
21
+ }
22
+ if (jimp) {
23
+ return { jimp };
24
+ }
25
+ throw new Boom('No image processing library available');
26
+ };
27
+ export const hkdfInfoKey = (type) => {
28
+ const hkdfInfo = MEDIA_HKDF_KEY_MAPPING[type];
29
+ return `WhatsApp ${hkdfInfo} Keys`;
30
+ };
31
+ export const getRawMediaUploadData = async (media, mediaType, logger) => {
32
+ const { stream } = await getStream(media);
33
+ logger?.debug('got stream for raw upload');
34
+ const hasher = Crypto.createHash('sha256');
35
+ const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
36
+ const fileWriteStream = createWriteStream(filePath);
37
+ let fileLength = 0;
38
+ try {
39
+ for await (const data of stream) {
40
+ fileLength += data.length;
41
+ hasher.update(data);
42
+ if (!fileWriteStream.write(data)) {
43
+ await once(fileWriteStream, 'drain');
44
+ }
45
+ }
46
+ fileWriteStream.end();
47
+ await once(fileWriteStream, 'finish');
48
+ stream.destroy();
49
+ const fileSha256 = hasher.digest();
50
+ logger?.debug('hashed data for raw upload');
51
+ return {
52
+ filePath: filePath,
53
+ fileSha256,
54
+ fileLength
55
+ };
56
+ }
57
+ catch (error) {
58
+ fileWriteStream.destroy();
59
+ stream.destroy();
60
+ try {
61
+ await fs.unlink(filePath);
62
+ }
63
+ catch {
64
+ //
65
+ }
66
+ throw error;
67
+ }
68
+ };
69
+ /** generates all the keys required to encrypt/decrypt & sign a media message */
70
+ export async function getMediaKeys(buffer, mediaType) {
71
+ if (!buffer) {
72
+ throw new Boom('Cannot derive from empty media key');
73
+ }
74
+ if (typeof buffer === 'string') {
75
+ buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
76
+ }
77
+ // expand using HKDF to 112 bytes, also pass in the relevant app info
78
+ const expandedMediaKey = await hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
79
+ return {
80
+ iv: expandedMediaKey.slice(0, 16),
81
+ cipherKey: expandedMediaKey.slice(16, 48),
82
+ macKey: expandedMediaKey.slice(48, 80)
83
+ };
84
+ }
85
+ /** Extracts video thumb using FFMPEG */
86
+ const extractVideoThumb = async (path, destPath, time, size) => new Promise((resolve, reject) => {
87
+ const cmd = `ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`;
88
+ exec(cmd, err => {
89
+ if (err) {
90
+ reject(err);
91
+ }
92
+ else {
93
+ resolve();
94
+ }
95
+ });
96
+ });
97
+ export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
98
+ // TODO: Move entirely to sharp, removing jimp as it supports readable streams
99
+ // This will have positive speed and performance impacts as well as minimizing RAM usage.
100
+ if (bufferOrFilePath instanceof Readable) {
101
+ bufferOrFilePath = await toBuffer(bufferOrFilePath);
102
+ }
103
+ const lib = await getImageProcessingLibrary();
104
+ if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
105
+ const img = lib.sharp.default(bufferOrFilePath);
106
+ const dimensions = await img.metadata();
107
+ const buffer = await img.resize(width).jpeg({ quality: 50 }).toBuffer();
108
+ return {
109
+ buffer,
110
+ original: {
111
+ width: dimensions.width,
112
+ height: dimensions.height
113
+ }
114
+ };
115
+ }
116
+ else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
117
+ const jimp = await lib.jimp.Jimp.read(bufferOrFilePath);
118
+ const dimensions = {
119
+ width: jimp.width,
120
+ height: jimp.height
121
+ };
122
+ const buffer = await jimp
123
+ .resize({ w: width, mode: lib.jimp.ResizeStrategy.BILINEAR })
124
+ .getBuffer('image/jpeg', { quality: 50 });
125
+ return {
126
+ buffer,
127
+ original: dimensions
128
+ };
129
+ }
130
+ else {
131
+ throw new Boom('No image processing library available');
132
+ }
133
+ };
134
+ export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''));
135
+ export const generateProfilePicture = async (mediaUpload, dimensions) => {
136
+ let buffer;
137
+ const { width: w = 640, height: h = 640 } = dimensions || {};
138
+ if (Buffer.isBuffer(mediaUpload)) {
139
+ buffer = mediaUpload;
140
+ }
141
+ else {
142
+ // Use getStream to handle all WAMediaUpload types (Buffer, Stream, URL)
143
+ const { stream } = await getStream(mediaUpload);
144
+ // Convert the resulting stream to a buffer
145
+ buffer = await toBuffer(stream);
146
+ }
147
+ const lib = await getImageProcessingLibrary();
148
+ let img;
149
+ if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
150
+ img = lib.sharp
151
+ .default(buffer)
152
+ .resize(w, h)
153
+ .jpeg({
154
+ quality: 50
155
+ })
156
+ .toBuffer();
157
+ }
158
+ else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
159
+ const jimp = await lib.jimp.Jimp.read(buffer);
160
+ const min = Math.min(jimp.width, jimp.height);
161
+ const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min });
162
+ img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy.BILINEAR }).getBuffer('image/jpeg', { quality: 50 });
163
+ }
164
+ else {
165
+ throw new Boom('No image processing library available');
166
+ }
167
+ return {
168
+ img: await img
169
+ };
170
+ };
171
+ /** gets the SHA256 of the given media message */
172
+ export const mediaMessageSHA256B64 = (message) => {
173
+ const media = Object.values(message)[0];
174
+ return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64');
175
+ };
176
+ export async function getAudioDuration(buffer) {
177
+ const musicMetadata = await import('music-metadata');
178
+ let metadata;
179
+ const options = {
180
+ duration: true
181
+ };
182
+ if (Buffer.isBuffer(buffer)) {
183
+ metadata = await musicMetadata.parseBuffer(buffer, undefined, options);
184
+ }
185
+ else if (typeof buffer === 'string') {
186
+ metadata = await musicMetadata.parseFile(buffer, options);
187
+ }
188
+ else {
189
+ metadata = await musicMetadata.parseStream(buffer, undefined, options);
190
+ }
191
+ return metadata.format.duration;
192
+ }
193
+ /**
194
+ referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
195
+ */
196
+ export async function getAudioWaveform(buffer, logger) {
197
+ try {
198
+ // @ts-ignore
199
+ const { default: decoder } = await import('audio-decode');
200
+ let audioData;
201
+ if (Buffer.isBuffer(buffer)) {
202
+ audioData = buffer;
203
+ }
204
+ else if (typeof buffer === 'string') {
205
+ const rStream = createReadStream(buffer);
206
+ audioData = await toBuffer(rStream);
207
+ }
208
+ else {
209
+ audioData = await toBuffer(buffer);
210
+ }
211
+ const audioBuffer = await decoder(audioData);
212
+ const rawData = audioBuffer.getChannelData(0); // We only need to work with one channel of data
213
+ const samples = 64; // Number of samples we want to have in our final data set
214
+ const blockSize = Math.floor(rawData.length / samples); // the number of samples in each subdivision
215
+ const filteredData = [];
216
+ for (let i = 0; i < samples; i++) {
217
+ const blockStart = blockSize * i; // the location of the first sample in the block
218
+ let sum = 0;
219
+ for (let j = 0; j < blockSize; j++) {
220
+ sum = sum + Math.abs(rawData[blockStart + j]); // find the sum of all the samples in the block
221
+ }
222
+ filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
223
+ }
224
+ // This guarantees that the largest data point will be set to 1, and the rest of the data will scale proportionally.
225
+ const multiplier = Math.pow(Math.max(...filteredData), -1);
226
+ const normalizedData = filteredData.map(n => n * multiplier);
227
+ // Generate waveform like WhatsApp
228
+ const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)));
229
+ return waveform;
230
+ }
231
+ catch (e) {
232
+ logger?.debug('Failed to generate waveform: ' + e);
233
+ }
234
+ }
235
+ export const toReadable = (buffer) => {
236
+ const readable = new Readable({ read: () => { } });
237
+ readable.push(buffer);
238
+ readable.push(null);
239
+ return readable;
240
+ };
241
+ export const toBuffer = async (stream) => {
242
+ const chunks = [];
243
+ for await (const chunk of stream) {
244
+ chunks.push(chunk);
245
+ }
246
+ stream.destroy();
247
+ return Buffer.concat(chunks);
248
+ };
249
+ export const getStream = async (item, opts) => {
250
+ if (Buffer.isBuffer(item)) {
251
+ return { stream: toReadable(item), type: 'buffer' };
252
+ }
253
+ if ('stream' in item) {
254
+ return { stream: item.stream, type: 'readable' };
255
+ }
256
+ const urlStr = item.url.toString();
257
+ if (urlStr.startsWith('data:')) {
258
+ const buffer = Buffer.from(urlStr.split(',')[1], 'base64');
259
+ return { stream: toReadable(buffer), type: 'buffer' };
260
+ }
261
+ if (urlStr.startsWith('http://') || urlStr.startsWith('https://')) {
262
+ return { stream: await getHttpStream(item.url, opts), type: 'remote' };
263
+ }
264
+ return { stream: createReadStream(item.url), type: 'file' };
265
+ };
266
+ /** generates a thumbnail for a given media, if required */
267
+ export async function generateThumbnail(file, mediaType, options) {
268
+ let thumbnail;
269
+ let originalImageDimensions;
270
+ if (mediaType === 'image') {
271
+ const { buffer, original } = await extractImageThumb(file);
272
+ thumbnail = buffer.toString('base64');
273
+ if (original.width && original.height) {
274
+ originalImageDimensions = {
275
+ width: original.width,
276
+ height: original.height
277
+ };
278
+ }
279
+ }
280
+ else if (mediaType === 'video') {
281
+ const imgFilename = join(getTmpFilesDirectory(), generateMessageIDV2() + '.jpg');
282
+ try {
283
+ await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
284
+ const buff = await fs.readFile(imgFilename);
285
+ thumbnail = buff.toString('base64');
286
+ await fs.unlink(imgFilename);
287
+ }
288
+ catch (err) {
289
+ options.logger?.debug('could not generate video thumb: ' + err);
290
+ }
291
+ }
292
+ return {
293
+ thumbnail,
294
+ originalImageDimensions
295
+ };
296
+ }
297
+ export const getHttpStream = async (url, options = {}) => {
298
+ const response = await fetch(url.toString(), {
299
+ dispatcher: options.dispatcher,
300
+ method: 'GET',
301
+ headers: options.headers
302
+ });
303
+ if (!response.ok) {
304
+ throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
305
+ }
306
+ // @ts-ignore Node18+ Readable.fromWeb exists
307
+ return Readable.fromWeb(response.body);
308
+ };
309
+ export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
310
+ const { stream, type } = await getStream(media, opts);
311
+ logger?.debug('fetched media stream');
312
+ const mediaKey = Crypto.randomBytes(32);
313
+ const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
314
+ const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-enc');
315
+ const encFileWriteStream = createWriteStream(encFilePath);
316
+ let originalFileStream;
317
+ let originalFilePath;
318
+ if (saveOriginalFileIfRequired) {
319
+ originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-original');
320
+ originalFileStream = createWriteStream(originalFilePath);
321
+ }
322
+ let fileLength = 0;
323
+ const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
324
+ const hmac = Crypto.createHmac('sha256', macKey).update(iv);
325
+ const sha256Plain = Crypto.createHash('sha256');
326
+ const sha256Enc = Crypto.createHash('sha256');
327
+ const onChunk = (buff) => {
328
+ sha256Enc.update(buff);
329
+ hmac.update(buff);
330
+ encFileWriteStream.write(buff);
331
+ };
332
+ try {
333
+ for await (const data of stream) {
334
+ fileLength += data.length;
335
+ if (type === 'remote' &&
336
+ opts?.maxContentLength &&
337
+ fileLength + data.length > opts.maxContentLength) {
338
+ throw new Boom(`content length exceeded when encrypting "${type}"`, {
339
+ data: { media, type }
340
+ });
341
+ }
342
+ if (originalFileStream) {
343
+ if (!originalFileStream.write(data)) {
344
+ await once(originalFileStream, 'drain');
345
+ }
346
+ }
347
+ sha256Plain.update(data);
348
+ onChunk(aes.update(data));
349
+ }
350
+ onChunk(aes.final());
351
+ const mac = hmac.digest().slice(0, 10);
352
+ sha256Enc.update(mac);
353
+ const fileSha256 = sha256Plain.digest();
354
+ const fileEncSha256 = sha256Enc.digest();
355
+ encFileWriteStream.write(mac);
356
+ encFileWriteStream.end();
357
+ originalFileStream?.end?.();
358
+ stream.destroy();
359
+ logger?.debug('encrypted data successfully');
360
+ return {
361
+ mediaKey,
362
+ originalFilePath,
363
+ encFilePath,
364
+ mac,
365
+ fileEncSha256,
366
+ fileSha256,
367
+ fileLength
368
+ };
369
+ }
370
+ catch (error) {
371
+ // destroy all streams with error
372
+ encFileWriteStream.destroy();
373
+ originalFileStream?.destroy?.();
374
+ aes.destroy();
375
+ hmac.destroy();
376
+ sha256Plain.destroy();
377
+ sha256Enc.destroy();
378
+ stream.destroy();
379
+ try {
380
+ await fs.unlink(encFilePath);
381
+ if (originalFilePath) {
382
+ await fs.unlink(originalFilePath);
383
+ }
384
+ }
385
+ catch (err) {
386
+ logger?.error({ err }, 'failed deleting tmp files');
387
+ }
388
+ throw error;
389
+ }
390
+ };
391
+ const DEF_HOST = 'mmg.whatsapp.net';
392
+ const AES_CHUNK_SIZE = 16;
393
+ const toSmallestChunkSize = (num) => {
394
+ return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
395
+ };
396
+ export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
397
+ export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
398
+ const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/');
399
+ const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
400
+ if (!downloadUrl) {
401
+ throw new Boom('No valid media URL or directPath present in message', { statusCode: 400 });
402
+ }
403
+ const keys = await getMediaKeys(mediaKey, type);
404
+ return downloadEncryptedContent(downloadUrl, keys, opts);
405
+ };
406
+ /**
407
+ * Decrypts and downloads an AES256-CBC encrypted file given the keys.
408
+ * Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
409
+ * */
410
+ export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
411
+ let bytesFetched = 0;
412
+ let startChunk = 0;
413
+ let firstBlockIsIV = false;
414
+ // if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
415
+ if (startByte) {
416
+ const chunk = toSmallestChunkSize(startByte || 0);
417
+ if (chunk) {
418
+ startChunk = chunk - AES_CHUNK_SIZE;
419
+ bytesFetched = chunk;
420
+ firstBlockIsIV = true;
421
+ }
422
+ }
423
+ const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
424
+ const headersInit = options?.headers ? options.headers : undefined;
425
+ const headers = {
426
+ ...(headersInit
427
+ ? Array.isArray(headersInit)
428
+ ? Object.fromEntries(headersInit)
429
+ : headersInit
430
+ : {}),
431
+ Origin: DEFAULT_ORIGIN
432
+ };
433
+ if (startChunk || endChunk) {
434
+ headers.Range = `bytes=${startChunk}-`;
435
+ if (endChunk) {
436
+ headers.Range += endChunk;
437
+ }
438
+ }
439
+ // download the message
440
+ const fetched = await getHttpStream(downloadUrl, {
441
+ ...(options || {}),
442
+ headers
443
+ });
444
+ let remainingBytes = Buffer.from([]);
445
+ let aes;
446
+ const pushBytes = (bytes, push) => {
447
+ if (startByte || endByte) {
448
+ const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
449
+ const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
450
+ push(bytes.slice(start, end));
451
+ bytesFetched += bytes.length;
452
+ }
453
+ else {
454
+ push(bytes);
455
+ }
456
+ };
457
+ const output = new Transform({
458
+ transform(chunk, _, callback) {
459
+ let data = Buffer.concat([remainingBytes, chunk]);
460
+ const decryptLength = toSmallestChunkSize(data.length);
461
+ remainingBytes = data.slice(decryptLength);
462
+ data = data.slice(0, decryptLength);
463
+ if (!aes) {
464
+ let ivValue = iv;
465
+ if (firstBlockIsIV) {
466
+ ivValue = data.slice(0, AES_CHUNK_SIZE);
467
+ data = data.slice(AES_CHUNK_SIZE);
468
+ }
469
+ aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
470
+ // if an end byte that is not EOF is specified
471
+ // stop auto padding (PKCS7) -- otherwise throws an error for decryption
472
+ if (endByte) {
473
+ aes.setAutoPadding(false);
474
+ }
475
+ }
476
+ try {
477
+ pushBytes(aes.update(data), b => this.push(b));
478
+ callback();
479
+ }
480
+ catch (error) {
481
+ callback(error);
482
+ }
483
+ },
484
+ final(callback) {
485
+ try {
486
+ pushBytes(aes.final(), b => this.push(b));
487
+ callback();
488
+ }
489
+ catch (error) {
490
+ callback(error);
491
+ }
492
+ }
493
+ });
494
+ return fetched.pipe(output, { end: true });
495
+ };
496
+ export function extensionForMediaMessage(message) {
497
+ const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1];
498
+ const type = Object.keys(message)[0];
499
+ let extension;
500
+ if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') {
501
+ extension = '.jpeg';
502
+ }
503
+ else {
504
+ const messageContent = message[type];
505
+ extension = getExtension(messageContent.mimetype);
506
+ }
507
+ return extension;
508
+ }
509
+ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
510
+ return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
511
+ // send a query JSON to obtain the url & auth token to upload our media
512
+ let uploadInfo = await refreshMediaConn(false);
513
+ let urls;
514
+ const hosts = [...customUploadHosts, ...uploadInfo.hosts];
515
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
516
+ for (const { hostname } of hosts) {
517
+ logger.debug(`uploading to "${hostname}"`);
518
+ const auth = encodeURIComponent(uploadInfo.auth); // the auth token
519
+ const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
520
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
521
+ let result;
522
+ try {
523
+ const stream = createReadStream(filePath);
524
+ const response = await fetch(url, {
525
+ dispatcher: fetchAgent,
526
+ method: 'POST',
527
+ body: stream,
528
+ headers: {
529
+ ...(() => {
530
+ const hdrs = options?.headers;
531
+ if (!hdrs)
532
+ return {};
533
+ return Array.isArray(hdrs) ? Object.fromEntries(hdrs) : hdrs;
534
+ })(),
535
+ 'Content-Type': 'application/octet-stream',
536
+ Origin: DEFAULT_ORIGIN
537
+ },
538
+ duplex: 'half',
539
+ // Note: custom agents/proxy require undici Agent; omitted here.
540
+ signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
541
+ });
542
+ let parsed = undefined;
543
+ try {
544
+ parsed = await response.json();
545
+ }
546
+ catch {
547
+ parsed = undefined;
548
+ }
549
+ result = parsed;
550
+ if (result?.url || result?.directPath) {
551
+ urls = {
552
+ mediaUrl: result.url,
553
+ directPath: result.direct_path,
554
+ meta_hmac: result.meta_hmac,
555
+ fbid: result.fbid,
556
+ ts: result.ts
557
+ };
558
+ break;
559
+ }
560
+ else {
561
+ uploadInfo = await refreshMediaConn(true);
562
+ throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
563
+ }
564
+ }
565
+ catch (error) {
566
+ const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
567
+ logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
568
+ }
569
+ }
570
+ if (!urls) {
571
+ throw new Boom('Media upload failed on all hosts', { statusCode: 500 });
572
+ }
573
+ return urls;
574
+ };
575
+ };
576
+ const getMediaRetryKey = (mediaKey) => {
577
+ return hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
578
+ };
579
+ /**
580
+ * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
581
+ */
582
+ export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
583
+ const recp = { stanzaId: key.id };
584
+ const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
585
+ const iv = Crypto.randomBytes(12);
586
+ const retryKey = await getMediaRetryKey(mediaKey);
587
+ const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
588
+ const req = {
589
+ tag: 'receipt',
590
+ attrs: {
591
+ id: key.id,
592
+ to: jidNormalizedUser(meId),
593
+ type: 'server-error'
594
+ },
595
+ content: [
596
+ // this encrypt node is actually pretty useless
597
+ // the media is returned even without this node
598
+ // keeping it here to maintain parity with WA Web
599
+ {
600
+ tag: 'encrypt',
601
+ attrs: {},
602
+ content: [
603
+ { tag: 'enc_p', attrs: {}, content: ciphertext },
604
+ { tag: 'enc_iv', attrs: {}, content: iv }
605
+ ]
606
+ },
607
+ {
608
+ tag: 'rmr',
609
+ attrs: {
610
+ jid: key.remoteJid,
611
+ from_me: (!!key.fromMe).toString(),
612
+ // @ts-ignore
613
+ participant: key.participant || undefined
614
+ }
615
+ }
616
+ ]
617
+ };
618
+ return req;
619
+ };
620
+ export const decodeMediaRetryNode = (node) => {
621
+ const rmrNode = getBinaryNodeChild(node, 'rmr');
622
+ const event = {
623
+ key: {
624
+ id: node.attrs.id,
625
+ remoteJid: rmrNode.attrs.jid,
626
+ fromMe: rmrNode.attrs.from_me === 'true',
627
+ participant: rmrNode.attrs.participant
628
+ }
629
+ };
630
+ const errorNode = getBinaryNodeChild(node, 'error');
631
+ if (errorNode) {
632
+ const errorCode = +errorNode.attrs.code;
633
+ event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
634
+ data: errorNode.attrs,
635
+ statusCode: getStatusCodeForMediaRetry(errorCode)
636
+ });
637
+ }
638
+ else {
639
+ const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
640
+ const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
641
+ const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
642
+ if (ciphertext && iv) {
643
+ event.media = { ciphertext, iv };
644
+ }
645
+ else {
646
+ event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
647
+ }
648
+ }
649
+ return event;
650
+ };
651
+ export const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
652
+ const retryKey = await getMediaRetryKey(mediaKey);
653
+ const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
654
+ return proto.MediaRetryNotification.decode(plaintext);
655
+ };
656
+ export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
657
+ const MEDIA_RETRY_STATUS_MAP = {
658
+ [proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
659
+ [proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
660
+ [proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
661
+ [proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
662
+ };
663
+ //# sourceMappingURL=messages-media.js.map