@dongdev/fca-unofficial 3.0.27 → 3.0.28

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/module/config.js CHANGED
@@ -1,30 +1,30 @@
1
- const fs = require("fs");
2
- const path = require("path");
3
- const logger = require("../func/logger");
4
- const defaultConfig = {
5
- autoUpdate: true,
6
- mqtt: { enabled: true, reconnectInterval: 3600 },
7
- autoLogin: true,
8
- apiServer: "https://minhdong.site",
9
- apiKey: "",
10
- credentials: { email: "", password: "", twofactor: "" }
11
- };
12
-
13
- function loadConfig() {
14
- const configPath = path.join(process.cwd(), "fca-config.json");
15
- let config;
16
- if (!fs.existsSync(configPath)) {
17
- config = defaultConfig;
18
- } else {
19
- try {
20
- const fileContent = fs.readFileSync(configPath, "utf8");
21
- config = Object.assign({}, defaultConfig, JSON.parse(fileContent));
22
- } catch (err) {
23
- logger(`Error reading config file: ${err.message}`, "error");
24
- config = defaultConfig;
25
- }
26
- }
27
- return { config, configPath };
28
- }
29
-
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const logger = require("../func/logger");
4
+ const defaultConfig = {
5
+ autoUpdate: true,
6
+ mqtt: { enabled: true, reconnectInterval: 3600 },
7
+ autoLogin: true,
8
+ apiServer: "https://minhdong.site",
9
+ apiKey: "",
10
+ credentials: { email: "", password: "", twofactor: "" }
11
+ };
12
+
13
+ function loadConfig() {
14
+ const configPath = path.join(process.cwd(), "fca-config.json");
15
+ let config;
16
+ if (!fs.existsSync(configPath)) {
17
+ config = defaultConfig;
18
+ } else {
19
+ try {
20
+ const fileContent = fs.readFileSync(configPath, "utf8");
21
+ config = Object.assign({}, defaultConfig, JSON.parse(fileContent));
22
+ } catch (err) {
23
+ logger(`Error reading config file: ${err.message}`, "error");
24
+ config = defaultConfig;
25
+ }
26
+ }
27
+ return { config, configPath };
28
+ }
29
+
30
30
  module.exports = { loadConfig, defaultConfig };
