Utilities for handing node streams.
-
npm
npm install --save @guanghechen/stream
-
yarn
yarn add @guanghechen/stream
Name | Description |
---|---|
concatStreams |
Concatenate readable streams to async iterator. |
consumeStream |
Consume readable stream. |
consumeStreams |
Consume Consume multiple streams serially. |
mergeStreams |
Merge multiple readable streams into one readable streams. |
stream2buffer |
Consume read stream and encode the contents into buffer. |
-
Basic.
import { consumeStreams } from '@guanghechen/stream' import fs from 'node:fs' const filepaths = ['a.txt', 'b.txt', 'c.txt'] const readers: NodeJS.ReadableStream[] = plainFilepaths.map(fp => fs.createReadStream(fp)) const writer: NodeJS.WritableStream = fs.createWriteStream('out.txt') await consumeStreams(readers, writer)
-
Middlewares, i.e., cipher data before output.
import { consumeStreams } from '@guanghechen/stream' import crypto from 'crypto' import fs from 'node:fs' const filepaths = ['a.txt', 'b.txt', 'c.txt'] const readers: NodeJS.ReadableStream[] = plainFilepaths.map(fp => fs.createReadStream(fp)) const writer: NodeJS.WritableStream = fs.createWriteStream('out.txt') const iv: Buffer = crypto.randomBytes(32) const key: Buffer = crypto.randomBytes(32) const cipher: crypto.Cipher = crypto.createCipheriv('aes-256-gcm', key, iv) await consumeStreams(readers, writer, cipher)