🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

tokenbucket

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tokenbucket - npm Package Compare versions

Comparing version

to
0.3.2

26

lib/tokenbucket.js

@@ -246,12 +246,11 @@ 'use strict';

* @desc Remove the requested number of tokens. If the bucket (and any parent buckets) contains enough tokens this will happen immediately. Otherwise, it will wait to get enough tokens.
*
* Operational errors will be returned with the following `name` property, so they can be handled accordingly:
* @param {Number} tokensToRemove - The number of tokens to remove.
* @returns {external:Promise}
* @fulfil {Number} - The remaining tokens number, taking into account the parent if it has it.
* @reject {Error} - Operational errors will be returned with the following `name` property, so they can be handled accordingly:
* * `'NotEnoughSize'` - The requested tokens are greater than the bucket size.
* * `'NoInfinityRemoval'` - It is not possible to remove infinite tokens, because even if the bucket has infinite size, the `tokensLeft` would be indeterminant.
* * `'ExceedsMaxWait'` - The time we need to wait to be able to remove the tokens requested exceed the time set in `maxWait` configuration (parent or child).
* @param {Number} tokensToRemove - The number of tokens to remove.
* @returns {external:Promise}
* @fulfil {Number} - The remaining tokens number, taking into account the parent if it has it.
* @reject {Error} - See description for the different operational errors, and the example with how to handle them.
*
* .
* @example

@@ -410,7 +409,6 @@ * We have some code that uses 3 API requests, so we would need to remove 3 tokens from our rate limiter bucket.

*
* If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
* If there is an error with Redis it will be rejected with the error returned by Redis.
* @returns {external:Promise}
* @fulfil {true}
* @reject {Error} - See description for the operational error, and the example with how to handle it.
* @reject {Error} - If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
* If there is an error with Redis it will be rejected with the error returned by Redis.
* @example

@@ -474,10 +472,8 @@ * We have a worker process that uses 1 API requests, so we would need to remove 1 token (default) from our rate limiter bucket.

