Package Exports
- stream-to-it
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stream-to-it) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
stream-to-it
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())
}
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(stream): Function
Convert a Readable
stream to a source iterable.
toIterable.sink(sink): 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.
Contribute
Feel free to dive in! Open an issue or submit PRs.
License
MIT © Alan Shaw