Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kldzj/stream-throttle

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kldzj/stream-throttle

Throttle one or multiple stream(s) to a maximum rate

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
536
increased by1.13%
Maintainers
1
Weekly downloads
 
Created
Source

stream-throttle

A simple Node.js stream throttler.

Installation

Using yarn:

$ yarn add @kldzj/stream-throttle

Using npm:

$ npm i -S @kldzj/stream-throttle

Usage

Options
  • rate: The rate in bytes per second at which to emit data.
  • burst: The burst rate in bytes per second. Must be less than or equal to rate. Defaults to rate.
  • chunkSize: The size of chunks to split the received data into. Must be less than or equal to rate. Defaults to rate / 10.
Throttle

Used to throttle the output rate of a single stream.

import { Throttle } from '@kldzj/stream-throttle';

const throttle = new Throttle({ rate: 10 });
process.stdin.pipe(throttle).pipe(process.stdout);
ThrottleGroup

Used to throttle the output rate across multiple streams.

import { createConnection } from 'net';
import { ThrottleGroup } from '@kldzj/stream-throttle';

const group = new ThrottleGroup({ rate: 1024 * 1024 });
const addr = { host: 'www.google.com', port: 80 };
const throttled1 = createConnection(addr).pipe(group.createThrottle());
const throttled2 = createConnection(addr).pipe(group.createThrottle());
// ...
Changing the rate on-the-fly
const group = new ThrottleGroup({ rate: 1024 * 1024 });
// or group = new Throttle({ rate: 1024 * 1024 }).group;
// ...

group.setRate(4 * 1024 * 1024);
// in case you calculate the chunkSize differently
// you should call `setChunkSize` as well, as `setRate`
// recalculates `chunkSize`
group.setChunkSize(group.rate / 6);
// in addition, you can also call `setBurst` if
// you want to change the burst rate
group.setBurst(group.rate * 1.5);

// ...

Special thanks

  • @tjgq for the initial implementation (stream-throttle).

Keywords

FAQs

Package last updated on 27 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc