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

leaky-bucket

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

leaky-bucket - npm Package Compare versions

Comparing version 3.0.4 to 4.0.0

src/EventEmitter.js

25

package.json
{
"name": "leaky-bucket",
"description": "A fast and efficient leaky bucket implementation",
"version": "3.0.4",
"homepage": "https://github.com/eventEmitter/leaky-bucket",
"author": "Lina van der Weg <lina@joinbox.com> (http://joinbox.com/)",
"version": "4.0.0",
"homepage": "https://github.com/linaGirl/leaky-bucket",
"author": "Lina van der Weg <lina@vanderweg.ch> (http://vanderweg.ch/)",
"license": "MIT",
"repository": {
"url": "https://github.com/eventEmitter/leaky-bucket.git",
"url": "https://github.com/linaGirl/leaky-bucket.git",
"type": "git"
},
"engines": {
"node": ">=v4.2"
"node": ">=v12"
},
"bugs": {
"url": "https://github.com/eventEmitter/leaky-bucket/issues"
"url": "https://github.com/linaGirl/leaky-bucket/issues"
},
"dependencies": {
"logd": "^2.3.0",
"logd-console-transport": "^1.1.0"
},
"dependencies": {},
"devDependencies": {
"section-tests": "^2.2.4"
"section-tests": "^3.2.0"
},
"optionalDependencies": {},
"keywords": [

@@ -32,5 +28,6 @@ "leaky-bucket",

"scripts": {
"test": "node --experimental-modules --no-warnings ./node_modules/.bin/section ./test/*.mjs --ld"
"test": "node --experimental-modules --no-warnings ./node_modules/.bin/section ./test/*.js --ld"
},
"main": "./index.mjs"
"main": "./src/LeakyBucket.js",
"type": "module"
}
# leaky-bucket
A fast and efficient leaky bucket
A fast and efficient leaky bucket for node.js and the Browser

@@ -13,4 +13,11 @@ Leaky buckets are often used to rate limits calls to APIs. They can be used on the server, to make sure

ATTENTION: Version 3+ is a rewrite of this library, it makes use of es modules and thus needs to be started using the --experimental-modules flag in node 10 and 12.
New in Version 4:
- dropped node.js support for node <12 (es modules)
- works now in modern browsers too (removed node.js dependencies)
- added a debug flag to the constructo
- added idleTimeout event and constructor flag
- added the initalCapacity option to the constructor
- added the canExecuteNow method
## installation

@@ -20,7 +27,3 @@

## build status
[![Build Status](https://travis-ci.org/eventEmitter/leaky-bucket.png?branch=master)](https://travis-ci.org/eventEmitter/leaky-bucket)
## API

@@ -36,2 +39,5 @@

- timeout: defines, how long it takes until items are rejected due to an overflow. defaults to the value of the interval, so that the overflow occurs at the same time the bucket is empty.
- debug: print log messages using console.log
- initialCapacity: the bucket starts normally with the full capacity, this lets you override this with a custom value
- idleTimeout: if set, the bucket will emit the idleTimeout event after the specified amount of milliseconds as soon the bucket is empty and at full capacity

@@ -50,3 +56,3 @@

### throttle
### throttle()

@@ -82,3 +88,3 @@ The throttle method is used to delay items until the bucket leaks them, thus rate limiting them. If the bucket is overflowing, which is when items cannot be executed within the timeout, the throttle method will reject using an error.

### pause
### pause()

@@ -91,2 +97,118 @@ The pause method can be use to pause the bucket for n seconds until it is allwed to resume.

````
### pay(cost)
removes the defined cost from the bucket
```javascript
bucket.pay(cost);
````
### end()
shuts down the bucket. Removes all pending items wihtout executing them. The bucket cannot be reused thereafter!
```javascript
bucket.end();
````
### event 'idleTimeout'
Is emitted, if the bucket is at full capacity and idle for N milliseconds
```javascript
const bucket = new Bucket({
capacity: 60,
interval: 60,
idleTimeout: 2000,
});
bucket.on('idleTimeout', (bucketInstance) => {
bucket.end();
});
// you may remove the listener if you want
bucket.off('idleTimeout');
````
## Browser
The bucket can used in the browser. Import `src/LeakyBucket.js` for tht usecase.
## Debugging
In order to debug the internals of the bucket you may enable debugging by passing the debug flag to the constructor.
```javascript
const bucket = new Bucket({
capacity: 60,
interval: 60,
debug: true,
});
````
## express.js
If you'd like to throttle incoming requests using the leaky bucket with express, you may register it as middleware. The example below shows a bucket per user, identified by a cookie identifying the user. The bucket gets deleted after a user has not sent requests for 2 minutes.
```javascript
import LeakyBucket from 'leaky-bucket';
import express from 'express';
const buckets = new Map();
app.use((req, res, next) => {
const userUid = req.cookies.userUid;
// set up a bucket for the user
if (!users.has(userUid)) {
const bucket = new Bucket({
capacity: 60,
interval: 60,
idleTimeout: 120 * 1000 // 120 seconds
});
// end the bucket, remove it from memory
bucket.on('idleTimeout', () => {
bucket.end();
buckets.delete(userUid);
});
// store for later use
buckets.set(userUid, bucket);
}
// get the users bucket
const usersBucket = buckets.get(userUid);
try {
// try to execute the request, if the bucket is empty, it will throw an error
usersBucket.throttle(costOfOperation);
} catch (e) {
// bucket is over capacity
res.status(420).send(`Enhance your calm!`);
return;
}
// all set and fine, continue to process the request
res.set('x-request-cost', costOfOperation);
res.set('x-rate-limit', `${bucket.currentCapacity}/${bucket.capacity}`);
next();
});
````

Sorry, the diff of this file is not supported yet

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