From d3a1a1e11ae8c6893c026bb6b6a3fe98c7149637 Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Thu, 17 Dec 2020 22:44:40 +0100 Subject: [PATCH] Resolved circular dependency --- app/index.js | 4 ++++ app/scripts/network-helper.js | 13 +++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/index.js b/app/index.js index 4242a56..3f4f469 100644 --- a/app/index.js +++ b/app/index.js @@ -7,6 +7,7 @@ const scraper = require("./scripts/scraper.js"); const searcher = require("./scripts/searcher.js"); const uScraper = require("./scripts/user-scraper.js"); const latestFetch = require("./scripts/latest-fetch.js"); +const fetchPlatformData = require("./scripts/platform-data.js").fetchPlatformData; // Classes from file const Credentials = require("./scripts/classes/credentials.js"); @@ -69,6 +70,9 @@ module.exports.login = async function (username, password) { const result = await networkHelper.authenticate(creds); shared.isLogged = result.success; + // Load platform data + if (result.success) await fetchPlatformData(); + /* istambul ignore next */ if (result.success) shared.logger.info("User logged in through the platform"); else shared.logger.warn(`Error during authentication: ${result.message}`); diff --git a/app/scripts/network-helper.js b/app/scripts/network-helper.js index 26a06fa..dc2b799 100644 --- a/app/scripts/network-helper.js +++ b/app/scripts/network-helper.js @@ -11,7 +11,6 @@ const shared = require("./shared.js"); const f95url = require("./constants/url.js"); const f95selector = require("./constants/css-selector.js"); const LoginResult = require("./classes/login-result.js"); -const fetchPlatformData = require("./platform-data.js").fetchPlatformData; // Global variables const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) " + @@ -96,12 +95,9 @@ module.exports.authenticate = async function (credentials, force) { const errorMessage = $("body").find(f95selector.LOGIN_MESSAGE_ERROR).text().replace(/\n/g, ""); // Return the result of the authentication - if (errorMessage === "") { - // Fetch data - await fetchPlatformData(); - return new LoginResult(true, "Authentication successful"); - } - else return new LoginResult(false, errorMessage); + const result = errorMessage.trim() === ""; + const message = errorMessage.trim() === "" ? "Authentication successful" : errorMessage; + return new LoginResult(result, message); } catch (e) { shared.logger.error(`Error ${e.message} occurred while authenticating to ${secureURL}`); return new LoginResult(false, `Error ${e.message} while authenticating`); @@ -230,10 +226,11 @@ async function _axiosUrlExists(url) { // Local variables let valid = false; try { - const response = await axios.head(url); + const response = await axios.head(url, {timeout: 3000}); valid = response && !/4\d\d/.test(response.status); } catch (error) { if (error.code === "ENOTFOUND") valid = false; + else if (error.code === "ETIMEDOUT") valid = false; else throw error; } return valid;