Seamlessly use Node.js streams with it-pipe and friends.
it-pipe
import fs from 'node:fs'import * as toIterable from 'stream-to-it'const readable = fs.createReadStream('/path/to/file')// Node.js streams are already async iterable so this is just s => sconst source = toIterable.source<Buffer>(readable)for await (const chunk of source) { console.log(chunk.toString())} Copy
import fs from 'node:fs'import * as toIterable from 'stream-to-it'const readable = fs.createReadStream('/path/to/file')// Node.js streams are already async iterable so this is just s => sconst source = toIterable.source<Buffer>(readable)for await (const chunk of source) { console.log(chunk.toString())}
Also works with browser ReadableStream:
ReadableStream
import * as toIterable from 'stream-to-it'const res = await fetch('http://example.org/file.jpg')if (res.body == null) { throw new Error('Body was not set')}for await (const chunk of toIterable.source(res.body)) { console.log(chunk.toString())} Copy
import * as toIterable from 'stream-to-it'const res = await fetch('http://example.org/file.jpg')if (res.body == null) { throw new Error('Body was not set')}for await (const chunk of toIterable.source(res.body)) { console.log(chunk.toString())}
import fs from 'node:fs'import { pipe } from 'it-pipe'import * as toIterable from 'stream-to-it'const source = [Buffer.from('Hello '), Buffer.from('World!')]const sink = toIterable.sink(fs.createWriteStream('/path/to/file'))await pipe(source, sink) Copy
import fs from 'node:fs'import { pipe } from 'it-pipe'import * as toIterable from 'stream-to-it'const source = [Buffer.from('Hello '), Buffer.from('World!')]const sink = toIterable.sink(fs.createWriteStream('/path/to/file'))await pipe(source, sink)
import fs from 'node:fs'import { Transform } from 'node:stream'import { pipe } from 'it-pipe'import * as toIterable from 'stream-to-it'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 async source => { const chunks = [] for await (const chunk of source) chunks.push(chunk) return chunks })console.log(output) // [ false, true, false, false ] Copy
import fs from 'node:fs'import { Transform } from 'node:stream'import { pipe } from 'it-pipe'import * as toIterable from 'stream-to-it'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 async source => { const chunks = [] for await (const chunk of source) chunks.push(chunk) return chunks })console.log(output) // [ false, true, false, false ]
it-to-stream
Seamlessly use Node.js streams with
it-pipe
and friends.Example: Convert readable stream to source iterable
Also works with browser
ReadableStream
:Example: Convert writable stream to sink iterable
Example: Convert transform stream to transform iterable
Related
it-to-stream
Convert streaming iterables to Node.js streamsit-pipe
Utility to "pipe" async iterables together