@hbmodsofc/baileys 1.5.2 → 1.7.7

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 (155) hide show
  1. package/LICENSE +1 -1
  2. package/WAProto/index.js +19671 -152026
  3. package/lib/Defaults/index.d.ts +12 -8
  4. package/lib/Defaults/index.js +90 -124
  5. package/lib/Signal/Group/group_cipher.d.ts +0 -1
  6. package/lib/Signal/Group/group_cipher.js +28 -39
  7. package/lib/Signal/Group/sender-chain-key.d.ts +1 -1
  8. package/lib/Signal/Group/sender-chain-key.js +9 -2
  9. package/lib/Signal/Group/sender-key-distribution-message.js +3 -3
  10. package/lib/Signal/Group/sender-key-message.js +3 -3
  11. package/lib/Signal/Group/sender-key-state.d.ts +4 -4
  12. package/lib/Signal/Group/sender-key-state.js +47 -16
  13. package/lib/Signal/libsignal.d.ts +7 -3
  14. package/lib/Signal/libsignal.js +224 -39
  15. package/lib/Signal/lid-mapping.d.ts +26 -0
  16. package/lib/Signal/lid-mapping.js +146 -0
  17. package/lib/Socket/Client/index.d.ts +2 -3
  18. package/lib/Socket/Client/index.js +2 -3
  19. package/lib/Socket/Client/{abstract-socket-client.d.ts → types.d.ts} +1 -3
  20. package/lib/Socket/Client/{web-socket-client.d.ts → websocket.d.ts} +1 -1
  21. package/lib/Socket/Client/{web-socket-client.js → websocket.js} +10 -16
  22. package/lib/Socket/business.d.ts +94 -78
  23. package/lib/Socket/business.js +130 -11
  24. package/lib/Socket/chats.d.ts +63 -233
  25. package/lib/Socket/chats.js +234 -184
  26. package/lib/Socket/communities.d.ts +232 -0
  27. package/lib/Socket/communities.js +402 -0
  28. package/lib/Socket/groups.d.ts +62 -41
  29. package/lib/Socket/groups.js +76 -64
  30. package/lib/Socket/index.d.ts +129 -83
  31. package/lib/Socket/index.js +13 -6
  32. package/lib/Socket/messages-recv.d.ts +59 -48
  33. package/lib/Socket/messages-recv.js +516 -371
  34. package/lib/Socket/messages-send.d.ts +86 -67
  35. package/lib/Socket/messages-send.js +1091 -1
  36. package/lib/Socket/mex.d.ts +2 -0
  37. package/lib/Socket/mex.js +45 -0
  38. package/lib/Socket/newsletter.d.ts +76 -64
  39. package/lib/Socket/newsletter.js +184 -1
  40. package/lib/Socket/socket.d.ts +19 -13
  41. package/lib/Socket/socket.js +805 -1
  42. package/lib/Types/Auth.d.ts +4 -10
  43. package/lib/Types/Bussines.d.ts +24 -0
  44. package/lib/Types/Bussines.js +2 -0
  45. package/lib/Types/Call.d.ts +1 -1
  46. package/lib/Types/Chat.d.ts +29 -9
  47. package/lib/Types/Chat.js +7 -1
  48. package/lib/Types/Contact.d.ts +5 -1
  49. package/lib/Types/Events.d.ts +55 -14
  50. package/lib/Types/GroupMetadata.d.ts +15 -5
  51. package/lib/Types/Label.d.ts +11 -0
  52. package/lib/Types/Label.js +1 -1
  53. package/lib/Types/LabelAssociation.js +1 -1
  54. package/lib/Types/Message.d.ts +75 -49
  55. package/lib/Types/Message.js +10 -7
  56. package/lib/Types/Newsletter.d.ts +129 -98
  57. package/lib/Types/Newsletter.js +33 -38
  58. package/lib/Types/Product.d.ts +1 -1
  59. package/lib/Types/Signal.d.ts +29 -1
  60. package/lib/Types/Socket.d.ts +48 -22
  61. package/lib/Types/State.d.ts +13 -2
  62. package/lib/Types/State.js +12 -0
  63. package/lib/Types/USync.d.ts +1 -1
  64. package/lib/Types/index.d.ts +10 -3
  65. package/lib/Types/index.js +2 -2
  66. package/lib/Utils/auth-utils.d.ts +3 -3
  67. package/lib/Utils/auth-utils.js +378 -102
  68. package/lib/Utils/baileys-event-stream.js +1 -1
  69. package/lib/Utils/business.d.ts +2 -2
  70. package/lib/Utils/business.js +19 -13
  71. package/lib/Utils/chat-utils.d.ts +21 -22
  72. package/lib/Utils/chat-utils.js +201 -154
  73. package/lib/Utils/crypto.d.ts +18 -19
  74. package/lib/Utils/crypto.js +78 -37
  75. package/lib/Utils/decode-wa-message.d.ts +34 -7
  76. package/lib/Utils/decode-wa-message.js +138 -66
  77. package/lib/Utils/event-buffer.d.ts +6 -8
  78. package/lib/Utils/event-buffer.js +81 -43
  79. package/lib/Utils/generics.d.ts +27 -27
  80. package/lib/Utils/generics.js +128 -133
  81. package/lib/Utils/history.d.ts +9 -5
  82. package/lib/Utils/history.js +17 -23
  83. package/lib/Utils/index.d.ts +2 -0
  84. package/lib/Utils/index.js +2 -0
  85. package/lib/Utils/lidToJid-test.d.ts +11 -0
  86. package/lib/Utils/lidToJid-test.js +27 -0
  87. package/lib/Utils/link-preview.d.ts +4 -4
  88. package/lib/Utils/link-preview.js +40 -12
  89. package/lib/Utils/logger.d.ts +11 -3
  90. package/lib/Utils/lt-hash.d.ts +8 -8
  91. package/lib/Utils/lt-hash.js +23 -24
  92. package/lib/Utils/make-mutex.d.ts +2 -2
  93. package/lib/Utils/make-mutex.js +3 -2
  94. package/lib/Utils/message-retry-manager.d.ts +81 -0
  95. package/lib/Utils/message-retry-manager.js +152 -0
  96. package/lib/Utils/messages-media.d.ts +37 -41
  97. package/lib/Utils/messages-media.js +252 -368
  98. package/lib/Utils/messages.d.ts +13 -15
  99. package/lib/Utils/messages.js +274 -261
  100. package/lib/Utils/noise-handler.d.ts +13 -15
  101. package/lib/Utils/noise-handler.js +20 -26
  102. package/lib/Utils/process-message.d.ts +9 -8
  103. package/lib/Utils/process-message.js +157 -93
  104. package/lib/Utils/signal.d.ts +6 -5
  105. package/lib/Utils/signal.js +37 -29
  106. package/lib/Utils/use-multi-file-auth-state.d.ts +1 -2
  107. package/lib/Utils/use-multi-file-auth-state.js +12 -7
  108. package/lib/Utils/validate-connection.d.ts +5 -6
  109. package/lib/Utils/validate-connection.js +39 -97
  110. package/lib/WABinary/constants.d.ts +24 -27
  111. package/lib/WABinary/constants.js +1276 -13
  112. package/lib/WABinary/decode.d.ts +3 -4
  113. package/lib/WABinary/decode.js +28 -14
  114. package/lib/WABinary/encode.d.ts +1 -2
  115. package/lib/WABinary/encode.js +134 -147
  116. package/lib/WABinary/generic-utils.d.ts +4 -7
  117. package/lib/WABinary/generic-utils.js +40 -125
  118. package/lib/WABinary/jid-utils.d.ts +13 -8
  119. package/lib/WABinary/jid-utils.js +27 -16
  120. package/lib/WAM/BinaryInfo.d.ts +2 -11
  121. package/lib/WAM/constants.d.ts +3 -2
  122. package/lib/WAM/constants.js +2252 -2359
  123. package/lib/WAM/encode.d.ts +1 -2
  124. package/lib/WAM/encode.js +8 -11
  125. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +2 -2
  126. package/lib/WAUSync/Protocols/USyncContactProtocol.js +3 -4
  127. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +2 -2
  128. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +5 -5
  129. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +2 -2
  130. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +5 -5
  131. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +2 -2
  132. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +5 -6
  133. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +2 -2
  134. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +1 -1
  135. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +4 -3
  136. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +11 -3
  137. package/lib/WAUSync/USyncQuery.d.ts +2 -2
  138. package/lib/WAUSync/USyncQuery.js +19 -15
  139. package/lib/WAUSync/USyncUser.d.ts +5 -5
  140. package/lib/WAUSync/index.d.ts +1 -1
  141. package/lib/WAUSync/index.js +1 -1
  142. package/package.json +102 -102
  143. package/lib/Defaults/baileys-version.json +0 -3
  144. package/lib/Defaults/phonenumber-mcc.json +0 -223
  145. package/lib/Signal/Group/queue-job.d.ts +0 -1
  146. package/lib/Signal/Group/queue-job.js +0 -57
  147. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
  148. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  149. package/lib/Socket/hbmods.d.ts +0 -253
  150. package/lib/Socket/hbmods.js +0 -1
  151. package/lib/Socket/registration.d.ts +0 -267
  152. package/lib/Socket/registration.js +0 -166
  153. package/lib/Socket/usync.d.ts +0 -36
  154. package/lib/Socket/usync.js +0 -70
  155. /package/lib/Socket/Client/{abstract-socket-client.js → types.js} +0 -0
