Skip to content

bunchtogether/async-iterable-to-readable-stream

Repository files navigation

Async Iterable to Readable Stream

CircleCI npm version

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");
});

Install

yarn add async-iterable-to-readable-stream

API

Table of Contents

index

Convert an async iterable into a readable stream

Parameters

Returns any Readable<Buffer | string>

About

Convert an async iterable into a readable stream. No dependencies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published