fca-nx 6.8.8 → 7.0.5
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 +24 -10
- package/func/checkUpdate.js +1 -1
- package/func/logAdapter.js +2 -0
- package/index.js +3 -0
- package/module/config.js +5 -2
- package/module/login.js +38 -13
- package/module/loginHelper.js +164 -127
- package/module/options.js +7 -1
- package/package.json +2 -2
- package/src/api/action/addfriend.js +9 -3
- package/src/api/action/changeAvatar.js +1 -0
- package/src/api/action/changeCover.js +3 -1
- package/src/api/action/enableAutoSaveAppState.js +18 -5
- package/src/api/action/refreshFb_dtsg.js +18 -76
- package/src/api/http/httpGet.js +6 -2
- package/src/api/http/httpPost.js +7 -2
- package/src/api/messaging/createNewGroup.js +1 -1
- package/src/api/messaging/deleteMessage.js +1 -1
- package/src/api/messaging/forwardMessage.js +9 -1
- package/src/api/messaging/getEmojiUrl.js +5 -1
- package/src/api/messaging/getMessage.js +4 -2
- package/src/api/messaging/getThemePictures.js +21 -28
- package/src/api/messaging/markAsRead.js +1 -0
- package/src/api/messaging/muteThread.js +1 -1
- package/src/api/messaging/pinMessage.js +10 -1
- package/src/api/messaging/scheduler.js +71 -4
- package/src/api/messaging/sendBroadcast.js +15 -0
- package/src/api/messaging/sendMessage.js +23 -1
- package/src/api/messaging/sendTypingIndicator.js +0 -15
- package/src/api/messaging/sessionGuard.js +45 -9
- package/src/api/messaging/setMessageReaction.js +2 -1
- package/src/api/messaging/threadColors.js +45 -12
- package/src/api/messaging/uploadAttachment.js +37 -3
- package/src/api/socket/OldMessage.js +7 -2
- package/src/api/socket/core/connectMqtt.js +68 -81
- package/src/api/socket/core/emitAuth.js +11 -2
- package/src/api/socket/core/getSeqID.js +28 -36
- package/src/api/socket/core/getTaskResponseData.js +3 -1
- package/src/api/socket/core/parseDelta.js +1 -1
- package/src/api/socket/detail/buildStream.js +4 -1
- package/src/api/socket/detail/constants.js +4 -1
- package/src/api/socket/e2ee/crypto.js +40 -2
- package/src/api/socket/e2ee/index.js +124 -73
- package/src/api/socket/e2ee/protocol.js +40 -32
- package/src/api/socket/e2ee/ratchet.js +43 -37
- package/src/api/socket/e2ee/store.js +47 -133
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +18 -140
- package/src/api/socket/listenE2EE.js +23 -12
- package/src/api/socket/listenMqtt.js +94 -110
- package/src/api/socket/middleware/index.js +61 -7
- package/src/api/socket/sendMessage.js +78 -170
- package/src/api/socket/sendMessageMqtt.js +6 -0
- package/src/api/threads/getThreadHistory.js +68 -38
- package/src/api/threads/getThreadInfo.js +4 -3
- package/src/api/threads/getThreadList.js +34 -31
- package/src/api/threads/getThreadPictures.js +1 -1
- package/src/api/users/getUserInfo.js +5 -2
- package/src/api/users/getUserInfoV2.js +65 -59
- package/src/app/createFcaClient.js +2 -2
- package/src/core/sendReqMqtt.js +2 -1
- package/src/database/helpers.js +12 -0
- package/src/database/models/index.js +8 -9
- package/src/utils/broadcast.js +1 -1
- package/src/utils/clientIdentity.js +50 -3
- package/src/utils/cookies.js +4 -2
- package/src/utils/format/utils.js +10 -0
- package/src/utils/headers.js +26 -12
- package/src/utils/loginParser/autoLogin.js +12 -0
- package/src/utils/loginParser/parseAndCheckLogin.js +7 -0
- package/src/utils/loginParser/textUtils.js +6 -0
- package/src/utils/request/config.js +4 -1
- package/src/utils/request/methods.js +0 -13
- package/src/utils/request/proxy.js +13 -0
- package/src/utils/request/retry.js +0 -10
- package/src/utils/request/sanitize.js +1 -1
- package/src/utils/textEffects.js +85 -0
- package/src/utils/utils.js +17 -3
- package/src/utils/instanceLock.js +0 -35
- package/src/utils/ipGuard.js +0 -58
- package/src/utils/sendThrottle.js +0 -51
|
@@ -14,19 +14,6 @@ const {
|
|
|
14
14
|
isPairArrayList,
|
|
15
15
|
} = require("./helpers");
|
|
16
16
|
|
|
17
|
-
let __lastFbPostAt = 0;
|
|
18
|
-
const __MIN_FB_POST_GAP_MS = 180;
|
|
19
|
-
async function __paceFbPost(url) {
|
|
20
|
-
try {
|
|
21
|
-
const u = String(url || "");
|
|
22
|
-
if (!/facebook\.com|messenger\.com/i.test(u)) return;
|
|
23
|
-
const now = Date.now();
|
|
24
|
-
const wait = __MIN_FB_POST_GAP_MS - (now - __lastFbPostAt);
|
|
25
|
-
if (wait > 0) await new Promise(r => setTimeout(r, wait + Math.floor(Math.random() * 80)));
|
|
26
|
-
__lastFbPostAt = Date.now();
|
|
27
|
-
} catch (_) {}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
17
|
function cleanGet(url, ctx) {
|
|
31
18
|
return requestWithRetry(() => client.get(url, cfg()), 3, 1000, ctx);
|
|
32
19
|
}
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
const { HttpsProxyAgent } = require("https-proxy-agent");
|
|
4
4
|
const { client } = require("./client");
|
|
5
5
|
|
|
6
|
+
// Per-proxy-URL agent cache so we don't create a new agent (and socket pool)
|
|
7
|
+
// on every single request, while still letting different sessions in the
|
|
8
|
+
// same process use different proxies (or no proxy) without stepping on each
|
|
9
|
+
// other. Keyed by the proxy URL string; "" represents "no proxy".
|
|
6
10
|
const agentCache = new Map();
|
|
7
11
|
|
|
8
12
|
function getAgentForProxy(proxyUrl) {
|
|
@@ -14,6 +18,15 @@ function getAgentForProxy(proxyUrl) {
|
|
|
14
18
|
return agent;
|
|
15
19
|
}
|
|
16
20
|
|
|
21
|
+
// Global default proxy setter, kept for backward compatibility with
|
|
22
|
+
// existing callers (api.setOptions({ proxy })) and single-account use.
|
|
23
|
+
// NOTE: this changes the DEFAULT used only when a request doesn't specify
|
|
24
|
+
// its own per-session proxy (see cfg() in ./config.js, which now prefers
|
|
25
|
+
// ctx.globalOptions.proxy when present). In a multi-account process, each
|
|
26
|
+
// account's requests should carry their own ctx.globalOptions.proxy so
|
|
27
|
+
// calling setProxy() for one account can no longer change another running
|
|
28
|
+
// account's outbound IP mid-session - that accidental cross-session proxy
|
|
29
|
+
// change was flagged as a real risk for "abnormal session" detection.
|
|
17
30
|
function setProxy(proxyUrl) {
|
|
18
31
|
if (!proxyUrl) {
|
|
19
32
|
client.defaults.httpAgent = undefined;
|
|
@@ -2,18 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const { delay } = require("./client");
|
|
4
4
|
|
|
5
|
-
let __lastFbAt = 0;
|
|
6
|
-
async function __pace() {
|
|
7
|
-
const now = Date.now();
|
|
8
|
-
const gap = 150 + Math.floor(Math.random() * 100);
|
|
9
|
-
const wait = gap - (now - __lastFbAt);
|
|
10
|
-
if (wait > 0) await delay(wait);
|
|
11
|
-
__lastFbAt = Date.now();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
5
|
async function requestWithRetry(fn, retries = 3, baseDelay = 1000, ctx) {
|
|
15
6
|
let lastError;
|
|
16
|
-
try { await __pace(); } catch (_) {}
|
|
17
7
|
const emit = (event, payload) => {
|
|
18
8
|
try {
|
|
19
9
|
if (ctx && ctx._emitter && typeof ctx._emitter.emit === "function") {
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const TEXT_EFFECTS = Object.freeze({
|
|
4
|
+
hearts: "1",
|
|
5
|
+
heart: "1",
|
|
6
|
+
love: "1",
|
|
7
|
+
gift: "2",
|
|
8
|
+
present: "2",
|
|
9
|
+
box: "2",
|
|
10
|
+
giftbox: "2",
|
|
11
|
+
fire: "3",
|
|
12
|
+
flame: "3",
|
|
13
|
+
hot: "3",
|
|
14
|
+
confetti: "4",
|
|
15
|
+
confecti: "4",
|
|
16
|
+
party: "4",
|
|
17
|
+
celebrate: "4",
|
|
18
|
+
celebration: "4",
|
|
19
|
+
sparkles: "5",
|
|
20
|
+
sparkle: "5",
|
|
21
|
+
star: "5",
|
|
22
|
+
stars: "5",
|
|
23
|
+
magic: "5"
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const EFFECT_PROPERTIES = [
|
|
27
|
+
"textEffect",
|
|
28
|
+
"text_effect",
|
|
29
|
+
"text_effect_id",
|
|
30
|
+
"textEffectId",
|
|
31
|
+
"textEffectID",
|
|
32
|
+
"effect",
|
|
33
|
+
"effect_id",
|
|
34
|
+
"effectID",
|
|
35
|
+
"text_effect_name",
|
|
36
|
+
"effectName",
|
|
37
|
+
"effect_name"
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
function toTextEffectId(effect) {
|
|
41
|
+
if (effect === null || effect === undefined) return null;
|
|
42
|
+
if (typeof effect === "number" && Number.isFinite(effect)) return String(effect);
|
|
43
|
+
if (typeof effect !== "string") return null;
|
|
44
|
+
|
|
45
|
+
const value = effect.trim();
|
|
46
|
+
if (!value) return null;
|
|
47
|
+
return TEXT_EFFECTS[value.toLowerCase()] || value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getTextEffectId(message) {
|
|
51
|
+
if (!message || typeof message !== "object") return null;
|
|
52
|
+
for (const property of EFFECT_PROPERTIES) {
|
|
53
|
+
if (message[property] !== undefined && message[property] !== null) {
|
|
54
|
+
const effectID = toTextEffectId(message[property]);
|
|
55
|
+
if (effectID) return effectID;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const rawIDs = Array.isArray(message.text_effect_ids)
|
|
60
|
+
? message.text_effect_ids
|
|
61
|
+
: [message.text_effect_ids];
|
|
62
|
+
for (const rawID of rawIDs) {
|
|
63
|
+
const effectID = toTextEffectId(rawID);
|
|
64
|
+
if (effectID) return effectID;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getTextEffectIds(message) {
|
|
70
|
+
if (!message || typeof message !== "object") return [];
|
|
71
|
+
const rawIDs = Array.isArray(message.text_effect_ids)
|
|
72
|
+
? message.text_effect_ids
|
|
73
|
+
: [message.text_effect_ids];
|
|
74
|
+
return rawIDs
|
|
75
|
+
.map(toTextEffectId)
|
|
76
|
+
.filter(Boolean);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
EFFECT_PROPERTIES,
|
|
81
|
+
TEXT_EFFECTS,
|
|
82
|
+
getTextEffectId,
|
|
83
|
+
getTextEffectIds,
|
|
84
|
+
toTextEffectId
|
|
85
|
+
};
|
package/src/utils/utils.js
CHANGED
|
@@ -225,7 +225,13 @@ function decodeClientPayload(payload) {
|
|
|
225
225
|
|
|
226
226
|
function parseAndCheckLogin(ctx, defaultFuncs) {
|
|
227
227
|
return function (res) {
|
|
228
|
-
|
|
228
|
+
// [Fixed by xalman] this used to read res.statusCode/res.body, which are the old
|
|
229
|
+
// `request`/`request-promise` library's property names. The actual
|
|
230
|
+
// HTTP client here (src/utils/request) is axios-based, whose
|
|
231
|
+
// responses use res.status/res.data instead - res.body was always
|
|
232
|
+
// undefined, so this threw "Empty response body" on every single
|
|
233
|
+
// call. That silently broke OldMessage.js (used for DM attachments,
|
|
234
|
+
// and as the fallback whenever an MQTT send fails).
|
|
229
235
|
var status = res && res.status !== undefined ? res.status : (res && res.statusCode);
|
|
230
236
|
if (status >= 500 && status < 600) {
|
|
231
237
|
throw { error: "Facebook internal server error: " + status, res };
|
|
@@ -297,7 +303,7 @@ function makeDefaults(html, userID, ctx) {
|
|
|
297
303
|
|
|
298
304
|
function _formatAttachment(attachment, atch) {
|
|
299
305
|
atch = atch || attachment.mercury || {};
|
|
300
|
-
|
|
306
|
+
// Also pull in mercuryJSON if mercury was empty
|
|
301
307
|
if (attachment.mercuryJSON && !atch.attach_type && !atch.blob_attachment && !atch.sticker_attachment) {
|
|
302
308
|
try { Object.assign(atch, JSON.parse(attachment.mercuryJSON)); } catch (_) {}
|
|
303
309
|
}
|
|
@@ -371,7 +377,8 @@ function _formatAttachment(attachment, atch) {
|
|
|
371
377
|
attach.framesPerCol = preview.framesPerCol || atch.frames_per_column;
|
|
372
378
|
break;
|
|
373
379
|
default: {
|
|
374
|
-
|
|
380
|
+
// blob_attachment lives inside atch (mercury) for MQTT delta messages.
|
|
381
|
+
// Fall back to attachment.blob_attachment for ForcedFetch / getThreadHistory paths.
|
|
375
382
|
var blob = atch.blob_attachment || attachment.blob_attachment;
|
|
376
383
|
if (blob) {
|
|
377
384
|
var typename = blob.__typename || "";
|
|
@@ -414,6 +421,7 @@ function _formatAttachment(attachment, atch) {
|
|
|
414
421
|
attach.ID = blob.legacy_attachment_id || blob.message_file_fbid || attach.ID;
|
|
415
422
|
}
|
|
416
423
|
|
|
424
|
+
// sticker_attachment lives inside atch (mercury) in MQTT delta messages
|
|
417
425
|
if (!blob && atch.sticker_attachment) {
|
|
418
426
|
var sticker = atch.sticker_attachment;
|
|
419
427
|
attach.type = "sticker";
|
|
@@ -444,6 +452,7 @@ function _extractMentions(delta, body) {
|
|
|
444
452
|
var mentions = {};
|
|
445
453
|
var d = delta.data || {};
|
|
446
454
|
|
|
455
|
+
// Strategy 1: prng JSON array [{i, o, l}] — legacy delta format
|
|
447
456
|
try {
|
|
448
457
|
if (d.prng) {
|
|
449
458
|
JSON.parse(d.prng).forEach(function(u) {
|
|
@@ -453,6 +462,7 @@ function _extractMentions(delta, body) {
|
|
|
453
462
|
}
|
|
454
463
|
} catch (_) {}
|
|
455
464
|
|
|
465
|
+
// Strategy 2: flat CSV fields in data (data.mention_ids / mentions_ids / mentionIds)
|
|
456
466
|
if (!Object.keys(mentions).length) {
|
|
457
467
|
var mids = _splitCsv(d.mention_ids || d.mentions_ids || d.mentionIds);
|
|
458
468
|
var mos = _splitCsv(d.mention_offsets || d.mentions_offsets || d.mentionOffsets);
|
|
@@ -463,6 +473,7 @@ function _extractMentions(delta, body) {
|
|
|
463
473
|
}
|
|
464
474
|
}
|
|
465
475
|
|
|
476
|
+
// Strategy 3: ranges / profileRanges array (newer delta format)
|
|
466
477
|
if (!Object.keys(mentions).length) {
|
|
467
478
|
var ranges = [];
|
|
468
479
|
if (Array.isArray(delta.ranges)) ranges = delta.ranges;
|
|
@@ -478,6 +489,9 @@ function _extractMentions(delta, body) {
|
|
|
478
489
|
});
|
|
479
490
|
}
|
|
480
491
|
|
|
492
|
+
// Strategy 4: Gb protobuf nested format — Facebook encodes mentions inside
|
|
493
|
+
// messageMetadata.data.data.Gb.asMap.data when sending via the newer Gb path.
|
|
494
|
+
// Reference: stfca formatDeltaMessage (confirmed working in v1.3.2+)
|
|
481
495
|
if (!Object.keys(mentions).length) {
|
|
482
496
|
try {
|
|
483
497
|
var md2 = delta.messageMetadata || {};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const STALE_MS = 120000;
|
|
6
|
-
let timer = null;
|
|
7
|
-
|
|
8
|
-
function acquire(userID) {
|
|
9
|
-
if (/^(off|0|false)$/i.test(String(process.env.FCA_NX_INSTANCE_LOCK || ""))) return { ok: true };
|
|
10
|
-
const dir = process.env.FCA_NX_STATE_DIR || path.join(process.cwd(), ".fca-nx");
|
|
11
|
-
const file = path.join(dir, "instance_" + String(userID || "default") + ".lock");
|
|
12
|
-
try {
|
|
13
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
-
try {
|
|
15
|
-
const cur = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
16
|
-
if (cur && cur.pid !== process.pid && Date.now() - cur.at < STALE_MS) {
|
|
17
|
-
let alive = true;
|
|
18
|
-
try { process.kill(cur.pid, 0); } catch (e) { alive = e.code === "EPERM"; }
|
|
19
|
-
|
|
20
|
-
if (alive || Date.now() - cur.at < 45000) return { ok: false, pid: cur.pid };
|
|
21
|
-
}
|
|
22
|
-
} catch (_) {}
|
|
23
|
-
const beat = () => { try { fs.writeFileSync(file, JSON.stringify({ pid: process.pid, at: Date.now() })); } catch (_) {} };
|
|
24
|
-
beat();
|
|
25
|
-
if (timer) clearInterval(timer);
|
|
26
|
-
timer = setInterval(beat, 30000);
|
|
27
|
-
if (timer.unref) timer.unref();
|
|
28
|
-
const release = () => { try { const c = JSON.parse(fs.readFileSync(file, "utf8")); if (c.pid === process.pid) fs.unlinkSync(file); } catch (_) {} };
|
|
29
|
-
process.once("exit", release);
|
|
30
|
-
for (const sig of ["SIGINT", "SIGTERM"]) process.once(sig, () => { release(); process.exit(0); });
|
|
31
|
-
} catch (_) { }
|
|
32
|
-
return { ok: true };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
module.exports = { acquire };
|
package/src/utils/ipGuard.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("fs");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const logger = require("../../func/logger");
|
|
6
|
-
|
|
7
|
-
const ENDPOINTS = [
|
|
8
|
-
"https://api.ipify.org?format=json",
|
|
9
|
-
"https://ipinfo.io/json"
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
function stateFile() {
|
|
13
|
-
const dir = process.env.FCA_NX_STATE_DIR || path.join(process.cwd(), ".fca-nx");
|
|
14
|
-
return { dir, file: path.join(dir, "last_login_ip.json") };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async function lookupIP(client) {
|
|
18
|
-
for (const url of ENDPOINTS) {
|
|
19
|
-
try {
|
|
20
|
-
const res = await client.get(url, { timeout: 5000, headers: { Accept: "application/json" } });
|
|
21
|
-
const ip = res && res.data && (res.data.ip || res.data.query);
|
|
22
|
-
if (typeof ip === "string" && ip) return { ip, country: res.data.country || null, region: res.data.region || null };
|
|
23
|
-
} catch (_) { }
|
|
24
|
-
}
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async function checkLoginIP(client, userID) {
|
|
29
|
-
const mode = String(process.env.FCA_NX_IP_GUARD || "warn").toLowerCase();
|
|
30
|
-
if (mode === "off" || mode === "0" || mode === "false") return { ok: true, changed: false, ip: null, previous: null };
|
|
31
|
-
const now = await lookupIP(client);
|
|
32
|
-
if (!now) {
|
|
33
|
-
logger("ipGuard: could not determine public IP (continuing)", "warn");
|
|
34
|
-
return { ok: true, changed: false, ip: null, previous: null };
|
|
35
|
-
}
|
|
36
|
-
const { dir, file } = stateFile();
|
|
37
|
-
let all = {};
|
|
38
|
-
try { all = JSON.parse(fs.readFileSync(file, "utf8")) || {}; } catch (_) {}
|
|
39
|
-
const key = String(userID || "default");
|
|
40
|
-
const prev = all[key] && all[key].ip ? all[key] : null;
|
|
41
|
-
const changed = !!prev && prev.ip !== now.ip;
|
|
42
|
-
if (changed) {
|
|
43
|
-
const msg = `ipGuard: login IP changed ${prev.ip} -> ${now.ip}. Logging into the same account from different IPs/locations is a top cause of "automated behaviour", checkpoints and suspensions. Use ONE host with a fixed IP (or a static proxy via the 'proxy' option).`;
|
|
44
|
-
if (mode === "block") {
|
|
45
|
-
logger(msg + " Login refused (FCA_NX_IP_GUARD=block). Set FCA_NX_IP_GUARD=warn to override.", "error");
|
|
46
|
-
return { ok: false, changed: true, ip: now.ip, previous: prev.ip };
|
|
47
|
-
}
|
|
48
|
-
logger(msg, "warn");
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
52
|
-
all[key] = { ip: now.ip, country: now.country, region: now.region, at: new Date().toISOString() };
|
|
53
|
-
fs.writeFileSync(file, JSON.stringify(all, null, 2));
|
|
54
|
-
} catch (_) {}
|
|
55
|
-
return { ok: true, changed, ip: now.ip, previous: prev ? prev.ip : null };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
module.exports = { checkLoginIP, lookupIP };
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const state = { last: 0, chain: Promise.resolve(), delay: null, jitter: null, stamps: [] };
|
|
4
|
-
|
|
5
|
-
function maxPerMin() {
|
|
6
|
-
const n = Number(process.env.FCA_NX_SEND_MAX_PER_MIN);
|
|
7
|
-
return Number.isFinite(n) && n >= 0 ? n : 12;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function readNum(v, d) {
|
|
11
|
-
const n = Number(v);
|
|
12
|
-
return Number.isFinite(n) && n >= 0 ? n : d;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function getDelay() {
|
|
16
|
-
return state.delay != null ? state.delay : readNum(process.env.FCA_NX_SEND_DELAY_MS, 1500);
|
|
17
|
-
}
|
|
18
|
-
function getJitter() {
|
|
19
|
-
return state.jitter != null ? state.jitter : readNum(process.env.FCA_NX_SEND_JITTER_MS, 600);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function setSendDelay(ms, jitterMs) {
|
|
23
|
-
if (ms != null) state.delay = readNum(ms, 1000);
|
|
24
|
-
if (jitterMs != null) state.jitter = readNum(jitterMs, 400);
|
|
25
|
-
return { delay: getDelay(), jitter: getJitter() };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function waitTurn() {
|
|
29
|
-
const delay = getDelay();
|
|
30
|
-
if (delay <= 0) return Promise.resolve();
|
|
31
|
-
const turn = state.chain.then(async () => {
|
|
32
|
-
const gap = delay + Math.floor(Math.random() * (getJitter() + 1));
|
|
33
|
-
const wait = state.last + gap - Date.now();
|
|
34
|
-
if (wait > 0) await new Promise(r => setTimeout(r, wait));
|
|
35
|
-
const cap = maxPerMin();
|
|
36
|
-
if (cap > 0) {
|
|
37
|
-
for (;;) {
|
|
38
|
-
const now = Date.now();
|
|
39
|
-
state.stamps = state.stamps.filter(t => now - t < 60000);
|
|
40
|
-
if (state.stamps.length < cap) break;
|
|
41
|
-
await new Promise(r => setTimeout(r, state.stamps[0] + 60000 - now + 50));
|
|
42
|
-
}
|
|
43
|
-
state.stamps.push(Date.now());
|
|
44
|
-
}
|
|
45
|
-
state.last = Date.now();
|
|
46
|
-
});
|
|
47
|
-
state.chain = turn.catch(() => {});
|
|
48
|
-
return turn;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
module.exports = { waitTurn, setSendDelay };
|