streamss-through
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

streamss-through

A sync/async stream2 transformer

NPM version Build Status

Works with node v8.x and greater.

Through can be used in synchronous mode with:

const { through } = require('streamss-through')

process.stdin
.pipe(through(
  function transform (data){
    // synchronous mode
  },
  function flush (){
    // synchronous mode
  }
))

or in asynchronous mode:

const { through } = require('streamss-through')

process.stdin
.pipe(through(
  function transform (data, enc, done){
    // asynchronous mode
    done() // explicit call of done required
  },
  function flush (done){
    // asynchronous mode
    done() // explicit call of done required
  }
))

Through([options], transform, flush)

Parameters:

  • {Object} [options] - Stream options
  • {Boolean} options.objectMode - Whether this stream should behave as a stream of objects. Default=false
  • {Number} options.highWaterMark - The maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. Default=16kb
  • {String} options.encoding - Set encoding for string-decoder
  • {Boolean} options.decodeStrings - Do not decode strings if set to false. Default=true
  • {Boolean} options.passError - Pass error to next pipe. Default=true
  • {Function} transform - Function called on transform
  • {Function} flush - Function called on flush

throughObj([options], transform, flush)

Shortcut for object mode

Parameters:

  • {Object} [options] - Stream options
  • {Function} transform - Function called on transform
  • {Function} flush - Function called on flush

Example:

const { through } = require('streamss-through')
let cnt = 0

require('fs').createReadStream(__filename, { encoding: 'utf8', highWaterMark: 30 })
.pipe(through(
  { decodeStrings: false },
  function transform(str) {
    cnt += 1
    this.push(str.replace(/\s/g, '‧') + '\n')
  },
  function flush() {
    console.log('\ncounted num of chunks: ' + cnt)
  }
))
.pipe(process.stdout)

Try it with:

node examples/test.js

Check out the tests for more examples.

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and licence.

License

Copyright (c) 2014- Commenthol. (MIT License)

See LICENSE for more info.

Package Sidebar

Install

npm i streamss-through

Weekly Downloads

14

Version

2.0.1

License

MIT

Unpacked Size

9.41 kB

Total Files

5

Last publish

Collaborators

  • commenthol