Yet another timed stream package. Mostly an exercise.
This module offers features similar to node-throttle
and node-brake
: throttle a stream with time constraints (rate and period at which it emits data)
There is one more feature that this package provide: ability to pause the stream.
The rate can be adjusted on the fly.
Internally it uses backpressure to ensure the rate and period are constant. It also uses a passthrough stream in paused mode and reads to it at the given rate & period.
const fs = require('fs')
const TimedStream = require('../')
console.log("Reading file rate.js at constant rate of 10bps with a period of 1 seconds between each data burst")
const t = new TimedStream({
rate: 10 /* 1 byte per second */,
period: 1000 /* try to emit data every 1000 ms */
})
let c = 0 // counting data length
t.on('data', data => {
c += data.length
console.log(data.toString())
})
t.on('end', () => {
console.log("Total length: " + c)
})
fs.createReadStream(__filename).pipe(t)
For more insight please take a look at tests
Install via npm:
npm install --save timed-stream
A TimedStream allow data to passthrough at a given rate and pause/resume at any time
Kind: global class
- TimedStream
- new TimedStream([options])
- .rate :
number
- .period :
number
- .streamPaused :
boolean
- .totalTime :
number
- .pauseStream()
- .resumeStream()
- .destroy()
Creates an instance of TimedStream.
Param | Type | Default | Description |
---|---|---|---|
[options] | object |
Options forwarder to Stream.Transform ctor | |
[options.rate] | number |
0 |
Bytes per seconds (0: unlimited) |
[options.period] | number |
100 |
Time between data event in ms |
The rate in bytes per second
Kind: instance property of TimedStream
The period between data events in ms
Kind: instance property of TimedStream
True if the stream is paused
Kind: instance property of TimedStream
The total time that the data flowed
Kind: instance property of TimedStream
Read only: true
Pause the stream
Kind: instance method of TimedStream
Resume the stream
Kind: instance method of TimedStream
Destroy the stream. A destroyed stream will not emit 'end' or any other event
Kind: instance method of TimedStream