Socket
Socket
Sign inDemoInstall

it-to-stream

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

it-to-stream

Convert streaming iterables to Node.js streams


Version published
Weekly downloads
73K
increased by8.06%
Maintainers
1
Weekly downloads
 
Created
Source

it-to-stream

Build Status dependencies Status JavaScript Style Guide

Convert streaming iterables to Node.js streams

Install

npm i it-to-stream

Usage

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

Convert source iterable to readable stream

// A streaming iterable "source" is just an (async) iterable
const source = (async function * () {
  for (const value of [1, 2, 3, 4]) yield Buffer.from(value.toString())
})()

const readable = toStream.readable(source)

// Now we have a readable stream, we can consume it by
readable.on('data', console.log)
// or
readable.pipe(writable)
// or
pipeline(readable, writable, err => console.log(err || 'done'))

Convert sink iterable to writable stream

// A streaming iterable "sink" is an (async) function that takes a "source"
// and consumes it.
const sink = async source => {
  for await (const chunk of source) {
    console.log(chunk.toString())
  }
}

const writable = toStream.writable(sink)

// Now we have a writable stream, we can pipe to it
fs.createReadStream('/path/to/file').pipe(writable)

Convert transform iterable to transform stream

// A streaming iterable "transform" is a function that takes a "source" and
// returns a "source".
const transform = source => (async function * () {
  for await (const chunk of source) {
    // Replace all space characters with dashes
    yield Buffer.from(chunk.toString().replace(/ /g, '-'))
  }
})()

const transform = toStream.transform(transform)

// Now we have a transform stream, we can pipe to and from it
fs.createReadStream('/path/to/file')
  .pipe(transform)
  .pipe(fs.createWriteStream('/path/to/file2'))

API

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

toStream.readable(source, [options]): Readable

Convert a source iterable to a Readable stream.

options are passed directly to the Readable constructor.

toStream.writable(sink, [options]): Writable

Convert a sink iterable to a Writable stream.

options are passed directly to the Writable constructor.

toStream.transform(transform, [options]): Transform

Convert a transform iterable to a Transform stream.

options are passed directly to the Transform constructor.

toStream.duplex(duplex, [options]): Duplex

Convert a duplex iterable to a Duplex stream.

options are passed directly to the Duplex constructor.

  • stream-to-it Convert Node.js streams to streaming iterables
  • 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 08 Apr 2021

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