express-rate-limit 6.0.1 → 6.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-rate-limit",
3
- "version": "6.0.1",
3
+ "version": "6.0.2",
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",
@@ -28,11 +28,11 @@
28
28
  "attack"
29
29
  ],
30
30
  "type": "module",
31
- "types": "./dist/esm/index.d.ts",
31
+ "types": "./dist/index.d.ts",
32
32
  "exports": {
33
33
  ".": {
34
- "import": "./dist/esm/index.js",
35
- "require": "./dist/cjs/index.js"
34
+ "import": "./dist/index.mjs",
35
+ "require": "./dist/index.cjs"
36
36
  }
37
37
  },
38
38
  "files": [
@@ -48,17 +48,21 @@
48
48
  },
49
49
  "scripts": {
50
50
  "clean": "del-cli dist/ coverage/ *.log *.tmp *.bak *.tgz",
51
- "build:cjs": "tsc --project config/typescript/cjs.json && cpy --rename package.json config/node/cjs.json dist/cjs/",
52
- "build:esm": "tsc --project config/typescript/esm.json && cpy --rename package.json config/node/esm.json dist/esm/",
53
- "build": "run-p build:*",
54
- "compile": "run-s clean build",
55
- "lint": "xo",
56
- "autofix": "xo --fix",
57
- "test-lib": "cross-env TS_NODE_PROJECT=config/typescript/test.json NODE_OPTIONS=--experimental-vm-modules jest",
58
- "test": "run-s compile lint test-lib",
59
- "view-coverage": "npx serve coverage/lcov-report",
51
+ "build:cjs": "esbuild source/index.ts --bundle --format=cjs --outfile=dist/index.cjs --footer:js='module.exports = rateLimit;'",
52
+ "build:esm": "esbuild source/index.ts --bundle --format=esm --outfile=dist/index.mjs",
53
+ "build:types": "dts-bundle-generator --out-file=dist/index.d.ts source/index.ts",
54
+ "compile": "run-s clean build:*",
55
+ "lint:code": "xo --ignore test/external/",
56
+ "lint:rest": "prettier --ignore-path .gitignore --ignore-unknown --check .",
57
+ "lint": "run-s lint:*",
58
+ "autofix:code": "xo --ignore test/external/ --fix",
59
+ "autofix:rest": "prettier --ignore-path .gitignore --ignore-unknown --write .",
60
+ "autofix": "run-s autofix:*",
61
+ "test:lib": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
62
+ "test:ext": "cd test/external/ && bash run-all-tests",
63
+ "test": "npm pack && run-s lint test:*",
60
64
  "pre-commit": "lint-staged",
61
- "prepare": "npm run compile && husky install config/husky"
65
+ "prepare": "run-s compile && husky install config/husky"
62
66
  },
63
67
  "peerDependencies": {
64
68
  "express": "^4"
@@ -69,9 +73,10 @@
69
73
  "@types/jest": "^27.0.3",
70
74
  "@types/node": "^16.11.17",
71
75
  "@types/supertest": "^2.0.11",
72
- "cpy-cli": "^3.1.1",
73
76
  "cross-env": "^7.0.3",
74
77
  "del-cli": "^4.0.1",
78
+ "dts-bundle-generator": "^6.2.0",
79
+ "esbuild": "^0.14.8",
75
80
  "express": "^4.17.1",
76
81
  "husky": "^7.0.4",
77
82
  "jest": "^27.4.3",
@@ -119,7 +124,7 @@
119
124
  ],
120
125
  "testTimeout": 30000,
121
126
  "testMatch": [
122
- "**/test/**/*-test.[jt]s?(x)"
127
+ "**/test/library/**/*-test.[jt]s?(x)"
123
128
  ],
124
129
  "moduleFileExtensions": [
125
130
  "js",
@@ -133,7 +138,7 @@
133
138
  }
134
139
  },
135
140
  "lint-staged": {
136
- "{source,test}/**/*.ts": "xo --fix",
137
- "**/*.{json,yaml,md}": "prettier --write"
141
+ "{source,test}/**/*.ts": "xo --ignore test/external/ --fix",
142
+ "**/*.{json,yaml,md}": "prettier --ignore-path .gitignore --ignore-unknown --write "
138
143
  }
139
144
  }
package/readme.md CHANGED
@@ -14,9 +14,9 @@ public APIs and/or endpoints such as password reset. Plays nice with
14
14
 
15
15
  ### Alternate Rate-limiters
16
16
 
