@hansaka02/baileys 7.3.2 → 7.3.6

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 (210) hide show
  1. package/lib/Defaults/baileys-version.json +2 -2
  2. package/lib/Defaults/connection.js +51 -0
  3. package/lib/Defaults/constants.js +74 -0
  4. package/lib/Defaults/history.js +19 -0
  5. package/lib/Defaults/index.js +36 -142
  6. package/lib/Defaults/media.js +48 -0
  7. package/lib/Defaults/prefix.js +18 -0
  8. package/lib/Signal/Group/group-session-builder.js +10 -42
  9. package/lib/Signal/Group/group_cipher.js +9 -6
  10. package/lib/Signal/Group/index.js +39 -53
  11. package/lib/Signal/Group/keyhelper.js +8 -41
  12. package/lib/Signal/Group/sender-chain-key.js +5 -18
  13. package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
  14. package/lib/Signal/Group/sender-key-message.js +12 -8
  15. package/lib/Signal/Group/sender-key-record.js +7 -16
  16. package/lib/Signal/Group/sender-key-state.js +15 -61
  17. package/lib/Signal/Group/sender-message-key.js +2 -2
  18. package/lib/Signal/libsignal.js +237 -177
  19. package/lib/Signal/lid-mapping.js +128 -71
  20. package/lib/Socket/Client/types.js +2 -2
  21. package/lib/Socket/Client/websocket.js +25 -16
  22. package/lib/Socket/business.js +46 -33
  23. package/lib/Socket/chats.js +286 -170
  24. package/lib/Socket/community.js +215 -77
  25. package/lib/Socket/groups.js +77 -61
  26. package/lib/Socket/index.js +4 -4
  27. package/lib/Socket/messages-recv.js +629 -457
  28. package/lib/Socket/messages-send.js +645 -656
  29. package/lib/Socket/mex.js +61 -0
  30. package/lib/Socket/newsletter.js +166 -245
  31. package/lib/Socket/socket.js +396 -170
  32. package/lib/Store/index.js +27 -11
  33. package/lib/Store/make-cache-manager-store.js +14 -15
  34. package/lib/Store/make-in-memory-store.js +28 -24
  35. package/lib/Types/LabelAssociation.js +2 -2
  36. package/lib/Types/Message.js +6 -6
  37. package/lib/Types/MexUpdates.js +5 -5
  38. package/lib/Types/Newsletter.js +32 -25
  39. package/lib/Types/State.js +4 -4
  40. package/lib/Types/index.js +28 -12
  41. package/lib/Utils/auth-utils.js +212 -375
  42. package/lib/Utils/baileys-event-stream.js +68 -69
  43. package/lib/Utils/browser-utils.js +43 -0
  44. package/lib/Utils/business.js +63 -53
  45. package/lib/Utils/chat-utils.js +241 -106
  46. package/lib/Utils/crypto.js +25 -45
  47. package/lib/Utils/decode-wa-message.js +361 -311
  48. package/lib/Utils/event-buffer.js +97 -42
  49. package/lib/Utils/generics.js +90 -207
  50. package/lib/Utils/history.js +29 -27
  51. package/lib/Utils/index.js +28 -14
  52. package/lib/Utils/link-preview.js +24 -62
  53. package/lib/Utils/logger.js +5 -5
  54. package/lib/Utils/lt-hash.js +29 -23
  55. package/lib/Utils/make-mutex.js +26 -28
  56. package/lib/Utils/message-retry-manager.js +55 -7
  57. package/lib/Utils/messages-media.js +434 -247
  58. package/lib/Utils/messages.js +963 -917
  59. package/lib/Utils/noise-handler.js +60 -20
  60. package/lib/Utils/pre-key-manager.js +126 -0
  61. package/lib/Utils/process-message.js +216 -141
  62. package/lib/Utils/signal.js +75 -37
  63. package/lib/Utils/use-multi-file-auth-state.js +18 -22
  64. package/lib/Utils/validate-connection.js +96 -66
  65. package/lib/WABinary/constants.js +1268 -1268
  66. package/lib/WABinary/decode.js +62 -34
  67. package/lib/WABinary/encode.js +57 -36
  68. package/lib/WABinary/generic-utils.js +4 -4
  69. package/lib/WABinary/index.js +27 -11
  70. package/lib/WABinary/jid-utils.js +58 -11
  71. package/lib/WAM/constants.js +19064 -11563
  72. package/lib/WAM/encode.js +71 -14
  73. package/lib/WAM/index.js +27 -11
  74. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +20 -16
  75. package/lib/WAUSync/Protocols/USyncContactProtocol.js +2 -2
  76. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +7 -4
  77. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +2 -2
  78. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -2
  79. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -2
  80. package/lib/WAUSync/Protocols/index.js +27 -11
  81. package/lib/WAUSync/USyncQuery.js +51 -28
  82. package/lib/WAUSync/index.js +27 -11
  83. package/lib/index.js +60 -31
  84. package/package.json +12 -17
  85. package/WAProto/AICommon/AICommon.d.ts +0 -11702
  86. package/WAProto/Adv/Adv.d.ts +0 -643
  87. package/WAProto/BotMetadata/BotMetadata.d.ts +0 -5654
  88. package/WAProto/Cert/Cert.d.ts +0 -613
  89. package/WAProto/ChatLockSettings/ChatLockSettings.d.ts +0 -476
  90. package/WAProto/CompanionReg/CompanionReg.d.ts +0 -1361
  91. package/WAProto/DeviceCapabilities/DeviceCapabilities.d.ts +0 -577
  92. package/WAProto/E2E/E2E.d.ts +0 -41724
  93. package/WAProto/Ephemeral/Ephemeral.d.ts +0 -114
  94. package/WAProto/HistorySync/HistorySync.d.ts +0 -51700
  95. package/WAProto/LidMigrationSyncPayload/LidMigrationSyncPayload.d.ts +0 -229
  96. package/WAProto/MdStorageChatRowOpaqueData/MdStorageChatRowOpaqueData.d.ts +0 -583
  97. package/WAProto/MdStorageMsgRowOpaqueData/MdStorageMsgRowOpaqueData.d.ts +0 -42897
  98. package/WAProto/MmsRetry/MmsRetry.d.ts +0 -243
  99. package/WAProto/Protocol/Protocol.d.ts +0 -270
  100. package/WAProto/Reporting/Reporting.d.ts +0 -371
  101. package/WAProto/ServerSync/ServerSync.d.ts +0 -1285
  102. package/WAProto/SignalLocalStorageProtocol/SignalLocalStorageProtocol.d.ts +0 -1868
  103. package/WAProto/SignalWhisperTextProtocol/SignalWhisperTextProtocol.d.ts +0 -767
  104. package/WAProto/StatusAttributions/StatusAttributions.d.ts +0 -1027
  105. package/WAProto/SyncAction/SyncAction.d.ts +0 -11193
  106. package/WAProto/UserPassword/UserPassword.d.ts +0 -363
  107. package/WAProto/VnameCert/VnameCert.d.ts +0 -821
  108. package/WAProto/Wa6/Wa6.d.ts +0 -2128
  109. package/WAProto/Web/Web.d.ts +0 -46383
  110. package/WAProto/index.d.ts +0 -55
  111. package/lib/Defaults/index.d.ts +0 -77
  112. package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
  113. package/lib/Signal/Group/group-session-builder.d.ts +0 -17
  114. package/lib/Signal/Group/group_cipher.d.ts +0 -19
  115. package/lib/Signal/Group/index.d.ts +0 -11
  116. package/lib/Signal/Group/keyhelper.d.ts +0 -16
  117. package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
  118. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
  119. package/lib/Signal/Group/sender-key-message.d.ts +0 -19
  120. package/lib/Signal/Group/sender-key-name.d.ts +0 -19
  121. package/lib/Signal/Group/sender-key-record.d.ts +0 -32
  122. package/lib/Signal/Group/sender-key-state.d.ts +0 -44
  123. package/lib/Signal/Group/sender-message-key.d.ts +0 -11
  124. package/lib/Signal/libsignal.d.ts +0 -8
  125. package/lib/Signal/lid-mapping.d.ts +0 -28
  126. package/lib/Socket/Client/index.d.ts +0 -2
  127. package/lib/Socket/Client/types.d.ts +0 -16
  128. package/lib/Socket/Client/websocket.d.ts +0 -13
  129. package/lib/Socket/business.d.ts +0 -187
  130. package/lib/Socket/chats.d.ts +0 -97
  131. package/lib/Socket/community.d.ts +0 -129
  132. package/lib/Socket/groups.d.ts +0 -129
  133. package/lib/Socket/index.d.ts +0 -191
  134. package/lib/Socket/messages-recv.d.ts +0 -174
  135. package/lib/Socket/messages-send.d.ts +0 -165
  136. package/lib/Socket/newsletter.d.ts +0 -145
  137. package/lib/Socket/socket.d.ts +0 -45
  138. package/lib/Socket/usync.d.ts +0 -37
  139. package/lib/Socket/usync.js +0 -83
  140. package/lib/Store/index.d.ts +0 -4
  141. package/lib/Store/make-cache-manager-store.d.ts +0 -14
  142. package/lib/Store/make-in-memory-store.d.ts +0 -123
  143. package/lib/Store/make-ordered-dictionary.d.ts +0 -12
  144. package/lib/Store/object-repository.d.ts +0 -10
  145. package/lib/Types/Auth.d.ts +0 -121
  146. package/lib/Types/Bussiness.d.ts +0 -28
  147. package/lib/Types/Call.d.ts +0 -14
  148. package/lib/Types/Chat.d.ts +0 -143
  149. package/lib/Types/Contact.d.ts +0 -23
  150. package/lib/Types/Events.d.ts +0 -226
  151. package/lib/Types/GroupMetadata.d.ts +0 -66
  152. package/lib/Types/Label.d.ts +0 -48
  153. package/lib/Types/LabelAssociation.d.ts +0 -35
  154. package/lib/Types/Message.d.ts +0 -484
  155. package/lib/Types/MexUpdates.d.ts +0 -9
  156. package/lib/Types/Newsletter.d.ts +0 -109
  157. package/lib/Types/Product.d.ts +0 -92
  158. package/lib/Types/Signal.d.ts +0 -98
  159. package/lib/Types/Socket.d.ts +0 -141
  160. package/lib/Types/State.d.ts +0 -41
  161. package/lib/Types/USync.d.ts +0 -26
  162. package/lib/Types/index.d.ts +0 -80
  163. package/lib/Utils/auth-utils.d.ts +0 -21
  164. package/lib/Utils/baileys-event-stream.d.ts +0 -18
  165. package/lib/Utils/business.d.ts +0 -29
  166. package/lib/Utils/chat-utils.d.ts +0 -82
  167. package/lib/Utils/crypto.d.ts +0 -56
  168. package/lib/Utils/decode-wa-message.d.ts +0 -53
  169. package/lib/Utils/event-buffer.d.ts +0 -39
  170. package/lib/Utils/generics.d.ts +0 -117
  171. package/lib/Utils/history.d.ts +0 -23
  172. package/lib/Utils/index.d.ts +0 -20
  173. package/lib/Utils/link-preview.d.ts +0 -23
  174. package/lib/Utils/logger.d.ts +0 -13
  175. package/lib/Utils/lt-hash.d.ts +0 -14
  176. package/lib/Utils/make-mutex.d.ts +0 -9
  177. package/lib/Utils/message-retry-manager.d.ts +0 -88
  178. package/lib/Utils/messages-media.d.ts +0 -135
  179. package/lib/Utils/messages.d.ts +0 -105
  180. package/lib/Utils/noise-handler.d.ts +0 -20
  181. package/lib/Utils/process-message.d.ts +0 -49
  182. package/lib/Utils/signal.d.ts +0 -42
  183. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  184. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  185. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  186. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  187. package/lib/Utils/use-single-file-auth-state.js +0 -80
  188. package/lib/Utils/validate-connection.d.ts +0 -13
  189. package/lib/WABinary/constants.d.ts +0 -30
  190. package/lib/WABinary/decode.d.ts +0 -9
  191. package/lib/WABinary/encode.d.ts +0 -3
  192. package/lib/WABinary/generic-utils.d.ts +0 -28
  193. package/lib/WABinary/index.d.ts +0 -5
  194. package/lib/WABinary/jid-utils.d.ts +0 -58
  195. package/lib/WABinary/types.d.ts +0 -22
  196. package/lib/WAM/BinaryInfo.d.ts +0 -16
  197. package/lib/WAM/constants.d.ts +0 -47
  198. package/lib/WAM/encode.d.ts +0 -3
  199. package/lib/WAM/index.d.ts +0 -3
  200. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +0 -28
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
  202. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -26
  203. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -14
  204. package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +0 -10
  205. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -14
  206. package/lib/WAUSync/Protocols/index.d.ts +0 -6
  207. package/lib/WAUSync/USyncQuery.d.ts +0 -31
  208. package/lib/WAUSync/USyncUser.d.ts +0 -12
  209. package/lib/WAUSync/index.d.ts +0 -3
  210. package/lib/index.d.ts +0 -13
