@dongdev/fca-unofficial 3.0.31 → 4.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 +191 -0
- package/README.md +224 -406
- package/dist/index.d.mts +1241 -0
- package/dist/index.d.ts +1241 -0
- package/dist/index.js +27749 -0
- package/dist/index.mjs +27713 -0
- package/docs/ARCHITECTURE.md +467 -0
- package/docs/DOCS.md +686 -0
- package/fca-config.example.json +33 -0
- package/package.json +32 -22
- package/test/fca.test.cjs +533 -0
- package/CHANGELOG.md +0 -296
- package/DOCS.md +0 -2712
- package/func/checkUpdate.js +0 -222
- package/func/logAdapter.js +0 -33
- package/func/logger.js +0 -48
- package/index.d.ts +0 -751
- package/index.js +0 -8
- package/module/config.js +0 -40
- package/module/login.js +0 -133
- package/module/loginHelper.js +0 -1296
- package/module/options.js +0 -44
- package/src/api/action/addExternalModule.js +0 -25
- package/src/api/action/changeAvatar.js +0 -137
- package/src/api/action/changeBio.js +0 -75
- package/src/api/action/enableAutoSaveAppState.js +0 -73
- package/src/api/action/getCurrentUserID.js +0 -7
- package/src/api/action/handleFriendRequest.js +0 -57
- package/src/api/action/logout.js +0 -76
- package/src/api/action/refreshFb_dtsg.js +0 -48
- package/src/api/action/setPostReaction.js +0 -106
- package/src/api/action/unfriend.js +0 -54
- package/src/api/http/httpGet.js +0 -46
- package/src/api/http/httpPost.js +0 -52
- package/src/api/http/postFormData.js +0 -47
- package/src/api/messaging/addUserToGroup.js +0 -68
- package/src/api/messaging/changeAdminStatus.js +0 -126
- package/src/api/messaging/changeArchivedStatus.js +0 -55
- package/src/api/messaging/changeBlockedStatus.js +0 -48
- package/src/api/messaging/changeGroupImage.js +0 -91
- package/src/api/messaging/changeNickname.js +0 -70
- package/src/api/messaging/changeThreadColor.js +0 -79
- package/src/api/messaging/changeThreadEmoji.js +0 -111
- package/src/api/messaging/createNewGroup.js +0 -88
- package/src/api/messaging/createPoll.js +0 -46
- package/src/api/messaging/createThemeAI.js +0 -98
- package/src/api/messaging/deleteMessage.js +0 -136
- package/src/api/messaging/deleteThread.js +0 -56
- package/src/api/messaging/editMessage.js +0 -68
- package/src/api/messaging/forwardAttachment.js +0 -57
- package/src/api/messaging/getEmojiUrl.js +0 -29
- package/src/api/messaging/getFriendsList.js +0 -82
- package/src/api/messaging/getMessage.js +0 -829
- package/src/api/messaging/getThemePictures.js +0 -62
- package/src/api/messaging/handleMessageRequest.js +0 -65
- package/src/api/messaging/markAsDelivered.js +0 -57
- package/src/api/messaging/markAsRead.js +0 -88
- package/src/api/messaging/markAsReadAll.js +0 -49
- package/src/api/messaging/markAsSeen.js +0 -61
- package/src/api/messaging/muteThread.js +0 -50
- package/src/api/messaging/removeUserFromGroup.js +0 -62
- package/src/api/messaging/resolvePhotoUrl.js +0 -43
- package/src/api/messaging/scheduler.js +0 -264
- package/src/api/messaging/searchForThread.js +0 -53
- package/src/api/messaging/sendMessage.js +0 -270
- package/src/api/messaging/sendTypingIndicator.js +0 -74
- package/src/api/messaging/setMessageReaction.js +0 -90
- package/src/api/messaging/setTitle.js +0 -124
- package/src/api/messaging/shareContact.js +0 -49
- package/src/api/messaging/threadColors.js +0 -128
- package/src/api/messaging/unsendMessage.js +0 -81
- package/src/api/messaging/uploadAttachment.js +0 -492
- package/src/api/socket/core/connectMqtt.js +0 -258
- package/src/api/socket/core/emitAuth.js +0 -103
- package/src/api/socket/core/getSeqID.js +0 -320
- package/src/api/socket/core/getTaskResponseData.js +0 -25
- package/src/api/socket/core/parseDelta.js +0 -377
- package/src/api/socket/detail/buildStream.js +0 -215
- package/src/api/socket/detail/constants.js +0 -28
- package/src/api/socket/listenMqtt.js +0 -377
- package/src/api/socket/middleware/index.js +0 -216
- package/src/api/threads/getThreadHistory.js +0 -664
- package/src/api/threads/getThreadInfo.js +0 -296
- package/src/api/threads/getThreadList.js +0 -293
- package/src/api/threads/getThreadPictures.js +0 -78
- package/src/api/users/getUserID.js +0 -65
- package/src/api/users/getUserInfo.js +0 -402
- package/src/api/users/getUserInfoV2.js +0 -134
- package/src/core/sendReqMqtt.js +0 -96
- package/src/database/helpers.js +0 -53
- package/src/database/models/index.js +0 -88
- package/src/database/models/thread.js +0 -50
- package/src/database/models/user.js +0 -46
- package/src/database/threadData.js +0 -94
- package/src/database/userData.js +0 -98
- package/src/remote/remoteClient.js +0 -123
- package/src/utils/broadcast.js +0 -51
- package/src/utils/client.js +0 -10
- package/src/utils/constants.js +0 -23
- package/src/utils/cookies.js +0 -68
- package/src/utils/format/attachment.js +0 -357
- package/src/utils/format/cookie.js +0 -9
- package/src/utils/format/date.js +0 -50
- package/src/utils/format/decode.js +0 -44
- package/src/utils/format/delta.js +0 -194
- package/src/utils/format/ids.js +0 -64
- package/src/utils/format/index.js +0 -64
- package/src/utils/format/message.js +0 -88
- package/src/utils/format/presence.js +0 -132
- package/src/utils/format/readTyp.js +0 -44
- package/src/utils/format/thread.js +0 -42
- package/src/utils/format/utils.js +0 -141
- package/src/utils/headers.js +0 -115
- package/src/utils/loginParser/autoLogin.js +0 -125
- package/src/utils/loginParser/helpers.js +0 -43
- package/src/utils/loginParser/index.js +0 -10
- package/src/utils/loginParser/parseAndCheckLogin.js +0 -220
- package/src/utils/loginParser/textUtils.js +0 -28
- package/src/utils/request/client.js +0 -26
- package/src/utils/request/config.js +0 -23
- package/src/utils/request/defaults.js +0 -46
- package/src/utils/request/helpers.js +0 -46
- package/src/utils/request/index.js +0 -17
- package/src/utils/request/methods.js +0 -163
- package/src/utils/request/proxy.js +0 -21
- package/src/utils/request/retry.js +0 -77
- package/src/utils/request/sanitize.js +0 -49
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const axios = require("axios");
|
|
4
|
-
const { CookieJar } = require("tough-cookie");
|
|
5
|
-
const { wrapper } = require("axios-cookiejar-support");
|
|
6
|
-
|
|
7
|
-
const jar = new CookieJar();
|
|
8
|
-
const client = wrapper(
|
|
9
|
-
axios.create({
|
|
10
|
-
jar,
|
|
11
|
-
withCredentials: true,
|
|
12
|
-
timeout: 60000,
|
|
13
|
-
validateStatus: (s) => s >= 200 && s < 600,
|
|
14
|
-
maxRedirects: 5,
|
|
15
|
-
maxContentLength: Infinity,
|
|
16
|
-
maxBodyLength: Infinity,
|
|
17
|
-
})
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
21
|
-
|
|
22
|
-
module.exports = {
|
|
23
|
-
jar,
|
|
24
|
-
client,
|
|
25
|
-
delay,
|
|
26
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { sanitizeHeaders } = require("./sanitize");
|
|
4
|
-
const { jar, client } = require("./client");
|
|
5
|
-
|
|
6
|
-
function cfg(base = {}) {
|
|
7
|
-
const { reqJar, headers, params, agent, timeout } = base;
|
|
8
|
-
return {
|
|
9
|
-
headers: sanitizeHeaders(headers),
|
|
10
|
-
params,
|
|
11
|
-
jar: reqJar || jar,
|
|
12
|
-
withCredentials: true,
|
|
13
|
-
timeout: timeout || 60000,
|
|
14
|
-
httpAgent: agent || client.defaults.httpAgent,
|
|
15
|
-
httpsAgent: agent || client.defaults.httpsAgent,
|
|
16
|
-
proxy: false,
|
|
17
|
-
validateStatus: (s) => s >= 200 && s < 600,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
module.exports = {
|
|
22
|
-
cfg,
|
|
23
|
-
};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const constMod = require("../constants");
|
|
4
|
-
const getFrom = constMod.getFrom || constMod;
|
|
5
|
-
const { get, post, postFormData } = require("./methods");
|
|
6
|
-
|
|
7
|
-
function makeDefaults(html, userID, ctx) {
|
|
8
|
-
let reqCounter = 1;
|
|
9
|
-
const revision =
|
|
10
|
-
getFrom(html || "", 'revision":', ",") ||
|
|
11
|
-
getFrom(html || "", '"client_revision":', ",") ||
|
|
12
|
-
"";
|
|
13
|
-
function mergeWithDefaults(obj) {
|
|
14
|
-
const base = {
|
|
15
|
-
av: userID,
|
|
16
|
-
__user: userID,
|
|
17
|
-
__req: (reqCounter++).toString(36),
|
|
18
|
-
__rev: revision,
|
|
19
|
-
__a: 1,
|
|
20
|
-
};
|
|
21
|
-
if (ctx?.fb_dtsg) base.fb_dtsg = ctx.fb_dtsg;
|
|
22
|
-
if (ctx?.jazoest) base.jazoest = ctx.jazoest;
|
|
23
|
-
if (!obj) return base;
|
|
24
|
-
for (const k of Object.keys(obj)) if (!(k in base)) base[k] = obj[k];
|
|
25
|
-
return base;
|
|
26
|
-
}
|
|
27
|
-
return {
|
|
28
|
-
get: (url, j, qs, ctxx, customHeader = {}) =>
|
|
29
|
-
get(url, j, mergeWithDefaults(qs), ctx?.globalOptions, ctxx || ctx, customHeader),
|
|
30
|
-
post: (url, j, form, ctxx, customHeader = {}) =>
|
|
31
|
-
post(url, j, mergeWithDefaults(form), ctx?.globalOptions, ctxx || ctx, customHeader),
|
|
32
|
-
postFormData: (url, j, form, qs, ctxx) =>
|
|
33
|
-
postFormData(
|
|
34
|
-
url,
|
|
35
|
-
j,
|
|
36
|
-
mergeWithDefaults(form),
|
|
37
|
-
mergeWithDefaults(qs),
|
|
38
|
-
ctx?.globalOptions,
|
|
39
|
-
ctxx || ctx
|
|
40
|
-
),
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
module.exports = {
|
|
45
|
-
makeDefaults,
|
|
46
|
-
};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const formatMod = require("../format");
|
|
4
|
-
const getType = formatMod.getType || formatMod;
|
|
5
|
-
|
|
6
|
-
function toStringVal(v) {
|
|
7
|
-
if (v === undefined || v === null) return "";
|
|
8
|
-
if (typeof v === "bigint") return v.toString();
|
|
9
|
-
if (typeof v === "boolean") return v ? "true" : "false";
|
|
10
|
-
return String(v);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function isStream(v) {
|
|
14
|
-
return (
|
|
15
|
-
v &&
|
|
16
|
-
typeof v === "object" &&
|
|
17
|
-
typeof v.pipe === "function" &&
|
|
18
|
-
typeof v.on === "function"
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function isBlobLike(v) {
|
|
23
|
-
return (
|
|
24
|
-
v &&
|
|
25
|
-
typeof v.arrayBuffer === "function" &&
|
|
26
|
-
(typeof v.type === "string" || typeof v.name === "string")
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function isPairArrayList(arr) {
|
|
31
|
-
return (
|
|
32
|
-
Array.isArray(arr) &&
|
|
33
|
-
arr.length > 0 &&
|
|
34
|
-
arr.every(
|
|
35
|
-
(x) => Array.isArray(x) && x.length === 2 && typeof x[0] === "string"
|
|
36
|
-
)
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
module.exports = {
|
|
41
|
-
getType,
|
|
42
|
-
toStringVal,
|
|
43
|
-
isStream,
|
|
44
|
-
isBlobLike,
|
|
45
|
-
isPairArrayList,
|
|
46
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { jar, client } = require("./client");
|
|
4
|
-
const { cleanGet, get, post, postFormData } = require("./methods");
|
|
5
|
-
const { makeDefaults } = require("./defaults");
|
|
6
|
-
const { setProxy } = require("./proxy");
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
cleanGet,
|
|
10
|
-
get,
|
|
11
|
-
post,
|
|
12
|
-
postFormData,
|
|
13
|
-
jar,
|
|
14
|
-
setProxy,
|
|
15
|
-
makeDefaults,
|
|
16
|
-
client,
|
|
17
|
-
};
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const FormData = require("form-data");
|
|
4
|
-
const headersMod = require("../headers");
|
|
5
|
-
const getHeaders = headersMod.getHeaders || headersMod;
|
|
6
|
-
const { client } = require("./client");
|
|
7
|
-
const { cfg } = require("./config");
|
|
8
|
-
const { requestWithRetry } = require("./retry");
|
|
9
|
-
const {
|
|
10
|
-
getType,
|
|
11
|
-
toStringVal,
|
|
12
|
-
isStream,
|
|
13
|
-
isBlobLike,
|
|
14
|
-
isPairArrayList,
|
|
15
|
-
} = require("./helpers");
|
|
16
|
-
|
|
17
|
-
function cleanGet(url, ctx) {
|
|
18
|
-
return requestWithRetry(() => client.get(url, cfg()), 3, 1000, ctx);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function get(url, reqJar, qs, options, ctx, customHeader) {
|
|
22
|
-
const headers = getHeaders(url, options, ctx, customHeader);
|
|
23
|
-
return requestWithRetry(
|
|
24
|
-
() => client.get(url, cfg({ reqJar, headers, params: qs })),
|
|
25
|
-
3,
|
|
26
|
-
1000,
|
|
27
|
-
ctx
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function post(url, reqJar, form, options, ctx, customHeader) {
|
|
32
|
-
const headers = getHeaders(url, options, ctx, customHeader);
|
|
33
|
-
const ct = String(
|
|
34
|
-
headers["Content-Type"] || headers["content-type"] || "application/x-www-form-urlencoded"
|
|
35
|
-
).toLowerCase();
|
|
36
|
-
let data;
|
|
37
|
-
if (ct.includes("json")) {
|
|
38
|
-
data = JSON.stringify(form || {});
|
|
39
|
-
headers["Content-Type"] = "application/json";
|
|
40
|
-
} else {
|
|
41
|
-
const p = new URLSearchParams();
|
|
42
|
-
if (form && typeof form === "object") {
|
|
43
|
-
for (const k of Object.keys(form)) {
|
|
44
|
-
let v = form[k];
|
|
45
|
-
if (isPairArrayList(v)) {
|
|
46
|
-
for (const [kk, vv] of v) p.append(`${k}[${kk}]`, toStringVal(vv));
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (Array.isArray(v)) {
|
|
50
|
-
for (const x of v) {
|
|
51
|
-
if (Array.isArray(x) && x.length === 2 && typeof x[1] !== "object")
|
|
52
|
-
p.append(k, toStringVal(x[1]));
|
|
53
|
-
else p.append(k, toStringVal(x));
|
|
54
|
-
}
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (getType(v) === "Object") v = JSON.stringify(v);
|
|
58
|
-
p.append(k, toStringVal(v));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
data = p.toString();
|
|
62
|
-
headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
63
|
-
}
|
|
64
|
-
return requestWithRetry(
|
|
65
|
-
() => client.post(url, data, cfg({ reqJar, headers })),
|
|
66
|
-
3,
|
|
67
|
-
1000,
|
|
68
|
-
ctx
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function postFormData(url, reqJar, form, qs, options, ctx) {
|
|
73
|
-
const fd = new FormData();
|
|
74
|
-
if (form && typeof form === "object") {
|
|
75
|
-
for (const k of Object.keys(form)) {
|
|
76
|
-
const v = form[k];
|
|
77
|
-
if (v === undefined || v === null) continue;
|
|
78
|
-
if (isPairArrayList(v)) {
|
|
79
|
-
for (const [kk, vv] of v)
|
|
80
|
-
fd.append(
|
|
81
|
-
`${k}[${kk}]`,
|
|
82
|
-
typeof vv === "object" && !Buffer.isBuffer(vv) && !isStream(vv)
|
|
83
|
-
? JSON.stringify(vv)
|
|
84
|
-
: vv
|
|
85
|
-
);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (Array.isArray(v)) {
|
|
89
|
-
for (const x of v) {
|
|
90
|
-
if (
|
|
91
|
-
Array.isArray(x) &&
|
|
92
|
-
x.length === 2 &&
|
|
93
|
-
x[1] &&
|
|
94
|
-
typeof x[1] === "object" &&
|
|
95
|
-
!Buffer.isBuffer(x[1]) &&
|
|
96
|
-
!isStream(x[1])
|
|
97
|
-
) {
|
|
98
|
-
fd.append(k, x[0], x[1]);
|
|
99
|
-
} else if (
|
|
100
|
-
Array.isArray(x) &&
|
|
101
|
-
x.length === 2 &&
|
|
102
|
-
typeof x[1] !== "object"
|
|
103
|
-
) {
|
|
104
|
-
fd.append(k, toStringVal(x[1]));
|
|
105
|
-
} else if (
|
|
106
|
-
x &&
|
|
107
|
-
typeof x === "object" &&
|
|
108
|
-
"value" in x &&
|
|
109
|
-
"options" in x
|
|
110
|
-
) {
|
|
111
|
-
fd.append(k, x.value, x.options || {});
|
|
112
|
-
} else if (isStream(x) || Buffer.isBuffer(x) || typeof x === "string") {
|
|
113
|
-
fd.append(k, x);
|
|
114
|
-
} else if (isBlobLike(x)) {
|
|
115
|
-
const buf = Buffer.from(await x.arrayBuffer());
|
|
116
|
-
fd.append(k, buf, {
|
|
117
|
-
filename: x.name || k,
|
|
118
|
-
contentType: x.type || undefined,
|
|
119
|
-
});
|
|
120
|
-
} else {
|
|
121
|
-
fd.append(k, JSON.stringify(x));
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
if (v && typeof v === "object" && "value" in v && "options" in v) {
|
|
127
|
-
fd.append(k, v.value, v.options || {});
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
if (isStream(v) || Buffer.isBuffer(v) || typeof v === "string") {
|
|
131
|
-
fd.append(k, v);
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
if (isBlobLike(v)) {
|
|
135
|
-
const buf = Buffer.from(await v.arrayBuffer());
|
|
136
|
-
fd.append(k, buf, {
|
|
137
|
-
filename: v.name || k,
|
|
138
|
-
contentType: v.type || undefined,
|
|
139
|
-
});
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
if (typeof v === "number" || typeof v === "boolean") {
|
|
143
|
-
fd.append(k, toStringVal(v));
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
fd.append(k, JSON.stringify(v));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
const headers = { ...getHeaders(url, options, ctx), ...fd.getHeaders() };
|
|
150
|
-
return requestWithRetry(
|
|
151
|
-
() => client.post(url, fd, cfg({ reqJar, headers, params: qs })),
|
|
152
|
-
3,
|
|
153
|
-
1000,
|
|
154
|
-
ctx
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
module.exports = {
|
|
159
|
-
cleanGet,
|
|
160
|
-
get,
|
|
161
|
-
post,
|
|
162
|
-
postFormData,
|
|
163
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { HttpsProxyAgent } = require("https-proxy-agent");
|
|
4
|
-
const { client } = require("./client");
|
|
5
|
-
|
|
6
|
-
function setProxy(proxyUrl) {
|
|
7
|
-
if (!proxyUrl) {
|
|
8
|
-
client.defaults.httpAgent = undefined;
|
|
9
|
-
client.defaults.httpsAgent = undefined;
|
|
10
|
-
client.defaults.proxy = false;
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
const agent = new HttpsProxyAgent(proxyUrl);
|
|
14
|
-
client.defaults.httpAgent = agent;
|
|
15
|
-
client.defaults.httpsAgent = agent;
|
|
16
|
-
client.defaults.proxy = false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = {
|
|
20
|
-
setProxy,
|
|
21
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { delay } = require("./client");
|
|
4
|
-
|
|
5
|
-
async function requestWithRetry(fn, retries = 3, baseDelay = 1000, ctx) {
|
|
6
|
-
let lastError;
|
|
7
|
-
const emit = (event, payload) => {
|
|
8
|
-
try {
|
|
9
|
-
if (ctx && ctx._emitter && typeof ctx._emitter.emit === "function") {
|
|
10
|
-
ctx._emitter.emit(event, payload);
|
|
11
|
-
}
|
|
12
|
-
} catch {}
|
|
13
|
-
};
|
|
14
|
-
for (let i = 0; i < retries; i++) {
|
|
15
|
-
try {
|
|
16
|
-
return await fn();
|
|
17
|
-
} catch (e) {
|
|
18
|
-
lastError = e;
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
e?.code === "ERR_INVALID_CHAR" ||
|
|
22
|
-
(e?.message && e.message.includes("Invalid character in header"))
|
|
23
|
-
) {
|
|
24
|
-
const err = new Error(
|
|
25
|
-
"Invalid header content detected. Request aborted to prevent crash."
|
|
26
|
-
);
|
|
27
|
-
err.error = "Invalid header content";
|
|
28
|
-
err.originalError = e;
|
|
29
|
-
err.code = "ERR_INVALID_CHAR";
|
|
30
|
-
return Promise.reject(err);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const status = e?.response?.status || e?.statusCode || 0;
|
|
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
|
-
}
|
|
39
|
-
if (status >= 400 && status < 500 && status !== 429) {
|
|
40
|
-
return e.response || Promise.reject(e);
|
|
41
|
-
}
|
|
42
|
-
if (i === retries - 1) {
|
|
43
|
-
return e.response || Promise.reject(e);
|
|
44
|
-
}
|
|
45
|
-
const netCode = e?.code || "";
|
|
46
|
-
const msg = e && e.message ? e.message : String(e || "");
|
|
47
|
-
if (
|
|
48
|
-
!status &&
|
|
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
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const backoffDelay = Math.min(
|
|
65
|
-
baseDelay * Math.pow(2, i) + Math.floor(Math.random() * 200),
|
|
66
|
-
30000
|
|
67
|
-
);
|
|
68
|
-
await delay(backoffDelay);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
const finalError = lastError || new Error("Request failed after retries");
|
|
72
|
-
return Promise.reject(finalError);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
module.exports = {
|
|
76
|
-
requestWithRetry,
|
|
77
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function sanitizeHeaderValue(value) {
|
|
4
|
-
if (value === null || value === undefined) return "";
|
|
5
|
-
const str = String(value);
|
|
6
|
-
return str.replace(/[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F\r\n]/g, "").trim();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function sanitizeHeaderName(name) {
|
|
10
|
-
if (!name || typeof name !== "string") return "";
|
|
11
|
-
return name.replace(/[^\x21-\x7E]/g, "").trim();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function sanitizeHeaders(headers) {
|
|
15
|
-
if (!headers || typeof headers !== "object") return {};
|
|
16
|
-
const sanitized = {};
|
|
17
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
18
|
-
const sanitizedKey = sanitizeHeaderName(key);
|
|
19
|
-
if (!sanitizedKey) continue;
|
|
20
|
-
|
|
21
|
-
if (Array.isArray(value)) continue;
|
|
22
|
-
if (value !== null && typeof value === "object") continue;
|
|
23
|
-
if (typeof value === "function") continue;
|
|
24
|
-
|
|
25
|
-
if (typeof value === "string") {
|
|
26
|
-
const trimmed = value.trim();
|
|
27
|
-
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
|
28
|
-
try {
|
|
29
|
-
const parsed = JSON.parse(trimmed);
|
|
30
|
-
if (Array.isArray(parsed)) continue;
|
|
31
|
-
} catch {
|
|
32
|
-
// continue with normal sanitization
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const sanitizedValue = sanitizeHeaderValue(value);
|
|
38
|
-
if (sanitizedValue !== "") {
|
|
39
|
-
sanitized[sanitizedKey] = sanitizedValue;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return sanitized;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
module.exports = {
|
|
46
|
-
sanitizeHeaderValue,
|
|
47
|
-
sanitizeHeaderName,
|
|
48
|
-
sanitizeHeaders,
|
|
49
|
-
};
|