-
-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathcheck-redirects.js
More file actions
73 lines (59 loc) · 1.86 KB
/
Copy pathcheck-redirects.js
File metadata and controls
73 lines (59 loc) · 1.86 KB
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
68
69
70
71
72
73
import fs from "fs";
import { URL } from "url";
// Load JSON safely (no import needed)
const blogs = JSON.parse(fs.readFileSync("./data.json", "utf8"));
// Configuration
const CONCURRENCY = 10;
const TIMEOUT_MS = 5000;
// Helper: timeout wrapper
function withTimeout(promise, ms) {
const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timeout exceeded")), ms)
);
return Promise.race([promise, timeout]);
}
// Check single blog
async function checkRedirect(blog) {
const { name, url } = blog;
try {
const response = await withTimeout(fetch(url, { redirect: "follow" }), TIMEOUT_MS);
const finalUrl = response.url;
const originalHost = new URL(url).hostname.replace(/^www\./, "");
const finalHost = new URL(finalUrl).hostname.replace(/^www\./, "");
if (originalHost !== finalHost) {
return { name, url, finalUrl, status: response.status, redirected: true };
}
} catch (err) {
return { name, url, error: err.message };
}
return null;
}
// Run concurrent checks
async function runConcurrent() {
const results = [];
const queue = [...blogs];
async function worker() {
while (queue.length > 0) {
const blog = queue.pop();
const result = await checkRedirect(blog);
if (result) results.push(result);
}
}
const workers = Array.from({ length: CONCURRENCY }, worker);
await Promise.all(workers);
return results;
}
// Main
async function main() {
console.log(`Checking ${blogs.length} blogs...\n`);
const results = await runConcurrent();
if (results.length === 0) {
console.log("✅ No redirects detected.");
} else {
console.log("⚠️ Redirected or failed URLs:");
console.table(results);
fs.writeFileSync("redirects.json", JSON.stringify(results, null, 2));
console.log("\nSaved report to redirects.json");
}
}
main();