Convert an async iterable into a readable stream. No dependencies.
If you encounter an issue, fork the repository, write tests demonstrating the issue, and create a pull request.
const asyncIterableToReadableStream = require('async-iterable-to-readable-stream');
const crypto = require('crypto');
const chunks = [
crypto.randomBytes(Math.round(Math.random() * 256)),
crypto.randomBytes(Math.round(Math.random() * 256)),
crypto.randomBytes(Math.round(Math.random() * 256)),
crypto.randomBytes(Math.round(Math.random() * 256)),
];
async function* getChunks() {
for (const item of chunks) {
await new Promise((resolve) => setImmediate(resolve));
yield item;
}
}
const iterable = getChunks();
const stream = asyncIterableToReadableStream(iterable);
stream.on('data', (chunk) => {
console.log(`Chunk with length ${chunk.length}`);
});
stream.on('error', (error) => {
console.error(error);
});
stream.on('end', () => {
console.log("End");
});
stream.on('close', () => {
console.log("Close");
});
yarn add async-iterable-to-readable-stream
Convert an async iterable into a readable stream
Returns any Readable<Buffer | string>