rixleys 1.0.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.
- package/LICENSE +21 -0
- package/README.MD +2969 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +5479 -0
- package/WAProto/index.js +97692 -0
- package/WAProto/p.html +1 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.js +120 -0
- package/lib/Defaults/rixleys-version.json +3 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/core/.eslintrc.json +31 -0
- package/lib/Signal/core/CODE_OF_CONDUCT.md +102 -0
- package/lib/Signal/core/LICENSE +621 -0
- package/lib/Signal/core/README.md +64 -0
- package/lib/Signal/core/SECURITY.md +78 -0
- package/lib/Signal/core/generate-proto.sh +1 -0
- package/lib/Signal/core/index.d.ts +55 -0
- package/lib/Signal/core/index.js +10 -0
- package/lib/Signal/core/protos/WhisperTextProtocol.proto +28 -0
- package/lib/Signal/core/src/.eslintrc.json +31 -0
- package/lib/Signal/core/src/WhisperTextProtocol.js +933 -0
- package/lib/Signal/core/src/base_key_type.js +7 -0
- package/lib/Signal/core/src/chain_type.js +6 -0
- package/lib/Signal/core/src/crypto.d.ts +19 -0
- package/lib/Signal/core/src/crypto.js +98 -0
- package/lib/Signal/core/src/curve.d.ts +22 -0
- package/lib/Signal/core/src/curve.js +142 -0
- package/lib/Signal/core/src/errors.js +33 -0
- package/lib/Signal/core/src/keyhelper.js +45 -0
- package/lib/Signal/core/src/numeric_fingerprint.js +72 -0
- package/lib/Signal/core/src/protobufs.js +10 -0
- package/lib/Signal/core/src/protocol_address.js +40 -0
- package/lib/Signal/core/src/queue_job.js +69 -0
- package/lib/Signal/core/src/session_builder.js +164 -0
- package/lib/Signal/core/src/session_cipher.js +336 -0
- package/lib/Signal/core/src/session_record.js +316 -0
- package/lib/Signal/libsignal.js +184 -0
- package/lib/Socket/Client/index.js +18 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +72 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +997 -0
- package/lib/Socket/groups.js +418 -0
- package/lib/Socket/index.js +238 -0
- package/lib/Socket/messages-recv.js +1106 -0
- package/lib/Socket/messages-send.js +882 -0
- package/lib/Socket/newsletter.js +250 -0
- package/lib/Socket/socket.js +646 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +8 -0
- package/lib/Store/make-in-memory-store.js +439 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +7 -0
- package/lib/Types/Newsletter.js +18 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.js +199 -0
- package/lib/Utils/browser-utils.js +35 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.js +730 -0
- package/lib/Utils/crypto.js +193 -0
- package/lib/Utils/decode-wa-message.js +217 -0
- package/lib/Utils/event-buffer.js +518 -0
- package/lib/Utils/generics.js +467 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.js +36 -0
- package/lib/Utils/link-preview.js +151 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/message-retry-manager.js +185 -0
- package/lib/Utils/messages-media.js +879 -0
- package/lib/Utils/messages.js +1049 -0
- package/lib/Utils/noise-handler.js +150 -0
- package/lib/Utils/process-message.js +404 -0
- package/lib/Utils/rixleys-event-stream.js +63 -0
- package/lib/Utils/serializer.js +238 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/use-multi-file-auth-state.js +124 -0
- package/lib/Utils/validate-connection.js +229 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +265 -0
- package/lib/WABinary/encode.js +250 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +85 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +54 -0
- package/package.json +113 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.getUrlInfo = exports.getLinkPreview = void 0;
|
|
5
|
+
|
|
6
|
+
const cheerio = require("cheerio");
|
|
7
|
+
const axios = require("axios");
|
|
8
|
+
const messages_1 = require("./messages");
|
|
9
|
+
const messages_media_1 = require("./messages-media");
|
|
10
|
+
|
|
11
|
+
const THUMBNAIL_WIDTH_PX = 192;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Built-in Lightweight Link Preview Extractor (Zero External Peer Dependency)
|
|
15
|
+
*/
|
|
16
|
+
async function getLinkPreview(targetUrl, options = {}) {
|
|
17
|
+
const timeout = options.timeout || 5000;
|
|
18
|
+
const headers = {
|
|
19
|
+
"User-Agent":
|
|
20
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
21
|
+
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
|
|
22
|
+
...(options.headers || {})
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const response = await axios.get(targetUrl, {
|
|
27
|
+
headers,
|
|
28
|
+
timeout,
|
|
29
|
+
maxRedirects: 5,
|
|
30
|
+
validateStatus: (status) => status >= 200 && status < 400
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const html = response.data;
|
|
34
|
+
if (typeof html !== "string") return null;
|
|
35
|
+
|
|
36
|
+
const $ = cheerio.load(html);
|
|
37
|
+
|
|
38
|
+
// Extract Title
|
|
39
|
+
const title =
|
|
40
|
+
$('meta[property="og:title"]').attr("content") ||
|
|
41
|
+
$('meta[name="twitter:title"]').attr("content") ||
|
|
42
|
+
$("title").text() ||
|
|
43
|
+
"";
|
|
44
|
+
|
|
45
|
+
// Extract Description
|
|
46
|
+
const description =
|
|
47
|
+
$('meta[property="og:description"]').attr("content") ||
|
|
48
|
+
$('meta[name="twitter:description"]').attr("content") ||
|
|
49
|
+
$('meta[name="description"]').attr("content") ||
|
|
50
|
+
"";
|
|
51
|
+
|
|
52
|
+
// Extract Images
|
|
53
|
+
let images = [];
|
|
54
|
+
const ogImage = $('meta[property="og:image"]').attr("content");
|
|
55
|
+
const twImage = $('meta[name="twitter:image"]').attr("content");
|
|
56
|
+
const linkIcon = $('link[rel="image_src"]').attr("href") || $('link[rel="icon"]').attr("href");
|
|
57
|
+
|
|
58
|
+
const rawImg = ogImage || twImage || linkIcon;
|
|
59
|
+
if (rawImg) {
|
|
60
|
+
try {
|
|
61
|
+
const absoluteImgUrl = new URL(rawImg, targetUrl).href;
|
|
62
|
+
images.push(absoluteImgUrl);
|
|
63
|
+
} catch {
|
|
64
|
+
images.push(rawImg);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const canonicalUrl =
|
|
69
|
+
$('link[rel="canonical"]').attr("href") ||
|
|
70
|
+
$('meta[property="og:url"]').attr("content") ||
|
|
71
|
+
targetUrl;
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
url: canonicalUrl,
|
|
75
|
+
title: title.trim(),
|
|
76
|
+
description: description.trim(),
|
|
77
|
+
images,
|
|
78
|
+
mediaType: "website"
|
|
79
|
+
};
|
|
80
|
+
} catch (err) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
exports.getLinkPreview = getLinkPreview;
|
|
86
|
+
|
|
87
|
+
/** Fetches an image and generates a thumbnail for it */
|
|
88
|
+
const getCompressedJpegThumbnail = async (url, { thumbnailWidth, fetchOpts }) => {
|
|
89
|
+
const stream = await (0, messages_media_1.getHttpStream)(url, fetchOpts);
|
|
90
|
+
const result = await (0, messages_media_1.extractImageThumb)(stream, thumbnailWidth);
|
|
91
|
+
return result;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Given a piece of text, checks for any URL present, generates link preview for the same and returns it
|
|
96
|
+
*/
|
|
97
|
+
const getUrlInfo = async (text, opts = {
|
|
98
|
+
thumbnailWidth: THUMBNAIL_WIDTH_PX,
|
|
99
|
+
fetchOpts: { timeout: 3000 }
|
|
100
|
+
}) => {
|
|
101
|
+
var _a;
|
|
102
|
+
try {
|
|
103
|
+
let previewLink = text;
|
|
104
|
+
if (!text.startsWith("https://") && !text.startsWith("http://")) {
|
|
105
|
+
previewLink = "https://" + previewLink;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const info = await getLinkPreview(previewLink, opts.fetchOpts);
|
|
109
|
+
|
|
110
|
+
if (info && info.title) {
|
|
111
|
+
const [image] = info.images || [];
|
|
112
|
+
const urlInfo = {
|
|
113
|
+
"canonical-url": info.url,
|
|
114
|
+
"matched-text": text,
|
|
115
|
+
title: info.title,
|
|
116
|
+
description: info.description,
|
|
117
|
+
originalThumbnailUrl: image
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
if (opts.uploadImage && image) {
|
|
121
|
+
const { imageMessage } = await (0, messages_1.prepareWAMessageMedia)(
|
|
122
|
+
{ image: { url: image } },
|
|
123
|
+
{
|
|
124
|
+
upload: opts.uploadImage,
|
|
125
|
+
mediaTypeOverride: "thumbnail-link",
|
|
126
|
+
options: opts.fetchOpts
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
|
|
130
|
+
? Buffer.from(imageMessage.jpegThumbnail)
|
|
131
|
+
: undefined;
|
|
132
|
+
urlInfo.highQualityThumbnail = imageMessage || undefined;
|
|
133
|
+
} else if (image) {
|
|
134
|
+
try {
|
|
135
|
+
urlInfo.jpegThumbnail = (await getCompressedJpegThumbnail(image, opts)).buffer;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
(_a = opts.logger) === null || _a === void 0
|
|
138
|
+
? void 0
|
|
139
|
+
: _a.debug({ err: error.stack, url: previewLink }, "error in generating thumbnail");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return urlInfo;
|
|
143
|
+
}
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (!error.message?.includes("receive a valid")) {
|
|
146
|
+
// ignore preview fail
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
exports.getUrlInfo = getUrlInfo;
|
|
@@ -0,0 +1,7 @@
|
|
|
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()}"` });
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LT_HASH_ANTI_TAMPERING = void 0;
|
|
4
|
+
const crypto_1 = require("./crypto");
|
|
5
|
+
/**
|
|
6
|
+
* LT Hash is a summation based hash algorithm that maintains the integrity of a piece of data
|
|
7
|
+
* over a series of mutations. You can add/remove mutations and it'll return a hash equal to
|
|
8
|
+
* if the same series of mutations was made sequentially.
|
|
9
|
+
*/
|
|
10
|
+
const o = 128;
|
|
11
|
+
class d {
|
|
12
|
+
constructor(e) {
|
|
13
|
+
this.salt = e;
|
|
14
|
+
}
|
|
15
|
+
add(e, t) {
|
|
16
|
+
var r = this;
|
|
17
|
+
for (const item of t) {
|
|
18
|
+
e = r._addSingle(e, item);
|
|
19
|
+
}
|
|
20
|
+
return e;
|
|
21
|
+
}
|
|
22
|
+
subtract(e, t) {
|
|
23
|
+
var r = this;
|
|
24
|
+
for (const item of t) {
|
|
25
|
+
e = r._subtractSingle(e, item);
|
|
26
|
+
}
|
|
27
|
+
return e;
|
|
28
|
+
}
|
|
29
|
+
subtractThenAdd(e, t, r) {
|
|
30
|
+
var n = this;
|
|
31
|
+
return n.add(n.subtract(e, r), t);
|
|
32
|
+
}
|
|
33
|
+
async _addSingle(e, t) {
|
|
34
|
+
var r = this;
|
|
35
|
+
const n = new Uint8Array(await (0, crypto_1.hkdf)(Buffer.from(t), o, { info: r.salt })).buffer;
|
|
36
|
+
return r.performPointwiseWithOverflow(await e, n, ((e, t) => e + t));
|
|
37
|
+
}
|
|
38
|
+
async _subtractSingle(e, t) {
|
|
39
|
+
var r = this;
|
|
40
|
+
const n = new Uint8Array(await (0, crypto_1.hkdf)(Buffer.from(t), o, { info: r.salt })).buffer;
|
|
41
|
+
return r.performPointwiseWithOverflow(await e, n, ((e, t) => e - t));
|
|
42
|
+
}
|
|
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);
|
|
47
|
+
}
|
|
48
|
+
return a;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.LT_HASH_ANTI_TAMPERING = new d('WhatsApp Patch Integrity');
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeKeyedMutex = exports.makeMutex = void 0;
|
|
4
|
+
const makeMutex = () => {
|
|
5
|
+
let task = Promise.resolve();
|
|
6
|
+
let taskTimeout;
|
|
7
|
+
return {
|
|
8
|
+
mutex(code) {
|
|
9
|
+
task = (async () => {
|
|
10
|
+
// wait for the previous task to complete
|
|
11
|
+
// if there is an error, we swallow so as to not block the queue
|
|
12
|
+
try {
|
|
13
|
+
await task;
|
|
14
|
+
}
|
|
15
|
+
catch (_a) { }
|
|
16
|
+
try {
|
|
17
|
+
// execute the current task
|
|
18
|
+
const result = await code();
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
clearTimeout(taskTimeout);
|
|
23
|
+
}
|
|
24
|
+
})();
|
|
25
|
+
// we replace the existing task, appending the new piece of execution to it
|
|
26
|
+
// so the next task will have to wait for this one to finish
|
|
27
|
+
return task;
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
exports.makeMutex = makeMutex;
|
|
32
|
+
const makeKeyedMutex = () => {
|
|
33
|
+
const map = {};
|
|
34
|
+
return {
|
|
35
|
+
mutex(key, task) {
|
|
36
|
+
if (!map[key]) {
|
|
37
|
+
map[key] = (0, exports.makeMutex)();
|
|
38
|
+
}
|
|
39
|
+
return map[key].mutex(task);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.makeKeyedMutex = makeKeyedMutex;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MessageRetryManager = exports.RetryReason = void 0;
|
|
4
|
+
|
|
5
|
+
const lru_cache_1 = require("lru-cache");
|
|
6
|
+
|
|
7
|
+
const RECENT_MESSAGES_SIZE = 1000;
|
|
8
|
+
const MESSAGE_KEY_SEPARATOR = '\u0000';
|
|
9
|
+
const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000;
|
|
10
|
+
|
|
11
|
+
var RetryReason;
|
|
12
|
+
(function (RetryReason) {
|
|
13
|
+
RetryReason[RetryReason["UnknownError"] = 0] = "UnknownError";
|
|
14
|
+
RetryReason[RetryReason["SignalErrorNoSession"] = 1] = "SignalErrorNoSession";
|
|
15
|
+
RetryReason[RetryReason["SignalErrorInvalidKey"] = 2] = "SignalErrorInvalidKey";
|
|
16
|
+
RetryReason[RetryReason["SignalErrorInvalidKeyId"] = 3] = "SignalErrorInvalidKeyId";
|
|
17
|
+
RetryReason[RetryReason["SignalErrorInvalidMessage"] = 4] = "SignalErrorInvalidMessage";
|
|
18
|
+
RetryReason[RetryReason["SignalErrorInvalidSignature"] = 5] = "SignalErrorInvalidSignature";
|
|
19
|
+
RetryReason[RetryReason["SignalErrorFutureMessage"] = 6] = "SignalErrorFutureMessage";
|
|
20
|
+
RetryReason[RetryReason["SignalErrorBadMac"] = 7] = "SignalErrorBadMac";
|
|
21
|
+
RetryReason[RetryReason["SignalErrorInvalidSession"] = 8] = "SignalErrorInvalidSession";
|
|
22
|
+
RetryReason[RetryReason["SignalErrorInvalidMsgKey"] = 9] = "SignalErrorInvalidMsgKey";
|
|
23
|
+
RetryReason[RetryReason["BadBroadcastEphemeralSetting"] = 10] = "BadBroadcastEphemeralSetting";
|
|
24
|
+
RetryReason[RetryReason["UnknownCompanionNoPrekey"] = 11] = "UnknownCompanionNoPrekey";
|
|
25
|
+
RetryReason[RetryReason["AdvFailure"] = 12] = "AdvFailure";
|
|
26
|
+
RetryReason[RetryReason["StatusRevokeDelay"] = 13] = "StatusRevokeDelay";
|
|
27
|
+
})(RetryReason = exports.RetryReason || (exports.RetryReason = {}));
|
|
28
|
+
|
|
29
|
+
const MAC_ERROR_CODES = new Set([RetryReason.SignalErrorInvalidMessage, RetryReason.SignalErrorBadMac]);
|
|
30
|
+
|
|
31
|
+
class MessageRetryManager {
|
|
32
|
+
constructor(logger, maxMsgRetryCount = 5) {
|
|
33
|
+
this.logger = logger;
|
|
34
|
+
this.maxMsgRetryCount = maxMsgRetryCount;
|
|
35
|
+
this.messageKeyIndex = new Map();
|
|
36
|
+
this.pendingPhoneRequests = {};
|
|
37
|
+
this.statistics = {
|
|
38
|
+
totalRetries: 0,
|
|
39
|
+
successfulRetries: 0,
|
|
40
|
+
failedRetries: 0,
|
|
41
|
+
mediaRetries: 0,
|
|
42
|
+
sessionRecreations: 0,
|
|
43
|
+
phoneRequests: 0
|
|
44
|
+
};
|
|
45
|
+
this.recentMessagesMap = new lru_cache_1.LRUCache({
|
|
46
|
+
max: RECENT_MESSAGES_SIZE,
|
|
47
|
+
ttl: 5 * 60 * 1000,
|
|
48
|
+
ttlAutopurge: true,
|
|
49
|
+
dispose: (_value, key) => {
|
|
50
|
+
const separatorIndex = key.lastIndexOf(MESSAGE_KEY_SEPARATOR);
|
|
51
|
+
if (separatorIndex > -1) {
|
|
52
|
+
const messageId = key.slice(separatorIndex + MESSAGE_KEY_SEPARATOR.length);
|
|
53
|
+
this.messageKeyIndex.delete(messageId);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
this.sessionRecreateHistory = new lru_cache_1.LRUCache({
|
|
58
|
+
max: 10000,
|
|
59
|
+
ttl: RECREATE_SESSION_TIMEOUT,
|
|
60
|
+
ttlAutopurge: true
|
|
61
|
+
});
|
|
62
|
+
this.retryCounters = new lru_cache_1.LRUCache({
|
|
63
|
+
max: 5000,
|
|
64
|
+
ttl: 15 * 60 * 1000,
|
|
65
|
+
ttlAutopurge: true
|
|
66
|
+
});
|
|
67
|
+
this.baseKeys = new lru_cache_1.LRUCache({
|
|
68
|
+
max: 1024,
|
|
69
|
+
ttl: 15 * 60 * 1000,
|
|
70
|
+
ttlAutopurge: true
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
addRecentMessage(key, message) {
|
|
75
|
+
const id = key.id;
|
|
76
|
+
if (!id) return;
|
|
77
|
+
const keyStr = this.keyToString(key);
|
|
78
|
+
this.recentMessagesMap.set(keyStr, { message, timestamp: Date.now() });
|
|
79
|
+
this.messageKeyIndex.set(id, keyStr);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getRecentMessage(key) {
|
|
83
|
+
const keyStr = this.keyToString(key);
|
|
84
|
+
return this.recentMessagesMap.get(keyStr);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
shouldRecreateSession(jid, hasSession, errorCode) {
|
|
88
|
+
if (!hasSession) {
|
|
89
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
90
|
+
this.statistics.sessionRecreations++;
|
|
91
|
+
return { reason: 'no existing session', recreate: true };
|
|
92
|
+
}
|
|
93
|
+
if (errorCode !== undefined && MAC_ERROR_CODES.has(errorCode)) {
|
|
94
|
+
this.sessionRecreateHistory.set(jid, Date.now());
|
|
95
|
+
this.statistics.sessionRecreations++;
|
|
96
|
+
this.logger.warn({ jid, errorCode: RetryReason[errorCode] }, 'MAC error detected, forcing immediate session recreation');
|
|
97
|
+
return {
|
|
98
|
+
reason: `MAC error (code ${errorCode}: ${RetryReason[errorCode]}), immediate session recreation`,
|
|
99
|
+
recreate: true
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const now = Date.now();
|
|
103
|
+
const prevTime = this.sessionRecreateHistory.get(jid);
|
|
104
|
+
if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
|
|
105
|
+
this.sessionRecreateHistory.set(jid, now);
|
|
106
|
+
this.statistics.sessionRecreations++;
|
|
107
|
+
return { reason: 'session recreation timeout window expired', recreate: true };
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
reason: `session recreated recently at ${new Date(prevTime).toISOString()}, skipping`,
|
|
111
|
+
recreate: false
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
parseRetryErrorCode(errorAttr) {
|
|
116
|
+
if (errorAttr === undefined || errorAttr === '') return undefined;
|
|
117
|
+
const code = parseInt(errorAttr, 10);
|
|
118
|
+
if (Number.isNaN(code)) return undefined;
|
|
119
|
+
if (code >= RetryReason.UnknownError && code <= RetryReason.StatusRevokeDelay) return code;
|
|
120
|
+
return RetryReason.UnknownError;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
isMacError(errorCode) {
|
|
124
|
+
return errorCode !== undefined && MAC_ERROR_CODES.has(errorCode);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
incrementRetryCount(messageId) {
|
|
128
|
+
this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
|
|
129
|
+
this.statistics.totalRetries++;
|
|
130
|
+
return this.retryCounters.get(messageId);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getRetryCount(messageId) {
|
|
134
|
+
return this.retryCounters.get(messageId) || 0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
hasExceededMaxRetries(messageId) {
|
|
138
|
+
return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
markRetrySuccess(messageId) {
|
|
142
|
+
this.statistics.successfulRetries++;
|
|
143
|
+
this.retryCounters.delete(messageId);
|
|
144
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
145
|
+
this.removeRecentMessage(messageId);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
markRetryFailed(messageId) {
|
|
149
|
+
this.statistics.failedRetries++;
|
|
150
|
+
this.retryCounters.delete(messageId);
|
|
151
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
152
|
+
this.removeRecentMessage(messageId);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
schedulePhoneRequest(messageId, callback, delayMs = 3000) {
|
|
156
|
+
this.cancelPendingPhoneRequest(messageId);
|
|
157
|
+
this.pendingPhoneRequests[messageId] = setTimeout(() => {
|
|
158
|
+
delete this.pendingPhoneRequests[messageId];
|
|
159
|
+
this.statistics.phoneRequests++;
|
|
160
|
+
callback();
|
|
161
|
+
}, delayMs);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
cancelPendingPhoneRequest(messageId) {
|
|
165
|
+
const timeout = this.pendingPhoneRequests[messageId];
|
|
166
|
+
if (timeout) {
|
|
167
|
+
clearTimeout(timeout);
|
|
168
|
+
delete this.pendingPhoneRequests[messageId];
|
|
169
|
+
this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
keyToString(key) {
|
|
174
|
+
return `${key.to}${MESSAGE_KEY_SEPARATOR}${key.id}`;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
removeRecentMessage(messageId) {
|
|
178
|
+
const keyStr = this.messageKeyIndex.get(messageId);
|
|
179
|
+
if (!keyStr) return;
|
|
180
|
+
this.recentMessagesMap.delete(keyStr);
|
|
181
|
+
this.messageKeyIndex.delete(messageId);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
exports.MessageRetryManager = MessageRetryManager;
|