-
Notifications
You must be signed in to change notification settings - Fork 1
/
preloader.js
56 lines (44 loc) · 1.59 KB
/
preloader.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
45
46
47
48
49
50
51
52
53
54
55
56
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
async function checkUrl(url) {
try {
// Use curl to check the HTTP status code. -I for header-only, -s for silent, -o /dev/null to discard output.
const { stdout, stderr } = await execAsync(`curl --max-time 25 -I -s -o /dev/null -w "%{http_code}" ${url}`);
const statusCode = parseInt(stdout.trim(), 10);
if (statusCode >= 200 && statusCode < 400) {
return { url, valid: true };
} else {
return { url, valid: false, statusCode }; // Include status code for invalid URLs
}
} catch (error) {
// Handle errors like network issues or invalid URLs that curl can't process
console.error(`Error checking ${url}:`, error.message);
return { url, valid: false, error: error.message }; // Include the error message
}
}
async function processUrls(urls) {
const results = await Promise.all(urls.map(checkUrl));
const validUrls = results.filter(result => result.valid).map(result => result.url);
const invalidUrls = results.filter(result => !result.valid);
return { validUrls, invalidUrls };
}
async function main() {
const urls = [
];
const { validUrls, invalidUrls } = await processUrls(urls);
console.log('Valid URLs:');
validUrls.forEach(url => console.log(url));
/* console.log('\nInvalid URLs:');
invalidUrls.forEach(url => {
console.log(url);
if(url.statusCode){
console.log(`Status Code: ${url.statusCode}`);
}
if (url.error) {
console.log(`Error: ${url.error}`);
}
console.log("---")
}); */
}
main();