@kelvdra/baileys 1.0.0

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 (385) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1478 -0
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +4633 -0
  5. package/WAProto/fix-imports.js +29 -0
  6. package/WAProto/index.d.ts +37016 -0
  7. package/WAProto/index.js +169659 -0
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/index.d.ts +62 -0
  10. package/lib/Defaults/index.d.ts.map +1 -0
  11. package/lib/Defaults/index.js +115 -0
  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/Group/group-session-builder.d.ts +15 -0
  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/Group/group_cipher.js +82 -0
  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/Group/sender-message-key.js +26 -0
  60. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  61. package/lib/Signal/libsignal.d.ts +5 -0
  62. package/lib/Signal/libsignal.d.ts.map +1 -0
  63. package/lib/Signal/libsignal.js +342 -0
  64. package/lib/Signal/libsignal.js.map +1 -0
  65. package/lib/Signal/lid-mapping.d.ts +23 -0
  66. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  67. package/lib/Signal/lid-mapping.js +171 -0
  68. package/lib/Signal/lid-mapping.js.map +1 -0
  69. package/lib/Socket/Client/index.d.ts +3 -0
  70. package/lib/Socket/Client/index.d.ts.map +1 -0
  71. package/lib/Socket/Client/index.js +3 -0
  72. package/lib/Socket/Client/index.js.map +1 -0
  73. package/lib/Socket/Client/types.d.ts +16 -0
  74. package/lib/Socket/Client/types.d.ts.map +1 -0
  75. package/lib/Socket/Client/types.js +11 -0
  76. package/lib/Socket/Client/types.js.map +1 -0
  77. package/lib/Socket/Client/websocket.d.ts +13 -0
  78. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  79. package/lib/Socket/Client/websocket.js +50 -0
  80. package/lib/Socket/Client/websocket.js.map +1 -0
  81. package/lib/Socket/business.d.ts +188 -0
  82. package/lib/Socket/business.d.ts.map +1 -0
  83. package/lib/Socket/business.js +376 -0
  84. package/lib/Socket/business.js.map +1 -0
  85. package/lib/Socket/chats.d.ts +98 -0
  86. package/lib/Socket/chats.d.ts.map +1 -0
  87. package/lib/Socket/chats.js +962 -0
  88. package/lib/Socket/chats.js.map +1 -0
  89. package/lib/Socket/communities.d.ts +244 -0
  90. package/lib/Socket/communities.d.ts.map +1 -0
  91. package/lib/Socket/communities.js +431 -0
  92. package/lib/Socket/communities.js.map +1 -0
  93. package/lib/Socket/groups.d.ts +137 -0
  94. package/lib/Socket/groups.d.ts.map +1 -0
  95. package/lib/Socket/groups.js +327 -0
  96. package/lib/Socket/groups.js.map +1 -0
  97. package/lib/Socket/hydra.d.ts +174 -0
  98. package/lib/Socket/hydra.js +715 -0
  99. package/lib/Socket/index.d.ts +231 -0
  100. package/lib/Socket/index.d.ts.map +1 -0
  101. package/lib/Socket/index.js +18 -0
  102. package/lib/Socket/index.js.map +1 -0
  103. package/lib/Socket/messages-recv.d.ts +173 -0
  104. package/lib/Socket/messages-recv.d.ts.map +1 -0
  105. package/lib/Socket/messages-recv.js +1228 -0
  106. package/lib/Socket/messages-recv.js.map +1 -0
  107. package/lib/Socket/messages-send.d.ts +169 -0
  108. package/lib/Socket/messages-send.d.ts.map +1 -0
  109. package/lib/Socket/messages-send.js +1367 -0
  110. package/lib/Socket/messages-send.js.map +1 -0
  111. package/lib/Socket/mex.d.ts +3 -0
  112. package/lib/Socket/mex.d.ts.map +1 -0
  113. package/lib/Socket/mex.js +42 -0
  114. package/lib/Socket/mex.js.map +1 -0
  115. package/lib/Socket/newsletter.d.ts +147 -0
  116. package/lib/Socket/newsletter.d.ts.map +1 -0
  117. package/lib/Socket/newsletter.js +181 -0
  118. package/lib/Socket/newsletter.js.map +1 -0
  119. package/lib/Socket/socket.d.ts +51 -0
  120. package/lib/Socket/socket.d.ts.map +1 -0
  121. package/lib/Socket/socket.js +841 -0
  122. package/lib/Socket/socket.js.map +1 -0
  123. package/lib/Store/index.d.ts +4 -0
  124. package/lib/Store/index.js +4 -0
  125. package/lib/Store/make-cache-manager-store.d.ts +14 -0
  126. package/lib/Store/make-cache-manager-store.js +81 -0
  127. package/lib/Store/make-in-memory-store.d.ts +123 -0
  128. package/lib/Store/make-in-memory-store.js +416 -0
  129. package/lib/Store/make-ordered-dictionary.d.ts +12 -0
  130. package/lib/Store/make-ordered-dictionary.js +82 -0
  131. package/lib/Store/object-repository.d.ts +10 -0
  132. package/lib/Store/object-repository.js +31 -0
  133. package/lib/Types/Auth.d.ts +111 -0
  134. package/lib/Types/Auth.d.ts.map +1 -0
  135. package/lib/Types/Auth.js +2 -0
  136. package/lib/Types/Auth.js.map +1 -0
  137. package/lib/Types/Bussines.d.ts +25 -0
  138. package/lib/Types/Bussines.d.ts.map +1 -0
  139. package/lib/Types/Bussines.js +2 -0
  140. package/lib/Types/Bussines.js.map +1 -0
  141. package/lib/Types/Call.d.ts +14 -0
  142. package/lib/Types/Call.d.ts.map +1 -0
  143. package/lib/Types/Call.js +2 -0
  144. package/lib/Types/Call.js.map +1 -0
  145. package/lib/Types/Chat.d.ts +123 -0
  146. package/lib/Types/Chat.d.ts.map +1 -0
  147. package/lib/Types/Chat.js +8 -0
  148. package/lib/Types/Chat.js.map +1 -0
  149. package/lib/Types/Contact.d.ts +24 -0
  150. package/lib/Types/Contact.d.ts.map +1 -0
  151. package/lib/Types/Contact.js +2 -0
  152. package/lib/Types/Contact.js.map +1 -0
  153. package/lib/Types/Events.d.ts +202 -0
  154. package/lib/Types/Events.d.ts.map +1 -0
  155. package/lib/Types/Events.js +2 -0
  156. package/lib/Types/Events.js.map +1 -0
  157. package/lib/Types/GroupMetadata.d.ts +67 -0
  158. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  159. package/lib/Types/GroupMetadata.js +2 -0
  160. package/lib/Types/GroupMetadata.js.map +1 -0
  161. package/lib/Types/Label.d.ts +47 -0
  162. package/lib/Types/Label.d.ts.map +1 -0
  163. package/lib/Types/Label.js +25 -0
  164. package/lib/Types/Label.js.map +1 -0
  165. package/lib/Types/LabelAssociation.d.ts +30 -0
  166. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  167. package/lib/Types/LabelAssociation.js +7 -0
  168. package/lib/Types/LabelAssociation.js.map +1 -0
  169. package/lib/Types/Message.d.ts +303 -0
  170. package/lib/Types/Message.d.ts.map +1 -0
  171. package/lib/Types/Message.js +11 -0
  172. package/lib/Types/Message.js.map +1 -0
  173. package/lib/Types/Newsletter.d.ts +135 -0
  174. package/lib/Types/Newsletter.d.ts.map +1 -0
  175. package/lib/Types/Newsletter.js +31 -0
  176. package/lib/Types/Newsletter.js.map +1 -0
  177. package/lib/Types/Product.d.ts +79 -0
  178. package/lib/Types/Product.d.ts.map +1 -0
  179. package/lib/Types/Product.js +2 -0
  180. package/lib/Types/Product.js.map +1 -0
  181. package/lib/Types/Signal.d.ts +76 -0
  182. package/lib/Types/Signal.d.ts.map +1 -0
  183. package/lib/Types/Signal.js +2 -0
  184. package/lib/Types/Signal.js.map +1 -0
  185. package/lib/Types/Socket.d.ts +133 -0
  186. package/lib/Types/Socket.d.ts.map +1 -0
  187. package/lib/Types/Socket.js +3 -0
  188. package/lib/Types/Socket.js.map +1 -0
  189. package/lib/Types/State.d.ts +39 -0
  190. package/lib/Types/State.d.ts.map +1 -0
  191. package/lib/Types/State.js +13 -0
  192. package/lib/Types/State.js.map +1 -0
  193. package/lib/Types/USync.d.ts +26 -0
  194. package/lib/Types/USync.d.ts.map +1 -0
  195. package/lib/Types/USync.js +2 -0
  196. package/lib/Types/USync.js.map +1 -0
  197. package/lib/Types/index.d.ts +65 -0
  198. package/lib/Types/index.d.ts.map +1 -0
  199. package/lib/Types/index.js +26 -0
  200. package/lib/Types/index.js.map +1 -0
  201. package/lib/Utils/auth-utils.d.ts +19 -0
  202. package/lib/Utils/auth-utils.d.ts.map +1 -0
  203. package/lib/Utils/auth-utils.js +257 -0
  204. package/lib/Utils/auth-utils.js.map +1 -0
  205. package/lib/Utils/baileys-event-stream.d.ts +17 -0
  206. package/lib/Utils/baileys-event-stream.d.ts.map +1 -0
  207. package/lib/Utils/baileys-event-stream.js +56 -0
  208. package/lib/Utils/baileys-event-stream.js.map +1 -0
  209. package/lib/Utils/browser-utils.d.ts +4 -0
  210. package/lib/Utils/browser-utils.d.ts.map +1 -0
  211. package/lib/Utils/browser-utils.js +28 -0
  212. package/lib/Utils/browser-utils.js.map +1 -0
  213. package/lib/Utils/business.d.ts +23 -0
  214. package/lib/Utils/business.d.ts.map +1 -0
  215. package/lib/Utils/business.js +231 -0
  216. package/lib/Utils/business.js.map +1 -0
  217. package/lib/Utils/chat-utils.d.ts +70 -0
  218. package/lib/Utils/chat-utils.d.ts.map +1 -0
  219. package/lib/Utils/chat-utils.js +763 -0
  220. package/lib/Utils/chat-utils.js.map +1 -0
  221. package/lib/Utils/crypto.d.ts +41 -0
  222. package/lib/Utils/crypto.d.ts.map +1 -0
  223. package/lib/Utils/crypto.js +142 -0
  224. package/lib/Utils/crypto.js.map +1 -0
  225. package/lib/Utils/decode-wa-message.d.ts +48 -0
  226. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  227. package/lib/Utils/decode-wa-message.js +279 -0
  228. package/lib/Utils/decode-wa-message.js.map +1 -0
  229. package/lib/Utils/event-buffer.d.ts +34 -0
  230. package/lib/Utils/event-buffer.d.ts.map +1 -0
  231. package/lib/Utils/event-buffer.js +548 -0
  232. package/lib/Utils/event-buffer.js.map +1 -0
  233. package/lib/Utils/generics.d.ts +90 -0
  234. package/lib/Utils/generics.d.ts.map +1 -0
  235. package/lib/Utils/generics.js +381 -0
  236. package/lib/Utils/generics.js.map +1 -0
  237. package/lib/Utils/history.d.ts +19 -0
  238. package/lib/Utils/history.d.ts.map +1 -0
  239. package/lib/Utils/history.js +84 -0
  240. package/lib/Utils/history.js.map +1 -0
  241. package/lib/Utils/index.d.ts +20 -0
  242. package/lib/Utils/index.d.ts.map +1 -0
  243. package/lib/Utils/index.js +20 -0
  244. package/lib/Utils/index.js.map +1 -0
  245. package/lib/Utils/link-preview.d.ts +21 -0
  246. package/lib/Utils/link-preview.d.ts.map +1 -0
  247. package/lib/Utils/link-preview.js +85 -0
  248. package/lib/Utils/link-preview.js.map +1 -0
  249. package/lib/Utils/logger.d.ts +12 -0
  250. package/lib/Utils/logger.d.ts.map +1 -0
  251. package/lib/Utils/logger.js +3 -0
  252. package/lib/Utils/logger.js.map +1 -0
  253. package/lib/Utils/lt-hash.d.ts +13 -0
  254. package/lib/Utils/lt-hash.d.ts.map +1 -0
  255. package/lib/Utils/lt-hash.js +48 -0
  256. package/lib/Utils/lt-hash.js.map +1 -0
  257. package/lib/Utils/make-mutex.d.ts +8 -0
  258. package/lib/Utils/make-mutex.d.ts.map +1 -0
  259. package/lib/Utils/make-mutex.js +40 -0
  260. package/lib/Utils/make-mutex.js.map +1 -0
  261. package/lib/Utils/message-retry-manager.d.ts +82 -0
  262. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  263. package/lib/Utils/message-retry-manager.js +149 -0
  264. package/lib/Utils/message-retry-manager.js.map +1 -0
  265. package/lib/Utils/messages-media.d.ts +114 -0
  266. package/lib/Utils/messages-media.d.ts.map +1 -0
  267. package/lib/Utils/messages-media.js +684 -0
  268. package/lib/Utils/messages-media.js.map +1 -0
  269. package/lib/Utils/messages.d.ts +76 -0
  270. package/lib/Utils/messages.d.ts.map +1 -0
  271. package/lib/Utils/messages.js +820 -0
  272. package/lib/Utils/messages.js.map +1 -0
  273. package/lib/Utils/noise-handler.d.ts +20 -0
  274. package/lib/Utils/noise-handler.d.ts.map +1 -0
  275. package/lib/Utils/noise-handler.js +147 -0
  276. package/lib/Utils/noise-handler.js.map +1 -0
  277. package/lib/Utils/pre-key-manager.d.ts +28 -0
  278. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  279. package/lib/Utils/pre-key-manager.js +106 -0
  280. package/lib/Utils/pre-key-manager.js.map +1 -0
  281. package/lib/Utils/process-message.d.ts +42 -0
  282. package/lib/Utils/process-message.d.ts.map +1 -0
  283. package/lib/Utils/process-message.js +413 -0
  284. package/lib/Utils/process-message.js.map +1 -0
  285. package/lib/Utils/signal.d.ts +34 -0
  286. package/lib/Utils/signal.d.ts.map +1 -0
  287. package/lib/Utils/signal.js +159 -0
  288. package/lib/Utils/signal.js.map +1 -0
  289. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  290. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  291. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  292. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  293. package/lib/Utils/validate-connection.d.ts +11 -0
  294. package/lib/Utils/validate-connection.d.ts.map +1 -0
  295. package/lib/Utils/validate-connection.js +195 -0
  296. package/lib/Utils/validate-connection.js.map +1 -0
  297. package/lib/WABinary/constants.d.ts +28 -0
  298. package/lib/WABinary/constants.d.ts.map +1 -0
  299. package/lib/WABinary/constants.js +1301 -0
  300. package/lib/WABinary/constants.js.map +1 -0
  301. package/lib/WABinary/decode.d.ts +7 -0
  302. package/lib/WABinary/decode.d.ts.map +1 -0
  303. package/lib/WABinary/decode.js +238 -0
  304. package/lib/WABinary/decode.js.map +1 -0
  305. package/lib/WABinary/encode.d.ts +3 -0
  306. package/lib/WABinary/encode.d.ts.map +1 -0
  307. package/lib/WABinary/encode.js +216 -0
  308. package/lib/WABinary/encode.js.map +1 -0
  309. package/lib/WABinary/generic-utils.d.ts +15 -0
  310. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  311. package/lib/WABinary/generic-utils.js +111 -0
  312. package/lib/WABinary/generic-utils.js.map +1 -0
  313. package/lib/WABinary/index.d.ts +6 -0
  314. package/lib/WABinary/index.d.ts.map +1 -0
  315. package/lib/WABinary/index.js +6 -0
  316. package/lib/WABinary/index.js.map +1 -0
  317. package/lib/WABinary/jid-utils.d.ts +48 -0
  318. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  319. package/lib/WABinary/jid-utils.js +96 -0
  320. package/lib/WABinary/jid-utils.js.map +1 -0
  321. package/lib/WABinary/types.d.ts +19 -0
  322. package/lib/WABinary/types.d.ts.map +1 -0
  323. package/lib/WABinary/types.js +2 -0
  324. package/lib/WABinary/types.js.map +1 -0
  325. package/lib/WAM/BinaryInfo.d.ts +9 -0
  326. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  327. package/lib/WAM/BinaryInfo.js +10 -0
  328. package/lib/WAM/BinaryInfo.js.map +1 -0
  329. package/lib/WAM/constants.d.ts +40 -0
  330. package/lib/WAM/constants.d.ts.map +1 -0
  331. package/lib/WAM/constants.js +22853 -0
  332. package/lib/WAM/constants.js.map +1 -0
  333. package/lib/WAM/encode.d.ts +3 -0
  334. package/lib/WAM/encode.d.ts.map +1 -0
  335. package/lib/WAM/encode.js +150 -0
  336. package/lib/WAM/encode.js.map +1 -0
  337. package/lib/WAM/index.d.ts +4 -0
  338. package/lib/WAM/index.d.ts.map +1 -0
  339. package/lib/WAM/index.js +4 -0
  340. package/lib/WAM/index.js.map +1 -0
  341. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  342. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  343. package/lib/WAUSync/Protocols/USyncContactProtocol.js +29 -0
  344. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  345. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  346. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  347. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  348. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  349. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  350. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  351. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  352. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  353. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  354. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  355. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  356. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  357. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  358. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  359. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  360. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  361. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  362. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  363. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  364. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  365. package/lib/WAUSync/Protocols/index.d.ts +5 -0
  366. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  367. package/lib/WAUSync/Protocols/index.js +5 -0
  368. package/lib/WAUSync/Protocols/index.js.map +1 -0
  369. package/lib/WAUSync/USyncQuery.d.ts +29 -0
  370. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  371. package/lib/WAUSync/USyncQuery.js +94 -0
  372. package/lib/WAUSync/USyncQuery.js.map +1 -0
  373. package/lib/WAUSync/USyncUser.d.ts +13 -0
  374. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  375. package/lib/WAUSync/USyncUser.js +23 -0
  376. package/lib/WAUSync/USyncUser.js.map +1 -0
  377. package/lib/WAUSync/index.d.ts +4 -0
  378. package/lib/WAUSync/index.d.ts.map +1 -0
  379. package/lib/WAUSync/index.js +4 -0
  380. package/lib/WAUSync/index.js.map +1 -0
  381. package/lib/index.d.ts +13 -0
  382. package/lib/index.d.ts.map +1 -0
  383. package/lib/index.js +12 -0
  384. package/lib/index.js.map +1 -0
  385. package/package.json +104 -0
