@eduh_dev021/baileys 1.0.0 → 1.1.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 (526) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +1526 -178
  3. package/WAProto/index.d.ts +1 -1
  4. package/WAProto/index.js +1 -1
  5. package/cjs/WAProto/index.d.ts +1 -1
  6. package/cjs/WAProto/index.js +0 -0
  7. package/cjs/engine-requirements.js +0 -0
  8. package/cjs/lib/Addon/cache.js +179 -0
  9. package/cjs/lib/Addon/crc32.js +41 -0
  10. package/cjs/lib/Addon/file-type.js +217 -0
  11. package/cjs/lib/Addon/get-file.js +99 -0
  12. package/cjs/lib/Addon/http-error.d.ts +0 -0
  13. package/cjs/lib/Addon/http-error.js +0 -0
  14. package/cjs/lib/Addon/index.d.ts +93 -0
  15. package/cjs/lib/Addon/index.js +18 -10
  16. package/cjs/lib/Addon/keyed-db.d.ts +0 -0
  17. package/cjs/lib/Addon/keyed-db.js +0 -0
  18. package/cjs/lib/Addon/link-preview.d.ts +0 -0
  19. package/cjs/lib/Addon/link-preview.js +0 -0
  20. package/cjs/lib/Addon/media-info.js +98 -0
  21. package/cjs/lib/Addon/mutex.js +78 -0
  22. package/cjs/lib/Addon/qr.js +464 -0
  23. package/cjs/lib/Addon/queue.js +54 -0
  24. package/cjs/lib/Addon/waveform.js +130 -0
  25. package/cjs/lib/Addon/zip.js +136 -0
  26. package/cjs/lib/Defaults/index.d.ts +2 -2
  27. package/cjs/lib/Defaults/index.js +2 -2
  28. package/cjs/lib/Signal/Group/ciphertext-message.d.ts +0 -0
  29. package/cjs/lib/Signal/Group/ciphertext-message.js +0 -0
  30. package/cjs/lib/Signal/Group/group-cipher.d.ts +0 -0
  31. package/cjs/lib/Signal/Group/group-cipher.js +0 -0
  32. package/cjs/lib/Signal/Group/group-session-builder.d.ts +0 -0
  33. package/cjs/lib/Signal/Group/group-session-builder.js +0 -0
  34. package/cjs/lib/Signal/Group/index.d.ts +0 -0
  35. package/cjs/lib/Signal/Group/index.js +0 -0
  36. package/cjs/lib/Signal/Group/keyhelper.d.ts +0 -0
  37. package/cjs/lib/Signal/Group/keyhelper.js +0 -0
  38. package/cjs/lib/Signal/Group/sender-chain-key.d.ts +0 -0
  39. package/cjs/lib/Signal/Group/sender-chain-key.js +0 -0
  40. package/cjs/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -0
  41. package/cjs/lib/Signal/Group/sender-key-distribution-message.js +0 -0
  42. package/cjs/lib/Signal/Group/sender-key-message.d.ts +0 -0
  43. package/cjs/lib/Signal/Group/sender-key-message.js +0 -0
  44. package/cjs/lib/Signal/Group/sender-key-name.d.ts +0 -0
  45. package/cjs/lib/Signal/Group/sender-key-name.js +0 -0
  46. package/cjs/lib/Signal/Group/sender-key-record.d.ts +0 -0
  47. package/cjs/lib/Signal/Group/sender-key-record.js +0 -0
  48. package/cjs/lib/Signal/Group/sender-key-state.d.ts +0 -0
  49. package/cjs/lib/Signal/Group/sender-key-state.js +0 -0
  50. package/cjs/lib/Signal/Group/sender-message-key.d.ts +0 -0
  51. package/cjs/lib/Signal/Group/sender-message-key.js +0 -0
  52. package/cjs/lib/Signal/libsignal.d.ts +0 -0
  53. package/cjs/lib/Signal/libsignal.js +0 -0
  54. package/cjs/lib/Signal/lid-mapping.d.ts +0 -0
  55. package/cjs/lib/Signal/lid-mapping.js +0 -0
  56. package/cjs/lib/Socket/Client/index.d.ts +0 -0
  57. package/cjs/lib/Socket/Client/index.js +0 -0
  58. package/cjs/lib/Socket/Client/types.d.ts +0 -0
  59. package/cjs/lib/Socket/Client/types.js +0 -0
  60. package/cjs/lib/Socket/Client/websocket.d.ts +0 -0
  61. package/cjs/lib/Socket/Client/websocket.js +0 -0
  62. package/cjs/lib/Socket/business.d.ts +0 -0
  63. package/cjs/lib/Socket/business.js +0 -0
  64. package/cjs/lib/Socket/chats.d.ts +0 -0
  65. package/cjs/lib/Socket/chats.js +0 -0
  66. package/cjs/lib/Socket/communities.d.ts +0 -0
  67. package/cjs/lib/Socket/communities.js +0 -0
  68. package/cjs/lib/Socket/groups.d.ts +0 -0
  69. package/cjs/lib/Socket/groups.js +0 -0
  70. package/cjs/lib/Socket/index.d.ts +0 -0
  71. package/cjs/lib/Socket/index.js +0 -0
  72. package/cjs/lib/Socket/messages-recv.d.ts +0 -0
  73. package/cjs/lib/Socket/messages-recv.js +0 -0
  74. package/cjs/lib/Socket/messages-send.d.ts +0 -0
  75. package/cjs/lib/Socket/messages-send.js +7 -7
  76. package/cjs/lib/Socket/mex.d.ts +0 -0
  77. package/cjs/lib/Socket/mex.js +0 -0
  78. package/cjs/lib/Socket/newsletter.d.ts +0 -0
  79. package/cjs/lib/Socket/newsletter.js +1 -1
  80. package/cjs/lib/Socket/socket.d.ts +0 -0
  81. package/cjs/lib/Socket/socket.js +0 -0
  82. package/cjs/lib/Store/index.d.ts +0 -0
  83. package/cjs/lib/Store/index.js +0 -0
  84. package/cjs/lib/Store/make-in-memory-store.d.ts +0 -0
  85. package/cjs/lib/Store/make-in-memory-store.js +0 -0
  86. package/cjs/lib/Store/make-ordered-dictionary.d.ts +0 -0
  87. package/cjs/lib/Store/make-ordered-dictionary.js +0 -0
  88. package/cjs/lib/Store/object-repository.d.ts +0 -0
  89. package/cjs/lib/Store/object-repository.js +0 -0
  90. package/cjs/lib/Transport/crypto.d.ts +0 -0
  91. package/cjs/lib/Transport/crypto.js +0 -0
  92. package/cjs/lib/Transport/curve.d.ts +0 -0
  93. package/cjs/lib/Transport/curve.js +0 -0
  94. package/cjs/lib/Transport/index.d.ts +0 -0
  95. package/cjs/lib/Transport/index.js +0 -0
  96. package/cjs/lib/Transport/internals.d.ts +0 -0
  97. package/cjs/lib/Transport/internals.js +0 -0
  98. package/cjs/lib/Transport/session-builder.d.ts +0 -0
  99. package/cjs/lib/Transport/session-builder.js +0 -0
  100. package/cjs/lib/Transport/session-cipher.d.ts +0 -0
  101. package/cjs/lib/Transport/session-cipher.js +0 -0
  102. package/cjs/lib/Transport/session-record.d.ts +0 -0
  103. package/cjs/lib/Transport/session-record.js +0 -0
  104. package/cjs/lib/Types/Auth.d.ts +0 -0
  105. package/cjs/lib/Types/Auth.js +0 -0
  106. package/cjs/lib/Types/Bussines.d.ts +0 -0
  107. package/cjs/lib/Types/Bussines.js +0 -0
  108. package/cjs/lib/Types/Call.d.ts +0 -0
  109. package/cjs/lib/Types/Call.js +0 -0
  110. package/cjs/lib/Types/Chat.d.ts +0 -0
  111. package/cjs/lib/Types/Chat.js +0 -0
  112. package/cjs/lib/Types/Contact.d.ts +0 -0
  113. package/cjs/lib/Types/Contact.js +0 -0
  114. package/cjs/lib/Types/Events.d.ts +0 -0
  115. package/cjs/lib/Types/Events.js +0 -0
  116. package/cjs/lib/Types/GroupMetadata.d.ts +0 -0
  117. package/cjs/lib/Types/GroupMetadata.js +0 -0
  118. package/cjs/lib/Types/Label.d.ts +0 -0
  119. package/cjs/lib/Types/Label.js +0 -0
  120. package/cjs/lib/Types/LabelAssociation.d.ts +0 -0
  121. package/cjs/lib/Types/LabelAssociation.js +0 -0
  122. package/cjs/lib/Types/Message.d.ts +0 -0
  123. package/cjs/lib/Types/Message.js +0 -0
  124. package/cjs/lib/Types/Mex.d.ts +0 -0
  125. package/cjs/lib/Types/Mex.js +0 -0
  126. package/cjs/lib/Types/Product.d.ts +0 -0
  127. package/cjs/lib/Types/Product.js +0 -0
  128. package/cjs/lib/Types/RichType.d.ts +0 -0
  129. package/cjs/lib/Types/RichType.js +0 -0
  130. package/cjs/lib/Types/Signal.d.ts +0 -0
  131. package/cjs/lib/Types/Signal.js +0 -0
  132. package/cjs/lib/Types/Socket.d.ts +0 -0
  133. package/cjs/lib/Types/Socket.js +0 -0
  134. package/cjs/lib/Types/State.d.ts +0 -0
  135. package/cjs/lib/Types/State.js +0 -0
  136. package/cjs/lib/Types/USync.d.ts +0 -0
  137. package/cjs/lib/Types/USync.js +0 -0
  138. package/cjs/lib/Types/index.d.ts +0 -0
  139. package/cjs/lib/Types/index.js +0 -0
  140. package/cjs/lib/Utils/auth-utils.d.ts +0 -0
  141. package/cjs/lib/Utils/auth-utils.js +0 -0
  142. package/cjs/lib/Utils/browser-utils.d.ts +0 -0
  143. package/cjs/lib/Utils/browser-utils.js +0 -0
  144. package/cjs/lib/Utils/business.d.ts +0 -0
  145. package/cjs/lib/Utils/business.js +0 -0
  146. package/cjs/lib/Utils/chat-utils.d.ts +0 -0
  147. package/cjs/lib/Utils/chat-utils.js +0 -0
  148. package/cjs/lib/Utils/companion-reg-client-utils.d.ts +0 -0
  149. package/cjs/lib/Utils/companion-reg-client-utils.js +0 -0
  150. package/cjs/lib/Utils/crypto.d.ts +0 -0
  151. package/cjs/lib/Utils/crypto.js +0 -0
  152. package/cjs/lib/Utils/decode-wa-message.d.ts +0 -0
  153. package/cjs/lib/Utils/decode-wa-message.js +0 -0
  154. package/cjs/lib/Utils/event-buffer.d.ts +0 -0
  155. package/cjs/lib/Utils/event-buffer.js +0 -0
  156. package/cjs/lib/Utils/generics.d.ts +0 -0
  157. package/cjs/lib/Utils/generics.js +1 -1
  158. package/cjs/lib/Utils/history.d.ts +0 -0
  159. package/cjs/lib/Utils/history.js +0 -0
  160. package/cjs/lib/Utils/identity-change-handler.d.ts +0 -0
  161. package/cjs/lib/Utils/identity-change-handler.js +0 -0
  162. package/cjs/lib/Utils/index.d.ts +0 -0
  163. package/cjs/lib/Utils/index.js +0 -0
  164. package/cjs/lib/Utils/link-preview.d.ts +0 -0
  165. package/cjs/lib/Utils/link-preview.js +0 -0
  166. package/cjs/lib/Utils/logger.d.ts +0 -0
  167. package/cjs/lib/Utils/logger.js +0 -0
  168. package/cjs/lib/Utils/lt-hash.d.ts +0 -0
  169. package/cjs/lib/Utils/lt-hash.js +0 -0
  170. package/cjs/lib/Utils/make-mutex.d.ts +0 -0
  171. package/cjs/lib/Utils/make-mutex.js +0 -0
  172. package/cjs/lib/Utils/message-retry-manager.d.ts +0 -0
  173. package/cjs/lib/Utils/message-retry-manager.js +0 -0
  174. package/cjs/lib/Utils/messages-media.d.ts +0 -0
  175. package/cjs/lib/Utils/messages-media.js +0 -0
  176. package/cjs/lib/Utils/messages.d.ts +0 -0
  177. package/cjs/lib/Utils/messages.js +11 -11
  178. package/cjs/lib/Utils/noise-handler.d.ts +0 -0
  179. package/cjs/lib/Utils/noise-handler.js +0 -0
  180. package/cjs/lib/Utils/offline-node-processor.d.ts +0 -0
  181. package/cjs/lib/Utils/offline-node-processor.js +0 -0
  182. package/cjs/lib/Utils/pre-key-manager.d.ts +0 -0
  183. package/cjs/lib/Utils/pre-key-manager.js +0 -0
  184. package/cjs/lib/Utils/process-message.d.ts +0 -0
  185. package/cjs/lib/Utils/process-message.js +0 -0
  186. package/cjs/lib/Utils/reporting-utils.d.ts +0 -0
  187. package/cjs/lib/Utils/reporting-utils.js +0 -0
  188. package/cjs/lib/Utils/rich-message-utils.d.ts +0 -0
  189. package/cjs/lib/Utils/rich-message-utils.js +6 -6
  190. package/cjs/lib/Utils/signal.d.ts +0 -0
  191. package/cjs/lib/Utils/signal.js +0 -0
  192. package/cjs/lib/Utils/stanza-ack.d.ts +0 -0
  193. package/cjs/lib/Utils/stanza-ack.js +0 -0
  194. package/cjs/lib/Utils/sync-action-utils.d.ts +0 -0
  195. package/cjs/lib/Utils/sync-action-utils.js +0 -0
  196. package/cjs/lib/Utils/tc-token-utils.d.ts +0 -0
  197. package/cjs/lib/Utils/tc-token-utils.js +0 -0
  198. package/cjs/lib/Utils/use-multi-file-auth-state.d.ts +0 -0
  199. package/cjs/lib/Utils/use-multi-file-auth-state.js +0 -0
  200. package/cjs/lib/Utils/use-single-file-auth-state.d.ts +0 -0
  201. package/cjs/lib/Utils/use-single-file-auth-state.js +0 -0
  202. package/cjs/lib/Utils/use-sqlite-auth-state.d.ts +0 -0
  203. package/cjs/lib/Utils/use-sqlite-auth-state.js +0 -0
  204. package/cjs/lib/Utils/validate-connection.d.ts +0 -0
  205. package/cjs/lib/Utils/validate-connection.js +0 -0
  206. package/cjs/lib/WABinary/constants.d.ts +0 -0
  207. package/cjs/lib/WABinary/constants.js +0 -0
  208. package/cjs/lib/WABinary/decode.d.ts +0 -0
  209. package/cjs/lib/WABinary/decode.js +0 -0
  210. package/cjs/lib/WABinary/encode.d.ts +0 -0
  211. package/cjs/lib/WABinary/encode.js +0 -0
  212. package/cjs/lib/WABinary/generic-utils.d.ts +0 -0
  213. package/cjs/lib/WABinary/generic-utils.js +0 -0
  214. package/cjs/lib/WABinary/index.d.ts +0 -0
  215. package/cjs/lib/WABinary/index.js +0 -0
  216. package/cjs/lib/WABinary/jid-utils.d.ts +0 -0
  217. package/cjs/lib/WABinary/jid-utils.js +0 -0
  218. package/cjs/lib/WABinary/types.d.ts +0 -0
  219. package/cjs/lib/WABinary/types.js +0 -0
  220. package/cjs/lib/WACrypto/appstate-keys.js +0 -0
  221. package/cjs/lib/WACrypto/curves/constants.js +0 -0
  222. package/cjs/lib/WACrypto/hkdf.js +0 -0
  223. package/cjs/lib/WACrypto/index.js +0 -0
  224. package/cjs/lib/WACrypto/lthash.js +0 -0
  225. package/cjs/lib/WACrypto/math/constants.js +0 -0
  226. package/cjs/lib/WACrypto/math/edwards.js +0 -0
  227. package/cjs/lib/WACrypto/math/fe.js +0 -0
  228. package/cjs/lib/WACrypto/math/le.js +0 -0
  229. package/cjs/lib/WACrypto/math/mod.js +0 -0
  230. package/cjs/lib/WACrypto/util/bytes.js +0 -0
  231. package/cjs/lib/WACrypto/x25519.js +0 -0
  232. package/cjs/lib/WACrypto/xeddsa.js +0 -0
  233. package/cjs/lib/WAM/BinaryInfo.d.ts +0 -0
  234. package/cjs/lib/WAM/BinaryInfo.js +0 -0
  235. package/cjs/lib/WAM/constants.d.ts +0 -0
  236. package/cjs/lib/WAM/constants.js +0 -0
  237. package/cjs/lib/WAM/encode.d.ts +0 -0
  238. package/cjs/lib/WAM/encode.js +0 -0
  239. package/cjs/lib/WAM/index.d.ts +0 -0
  240. package/cjs/lib/WAM/index.js +0 -0
  241. package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -0
  242. package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -0
  243. package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -0
  244. package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -0
  245. package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -0
  246. package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -0
  247. package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -0
  248. package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -0
  249. package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -0
  250. package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -0
  251. package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -0
  252. package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -0
  253. package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -0
  254. package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -0
  255. package/cjs/lib/WAUSync/Protocols/index.d.ts +0 -0
  256. package/cjs/lib/WAUSync/Protocols/index.js +0 -0
  257. package/cjs/lib/WAUSync/USyncQuery.d.ts +0 -0
  258. package/cjs/lib/WAUSync/USyncQuery.js +0 -0
  259. package/cjs/lib/WAUSync/USyncUser.d.ts +0 -0
  260. package/cjs/lib/WAUSync/USyncUser.js +0 -0
  261. package/cjs/lib/WAUSync/index.d.ts +0 -0
  262. package/cjs/lib/WAUSync/index.js +0 -0
  263. package/cjs/lib/index.d.ts +0 -0
  264. package/cjs/lib/index.js +0 -0
  265. package/cjs/package.json +0 -0
  266. package/engine-requirements.js +0 -0
  267. package/lib/Addon/cache.js +201 -0
  268. package/lib/Addon/crc32.js +21 -0
  269. package/lib/Addon/file-type.js +198 -0
  270. package/lib/Addon/get-file.js +98 -0
  271. package/lib/Addon/http-error.d.ts +0 -0
  272. package/lib/Addon/http-error.js +0 -0
  273. package/lib/Addon/index.d.ts +93 -0
  274. package/lib/Addon/index.js +23 -1
  275. package/lib/Addon/keyed-db.d.ts +0 -0
  276. package/lib/Addon/keyed-db.js +0 -0
  277. package/lib/Addon/link-preview.d.ts +0 -0
  278. package/lib/Addon/link-preview.js +0 -0
  279. package/lib/Addon/media-info.js +96 -0
  280. package/lib/Addon/mutex.js +76 -0
  281. package/lib/Addon/qr.js +531 -0
  282. package/lib/Addon/queue.js +44 -0
  283. package/lib/Addon/waveform.js +135 -0
  284. package/lib/Addon/zip.js +144 -0
  285. package/lib/Defaults/index.d.ts +2 -2
  286. package/lib/Defaults/index.js +3 -3
  287. package/lib/Signal/Group/ciphertext-message.d.ts +0 -0
  288. package/lib/Signal/Group/ciphertext-message.js +0 -0
  289. package/lib/Signal/Group/group-cipher.d.ts +0 -0
  290. package/lib/Signal/Group/group-cipher.js +0 -0
  291. package/lib/Signal/Group/group-session-builder.d.ts +0 -0
  292. package/lib/Signal/Group/group-session-builder.js +0 -0
  293. package/lib/Signal/Group/index.d.ts +0 -0
  294. package/lib/Signal/Group/index.js +0 -0
  295. package/lib/Signal/Group/keyhelper.d.ts +0 -0
  296. package/lib/Signal/Group/keyhelper.js +0 -0
  297. package/lib/Signal/Group/sender-chain-key.d.ts +0 -0
  298. package/lib/Signal/Group/sender-chain-key.js +0 -0
  299. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -0
  300. package/lib/Signal/Group/sender-key-distribution-message.js +0 -0
  301. package/lib/Signal/Group/sender-key-message.d.ts +0 -0
  302. package/lib/Signal/Group/sender-key-message.js +0 -0
  303. package/lib/Signal/Group/sender-key-name.d.ts +0 -0
  304. package/lib/Signal/Group/sender-key-name.js +0 -0
  305. package/lib/Signal/Group/sender-key-record.d.ts +0 -0
  306. package/lib/Signal/Group/sender-key-record.js +0 -0
  307. package/lib/Signal/Group/sender-key-state.d.ts +0 -0
  308. package/lib/Signal/Group/sender-key-state.js +0 -0
  309. package/lib/Signal/Group/sender-message-key.d.ts +0 -0
  310. package/lib/Signal/Group/sender-message-key.js +0 -0
  311. package/lib/Signal/libsignal.d.ts +0 -0
  312. package/lib/Signal/libsignal.js +0 -0
  313. package/lib/Signal/lid-mapping.d.ts +0 -0
  314. package/lib/Signal/lid-mapping.js +0 -0
  315. package/lib/Socket/Client/index.d.ts +0 -0
  316. package/lib/Socket/Client/index.js +0 -0
  317. package/lib/Socket/Client/types.d.ts +0 -0
  318. package/lib/Socket/Client/types.js +0 -0
  319. package/lib/Socket/Client/websocket.d.ts +0 -0
  320. package/lib/Socket/Client/websocket.js +0 -0
  321. package/lib/Socket/business.d.ts +0 -0
  322. package/lib/Socket/business.js +0 -0
  323. package/lib/Socket/chats.d.ts +0 -0
  324. package/lib/Socket/chats.js +1 -1
  325. package/lib/Socket/communities.d.ts +0 -0
  326. package/lib/Socket/communities.js +0 -0
  327. package/lib/Socket/groups.d.ts +0 -0
  328. package/lib/Socket/groups.js +0 -0
  329. package/lib/Socket/index.d.ts +0 -0
  330. package/lib/Socket/index.js +0 -0
  331. package/lib/Socket/messages-recv.d.ts +0 -0
  332. package/lib/Socket/messages-recv.js +2 -2
  333. package/lib/Socket/messages-send.d.ts +0 -0
  334. package/lib/Socket/messages-send.js +19 -19
  335. package/lib/Socket/mex.d.ts +0 -0
  336. package/lib/Socket/mex.js +0 -0
  337. package/lib/Socket/newsletter.d.ts +0 -0
  338. package/lib/Socket/newsletter.js +1 -1
  339. package/lib/Socket/socket.d.ts +0 -0
  340. package/lib/Socket/socket.js +0 -0
  341. package/lib/Store/index.d.ts +0 -0
  342. package/lib/Store/index.js +0 -0
  343. package/lib/Store/make-in-memory-store.d.ts +0 -0
  344. package/lib/Store/make-in-memory-store.js +1 -1
  345. package/lib/Store/make-ordered-dictionary.d.ts +0 -0
  346. package/lib/Store/make-ordered-dictionary.js +0 -0
  347. package/lib/Store/object-repository.d.ts +0 -0
  348. package/lib/Store/object-repository.js +0 -0
  349. package/lib/Transport/crypto.d.ts +0 -0
  350. package/lib/Transport/crypto.js +0 -0
  351. package/lib/Transport/curve.d.ts +0 -0
  352. package/lib/Transport/curve.js +0 -0
  353. package/lib/Transport/index.d.ts +0 -0
  354. package/lib/Transport/index.js +0 -0
  355. package/lib/Transport/internals.d.ts +0 -0
  356. package/lib/Transport/internals.js +0 -0
  357. package/lib/Transport/session-builder.d.ts +0 -0
  358. package/lib/Transport/session-builder.js +0 -0
  359. package/lib/Transport/session-cipher.d.ts +0 -0
  360. package/lib/Transport/session-cipher.js +0 -0
  361. package/lib/Transport/session-record.d.ts +0 -0
  362. package/lib/Transport/session-record.js +0 -0
  363. package/lib/Types/Auth.d.ts +0 -0
  364. package/lib/Types/Auth.js +0 -0
  365. package/lib/Types/Bussines.d.ts +0 -0
  366. package/lib/Types/Bussines.js +0 -0
  367. package/lib/Types/Call.d.ts +0 -0
  368. package/lib/Types/Call.js +0 -0
  369. package/lib/Types/Chat.d.ts +0 -0
  370. package/lib/Types/Chat.js +0 -0
  371. package/lib/Types/Contact.d.ts +0 -0
  372. package/lib/Types/Contact.js +0 -0
  373. package/lib/Types/Events.d.ts +0 -0
  374. package/lib/Types/Events.js +0 -0
  375. package/lib/Types/GroupMetadata.d.ts +0 -0
  376. package/lib/Types/GroupMetadata.js +0 -0
  377. package/lib/Types/Label.d.ts +0 -0
  378. package/lib/Types/Label.js +0 -0
  379. package/lib/Types/LabelAssociation.d.ts +0 -0
  380. package/lib/Types/LabelAssociation.js +0 -0
  381. package/lib/Types/Message.d.ts +0 -0
  382. package/lib/Types/Message.js +1 -1
  383. package/lib/Types/Mex.d.ts +0 -0
  384. package/lib/Types/Mex.js +0 -0
  385. package/lib/Types/Product.d.ts +0 -0
  386. package/lib/Types/Product.js +0 -0
  387. package/lib/Types/RichType.d.ts +0 -0
  388. package/lib/Types/RichType.js +0 -0
  389. package/lib/Types/Signal.d.ts +0 -0
  390. package/lib/Types/Signal.js +0 -0
  391. package/lib/Types/Socket.d.ts +0 -0
  392. package/lib/Types/Socket.js +0 -0
  393. package/lib/Types/State.d.ts +0 -0
  394. package/lib/Types/State.js +0 -0
  395. package/lib/Types/USync.d.ts +0 -0
  396. package/lib/Types/USync.js +0 -0
  397. package/lib/Types/index.d.ts +0 -0
  398. package/lib/Types/index.js +0 -0
  399. package/lib/Utils/auth-utils.d.ts +0 -0
  400. package/lib/Utils/auth-utils.js +0 -0
  401. package/lib/Utils/browser-utils.d.ts +0 -0
  402. package/lib/Utils/browser-utils.js +0 -0
  403. package/lib/Utils/business.d.ts +0 -0
  404. package/lib/Utils/business.js +0 -0
  405. package/lib/Utils/chat-utils.d.ts +0 -0
  406. package/lib/Utils/chat-utils.js +0 -0
  407. package/lib/Utils/companion-reg-client-utils.d.ts +0 -0
  408. package/lib/Utils/companion-reg-client-utils.js +0 -0
  409. package/lib/Utils/crypto.d.ts +0 -0
  410. package/lib/Utils/crypto.js +0 -0
  411. package/lib/Utils/decode-wa-message.d.ts +0 -0
  412. package/lib/Utils/decode-wa-message.js +0 -0
  413. package/lib/Utils/event-buffer.d.ts +0 -0
  414. package/lib/Utils/event-buffer.js +0 -0
  415. package/lib/Utils/generics.d.ts +0 -0
  416. package/lib/Utils/generics.js +1 -1
  417. package/lib/Utils/history.d.ts +0 -0
  418. package/lib/Utils/history.js +0 -0
  419. package/lib/Utils/identity-change-handler.d.ts +0 -0
  420. package/lib/Utils/identity-change-handler.js +0 -0
  421. package/lib/Utils/index.d.ts +0 -0
  422. package/lib/Utils/index.js +0 -0
  423. package/lib/Utils/link-preview.d.ts +0 -0
  424. package/lib/Utils/link-preview.js +0 -0
  425. package/lib/Utils/logger.d.ts +0 -0
  426. package/lib/Utils/logger.js +0 -0
  427. package/lib/Utils/lt-hash.d.ts +0 -0
  428. package/lib/Utils/lt-hash.js +0 -0
  429. package/lib/Utils/make-mutex.d.ts +0 -0
  430. package/lib/Utils/make-mutex.js +0 -0
  431. package/lib/Utils/message-retry-manager.d.ts +0 -0
  432. package/lib/Utils/message-retry-manager.js +0 -0
  433. package/lib/Utils/messages-media.d.ts +0 -0
  434. package/lib/Utils/messages-media.js +2 -2
  435. package/lib/Utils/messages.d.ts +0 -0
  436. package/lib/Utils/messages.js +46 -50
  437. package/lib/Utils/noise-handler.d.ts +0 -0
  438. package/lib/Utils/noise-handler.js +0 -0
  439. package/lib/Utils/offline-node-processor.d.ts +0 -0
  440. package/lib/Utils/offline-node-processor.js +0 -0
  441. package/lib/Utils/pre-key-manager.d.ts +0 -0
  442. package/lib/Utils/pre-key-manager.js +0 -0
  443. package/lib/Utils/process-message.d.ts +0 -0
  444. package/lib/Utils/process-message.js +0 -0
  445. package/lib/Utils/reporting-utils.d.ts +0 -0
  446. package/lib/Utils/reporting-utils.js +0 -0
  447. package/lib/Utils/rich-message-utils.d.ts +0 -0
  448. package/lib/Utils/rich-message-utils.js +10 -13
  449. package/lib/Utils/signal.d.ts +0 -0
  450. package/lib/Utils/signal.js +0 -0
  451. package/lib/Utils/stanza-ack.d.ts +0 -0
  452. package/lib/Utils/stanza-ack.js +0 -0
  453. package/lib/Utils/sync-action-utils.d.ts +0 -0
  454. package/lib/Utils/sync-action-utils.js +0 -0
  455. package/lib/Utils/tc-token-utils.d.ts +0 -0
  456. package/lib/Utils/tc-token-utils.js +0 -0
  457. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -0
  458. package/lib/Utils/use-multi-file-auth-state.js +1 -1
  459. package/lib/Utils/use-single-file-auth-state.d.ts +0 -0
  460. package/lib/Utils/use-single-file-auth-state.js +1 -1
  461. package/lib/Utils/use-sqlite-auth-state.d.ts +0 -0
  462. package/lib/Utils/use-sqlite-auth-state.js +1 -1
  463. package/lib/Utils/validate-connection.d.ts +0 -0
  464. package/lib/Utils/validate-connection.js +0 -0
  465. package/lib/WABinary/constants.d.ts +0 -0
  466. package/lib/WABinary/constants.js +0 -0
  467. package/lib/WABinary/decode.d.ts +0 -0
  468. package/lib/WABinary/decode.js +0 -0
  469. package/lib/WABinary/encode.d.ts +0 -0
  470. package/lib/WABinary/encode.js +0 -0
  471. package/lib/WABinary/generic-utils.d.ts +0 -0
  472. package/lib/WABinary/generic-utils.js +0 -2
  473. package/lib/WABinary/index.d.ts +0 -0
  474. package/lib/WABinary/index.js +0 -0
  475. package/lib/WABinary/jid-utils.d.ts +0 -0
  476. package/lib/WABinary/jid-utils.js +0 -0
  477. package/lib/WABinary/types.d.ts +0 -0
  478. package/lib/WABinary/types.js +0 -0
  479. package/lib/WACrypto/appstate-keys.js +0 -0
  480. package/lib/WACrypto/curves/constants.js +0 -0
  481. package/lib/WACrypto/hkdf.js +0 -0
  482. package/lib/WACrypto/index.js +0 -0
  483. package/lib/WACrypto/lthash.js +1 -1
  484. package/lib/WACrypto/math/constants.js +0 -0
  485. package/lib/WACrypto/math/edwards.js +0 -0
  486. package/lib/WACrypto/math/fe.js +0 -0
  487. package/lib/WACrypto/math/le.js +0 -0
  488. package/lib/WACrypto/math/mod.js +0 -0
  489. package/lib/WACrypto/util/bytes.js +0 -0
  490. package/lib/WACrypto/x25519.js +3 -3
  491. package/lib/WACrypto/xeddsa.js +0 -0
  492. package/lib/WAM/BinaryInfo.d.ts +0 -0
  493. package/lib/WAM/BinaryInfo.js +0 -0
  494. package/lib/WAM/constants.d.ts +0 -0
  495. package/lib/WAM/constants.js +0 -0
  496. package/lib/WAM/encode.d.ts +0 -0
  497. package/lib/WAM/encode.js +0 -0
  498. package/lib/WAM/index.d.ts +0 -0
  499. package/lib/WAM/index.js +0 -0
  500. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -0
  501. package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -0
  502. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -0
  503. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -0
  504. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -0
  505. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -0
  506. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -0
  507. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -0
  508. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -0
  509. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -0
  510. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -0
  511. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -0
  512. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -0
  513. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -0
  514. package/lib/WAUSync/Protocols/index.d.ts +0 -0
  515. package/lib/WAUSync/Protocols/index.js +0 -0
  516. package/lib/WAUSync/USyncQuery.d.ts +0 -0
  517. package/lib/WAUSync/USyncQuery.js +0 -0
  518. package/lib/WAUSync/USyncUser.d.ts +0 -0
  519. package/lib/WAUSync/USyncUser.js +0 -0
  520. package/lib/WAUSync/index.d.ts +0 -0
  521. package/lib/WAUSync/index.js +0 -0
  522. package/lib/index.d.ts +0 -0
  523. package/lib/index.js +0 -0
  524. package/package.json +4 -9
  525. package/tests/addon-test.mjs +162 -0
  526. package/tests/differential-test.mjs +90 -0
