express-rate-limit 6.10.0 → 6.11.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.
package/changelog.md CHANGED
@@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to
7
7
  [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8
8
 
9
+ ## [6.11.0](https://github.com/express-rate-limit/express-rate-limit/releases/tag/v6.11.0)
10
+
11
+ ### Added
12
+
13
+ - Support for retrieving the current hit count and reset time for a given key
14
+ from a store (See
15
+ [#390](https://github.com/express-rate-limit/express-rate-limit/issues/389)).
16
+
9
17
  ## [6.10.0](https://github.com/express-rate-limit/express-rate-limit/releases/tag/v6.10.0)
10
18
 
11
19
  ### Added
package/dist/index.cjs CHANGED
@@ -375,12 +375,29 @@ var MemoryStore = class {
375
375
  if (this.interval.unref)
376
376
  this.interval.unref();
377
377
  }
378
+ /**
379
+ * Method to fetch a client's hit count and reset time.
380
+ *
381
+ * @param key {string} - The identifier for a client.
382
+ *
383
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
384
+ *
385
+ * @public
386
+ */
387
+ async get(key) {
388
+ if (this.hits[key] !== void 0)
389
+ return {
390
+ totalHits: this.hits[key],
391
+ resetTime: this.resetTime
392
+ };
393
+ return void 0;
394
+ }
378
395
  /**
379
396
  * Method to increment a client's hit counter.
380
397
  *
381
398
  * @param key {string} - The identifier for a client.
382
399
  *
383
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
400
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
384
401
  *
385
402
  * @public
386
403
  */
@@ -447,6 +464,10 @@ var promisifyStore = (passedStore) => {
447
464
  }
448
465
  const legacyStore = passedStore;
449
466
  class PromisifiedStore {
467
+ /* istanbul ignore next */
468
+ async get(key) {
469
+ return void 0;
470
+ }
450
471
  async increment(key) {
451
472
  return new Promise((resolve, reject) => {
452
473
  legacyStore.incr(
@@ -556,6 +577,7 @@ var handleAsyncErrors = (fn) => async (request, response, next) => {
556
577
  }
557
578
  };
558
579
  var rateLimit = (passedOptions) => {
580
+ var _a;
559
581
  const config = parseOptions(passedOptions != null ? passedOptions : {});
560
582
  const options = getOptionsFromConfig(config);
561
583
  if (typeof config.store.init === "function")
@@ -636,6 +658,9 @@ var rateLimit = (passedOptions) => {
636
658
  }
637
659
  );
638
660
  middleware.resetKey = config.store.resetKey.bind(config.store);
661
+ middleware.getKey = (_a = config.store.get) == null ? void 0 : _a.bind(
662
+ config.store
663
+ );
639
664
  return middleware;
640
665
  };
641
666
  var lib_default = rateLimit;
package/dist/index.d.cts CHANGED
@@ -46,7 +46,7 @@ export type RateLimitReachedEventHandler = (request: Request, response: Response
46
46
  * @property totalHits {number} - The number of hits for that client so far.
47
47
  * @property resetTime {Date | undefined} - The time when the counter resets.
48
48
  */
49
- export type IncrementResponse = {
49
+ export type ClientRateLimitInfo = {
50
50
  totalHits: number;
51
51
  resetTime: Date | undefined;
52
52
  };
@@ -60,6 +60,14 @@ export type RateLimitRequestHandler = RequestHandler & {
60
60
  * @param key {string} - The identifier for a client.
61
61
  */
62
62
  resetKey: (key: string) => void;
63
+ /**
64
+ * Method to fetch a client's hit count and reset time.
65
+ *
66
+ * @param key {string} - The identifier for a client.
67
+ *
68
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
69
+ */
70
+ getKey?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
63
71
  };
64
72
  /**
65
73
  * An interface that all hit counter stores must implement.
@@ -102,14 +110,22 @@ export type Store = {
102
110
  * @param options {Options} - The options used to setup the middleware.
103
111
  */
104
112
  init?: (options: Options) => void;
113
+ /**
114
+ * Method to fetch a client's hit count and reset time.
115
+ *
116
+ * @param key {string} - The identifier for a client.
117
+ *
118
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
119
+ */
120
+ get?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
105
121
  /**
106
122
  * Method to increment a client's hit counter.
107
123
  *
108
124
  * @param key {string} - The identifier for a client.
109
125
  *
110
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
126
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
111
127
  */
112
- increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
128
+ increment: (key: string) => Promise<ClientRateLimitInfo> | ClientRateLimitInfo;
113
129
  /**
114
130
  * Method to decrement a client's hit counter.
115
131
  *
@@ -329,16 +345,26 @@ export declare class MemoryStore implements Store {
329
345
  * @param options {Options} - The options used to setup the middleware.
330
346
  */
331
347
  init(options: Options): void;
348
+ /**
349
+ * Method to fetch a client's hit count and reset time.
350
+ *
351
+ * @param key {string} - The identifier for a client.
352
+ *
353
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
354
+ *
355
+ * @public
356
+ */
357
+ get(key: string): Promise<ClientRateLimitInfo | undefined>;
332
358
  /**
333
359
  * Method to increment a client's hit counter.
334
360
  *
335
361
  * @param key {string} - The identifier for a client.
336
362
  *
337
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
363
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
338
364
  *
339
365
  * @public
340
366
  */
341
- increment(key: string): Promise<IncrementResponse>;
367
+ increment(key: string): Promise<ClientRateLimitInfo>;
342
368
  /**
343
369
  * Method to decrement a client's hit counter.
344
370
  *
package/dist/index.d.mts CHANGED
@@ -46,7 +46,7 @@ export type RateLimitReachedEventHandler = (request: Request, response: Response
46
46
  * @property totalHits {number} - The number of hits for that client so far.
47
47
  * @property resetTime {Date | undefined} - The time when the counter resets.
48
48
  */
49
- export type IncrementResponse = {
49
+ export type ClientRateLimitInfo = {
50
50
  totalHits: number;
51
51
  resetTime: Date | undefined;
52
52
  };
@@ -60,6 +60,14 @@ export type RateLimitRequestHandler = RequestHandler & {
60
60
  * @param key {string} - The identifier for a client.
61
61
  */
62
62
  resetKey: (key: string) => void;
63
+ /**
64
+ * Method to fetch a client's hit count and reset time.
65
+ *
66
+ * @param key {string} - The identifier for a client.
67
+ *
68
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
69
+ */
70
+ getKey?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
63
71
  };
64
72
  /**
65
73
  * An interface that all hit counter stores must implement.
@@ -102,14 +110,22 @@ export type Store = {
102
110
  * @param options {Options} - The options used to setup the middleware.
103
111
  */
104
112
  init?: (options: Options) => void;
113
+ /**
114
+ * Method to fetch a client's hit count and reset time.
115
+ *
116
+ * @param key {string} - The identifier for a client.
117
+ *
118
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
119
+ */
120
+ get?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
105
121
  /**
106
122
  * Method to increment a client's hit counter.
107
123
  *
108
124
  * @param key {string} - The identifier for a client.
109
125
  *
110
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
126
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
111
127
  */
112
- increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
128
+ increment: (key: string) => Promise<ClientRateLimitInfo> | ClientRateLimitInfo;
113
129
  /**
114
130
  * Method to decrement a client's hit counter.
115
131
  *
@@ -329,16 +345,26 @@ export declare class MemoryStore implements Store {
329
345
  * @param options {Options} - The options used to setup the middleware.
330
346
  */
331
347
  init(options: Options): void;
348
+ /**
349
+ * Method to fetch a client's hit count and reset time.
350
+ *
351
+ * @param key {string} - The identifier for a client.
352
+ *
353
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
354
+ *
355
+ * @public
356
+ */
357
+ get(key: string): Promise<ClientRateLimitInfo | undefined>;
332
358
  /**
333
359
  * Method to increment a client's hit counter.
334
360
  *
335
361
  * @param key {string} - The identifier for a client.
336
362
  *
337
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
363
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
338
364
  *
339
365
  * @public
340
366
  */
341
- increment(key: string): Promise<IncrementResponse>;
367
+ increment(key: string): Promise<ClientRateLimitInfo>;
342
368
  /**
343
369
  * Method to decrement a client's hit counter.
344
370
  *
package/dist/index.d.ts CHANGED
@@ -46,7 +46,7 @@ export type RateLimitReachedEventHandler = (request: Request, response: Response
46
46
  * @property totalHits {number} - The number of hits for that client so far.
47
47
  * @property resetTime {Date | undefined} - The time when the counter resets.
48
48
  */
49
- export type IncrementResponse = {
49
+ export type ClientRateLimitInfo = {
50
50
  totalHits: number;
51
51
  resetTime: Date | undefined;
52
52
  };
@@ -60,6 +60,14 @@ export type RateLimitRequestHandler = RequestHandler & {
60
60
  * @param key {string} - The identifier for a client.
61
61
  */
62
62
  resetKey: (key: string) => void;
63
+ /**
64
+ * Method to fetch a client's hit count and reset time.
65
+ *
66
+ * @param key {string} - The identifier for a client.
67
+ *
68
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
69
+ */
70
+ getKey?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
63
71
  };
64
72
  /**
65
73
  * An interface that all hit counter stores must implement.
@@ -102,14 +110,22 @@ export type Store = {
102
110
  * @param options {Options} - The options used to setup the middleware.
103
111
  */
104
112
  init?: (options: Options) => void;
113
+ /**
114
+ * Method to fetch a client's hit count and reset time.
115
+ *
116
+ * @param key {string} - The identifier for a client.
117
+ *
118
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
119
+ */
120
+ get?: (key: string) => Promise<ClientRateLimitInfo | undefined> | ClientRateLimitInfo | undefined;
105
121
  /**
106
122
  * Method to increment a client's hit counter.
107
123
  *
108
124
  * @param key {string} - The identifier for a client.
109
125
  *
110
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
126
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
111
127
  */
112
- increment: (key: string) => Promise<IncrementResponse> | IncrementResponse;
128
+ increment: (key: string) => Promise<ClientRateLimitInfo> | ClientRateLimitInfo;
113
129
  /**
114
130
  * Method to decrement a client's hit counter.
115
131
  *
@@ -329,16 +345,26 @@ export declare class MemoryStore implements Store {
329
345
  * @param options {Options} - The options used to setup the middleware.
330
346
  */
331
347
  init(options: Options): void;
348
+ /**
349
+ * Method to fetch a client's hit count and reset time.
350
+ *
351
+ * @param key {string} - The identifier for a client.
352
+ *
353
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
354
+ *
355
+ * @public
356
+ */
357
+ get(key: string): Promise<ClientRateLimitInfo | undefined>;
332
358
  /**
333
359
  * Method to increment a client's hit counter.
334
360
  *
335
361
  * @param key {string} - The identifier for a client.
336
362
  *
337
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
363
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
338
364
  *
339
365
  * @public
340
366
  */
341
- increment(key: string): Promise<IncrementResponse>;
367
+ increment(key: string): Promise<ClientRateLimitInfo>;
342
368
  /**
343
369
  * Method to decrement a client's hit counter.
344
370
  *
package/dist/index.mjs CHANGED
@@ -349,12 +349,29 @@ var MemoryStore = class {
349
349
  if (this.interval.unref)
350
350
  this.interval.unref();
351
351
  }
352
+ /**
353
+ * Method to fetch a client's hit count and reset time.
354
+ *
355
+ * @param key {string} - The identifier for a client.
356
+ *
357
+ * @returns {ClientRateLimitInfo | undefined} - The number of hits and reset time for that client.
358
+ *
359
+ * @public
360
+ */
361
+ async get(key) {
362
+ if (this.hits[key] !== void 0)
363
+ return {
364
+ totalHits: this.hits[key],
365
+ resetTime: this.resetTime
366
+ };
367
+ return void 0;
368
+ }
352
369
  /**
353
370
  * Method to increment a client's hit counter.
354
371
  *
355
372
  * @param key {string} - The identifier for a client.
356
373
  *
357
- * @returns {IncrementResponse} - The number of hits and reset time for that client.
374
+ * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client.
358
375
  *
359
376
  * @public
360
377
  */
@@ -421,6 +438,10 @@ var promisifyStore = (passedStore) => {
421
438
  }
422
439
  const legacyStore = passedStore;
423
440
  class PromisifiedStore {
441
+ /* istanbul ignore next */
442
+ async get(key) {
443
+ return void 0;
444
+ }
424
445
  async increment(key) {
425
446
  return new Promise((resolve, reject) => {
426
447
  legacyStore.incr(
@@ -530,6 +551,7 @@ var handleAsyncErrors = (fn) => async (request, response, next) => {
530
551
  }
531
552
  };
532
553
  var rateLimit = (passedOptions) => {
554
+ var _a;
533
555
  const config = parseOptions(passedOptions != null ? passedOptions : {});
534
556
  const options = getOptionsFromConfig(config);
535
557
  if (typeof config.store.init === "function")
@@ -610,6 +632,9 @@ var rateLimit = (passedOptions) => {
610
632
  }
611
633
  );
612
634
  middleware.resetKey = config.store.resetKey.bind(config.store);
635
+ middleware.getKey = (_a = config.store.get) == null ? void 0 : _a.bind(
636
+ config.store
637
+ );
613
638
  return middleware;
614
639
  };
615
640
  var lib_default = rateLimit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-rate-limit",
3
- "version": "6.10.0",
3
+ "version": "6.11.0",
4
4
  "description": "Basic IP rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset.",
5
5
  "author": {
6
6
  "name": "Nathan Friedly",
@@ -60,11 +60,11 @@
60
60
  "build:esm": "esbuild --platform=node --bundle --target=es2019 --format=esm --outfile=dist/index.mjs source/index.ts",
61
61
  "build:types": "dts-bundle-generator --out-file=dist/index.d.ts source/index.ts && cp dist/index.d.ts dist/index.d.cts && cp dist/index.d.ts dist/index.d.mts",
62
62
  "compile": "run-s clean build:*",
63
- "lint:code": "xo --ignore test/external/",
64
- "lint:rest": "prettier --ignore-path .gitignore --ignore-unknown --check .",
63
+ "lint:code": "xo",
64
+ "lint:rest": "prettier --check .",
65
65
  "lint": "run-s lint:*",
66
- "format:code": "npm run lint:code -- --fix",
67
- "format:rest": "npm run lint:rest -- --write .",
66
+ "format:code": "xo --fix",
67
+ "format:rest": "prettier --write .",
68
68
  "format": "run-s format:*",
69
69
  "test:lib": "cross-env NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-vm-modules jest --config config/jest.json",
70
70
  "test:ext": "cd test/external/ && bash run-all-tests",
@@ -119,11 +119,14 @@
119
119
  "@typescript-eslint/no-unsafe-assignment": 0
120
120
  }
121
121
  }
122
+ ],
123
+ "ignore": [
124
+ "test/external"
122
125
  ]
123
126
  },
124
127
  "prettier": "@express-rate-limit/prettier",
125
128
  "lint-staged": {
126
- "{source,test}/**/*.ts": "xo --ignore test/external/ --fix",
127
- "**/*.{json,yaml,md}": "prettier --ignore-path .gitignore --ignore-unknown --write "
129
+ "{source,test}/**/*.ts": "xo --fix",
130
+ "**/*.{json,yaml,md}": "prettier --write "
128
131
  }
129
132
  }