17
- > This module does not share state with other processes/servers by
18
- > default. If you need a more robust solution, I recommend using an external
19
- > store. See the [`stores` section](#store) below for a list of external stores.
17
+ > This module does not share state with other processes/servers by default. If
18
+ > you need a more robust solution, I recommend using an external store. See the
19
+ > [`stores` section](#store) below for a list of external stores.
20
20
 
21
21
  This module was designed to only handle the basics and didn't even support
22
22
  external stores initially. These other options all are excellent pieces of
@@ -275,9 +275,9 @@ const keyGenerator = (request /*, response*/) => request.ip
275
275
 
276
276
  The function to handle requests once the max limit is exceeded. It receives the
277
277
  `request` and the `response` objects. The `next` param is available if you need
278
- to pass to the next middleware/route. Finally, the `options` param has all
279
- of the options that originally passed in when creating the current limiter and
280
- the default values for other options.
278
+ to pass to the next middleware/route. Finally, the `options` param has all of
279
+ the options that originally passed in when creating the current limiter and the
280
+ default values for other options.
281
281
 
282
282
  The `request.rateLimit` object has `limit`, `current`, and `remaining` number of
283
283
  requests and, if the store provides it, a `resetTime` Date object.
package/tsconfig.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "declaration": true,
4
- "sourceMap": true,
5
4
 
6
5
  "strict": true,
7
6
  "noUnusedLocals": true,
@@ -9,9 +8,7 @@
9
8
  "noFallthroughCasesInSwitch": true,
10
9
 
11
10
  "moduleResolution": "node",
12
- "esModuleInterop": true,
13
-
14
- "inlineSources": true
11
+ "esModuleInterop": true
15
12
  },
16
13
  "include": ["./source/**/*.ts"],
17
14
  "exclude": ["./node_modules"]
@@ -1,2 +0,0 @@
1
- export * from './types.js';
2
- export { default } from './lib.js';
package/dist/cjs/index.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
- // /source/index.ts
3
- // Export away!
4
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
- if (k2 === undefined) k2 = k;
6
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
7
- }) : (function(o, m, k, k2) {
8
- if (k2 === undefined) k2 = k;
9
- o[k2] = m[k];
10
- }));
11
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
13
- };
14
- exports.__esModule = true;
15
- exports["default"] = void 0;
16
- __exportStar(require("./types.js"), exports);
17
- var lib_js_1 = require("./lib.js");
18
- __createBinding(exports, lib_js_1, "default");
19
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/index.ts"],"names":[],"mappings":";AAAA,mBAAmB;AACnB,eAAe;;;;;;;;;;;;;AAEf,6CAA0B;AAC1B,mCAAkC;AAAzB,8CAAO","sourcesContent":["// /source/index.ts\n// Export away!\n\nexport * from './types.js'\nexport { default } from './lib.js'\n"]}
package/dist/cjs/lib.d.ts DELETED
@@ -1,15 +0,0 @@
1
- import { Options, RateLimitRequestHandler, LegacyStore, Store } from './types.js';
2
- /**
3
- *
4
- * Create an instance of IP rate-limiting middleware for Express.
5
- *
6
- * @param passedOptions {Options} - Options to configure the rate limiter
7
- *
8
- * @returns {RateLimitRequestHandler} - The middleware that rate-limits clients based on your configuration
9
- *
10
- * @public
11
- */
12
- declare const rateLimit: (passedOptions?: (Omit<Partial<Options>, "store"> & {
13
- store?: LegacyStore | Store | undefined;
14
- }) | undefined) => RateLimitRequestHandler;
15
- export default rateLimit;
package/dist/cjs/lib.js DELETED
@@ -1,366 +0,0 @@
1
- "use strict";
2
- // /source/lib.ts
3
- // The option parser and rate limiting middleware
4
- var __assign = (this && this.__assign) || function () {
5
- __assign = Object.assign || function(t) {
6
- for (var s, i = 1, n = arguments.length; i < n; i++) {
7
- s = arguments[i];
8
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
9
- t[p] = s[p];
10
- }
11
- return t;
12
- };
13
- return __assign.apply(this, arguments);
14
- };
15
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
- step((generator = generator.apply(thisArg, _arguments || [])).next());
22
- });
23
- };
24
- var __generator = (this && this.__generator) || function (thisArg, body) {
25
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
27
- function verb(n) { return function (v) { return step([n, v]); }; }
28
- function step(op) {
29
- if (f) throw new TypeError("Generator is already executing.");
30
- while (_) try {
31
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
32
- if (y = 0, t) op = [op[0] & 2, t.value];
33
- switch (op[0]) {
34
- case 0: case 1: t = op; break;
35
- case 4: _.label++; return { value: op[1], done: false };
36
- case 5: _.label++; y = op[1]; op = [0]; continue;
37
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
- default:
39
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
- if (t[2]) _.ops.pop();
44
- _.trys.pop(); continue;
45
- }
46
- op = body.call(thisArg, _);
47
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
- }
50
- };
51
- var __importDefault = (this && this.__importDefault) || function (mod) {
52
- return (mod && mod.__esModule) ? mod : { "default": mod };
53
- };
54
- exports.__esModule = true;
55
- var memory_store_js_1 = __importDefault(require("./memory-store.js"));
56
- /**
57
- * Type guard to check if a store is legacy store.
58
- *
59
- * @param store {LegacyStore | Store} - The store to check
60
- *
61
- * @return {boolean} - Whether the store is a legacy store
62
- */
63
- var isLegacyStore = function (store) {
64
- // Check that `incr` exists but `increment` does not - store authors might want
65
- // to keep both around for backwards compatibility.
66
- return typeof store.incr === 'function' &&
67
- typeof store.increment !== 'function';
68
- };
69
- /**
70
- * Converts a legacy store to the promisified version.
71
- *
72
- * @param store {LegacyStore | Store} - The legacy store or even a modern store
73
- *
74
- * @returns {Store} - The promisified version of the store
75
- */
76
- var promisifyStore = function (passedStore) {
77
- if (!isLegacyStore(passedStore)) {
78
- // It's not an old store, return as is
79
- return passedStore;
80
- }
81
- // Why can't Typescript understand this?
82
- var legacyStore = passedStore;
83
- // A promisified version of the store
84
- var PromisifiedStore = /** @class */ (function () {
85
- function PromisifiedStore() {
86
- }
87
- PromisifiedStore.prototype.increment = function (key) {
88
- return __awaiter(this, void 0, void 0, function () {
89
- return __generator(this, function (_a) {
90
- return [2 /*return*/, new Promise(function (resolve, reject) {
91
- legacyStore.incr(key, function (error, totalHits, resetTime) {
92
- if (error)
93
- reject(error);
94
- resolve({ totalHits: totalHits, resetTime: resetTime });
95
- });
96
- })];
97
- });
98
- });
99
- };
100
- PromisifiedStore.prototype.decrement = function (key) {
101
- return __awaiter(this, void 0, void 0, function () {
102
- return __generator(this, function (_a) {
103
- return [2 /*return*/, Promise.resolve(legacyStore.decrement(key))];
104
- });
105
- });
106
- };
107
- PromisifiedStore.prototype.resetKey = function (key) {
108
- return __awaiter(this, void 0, void 0, function () {
109
- return __generator(this, function (_a) {
110
- return [2 /*return*/, Promise.resolve(legacyStore.resetKey(key))];
111
- });
112
- });
113
- };
114
- PromisifiedStore.prototype.resetAll = function () {
115
- return __awaiter(this, void 0, void 0, function () {
116
- return __generator(this, function (_a) {
117
- if (typeof legacyStore.resetAll === 'function')
118
- return [2 /*return*/, Promise.resolve(legacyStore.resetAll())];
119
- return [2 /*return*/];
120
- });
121
- });
122
- };
123
- return PromisifiedStore;
124
- }());
125
- return new PromisifiedStore();
126
- };
127
- /**
128
- * Adds the defaults for options the user has not specified.
129
- *
130
- * @param options {Options} - The options the user specifies
131
- *
132
- * @returns {Options} - A complete configuration object
133
- */
134
- var parseOptions = function (passedOptions) {
135
- // Now add the defaults for the other options
136
- var _a, _b;
137
- var options = __assign({ windowMs: 60 * 1000, store: new memory_store_js_1["default"](), max: 5, message: 'Too many requests, please try again later.', statusCode: 429, legacyHeaders: (_a = passedOptions.headers) !== null && _a !== void 0 ? _a : true, standardHeaders: (_b = passedOptions.draft_polli_ratelimit_headers) !== null && _b !== void 0 ? _b : false, requestPropertyName: 'rateLimit', skipFailedRequests: false, skipSuccessfulRequests: false, requestWasSuccessful: function (_request, response) { return response.statusCode < 400; }, skip: function (_request, _response) {
138
- return false;
139
- }, keyGenerator: function (request, _response) {
140
- if (!request.ip) {
141
- console.error('WARN | `express-rate-limit` | `request.ip` is undefined. You can avoid this by providing a custom `keyGenerator` function, but it may be indicative of a larger issue.');
142
- }
143
- return request.ip;
144
- }, handler: function (_request, response, _next, _optionsUsed) {
145
- response.status(options.statusCode).send(options.message);
146
- }, onLimitReached: function (_request, _response, _optionsUsed) { } }, passedOptions);
147
- // Ensure that the store passed implements the either the `Store` or `LegacyStore`
148
- // interface
149
- if ((typeof options.store.incr !== 'function' &&
150
- typeof options.store.increment !== 'function') ||
151
- typeof options.store.decrement !== 'function' ||
152
- typeof options.store.resetKey !== 'function' ||
153
- (typeof options.store.resetAll !== 'undefined' &&
154
- typeof options.store.resetAll !== 'function') ||
155
- (typeof options.store.init !== 'undefined' &&
156
- typeof options.store.init !== 'function')) {
157
- throw new TypeError('An invalid store was passed. Please ensure that the store is a class that implements the `Store` interface.');
158
- }
159
- // Promisify the store, if it is not already
160
- options.store = promisifyStore(options.store);
161
- // Return the 'clean' options
162
- return options;
163
- };
164
- /**
165
- * Just pass on any errors for the developer to handle, usually as a HTTP 500
166
- * Internal Server Error.
167
- *
168
- * @param fn {Express.RequestHandler} - The request handler for which to handle errors
169
- *
170
- * @returns {Express.RequestHandler} - The request handler wrapped with a `.catch` clause
171
- *
172
- * @private
173
- */
174
- var handleAsyncErrors = function (fn) {
175
- return function (request, response, next) { return __awaiter(void 0, void 0, void 0, function () {
176
- var error_1;
177
- return __generator(this, function (_a) {
178
- switch (_a.label) {
179
- case 0:
180
- _a.trys.push([0, 2, , 3]);
181
- return [4 /*yield*/, Promise.resolve(fn(request, response, next))["catch"](next)];
182
- case 1:
183
- _a.sent();
184
- return [3 /*break*/, 3];
185
- case 2:
186
- error_1 = _a.sent();
187
- next(error_1);
188
- return [3 /*break*/, 3];
189
- case 3: return [2 /*return*/];
190
- }
191
- });
192
- }); };
193
- };
194
- /**
195
- *
196
- * Create an instance of IP rate-limiting middleware for Express.
197
- *
198
- * @param passedOptions {Options} - Options to configure the rate limiter
199
- *
200
- * @returns {RateLimitRequestHandler} - The middleware that rate-limits clients based on your configuration
201
- *
202
- * @public
203
- */
204
- var rateLimit = function (passedOptions) {
205
- // Parse the options and add the default values for unspecified options
206
- var options = parseOptions(passedOptions !== null && passedOptions !== void 0 ? passedOptions : {});
207
- // Call the `init` method on the store, if it exists
208
- if (typeof options.store.init === 'function')
209
- options.store.init(options);
210
- // Then return the actual middleware
211
- var middleware = handleAsyncErrors(function (request, response, next) { return __awaiter(void 0, void 0, void 0, function () {
212
- var skip, augmentedRequest, key, _a, totalHits, resetTime, retrieveQuota, maxHits, deltaSeconds, decremented_1, decrementKey_1;
213
- return __generator(this, function (_b) {
214
- switch (_b.label) {
215
- case 0: return [4 /*yield*/, options.skip(request, response)];
216
- case 1:
217
- skip = _b.sent();
218
- if (skip) {
219
- next();
220
- return [2 /*return*/];
221
- }
222
- augmentedRequest = request;
223
- return [4 /*yield*/, options.keyGenerator(request, response)
224
- // Increment the client's hit counter by one
225
- ];
226
- case 2:
227
- key = _b.sent();
228
- return [4 /*yield*/, options.store.increment(key)
229
- // Get the quota (max number of hits) for each client
230
- ];
231
- case 3:
232
- _a = _b.sent(), totalHits = _a.totalHits, resetTime = _a.resetTime;
233
- retrieveQuota = typeof options.max === 'function'
234
- ? options.max(request, response)
235
- : options.max;
236
- return [4 /*yield*/, retrieveQuota
237
- // Set the rate limit information on the augmented request object
238
- ];
239
- case 4:
240
- maxHits = _b.sent();
241
- // Set the rate limit information on the augmented request object
242
- augmentedRequest[options.requestPropertyName] = {
243
- limit: maxHits,
244
- current: totalHits,
245
- remaining: Math.max(maxHits - totalHits, 0),
246
- resetTime: resetTime
247
- };
248
- // Set the X-RateLimit headers on the response object if enabled
249
- if (options.legacyHeaders && !response.headersSent) {
250
- response.setHeader('X-RateLimit-Limit', maxHits);
251
- response.setHeader('X-RateLimit-Remaining', augmentedRequest[options.requestPropertyName].remaining);
252
- // If we have a resetTime, also provide the current date to help avoid issues with incorrect clocks
253
- if (resetTime instanceof Date) {
254
- response.setHeader('Date', new Date().toUTCString());
255
- response.setHeader('X-RateLimit-Reset', Math.ceil(resetTime.getTime() / 1000));
256
- }
257
- }
258
- // Set the standardized RateLimit headers on the response object
259
- // if enabled
260
- if (options.standardHeaders && !response.headersSent) {
261
- response.setHeader('RateLimit-Limit', maxHits);
262
- response.setHeader('RateLimit-Remaining', augmentedRequest[options.requestPropertyName].remaining);
263
- if (resetTime) {
264
- deltaSeconds = Math.ceil((resetTime.getTime() - Date.now()) / 1000);
265
- response.setHeader('RateLimit-Reset', Math.max(0, deltaSeconds));
266
- }
267
- }
268
- // If we are to skip failed/successfull requests, decrement the
269
- // counter accordingly once we know the status code of the request
270
- if (options.skipFailedRequests || options.skipSuccessfulRequests) {
271
- decremented_1 = false;
272
- decrementKey_1 = function () { return __awaiter(void 0, void 0, void 0, function () {
273
- return __generator(this, function (_a) {
274
- switch (_a.label) {
275
- case 0:
276
- if (!!decremented_1) return [3 /*break*/, 2];
277
- return [4 /*yield*/, options.store.decrement(key)];
278
- case 1:
279
- _a.sent();
280
- decremented_1 = true;
281
- _a.label = 2;
282
- case 2: return [2 /*return*/];
283
- }
284
- });
285
- }); };
286
- if (options.skipFailedRequests) {
287
- response.on('finish', function () { return __awaiter(void 0, void 0, void 0, function () {
288
- return __generator(this, function (_a) {
289
- switch (_a.label) {
290
- case 0:
291
- if (!!options.requestWasSuccessful(request, response)) return [3 /*break*/, 2];
292
- return [4 /*yield*/, decrementKey_1()];
293
- case 1:
294
- _a.sent();
295
- _a.label = 2;
296
- case 2: return [2 /*return*/];
297
- }
298
- });
299
- }); });
300
- response.on('close', function () { return __awaiter(void 0, void 0, void 0, function () {
301
- return __generator(this, function (_a) {
302
- switch (_a.label) {
303
- case 0:
304
- if (!!response.writableEnded) return [3 /*break*/, 2];
305
- return [4 /*yield*/, decrementKey_1()];
306
- case 1:
307
- _a.sent();
308
- _a.label = 2;
309
- case 2: return [2 /*return*/];
310
- }
311
- });
312
- }); });
313
- response.on('error', function () { return __awaiter(void 0, void 0, void 0, function () {
314
- return __generator(this, function (_a) {
315
- switch (_a.label) {
316
- case 0: return [4 /*yield*/, decrementKey_1()];
317
- case 1:
318
- _a.sent();
319
- return [2 /*return*/];
320
- }
321
- });
322
- }); });
323
- }
324
- if (options.skipSuccessfulRequests) {
325
- response.on('finish', function () { return __awaiter(void 0, void 0, void 0, function () {
326
- return __generator(this, function (_a) {
327
- switch (_a.label) {
328
- case 0:
329
- if (!options.requestWasSuccessful(request, response)) return [3 /*break*/, 2];
330
- return [4 /*yield*/, decrementKey_1()];
331
- case 1:
332
- _a.sent();
333
- _a.label = 2;
334
- case 2: return [2 /*return*/];
335
- }
336
- });
337
- }); });
338
- }
339
- }
340
- // Call the {@link Options.onLimitReached} callback on
341
- // the first request where client exceeds their rate limit.
342
- if (maxHits && totalHits === maxHits + 1) {
343
- options.onLimitReached(request, response, options);
344
- }
345
- // If the client has exceeded their rate limit, set the Retry-After
346
- // header and call the {@link Options.handler} function
347
- if (maxHits && totalHits > maxHits) {
348
- if ((options.legacyHeaders || options.standardHeaders) &&
349
- !response.headersSent) {
350
- response.setHeader('Retry-After', Math.ceil(options.windowMs / 1000));
351
- }
352
- options.handler(request, response, next, options);
353
- return [2 /*return*/];
354
- }
355
- next();
356
- return [2 /*return*/];
357
- }
358
- });
359
- }); });
360
- middleware.resetKey =
361
- options.store.resetKey.bind(options.store);
362
- return middleware;
363
- };
364
- // Export it to the world!
365
- exports["default"] = rateLimit;
366
- //# sourceMappingURL=lib.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"lib.js","sourceRoot":"","sources":["../../source/lib.ts"],"names":[],"mappings":";AAAA,iBAAiB;AACjB,iDAAiD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYjD,sEAA2C;AAE3C;;;;;;GAMG;AACH,IAAM,aAAa,GAAG,UAAC,KAA0B;IAChD,+EAA+E;IAC/E,mDAAmD;IACnD,OAAA,OAAQ,KAAa,CAAC,IAAI,KAAK,UAAU;QACzC,OAAQ,KAAa,CAAC,SAAS,KAAK,UAAU;AAD9C,CAC8C,CAAA;AAE/C;;;;;;GAMG;AACH,IAAM,cAAc,GAAG,UAAC,WAAgC;IACvD,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;QAChC,sCAAsC;QACtC,OAAO,WAAW,CAAA;KAClB;IAED,wCAAwC;IACxC,IAAM,WAAW,GAAG,WAAW,CAAA;IAE/B,qCAAqC;IACrC;QAAA;QA6BA,CAAC;QA5BM,oCAAS,GAAf,UAAgB,GAAW;;;oBAC1B,sBAAO,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;4BAClC,WAAW,CAAC,IAAI,CACf,GAAG,EACH,UACC,KAAwB,EACxB,SAAiB,EACjB,SAA2B;gCAE3B,IAAI,KAAK;oCAAE,MAAM,CAAC,KAAK,CAAC,CAAA;gCACxB,OAAO,CAAC,EAAE,SAAS,WAAA,EAAE,S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OAAO,EAAE,QAAQ,CAAC,EAA/C,wBAA+C;4CAClD,qBAAM,cAAY,EAAE,EAAA;;4CAApB,SAAoB,CAAA;;;;;iCACrB,CAAC,CAAA;yBACF;qBACD;oBAED,sDAAsD;oBACtD,2DAA2D;oBAC3D,IAAI,OAAO,IAAI,SAAS,KAAK,OAAO,GAAG,CAAC,EAAE;wBACzC,OAAO,CAAC,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAA;qBAClD;oBAED,mEAAmE;oBACnE,uDAAuD;oBACvD,IAAI,OAAO,IAAI,SAAS,GAAG,OAAO,EAAE;wBACnC,IACC,CAAC,OAAO,CAAC,aAAa,IAAI,OAAO,CAAC,eAAe,CAAC;4BAClD,CAAC,QAAQ,CAAC,WAAW,EACpB;4BACD,QAAQ,CAAC,SAAS,CAAC,aAAa,EAAE,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC,CAAA;yBACrE;wBAED,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;wBACjD,sBAAM;qBACN;oBAED,IAAI,EAAE,CAAA;;;;SACN,CACD,CAIA;IAAC,UAAsC,CAAC,QAAQ;QAChD,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAE3C,OAAO,UAAqC,CAAA;AAC7C,CAAC,CAAA;AAED,0BAA0B;AAC1B,qBAAe,SAAS,CAAA","sourcesContent":["// /source/lib.ts\n// The option parser and rate limiting middleware\n\nimport Express from 'express'\n\nimport {\n\tOptions,\n\tAugmentedRequest,\n\tRateLimitRequestHandler,\n\tLegacyStore,\n\tStore,\n\tIncrementResponse,\n} from './types.js'\nimport MemoryStore from './memory-store.js'\n\n/**\n * Type guard to check if a store is legacy store.\n *\n * @param store {LegacyStore | Store} - The store to check\n *\n * @return {boolean} - Whether the store is a legacy store\n */\nconst isLegacyStore = (store: LegacyStore | Store): store is LegacyStore =>\n\t// Check that `incr` exists but `increment` does not - store authors might want\n\t// to keep both around for backwards compatibility.\n\ttypeof (store as any).incr === 'function' &&\n\ttypeof (store as any).increment !== 'function'\n\n/**\n * Converts a legacy store to the promisified version.\n *\n * @param store {LegacyStore | Store} - The legacy store or even a modern store\n *\n * @returns {Store} - The promisified version of the store\n */\nconst promisifyStore = (passedStore: LegacyStore | Store): Store => {\n\tif (!isLegacyStore(passedStore)) {\n\t\t// It's not an old store, return as is\n\t\treturn passedStore\n\t}\n\n\t// Why can't Typescript understand this?\n\tconst legacyStore = passedStore\n\n\t// A promisified version of the store\n\tclass PromisifiedStore implements Store {\n\t\tasync increment(key: string): Promise<IncrementResponse> {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tlegacyStore.incr(\n\t\t\t\t\tkey,\n\t\t\t\t\t(\n\t\t\t\t\t\terror: Error | undefined,\n\t\t\t\t\t\ttotalHits: number,\n\t\t\t\t\t\tresetTime: Date | undefined,\n\t\t\t\t\t) => {\n\t\t\t\t\t\tif (error) reject(error)\n\t\t\t\t\t\tresolve({ totalHits, resetTime })\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t})\n\t\t}\n\n\t\tasync decrement(key: string): Promise<void> {\n\t\t\treturn Promise.resolve(legacyStore.decrement(key))\n\t\t}\n\n\t\tasync resetKey(key: string): Promise<void> {\n\t\t\treturn Promise.resolve(legacyStore.resetKey(key))\n\t\t}\n\n\t\tasync resetAll(): Promise<void> {\n\t\t\tif (typeof legacyStore.resetAll === 'function')\n\t\t\t\treturn Promise.resolve(legacyStore.resetAll())\n\t\t}\n\t}\n\n\treturn new PromisifiedStore()\n}\n\n/**\n * Adds the defaults for options the user has not specified.\n *\n * @param options {Options} - The options the user specifies\n *\n * @returns {Options} - A complete configuration object\n */\nconst parseOptions = (\n\tpassedOptions: Omit<Partial<Options>, 'store'> & {\n\t\tstore?: Store | LegacyStore\n\t},\n): Options => {\n\t// Now add the defaults for the other options\n\n\tconst options = {\n\t\twindowMs: 60 * 1000,\n\t\tstore: new MemoryStore(),\n\t\tmax: 5,\n\t\tmessage: 'Too many requests, please try again later.',\n\t\tstatusCode: 429,\n\t\tlegacyHeaders: passedOptions.headers ?? true,\n\t\tstandardHeaders: passedOptions.draft_polli_ratelimit_headers ?? false,\n\t\trequestPropertyName: 'rateLimit',\n\t\tskipFailedRequests: false,\n\t\tskipSuccessfulRequests: false,\n\t\trequestWasSuccessful: (\n\t\t\t_request: Express.Request,\n\t\t\tresponse: Express.Response,\n\t\t): boolean => response.statusCode < 400,\n\t\tskip: (_request: Express.Request, _response: Express.Response): boolean =>\n\t\t\tfalse,\n\t\tkeyGenerator: (\n\t\t\trequest: Express.Request,\n\t\t\t_response: Express.Response,\n\t\t): string => {\n\t\t\tif (!request.ip) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t'WARN | `express-rate-limit` | `request.ip` is undefined. You can avoid this by providing a custom `keyGenerator` function, but it may be indicative of a larger issue.',\n\t\t\t\t)\n\t\t\t}\n\n\t\t\treturn request.ip\n\t\t},\n\t\thandler: (\n\t\t\t_request: Express.Request,\n\t\t\tresponse: Express.Response,\n\t\t\t_next: Express.NextFunction,\n\t\t\t_optionsUsed: Options,\n\t\t): void => {\n\t\t\tresponse.status(options.statusCode).send(options.message)\n\t\t},\n\t\tonLimitReached: (\n\t\t\t_request: Express.Request,\n\t\t\t_response: Express.Response,\n\t\t\t_optionsUsed: Options,\n\t\t): void => {},\n\t\t...passedOptions,\n\t}\n\n\t// Ensure that the store passed implements the either the `Store` or `LegacyStore`\n\t// interface\n\tif (\n\t\t(typeof (options.store as LegacyStore).incr !== 'function' &&\n\t\t\ttypeof (options.store as Store).increment !== 'function') ||\n\t\ttypeof options.store.decrement !== 'function' ||\n\t\ttypeof options.store.resetKey !== 'function' ||\n\t\t(typeof options.store.resetAll !== 'undefined' &&\n\t\t\ttypeof options.store.resetAll !== 'function') ||\n\t\t(typeof (options.store as Store).init !== 'undefined' &&\n\t\t\ttypeof (options.store as Store).init !== 'function')\n\t) {\n\t\tthrow new TypeError(\n\t\t\t'An invalid store was passed. Please ensure that the store is a class that implements the `Store` interface.',\n\t\t)\n\t}\n\n\t// Promisify the store, if it is not already\n\toptions.store = promisifyStore(options.store)\n\n\t// Return the 'clean' options\n\treturn options as Options\n}\n\n/**\n * Just pass on any errors for the developer to handle, usually as a HTTP 500\n * Internal Server Error.\n *\n * @param fn {Express.RequestHandler} - The request handler for which to handle errors\n *\n * @returns {Express.RequestHandler} - The request handler wrapped with a `.catch` clause\n *\n * @private\n */\nconst handleAsyncErrors =\n\t(fn: Express.RequestHandler): Express.RequestHandler =>\n\tasync (\n\t\trequest: Express.Request,\n\t\tresponse: Express.Response,\n\t\tnext: Express.NextFunction,\n\t) => {\n\t\ttry {\n\t\t\tawait Promise.resolve(fn(request, response, next)).catch(next)\n\t\t} catch (error: unknown) {\n\t\t\tnext(error)\n\t\t}\n\t}\n\n/**\n *\n * Create an instance of IP rate-limiting middleware for Express.\n *\n * @param passedOptions {Options} - Options to configure the rate limiter\n *\n * @returns {RateLimitRequestHandler} - The middleware that rate-limits clients based on your configuration\n *\n * @public\n */\nconst rateLimit = (\n\tpassedOptions?: Omit<Partial<Options>, 'store'> & {\n\t\tstore?: Store | LegacyStore\n\t},\n): RateLimitRequestHandler => {\n\t// Parse the options and add the default values for unspecified options\n\tconst options = parseOptions(passedOptions ?? {})\n\t// Call the `init` method on the store, if it exists\n\tif (typeof options.store.init === 'function') options.store.init(options)\n\n\t// Then return the actual middleware\n\tconst middleware = handleAsyncErrors(\n\t\tasync (\n\t\t\trequest: Express.Request,\n\t\t\tresponse: Express.Response,\n\t\t\tnext: Express.NextFunction,\n\t\t) => {\n\t\t\t// First check if we should skip the request\n\t\t\tconst skip = await options.skip(request, response)\n\t\t\tif (skip) {\n\t\t\t\tnext()\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\t// Create an augmented request\n\t\t\tconst augmentedRequest = request as AugmentedRequest\n\n\t\t\t// Get a unique key for the client\n\t\t\tconst key = await options.keyGenerator(request, response)\n\t\t\t// Increment the client's hit counter by one\n\t\t\tconst { totalHits, resetTime } = await options.store.increment(key)\n\n\t\t\t// Get the quota (max number of hits) for each client\n\t\t\tconst retrieveQuota =\n\t\t\t\ttypeof options.max === 'function'\n\t\t\t\t\t? options.max(request, response)\n\t\t\t\t\t: options.max\n\n\t\t\tconst maxHits = await retrieveQuota\n\t\t\t// Set the rate limit information on the augmented request object\n\t\t\taugmentedRequest[options.requestPropertyName] = {\n\t\t\t\tlimit: maxHits,\n\t\t\t\tcurrent: totalHits,\n\t\t\t\tremaining: Math.max(maxHits - totalHits, 0),\n\t\t\t\tresetTime,\n\t\t\t}\n\n\t\t\t// Set the X-RateLimit headers on the response object if enabled\n\t\t\tif (options.legacyHeaders && !response.headersSent) {\n\t\t\t\tresponse.setHeader('X-RateLimit-Limit', maxHits)\n\t\t\t\tresponse.setHeader(\n\t\t\t\t\t'X-RateLimit-Remaining',\n\t\t\t\t\taugmentedRequest[options.requestPropertyName].remaining,\n\t\t\t\t)\n\n\t\t\t\t// If we have a resetTime, also provide the current date to help avoid issues with incorrect clocks\n\t\t\t\tif (resetTime instanceof Date) {\n\t\t\t\t\tresponse.setHeader('Date', new Date().toUTCString())\n\t\t\t\t\tresponse.setHeader(\n\t\t\t\t\t\t'X-RateLimit-Reset',\n\t\t\t\t\t\tMath.ceil(resetTime.getTime() / 1000),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Set the standardized RateLimit headers on the response object\n\t\t\t// if enabled\n\t\t\tif (options.standardHeaders && !response.headersSent) {\n\t\t\t\tresponse.setHeader('RateLimit-Limit', maxHits)\n\t\t\t\tresponse.setHeader(\n\t\t\t\t\t'RateLimit-Remaining',\n\t\t\t\t\taugmentedRequest[options.requestPropertyName].remaining,\n\t\t\t\t)\n\n\t\t\t\tif (resetTime) {\n\t\t\t\t\tconst deltaSeconds = Math.ceil(\n\t\t\t\t\t\t(resetTime.getTime() - Date.now()) / 1000,\n\t\t\t\t\t)\n\t\t\t\t\tresponse.setHeader('RateLimit-Reset', Math.max(0, deltaSeconds))\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// If we are to skip failed/successfull requests, decrement the\n\t\t\t// counter accordingly once we know the status code of the request\n\t\t\tif (options.skipFailedRequests || options.skipSuccessfulRequests) {\n\t\t\t\tlet decremented = false\n\t\t\t\tconst decrementKey = async () => {\n\t\t\t\t\tif (!decremented) {\n\t\t\t\t\t\tawait options.store.decrement(key)\n\t\t\t\t\t\tdecremented = true\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (options.skipFailedRequests) {\n\t\t\t\t\tresponse.on('finish', async () => {\n\t\t\t\t\t\tif (!options.requestWasSuccessful(request, response))\n\t\t\t\t\t\t\tawait decrementKey()\n\t\t\t\t\t})\n\t\t\t\t\tresponse.on('close', async () => {\n\t\t\t\t\t\tif (!response.writableEnded) await decrementKey()\n\t\t\t\t\t})\n\t\t\t\t\tresponse.on('error', async () => {\n\t\t\t\t\t\tawait decrementKey()\n\t\t\t\t\t})\n\t\t\t\t}\n\n\t\t\t\tif (options.skipSuccessfulRequests) {\n\t\t\t\t\tresponse.on('finish', async () => {\n\t\t\t\t\t\tif (options.requestWasSuccessful(request, response))\n\t\t\t\t\t\t\tawait decrementKey()\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Call the {@link Options.onLimitReached} callback on\n\t\t\t// the first request where client exceeds their rate limit.\n\t\t\tif (maxHits && totalHits === maxHits + 1) {\n\t\t\t\toptions.onLimitReached(request, response, options)\n\t\t\t}\n\n\t\t\t// If the client has exceeded their rate limit, set the Retry-After\n\t\t\t// header and call the {@link Options.handler} function\n\t\t\tif (maxHits && totalHits > maxHits) {\n\t\t\t\tif (\n\t\t\t\t\t(options.legacyHeaders || options.standardHeaders) &&\n\t\t\t\t\t!response.headersSent\n\t\t\t\t) {\n\t\t\t\t\tresponse.setHeader('Retry-After', Math.ceil(options.windowMs / 1000))\n\t\t\t\t}\n\n\t\t\t\toptions.handler(request, response, next, options)\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tnext()\n\t\t},\n\t)\n\n\t// Export the store's function to reset the hit counter for a particular\n\t// client based on their identifier\n\t;(middleware as RateLimitRequestHandler).resetKey =\n\t\toptions.store.resetKey.bind(options.store)\n\n\treturn middleware as RateLimitRequestHandler\n}\n\n// Export it to the world!\nexport default rateLimit\n"]}
@@ -1,61 +0,0 @@
1
- import { Store, Options, IncrementResponse } from './types.js';
2
- /**
3
- * A {@link Store} that stores the hit count for each client in
4
- * memory.
5
- *
6
- * @public
7
- */
8
- export default class MemoryStore implements Store {
9
- /**
10
- * The duration of time before which all hit counts are reset (in milliseconds).
11
- */
12
- windowMs: number;
13
- /**
14
- * The map that stores the number of hits for each client in memory.
15
- */
16
- hits: {
17
- [key: string]: number | undefined;
18
- };
19
- /**
20
- * The time at which all hit counts will be reset.
21
- */
22
- resetTime: Date;
23
- /**
24
- * Method that initializes the store.
25
- *
26
- * @param options {Options} - The options used to setup the middleware
27
- */
28
- init(options: Options): void;
29
- /**
30
- * Method to increment a client's hit counter.
31
- *
32
- * @param key {string} - The identifier for a client
33
- *
34
- * @returns {IncrementResponse} - The number of hits and reset time for that client
35
- *
36
- * @public
37
- */
38
- increment(key: string): Promise<IncrementResponse>;
39
- /**
40
- * Method to decrement a client's hit counter.
41
- *
42
- * @param key {string} - The identifier for a client
43
- *
44
- * @public
45
- */
46
- decrement(key: string): Promise<void>;
47
- /**
48
- * Method to reset a client's hit counter.
49
- *
50
- * @param key {string} - The identifier for a client
51
- *
52
- * @public
53
- */
54
- resetKey(key: string): Promise<void>;
55
- /**
56
- * Method to reset everyone's hit counter.
57
- *
58
- * @public
59
- */
60
- resetAll(): Promise<void>;
61
- }