-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.js
30 lines (27 loc) · 895 Bytes
/
check.js
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
import { addError, getList, onExit } from "./core";
onExit();
// check list of redirects for broken links
async function checkList(list) {
return await Promise.all(
// for each redirect
list.map(async ({ to, file, index }) => {
try {
// do simple request to target url
const response = await fetch(to);
if (
// only fail on certain status codes that might indicate link is "broken"
// select as desired from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
[
400, 404, 405, 406, 408, 409, 410, 421, 500, 501, 502, 503, 504,
].includes(response.status)
)
throw Error(response.status);
} catch (error) {
addError(
`${file} entry ${index} "to: ${to}" may be a broken link\n (${error})`
);
}
})
);
}
await checkList(getList(true));