Skip to content

Commit 6351e4c

Browse files
committed
fix: bumped deps, added ability to whitelist globs for rate limiting (e.g. /report)
1 parent 7d1e892 commit 6351e4c

File tree

3 files changed

+733
-549
lines changed

3 files changed

+733
-549
lines changed

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const etag = require('koa-etag');
1919
const json = require('koa-json');
2020
const koa404Handler = require('koa-404-handler');
2121
const koaConnect = require('koa-connect');
22+
const multimatch = require('multimatch');
2223
const proxyWrap = require('findhit-proxywrap');
2324
const removeTrailingSlashes = require('koa-no-trailing-slash');
2425
const requestId = require('express-request-id');
@@ -35,6 +36,7 @@ class API {
3536
constructor(config) {
3637
this.config = {
3738
...sharedConfig('API'),
39+
rateLimitIgnoredGlobs: [],
3840
...config
3941
};
4042

@@ -100,6 +102,24 @@ class API {
100102
if (this.config.auth) app.use(auth(this.config.auth));
101103

102104
// rate limiting
105+
if (this.config.rateLimit) {
106+
app.use((ctx, next) => {
107+
// check against ignored/whitelisted paths
108+
if (
109+
Array.isArray(this.config.rateLimitIgnoredGlobs) &&
110+
this.config.rateLimitIgnoredGlobs.length > 0
111+
) {
112+
const match = multimatch(ctx.path, this.config.rateLimitIgnoredGlobs);
113+
if (Array.isArray(match) && match.length > 0) return next();
114+
}
115+
116+
return ratelimit({
117+
...this.config.rateLimit,
118+
db: client
119+
})(ctx, next);
120+
});
121+
}
122+
103123
if (this.config.rateLimit)
104124
app.use(
105125
ratelimit({

package.json

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
2121
],
2222
"dependencies": {
23-
"@koa/router": "^9.4.0",
24-
"@ladjs/i18n": "^6.0.5",
23+
"@koa/router": "^10.0.0",
24+
"@ladjs/i18n": "^7.0.1",
2525
"@ladjs/redis": "^1.0.7",
26-
"@ladjs/shared-config": "^3.0.9",
26+
"@ladjs/shared-config": "^3.0.10",
2727
"@ladjs/store-ip-address": "^0.0.7",
2828
"boolean": "3.0.1",
29-
"cabin": "^8.0.7",
29+
"cabin": "^9.0.4",
3030
"express-request-id": "^1.4.1",
3131
"findhit-proxywrap": "^0.3.12",
3232
"kcors": "^2.2.2",
33-
"koa": "^2.13.0",
33+
"koa": "^2.13.1",
3434
"koa-404-handler": "^0.0.2",
3535
"koa-basic-auth": "^4.0.0",
3636
"koa-better-error-handler": "^6.0.1",
@@ -42,28 +42,29 @@
4242
"koa-etag": "^4.0.0",
4343
"koa-json": "^2.0.2",
4444
"koa-no-trailing-slash": "^2.1.0",
45-
"koa-simple-ratelimit": "^5.0.1",
45+
"koa-simple-ratelimit": "^5.1.0",
4646
"lodash": "^4.17.20",
47+
"multimatch": "^5.0.0",
4748
"request-received": "^0.0.3",
4849
"response-time": "^2.3.2"
4950
},
5051
"devDependencies": {
5152
"@commitlint/cli": "^11.0.0",
5253
"@commitlint/config-conventional": "^11.0.0",
53-
"ava": "^3.13.0",
54-
"codecov": "^3.8.0",
55-
"cross-env": "^7.0.2",
56-
"eslint": "^7.12.0",
57-
"eslint-config-xo-lass": "^1.0.4",
58-
"fixpack": "^3.0.6",
59-
"husky": "^4.3.0",
60-
"lint-staged": "10.4.2",
61-
"mongoose": "^5.10.10",
54+
"ava": "^3.15.0",
55+
"codecov": "^3.8.1",
56+
"cross-env": "^7.0.3",
57+
"eslint": "^7.19.0",
58+
"eslint-config-xo-lass": "^1.0.5",
59+
"fixpack": "^4.0.0",
60+
"husky": "^4.3.8",
61+
"lint-staged": "10.5.3",
62+
"mongoose": "^5.11.14",
6263
"nyc": "^15.1.0",
6364
"remark-cli": "^9.0.0",
64-
"remark-preset-github": "^3.0.4",
65-
"supertest": "^5.0.0",
66-
"xo": "^0.34.1"
65+
"remark-preset-github": "^4.0.1",
66+
"supertest": "^6.1.3",
67+
"xo": "^0.37.1"
6768
},
6869
"engines": {
6970
"node": ">=8.3"

0 commit comments

Comments
 (0)