* @desc Loads the bucket lastFill and tokensLeft as it was saved in Redis. If it has any parents with `redis` options, they will get loaded too.
*
* If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
* If there is an error with Redis it will be rejected with the error returned by Redis.
* @returns {external:Promise}
* @fulfil {true}
* @reject {Error} - See description for the operational error, and the example with how to handle it.
* @example
* See {@link module:tokenbucket#save} ``` ```
* @reject {Error} - If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
* If there is an error with Redis it will be rejected with the error returned by Redis.
* @example @lang off
* See {@link module:tokenbucket#save}
*/

@@ -484,0 +480,0 @@

{
"name": "tokenbucket",
"version": "0.3.1",
"version": "0.3.2",
"description": "A flexible rate limiter using different variations of the Token Bucket algorithm, with hierarchy support, and optional persistence in Redis. Useful for limiting API requests, or other tasks that need to be throttled.",

@@ -45,3 +45,3 @@ "keywords": [

"gulp-istanbul": "0.8.1",
"gulp-jsdoc-to-markdown": "1.0.2",
"gulp-jsdoc-to-markdown": "1.1.1",
"gulp-mocha": "2.0.1",

@@ -48,0 +48,0 @@ "gulp-plumber": "1.0.0",

@@ -42,22 +42,22 @@ [![Dependency status](https://img.shields.io/david/jesucarr/tokenbucket.svg?style=flat)](https://david-dm.org/jesucarr/tokenbucket)

#### new TokenBucket([options])
**Params**
- [options] <code>Object</code> - The options object
- [.size] <code>Number</code> <code> = 1</code> - Maximum number of tokens to hold in the bucket. Also known as the burst size.
- [.tokensToAddPerInterval] <code>Number</code> <code> = 1</code> - Number of tokens to add to the bucket in one interval.
- [.interval] <code>Number</code> | <code>String</code> <code> = 1000</code> - The time passing between adding tokens, in milliseconds or as one of the following strings: 'second', 'minute', 'hour', day'.
- [.lastFill] <code>Number</code> - The timestamp of the last time when tokens where added to the bucket (last interval).
- [.tokensLeft] <code>Number</code> <code> = size</code> - By default it will initialize full of tokens, but you can set here the number of tokens you want to initialize it with.
- [.spread] <code>Boolean</code> <code> = false</code> - By default it will wait the interval, and then add all the tokensToAddPerInterval at once. If you set this to true, it will insert fractions of tokens at any given time, spreading the token addition along the interval.
- [.maxWait] <code>Number</code> | <code>String</code> - The maximum time that we would wait for enough tokens to be added, in milliseconds or as one of the following strings: 'second', 'minute', 'hour', day'. If any of the parents in the hierarchy has `maxWait`, we will use the smallest value.
- [.parentBucket] <code>TokenBucket</code> - A token bucket that will act as the parent of this bucket. Tokens removed in the children, will also be removed in the parent, and if the parent reach its limit, the children will get limited too.
- [.redis] <code>Object</code> - Options object for Redis
- .bucketName <code>String</code> - The name of the bucket to reference it in Redis. This is the only required field to set Redis persistance. The `bucketName` for each bucket **must be unique**.
- [.redisClient] <code>[redisClient](https://github.com/mranney/node_redis#rediscreateclient)</code> - The [Redis client](https://github.com/mranney/node_redis#rediscreateclient) to save the bucket.
- [.redisClientConfig] <code>Object</code> - [Redis client configuration](https://github.com/mranney/node_redis#rediscreateclient) to create the Redis client and save the bucket. If the `redisClient` option is set, this option will be ignored.
- [.port] <code>Number</code> <code> = 6379</code> - The connection port for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient).
- [.host] <code>String</code> <code> = &#x27;127.0.0.1&#x27;</code> - The connection host for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient)
- [.unixSocket] <code>String</code> - The connection unix socket for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient)
- [.options] <code>String</code> - The options for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [options] | <code>Object</code> | | The options object |
| [options.size] | <code>Number</code> | <code>1</code> | Maximum number of tokens to hold in the bucket. Also known as the burst size. |
| [options.tokensToAddPerInterval] | <code>Number</code> | <code>1</code> | Number of tokens to add to the bucket in one interval. |
| [options.interval] | <code>Number</code> &#124; <code>String</code> | <code>1000</code> | The time passing between adding tokens, in milliseconds or as one of the following strings: 'second', 'minute', 'hour', day'. |
| [options.lastFill] | <code>Number</code> | | The timestamp of the last time when tokens where added to the bucket (last interval). |
| [options.tokensLeft] | <code>Number</code> | <code>size</code> | By default it will initialize full of tokens, but you can set here the number of tokens you want to initialize it with. |
| [options.spread] | <code>Boolean</code> | <code>false</code> | By default it will wait the interval, and then add all the tokensToAddPerInterval at once. If you set this to true, it will insert fractions of tokens at any given time, spreading the token addition along the interval. |
| [options.maxWait] | <code>Number</code> &#124; <code>String</code> | | The maximum time that we would wait for enough tokens to be added, in milliseconds or as one of the following strings: 'second', 'minute', 'hour', day'. If any of the parents in the hierarchy has `maxWait`, we will use the smallest value. |
| [options.parentBucket] | <code>TokenBucket</code> | | A token bucket that will act as the parent of this bucket. Tokens removed in the children, will also be removed in the parent, and if the parent reach its limit, the children will get limited too. |
| [options.redis] | <code>Object</code> | | Options object for Redis |
| options.redis.bucketName | <code>String</code> | | The name of the bucket to reference it in Redis. This is the only required field to set Redis persistance. The `bucketName` for each bucket **must be unique**. |
| [options.redis.redisClient] | <code>[redisClient](https://github.com/mranney/node_redis#rediscreateclient)</code> | | The [Redis client](https://github.com/mranney/node_redis#rediscreateclient) to save the bucket. |
| [options.redis.redisClientConfig] | <code>Object</code> | | [Redis client configuration](https://github.com/mranney/node_redis#rediscreateclient) to create the Redis client and save the bucket. If the `redisClient` option is set, this option will be ignored. |
| [options.redis.redisClientConfig.port] | <code>Number</code> | <code>6379</code> | The connection port for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient). |
| [options.redis.redisClientConfig.host] | <code>String</code> | <code>&#x27;127.0.0.1&#x27;</code> | The connection host for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient) |
| [options.redis.redisClientConfig.unixSocket] | <code>String</code> | | The connection unix socket for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient) |
| [options.redis.redisClientConfig.options] | <code>String</code> | | The options for the Redis client. See [configuration instructions](https://github.com/mranney/node_redis#rediscreateclient) This options will be properties of the class instances. The properties `tokensLeft` and `lastFill` will get updated when we add/remove tokens. |
This options will be properties of the class instances. The properties `tokensLeft` and `lastFill` will get updated when we add/remove tokens.

@@ -142,3 +142,5 @@ **Example**

Operational errors will be returned with the following `name` property, so they can be handled accordingly:
**Kind**: instance method of <code>[TokenBucket](#exp_module_tokenbucket--TokenBucket)</code>
**Fulfil**: <code>Number</code> - The remaining tokens number, taking into account the parent if it has it.
**Reject**: <code>Error</code> - Operational errors will be returned with the following `name` property, so they can be handled accordingly:
* `'NotEnoughSize'` - The requested tokens are greater than the bucket size.

@@ -148,10 +150,6 @@ * `'NoInfinityRemoval'` - It is not possible to remove infinite tokens, because even if the bucket has infinite size, the `tokensLeft` would be indeterminant.

**Kind**: instance method of <code>[TokenBucket](#exp_module_tokenbucket--TokenBucket)</code>
**Fulfil**: <code>Number</code> - The remaining tokens number, taking into account the parent if it has it.
**Reject**: <code>Error</code> - See description for the different operational errors, and the example with how to handle them.
.
**Params**
- tokensToRemove <code>Number</code> - The number of tokens to remove.
| Param | Type | Description |
| --- | --- | --- |
| tokensToRemove | <code>Number</code> | The number of tokens to remove. |
**Example**

@@ -177,7 +175,5 @@ We have some code that uses 3 API requests, so we would need to remove 3 tokens from our rate limiter bucket.

**Returns**: <code>Boolean</code> - If it could remove the tokens inmediately it will return `true`, if not possible or needs to wait, it will return `false`.
**Params**
- tokensToRemove <code>Number</code> - The number of tokens to remove.
| Param | Type | Description |
| --- | --- | --- |
| tokensToRemove | <code>Number</code> | The number of tokens to remove. |
**Example**

@@ -195,8 +191,6 @@ ```javascript

If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
If there is an error with Redis it will be rejected with the error returned by Redis.
**Kind**: instance method of <code>[TokenBucket](#exp_module_tokenbucket--TokenBucket)</code>
**Fulfil**: <code>true</code>
**Reject**: <code>Error</code> - See description for the operational error, and the example with how to handle it.
**Reject**: <code>Error</code> - If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
If there is an error with Redis it will be rejected with the error returned by Redis.
**Example**

@@ -229,10 +223,8 @@ We have a worker process that uses 1 API requests, so we would need to remove 1 token (default) from our rate limiter bucket.

If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
If there is an error with Redis it will be rejected with the error returned by Redis.
**Kind**: instance method of <code>[TokenBucket](#exp_module_tokenbucket--TokenBucket)</code>
**Fulfil**: <code>true</code>
**Reject**: <code>Error</code> - See description for the operational error, and the example with how to handle it.
**Reject**: <code>Error</code> - If we call this function and we didn't set the redis options, the error will have `'NoRedisOptions'` as the `name` property, so it can be handled specifically.
If there is an error with Redis it will be rejected with the error returned by Redis.
**Example**
See [save](#module_tokenbucket--TokenBucket#save) ``` ```
See [save](#module_tokenbucket--TokenBucket#save)

@@ -239,0 +231,0 @@ ## Testing

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet