-
Notifications
You must be signed in to change notification settings - Fork 59
/
index.js
44 lines (38 loc) · 843 Bytes
/
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const lineByLine = require("line-by-line");
const axios = require("axios");
const fs = require("fs");
fs.truncateSync("broken.txt", 0);
const noOp = () => {};
const log = (data) => {
fs.appendFile("broken.txt", data + "\r\n", (err) => {
if (err) {
console.log(err);
}
});
}
const checkResponseCode = (url) => {
(async () => {
try {
await axios.get(encodeURI(url));
} catch (err) {
if (err.response.status > 400 && err.response.status !== 403) {
log(err.response.status + " -> " + url);
} else {
log(url);
}
}
})().catch(() => {
log(url);
});
};
const forEachUrl = (callback) => {
const lr = new lineByLine("readme.md");
lr.on("error", noOp);
lr.on("line", (line) => {
const url = line.match(/http\S+/gi);
if (url && url[0]) {
callback(url[0]);
}
});
};
forEachUrl(checkResponseCode);