fca-nx 7.5.6 → 7.6.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/README.md +28 -0
- package/docs/ERRORS.md +46 -0
- package/docs/LIMITATIONS.md +32 -0
- package/index.d.ts +19 -0
- package/index.js +4 -0
- package/module/login.js +19 -12
- package/module/loginHelper.js +100 -154
- package/package.json +5 -3
- package/src/api/messaging/sessionGuard.js +5 -1
- package/src/api/socket/OldMessage.js +24 -8
- package/src/api/socket/core/connectMqtt.js +62 -86
- package/src/api/socket/core/emitAuth.js +7 -0
- package/src/api/socket/core/getSeqID.js +13 -3
- package/src/api/socket/core/reconnectPolicy.js +40 -0
- package/src/api/socket/e2ee/index.js +4 -2
- package/src/api/socket/legacySendError.js +35 -0
- package/src/api/socket/listenE2EE.js +2 -2
- package/src/api/socket/listenMqtt.js +17 -37
- package/src/api/socket/sendMessage.js +7 -3
- package/src/database/models/index.js +22 -17
- package/src/database/threadData.js +6 -0
- package/src/database/userData.js +6 -0
- package/src/utils/appStateCookies.js +151 -0
- package/src/utils/fcaError.js +188 -0
- package/src/utils/jarSnapshot.js +20 -0
- package/src/utils/logSanitize.js +36 -0
- package/src/utils/request/methods.js +6 -4
- package/src/utils/request/retry.js +63 -29
|
@@ -1,19 +1,65 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const CONNECT_FAILURES = new Set([
|
|
4
|
+
"ECONNREFUSED",
|
|
5
|
+
"ENOTFOUND",
|
|
6
|
+
"EAI_AGAIN",
|
|
7
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
8
|
+
"ENETUNREACH",
|
|
9
|
+
"EHOSTUNREACH"
|
|
10
|
+
]);
|
|
4
11
|
|
|
5
|
-
|
|
12
|
+
function delay(ms) {
|
|
13
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function statusOf(errorOrResponse) {
|
|
17
|
+
if (!errorOrResponse || typeof errorOrResponse !== "object") return 0;
|
|
18
|
+
return Number(
|
|
19
|
+
errorOrResponse.status ||
|
|
20
|
+
errorOrResponse.statusCode ||
|
|
21
|
+
(errorOrResponse.response && errorOrResponse.response.status) ||
|
|
22
|
+
0
|
|
23
|
+
) || 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isDefiniteConnectFailure(error) {
|
|
27
|
+
const code = error && (error.code || (error.cause && error.cause.code));
|
|
28
|
+
return CONNECT_FAILURES.has(code);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function shouldRetry(error, idempotent, attempt, retries) {
|
|
32
|
+
if (attempt >= retries - 1) return false;
|
|
33
|
+
const status = statusOf(error);
|
|
34
|
+
if (status === 429) return true;
|
|
35
|
+
if (!idempotent) return !status && isDefiniteConnectFailure(error);
|
|
36
|
+
if (status >= 400 && status < 500) return false;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function requestWithRetry(fn, retries = 3, baseDelay = 1000, ctx, policy) {
|
|
6
41
|
let lastError;
|
|
42
|
+
const idempotent = !policy || policy.idempotent !== false;
|
|
7
43
|
const emit = (event, payload) => {
|
|
8
44
|
try {
|
|
9
45
|
if (ctx && ctx._emitter && typeof ctx._emitter.emit === "function") {
|
|
10
46
|
ctx._emitter.emit(event, payload);
|
|
11
47
|
}
|
|
12
|
-
} catch {}
|
|
48
|
+
} catch (_) { }
|
|
13
49
|
};
|
|
50
|
+
|
|
14
51
|
for (let i = 0; i < retries; i++) {
|
|
15
52
|
try {
|
|
16
|
-
|
|
53
|
+
const result = await fn();
|
|
54
|
+
const status = statusOf(result);
|
|
55
|
+
if (status === 429 || (idempotent && status >= 500 && status < 600)) {
|
|
56
|
+
const error = new Error(`HTTP ${status}`);
|
|
57
|
+
error.status = status;
|
|
58
|
+
error.response = result;
|
|
59
|
+
error.code = status === 429 ? "RATE_LIMITED" : `HTTP_${status}`;
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
17
63
|
} catch (e) {
|
|
18
64
|
lastError = e;
|
|
19
65
|
|
|
@@ -21,44 +67,31 @@ async function requestWithRetry(fn, retries = 3, baseDelay = 1000, ctx) {
|
|
|
21
67
|
e?.code === "ERR_INVALID_CHAR" ||
|
|
22
68
|
(e?.message && e.message.includes("Invalid character in header"))
|
|
23
69
|
) {
|
|
24
|
-
const err = new Error(
|
|
25
|
-
"Invalid header content detected. Request aborted to prevent crash."
|
|
26
|
-
);
|
|
70
|
+
const err = new Error("Invalid header content detected. Request aborted to prevent crash.");
|
|
27
71
|
err.error = "Invalid header content";
|
|
28
72
|
err.originalError = e;
|
|
29
73
|
err.code = "ERR_INVALID_CHAR";
|
|
30
74
|
return Promise.reject(err);
|
|
31
75
|
}
|
|
32
76
|
|
|
33
|
-
const status = e
|
|
34
|
-
const url = e?.config?.url || "";
|
|
35
|
-
const method = String(e?.config?.method || "").toUpperCase();
|
|
36
|
-
if (status === 429) {
|
|
37
|
-
emit("rateLimit", { status, url, method });
|
|
38
|
-
}
|
|
77
|
+
const status = statusOf(e);
|
|
78
|
+
const url = e?.config?.url || e?.response?.config?.url || "";
|
|
79
|
+
const method = String(e?.config?.method || (policy && policy.method) || "").toUpperCase();
|
|
80
|
+
if (status === 429) emit("rateLimit", { status, url, method });
|
|
39
81
|
if (status >= 400 && status < 500 && status !== 429) {
|
|
40
82
|
return e.response || Promise.reject(e);
|
|
41
83
|
}
|
|
42
|
-
if (
|
|
84
|
+
if (!idempotent && status >= 500) {
|
|
85
|
+
return e.response || Promise.reject(e);
|
|
86
|
+
}
|
|
87
|
+
if (!shouldRetry(e, idempotent, i, retries)) {
|
|
43
88
|
return e.response || Promise.reject(e);
|
|
44
89
|
}
|
|
90
|
+
|
|
45
91
|
const netCode = e?.code || "";
|
|
46
92
|
const msg = e && e.message ? e.message : String(e || "");
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
(netCode === "UND_ERR_CONNECT_TIMEOUT" ||
|
|
50
|
-
netCode === "ETIMEDOUT" ||
|
|
51
|
-
netCode === "ECONNRESET" ||
|
|
52
|
-
netCode === "ECONNREFUSED" ||
|
|
53
|
-
netCode === "ENOTFOUND" ||
|
|
54
|
-
/timeout|connect timeout|network error|fetch failed/i.test(msg))
|
|
55
|
-
) {
|
|
56
|
-
emit("networkError", {
|
|
57
|
-
code: netCode,
|
|
58
|
-
message: msg,
|
|
59
|
-
url,
|
|
60
|
-
method,
|
|
61
|
-
});
|
|
93
|
+
if (!status && isDefiniteConnectFailure(e)) {
|
|
94
|
+
emit("networkError", { code: netCode, message: msg, url, method });
|
|
62
95
|
}
|
|
63
96
|
|
|
64
97
|
const backoffDelay = Math.min(
|
|
@@ -74,4 +107,5 @@ async function requestWithRetry(fn, retries = 3, baseDelay = 1000, ctx) {
|
|
|
74
107
|
|
|
75
108
|
module.exports = {
|
|
76
109
|
requestWithRetry,
|
|
110
|
+
shouldRetry
|
|
77
111
|
};
|