package/module/login.js CHANGED
@@ -1,134 +1,133 @@
1
- const { getType } = require("../src/utils/format");
2
- const { setOptions } = require("./options");
3
- const { loadConfig } = require("./config");
4
- const { checkAndUpdateVersion } = require("../func/checkUpdate");
5
- const loginHelper = require("./loginHelper");
6
- const logger = require("../func/logger");
7
-
8
- const { config } = loadConfig();
9
- global.fca = { config };
10
-
11
- // Global error handlers to prevent bot crashes
12
- // Handle unhandled promise rejections (e.g., fetch timeouts, network errors)
13
- if (!global.fca._errorHandlersInstalled) {
14
- global.fca._errorHandlersInstalled = true;
15
-
16
- process.on("unhandledRejection", (reason, promise) => {
17
- try {
18
- // Check if it's a fetch/network timeout error
19
- if (reason && typeof reason === "object") {
20
- const errorCode = reason.code || reason.cause?.code;
21
- const errorMessage = reason.message || String(reason);
22
-
23
- // Suppress Sequelize instance errors (handled gracefully in getBackupModel)
24
- if (errorMessage.includes("No Sequelize instance passed")) {
25
- return; // Silently ignore - already handled
26
- }
27
-
28
- // Handle fetch timeout errors gracefully
29
- if (errorCode === "UND_ERR_CONNECT_TIMEOUT" ||
30
- errorCode === "ETIMEDOUT" ||
31
- errorMessage.includes("Connect Timeout") ||
32
- errorMessage.includes("fetch failed")) {
33
- logger(`Network timeout error caught (non-fatal): ${errorMessage}`, "warn");
34
- return; // Don't crash, just log
35
- }
36
-
37
- // Handle other network errors
38
- if (errorCode === "ECONNREFUSED" ||
39
- errorCode === "ENOTFOUND" ||
40
- errorCode === "ECONNRESET" ||
41
- errorMessage.includes("ECONNREFUSED") ||
42
- errorMessage.includes("ENOTFOUND")) {
43
- logger(`Network connection error caught (non-fatal): ${errorMessage}`, "warn");
44
- return; // Don't crash, just log
45
- }
46
- }
47
-
48
- // For other unhandled rejections, log but don't crash
49
- logger(`Unhandled promise rejection (non-fatal): ${reason && reason.message ? reason.message : String(reason)}`, "error");
50
- } catch (e) {
51
- // Fallback if logger fails - silent
52
- }
53
- });
54
-
55
- // Handle uncaught exceptions (last resort)
56
- process.on("uncaughtException", (error) => {
57
- try {
58
- const errorMessage = error.message || String(error);
59
- const errorCode = error.code;
60
-
61
- // Suppress Sequelize instance errors (handled gracefully in getBackupModel)
62
- if (errorMessage.includes("No Sequelize instance passed")) {
63
- return; // Silently ignore - already handled
64
- }
65
-
66
- // Handle fetch/network errors
67
- if (errorCode === "UND_ERR_CONNECT_TIMEOUT" ||
68
- errorCode === "ETIMEDOUT" ||
69
- errorMessage.includes("Connect Timeout") ||
70
- errorMessage.includes("fetch failed")) {
71
- logger(`Uncaught network timeout error (non-fatal): ${errorMessage}`, "warn");
72
- return; // Don't crash
73
- }
74
-
75
- // For other uncaught exceptions, log but try to continue
76
- logger(`Uncaught exception (attempting to continue): ${errorMessage}`, "error");
77
- // Note: We don't exit here to allow bot to continue running
78
- // In production, you might want to restart the process instead
79
- } catch (e) {
80
- // Fallback if logger fails - silent
81
- }
82
- });
83
- }
84
-
85
- function login(loginData, options, callback) {
86
- if (getType(options) === "Function" || getType(options) === "AsyncFunction") {
87
- callback = options;
88
- options = {};
89
- }
90
- const globalOptions = {
91
- selfListen: false,
92
- selfListenEvent: false,
93
- listenEvents: false,
94
- listenTyping: false,
95
- updatePresence: false,
96
- forceLogin: false,
97
- autoMarkDelivery: true,
98
- autoMarkRead: false,
99
- autoReconnect: true,
100
- online: true,
101
- emitReady: false,
102
- userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
103
- };
104
- setOptions(globalOptions, options);
105
- let prCallback = null;
106
- let rejectFunc = null;
107
- let resolveFunc = null;
108
- let returnPromise = null;
109
- if (getType(callback) !== "Function" && getType(callback) !== "AsyncFunction") {
110
- returnPromise = new Promise(function (resolve, reject) {
111
- resolveFunc = resolve;
112
- rejectFunc = reject;
113
- });
114
- prCallback = function (error, api) {
115
- if (error) return rejectFunc(error);
116
- return resolveFunc(api);
117
- };
118
- callback = prCallback;
119
- }
120
- const proceed = () => loginHelper(loginData.appState, loginData.Cookie, loginData.email, loginData.password, globalOptions, callback, prCallback);
121
- if (config && config.autoUpdate) {
122
- const p = checkAndUpdateVersion();
123
- if (p && typeof p.then === "function") {
124
- p.then(proceed).catch(err => callback(err));
125
- } else {
126
- proceed();
127
- }
128
- } else {
129
- proceed();
130
- }
131
- return returnPromise;
132
- }
133
-
134
- module.exports = login;
1
+ const { getType } = require("../src/utils/format");
2
+ const { setOptions } = require("./options");
3
+ const { loadConfig } = require("./config");
4
+ const { checkAndUpdateVersion } = require("../func/checkUpdate");
5
+ const loginHelper = require("./loginHelper");
6
+ const logger = require("../func/logger");
7
+
8
+ const { config } = loadConfig();
9
+ global.fca = { config };
10
+
11
+ // Global error handlers to prevent bot crashes
12
+ // Handle unhandled promise rejections (e.g., fetch timeouts, network errors)
13
+ if (!global.fca._errorHandlersInstalled) {
14
+ global.fca._errorHandlersInstalled = true;
15
+
16
+ process.on("unhandledRejection", (reason, promise) => {
17
+ try {
18
+ // Check if it's a fetch/network timeout error
19
+ if (reason && typeof reason === "object") {
20
+ const errorCode = reason.code || reason.cause?.code;
21
+ const errorMessage = reason.message || String(reason);
22
+
23
+ // Suppress Sequelize instance errors (handled gracefully in getBackupModel)
24
+ if (errorMessage.includes("No Sequelize instance passed")) {
25
+ return; // Silently ignore - already handled
26
+ }
27
+
28
+ // Handle fetch timeout errors gracefully
29
+ if (errorCode === "UND_ERR_CONNECT_TIMEOUT" ||
30
+ errorCode === "ETIMEDOUT" ||
31
+ errorMessage.includes("Connect Timeout") ||
32
+ errorMessage.includes("fetch failed")) {
33
+ logger(`Network timeout error caught (non-fatal): ${errorMessage}`, "warn");
34
+ return; // Don't crash, just log
35
+ }
36
+
37
+ // Handle other network errors
38
+ if (errorCode === "ECONNREFUSED" ||
39
+ errorCode === "ENOTFOUND" ||
40
+ errorCode === "ECONNRESET" ||
41
+ errorMessage.includes("ECONNREFUSED") ||
42
+ errorMessage.includes("ENOTFOUND")) {
43
+ logger(`Network connection error caught (non-fatal): ${errorMessage}`, "warn");
44
+ return; // Don't crash, just log
45
+ }
46
+ }
47
+
48
+ // For other unhandled rejections, log but don't crash
49
+ logger(`Unhandled promise rejection (non-fatal): ${reason && reason.message ? reason.message : String(reason)}`, "error");
50
+ } catch (e) {
51
+ // Fallback if logger fails - silent
52
+ }
53
+ });
54
+
55
+ // Handle uncaught exceptions (last resort)
56
+ process.on("uncaughtException", (error) => {
57
+ try {
58
+ const errorMessage = error.message || String(error);
59
+ const errorCode = error.code;
60
+
61
+ // Suppress Sequelize instance errors (handled gracefully in getBackupModel)
62
+ if (errorMessage.includes("No Sequelize instance passed")) {
63
+ return; // Silently ignore - already handled
64
+ }
65
+
66
+ // Handle fetch/network errors
67
+ if (errorCode === "UND_ERR_CONNECT_TIMEOUT" ||
68
+ errorCode === "ETIMEDOUT" ||
69
+ errorMessage.includes("Connect Timeout") ||
70
+ errorMessage.includes("fetch failed")) {
71
+ logger(`Uncaught network timeout error (non-fatal): ${errorMessage}`, "warn");
72
+ return; // Don't crash
73
+ }
74
+
75
+ // For other uncaught exceptions, log but try to continue
76
+ logger(`Uncaught exception (attempting to continue): ${errorMessage}`, "error");
77
+ // Note: We don't exit here to allow bot to continue running
78
+ // In production, you might want to restart the process instead
79
+ } catch (e) {
80
+ // Fallback if logger fails - silent
81
+ }
82
+ });
83
+ }
84
+
85
+ function login(loginData, options, callback) {
86
+ if (getType(options) === "Function" || getType(options) === "AsyncFunction") {
87
+ callback = options;
88
+ options = {};
89
+ }
90
+ const globalOptions = {
91
+ selfListen: false,
92
+ selfListenEvent: false,
93
+ listenEvents: false,
94
+ listenTyping: false,
95
+ updatePresence: false,
96
+ forceLogin: false,
97
+ autoMarkRead: false,
98
+ autoReconnect: true,
99
+ online: true,
100
+ emitReady: false,
101
+ userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
102
+ };
103
+ setOptions(globalOptions, options);
104
+ let prCallback = null;
105
+ let rejectFunc = null;
106
+ let resolveFunc = null;
107
+ let returnPromise = null;
108
+ if (getType(callback) !== "Function" && getType(callback) !== "AsyncFunction") {
109
+ returnPromise = new Promise(function (resolve, reject) {
110
+ resolveFunc = resolve;
111
+ rejectFunc = reject;
112
+ });
113
+ prCallback = function (error, api) {
114
+ if (error) return rejectFunc(error);
115
+ return resolveFunc(api);
116
+ };
117
+ callback = prCallback;
118
+ }
119
+ const proceed = () => loginHelper(loginData.appState, loginData.Cookie, loginData.email, loginData.password, globalOptions, callback, prCallback);
120
+ if (config && config.autoUpdate) {
121
+ const p = checkAndUpdateVersion();
122
+ if (p && typeof p.then === "function") {
123
+ p.then(proceed).catch(err => callback(err));
124
+ } else {
125
+ proceed();
126
+ }
127
+ } else {
128
+ proceed();
129
+ }
130
+ return returnPromise;
131
+ }
132
+
133
+ module.exports = login;