# speed-limiter

> Throttle the speed of streams

Latest version **1.0.2** (published 2021-07-24) · MIT license · 16.0K weekly downloads

## Install

```sh
npm install speed-limiter
pnpm add speed-limiter
yarn add speed-limiter
bun add speed-limiter
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-07-24 |
| First published | 2021-05-13 |
| Weekly downloads | 16.0K |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alex |
| Maintainers | alxhotel |
| Keywords | throttle, stream, speed, limiter, bandwidth, limit, rate, chunk |

## Links

- npm: https://www.npmjs.com/package/speed-limiter
- Repository: https://github.com/alxhotel/speed-limiter
- Homepage: https://github.com/alxhotel/speed-limiter#readme
- Issues: https://github.com/alxhotel/speed-limiter/issues
- npm.io page: https://npm.io/package/speed-limiter

## Dependencies (2)

- [limiter](https://npm.io/package/limiter.md) ^1.1.5
- [streamx](https://npm.io/package/streamx.md) ^2.10.3

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads
- [medsci-skills](https://npm.io/package/medsci-skills.md) — 947 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-07-24
- 1.0.1 — 2021-07-24
- 1.0.0 — 2021-07-18
- 0.2.4 — 2021-06-02
- 0.2.3 — 2021-06-02
- 0.2.2 — 2021-05-30
- 0.2.1 — 2021-05-29
- 0.2.0 — 2021-05-15
- 0.1.5 — 2021-05-14
- 0.1.4 — 2021-05-14
- 0.1.3 — 2021-05-14
- 0.1.2 — 2021-05-13
- 0.1.0 — 2021-05-13

## README

# speed-limiter

[![NPM Version](https://img.shields.io/npm/v/speed-limiter.svg)](https://www.npmjs.com/package/speed-limiter)
[![Build Status](https://img.shields.io/github/workflow/status/alxhotel/speed-limiter/ci/main)](https://github.com/alxhotel/speed-limiter/actions)

Throttle the speed of streams in NodeJS

## Installation

```sh
npm install speed-limiter
```

## Usage

```js
const { ThrottleGroup } = require('speed-limiter')

const rate = 200 * 1000 // 200 KB/s
const throttleGroup = new ThrottleGroup({ rate })

// Create a new throttle
const throttle = throttleGroup.throttle()

// Use it throttle as any other Transform
let dataReceived = ''
const dataToSend = 'hello'
throttle.on('data', (data) => {
  dataReceived += data.toString()
})
throttle.on('end', () => {
  console.log('Ended')
})
throttle.write(dataToSend)
throttle.end()
```

## API

#### `const throttleGroup = new ThrottleGroup(opts)`

Initialize the throttle group.

The param `opts` can have these parameters:

```js
{
  enabled: Boolean,  // Enables/disables the throttling (defaul=true)
  rate: Number,      // Sets the max. rate (in bytes/sec)
  chunksize: Number, // Sets the chunk size used (deault=rate/10)
}
```

Note: the `rate` parameter is required

#### `throttleGroup.getEnabled()`

Returns a `boolean`.

If true, the throttling is enabled for the whole `throttleGroup`, otherwise not.

However, if a specific `throttle` in the group has the throttling disabled, then only
that throttle will block the data.

#### `throttleGroup.getRate()`

Returns a `number`.

Gets the bytes/sec rate at which the throttle group rate is set.

#### `throttleGroup.getChunksize()`

Returns a `number`.

Gets the chunk size used in the rate limiter.

#### `throttleGroup.setEnabled(enabled)` 

Used to disable or enabling the throttling of all the throttles of `throttleGroup`.

#### `throttleGroup.setRate(rate)`

Sets the maxium rate (in bytes/sec) at which the whole group of throttles can pass data.

#### `throttleGroup.setChunksize(chunksize)`

Sets the chunk size used in the rate limiter.

#### `const throttle = new Throttle(opts)`

Initialize the throttle instance.

The param `opts` can have these parameters:

```js
{
  enabled: Boolean,     // Enables/disables the throttling for that throttle (default=true)
  rate: Number,         // Sets the max. rate (in bytes/sec)
  chunksize: Number,    // Sets the chunk size used (default=rate/10)
  group: ThrottleGroup, // Sets the throttle group for that throttle (default=null)
}
```

If the `group` parameter is null, then a new `ThrottleGroup` will be created.

Note: the `rate` parameter is required

#### `throttle.getEnabled()`

Returns a `boolean`.

If true, the throttling is enabled for `throttle`, otherwise not.

#### `throttle.getGroup()`

Returns the `ThrottleGroup` of `throttle`.

#### `throttle.setEnabled(enabled)`

Used to disable or enabling the throttling of `throttle`.

## License

MIT. Copyright (c) [Alex](https://github.com/alxhotel)

---
_Source: https://npm.io/package/speed-limiter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
