Skip to content

Commit 187d6ae

Browse files
committed
feat: allow glob paths to be ignored from body parser
1 parent f282317 commit 187d6ae

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

index.js

Lines changed: 18 additions & 1 deletion
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 ratelimit = require('@ladjs/koa-simple-ratelimit');
2324
const removeTrailingSlashes = require('koa-no-trailing-slash');
2425
const requestId = require('express-request-id');
@@ -129,7 +130,23 @@ class API {
129130
}
130131

131132
// Body parser
132-
app.use(bodyParser());
133+
// POST /v1/logs (1 MB max so 1.1 MB w/overhead)
134+
// POST /v1/emails (50 MB max so 51 MB w/overhead)
135+
app.use((ctx, next) => {
136+
// check against ignored paths
137+
if (
138+
Array.isArray(this.config.bodyParserIgnoredPathGlobs) &&
139+
this.config.bodyParserIgnoredPathGlobs.length > 0
140+
) {
141+
const match = multimatch(
142+
ctx.path,
143+
this.config.bodyParserIgnoredPathGlobs
144+
);
145+
if (Array.isArray(match) && match.length > 0) return next();
146+
}
147+
148+
return bodyParser()(ctx, next);
149+
});
133150

134151
// Pretty-printed json responses
135152
app.use(json());

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"koa-json": "^2.0.2",
3535
"koa-no-trailing-slash": "^2.1.0",
3636
"lodash": "^4.17.21",
37+
"multimatch": "5",
3738
"request-received": "^0.0.3",
3839
"response-time": "^2.3.2"
3940
},

0 commit comments

Comments
 (0)