@pontalabs/baileys 1.0.11 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +96 -85
  2. package/lib/Defaults/connection.js +39 -0
  3. package/lib/Defaults/constants.js +50 -0
  4. package/lib/Defaults/history.js +13 -0
  5. package/lib/Defaults/index.js +36 -151
  6. package/lib/Defaults/media.js +43 -0
  7. package/lib/Defaults/phonenumber-mcc.json +221 -221
  8. package/lib/Defaults/prefix.js +12 -0
  9. package/lib/Defaults/vialeys-version.json +7 -0
  10. package/lib/Signal/Group/ciphertext-message.js +10 -15
  11. package/lib/Signal/Group/group-session-builder.js +34 -60
  12. package/lib/Signal/Group/group_cipher.js +69 -76
  13. package/lib/Signal/Group/index.js +41 -60
  14. package/lib/Signal/Group/keyhelper.js +12 -57
  15. package/lib/Signal/Group/sender-chain-key.js +20 -33
  16. package/lib/Signal/Group/sender-key-distribution-message.js +43 -48
  17. package/lib/Signal/Group/sender-key-message.js +58 -53
  18. package/lib/Signal/Group/sender-key-name.js +21 -31
  19. package/lib/Signal/Group/sender-key-record.js +21 -34
  20. package/lib/Signal/Group/sender-key-state.js +59 -106
  21. package/lib/Signal/Group/sender-message-key.js +16 -21
  22. package/lib/Signal/libsignal.js +282 -203
  23. package/lib/Signal/lid-mapping.js +164 -184
  24. package/lib/Socket/Client/index.js +31 -22
  25. package/lib/Socket/Client/types.js +9 -15
  26. package/lib/Socket/Client/websocket.js +42 -37
  27. package/lib/Socket/business.js +212 -336
  28. package/lib/Socket/chats.js +696 -940
  29. package/lib/Socket/community.js +454 -369
  30. package/lib/Socket/groups.js +278 -280
  31. package/lib/Socket/index.js +9 -21
  32. package/lib/Socket/messages-recv.js +1027 -1570
  33. package/lib/Socket/messages-send.js +1180 -1136
  34. package/lib/Socket/mex.js +59 -0
  35. package/lib/Socket/newsletter.js +193 -251
  36. package/lib/Socket/socket.js +758 -898
  37. package/lib/Store/index.js +35 -24
  38. package/lib/Store/make-cache-manager-store.js +53 -66
  39. package/lib/Store/make-in-memory-store.js +319 -325
  40. package/lib/Store/make-ordered-dictionary.js +49 -57
  41. package/lib/Store/object-repository.js +10 -15
  42. package/lib/Types/Auth.js +2 -3
  43. package/lib/Types/Bussines.js +2 -3
  44. package/lib/Types/Call.js +2 -3
  45. package/lib/Types/Chat.js +10 -9
  46. package/lib/Types/Contact.js +2 -3
  47. package/lib/Types/Events.js +2 -3
  48. package/lib/Types/GroupMetadata.js +2 -3
  49. package/lib/Types/Label.js +6 -12
  50. package/lib/Types/LabelAssociation.js +4 -13
  51. package/lib/Types/Message.js +10 -18
  52. package/lib/Types/MexUpdates.js +15 -18
  53. package/lib/Types/Newsletter.js +26 -34
  54. package/lib/Types/Product.js +2 -3
  55. package/lib/Types/Signal.js +2 -3
  56. package/lib/Types/Socket.js +2 -3
  57. package/lib/Types/State.js +8 -13
  58. package/lib/Types/USync.js +2 -3
  59. package/lib/Types/index.js +49 -40
  60. package/lib/Utils/auth-utils.js +190 -495
  61. package/lib/Utils/browser-utils.js +29 -0
  62. package/lib/Utils/business.js +177 -199
  63. package/lib/Utils/chat-utils.js +711 -624
  64. package/lib/Utils/crypto.js +116 -161
  65. package/lib/Utils/decode-wa-message.js +225 -321
  66. package/lib/Utils/event-buffer.js +363 -405
  67. package/lib/Utils/generics.js +318 -472
  68. package/lib/Utils/history.js +72 -76
  69. package/lib/Utils/index.js +49 -41
  70. package/lib/Utils/link-preview.js +59 -103
  71. package/lib/Utils/logger.js +4 -7
  72. package/lib/Utils/lt-hash.js +29 -42
  73. package/lib/Utils/make-mutex.js +26 -42
  74. package/lib/Utils/message-retry-manager.js +134 -160
  75. package/lib/Utils/messages-media.js +665 -731
  76. package/lib/Utils/messages.js +518 -1
  77. package/lib/Utils/noise-handler.js +127 -125
  78. package/lib/Utils/pre-key-manager.js +86 -0
  79. package/lib/Utils/process-message.js +739 -351
  80. package/lib/Utils/signal.js +146 -129
  81. package/lib/Utils/use-multi-file-auth-state.js +68 -135
  82. package/lib/Utils/validate-connection.js +183 -142
  83. package/lib/Utils/vialeys-event-stream.js +41 -0
  84. package/lib/WABinary/constants.js +1281 -1289
  85. package/lib/WABinary/decode.js +145 -200
  86. package/lib/WABinary/encode.js +149 -202
  87. package/lib/WABinary/generic-utils.js +79 -97
  88. package/lib/WABinary/index.js +36 -25
  89. package/lib/WABinary/jid-utils.js +91 -123
  90. package/lib/WABinary/types.js +2 -3
  91. package/lib/WAM/BinaryInfo.js +8 -13
  92. package/lib/WAM/constants.js +16584 -11464
  93. package/lib/WAM/encode.js +117 -133
  94. package/lib/WAM/index.js +34 -23
  95. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +62 -69
  96. package/lib/WAUSync/Protocols/USyncContactProtocol.js +11 -25
  97. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +32 -44
  98. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +13 -23
  99. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +24 -38
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +17 -31
  101. package/lib/WAUSync/Protocols/index.js +37 -26
  102. package/lib/WAUSync/USyncQuery.js +66 -66
  103. package/lib/WAUSync/USyncUser.js +13 -18
  104. package/lib/WAUSync/index.js +34 -23
  105. package/lib/index.js +95 -28
  106. package/package.json +2 -1
  107. package/lib/Defaults/baileys-version.json +0 -3
  108. package/lib/Signal/Group/queue-job.d.ts +0 -1
  109. package/lib/Signal/Group/queue-job.js +0 -57
  110. package/lib/Socket/usync.d.ts +0 -37
  111. package/lib/Socket/usync.js +0 -83
  112. package/lib/Utils/baileys-event-stream.d.ts +0 -18
  113. package/lib/Utils/baileys-event-stream.js +0 -70
  114. package/lib/Utils/signal-housekeeping.d.ts +0 -19
  115. package/lib/Utils/signal-housekeeping.js +0 -73
  116. package/lib/Utils/use-mongo-file-auth-state.d.ts +0 -6
  117. package/lib/Utils/use-mongo-file-auth-state.js +0 -84
  118. package/lib/Utils/use-single-file-auth-state.d.ts +0 -13
  119. package/lib/Utils/use-single-file-auth-state.js +0 -80
