diff --git a/lib/miniprofiler.js b/lib/miniprofiler.js index 3f2a2f4..b74c612 100755 --- a/lib/miniprofiler.js +++ b/lib/miniprofiler.js @@ -10,19 +10,20 @@ * Guilherme Oenning, 2016 @goenning */ - var _ = require('./utils.js'); - var qs = require('querystring'); - var url = require('url'); - var ui = require('./ui.js'); - var clientParser = require('./client-parser.js'); +var _ = require('./utils.js'); +var qs = require('querystring'); +var url = require('url'); +var ui = require('./ui.js'); +var clientParser = require('./client-parser.js'); - const hostname = require('os').hostname; +const hostname = require('os').hostname; var ignoredPaths = []; - var trivialDurationThresholdMilliseconds = 2.5; - var popupShowTimeWithChildren = false; - var popupRenderPosition = 'left'; +var trivialDurationThresholdMilliseconds = 2.5; +var popupShowTimeWithChildren = false; +var popupRenderPosition = 'left'; +var resourcePath = '/mini-profiler-resources/'; - exports.storage = { +exports.storage = { InMemoryStorage: require('./storages/inmemory.js'), RedisStorage: require('./storages/redis.js') }; @@ -36,7 +37,7 @@ for (let framework of ['koa', 'express', 'hapi']) { let func = require(`./middlewares/${framework}.js`); exports[framework] = function(options) { - options = options || { }; + options = options || {}; if (!options.enable) options.enable = () => { return true; }; if (!options.authorize) options.authorize = () => { return true; }; @@ -47,7 +48,6 @@ for (let framework of ['koa', 'express', 'hapi']) { exports[framework].for = func.buildMiddleware; } -var resourcePath = '/mini-profiler-resources/'; var version = require('../package.json').version; var contentTypes = { @@ -100,10 +100,10 @@ function handleRequest(enable, authorize, req, res) { var lastPathSegment = segments[segments.length - 1]; var handler = (lastPathSegment == 'results') ? results : assets; handler(req, res, lastPathSegment, (result) => { - res.writeHead(result.status, { 'Content-Type': result.type }); - res.end(result.body); - resolve(true); - }); + res.writeHead(result.status, { 'Content-Type': result.type }); + res.end(result.body); + resolve(true); + }); }); } @@ -140,7 +140,7 @@ function results(req, res, lastPathSegment, done) { if (!data) { done({ type: 'text/plain; charset=utf-8', - status:404, + status: 404, body: `Id '${id}' not found.` }); return; @@ -218,22 +218,24 @@ function include(id) { * - trivialDurationThresholdMilliseconds: double ; any step lasting longer than this will be considered trivial, and hidden by default * - popupShowTimeWithChildren: boolean ; whether or not to include the "time with children" column * - popupRenderPosition: 'left', 'right', 'bottomLeft', 'bottomRight' ; which side of the screen to display timings on + * - resourcePath: string ; if your site root is in a subdirectory, specify here, e.g., /siteroot */ - function configure(options){ - options = options || {}; - - ignoredPaths = options.ignoredPaths || ignoredPaths; - trivialDurationThresholdMilliseconds = options.trivialDurationThresholdMilliseconds || trivialDurationThresholdMilliseconds; - popupShowTimeWithChildren = options.popupShowTimeWithChildren || popupShowTimeWithChildren; - popupRenderPosition = options.popupRenderPosition || popupRenderPosition; - storage = options.storage || storage; - } +function configure(options) { + options = options || {}; + + ignoredPaths = options.ignoredPaths || ignoredPaths; + trivialDurationThresholdMilliseconds = options.trivialDurationThresholdMilliseconds || trivialDurationThresholdMilliseconds; + popupShowTimeWithChildren = options.popupShowTimeWithChildren || popupShowTimeWithChildren; + popupRenderPosition = options.popupRenderPosition || popupRenderPosition; + storage = options.storage || storage; + resourcePath = `${options.resourcePath ? options.resourcePath.replace(/\/$/, '') : ''}${resourcePath}`; +} /* * Begins profiling the given request. */ - function startProfiling(request, enabled, authorized) { - var currentRequestExtension = { +function startProfiling(request, enabled, authorized) { + var currentRequestExtension = { enabled: enabled, authorized: authorized }; @@ -286,7 +288,7 @@ function include(id) { /* * Stops profiling the given request. */ - function stopProfiling(extension, request){ +function stopProfiling(extension, request) { var time = process.hrtime(); extension.stopTime = time; @@ -301,29 +303,29 @@ function include(id) { * * You should only use this method directly in cases when calls to addProfiling won't suffice. */ - function step(name, request, call) { - var time = process.hrtime(); +function step(name, request, call) { + var time = process.hrtime(); - var extension = request.miniprofiler; + var extension = request.miniprofiler; - var newStep = makeStep(name, time, extension.stepGraph); - extension.stepGraph.steps.push(newStep); - extension.stepGraph = newStep; + var newStep = makeStep(name, time, extension.stepGraph); + extension.stepGraph.steps.push(newStep); + extension.stepGraph = newStep; - var result; - if (call.length) { + var result; + if (call.length) { result = call(() => { - unstep(name, request); - }); + unstep(name, request); + }); } else { try { - result = call(); - } finally { - unstep(name, request); - } - } + result = call(); + } finally { + unstep(name, request); + } + } - return result; + return result; } /* @@ -338,11 +340,11 @@ function include(id) { * when the query has completed. Implicitly, any execution of a callback is considered * to have ended the query. */ - function timeQuery(extension, type, query, executeFunction) { - var timing = startTimeQuery(extension, type, query); - var params = Array.prototype.slice.call(arguments, 4); +function timeQuery(extension, type, query, executeFunction) { + var timing = startTimeQuery(extension, type, query); + var params = Array.prototype.slice.call(arguments, 4); - for(var i = 0; i < params.length; i++){ + for (var i = 0; i < params.length; i++) { if (_.isFunction(params[i])) { var param = params[i]; params[i] = function() { @@ -403,7 +405,7 @@ function describePerformance(root, request) { return ret; } -function diff(start, stop){ +function diff(start, stop) { var deltaSecs = stop[0] - start[0]; var deltaNanoSecs = stop[1] - start[1]; @@ -415,14 +417,14 @@ function diff(start, stop){ function callStack(stack) { var sp = stack.split('\n'); var ret = []; - for(var i = 2; i < sp.length; i++) { + for (var i = 2; i < sp.length; i++) { var st = sp[i].trim().split(' '); ret.push(st[1]); } return ret.join(' '); } -function describeTimings(timing, root){ +function describeTimings(timing, root) { var id = _.uuid(); var name = timing.name; var elapsedMs = diff(timing.startTime, timing.stopTime); @@ -430,7 +432,7 @@ function describeTimings(timing, root){ var customTimings = describeCustomTimings(timing.customTimings, root); var children = []; - for(var i = 0; i < timing.steps.length; i++){ + for (var i = 0; i < timing.steps.length; i++) { var step = timing.steps[i]; children.push(describeTimings(step, root)); } @@ -447,12 +449,12 @@ function describeTimings(timing, root){ function describeCustomTimings(customTimings, root) { var ret = {}; - for(var prop in customTimings) { + for (var prop in customTimings) { var arr = customTimings[prop]; var retArr = []; - for(var i = 0; i < arr.length; i++) { + for (var i = 0; i < arr.length; i++) { var timing = {}; timing.Id = arr[i].id; timing.ExecuteType = arr[i].executeType; @@ -470,6 +472,6 @@ function describeCustomTimings(customTimings, root) { return ret; } -function makeStep(name, time, parent){ +function makeStep(name, time, parent) { return { name: name, startTime: time, stopTime: null, parent: parent, steps: [], customTimings: {} }; -} +} \ No newline at end of file