controlstream

read Streams that have a read function, and pipe to multiple destinations

Usage no npm install needed!

<script type="module">
  import controlstream from 'https://cdn.skypack.dev/controlstream';
</script>

README

ReadStream

Install

npm install controlstream

create a ReadStream

var controlStream = require('readstream').controlStream
var cs = new controlStream()

fs.createReadStream('file').pipe(cs)

cs.read(50, function(err, data){
    //first 50 bytes are available in data
    
})

move to a certain location

cs.curser = 100
cs.read(50, function(err, data){
    //read bytes 100 to 150	

})	

pipe to a destination

var controlStream = require('controlstream').controlStream
var cs = new controlStream()

fs.createReadStream('file').pipe(cs).pipe(dest)

pipe to multiple destinations

var controlStream = require('controlstream').controlStream
var cs = new controlStream()

fs.createReadStream('file').pipe(cs)

cs.pipe(dest1)
cs.pipe(dest2)

Advanced

var controlStream = require('controlstream').controlStream
var cs = new controlStream({slice:true})

if you pass options {slice:true}, every time you read, that data is no longer in the buffer

cs.read(10, function(err, data){
    //read a header and decide what to do with it
    cs.pipe(dest)
    //pipe the remaining bytes to a destination
    cs.pipe(dest2)
    //pipe remaining bytes to another destination
})

only piping to one destination? or is this the last destination you are piping to?

cs.pipe(dest, {slice:true})
//when cs is done piping, there will be nothing left in the buffer

describe pace and cadence of the pipe (bytes per second)

var controlStream = require('controlstream').controlStream
var cs = new controlStream()

fs.createReadStream('file').pipe(cs)

cs.pipe(dest1, {pace:50,cadence:1000})
//pipe at 50 bytes per second