Created
July 27, 2021 18:53
-
-
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
This file contains hidden or 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 { 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