@@ -1,160 +1,134 @@
1
- "use strict"
2
-
3
- Object.defineProperty(exports, "__esModule", { value: true })
4
-
5
- const LRUCache_1 = require("lru-cache")
6
-
7
- /** Number of sent messages to cache in memory for handling retry receipts */
8
- const RECENT_MESSAGES_SIZE = 512
9
-
10
- /** Timeout for session recreation - 1 hour */
11
- const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000 // 1 hour in milliseconds
12
-
13
- const PHONE_REQUEST_DELAY = 3000
14
-
15
- class MessageRetryManager {
16
- constructor(logger, maxMsgRetryCount) {
17
- this.logger = logger
18
- this.recentMessagesMap = new LRUCache_1.LRUCache({
19
- max: RECENT_MESSAGES_SIZE
20
- })
21
- this.sessionRecreateHistory = new LRUCache_1.LRUCache({
22
- ttl: RECREATE_SESSION_TIMEOUT * 2,
23
- ttlAutopurge: true
24
- })
25
- this.retryCounters = new LRUCache_1.LRUCache({
26
- ttl: 15 * 60 * 1000,
27
- ttlAutopurge: true,
28
- updateAgeOnGet: true
29
- }) // 15 minutes TTL
30
- this.pendingPhoneRequests = {}
31
- this.maxMsgRetryCount = 5
32
- this.statistics = {
33
- totalRetries: 0,
34
- successfulRetries: 0,
35
- failedRetries: 0,
36
- mediaRetries: 0,
37
- sessionRecreations: 0,
38
- phoneRequests: 0
39
- }
40
- this.maxMsgRetryCount = maxMsgRetryCount
41
- }
42
- /**
43
- * Add a recent message to the cache for retry handling
44
- */
45
- addRecentMessage(to, id, message) {
46
- const key = { to, id }
47
- const keyStr = this.keyToString(key)
48
- // Add new message
49
- this.recentMessagesMap.set(keyStr, {
50
- message,
51
- timestamp: Date.now()
52
- })
53
- this.logger.debug(`Added message to retry cache: ${to}/${id}`)
54
- }
55
- /**
56
- * Get a recent message from the cache
57
- */
58
- getRecentMessage(to, id) {
59
- const key = { to, id }
60
- const keyStr = this.keyToString(key)
61
- return this.recentMessagesMap.get(keyStr)
62
- }
63
- /**
64
- * Check if a session should be recreated based on retry count and history
65
- */
66
- shouldRecreateSession(jid, retryCount, hasSession) {
67
- // If we don't have a session, always recreate
68
- if (!hasSession) {
69
- this.sessionRecreateHistory.set(jid, Date.now())
70
- this.statistics.sessionRecreations++
71
- return {
72
- reason: "we don't have a Signal session with them",
73
- recreate: true
74
- }
75
- }
76
- // Only consider recreation if retry count > 1
77
- if (retryCount < 2) {
78
- return { reason: '', recreate: false }
79
- }
80
- const now = Date.now()
81
- const prevTime = this.sessionRecreateHistory.get(jid)
82
- // If no previous recreation or it's been more than an hour
83
- if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
84
- this.sessionRecreateHistory.set(jid, now)
85
- this.statistics.sessionRecreations++
86
- return {
87
- reason: 'retry count > 1 and over an hour since last recreation',
88
- recreate: true
89
- }
90
- }
91
- return { reason: '', recreate: false }
92
- }
93
- /**
94
- * Increment retry counter for a message
95
- */
96
- incrementRetryCount(messageId) {
97
- this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1)
98
- this.statistics.totalRetries++
99
- return this.retryCounters.get(messageId)
100
- }
101
- /**
102
- * Get retry count for a message
103
- */
104
- getRetryCount(messageId) {
105
- return this.retryCounters.get(messageId) || 0
106
- }
107
- /**
108
- * Check if message has exceeded maximum retry attempts
109
- */
110
- hasExceededMaxRetries(messageId) {
111
- return this.getRetryCount(messageId) >= this.maxMsgRetryCount
112
- }
113
- /**
114
- * Mark retry as successful
115
- */
116
- markRetrySuccess(messageId) {
117
- this.statistics.successfulRetries++
118
- // Clean up retry counter for successful message
119
- this.retryCounters.delete(messageId)
120
- this.cancelPendingPhoneRequest(messageId)
121
- }
122
- /**
123
- * Mark retry as failed
124
- */
125
- markRetryFailed(messageId) {
126
- this.statistics.failedRetries++
127
- this.retryCounters.delete(messageId)
128
- }
129
- /**
130
- * Schedule a phone request with delay
131
- */
132
- schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
133
- // Cancel any existing request for this message
134
- this.cancelPendingPhoneRequest(messageId)
135
- this.pendingPhoneRequests[messageId] = setTimeout(() => {
136
- delete this.pendingPhoneRequests[messageId]
137
- this.statistics.phoneRequests++
138
- callback()
139
- }, delay)
140
- this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`)
141
- }
142
- /**
143
- * Cancel pending phone request
144
- */
145
- cancelPendingPhoneRequest(messageId) {
146
- const timeout = this.pendingPhoneRequests[messageId]
147
- if (timeout) {
148
- clearTimeout(timeout)
149
- delete this.pendingPhoneRequests[messageId]
150
- this.logger.debug(`Cancelled pending phone request for message ${messageId}`)
151
- }
152
- }
153
- keyToString(key) {
154
- return `${key.to}:${key.id}`
155
- }
156
- }
157
-
158
- module.exports = {
159
- MessageRetryManager
160
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const { LRUCache: LRUCache } = require("lru-cache");
4
+ const RECENT_MESSAGES_SIZE = 512;
5
+ const MESSAGE_KEY_SEPARATOR = "\0";
6
+ const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1e3;
7
+ const PHONE_REQUEST_DELAY = 3e3;
8
+ class MessageRetryManager {
9
+ constructor(logger, maxMsgRetryCount) {
10
+ this.logger = logger;
11
+ this.recentMessagesMap = new LRUCache({
12
+ max: RECENT_MESSAGES_SIZE,
13
+ ttl: 5 * 60 * 1e3,
14
+ ttlAutopurge: true,
15
+ dispose: (_value, key) => {
16
+ const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
17
+ if (separatorIndex > -1) {
18
+ const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
19
+ this.messageKeyIndex.delete(messageId);
20
+ }
21
+ },
22
+ });
23
+ this.messageKeyIndex = new Map();
24
+ this.sessionRecreateHistory = new LRUCache({
25
+ ttl: RECREATE_SESSION_TIMEOUT * 2,
26
+ ttlAutopurge: true,
27
+ });
28
+ this.retryCounters = new LRUCache({
29
+ ttl: 15 * 60 * 1e3,
30
+ ttlAutopurge: true,
31
+ updateAgeOnGet: true,
32
+ });
33
+ this.pendingPhoneRequests = {};
34
+ this.maxMsgRetryCount = 5;
35
+ this.statistics = {
36
+ totalRetries: 0,
37
+ successfulRetries: 0,
38
+ failedRetries: 0,
39
+ mediaRetries: 0,
40
+ sessionRecreations: 0,
41
+ phoneRequests: 0,
42
+ };
43
+ this.maxMsgRetryCount = maxMsgRetryCount;
44
+ }
45
+ addRecentMessage(to, id, message) {
46
+ const key = { to: to, id: id };
47
+ const keyStr = this.keyToString(key);
48
+ this.recentMessagesMap.set(keyStr, {
49
+ message: message,
50
+ timestamp: Date.now(),
51
+ });
52
+ this.messageKeyIndex.set(id, keyStr);
53
+ this.logger.debug(`Added message to retry cache: ${to}/${id}`);
54
+ }
55
+ getRecentMessage(to, id) {
56
+ const key = { to: to, id: id };
57
+ const keyStr = this.keyToString(key);
58
+ return this.recentMessagesMap.get(keyStr);
59
+ }
60
+ shouldRecreateSession(jid, retryCount, hasSession) {
61
+ if (!hasSession) {
62
+ this.sessionRecreateHistory.set(jid, Date.now());
63
+ this.statistics.sessionRecreations++;
64
+ return {
65
+ reason: "we don't have a Signal session with them",
66
+ recreate: true,
67
+ };
68
+ }
69
+ if (retryCount < 2) {
70
+ return { reason: "", recreate: false };
71
+ }
72
+ const now = Date.now();
73
+ const prevTime = this.sessionRecreateHistory.get(jid);
74
+ if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
75
+ this.sessionRecreateHistory.set(jid, now);
76
+ this.statistics.sessionRecreations++;
77
+ return {
78
+ reason: "retry count > 1 and over an hour since last recreation",
79
+ recreate: true,
80
+ };
81
+ }
82
+ return { reason: "", recreate: false };
83
+ }
84
+ incrementRetryCount(messageId) {
85
+ this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
86
+ this.statistics.totalRetries++;
87
+ return this.retryCounters.get(messageId);
88
+ }
89
+ getRetryCount(messageId) {
90
+ return this.retryCounters.get(messageId) || 0;
91
+ }
92
+ hasExceededMaxRetries(messageId) {
93
+ return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
94
+ }
95
+ markRetrySuccess(messageId) {
96
+ this.statistics.successfulRetries++;
97
+ this.retryCounters.delete(messageId);
98
+ this.cancelPendingPhoneRequest(messageId);
99
+ this.removeRecentMessage(messageId);
100
+ }
101
+ markRetryFailed(messageId) {
102
+ this.statistics.failedRetries++;
103
+ this.retryCounters.delete(messageId);
104
+ this.cancelPendingPhoneRequest(messageId);
105
+ this.removeRecentMessage(messageId);
106
+ }
107
+ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
108
+ this.cancelPendingPhoneRequest(messageId);
109
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
110
+ delete this.pendingPhoneRequests[messageId];
111
+ this.statistics.phoneRequests++;
112
+ callback();
113
+ }, delay);
114
+ this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
115
+ }
116
+ cancelPendingPhoneRequest(messageId) {
117
+ const timeout = this.pendingPhoneRequests[messageId];
118
+ if (timeout) {
119
+ clearTimeout(timeout);
120
+ delete this.pendingPhoneRequests[messageId];
121
+ this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
122
+ }
123
+ }
124
+ keyToString(key) {
125
+ return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
126
+ }
127
+ removeRecentMessage(messageId) {
128
+ const keyStr = this.messageKeyIndex.get(messageId);
129
+ if (!keyStr) return;
130
+ this.recentMessagesMap.delete(keyStr);
131
+ this.messageKeyIndex.delete(messageId);
132
+ }
133
+ }
134
+ module.exports = { MessageRetryManager: MessageRetryManager };