timed-stream
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

node-timed-stream

Greenkeeper badge Build Status codecov Dependency Status devDependencies contributions welcome Code Climate npm version

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.

Examples

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

Installing

Install via npm:

npm install --save timed-stream

JSDoc

TimedStream

A TimedStream allow data to passthrough at a given rate and pause/resume at any time

Kind: global class

new TimedStream([options])

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

timedStream.rate : number

The rate in bytes per second

Kind: instance property of TimedStream

timedStream.period : number

The period between data events in ms

Kind: instance property of TimedStream

timedStream.streamPaused : boolean

True if the stream is paused

Kind: instance property of TimedStream

timedStream.totalTime : number

The total time that the data flowed

Kind: instance property of TimedStream Read only: true

timedStream.pauseStream()

Pause the stream

Kind: instance method of TimedStream

timedStream.resumeStream()

Resume the stream

Kind: instance method of TimedStream

timedStream.destroy()

Destroy the stream. A destroyed stream will not emit 'end' or any other event

Kind: instance method of TimedStream

Package Sidebar

Install

npm i timed-stream

Weekly Downloads

0

Version

1.1.0

License

MIT

Last publish

Collaborators

  • tinyman