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