@@ -0,0 +1,96 @@
1
+ /**
2
+ * `detectMediaInfo` — light-weight media probing on top of the system
3
+ * `ffprobe` binary (ships with ffmpeg). Returns the dimensions and/or
4
+ * duration of an image, audio or video file:
5
+ *
6
+ * await detectMediaInfo('/tmp/photo.jpg'); // -> { width: 640, height: 480, durationSeconds: 0 }
7
+ * await detectMediaInfo(voiceNoteBuffer); // -> { durationSeconds: 12 }
8
+ * await detectMediaInfo('/tmp/clip.mp4'); // -> { durationSeconds: 9, width: 1280, height: 720 }
9
+ *
10
+ * Buffers are probed through stdin, paths are probed directly. Resolves to
11
+ * `{}` when the container cannot be parsed and throws (with ffprobe's stderr)
12
+ * when the input is not recognizable media at all.
13
+ */
14
+ import { spawn } from 'node:child_process';
15
+
16
+ const PROBE_TIMEOUT_MS = 15_000;
17
+
18
+ function runFfprobe(args, stdinData) {
19
+ return new Promise((resolve, reject) => {
20
+ const child = spawn('ffprobe', [
21
+ '-v', 'quiet',
22
+ '-print_format', 'json',
23
+ '-show_format',
24
+ '-show_streams',
25
+ ...args
26
+ ], { stdio: stdinData ? ['pipe', 'pipe', 'pipe'] : ['ignore', 'pipe', 'pipe'] });
27
+
28
+ let stdout = '';
29
+ let stderr = '';
30
+ const timer = setTimeout(() => {
31
+ child.kill('SIGKILL');
32
+ reject(new Error('ffprobe timed out'));
33
+ }, PROBE_TIMEOUT_MS);
34
+
35
+ child.stdout.on('data', c => (stdout += c));
36
+ child.stderr.on('data', c => (stderr += c));
37
+ child.on('error', err => { clearTimeout(timer); reject(err); });
38
+ child.on('close', code => {
39
+ clearTimeout(timer);
40
+ if (code !== 0) {
41
+ reject(new Error(stderr.trim() || `ffprobe exited with code ${code}`));
42
+ return;
43
+ }
44
+ try {
45
+ resolve(JSON.parse(stdout));
46
+ }
47
+ catch {
48
+ resolve({});
49
+ }
50
+ });
51
+
52
+ if (stdinData) {
53
+ child.stdin.on('error', () => { /* ffprobe may close stdin early — ignore */ });
54
+ child.stdin.end(stdinData);
55
+ }
56
+ });
57
+ }
58
+
59
+ /** Maps an ffprobe JSON report onto the compact info shape. */
60
+ function mapProbeReport(report) {
61
+ const info = {};
62
+ const duration = Number(report?.format?.duration);
63
+ if (Number.isFinite(duration)) {
64
+ info.durationSeconds = Math.round(duration);
65
+ }
66
+ const streams = Array.isArray(report?.streams) ? report.streams : [];
67
+ const video = streams.find(s => (s.width && s.height) || s.codec_type === 'video');
68
+ if (video?.width && video?.height) {
69
+ info.width = video.width;
70
+ info.height = video.height;
71
+ }
72
+ return info;
73
+ }
74
+
75
+ /**
76
+ * Probes a Buffer/Uint8Array or a path and resolves to
77
+ * `{ width?, height?, durationSeconds? }` (fields only appear when reported).
78
+ */
79
+ export async function detectMediaInfo(input) {
80
+ let args;
81
+ let stdinData;
82
+
83
+ if (typeof input === 'string') {
84
+ args = [input];
85
+ }
86
+ else if (Buffer.isBuffer(input) || input instanceof Uint8Array) {
87
+ args = ['-i', 'pipe:0'];
88
+ stdinData = Buffer.from(input.buffer, input.byteOffset, input.byteLength);
89
+ }
90
+ else {
91
+ throw new TypeError('detectMediaInfo: expects a Buffer, Uint8Array or a file path');
92
+ }
93
+
94
+ const report = await runFfprobe(args, stdinData);
95
+ return mapProbeReport(report);
96
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * `createMutex` — an async mutual-exclusion lock, API-compatible with the
3
+ * widely used async-mutex library:
4
+ *
5
+ * const mutex = createMutex();
6
+ * const result = await mutex.runExclusive(async () => { ... }); // serialized
7
+ * if (!mutex.isLocked()) { ... }
8
+ * const release = await mutex.acquire(); // manual acquire/release
9
+ * release();
10
+ * const release2 = mutex.tryLock(); // undefined when already locked
11
+ *
12
+ * Waiters are served strictly in FIFO order. If a task throws or rejects, the
13
+ * lock is released and the error propagates to its caller only — the mutex
14
+ * stays healthy for the next waiter.
15
+ */
16
+ export function createMutex() {
17
+ let locked = false;
18
+ const queue = []; // pending { resolve, reject } — resolve receives a release fn
19
+
20
+ function dispatchNext() {
21
+ const next = queue.shift();
22
+ if (next) {
23
+ next.resolve(release);
24
+ }
25
+ else {
26
+ locked = false;
27
+ }
28
+ }
29
+
30
+ function release() {
31
+ dispatchNext();
32
+ }
33
+
34
+ function acquire() {
35
+ return new Promise((resolve, reject) => {
36
+ if (!locked) {
37
+ locked = true;
38
+ resolve(release);
39
+ }
40
+ else {
41
+ queue.push({ resolve, reject });
42
+ }
43
+ });
44
+ }
45
+
46
+ function tryAcquire() {
47
+ if (locked) return undefined;
48
+ locked = true;
49
+ return release;
50
+ }
51
+
52
+ async function runExclusive(task) {
53
+ const releaseFn = await acquire();
54
+ try {
55
+ return await task();
56
+ }
57
+ finally {
58
+ releaseFn();
59
+ }
60
+ }
61
+
62
+ return {
63
+ /** Alias of acquire(). */
64
+ lock: acquire,
65
+ /** Acquires the lock, resolving to a release function. */
66
+ acquire,
67
+ /** Acquires without waiting: returns a release function, or undefined when locked. */
68
+ tryAcquire,
69
+ /** Alias of tryAcquire(). */
70
+ tryLock: tryAcquire,
71
+ /** Runs `task` while holding the lock; resolves/rejects with the task result. */
72
+ runExclusive,
73
+ /** True while the lock is held. */
74
+ isLocked: () => locked
75
+ };
76
+ }
@@ -0,0 +1,531 @@
1
+ /**
2
+ * `encodeQR` — a fully self-contained QR Code (Model 2) encoder used for the
3
+ * terminal pairing flow. Implements ISO/IEC 18004 directly: byte-mode
4
+ * segments, versions 1-40, all four error-correction levels, Reed-Solomon
5
+ * interleaving, and automatic mask selection by penalty scoring.
6
+ *
7
+ * const { ascii, getPngBuffer } = await encodeQR(pairingData);
8
+ * console.log(ascii); // half-block characters for the terminal
9
+ * const png = await getPngBuffer(); // PNG (grayscale, 8 px/module)
10
+ *
11
+ * Renders a QR with a 2-module quiet zone for the terminal output and a
12
+ * 4-module quiet zone for the PNG (decoders require the larger border).
13
+ *
14
+ * The encoding algorithm follows the QR Code standard; the mask/penalty
15
+ * structure is adapted from Project Nayuki's MIT-licensed qrcodegen reference,
16
+ * and the EC capacity tables are ISO/IEC 18004 spec data.
17
+ */
18
+ import { deflateSync } from 'node:zlib';
19
+ import { crc32 } from './crc32.js';
20
+
21
+ /* -- Tables from ISO/IEC 18004: error-correction capacity per version -- */
22
+
23
+ // ECC codewords per block, indexed [level 0..3][version 0..40] (index 0 unused)
24
+ const ECC_CODEWORDS_PER_BLOCK = [
25
+ // Low
26
+ [-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
27
+ // Medium
28
+ [-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28],
29
+ // Quartile
30
+ [-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30],
31
+ // High
32
+ [-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
33
+ ];
34
+
35
+ // Number of Reed-Solomon blocks per version/level
36
+ const NUM_ERROR_CORRECTION_BLOCKS = [
37
+ [-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25],
38
+ [-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49],
39
+ [-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68],
40
+ [-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81]
41
+ ];
42
+
43
+ const ECC_FORMAT_BITS = { L: 1, M: 0, Q: 3, H: 2 }; // level -> 2-bit format indicator
44
+ const PENALTY_N1 = 3, PENALTY_N2 = 3, PENALTY_N3 = 40, PENALTY_N4 = 10;
45
+
46
+ /* -- Reed-Solomon over GF(2^8 / 0x11D) -- */
47
+
48
+ function rsDivisor(degree) {
49
+ const result = new Uint8Array(degree);
50
+ result[degree - 1] = 1;
51
+ let root = 1;
52
+ for (let i = 0; i < degree; i++) {
53
+ for (let j = 0; j < degree; j++) {
54
+ // Russian-peasant multiplication by root
55
+ let z = 0;
56
+ for (let bit = 7; bit >= 0; bit--) {
57
+ z = (z << 1) ^ ((z >>> 7) * 0x11D);
58
+ z ^= ((root >>> bit) & 1) * result[j];
59
+ }
60
+ result[j] = z & 0xFF;
61
+ if (j + 1 < degree) result[j] ^= result[j + 1];
62
+ }
63
+ root = rsMultiply(root, 0x02);
64
+ }
65
+ return result;
66
+ }
67
+
68
+ function rsMultiply(x, y) {
69
+ let z = 0;
70
+ for (let i = 7; i >= 0; i--) {
71
+ z = (z << 1) ^ ((z >>> 7) * 0x11D);
72
+ z ^= ((y >>> i) & 1) * x;
73
+ }
74
+ return z & 0xFF;
75
+ }
76
+
77
+ function rsRemainder(data, divisor) {
78
+ const result = new Uint8Array(divisor.length);
79
+ for (const b of data) {
80
+ const factor = b ^ result[0];
81
+ result.copyWithin(0, 1);
82
+ result[result.length - 1] = 0;
83
+ for (let i = 0; i < divisor.length; i++) {
84
+ result[i] ^= rsMultiply(divisor[i], factor);
85
+ }
86
+ }
87
+ return result;
88
+ }
89
+
90
+ /* -- Codeword capacity -- */
91
+
92
+ function numRawDataModules(ver) {
93
+ let result = (16 * ver + 128) * ver + 64;
94
+ if (ver >= 2) {
95
+ const numAlign = Math.floor(ver / 7) + 2;
96
+ result -= (25 * numAlign - 10) * numAlign - 55;
97
+ if (ver >= 7) result -= 36;
98
+ }
99
+ return result;
100
+ }
101
+
102
+ function numDataCodewords(ver, level) {
103
+ return Math.floor(numRawDataModules(ver) / 8)
104
+ - ECC_CODEWORDS_PER_BLOCK[level][ver] * NUM_ERROR_CORRECTION_BLOCKS[level][ver];
105
+ }
106
+
107
+ function alignmentPositions(size, ver) {
108
+ if (ver === 1) return [];
109
+ const numAlign = Math.floor(ver / 7) + 2;
110
+ const step = Math.floor((ver * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4)) * 2;
111
+ const result = [6];
112
+ for (let pos = size - 7; result.length < numAlign; pos -= step) {
113
+ result.splice(1, 0, pos);
114
+ }
115
+ return result;
116
+ }
117
+
118
+ /* -- Matrix construction -- */
119
+
120
+ class QrMatrix {
121
+ constructor(version) {
122
+ this.version = version;
123
+ this.size = version * 4 + 17;
124
+ this.modules = Array.from({ length: this.size }, () => new Array(this.size).fill(false));
125
+ this.isFunction = Array.from({ length: this.size }, () => new Array(this.size).fill(false));
126
+ }
127
+
128
+ set(x, y, dark) {
129
+ this.modules[y][x] = dark;
130
+ this.isFunction[y][x] = true;
131
+ }
132
+
133
+ drawFunctionPatterns(level) {
134
+ // Timing patterns
135
+ for (let i = 0; i < this.size; i++) {
136
+ this.set(6, i, i % 2 === 0);
137
+ this.set(i, 6, i % 2 === 0);
138
+ }
139
+ // Finder patterns (three corners) with separators
140
+ for (const [cx, cy] of [[3, 3], [this.size - 4, 3], [3, this.size - 4]]) {
141
+ for (let dy = -4; dy <= 4; dy++) {
142
+ for (let dx = -4; dx <= 4; dx++) {
143
+ const dist = Math.max(Math.abs(dx), Math.abs(dy));
144
+ const x = cx + dx, y = cy + dy;
145
+ if (x >= 0 && x < this.size && y >= 0 && y < this.size) {
146
+ this.set(x, y, dist !== 2 && dist !== 4);
147
+ }
148
+ }
149
+ }
150
+ }
151
+ // Alignment patterns (skipping the three finder corners)
152
+ const positions = alignmentPositions(this.size, this.version);
153
+ for (let i = 0; i < positions.length; i++) {
154
+ for (let j = 0; j < positions.length; j++) {
155
+ const isCorner = (i === 0 && j === 0) || (i === 0 && j === positions.length - 1) || (i === positions.length - 1 && j === 0);
156
+ if (!isCorner) this.drawAlignment(positions[i], positions[j]);
157
+ }
158
+ }
159
+ this.drawFormatBits(level, 0); // placeholder, rewritten once the mask is chosen
160
+ this.drawVersionBits();
161
+ }
162
+
163
+ drawAlignment(cx, cy) {
164
+ for (let dy = -2; dy <= 2; dy++) {
165
+ for (let dx = -2; dx <= 2; dx++) {
166
+ this.set(cx + dx, cy + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
167
+ }
168
+ }
169
+ }
170
+
171
+ drawFormatBits(level, mask) {
172
+ const data = ECC_FORMAT_BITS[level] << 3 | mask;
173
+ let rem = data;
174
+ for (let i = 0; i < 10; i++) {
175
+ rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
176
+ }
177
+ const bits = (data << 10 | rem) ^ 0x5412;
178
+ const bitAt = i => ((bits >>> i) & 1) !== 0;
179
+
180
+ for (let i = 0; i <= 5; i++) this.set(8, i, bitAt(i));
181
+ this.set(8, 7, bitAt(6));
182
+ this.set(8, 8, bitAt(7));
183
+ this.set(7, 8, bitAt(8));
184
+ for (let i = 9; i < 15; i++) this.set(14 - i, 8, bitAt(i));
185
+
186
+ for (let i = 0; i < 8; i++) this.set(this.size - 1 - i, 8, bitAt(i));
187
+ for (let i = 8; i < 15; i++) this.set(8, this.size - 15 + i, bitAt(i));
188
+ this.set(8, this.size - 8, true); // always-dark module
189
+ }
190
+
191
+ drawVersionBits() {
192
+ if (this.version < 7) return;
193
+ let rem = this.version;
194
+ for (let i = 0; i < 12; i++) {
195
+ rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
196
+ }
197
+ const bits = this.version << 12 | rem;
198
+ for (let i = 0; i < 18; i++) {
199
+ const dark = ((bits >>> i) & 1) !== 0;
200
+ const a = this.size - 11 + i % 3;
201
+ const b = Math.floor(i / 3);
202
+ this.set(a, b, dark);
203
+ this.set(b, a, dark);
204
+ }
205
+ }
206
+
207
+ /** Places the interleaved codewords into the non-function modules. */
208
+ drawCodewords(data) {
209
+ let bitIndex = 0;
210
+ for (let right = this.size - 1; right >= 1; right -= 2) {
211
+ if (right === 6) right = 5;
212
+ for (let vert = 0; vert < this.size; vert++) {
213
+ for (let j = 0; j < 2; j++) {
214
+ const x = right - j;
215
+ const upward = ((right + 1) & 2) === 0;
216
+ const y = upward ? this.size - 1 - vert : vert;
217
+ if (!this.isFunction[y][x] && bitIndex < data.length * 8) {
218
+ this.modules[y][x] = ((data[bitIndex >>> 3] >>> (7 - (bitIndex & 7))) & 1) !== 0;
219
+ bitIndex++;
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ applyMask(mask) {
227
+ for (let y = 0; y < this.size; y++) {
228
+ for (let x = 0; x < this.size; x++) {
229
+ let invert;
230
+ switch (mask) {
231
+ case 0: invert = (x + y) % 2 === 0; break;
232
+ case 1: invert = y % 2 === 0; break;
233
+ case 2: invert = x % 3 === 0; break;
234
+ case 3: invert = (x + y) % 3 === 0; break;
235
+ case 4: invert = (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0; break;
236
+ case 5: invert = (x * y) % 2 + (x * y) % 3 === 0; break;
237
+ case 6: invert = ((x * y) % 2 + (x * y) % 3) % 2 === 0; break;
238
+ case 7: invert = ((x + y) % 2 + (x * y) % 3) % 2 === 0; break;
239
+ }
240
+ if (!this.isFunction[y][x] && invert) {
241
+ this.modules[y][x] = !this.modules[y][x];
242
+ }
243
+ }
244
+ }
245
+ }
246
+
247
+ penaltyScore() {
248
+ const size = this.size;
249
+ let result = 0;
250
+
251
+ // Rule 1 + rule 3: runs of same color in rows and columns.
252
+ // The module grid is embedded in a light border to scan lines uniformly.
253
+ const lines = [];
254
+ for (let y = 0; y < size; y++) lines.push({ get: i => this.modules[y][i] });
255
+ for (let x = 0; x < size; x++) lines.push({ get: i => this.modules[i][x] });
256
+
257
+ for (const line of lines) {
258
+ let runColor = line.get(0);
259
+ let runLength = 1;
260
+ const runs = [];
261
+ for (let i = 1; i < size; i++) {
262
+ if (line.get(i) === runColor) {
263
+ runLength++;
264
+ }
265
+ else {
266
+ runs.push({ color: runColor, length: runLength });
267
+ runColor = line.get(i);
268
+ runLength = 1;
269
+ }
270
+ }
271
+ runs.push({ color: runColor, length: runLength });
272
+
273
+ // Rule 1: every run of 5+ same-colored modules
274
+ for (const run of runs) {
275
+ if (run.length >= 5) result += PENALTY_N1 + (run.length - 5);
276
+ }
277
+
278
+ // Rule 3: finder-like 1:1:3:1:1 pattern with 4 light modules on one side
279
+ let s = '';
280
+ for (let i = 0; i < size; i++) s += line.get(i) ? '1' : '0';
281
+ result += countFinderPatterns('0'.repeat(size) + s + '0'.repeat(size)) * PENALTY_N3;
282
+ }
283
+
284
+ // Rule 2: 2x2 blocks of the same color
285
+ for (let y = 0; y < size - 1; y++) {
286
+ for (let x = 0; x < size - 1; x++) {
287
+ const c = this.modules[y][x];
288
+ if (c === this.modules[y][x + 1] && c === this.modules[y + 1][x] && c === this.modules[y + 1][x + 1]) {
289
+ result += PENALTY_N2;
290
+ }
291
+ }
292
+ }
293
+
294
+ // Rule 4: dark/light balance
295
+ let dark = 0;
296
+ for (const row of this.modules) {
297
+ for (const cell of row) if (cell) dark++;
298
+ }
299
+ const total = size * size;
300
+ const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
301
+ result += Math.max(0, k) * PENALTY_N4;
302
+
303
+ return result;
304
+ }
305
+
306
+ clone() {
307
+ const copy = new QrMatrix(this.version);
308
+ for (let y = 0; y < this.size; y++) {
309
+ copy.modules[y] = [...this.modules[y]];
310
+ copy.isFunction[y] = [...this.isFunction[y]];
311
+ }
312
+ return copy;
313
+ }
314
+ }
315
+
316
+ /** Counts finder-like patterns (1011101 with 4+ light modules on one side) in a bit string. */
317
+ function countFinderPatterns(s) {
318
+ let count = 0;
319
+ for (let i = 0; i + 7 <= s.length; i++) {
320
+ if (s.startsWith('1011101', i)) {
321
+ const lightBefore = i >= 4 && s.slice(i - 4, i) === '0000';
322
+ const lightAfter = s.slice(i + 7, i + 11) === '0000';
323
+ if (lightBefore || lightAfter) count++;
324
+ }
325
+ }
326
+ return count;
327
+ }
328
+
329
+ /* -- Encoding pipeline -- */
330
+
331
+ function buildCodewords(text, level) {
332
+ const data = Buffer.from(text, 'utf8');
333
+ if (data.length > 2953) {
334
+ throw new RangeError('QR payload too long (byte mode supports up to 2953 bytes at ECC L)');
335
+ }
336
+
337
+ // Pick the smallest version that fits, boosting the EC level when possible
338
+ let version = 0;
339
+ let usedBits = 0;
340
+ for (let v = 1; v <= 40; v++) {
341
+ const capacityBits = numDataCodewords(v, 0) * 8; // level 0 = Low
342
+ const bits = 4 + (v <= 9 ? 8 : 16) + data.length * 8;
343
+ if (bits <= capacityBits) {
344
+ version = v;
345
+ usedBits = bits;
346
+ break;
347
+ }
348
+ }
349
+ if (version === 0) {
350
+ throw new RangeError('QR payload too long (exceeds version 40 capacity)');
351
+ }
352
+
353
+ let levelIndex = 0; // start at Low
354
+ const levels = ['L', 'M', 'Q', 'H'];
355
+ for (let i = 1; i < 4; i++) {
356
+ if (usedBits <= numDataCodewords(version, i) * 8) levelIndex = i;
357
+ }
358
+ const levelName = levels[levelIndex];
359
+
360
+ // Bit stream: mode + char count + data + terminator + pad
361
+ const capacityBits = numDataCodewords(version, levelIndex) * 8;
362
+ const bits = [];
363
+ const appendBits = (value, length) => {
364
+ for (let i = length - 1; i >= 0; i--) bits.push((value >>> i) & 1);
365
+ };
366
+ appendBits(0b0100, 4); // byte mode
367
+ appendBits(data.length, version <= 9 ? 8 : 16);
368
+ for (const byte of data) appendBits(byte, 8);
369
+ appendBits(0, Math.min(4, capacityBits - bits.length));
370
+ while (bits.length % 8 !== 0) bits.push(0);
371
+ for (let pad = 0xEC; bits.length < capacityBits; pad ^= 0xEC ^ 0x11) {
372
+ appendBits(pad, 8);
373
+ }
374
+
375
+ const dataCodewords = new Uint8Array(bits.length / 8);
376
+ for (let i = 0; i < dataCodewords.length; i++) {
377
+ for (let j = 0; j < 8; j++) {
378
+ dataCodewords[i] = (dataCodewords[i] << 1) | bits[i * 8 + j];
379
+ }
380
+ }
381
+
382
+ // Split into blocks, append ECC, interleave
383
+ const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[levelIndex][version];
384
+ const blockEccLen = ECC_CODEWORDS_PER_BLOCK[levelIndex][version];
385
+ const rawCodewords = Math.floor(numRawDataModules(version) / 8);
386
+ const numShortBlocks = numBlocks - (rawCodewords % numBlocks);
387
+ const shortBlockLen = Math.floor(rawCodewords / numBlocks);
388
+
389
+ const blocks = [];
390
+ const divisor = rsDivisor(blockEccLen);
391
+ for (let i = 0, k = 0; i < numBlocks; i++) {
392
+ const dat = dataCodewords.slice(k, k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1));
393
+ k += dat.length;
394
+ const ecc = rsRemainder(dat, divisor);
395
+ const isShort = i < numShortBlocks;
396
+ // Short blocks carry one placeholder byte (skipped during interleave)
397
+ const block = new Uint8Array(dat.length + ecc.length + (isShort ? 1 : 0));
398
+ block.set(dat, 0);
399
+ if (isShort) block[dat.length] = 0;
400
+ block.set(ecc, dat.length + (isShort ? 1 : 0));
401
+ blocks.push(block);
402
+ }
403
+
404
+ const result = [];
405
+ for (let i = 0; i < blocks[0].length; i++) {
406
+ for (let j = 0; j < numBlocks; j++) {
407
+ if (i !== shortBlockLen - blockEccLen || j >= numShortBlocks) {
408
+ result.push(blocks[j][i]);
409
+ }
410
+ }
411
+ }
412
+
413
+ return { version, levelName, codewords: new Uint8Array(result) };
414
+ }
415
+
416
+ function encodeMatrix(text) {
417
+ const { version, levelName, codewords } = buildCodewords(text);
418
+
419
+ let best = null;
420
+ let bestPenalty = Infinity;
421
+
422
+ // Try all 8 masks and keep the matrix with the lowest penalty score
423
+ for (let mask = 0; mask < 8; mask++) {
424
+ const matrix = new QrMatrix(version);
425
+ matrix.drawFunctionPatterns(levelName);
426
+ matrix.drawCodewords(codewords);
427
+ matrix.applyMask(mask);
428
+ matrix.drawFormatBits(levelName, mask);
429
+ const penalty = matrix.penaltyScore();
430
+ if (penalty < bestPenalty) {
431
+ bestPenalty = penalty;
432
+ best = matrix;
433
+ }
434
+ }
435
+ return best;
436
+ }
437
+
438
+ /* -- Renderers -- */
439
+
440
+ /** Half-block ASCII rendering with a 2-module quiet zone. */
441
+ function renderAscii(matrix) {
442
+ const border = 2;
443
+ const size = matrix.size + border * 2;
444
+ const get = (x, y) => {
445
+ const mx = x - border, my = y - border;
446
+ if (mx < 0 || my < 0 || mx >= matrix.size || my >= matrix.size) return false;
447
+ return matrix.modules[my][mx];
448
+ };
449
+ const lines = [];
450
+ for (let y = 0; y < size; y += 2) {
451
+ let line = '';
452
+ for (let x = 0; x < size; x++) {
453
+ const top = get(x, y);
454
+ const bottom = y + 1 < size ? get(x, y + 1) : false;
455
+ if (top && bottom) line += '\u2588'; // full block
456
+ else if (top) line += '\u2580'; // upper half
457
+ else if (bottom) line += '\u2584'; // lower half
458
+ else line += ' ';
459
+ }
460
+ lines.push(line);
461
+ }
462
+ return lines.join('\n') + '\n';
463
+ }
464
+
465
+ /** Minimal grayscale PNG encoder (8 bits, no palette). */
466
+ function renderPng(matrix, scale, border) {
467
+ const dim = (matrix.size + border * 2) * scale;
468
+ const width = dim, height = dim;
469
+
470
+ // Raw image data, one byte per pixel (0 = black, 255 = white), with filter byte per row
471
+ const stride = width + 1;
472
+ const raw = Buffer.alloc(stride * height);
473
+ for (let y = 0; y < height; y++) {
474
+ const rowStart = y * stride;
475
+ raw[rowStart] = 0; // filter: none
476
+ for (let x = 0; x < width; x++) {
477
+ const mx = Math.floor(x / scale) - border;
478
+ const my = Math.floor(y / scale) - border;
479
+ const dark = mx >= 0 && my >= 0 && mx < matrix.size && my < matrix.size && matrix.modules[my][mx];
480
+ raw[rowStart + 1 + x] = dark ? 0 : 255;
481
+ }
482
+ }
483
+
484
+ const IHDR = Buffer.alloc(13);
485
+ IHDR.writeUInt32BE(width, 0);
486
+ IHDR.writeUInt32BE(height, 4);
487
+ IHDR[8] = 8; // bit depth
488
+ IHDR[9] = 0; // color type: grayscale
489
+ IHDR[10] = 0; // compression
490
+ IHDR[11] = 0; // filter
491
+ IHDR[12] = 0; // interlace
492
+
493
+ const chunks = [];
494
+ const chunk = (type, data) => {
495
+ const len = Buffer.alloc(4);
496
+ len.writeUInt32BE(data.length, 0);
497
+ const typeBuf = Buffer.from(type, 'latin1');
498
+ const crcBuf = Buffer.concat([typeBuf, data]);
499
+ const crc = Buffer.alloc(4);
500
+ crc.writeUInt32BE(crc32(crcBuf), 0);
501
+ chunks.push(len, typeBuf, data, crc);
502
+ };
503
+
504
+ chunk('IHDR', IHDR);
505
+ chunk('IDAT', deflateSync(raw, { level: 9 }));
506
+ chunk('IEND', Buffer.alloc(0));
507
+
508
+ return Buffer.concat([Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), ...chunks]);
509
+ }
510
+
511
+ /**
512
+ * Encodes `text` into a QR Code and resolves to
513
+ * `{ ascii, getPngBuffer, getRaw, version, errorCorrectionLevel }`:
514
+ * - ascii -> terminal string (half-block glyphs, trailing newline)
515
+ * - getPngBuffer() -> Promise<Buffer> with an 8 px/module grayscale PNG
516
+ * - getRaw() -> boolean[][] of the module matrix (dark = true)
517
+ */
518
+ export async function encodeQR(text) {
519
+ if (typeof text !== 'string') {
520
+ throw new TypeError('encodeQR: expects a string payload');
521
+ }
522
+ const matrix = encodeMatrix(text);
523
+
524
+ return {
525
+ ascii: renderAscii(matrix),
526
+ getPngBuffer: async () => renderPng(matrix, 8, 4),
527
+ getRaw: async () => matrix.modules.map(row => [...row]),
528
+ version: matrix.version,
529
+ errorCorrectionLevel: 'L'
530
+ };
531
+ }