@@ -1,4 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.getUrlInfo = void 0;
4
37
  const messages_1 = require("./messages");
@@ -20,12 +53,11 @@ const getUrlInfo = async (text, opts = {
20
53
  thumbnailWidth: THUMBNAIL_WIDTH_PX,
21
54
  fetchOpts: { timeout: 3000 }
22
55
  }) => {
23
- var _a;
24
56
  try {
25
57
  // retries
26
58
  const retries = 0;
27
59
  const maxRetry = 5;
28
- const { getLinkPreview } = await import('link-preview-js');
60
+ const { getLinkPreview } = await Promise.resolve().then(() => __importStar(require('link-preview-js')));
29
61
  let previewLink = text;
30
62
  if (!text.startsWith('https://') && !text.startsWith('http://')) {
31
63
  previewLink = 'https://' + previewLink;
@@ -39,9 +71,9 @@ const getUrlInfo = async (text, opts = {
39
71
  if (retries >= maxRetry) {
40
72
  return false;
41
73
  }
42
- if (forwardedURLObj.hostname === urlObj.hostname
43
- || forwardedURLObj.hostname === 'www.' + urlObj.hostname
44
- || 'www.' + forwardedURLObj.hostname === urlObj.hostname) {
74
+ if (forwardedURLObj.hostname === urlObj.hostname ||
75
+ forwardedURLObj.hostname === 'www.' + urlObj.hostname ||
76
+ 'www.' + forwardedURLObj.hostname === urlObj.hostname) {
45
77
  retries + 1;
46
78
  return true;
47
79
  }
@@ -66,19 +98,15 @@ const getUrlInfo = async (text, opts = {
66
98
  mediaTypeOverride: 'thumbnail-link',
67
99
  options: opts.fetchOpts
68
100
  });
69
- urlInfo.jpegThumbnail = (imageMessage === null || imageMessage === void 0 ? void 0 : imageMessage.jpegThumbnail)
70
- ? Buffer.from(imageMessage.jpegThumbnail)
71
- : undefined;
101
+ urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined;
72
102
  urlInfo.highQualityThumbnail = imageMessage || undefined;
73
103
  }
74
104
  else {
75
105
  try {
76
- urlInfo.jpegThumbnail = image
77
- ? (await getCompressedJpegThumbnail(image, opts)).buffer
78
- : undefined;
106
+ urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined;
79
107
  }
80
108
  catch (error) {
81
- (_a = opts.logger) === null || _a === void 0 ? void 0 : _a.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail');
109
+ opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail');
82
110
  }
83
111
  }
84
112
  return urlInfo;
@@ -1,4 +1,12 @@
1
- declare const _default: import("pino").Logger<{
2
- timestamp: () => string;
3
- }>;
1
+ import P from 'pino';
2
+ export interface ILogger {
3
+ level: string;
4
+ child(obj: Record<string, unknown>): ILogger;
5
+ trace(obj: unknown, msg?: string): void;
6
+ debug(obj: unknown, msg?: string): void;
7
+ info(obj: unknown, msg?: string): void;
8
+ warn(obj: unknown, msg?: string): void;
9
+ error(obj: unknown, msg?: string): void;
10
+ }
11
+ declare const _default: P.Logger<never, boolean>;
4
12
  export default _default;
@@ -1,12 +1,12 @@
1
- declare class d {
1
+ declare class LTHash {
2
2
  salt: string;
3
3
  constructor(e: string);
4
- add(e: any, t: any): any;
5
- subtract(e: any, t: any): any;
6
- subtractThenAdd(e: any, t: any, r: any): any;
7
- _addSingle(e: any, t: any): ArrayBuffer;
8
- _subtractSingle(e: any, t: any): ArrayBuffer;
9
- performPointwiseWithOverflow(e: any, t: any, r: any): ArrayBuffer;
4
+ add(e: ArrayBuffer, t: ArrayBuffer[]): Promise<ArrayBuffer>;
5
+ subtract(e: ArrayBuffer, t: ArrayBuffer[]): Promise<ArrayBuffer>;
6
+ subtractThenAdd(e: ArrayBuffer, addList: ArrayBuffer[], subtractList: ArrayBuffer[]): Promise<ArrayBuffer>;
7
+ private _addSingle;
8
+ private _subtractSingle;
9
+ private performPointwiseWithOverflow;
10
10
  }
11
- export declare const LT_HASH_ANTI_TAMPERING: d;
11
+ export declare const LT_HASH_ANTI_TAMPERING: LTHash;
12
12
  export {};
@@ -8,44 +8,43 @@ const crypto_1 = require("./crypto");
8
8
  * if the same series of mutations was made sequentially.
9
9
  */
10
10
  const o = 128;
11
- class d {
11
+ class LTHash {
12
12
  constructor(e) {
13
13
  this.salt = e;
14
14
  }
15
- add(e, t) {
16
- var r = this;
15
+ async add(e, t) {
17
16
  for (const item of t) {
18
- e = r._addSingle(e, item);
17
+ e = await this._addSingle(e, item);
19
18
  }
20
19
  return e;
21
20
  }
22
- subtract(e, t) {
23
- var r = this;
21
+ async subtract(e, t) {
24
22
  for (const item of t) {
25
- e = r._subtractSingle(e, item);
23
+ e = await this._subtractSingle(e, item);
26
24
  }
27
25
  return e;
28
26
  }
29
- subtractThenAdd(e, t, r) {
30
- var n = this;
31
- return n.add(n.subtract(e, r), t);
27
+ async subtractThenAdd(e, addList, subtractList) {
28
+ const subtracted = await this.subtract(e, subtractList);
29
+ return this.add(subtracted, addList);
32
30
  }
33
- _addSingle(e, t) {
34
- var r = this;
35
- const n = new Uint8Array((0, crypto_1.hkdf)(Buffer.from(t), o, { info: r.salt })).buffer;
36
- return r.performPointwiseWithOverflow(e, n, ((e, t) => e + t));
31
+ async _addSingle(e, t) {
32
+ const derived = new Uint8Array(await (0, crypto_1.hkdf)(Buffer.from(t), o, { info: this.salt })).buffer;
33
+ return this.performPointwiseWithOverflow(e, derived, (a, b) => a + b);
37
34
  }
38
- _subtractSingle(e, t) {
39
- var r = this;
40
- const n = new Uint8Array((0, crypto_1.hkdf)(Buffer.from(t), o, { info: r.salt })).buffer;
41
- return r.performPointwiseWithOverflow(e, n, ((e, t) => e - t));
35
+ async _subtractSingle(e, t) {
36
+ const derived = new Uint8Array(await (0, crypto_1.hkdf)(Buffer.from(t), o, { info: this.salt })).buffer;
37
+ return this.performPointwiseWithOverflow(e, derived, (a, b) => a - b);
42
38
  }
43
- performPointwiseWithOverflow(e, t, r) {
44
- const n = new DataView(e), i = new DataView(t), a = new ArrayBuffer(n.byteLength), s = new DataView(a);
45
- for (let e = 0; e < n.byteLength; e += 2) {
46
- s.setUint16(e, r(n.getUint16(e, !0), i.getUint16(e, !0)), !0);
39
+ performPointwiseWithOverflow(e, t, op) {
40
+ const n = new DataView(e);
41
+ const i = new DataView(t);
42
+ const out = new ArrayBuffer(n.byteLength);
43
+ const s = new DataView(out);
44
+ for (let offset = 0; offset < n.byteLength; offset += 2) {
45
+ s.setUint16(offset, op(n.getUint16(offset, true), i.getUint16(offset, true)), true);
47
46
  }
48
- return a;
47
+ return out;
49
48
  }
50
49
  }
51
- exports.LT_HASH_ANTI_TAMPERING = new d('WhatsApp Patch Integrity');
50
+ exports.LT_HASH_ANTI_TAMPERING = new LTHash('WhatsApp Patch Integrity');
@@ -1,7 +1,7 @@
1
1
  export declare const makeMutex: () => {
2
- mutex<T>(code: () => T | Promise<T>): Promise<T>;
2
+ mutex<T>(code: () => Promise<T> | T): Promise<T>;
3
3
  };
4
4
  export type Mutex = ReturnType<typeof makeMutex>;
5
5
  export declare const makeKeyedMutex: () => {
6
- mutex<T>(key: string, task: () => T | Promise<T>): Promise<T>;
6
+ mutex<T>(key: string, task: () => Promise<T> | T): Promise<T>;
7
7
  };
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.makeKeyedMutex = exports.makeMutex = void 0;
4
4
  const makeMutex = () => {
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
6
  let task = Promise.resolve();
6
7
  let taskTimeout;
7
8
  return {
@@ -12,7 +13,7 @@ const makeMutex = () => {
12
13
  try {
13
14
  await task;
14
15
  }
15
- catch (_a) { }
16
+ catch { }
16
17
  try {
17
18
  // execute the current task
18
19
  const result = await code();
@@ -25,7 +26,7 @@ const makeMutex = () => {
25
26
  // we replace the existing task, appending the new piece of execution to it
26
27
  // so the next task will have to wait for this one to finish
27
28
  return task;
28
- },
29
+ }
29
30
  };
30
31
  };
31
32
  exports.makeMutex = makeMutex;
@@ -0,0 +1,81 @@
1
+ import type { proto } from '../../WAProto/index.js';
2
+ import type { ILogger } from './logger';
3
+ export interface RecentMessageKey {
4
+ to: string;
5
+ id: string;
6
+ }
7
+ export interface RecentMessage {
8
+ message: proto.IMessage;
9
+ timestamp: number;
10
+ }
11
+ export interface SessionRecreateHistory {
12
+ [jid: string]: number;
13
+ }
14
+ export interface RetryCounter {
15
+ [messageId: string]: number;
16
+ }
17
+ export interface PendingPhoneRequest {
18
+ [messageId: string]: NodeJS.Timeout;
19
+ }
20
+ export interface RetryStatistics {
21
+ totalRetries: number;
22
+ successfulRetries: number;
23
+ failedRetries: number;
24
+ mediaRetries: number;
25
+ sessionRecreations: number;
26
+ phoneRequests: number;
27
+ }
28
+ export declare class MessageRetryManager {
29
+ private logger;
30
+ private recentMessagesMap;
31
+ private sessionRecreateHistory;
32
+ private retryCounters;
33
+ private pendingPhoneRequests;
34
+ private readonly maxMsgRetryCount;
35
+ private statistics;
36
+ constructor(logger: ILogger, maxMsgRetryCount: number);
37
+ /**
38
+ * Add a recent message to the cache for retry handling
39
+ */
40
+ addRecentMessage(to: string, id: string, message: proto.IMessage): void;
41
+ /**
42
+ * Get a recent message from the cache
43
+ */
44
+ getRecentMessage(to: string, id: string): RecentMessage | undefined;
45
+ /**
46
+ * Check if a session should be recreated based on retry count and history
47
+ */
48
+ shouldRecreateSession(jid: string, retryCount: number, hasSession: boolean): {
49
+ reason: string;
50
+ recreate: boolean;
51
+ };
52
+ /**
53
+ * Increment retry counter for a message
54
+ */
55
+ incrementRetryCount(messageId: string): number;
56
+ /**
57
+ * Get retry count for a message
58
+ */
59
+ getRetryCount(messageId: string): number;
60
+ /**
61
+ * Check if message has exceeded maximum retry attempts
62
+ */
63
+ hasExceededMaxRetries(messageId: string): boolean;
64
+ /**
65
+ * Mark retry as successful
66
+ */
67
+ markRetrySuccess(messageId: string): void;
68
+ /**
69
+ * Mark retry as failed
70
+ */
71
+ markRetryFailed(messageId: string): void;
72
+ /**
73
+ * Schedule a phone request with delay
74
+ */
75
+ schedulePhoneRequest(messageId: string, callback: () => void, delay?: number): void;
76
+ /**
77
+ * Cancel pending phone request
78
+ */
79
+ cancelPendingPhoneRequest(messageId: string): void;
80
+ private keyToString;
81
+ }
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageRetryManager = void 0;
4
+ const lru_cache_1 = require("lru-cache");
5
+ /** Number of sent messages to cache in memory for handling retry receipts */
6
+ const RECENT_MESSAGES_SIZE = 512;
7
+ /** Timeout for session recreation - 1 hour */
8
+ const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
9
+ const PHONE_REQUEST_DELAY = 3000;
10
+ class MessageRetryManager {
11
+ constructor(logger, maxMsgRetryCount) {
12
+ this.logger = logger;
13
+ this.recentMessagesMap = new lru_cache_1.LRUCache({
14
+ max: RECENT_MESSAGES_SIZE
15
+ });
16
+ this.sessionRecreateHistory = new lru_cache_1.LRUCache({
17
+ ttl: RECREATE_SESSION_TIMEOUT * 2,
18
+ ttlAutopurge: true
19
+ });
20
+ this.retryCounters = new lru_cache_1.LRUCache({
21
+ ttl: 15 * 60 * 1000,
22
+ ttlAutopurge: true,
23
+ updateAgeOnGet: true
24
+ }); // 15 minutes TTL
25
+ this.pendingPhoneRequests = {};
26
+ this.maxMsgRetryCount = 5;
27
+ this.statistics = {
28
+ totalRetries: 0,
29
+ successfulRetries: 0,
30
+ failedRetries: 0,
31
+ mediaRetries: 0,
32
+ sessionRecreations: 0,
33
+ phoneRequests: 0
34
+ };
35
+ this.maxMsgRetryCount = maxMsgRetryCount;
36
+ }
37
+ /**
38
+ * Add a recent message to the cache for retry handling
39
+ */
40
+ addRecentMessage(to, id, message) {
41
+ const key = { to, id };
42
+ const keyStr = this.keyToString(key);
43
+ // Add new message
44
+ this.recentMessagesMap.set(keyStr, {
45
+ message,
46
+ timestamp: Date.now()
47
+ });
48
+ this.logger.debug(`Added message to retry cache: ${to}/${id}`);
49
+ }
50
+ /**
51
+ * Get a recent message from the cache
52
+ */
53
+ getRecentMessage(to, id) {
54
+ const key = { to, id };
55
+ const keyStr = this.keyToString(key);
56
+ return this.recentMessagesMap.get(keyStr);
57
+ }
58
+ /**
59
+ * Check if a session should be recreated based on retry count and history
60
+ */
61
+ shouldRecreateSession(jid, retryCount, hasSession) {
62
+ // If we don't have a session, always recreate
63
+ if (!hasSession) {
64
+ this.sessionRecreateHistory.set(jid, Date.now());
65
+ this.statistics.sessionRecreations++;
66
+ return {
67
+ reason: "we don't have a Signal session with them",
68
+ recreate: true
69
+ };
70
+ }
71
+ // Only consider recreation if retry count > 1
72
+ if (retryCount < 2) {
73
+ return { reason: '', recreate: false };
74
+ }
75
+ const now = Date.now();
76
+ const prevTime = this.sessionRecreateHistory.get(jid);
77
+ // If no previous recreation or it's been more than an hour
78
+ if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
79
+ this.sessionRecreateHistory.set(jid, now);
80
+ this.statistics.sessionRecreations++;
81
+ return {
82
+ reason: 'retry count > 1 and over an hour since last recreation',
83
+ recreate: true
84
+ };
85
+ }
86
+ return { reason: '', recreate: false };
87
+ }
88
+ /**
89
+ * Increment retry counter for a message
90
+ */
91
+ incrementRetryCount(messageId) {
92
+ this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
93
+ this.statistics.totalRetries++;
94
+ return this.retryCounters.get(messageId);
95
+ }
96
+ /**
97
+ * Get retry count for a message
98
+ */
99
+ getRetryCount(messageId) {
100
+ return this.retryCounters.get(messageId) || 0;
101
+ }
102
+ /**
103
+ * Check if message has exceeded maximum retry attempts
104
+ */
105
+ hasExceededMaxRetries(messageId) {
106
+ return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
107
+ }
108
+ /**
109
+ * Mark retry as successful
110
+ */
111
+ markRetrySuccess(messageId) {
112
+ this.statistics.successfulRetries++;
113
+ // Clean up retry counter for successful message
114
+ this.retryCounters.delete(messageId);
115
+ this.cancelPendingPhoneRequest(messageId);
116
+ }
117
+ /**
118
+ * Mark retry as failed
119
+ */
120
+ markRetryFailed(messageId) {
121
+ this.statistics.failedRetries++;
122
+ this.retryCounters.delete(messageId);
123
+ }
124
+ /**
125
+ * Schedule a phone request with delay
126
+ */
127
+ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
128
+ // Cancel any existing request for this message
129
+ this.cancelPendingPhoneRequest(messageId);
130
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
131
+ delete this.pendingPhoneRequests[messageId];
132
+ this.statistics.phoneRequests++;
133
+ callback();
134
+ }, delay);
135
+ this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
136
+ }
137
+ /**
138
+ * Cancel pending phone request
139
+ */
140
+ cancelPendingPhoneRequest(messageId) {
141
+ const timeout = this.pendingPhoneRequests[messageId];
142
+ if (timeout) {
143
+ clearTimeout(timeout);
144
+ delete this.pendingPhoneRequests[messageId];
145
+ this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
146
+ }
147
+ }
148
+ keyToString(key) {
149
+ return `${key.to}:${key.id}`;
150
+ }
151
+ }
152
+ exports.MessageRetryManager = MessageRetryManager;
@@ -1,27 +1,33 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
1
  import { Boom } from '@hapi/boom';
5
- import { AxiosRequestConfig } from 'axios';
6
- import type { Logger } from 'pino';
2
+ import { type AxiosRequestConfig } from 'axios';
7
3
  import { Readable, Transform } from 'stream';
8
4
  import { URL } from 'url';
9
- import { proto } from '../../WAProto';
10
- import { DownloadableMessage, MediaConnInfo, MediaDecryptionKeyInfo, MediaType, SocketConfig, WAMediaUpload, WAMediaUploadFunction, WAMessageContent } from '../Types';
11
- import { BinaryNode } from '../WABinary';
5
+ import { proto } from '../../WAProto/index.js';
6
+ import { type MediaType } from '../Defaults';
7
+ import type { DownloadableMessage, MediaConnInfo, MediaDecryptionKeyInfo, SocketConfig, WAMediaUpload, WAMediaUploadFunction, WAMessageContent } from '../Types';
8
+ import { type BinaryNode } from '../WABinary';
9
+ import type { ILogger } from './logger';
12
10
  export declare const hkdfInfoKey: (type: MediaType) => string;
11
+ export declare const getRawMediaUploadData: (media: WAMediaUpload, mediaType: MediaType, logger?: ILogger) => Promise<{
12
+ filePath: string;
13
+ fileSha256: Buffer<ArrayBufferLike>;
14
+ fileLength: number;
15
+ }>;
13
16
  /** generates all the keys required to encrypt/decrypt & sign a media message */
14
- export declare function getMediaKeys(buffer: Uint8Array | string | null | undefined, mediaType: MediaType): MediaDecryptionKeyInfo;
17
+ export declare function getMediaKeys(buffer: Uint8Array | string | null | undefined, mediaType: MediaType): Promise<MediaDecryptionKeyInfo>;
15
18
  export declare const extractImageThumb: (bufferOrFilePath: Readable | Buffer | string, width?: number) => Promise<{
16
- buffer: Buffer;
19
+ buffer: any;
17
20
  original: {
18
- width: number | undefined;
19
- height: number | undefined;
21
+ width: any;
22
+ height: any;
20
23
  };
21
24
  }>;
22
25
  export declare const encodeBase64EncodedStringForUpload: (b64: string) => string;
23
- export declare const generateProfilePicture: (mediaUpload: WAMediaUpload) => Promise<{
24
- img: Buffer;
26
+ export declare const generateProfilePicture: (mediaUpload: WAMediaUpload, dimensions?: {
27
+ width: number;
28
+ height: number;
29
+ }) => Promise<{
30
+ img: Buffer<ArrayBufferLike>;
25
31
  }>;
26
32
  /** gets the SHA256 of the given media message */
27
33
  export declare const mediaMessageSHA256B64: (message: WAMessageContent) => string | null | undefined;
@@ -29,9 +35,9 @@ export declare function getAudioDuration(buffer: Buffer | string | Readable): Pr
29
35
  /**
30
36
  referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
31
37
  */
32
- export declare function getAudioWaveform(buffer: Buffer | string | Readable, logger?: Logger): Promise<Uint8Array | undefined>;
38
+ export declare function getAudioWaveform(buffer: Buffer | string | Readable, logger?: ILogger): Promise<Uint8Array<ArrayBuffer> | undefined>;
33
39
  export declare const toReadable: (buffer: Buffer) => Readable;
34
- export declare const toBuffer: (stream: Readable) => Promise<Buffer>;
40
+ export declare const toBuffer: (stream: Readable) => Promise<Buffer<ArrayBuffer>>;
35
41
  export declare const getStream: (item: WAMediaUpload, opts?: AxiosRequestConfig) => Promise<{
36
42
  readonly stream: Readable;
37
43
  readonly type: "buffer";
@@ -47,7 +53,7 @@ export declare const getStream: (item: WAMediaUpload, opts?: AxiosRequestConfig)
47
53
  }>;
48
54
  /** generates a thumbnail for a given media, if required */
49
55
  export declare function generateThumbnail(file: string, mediaType: 'video' | 'image', options: {
50
- logger?: Logger;
56
+ logger?: ILogger;
51
57
  }): Promise<{
52
58
  thumbnail: string | undefined;
53
59
  originalImageDimensions: {
@@ -60,32 +66,22 @@ export declare const getHttpStream: (url: string | URL, options?: AxiosRequestCo
60
66
  }) => Promise<Readable>;
61
67
  type EncryptedStreamOptions = {
62
68
  saveOriginalFileIfRequired?: boolean;
63
- logger?: Logger;
69
+ logger?: ILogger;
64
70
  opts?: AxiosRequestConfig;
65
71
  };
66
- export declare const prepareStream: (media: WAMediaUpload, mediaType: MediaType, { logger, saveOriginalFileIfRequired, opts }?: EncryptedStreamOptions) => Promise<{
67
- mediaKey: undefined;
68
- encWriteStream: Buffer;
69
- fileLength: number;
70
- fileSha256: Buffer;
71
- fileEncSha256: undefined;
72
- bodyPath: string | undefined;
73
- didSaveToTmpPath: boolean;
74
- }>;
75
72
  export declare const encryptedStream: (media: WAMediaUpload, mediaType: MediaType, { logger, saveOriginalFileIfRequired, opts }?: EncryptedStreamOptions) => Promise<{
76
- mediaKey: Buffer;
77
- encWriteStream: Readable;
78
- bodyPath: string | undefined;
79
- mac: Buffer;
80
- fileEncSha256: Buffer;
81
- fileSha256: Buffer;
73
+ mediaKey: Buffer<ArrayBufferLike>;
74
+ originalFilePath: string | undefined;
75
+ encFilePath: string;
76
+ mac: Buffer<ArrayBuffer>;
77
+ fileEncSha256: Buffer<ArrayBufferLike>;
78
+ fileSha256: Buffer<ArrayBufferLike>;
82
79
  fileLength: number;
83
- didSaveToTmpPath: boolean;
84
80
  }>;
85
81
  export type MediaDownloadOptions = {
86
82
  startByte?: number;
87
83
  endByte?: number;
88
- options?: AxiosRequestConfig<any>;
84
+ options?: AxiosRequestConfig<{}>;
89
85
  };
90
86
  export declare const getUrlFromDirectPath: (directPath: string) => string;
91
87
  export declare const downloadContentFromMessage: ({ mediaKey, directPath, url }: DownloadableMessage, type: MediaType, opts?: MediaDownloadOptions) => Promise<Transform>;
@@ -99,18 +95,18 @@ export declare const getWAUploadToServer: ({ customUploadHosts, fetchAgent, logg
99
95
  /**
100
96
  * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
101
97
  */
102
- export declare const encryptMediaRetryRequest: (key: proto.IMessageKey, mediaKey: Buffer | Uint8Array, meId: string) => BinaryNode;
98
+ export declare const encryptMediaRetryRequest: (key: proto.IMessageKey, mediaKey: Buffer | Uint8Array, meId: string) => Promise<BinaryNode>;
103
99
  export declare const decodeMediaRetryNode: (node: BinaryNode) => {
104
- key: proto.IMessageKey;
100
+ key: import("../Types").WAMessageKey;
105
101
  media?: {
106
102
  ciphertext: Uint8Array;
107
103
  iv: Uint8Array;
108
- } | undefined;
109
- error?: Boom<any> | undefined;
104
+ };
105
+ error?: Boom;
110
106
  };
111
107
  export declare const decryptMediaRetryData: ({ ciphertext, iv }: {
112
108
  ciphertext: Uint8Array;
113
109
  iv: Uint8Array;
114
- }, mediaKey: Uint8Array, msgId: string) => proto.MediaRetryNotification;
115
- export declare const getStatusCodeForMediaRetry: (code: number) => any;
110
+ }, mediaKey: Uint8Array, msgId: string) => Promise<proto.MediaRetryNotification>;
111
+ export declare const getStatusCodeForMediaRetry: (code: number) => 200 | 412 | 404 | 418;
116
112
  export {};