async-ratelimiter
Advanced tools
Comparing version 1.1.4 to 1.2.0
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="1.2.0"></a> | ||
# [1.2.0](https://github.com/microlinkhq/async-ratelimiter/compare/v1.1.4...v1.2.0) (2019-03-27) | ||
<a name="1.1.4"></a> | ||
@@ -7,0 +12,0 @@ ## [1.1.4](https://github.com/microlinkhq/async-ratelimiter/compare/v1.1.3...v1.1.4) (2019-03-27) |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://documentup.com/microlinkhq/async-ratelimiter", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"main": "src/index.js", | ||
@@ -14,7 +14,22 @@ "author": { | ||
"contributors": [ | ||
"Kiko Beats <josefrancisco.verdu@gmail.com>", | ||
"Marcus Poehls <marcus.poehls@gmail.com>", | ||
"Ayan Yenbekbay <ayan.yenb@gmail.com>", | ||
"Nico Kaiser <nico@kaiser.me>", | ||
"amanda <amandalucis@gmail.com>" | ||
{ | ||
"name": "Kiko Beats", | ||
"email": "josefrancisco.verdu@gmail.com" | ||
}, | ||
{ | ||
"name": "Marcus Poehls", | ||
"email": "marcus.poehls@gmail.com" | ||
}, | ||
{ | ||
"name": "Ayan Yenbekbay", | ||
"email": "ayan.yenb@gmail.com" | ||
}, | ||
{ | ||
"name": "Nico Kaiser", | ||
"email": "nico@kaiser.me" | ||
}, | ||
{ | ||
"name": "amanda", | ||
"email": "amandalucis@gmail.com" | ||
} | ||
], | ||
@@ -21,0 +36,0 @@ "repository": { |
@@ -28,2 +28,3 @@ # async-ratelimiter | ||
const RateLimiter = require('async-ratelimiter') | ||
const { getClientIp } = require('request-ip') | ||
const Redis = require('ioredis') | ||
@@ -35,3 +36,4 @@ | ||
const apiQuota = async (req, res, handler) => { | ||
const apiQuota = async (req, res, next) => { | ||
const clientIp = getClientIp(req) | ||
const limit = await rateLimiter.get({ id: req.clientIp }) | ||
@@ -51,3 +53,3 @@ | ||
}) | ||
: handler(req, res) | ||
: next(req, res) | ||
} | ||
@@ -60,2 +62,4 @@ ``` | ||
It creates an rate limiter instance. | ||
#### options | ||
@@ -110,2 +114,3 @@ | ||
*Required*</br> | ||
Type: `string` | ||
@@ -117,3 +122,4 @@ | ||
Type: `number` | ||
Type: `number`</br> | ||
Default: `this.max` | ||
@@ -124,3 +130,4 @@ The maximum number of requests within `duration`. If provided, it overrides the default `max` value. This is useful for custom limits that differ between IDs. | ||
Type: `number` | ||
Type: `number`</br> | ||
Default: `this.max` | ||
@@ -131,7 +138,31 @@ How long keep records of requests in milliseconds. If provided, it overrides the default `duration` value. | ||
Type: `boolean` | ||
Type: `boolean`</br> | ||
Default: `true` | ||
When set to `false`, the remaining number of calls is not decreased. This is useful for just reading the remaining calls without actually decreasing them. | ||
When set to `false`, the remaining number of calls is not decreased. | ||
In some scenarios it might be useful to be able to read the current "remaining" value for a limiter. | ||
```js | ||
const loginHandler = async (req, res, next) => { | ||
const clientIp = getClientIp(req) | ||
const limit = await rateLimiter.get({ id: clientIp, decrease: false }) | ||
if (!limit.remaining) return sendError(req, res, 429) | ||
try { | ||
await doLogin(req) | ||
} catch (err) { | ||
if (err) { | ||
await rateLimiter.get({ id: req.clientIp }) | ||
return sendError(req, res, 401) | ||
} | ||
} | ||
next(req, res) | ||
} | ||
``` | ||
In this example, new login attempts are rejected when more at least 10 unsuccessful login attempts happened in the last 60 seconds. | ||
## License | ||
@@ -138,0 +169,0 @@ |
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
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
13809
166