Skip to content

Commit 06f3fc8

Browse files
chore: lift json regex to top of file (#1670)
1 parent fb8b7fc commit 06f3fc8

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

lib/plugins/bodyParser.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var jsonParser = require('./jsonBodyParser');
1010
var formParser = require('./formBodyParser');
1111
var multipartParser = require('./multipartBodyParser');
1212
var fieldedTextParser = require('./fieldedTextBodyParser.js');
13+
var regex = require('./utils/regex');
1314

1415
///--- Globals
1516

@@ -162,12 +163,6 @@ function bodyParser(options) {
162163
var parser;
163164
var type = req.contentType().toLowerCase();
164165

165-
var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');
166-
// map any +json to application/json
167-
if (jsonPatternMatcher.test(type)) {
168-
type = 'application/json';
169-
}
170-
171166
switch (type) {
172167
case 'application/json':
173168
parser = parseJson[0];
@@ -192,6 +187,18 @@ function bodyParser(options) {
192187
break;
193188
}
194189

190+
// if we find no matches from the direct string comparisons, perform
191+
// more expensive regex matches. map any +json to application/json.
192+
// theoretically these could be mapped to application/json prior to the
193+
// switch statement, but putting it here allows us to skip the regex
194+
// entirely unless absolutely necessary. additional types could be
195+
// added later at some point.
196+
if (!parser) {
197+
if (regex.jsonContentType.test(type)) {
198+
parser = parseJson[0];
199+
}
200+
}
201+
195202
if (parser) {
196203
parser(req, res, next);
197204
} else if (opts && opts.rejectUnknown) {

lib/plugins/jsonBodyParser.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var assert = require('assert-plus');
66
var errors = require('restify-errors');
77

88
var bodyReader = require('./bodyReader');
9+
var regex = require('./utils/regex');
910

1011
///--- API
1112

@@ -28,13 +29,16 @@ function jsonBodyParser(options) {
2829
// save original body on req.rawBody and req._body
2930
req.rawBody = req._body = req.body;
3031

31-
var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json');
3232
var contentType = req.getContentType();
3333

34-
if (contentType !== 'application/json' || !req.body) {
35-
if (!jsonPatternMatcher.test(contentType)) {
36-
return next();
37-
}
34+
// check for empty body first, don't pay regex tax unless necessary.
35+
// for content type, check for exact match and any of the *+json types
36+
if (
37+
!req.body ||
38+
(contentType !== 'application/json' &&
39+
!regex.jsonContentType.test(contentType))
40+
) {
41+
return next();
3842
}
3943

4044
var params;

lib/plugins/utils/regex.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
jsonContentType: new RegExp('^application/[a-zA-Z.]+\\+json')
5+
};

0 commit comments

Comments
 (0)