Skip to content

Commit 5a068ab

Browse files
committed
lots of duplicated code, but it's working for both express + koa
1 parent 888e544 commit 5a068ab

File tree

12 files changed

+312
-220
lines changed

12 files changed

+312
-220
lines changed

lib/middlewares/express.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ module.exports = function(f, profiler) {
66

77
if(req.path.startsWith(profiler.resourcePath)) {
88
if (!enabled) {
9-
res.setHeader('Content-Type', 'text/plain');
9+
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
1010
res.writeHead(404);
11-
res.end('MiniProfiler is disabled.');
11+
res.end('MiniProfiler is disabled');
1212
return;
1313
}
1414

1515
var sp = req.path.split('/');
1616
var reqPath = sp[sp.length - 1];
1717
if(reqPath == 'results')
18-
profiler.results(req, res);
18+
profiler.results(req, res, (result) => {
19+
res.setHeader('Content-Type', result.type);
20+
res.writeHead(result.status);
21+
res.end(result.body);
22+
});
1923
else
20-
profiler.static(reqPath, res);
24+
profiler.static(reqPath, res, (result) => {
25+
res.setHeader('Content-Type', result.type);
26+
res.writeHead(result.status);
27+
res.end(result.body);
28+
});
2129
return;
2230
}
2331
var id = profiler.startProfiling(req, enabled);

lib/middlewares/koa.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,46 @@ module.exports = function(f, profiler) {
66

77
if (this.path.startsWith(profiler.resourcePath)) {
88
if (!enabled) {
9-
this.type = 'text';
9+
this.type = 'text/plain; charset=utf-8';
1010
this.status = 404;
1111
this.body = 'MiniProfiler is disabled';
12-
return;
13-
}
12+
} else {
13+
var sp = this.path.split('/');
14+
var reqPath = sp[sp.length - 1];
15+
if (reqPath == 'results') {
16+
yield new Promise((resolve, reject) => {
17+
profiler.results(this.req, this.res, (result) => {
18+
this.type = result.type;
19+
this.status = result.status;
20+
this.body = result.body;
21+
resolve();
22+
});
23+
});
24+
}
25+
else {
26+
yield new Promise((resolve, reject) => {
27+
profiler.static(reqPath, this.res, (result) => {
28+
this.type = result.type;
29+
this.status = result.status;
30+
this.body = result.body;
31+
resolve();
32+
});
33+
});
34+
}
35+
}
36+
} else {
37+
this.req.path = this.request.path;
38+
var id = profiler.startProfiling(this.req, enabled);
1439

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);
40+
if (enabled) {
41+
var request = this.req;
42+
this.res.on('finish', function() {
43+
profiler.stopProfiling(request);
44+
});
45+
this.res.setHeader('X-MiniProfiler-Ids', `["${id}"]`);
46+
}
2447

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;
48+
yield next;
49+
}
3450
};
3551
};

