UNPKG

hono-rate-limiter

Version:
111 lines (110 loc) 3.65 kB
import type { Env, Input } from "hono/types"; import type { ClientRateLimitInfo, ConfigType, Store, WSConfigType } from "./types"; /** * A `Store` that stores the hit count for each client in memory. * * @public */ export declare class MemoryStore<E extends Env = Env, P extends string = string, I extends Input = Input> implements Store<E, P, I> { #private; /** * These two maps store usage (requests) and reset time by key (for example, IP * addresses or API keys). * * They are split into two to avoid having to iterate through the entire set to * determine which ones need reset. Instead, `Client`s are moved from `previous` * to `current` as they hit the endpoint. Once `windowMs` has elapsed, all clients * left in `previous`, i.e., those that have not made any recent requests, are * known to be expired and can be deleted in bulk. */ previous: Map<string, Required<ClientRateLimitInfo>>; current: Map<string, Required<ClientRateLimitInfo>>; /** * A reference to the active timer. */ interval?: any; /** * Method that initializes the store. * * @param options {ConfigType | WSConfigType} - The options used to setup the middleware. */ init(options: ConfigType<E, P, I> | WSConfigType<E, P, I>): void; /** * Method to fetch a client's hit count and reset time. * * @param key {string} - The identifier for a client. * * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client. * * @public */ get(key: string): ClientRateLimitInfo | undefined; /** * Method to increment a client's hit counter. * * @param key {string} - The identifier for a client. * * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client. * * @public */ increment(key: string): ClientRateLimitInfo; /** * Method to decrement a client's hit counter. * * @param key {string} - The identifier for a client. * * @public */ decrement(key: string): void; /** * Method to reset a client's hit counter. * * @param key {string} - The identifier for a client. * * @public */ resetKey(key: string): void; /** * Method to reset everyone's hit counter. * * @public */ resetAll(): void; /** * Method to stop the timer (if currently running) and prevent any memory * leaks. * * @public */ shutdown(): void; /** * Recycles a client by setting its hit count to zero, and reset time to * `windowMs` milliseconds from now. * * NOT to be confused with `#resetKey()`, which removes a client from both the * `current` and `previous` maps. * * @param client {Client} - The client to recycle. * @param now {number} - The current time, to which the `windowMs` is added to get the `resetTime` for the client. * * @return {Client} - The modified client that was passed in, to allow for chaining. */ private resetClient; /** * Retrieves or creates a client, given a key. Also ensures that the client being * returned is in the `current` map. * * @param key {string} - The key under which the client is (or is to be) stored. * * @returns {Client} - The requested client. */ private getClient; /** * Move current clients to previous, create a new map for current. * * This function is called every `windowMs`. */ private clearExpired; } export default MemoryStore;