1.0.4 • Published 4 years ago

dynamic-stream-throttle v1.0.4

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Dynamic Stream Throttle

Dynamically limit the speed of a Node stream\ Adapted from node-stream-throttle

Usage

Installation npm i dynamic-stream-throttle

getThrottledStream takes an object with rate parameters and returns a modified stream.Transform object
The returned Transform has an updateThrottleOptions function to change the rate of throttling.

Throttle rate parameters

  • rateBytes throttle the stream speed to a number of bytes per second
  • chunkSizeBytes control the chunk size that is processed by the stream at each read

Example

const Throttle = require("dynamic-stream-throttle");
const { Readable } = require("stream");

// create simple readable stream
const readableStream = new Readable({
  read() {
    this.push("hello!");
  },
});

var bytesPerSecond = 1;

//create a stream which throttles output to 1 byte per second
const throttledStream = Throttle.getThrottledStream({ rateBytes: bytesPerSecond });

//pipe readableStream to throttledStream, then stdout
readableStream.pipe(throttledStream).pipe(process.stdout);

//increase rate every 5 seconds
setInterval(() => {
  throttledStream.updateThrottleOptions({ rateBytes: (bytesPerSecond *= 10) });
}, 5 * 1000);
1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago