Miss any of our Open RFC calls?Watch the recordings here! »

streamss-through

2.0.0 • 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.

Install

npm i streamss-through

DownloadsWeekly Downloads

5

Version

2.0.0

License

MIT

Unpacked Size

9.6 kB

Total Files

5

Last publish

Collaborators

  • avatar