queenruva-sockets 5.6.23

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 (205) hide show
  1. package/LICENSE +31 -0
  2. package/README.md +1082 -0
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +5479 -0
  5. package/WAProto/fix-imports.js +85 -0
  6. package/WAProto/index.d.ts +14017 -0
  7. package/WAProto/index.js +97691 -0
  8. package/engine-requirements.js +11 -0
  9. package/lib/Defaults/index.d.ts +75 -0
  10. package/lib/Defaults/index.js +129 -0
  11. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  12. package/lib/Signal/Group/ciphertext-message.js +12 -0
  13. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  14. package/lib/Signal/Group/group-session-builder.js +30 -0
  15. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  16. package/lib/Signal/Group/group_cipher.js +82 -0
  17. package/lib/Signal/Group/index.d.ts +12 -0
  18. package/lib/Signal/Group/index.js +12 -0
  19. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  20. package/lib/Signal/Group/keyhelper.js +18 -0
  21. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  22. package/lib/Signal/Group/sender-chain-key.js +26 -0
  23. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  24. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  25. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  26. package/lib/Signal/Group/sender-key-message.js +66 -0
  27. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  28. package/lib/Signal/Group/sender-key-name.js +48 -0
  29. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  30. package/lib/Signal/Group/sender-key-record.js +41 -0
  31. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  32. package/lib/Signal/Group/sender-key-state.js +84 -0
  33. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  34. package/lib/Signal/Group/sender-message-key.js +26 -0
  35. package/lib/Signal/libsignal.d.ts +5 -0
  36. package/lib/Signal/libsignal.js +431 -0
  37. package/lib/Signal/lid-mapping.d.ts +23 -0
  38. package/lib/Signal/lid-mapping.js +277 -0
  39. package/lib/Socket/Client/index.d.ts +3 -0
  40. package/lib/Socket/Client/index.js +3 -0
  41. package/lib/Socket/Client/types.d.ts +16 -0
  42. package/lib/Socket/Client/types.js +11 -0
  43. package/lib/Socket/Client/websocket.d.ts +13 -0
  44. package/lib/Socket/Client/websocket.js +54 -0
  45. package/lib/Socket/business.d.ts +217 -0
  46. package/lib/Socket/business.js +379 -0
  47. package/lib/Socket/chats.d.ts +124 -0
  48. package/lib/Socket/chats.js +1193 -0
  49. package/lib/Socket/communities.d.ts +273 -0
  50. package/lib/Socket/communities.js +431 -0
  51. package/lib/Socket/groups.d.ts +161 -0
  52. package/lib/Socket/groups.js +347 -0
  53. package/lib/Socket/index.d.ts +260 -0
  54. package/lib/Socket/index.js +12 -0
  55. package/lib/Socket/messages-recv.d.ts +203 -0
  56. package/lib/Socket/messages-recv.js +1772 -0
  57. package/lib/Socket/messages-send.d.ts +199 -0
  58. package/lib/Socket/messages-send.js +1160 -0
  59. package/lib/Socket/mex.d.ts +3 -0
  60. package/lib/Socket/mex.js +42 -0
  61. package/lib/Socket/newsletter.d.ts +170 -0
  62. package/lib/Socket/newsletter.js +181 -0
  63. package/lib/Socket/socket.d.ts +59 -0
  64. package/lib/Socket/socket.js +1029 -0
  65. package/lib/Types/Auth.d.ts +117 -0
  66. package/lib/Types/Auth.js +2 -0
  67. package/lib/Types/Bussines.d.ts +25 -0
  68. package/lib/Types/Bussines.js +2 -0
  69. package/lib/Types/Call.d.ts +15 -0
  70. package/lib/Types/Call.js +2 -0
  71. package/lib/Types/Chat.d.ts +124 -0
  72. package/lib/Types/Chat.js +8 -0
  73. package/lib/Types/Contact.d.ts +26 -0
  74. package/lib/Types/Contact.js +2 -0
  75. package/lib/Types/Events.d.ts +256 -0
  76. package/lib/Types/Events.js +2 -0
  77. package/lib/Types/GroupMetadata.d.ts +71 -0
  78. package/lib/Types/GroupMetadata.js +2 -0
  79. package/lib/Types/Label.d.ts +47 -0
  80. package/lib/Types/Label.js +25 -0
  81. package/lib/Types/LabelAssociation.d.ts +30 -0
  82. package/lib/Types/LabelAssociation.js +7 -0
  83. package/lib/Types/Message.d.ts +320 -0
  84. package/lib/Types/Message.js +11 -0
  85. package/lib/Types/Mex.d.ts +141 -0
  86. package/lib/Types/Mex.js +37 -0
  87. package/lib/Types/Product.d.ts +79 -0
  88. package/lib/Types/Product.js +2 -0
  89. package/lib/Types/Signal.d.ts +87 -0
  90. package/lib/Types/Signal.js +2 -0
  91. package/lib/Types/Socket.d.ts +136 -0
  92. package/lib/Types/Socket.js +3 -0
  93. package/lib/Types/State.d.ts +97 -0
  94. package/lib/Types/State.js +56 -0
  95. package/lib/Types/USync.d.ts +26 -0
  96. package/lib/Types/USync.js +2 -0
  97. package/lib/Types/index.d.ts +65 -0
  98. package/lib/Types/index.js +26 -0
  99. package/lib/Utils/auth-utils.d.ts +24 -0
  100. package/lib/Utils/auth-utils.js +302 -0
  101. package/lib/Utils/browser-utils.d.ts +4 -0
  102. package/lib/Utils/browser-utils.js +28 -0
  103. package/lib/Utils/business.d.ts +23 -0
  104. package/lib/Utils/business.js +231 -0
  105. package/lib/Utils/chat-utils.d.ts +100 -0
  106. package/lib/Utils/chat-utils.js +872 -0
  107. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  108. package/lib/Utils/companion-reg-client-utils.js +35 -0
  109. package/lib/Utils/crypto.d.ts +37 -0
  110. package/lib/Utils/crypto.js +118 -0
  111. package/lib/Utils/decode-wa-message.d.ts +66 -0
  112. package/lib/Utils/decode-wa-message.js +311 -0
  113. package/lib/Utils/event-buffer.d.ts +36 -0
  114. package/lib/Utils/event-buffer.js +622 -0
  115. package/lib/Utils/generics.d.ts +91 -0
  116. package/lib/Utils/generics.js +378 -0
  117. package/lib/Utils/history.d.ts +24 -0
  118. package/lib/Utils/history.js +134 -0
  119. package/lib/Utils/identity-change-handler.d.ts +44 -0
  120. package/lib/Utils/identity-change-handler.js +50 -0
  121. package/lib/Utils/index.d.ts +22 -0
  122. package/lib/Utils/index.js +22 -0
  123. package/lib/Utils/link-preview.d.ts +21 -0
  124. package/lib/Utils/link-preview.js +85 -0
  125. package/lib/Utils/logger.d.ts +12 -0
  126. package/lib/Utils/logger.js +3 -0
  127. package/lib/Utils/lt-hash.d.ts +8 -0
  128. package/lib/Utils/lt-hash.js +8 -0
  129. package/lib/Utils/make-mutex.d.ts +9 -0
  130. package/lib/Utils/make-mutex.js +33 -0
  131. package/lib/Utils/message-retry-manager.d.ts +115 -0
  132. package/lib/Utils/message-retry-manager.js +265 -0
  133. package/lib/Utils/messages-media.d.ts +133 -0
  134. package/lib/Utils/messages-media.js +786 -0
  135. package/lib/Utils/messages.d.ts +91 -0
  136. package/lib/Utils/messages.js +893 -0
  137. package/lib/Utils/noise-handler.d.ts +20 -0
  138. package/lib/Utils/noise-handler.js +201 -0
  139. package/lib/Utils/offline-node-processor.d.ts +17 -0
  140. package/lib/Utils/offline-node-processor.js +40 -0
  141. package/lib/Utils/pre-key-manager.d.ts +28 -0
  142. package/lib/Utils/pre-key-manager.js +106 -0
  143. package/lib/Utils/process-message.d.ts +60 -0
  144. package/lib/Utils/process-message.js +597 -0
  145. package/lib/Utils/reporting-utils.d.ts +11 -0
  146. package/lib/Utils/reporting-utils.js +258 -0
  147. package/lib/Utils/signal.d.ts +47 -0
  148. package/lib/Utils/signal.js +201 -0
  149. package/lib/Utils/stanza-ack.d.ts +11 -0
  150. package/lib/Utils/stanza-ack.js +38 -0
  151. package/lib/Utils/sync-action-utils.d.ts +19 -0
  152. package/lib/Utils/sync-action-utils.js +49 -0
  153. package/lib/Utils/tc-token-utils.d.ts +37 -0
  154. package/lib/Utils/tc-token-utils.js +163 -0
  155. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  156. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  157. package/lib/Utils/validate-connection.d.ts +11 -0
  158. package/lib/Utils/validate-connection.js +203 -0
  159. package/lib/WABinary/constants.d.ts +28 -0
  160. package/lib/WABinary/constants.js +1301 -0
  161. package/lib/WABinary/decode.d.ts +7 -0
  162. package/lib/WABinary/decode.js +262 -0
  163. package/lib/WABinary/encode.d.ts +3 -0
  164. package/lib/WABinary/encode.js +220 -0
  165. package/lib/WABinary/generic-utils.d.ts +15 -0
  166. package/lib/WABinary/generic-utils.js +113 -0
  167. package/lib/WABinary/index.d.ts +6 -0
  168. package/lib/WABinary/index.js +6 -0
  169. package/lib/WABinary/jid-utils.d.ts +48 -0
  170. package/lib/WABinary/jid-utils.js +96 -0
  171. package/lib/WABinary/types.d.ts +19 -0
  172. package/lib/WABinary/types.js +2 -0
  173. package/lib/WAM/BinaryInfo.d.ts +9 -0
  174. package/lib/WAM/BinaryInfo.js +10 -0
  175. package/lib/WAM/constants.d.ts +40 -0
  176. package/lib/WAM/constants.js +22853 -0
  177. package/lib/WAM/encode.d.ts +3 -0
  178. package/lib/WAM/encode.js +150 -0
  179. package/lib/WAM/index.d.ts +4 -0
  180. package/lib/WAM/index.js +4 -0
  181. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  182. package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
  183. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  184. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  185. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  186. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  187. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  188. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  189. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  190. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  191. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  192. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  193. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  194. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  195. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  196. package/lib/WAUSync/Protocols/index.js +6 -0
  197. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  198. package/lib/WAUSync/USyncQuery.js +98 -0
  199. package/lib/WAUSync/USyncUser.d.ts +17 -0
  200. package/lib/WAUSync/USyncUser.js +31 -0
  201. package/lib/WAUSync/index.d.ts +4 -0
  202. package/lib/WAUSync/index.js +4 -0
  203. package/lib/index.d.ts +12 -0
  204. package/lib/index.js +11 -0
  205. package/package.json +160 -0
