Convert Node.js streams to streaming iterables
npm i stream-to-it
const toIterable = require('stream-to-it')
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())
}
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)
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 ]
const toIterable = require('stream-to-it')
Convert a Readable
stream or a browser ReadableStream
to a source iterable.
Convert a Writable
stream to a sink iterable.
Convert a Transform
stream to a transform iterable.
Convert a Duplex
stream to a duplex iterable.
it-to-stream
Convert streaming iterables to Node.js streamsit-pipe
Utility to "pipe" async iterables together
Feel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw