Skip to content

Commit f00c995

Browse files
committed
draft for koa supports: needs to remove dup, fix results response
1 parent 322b1c2 commit f00c995

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

examples/koa.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var koa = require('koa');
2+
var app = koa();
3+
var miniprofiler = require('../lib/miniprofiler.js')
4+
5+
app.use(miniprofiler.koa(() => {return false;}));
6+
7+
app.use(function *(){
8+
this.body = '<html><body>hello' + this.req.miniprofiler.include() + '</body></html>';
9+
});
10+
11+
app.listen(8080);

lib/middlewares/koa.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = function(f, profiler) {
2+
if(!f) f = function() { return true; };
3+
return function *(next) {
4+
profiler.configure();
5+
var enabled = f(this.req, this.res);
6+
7+
if (this.path.startsWith(profiler.resourcePath)) {
8+
if (!enabled) {
9+
this.type = 'text';
10+
this.status = 404;
11+
this.body = 'MiniProfiler is disabled';
12+
return;
13+
}
14+
15+
var sp = this.path.split('/');
16+
var reqPath = sp[sp.length - 1];
17+
if(reqPath == 'results')
18+
profiler.results(this.req, this.res);
19+
else
20+
profiler.static(reqPath, this.res);
21+
return;
22+
}
23+
var id = profiler.startProfiling(this.req, enabled);
24+
25+
if (enabled) {
26+
var request = this.req
27+
this.res.on('finish', function() {
28+
profiler.stopProfiling(request);
29+
});
30+
this.res.setHeader('X-MiniProfiler-Ids', `["${id}"]`);
31+
}
32+
33+
yield next;
34+
};
35+
};

lib/miniprofiler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ exports.configure = configure;
3737
exports.express = function(f) {
3838
return require('./middlewares/express.js')(f, shared());
3939
};
40+
exports.koa = function(f) {
41+
return require('./middlewares/koa.js')(f, shared());
42+
};
4043

4144
exports.for = {
4245
pg: require('./providers/miniprofiler.pg.js'),

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"eslint": "^2.10.2",
3131
"express": "^4.13.4",
3232
"istanbul": "^0.4.3",
33+
"koa": "^1.2.0",
3334
"mocha": "^2.4.5",
3435
"pg": "^4.5.5",
3536
"pug": "^2.0.0-alpha7",

0 commit comments

Comments
 (0)