leaky-bucket
Advanced tools
Comparing version 3.0.4 to 4.0.0
{ | ||
"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" | ||
} |
138
README.md
# 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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35638
0
770
208
Yes
1
- Removedlogd@^2.3.0
- Removedlogd-console-transport@^1.1.0
- Removedansi-styles@3.2.1(transitive)
- Removedapp-root-path@2.2.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedee-argv@0.1.4(transitive)
- Removedee-class@1.4.0(transitive)
- Removedee-log@0.3.11(transitive)
- Removedee-types@2.2.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedlogd@2.3.3(transitive)
- Removedlogd-console-output@1.3.0(transitive)
- Removedlogd-console-transport@1.1.0(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removeduuid@3.4.0(transitive)
- Removedwrappy@1.0.2(transitive)