@@ -0,0 +1,684 @@
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
+ /**
298
+ * Mengambil stream dari URL, mendukung Web ReadableStream dan Node.js Stream
299
+ */
300
+ export const getHttpStream = async (url, options = {}) => {
301
+ const response = await fetch(url.toString(), {
302
+ dispatcher: options.dispatcher,
303
+ method: 'GET',
304
+ headers: options.headers
305
+ });
306
+
307
+ if (!response.ok) {
308
+ throw new Boom(`Failed to fetch stream from ${url}`, {
309
+ statusCode: response.status,
310
+ data: { url }
311
+ });
312
+ }
313
+
314
+ const body = response.body;
315
+
316
+ // Jika body adalah Web ReadableStream (Node.js 18+), konversi
317
+ if (body && typeof body === 'object' && 'pipeTo' in body && typeof body.pipeTo === 'function') {
318
+ // @ts-ignore - Readable.fromWeb exists in Node.js 18+
319
+ return Readable.fromWeb(body);
320
+ }
321
+
322
+ // Jika body sudah Node.js Readable (misal: PassThrough dari node-fetch)
323
+ if (body && typeof body.pipe === 'function' && typeof body.read === 'function') {
324
+ return body;
325
+ }
326
+
327
+ // Fallback: buat stream kosong atau error
328
+ throw new Error('Response body is not a readable stream');
329
+ };
330
+ export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
331
+ const { stream, type } = await getStream(media, opts);
332
+ logger?.debug('fetched media stream');
333
+ const mediaKey = Crypto.randomBytes(32);
334
+ const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
335
+ const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-enc');
336
+ const encFileWriteStream = createWriteStream(encFilePath);
337
+ let originalFileStream;
338
+ let originalFilePath;
339
+ if (saveOriginalFileIfRequired) {
340
+ originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-original');
341
+ originalFileStream = createWriteStream(originalFilePath);
342
+ }
343
+ let fileLength = 0;
344
+ const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
345
+ const hmac = Crypto.createHmac('sha256', macKey).update(iv);
346
+ const sha256Plain = Crypto.createHash('sha256');
347
+ const sha256Enc = Crypto.createHash('sha256');
348
+ const onChunk = (buff) => {
349
+ sha256Enc.update(buff);
350
+ hmac.update(buff);
351
+ encFileWriteStream.write(buff);
352
+ };
353
+ try {
354
+ for await (const data of stream) {
355
+ fileLength += data.length;
356
+ if (type === 'remote' &&
357
+ opts?.maxContentLength &&
358
+ fileLength + data.length > opts.maxContentLength) {
359
+ throw new Boom(`content length exceeded when encrypting "${type}"`, {
360
+ data: { media, type }
361
+ });
362
+ }
363
+ if (originalFileStream) {
364
+ if (!originalFileStream.write(data)) {
365
+ await once(originalFileStream, 'drain');
366
+ }
367
+ }
368
+ sha256Plain.update(data);
369
+ onChunk(aes.update(data));
370
+ }
371
+ onChunk(aes.final());
372
+ const mac = hmac.digest().slice(0, 10);
373
+ sha256Enc.update(mac);
374
+ const fileSha256 = sha256Plain.digest();
375
+ const fileEncSha256 = sha256Enc.digest();
376
+ encFileWriteStream.write(mac);
377
+ encFileWriteStream.end();
378
+ originalFileStream?.end?.();
379
+ stream.destroy();
380
+ logger?.debug('encrypted data successfully');
381
+ return {
382
+ mediaKey,
383
+ originalFilePath,
384
+ encFilePath,
385
+ mac,
386
+ fileEncSha256,
387
+ fileSha256,
388
+ fileLength
389
+ };
390
+ }
391
+ catch (error) {
392
+ // destroy all streams with error
393
+ encFileWriteStream.destroy();
394
+ originalFileStream?.destroy?.();
395
+ aes.destroy();
396
+ hmac.destroy();
397
+ sha256Plain.destroy();
398
+ sha256Enc.destroy();
399
+ stream.destroy();
400
+ try {
401
+ await fs.unlink(encFilePath);
402
+ if (originalFilePath) {
403
+ await fs.unlink(originalFilePath);
404
+ }
405
+ }
406
+ catch (err) {
407
+ logger?.error({ err }, 'failed deleting tmp files');
408
+ }
409
+ throw error;
410
+ }
411
+ };
412
+ const DEF_HOST = 'mmg.whatsapp.net';
413
+ const AES_CHUNK_SIZE = 16;
414
+ const toSmallestChunkSize = (num) => {
415
+ return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
416
+ };
417
+ export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
418
+ export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
419
+ const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/');
420
+ const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
421
+ if (!downloadUrl) {
422
+ throw new Boom('No valid media URL or directPath present in message', { statusCode: 400 });
423
+ }
424
+ const keys = await getMediaKeys(mediaKey, type);
425
+ return downloadEncryptedContent(downloadUrl, keys, opts);
426
+ };
427
+ /**
428
+ * Decrypts and downloads an AES256-CBC encrypted file given the keys.
429
+ * Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
430
+ * */
431
+ export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
432
+ let bytesFetched = 0;
433
+ let startChunk = 0;
434
+ let firstBlockIsIV = false;
435
+ // if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
436
+ if (startByte) {
437
+ const chunk = toSmallestChunkSize(startByte || 0);
438
+ if (chunk) {
439
+ startChunk = chunk - AES_CHUNK_SIZE;
440
+ bytesFetched = chunk;
441
+ firstBlockIsIV = true;
442
+ }
443
+ }
444
+ const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
445
+ const headersInit = options?.headers ? options.headers : undefined;
446
+ const headers = {
447
+ ...(headersInit
448
+ ? Array.isArray(headersInit)
449
+ ? Object.fromEntries(headersInit)
450
+ : headersInit
451
+ : {}),
452
+ Origin: DEFAULT_ORIGIN
453
+ };
454
+ if (startChunk || endChunk) {
455
+ headers.Range = `bytes=${startChunk}-`;
456
+ if (endChunk) {
457
+ headers.Range += endChunk;
458
+ }
459
+ }
460
+ // download the message
461
+ const fetched = await getHttpStream(downloadUrl, {
462
+ ...(options || {}),
463
+ headers
464
+ });
465
+ let remainingBytes = Buffer.from([]);
466
+ let aes;
467
+ const pushBytes = (bytes, push) => {
468
+ if (startByte || endByte) {
469
+ const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
470
+ const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
471
+ push(bytes.slice(start, end));
472
+ bytesFetched += bytes.length;
473
+ }
474
+ else {
475
+ push(bytes);
476
+ }
477
+ };
478
+ const output = new Transform({
479
+ transform(chunk, _, callback) {
480
+ let data = Buffer.concat([remainingBytes, chunk]);
481
+ const decryptLength = toSmallestChunkSize(data.length);
482
+ remainingBytes = data.slice(decryptLength);
483
+ data = data.slice(0, decryptLength);
484
+ if (!aes) {
485
+ let ivValue = iv;
486
+ if (firstBlockIsIV) {
487
+ ivValue = data.slice(0, AES_CHUNK_SIZE);
488
+ data = data.slice(AES_CHUNK_SIZE);
489
+ }
490
+ aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
491
+ // if an end byte that is not EOF is specified
492
+ // stop auto padding (PKCS7) -- otherwise throws an error for decryption
493
+ if (endByte) {
494
+ aes.setAutoPadding(false);
495
+ }
496
+ }
497
+ try {
498
+ pushBytes(aes.update(data), b => this.push(b));
499
+ callback();
500
+ }
501
+ catch (error) {
502
+ callback(error);
503
+ }
504
+ },
505
+ final(callback) {
506
+ try {
507
+ pushBytes(aes.final(), b => this.push(b));
508
+ callback();
509
+ }
510
+ catch (error) {
511
+ callback(error);
512
+ }
513
+ }
514
+ });
515
+ return fetched.pipe(output, { end: true });
516
+ };
517
+ export function extensionForMediaMessage(message) {
518
+ const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1];
519
+ const type = Object.keys(message)[0];
520
+ let extension;
521
+ if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') {
522
+ extension = '.jpeg';
523
+ }
524
+ else {
525
+ const messageContent = message[type];
526
+ extension = getExtension(messageContent.mimetype);
527
+ }
528
+ return extension;
529
+ }
530
+ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
531
+ return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
532
+ // send a query JSON to obtain the url & auth token to upload our media
533
+ let uploadInfo = await refreshMediaConn(false);
534
+ let urls;
535
+ const hosts = [...customUploadHosts, ...uploadInfo.hosts];
536
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
537
+ for (const { hostname } of hosts) {
538
+ logger.debug(`uploading to "${hostname}"`);
539
+ const auth = encodeURIComponent(uploadInfo.auth); // the auth token
540
+ const url = `https://${hostname}${MEDIA_PATH_MAP[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
541
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
542
+ let result;
543
+ try {
544
+ const stream = createReadStream(filePath);
545
+ const response = await fetch(url, {
546
+ dispatcher: fetchAgent,
547
+ method: 'POST',
548
+ body: stream,
549
+ headers: {
550
+ ...(() => {
551
+ const hdrs = options?.headers;
552
+ if (!hdrs)
553
+ return {};
554
+ return Array.isArray(hdrs) ? Object.fromEntries(hdrs) : hdrs;
555
+ })(),
556
+ 'Content-Type': 'application/octet-stream',
557
+ Origin: DEFAULT_ORIGIN
558
+ },
559
+ duplex: 'half',
560
+ // Note: custom agents/proxy require undici Agent; omitted here.
561
+ signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
562
+ });
563
+ let parsed = undefined;
564
+ try {
565
+ parsed = await response.json();
566
+ }
567
+ catch {
568
+ parsed = undefined;
569
+ }
570
+ result = parsed;
571
+ if (result?.url || result?.directPath) {
572
+ urls = {
573
+ mediaUrl: result.url,
574
+ directPath: result.direct_path,
575
+ meta_hmac: result.meta_hmac,
576
+ fbid: result.fbid,
577
+ ts: result.ts
578
+ };
579
+ break;
580
+ }
581
+ else {
582
+ uploadInfo = await refreshMediaConn(true);
583
+ throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
584
+ }
585
+ }
586
+ catch (error) {
587
+ const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
588
+ logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
589
+ }
590
+ }
591
+ if (!urls) {
592
+ throw new Boom('Media upload failed on all hosts', { statusCode: 500 });
593
+ }
594
+ return urls;
595
+ };
596
+ };
597
+ const getMediaRetryKey = (mediaKey) => {
598
+ return hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
599
+ };
600
+ /**
601
+ * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
602
+ */
603
+ export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
604
+ const recp = { stanzaId: key.id };
605
+ const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
606
+ const iv = Crypto.randomBytes(12);
607
+ const retryKey = await getMediaRetryKey(mediaKey);
608
+ const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
609
+ const req = {
610
+ tag: 'receipt',
611
+ attrs: {
612
+ id: key.id,
613
+ to: jidNormalizedUser(meId),
614
+ type: 'server-error'
615
+ },
616
+ content: [
617
+ // this encrypt node is actually pretty useless
618
+ // the media is returned even without this node
619
+ // keeping it here to maintain parity with WA Web
620
+ {
621
+ tag: 'encrypt',
622
+ attrs: {},
623
+ content: [
624
+ { tag: 'enc_p', attrs: {}, content: ciphertext },
625
+ { tag: 'enc_iv', attrs: {}, content: iv }
626
+ ]
627
+ },
628
+ {
629
+ tag: 'rmr',
630
+ attrs: {
631
+ jid: key.remoteJid,
632
+ from_me: (!!key.fromMe).toString(),
633
+ // @ts-ignore
634
+ participant: key.participant || undefined
635
+ }
636
+ }
637
+ ]
638
+ };
639
+ return req;
640
+ };
641
+ export const decodeMediaRetryNode = (node) => {
642
+ const rmrNode = getBinaryNodeChild(node, 'rmr');
643
+ const event = {
644
+ key: {
645
+ id: node.attrs.id,
646
+ remoteJid: rmrNode.attrs.jid,
647
+ fromMe: rmrNode.attrs.from_me === 'true',
648
+ participant: rmrNode.attrs.participant
649
+ }
650
+ };
651
+ const errorNode = getBinaryNodeChild(node, 'error');
652
+ if (errorNode) {
653
+ const errorCode = +errorNode.attrs.code;
654
+ event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
655
+ data: errorNode.attrs,
656
+ statusCode: getStatusCodeForMediaRetry(errorCode)
657
+ });
658
+ }
659
+ else {
660
+ const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
661
+ const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
662
+ const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
663
+ if (ciphertext && iv) {
664
+ event.media = { ciphertext, iv };
665
+ }
666
+ else {
667
+ event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
668
+ }
669
+ }
670
+ return event;
671
+ };
672
+ export const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
673
+ const retryKey = await getMediaRetryKey(mediaKey);
674
+ const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
675
+ return proto.MediaRetryNotification.decode(plaintext);
676
+ };
677
+ export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
678
+ const MEDIA_RETRY_STATUS_MAP = {
679
+ [proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
680
+ [proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
681
+ [proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
682
+ [proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
683
+ };
684
+ //# sourceMappingURL=messages-media.js.map