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

fastify-rate-limit

Package Overview
Dependencies
Maintainers
13
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-rate-limit - npm Package Compare versions

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
})
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