From e3bac552123e2cf79f33950bab0aaabb622b63ed Mon Sep 17 00:00:00 2001 From: Ifiok Idiang Date: Mon, 14 May 2018 20:52:19 +0100 Subject: [PATCH 1/5] Update body_parser.ts Updated conditionals from the switch to if statement to use includes for validating the content-type header. --- src/lib/body_parser.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib/body_parser.ts b/src/lib/body_parser.ts index 1045aee..d81ed1a 100644 --- a/src/lib/body_parser.ts +++ b/src/lib/body_parser.ts @@ -10,17 +10,14 @@ export default function(req, res, next) { if (!contentType) { return next() } - switch (contentType) { - case 'application/x-www-form-urlencoded': - req.body = qs.parse(req.rawBody) - break - case 'application/json': - try { - req.body = JSON.parse(req.rawBody) - } catch (e) { - return next(new InvalidContentError('Invalid JSON: ' + e.message)) - } - break + if (contentType.includes('application/x-www-form-urlencoded')) { + req.body = qs.parse(req.rawBody) + } else if (contentType.includes('application/json')) { + try { + req.body = JSON.parse(req.rawBody) + } catch (e) { + return next(new InvalidContentError('Invalid JSON: ' + e.message)) + } } next() -} \ No newline at end of file +} From 781e3f9e9c0d9d7d10c02bc2733acb3d2bd13bb2 Mon Sep 17 00:00:00 2001 From: Ifiok Idiang Date: Thu, 7 Jun 2018 06:36:13 +0100 Subject: [PATCH 2/5] used regex for content type matching for extended json types --- src/lib/body_parser.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/lib/body_parser.ts b/src/lib/body_parser.ts index d81ed1a..201960d 100644 --- a/src/lib/body_parser.ts +++ b/src/lib/body_parser.ts @@ -6,18 +6,27 @@ export default function(req, res, next) { if (!req.body) { return next() } - const contentType = req.header('content-type') + let contentType = req.header('content-type') if (!contentType) { return next() } - if (contentType.includes('application/x-www-form-urlencoded')) { - req.body = qs.parse(req.rawBody) - } else if (contentType.includes('application/json')) { - try { - req.body = JSON.parse(req.rawBody) - } catch (e) { - return next(new InvalidContentError('Invalid JSON: ' + e.message)) - } + var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); + // map any +json to application/json + if (jsonPatternMatcher.test(contentType)) { + contentType = 'application/json'; + } + + switch (contentType) { + case 'application/x-www-form-urlencoded': + req.body = qs.parse(req.rawBody) + break + case 'application/json': + try { + req.body = JSON.parse(req.rawBody) + } catch (e) { + return next(new InvalidContentError('Invalid JSON: ' + e.message)) + } + break } next() } From 83cb668e7abc2dd178046a36add975058d7e3291 Mon Sep 17 00:00:00 2001 From: Ifiok Idiang Date: Thu, 7 Jun 2018 06:53:59 +0100 Subject: [PATCH 3/5] upated package script s to use local installs of mocha --- package.json | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 0f856b5..d7333b9 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "scripts": { "prepublish": "npm run build", "build": "tsc", - "test": "mocha -r ts-node/register src/**/*.spec.ts", - "testwatch": "mocha -r ts-node/register -w --watch-extensions ts src/**/*.spec.ts", + "test": "./node_modules/mocha/bin/mocha -r ts-node/register src/**/*.spec.ts", + "testwatch": "./node_modules/mocha/bin/mocha -r ts-node/register -w --watch-extensions ts src/**/*.spec.ts", "lint": "tslint src/**/*.ts" }, "repository": { @@ -26,6 +26,9 @@ "serverless" ], "author": "Krishna Kant Sharma ", + "contributors": [ + "Ifiok Idiang " + ], "license": "MIT", "bugs": { "url": "https://github.com/kksharma1618/lambda-restify/issues" From b307ef3c29209feca62f797aa085ec4d99e9ed6f Mon Sep 17 00:00:00 2001 From: Ifiok Idiang Date: Thu, 7 Jun 2018 07:05:02 +0100 Subject: [PATCH 4/5] fixed lint errors --- src/lib/body_parser.ts | 2 +- src/lib/router.ts | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lib/body_parser.ts b/src/lib/body_parser.ts index 201960d..33d922a 100644 --- a/src/lib/body_parser.ts +++ b/src/lib/body_parser.ts @@ -10,7 +10,7 @@ export default function(req, res, next) { if (!contentType) { return next() } - var jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); + const jsonPatternMatcher = new RegExp('^application/[a-zA-Z.]+\\+json'); // map any +json to application/json if (jsonPatternMatcher.test(contentType)) { contentType = 'application/json'; diff --git a/src/lib/router.ts b/src/lib/router.ts index f58dc8a..c6bc346 100644 --- a/src/lib/router.ts +++ b/src/lib/router.ts @@ -156,9 +156,9 @@ export default class Router extends EventEmitter { return } - for (let i = 0; i < routes.length; i++) { + for (const route of routes) { try { - params = matchURL(routes[i].path, req.path()) + params = matchURL(route.path, req.path()) // console.log('p', params, req.path()) } catch (e) { this.log.trace({ err: e }, 'error parsing URL') @@ -170,12 +170,12 @@ export default class Router extends EventEmitter { continue } - reverse = this.reverse[routes[i].path.source] + reverse = this.reverse[route.path.source] - if (routes[i].types.length && req.isUpload()) { + if (route.types.length && req.isUpload()) { candidates.push({ p: params, - r: routes[i] + r: route }) typed = true continue @@ -185,15 +185,15 @@ export default class Router extends EventEmitter { // not the first one. However, if neither the client nor // server specified any version, we're done, because neither // cared - if (routes[i].versions.length === 0 && req.version() === '*') { - r = routes[i] + if (route.versions.length === 0 && req.version() === '*') { + r = route break } - if (routes[i].versions.length > 0) { + if (route.versions.length > 0) { candidates.push({ p: params, - r: routes[i] + r: route }) versioned = true } From 7091e9d8429d08b2dd5be0760787e33b57e0ccf8 Mon Sep 17 00:00:00 2001 From: Ifiok Idiang Date: Thu, 7 Jun 2018 07:05:45 +0100 Subject: [PATCH 5/5] added tslint as local dev dependency and referenced it locally --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d7333b9..d7b70cc 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "build": "tsc", "test": "./node_modules/mocha/bin/mocha -r ts-node/register src/**/*.spec.ts", "testwatch": "./node_modules/mocha/bin/mocha -r ts-node/register -w --watch-extensions ts src/**/*.spec.ts", - "lint": "tslint src/**/*.ts" + "lint": "./node_modules/tslint/bin/tslint src/**/*.ts" }, "repository": { "type": "git", @@ -49,6 +49,7 @@ "nodemon": "^1.11.0", "npm-run-series": "^1.0.0", "ts-node": "^3.0.4", + "tslint": "^5.10.0", "typescript": "^2.3.2" }, "dependencies": {