@@ -0,0 +1,302 @@
1
+ import NodeCache from '@cacheable/node-cache';
2
+ import { Boom } from '@hapi/boom';
3
+ import { AsyncLocalStorage } from 'async_hooks';
4
+ import { Mutex } from 'async-mutex';
5
+ import { randomBytes } from 'crypto';
6
+ import PQueue from 'p-queue';
7
+ import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
8
+ import { Curve, signedKeyPair } from './crypto.js';
9
+ import { delay, generateRegistrationId } from './generics.js';
10
+ import { PreKeyManager } from './pre-key-manager.js';
11
+ /**
12
+ * Adds caching capability to a SignalKeyStore
13
+ * @param store the store to add caching to
14
+ * @param logger to log trace events
15
+ * @param _cache cache store to use
16
+ */
17
+ export function makeCacheableSignalKeyStore(store, logger, _cache) {
18
+ const cache = _cache ||
19
+ new NodeCache({
20
+ stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
21
+ useClones: false,
22
+ deleteOnExpire: true
23
+ });
24
+ // Mutex for protecting cache operations
25
+ const cacheMutex = new Mutex();
26
+ function getUniqueId(type, id) {
27
+ return `${type}.${id}`;
28
+ }
29
+ return {
30
+ async get(type, ids) {
31
+ return cacheMutex.runExclusive(async () => {
32
+ const data = {};
33
+ const idsToFetch = [];
34
+ for (const id of ids) {
35
+ const item = (await cache.get(getUniqueId(type, id)));
36
+ if (typeof item !== 'undefined') {
37
+ data[id] = item;
38
+ }
39
+ else {
40
+ idsToFetch.push(id);
41
+ }
42
+ }
43
+ if (idsToFetch.length) {
44
+ logger?.trace({ items: idsToFetch.length }, 'loading from store');
45
+ const fetched = await store.get(type, idsToFetch);
46
+ for (const id of idsToFetch) {
47
+ const item = fetched[id];
48
+ if (item) {
49
+ data[id] = item;
50
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
51
+ await cache.set(getUniqueId(type, id), item);
52
+ }
53
+ }
54
+ }
55
+ return data;
56
+ });
57
+ },
58
+ async set(data) {
59
+ return cacheMutex.runExclusive(async () => {
60
+ let keys = 0;
61
+ for (const type in data) {
62
+ for (const id in data[type]) {
63
+ await cache.set(getUniqueId(type, id), data[type][id]);
64
+ keys += 1;
65
+ }
66
+ }
67
+ logger?.trace({ keys }, 'updated cache');
68
+ await store.set(data);
69
+ });
70
+ },
71
+ async clear() {
72
+ await cache.flushAll();
73
+ await store.clear?.();
74
+ }
75
+ };
76
+ }
77
+ /**
78
+ * Adds DB-like transaction capability to the SignalKeyStore
79
+ * Uses AsyncLocalStorage for automatic context management
80
+ * @param state the key store to apply this capability to
81
+ * @param logger logger to log events
82
+ * @returns SignalKeyStore with transaction capability
83
+ */
84
+ export const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
85
+ const txStorage = new AsyncLocalStorage();
86
+ // Queues for concurrency control (keyed by signal data type - bounded set)
87
+ const keyQueues = new Map();
88
+ // Transaction mutexes with reference counting for cleanup
89
+ const txMutexes = new Map();
90
+ const txMutexRefCounts = new Map();
91
+ // Pre-key manager for specialized operations
92
+ const preKeyManager = new PreKeyManager(state, logger);
93
+ /**
94
+ * Get or create a queue for a specific key type
95
+ */
96
+ function getQueue(key) {
97
+ if (!keyQueues.has(key)) {
98
+ keyQueues.set(key, new PQueue({ concurrency: 1 }));
99
+ }
100
+ return keyQueues.get(key);
101
+ }
102
+ /**
103
+ * Get or create a transaction mutex
104
+ */
105
+ function getTxMutex(key) {
106
+ if (!txMutexes.has(key)) {
107
+ txMutexes.set(key, new Mutex());
108
+ txMutexRefCounts.set(key, 0);
109
+ }
110
+ return txMutexes.get(key);
111
+ }
112
+ /**
113
+ * Acquire a reference to a transaction mutex
114
+ */
115
+ function acquireTxMutexRef(key) {
116
+ const count = txMutexRefCounts.get(key) ?? 0;
117
+ txMutexRefCounts.set(key, count + 1);
118
+ }
119
+ /**
120
+ * Release a reference to a transaction mutex and cleanup if no longer needed
121
+ */
122
+ function releaseTxMutexRef(key) {
123
+ const count = (txMutexRefCounts.get(key) ?? 1) - 1;
124
+ txMutexRefCounts.set(key, count);
125
+ // Cleanup if no more references and mutex is not locked
126
+ if (count <= 0) {
127
+ const mutex = txMutexes.get(key);
128
+ if (mutex && !mutex.isLocked()) {
129
+ txMutexes.delete(key);
130
+ txMutexRefCounts.delete(key);
131
+ }
132
+ }
133
+ }
134
+ /**
135
+ * Check if currently in a transaction
136
+ */
137
+ function isInTransaction() {
138
+ return !!txStorage.getStore();
139
+ }
140
+ /**
141
+ * Commit transaction with retries
142
+ */
143
+ async function commitWithRetry(mutations) {
144
+ if (Object.keys(mutations).length === 0) {
145
+ logger.trace('no mutations in transaction');
146
+ return;
147
+ }
148
+ logger.trace('committing transaction');
149
+ for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
150
+ try {
151
+ await state.set(mutations);
152
+ logger.trace({ mutationCount: Object.keys(mutations).length }, 'committed transaction');
153
+ return;
154
+ }
155
+ catch (error) {
156
+ const retriesLeft = maxCommitRetries - attempt - 1;
157
+ logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
158
+ if (retriesLeft === 0) {
159
+ throw error;
160
+ }
161
+ await delay(delayBetweenTriesMs);
162
+ }
163
+ }
164
+ }
165
+ return {
166
+ get: async (type, ids) => {
167
+ const ctx = txStorage.getStore();
168
+ if (!ctx) {
169
+ // No transaction - direct read without exclusive lock for concurrency
170
+ return state.get(type, ids);
171
+ }
172
+ // In transaction - check cache first
173
+ const cached = ctx.cache[type] || {};
174
+ const missing = ids.filter(id => !(id in cached));
175
+ if (missing.length > 0) {
176
+ ctx.dbQueries++;
177
+ logger.trace({ type, count: missing.length }, 'fetching missing keys in transaction');
178
+ const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
179
+ // Update cache
180
+ ctx.cache[type] = ctx.cache[type] || {};
181
+ Object.assign(ctx.cache[type], fetched);
182
+ }
183
+ // Return requested ids from cache
184
+ const result = {};
185
+ for (const id of ids) {
186
+ const value = ctx.cache[type]?.[id];
187
+ if (value !== undefined && value !== null) {
188
+ result[id] = value;
189
+ }
190
+ }
191
+ return result;
192
+ },
193
+ set: async (data) => {
194
+ const ctx = txStorage.getStore();
195
+ if (!ctx) {
196
+ // No transaction - direct write with queue protection
197
+ const types = Object.keys(data);
198
+ // Process pre-keys with validation
199
+ for (const type_ of types) {
200
+ const type = type_;
201
+ if (type === 'pre-key') {
202
+ await preKeyManager.validateDeletions(data, type);
203
+ }
204
+ }
205
+ // Write all data in parallel
206
+ await Promise.all(types.map(type => getQueue(type).add(async () => {
207
+ const typeData = { [type]: data[type] };
208
+ await state.set(typeData);
209
+ })));
210
+ return;
211
+ }
212
+ // In transaction - update cache and mutations
213
+ logger.trace({ types: Object.keys(data) }, 'caching in transaction');
214
+ for (const key_ in data) {
215
+ const key = key_;
216
+ // Ensure structures exist
217
+ ctx.cache[key] = ctx.cache[key] || {};
218
+ ctx.mutations[key] = ctx.mutations[key] || {};
219
+ // Special handling for pre-keys
220
+ if (key === 'pre-key') {
221
+ await preKeyManager.processOperations(data, key, ctx.cache, ctx.mutations, true);
222
+ }
223
+ else {
224
+ // Normal key types
225
+ Object.assign(ctx.cache[key], data[key]);
226
+ Object.assign(ctx.mutations[key], data[key]);
227
+ }
228
+ }
229
+ },
230
+ isInTransaction,
231
+ transaction: async (work, key) => {
232
+ const existing = txStorage.getStore();
233
+ // Nested transaction - reuse existing context
234
+ if (existing) {
235
+ logger.trace('reusing existing transaction context');
236
+ return work();
237
+ }
238
+ // New transaction - acquire mutex and create context
239
+ const mutex = getTxMutex(key);
240
+ acquireTxMutexRef(key);
241
+ try {
242
+ return await mutex.runExclusive(async () => {
243
+ const ctx = {
244
+ cache: {},
245
+ mutations: {},
246
+ dbQueries: 0
247
+ };
248
+ logger.trace('entering transaction');
249
+ try {
250
+ const result = await txStorage.run(ctx, work);
251
+ // Commit mutations
252
+ await commitWithRetry(ctx.mutations);
253
+ logger.trace({ dbQueries: ctx.dbQueries }, 'transaction completed');
254
+ return result;
255
+ }
256
+ catch (error) {
257
+ logger.error({ error }, 'transaction failed, rolling back');
258
+ throw error;
259
+ }
260
+ });
261
+ }
262
+ finally {
263
+ releaseTxMutexRef(key);
264
+ }
265
+ }
266
+ };
267
+ };
268
+ /**
269
+ * Returns the authenticated user's JID, or throws a Boom-401 if creds are not yet authenticated.
270
+ * Use this anywhere we'd otherwise reach for `creds.me!.id` to fail fast with a descriptive error.
271
+ */
272
+ export const assertMeId = (creds) => {
273
+ const id = creds.me?.id;
274
+ if (!id) {
275
+ throw new Boom('Cannot proceed: socket is not authenticated yet (creds.me.id is missing)', { statusCode: 401 });
276
+ }
277
+ return id;
278
+ };
279
+ export const initAuthCreds = () => {
280
+ const identityKey = Curve.generateKeyPair();
281
+ return {
282
+ noiseKey: Curve.generateKeyPair(),
283
+ pairingEphemeralKeyPair: Curve.generateKeyPair(),
284
+ signedIdentityKey: identityKey,
285
+ signedPreKey: signedKeyPair(identityKey, 1),
286
+ registrationId: generateRegistrationId(),
287
+ advSecretKey: randomBytes(32).toString('base64'),
288
+ processedHistoryMessages: [],
289
+ nextPreKeyId: 1,
290
+ firstUnuploadedPreKeyId: 1,
291
+ accountSyncCounter: 0,
292
+ accountSettings: {
293
+ unarchiveChats: false
294
+ },
295
+ registered: false,
296
+ pairingCode: undefined,
297
+ lastPropHash: undefined,
298
+ routingInfo: undefined,
299
+ additionalData: undefined
300
+ };
301
+ };
302
+ //# sourceMappingURL=auth-utils.js.map
@@ -0,0 +1,4 @@
1
+ import type { BrowsersMap } from '../Types/index.js';
2
+ export declare const Browsers: BrowsersMap;
3
+ export declare const getPlatformId: (browser: string) => string;
4
+ //# sourceMappingURL=browser-utils.d.ts.map
@@ -0,0 +1,28 @@
1
+ import { platform, release } from 'os';
2
+ import { proto } from '../../WAProto/index.js';
3
+ const PLATFORM_MAP = {
4
+ aix: 'AIX',
5
+ darwin: 'Mac OS',
6
+ win32: 'Windows',
7
+ android: 'Android',
8
+ freebsd: 'FreeBSD',
9
+ openbsd: 'OpenBSD',
10
+ sunos: 'Solaris',
11
+ linux: undefined,
12
+ haiku: undefined,
13
+ cygwin: undefined,
14
+ netbsd: undefined
15
+ };
16
+ export const Browsers = {
17
+ ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
18
+ macOS: browser => ['Mac OS', browser, '14.4.1'],
19
+ baileys: browser => ['Baileys', browser, '6.5.0'],
20
+ windows: browser => ['Windows', browser, '10.0.22631'],
21
+ /** The appropriate browser based on your OS & release */
22
+ appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
23
+ };
24
+ export const getPlatformId = (browser) => {
25
+ const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
26
+ return platformType ? platformType.toString() : '1'; //chrome
27
+ };
28
+ //# sourceMappingURL=browser-utils.js.map
@@ -0,0 +1,23 @@
1
+ import type { CatalogCollection, OrderDetails, Product, ProductCreate, ProductUpdate, WAMediaUpload, WAMediaUploadFunction } from '../Types/index.js';
2
+ import { type BinaryNode } from '../WABinary/index.js';
3
+ export declare const parseCatalogNode: (node: BinaryNode) => {
4
+ products: Product[];
5
+ nextPageCursor: string | undefined;
6
+ };
7
+ export declare const parseCollectionsNode: (node: BinaryNode) => {
8
+ collections: CatalogCollection[];
9
+ };
10
+ export declare const parseOrderDetailsNode: (node: BinaryNode) => OrderDetails;
11
+ export declare const toProductNode: (productId: string | undefined, product: ProductCreate | ProductUpdate) => BinaryNode;
12
+ export declare const parseProductNode: (productNode: BinaryNode) => Product;
13
+ /**
14
+ * Uploads images not already uploaded to WA's servers
15
+ */
16
+ export declare function uploadingNecessaryImagesOfProduct<T extends ProductUpdate | ProductCreate>(product: T, waUploadToServer: WAMediaUploadFunction, timeoutMs?: number): Promise<T>;
17
+ /**
18
+ * Uploads images not already uploaded to WA's servers
19
+ */
20
+ export declare const uploadingNecessaryImages: (images: WAMediaUpload[], waUploadToServer: WAMediaUploadFunction, timeoutMs?: number) => Promise<{
21
+ url: string;
22
+ }[]>;
23
+ //# sourceMappingURL=business.d.ts.map
@@ -0,0 +1,231 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { createHash } from 'crypto';
3
+ import { createWriteStream, promises as fs } from 'fs';
4
+ import { tmpdir } from 'os';
5
+ import { join } from 'path';
6
+ import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary/index.js';
7
+ import { generateMessageIDV2 } from './generics.js';
8
+ import { getStream, getUrlFromDirectPath } from './messages-media.js';
9
+ export const parseCatalogNode = (node) => {
10
+ const catalogNode = getBinaryNodeChild(node, 'product_catalog');
11
+ const products = getBinaryNodeChildren(catalogNode, 'product').map(parseProductNode);
12
+ const paging = getBinaryNodeChild(catalogNode, 'paging');
13
+ return {
14
+ products,
15
+ nextPageCursor: paging ? getBinaryNodeChildString(paging, 'after') : undefined
16
+ };
17
+ };
18
+ export const parseCollectionsNode = (node) => {
19
+ const collectionsNode = getBinaryNodeChild(node, 'collections');
20
+ const collections = getBinaryNodeChildren(collectionsNode, 'collection').map(collectionNode => {
21
+ const id = getBinaryNodeChildString(collectionNode, 'id');
22
+ const name = getBinaryNodeChildString(collectionNode, 'name');
23
+ const products = getBinaryNodeChildren(collectionNode, 'product').map(parseProductNode);
24
+ return {
25
+ id,
26
+ name,
27
+ products,
28
+ status: parseStatusInfo(collectionNode)
29
+ };
30
+ });
31
+ return {
32
+ collections
33
+ };
34
+ };
35
+ export const parseOrderDetailsNode = (node) => {
36
+ const orderNode = getBinaryNodeChild(node, 'order');
37
+ const products = getBinaryNodeChildren(orderNode, 'product').map(productNode => {
38
+ const imageNode = getBinaryNodeChild(productNode, 'image');
39
+ return {
40
+ id: getBinaryNodeChildString(productNode, 'id'),
41
+ name: getBinaryNodeChildString(productNode, 'name'),
42
+ imageUrl: getBinaryNodeChildString(imageNode, 'url'),
43
+ price: +getBinaryNodeChildString(productNode, 'price'),
44
+ currency: getBinaryNodeChildString(productNode, 'currency'),
45
+ quantity: +getBinaryNodeChildString(productNode, 'quantity')
46
+ };
47
+ });
48
+ const priceNode = getBinaryNodeChild(orderNode, 'price');
49
+ const orderDetails = {
50
+ price: {
51
+ total: +getBinaryNodeChildString(priceNode, 'total'),
52
+ currency: getBinaryNodeChildString(priceNode, 'currency')
53
+ },
54
+ products
55
+ };
56
+ return orderDetails;
57
+ };
58
+ export const toProductNode = (productId, product) => {
59
+ const attrs = {};
60
+ const content = [];
61
+ if (typeof productId !== 'undefined') {
62
+ content.push({
63
+ tag: 'id',
64
+ attrs: {},
65
+ content: Buffer.from(productId)
66
+ });
67
+ }
68
+ if (typeof product.name !== 'undefined') {
69
+ content.push({
70
+ tag: 'name',
71
+ attrs: {},
72
+ content: Buffer.from(product.name)
73
+ });
74
+ }
75
+ if (typeof product.description !== 'undefined') {
76
+ content.push({
77
+ tag: 'description',
78
+ attrs: {},
79
+ content: Buffer.from(product.description)
80
+ });
81
+ }
82
+ if (typeof product.retailerId !== 'undefined') {
83
+ content.push({
84
+ tag: 'retailer_id',
85
+ attrs: {},
86
+ content: Buffer.from(product.retailerId)
87
+ });
88
+ }
89
+ if (product.images.length) {
90
+ content.push({
91
+ tag: 'media',
92
+ attrs: {},
93
+ content: product.images.map(img => {
94
+ if (!('url' in img)) {
95
+ throw new Boom('Expected img for product to already be uploaded', { statusCode: 400 });
96
+ }
97
+ return {
98
+ tag: 'image',
99
+ attrs: {},
100
+ content: [
101
+ {
102
+ tag: 'url',
103
+ attrs: {},
104
+ content: Buffer.from(img.url.toString())
105
+ }
106
+ ]
107
+ };
108
+ })
109
+ });
110
+ }
111
+ if (typeof product.price !== 'undefined') {
112
+ content.push({
113
+ tag: 'price',
114
+ attrs: {},
115
+ content: Buffer.from(product.price.toString())
116
+ });
117
+ }
118
+ if (typeof product.currency !== 'undefined') {
119
+ content.push({
120
+ tag: 'currency',
121
+ attrs: {},
122
+ content: Buffer.from(product.currency)
123
+ });
124
+ }
125
+ if ('originCountryCode' in product) {
126
+ if (typeof product.originCountryCode === 'undefined') {
127
+ attrs['compliance_category'] = 'COUNTRY_ORIGIN_EXEMPT';
128
+ }
129
+ else {
130
+ content.push({
131
+ tag: 'compliance_info',
132
+ attrs: {},
133
+ content: [
134
+ {
135
+ tag: 'country_code_origin',
136
+ attrs: {},
137
+ content: Buffer.from(product.originCountryCode)
138
+ }
139
+ ]
140
+ });
141
+ }
142
+ }
143
+ if (typeof product.isHidden !== 'undefined') {
144
+ attrs['is_hidden'] = product.isHidden.toString();
145
+ }
146
+ const node = {
147
+ tag: 'product',
148
+ attrs,
149
+ content
150
+ };
151
+ return node;
152
+ };
153
+ export const parseProductNode = (productNode) => {
154
+ const isHidden = productNode.attrs.is_hidden === 'true';
155
+ const id = getBinaryNodeChildString(productNode, 'id');
156
+ const mediaNode = getBinaryNodeChild(productNode, 'media');
157
+ const statusInfoNode = getBinaryNodeChild(productNode, 'status_info');
158
+ const product = {
159
+ id,
160
+ imageUrls: parseImageUrls(mediaNode),
161
+ reviewStatus: {
162
+ whatsapp: getBinaryNodeChildString(statusInfoNode, 'status')
163
+ },
164
+ availability: 'in stock',
165
+ name: getBinaryNodeChildString(productNode, 'name'),
166
+ retailerId: getBinaryNodeChildString(productNode, 'retailer_id'),
167
+ url: getBinaryNodeChildString(productNode, 'url'),
168
+ description: getBinaryNodeChildString(productNode, 'description'),
169
+ price: +getBinaryNodeChildString(productNode, 'price'),
170
+ currency: getBinaryNodeChildString(productNode, 'currency'),
171
+ isHidden
172
+ };
173
+ return product;
174
+ };
175
+ /**
176
+ * Uploads images not already uploaded to WA's servers
177
+ */
178
+ export async function uploadingNecessaryImagesOfProduct(product, waUploadToServer, timeoutMs = 30000) {
179
+ product = {
180
+ ...product,
181
+ images: product.images
182
+ ? await uploadingNecessaryImages(product.images, waUploadToServer, timeoutMs)
183
+ : product.images
184
+ };
185
+ return product;
186
+ }
187
+ /**
188
+ * Uploads images not already uploaded to WA's servers
189
+ */
190
+ export const uploadingNecessaryImages = async (images, waUploadToServer, timeoutMs = 30000) => {
191
+ const results = await Promise.all(images.map(async (img) => {
192
+ if ('url' in img) {
193
+ const url = img.url.toString();
194
+ if (url.includes('.whatsapp.net')) {
195
+ return { url };
196
+ }
197
+ }
198
+ const { stream } = await getStream(img);
199
+ const hasher = createHash('sha256');
200
+ const filePath = join(tmpdir(), 'img' + generateMessageIDV2());
201
+ const encFileWriteStream = createWriteStream(filePath);
202
+ for await (const block of stream) {
203
+ hasher.update(block);
204
+ encFileWriteStream.write(block);
205
+ }
206
+ const sha = hasher.digest('base64');
207
+ const { directPath } = await waUploadToServer(filePath, {
208
+ mediaType: 'product-catalog-image',
209
+ fileEncSha256B64: sha,
210
+ timeoutMs
211
+ });
212
+ await fs.unlink(filePath).catch(err => console.log('Error deleting temp file ', err));
213
+ return { url: getUrlFromDirectPath(directPath) };
214
+ }));
215
+ return results;
216
+ };
217
+ const parseImageUrls = (mediaNode) => {
218
+ const imgNode = getBinaryNodeChild(mediaNode, 'image');
219
+ return {
220
+ requested: getBinaryNodeChildString(imgNode, 'request_image_url'),
221
+ original: getBinaryNodeChildString(imgNode, 'original_image_url')
222
+ };
223
+ };
224
+ const parseStatusInfo = (mediaNode) => {
225
+ const node = getBinaryNodeChild(mediaNode, 'status_info');
226
+ return {
227
+ status: getBinaryNodeChildString(node, 'status'),
228
+ canAppeal: getBinaryNodeChildString(node, 'can_appeal') === 'true'
229
+ };
230
+ };
231
+ //# sourceMappingURL=business.js.map
@@ -0,0 +1,100 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import type { BaileysEventEmitter, ChatModification, ChatMutation, Contact, InitialAppStateSyncOptions, LTHashState, WAPatchCreate, WAPatchName } from '../Types/index.js';
3
+ import { type BinaryNode } from '../WABinary/index.js';
4
+ import type { ILogger } from './logger.js';
5
+ type FetchAppStateSyncKey = (keyId: string) => Promise<proto.Message.IAppStateSyncKeyData | null | undefined>;
6
+ export type ChatMutationMap = {
7
+ [index: string]: ChatMutation;
8
+ };
9
+ type Mac = {
10
+ indexMac: Uint8Array;
11
+ valueMac: Uint8Array;
12
+ operation: proto.SyncdMutation.SyncdOperation;
13
+ };
14
+ export declare const makeLtHashGenerator: ({ indexValueMap, hash }: Pick<LTHashState, "hash" | "indexValueMap">) => {
15
+ mix: ({ indexMac, valueMac, operation }: Mac) => void;
16
+ finish: () => {
17
+ hash: Buffer<ArrayBuffer>;
18
+ indexValueMap: {
19
+ [indexMacBase64: string]: {
20
+ valueMac: Uint8Array | Buffer;
21
+ };
22
+ };
23
+ };
24
+ };
25
+ export declare const newLTHashState: () => LTHashState;
26
+ export declare const ensureLTHashStateVersion: (state: LTHashState) => LTHashState;
27
+ export declare const MAX_SYNC_ATTEMPTS = 2;
28
+ /**
29
+ * Check if an error is a missing app state sync key.
30
+ * WA Web treats these as "Blocked" (waits for key arrival), not fatal.
31
+ * In Baileys we retry with a snapshot which may use a different key.
32
+ */
33
+ export declare const isMissingKeyError: (error: any) => boolean;
34
+ /**
35
+ * Determines if an app state sync error is unrecoverable.
36
+ * TypeError indicates a WASM crash; otherwise we give up after MAX_SYNC_ATTEMPTS.
37
+ * Missing keys are NOT checked here — they are handled separately as "Blocked".
38
+ */
39
+ export declare const isAppStateSyncIrrecoverable: (error: any, attempts: number) => boolean;
40
+ export declare const encodeSyncdPatch: ({ type, index, syncAction, apiVersion, operation }: WAPatchCreate, myAppStateKeyId: string, state: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey) => Promise<{
41
+ patch: proto.ISyncdPatch;
42
+ state: LTHashState;
43
+ }>;
44
+ export declare const decodeSyncdMutations: (msgMutations: (proto.ISyncdMutation | proto.ISyncdRecord)[], initialState: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, onMutation: (mutation: ChatMutation) => void, validateMacs: boolean) => Promise<{
45
+ hash: Buffer<ArrayBuffer>;
46
+ indexValueMap: {
47
+ [indexMacBase64: string]: {
48
+ valueMac: Uint8Array | Buffer;
49
+ };
50
+ };
51
+ }>;
52
+ export declare const decodeSyncdPatch: (msg: proto.ISyncdPatch, name: WAPatchName, initialState: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, onMutation: (mutation: ChatMutation) => void, validateMacs: boolean) => Promise<{
53
+ hash: Buffer<ArrayBuffer>;
54
+ indexValueMap: {
55
+ [indexMacBase64: string]: {
56
+ valueMac: Uint8Array | Buffer;
57
+ };
58
+ };
59
+ }>;
60
+ export declare const extractSyncdPatches: (result: BinaryNode, options: RequestInit) => Promise<{
61
+ critical_unblock_low: {
62
+ patches: proto.ISyncdPatch[];
63
+ hasMorePatches: boolean;
64
+ snapshot?: proto.ISyncdSnapshot;
65
+ };
66
+ regular_high: {
67
+ patches: proto.ISyncdPatch[];
68
+ hasMorePatches: boolean;
69
+ snapshot?: proto.ISyncdSnapshot;
70
+ };
71
+ regular_low: {
72
+ patches: proto.ISyncdPatch[];
73
+ hasMorePatches: boolean;
74
+ snapshot?: proto.ISyncdSnapshot;
75
+ };
76
+ critical_block: {
77
+ patches: proto.ISyncdPatch[];
78
+ hasMorePatches: boolean;
79
+ snapshot?: proto.ISyncdSnapshot;
80
+ };
81
+ regular: {
82
+ patches: proto.ISyncdPatch[];
83
+ hasMorePatches: boolean;
84
+ snapshot?: proto.ISyncdSnapshot;
85
+ };
86
+ }>;
87
+ export declare const downloadExternalBlob: (blob: proto.IExternalBlobReference, options: RequestInit) => Promise<Buffer<ArrayBuffer>>;
88
+ export declare const downloadExternalPatch: (blob: proto.IExternalBlobReference, options: RequestInit) => Promise<proto.SyncdMutations>;
89
+ export declare const decodeSyncdSnapshot: (name: WAPatchName, snapshot: proto.ISyncdSnapshot, getAppStateSyncKey: FetchAppStateSyncKey, minimumVersionNumber: number | undefined, validateMacs?: boolean, logger?: ILogger) => Promise<{
90
+ state: LTHashState;
91
+ mutationMap: ChatMutationMap;
92
+ }>;
93
+ export declare const decodePatches: (name: WAPatchName, syncds: proto.ISyncdPatch[], initial: LTHashState, getAppStateSyncKey: FetchAppStateSyncKey, options: RequestInit, minimumVersionNumber?: number, logger?: ILogger, validateMacs?: boolean) => Promise<{
94
+ state: LTHashState;
95
+ mutationMap: ChatMutationMap;
96
+ }>;
97
+ export declare const chatModificationToAppPatch: (mod: ChatModification, jid: string) => WAPatchCreate;
98
+ export declare const processSyncAction: (syncAction: ChatMutation, ev: BaileysEventEmitter, me: Contact, initialSyncOpts?: InitialAppStateSyncOptions, logger?: ILogger) => void;
99
+ export {};
100
+ //# sourceMappingURL=chat-utils.d.ts.map