rate-limit-memcached
Version:
A memcached store for the express-rate-limit middleware.
127 lines (123 loc) • 3.81 kB
text/typescript
import Memcached from 'memcached';
import { Store, Options as Options$1, IncrementResponse } from 'express-rate-limit';
/**
* A memcached client.
*/
type MemcachedClient = {
get: (key: string, cb: (error: any, data: any) => void) => void;
set: (key: string, value: any, time: number, cb: (error: any) => void) => void;
add: (key: string, value: any, time: number, cb: (error: any) => void) => void;
del: (key: string, cb: (error: any) => void) => void;
incr: (key: string, amount: number, cb: (error: any) => void) => void;
decr: (key: string, amount: number, cb: (error: any) => void) => void;
};
/**
* The configuration options for the store.
*/
type Options = {
/**
* The text to prepend to the key.
*/
prefix: string;
/**
* The `memcached` client to use.
*/
client: MemcachedClient;
/**
* A list of memcached server URLs to store the keys in, passed to the default
* memcached client.
*
* Note that the default client is only used if another client is not passed
* to the store.
*/
locations: string[];
/**
* The configuration to pass to the default client, along with the `locations`.
*/
config: Memcached.options;
};
/**
* The promisifed version of the `MemcachedClient`.
*/
type PromisifiedMemcachedClient = {
get: <T>(key: string) => Promise<T | undefined>;
set: (key: string, value: any, time: number) => Promise<void>;
add: (key: string, value: any, time: number) => Promise<void>;
del: (key: string) => Promise<boolean | undefined>;
incr: (key: string, amount: number) => Promise<boolean | number>;
decr: (key: string, amount: number) => Promise<boolean | number>;
};
/**
* A `Store` for the `express-rate-limit` package that stores hit counts in
* Memcached.
*/
declare class MemcachedStore implements Store {
/**
* The number of seconds to remember a client's requests.
*/
expiration: number;
/**
* The text to prepend to the key.
*/
prefix: string;
/**
* The `memcached` client to use.
*/
client: MemcachedClient;
/**
* The promisifed functions from the `client` object.
*/
fns: PromisifiedMemcachedClient;
/**
* @constructor for `MemcachedStore`.
*
* @param options {Options} - The options used to configure the store's behaviour.
*/
constructor(options?: Partial<Options>);
/**
* Method that actually initializes the store.
*
* @param options {RateLimitConfiguration} - The options used to setup the middleware.
*
* @impl
*/
init(options: Options$1): void;
/**
* Method to prefix the keys with the given text.
*
* @param key {string} - The key.
*
* @returns {string} - The text + the key.
*/
prefixKey(key: string): string;
/**
* Method that returns the name of the key used to store the reset timestamp
* for the given key.
*
* @param key {string} - The key.
*
* @returns {string} - The expiry key's name.
*/
expiryKey(key: string): string;
/**
* Method to increment a client's hit counter.
*
* @param key {string} - The identifier for a client.
*
* @returns {IncrementResponse} - The number of hits and reset time for that client.
*/
increment(key: string): Promise<IncrementResponse>;
/**
* Method to decrement a client's hit counter.
*
* @param key {string} - The identifier for a client
*/
decrement(key: string): Promise<void>;
/**
* Method to reset a client's hit counter.
*
* @param key {string} - The identifier for a client.
*/
resetKey(key: string): Promise<void>;
}
export { MemcachedClient, MemcachedStore, Options };