@vinzsocket/baileys 2.8.35 → 3.0.10

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 (410) hide show
  1. package/WAProto/GenerateStatics.sh +2 -2
  2. package/WAProto/WAProto.proto +1339 -493
  3. package/WAProto/fix-imports.js +75 -19
  4. package/WAProto/index.d.ts +5274 -1945
  5. package/WAProto/index.js +81079 -20694
  6. package/lib/Defaults/index.d.ts +75 -0
  7. package/lib/Defaults/index.d.ts.map +1 -0
  8. package/lib/Defaults/index.js +122 -110
  9. package/lib/Defaults/index.js.map +1 -0
  10. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  11. package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
  12. package/lib/Signal/Group/ciphertext-message.js +11 -18
  13. package/lib/Signal/Group/ciphertext-message.js.map +1 -0
  14. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  15. package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
  16. package/lib/Signal/Group/group-session-builder.js +27 -35
  17. package/lib/Signal/Group/group-session-builder.js.map +1 -0
  18. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  19. package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
  20. package/lib/Signal/Group/group_cipher.js +75 -95
  21. package/lib/Signal/Group/group_cipher.js.map +1 -0
  22. package/lib/Signal/Group/index.d.ts +12 -0
  23. package/lib/Signal/Group/index.d.ts.map +1 -0
  24. package/lib/Signal/Group/index.js +12 -79
  25. package/lib/Signal/Group/index.js.map +1 -0
  26. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  27. package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
  28. package/lib/Signal/Group/keyhelper.js +14 -25
  29. package/lib/Signal/Group/keyhelper.js.map +1 -0
  30. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  31. package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
  32. package/lib/Signal/Group/sender-chain-key.js +24 -37
  33. package/lib/Signal/Group/sender-chain-key.js.map +1 -0
  34. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  35. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
  36. package/lib/Signal/Group/sender-key-distribution-message.js +62 -61
  37. package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
  38. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  39. package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
  40. package/lib/Signal/Group/sender-key-message.js +64 -68
  41. package/lib/Signal/Group/sender-key-message.js.map +1 -0
  42. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  43. package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
  44. package/lib/Signal/Group/sender-key-name.js +41 -47
  45. package/lib/Signal/Group/sender-key-name.js.map +1 -0
  46. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  47. package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
  48. package/lib/Signal/Group/sender-key-record.js +36 -50
  49. package/lib/Signal/Group/sender-key-record.js.map +1 -0
  50. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  51. package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
  52. package/lib/Signal/Group/sender-key-state.js +76 -96
  53. package/lib/Signal/Group/sender-key-state.js.map +1 -0
  54. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  55. package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
  56. package/lib/Signal/Group/sender-message-key.js +25 -34
  57. package/lib/Signal/Group/sender-message-key.js.map +1 -0
  58. package/lib/Signal/libsignal.d.ts +5 -0
  59. package/lib/Signal/libsignal.d.ts.map +1 -0
  60. package/lib/Signal/libsignal.js +415 -402
  61. package/lib/Signal/libsignal.js.map +1 -0
  62. package/lib/Signal/lid-mapping.d.ts +23 -0
  63. package/lib/Signal/lid-mapping.d.ts.map +1 -0
  64. package/lib/Signal/lid-mapping.js +273 -111
  65. package/lib/Signal/lid-mapping.js.map +1 -0
  66. package/lib/Socket/Client/index.d.ts +3 -0
  67. package/lib/Socket/Client/index.d.ts.map +1 -0
  68. package/lib/Socket/Client/index.js +3 -27
  69. package/lib/Socket/Client/index.js.map +1 -0
  70. package/lib/Socket/Client/types.d.ts +16 -0
  71. package/lib/Socket/Client/types.d.ts.map +1 -0
  72. package/lib/Socket/Client/types.js +10 -17
  73. package/lib/Socket/Client/types.js.map +1 -0
  74. package/lib/Socket/Client/websocket.d.ts +13 -0
  75. package/lib/Socket/Client/websocket.d.ts.map +1 -0
  76. package/lib/Socket/Client/websocket.js +53 -57
  77. package/lib/Socket/Client/websocket.js.map +1 -0
  78. package/lib/Socket/business.d.ts +217 -0
  79. package/lib/Socket/business.d.ts.map +1 -0
  80. package/lib/Socket/business.js +374 -363
  81. package/lib/Socket/business.js.map +1 -0
  82. package/lib/Socket/chats.d.ts +124 -0
  83. package/lib/Socket/chats.d.ts.map +1 -0
  84. package/lib/Socket/chats.js +1162 -1041
  85. package/lib/Socket/chats.js.map +1 -0
  86. package/lib/Socket/communities.d.ts +273 -0
  87. package/lib/Socket/communities.d.ts.map +1 -0
  88. package/lib/Socket/communities.js +418 -469
  89. package/lib/Socket/communities.js.map +1 -0
  90. package/lib/Socket/groups.d.ts +161 -0
  91. package/lib/Socket/groups.d.ts.map +1 -0
  92. package/lib/Socket/groups.js +337 -393
  93. package/lib/Socket/groups.js.map +1 -0
  94. package/lib/Socket/index.d.ts +260 -0
  95. package/lib/Socket/index.d.ts.map +1 -0
  96. package/lib/Socket/index.js +10 -20
  97. package/lib/Socket/index.js.map +1 -0
  98. package/lib/Socket/messages-recv.d.ts +203 -0
  99. package/lib/Socket/messages-recv.d.ts.map +1 -0
  100. package/lib/Socket/messages-recv.js +1738 -1394
  101. package/lib/Socket/messages-recv.js.map +1 -0
  102. package/lib/Socket/messages-send.d.ts +199 -0
  103. package/lib/Socket/messages-send.d.ts.map +1 -0
  104. package/lib/Socket/messages-send.js +1101 -1245
  105. package/lib/Socket/messages-send.js.map +1 -0
  106. package/lib/Socket/mex.d.ts +3 -0
  107. package/lib/Socket/mex.d.ts.map +1 -0
  108. package/lib/Socket/mex.js +38 -51
  109. package/lib/Socket/mex.js.map +1 -0
  110. package/lib/Socket/newsletter.d.ts +170 -0
  111. package/lib/Socket/newsletter.d.ts.map +1 -0
  112. package/lib/Socket/newsletter.js +175 -225
  113. package/lib/Socket/newsletter.js.map +1 -0
  114. package/lib/Socket/socket.d.ts +59 -0
  115. package/lib/Socket/socket.d.ts.map +1 -0
  116. package/lib/Socket/socket.js +930 -914
  117. package/lib/Socket/socket.js.map +1 -0
  118. package/lib/Types/Auth.d.ts +117 -0
  119. package/lib/Types/Auth.d.ts.map +1 -0
  120. package/lib/Types/Auth.js +2 -5
  121. package/lib/Types/Auth.js.map +1 -0
  122. package/lib/Types/Bussines.d.ts +25 -0
  123. package/lib/Types/Bussines.d.ts.map +1 -0
  124. package/lib/Types/Bussines.js +2 -5
  125. package/lib/Types/Bussines.js.map +1 -0
  126. package/lib/Types/Call.d.ts +15 -0
  127. package/lib/Types/Call.d.ts.map +1 -0
  128. package/lib/Types/Call.js +2 -5
  129. package/lib/Types/Call.js.map +1 -0
  130. package/lib/Types/Chat.d.ts +124 -0
  131. package/lib/Types/Chat.d.ts.map +1 -0
  132. package/lib/Types/Chat.js +7 -7
  133. package/lib/Types/Chat.js.map +1 -0
  134. package/lib/Types/Contact.d.ts +26 -0
  135. package/lib/Types/Contact.d.ts.map +1 -0
  136. package/lib/Types/Contact.js +2 -5
  137. package/lib/Types/Contact.js.map +1 -0
  138. package/lib/Types/Events.d.ts +256 -0
  139. package/lib/Types/Events.d.ts.map +1 -0
  140. package/lib/Types/Events.js +2 -3
  141. package/lib/Types/Events.js.map +1 -0
  142. package/lib/Types/GroupMetadata.d.ts +71 -0
  143. package/lib/Types/GroupMetadata.d.ts.map +1 -0
  144. package/lib/Types/GroupMetadata.js +2 -5
  145. package/lib/Types/GroupMetadata.js.map +1 -0
  146. package/lib/Types/Label.d.ts +47 -0
  147. package/lib/Types/Label.d.ts.map +1 -0
  148. package/lib/Types/Label.js +22 -28
  149. package/lib/Types/Label.js.map +1 -0
  150. package/lib/Types/LabelAssociation.d.ts +30 -0
  151. package/lib/Types/LabelAssociation.d.ts.map +1 -0
  152. package/lib/Types/LabelAssociation.js +4 -10
  153. package/lib/Types/LabelAssociation.js.map +1 -0
  154. package/lib/Types/Message.d.ts +320 -0
  155. package/lib/Types/Message.d.ts.map +1 -0
  156. package/lib/Types/Message.js +8 -20
  157. package/lib/Types/Message.js.map +1 -0
  158. package/lib/Types/Mex.d.ts +141 -0
  159. package/lib/Types/Mex.d.ts.map +1 -0
  160. package/lib/Types/Mex.js +37 -0
  161. package/lib/Types/Mex.js.map +1 -0
  162. package/lib/Types/Product.d.ts +79 -0
  163. package/lib/Types/Product.d.ts.map +1 -0
  164. package/lib/Types/Product.js +2 -5
  165. package/lib/Types/Product.js.map +1 -0
  166. package/lib/Types/Signal.d.ts +87 -0
  167. package/lib/Types/Signal.d.ts.map +1 -0
  168. package/lib/Types/Signal.js +2 -3
  169. package/lib/Types/Signal.js.map +1 -0
  170. package/lib/Types/Socket.d.ts +136 -0
  171. package/lib/Types/Socket.d.ts.map +1 -0
  172. package/lib/Types/Socket.js +3 -4
  173. package/lib/Types/Socket.js.map +1 -0
  174. package/lib/Types/State.d.ts +97 -0
  175. package/lib/Types/State.d.ts.map +1 -0
  176. package/lib/Types/State.js +54 -17
  177. package/lib/Types/State.js.map +1 -0
  178. package/lib/Types/USync.d.ts +26 -0
  179. package/lib/Types/USync.d.ts.map +1 -0
  180. package/lib/Types/USync.js +2 -3
  181. package/lib/Types/USync.js.map +1 -0
  182. package/lib/Types/index.d.ts +65 -0
  183. package/lib/Types/index.d.ts.map +1 -0
  184. package/lib/Types/index.js +24 -165
  185. package/lib/Types/index.js.map +1 -0
  186. package/lib/Utils/auth-utils.d.ts +24 -0
  187. package/lib/Utils/auth-utils.d.ts.map +1 -0
  188. package/lib/Utils/auth-utils.js +272 -461
  189. package/lib/Utils/auth-utils.js.map +1 -0
  190. package/lib/Utils/browser-utils.d.ts +4 -0
  191. package/lib/Utils/browser-utils.d.ts.map +1 -0
  192. package/lib/Utils/browser-utils.js +28 -0
  193. package/lib/Utils/browser-utils.js.map +1 -0
  194. package/lib/Utils/business.d.ts +23 -0
  195. package/lib/Utils/business.d.ts.map +1 -0
  196. package/lib/Utils/business.js +207 -223
  197. package/lib/Utils/business.js.map +1 -0
  198. package/lib/Utils/chat-utils.d.ts +100 -0
  199. package/lib/Utils/chat-utils.d.ts.map +1 -0
  200. package/lib/Utils/chat-utils.js +828 -803
  201. package/lib/Utils/chat-utils.js.map +1 -0
  202. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  203. package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
  204. package/lib/Utils/companion-reg-client-utils.js +35 -0
  205. package/lib/Utils/companion-reg-client-utils.js.map +1 -0
  206. package/lib/Utils/crypto.d.ts +37 -0
  207. package/lib/Utils/crypto.d.ts.map +1 -0
  208. package/lib/Utils/crypto.js +87 -144
  209. package/lib/Utils/crypto.js.map +1 -0
  210. package/lib/Utils/decode-wa-message.d.ts +66 -0
  211. package/lib/Utils/decode-wa-message.d.ts.map +1 -0
  212. package/lib/Utils/decode-wa-message.js +291 -262
  213. package/lib/Utils/decode-wa-message.js.map +1 -0
  214. package/lib/Utils/event-buffer.d.ts +36 -0
  215. package/lib/Utils/event-buffer.d.ts.map +1 -0
  216. package/lib/Utils/event-buffer.js +598 -560
  217. package/lib/Utils/event-buffer.js.map +1 -0
  218. package/lib/Utils/generics.d.ts +91 -0
  219. package/lib/Utils/generics.d.ts.map +1 -0
  220. package/lib/Utils/generics.js +329 -363
  221. package/lib/Utils/generics.js.map +1 -0
  222. package/lib/Utils/history.d.ts +24 -0
  223. package/lib/Utils/history.d.ts.map +1 -0
  224. package/lib/Utils/history.js +129 -96
  225. package/lib/Utils/history.js.map +1 -0
  226. package/lib/Utils/identity-change-handler.d.ts +44 -0
  227. package/lib/Utils/identity-change-handler.d.ts.map +1 -0
  228. package/lib/Utils/identity-change-handler.js +50 -0
  229. package/lib/Utils/identity-change-handler.js.map +1 -0
  230. package/lib/Utils/index.d.ts +22 -0
  231. package/lib/Utils/index.d.ts.map +1 -0
  232. package/lib/Utils/index.js +22 -236
  233. package/lib/Utils/index.js.map +1 -0
  234. package/lib/Utils/link-preview.d.ts +21 -0
  235. package/lib/Utils/link-preview.d.ts.map +1 -0
  236. package/lib/Utils/link-preview.js +69 -86
  237. package/lib/Utils/link-preview.js.map +1 -0
  238. package/lib/Utils/logger.d.ts +12 -0
  239. package/lib/Utils/logger.d.ts.map +1 -0
  240. package/lib/Utils/logger.js +3 -11
  241. package/lib/Utils/logger.js.map +1 -0
  242. package/lib/Utils/lt-hash.d.ts +8 -0
  243. package/lib/Utils/lt-hash.d.ts.map +1 -0
  244. package/lib/Utils/lt-hash.js +2 -52
  245. package/lib/Utils/lt-hash.js.map +1 -0
  246. package/lib/Utils/make-mutex.d.ts +9 -0
  247. package/lib/Utils/make-mutex.d.ts.map +1 -0
  248. package/lib/Utils/make-mutex.js +30 -43
  249. package/lib/Utils/make-mutex.js.map +1 -0
  250. package/lib/Utils/message-retry-manager.d.ts +115 -0
  251. package/lib/Utils/message-retry-manager.d.ts.map +1 -0
  252. package/lib/Utils/message-retry-manager.js +259 -162
  253. package/lib/Utils/message-retry-manager.js.map +1 -0
  254. package/lib/Utils/messages-media.d.ts +133 -0
  255. package/lib/Utils/messages-media.d.ts.map +1 -0
  256. package/lib/Utils/messages-media.js +732 -711
  257. package/lib/Utils/messages-media.js.map +1 -0
  258. package/lib/Utils/messages.d.ts +91 -0
  259. package/lib/Utils/messages.d.ts.map +1 -0
  260. package/lib/Utils/messages.js +817 -803
  261. package/lib/Utils/messages.js.map +1 -0
  262. package/lib/Utils/noise-handler.d.ts +20 -0
  263. package/lib/Utils/noise-handler.d.ts.map +1 -0
  264. package/lib/Utils/noise-handler.js +195 -168
  265. package/lib/Utils/noise-handler.js.map +1 -0
  266. package/lib/Utils/offline-node-processor.d.ts +17 -0
  267. package/lib/Utils/offline-node-processor.d.ts.map +1 -0
  268. package/lib/Utils/offline-node-processor.js +40 -0
  269. package/lib/Utils/offline-node-processor.js.map +1 -0
  270. package/lib/Utils/pre-key-manager.d.ts +28 -0
  271. package/lib/Utils/pre-key-manager.d.ts.map +1 -0
  272. package/lib/Utils/pre-key-manager.js +106 -0
  273. package/lib/Utils/pre-key-manager.js.map +1 -0
  274. package/lib/Utils/process-message.d.ts +60 -0
  275. package/lib/Utils/process-message.d.ts.map +1 -0
  276. package/lib/Utils/process-message.js +601 -424
  277. package/lib/Utils/process-message.js.map +1 -0
  278. package/lib/Utils/reporting-utils.d.ts +11 -0
  279. package/lib/Utils/reporting-utils.d.ts.map +1 -0
  280. package/lib/Utils/reporting-utils.js +258 -0
  281. package/lib/Utils/reporting-utils.js.map +1 -0
  282. package/lib/Utils/signal.d.ts +47 -0
  283. package/lib/Utils/signal.d.ts.map +1 -0
  284. package/lib/Utils/signal.js +180 -206
  285. package/lib/Utils/signal.js.map +1 -0
  286. package/lib/Utils/stanza-ack.d.ts +11 -0
  287. package/lib/Utils/stanza-ack.d.ts.map +1 -0
  288. package/lib/Utils/stanza-ack.js +38 -0
  289. package/lib/Utils/stanza-ack.js.map +1 -0
  290. package/lib/Utils/sync-action-utils.d.ts +19 -0
  291. package/lib/Utils/sync-action-utils.d.ts.map +1 -0
  292. package/lib/Utils/sync-action-utils.js +49 -0
  293. package/lib/Utils/sync-action-utils.js.map +1 -0
  294. package/lib/Utils/tc-token-utils.d.ts +37 -0
  295. package/lib/Utils/tc-token-utils.d.ts.map +1 -0
  296. package/lib/Utils/tc-token-utils.js +163 -0
  297. package/lib/Utils/tc-token-utils.js.map +1 -0
  298. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  299. package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
  300. package/lib/Utils/use-multi-file-auth-state.js +98 -101
  301. package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
  302. package/lib/Utils/validate-connection.d.ts +11 -0
  303. package/lib/Utils/validate-connection.d.ts.map +1 -0
  304. package/lib/Utils/validate-connection.js +191 -188
  305. package/lib/Utils/validate-connection.js.map +1 -0
  306. package/lib/WABinary/constants.d.ts +28 -0
  307. package/lib/WABinary/constants.d.ts.map +1 -0
  308. package/lib/WABinary/constants.js +1295 -36
  309. package/lib/WABinary/constants.js.map +1 -0
  310. package/lib/WABinary/decode.d.ts +7 -0
  311. package/lib/WABinary/decode.d.ts.map +1 -0
  312. package/lib/WABinary/decode.js +254 -232
  313. package/lib/WABinary/decode.js.map +1 -0
  314. package/lib/WABinary/encode.d.ts +3 -0
  315. package/lib/WABinary/encode.d.ts.map +1 -0
  316. package/lib/WABinary/encode.js +208 -210
  317. package/lib/WABinary/encode.js.map +1 -0
  318. package/lib/WABinary/generic-utils.d.ts +15 -0
  319. package/lib/WABinary/generic-utils.d.ts.map +1 -0
  320. package/lib/WABinary/generic-utils.js +97 -106
  321. package/lib/WABinary/generic-utils.js.map +1 -0
  322. package/lib/WABinary/index.d.ts +6 -0
  323. package/lib/WABinary/index.d.ts.map +1 -0
  324. package/lib/WABinary/index.js +6 -60
  325. package/lib/WABinary/index.js.map +1 -0
  326. package/lib/WABinary/jid-utils.d.ts +48 -0
  327. package/lib/WABinary/jid-utils.d.ts.map +1 -0
  328. package/lib/WABinary/jid-utils.js +83 -70
  329. package/lib/WABinary/jid-utils.js.map +1 -0
  330. package/lib/WABinary/types.d.ts +19 -0
  331. package/lib/WABinary/types.d.ts.map +1 -0
  332. package/lib/WABinary/types.js +2 -4
  333. package/lib/WABinary/types.js.map +1 -0
  334. package/lib/WAM/BinaryInfo.d.ts +9 -0
  335. package/lib/WAM/BinaryInfo.d.ts.map +1 -0
  336. package/lib/WAM/BinaryInfo.js +9 -16
  337. package/lib/WAM/BinaryInfo.js.map +1 -0
  338. package/lib/WAM/constants.d.ts +40 -0
  339. package/lib/WAM/constants.d.ts.map +1 -0
  340. package/lib/WAM/constants.js +22853 -13001
  341. package/lib/WAM/constants.js.map +1 -0
  342. package/lib/WAM/encode.d.ts +3 -0
  343. package/lib/WAM/encode.d.ts.map +1 -0
  344. package/lib/WAM/encode.js +133 -133
  345. package/lib/WAM/encode.js.map +1 -0
  346. package/lib/WAM/index.d.ts +4 -0
  347. package/lib/WAM/index.d.ts.map +1 -0
  348. package/lib/WAM/index.js +4 -38
  349. package/lib/WAM/index.js.map +1 -0
  350. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  351. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
  352. package/lib/WAUSync/Protocols/USyncContactProtocol.js +50 -34
  353. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
  354. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  355. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
  356. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -61
  357. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
  358. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  359. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
  360. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +25 -32
  361. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
  362. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  363. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
  364. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +35 -40
  365. package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
  366. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  367. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
  368. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +23 -29
  369. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
  370. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  371. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
  372. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +47 -64
  373. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
  374. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  375. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
  376. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +27 -26
  377. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
  378. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  379. package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
  380. package/lib/WAUSync/Protocols/index.js +6 -60
  381. package/lib/WAUSync/Protocols/index.js.map +1 -0
  382. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  383. package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
  384. package/lib/WAUSync/USyncQuery.js +94 -97
  385. package/lib/WAUSync/USyncQuery.js.map +1 -0
  386. package/lib/WAUSync/USyncUser.d.ts +17 -0
  387. package/lib/WAUSync/USyncUser.d.ts.map +1 -0
  388. package/lib/WAUSync/USyncUser.js +30 -29
  389. package/lib/WAUSync/USyncUser.js.map +1 -0
  390. package/lib/WAUSync/index.d.ts +4 -0
  391. package/lib/WAUSync/index.d.ts.map +1 -0
  392. package/lib/WAUSync/index.js +4 -38
  393. package/lib/WAUSync/index.js.map +1 -0
  394. package/lib/index.d.ts +12 -0
  395. package/lib/index.d.ts.map +1 -0
  396. package/lib/index.js +11 -114
  397. package/lib/index.js.map +1 -0
  398. package/package.json +47 -20
  399. package/README.md +0 -1315
  400. package/lib/Signal/Group/queue-job.js +0 -61
  401. package/lib/Store/index.js +0 -49
  402. package/lib/Store/make-cache-manager-store.js +0 -106
  403. package/lib/Store/make-in-memory-store.js +0 -497
  404. package/lib/Store/make-ordered-dictionary.js +0 -83
  405. package/lib/Store/object-repository.js +0 -32
  406. package/lib/Types/Newsletter.js +0 -37
  407. package/lib/Utils/baileys-event-stream.js +0 -74
  408. package/lib/Utils/extended-messages.js +0 -285
  409. package/lib/Utils/lid-resolver.js +0 -81
  410. package/lib/Utils/mention.js +0 -137
