@vanzxy/baileys 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/WAProto/index.js +106053 -0
- package/cjs/lib/Defaults/index.js +168 -0
- package/cjs/lib/Signal/Group/ciphertext-message.js +16 -0
- package/cjs/lib/Signal/Group/group-session-builder.js +67 -0
- package/cjs/lib/Signal/Group/group_cipher.js +86 -0
- package/cjs/lib/Signal/Group/index.js +58 -0
- package/cjs/lib/Signal/Group/keyhelper.js +56 -0
- package/cjs/lib/Signal/Group/sender-chain-key.js +30 -0
- package/cjs/lib/Signal/Group/sender-key-distribution-message.js +67 -0
- package/cjs/lib/Signal/Group/sender-key-message.js +70 -0
- package/cjs/lib/Signal/Group/sender-key-name.js +52 -0
- package/cjs/lib/Signal/Group/sender-key-record.js +45 -0
- package/cjs/lib/Signal/Group/sender-key-state.js +88 -0
- package/cjs/lib/Signal/Group/sender-message-key.js +30 -0
- package/cjs/lib/Signal/libsignal.js +467 -0
- package/cjs/lib/Signal/lid-mapping.js +281 -0
- package/cjs/lib/Socket/Client/index.js +19 -0
- package/cjs/lib/Socket/Client/types.js +15 -0
- package/cjs/lib/Socket/Client/websocket.js +61 -0
- package/cjs/lib/Socket/business.js +384 -0
- package/cjs/lib/Socket/chats.js +1257 -0
- package/cjs/lib/Socket/communities.js +442 -0
- package/cjs/lib/Socket/dugong.js +753 -0
- package/cjs/lib/Socket/groups.js +360 -0
- package/cjs/lib/Socket/index.js +19 -0
- package/cjs/lib/Socket/messages-recv.js +1790 -0
- package/cjs/lib/Socket/messages-send.js +1390 -0
- package/cjs/lib/Socket/mex.js +46 -0
- package/cjs/lib/Socket/newsletter.js +340 -0
- package/cjs/lib/Socket/socket.js +1007 -0
- package/cjs/lib/Store/index.js +20 -0
- package/cjs/lib/Store/make-in-memory-store.js +438 -0
- package/cjs/lib/Store/make-ordered-dictionary.js +82 -0
- package/cjs/lib/Store/object-repository.js +28 -0
- package/cjs/lib/Types/Auth.js +3 -0
- package/cjs/lib/Types/Bussines.js +3 -0
- package/cjs/lib/Types/Call.js +3 -0
- package/cjs/lib/Types/Chat.js +11 -0
- package/cjs/lib/Types/Contact.js +3 -0
- package/cjs/lib/Types/Events.js +4 -0
- package/cjs/lib/Types/GroupMetadata.js +3 -0
- package/cjs/lib/Types/Label.js +26 -0
- package/cjs/lib/Types/LabelAssociation.js +8 -0
- package/cjs/lib/Types/Message.js +19 -0
- package/cjs/lib/Types/Mex.js +40 -0
- package/cjs/lib/Types/Product.js +3 -0
- package/cjs/lib/Types/RichType.js +24 -0
- package/cjs/lib/Types/Signal.js +4 -0
- package/cjs/lib/Types/Socket.js +4 -0
- package/cjs/lib/Types/State.js +54 -0
- package/cjs/lib/Types/USync.js +4 -0
- package/cjs/lib/Types/index.js +42 -0
- package/cjs/lib/Utils/MessageBuilder.js +2164 -0
- package/cjs/lib/Utils/auth-utils.js +312 -0
- package/cjs/lib/Utils/browser-utils.js +32 -0
- package/cjs/lib/Utils/business.js +245 -0
- package/cjs/lib/Utils/chat-utils.js +937 -0
- package/cjs/lib/Utils/companion-reg-client-utils.js +45 -0
- package/cjs/lib/Utils/crypto.js +196 -0
- package/cjs/lib/Utils/decode-wa-message.js +324 -0
- package/cjs/lib/Utils/event-buffer.js +643 -0
- package/cjs/lib/Utils/generics.js +429 -0
- package/cjs/lib/Utils/history.js +151 -0
- package/cjs/lib/Utils/identity-change-handler.js +56 -0
- package/cjs/lib/Utils/index.js +42 -0
- package/cjs/lib/Utils/link-preview.js +122 -0
- package/cjs/lib/Utils/logger.js +8 -0
- package/cjs/lib/Utils/lt-hash.js +62 -0
- package/cjs/lib/Utils/make-mutex.js +38 -0
- package/cjs/lib/Utils/message-retry-manager.js +267 -0
- package/cjs/lib/Utils/messages-media.js +933 -0
- package/cjs/lib/Utils/messages.js +2175 -0
- package/cjs/lib/Utils/noise-handler.js +205 -0
- package/cjs/lib/Utils/offline-node-processor.js +43 -0
- package/cjs/lib/Utils/pre-key-manager.js +113 -0
- package/cjs/lib/Utils/process-message.js +772 -0
- package/cjs/lib/Utils/reporting-utils.js +263 -0
- package/cjs/lib/Utils/rich-message-utils.js +439 -0
- package/cjs/lib/Utils/signal.js +214 -0
- package/cjs/lib/Utils/stanza-ack.js +41 -0
- package/cjs/lib/Utils/sync-action-utils.js +54 -0
- package/cjs/lib/Utils/tc-token-utils.js +174 -0
- package/cjs/lib/Utils/use-multi-file-auth-state.js +125 -0
- package/cjs/lib/Utils/use-single-file-auth-state.js +121 -0
- package/cjs/lib/Utils/use-sqlite-auth-state.js +182 -0
- package/cjs/lib/Utils/validate-connection.js +210 -0
- package/cjs/lib/WABinary/constants.js +1470 -0
- package/cjs/lib/WABinary/decode.js +301 -0
- package/cjs/lib/WABinary/encode.js +257 -0
- package/cjs/lib/WABinary/generic-utils.js +274 -0
- package/cjs/lib/WABinary/index.js +22 -0
- package/cjs/lib/WABinary/jid-utils.js +114 -0
- package/cjs/lib/WABinary/types.js +37 -0
- package/cjs/lib/WAM/BinaryInfo.js +14 -0
- package/cjs/lib/WAM/constants.js +22856 -0
- package/cjs/lib/WAM/encode.js +154 -0
- package/cjs/lib/WAM/index.js +20 -0
- package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.js +56 -0
- package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.js +67 -0
- package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
- package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.js +29 -0
- package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +55 -0
- package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.js +33 -0
- package/cjs/lib/WAUSync/Protocols/index.js +24 -0
- package/cjs/lib/WAUSync/USyncQuery.js +126 -0
- package/cjs/lib/WAUSync/USyncUser.js +35 -0
- package/cjs/lib/WAUSync/index.js +20 -0
- package/cjs/lib/index.js +34 -0
- package/cjs/package.json +1 -0
- package/lib/Socket/messages-send.js +4 -3
- package/lib/Utils/messages.js +4 -1
- package/package.json +16 -5
- package/postinstall-banner.js +3 -2
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getUrlInfo = void 0;
|
|
37
|
+
const messages_js_1 = require("./messages.js");
|
|
38
|
+
const messages_media_js_1 = require("./messages-media.js");
|
|
39
|
+
const THUMBNAIL_WIDTH_PX = 192;
|
|
40
|
+
/** Fetches an image and generates a thumbnail for it */
|
|
41
|
+
const getCompressedJpegThumbnail = async (url, { thumbnailWidth, fetchOpts }) => {
|
|
42
|
+
const stream = await (0, messages_media_js_1.getHttpStream)(url, fetchOpts);
|
|
43
|
+
const result = await (0, messages_media_js_1.extractImageThumb)(stream, thumbnailWidth);
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Given a piece of text, checks for any URL present, generates link preview for the same and returns it
|
|
48
|
+
* Return undefined if the fetch failed or no URL was found
|
|
49
|
+
* @param text first matched URL in text
|
|
50
|
+
* @returns the URL info required to generate link preview
|
|
51
|
+
*/
|
|
52
|
+
const getUrlInfo = async (text, opts = {
|
|
53
|
+
thumbnailWidth: THUMBNAIL_WIDTH_PX,
|
|
54
|
+
fetchOpts: { timeout: 3000 }
|
|
55
|
+
}) => {
|
|
56
|
+
try {
|
|
57
|
+
// retries
|
|
58
|
+
let retries = 0;
|
|
59
|
+
const maxRetry = 5;
|
|
60
|
+
const { getLinkPreview } = await Promise.resolve().then(() => __importStar(require('link-preview-js')));
|
|
61
|
+
let previewLink = text;
|
|
62
|
+
if (!text.startsWith('https://') && !text.startsWith('http://')) {
|
|
63
|
+
previewLink = 'https://' + previewLink;
|
|
64
|
+
}
|
|
65
|
+
const info = await getLinkPreview(previewLink, {
|
|
66
|
+
...opts.fetchOpts,
|
|
67
|
+
followRedirects: 'follow',
|
|
68
|
+
handleRedirects: (baseURL, forwardedURL) => {
|
|
69
|
+
const urlObj = new URL(baseURL);
|
|
70
|
+
const forwardedURLObj = new URL(forwardedURL);
|
|
71
|
+
if (retries >= maxRetry) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
if (forwardedURLObj.hostname === urlObj.hostname ||
|
|
75
|
+
forwardedURLObj.hostname === 'www.' + urlObj.hostname ||
|
|
76
|
+
'www.' + forwardedURLObj.hostname === urlObj.hostname) {
|
|
77
|
+
retries += 1;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
headers: opts.fetchOpts?.headers
|
|
85
|
+
});
|
|
86
|
+
if (info && 'title' in info && info.title) {
|
|
87
|
+
const [image] = info.images;
|
|
88
|
+
const urlInfo = {
|
|
89
|
+
'canonical-url': info.url,
|
|
90
|
+
'matched-text': text,
|
|
91
|
+
title: info.title,
|
|
92
|
+
description: info.description,
|
|
93
|
+
originalThumbnailUrl: image
|
|
94
|
+
};
|
|
95
|
+
if (opts.uploadImage) {
|
|
96
|
+
const { imageMessage } = await (0, messages_js_1.prepareWAMessageMedia)({ image: { url: image } }, {
|
|
97
|
+
upload: opts.uploadImage,
|
|
98
|
+
mediaTypeOverride: 'thumbnail-link',
|
|
99
|
+
options: opts.fetchOpts
|
|
100
|
+
});
|
|
101
|
+
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail ? Buffer.from(imageMessage.jpegThumbnail) : undefined;
|
|
102
|
+
urlInfo.highQualityThumbnail = imageMessage || undefined;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
try {
|
|
106
|
+
urlInfo.jpegThumbnail = image ? (await getCompressedJpegThumbnail(image, opts)).buffer : undefined;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
opts.logger?.debug({ err: error.stack, url: previewLink }, 'error in generating thumbnail');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return urlInfo;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (!error.message.includes('receive a valid')) {
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
exports.getUrlInfo = getUrlInfo;
|
|
122
|
+
//# sourceMappingURL=link-preview.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const pino_1 = __importDefault(require("pino"));
|
|
7
|
+
exports.default = (0, pino_1.default)({ timestamp: () => `,"time":"${new Date().toJSON()}"` });
|
|
8
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.LT_HASH_ANTI_TAMPERING = void 0;
|
|
37
|
+
/**
|
|
38
|
+
* LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
|
|
39
|
+
* over a series of mutations. You can add/remove mutations and it'll return a hash equal to
|
|
40
|
+
* if the same series of mutations was made sequentially.
|
|
41
|
+
*
|
|
42
|
+
* Vanz@Fix (bug 36 follow-up): whatsapp-rust-bridge is a native module and was previously
|
|
43
|
+
* statically imported here, which crashed the *entire* library at import time on platforms
|
|
44
|
+
* without a prebuilt binary (ARM musl/Alpine/Termux/some Pterodactyl nodes) — even for bots
|
|
45
|
+
* that never touch app-state sync. Same top-level-await try/catch pattern as crypto.js:
|
|
46
|
+
* only throws (with a clear message) if app-state sync (LT hash) is actually used.
|
|
47
|
+
*/
|
|
48
|
+
let LT_HASH_ANTI_TAMPERING;
|
|
49
|
+
try {
|
|
50
|
+
const { LTHashAntiTampering } = await Promise.resolve().then(() => __importStar(require('whatsapp-rust-bridge')));
|
|
51
|
+
exports.LT_HASH_ANTI_TAMPERING = LT_HASH_ANTI_TAMPERING = new LTHashAntiTampering();
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
const message = '`whatsapp-rust-bridge` failed to load (no prebuilt binary for this platform/arch). ' +
|
|
55
|
+
'App-state sync (LT hash) is unavailable here. Original error: ' + (err?.message || err);
|
|
56
|
+
exports.LT_HASH_ANTI_TAMPERING = LT_HASH_ANTI_TAMPERING = new Proxy({}, {
|
|
57
|
+
get() {
|
|
58
|
+
throw new Error(message);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=lt-hash.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeKeyedMutex = exports.makeMutex = void 0;
|
|
4
|
+
const async_mutex_1 = require("async-mutex");
|
|
5
|
+
const makeMutex = () => {
|
|
6
|
+
const mutex = new async_mutex_1.Mutex();
|
|
7
|
+
return {
|
|
8
|
+
mutex(code) {
|
|
9
|
+
return mutex.runExclusive(code);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
exports.makeMutex = makeMutex;
|
|
14
|
+
const makeKeyedMutex = () => {
|
|
15
|
+
const map = new Map();
|
|
16
|
+
return {
|
|
17
|
+
async mutex(key, task) {
|
|
18
|
+
let entry = map.get(key);
|
|
19
|
+
if (!entry) {
|
|
20
|
+
entry = { mutex: new async_mutex_1.Mutex(), refCount: 0 };
|
|
21
|
+
map.set(key, entry);
|
|
22
|
+
}
|
|
23
|
+
entry.refCount++;
|
|
24
|
+
try {
|
|
25
|
+
return await entry.mutex.runExclusive(task);
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
entry.refCount--;
|
|
29
|
+
// only delete it if this is still the current entry
|
|
30
|
+
if (entry.refCount === 0 && map.get(key) === entry) {
|
|
31
|
+
map.delete(key);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
exports.makeKeyedMutex = makeKeyedMutex;
|
|
38
|
+
//# sourceMappingURL=make-mutex.js.map
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageRetryManager = exports.RetryReason = 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 = 1024;
|
|
7
|
+
const MESSAGE_KEY_SEPARATOR = '\u0000';
|
|
8
|
+
/** Timeout for session recreation - 1 hour */
|
|
9
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
|
|
10
|
+
const PHONE_REQUEST_DELAY = 3000;
|
|
11
|
+
(function (RetryReason) {
|
|
12
|
+
RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
|
|
13
|
+
RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
|
|
14
|
+
RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
|
|
15
|
+
RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
|
|
16
|
+
/** MAC verification failed - most common cause of decryption failures */
|
|
17
|
+
RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
|
|
18
|
+
RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
|
|
19
|
+
RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
|
|
20
|
+
/** Explicit MAC failure - session is definitely out of sync */
|
|
21
|
+
RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
|
|
22
|
+
RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
|
|
23
|
+
RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
|
|
24
|
+
RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
|
|
25
|
+
RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
|
|
26
|
+
RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
|
|
27
|
+
RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
|
|
28
|
+
})(exports.RetryReason || (exports.RetryReason = {}));
|
|
29
|
+
/** Error codes that indicate a MAC failure and require immediate session recreation */
|
|
30
|
+
const MAC_ERROR_CODES = new Set([exports.RetryReason.SignalErrorInvalidMessage, exports.RetryReason.SignalErrorBadMac]);
|
|
31
|
+
class MessageRetryManager {
|
|
32
|
+
constructor(logger, maxMsgRetryCount) {
|
|
33
|
+
this.logger = logger;
|
|
34
|
+
this.recentMessagesMap = new lru_cache_1.LRUCache({
|
|
35
|
+
max: RECENT_MESSAGES_SIZE,
|
|
36
|
+
ttl: 5 * 60 * 1000,
|
|
37
|
+
ttlAutopurge: true,
|
|
38
|
+
dispose: (_value, key) => {
|
|
39
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
40
|
+
if (separatorIndex > -1) {
|
|
41
|
+
const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
|
|
42
|
+
this.messageKeyIndex.delete(messageId);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
this.messageKeyIndex = new Map();
|
|
47
|
+
this.sessionRecreateHistory = new lru_cache_1.LRUCache({
|
|
48
|
+
ttl: RECREATE_SESSION_TIMEOUT * 2,
|
|
49
|
+
ttlAutopurge: true
|
|
50
|
+
});
|
|
51
|
+
this.retryCounters = new lru_cache_1.LRUCache({
|
|
52
|
+
ttl: 15 * 60 * 1000,
|
|
53
|
+
ttlAutopurge: true,
|
|
54
|
+
updateAgeOnGet: true
|
|
55
|
+
}); // 15 minutes TTL
|
|
56
|
+
this.baseKeys = new lru_cache_1.LRUCache({
|
|
57
|
+
max: 1024,
|
|
58
|
+
ttl: 15 * 60 * 1000,
|
|
59
|
+
ttlAutopurge: true
|
|
60
|
+
});
|
|
61
|
+
this.pendingPhoneRequests = {};
|
|
62
|
+
this.maxMsgRetryCount = 5;
|
|
63
|
+
this.statistics = {
|
|
64
|
+
totalRetries: 0,
|
|
65
|
+
successfulRetries: 0,
|
|
66
|
+
failedRetries: 0,
|
|
67
|
+
mediaRetries: 0,
|
|
68
|
+
sessionRecreations: 0,
|
|
69
|
+
phoneRequests: 0
|
|
70
|
+
};
|
|
71
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Add a recent message to the cache for retry handling
|
|
75
|
+
*/
|
|
76
|
+
addRecentMessage(to, id, message) {
|
|
77
|
+
const key = { to, id };
|
|
78
|
+
const keyStr = this.keyToString(key);
|
|
79
|
+
// Add new message
|
|
80
|
+
this.recentMessagesMap.set(keyStr, {
|
|
81
|
+
message,
|
|
82
|
+
timestamp: Date.now()
|
|
83
|
+
});
|
|
84
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
85
|
+
this.logger.debug(`Added message to retry cache: ${to}/${id}`);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get a recent message from the cache
|
|
89
|
+
*/
|
|
90
|
+
getRecentMessage(to, id) {
|
|
91
|
+
const key = { to, id };
|
|
92
|
+
const keyStr = this.keyToString(key);
|
|
93
|
+
return this.recentMessagesMap.get(keyStr);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Check if a session should be recreated based on retry count, history, and error code.
|
|
97
|
+
* MAC errors (codes 4 and 7) trigger immediate session recreation regardless of timeout.
|
|
98
|
+
*/
|
|
99
|
+
shouldRecreateSession(jid, hasSession, errorCode) {
|
|
100
|
+
// If we don't have a session, always recreate
|
|
101
|
+
if (!hasSession) {
|
|
102
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
103
|
+
this.statistics.sessionRecreations++;
|
|
104
|
+
return {
|
|
105
|
+
reason: "we don't have a Signal session with them",
|
|
106
|
+
recreate: true
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// IMMEDIATE recreation for MAC errors - session is definitely out of sync
|
|
110
|
+
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
111
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
112
|
+
this.statistics.sessionRecreations++;
|
|
113
|
+
this.logger.warn({ jid, errorCode: exports.RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
|
|
114
|
+
return {
|
|
115
|
+
reason: `MAC error (code ${errorCode}: ${exports.RetryReason[errorCode]}), immediate session recreation`,
|
|
116
|
+
recreate: true
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
121
|
+
// If no previous recreation or it's been more than an hour
|
|
122
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
123
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
124
|
+
this.statistics.sessionRecreations++;
|
|
125
|
+
return {
|
|
126
|
+
reason: 'retry count > 1 and over an hour since last recreation',
|
|
127
|
+
recreate: true
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
return { reason: '', recreate: false };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Parse error code from retry receipt's retry node.
|
|
134
|
+
* Returns undefined if no error code is present.
|
|
135
|
+
*/
|
|
136
|
+
parseRetryErrorCode(errorAttr) {
|
|
137
|
+
if (errorAttr === undefined || errorAttr === '') {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
const code = parseInt(errorAttr, 10);
|
|
141
|
+
if (Number.isNaN(code)) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
// Validate it's a known RetryReason
|
|
145
|
+
if (code >= exports.RetryReason.UnknownError && code <= exports.RetryReason.StatusRevokeDelay) {
|
|
146
|
+
return code;
|
|
147
|
+
}
|
|
148
|
+
return exports.RetryReason.UnknownError;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Check if an error code indicates a MAC failure
|
|
152
|
+
*/
|
|
153
|
+
isMacError(errorCode) {
|
|
154
|
+
return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Increment retry counter for a message
|
|
158
|
+
*/
|
|
159
|
+
incrementRetryCount(messageId) {
|
|
160
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
161
|
+
this.statistics.totalRetries++;
|
|
162
|
+
return this.retryCounters.get(messageId);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get retry count for a message
|
|
166
|
+
*/
|
|
167
|
+
getRetryCount(messageId) {
|
|
168
|
+
return this.retryCounters.get(messageId) || 0;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Check if message has exceeded maximum retry attempts
|
|
172
|
+
*/
|
|
173
|
+
hasExceededMaxRetries(messageId) {
|
|
174
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Mark retry as successful
|
|
178
|
+
*/
|
|
179
|
+
markRetrySuccess(messageId) {
|
|
180
|
+
this.statistics.successfulRetries++;
|
|
181
|
+
// Clean up retry counter for successful message
|
|
182
|
+
this.retryCounters.delete(messageId);
|
|
183
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
184
|
+
this.removeRecentMessage(messageId);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Mark retry as failed
|
|
188
|
+
*/
|
|
189
|
+
markRetryFailed(messageId) {
|
|
190
|
+
this.statistics.failedRetries++;
|
|
191
|
+
this.retryCounters.delete(messageId);
|
|
192
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
193
|
+
this.removeRecentMessage(messageId);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Schedule a phone request with delay
|
|
197
|
+
*/
|
|
198
|
+
schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
|
|
199
|
+
// Cancel any existing request for this message
|
|
200
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
201
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
202
|
+
delete this.pendingPhoneRequests[messageId];
|
|
203
|
+
this.statistics.phoneRequests++;
|
|
204
|
+
callback();
|
|
205
|
+
}, delay);
|
|
206
|
+
this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Cancel pending phone request
|
|
210
|
+
*/
|
|
211
|
+
cancelPendingPhoneRequest(messageId) {
|
|
212
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
213
|
+
if (timeout) {
|
|
214
|
+
clearTimeout(timeout);
|
|
215
|
+
delete this.pendingPhoneRequests[messageId];
|
|
216
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
clear() {
|
|
220
|
+
this.recentMessagesMap.clear();
|
|
221
|
+
this.messageKeyIndex.clear();
|
|
222
|
+
this.sessionRecreateHistory.clear();
|
|
223
|
+
this.retryCounters.clear();
|
|
224
|
+
this.baseKeys.clear();
|
|
225
|
+
for (const messageId of Object.keys(this.pendingPhoneRequests)) {
|
|
226
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
227
|
+
}
|
|
228
|
+
this.statistics = {
|
|
229
|
+
totalRetries: 0,
|
|
230
|
+
successfulRetries: 0,
|
|
231
|
+
failedRetries: 0,
|
|
232
|
+
mediaRetries: 0,
|
|
233
|
+
sessionRecreations: 0,
|
|
234
|
+
phoneRequests: 0
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
saveBaseKey(addr, msgId, baseKey) {
|
|
238
|
+
this.baseKeys.set(`${addr}:${msgId}`, baseKey);
|
|
239
|
+
}
|
|
240
|
+
hasSameBaseKey(addr, msgId, baseKey) {
|
|
241
|
+
const stored = this.baseKeys.get(`${addr}:${msgId}`);
|
|
242
|
+
if (!stored || stored.length !== baseKey.length) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
for (let i = 0; i < stored.length; i++) {
|
|
246
|
+
if (stored[i] !== baseKey[i])
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
return true;
|
|
250
|
+
}
|
|
251
|
+
deleteBaseKey(addr, msgId) {
|
|
252
|
+
this.baseKeys.delete(`${addr}:${msgId}`);
|
|
253
|
+
}
|
|
254
|
+
keyToString(key) {
|
|
255
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
256
|
+
}
|
|
257
|
+
removeRecentMessage(messageId) {
|
|
258
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
259
|
+
if (!keyStr) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
this.recentMessagesMap.delete(keyStr);
|
|
263
|
+
this.messageKeyIndex.delete(messageId);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
exports.MessageRetryManager = MessageRetryManager;
|
|
267
|
+
//# sourceMappingURL=message-retry-manager.js.map
|