lib/miniprofiler.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -84,68 +84,68 @@ var version = '';
8484
var contentTypes = {
8585
css: 'text/css',
8686
js: 'text/javascript',
87-
tmpl: 'text/html'
87+
tmpl: 'text/html; charset=utf-8'
8888
};
89-
function static(reqPath, res) {
90-
fs.readFile(path.join(includesDir, reqPath), function(err, data) {
89+
90+
function static(requestPath, res, done) {
91+
fs.readFile(path.join(includesDir, requestPath), 'utf-8', function(err, data) {
9192
if (err) {
9293
debug(err);
93-
res.setHeader('Content-Type', 'text/plain');
94-
res.writeHead(404);
95-
res.end('Resource unavailable.');
96-
return;
97-
}
98-
var rs = reqPath.split('.');
99-
res.setHeader('Content-Type', contentTypes[rs[rs.length - 1]]);
100-
res.writeHead(200);
101-
res.end(data);
94+
done({
95+
type: 'text/plain; charset=utf-8',
96+
status: 404,
97+
body: 'Resource unavailable.'
98+
});
99+
} else {
100+
var rs = requestPath.split('.');
101+
done({
102+
type: contentTypes[rs[rs.length - 1]],
103+
status: 200,
104+
body: data
105+
});
106+
}
102107
});
103108
}
104109

105-
function results(req, res) {
106-
var proc = function(post) {
110+
function results(req, res, done) {
111+
var proc = function(post, done) {
107112
// todo: store client timings
108113
var id = post.id || url.parse(req.url, true).query.id;
109114
var data = storage(id);
110115
if (!data) {
111116
debug(`Id '${id}' not found.`);
112-
res.setHeader('Content-Type', 'text/plain');
113-
res.writeHead(404);
114-
res.end(`Id '${id}' not found.`);
117+
done({
118+
type: 'text/plain; charset=utf-8',
119+
status:404,
120+
body: `Id '${id}' not found.`
121+
});
115122
return;
116123
}
117124

118125
if (post.popup == '1') {
119-
res.setHeader('Content-Type', 'application/json');
120-
res.end(data);
126+
done({
127+
type: 'application/json',
128+
status: 200,
129+
body: data
130+
});
121131
return;
122132
}
123133

124134
var json = JSON.parse(data);
125-
res.setHeader('Content-Type', 'text/html');
126-
res.end(includes.share({
127-
name: json.Name,
128-
duration: json.DurationMilliseconds,
129-
path: resourcePath,
130-
json: data,
131-
includes: include(id),
132-
version: version
133-
}));
134-
135+
done({
136+
type: 'text/html; charset=utf-8',
137+
status: 200,
138+
body: includes.share({
139+
name: json.Name,
140+
duration: json.DurationMilliseconds,
141+
path: resourcePath,
142+
json: data,
143+
includes: include(id),
144+
version: version
145+
})
146+
});
135147
};
136148

137-
/*
138-
139-
https://nodejs.org/api/http.html#http_class_http_clientrequest
140-
Request does not have `body` attribute
141-
Do we still need this?
142-
143-
if(req.body) {
144-
proc(req.body);
145-
return;
146-
}
147-
*/
148-
149149
var body = '';
150150
req.on('data', function(data) {
151151
body += data;
@@ -155,7 +155,7 @@ function results(req, res) {
155155

156156
req.on('end', function() {
157157
var post = qs.parse(body);
158-
proc(post);
158+
proc(post, done);
159159
});
160160
}
161161

tests/assets-test.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
var expect = require('chai').expect;
2-
var server = require('./server/default');
32
var fs = require('fs');
43

5-
describe('MiniProfiler Assets Tests', function() {
6-
before(server.start);
7-
after(server.stop);
4+
for (var fw of ['express', 'koa']) {
5+
var server = require(`./server/${fw}/default`);
86

9-
var files = [
10-
'includes.css',
11-
'includes.tmpl',
12-
'includes.js'
13-
];
7+
describe(`[${fw}] MiniProfiler Assets Tests`, function() {
8+
before(server.start);
9+
after(server.stop);
1410

15-
files.forEach((file) => {
16-
it(`Should return ${file} file`, function(done) {
17-
server.get(`/mini-profiler-resources/${file}`, (err, response, body) => {
18-
expect(body).to.be.equal(fs.readFileSync(`./ui/${file}`, 'utf-8'));
19-
done();
11+
var files = [
12+
'includes.css',
13+
'includes.tmpl',
14+
'includes.js'
15+
];
16+
17+
files.forEach((file) => {
18+
it(`Should return ${file} file`, function(done) {
19+
server.get(`/mini-profiler-resources/${file}`, (err, response, body) => {
20+
expect(body).to.be.equal(fs.readFileSync(`./ui/${file}`, 'utf-8'));
21+
done();
22+
});
2023
});
2124
});
22-
});
2325

24-
it('Unknown file should return 404', function(done) {
25-
server.get('/mini-profiler-resources/unkown.js', (err, response, body) => {
26-
expect(response.statusCode).to.be.equal(404);
27-
expect(response.headers['content-type']).to.be.equal('text/plain');
28-
expect(body).to.be.equal('Resource unavailable.');
29-
done();
26+
it('Unknown file should return 404', function(done) {
27+
server.get('/mini-profiler-resources/unknown.js', (err, response, body) => {
28+
expect(response.statusCode).to.be.equal(404);
29+
expect(body).to.be.equal('Resource unavailable.');
30+
expect(response.headers['content-type']).to.be.equal('text/plain; charset=utf-8');
31+
done();
32+
});
3033
});
31-
});
3234

33-
});
35+
});
36+
}

tests/basic-test.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
var expect = require('chai').expect;
2-
var server = require('./server/default');
32

4-
describe('MiniProfiler Tests', function() {
5-
before(server.start);
6-
after(server.stop);
3+
for (var fw of ['express', 'koa']) {
4+
var server = require(`./server/${fw}/default`);
75

8-
it('Profiled routes should always return Profiler ID', function(done) {
9-
server.get('/', (err, response) => {
10-
expect(response.headers).to.include.keys('x-miniprofiler-ids');
11-
done();
12-
});
13-
});
6+
describe(`[${fw}] MiniProfiler Tests`, function() {
7+
before(server.start);
8+
after(server.stop);
149

15-
it('Custom timed query should be profiled', function(done) {
16-
server.get('/js-sleep', (err, response) => {
17-
var ids = JSON.parse(response.headers['x-miniprofiler-ids']);
18-
expect(ids).to.have.lengthOf(1);
19-
20-
server.post('/mini-profiler-resources/results', { id: ids[0], popup: 1 }, (err, response, body) => {
21-
var result = JSON.parse(body);
22-
expect(result.Id).to.equal(ids[0]);
23-
expect(result.Name).to.equal('/js-sleep');
24-
expect(result.DurationMilliseconds).to.be.above(200);
25-
expect(result.Root.Children).to.be.empty;
26-
expect(result.Root.CustomTimings).to.have.property('custom');
27-
expect(result.Root.CustomTimings.custom).to.have.lengthOf(1);
28-
29-
expect(result.Root.CustomTimings.custom[0].ExecuteType).to.be.equal('custom');
30-
expect(result.Root.CustomTimings.custom[0].CommandString).to.be.equal('Sleeping...');
31-
expect(result.Root.CustomTimings.custom[0].DurationMilliseconds).to.be.below(result.DurationMilliseconds);
10+
it('Profiled routes should always return Profiler ID', function(done) {
11+
server.get('/', (err, response) => {
12+
expect(response.headers).to.include.keys('x-miniprofiler-ids');
3213
done();
3314
});
3415
});
3516

36-
});
17+
it('Custom timed query should be profiled', function(done) {
18+
server.get('/js-sleep', (err, response) => {
19+
var ids = JSON.parse(response.headers['x-miniprofiler-ids']);
20+
expect(ids).to.have.lengthOf(1);
3721

38-
});
22+
server.post('/mini-profiler-resources/results', { id: ids[0], popup: 1 }, (err, response, body) => {
23+
var result = JSON.parse(body);
24+
expect(result.Id).to.equal(ids[0]);
25+
expect(result.Name).to.equal('/js-sleep');
26+
expect(result.DurationMilliseconds).to.be.above(200);
27+
expect(result.Root.Children).to.be.empty;
28+
expect(result.Root.CustomTimings).to.have.property('custom');
29+
expect(result.Root.CustomTimings.custom).to.have.lengthOf(1);
30+
31+
expect(result.Root.CustomTimings.custom[0].ExecuteType).to.be.equal('custom');
32+
expect(result.Root.CustomTimings.custom[0].CommandString).to.be.equal('Sleeping...');
33+
expect(result.Root.CustomTimings.custom[0].DurationMilliseconds).to.be.below(result.DurationMilliseconds);
34+
done();
35+
});
36+
});
37+
38+
});
39+
40+
});
41+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var miniprofiler = require('../../lib/miniprofiler.js');
1+
var miniprofiler = require('../../../lib/miniprofiler.js');
22
var express = require('express');
33

44
var app = express();
@@ -29,4 +29,4 @@ app.get('/js-sleep', function(req, res) {
2929
}, 300);
3030
});
3131

32-
module.exports = require('./base.js')(app);
32+
module.exports = require('../base.js')(app);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var miniprofiler = require('../../lib/miniprofiler.js');
1+
var miniprofiler = require('../../../lib/miniprofiler.js');
22
var express = require('express');
33

44
var app = express();
@@ -13,4 +13,4 @@ app.get('/', (req, res) => {
1313
res.send();
1414
});
1515

16-
module.exports = require('./base.js')(app);
16+
module.exports = require('../base.js')(app);

0 commit comments

Comments
 (0)