@@ -1,491 +1,302 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.initAuthCreds = exports.addTransactionCapability = void 0;
7
- exports.makeCacheableSignalKeyStore = makeCacheableSignalKeyStore;
8
- var _nodeCache = _interopRequireDefault(require("@cacheable/node-cache"));
9
- var _asyncMutex = require("async-mutex");
10
- var _crypto = require("crypto");
11
- var _lruCache = require("lru-cache");
12
- var _index = require("../Defaults/index.js");
13
- var _crypto2 = require("./crypto.js");
14
- var _generics = require("./generics.js");
15
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
1
+ import NodeCache from '@cacheable/node-cache';
2
+ import { Boom } from '@hapi/boom';
3
+ import { AsyncLocalStorage } from 'async_hooks';
4
+ import { Mutex } from 'async-mutex';
5
+ import { randomBytes } from 'crypto';
6
+ import PQueue from 'p-queue';
7
+ import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
8
+ import { Curve, signedKeyPair } from './crypto.js';
9
+ import { delay, generateRegistrationId } from './generics.js';
10
+ import { PreKeyManager } from './pre-key-manager.js';
16
11
  /**
17
12
  * Adds caching capability to a SignalKeyStore
18
13
  * @param store the store to add caching to
19
14
  * @param logger to log trace events
20
15
  * @param _cache cache store to use
21
16
  */
