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