Skip to content

Commit

Permalink
Merge pull request #79 from justindhillon/ignoredURLs
Browse files Browse the repository at this point in the history
https://example.com/anything doesn't retrun false positive anymore
  • Loading branch information
justindhillon authored Mar 5, 2024
2 parents 803884c + e4a0e44 commit c2bed41
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions checkLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ const axios = require("axios");

// Return true if link is broken
export async function checkLink(link: string): Promise<boolean> {
const ignoredCodes: Set<number> = new Set([999, 429, 403, 401]);
const ignoredURLs: Set<string> = new Set(['example.com']);

const url = new URL(link);
if (ignoredURLs.has(url.host))
return false;

const params: object = {
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
Expand All @@ -17,13 +24,11 @@ export async function checkLink(link: string): Promise<boolean> {
}
};

const falsePositives: Set<number> = new Set([999, 429, 403, 401]);

try {
await axios.head(link, params);
} catch (err: any) {
// If false positive, return false
if (falsePositives.has(err.response.status))
if (ignoredCodes.has(err.response.status))
return false;

// If HEAD is not allowed try GET
Expand All @@ -32,7 +37,7 @@ export async function checkLink(link: string): Promise<boolean> {
await axios.get(link, params);
} catch (error: any) {
// If false positive, return false
if (falsePositives.has(err.response.status))
if (ignoredCodes.has(err.response.status))
return false;

return true;
Expand Down

0 comments on commit c2bed41

Please sign in to comment.