22
- function makeCacheableSignalKeyStore(store, logger, _cache) {
23
- const cache = _cache || new _nodeCache.default({
24
- stdTTL: _index.DEFAULT_CACHE_TTLS.SIGNAL_STORE,
25
- // 5 minutes
26
- useClones: false,
27
- deleteOnExpire: true
28
- });
29
- // Mutex for protecting cache operations
30
- const cacheMutex = new _asyncMutex.Mutex();
31
- function getUniqueId(type, id) {
32
- return `${type}.${id}`;
33
- }
34
- return {
35
- async get(type, ids) {
36
- return cacheMutex.runExclusive(async () => {
37
- const data = {};
38
- const idsToFetch = [];
39
- for (const id of ids) {
40
- const item = cache.get(getUniqueId(type, id));
41
- if (typeof item !== 'undefined') {
42
- data[id] = item;
43
- } else {
44
- idsToFetch.push(id);
45
- }
46
- }
47
- if (idsToFetch.length) {
48
- logger?.trace({
49
- items: idsToFetch.length
50
- }, 'loading from store');
51
- const fetched = await store.get(type, idsToFetch);
52
- for (const id of idsToFetch) {
53
- const item = fetched[id];
54
- if (item) {
55
- data[id] = item;
56
- cache.set(getUniqueId(type, id), item);
57
- }
58
- }
59
- }
60
- return data;
61
- });
62
- },
63
- async set(data) {
64
- return cacheMutex.runExclusive(async () => {
65
- let keys = 0;
66
- for (const type in data) {
67
- for (const id in data[type]) {
68
- cache.set(getUniqueId(type, id), data[type][id]);
69
- keys += 1;
70
- }
71
- }
72
- logger?.trace({
73
- keys
74
- }, 'updated cache');
75
- await store.set(data);
76
- });
77
- },
78
- async clear() {
79
- cache.flushAll();
80
- await store.clear?.();
81
- }
82
- };
83
- }
84
- // Module-level specialized mutexes for pre-key operations
85
- const preKeyMutex = new _asyncMutex.Mutex();
86
- const signedPreKeyMutex = new _asyncMutex.Mutex();
87
- /**
88
- * Get the appropriate mutex for the key type
89
- */
90
- const getPreKeyMutex = keyType => {
91
- return keyType === 'signed-pre-key' ? signedPreKeyMutex : preKeyMutex;
92
- };
93
- /**
94
- * Handles pre-key operations with mutex protection
95
- */
96
- async function handlePreKeyOperations(data, keyType, transactionCache, mutations, logger, isInTransaction, state) {
97
- const mutex = getPreKeyMutex(keyType);
98
- await mutex.runExclusive(async () => {
99
- const keyData = data[keyType];
100
- if (!keyData) return;
101
- // Ensure structures exist
102
- transactionCache[keyType] = transactionCache[keyType] || {};
103
- mutations[keyType] = mutations[keyType] || {};
104
- // Separate deletions from updates for batch processing
105
- const deletionKeys = [];
106
- const updateKeys = [];
107
- for (const keyId in keyData) {
108
- if (keyData[keyId] === null) {
109
- deletionKeys.push(keyId);
110
- } else {
111
- updateKeys.push(keyId);
112
- }
113
- }
114
- // Process updates first (no validation needed)
115
- for (const keyId of updateKeys) {
116
- if (transactionCache[keyType]) {
117
- transactionCache[keyType][keyId] = keyData[keyId];
118
- }
119
- if (mutations[keyType]) {
120
- mutations[keyType][keyId] = keyData[keyId];
121
- }
122
- }
123
- // Process deletions with validation
124
- if (deletionKeys.length === 0) return;
125
- if (isInTransaction) {
126
- // In transaction, only allow deletion if key exists in cache
127
- for (const keyId of deletionKeys) {
128
- if (transactionCache[keyType]) {
129
- transactionCache[keyType][keyId] = null;
130
- if (mutations[keyType]) {
131
- // Mark for deletion in mutations
132
- mutations[keyType][keyId] = null;
133
- }
134
- } else {
135
- logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`);
136
- }
137
- }
138
- return;
139
- }
140
- // Outside transaction, batch validate all deletions
141
- if (!state) return;
142
- const existingKeys = await state.get(keyType, deletionKeys);
143
- for (const keyId of deletionKeys) {
144
- if (existingKeys[keyId]) {
145
- if (transactionCache[keyType]) transactionCache[keyType][keyId] = null;
146
- if (mutations[keyType]) mutations[keyType][keyId] = null;
147
- } else {
148
- logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
149
- }
17
+ export function makeCacheableSignalKeyStore(store, logger, _cache) {
18
+ const cache = _cache ||
19
+ new NodeCache({
20
+ stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
21
+ useClones: false,
22
+ deleteOnExpire: true
23
+ });
24
+ // Mutex for protecting cache operations
25
+ const cacheMutex = new Mutex();
26
+ function getUniqueId(type, id) {
27
+ return `${type}.${id}`;
150
28
  }
151
- });
152
- }
153
- /**
154
- * Handles normal key operations for transactions
155
- */
156
- function handleNormalKeyOperations(data, key, transactionCache, mutations) {
157
- Object.assign(transactionCache[key], data[key]);
158
- mutations[key] = mutations[key] || {};
159
- Object.assign(mutations[key], data[key]);
160
- }
161
- /**
162
- * Process pre-key deletions with validation
163
- */
164
- async function processPreKeyDeletions(data, keyType, state, logger) {
165
- const mutex = getPreKeyMutex(keyType);
166
- await mutex.runExclusive(async () => {
167
- const keyData = data[keyType];
168
- if (!keyData) return;
169
- // Validate deletions
170
- for (const keyId in keyData) {
171
- if (keyData[keyId] === null) {
172
- const existingKeys = await state.get(keyType, [keyId]);
173
- if (!existingKeys[keyId]) {
174
- logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
175
- if (data[keyType]) delete data[keyType][keyId];
29
+ return {
30
+ async get(type, ids) {
31
+ return cacheMutex.runExclusive(async () => {
32
+ const data = {};
33
+ const idsToFetch = [];
34
+ for (const id of ids) {
35
+ const item = (await cache.get(getUniqueId(type, id)));
36
+ if (typeof item !== 'undefined') {
37
+ data[id] = item;
38
+ }
39
+ else {
40
+ idsToFetch.push(id);
41
+ }
42
+ }
43
+ if (idsToFetch.length) {
44
+ logger?.trace({ items: idsToFetch.length }, 'loading from store');
45
+ const fetched = await store.get(type, idsToFetch);
46
+ for (const id of idsToFetch) {
47
+ const item = fetched[id];
48
+ if (item) {
49
+ data[id] = item;
50
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
51
+ await cache.set(getUniqueId(type, id), item);
52
+ }
53
+ }
54
+ }
55
+ return data;
56
+ });
57
+ },
58
+ async set(data) {
59
+ return cacheMutex.runExclusive(async () => {
60
+ let keys = 0;
61
+ for (const type in data) {
62
+ for (const id in data[type]) {
63
+ await cache.set(getUniqueId(type, id), data[type][id]);
64
+ keys += 1;
65
+ }
66
+ }
67
+ logger?.trace({ keys }, 'updated cache');
68
+ await store.set(data);
69
+ });
70
+ },
71
+ async clear() {
72
+ await cache.flushAll();
73
+ await store.clear?.();
176
74
  }
177
- }
178
- }
179
- });
180
- }
181
- /**
182
- * Executes a function with mutexes acquired for given key types
183
- * Uses async-mutex's runExclusive with efficient batching
184
- */
185
- async function withMutexes(keyTypes, getKeyTypeMutex, fn) {
186
- if (keyTypes.length === 0) {
187
- return fn();
188
- }
189
- if (keyTypes.length === 1) {
190
- return getKeyTypeMutex(keyTypes[0]).runExclusive(fn);
191
- }
192
- // For multiple mutexes, sort by key type to prevent deadlocks
193
- // Then acquire all mutexes in order using Promise.all for better efficiency
194
- const sortedKeyTypes = [...keyTypes].sort();
195
- const mutexes = sortedKeyTypes.map(getKeyTypeMutex);
196
- // Acquire all mutexes in order to prevent deadlocks
197
- const releases = [];
198
- try {
199
- for (const mutex of mutexes) {
200
- releases.push(await mutex.acquire());
201
- }
202
- return await fn();
203
- } finally {
204
- // Release in reverse order
205
- while (releases.length > 0) {
206
- const release = releases.pop();
207
- if (release) release();
208
- }
209
- }
75
+ };
210
76
  }
211
77
  /**
212
- * Adds DB like transaction capability (https://en.wikipedia.org/wiki/Database_transaction) to the SignalKeyStore,
213
- * this allows batch read & write operations & improves the performance of the lib
78
+ * Adds DB-like transaction capability to the SignalKeyStore
79
+ * Uses AsyncLocalStorage for automatic context management
214
80
  * @param state the key store to apply this capability to
215
81
  * @param logger logger to log events
216
82
  * @returns SignalKeyStore with transaction capability
217
83
  */
218
- const addTransactionCapability = (state, logger, {
219
- maxCommitRetries,
220
- delayBetweenTriesMs
221
- }) => {
222
- // number of queries made to the DB during the transaction
223
- // only there for logging purposes
224
- let dbQueriesInTransaction = 0;
225
- let transactionCache = {};
226
- let mutations = {};
227
- // LRU Cache to hold mutexes for different key types
228
- const mutexCache = new _lruCache.LRUCache({
229
- ttl: 60 * 60 * 1000,
230
- // 1 hour
231
- ttlAutopurge: true,
232
- updateAgeOnGet: true
233
- });
234
- let transactionsInProgress = 0;
235
- function getKeyTypeMutex(type) {
236
- return getMutex(`keytype:${type}`);
237
- }
238
- function getSenderKeyMutex(senderKeyName) {
239
- return getMutex(`senderkey:${senderKeyName}`);
240
- }
241
- function getTransactionMutex(key) {
242
- return getMutex(`transaction:${key}`);
243
- }
244
- // Get or create a mutex for a specific key name
245
- function getMutex(key) {
246
- let mutex = mutexCache.get(key);
247
- if (!mutex) {
248
- mutex = new _asyncMutex.Mutex();
249
- mutexCache.set(key, mutex);
250
- logger.info({
251
- key
252
- }, 'created new mutex');
253
- }
254
- return mutex;
255
- }
256
- // Sender key operations with proper mutex sequencing
257
- function queueSenderKeyOperation(senderKeyName, operation) {
258
- return getSenderKeyMutex(senderKeyName).runExclusive(operation);
259
- }
260
- // Check if we are currently in a transaction
261
- function isInTransaction() {
262
- return transactionsInProgress > 0;
263
- }
264
- // Helper function to handle transaction commit with retries
265
- async function commitTransaction() {
266
- if (!Object.keys(mutations).length) {
267
- logger.trace('no mutations in transaction');
268
- return;
269
- }
270
- logger.trace('committing transaction');
271
- let tries = maxCommitRetries;
272
- while (tries > 0) {
273
- tries -= 1;
274
- try {
275
- await state.set(mutations);
276
- logger.trace({
277
- dbQueriesInTransaction
278
- }, 'committed transaction');
279
- return;
280
- } catch (error) {
281
- logger.warn(`failed to commit ${Object.keys(mutations).length} mutations, tries left=${tries}`);
282
- if (tries > 0) {
283
- await (0, _generics.delay)(delayBetweenTriesMs);
84
+ export const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
85
+ const txStorage = new AsyncLocalStorage();
86
+ // Queues for concurrency control (keyed by signal data type - bounded set)
87
+ const keyQueues = new Map();
88
+ // Transaction mutexes with reference counting for cleanup
89
+ const txMutexes = new Map();
90
+ const txMutexRefCounts = new Map();
91
+ // Pre-key manager for specialized operations
92
+ const preKeyManager = new PreKeyManager(state, logger);
93
+ /**
94
+ * Get or create a queue for a specific key type
95
+ */
96
+ function getQueue(key) {
97
+ if (!keyQueues.has(key)) {
98
+ keyQueues.set(key, new PQueue({ concurrency: 1 }));
284
99
  }
285
- }
100
+ return keyQueues.get(key);
286
101
  }
287
- }
288
- // Helper function to clean up transaction state
289
- function cleanupTransactionState() {
290
- transactionsInProgress -= 1;
291
- if (transactionsInProgress === 0) {
292
- transactionCache = {};
293
- mutations = {};
294
- dbQueriesInTransaction = 0;
102
+ /**
103
+ * Get or create a transaction mutex
104
+ */
105
+ function getTxMutex(key) {
106
+ if (!txMutexes.has(key)) {
107
+ txMutexes.set(key, new Mutex());
108
+ txMutexRefCounts.set(key, 0);
109
+ }
110
+ return txMutexes.get(key);
295
111
  }
296
- }
297
- // Helper function to execute work within transaction
298
- async function executeTransactionWork(work) {
299
- const result = await work();
300
- // commit if this is the outermost transaction
301
- if (transactionsInProgress === 1) {
302
- await commitTransaction();
112
+ /**
113
+ * Acquire a reference to a transaction mutex
114
+ */
115
+ function acquireTxMutexRef(key) {
116
+ const count = txMutexRefCounts.get(key) ?? 0;
117
+ txMutexRefCounts.set(key, count + 1);
303
118
  }
304
- return result;
305
- }
306
- return {
307
- get: async (type, ids) => {
308
- if (isInTransaction()) {
309
- const dict = transactionCache[type];
310
- const idsRequiringFetch = dict ? ids.filter(item => typeof dict[item] === 'undefined') : ids;
311
- // only fetch if there are any items to fetch
312
- if (idsRequiringFetch.length) {
313
- dbQueriesInTransaction += 1;
314
- // Use per-sender-key queue for sender-key operations when possible
315
- if (type === 'sender-key') {
316
- logger.info({
317
- idsRequiringFetch
318
- }, 'processing sender keys in transaction');
319
- // For sender keys, process each one with queued operations to maintain serialization
320
- for (const senderKeyName of idsRequiringFetch) {
321
- await queueSenderKeyOperation(senderKeyName, async () => {
322
- logger.info({
323
- senderKeyName
324
- }, 'fetching sender key in transaction');
325
- const result = await state.get(type, [senderKeyName]);
326
- // Update transaction cache
327
- transactionCache[type] || (transactionCache[type] = {});
328
- Object.assign(transactionCache[type], result);
329
- logger.info({
330
- senderKeyName,
331
- hasResult: !!result[senderKeyName]
332
- }, 'sender key fetch complete');
333
- });
119
+ /**
120
+ * Release a reference to a transaction mutex and cleanup if no longer needed
121
+ */
122
+ function releaseTxMutexRef(key) {
123
+ const count = (txMutexRefCounts.get(key) ?? 1) - 1;
124
+ txMutexRefCounts.set(key, count);
125
+ // Cleanup if no more references and mutex is not locked
126
+ if (count <= 0) {
127
+ const mutex = txMutexes.get(key);
128
+ if (mutex && !mutex.isLocked()) {
129
+ txMutexes.delete(key);
130
+ txMutexRefCounts.delete(key);
334
131
  }
335
- } else {
336
- // Use runExclusive for cleaner mutex handling
337
- await getKeyTypeMutex(type).runExclusive(async () => {
338
- const result = await state.get(type, idsRequiringFetch);
339
- // Update transaction cache
340
- transactionCache[type] || (transactionCache[type] = {});
341
- Object.assign(transactionCache[type], result);
342
- });
343
- }
344
132
  }
345
- return ids.reduce((dict, id) => {
346
- const value = transactionCache[type]?.[id];
347
- if (value) {
348
- dict[id] = value;
349
- }
350
- return dict;
351
- }, {});
352
- } else {
353
- // Not in transaction, fetch directly with queue protection
354
- if (type === 'sender-key') {
355
- // For sender keys, use individual queues to maintain per-key serialization
356
- const results = {};
357
- for (const senderKeyName of ids) {
358
- const result = await queueSenderKeyOperation(senderKeyName, async () => await state.get(type, [senderKeyName]));
359
- Object.assign(results, result);
360
- }
361
- return results;
362
- } else {
363
- return await getKeyTypeMutex(type).runExclusive(() => state.get(type, ids));
133
+ }
134
+ /**
135
+ * Check if currently in a transaction
136
+ */
137
+ function isInTransaction() {
138
+ return !!txStorage.getStore();
139
+ }
140
+ /**
141
+ * Commit transaction with retries
142
+ */
143
+ async function commitWithRetry(mutations) {
144
+ if (Object.keys(mutations).length === 0) {
145
+ logger.trace('no mutations in transaction');
146
+ return;
364
147
  }
365
- }
366
- },
367
- set: async data => {
368
- if (isInTransaction()) {
369
- logger.trace({
370
- types: Object.keys(data)
371
- }, 'caching in transaction');
372
- for (const key_ in data) {
373
- const key = key_;
374
- transactionCache[key] = transactionCache[key] || {};
375
- // Special handling for pre-keys and signed-pre-keys
376
- if (key === 'pre-key') {
377
- await handlePreKeyOperations(data, key, transactionCache, mutations, logger, true);
378
- } else {
379
- // Normal handling for other key types
380
- handleNormalKeyOperations(data, key, transactionCache, mutations);
381
- }
148
+ logger.trace('committing transaction');
149
+ for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
150
+ try {
151
+ await state.set(mutations);
152
+ logger.trace({ mutationCount: Object.keys(mutations).length }, 'committed transaction');
153
+ return;
154
+ }
155
+ catch (error) {
156
+ const retriesLeft = maxCommitRetries - attempt - 1;
157
+ logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
158
+ if (retriesLeft === 0) {
159
+ throw error;
160
+ }
161
+ await delay(delayBetweenTriesMs);
162
+ }
382
163
  }
383
- } else {
384
- // Not in transaction, apply directly with mutex protection
385
- const hasSenderKeys = 'sender-key' in data;
386
- const senderKeyNames = hasSenderKeys ? Object.keys(data['sender-key'] || {}) : [];
387
- if (hasSenderKeys) {
388
- logger.info({
389
- senderKeyNames
390
- }, 'processing sender key set operations');
391
- // Handle sender key operations with per-key queues
392
- for (const senderKeyName of senderKeyNames) {
393
- await queueSenderKeyOperation(senderKeyName, async () => {
394
- // Create data subset for this specific sender key
395
- const senderKeyData = {
396
- 'sender-key': {
397
- [senderKeyName]: data['sender-key'][senderKeyName]
164
+ }
165
+ return {
166
+ get: async (type, ids) => {
167
+ const ctx = txStorage.getStore();
168
+ if (!ctx) {
169
+ // No transaction - direct read without exclusive lock for concurrency
170
+ return state.get(type, ids);
171
+ }
172
+ // In transaction - check cache first
173
+ const cached = ctx.cache[type] || {};
174
+ const missing = ids.filter(id => !(id in cached));
175
+ if (missing.length > 0) {
176
+ ctx.dbQueries++;
177
+ logger.trace({ type, count: missing.length }, 'fetching missing keys in transaction');
178
+ const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
179
+ // Update cache
180
+ ctx.cache[type] = ctx.cache[type] || {};
181
+ Object.assign(ctx.cache[type], fetched);
182
+ }
183
+ // Return requested ids from cache
184
+ const result = {};
185
+ for (const id of ids) {
186
+ const value = ctx.cache[type]?.[id];
187
+ if (value !== undefined && value !== null) {
188
+ result[id] = value;
398
189
  }
399
- };
400
- logger.trace({
401
- senderKeyName
402
- }, 'storing sender key');
403
- // Apply changes to the store
404
- await state.set(senderKeyData);
405
- logger.trace({
406
- senderKeyName
407
- }, 'sender key stored');
408
- });
409
- }
410
- // Handle any non-sender-key data with regular mutexes
411
- const nonSenderKeyData = {
412
- ...data
413
- };
414
- delete nonSenderKeyData['sender-key'];
415
- if (Object.keys(nonSenderKeyData).length > 0) {
416
- await withMutexes(Object.keys(nonSenderKeyData), getKeyTypeMutex, async () => {
417
- // Process pre-keys and signed-pre-keys separately with specialized mutexes
418
- for (const key_ in nonSenderKeyData) {
419
- const keyType = key_;
420
- if (keyType === 'pre-key') {
421
- await processPreKeyDeletions(nonSenderKeyData, keyType, state, logger);
190
+ }
191
+ return result;
192
+ },
193
+ set: async (data) => {
194
+ const ctx = txStorage.getStore();
195
+ if (!ctx) {
196
+ // No transaction - direct write with queue protection
197
+ const types = Object.keys(data);
198
+ // Process pre-keys with validation
199
+ for (const type_ of types) {
200
+ const type = type_;
201
+ if (type === 'pre-key') {
202
+ await preKeyManager.validateDeletions(data, type);
203
+ }
422
204
  }
423
- }
424
- // Apply changes to the store
425
- await state.set(nonSenderKeyData);
426
- });
427
- }
428
- } else {
429
- // No sender keys - use original logic
430
- await withMutexes(Object.keys(data), getKeyTypeMutex, async () => {
431
- // Process pre-keys and signed-pre-keys separately with specialized mutexes
205
+ // Write all data in parallel
206
+ await Promise.all(types.map(type => getQueue(type).add(async () => {
207
+ const typeData = { [type]: data[type] };
208
+ await state.set(typeData);
209
+ })));
210
+ return;
211
+ }
212
+ // In transaction - update cache and mutations
213
+ logger.trace({ types: Object.keys(data) }, 'caching in transaction');
432
214
  for (const key_ in data) {
433
- const keyType = key_;
434
- if (keyType === 'pre-key') {
435
- await processPreKeyDeletions(data, keyType, state, logger);
436
- }
215
+ const key = key_;
216
+ // Ensure structures exist
217
+ ctx.cache[key] = ctx.cache[key] || {};
218
+ ctx.mutations[key] = ctx.mutations[key] || {};
219
+ // Special handling for pre-keys
220
+ if (key === 'pre-key') {
221
+ await preKeyManager.processOperations(data, key, ctx.cache, ctx.mutations, true);
222
+ }
223
+ else {
224
+ // Normal key types
225
+ Object.assign(ctx.cache[key], data[key]);
226
+ Object.assign(ctx.mutations[key], data[key]);
227
+ }
228
+ }
229
+ },
230
+ isInTransaction,
231
+ transaction: async (work, key) => {
232
+ const existing = txStorage.getStore();
233
+ // Nested transaction - reuse existing context
234
+ if (existing) {
235
+ logger.trace('reusing existing transaction context');
236
+ return work();
237
+ }
238
+ // New transaction - acquire mutex and create context
239
+ const mutex = getTxMutex(key);
240
+ acquireTxMutexRef(key);
241
+ try {
242
+ return await mutex.runExclusive(async () => {
243
+ const ctx = {
244
+ cache: {},
245
+ mutations: {},
246
+ dbQueries: 0
247
+ };
248
+ logger.trace('entering transaction');
249
+ try {
250
+ const result = await txStorage.run(ctx, work);
251
+ // Commit mutations
252
+ await commitWithRetry(ctx.mutations);
253
+ logger.trace({ dbQueries: ctx.dbQueries }, 'transaction completed');
254
+ return result;
255
+ }
256
+ catch (error) {
257
+ logger.error({ error }, 'transaction failed, rolling back');
258
+ throw error;
259
+ }
260
+ });
261
+ }
262
+ finally {
263
+ releaseTxMutexRef(key);
437
264
  }
438
- // Apply changes to the store
439
- await state.set(data);
440
- });
441
- }
442
- }
443
- },
444
- isInTransaction,
445
- async transaction(work, key) {
446
- const releaseTxMutex = await getTransactionMutex(key).acquire();
447
- try {
448
- transactionsInProgress += 1;
449
- if (transactionsInProgress === 1) {
450
- logger.trace('entering transaction');
451
- }
452
- // Release the transaction mutex now that we've updated the counter
453
- // This allows other transactions to start preparing
454
- releaseTxMutex();
455
- try {
456
- return await executeTransactionWork(work);
457
- } finally {
458
- cleanupTransactionState();
459
265
  }
460
- } catch (error) {
461
- releaseTxMutex();
462
- throw error;
463
- }
266
+ };
267
+ };
268
+ /**
269
+ * Returns the authenticated user's JID, or throws a Boom-401 if creds are not yet authenticated.
270
+ * Use this anywhere we'd otherwise reach for `creds.me!.id` to fail fast with a descriptive error.
271
+ */
272
+ export const assertMeId = (creds) => {
273
+ const id = creds.me?.id;
274
+ if (!id) {
275
+ throw new Boom('Cannot proceed: socket is not authenticated yet (creds.me.id is missing)', { statusCode: 401 });
464
276
  }
465
- };
277
+ return id;
466
278
  };
467
- exports.addTransactionCapability = addTransactionCapability;
468
- const initAuthCreds = () => {
469
- const identityKey = _crypto2.Curve.generateKeyPair();
470
- return {
471
- noiseKey: _crypto2.Curve.generateKeyPair(),
472
- pairingEphemeralKeyPair: _crypto2.Curve.generateKeyPair(),
473
- signedIdentityKey: identityKey,
474
- signedPreKey: (0, _crypto2.signedKeyPair)(identityKey, 1),
475
- registrationId: (0, _generics.generateRegistrationId)(),
476
- advSecretKey: (0, _crypto.randomBytes)(32).toString('base64'),
477
- processedHistoryMessages: [],
478
- nextPreKeyId: 1,
479
- firstUnuploadedPreKeyId: 1,
480
- accountSyncCounter: 0,
481
- accountSettings: {
482
- unarchiveChats: false
483
- },
484
- registered: false,
485
- pairingCode: undefined,
486
- lastPropHash: undefined,
487
- routingInfo: undefined
488
- };
279
+ export const initAuthCreds = () => {
280
+ const identityKey = Curve.generateKeyPair();
281
+ return {
282
+ noiseKey: Curve.generateKeyPair(),
283
+ pairingEphemeralKeyPair: Curve.generateKeyPair(),
284
+ signedIdentityKey: identityKey,
285
+ signedPreKey: signedKeyPair(identityKey, 1),
286
+ registrationId: generateRegistrationId(),
287
+ advSecretKey: randomBytes(32).toString('base64'),
288
+ processedHistoryMessages: [],
289
+ nextPreKeyId: 1,
290
+ firstUnuploadedPreKeyId: 1,
291
+ accountSyncCounter: 0,
292
+ accountSettings: {
293
+ unarchiveChats: false
294
+ },
295
+ registered: false,
296
+ pairingCode: undefined,
297
+ lastPropHash: undefined,
298
+ routingInfo: undefined,
299
+ additionalData: undefined
300
+ };
489
301
  };
490
- //# sourceMappingURL=auth-utils.js.map
491
- exports.initAuthCreds = initAuthCreds;
302
+ //# sourceMappingURL=auth-utils.js.map