Skip to content

Commit 844d05c

Browse files
author
Guilherme Oenning
committed
refactor middlewares
1 parent 338fc0f commit 844d05c

File tree

4 files changed

+9
-31
lines changed

4 files changed

+9
-31
lines changed

lib/middlewares/express.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ module.exports = {
66
},
77
mainMiddleware: function(f, handleRequest) {
88
return function(req, res, next) {
9-
var enabled = f(req, res);
10-
11-
var respondWith = (res, result) => {
12-
res.writeHead(result.status, { 'Content-Type': result.type });
13-
res.end(result.body);
14-
};
15-
16-
handleRequest(enabled, req, res, respondWith).then((handled) => {
9+
handleRequest(f, req, res).then((handled) => {
1710
if (!handled)
1811
next();
1912
}).catch(next);

lib/middlewares/hapi.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
var onHapiRequest = function(f, handleRequest, request, reply) {
2-
var enabled = f(request.raw.req, request.raw.res);
3-
4-
var respondWith = (res, result) => {
5-
reply(result.body).type(result.type).code(result.status);
6-
};
7-
8-
handleRequest(enabled, request.raw.req, request.raw.res, respondWith).then((handled) => {
2+
handleRequest(f, request.raw.req, request.raw.res).then((handled) => {
93
if (!handled)
104
reply.continue();
115
});

lib/middlewares/koa.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ module.exports = {
99
},
1010
mainMiddleware: function(f, handleRequest) {
1111
return function *(next) {
12-
var enabled = f(this.req, this.res);
13-
14-
var respondWith = (res, result) => {
15-
this.status = result.status;
16-
this.type = result.type;
17-
this.body = result.body;
18-
};
19-
20-
var handled = yield handleRequest(enabled, this.req, this.res, respondWith);
12+
var handled = yield handleRequest(f, this.req, this.res);
2113
if (!handled)
2214
yield next;
2315
};

lib/miniprofiler.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ function getPath(req) {
7979
return url.parse(req.url).pathname;
8080
}
8181

82-
function handleRequest(enabled, req, res, respondWith) {
82+
function handleRequest(f, req, res) {
8383
return new Promise((resolve, reject) => {
84+
var enabled = f(req, res);
8485
var reqPath = getPath(req);
8586

8687
if(!reqPath.startsWith(resourcePath)){
@@ -95,18 +96,16 @@ function handleRequest(enabled, req, res, respondWith) {
9596
}
9697

9798
if (!enabled) {
98-
respondWith(res, {
99-
type: 'text/plain; charset=utf-8',
100-
status: 404,
101-
body: 'MiniProfiler is disabled'
102-
});
99+
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
100+
res.end('MiniProfiler is disabled');
103101
resolve(true);
104102
} else {
105103
var segments = _.compact(reqPath.split('/'));
106104
var lastPathSegment = segments[segments.length - 1];
107105
var handler = (lastPathSegment == 'results') ? results : static;
108106
handler(req, res, lastPathSegment, (result) => {
109-
respondWith(res, result);
107+
res.writeHead(result.status, { 'Content-Type': result.type });
108+
res.end(result.body);
110109
resolve(true);
111110
});
112111
}

0 commit comments

Comments
 (0)