@prexjs/rate-limit 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,700 @@
1
+ import { Request, Response, NextFunction, RequestHandler } from '@prexjs/core';
2
+ export { NextFunction, Request, RequestHandler, Response } from '@prexjs/core';
3
+
4
+ /**
5
+ * Returns the IP address itself for IPv4, or a CIDR-notation subnet for IPv6.
6
+ *
7
+ * If you write a custom keyGenerator that allows a fallback to IP address for
8
+ * unauthenticated users, return ipKeyGenerator(req.ip) rather than just req.ip.
9
+ *
10
+ * For more information, {@see Options.ipv6Subnet}.
11
+ *
12
+ * @param ip {string} - The IP address to process, usually request.ip.
13
+ * @param ipv6Subnet {number | false} - The subnet mask for IPv6 addresses.
14
+ *
15
+ * @returns {string} - The key generated from the IP address
16
+ *
17
+ * @public
18
+ */
19
+ declare function ipKeyGenerator(ip: string, ipv6Subnet?: number | false): any;
20
+
21
+ declare const SUPPORTED_DRAFT_VERSIONS: readonly ["draft-6", "draft-7", "draft-8"];
22
+
23
+ /**
24
+ * The validations that can be run, as well as the methods to run them.
25
+ */
26
+ declare const validations: {
27
+ enabled: {
28
+ [key: string]: boolean;
29
+ };
30
+ disable(): void;
31
+ /**
32
+ * Checks whether the IP address is valid, and that it does not have a port
33
+ * number in it.
34
+ *
35
+ * See https://github.com/rate-limit/rate-limit/wiki/Error-Codes#err_erl_invalid_ip_address.
36
+ *
37
+ * @param ip {string | undefined} - The IP address provided by @prexjs/core as request.ip.
38
+ *
39
+ * @returns {void}
40
+ */
41
+ ip(ip: string | undefined): void;
42
+ /**
43
+ * Makes sure the trust proxy setting is not set to `true`.
44
+ *
45
+ * See https://github.com/rate-limit/rate-limit/wiki/Error-Codes#err_erl_permissive_trust_proxy.
46
+ *
47
+ * @param request {Request} - The @prexjs/core request object.
48
+ *
49
+ * @returns {void}
50
+ */
51
+ trustProxy(request: Request): void;
52
+ /**
53
+ * Makes sure the trust proxy setting is set in case the `X-Forwarded-For`
54
+ * header is present.
55
+ *
56
+ * See https://github.com/rate-limit/rate-limit/wiki/Error-Codes#err_erl_unset_trust_proxy.
57
+ *
58
+ * @param request {Request} - The @prexjs/core request object.
59
+ *
60
+ * @returns {void}
61
+ */
62
+ xForwardedForHeader(request: Request): void;
63
+ /**
64
+ * Alert the user if the Forwarded header is set (standardized version of X-Forwarded-For - not supported by @prexjs/core as of version 5.1.0)
65
+ *
66
+ * @param request {Request} - The @prexjs/core request object.
67
+ *
68
+ * @returns {void}
69
+ */
70
+ forwardedHeader(request: Request): void;
71
+ /**
72
+ * Ensures totalHits value from store is a positive integer.
73
+ *
74
+ * @param hits {any} - The `totalHits` returned by the store.
75
+ */
76
+ positiveHits(hits: any): void;
77
+ /**
78
+ * Ensures a single store instance is not used with multiple rate-limit instances
79
+ */
80
+ unsharedStore(store: Store): void;
81
+ /**
82
+ * Ensures a given key is incremented only once per request.
83
+ *
84
+ * @param request {Request} - The @prexjs/core request object.
85
+ * @param store {Store} - The store class.
86
+ * @param key {string} - The key used to store the client's hit count.
87
+ *
88
+ * @returns {void}
89
+ */
90
+ singleCount(request: Request, store: Store, key: string): void;
91
+ /**
92
+ * Warns the user that the behaviour for `max: 0` / `limit: 0` is
93
+ * changing in the next major release.
94
+ *
95
+ * @param limit {number} - The maximum number of hits per client.
96
+ *
97
+ * @returns {void}
98
+ */
99
+ limit(limit: number): void;
100
+ /**
101
+ * Warns the user that the `draft_polli_ratelimit_headers` option is deprecated
102
+ * and will be removed in the next major release.
103
+ *
104
+ * @param draft_polli_ratelimit_headers {any | undefined} - The now-deprecated setting that was used to enable standard headers.
105
+ *
106
+ * @returns {void}
107
+ */
108
+ draftPolliHeaders(draft_polli_ratelimit_headers?: any): void;
109
+ /**
110
+ * Warns the user that the `onLimitReached` option is deprecated and
111
+ * will be removed in the next major release.
112
+ *
113
+ * @param onLimitReached {any | undefined} - The maximum number of hits per client.
114
+ *
115
+ * @returns {void}
116
+ */
117
+ onLimitReached(onLimitReached?: any): void;
118
+ /**
119
+ * Warns the user when an invalid/unsupported version of the draft spec is passed.
120
+ *
121
+ * @param version {any | undefined} - The version passed by the user.
122
+ *
123
+ * @returns {void}
124
+ */
125
+ headersDraftVersion(version?: any): void;
126
+ /**
127
+ * Warns the user when the selected headers option requires a reset time but
128
+ * the store does not provide one.
129
+ *
130
+ * @param resetTime {Date | undefined} - The timestamp when the client's hit count will be reset.
131
+ *
132
+ * @returns {void}
133
+ */
134
+ headersResetTime(resetTime?: Date): void;
135
+ knownOptions(passedOptions?: Partial<Options>): void;
136
+ /**
137
+ * Checks the options.validate setting to ensure that only recognized
138
+ * validations are enabled or disabled.
139
+ *
140
+ * If any unrecognized values are found, an error is logged that
141
+ * includes the list of supported validations.
142
+ */
143
+ validationsConfig(): void;
144
+ /**
145
+ * Checks to see if the instance was created inside of a request handler,
146
+ * which would prevent it from working correctly, with the default memory
147
+ * store (or any other store with localKeys.)
148
+ */
149
+ creationStack(store: Store): void;
150
+ ipv6Subnet(ipv6Subnet?: any): void;
151
+ ipv6SubnetOrKeyGenerator(options: Partial<Options>): void;
152
+ keyGeneratorIpFallback(keyGenerator?: ValueDeterminingMiddleware<string>): void;
153
+ /**
154
+ * Checks to see if the window duration is greater than 2^32 - 1. This is only
155
+ * called by the default MemoryStore, since it uses Node's setInterval method.
156
+ *
157
+ * See https://nodejs.org/api/timers.html#setintervalcallback-delay-args.
158
+ */
159
+ windowMs(windowMs: number): void;
160
+ };
161
+ type Validations = typeof validations;
162
+
163
+ /**
164
+ * Basic logging function
165
+ *
166
+ * @param error {unknown} - The error to log
167
+ * @param message {string | undefined} - Additional details about the error
168
+ */
169
+ type LoggerFn = (error: unknown, message?: string) => void;
170
+ /**
171
+ * Minimal interface for logging warnings and errors
172
+ */
173
+ type Logger = {
174
+ /**
175
+ * Function to log an error
176
+ */
177
+ error: LoggerFn;
178
+ /**
179
+ * Function to log a warning
180
+ */
181
+ warn: LoggerFn;
182
+ };
183
+ /**
184
+ * Callback that fires when a client's hit counter is incremented.
185
+ *
186
+ * @param error {Error | undefined} - The error that occurred, if any.
187
+ * @param totalHits {number} - The number of hits for that client so far.
188
+ * @param resetTime {Date | undefined} - The time when the counter resets.
189
+ */
190
+ type IncrementCallback = (error: Error | undefined, totalHits: number, resetTime: Date | undefined) => void;
191
+ /**
192
+ * Method (in the form of middleware) to generate/retrieve a value based on the
193
+ * incoming request.
194
+ *
195
+ * @param request {Request} - The @prexjs/core request object.
196
+ * @param response {Response} - The @prexjs/core response object.
197
+ *
198
+ * @returns {T} - The value needed.
199
+ */
200
+ type ValueDeterminingMiddleware<T> = (request: Request, response: Response) => T | Promise<T>;
201
+ /**
202
+ * @prexjs/core request handler that sends back a response when a client is
203
+ * rate-limited.
204
+ *
205
+ * @param request {Request} - The @prexjs/core request object.
206
+ * @param response {Response} - The @prexjs/core response object.
207
+ * @param next {NextFunction} - The @prexjs/core `next` function, can be called to skip responding.
208
+ * @param optionsUsed {Options} - The options used to set up the middleware.
209
+ */
210
+ type RateLimitExceededEventHandler = (request: Request, response: Response, next: NextFunction, optionsUsed: Options) => void;
211
+ /**
212
+ * Event callback that is triggered on a client's first request that exceeds the limit
213
+ * but not for subsequent requests. May be used for logging, etc. Should *not*
214
+ * send a response.
215
+ *
216
+ * @param request {Request} - The @prexjs/core request object.
217
+ * @param response {Response} - The @prexjs/core response object.
218
+ * @param optionsUsed {Options} - The options used to set up the middleware.
219
+ */
220
+ type RateLimitReachedEventHandler = (request: Request, response: Response, optionsUsed: Options) => void;
221
+ /**
222
+ * Data returned from the `Store` when a client's hit counter is incremented.
223
+ *
224
+ * @property totalHits {number} - The number of hits for that client so far.
225
+ * @property resetTime {Date | undefined} - The time when the counter resets.
226
+ */
227
+ type ClientRateLimitInfo = {
228
+ totalHits: number;
229
+ resetTime: Date | undefined;
230
+ };
231
+ type IncrementResponse = ClientRateLimitInfo;
232
+ /**
233
+ * A modified @prexjs/core request handler with the rate limit functions.
234
+ */
235
+ type RateLimitRequestHandler = RequestHandler & {
236
+ /**
237
+ * Method to reset a client's hit counter.
238
+ *
239
+ * @param key {string} - The identifier for a client.
240
+ */
241
+ resetKey: (key: string) => void;
242
+ /**
243
+ * Method to fetch a client's hit count and reset time.
244
+ *
245
+ * @param key {string} - The identifier for a client.
246
+ *
247
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
248
+ */
249
+ getKey: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
250
+ };
251
+ /**
252
+ * An interface that all hit counter stores must implement.
253
+ *
254
+ * @deprecated 6.x - Implement the `Store` interface instead.
255
+ */
256
+ type LegacyStore = {
257
+ /**
258
+ * Method to increment a client's hit counter.
259
+ *
260
+ * @param key {string} - The identifier for a client.
261
+ * @param callback {IncrementCallback} - The callback to call once the counter is incremented.
262
+ */
263
+ incr: (key: string, callback: IncrementCallback) => void;
264
+ /**
265
+ * Method to decrement a client's hit counter.
266
+ *
267
+ * @param key {string} - The identifier for a client.
268
+ */
269
+ decrement: (key: string) => void;
270
+ /**
271
+ * Method to reset a client's hit counter.
272
+ *
273
+ * @param key {string} - The identifier for a client.
274
+ */
275
+ resetKey: (key: string) => void;
276
+ /**
277
+ * Method to reset everyone's hit counter.
278
+ */
279
+ resetAll?: () => void;
280
+ };
281
+ /**
282
+ * An interface that all hit counter stores must implement.
283
+ */
284
+ type Store = {
285
+ /**
286
+ * Method that initializes the store, and has access to the options passed to
287
+ * the middleware too.
288
+ *
289
+ * Called once during initialization.
290
+ *
291
+ * Errors / promise rejections will be caught and logged.
292
+ *
293
+ * Note that the result is not awaited - other store methods (such as increment) may be called before init returns and/or after it throws/rejects.
294
+ *
295
+ * @param options {Options} - The options used to setup the middleware.
296
+ */
297
+ init?: (options: Options) => void | Promise<void>;
298
+ /**
299
+ * Method to fetch a client's hit count and reset time.
300
+ *
301
+ * @param key {string} - The identifier for a client.
302
+ *
303
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
304
+ */
305
+ get?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
306
+ /**
307
+ * Method to increment a client's hit counter.
308
+ *
309
+ * @param key {string} - The identifier for a client.
310
+ *
311
+ * @returns {IncrementResponse | undefined} - The number of hits and reset time for that client.
312
+ */
313
+ increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
314
+ /**
315
+ * Method to decrement a client's hit counter.
316
+ *
317
+ * @param key {string} - The identifier for a client.
318
+ */
319
+ decrement: (key: string) => Promise<void> | void;
320
+ /**
321
+ * Method to reset a client's hit counter.
322
+ *
323
+ * @param key {string} - The identifier for a client.
324
+ */
325
+ resetKey: (key: string) => Promise<void> | void;
326
+ /**
327
+ * Method to reset everyone's hit counter.
328
+ */
329
+ resetAll?: () => Promise<void> | void;
330
+ /**
331
+ * Method to shutdown the store, stop timers, and release all resources.
332
+ */
333
+ shutdown?: () => Promise<void> | void;
334
+ /**
335
+ * Flag to indicate that keys incremented in one instance of this store can
336
+ * not affect other instances. Typically false if a database is used, true for
337
+ * MemoryStore.
338
+ *
339
+ * Used to help detect double-counting misconfigurations.
340
+ */
341
+ localKeys?: boolean;
342
+ /**
343
+ * Optional value that the store prepends to keys
344
+ *
345
+ * Used by the double-count check to avoid false-positives when a key is counted
346
+ * twice, but with different prefixes.
347
+ */
348
+ prefix?: string;
349
+ };
350
+ type DraftHeadersVersion = (typeof SUPPORTED_DRAFT_VERSIONS)[number];
351
+ /**
352
+ * Validate configuration object for enabling or disabling specific validations.
353
+ *
354
+ * The keys must also be keys in the validations object, except `enable`, `disable`,
355
+ * and `default`.
356
+ */
357
+ type EnabledValidations = {
358
+ [key in keyof Omit<Validations, "enabled" | "disable"> | "default"]?: boolean;
359
+ };
360
+ /**
361
+ * The configuration options for the rate limiter.
362
+ */
363
+ type Options = {
364
+ /**
365
+ * How long we should remember the requests.
366
+ *
367
+ * Defaults to `60000` ms (= 1 minute).
368
+ */
369
+ windowMs: number;
370
+ /**
371
+ * The maximum number of connections to allow during the `window` before
372
+ * rate limiting the client.
373
+ *
374
+ * Can be the limit itself as a number or @prexjs/core middleware that parses
375
+ * the request and then figures out the limit.
376
+ *
377
+ * Defaults to `5`.
378
+ */
379
+ limit: number | ValueDeterminingMiddleware<number>;
380
+ /**
381
+ * The response body to send back when a client is rate limited.
382
+ *
383
+ * Defaults to `'Too many requests, please try again later.'`
384
+ */
385
+ message: any | ValueDeterminingMiddleware<any>;
386
+ /**
387
+ * The HTTP status code to send back when a client is rate limited.
388
+ *
389
+ * Defaults to `HTTP 429 Too Many Requests` (RFC 6585).
390
+ */
391
+ statusCode: number;
392
+ /**
393
+ * Whether to send `X-RateLimit-*` headers with the rate limit and the number
394
+ * of requests.
395
+ *
396
+ * Defaults to `true` (for backward compatibility).
397
+ */
398
+ legacyHeaders: boolean;
399
+ /**
400
+ * Whether to enable support for the standardized rate limit headers (`RateLimit-*`).
401
+ *
402
+ * Defaults to `false` (for backward compatibility, but its use is recommended).
403
+ */
404
+ standardHeaders: boolean | DraftHeadersVersion;
405
+ /**
406
+ * The name used to identify the quota policy in the `RateLimit` headers as per
407
+ * the 8th draft of the IETF specification.
408
+ *
409
+ * Defaults to `{limit}-in-{window}`.
410
+ */
411
+ identifier: string | ValueDeterminingMiddleware<string>;
412
+ /**
413
+ * Method to generate a custom `Retry-After` header value (in seconds) when
414
+ * a client is rate limited.
415
+ *
416
+ * This is advisory only, it customizes what the header says, it does not
417
+ * change how long the limiter actually keeps blocking the client for.
418
+ * That is still controlled by `windowMs`.
419
+ *
420
+ * By default, the number of seconds remaining until the window resets.
421
+ */
422
+ retryAfter?: number | ValueDeterminingMiddleware<number>;
423
+ /**
424
+ * The name of the property on the request object to store the rate limit info.
425
+ *
426
+ * Defaults to `rateLimit`.
427
+ */
428
+ requestPropertyName: string;
429
+ /**
430
+ * If `true`, the library will (by default) skip all requests that have a 4XX
431
+ * or 5XX status.
432
+ *
433
+ * Defaults to `false`.
434
+ */
435
+ skipFailedRequests: boolean;
436
+ /**
437
+ * If `true`, the library will (by default) skip all requests that have a
438
+ * status code less than 400.
439
+ *
440
+ * Defaults to `false`.
441
+ */
442
+ skipSuccessfulRequests: boolean;
443
+ /**
444
+ * Method to generate custom identifiers for clients.
445
+ *
446
+ * By default, the client's IP address is used.
447
+ */
448
+ keyGenerator: ValueDeterminingMiddleware<string>;
449
+ /**
450
+ * IPv6 subnet mask applied to IPv6 addresses in the default keyGenerator.
451
+ *
452
+ * Default is 56. The valid range is technically 1-128 but the value should
453
+ * generally be in the 32-64 range.
454
+ *
455
+ * Smaller numbers are more aggressive, larger numbers are more lenient. Try
456
+ * bumping to 60 or 64 if you see evidence of users being blocked incorrectly.
457
+ *
458
+ * May also be set to a function that returns a number based on the request.
459
+ *
460
+ * See the documentation for more info:
461
+ * https://@prexjs/core-rate-limit.mintlify.app/reference/configuration#ipv6subnet.
462
+ */
463
+ ipv6Subnet: 64 | 60 | 56 | 52 | 50 | 48 | 32 | number | ValueDeterminingMiddleware<number> | false;
464
+ /**
465
+ * @prexjs/core request handler that sends back a response when a client is
466
+ * rate-limited.
467
+ *
468
+ * By default, sends back the `statusCode` and `message` set via the options.
469
+ */
470
+ handler: RateLimitExceededEventHandler;
471
+ /**
472
+ * Method (in the form of middleware) to determine whether or not this request
473
+ * counts towards a client's quota.
474
+ *
475
+ * By default, skips no requests.
476
+ */
477
+ skip: ValueDeterminingMiddleware<boolean>;
478
+ /**
479
+ * Method to determine whether or not the request counts as 'successful'. Used
480
+ * when either `skipSuccessfulRequests` or `skipFailedRequests` is set to true.
481
+ *
482
+ * By default, requests with a response status code less than 400 are considered
483
+ * successful.
484
+ */
485
+ requestWasSuccessful: ValueDeterminingMiddleware<boolean>;
486
+ /**
487
+ * The `Store` to use to store the hit count for each client.
488
+ *
489
+ * By default, the built-in `MemoryStore` will be used.
490
+ */
491
+ store: Store | LegacyStore;
492
+ /**
493
+ * The list of validation checks that should run.
494
+ */
495
+ validate: boolean | EnabledValidations;
496
+ /**
497
+ * Whether to send `X-RateLimit-*` headers with the rate limit and the number
498
+ * of requests.
499
+ *
500
+ * @deprecated 6.x - This option was renamed to `legacyHeaders`.
501
+ */
502
+ headers?: boolean;
503
+ /**
504
+ * The maximum number of connections to allow during the `window` before
505
+ * rate limiting the client.
506
+ *
507
+ * Can be the limit itself as a number or @prexjs/core middleware that parses
508
+ * the request and then figures out the limit.
509
+ *
510
+ * @deprecated 7.x - This option was renamed to `limit`. However, it will not
511
+ * be removed from the library in the foreseeable future.
512
+ */
513
+ max?: number | ValueDeterminingMiddleware<number>;
514
+ /**
515
+ * If the Store generates an error, allow the request to pass.
516
+ */
517
+ passOnStoreError: boolean;
518
+ /**
519
+ * The logger to use to log errors. If absent, logs to the console.
520
+ */
521
+ logger: Logger;
522
+ };
523
+ /**
524
+ * The extended request object that includes information about the client's
525
+ * rate limit.
526
+ */
527
+ type AugmentedRequest = Request & {
528
+ [key: string]: RateLimitInfo;
529
+ };
530
+ /**
531
+ * The rate limit related information for each client included in the
532
+ * @prexjs/core request object.
533
+ */
534
+ type RateLimitInfo = {
535
+ limit: number;
536
+ used: number;
537
+ remaining: number;
538
+ resetTime: Date | undefined;
539
+ key: string;
540
+ };
541
+
542
+ /**
543
+ * The record that stores information about a client - namely, how many times
544
+ * they have hit the endpoint, and when their hit count resets.
545
+ *
546
+ * Similar to `ClientRateLimitInfo`, except `resetTime` is a compulsory field.
547
+ */
548
+ type Client = {
549
+ totalHits: number;
550
+ resetTime: Date;
551
+ };
552
+ /**
553
+ * A `Store` that stores the hit count for each client in memory.
554
+ *
555
+ * @public
556
+ */
557
+ declare class MemoryStore implements Store {
558
+ private validations?;
559
+ /**
560
+ * The duration of time before which all hit counts are reset (in milliseconds).
561
+ */
562
+ windowMs: number;
563
+ /**
564
+ * These two maps store usage (requests) and reset time by key (for example, IP
565
+ * addresses or API keys).
566
+ *
567
+ * They are split into two to avoid having to iterate through the entire set to
568
+ * determine which ones need reset. Instead, `Client`s are moved from `previous`
569
+ * to `current` as they hit the endpoint. Once `windowMs` has elapsed, all clients
570
+ * left in `previous`, i.e., those that have not made any recent requests, are
571
+ * known to be expired and can be deleted in bulk.
572
+ */
573
+ previous: Map<string, Client>;
574
+ current: Map<string, Client>;
575
+ /**
576
+ * A reference to the active timer.
577
+ */
578
+ interval?: NodeJS.Timeout;
579
+ /**
580
+ * Confirmation that the keys incremented in once instance of MemoryStore
581
+ * cannot affect other instances.
582
+ */
583
+ localKeys: boolean;
584
+ constructor(validations?: Validations);
585
+ /**
586
+ * Method that initializes the store.
587
+ *
588
+ * @param options {Options} - The options used to setup the middleware.
589
+ */
590
+ init(options: Options): void;
591
+ /**
592
+ * Method to fetch a client's hit count and reset time.
593
+ *
594
+ * @param key {string} - The identifier for a client.
595
+ *
596
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
597
+ *
598
+ * @public
599
+ */
600
+ get(key: string): Promise<ClientRateLimitInfo | undefined>;
601
+ /**
602
+ * Method to increment a client's hit counter.
603
+ *
604
+ * @param key {string} - The identifier for a client.
605
+ *
606
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
607
+ *
608
+ * @public
609
+ */
610
+ increment(key: string): Promise<ClientRateLimitInfo>;
611
+ /**
612
+ * Method to decrement a client's hit counter.
613
+ *
614
+ * @param key {string} - The identifier for a client.
615
+ *
616
+ * @public
617
+ */
618
+ decrement(key: string): Promise<void>;
619
+ /**
620
+ * Method to reset a client's hit counter.
621
+ *
622
+ * @param key {string} - The identifier for a client.
623
+ *
624
+ * @public
625
+ */
626
+ resetKey(key: string): Promise<void>;
627
+ /**
628
+ * Method to reset everyone's hit counter.
629
+ *
630
+ * @public
631
+ */
632
+ resetAll(): Promise<void>;
633
+ /**
634
+ * Method to stop the timer (if currently running) and prevent any memory
635
+ * leaks.
636
+ *
637
+ * @public
638
+ */
639
+ shutdown(): void;
640
+ /**
641
+ * Recycles a client by setting its hit count to zero, and reset time to
642
+ * `windowMs` milliseconds from now.
643
+ *
644
+ * NOT to be confused with `#resetKey()`, which removes a client from both the
645
+ * `current` and `previous` maps.
646
+ *
647
+ * @param client {Client} - The client to recycle.
648
+ * @param now {number} - The current time, to which the `windowMs` is added to get the `resetTime` for the client.
649
+ *
650
+ * @return {Client} - The modified client that was passed in, to allow for chaining.
651
+ */
652
+ private resetClient;
653
+ /**
654
+ * Retrieves or creates a client, given a key. Also ensures that the client being
655
+ * returned is in the `current` map.
656
+ *
657
+ * @param key {string} - The key under which the client is (or is to be) stored.
658
+ *
659
+ * @returns {Client} - The requested client.
660
+ */
661
+ private getClient;
662
+ /**
663
+ * Move current clients to previous, create a new map for current.
664
+ *
665
+ * This function is called every `windowMs`.
666
+ */
667
+ private clearExpired;
668
+ }
669
+
670
+ /**
671
+ *
672
+ * Create an instance of IP rate-limiting middleware for @prexjs/core.
673
+ *
674
+ * @param passedOptions {Options} - Options to configure the rate limiter.
675
+ *
676
+ * @returns {RateLimitRequestHandler} - The middleware that rate-limits clients based on your configuration.
677
+ *
678
+ * @public
679
+ */
680
+ declare const rateLimit: (passedOptions?: Partial<Options>) => RateLimitRequestHandler;
681
+
682
+ /**
683
+ * @deprecated these are being replaced in the next major version
684
+ */
685
+ declare const SECOND: number;
686
+ /**
687
+ * @deprecated these are being replaced in the next major version
688
+ */
689
+ declare const MINUTE: number;
690
+ /**
691
+ * @deprecated these are being replaced in the next major version
692
+ */
693
+ declare const HOUR: number;
694
+ /**
695
+ * @deprecated these are being replaced in the next major version
696
+ */
697
+ declare const DAY: number;
698
+
699
+ export { DAY, HOUR, MINUTE, MemoryStore, SECOND, rateLimit as default, ipKeyGenerator, rateLimit };
700
+ export type { AugmentedRequest, ClientRateLimitInfo, DraftHeadersVersion, EnabledValidations, IncrementCallback, IncrementResponse, LegacyStore, Logger, LoggerFn, Options, RateLimitExceededEventHandler, RateLimitInfo, RateLimitReachedEventHandler, RateLimitRequestHandler, Store, ValueDeterminingMiddleware };