Skip to content

Commit bf623f6

Browse files
sweatercomebackNickCraver
authored andcommitted
Adds configurable resourcePath (#9)
This change allows you to modify the resourcePath with a subdirectory through configuration options: miniprofiler.configure({ resourcePath: '/myapproot' }); app.use(miniprofiler.express()); This allows miniprofiler resources to load if your app root is not /.
1 parent e92150e commit bf623f6

File tree

1 file changed

+59
-57
lines changed

1 file changed

+59
-57
lines changed

lib/miniprofiler.js

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010
* Guilherme Oenning, 2016 @goenning
1111
*/
1212

13-
var _ = require('./utils.js');
14-
var qs = require('querystring');
15-
var url = require('url');
16-
var ui = require('./ui.js');
17-
var clientParser = require('./client-parser.js');
13+
var _ = require('./utils.js');
14+
var qs = require('querystring');
15+
var url = require('url');
16+
var ui = require('./ui.js');
17+
var clientParser = require('./client-parser.js');
1818

19-
const hostname = require('os').hostname;
19+
const hostname = require('os').hostname;
2020
var ignoredPaths = [];
21-
var trivialDurationThresholdMilliseconds = 2.5;
22-
var popupShowTimeWithChildren = false;
23-
var popupRenderPosition = 'left';
21+
var trivialDurationThresholdMilliseconds = 2.5;
22+
var popupShowTimeWithChildren = false;
23+
var popupRenderPosition = 'left';
24+
var resourcePath = '/mini-profiler-resources/';
2425

25-
exports.storage = {
26+
exports.storage = {
2627
InMemoryStorage: require('./storages/inmemory.js'),
2728
RedisStorage: require('./storages/redis.js')
2829
};
@@ -36,7 +37,7 @@ for (let framework of ['koa', 'express', 'hapi']) {
3637
let func = require(`./middlewares/${framework}.js`);
3738

3839
exports[framework] = function(options) {
39-
options = options || { };
40+
options = options || {};
4041

4142
if (!options.enable) options.enable = () => { return true; };
4243
if (!options.authorize) options.authorize = () => { return true; };
@@ -47,7 +48,6 @@ for (let framework of ['koa', 'express', 'hapi']) {
4748
exports[framework].for = func.buildMiddleware;
4849
}
4950

50-
var resourcePath = '/mini-profiler-resources/';
5151
var version = require('../package.json').version;
5252

5353
var contentTypes = {
@@ -100,10 +100,10 @@ function handleRequest(enable, authorize, req, res) {
100100
var lastPathSegment = segments[segments.length - 1];
101101
var handler = (lastPathSegment == 'results') ? results : assets;
102102
handler(req, res, lastPathSegment, (result) => {
103-
res.writeHead(result.status, { 'Content-Type': result.type });
104-
res.end(result.body);
105-
resolve(true);
106-
});
103+
res.writeHead(result.status, { 'Content-Type': result.type });
104+
res.end(result.body);
105+
resolve(true);
106+
});
107107
});
108108
}
109109

@@ -140,7 +140,7 @@ function results(req, res, lastPathSegment, done) {
140140
if (!data) {
141141
done({
142142
type: 'text/plain; charset=utf-8',
143-
status:404,
143+
status: 404,
144144
body: `Id '${id}' not found.`
145145
});
146146
return;
@@ -218,22 +218,24 @@ function include(id) {
218218
* - trivialDurationThresholdMilliseconds: double ; any step lasting longer than this will be considered trivial, and hidden by default
219219
* - popupShowTimeWithChildren: boolean ; whether or not to include the "time with children" column
220220
* - popupRenderPosition: 'left', 'right', 'bottomLeft', 'bottomRight' ; which side of the screen to display timings on
221+
* - resourcePath: string ; if your site root is in a subdirectory, specify here, e.g., /siteroot
221222
*/
222-
function configure(options){
223-
options = options || {};
224-
225-
ignoredPaths = options.ignoredPaths || ignoredPaths;
226-
trivialDurationThresholdMilliseconds = options.trivialDurationThresholdMilliseconds || trivialDurationThresholdMilliseconds;
227-
popupShowTimeWithChildren = options.popupShowTimeWithChildren || popupShowTimeWithChildren;
228-
popupRenderPosition = options.popupRenderPosition || popupRenderPosition;
229-
storage = options.storage || storage;
230-
}
223+
function configure(options) {
224+
options = options || {};
225+
226+
ignoredPaths = options.ignoredPaths || ignoredPaths;
227+
trivialDurationThresholdMilliseconds = options.trivialDurationThresholdMilliseconds || trivialDurationThresholdMilliseconds;
228+
popupShowTimeWithChildren = options.popupShowTimeWithChildren || popupShowTimeWithChildren;
229+
popupRenderPosition = options.popupRenderPosition || popupRenderPosition;
230+
storage = options.storage || storage;
231+
resourcePath = `${options.resourcePath ? options.resourcePath.replace(/\/$/, '') : ''}${resourcePath}`;
232+
}
231233

232234
/*
233235
* Begins profiling the given request.
234236
*/
235-
function startProfiling(request, enabled, authorized) {
236-
var currentRequestExtension = {
237+
function startProfiling(request, enabled, authorized) {
238+
var currentRequestExtension = {
237239
enabled: enabled,
238240
authorized: authorized
239241
};
@@ -286,7 +288,7 @@ function include(id) {
286288
/*
287289
* Stops profiling the given request.
288290
*/
289-
function stopProfiling(extension, request){
291+
function stopProfiling(extension, request) {
290292
var time = process.hrtime();
291293

292294
extension.stopTime = time;
@@ -301,29 +303,29 @@ function include(id) {
301303
*
302304
* You should only use this method directly in cases when calls to addProfiling won't suffice.
303305
*/
304-
function step(name, request, call) {
305-
var time = process.hrtime();
306+
function step(name, request, call) {
307+
var time = process.hrtime();
306308

307-
var extension = request.miniprofiler;
309+
var extension = request.miniprofiler;
308310

309-
var newStep = makeStep(name, time, extension.stepGraph);
310-
extension.stepGraph.steps.push(newStep);
311-
extension.stepGraph = newStep;
311+
var newStep = makeStep(name, time, extension.stepGraph);
312+
extension.stepGraph.steps.push(newStep);
313+
extension.stepGraph = newStep;
312314

313-
var result;
314-
if (call.length) {
315+
var result;
316+
if (call.length) {
315317
result = call(() => {
316-
unstep(name, request);
317-
});
318+
unstep(name, request);
319+
});
318320
} else {
319321
try {
320-
result = call();
321-
} finally {
322-
unstep(name, request);
323-
}
324-
}
322+
result = call();
323+
} finally {
324+
unstep(name, request);
325+
}
326+
}
325327

326-
return result;
328+
return result;
327329
}
328330

329331
/*
@@ -338,11 +340,11 @@ function include(id) {
338340
* when the query has completed. Implicitly, any execution of a callback is considered
339341
* to have ended the query.
340342
*/
341-
function timeQuery(extension, type, query, executeFunction) {
342-
var timing = startTimeQuery(extension, type, query);
343-
var params = Array.prototype.slice.call(arguments, 4);
343+
function timeQuery(extension, type, query, executeFunction) {
344+
var timing = startTimeQuery(extension, type, query);
345+
var params = Array.prototype.slice.call(arguments, 4);
344346

345-
for(var i = 0; i < params.length; i++){
347+
for (var i = 0; i < params.length; i++) {
346348
if (_.isFunction(params[i])) {
347349
var param = params[i];
348350
params[i] = function() {
@@ -403,7 +405,7 @@ function describePerformance(root, request) {
403405
return ret;
404406
}
405407

406-
function diff(start, stop){
408+
function diff(start, stop) {
407409
var deltaSecs = stop[0] - start[0];
408410
var deltaNanoSecs = stop[1] - start[1];
409411

@@ -415,22 +417,22 @@ function diff(start, stop){
415417
function callStack(stack) {
416418
var sp = stack.split('\n');
417419
var ret = [];
418-
for(var i = 2; i < sp.length; i++) {
420+
for (var i = 2; i < sp.length; i++) {
419421
var st = sp[i].trim().split(' ');
420422
ret.push(st[1]);
421423
}
422424
return ret.join(' ');
423425
}
424426

425-
function describeTimings(timing, root){
427+
function describeTimings(timing, root) {
426428
var id = _.uuid();
427429
var name = timing.name;
428430
var elapsedMs = diff(timing.startTime, timing.stopTime);
429431
var sinceRootMs = diff(root.startTime, timing.startTime);
430432
var customTimings = describeCustomTimings(timing.customTimings, root);
431433

432434
var children = [];
433-
for(var i = 0; i < timing.steps.length; i++){
435+
for (var i = 0; i < timing.steps.length; i++) {
434436
var step = timing.steps[i];
435437
children.push(describeTimings(step, root));
436438
}
@@ -447,12 +449,12 @@ function describeTimings(timing, root){
447449

448450
function describeCustomTimings(customTimings, root) {
449451
var ret = {};
450-
for(var prop in customTimings) {
452+
for (var prop in customTimings) {
451453

452454
var arr = customTimings[prop];
453455
var retArr = [];
454456

455-
for(var i = 0; i < arr.length; i++) {
457+
for (var i = 0; i < arr.length; i++) {
456458
var timing = {};
457459
timing.Id = arr[i].id;
458460
timing.ExecuteType = arr[i].executeType;
@@ -470,6 +472,6 @@ function describeCustomTimings(customTimings, root) {
470472
return ret;
471473
}
472474

473-
function makeStep(name, time, parent){
475+
function makeStep(name, time, parent) {
474476
return { name: name, startTime: time, stopTime: null, parent: parent, steps: [], customTimings: {} };
475-
}
477+
}

0 commit comments

Comments
 (0)