npm.io
2.0.5 • Published 2 weeks ago

@guanghechen/stream

Licence
MIT
Version
2.0.5
Deps
1
Size
34 kB
Vulns
0
Weekly
0

@guanghechen/stream


Utilities for handling node streams.

Install

  • npm

    npm install --save @guanghechen/stream
  • yarn

    yarn add @guanghechen/stream

Usage

Name Description
concatStreams Concatenate readable streams to async iterator.
consumeStream Consume readable stream.
consumeStreams Consume multiple streams serially.
mergeStreams Merge multiple readable streams into one readable streams.
stream2buffer Consume read stream and encode the contents into buffer.
stream2bytes Consume read stream and encode the contents into bytes.

Example

  • Basic.

    import { consumeStreams } from '@guanghechen/stream'
    import fs from 'node:fs'
    
    const filepaths = ['a.txt', 'b.txt', 'c.txt']
    const readers: NodeJS.ReadableStream[] = filepaths.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[] = filepaths.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)

Reference

Keywords