fastify-rate-limit
Advanced tools
Comparing version 5.1.0 to 5.2.0
14
index.js
@@ -25,3 +25,3 @@ 'use strict' | ||
function rateLimitPlugin (fastify, settings, next) { | ||
async function rateLimitPlugin (fastify, settings) { | ||
// create the object that will hold the "main" settings that can be shared during the build | ||
@@ -121,7 +121,5 @@ // 'global' will define, if the rate limit should be apply by default on all route. default : true | ||
} | ||
next() | ||
} | ||
function buildRouteRate (pluginComponent, params, routeOptions) { | ||
async function buildRouteRate (pluginComponent, params, routeOptions) { | ||
if (routeOptions[routeRateAdded]) { | ||
@@ -164,3 +162,3 @@ return | ||
function onIncr (err, { current, ttl }) { | ||
async function onIncr (err, { current, ttl }) { | ||
if (err && params.skipOnError === false) { | ||
@@ -170,3 +168,3 @@ return next(err) | ||
const maximum = getMax() | ||
const maximum = await getMax() | ||
const timeLeft = Math.floor(ttl / 1000) | ||
@@ -212,7 +210,7 @@ | ||
function getMax () { | ||
async function getMax () { | ||
if (typeof params.max === 'number') { | ||
return params.max | ||
} else { | ||
return params.max(req, key) | ||
return await params.max(req, key) | ||
} | ||
@@ -219,0 +217,0 @@ } |
{ | ||
"name": "fastify-rate-limit", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"description": "A low overhead rate limiter for your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -90,3 +90,3 @@ # fastify-rate-limit | ||
- `global` : indicates if the plugin should apply the rate limit setting to all routes within the encapsulation scope | ||
- `max`: is the maximum number of requests a single client can perform inside a timeWindow. It can be a sync function with the signature `(req, key) => {}` where `req` is the Fastify request object and `key` is the value generated by the `keyGenerator`. The function **must** return a number. | ||
- `max`: is the maximum number of requests a single client can perform inside a timeWindow. It can be an async function with the signature `async (req, key) => {}` where `req` is the Fastify request object and `key` is the value generated by the `keyGenerator`. The function **must** return a number. | ||
- `ban`: is the maximum number of 429 responses to return to a single client before returning 403. When the ban limit is exceeded the context field will have `ban=true` in the errorResponseBuilder. This parameter is an in-memory counter and could not work properly in a distributed environment. | ||
@@ -125,3 +125,3 @@ - `timeWindow:` the duration of the time window. It can be expressed in milliseconds or as a string (in the [`ms`](https://github.com/zeit/ms) format) | ||
keyGenerator (req) { return req.headers['service-key'] }, | ||
max: (req, key) => { return key === 'pro' ? 3 : 2 }, | ||
max: async (req, key) => { return key === 'pro' ? 3 : 2 }, | ||
timeWindow: 1000 | ||
@@ -128,0 +128,0 @@ }) |
@@ -799,1 +799,14 @@ 'use strict' | ||
}) | ||
test('Before async in "max"', async t => { | ||
const fastify = Fastify() | ||
await fastify.register(rateLimit, { | ||
keyGenerator (req) { return req.headers['api-key'] }, | ||
max: async (req, key) => { return await requestSequence(key) }, | ||
timeWindow: 10000 | ||
}) | ||
await fastify.get('/', (req, res) => { res.send('hello') }) | ||
const requestSequence = async (key) => await key === 'pro' ? 5 : 2 | ||
}) |
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
89578
2324