1 | // Generated by dts-bundle-generator v8.0.1
|
2 |
|
3 | import { NextFunction, Request, RequestHandler, Response } from 'express';
|
4 |
|
5 | declare const validations: {
|
6 | enabled: {
|
7 | [key: string]: boolean;
|
8 | };
|
9 | disable(): void;
|
10 | /**
|
11 | * Checks whether the IP address is valid, and that it does not have a port
|
12 | * number in it.
|
13 | *
|
14 | * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_invalid_ip_address.
|
15 | *
|
16 | * @param ip {string | undefined} - The IP address provided by Express as request.ip.
|
17 | *
|
18 | * @returns {void}
|
19 | */
|
20 | ip(ip: string | undefined): void;
|
21 | /**
|
22 | * Makes sure the trust proxy setting is not set to `true`.
|
23 | *
|
24 | * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_permissive_trust_proxy.
|
25 | *
|
26 | * @param request {Request} - The Express request object.
|
27 | *
|
28 | * @returns {void}
|
29 | */
|
30 | trustProxy(request: Request): void;
|
31 | /**
|
32 | * Makes sure the trust proxy setting is set in case the `X-Forwarded-For`
|
33 | * header is present.
|
34 | *
|
35 | * See https://github.com/express-rate-limit/express-rate-limit/wiki/Error-Codes#err_erl_unset_trust_proxy.
|
36 | *
|
37 | * @param request {Request} - The Express request object.
|
38 | *
|
39 | * @returns {void}
|
40 | */
|
41 | xForwardedForHeader(request: Request): void;
|
42 | /**
|
43 | * Ensures totalHits value from store is a positive integer.
|
44 | *
|
45 | * @param hits {any} - The `totalHits` returned by the store.
|
46 | */
|
47 | positiveHits(hits: any): void;
|
48 | /**
|
49 | * Ensures a single store instance is not used with multiple express-rate-limit instances
|
50 | */
|
51 | unsharedStore(store: Store): void;
|
52 | /**
|
53 | * Ensures a given key is incremented only once per request.
|
54 | *
|
55 | * @param request {Request} - The Express request object.
|
56 | * @param store {Store} - The store class.
|
57 | * @param key {string} - The key used to store the client's hit count.
|
58 | *
|
59 | * @returns {void}
|
60 | */
|
61 | singleCount(request: Request, store: Store, key: string): void;
|
62 | /**
|
63 | * Warns the user that the behaviour for `max: 0` / `limit: 0` is
|
64 | * changing in the next major release.
|
65 | *
|
66 | * @param limit {number} - The maximum number of hits per client.
|
67 | *
|
68 | * @returns {void}
|
69 | */
|
70 | limit(limit: number): void;
|
71 | /**
|
72 | * Warns the user that the `draft_polli_ratelimit_headers` option is deprecated
|
73 | * and will be removed in the next major release.
|
74 | *
|
75 | * @param draft_polli_ratelimit_headers {any | undefined} - The now-deprecated setting that was used to enable standard headers.
|
76 | *
|
77 | * @returns {void}
|
78 | */
|
79 | draftPolliHeaders(draft_polli_ratelimit_headers?: any): void;
|
80 | /**
|
81 | * Warns the user that the `onLimitReached` option is deprecated and
|
82 | * will be removed in the next major release.
|
83 | *
|
84 | * @param onLimitReached {any | undefined} - The maximum number of hits per client.
|
85 | *
|
86 | * @returns {void}
|
87 | */
|
88 | onLimitReached(onLimitReached?: any): void;
|
89 | /**
|
90 | * Warns the user when the selected headers option requires a reset time but
|
91 | * the store does not provide one.
|
92 | *
|
93 | * @param resetTime {Date | undefined} - The timestamp when the client's hit count will be reset.
|
94 | *
|
95 | * @returns {void}
|
96 | */
|
97 | headersResetTime(resetTime?: Date): void;
|
98 | /**
|
99 | * Checks the options.validate setting to ensure that only recognized
|
100 | * validations are enabled or disabled.
|
101 | *
|
102 | * If any unrecognized values are found, an error is logged that
|
103 | * includes the list of supported vaidations.
|
104 | */
|
105 | validationsConfig(): void;
|
106 | /**
|
107 | * Checks to see if the instance was created inside of a request handler,
|
108 | * which would prevent it from working correctly, with the default memory
|
109 | * store (or any other store with localKeys.)
|
110 | */
|
111 | creationStack(store: Store): void;
|
112 | };
|
113 | export type Validations = typeof validations;
|
114 | /**
|
115 | * Callback that fires when a client's hit counter is incremented.
|
116 | *
|
117 | * @param error {Error | undefined} - The error that occurred, if any.
|
118 | * @param totalHits {number} - The number of hits for that client so far.
|
119 | * @param resetTime {Date | undefined} - The time when the counter resets.
|
120 | */
|
121 | export type IncrementCallback = (error: Error | undefined, totalHits: number, resetTime: Date | undefined) => void;
|
122 | /**
|
123 | * Method (in the form of middleware) to generate/retrieve a value based on the
|
124 | * incoming request.
|
125 | *
|
126 | * @param request {Request} - The Express request object.
|
127 | * @param response {Response} - The Express response object.
|
128 | *
|
129 | * @returns {T} - The value needed.
|
130 | */
|
131 | export type ValueDeterminingMiddleware<T> = (request: Request, response: Response) => T | Promise<T>;
|
132 | /**
|
133 | * Express request handler that sends back a response when a client is
|
134 | * rate-limited.
|
135 | *
|
136 | * @param request {Request} - The Express request object.
|
137 | * @param response {Response} - The Express response object.
|
138 | * @param next {NextFunction} - The Express `next` function, can be called to skip responding.
|
139 | * @param optionsUsed {Options} - The options used to set up the middleware.
|
140 | */
|
141 | export type RateLimitExceededEventHandler = (request: Request, response: Response, next: NextFunction, optionsUsed: Options) => void;
|
142 | /**
|
143 | * Event callback that is triggered on a client's first request that exceeds the limit
|
144 | * but not for subsequent requests. May be used for logging, etc. Should *not*
|
145 | * send a response.
|
146 | *
|
147 | * @param request {Request} - The Express request object.
|
148 | * @param response {Response} - The Express response object.
|
149 | * @param optionsUsed {Options} - The options used to set up the middleware.
|
150 | */
|
151 | export type RateLimitReachedEventHandler = (request: Request, response: Response, optionsUsed: Options) => void;
|
152 | /**
|
153 | * Data returned from the `Store` when a client's hit counter is incremented.
|
154 | *
|
155 | * @property totalHits {number} - The number of hits for that client so far.
|
156 | * @property resetTime {Date | undefined} - The time when the counter resets.
|
157 | */
|
158 | export type ClientRateLimitInfo = {
|
159 | totalHits: number;
|
160 | resetTime: Date | undefined;
|
161 | };
|
162 | export type IncrementResponse = ClientRateLimitInfo;
|
163 | /**
|
164 | * A modified Express request handler with the rate limit functions.
|
165 | */
|
166 | export type RateLimitRequestHandler = RequestHandler & {
|
167 | /**
|
168 | * Method to reset a client's hit counter.
|
169 | *
|
170 | * @param key {string} - The identifier for a client.
|
171 | */
|
172 | resetKey: (key: string) => void;
|
173 | /**
|
174 | * Method to fetch a client's hit count and reset time.
|
175 | *
|
176 | * @param key {string} - The identifier for a client.
|
177 | *
|
178 | * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
|
179 | */
|
180 | getKey: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
|
181 | };
|
182 | /**
|
183 | * An interface that all hit counter stores must implement.
|
184 | *
|
185 | * @deprecated 6.x - Implement the `Store` interface instead.
|
186 | */
|
187 | export type LegacyStore = {
|
188 | /**
|
189 | * Method to increment a client's hit counter.
|
190 | *
|
191 | * @param key {string} - The identifier for a client.
|
192 | * @param callback {IncrementCallback} - The callback to call once the counter is incremented.
|
193 | */
|
194 | incr: (key: string, callback: IncrementCallback) => void;
|
195 | /**
|
196 | * Method to decrement a client's hit counter.
|
197 | *
|
198 | * @param key {string} - The identifier for a client.
|
199 | */
|
200 | decrement: (key: string) => void;
|
201 | /**
|
202 | * Method to reset a client's hit counter.
|
203 | *
|
204 | * @param key {string} - The identifier for a client.
|
205 | */
|
206 | resetKey: (key: string) => void;
|
207 | /**
|
208 | * Method to reset everyone's hit counter.
|
209 | */
|
210 | resetAll?: () => void;
|
211 | };
|
212 | /**
|
213 | * An interface that all hit counter stores must implement.
|
214 | */
|
215 | export type Store = {
|
216 | /**
|
217 | * Method that initializes the store, and has access to the options passed to
|
218 | * the middleware too.
|
219 | *
|
220 | * @param options {Options} - The options used to setup the middleware.
|
221 | */
|
222 | init?: (options: Options) => void;
|
223 | /**
|
224 | * Method to fetch a client's hit count and reset time.
|
225 | *
|
226 | * @param key {string} - The identifier for a client.
|
227 | *
|
228 | * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
|
229 | */
|
230 | get?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
|
231 | /**
|
232 | * Method to increment a client's hit counter.
|
233 | *
|
234 | * @param key {string} - The identifier for a client.
|
235 | *
|
236 | * @returns {IncrementResponse | undefined} - The number of hits and reset time for that client.
|
237 | */
|
238 | increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
|
239 | /**
|
240 | * Method to decrement a client's hit counter.
|
241 | *
|
242 | * @param key {string} - The identifier for a client.
|
243 | */
|
244 | decrement: (key: string) => Promise<void> | void;
|
245 | /**
|
246 | * Method to reset a client's hit counter.
|
247 | *
|
248 | * @param key {string} - The identifier for a client.
|
249 | */
|
250 | resetKey: (key: string) => Promise<void> | void;
|
251 | /**
|
252 | * Method to reset everyone's hit counter.
|
253 | */
|
254 | resetAll?: () => Promise<void> | void;
|
255 | /**
|
256 | * Method to shutdown the store, stop timers, and release all resources.
|
257 | */
|
258 | shutdown?: () => Promise<void> | void;
|
259 | /**
|
260 | * Flag to indicate that keys incremented in one instance of this store can
|
261 | * not affect other instances. Typically false if a database is used, true for
|
262 | * MemoryStore.
|
263 | *
|
264 | * Used to help detect double-counting misconfigurations.
|
265 | */
|
266 | localKeys?: boolean;
|
267 | /**
|
268 | * Optional value that the store prepends to keys
|
269 | *
|
270 | * Used by the double-count check to avoid false-positives when a key is counted twice, but with different prefixes
|
271 | */
|
272 | prefix?: string;
|
273 | };
|
274 | export type DraftHeadersVersion = "draft-6" | "draft-7";
|
275 | /**
|
276 | * Validate configuration object for enabling or disabling specific validations.
|
277 | *
|
278 | * The keys must also be keys in the validations object, except `enable`, `disable`,
|
279 | * and `default`.
|
280 | */
|
281 | export type EnabledValidations = {
|
282 | [key in keyof Omit<Validations, "enabled" | "disable"> | "default"]?: boolean;
|
283 | };
|
284 | /**
|
285 | * The configuration options for the rate limiter.
|
286 | */
|
287 | export type Options = {
|
288 | /**
|
289 | * How long we should remember the requests.
|
290 | *
|
291 | * Defaults to `60000` ms (= 1 minute).
|
292 | */
|
293 | windowMs: number;
|
294 | /**
|
295 | * The maximum number of connections to allow during the `window` before
|
296 | * rate limiting the client.
|
297 | *
|
298 | * Can be the limit itself as a number or express middleware that parses
|
299 | * the request and then figures out the limit.
|
300 | *
|
301 | * Defaults to `5`.
|
302 | */
|
303 | limit: number | ValueDeterminingMiddleware<number>;
|
304 | /**
|
305 | * The response body to send back when a client is rate limited.
|
306 | *
|
307 | * Defaults to `'Too many requests, please try again later.'`
|
308 | */
|
309 | message: any | ValueDeterminingMiddleware<any>;
|
310 | /**
|
311 | * The HTTP status code to send back when a client is rate limited.
|
312 | *
|
313 | * Defaults to `HTTP 429 Too Many Requests` (RFC 6585).
|
314 | */
|
315 | statusCode: number;
|
316 | /**
|
317 | * Whether to send `X-RateLimit-*` headers with the rate limit and the number
|
318 | * of requests.
|
319 | *
|
320 | * Defaults to `true` (for backward compatibility).
|
321 | */
|
322 | legacyHeaders: boolean;
|
323 | /**
|
324 | * Whether to enable support for the standardized rate limit headers (`RateLimit-*`).
|
325 | *
|
326 | * Defaults to `false` (for backward compatibility, but its use is recommended).
|
327 | */
|
328 | standardHeaders: boolean | DraftHeadersVersion;
|
329 | /**
|
330 | * The name of the property on the request object to store the rate limit info.
|
331 | *
|
332 | * Defaults to `rateLimit`.
|
333 | */
|
334 | requestPropertyName: string;
|
335 | /**
|
336 | * If `true`, the library will (by default) skip all requests that have a 4XX
|
337 | * or 5XX status.
|
338 | *
|
339 | * Defaults to `false`.
|
340 | */
|
341 | skipFailedRequests: boolean;
|
342 | /**
|
343 | * If `true`, the library will (by default) skip all requests that have a
|
344 | * status code less than 400.
|
345 | *
|
346 | * Defaults to `false`.
|
347 | */
|
348 | skipSuccessfulRequests: boolean;
|
349 | /**
|
350 | * Method to generate custom identifiers for clients.
|
351 | *
|
352 | * By default, the client's IP address is used.
|
353 | */
|
354 | keyGenerator: ValueDeterminingMiddleware<string>;
|
355 | /**
|
356 | * Express request handler that sends back a response when a client is
|
357 | * rate-limited.
|
358 | *
|
359 | * By default, sends back the `statusCode` and `message` set via the options.
|
360 | */
|
361 | handler: RateLimitExceededEventHandler;
|
362 | /**
|
363 | * Method (in the form of middleware) to determine whether or not this request
|
364 | * counts towards a client's quota.
|
365 | *
|
366 | * By default, skips no requests.
|
367 | */
|
368 | skip: ValueDeterminingMiddleware<boolean>;
|
369 | /**
|
370 | * Method to determine whether or not the request counts as 'succesful'. Used
|
371 | * when either `skipSuccessfulRequests` or `skipFailedRequests` is set to true.
|
372 | *
|
373 | * By default, requests with a response status code less than 400 are considered
|
374 | * successful.
|
375 | */
|
376 | requestWasSuccessful: ValueDeterminingMiddleware<boolean>;
|
377 | /**
|
378 | * The `Store` to use to store the hit count for each client.
|
379 | *
|
380 | * By default, the built-in `MemoryStore` will be used.
|
381 | */
|
382 | store: Store | LegacyStore;
|
383 | /**
|
384 | * The list of validation checks that should run.
|
385 | */
|
386 | validate: boolean | EnabledValidations;
|
387 | /**
|
388 | * Whether to send `X-RateLimit-*` headers with the rate limit and the number
|
389 | * of requests.
|
390 | *
|
391 | * @deprecated 6.x - This option was renamed to `legacyHeaders`.
|
392 | */
|
393 | headers?: boolean;
|
394 | /**
|
395 | * The maximum number of connections to allow during the `window` before
|
396 | * rate limiting the client.
|
397 | *
|
398 | * Can be the limit itself as a number or express middleware that parses
|
399 | * the request and then figures out the limit.
|
400 | *
|
401 | * @deprecated 7.x - This option was renamed to `limit`. However, it will not
|
402 | * be removed from the library in the foreseeable future.
|
403 | */
|
404 | max?: number | ValueDeterminingMiddleware<number>;
|
405 | /**
|
406 | * If the Store generates an error, allow the request to pass.
|
407 | */
|
408 | passOnStoreError: boolean;
|
409 | };
|
410 | /**
|
411 | * The extended request object that includes information about the client's
|
412 | * rate limit.
|
413 | */
|
414 | export type AugmentedRequest = Request & {
|
415 | [key: string]: RateLimitInfo;
|
416 | };
|
417 | /**
|
418 | * The rate limit related information for each client included in the
|
419 | * Express request object.
|
420 | */
|
421 | export type RateLimitInfo = {
|
422 | limit: number;
|
423 | used: number;
|
424 | remaining: number;
|
425 | resetTime: Date | undefined;
|
426 | };
|
427 | /**
|
428 | *
|
429 | * Create an instance of IP rate-limiting middleware for Express.
|
430 | *
|
431 | * @param passedOptions {Options} - Options to configure the rate limiter.
|
432 | *
|
433 | * @returns {RateLimitRequestHandler} - The middleware that rate-limits clients based on your configuration.
|
434 | *
|
435 | * @public
|
436 | */
|
437 | export declare const rateLimit: (passedOptions?: Partial<Options>) => RateLimitRequestHandler;
|
438 | /**
|
439 | * The record that stores information about a client - namely, how many times
|
440 | * they have hit the endpoint, and when their hit count resets.
|
441 | *
|
442 | * Similar to `ClientRateLimitInfo`, except `resetTime` is a compulsory field.
|
443 | */
|
444 | export type Client = {
|
445 | totalHits: number;
|
446 | resetTime: Date;
|
447 | };
|
448 | /**
|
449 | * A `Store` that stores the hit count for each client in memory.
|
450 | *
|
451 | * @public
|
452 | */
|
453 | export declare class MemoryStore implements Store {
|
454 | /**
|
455 | * The duration of time before which all hit counts are reset (in milliseconds).
|
456 | */
|
457 | windowMs: number;
|
458 | /**
|
459 | * These two maps store usage (requests) and reset time by key (for example, IP
|
460 | * addresses or API keys).
|
461 | *
|
462 | * They are split into two to avoid having to iterate through the entire set to
|
463 | * determine which ones need reset. Instead, `Client`s are moved from `previous`
|
464 | * to `current` as they hit the endpoint. Once `windowMs` has elapsed, all clients
|
465 | * left in `previous`, i.e., those that have not made any recent requests, are
|
466 | * known to be expired and can be deleted in bulk.
|
467 | */
|
468 | previous: Map<string, Client>;
|
469 | current: Map<string, Client>;
|
470 | /**
|
471 | * A reference to the active timer.
|
472 | */
|
473 | interval?: NodeJS.Timeout;
|
474 | /**
|
475 | * Confirmation that the keys incremented in once instance of MemoryStore
|
476 | * cannot affect other instances.
|
477 | */
|
478 | localKeys: boolean;
|
479 | /**
|
480 | * Method that initializes the store.
|
481 | *
|
482 | * @param options {Options} - The options used to setup the middleware.
|
483 | */
|
484 | init(options: Options): void;
|
485 | /**
|
486 | * Method to fetch a client's hit count and reset time.
|
487 | *
|
488 | * @param key {string} - The identifier for a client.
|
489 | *
|
490 | * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
|
491 | *
|
492 | * @public
|
493 | */
|
494 | get(key: string): Promise<ClientRateLimitInfo | undefined>;
|
495 | /**
|
496 | * Method to increment a client's hit counter.
|
497 | *
|
498 | * @param key {string} - The identifier for a client.
|
499 | *
|
500 | * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
|
501 | *
|
502 | * @public
|
503 | */
|
504 | increment(key: string): Promise<ClientRateLimitInfo>;
|
505 | /**
|
506 | * Method to decrement a client's hit counter.
|
507 | *
|
508 | * @param key {string} - The identifier for a client.
|
509 | *
|
510 | * @public
|
511 | */
|
512 | decrement(key: string): Promise<void>;
|
513 | /**
|
514 | * Method to reset a client's hit counter.
|
515 | *
|
516 | * @param key {string} - The identifier for a client.
|
517 | *
|
518 | * @public
|
519 | */
|
520 | resetKey(key: string): Promise<void>;
|
521 | /**
|
522 | * Method to reset everyone's hit counter.
|
523 | *
|
524 | * @public
|
525 | */
|
526 | resetAll(): Promise<void>;
|
527 | /**
|
528 | * Method to stop the timer (if currently running) and prevent any memory
|
529 | * leaks.
|
530 | *
|
531 | * @public
|
532 | */
|
533 | shutdown(): void;
|
534 | /**
|
535 | * Recycles a client by setting its hit count to zero, and reset time to
|
536 | * `windowMs` milliseconds from now.
|
537 | *
|
538 | * NOT to be confused with `#resetKey()`, which removes a client from both the
|
539 | * `current` and `previous` maps.
|
540 | *
|
541 | * @param client {Client} - The client to recycle.
|
542 | * @param now {number} - The current time, to which the `windowMs` is added to get the `resetTime` for the client.
|
543 | *
|
544 | * @return {Client} - The modified client that was passed in, to allow for chaining.
|
545 | */
|
546 | private resetClient;
|
547 | /**
|
548 | * Retrieves or creates a client, given a key. Also ensures that the client being
|
549 | * returned is in the `current` map.
|
550 | *
|
551 | * @param key {string} - The key under which the client is (or is to be) stored.
|
552 | *
|
553 | * @returns {Client} - The requested client.
|
554 | */
|
555 | private getClient;
|
556 | /**
|
557 | * Move current clients to previous, create a new map for current.
|
558 | *
|
559 | * This function is called every `windowMs`.
|
560 | */
|
561 | private clearExpired;
|
562 | }
|
563 |
|
564 | export {
|
565 | rateLimit as default,
|
566 | };
|
567 |
|
568 | export {};
|