A sync/async stream2 transformer
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
}
))
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 tofalse
. Default=true{Boolean} options.passError
- Pass error to next pipe. Default=true{Function} transform
- Function called on transform{Function} flush
- Function called on flush
Shortcut for object mode
Parameters:
{Object} [options]
- Stream options{Function} transform
- Function called on transform{Function} flush
- Function called on flush
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.
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.
Copyright (c) 2014- Commenthol. (MIT License)
See LICENSE for more info.