1.0.5 • Published 3 months ago

@specter-labs/fastify-rate-limit v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

Fastify Rate Limit

A simple HTTP rate limiting plugin for Fastify.

Installation

npm install @specter-labs/fastify-rate-limit

Usage

import fastify from 'fastify'
import fastifyRateLimit from '@specter-labs/fastify-rate-limit'

// Implement your own storage adapter
const redisStorageAdapter = {
  async push({ path, data, expiresIn }: { path: string, data: unknown, expiresIn?: number })
  async remove({ pattern }) { /* implementation */ }
}

const app = fastify()

// Register the plugin
app.register(fastifyRateLimit, {
  storageAdapter: redisStorageAdapter,
  ttl: 300, // 5 minutes default TTL
  prefix: 'my-app:rate-limit' // custom prefix
})

Storage

In memory

This plugin provides a in-memory storage adapter instance that can be used out-of-the-box.

import fastify from 'fastify'
import fastifyRateLimitm { defaultStorageAdapter } from '@specter-labs/fastify-rate-limit'

const app = fastify();

app.register(fastifyRateLimit, {
  storageAdapter: defaultStorageAdapter,
  exception: Error
})

API

Plugin Options

  • storageAdapter: Required. An object implementing the IStorageAdapter interface
  • exception: Required. A custom exception error class to throw when the rate limit is exceeded.
  • keyGenerator: Optional. Function to generate cache keys from requests
  • prefix: Optional. Prefix for cache keys
  • maxRequests: Optional. Maximum number of requests allowed per minute.
  • maxBurst: Optional. Maximum burst of requests allowed per second.
  • ignorePaths: Optional. List of paths to ignore for rate limiting.

License

ISC

1.0.5

3 months ago

1.0.4

3 months ago

1.0.3

3 months ago

1.0.2

3 months ago

1.0.1

3 months ago

1.0.0

3 months ago