@@ -2,47 +2,54 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true })
4
4
 
5
- const lru_cache_1 = require("lru-cache")
6
- const WABinary_1 = require("../WABinary")
5
+ const { LRUCache } = require("lru-cache")
6
+ const {
7
+ isHostedPnUser,
8
+ isLidUser,
9
+ isPnUser,
10
+ jidDecode,
11
+ jidNormalizedUser,
12
+ WAJIDDomains
13
+ } = require("../WABinary")
7
14
 
8
15
  class LIDMappingStore {
9
- constructor(keys, onWhatsAppFunc, logger) {
10
- this.mappingCache = new lru_cache_1.LRUCache({
11
- ttl: 7 * 24 * 60 * 60 * 1000, // 7 days
16
+ constructor(keys, logger, pnToLIDFunc) {
17
+ this.mappingCache = new LRUCache({
18
+ ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
12
19
  ttlAutopurge: true,
13
20
  updateAgeOnGet: true
14
21
  })
15
- this.keys = keys
16
- this.logger = logger
17
- this.onWhatsAppFunc = onWhatsAppFunc // needed to get LID from PN if not found
18
- }
19
- /**
22
+ this.keys = keys
23
+ this.pnToLIDFunc = pnToLIDFunc
24
+ this.logger = logger
25
+ }
26
+
27
+ /**
20
28
  * Store LID-PN mapping - USER LEVEL
21
29
  */
22
30
  async storeLIDPNMappings(pairs) {
23
31
  // Validate inputs
24
32
  const pairMap = {}
25
- const logger = this.logger
26
33
 
27
34
  for (const { lid, pn } of pairs) {
28
- if (!((WABinary_1.isLidUser(lid) && WABinary_1.isJidUser(pn)) || (WABinary_1.isJidUser(lid) && WABinary_1.isLidUser(pn)))) {
29
- logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`)
35
+ if (!((isLidUser(lid) && isPnUser(pn)) || (isPnUser(lid) && isLidUser(pn)))) {
36
+ this.logger.warn(`Invalid LID-PN mapping: ${lid}, ${pn}`)
30
37
  continue
31
38
  }
32
39
 
33
- const lidDecoded = WABinary_1.jidDecode(lid)
34
- const pnDecoded = WABinary_1.jidDecode(pn)
40
+ const lidDecoded = jidDecode(lid)
41
+ const pnDecoded = jidDecode(pn)
35
42
 
36
43
  if (!lidDecoded || !pnDecoded) return
37
44
 
38
45
  const pnUser = pnDecoded.user
39
46
  const lidUser = lidDecoded.user
40
47
 
41
- // Check if mapping already exists (cache first, then database)
42
48
  let existingLidUser = this.mappingCache.get(`pn:${pnUser}`)
43
49
 
44
50
  if (!existingLidUser) {
45
- // Cache miss - check database
51
+ this.logger.trace(`Cache miss for PN user ${pnUser}; checking database`)
52
+
46
53
  const stored = await this.keys.get('lid-mapping', [pnUser])
47
54
 
48
55
  existingLidUser = stored[pnUser]
@@ -55,102 +62,150 @@ class LIDMappingStore {
55
62
  }
56
63
 
57
64
  if (existingLidUser === lidUser) {
58
- logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping')
65
+ this.logger.debug({ pnUser, lidUser }, 'LID mapping already exists, skipping')
59
66
  continue
60
67
  }
61
68
 
62
69
  pairMap[pnUser] = lidUser
63
70
  }
64
71
 
65
- logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`)
72
+ this.logger.trace({ pairMap }, `Storing ${Object.keys(pairMap).length} pn mappings`)
66
73
 
67
74
  await this.keys.transaction(async () => {
68
75
  for (const [pnUser, lidUser] of Object.entries(pairMap)) {
69
76
  await this.keys.set({
70
77
  'lid-mapping': {
71
- [pnUser]: lidUser, // "554396160286" -> "102765716062358"
72
- [`${lidUser}_reverse`]: pnUser // "102765716062358_reverse" -> "554396160286"
78
+ [pnUser]: lidUser,
79
+ [`${lidUser}_reverse`]: pnUser
73
80
  }
74
81
  })
75
82
 
76
- // Update cache with both directions
77
83
  this.mappingCache.set(`pn:${pnUser}`, lidUser)
78
84
  this.mappingCache.set(`lid:${lidUser}`, pnUser)
79
85
  }
80
86
  }, 'lid-mapping')
81
87
  }
88
+
82
89
  /**
83
90
  * Get LID for PN - Returns device-specific LID based on user mapping
84
91
  */
85
92
  async getLIDForPN(pn) {
86
- if (!WABinary_1.isJidUser(pn)) return null
87
-
88
- const logger = this.logger
89
- const decoded = WABinary_1.jidDecode(pn)
90
-
91
- if (!decoded) return null
92
-
93
- // Check cache first for PN → LID mapping
94
- const pnUser = decoded.user
93
+ return (await this.getLIDsForPNs([pn]))?.[0]?.lid || null
94
+ }
95
+
96
+ async getLIDsForPNs(pns) {
97
+ const usyncFetch = {}
95
98
 
96
- let lidUser = this.mappingCache.get(`pn:${pnUser}`)
99
+ // mapped from pn to lid mapping to prevent duplication in results later
100
+ const successfulPairs = {}
97
101
 
98
- if (!lidUser) {
99
- // Cache miss - check database
100
- const stored = await this.keys.get('lid-mapping', [pnUser])
102
+ for (const pn of pns) {
103
+ if (!isPnUser(pn) && !isHostedPnUser(pn)) continue
101
104
 
102
- lidUser = stored[pnUser]
105
+ const decoded = jidDecode(pn)
106
+ if (!decoded) continue
103
107
 
104
- if (lidUser) {
105
- // Cache the database result
106
- this.mappingCache.set(`pn:${pnUser}`, lidUser)
107
- }
108
+ // Check cache first for PN → LID mapping
109
+ const pnUser = decoded.user
108
110
 
109
- else {
110
- // Not in database - try USync
111
- logger.trace(`No LID mapping found for PN user ${pnUser}; getting from USync`)
111
+ let lidUser = this.mappingCache.get(`pn:${pnUser}`)
112
+
113
+ if (!lidUser) {
114
+ // Cache miss - check database
115
+ const stored = await this.keys.get('lid-mapping', [pnUser])
112
116
 
113
- const { exists, lid } = (await this.onWhatsAppFunc?.(pn))?.[0] // this function already adds LIDs to mapping
117
+ lidUser = stored[pnUser]
114
118
 
115
- if (exists && lid) {
116
- lidUser = WABinary_1.jidDecode(lid)?.user
117
-
118
- if (lidUser) {
119
- // Cache the USync result
120
- this.mappingCache.set(`pn:${pnUser}`, lidUser)
121
- }
119
+ if (lidUser) {
120
+ this.mappingCache.set(`pn:${pnUser}`, lidUser)
121
+ this.mappingCache.set(`lid:${lidUser}`, pnUser)
122
122
  }
123
123
 
124
124
  else {
125
- return null
125
+ this.logger.trace(`No LID mapping found for PN user ${pnUser}; batch getting from USync`)
126
+
127
+ const device = decoded.device || 0
128
+
129
+ let normalizedPn = jidNormalizedUser(pn)
130
+
131
+ if (isHostedPnUser(normalizedPn)) {
132
+ normalizedPn = `${pnUser}@s.whatsapp.net`
133
+ }
134
+
135
+ if (!usyncFetch[normalizedPn]) {
136
+ usyncFetch[normalizedPn] = [device]
137
+ }
138
+
139
+ else {
140
+ usyncFetch[normalizedPn]?.push(device)
141
+ }
142
+
143
+ continue
126
144
  }
127
145
  }
146
+
147
+ lidUser = lidUser.toString()
148
+
149
+ if (!lidUser) {
150
+ this.logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`)
151
+ return null
152
+ }
153
+
154
+ // Push the PN device ID to the LID to maintain device separation
155
+ const pnDevice = decoded.device !== undefined ? decoded.device : 0
156
+ const deviceSpecificLid = `${lidUser}${!!pnDevice ? `:${pnDevice}` : ``}@${decoded.server === 'hosted' ? 'hosted.lid' : 'lid'}`
157
+
158
+ this.logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`)
159
+
160
+ successfulPairs[pn] = { lid: deviceSpecificLid, pn }
128
161
  }
129
162
 
130
- if (typeof lidUser !== 'string' || !lidUser) {
131
- logger.warn(`Invalid or empty LID user for PN ${pn}: lidUser = "${lidUser}"`)
132
- return null
163
+ if (Object.keys(usyncFetch).length > 0) {
164
+ const result = await this.pnToLIDFunc?.(Object.keys(usyncFetch)) // this function already adds LIDs to mapping
165
+
166
+ if (result && result.length > 0) {
167
+ await this.storeLIDPNMappings(result)
168
+
169
+ for (const pair of result) {
170
+ const pnDecoded = jidDecode(pair.pn)
171
+ const pnUser = pnDecoded?.user
172
+
173
+ if (!pnUser) continue
174
+
175
+ const lidUser = jidDecode(pair.lid)?.user
176
+
177
+ if (!lidUser) continue
178
+
179
+ for (const device of usyncFetch[pair.pn]) {
180
+ const deviceSpecificLid = `${lidUser}${!!device ? `:${device}` : ``}@${device === 99 ? 'hosted.lid' : 'lid'}`
181
+
182
+ this.logger.trace(`getLIDForPN: USYNC success for ${pair.pn} → ${deviceSpecificLid} (user mapping with device ${device})`)
183
+
184
+ const deviceSpecificPn = `${pnUser}${!!device ? `:${device}` : ``}@${device === 99 ? 'hosted' : 's.whatsapp.net'}`
185
+
186
+ successfulPairs[deviceSpecificPn] = { lid: deviceSpecificLid, pn: deviceSpecificPn }
187
+ }
188
+ }
189
+ }
190
+
191
+ else {
192
+ return null
193
+ }
133
194
  }
134
195
 
135
- // Push the PN device ID to the LID to maintain device separation
136
- const pnDevice = decoded.device !== undefined ? decoded.device : 0
137
- const deviceSpecificLid = `${lidUser}:${pnDevice}@lid`
138
-
139
- logger.trace(`getLIDForPN: ${pn} → ${deviceSpecificLid} (user mapping with device ${pnDevice})`)
140
-
141
- return deviceSpecificLid
196
+ return Object.values(successfulPairs)
142
197
  }
198
+
143
199
  /**
144
200
  * Get PN for LID - USER LEVEL with device construction
145
201
  */
146
202
  async getPNForLID(lid) {
147
- if (!WABinary_1.isLidUser(lid)) return null
148
-
149
- const logger = this.logger
150
- const decoded = WABinary_1.jidDecode(lid)
203
+ if (!isLidUser(lid)) return null
204
+
205
+ const decoded = jidDecode(lid)
151
206
 
152
207
  if (!decoded) return null
153
-
208
+
154
209
  // Check cache first for LID → PN mapping
155
210
  const lidUser = decoded.user
156
211
 
@@ -158,11 +213,12 @@ class LIDMappingStore {
158
213
 
159
214
  if (!pnUser || typeof pnUser !== 'string') {
160
215
  // Cache miss - check database
161
- const stored = await this.keys.get('lid-mapping', [`${lidUser}_reverse`]);
216
+ const stored = await this.keys.get('lid-mapping', [`${lidUser}_reverse`])
217
+
162
218
  pnUser = stored[`${lidUser}_reverse`]
163
219
 
164
220
  if (!pnUser || typeof pnUser !== 'string') {
165
- logger.trace(`No reverse mapping found for LID user: ${lidUser}`)
221
+ this.logger.trace(`No reverse mapping found for LID user: ${lidUser}`)
166
222
  return null
167
223
  }
168
224
 
@@ -171,8 +227,9 @@ class LIDMappingStore {
171
227
 
172
228
  // Construct device-specific PN JID
173
229
  const lidDevice = decoded.device !== undefined ? decoded.device : 0
174
- const pnJid = `${pnUser}:${lidDevice}@s.whatsapp.net`
175
- logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`)
230
+ const pnJid = `${pnUser}:${lidDevice}@${decoded.domainType === WAJIDDomains.HOSTED_LID ? 'hosted' : 's.whatsapp.net'}`
231
+
232
+ this.logger.trace(`Found reverse mapping: ${lid} → ${pnJid}`)
176
233
 
177
234
  return pnJid
178
235
  }
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true })
4
4
 
5
- const events_1 = require("events")
5
+ const { EventEmitter } = require("events")
6
6
 
7
- class AbstractSocketClient extends events_1.EventEmitter {
7
+ class AbstractSocketClient extends EventEmitter {
8
8
  constructor(url, config) {
9
9
  super()
10
10
  this.url = url
@@ -1,38 +1,45 @@
1
1
  "use strict"
2
2
 
3
- var __importDefault = (this && this.__importDefault) || function (mod) {
4
- return (mod && mod.__esModule) ? mod : { "default": mod }
5
- }
6
-
7
3
  Object.defineProperty(exports, "__esModule", { value: true })
8
4
 
9
- const ws_1 = __importDefault(require("ws"))
10
- const Defaults_1 = require("../../Defaults")
11
- const types_1 = require("./types")
5
+ const {
6
+ OPEN,
7
+ CLOSED,
8
+ CLOSING,
9
+ CONNECTING,
10
+ WebSocket
11
+ } = require("ws")
12
+ const { DEFAULT_ORIGIN } = require("../../Defaults/constants")
13
+ const { AbstractSocketClient } = require("./types")
12
14
 
13
- class WebSocketClient extends types_1.AbstractSocketClient {
15
+ class WebSocketClient extends AbstractSocketClient {
14
16
  constructor() {
15
17
  super(...arguments)
16
18
  this.socket = null
17
19
  }
20
+
18
21
  get isOpen() {
19
- return this.socket?.readyState === ws_1.default.OPEN
22
+ return this.socket?.readyState === OPEN
20
23
  }
24
+
21
25
  get isClosed() {
22
- return this.socket?.readyState === ws_1.default.CLOSED
26
+ return this.socket?.readyState === CLOSED
23
27
  }
28
+
24
29
  get isClosing() {
25
- this.socket?.readyState === ws_1.default.CLOSING
30
+ this.socket?.readyState === CLOSING
26
31
  }
32
+
27
33
  get isConnecting() {
28
- this.socket?.readyState === ws_1.default.CONNECTING
34
+ this.socket?.readyState === CONNECTING
29
35
  }
30
- async connect() {
36
+
37
+ connect() {
31
38
  if (this.socket) {
32
39
  return
33
40
  }
34
- this.socket = new ws_1.default(this.url, {
35
- origin: Defaults_1.DEFAULT_ORIGIN,
41
+ this.socket = new WebSocket(this.url, {
42
+ origin: DEFAULT_ORIGIN,
36
43
  headers: this.config.options?.headers,
37
44
  handshakeTimeout: this.config.connectTimeoutMs,
38
45
  timeout: this.config.connectTimeoutMs,
@@ -44,13 +51,15 @@ class WebSocketClient extends types_1.AbstractSocketClient {
44
51
  this.socket?.on(event, (...args) => this.emit(event, ...args))
45
52
  }
46
53
  }
47
- async close() {
54
+
55
+ close() {
48
56
  if (!this.socket) {
49
57
  return
50
58
  }
51
59
  this.socket.close()
52
60
  this.socket = null
53
61
  }
62
+
54
63
  send(str, cb) {
55
64
  this.socket?.send(str, cb)
56
65
  return Boolean(this.socket)
@@ -2,15 +2,28 @@
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", { value: true })
4
4
 
5
- const Utils_1 = require("../Utils")
6
- const WABinary_1 = require("../WABinary")
7
- const business_1 = require("../Utils/business")
8
- const generic_utils_1 = require("../WABinary/generic-utils")
9
- const messages_recv_1 = require("./messages-recv")
5
+ const {
6
+ toProductNode,
7
+ parseCatalogNode,
8
+ parseCollectionsNode,
9
+ parseOrderDetailsNode,
10
+ getRawMediaUploadData,
11
+ uploadingNecessaryImagesOfProduct
12
+ } = require("../Utils")
13
+ const {
14
+ S_WHATSAPP_NET,
15
+ jidNormalizedUser,
16
+ getBinaryNodeChild
17
+ } = require("../WABinary")
18
+ const { makeMessagesRecvSocket } = require("./messages-recv")
10
19
 
11
20
  const makeBusinessSocket = (config) => {
12
- const suki = messages_recv_1.makeMessagesRecvSocket(config)
13
- const { authState, query, waUploadToServer } = suki
21
+ const suki = makeMessagesRecvSocket(config)
22
+ const {
23
+ authState,
24
+ query,
25
+ waUploadToServer
26
+ } = suki
14
27
 
15
28
  const updateBussinesProfile = async (args) => {
16
29
  const node = []
@@ -63,7 +76,7 @@ const makeBusinessSocket = (config) => {
63
76
  const result = await query({
64
77
  tag: 'iq',
65
78
  attrs: {
66
- to: WABinary_1.S_WHATSAPP_NET,
79
+ to: S_WHATSAPP_NET,
67
80
  type: 'set',
68
81
  xmlns: 'w:biz'
69
82
  },
@@ -83,7 +96,7 @@ const makeBusinessSocket = (config) => {
83
96
  }
84
97
 
85
98
  const updateCoverPhoto = async (photo) => {
86
- const { fileSha256, filePath } = await Utils_1.getRawMediaUploadData(photo, 'biz-cover-photo')
99
+ const { fileSha256, filePath } = await getRawMediaUploadData(photo, 'biz-cover-photo')
87
100
  const fileSha256B64 = fileSha256.toString('base64')
88
101
 
89
102
  const { meta_hmac, fbid, ts } = await waUploadToServer(filePath, {
@@ -94,7 +107,7 @@ const makeBusinessSocket = (config) => {
94
107
  await query({
95
108
  tag: 'iq',
96
109
  attrs: {
97
- to: WABinary_1.S_WHATSAPP_NET,
110
+ to: S_WHATSAPP_NET,
98
111
  type: 'set',
99
112
  xmlns: 'w:biz'
100
113
  },
@@ -122,7 +135,7 @@ const makeBusinessSocket = (config) => {
122
135
  return await query({
123
136
  tag: 'iq',
124
137
  attrs: {
125
- to: WABinary_1.S_WHATSAPP_NET,
138
+ to: S_WHATSAPP_NET,
126
139
  type: 'set',
127
140
  xmlns: 'w:biz'
128
141
  },
@@ -146,7 +159,7 @@ const makeBusinessSocket = (config) => {
146
159
 
147
160
  const getCatalog = async ({ jid, limit, cursor }) => {
148
161
  jid = jid || authState.creds.me?.id
149
- jid = WABinary_1.jidNormalizedUser(jid)
162
+ jid = jidNormalizedUser(jid)
150
163
 
151
164
  const queryParamNodes = [
152
165
  {
@@ -177,7 +190,7 @@ const makeBusinessSocket = (config) => {
177
190
  const result = await query({
178
191
  tag: 'iq',
179
192
  attrs: {
180
- to: WABinary_1.S_WHATSAPP_NET,
193
+ to: S_WHATSAPP_NET,
181
194
  type: 'get',
182
195
  xmlns: 'w:biz:catalog'
183
196
  },
@@ -193,17 +206,17 @@ const makeBusinessSocket = (config) => {
193
206
  ]
194
207
  })
195
208
 
196
- return business_1.parseCatalogNode(result)
209
+ return parseCatalogNode(result)
197
210
  }
198
211
 
199
212
  const getCollections = async (jid, limit = 51) => {
200
213
  jid = jid || authState.creds.me?.id
201
- jid = WABinary_1.jidNormalizedUser(jid)
214
+ jid = jidNormalizedUser(jid)
202
215
 
203
216
  const result = await query({
204
217
  tag: 'iq',
205
218
  attrs: {
206
- to: WABinary_1.S_WHATSAPP_NET,
219
+ to: S_WHATSAPP_NET,
207
220
  type: 'get',
208
221
  xmlns: 'w:biz:catalog',
209
222
  'smax_id': '35'
@@ -240,14 +253,14 @@ const makeBusinessSocket = (config) => {
240
253
  ]
241
254
  })
242
255
 
243
- return business_1.parseCollectionsNode(result)
256
+ return parseCollectionsNode(result)
244
257
  }
245
258
 
246
259
  const getOrderDetails = async (orderId, tokenBase64) => {
247
260
  const result = await query({
248
261
  tag: 'iq',
249
262
  attrs: {
250
- to: WABinary_1.S_WHATSAPP_NET,
263
+ to: S_WHATSAPP_NET,
251
264
  type: 'get',
252
265
  xmlns: 'fb:thrift_iq',
253
266
  'smax_id': '5'
@@ -286,17 +299,17 @@ const makeBusinessSocket = (config) => {
286
299
  ]
287
300
  })
288
301
 
289
- return business_1.parseOrderDetailsNode(result)
302
+ return parseOrderDetailsNode(result)
290
303
  }
291
304
 
292
305
  const productUpdate = async (productId, update) => {
293
- update = await business_1.uploadingNecessaryImagesOfProduct(update, waUploadToServer)
294
- const editNode = business_1.toProductNode(productId, update)
306
+ update = await uploadingNecessaryImagesOfProduct(update, waUploadToServer)
307
+ const editNode = toProductNode(productId, update)
295
308
 
296
309
  const result = await query({
297
310
  tag: 'iq',
298
311
  attrs: {
299
- to: WABinary_1.S_WHATSAPP_NET,
312
+ to: S_WHATSAPP_NET,
300
313
  type: 'set',
301
314
  xmlns: 'w:biz:catalog'
302
315
  },
@@ -320,21 +333,21 @@ const makeBusinessSocket = (config) => {
320
333
  }
321
334
  ]
322
335
  })
323
- const productCatalogEditNode = generic_utils_1.getBinaryNodeChild(result, 'product_catalog_edit')
324
- const productNode = generic_utils_1.getBinaryNodeChild(productCatalogEditNode, 'product')
325
- return business_1.parseProductNode(productNode)
336
+ const productCatalogEditNode = getBinaryNodeChild(result, 'product_catalog_edit')
337
+ const productNode = getBinaryNodeChild(productCatalogEditNode, 'product')
338
+ return parseProductNode(productNode)
326
339
  }
327
340
 
328
341
  const productCreate = async (create) => {
329
342
  // ensure isHidden is defined
330
343
  create.isHidden = !!create.isHidden
331
- create = await business_1.uploadingNecessaryImagesOfProduct(create, waUploadToServer)
332
- const createNode = business_1.toProductNode(undefined, create)
344
+ create = await uploadingNecessaryImagesOfProduct(create, waUploadToServer)
345
+ const createNode = toProductNode(undefined, create)
333
346
 
334
347
  const result = await query({
335
348
  tag: 'iq',
336
349
  attrs: {
337
- to: WABinary_1.S_WHATSAPP_NET,
350
+ to: S_WHATSAPP_NET,
338
351
  type: 'set',
339
352
  xmlns: 'w:biz:catalog'
340
353
  },
@@ -358,17 +371,17 @@ const makeBusinessSocket = (config) => {
358
371
  }
359
372
  ]
360
373
  })
361
- const productCatalogAddNode = generic_utils_1.getBinaryNodeChild(result, 'product_catalog_add')
362
- const productNode = generic_utils_1.getBinaryNodeChild(productCatalogAddNode, 'product')
374
+ const productCatalogAddNode = getBinaryNodeChild(result, 'product_catalog_add')
375
+ const productNode = getBinaryNodeChild(productCatalogAddNode, 'product')
363
376
 
364
- return business_1.parseProductNode(productNode)
377
+ return parseProductNode(productNode)
365
378
  }
366
379
 
367
380
  const productDelete = async (productIds) => {
368
381
  const result = await query({
369
382
  tag: 'iq',
370
383
  attrs: {
371
- to: WABinary_1.S_WHATSAPP_NET,
384
+ to: S_WHATSAPP_NET,
372
385
  type: 'set',
373
386
  xmlns: 'w:biz:catalog'
374
387
  },
@@ -390,7 +403,7 @@ const makeBusinessSocket = (config) => {
390
403
  }
391
404
  ]
392
405
  })
393
- const productCatalogDelNode = generic_utils_1.getBinaryNodeChild(result, 'product_catalog_delete')
406
+ const productCatalogDelNode = getBinaryNodeChild(result, 'product_catalog_delete')
394
407
  return {
395
408
  deleted: +(productCatalogDelNode?.attrs?.deleted_count || 0)
396
409
  }