Socket
Socket
Sign inDemoInstall

stream-to-it

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-to-it

Convert Node.js streams to streaming iterables


Version published
Maintainers
1
Created
Source

stream-to-it

Build Status dependencies Status JavaScript Style Guide

Convert Node.js streams to streaming iterables

Install

npm i stream-to-it

Usage

const toIterable = require('stream-to-it')

Convert readable stream to source iterable

const readable = fs.createReadStream('/path/to/file')
// Node.js streams are already async iterable so this is just s => s
const source = toIterable.source(readable)

for await (const chunk of source) {
  console.log(chunk.toString())
}

Also works with browser ReadableStream:

const res = fetch('http://example.org/file.jpg')

for await (const chunk of toIterable.source(res.body)) {
  console.log(chunk.toString())
}

Convert writable stream to sink iterable

const pipe = require('it-pipe')

const source = [Buffer.from('Hello '), Buffer.from('World!')]
const sink = toIterable.sink(fs.createWriteStream('/path/to/file'))

await pipe(source, sink)

Convert transform stream to transform iterable

const { Transform } = require('stream')

const output = await pipe(
  [true, false, true, true],
  toIterable.transform(new Transform({ // Inverter transform :)
    transform (chunk, enc, cb) {
      cb(null, !chunk)
    }
  })),
  // Collect and return the chunks
  source => {
    const chunks = []
    for await (chunk of source) chunks.push(chunk)
    return chunks
  }
)

console.log(output) // [ false, true, false, false ]

API

const toIterable = require('stream-to-it')

toIterable.source(readable): Function

Convert a Readable stream or a browser ReadableStream to a source iterable.

toIterable.sink(writable): Function

Convert a Writable stream to a sink iterable.

toIterable.transform(transform): Function

Convert a Transform stream to a transform iterable.

toIterable.duplex(duplex): { sink: Function, source: Function }

Convert a Duplex stream to a duplex iterable.

  • it-to-stream Convert streaming iterables to Node.js streams
  • it-pipe Utility to "pipe" async iterables together

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw

Keywords

FAQs

Package last updated on 15 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc