Created
November 27, 2021 12:25
-
-
Save m-allanson/7a78c2edab7264ed2d6240c4a07df50f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require("https"); | |
const util = require("util"); | |
// Adapted from https://gist.github.com/krnlde/797e5e0a6f12cc9bd563123756fc101f | |
https.get[util.promisify.custom] = function getAsync(options) { | |
return new Promise((resolve, reject) => { | |
https | |
.get(options, (response) => { | |
response.end = new Promise((resolve) => response.on("end", resolve)); | |
resolve(response); | |
}) | |
.on("error", reject); | |
}); | |
}; | |
const get = util.promisify(https.get); | |
const sleep = (time) => { | |
return new Promise((resolve) => setTimeout(resolve, time)); | |
}; | |
const urls = [ | |
"https://www.example.com/", | |
"https://www.example.org/", | |
"https://www.example.net/", | |
"invalid4", | |
"https://www.example.com/", | |
"https://www.example.org/", | |
"https://www.example.net/", | |
"invalid8", | |
"https://www.example.com/", | |
"https://www.example.org/", | |
"https://www.example.net/", | |
"invalid12", | |
"https://www.example.com/", | |
"https://www.example.org/", | |
"https://www.example.net/", | |
"invalid16", | |
]; | |
const run = async (urls) => { | |
const results = await Promise.all( | |
urls.map(async (url) => { | |
try { | |
const response = await get(url); | |
return [response.statusCode, url]; | |
} catch (e) { | |
return [`ERR ${e.message}`, url]; | |
} | |
}) | |
); | |
results.map((res) => { | |
console.log(res[0], res[1]); | |
}); | |
}; | |
const runInBatches = async ({ urls, batchSize, delayMs }) => { | |
while (urls.length > 0) { | |
nextBatch = urls.splice(0, batchSize); | |
await run(nextBatch); | |
await sleep(delayMs); | |
} | |
}; | |
runInBatches({ urls, batchSize: 5, delayMs: 1000 }); | |
/** | |
Output looks like: | |
> node batchUrlChecks.js | |
200 https://www.example.com/ | |
200 https://www.example.org/ | |
200 https://www.example.net/ | |
ERR Invalid URL invalid4 | |
200 https://www.example.com/ | |
200 https://www.example.org/ | |
200 https://www.example.net/ | |
ERR Invalid URL invalid8 | |
200 https://www.example.com/ | |
200 https://www.example.org/ | |
200 https://www.example.net/ | |
ERR Invalid URL invalid12 | |
200 https://www.example.com/ | |
200 https://www.example.org/ | |
200 https://www.example.net/ | |
ERR Invalid URL invalid16 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment