Replaced ky with axios
							parent
							
								
									18cef3fed5
								
							
						
					
					
						commit
						19d8c2cdd9
					
				| 
						 | 
				
			
			@ -5,9 +5,6 @@ const {readFileSync, writeFileSync, existsSync} = require("fs");
 | 
			
		|||
 | 
			
		||||
// Public modules from npm
 | 
			
		||||
const axios = require("axios").default;
 | 
			
		||||
const ky = require("ky-universal").create({
 | 
			
		||||
    throwHttpErrors: false,
 | 
			
		||||
});
 | 
			
		||||
const cheerio = require("cheerio");
 | 
			
		||||
const axiosCookieJarSupport = require("axios-cookiejar-support").default;
 | 
			
		||||
const tough = require("tough-cookie");
 | 
			
		||||
| 
						 | 
				
			
			@ -249,23 +246,23 @@ module.exports.isStringAValidURL = function (url) {
 | 
			
		|||
 * @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
 | 
			
		||||
 * @param {Boolean} [checkRedirect] 
 | 
			
		||||
 * If true, the function will consider redirects a violation and return false.
 | 
			
		||||
 * Default: false
 | 
			
		||||
 * @returns {Promise<Boolean>} true if the URL exists, false otherwise
 | 
			
		||||
 */
 | 
			
		||||
module.exports.urlExists = async function (url, checkRedirect) {
 | 
			
		||||
    if (!exports.isStringAValidURL(url)) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
module.exports.urlExists = async function (url, checkRedirect = false) {
 | 
			
		||||
    // Local variables
 | 
			
		||||
    let valid = false;
 | 
			
		||||
 | 
			
		||||
    const response = await ky.head(url);
 | 
			
		||||
    let valid = response !== undefined && !/4\d\d/.test(response.status);
 | 
			
		||||
    if (exports.isStringAValidURL(url)) {
 | 
			
		||||
        const response = await axios.head(url);
 | 
			
		||||
        valid = response && !/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;
 | 
			
		||||
        if (valid && checkRedirect) {
 | 
			
		||||
            const redirectUrl = await exports.getUrlRedirect(url);
 | 
			
		||||
            valid = redirectUrl === url;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return valid;
 | 
			
		||||
| 
						 | 
				
			
			@ -278,7 +275,7 @@ module.exports.urlExists = async function (url, checkRedirect) {
 | 
			
		|||
 * @returns {Promise<String>} Redirect URL or the passed URL
 | 
			
		||||
 */
 | 
			
		||||
module.exports.getUrlRedirect = async function (url) {
 | 
			
		||||
    const response = await ky.head(url);
 | 
			
		||||
    return response.url;
 | 
			
		||||
    const response = await axios.head(url);
 | 
			
		||||
    return response.config.url;
 | 
			
		||||
};
 | 
			
		||||
//#endregion Utility methods
 | 
			
		||||
		Loading…
	
		Reference in New Issue