zapo-js 0.1.0 → 0.1.2

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 (366) hide show
  1. package/README.md +12 -7
  2. package/dist/appstate/WaAppStateCrypto.js +18 -25
  3. package/dist/appstate/WaAppStateSyncClient.js +181 -114
  4. package/dist/appstate/WaAppStateSyncResponseParser.js +16 -5
  5. package/dist/appstate/constants.js +4 -3
  6. package/dist/appstate/utils.js +10 -30
  7. package/dist/auth/WaAuthClient.js +48 -55
  8. package/dist/auth/flow/WaAuthCredentialsFlow.js +21 -14
  9. package/dist/auth/index.js +1 -3
  10. package/dist/auth/pairing/WaPairingFlow.js +21 -23
  11. package/dist/auth/pairing/WaQrFlow.js +37 -24
  12. package/dist/client/WaClient.js +103 -276
  13. package/dist/client/WaClientFactory.js +227 -110
  14. package/dist/client/connection/WaConnectionManager.js +292 -0
  15. package/dist/client/connection/WaKeyShareCoordinator.js +63 -0
  16. package/dist/client/connection/WaReceiptQueue.js +51 -0
  17. package/dist/client/coordinators/WaAppStateMutationCoordinator.js +471 -0
  18. package/dist/client/coordinators/WaGroupCoordinator.js +27 -17
  19. package/dist/client/coordinators/WaIncomingNodeCoordinator.js +20 -27
  20. package/dist/client/coordinators/WaMessageDispatchCoordinator.js +231 -686
  21. package/dist/client/coordinators/WaRetryCoordinator.js +70 -37
  22. package/dist/client/dirty.js +35 -29
  23. package/dist/client/events/chat.js +4 -3
  24. package/dist/client/events/group.js +59 -36
  25. package/dist/client/history-sync.js +53 -63
  26. package/dist/client/incoming.js +23 -20
  27. package/dist/client/mailbox.js +8 -8
  28. package/dist/client/messages.js +4 -4
  29. package/dist/client/messaging/fanout.js +189 -0
  30. package/dist/client/messaging/key-protocol.js +130 -0
  31. package/dist/client/messaging/participants.js +191 -0
  32. package/dist/crypto/core/hkdf.js +3 -8
  33. package/dist/crypto/core/index.js +1 -4
  34. package/dist/crypto/core/keys.js +2 -3
  35. package/dist/crypto/core/primitives.js +12 -15
  36. package/dist/crypto/core/random.js +7 -26
  37. package/dist/crypto/curves/Ed25519.js +7 -8
  38. package/dist/crypto/curves/X25519.js +13 -16
  39. package/dist/crypto/index.js +0 -5
  40. package/dist/esm/appstate/WaAppStateCrypto.js +6 -13
  41. package/dist/esm/appstate/WaAppStateSyncClient.js +174 -107
  42. package/dist/esm/appstate/WaAppStateSyncResponseParser.js +17 -6
  43. package/dist/esm/appstate/constants.js +3 -2
  44. package/dist/esm/appstate/utils.js +8 -27
  45. package/dist/esm/auth/WaAuthClient.js +48 -55
  46. package/dist/esm/auth/flow/WaAuthCredentialsFlow.js +21 -14
  47. package/dist/esm/auth/index.js +0 -1
  48. package/dist/esm/auth/pairing/WaPairingFlow.js +14 -16
  49. package/dist/esm/auth/pairing/WaQrFlow.js +37 -24
  50. package/dist/esm/client/WaClient.js +103 -276
  51. package/dist/esm/client/WaClientFactory.js +227 -110
  52. package/dist/esm/client/connection/WaConnectionManager.js +288 -0
  53. package/dist/esm/client/connection/WaKeyShareCoordinator.js +59 -0
  54. package/dist/esm/client/connection/WaReceiptQueue.js +47 -0
  55. package/dist/esm/client/coordinators/WaAppStateMutationCoordinator.js +467 -0
  56. package/dist/esm/client/coordinators/WaGroupCoordinator.js +20 -10
  57. package/dist/esm/client/coordinators/WaIncomingNodeCoordinator.js +20 -27
  58. package/dist/esm/client/coordinators/WaMessageDispatchCoordinator.js +232 -687
  59. package/dist/esm/client/coordinators/WaRetryCoordinator.js +71 -38
  60. package/dist/esm/client/dirty.js +30 -24
  61. package/dist/esm/client/events/chat.js +4 -3
  62. package/dist/esm/client/events/group.js +50 -28
  63. package/dist/esm/client/history-sync.js +50 -60
  64. package/dist/esm/client/incoming.js +23 -20
  65. package/dist/esm/client/mailbox.js +8 -8
  66. package/dist/esm/client/messages.js +1 -1
  67. package/dist/esm/client/messaging/fanout.js +186 -0
  68. package/dist/esm/client/messaging/key-protocol.js +127 -0
  69. package/dist/esm/client/messaging/participants.js +188 -0
  70. package/dist/esm/crypto/core/hkdf.js +3 -8
  71. package/dist/esm/crypto/core/index.js +0 -1
  72. package/dist/esm/crypto/core/keys.js +2 -3
  73. package/dist/esm/crypto/core/primitives.js +12 -15
  74. package/dist/esm/crypto/core/random.js +6 -25
  75. package/dist/esm/crypto/curves/Ed25519.js +4 -5
  76. package/dist/esm/crypto/curves/X25519.js +10 -13
  77. package/dist/esm/crypto/index.js +0 -2
  78. package/dist/esm/infra/log/ConsoleLogger.js +18 -17
  79. package/dist/esm/infra/log/PinoLogger.js +15 -9
  80. package/dist/esm/infra/log/types.js +11 -1
  81. package/dist/esm/infra/perf/BoundedTaskQueue.js +13 -17
  82. package/dist/esm/media/WaMediaCrypto.js +2 -4
  83. package/dist/esm/media/WaMediaTransferClient.js +226 -58
  84. package/dist/esm/media/conn.js +10 -6
  85. package/dist/esm/media/constants.js +4 -1
  86. package/dist/esm/message/WaMessageClient.js +4 -13
  87. package/dist/esm/message/ack.js +6 -6
  88. package/dist/esm/message/addon-crypto.js +59 -0
  89. package/dist/esm/message/incoming.js +106 -111
  90. package/dist/esm/message/index.js +2 -0
  91. package/dist/esm/message/reporting-token.js +438 -0
  92. package/dist/esm/message/use-case-secret.js +49 -0
  93. package/dist/esm/protocol/appstate.js +58 -0
  94. package/dist/esm/protocol/constants.js +2 -1
  95. package/dist/esm/protocol/index.js +2 -10
  96. package/dist/esm/protocol/jid.js +63 -51
  97. package/dist/esm/protocol/media.js +3 -3
  98. package/dist/esm/protocol/nodes.js +2 -0
  99. package/dist/esm/protocol/usync.js +11 -0
  100. package/dist/esm/retry/index.js +1 -0
  101. package/dist/esm/retry/outbound.js +4 -5
  102. package/dist/esm/retry/parse.js +58 -76
  103. package/dist/esm/retry/replay.js +48 -49
  104. package/dist/esm/retry/tracker.js +56 -0
  105. package/dist/esm/signal/api/SignalDeviceSyncApi.js +249 -82
  106. package/dist/esm/signal/api/SignalDigestSyncApi.js +6 -1
  107. package/dist/esm/signal/api/SignalIdentitySyncApi.js +49 -34
  108. package/dist/esm/signal/api/SignalMissingPreKeysSyncApi.js +70 -62
  109. package/dist/esm/signal/api/SignalSessionSyncApi.js +23 -30
  110. package/dist/esm/signal/crypto/WaAdvSignature.js +3 -5
  111. package/dist/esm/signal/group/SenderKeyChain.js +28 -23
  112. package/dist/esm/signal/group/SenderKeyCodec.js +2 -4
  113. package/dist/esm/signal/group/SenderKeyManager.js +26 -16
  114. package/dist/esm/signal/index.js +1 -0
  115. package/dist/esm/signal/session/SignalProtocol.js +49 -14
  116. package/dist/esm/signal/session/SignalRatchet.js +24 -15
  117. package/dist/esm/signal/session/SignalSession.js +14 -9
  118. package/dist/esm/signal/session/resolver.js +186 -0
  119. package/dist/esm/signal/store/sqlite.js +16 -37
  120. package/dist/esm/store/createStore.js +16 -18
  121. package/dist/esm/store/noop.store.js +3 -6
  122. package/dist/esm/store/providers/memory/appstate.store.js +30 -6
  123. package/dist/esm/store/providers/memory/contact.store.js +5 -0
  124. package/dist/esm/store/providers/memory/device-list.store.js +3 -30
  125. package/dist/esm/store/providers/memory/message.store.js +11 -5
  126. package/dist/esm/store/providers/memory/participants.store.js +1 -8
  127. package/dist/esm/store/providers/memory/sender-key.store.js +5 -7
  128. package/dist/esm/store/providers/memory/signal.store.js +13 -1
  129. package/dist/esm/store/providers/memory/thread.store.js +5 -0
  130. package/dist/esm/store/providers/sqlite/appstate.store.js +82 -1
  131. package/dist/esm/store/providers/sqlite/connection.js +18 -13
  132. package/dist/esm/store/providers/sqlite/contact.store.js +31 -18
  133. package/dist/esm/store/providers/sqlite/device-list.store.js +7 -35
  134. package/dist/esm/store/providers/sqlite/message.store.js +45 -32
  135. package/dist/esm/store/providers/sqlite/migrations.js +1 -1
  136. package/dist/esm/store/providers/sqlite/participants.store.js +1 -9
  137. package/dist/esm/store/providers/sqlite/retry.store.js +8 -11
  138. package/dist/esm/store/providers/sqlite/sender-key.store.js +25 -30
  139. package/dist/esm/store/providers/sqlite/signal.store.js +104 -22
  140. package/dist/esm/store/providers/sqlite/table-names.js +107 -0
  141. package/dist/esm/store/providers/sqlite/thread.store.js +35 -22
  142. package/dist/esm/transport/WaComms.js +25 -23
  143. package/dist/esm/transport/WaWebSocket.js +115 -12
  144. package/dist/esm/transport/binary/decoder.js +4 -4
  145. package/dist/esm/transport/binary/encoder.js +12 -4
  146. package/dist/esm/transport/index.js +1 -0
  147. package/dist/esm/transport/keepalive/WaKeepAlive.js +2 -8
  148. package/dist/esm/transport/node/WaNodeOrchestrator.js +2 -4
  149. package/dist/esm/transport/node/WaNodeTransport.js +0 -3
  150. package/dist/esm/transport/node/builders/{accountSync.js → account-sync.js} +16 -36
  151. package/dist/esm/transport/node/builders/index.js +2 -1
  152. package/dist/esm/transport/node/builders/message.js +9 -0
  153. package/dist/esm/transport/node/builders/pairing.js +4 -5
  154. package/dist/esm/transport/node/builders/usync.js +41 -0
  155. package/dist/esm/transport/node/helpers.js +107 -5
  156. package/dist/esm/transport/node/usync.js +35 -0
  157. package/dist/esm/transport/noise/WaFrameCodec.js +48 -33
  158. package/dist/esm/transport/noise/WaNoiseCert.js +3 -6
  159. package/dist/esm/transport/noise/WaNoiseSession.js +17 -10
  160. package/dist/esm/transport/proxy.js +27 -0
  161. package/dist/esm/transport/stream/parse.js +13 -48
  162. package/dist/esm/util/bytes.js +50 -32
  163. package/dist/esm/util/coercion.js +6 -14
  164. package/dist/esm/util/primitives.js +39 -14
  165. package/dist/infra/log/ConsoleLogger.js +18 -17
  166. package/dist/infra/log/PinoLogger.js +15 -9
  167. package/dist/infra/log/types.js +12 -0
  168. package/dist/infra/perf/BoundedTaskQueue.js +13 -17
  169. package/dist/media/WaMediaCrypto.js +1 -3
  170. package/dist/media/WaMediaTransferClient.js +259 -58
  171. package/dist/media/conn.js +10 -6
  172. package/dist/media/constants.js +4 -1
  173. package/dist/message/WaMessageClient.js +5 -14
  174. package/dist/message/ack.js +6 -6
  175. package/dist/message/addon-crypto.js +65 -0
  176. package/dist/message/incoming.js +104 -109
  177. package/dist/message/index.js +2 -0
  178. package/dist/message/reporting-token.js +443 -0
  179. package/dist/message/use-case-secret.js +55 -0
  180. package/dist/protocol/appstate.js +59 -1
  181. package/dist/protocol/constants.js +7 -1
  182. package/dist/protocol/index.js +20 -42
  183. package/dist/protocol/jid.js +64 -51
  184. package/dist/protocol/media.js +3 -3
  185. package/dist/protocol/nodes.js +2 -0
  186. package/dist/protocol/usync.js +14 -0
  187. package/dist/retry/index.js +3 -1
  188. package/dist/retry/outbound.js +6 -7
  189. package/dist/retry/parse.js +57 -75
  190. package/dist/retry/replay.js +46 -47
  191. package/dist/retry/tracker.js +59 -0
  192. package/dist/signal/api/SignalDeviceSyncApi.js +247 -80
  193. package/dist/signal/api/SignalDigestSyncApi.js +6 -1
  194. package/dist/signal/api/SignalIdentitySyncApi.js +49 -34
  195. package/dist/signal/api/SignalMissingPreKeysSyncApi.js +67 -59
  196. package/dist/signal/api/SignalSessionSyncApi.js +23 -30
  197. package/dist/signal/crypto/WaAdvSignature.js +2 -4
  198. package/dist/signal/group/SenderKeyChain.js +27 -22
  199. package/dist/signal/group/SenderKeyCodec.js +1 -3
  200. package/dist/signal/group/SenderKeyManager.js +26 -16
  201. package/dist/signal/index.js +3 -1
  202. package/dist/signal/session/SignalProtocol.js +49 -14
  203. package/dist/signal/session/SignalRatchet.js +24 -15
  204. package/dist/signal/session/SignalSession.js +14 -9
  205. package/dist/signal/session/resolver.js +189 -0
  206. package/dist/signal/store/sqlite.js +16 -37
  207. package/dist/store/createStore.js +16 -18
  208. package/dist/store/noop.store.js +3 -6
  209. package/dist/store/providers/memory/appstate.store.js +28 -4
  210. package/dist/store/providers/memory/contact.store.js +5 -0
  211. package/dist/store/providers/memory/device-list.store.js +3 -30
  212. package/dist/store/providers/memory/message.store.js +11 -5
  213. package/dist/store/providers/memory/participants.store.js +1 -8
  214. package/dist/store/providers/memory/sender-key.store.js +8 -10
  215. package/dist/store/providers/memory/signal.store.js +21 -9
  216. package/dist/store/providers/memory/thread.store.js +5 -0
  217. package/dist/store/providers/sqlite/appstate.store.js +81 -0
  218. package/dist/store/providers/sqlite/connection.js +18 -13
  219. package/dist/store/providers/sqlite/contact.store.js +31 -18
  220. package/dist/store/providers/sqlite/device-list.store.js +7 -35
  221. package/dist/store/providers/sqlite/message.store.js +45 -32
  222. package/dist/store/providers/sqlite/migrations.js +1 -1
  223. package/dist/store/providers/sqlite/participants.store.js +1 -9
  224. package/dist/store/providers/sqlite/retry.store.js +8 -11
  225. package/dist/store/providers/sqlite/sender-key.store.js +24 -29
  226. package/dist/store/providers/sqlite/signal.store.js +105 -23
  227. package/dist/store/providers/sqlite/table-names.js +113 -0
  228. package/dist/store/providers/sqlite/thread.store.js +35 -22
  229. package/dist/transport/WaComms.js +27 -25
  230. package/dist/transport/WaWebSocket.js +148 -12
  231. package/dist/transport/binary/decoder.js +4 -4
  232. package/dist/transport/binary/encoder.js +12 -4
  233. package/dist/transport/index.js +7 -1
  234. package/dist/transport/keepalive/WaKeepAlive.js +1 -7
  235. package/dist/transport/node/WaNodeOrchestrator.js +2 -4
  236. package/dist/transport/node/WaNodeTransport.js +0 -3
  237. package/dist/transport/node/builders/{accountSync.js → account-sync.js} +15 -35
  238. package/dist/transport/node/builders/index.js +12 -9
  239. package/dist/transport/node/builders/message.js +9 -0
  240. package/dist/transport/node/builders/pairing.js +4 -5
  241. package/dist/transport/node/builders/usync.js +45 -0
  242. package/dist/transport/node/helpers.js +112 -4
  243. package/dist/transport/node/usync.js +38 -0
  244. package/dist/transport/noise/WaFrameCodec.js +47 -32
  245. package/dist/transport/noise/WaNoiseCert.js +5 -8
  246. package/dist/transport/noise/WaNoiseSession.js +17 -10
  247. package/dist/transport/proxy.js +34 -0
  248. package/dist/transport/stream/parse.js +17 -53
  249. package/dist/types/appstate/WaAppStateCrypto.d.ts +0 -1
  250. package/dist/types/appstate/WaAppStateSyncClient.d.ts +5 -2
  251. package/dist/types/appstate/constants.d.ts +1 -0
  252. package/dist/types/appstate/store/sqlite.d.ts +4 -18
  253. package/dist/types/appstate/utils.d.ts +0 -1
  254. package/dist/types/auth/WaAuthClient.d.ts +10 -12
  255. package/dist/types/auth/index.d.ts +0 -2
  256. package/dist/types/auth/pairing/WaQrFlow.d.ts +1 -1
  257. package/dist/types/auth/types.d.ts +6 -9
  258. package/dist/types/client/WaClient.d.ts +27 -25
  259. package/dist/types/client/WaClientFactory.d.ts +22 -23
  260. package/dist/types/client/connection/WaConnectionManager.d.ts +64 -0
  261. package/dist/types/client/connection/WaKeyShareCoordinator.d.ts +14 -0
  262. package/dist/types/client/connection/WaReceiptQueue.d.ts +13 -0
  263. package/dist/types/client/coordinators/WaAppStateMutationCoordinator.d.ts +46 -0
  264. package/dist/types/client/coordinators/WaIncomingNodeCoordinator.d.ts +0 -1
  265. package/dist/types/client/coordinators/WaMessageDispatchCoordinator.d.ts +18 -41
  266. package/dist/types/client/coordinators/WaRetryCoordinator.d.ts +2 -0
  267. package/dist/types/client/dirty.d.ts +1 -0
  268. package/dist/types/client/events/group.d.ts +2 -1
  269. package/dist/types/client/index.d.ts +1 -1
  270. package/dist/types/client/messaging/fanout.d.ts +14 -0
  271. package/dist/types/client/messaging/key-protocol.d.ts +18 -0
  272. package/dist/types/client/messaging/participants.d.ts +13 -0
  273. package/dist/types/client/types.d.ts +24 -1
  274. package/dist/types/crypto/core/hkdf.d.ts +0 -6
  275. package/dist/types/crypto/core/index.d.ts +0 -1
  276. package/dist/types/crypto/core/random.d.ts +1 -7
  277. package/dist/types/crypto/index.d.ts +0 -2
  278. package/dist/types/index.d.ts +1 -1
  279. package/dist/types/infra/log/ConsoleLogger.d.ts +2 -1
  280. package/dist/types/infra/log/PinoLogger.d.ts +1 -1
  281. package/dist/types/infra/log/types.d.ts +1 -0
  282. package/dist/types/infra/perf/BoundedTaskQueue.d.ts +1 -1
  283. package/dist/types/media/WaMediaTransferClient.d.ts +13 -3
  284. package/dist/types/media/types.d.ts +5 -0
  285. package/dist/types/message/addon-crypto.d.ts +25 -0
  286. package/dist/types/message/index.d.ts +2 -0
  287. package/dist/types/message/reporting-token.d.ts +19 -0
  288. package/dist/types/message/use-case-secret.d.ts +20 -0
  289. package/dist/types/protocol/appstate.d.ts +58 -0
  290. package/dist/types/protocol/constants.d.ts +2 -1
  291. package/dist/types/protocol/index.d.ts +2 -10
  292. package/dist/types/protocol/jid.d.ts +3 -3
  293. package/dist/types/protocol/nodes.d.ts +2 -0
  294. package/dist/types/protocol/usync.d.ts +11 -0
  295. package/dist/types/retry/index.d.ts +1 -0
  296. package/dist/types/retry/replay.d.ts +0 -4
  297. package/dist/types/retry/tracker.d.ts +19 -0
  298. package/dist/types/retry/types.d.ts +4 -3
  299. package/dist/types/signal/api/SignalDeviceSyncApi.d.ts +13 -1
  300. package/dist/types/signal/group/SenderKeyCodec.d.ts +4 -6
  301. package/dist/types/signal/index.d.ts +1 -0
  302. package/dist/types/signal/session/SignalProtocol.d.ts +9 -0
  303. package/dist/types/signal/session/resolver.d.ts +17 -0
  304. package/dist/types/store/contracts/appstate.store.d.ts +3 -0
  305. package/dist/types/store/contracts/contact.store.d.ts +1 -0
  306. package/dist/types/store/contracts/device-list.store.d.ts +0 -3
  307. package/dist/types/store/contracts/message.store.d.ts +1 -0
  308. package/dist/types/store/contracts/participants.store.d.ts +0 -1
  309. package/dist/types/store/contracts/sender-key.store.d.ts +0 -1
  310. package/dist/types/store/contracts/signal.store.d.ts +6 -0
  311. package/dist/types/store/contracts/thread.store.d.ts +1 -0
  312. package/dist/types/store/index.d.ts +1 -1
  313. package/dist/types/store/providers/memory/appstate.store.d.ts +2 -0
  314. package/dist/types/store/providers/memory/contact.store.d.ts +1 -0
  315. package/dist/types/store/providers/memory/device-list.store.d.ts +0 -3
  316. package/dist/types/store/providers/memory/message.store.d.ts +1 -0
  317. package/dist/types/store/providers/memory/participants.store.d.ts +0 -1
  318. package/dist/types/store/providers/memory/sender-key.store.d.ts +0 -1
  319. package/dist/types/store/providers/memory/signal.store.d.ts +6 -0
  320. package/dist/types/store/providers/memory/thread.store.d.ts +1 -0
  321. package/dist/types/store/providers/sqlite/appstate.store.d.ts +2 -0
  322. package/dist/types/store/providers/sqlite/contact.store.d.ts +2 -0
  323. package/dist/types/store/providers/sqlite/device-list.store.d.ts +0 -3
  324. package/dist/types/store/providers/sqlite/message.store.d.ts +2 -0
  325. package/dist/types/store/providers/sqlite/participants.store.d.ts +0 -1
  326. package/dist/types/store/providers/sqlite/retry.store.d.ts +0 -1
  327. package/dist/types/store/providers/sqlite/sender-key.store.d.ts +0 -1
  328. package/dist/types/store/providers/sqlite/signal.store.d.ts +7 -0
  329. package/dist/types/store/providers/sqlite/table-names.d.ts +5 -0
  330. package/dist/types/store/providers/sqlite/thread.store.d.ts +2 -0
  331. package/dist/types/store/types.d.ts +3 -0
  332. package/dist/types/transport/WaWebSocket.d.ts +3 -0
  333. package/dist/types/transport/index.d.ts +2 -1
  334. package/dist/types/transport/keepalive/WaKeepAlive.d.ts +0 -1
  335. package/dist/types/transport/node/WaNodeTransport.d.ts +0 -9
  336. package/dist/types/transport/node/builders/group.d.ts +4 -6
  337. package/dist/types/transport/node/builders/index.d.ts +2 -1
  338. package/dist/types/transport/node/builders/message.d.ts +14 -25
  339. package/dist/types/transport/node/builders/retry.d.ts +2 -4
  340. package/dist/types/transport/node/builders/usync.d.ts +21 -0
  341. package/dist/types/transport/node/helpers.d.ts +8 -0
  342. package/dist/types/transport/node/usync.d.ts +2 -0
  343. package/dist/types/transport/noise/WaFrameCodec.d.ts +3 -0
  344. package/dist/types/transport/noise/WaNoiseSession.d.ts +1 -0
  345. package/dist/types/transport/proxy.d.ts +6 -0
  346. package/dist/types/transport/stream/parse.d.ts +0 -1
  347. package/dist/types/transport/types.d.ts +18 -1
  348. package/dist/types/util/bytes.d.ts +5 -0
  349. package/dist/types/util/primitives.d.ts +3 -0
  350. package/dist/util/bytes.js +55 -33
  351. package/dist/util/coercion.js +6 -14
  352. package/dist/util/primitives.js +42 -14
  353. package/package.json +27 -9
  354. package/proto/index.d.ts +1090 -1048
  355. package/proto/index.js +1 -1
  356. package/scripts/check-node-version.cjs +0 -1
  357. package/dist/crypto/core/encoding.js +0 -29
  358. package/dist/esm/crypto/core/encoding.js +0 -25
  359. package/dist/esm/util/base64.js +0 -18
  360. package/dist/esm/util/signal-address.js +0 -5
  361. package/dist/types/crypto/core/encoding.d.ts +0 -11
  362. package/dist/types/util/base64.d.ts +0 -4
  363. package/dist/types/util/signal-address.d.ts +0 -2
  364. package/dist/util/base64.js +0 -24
  365. package/dist/util/signal-address.js +0 -8
  366. /package/dist/types/transport/node/builders/{accountSync.d.ts → account-sync.d.ts} +0 -0
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SenderKeyMemoryStore = void 0;
4
+ const jid_1 = require("../../../protocol/jid");
4
5
  const coercion_1 = require("../../../util/coercion");
