Skip to content

Commit 8dbd7a7

Browse files
committed
raw http support
1 parent 43613fd commit 8dbd7a7

File tree

9 files changed

+122
-6
lines changed

9 files changed

+122
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ See [examples/express.js](/examples/express.js) for more examples.
6565

6666
Things to do:
6767

68+
- Refactor/Remove TODO comments
6869
- transform testcases into runnable examples (npm run example)
6970
- storing of client timings on first result postback (there's a todo in the `results` function about where to do this)
7071
- document more things

examples/http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var http = require('http');
22
var miniprofiler = require('../lib/miniprofiler.js');
33

4-
var profile = miniprofiler.profile();
4+
var profiler = miniprofiler.http();
55

66
http.createServer(function(req, res) {
7-
profile(req, res, function() {
7+
profiler(req, res, () => {
88
req.miniprofiler.step('Step 1', function(){
99
res.end('home');
1010
});

lib/middlewares/express.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = function(f, profiler) {
1414

1515
var sp = req.path.split('/');
1616
var reqPath = sp[sp.length - 1];
17+
1718
if(reqPath == 'results')
1819
profiler.results(req, res, (result) => {
1920
res.setHeader('Content-Type', result.type);
@@ -30,9 +31,9 @@ module.exports = function(f, profiler) {
3031
}
3132
var id = profiler.startProfiling(req, enabled);
3233

33-
res.locals.miniprofiler = enabled ? req.miniprofiler : {
34-
include: function() { return ''; }
35-
};
34+
//res.locals.miniprofiler = enabled ? req.miniprofiler : {
35+
// include: function() { return ''; }
36+
//};
3637

3738
if (enabled) {
3839
res.on('finish', function() {

lib/middlewares/http.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports = function(f, profiler) {
2+
if(!f) f = function() { return true; };
3+
return function(req, res, next) {
4+
profiler.configure();
5+
var enabled = f(req, res);
6+
7+
req.path = require('url').parse(req.url).pathname; //TODO remove this
8+
if(req.path.startsWith(profiler.resourcePath)) {
9+
if (!enabled) {
10+
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
11+
res.writeHead(404);
12+
res.end('MiniProfiler is disabled');
13+
return;
14+
}
15+
16+
var sp = req.path.split('/');
17+
var reqPath = sp[sp.length - 1];
18+
19+
if(reqPath == 'results')
20+
profiler.results(req, res, (result) => {
21+
res.setHeader('Content-Type', result.type);
22+
res.writeHead(result.status);
23+
res.end(result.body);
24+
});
25+
else
26+
profiler.static(reqPath, res, (result) => {
27+
res.setHeader('Content-Type', result.type);
28+
res.writeHead(result.status);
29+
res.end(result.body);
30+
});
31+
return;
32+
}
33+
var id = profiler.startProfiling(req, enabled);
34+
35+
//res.locals.miniprofiler = enabled ? req.miniprofiler : {
36+
// include: function() { return ''; }
37+
//};
38+
39+
if (enabled) {
40+
res.on('finish', function() {
41+
profiler.stopProfiling(req);
42+
});
43+
res.setHeader('X-MiniProfiler-Ids', `["${id}"]`);
44+
}
45+
next();
46+
};
47+
};

lib/miniprofiler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ exports.hapi = function(f) {
4646
return require('./middlewares/hapi.js')(f, shared());
4747
};
4848

49+
exports.http = function(f) {
50+
return require('./middlewares/http.js')(f, shared());
51+
};
52+
4953
exports.for = {
5054
pg: require('./providers/miniprofiler.pg.js'),
5155
redis: require('./providers/miniprofiler.redis.js')

tests/servers/http/default.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var miniprofiler = require('../../../lib/miniprofiler.js');
2+
const http = require('http');
3+
4+
var profiler = miniprofiler.http();
5+
6+
var server = http.createServer((req, res) => {
7+
8+
profiler(req, res, () => {
9+
res.writeHead(200, {'Content-Type': 'text/plain'});
10+
11+
if (req.path == '/') {
12+
res.end('');
13+
} else if (req.path == '/step') {
14+
req.miniprofiler.step('Step 1', () => {
15+
res.end('');
16+
});
17+
} else if (req.path == '/step-two') {
18+
req.miniprofiler.step('Step 1', () => {
19+
req.miniprofiler.step('Step 2', () => {
20+
res.end('');
21+
});
22+
});
23+
} else if (req.path == '/js-sleep') {
24+
req.miniprofiler.timeQuery('custom', 'Sleeping...', setTimeout, function() {
25+
res.end('');
26+
}, 50);
27+
}
28+
});
29+
30+
});
31+
32+
module.exports = server;

tests/servers/http/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var server;
2+
module.exports = {
3+
start: function(name) {
4+
server = require(`./${name}.js`);
5+
server.listen(8080);
6+
},
7+
stop: function() {
8+
server.close();
9+
}
10+
};

tests/servers/http/unprofiled.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var miniprofiler = require('../../../lib/miniprofiler.js');
2+
const http = require('http');
3+
4+
var disableMiniProfiler = (req) => {
5+
return false;
6+
};
7+
8+
var profiler = miniprofiler.http(disableMiniProfiler);
9+
10+
var server = http.createServer((req, res) => {
11+
12+
profiler(req, res, () => {
13+
if (req.path == '/') {
14+
res.writeHead(200, {'Content-Type': 'text/plain'});
15+
res.end('');
16+
}
17+
});
18+
19+
});
20+
21+
module.exports = server;

tests/servers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var request = require('request');
2-
var frameworks = [ 'koa', 'express', 'hapi' ];
2+
var frameworks = [ 'koa', 'express', 'hapi', 'http' ];
33
var all = [ ];
44

55
for (var fw of frameworks) {

0 commit comments

Comments
 (0)