包详细信息

@guanghechen/helper-stream

guanghechen227MIT5.0.7

Utilities for handing node streams.

stream, concat streams, merge streams

自述文件

@guanghechen/helper-stream


Utilities for handing node streams.

Install

  • npm

    npm install --save @guanghechen/helper-stream
    
  • yarn

    yarn add @guanghechen/helper-stream
    

Usage

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.

Example

  • Basic.

    import { consumeStreams } from '@guanghechen/helper-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/helper-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)