5
6
  const collections_1 = require("../../../util/collections");
6
- const signal_address_1 = require("../../../util/signal-address");
7
7
  const DEFAULT_SENDER_KEY_STORE_LIMITS = Object.freeze({
8
8
  senderKeys: 8192,
9
9
  senderDistributions: 8192
@@ -48,10 +48,6 @@ class SenderKeyMemoryStore {
48
48
  const record = this.senderKeys.get(this.makeKey(groupId, sender));
49
49
  return record ?? null;
50
50
  }
51
- async getDeviceSenderKeyDistribution(groupId, sender) {
52
- const record = this.senderDistributions.get(this.makeKey(groupId, sender));
53
- return record ?? null;
54
- }
55
51
  async getDeviceSenderKeyDistributions(groupId, senders) {
56
52
  return senders.map((sender) => this.senderDistributions.get(this.makeKey(groupId, sender)) ?? null);
57
53
  }
@@ -63,8 +59,10 @@ class SenderKeyMemoryStore {
63
59
  }
64
60
  async markForgetSenderKey(groupId, participants) {
65
61
  let deleted = 0;
66
- for (const participant of participants) {
67
- deleted += await this.deleteDeviceSenderKey(participant, groupId);
62
+ for (let index = 0; index < participants.length; index += 1) {
63
+ const participant = participants[index];
64
+ deleted += this.deleteMatching(this.senderKeys, participant, groupId);
65
+ deleted += this.deleteMatching(this.senderDistributions, participant, groupId);
68
66
  }
69
67
  return deleted;
70
68
  }
@@ -74,10 +72,10 @@ class SenderKeyMemoryStore {
74
72
  }
75
73
  deleteMatching(map, target, groupId) {
76
74
  let deleted = 0;
77
- const targetAddressKey = (0, signal_address_1.signalAddressKey)(target);
75
+ const targetAddressKey = (0, jid_1.signalAddressKey)(target);
78
76
  for (const [key, record] of map.entries()) {
79
77
  const sameGroup = groupId ? record.groupId === groupId : true;
80
- const sameAddress = (0, signal_address_1.signalAddressKey)(record.sender) === targetAddressKey;
78
+ const sameAddress = (0, jid_1.signalAddressKey)(record.sender) === targetAddressKey;
81
79
  if (sameGroup && sameAddress) {
82
80
  map.delete(key);
83
81
  deleted += 1;
@@ -86,7 +84,7 @@ class SenderKeyMemoryStore {
86
84
  return deleted;
87
85
  }
88
86
  makeKey(groupId, sender) {
89
- return `${groupId}|${(0, signal_address_1.signalAddressKey)(sender)}`;
87
+ return `${groupId}|${(0, jid_1.signalAddressKey)(sender)}`;
90
88
  }
91
89
  }
92
90
  exports.SenderKeyMemoryStore = SenderKeyMemoryStore;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WaSignalMemoryStore = void 0;
4
+ const jid_1 = require("../../../protocol/jid");
4
5
  const coercion_1 = require("../../../util/coercion");
5
6
  const collections_1 = require("../../../util/collections");
6
- const signal_address_1 = require("../../../util/signal-address");
7
7
  const DEFAULT_SIGNAL_STORE_LIMITS = Object.freeze({
8
8
  preKeys: 4096,
9
9
  sessions: 8192,
@@ -121,29 +121,41 @@ class WaSignalMemoryStore {
121
121
  return this.serverHasPreKeys;
122
122
  }
123
123
  async hasSession(address) {
124
- return this.signalSessions.has((0, signal_address_1.signalAddressKey)(address));
124
+ return this.signalSessions.has((0, jid_1.signalAddressKey)(address));
125
125
  }
126
126
  async hasSessions(addresses) {
127
- return addresses.map((address) => this.signalSessions.has((0, signal_address_1.signalAddressKey)(address)));
127
+ return addresses.map((address) => this.signalSessions.has((0, jid_1.signalAddressKey)(address)));
128
128
  }
129
129
  async getSession(address) {
130
- return this.signalSessions.get((0, signal_address_1.signalAddressKey)(address)) ?? null;
130
+ return this.signalSessions.get((0, jid_1.signalAddressKey)(address)) ?? null;
131
+ }
132
+ async getSessionsBatch(addresses) {
133
+ return addresses.map((address) => this.signalSessions.get((0, jid_1.signalAddressKey)(address)) ?? null);
131
134
  }
132
135
  async setSession(address, session) {
133
- (0, collections_1.setBoundedMapEntry)(this.signalSessions, (0, signal_address_1.signalAddressKey)(address), session, this.maxSessions);
136
+ (0, collections_1.setBoundedMapEntry)(this.signalSessions, (0, jid_1.signalAddressKey)(address), session, this.maxSessions);
137
+ }
138
+ async setSessionsBatch(entries) {
139
+ for (let index = 0; index < entries.length; index += 1) {
140
+ const entry = entries[index];
141
+ (0, collections_1.setBoundedMapEntry)(this.signalSessions, (0, jid_1.signalAddressKey)(entry.address), entry.session, this.maxSessions);
142
+ }
134
143
  }
135
144
  async deleteSession(address) {
136
- this.signalSessions.delete((0, signal_address_1.signalAddressKey)(address));
145
+ this.signalSessions.delete((0, jid_1.signalAddressKey)(address));
137
146
  }
138
147
  async getRemoteIdentity(address) {
139
- return this.remoteIdentities.get((0, signal_address_1.signalAddressKey)(address)) ?? null;
148
+ return this.remoteIdentities.get((0, jid_1.signalAddressKey)(address)) ?? null;
149
+ }
150
+ async getRemoteIdentities(addresses) {
151
+ return addresses.map((address) => this.remoteIdentities.get((0, jid_1.signalAddressKey)(address)) ?? null);
140
152
  }
141
153
  async setRemoteIdentity(address, identityKey) {
142
- (0, collections_1.setBoundedMapEntry)(this.remoteIdentities, (0, signal_address_1.signalAddressKey)(address), identityKey, this.maxRemoteIdentities);
154
+ (0, collections_1.setBoundedMapEntry)(this.remoteIdentities, (0, jid_1.signalAddressKey)(address), identityKey, this.maxRemoteIdentities);
143
155
  }
144
156
  async setRemoteIdentities(entries) {
145
157
  for (const entry of entries) {
146
- (0, collections_1.setBoundedMapEntry)(this.remoteIdentities, (0, signal_address_1.signalAddressKey)(entry.address), entry.identityKey, this.maxRemoteIdentities);
158
+ (0, collections_1.setBoundedMapEntry)(this.remoteIdentities, (0, jid_1.signalAddressKey)(entry.address), entry.identityKey, this.maxRemoteIdentities);
147
159
  }
148
160
  }
149
161
  async clear() {
@@ -14,6 +14,11 @@ class WaThreadMemoryStore {
14
14
  async upsert(record) {
15
15
  (0, collections_1.setBoundedMapEntry)(this.threads, record.jid, record, this.maxThreads);
16
16
  }
17
+ async upsertBatch(records) {
18
+ for (const record of records) {
19
+ (0, collections_1.setBoundedMapEntry)(this.threads, record.jid, record, this.maxThreads);
20
+ }
21
+ }
17
22
  async getByJid(jid) {
18
23
  return this.threads.get(jid) ?? null;
19
24
  }
@@ -7,6 +7,16 @@ const utils_1 = require("../../../appstate/utils");
7
7
  const BaseSqliteStore_1 = require("../../providers/sqlite/BaseSqliteStore");
8
8
  const bytes_1 = require("../../../util/bytes");
9
9
  const coercion_1 = require("../../../util/coercion");
10
+ function repeatSqlToken(token, count, separator) {
11
+ if (count <= 1) {
12
+ return token;
13
+ }
14
+ let out = token;
15
+ for (let index = 1; index < count; index += 1) {
16
+ out += separator + token;
17
+ }
18
+ return out;
19
+ }
10
20
  class WaAppStateSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
11
21
  constructor(options) {
12
22
  super(options, ['appState']);
@@ -88,6 +98,25 @@ class WaAppStateSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
88
98
  }
89
99
  return (0, coercion_1.asBytes)(row.key_data, 'appstate_sync_keys.key_data');
90
100
  }
101
+ async getSyncKeyDataBatch(keyIds) {
102
+ if (keyIds.length === 0) {
103
+ return [];
104
+ }
105
+ const db = await this.getConnection();
106
+ const uniqueKeyIds = [
107
+ ...new Map(keyIds.map((keyId) => [(0, bytes_1.bytesToHex)(keyId), keyId])).values()
108
+ ];
109
+ const placeholders = repeatSqlToken('?', uniqueKeyIds.length, ', ');
110
+ const params = [this.options.sessionId, ...uniqueKeyIds];
111
+ const rows = db.all(`SELECT key_id, key_data
112
+ FROM appstate_sync_keys
113
+ WHERE session_id = ? AND key_id IN (${placeholders})`, params);
114
+ const byKeyHex = new Map();
115
+ for (const row of rows) {
116
+ byKeyHex.set((0, bytes_1.bytesToHex)((0, coercion_1.asBytes)(row.key_id, 'appstate_sync_keys.key_id')), (0, coercion_1.asBytes)(row.key_data, 'appstate_sync_keys.key_data'));
117
+ }
118
+ return keyIds.map((keyId) => byKeyHex.get((0, bytes_1.bytesToHex)(keyId)) ?? null);
119
+ }
91
120
  async getActiveSyncKey() {
92
121
  const db = await this.getConnection();
93
122
  const rows = db.all(`SELECT key_id, key_data, timestamp, fingerprint
@@ -112,6 +141,7 @@ class WaAppStateSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
112
141
  WHERE session_id = ? AND collection = ?`, [this.options.sessionId, collection]);
113
142
  if (!versionRow) {
114
143
  return {
144
+ initialized: false,
115
145
  version: 0,
116
146
  hash: constants_1.APP_STATE_EMPTY_LT_HASH,
117
147
  indexValueMap: new Map()
@@ -125,11 +155,62 @@ class WaAppStateSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
125
155
  indexValueMap.set((0, coercion_1.asString)(row.index_mac_hex, 'appstate_collection_index_values.index_mac_hex'), (0, coercion_1.asBytes)(row.value_mac, 'appstate_collection_index_values.value_mac'));
126
156
  }
127
157
  return {
158
+ initialized: true,
128
159
  version: (0, coercion_1.asNumber)(versionRow.version, 'appstate_collection_versions.version'),
129
160
  hash: (0, coercion_1.asBytes)(versionRow.hash, 'appstate_collection_versions.hash'),
130
161
  indexValueMap
131
162
  };
132
163
  }
164
+ async getCollectionStates(collections) {
165
+ if (collections.length === 0) {
166
+ return [];
167
+ }
168
+ const db = await this.getConnection();
169
+ const uniqueCollections = [...new Set(collections)];
170
+ const placeholders = repeatSqlToken('?', uniqueCollections.length, ', ');
171
+ const params = [this.options.sessionId, ...uniqueCollections];
172
+ const versionRows = db.all(`SELECT collection, version, hash
173
+ FROM appstate_collection_versions
174
+ WHERE session_id = ? AND collection IN (${placeholders})`, params);
175
+ const valueRows = db.all(`SELECT collection, index_mac_hex, value_mac
176
+ FROM appstate_collection_index_values
177
+ WHERE session_id = ? AND collection IN (${placeholders})`, params);
178
+ const versionsByCollection = new Map();
179
+ for (const row of versionRows) {
180
+ const collection = (0, coercion_1.asString)(row.collection, 'appstate_collection_versions.collection');
181
+ versionsByCollection.set(collection, {
182
+ version: (0, coercion_1.asNumber)(row.version, 'appstate_collection_versions.version'),
183
+ hash: (0, coercion_1.asBytes)(row.hash, 'appstate_collection_versions.hash')
184
+ });
185
+ }
186
+ const indexValueMaps = new Map();
187
+ for (const row of valueRows) {
188
+ const collection = (0, coercion_1.asString)(row.collection, 'appstate_collection_index_values.collection');
189
+ const map = indexValueMaps.get(collection);
190
+ const targetMap = map ?? new Map();
191
+ targetMap.set((0, coercion_1.asString)(row.index_mac_hex, 'appstate_collection_index_values.index_mac_hex'), (0, coercion_1.asBytes)(row.value_mac, 'appstate_collection_index_values.value_mac'));
192
+ if (!map) {
193
+ indexValueMaps.set(collection, targetMap);
194
+ }
195
+ }
196
+ return collections.map((collection) => {
197
+ const version = versionsByCollection.get(collection);
198
+ if (!version) {
199
+ return {
200
+ initialized: false,
201
+ version: 0,
202
+ hash: constants_1.APP_STATE_EMPTY_LT_HASH,
203
+ indexValueMap: new Map()
204
+ };
205
+ }
206
+ return {
207
+ initialized: true,
208
+ version: version.version,
209
+ hash: version.hash,
210
+ indexValueMap: indexValueMaps.get(collection) ?? new Map()
211
+ };
212
+ });
213
+ }
133
214
  async setCollectionStates(updates) {
134
215
  if (updates.length === 0) {
135
216
  return;
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.openSqliteConnection = openSqliteConnection;
37
+ const table_names_1 = require("../../providers/sqlite/table-names");
37
38
  const primitives_1 = require("../../../util/primitives");
38
39
  const runtime_1 = require("../../../util/runtime");
39
40
  const BETTER_SQLITE3_MODULE = 'better-sqlite3';
@@ -87,21 +88,22 @@ function statementFor(db, sql) {
87
88
  }
88
89
  return statement;
89
90
  }
90
- function wrapConnection(db, driver, onClose) {
91
+ function wrapConnection(db, driver, resolveSql, onClose) {
91
92
  const statementCache = new Map();
92
93
  const cachedStatementFor = (sql) => {
93
- const cached = statementCache.get(sql);
94
+ const resolvedSql = resolveSql(sql);
95
+ const cached = statementCache.get(resolvedSql);
94
96
  if (cached) {
95
97
  return cached;
96
98
  }
97
- const statement = statementFor(db, sql);
98
- statementCache.set(sql, statement);
99
+ const statement = statementFor(db, resolvedSql);
100
+ statementCache.set(resolvedSql, statement);
99
101
  return statement;
100
102
  };
101
103
  return {
102
104
  driver,
103
105
  exec(sql) {
104
- db.exec(sql);
106
+ db.exec(resolveSql(sql));
105
107
  },
106
108
  run(sql, params) {
107
109
  const statement = cachedStatementFor(sql);
@@ -195,7 +197,7 @@ function closeDatabaseSafely(db) {
195
197
  return;
196
198
  }
197
199
  }
198
- async function openBetterSqlite(options, onClose) {
200
+ async function openBetterSqlite(options, resolveSql, onClose) {
199
201
  let loaded;
200
202
  try {
201
203
  loaded = await Promise.resolve(`${BETTER_SQLITE3_MODULE}`).then(s => __importStar(require(s)));
@@ -212,9 +214,9 @@ async function openBetterSqlite(options, onClose) {
212
214
  closeDatabaseSafely(db);
213
215
  throw error;
214
216
  }
215
- return wrapConnection(db, 'better-sqlite3', onClose);
217
+ return wrapConnection(db, 'better-sqlite3', resolveSql, onClose);
216
218
  }
217
- async function openBunSqlite(options, onClose) {
219
+ async function openBunSqlite(options, resolveSql, onClose) {
218
220
  let loaded;
219
221
  try {
220
222
  loaded = await Promise.resolve(`${BUN_SQLITE_MODULE}`).then(s => __importStar(require(s)));
@@ -237,7 +239,7 @@ async function openBunSqlite(options, onClose) {
237
239
  closeDatabaseSafely(db);
238
240
  throw error;
239
241
  }
240
- return wrapConnection(db, 'bun', onClose);
242
+ return wrapConnection(db, 'bun', resolveSql, onClose);
241
243
  }
242
244
  function resolveDriver(requested) {
243
245
  if (requested && requested !== 'auto') {
@@ -247,15 +249,18 @@ function resolveDriver(requested) {
247
249
  }
248
250
  async function openSqliteConnection(options) {
249
251
  const driver = resolveDriver(options.driver);
252
+ const resolvedTableNames = (0, table_names_1.resolveSqliteTableNames)(options.tableNames);
253
+ const resolveSql = (0, table_names_1.createSqliteTableNameSqlResolver)(resolvedTableNames);
250
254
  const normalizedOptions = {
251
255
  ...options,
252
256
  driver,
253
- pragmas: mergePragmas(options.pragmas)
257
+ pragmas: mergePragmas(options.pragmas),
258
+ tableNames: resolvedTableNames
254
259
  };
255
260
  const cacheKey = `${driver}|${options.path}|${Object.entries(normalizedOptions.pragmas ?? {})
256
261
  .sort(([left], [right]) => left.localeCompare(right))
257
262
  .map(([key, value]) => `${key}=${String(value)}`)
258
- .join(';')}`;
263
+ .join(';')}|${(0, table_names_1.serializeSqliteTableNames)(resolvedTableNames)}`;
259
264
  const cached = SQLITE_CONNECTION_CACHE.get(cacheKey);
260
265
  if (cached) {
261
266
  return cached;
@@ -264,8 +269,8 @@ async function openSqliteConnection(options) {
264
269
  SQLITE_CONNECTION_CACHE.delete(cacheKey);
265
270
  };
266
271
  const created = driver === 'bun'
267
- ? openBunSqlite(normalizedOptions, onClose)
268
- : openBetterSqlite(normalizedOptions, onClose);
272
+ ? openBunSqlite(normalizedOptions, resolveSql, onClose)
273
+ : openBetterSqlite(normalizedOptions, resolveSql, onClose);
269
274
  const guarded = created.catch((error) => {
270
275
  SQLITE_CONNECTION_CACHE.delete(cacheKey);
271
276
  throw error;
@@ -19,6 +19,37 @@ class WaContactSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
19
19
  }
20
20
  async upsert(record) {
21
21
  const db = await this.getConnection();
22
+ this.upsertContactRow(db, record);
23
+ }
24
+ async upsertBatch(records) {
25
+ if (records.length === 0) {
26
+ return;
27
+ }
28
+ await this.withTransaction((db) => {
29
+ for (const record of records) {
30
+ this.upsertContactRow(db, record);
31
+ }
32
+ });
33
+ }
34
+ async getByJid(jid) {
35
+ const db = await this.getConnection();
36
+ const row = db.get(`SELECT jid, display_name, push_name, lid, phone_number, last_updated_ms
37
+ FROM mailbox_contacts
38
+ WHERE session_id = ? AND jid = ?`, [this.options.sessionId, jid]);
39
+ return row ? decodeContactRow(row) : null;
40
+ }
41
+ async deleteByJid(jid) {
42
+ const db = await this.getConnection();
43
+ db.run(`DELETE FROM mailbox_contacts
44
+ WHERE session_id = ? AND jid = ?`, [this.options.sessionId, jid]);
45
+ const row = db.get('SELECT changes() AS total', []);
46
+ return row ? Number(row.total) : 0;
47
+ }
48
+ async clear() {
49
+ const db = await this.getConnection();
50
+ db.run('DELETE FROM mailbox_contacts WHERE session_id = ?', [this.options.sessionId]);
51
+ }
52
+ upsertContactRow(db, record) {
22
53
  db.run(`INSERT INTO mailbox_contacts (
23
54
  session_id,
24
55
  jid,
@@ -43,23 +74,5 @@ class WaContactSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
43
74
  record.lastUpdatedMs
44
75
  ]);
45
76
  }
46
- async getByJid(jid) {
47
- const db = await this.getConnection();
48
- const row = db.get(`SELECT jid, display_name, push_name, lid, phone_number, last_updated_ms
49
- FROM mailbox_contacts
50
- WHERE session_id = ? AND jid = ?`, [this.options.sessionId, jid]);
51
- return row ? decodeContactRow(row) : null;
52
- }
53
- async deleteByJid(jid) {
54
- const db = await this.getConnection();
55
- db.run(`DELETE FROM mailbox_contacts
56
- WHERE session_id = ? AND jid = ?`, [this.options.sessionId, jid]);
57
- const row = db.get('SELECT changes() AS total', []);
58
- return row ? Number(row.total) : 0;
59
- }
60
- async clear() {
61
- const db = await this.getConnection();
62
- db.run('DELETE FROM mailbox_contacts WHERE session_id = ?', [this.options.sessionId]);
63
- }
64
77
  }
65
78
  exports.WaContactSqliteStore = WaContactSqliteStore;
@@ -13,13 +13,6 @@ class WaDeviceListSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
13
13
  this.ttlMs = ttlMs;
14
14
  this.batchSize = (0, coercion_1.resolvePositive)(batchSize, DEFAULTS.batchSize, 'deviceList.sqlite.batchSize');
15
15
  }
16
- getTtlMs() {
17
- return this.ttlMs;
18
- }
19
- async upsertUserDevices(snapshot) {
20
- const db = await this.getConnection();
21
- this.upsertUserDevicesRow(db, snapshot);
22
- }
23
16
  async upsertUserDevicesBatch(snapshots) {
24
17
  if (snapshots.length === 0) {
25
18
  return;
@@ -30,26 +23,6 @@ class WaDeviceListSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
30
23
  }
31
24
  });
32
25
  }
33
- async getUserDevices(userJid, nowMs = Date.now()) {
34
- const db = await this.getConnection();
35
- const row = db.get(`SELECT user_jid, device_jids_json, updated_at_ms, expires_at_ms
36
- FROM device_list_cache
37
- WHERE session_id = ? AND user_jid = ?`, [this.options.sessionId, userJid]);
38
- if (!row) {
39
- return null;
40
- }
41
- const expiresAtMs = (0, coercion_1.asNumber)(row.expires_at_ms, 'device_list_cache.expires_at_ms');
42
- if (expiresAtMs <= nowMs) {
43
- db.run(`DELETE FROM device_list_cache
44
- WHERE session_id = ? AND user_jid = ?`, [this.options.sessionId, userJid]);
45
- return null;
46
- }
47
- return {
48
- userJid: (0, coercion_1.asString)(row.user_jid, 'device_list_cache.user_jid'),
49
- deviceJids: decodeDeviceJids(row.device_jids_json),
50
- updatedAtMs: (0, coercion_1.asNumber)(row.updated_at_ms, 'device_list_cache.updated_at_ms')
51
- };
52
- }
53
26
  async getUserDevicesBatch(userJids, nowMs = Date.now()) {
54
27
  if (userJids.length === 0) {
55
28
  return [];
@@ -61,7 +34,7 @@ class WaDeviceListSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
61
34
  for (let start = 0; start < uniqueUserJids.length; start += this.batchSize) {
62
35
  const end = Math.min(start + this.batchSize, uniqueUserJids.length);
63
36
  const batchLength = end - start;
64
- const placeholders = new Array(batchLength).fill('?').join(', ');
37
+ const placeholders = '?, '.repeat(batchLength).slice(0, -2);
65
38
  const params = [this.options.sessionId];
66
39
  for (let index = start; index < end; index += 1) {
67
40
  params.push(uniqueUserJids[index]);
@@ -133,7 +106,7 @@ class WaDeviceListSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
133
106
  for (let start = 0; start < userJids.length; start += this.batchSize) {
134
107
  const end = Math.min(start + this.batchSize, userJids.length);
135
108
  const batchLength = end - start;
136
- const placeholders = new Array(batchLength).fill('?').join(', ');
109
+ const placeholders = '?, '.repeat(batchLength).slice(0, -2);
137
110
  const params = [this.options.sessionId];
138
111
  for (let index = start; index < end; index += 1) {
139
112
  params.push(userJids[index]);
@@ -150,10 +123,9 @@ function decodeDeviceJids(raw) {
150
123
  if (!Array.isArray(parsed)) {
151
124
  throw new Error('device_list_cache.device_jids_json must be an array');
152
125
  }
153
- return parsed.filter((entry) => {
154
- if (typeof entry !== 'string') {
155
- throw new Error('device_list_cache.device_jids_json entry must be string');
156
- }
157
- return true;
158
- });
126
+ const deviceJids = new Array(parsed.length);
127
+ for (let index = 0; index < parsed.length; index += 1) {
128
+ deviceJids[index] = (0, coercion_1.asString)(parsed[index], 'device_list_cache.device_jids_json entry');
129
+ }
130
+ return deviceJids;
159
131
  }
@@ -23,38 +23,17 @@ class WaMessageSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
23
23
  }
24
24
  async upsert(record) {
25
25
  const db = await this.getConnection();
26
- db.run(`INSERT INTO mailbox_messages (
27
- session_id,
28
- message_id,
29
- thread_jid,
30
- sender_jid,
31
- participant_jid,
32
- from_me,
33
- timestamp_ms,
34
- enc_type,
35
- plaintext,
36
- message_bytes
37
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
38
- ON CONFLICT(session_id, message_id) DO UPDATE SET
39
- thread_jid=excluded.thread_jid,
40
- sender_jid=excluded.sender_jid,
41
- participant_jid=excluded.participant_jid,
42
- from_me=excluded.from_me,
43
- timestamp_ms=excluded.timestamp_ms,
44
- enc_type=excluded.enc_type,
45
- plaintext=excluded.plaintext,
46
- message_bytes=excluded.message_bytes`, [
47
- this.options.sessionId,
48
- record.id,
49
- record.threadJid,
50
- record.senderJid ?? null,
51
- record.participantJid ?? null,
52
- record.fromMe ? 1 : 0,
53
- record.timestampMs ?? null,
54
- record.encType ?? null,
55
- record.plaintext ?? null,
56
- record.messageBytes ?? null
57
- ]);
26
+ this.upsertMessageRow(db, record);
27
+ }
28
+ async upsertBatch(records) {
29
+ if (records.length === 0) {
30
+ return;
31
+ }
32
+ await this.withTransaction((db) => {
33
+ for (const record of records) {
34
+ this.upsertMessageRow(db, record);
35
+ }
36
+ });
58
37
  }
59
38
  async getById(id) {
60
39
  const db = await this.getConnection();
@@ -119,5 +98,39 @@ class WaMessageSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
119
98
  const db = await this.getConnection();
120
99
  db.run('DELETE FROM mailbox_messages WHERE session_id = ?', [this.options.sessionId]);
121
100
  }
101
+ upsertMessageRow(db, record) {
102
+ db.run(`INSERT INTO mailbox_messages (
103
+ session_id,
104
+ message_id,
105
+ thread_jid,
106
+ sender_jid,
107
+ participant_jid,
108
+ from_me,
109
+ timestamp_ms,
110
+ enc_type,
111
+ plaintext,
112
+ message_bytes
113
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
114
+ ON CONFLICT(session_id, message_id) DO UPDATE SET
115
+ thread_jid=excluded.thread_jid,
116
+ sender_jid=excluded.sender_jid,
117
+ participant_jid=excluded.participant_jid,
118
+ from_me=excluded.from_me,
119
+ timestamp_ms=excluded.timestamp_ms,
120
+ enc_type=excluded.enc_type,
121
+ plaintext=excluded.plaintext,
122
+ message_bytes=excluded.message_bytes`, [
123
+ this.options.sessionId,
124
+ record.id,
125
+ record.threadJid,
126
+ record.senderJid ?? null,
127
+ record.participantJid ?? null,
128
+ record.fromMe ? 1 : 0,
129
+ record.timestampMs ?? null,
130
+ record.encType ?? null,
131
+ record.plaintext ?? null,
132
+ record.messageBytes ?? null
133
+ ]);
134
+ }
122
135
  }
123
136
  exports.WaMessageSqliteStore = WaMessageSqliteStore;
@@ -296,7 +296,7 @@ function isMigrationAlreadyAppliedRace(error) {
296
296
  if (!(error instanceof Error)) {
297
297
  return false;
298
298
  }
299
- return error.message.includes('UNIQUE constraint failed: wa_migrations.id');
299
+ return /UNIQUE constraint failed: [A-Za-z_][A-Za-z0-9_]*\.id/.test(error.message);
300
300
  }
301
301
  function ensureMigrationTable(db) {
302
302
  db.exec(`
@@ -9,9 +9,6 @@ class WaParticipantsSqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
9
9
  super(options, ['participants']);
10
10
  this.ttlMs = ttlMs;
11
11
  }
12
- getTtlMs() {
13
- return this.ttlMs;
14
- }
15
12
  async upsertGroupParticipants(snapshot) {
16
13
  const db = await this.getConnection();
17
14
  db.run(`INSERT INTO group_participants_cache (
@@ -80,10 +77,5 @@ function decodeParticipants(raw) {
80
77
  if (!Array.isArray(parsed)) {
81
78
  throw new Error('group_participants_cache.participants_json must be an array');
82
79
  }
83
- return parsed.filter((entry) => {
84
- if (typeof entry !== 'string') {
85
- throw new Error('group_participants_cache.participants_json entry must be string');
86
- }
87
- return true;
88
- });
80
+ return parsed.map((entry) => (0, coercion_1.asString)(entry, 'group_participants_cache.participants_json entry'));
89
81
  }
@@ -116,12 +116,18 @@ class WaRetrySqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
116
116
  }
117
117
  async cleanupExpired(nowMs) {
118
118
  return this.withTransaction(async (db) => {
119
- const outboundCount = this.countRows(db, 'retry_outbound_messages', nowMs);
120
- const inboundCount = this.countRows(db, 'retry_inbound_counters', nowMs);
121
119
  db.run(`DELETE FROM retry_outbound_messages
122
120
  WHERE session_id = ? AND expires_at_ms <= ?`, [this.options.sessionId, nowMs]);
121
+ const outboundCountRow = db.get('SELECT changes() AS total', []);
122
+ const outboundCount = outboundCountRow
123
+ ? (0, coercion_1.asNumber)(outboundCountRow.total, 'retry_outbound_messages.changes')
124
+ : 0;
123
125
  db.run(`DELETE FROM retry_inbound_counters
124
126
  WHERE session_id = ? AND expires_at_ms <= ?`, [this.options.sessionId, nowMs]);
127
+ const inboundCountRow = db.get('SELECT changes() AS total', []);
128
+ const inboundCount = inboundCountRow
129
+ ? (0, coercion_1.asNumber)(inboundCountRow.total, 'retry_inbound_counters.changes')
130
+ : 0;
125
131
  return outboundCount + inboundCount;
126
132
  });
127
133
  }
@@ -135,14 +141,5 @@ class WaRetrySqliteStore extends BaseSqliteStore_1.BaseSqliteStore {
135
141
  ]);
136
142
  });
137
143
  }
138
- countRows(db, table, nowMs) {
139
- const row = db.get(`SELECT COUNT(*) AS total
140
- FROM ${table}
141
- WHERE session_id = ? AND expires_at_ms <= ?`, [this.options.sessionId, nowMs]);
142
- if (!row) {
143
- return 0;
144
- }
145
- return (0, coercion_1.asNumber)(row.total, `${table}.count`);
146
- }
147
144
  }
148
145
  exports.WaRetrySqliteStore = WaRetrySqliteStore;