112 lines
3.0 KiB
JavaScript
112 lines
3.0 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
// Public modules from npm
|
||
|
const axios = require("axios").default;
|
||
|
const _ = require("lodash");
|
||
|
const ky = require("ky-universal").create({
|
||
|
throwHttpErrors: false,
|
||
|
});
|
||
|
|
||
|
// Modules from file
|
||
|
const shared = require("./shared.js");
|
||
|
const {
|
||
|
F95_BASE_URL
|
||
|
} = require("./constants/url.js");
|
||
|
|
||
|
/**
|
||
|
* @protected
|
||
|
* Gets the HTML code of a page.
|
||
|
* @param {String} url URL to fetch
|
||
|
* @returns {Promise<String>} HTML code or `null` if an error arise
|
||
|
*/
|
||
|
module.exports.fetchHTML = async function (url) {
|
||
|
const userAgent =
|
||
|
"Mozilla/5.0 (X11; Linux x86_64)" +
|
||
|
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36";
|
||
|
|
||
|
try {
|
||
|
const response = await axios.get(url, {
|
||
|
headers: {
|
||
|
"User-Agent": userAgent
|
||
|
}
|
||
|
});
|
||
|
return response.data;
|
||
|
} catch (e) {
|
||
|
shared.logger.error(`Error ${e.message} occurred while trying to fetch ${url}`);
|
||
|
return null;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @protected
|
||
|
* Enforces the scheme of the URL is https and returns the new URL.
|
||
|
* @param {String} url
|
||
|
* @returns {String}
|
||
|
*/
|
||
|
module.exports.enforceHttpsUrl = function (url) {
|
||
|
const value = _.isString(url) ? url.replace(/^(https?:)?\/\//, "https://") : null;
|
||
|
return value;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @protected
|
||
|
* Check if the url belongs to the domain of the F95 platform.
|
||
|
* @param {String} url URL to check
|
||
|
* @returns {Boolean} true if the url belongs to the domain, false otherwise
|
||
|
*/
|
||
|
module.exports.isF95URL = function (url) {
|
||
|
if (url.toString().startsWith(F95_BASE_URL)) return true;
|
||
|
else return false;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @protected
|
||
|
* Checks if the string passed by parameter has a properly formatted and valid path to a URL.
|
||
|
* @param {String} url String to check for correctness
|
||
|
* @returns {Boolean} true if the string is a valid URL, false otherwise
|
||
|
*/
|
||
|
module.exports.isStringAValidURL = function (url) {
|
||
|
try {
|
||
|
new URL(url); // skipcq: JS-0078
|
||
|
return true;
|
||
|
} catch (err) {
|
||
|
return false;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @protected
|
||
|
* Check if a particular URL is valid and reachable on the web.
|
||
|
* @param {String} url URL to check
|
||
|
* @param {Boolean} checkRedirect If true, the function will consider redirects a violation and return false
|
||
|
* @returns {Promise<Boolean>} true if the URL exists, false otherwise
|
||
|
*/
|
||
|
module.exports.urlExists = async function (url, checkRedirect) {
|
||
|
if (!exports.isStringAValidURL(url)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
const response = await ky.head(url);
|
||
|
let valid = response !== undefined && !/4\d\d/.test(response.status);
|
||
|
|
||
|
if (!valid) return false;
|
||
|
|
||
|
if (checkRedirect) {
|
||
|
const redirectUrl = await exports.getUrlRedirect(url);
|
||
|
if (redirectUrl === url) valid = true;
|
||
|
else valid = false;
|
||
|
}
|
||
|
|
||
|
return valid;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @protected
|
||
|
* Check if the URL has a redirect to another page.
|
||
|
* @param {String} url URL to check for redirect
|
||
|
* @returns {Promise<String>} Redirect URL or the passed URL
|
||
|
*/
|
||
|
module.exports.getUrlRedirect = async function (url) {
|
||
|
const response = await ky.head(url);
|
||
|
return response.url;
|
||
|
};
|