From 844d05c2f38b39524ff2786a6e8945f778d49adf Mon Sep 17 00:00:00 2001 From: Guilherme Oenning Date: Wed, 1 Jun 2016 06:59:16 -0300 Subject: [PATCH] refactor middlewares --- lib/middlewares/express.js | 9 +-------- lib/middlewares/hapi.js | 8 +------- lib/middlewares/koa.js | 10 +--------- lib/miniprofiler.js | 13 ++++++------- 4 files changed, 9 insertions(+), 31 deletions(-) diff --git a/lib/middlewares/express.js b/lib/middlewares/express.js index 4227768..34bfab5 100644 --- a/lib/middlewares/express.js +++ b/lib/middlewares/express.js @@ -6,14 +6,7 @@ module.exports = { }, mainMiddleware: function(f, handleRequest) { return function(req, res, next) { - var enabled = f(req, res); - - var respondWith = (res, result) => { - res.writeHead(result.status, { 'Content-Type': result.type }); - res.end(result.body); - }; - - handleRequest(enabled, req, res, respondWith).then((handled) => { + handleRequest(f, req, res).then((handled) => { if (!handled) next(); }).catch(next); diff --git a/lib/middlewares/hapi.js b/lib/middlewares/hapi.js index f5585bf..b2904c0 100644 --- a/lib/middlewares/hapi.js +++ b/lib/middlewares/hapi.js @@ -1,11 +1,5 @@ var onHapiRequest = function(f, handleRequest, request, reply) { - var enabled = f(request.raw.req, request.raw.res); - - var respondWith = (res, result) => { - reply(result.body).type(result.type).code(result.status); - }; - - handleRequest(enabled, request.raw.req, request.raw.res, respondWith).then((handled) => { + handleRequest(f, request.raw.req, request.raw.res).then((handled) => { if (!handled) reply.continue(); }); diff --git a/lib/middlewares/koa.js b/lib/middlewares/koa.js index c32a8bd..0adbc70 100644 --- a/lib/middlewares/koa.js +++ b/lib/middlewares/koa.js @@ -9,15 +9,7 @@ module.exports = { }, mainMiddleware: function(f, handleRequest) { return function *(next) { - var enabled = f(this.req, this.res); - - var respondWith = (res, result) => { - this.status = result.status; - this.type = result.type; - this.body = result.body; - }; - - var handled = yield handleRequest(enabled, this.req, this.res, respondWith); + var handled = yield handleRequest(f, this.req, this.res); if (!handled) yield next; }; diff --git a/lib/miniprofiler.js b/lib/miniprofiler.js index 469d954..cb59251 100644 --- a/lib/miniprofiler.js +++ b/lib/miniprofiler.js @@ -79,8 +79,9 @@ function getPath(req) { return url.parse(req.url).pathname; } -function handleRequest(enabled, req, res, respondWith) { +function handleRequest(f, req, res) { return new Promise((resolve, reject) => { + var enabled = f(req, res); var reqPath = getPath(req); if(!reqPath.startsWith(resourcePath)){ @@ -95,18 +96,16 @@ function handleRequest(enabled, req, res, respondWith) { } if (!enabled) { - respondWith(res, { - type: 'text/plain; charset=utf-8', - status: 404, - body: 'MiniProfiler is disabled' - }); + res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }); + res.end('MiniProfiler is disabled'); resolve(true); } else { var segments = _.compact(reqPath.split('/')); var lastPathSegment = segments[segments.length - 1]; var handler = (lastPathSegment == 'results') ? results : static; handler(req, res, lastPathSegment, (result) => { - respondWith(res, result); + res.writeHead(result.status, { 'Content-Type': result.type }); + res.end(result.body); resolve(true); }); }