-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckLink.ts
67 lines (60 loc) · 1.62 KB
/
checkLink.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const axios = require("axios");
const ignoredCodes: Set<number> = new Set([
999,
429,
421,
405,
403,
401
]);
const ignoredURLs: Set<string> = new Set([
'localhost',
'127.0.0.1',
'example.com',
'www.example.com',
'example.org',
'www.example.org',
'goo.gl',
'fonts.googleapis.com',
'fonts.gstatic.com'
]);
const params: object = {
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "cross-site",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0",
"X-Amzn-Trace-Id": "Root=1-659f58c5-4de24ef7384486270161f185"
},
};
// Return true if link is broken
export async function checkLink(link: string): Promise<boolean> {
const url = new URL(link);
if (ignoredURLs.has(url.host))
return false;
if (url.host.indexOf("api.") !== -1)
return false
try {
await axios.head(link, params);
return false;
} catch (err: any) {
// If false positive, return false
if (err.response?.status && ignoredCodes.has(err.response.status))
return false;
// Head request is not allowed, make get request
try {
await axios.get(link, params);
return false;
} catch (err: any) {
if (err.response?.status && ignoredCodes.has(err.response.status))
return false;
}
// All failed, return true
return true;
}
}