@eduh_dev021/baileys 0.0.0-stage → 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 +23 -0
  2. package/README.md +1843 -2
  3. package/WAProto/index.d.ts +22220 -0
  4. package/WAProto/index.js +5 -0
  5. package/cjs/WAProto/index.d.ts +22220 -0
  6. package/cjs/WAProto/index.js +60125 -0
  7. package/cjs/engine-requirements.js +12 -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 +18 -0
  13. package/cjs/lib/Addon/http-error.js +66 -0
  14. package/cjs/lib/Addon/index.d.ts +96 -0
  15. package/cjs/lib/Addon/index.js +58 -0
  16. package/cjs/lib/Addon/keyed-db.d.ts +14 -0
  17. package/cjs/lib/Addon/keyed-db.js +264 -0
  18. package/cjs/lib/Addon/link-preview.d.ts +15 -0
  19. package/cjs/lib/Addon/link-preview.js +104 -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 +184 -0
  27. package/cjs/lib/Defaults/index.js +292 -0
  28. package/cjs/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  29. package/cjs/lib/Signal/Group/ciphertext-message.js +37 -0
  30. package/cjs/lib/Signal/Group/group-cipher.d.ts +10 -0
  31. package/cjs/lib/Signal/Group/group-cipher.js +105 -0
  32. package/cjs/lib/Signal/Group/group-session-builder.d.ts +7 -0
  33. package/cjs/lib/Signal/Group/group-session-builder.js +65 -0
  34. package/cjs/lib/Signal/Group/index.d.ts +11 -0
  35. package/cjs/lib/Signal/Group/index.js +67 -0
  36. package/cjs/lib/Signal/Group/keyhelper.d.ts +6 -0
  37. package/cjs/lib/Signal/Group/keyhelper.js +47 -0
  38. package/cjs/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  39. package/cjs/lib/Signal/Group/sender-chain-key.js +51 -0
  40. package/cjs/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  41. package/cjs/lib/Signal/Group/sender-key-distribution-message.js +80 -0
  42. package/cjs/lib/Signal/Group/sender-key-message.d.ts +18 -0
  43. package/cjs/lib/Signal/Group/sender-key-message.js +87 -0
  44. package/cjs/lib/Signal/Group/sender-key-name.d.ts +11 -0
  45. package/cjs/lib/Signal/Group/sender-key-name.js +73 -0
  46. package/cjs/lib/Signal/Group/sender-key-record.d.ts +12 -0
  47. package/cjs/lib/Signal/Group/sender-key-record.js +66 -0
  48. package/cjs/lib/Signal/Group/sender-key-state.d.ts +16 -0
  49. package/cjs/lib/Signal/Group/sender-key-state.js +106 -0
  50. package/cjs/lib/Signal/Group/sender-message-key.d.ts +11 -0
  51. package/cjs/lib/Signal/Group/sender-message-key.js +51 -0
  52. package/cjs/lib/Signal/libsignal.d.ts +54 -0
  53. package/cjs/lib/Signal/libsignal.js +421 -0
  54. package/cjs/lib/Signal/lid-mapping.d.ts +20 -0
  55. package/cjs/lib/Signal/lid-mapping.js +292 -0
  56. package/cjs/lib/Socket/Client/index.d.ts +2 -0
  57. package/cjs/lib/Socket/Client/index.js +23 -0
  58. package/cjs/lib/Socket/Client/types.d.ts +5 -0
  59. package/cjs/lib/Socket/Client/types.js +36 -0
  60. package/cjs/lib/Socket/Client/websocket.d.ts +12 -0
  61. package/cjs/lib/Socket/Client/websocket.js +143 -0
  62. package/cjs/lib/Socket/business.d.ts +387 -0
  63. package/cjs/lib/Socket/business.js +402 -0
  64. package/cjs/lib/Socket/chats.d.ts +144 -0
  65. package/cjs/lib/Socket/chats.js +1118 -0
  66. package/cjs/lib/Socket/communities.d.ts +554 -0
  67. package/cjs/lib/Socket/communities.js +456 -0
  68. package/cjs/lib/Socket/groups.d.ts +286 -0
  69. package/cjs/lib/Socket/groups.js +366 -0
  70. package/cjs/lib/Socket/index.d.ts +522 -0
  71. package/cjs/lib/Socket/index.js +32 -0
  72. package/cjs/lib/Socket/messages-recv.d.ts +326 -0
  73. package/cjs/lib/Socket/messages-recv.js +1670 -0
  74. package/cjs/lib/Socket/messages-send.d.ts +321 -0
  75. package/cjs/lib/Socket/messages-send.js +1252 -0
  76. package/cjs/lib/Socket/mex.d.ts +1 -0
  77. package/cjs/lib/Socket/mex.js +65 -0
  78. package/cjs/lib/Socket/newsletter.d.ts +283 -0
  79. package/cjs/lib/Socket/newsletter.js +243 -0
  80. package/cjs/lib/Socket/socket.d.ts +61 -0
  81. package/cjs/lib/Socket/socket.js +908 -0
  82. package/cjs/lib/Store/index.d.ts +3 -0
  83. package/cjs/lib/Store/index.js +25 -0
  84. package/cjs/lib/Store/make-in-memory-store.d.ts +63 -0
  85. package/cjs/lib/Store/make-in-memory-store.js +431 -0
  86. package/cjs/lib/Store/make-ordered-dictionary.d.ts +12 -0
  87. package/cjs/lib/Store/make-ordered-dictionary.js +101 -0
  88. package/cjs/lib/Store/object-repository.d.ts +10 -0
  89. package/cjs/lib/Store/object-repository.js +49 -0
  90. package/cjs/lib/Transport/crypto.d.ts +6 -0
  91. package/cjs/lib/Transport/crypto.js +107 -0
  92. package/cjs/lib/Transport/curve.d.ts +8 -0
  93. package/cjs/lib/Transport/curve.js +94 -0
  94. package/cjs/lib/Transport/index.d.ts +16 -0
  95. package/cjs/lib/Transport/index.js +67 -0
  96. package/cjs/lib/Transport/internals.d.ts +57 -0
  97. package/cjs/lib/Transport/internals.js +203 -0
  98. package/cjs/lib/Transport/session-builder.d.ts +40 -0
  99. package/cjs/lib/Transport/session-builder.js +169 -0
  100. package/cjs/lib/Transport/session-cipher.d.ts +26 -0
  101. package/cjs/lib/Transport/session-cipher.js +328 -0
  102. package/cjs/lib/Transport/session-record.d.ts +55 -0
  103. package/cjs/lib/Transport/session-record.js +289 -0
  104. package/cjs/lib/Types/Auth.d.ts +1 -0
  105. package/cjs/lib/Types/Auth.js +15 -0
  106. package/cjs/lib/Types/Bussines.d.ts +1 -0
  107. package/cjs/lib/Types/Bussines.js +15 -0
  108. package/cjs/lib/Types/Call.d.ts +1 -0
  109. package/cjs/lib/Types/Call.js +15 -0
  110. package/cjs/lib/Types/Chat.d.ts +1 -0
  111. package/cjs/lib/Types/Chat.js +33 -0
  112. package/cjs/lib/Types/Contact.d.ts +1 -0
  113. package/cjs/lib/Types/Contact.js +15 -0
  114. package/cjs/lib/Types/Events.d.ts +1 -0
  115. package/cjs/lib/Types/Events.js +1 -0
  116. package/cjs/lib/Types/GroupMetadata.d.ts +1 -0
  117. package/cjs/lib/Types/GroupMetadata.js +15 -0
  118. package/cjs/lib/Types/Label.d.ts +2 -0
  119. package/cjs/lib/Types/Label.js +49 -0
  120. package/cjs/lib/Types/LabelAssociation.d.ts +2 -0
  121. package/cjs/lib/Types/LabelAssociation.js +31 -0
  122. package/cjs/lib/Types/Message.d.ts +11 -0
  123. package/cjs/lib/Types/Message.js +58 -0
  124. package/cjs/lib/Types/Mex.d.ts +2 -0
  125. package/cjs/lib/Types/Mex.js +66 -0
  126. package/cjs/lib/Types/Product.d.ts +1 -0
  127. package/cjs/lib/Types/Product.js +15 -0
  128. package/cjs/lib/Types/RichType.d.ts +2 -0
  129. package/cjs/lib/Types/RichType.js +50 -0
  130. package/cjs/lib/Types/Signal.d.ts +1 -0
  131. package/cjs/lib/Types/Signal.js +1 -0
  132. package/cjs/lib/Types/Socket.d.ts +1 -0
  133. package/cjs/lib/Types/Socket.js +1 -0
  134. package/cjs/lib/Types/State.d.ts +5 -0
  135. package/cjs/lib/Types/State.js +83 -0
  136. package/cjs/lib/Types/USync.d.ts +1 -0
  137. package/cjs/lib/Types/USync.js +1 -0
  138. package/cjs/lib/Types/index.d.ts +13 -0
  139. package/cjs/lib/Types/index.js +64 -0
  140. package/cjs/lib/Utils/auth-utils.d.ts +58 -0
  141. package/cjs/lib/Utils/auth-utils.js +275 -0
  142. package/cjs/lib/Utils/browser-utils.d.ts +9 -0
  143. package/cjs/lib/Utils/browser-utils.js +56 -0
  144. package/cjs/lib/Utils/business.d.ts +49 -0
  145. package/cjs/lib/Utils/business.js +259 -0
  146. package/cjs/lib/Utils/chat-utils.d.ts +408 -0
  147. package/cjs/lib/Utils/chat-utils.js +821 -0
  148. package/cjs/lib/Utils/companion-reg-client-utils.d.ts +4 -0
  149. package/cjs/lib/Utils/companion-reg-client-utils.js +71 -0
  150. package/cjs/lib/Utils/crypto.d.ts +39 -0
  151. package/cjs/lib/Utils/crypto.js +155 -0
  152. package/cjs/lib/Utils/decode-wa-message.d.ts +83 -0
  153. package/cjs/lib/Utils/decode-wa-message.js +316 -0
  154. package/cjs/lib/Utils/event-buffer.d.ts +12 -0
  155. package/cjs/lib/Utils/event-buffer.js +605 -0
  156. package/cjs/lib/Utils/generics.d.ts +63 -0
  157. package/cjs/lib/Utils/generics.js +440 -0
  158. package/cjs/lib/Utils/history.d.ts +75 -0
  159. package/cjs/lib/Utils/history.js +153 -0
  160. package/cjs/lib/Utils/identity-change-handler.d.ts +13 -0
  161. package/cjs/lib/Utils/identity-change-handler.js +73 -0
  162. package/cjs/lib/Utils/index.d.ts +24 -0
  163. package/cjs/lib/Utils/index.js +67 -0
  164. package/cjs/lib/Utils/link-preview.d.ts +13 -0
  165. package/cjs/lib/Utils/link-preview.js +87 -0
  166. package/cjs/lib/Utils/logger.d.ts +12 -0
  167. package/cjs/lib/Utils/logger.js +40 -0
  168. package/cjs/lib/Utils/lt-hash.d.ts +6 -0
  169. package/cjs/lib/Utils/lt-hash.js +28 -0
  170. package/cjs/lib/Utils/make-mutex.d.ts +6 -0
  171. package/cjs/lib/Utils/make-mutex.js +58 -0
  172. package/cjs/lib/Utils/message-retry-manager.d.ts +79 -0
  173. package/cjs/lib/Utils/message-retry-manager.js +274 -0
  174. package/cjs/lib/Utils/messages-media.d.ts +134 -0
  175. package/cjs/lib/Utils/messages-media.js +799 -0
  176. package/cjs/lib/Utils/messages.d.ts +47 -0
  177. package/cjs/lib/Utils/messages.js +1667 -0
  178. package/cjs/lib/Utils/noise-handler.d.ts +20 -0
  179. package/cjs/lib/Utils/noise-handler.js +223 -0
  180. package/cjs/lib/Utils/offline-node-processor.d.ts +10 -0
  181. package/cjs/lib/Utils/offline-node-processor.js +60 -0
  182. package/cjs/lib/Utils/pre-key-manager.d.ts +25 -0
  183. package/cjs/lib/Utils/pre-key-manager.js +116 -0
  184. package/cjs/lib/Utils/process-message.d.ts +83 -0
  185. package/cjs/lib/Utils/process-message.js +607 -0
  186. package/cjs/lib/Utils/reporting-utils.d.ts +12 -0
  187. package/cjs/lib/Utils/reporting-utils.js +282 -0
  188. package/cjs/lib/Utils/rich-message-utils.d.ts +48 -0
  189. package/cjs/lib/Utils/rich-message-utils.js +381 -0
  190. package/cjs/lib/Utils/signal.d.ts +82 -0
  191. package/cjs/lib/Utils/signal.js +232 -0
  192. package/cjs/lib/Utils/stanza-ack.d.ts +16 -0
  193. package/cjs/lib/Utils/stanza-ack.js +53 -0
  194. package/cjs/lib/Utils/sync-action-utils.d.ts +2 -0
  195. package/cjs/lib/Utils/sync-action-utils.js +68 -0
  196. package/cjs/lib/Utils/tc-token-utils.d.ts +29 -0
  197. package/cjs/lib/Utils/tc-token-utils.js +182 -0
  198. package/cjs/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
  199. package/cjs/lib/Utils/use-multi-file-auth-state.js +135 -0
  200. package/cjs/lib/Utils/use-single-file-auth-state.d.ts +11 -0
  201. package/cjs/lib/Utils/use-single-file-auth-state.js +115 -0
  202. package/cjs/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  203. package/cjs/lib/Utils/use-sqlite-auth-state.js +140 -0
  204. package/cjs/lib/Utils/validate-connection.d.ts +44 -0
  205. package/cjs/lib/Utils/validate-connection.js +229 -0
  206. package/cjs/lib/WABinary/constants.d.ts +61 -0
  207. package/cjs/lib/WABinary/constants.js +1992 -0
  208. package/cjs/lib/WABinary/decode.d.ts +9 -0
  209. package/cjs/lib/WABinary/decode.js +292 -0
  210. package/cjs/lib/WABinary/encode.d.ts +2 -0
  211. package/cjs/lib/WABinary/encode.js +240 -0
  212. package/cjs/lib/WABinary/generic-utils.d.ts +79 -0
  213. package/cjs/lib/WABinary/generic-utils.js +252 -0
  214. package/cjs/lib/WABinary/index.d.ts +5 -0
  215. package/cjs/lib/WABinary/index.js +29 -0
  216. package/cjs/lib/WABinary/jid-utils.d.ts +28 -0
  217. package/cjs/lib/WABinary/jid-utils.js +151 -0
  218. package/cjs/lib/WABinary/types.d.ts +1 -0
  219. package/cjs/lib/WABinary/types.js +23 -0
  220. package/cjs/lib/WACrypto/appstate-keys.js +39 -0
  221. package/cjs/lib/WACrypto/curves/constants.js +37 -0
  222. package/cjs/lib/WACrypto/hkdf.js +38 -0
  223. package/cjs/lib/WACrypto/index.js +51 -0
  224. package/cjs/lib/WACrypto/lthash.js +59 -0
  225. package/cjs/lib/WACrypto/math/constants.js +53 -0
  226. package/cjs/lib/WACrypto/math/edwards.js +206 -0
  227. package/cjs/lib/WACrypto/math/fe.js +449 -0
  228. package/cjs/lib/WACrypto/math/le.js +44 -0
  229. package/cjs/lib/WACrypto/math/mod.js +36 -0
  230. package/cjs/lib/WACrypto/util/bytes.js +348 -0
  231. package/cjs/lib/WACrypto/x25519.js +157 -0
  232. package/cjs/lib/WACrypto/xeddsa.js +134 -0
  233. package/cjs/lib/WAM/BinaryInfo.d.ts +7 -0
  234. package/cjs/lib/WAM/BinaryInfo.js +35 -0
  235. package/cjs/lib/WAM/constants.d.ts +34926 -0
  236. package/cjs/lib/WAM/constants.js +22890 -0
  237. package/cjs/lib/WAM/encode.d.ts +1 -0
  238. package/cjs/lib/WAM/encode.js +162 -0
  239. package/cjs/lib/WAM/index.d.ts +3 -0
  240. package/cjs/lib/WAM/index.js +25 -0
  241. package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
  242. package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.js +77 -0
  243. package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  244. package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.js +76 -0
  245. package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  246. package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +52 -0
  247. package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  248. package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.js +61 -0
  249. package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
  250. package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.js +50 -0
  251. package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
  252. package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +76 -0
  253. package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
  254. package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.js +53 -0
  255. package/cjs/lib/WAUSync/Protocols/index.d.ts +5 -0
  256. package/cjs/lib/WAUSync/Protocols/index.js +29 -0
  257. package/cjs/lib/WAUSync/USyncQuery.d.ts +20 -0
  258. package/cjs/lib/WAUSync/USyncQuery.js +113 -0
  259. package/cjs/lib/WAUSync/USyncUser.d.ts +16 -0
  260. package/cjs/lib/WAUSync/USyncUser.js +56 -0
  261. package/cjs/lib/WAUSync/index.d.ts +3 -0
  262. package/cjs/lib/WAUSync/index.js +25 -0
  263. package/cjs/lib/index.d.ts +11 -0
  264. package/cjs/lib/index.js +56 -0
  265. package/cjs/package.json +3 -0
  266. package/engine-requirements.js +12 -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 +18 -0
  272. package/lib/Addon/http-error.js +40 -0
  273. package/lib/Addon/index.d.ts +96 -0
  274. package/lib/Addon/index.js +26 -0
  275. package/lib/Addon/keyed-db.d.ts +14 -0
  276. package/lib/Addon/keyed-db.js +241 -0
  277. package/lib/Addon/link-preview.d.ts +15 -0
  278. package/lib/Addon/link-preview.js +79 -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 +184 -0
  286. package/lib/Defaults/index.js +181 -0
  287. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  288. package/lib/Signal/Group/ciphertext-message.js +11 -0
  289. package/lib/Signal/Group/group-cipher.d.ts +10 -0
  290. package/lib/Signal/Group/group-cipher.js +81 -0
  291. package/lib/Signal/Group/group-session-builder.d.ts +7 -0
  292. package/lib/Signal/Group/group-session-builder.js +29 -0
  293. package/lib/Signal/Group/index.d.ts +11 -0
  294. package/lib/Signal/Group/index.js +11 -0
  295. package/lib/Signal/Group/keyhelper.d.ts +6 -0
  296. package/lib/Signal/Group/keyhelper.js +17 -0
  297. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  298. package/lib/Signal/Group/sender-chain-key.js +25 -0
  299. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  300. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  301. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  302. package/lib/Signal/Group/sender-key-message.js +65 -0
  303. package/lib/Signal/Group/sender-key-name.d.ts +11 -0
  304. package/lib/Signal/Group/sender-key-name.js +47 -0
  305. package/lib/Signal/Group/sender-key-record.d.ts +12 -0
  306. package/lib/Signal/Group/sender-key-record.js +40 -0
  307. package/lib/Signal/Group/sender-key-state.d.ts +16 -0
  308. package/lib/Signal/Group/sender-key-state.js +83 -0
  309. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  310. package/lib/Signal/Group/sender-message-key.js +25 -0
  311. package/lib/Signal/libsignal.d.ts +54 -0
  312. package/lib/Signal/libsignal.js +426 -0
  313. package/lib/Signal/lid-mapping.d.ts +20 -0
  314. package/lib/Signal/lid-mapping.js +274 -0
  315. package/lib/Socket/Client/index.d.ts +2 -0
  316. package/lib/Socket/Client/index.js +2 -0
  317. package/lib/Socket/Client/types.d.ts +5 -0
  318. package/lib/Socket/Client/types.js +10 -0
  319. package/lib/Socket/Client/websocket.d.ts +12 -0
  320. package/lib/Socket/Client/websocket.js +119 -0
  321. package/lib/Socket/business.d.ts +387 -0
  322. package/lib/Socket/business.js +379 -0
  323. package/lib/Socket/chats.d.ts +144 -0
  324. package/lib/Socket/chats.js +1216 -0
  325. package/lib/Socket/communities.d.ts +554 -0
  326. package/lib/Socket/communities.js +431 -0
  327. package/lib/Socket/groups.d.ts +286 -0
  328. package/lib/Socket/groups.js +347 -0
  329. package/lib/Socket/index.d.ts +522 -0
  330. package/lib/Socket/index.js +11 -0
  331. package/lib/Socket/messages-recv.d.ts +326 -0
  332. package/lib/Socket/messages-recv.js +1822 -0
  333. package/lib/Socket/messages-send.d.ts +321 -0
  334. package/lib/Socket/messages-send.js +1378 -0
  335. package/lib/Socket/mex.d.ts +1 -0
  336. package/lib/Socket/mex.js +41 -0
  337. package/lib/Socket/newsletter.d.ts +283 -0
  338. package/lib/Socket/newsletter.js +225 -0
  339. package/lib/Socket/socket.d.ts +61 -0
  340. package/lib/Socket/socket.js +992 -0
  341. package/lib/Store/index.d.ts +3 -0
  342. package/lib/Store/index.js +3 -0
  343. package/lib/Store/make-in-memory-store.d.ts +63 -0
  344. package/lib/Store/make-in-memory-store.js +419 -0
  345. package/lib/Store/make-ordered-dictionary.d.ts +12 -0
  346. package/lib/Store/make-ordered-dictionary.js +78 -0
  347. package/lib/Store/object-repository.d.ts +10 -0
  348. package/lib/Store/object-repository.js +23 -0
  349. package/lib/Transport/crypto.d.ts +6 -0
  350. package/lib/Transport/crypto.js +61 -0
  351. package/lib/Transport/curve.d.ts +8 -0
  352. package/lib/Transport/curve.js +66 -0
  353. package/lib/Transport/index.d.ts +16 -0
  354. package/lib/Transport/index.js +9 -0
  355. package/lib/Transport/internals.d.ts +57 -0
  356. package/lib/Transport/internals.js +155 -0
  357. package/lib/Transport/session-builder.d.ts +40 -0
  358. package/lib/Transport/session-builder.js +143 -0
  359. package/lib/Transport/session-cipher.d.ts +26 -0
  360. package/lib/Transport/session-cipher.js +301 -0
  361. package/lib/Transport/session-record.d.ts +55 -0
  362. package/lib/Transport/session-record.js +272 -0
  363. package/lib/Types/Auth.d.ts +1 -0
  364. package/lib/Types/Auth.js +1 -0
  365. package/lib/Types/Bussines.d.ts +1 -0
  366. package/lib/Types/Bussines.js +1 -0
  367. package/lib/Types/Call.d.ts +1 -0
  368. package/lib/Types/Call.js +1 -0
  369. package/lib/Types/Chat.d.ts +1 -0
  370. package/lib/Types/Chat.js +7 -0
  371. package/lib/Types/Contact.d.ts +1 -0
  372. package/lib/Types/Contact.js +1 -0
  373. package/lib/Types/Events.d.ts +1 -0
  374. package/lib/Types/Events.js +1 -0
  375. package/lib/Types/GroupMetadata.d.ts +1 -0
  376. package/lib/Types/GroupMetadata.js +1 -0
  377. package/lib/Types/Label.d.ts +2 -0
  378. package/lib/Types/Label.js +24 -0
  379. package/lib/Types/LabelAssociation.d.ts +2 -0
  380. package/lib/Types/LabelAssociation.js +6 -0
  381. package/lib/Types/Message.d.ts +11 -0
  382. package/lib/Types/Message.js +17 -0
  383. package/lib/Types/Mex.d.ts +2 -0
  384. package/lib/Types/Mex.js +38 -0
  385. package/lib/Types/Product.d.ts +1 -0
  386. package/lib/Types/Product.js +1 -0
  387. package/lib/Types/RichType.d.ts +2 -0
  388. package/lib/Types/RichType.js +22 -0
  389. package/lib/Types/Signal.d.ts +1 -0
  390. package/lib/Types/Signal.js +1 -0
  391. package/lib/Types/Socket.d.ts +1 -0
  392. package/lib/Types/Socket.js +1 -0
  393. package/lib/Types/State.d.ts +5 -0
  394. package/lib/Types/State.js +54 -0
  395. package/lib/Types/USync.d.ts +1 -0
  396. package/lib/Types/USync.js +1 -0
  397. package/lib/Types/index.d.ts +13 -0
  398. package/lib/Types/index.js +25 -0
  399. package/lib/Utils/auth-utils.d.ts +58 -0
  400. package/lib/Utils/auth-utils.js +316 -0
  401. package/lib/Utils/browser-utils.d.ts +9 -0
  402. package/lib/Utils/browser-utils.js +28 -0
  403. package/lib/Utils/business.d.ts +49 -0
  404. package/lib/Utils/business.js +230 -0
  405. package/lib/Utils/chat-utils.d.ts +408 -0
  406. package/lib/Utils/chat-utils.js +871 -0
  407. package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
  408. package/lib/Utils/companion-reg-client-utils.js +39 -0
  409. package/lib/Utils/crypto.d.ts +39 -0
  410. package/lib/Utils/crypto.js +117 -0
  411. package/lib/Utils/decode-wa-message.d.ts +83 -0
  412. package/lib/Utils/decode-wa-message.js +316 -0
  413. package/lib/Utils/event-buffer.d.ts +12 -0
  414. package/lib/Utils/event-buffer.js +621 -0
  415. package/lib/Utils/generics.d.ts +63 -0
  416. package/lib/Utils/generics.js +394 -0
  417. package/lib/Utils/history.d.ts +75 -0
  418. package/lib/Utils/history.js +133 -0
  419. package/lib/Utils/identity-change-handler.d.ts +13 -0
  420. package/lib/Utils/identity-change-handler.js +48 -0
  421. package/lib/Utils/index.d.ts +24 -0
  422. package/lib/Utils/index.js +24 -0
  423. package/lib/Utils/link-preview.d.ts +13 -0
  424. package/lib/Utils/link-preview.js +73 -0
  425. package/lib/Utils/logger.d.ts +12 -0
  426. package/lib/Utils/logger.js +12 -0
  427. package/lib/Utils/lt-hash.d.ts +6 -0
  428. package/lib/Utils/lt-hash.js +7 -0
  429. package/lib/Utils/make-mutex.d.ts +6 -0
  430. package/lib/Utils/make-mutex.js +32 -0
  431. package/lib/Utils/message-retry-manager.d.ts +79 -0
  432. package/lib/Utils/message-retry-manager.js +259 -0
  433. package/lib/Utils/messages-media.d.ts +134 -0
  434. package/lib/Utils/messages-media.js +802 -0
  435. package/lib/Utils/messages.d.ts +47 -0
  436. package/lib/Utils/messages.js +1907 -0
  437. package/lib/Utils/noise-handler.d.ts +20 -0
  438. package/lib/Utils/noise-handler.js +200 -0
  439. package/lib/Utils/offline-node-processor.d.ts +10 -0
  440. package/lib/Utils/offline-node-processor.js +40 -0
  441. package/lib/Utils/pre-key-manager.d.ts +25 -0
  442. package/lib/Utils/pre-key-manager.js +105 -0
  443. package/lib/Utils/process-message.d.ts +83 -0
  444. package/lib/Utils/process-message.js +748 -0
  445. package/lib/Utils/reporting-utils.d.ts +12 -0
  446. package/lib/Utils/reporting-utils.js +257 -0
  447. package/lib/Utils/rich-message-utils.d.ts +48 -0
  448. package/lib/Utils/rich-message-utils.js +366 -0
  449. package/lib/Utils/signal.d.ts +82 -0
  450. package/lib/Utils/signal.js +200 -0
  451. package/lib/Utils/stanza-ack.d.ts +16 -0
  452. package/lib/Utils/stanza-ack.js +37 -0
  453. package/lib/Utils/sync-action-utils.d.ts +2 -0
  454. package/lib/Utils/sync-action-utils.js +48 -0
  455. package/lib/Utils/tc-token-utils.d.ts +29 -0
  456. package/lib/Utils/tc-token-utils.js +163 -0
  457. package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
  458. package/lib/Utils/use-multi-file-auth-state.js +116 -0
  459. package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
  460. package/lib/Utils/use-single-file-auth-state.js +94 -0
  461. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  462. package/lib/Utils/use-sqlite-auth-state.js +109 -0
  463. package/lib/Utils/validate-connection.d.ts +44 -0
  464. package/lib/Utils/validate-connection.js +210 -0
  465. package/lib/WABinary/constants.d.ts +61 -0
  466. package/lib/WABinary/constants.js +1466 -0
  467. package/lib/WABinary/decode.d.ts +9 -0
  468. package/lib/WABinary/decode.js +261 -0
  469. package/lib/WABinary/encode.d.ts +2 -0
  470. package/lib/WABinary/encode.js +219 -0
  471. package/lib/WABinary/generic-utils.d.ts +79 -0
  472. package/lib/WABinary/generic-utils.js +226 -0
  473. package/lib/WABinary/index.d.ts +5 -0
  474. package/lib/WABinary/index.js +5 -0
  475. package/lib/WABinary/jid-utils.d.ts +28 -0
  476. package/lib/WABinary/jid-utils.js +95 -0
  477. package/lib/WABinary/types.d.ts +1 -0
  478. package/lib/WABinary/types.js +1 -0
  479. package/lib/WACrypto/appstate-keys.js +26 -0
  480. package/lib/WACrypto/curves/constants.js +12 -0
  481. package/lib/WACrypto/hkdf.js +27 -0
  482. package/lib/WACrypto/index.js +20 -0
  483. package/lib/WACrypto/lthash.js +54 -0
  484. package/lib/WACrypto/math/constants.js +24 -0
  485. package/lib/WACrypto/math/edwards.js +194 -0
  486. package/lib/WACrypto/math/fe.js +698 -0
  487. package/lib/WACrypto/math/le.js +23 -0
  488. package/lib/WACrypto/math/mod.js +15 -0
  489. package/lib/WACrypto/util/bytes.js +339 -0
  490. package/lib/WACrypto/x25519.js +145 -0
  491. package/lib/WACrypto/xeddsa.js +92 -0
  492. package/lib/WAM/BinaryInfo.d.ts +7 -0
  493. package/lib/WAM/BinaryInfo.js +9 -0
  494. package/lib/WAM/constants.d.ts +34926 -0
  495. package/lib/WAM/constants.js +22852 -0
  496. package/lib/WAM/encode.d.ts +1 -0
  497. package/lib/WAM/encode.js +149 -0
  498. package/lib/WAM/index.d.ts +3 -0
  499. package/lib/WAM/index.js +3 -0
  500. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
  501. package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
  502. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  503. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  504. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  505. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  506. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  507. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  508. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
  509. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  510. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
  511. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  512. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
  513. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  514. package/lib/WAUSync/Protocols/index.d.ts +5 -0
  515. package/lib/WAUSync/Protocols/index.js +5 -0
  516. package/lib/WAUSync/USyncQuery.d.ts +20 -0
  517. package/lib/WAUSync/USyncQuery.js +97 -0
  518. package/lib/WAUSync/USyncUser.d.ts +16 -0
  519. package/lib/WAUSync/USyncUser.js +30 -0
  520. package/lib/WAUSync/index.d.ts +3 -0
  521. package/lib/WAUSync/index.js +3 -0
  522. package/lib/index.d.ts +11 -0
  523. package/lib/index.js +11 -0
  524. package/package.json +74 -5
  525. package/tests/addon-test.mjs +162 -0
  526. package/tests/differential-test.mjs +90 -0
