it-to-stream
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
const source = (async function * () {
for (const value of [1, 2, 3, 4]) yield Buffer.from(value.toString())
})()
const readable = toStream.readable(source)
readable.on('data', console.log)
readable.pipe(writable)
pipeline(readable, writable, err => console.log(err || 'done'))
Convert sink iterable to writable stream
const sink = async source => {
for await (const chunk of source) {
console.log(chunk.toString())
}
}
const writable = toStream.writable(sink)
fs.createReadStream('/path/to/file').pipe(writable)
Convert transform iterable to transform stream
const transform = source => (async function * () {
for await (const chunk of source) {
yield Buffer.from(chunk.toString().replace(/ /g, '-'))
}
})()
const transform = toStream.transform(transform)
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.
Related
stream-to-it
Convert Node.js streams to streaming iterablesit-pipe
Utility to "pipe" async iterables together
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw