aladov3.13.0

Docs/Features

Rate Limiting

rateLimit (3.13.0) lets one IP address make a number of requests per window of time. A request over it is answered with 429 Too Many Requests and a Retry-After header (seconds), before authentication, before the body is read and before the handler.

src/main.ts
const app = initializeApplication({
  serverOptions: {
    port: 3000,
    trustProxy: 1, // behind a proxy: see below
    rateLimit: { requests: 100, windowMs: 60_000 },
  },
  controllers: [UserController],
});
Option Type Description
requests number How many requests an address may make within windowMs
windowMs number The window, in milliseconds
perEndpoint boolean false (default): an address has one count for the whole API. true: one count per endpoint, by method and path as registered, so /users/1 and /users/2 are one endpoint (/users/:id)
TypeScript
rateLimit: { requests: 100, windowMs: 60_000 }                    // 100 a minute per address, over all endpoints
rateLimit: { requests: 100, windowMs: 60_000, perEndpoint: true } // 100 a minute per address on each endpoint

The limit of a route

A route sets its own limit with @rateLimit, in place of the server's: it has a count of its own, per address for that route, and its requests are not counted for the server's limit. @rateLimit(false) leaves a route without a limit.

TypeScript
import { post, get, rateLimit, defineRequest, Request } from 'alado';

@post('/login')
@rateLimit({ requests: 5, windowMs: 60_000 }) // 5 attempts a minute per address, whatever the limit of the API
@defineRequest({ body: LoginDto })
async login(req: Request) { ... }

@get('/health')
@rateLimit(false)
async health() { ... }

@rateLimit is applied once per method and goes below the HTTP method decorator. Without decorators the same value is rateLimit in the route context (app.post(path, { ..., rateLimit }, handler)). A route can have a limit when the server has none.

Before you rely on it

  • Behind a proxy, set trustProxy. Requests are counted by req.ip. Without trustProxy that is the address of the proxy or load balancer, so all clients share one count and reach the limit together.
  • Counts are kept in memory, per process. With several processes (PM2 cluster, several containers) each one counts for itself, so an address gets up to the limit from each of them: divide requests by the number of processes, or limit in the proxy in front of them. Counts start from zero when the process restarts.

How requests are counted

  • Sliding window. The requests of the last windowMs are estimated from the count of the current window and of the one before it, so a burst at the end of one window and another at the start of the next do not get twice the limit through. A refused request is not counted.
  • IPv6 addresses are counted by their /64 network: a provider gives one customer a whole /64, so single addresses would be a new count for every request.
  • What is counted: every request that reaches a route, and those that reach none (404, 405, the docs), which share one count per address under perEndpoint. A HEAD answered by a GET route is counted with it. CORS preflights are not counted.
  • Memory. A limit counts up to 100 000 addresses at a time; above that the one that came first is dropped to make room. Addresses not seen for two windows are removed as new ones arrive. There is no timer per address.

With the rest of Alado

  • Hooks. onRequest runs before the limit; a 429 goes through beforeSend and is seen by onFinished. See Hooks.
  • CORS. A 429 carries the CORS headers, so a browser client can read it.
  • OpenAPI. A route under a limit, its own or the server's, lists the 429 response in the generated document.
  • Startup. A limit is checked when the server is created and when a route is defined: requests is a whole number from 1, windowMs a number from 1, and a name that is not an option (window) throws.

The types RateLimit and RouteRateLimit are exported from 'alado'.