HttpProtect class
Per-IP request counting, rate limiting and banning for an express-like server.
Signature:
export default class HttpProtect
Remarks
Counters and the block list live in Redis, so several processes behind a load balancer share one view of a client. Networks from @imqueue/net does the CIDR matching for HttpProtect.safeNetworks.
Constructing one opens a Redis connection unless HttpProtectOptions.redis supplies a client, and reads every threshold once — from the options, then from the environment. Changing an environment variable afterwards has no effect on a live instance.
The thresholds are not a rate. See HttpProtect.ttl for what the counter actually measures, and HttpProtectOptions.banLimit for how long a ban lasts, before putting this in front of real traffic.
Constructors
|
Constructor |
Modifiers |
Description |
|---|---|---|
|
Resolves every threshold, parses the exempt networks and connects to Redis. |
Properties
|
Property |
Modifiers |
Type |
Description |
|---|---|---|---|
|
|
number |
The resolved ban threshold, from HttpProtectOptions.banLimit, | |
|
|
string |
The Redis key of the block-list set, | |
|
|
(req: Request) => string | null |
Resolves the client IP for a request — | |
|
|
number |
The resolved 429 threshold, from HttpProtectOptions.maxRequests, | |
|
|
string |
The resolved Redis key prefix, from HttpProtectOptions.redisPrefix. | |
|
|
Networks |
The parsed exempt networks, from HttpProtectOptions.safeNetworks. | |
|
|
number |
The resolved idle timeout in seconds, from HttpProtectOptions.ttl, |
Methods
|
Method |
Modifiers |
Description |
|---|---|---|
|
The current block list, as a | ||
|
Opens a Redis connection and adopts it as this instance's client. | ||
|
Disconnects the Redis client and drops it. | ||
|
Whether an address is currently on the block list. | ||
|
Whether an address is currently over the 429 threshold. | ||
|
A middleware that rejects with a JSON error body. | ||
|
A middleware that rejects with a bare status code and no body. | ||
|
A middleware that rejects with | ||
|
Counts a request against its client IP and decides whether to serve it. |
Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.