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