@@ -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
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * `createQueue` — a promise task queue with bounded concurrency:
3
+ *
4
+ * const queue = createQueue(1); // serialize tasks (default is 1)
5
+ * const result = await queue.add(async () => { ... });
6
+ * await queue.push(fn); // push is an alias of add
7
+ *
8
+ * Tasks run in FIFO order; at most `concurrency` tasks are active at a time.
9
+ * A rejected task only rejects its own promise — the queue keeps working.
10
+ */
11
+ export function createQueue(concurrency = 1) {
12
+ const limit = Number.isFinite(concurrency) && concurrency > 0 ? Math.floor(concurrency) : 1;
13
+ const pending = []; // waiting tasks in FIFO order
14
+ let active = 0;
15
+
16
+ function pump() {
17
+ while (active < limit && pending.length > 0) {
18
+ const { task, resolve, reject } = pending.shift();
19
+ active++;
20
+ Promise.resolve()
21
+ .then(task)
22
+ .then(resolve, reject)
23
+ .finally(() => {
24
+ active--;
25
+ pump();
26
+ });
27
+ }
28
+ }
29
+
30
+ function add(task) {
31
+ if (typeof task !== 'function') {
32
+ return Promise.reject(new TypeError('queue.add() expects a function'));
33
+ }
34
+ return new Promise((resolve, reject) => {
35
+ pending.push({ task, resolve, reject });
36
+ pump();
37
+ });
38
+ }
39
+
40
+ return {
41
+ add,
42
+ push: add
43
+ };
44
+ }