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

fastify-rate-limit

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

fastify-rate-limit

A low overhead rate limiter for your routes

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.7K
decreased by-28.3%
Maintainers
1
Weekly downloads
 
Created
Source

fastify-rate-limit

js-standard-style Build Status

A low overhead rate limiter for your routes.

Install

npm i fastify-rate-limit

Usage

Register the plugin pass to it some custom option.
This plugin will add an onRequest hook to check if the clients (based on their ip) has done too many request in the given timeWindow.

const fastify = require('fastify')()

fastify.register(require('fastify-rate-limit'), {
  max: 100,
  timeWindow: '1 minute'
})

fastify.get('/', (req, reply) => {
  reply.send({ hello: 'world' })
})

fastify.listen(3000, err => {
  if (err) throw err
  console.log('Server listening at http://localhost:3000')
})

In case a client reaches the maximum number of allowed requests, a standard Fastify error will be returned to the user with the status code setted to 429:

{
  statusCode: 429,
  error: 'Too Many Requests',
  message: 'Rate limit exceeded, retry in 1 minute'
}

Options

You can pass the following options during the plugin registration, the values will be used in all the routes.

fastify.register(require('fastify-circuit-breaker'), {
  max: 3, // default 1000
  timeWindow: 5000, // default 1000 * 60
  cache: 10000 // default 5000
})
  • max: is the maximum numbers of request a single client can perform inside a timeWindow.
  • timeWindow: the duration of the time window, can be expressed in milliseconds (as a number) or as a string, see ms too see the supported formats.
  • cache: this plugin internally uses a lru cache to handle the clients, you can change the size of the cache with this option.

License

MIT

Copyright © 2018 Tomas Della Vedova

Keywords

FAQs

Package last updated on 17 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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