# stream-to-generator

> Convert Node.js readable streams to generator functions

Latest version **0.1.1** (published 2017-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install stream-to-generator
pnpm add stream-to-generator
yarn add stream-to-generator
bun add stream-to-generator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2017-01-10 |
| First published | 2017-01-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Ivan K |
| Maintainers | str_t |
| Keywords | stream, generator, es6 |

## Links

- npm: https://www.npmjs.com/package/stream-to-generator
- Repository: https://github.com/SEAPUNK/stream-to-generator
- Homepage: https://github.com/SEAPUNK/stream-to-generator#readme
- Issues: https://github.com/SEAPUNK/stream-to-generator/issues
- npm.io page: https://npm.io/package/stream-to-generator

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.1.1 (latest) — 2017-01-10
- 0.1.0 — 2017-01-10

## README

stream-to-generator
===

[![npm version](https://img.shields.io/npm/v/stream-to-generator.svg?style=flat-square)](https://npmjs.com/package/stream-to-generator)
[![javascript standard style](https://img.shields.io/badge/code%20style-standard-blue.svg?style=flat-square)](http://standardjs.com/)
[![travis build](https://img.shields.io/travis/SEAPUNK/stream-to-generator/master.svg?style=flat-square)](https://travis-ci.org/SEAPUNK/stream-to-generator)
[![coveralls coverage](https://img.shields.io/coveralls/SEAPUNK/stream-to-generator.svg?style=flat-square)](https://coveralls.io/github/SEAPUNK/stream-to-generator)
[![david dependencies](https://david-dm.org/SEAPUNK/stream-to-generator.svg?style=flat-square)](https://david-dm.org/SEAPUNK/stream-to-generator)
[![david dev dependencies](https://david-dm.org/SEAPUNK/stream-to-generator/dev-status.svg?style=flat-square)](https://david-dm.org/SEAPUNK/stream-to-generator)

Convert Node.js readable streams to generator functions

`npm install stream-to-generator`

- [usage](#usage)
- [api](#api)

---

usage
---

```js
import streamToGenerator from 'stream-to-generator'
import fs from 'fs'

async function main () {
  const fileLoc = path.join(__dirname, 'fixtures/LARGE_FILE')
  const readStream = fs.createReadStream(fileLoc)
  const data = await streamToGenerator(readStream, readHandler)
}

function * readHandler (read, finish) {
  let chunks = []

  let done, chunk
  while (true) {
    ;[done, chunk] = yield read()
    if (done) break
    chunks.push(chunk)
  }

  return yield finish(Buffer.concat(chunks))
}

```

api
---

`streamToGenerator(readStream, handlerFunction)`

Converts a readable stream to a generator function.

`readStream` is a readable stream.

`handlerFunction` is a generator function that takes two arguments:

- `read()` Creates a request for more data in the stream. This must be done with `yield`. If data could not be read, then the handler function is cancelled. This returns an array: `[end, data]`:
  - `end` is a Boolean, which indicates whether there will be more data or not. Yielding a `read()` after `end == true` will throw an error.
  - `data` is the next chunk of data gathered from the stream. If `end == true`, data will be undefined.
- `finish(retval)` Creates a request to end the handler. This resolves the created promise from calling `streamToGenerator()` with `retval`. This should be done with `yield` (the wrapper will be able to cancel the generator), but not required to.

Returns a `Promise`, which is resolved with `retval` when the readHandler calls `finish(retval)`. Otherwise, the `Promise` is rejected with an error if the stream or handler function encounters an error.

---
_Source: https://npm.io/package/stream-to-generator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
