Skip to content

Instantly share code, notes, and snippets.

@arafathusayn
Created July 27, 2021 18:53
Show Gist options
  • Select an option

  • Save arafathusayn/f5a9ae0397d9c958bf848a9f0f3e35ae to your computer and use it in GitHub Desktop.

Select an option

Save arafathusayn/f5a9ae0397d9c958bf848a9f0f3e35ae to your computer and use it in GitHub Desktop.
Get a single readable stream of contents from multiple URLs on Node.js
const { Readable } = require("stream");
const { default: got } = require("got");
/**
* @param {string[]} [urls]
* @param {import("got").Options} [options]
* @return {Promise<Readable>} `Promise<Readable>`
*/
const getStreamFromUrls = async (urls = [], options = {}) => {
async function* generateChunk() {
const fetchStreams = urls.map((url) => got.stream(url, options));
for (const stream of fetchStreams) {
for await (chunk of stream) {
yield chunk;
}
}
}
const readable = Readable.from(generateChunk());
return readable;
};
module.exports = getStreamFromUrls;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment