async-iterator-to-stream
Convert an async iterator/iterable to a readable stream
Last updated 5 years ago by julien-f .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install async-iterator-to-stream 
SYNC missed versions from official npm registry.

async-iterator-to-stream

Package Version Build Status PackagePhobia Latest Commit

Convert an async iterator/iterable to a readable stream

Even though this library is dedicated to async iterators, it is fully compatible with synchronous ones.

Furthermore, generators can be used to create readable stream factories!

Install

Installation of the npm package:

> npm install --save async-iterator-to-stream

Usage

const asyncIteratorToStream = require("async-iterator-to-stream");

// sync/async iterators
asyncIteratorToStream(new Set(["foo", "bar"]).values()).pipe(output);

// sync/async iterables
asyncIteratorToStream.obj([1, 2, 3]).pipe(output);

// if you pass a sync/async generator, it will return a factory instead of a
// stream
const createRangeStream = asyncIteratorToStream.obj(function*(n) {
  for (let i = 0; i < n; ++i) {
    yield i;
  }
});
createRangeStream(10).pipe(output);

Example

Let's implement a simpler fs.createReadStream to illustrate the usage of this library.

const asyncIteratorToStream = require("async-iterator-to-stream");

// promisified fs
const fs = require("mz/fs");

const createReadStream = asyncIteratorToStream(async function*(file) {
  const fd = await fs.open(file, "r");
  try {
    let size = yield;
    while (true) {
      const buf = Buffer.alloc(size);
      const [n] = await fs.read(fd, buf, 0, size, null);
      if (n < size) {
        yield buf.slice(0, n);
        return;
      }
      size = yield buf;
    }
  } finally {
    await fs.close(fd);
  }
});

createReadStream("foo.txt").pipe(process.stdout);

If your environment does not support async generators, you may use a sync generator instead and yield promises to wait for them.

Development

# Install dependencies
> npm

# Run the tests
> npm test

# Continuously compile
> npm run dev

# Continuously run the tests
> npm run dev-test

# Build for production
> npm run build

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet

Current Tags

  • 1.2.0                                ...           latest (5 years ago)

6 Versions

  • 1.2.0                                ...           5 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (18)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE