Skip to content

Commit 61b1766

Browse files
author
Guilherme Oenning
committed
starting refactor to split providers into it's own packages
1 parent ea010db commit 61b1766

File tree

11 files changed

+42
-43
lines changed

11 files changed

+42
-43
lines changed

lib/middlewares/express.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
buildMiddleware: function(provider) {
33
return function(req, res, next) {
4-
provider(req, res, next);
4+
provider.handler(req, res, next);
55
};
66
},
77
mainMiddleware: function(f, handleRequest) {

lib/middlewares/hapi.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@ var onHapiRequest = function(f, handleRequest, request, reply) {
1212
};
1313

1414
module.exports = {
15-
buildMiddleware: function(provider, name) {
15+
buildMiddleware: function(provider) {
1616
var plugin = {
1717
register: (server, options, next) => {
1818
server.ext('onRequest', function(request, reply) {
19-
provider(request.raw.req, request.raw.res, () => {
19+
provider.handler(request.raw.req, request.raw.res, () => {
2020
return reply.continue();
2121
});
2222
});
2323
next();
2424
}
2525
};
26-
2726
plugin.register.attributes = {
28-
name: `miniprofiler-hapi-${name}`,
27+
name: `miniprofiler-hapi-${provider.name}`,
2928
version: require('../../package.json').version
3029
};
3130

lib/middlewares/koa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
buildMiddleware: function(provider) {
33
return function *(next) {
44
yield new Promise((resolve, reject) => {
5-
provider(this.req, this.res, resolve);
5+
provider.handler(this.req, this.res, resolve);
66
});
77
yield next;
88
};

lib/miniprofiler.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ for (let framework of ['koa', 'express', 'hapi']) {
3636
return func.mainMiddleware(f, handleRequest);
3737
};
3838

39-
exports[framework].for = {
40-
pg: (pg) => {
41-
return func.buildMiddleware(require('./providers/miniprofiler.pg.js')(pg), 'pg');
42-
},
43-
redis: (redis) => {
44-
return func.buildMiddleware(require('./providers/miniprofiler.redis.js')(redis), 'redis');
45-
}
46-
};
39+
exports[framework].for = func.buildMiddleware;
4740
}
4841

4942
// GLOBALS

lib/providers/miniprofiler.pg.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ var pgQuery;
22

33
module.exports = function(pg) {
44
pgQuery = pgQuery || pg.Client.prototype.query;
5-
return function(req, res, next) {
6-
pg.Client.prototype.query = !req.miniprofiler.enabled ? pgQuery : function(config, values, callback) {
7-
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback);
8-
};
95

10-
next();
11-
};
6+
return {
7+
name: 'pg',
8+
handler: function(req, res, next) {
9+
10+
pg.Client.prototype.query = !req.miniprofiler.enabled ? pgQuery : function(config, values, callback) {
11+
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback);
12+
};
13+
14+
next();
15+
}
16+
};
1217
};
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
var redisSendCommand;
2-
32
var blacklist = ['info'];
43

54
module.exports = function(redis) {
65
redisSendCommand = redisSendCommand || redis.RedisClient.prototype.internal_send_command;
7-
return function(req, res, next) {
8-
9-
redis.RedisClient.prototype.internal_send_command = !req.miniprofiler.enabled ? redisSendCommand : function(command, args, callback) {
10-
var query = `${command} ${args.join(', ')}`.trim();
11-
if (this.ready && blacklist.indexOf(command) == -1)
12-
req.miniprofiler.timeQuery('redis', query, redisSendCommand.bind(this), command, args, callback);
13-
else
14-
redisSendCommand.call(this, command, args, callback);
15-
};
166

17-
next();
18-
};
19-
};
7+
return {
8+
name: 'redis',
9+
handler: function(req, res, next) {
10+
11+
redis.RedisClient.prototype.internal_send_command = !req.miniprofiler.enabled ? redisSendCommand : function(command, args, callback) {
12+
var query = `${command} ${args.join(', ')}`.trim();
13+
if (this.ready && blacklist.indexOf(command) == -1)
14+
req.miniprofiler.timeQuery('redis', query, redisSendCommand.bind(this), command, args, callback);
15+
else
16+
redisSendCommand.call(this, command, args, callback);
17+
};
18+
19+
next();
20+
}
21+
};
22+
};

tests/basic-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var expect = require('chai').expect;
2-
var eol = require('os').EOL;
32

43
module.exports = function(server) {
54
describe('Basic Tests', function() {
@@ -18,7 +17,7 @@ module.exports = function(server) {
1817
var ids = JSON.parse(response.headers['x-miniprofiler-ids']);
1918
expect(ids).to.have.lengthOf(1);
2019

21-
expect(body).to.be.equal(`<script async type="text/javascript" id="mini-profiler" src="/mini-profiler-resources/includes.js?v=" data-version="" data-path="/mini-profiler-resources/" data-current-id="${ids[0]}" data-ids="${ids[0]}" data-position="left" data-trivial="true" data-children="false" data-max-traces="15" data-controls="true" data-authorized="true" data-toggle-shortcut="" data-start-hidden="false" data-trivial-milliseconds="2.5"></script>` + eol);
20+
expect(body.trim()).to.be.equal(`<script async type="text/javascript" id="mini-profiler" src="/mini-profiler-resources/includes.js?v=" data-version="" data-path="/mini-profiler-resources/" data-current-id="${ids[0]}" data-ids="${ids[0]}" data-position="left" data-trivial="true" data-children="false" data-max-traces="15" data-controls="true" data-authorized="true" data-toggle-shortcut="" data-start-hidden="false" data-trivial-milliseconds="2.5"></script>`);
2221
done();
2322
});
2423
});

tests/custom-config-test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var expect = require('chai').expect;
2-
var eol = require('os').EOL;
32

43
module.exports = function(server) {
54
describe('Custom Configuration Tests', function() {
@@ -11,7 +10,7 @@ module.exports = function(server) {
1110
var ids = JSON.parse(response.headers['x-miniprofiler-ids']);
1211
expect(ids).to.have.lengthOf(1);
1312

14-
expect(body).to.be.equal(`<script async type="text/javascript" id="mini-profiler" src="/mini-profiler-resources/includes.js?v=" data-version="" data-path="/mini-profiler-resources/" data-current-id="${ids[0]}" data-ids="${ids[0]}" data-position="right" data-trivial="true" data-children="false" data-max-traces="15" data-controls="true" data-authorized="true" data-toggle-shortcut="" data-start-hidden="false" data-trivial-milliseconds="2.5"></script>` + eol);
13+
expect(body.trim()).to.be.equal(`<script async type="text/javascript" id="mini-profiler" src="/mini-profiler-resources/includes.js?v=" data-version="" data-path="/mini-profiler-resources/" data-current-id="${ids[0]}" data-ids="${ids[0]}" data-position="right" data-trivial="true" data-children="false" data-max-traces="15" data-controls="true" data-authorized="true" data-toggle-shortcut="" data-start-hidden="false" data-trivial-milliseconds="2.5"></script>`);
1514
done();
1615
});
1716
});

tests/servers/express/default.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var app = express();
88
var client = redis.createClient(6379, process.env.REDIS_PORT_6379_TCP_ADDR);
99

1010
app.use(miniprofiler.express());
11-
app.use(miniprofiler.express.for.pg(pg));
12-
app.use(miniprofiler.express.for.redis(redis));
11+
app.use(miniprofiler.express.for(require('../../../lib/providers/miniprofiler.pg.js')(pg)));
12+
app.use(miniprofiler.express.for(require('../../../lib/providers/miniprofiler.redis.js')(redis)));
1313

1414
app.get('/', (req, res) => {
1515
res.send(req.miniprofiler.include());

tests/servers/hapi/default.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ server.connection({ port: 8083 });
1313
server.register(miniprofiler.hapi(), (err) => {
1414
if (err) throw err;
1515
});
16-
server.register(miniprofiler.hapi.for.pg(pg), (err) => {
16+
17+
server.register(miniprofiler.hapi.for(require('../../../lib/providers/miniprofiler.pg.js')(pg)), (err) => {
1718
if (err) throw err;
1819
});
19-
server.register(miniprofiler.hapi.for.redis(redis), (err) => {
20+
server.register(miniprofiler.hapi.for(require('../../../lib/providers/miniprofiler.redis.js')(redis)), (err) => {
2021
if (err) throw err;
2122
});
2223

0 commit comments

Comments
 (0)