@@ -133,13 +133,14 @@ if (typeof String.prototype.startsWith != 'function') {
133133function middleware ( f ) {
134134 if ( ! f ) f = function ( ) { return true ; } ;
135135 return function ( req , res , next ) {
136- if ( ! f ( req , res ) ) {
137- next ( ) ;
138- return ;
139- }
140136 if ( ! configured ) configure ( ) ;
137+ var enabled = f ( req , res ) ;
141138
142139 if ( req . path . startsWith ( resourcePath ) ) {
140+ if ( ! enabled ) {
141+ res . send ( 404 ) ;
142+ return ;
143+ }
143144 var sp = req . path . split ( '/' ) ;
144145 var reqPath = sp [ sp . length - 1 ] ;
145146 if ( reqPath == 'results' )
@@ -150,15 +151,18 @@ function middleware(f) {
150151 }
151152
152153 var reqDomain = domain . create ( ) ;
153- req . miniprofiler = exports ;
154154 reqDomain . add ( req ) ;
155155 reqDomain . add ( res ) ;
156- res . on ( 'header' , function ( ) {
157- stopProfiling ( ) ;
158- } ) ;
156+ if ( enabled ) {
157+ res . on ( 'header' , function ( ) {
158+ stopProfiling ( ) ;
159+ } ) ;
160+ }
159161 reqDomain . run ( function ( ) {
160- var id = startProfiling ( req ) ;
161- res . setHeader ( "X-MiniProfiler-Ids" , '["' + id + '"]' ) ;
162+ var id = startProfiling ( req , enabled ) ;
163+ if ( enabled ) {
164+ res . setHeader ( "X-MiniProfiler-Ids" , '["' + id + '"]' ) ;
165+ }
162166 next ( ) ;
163167 } ) ;
164168 } ;
@@ -169,7 +173,7 @@ function include(id) {
169173 // not profiling
170174 if ( ! id ) {
171175 if ( ! domain || ! domain . miniprofiler_currentRequest ) return null ;
172- var extension = domain . miniprofiler_currentRequest . miniprofiler_extension ;
176+ var extension = domain . miniprofiler_currentRequest . miniprofiler ;
173177 id = extension . id ;
174178 }
175179 return includes . partial ( {
@@ -253,7 +257,7 @@ function addProfilingInstrumentation(toInstrument) {
253257 *
254258 * Note that this call must occur in the context of the domain that will service this request.
255259 */
256- function startProfiling ( request ) {
260+ function startProfiling ( request , enabled ) {
257261 if ( ! configured ) throw new Error ( 'configure() must be called before the first call to startProfiling' ) ;
258262
259263 var domain = getDomain ( '--startProfiling' ) ;
@@ -262,14 +266,28 @@ function startProfiling(request) {
262266 domain . miniprofiler_currentRequest = request ;
263267 var currentRequestExtension = { } ;
264268
265- currentRequestExtension . startDate = Date . now ( ) ;
266- currentRequestExtension . startTime = process . hrtime ( ) ;
267- currentRequestExtension . stopTime = null ;
268- currentRequestExtension . stepGraph = makeStep ( request . path , currentRequestExtension . startTime , null ) ;
269- currentRequestExtension . id = makeGuid ( ) ;
270- currentRequestExtension . customTimings = { } ;
269+ if ( enabled ) {
270+ currentRequestExtension . startDate = Date . now ( ) ;
271+ currentRequestExtension . startTime = process . hrtime ( ) ;
272+ currentRequestExtension . stopTime = null ;
273+ currentRequestExtension . stepGraph = makeStep ( request . path , currentRequestExtension . startTime , null ) ;
274+ currentRequestExtension . id = makeGuid ( ) ;
275+ currentRequestExtension . customTimings = { } ;
276+ }
277+ currentRequestExtension . timeQuery = function ( ) {
278+ var args = Array . prototype . slice . call ( arguments , enabled ? 0 : 3 ) ;
279+ if ( enabled ) {
280+ args . unshift ( currentRequestExtension ) ;
281+ timeQueryExtension . apply ( this , args ) ;
282+ } else {
283+ arguments [ 2 ] . apply ( this , args ) ;
284+ }
285+ } ;
286+ currentRequestExtension . include = function ( ) {
287+ return enabled ? include ( currentRequestExtension . id ) : '' ;
288+ } ;
271289
272- request . miniprofiler_extension = currentRequestExtension ;
290+ request . miniprofiler = currentRequestExtension ;
273291 return currentRequestExtension . id ;
274292}
275293
@@ -284,7 +302,7 @@ function stopProfiling(){
284302 // not profiling
285303 if ( ! domain || ! domain . miniprofiler_currentRequest ) return null ;
286304
287- var extension = domain . miniprofiler_currentRequest . miniprofiler_extension ;
305+ var extension = domain . miniprofiler_currentRequest . miniprofiler ;
288306
289307 var time = process . hrtime ( ) ;
290308
@@ -298,7 +316,7 @@ function stopProfiling(){
298316 var request = domain . miniprofiler_currentRequest ;
299317
300318 // get those references gone, we can't assume much about GC here
301- delete domain . miniprofiler_currentRequest . miniprofiler_extension ;
319+ delete domain . miniprofiler_currentRequest . miniprofiler ;
302320 delete domain . miniprofiler_currentRequest ;
303321
304322 var json = describePerformance ( extension , request ) ;
@@ -325,7 +343,7 @@ function step(name, call) {
325343
326344 var time = process . hrtime ( ) ;
327345
328- var extension = domain . miniprofiler_currentRequest . miniprofiler_extension ;
346+ var extension = domain . miniprofiler_currentRequest . miniprofiler ;
329347
330348 var newStep = makeStep ( name , time , extension . stepGraph ) ;
331349 extension . stepGraph . steps . push ( newStep ) ;
@@ -360,18 +378,22 @@ function step(name, call) {
360378 * to have ended the query.
361379 */
362380function timeQuery ( type , query , executeFunction /*, params[] */ ) {
363- var time = process . hrtime ( ) ;
364- var startDate = Date . now ( ) ;
365-
366- var domain = getDomain ( '--timeQuery: ' + type ) ;
367- var params = Array . prototype . slice . call ( arguments , 3 ) ;
368-
369381 // Not profiling
370382 if ( ! domain || ! domain . miniprofiler_currentRequest ) {
371383 return executeFunction . apply ( this , params ) ;
372384 }
385+ var domain = getDomain ( '--timeQuery: ' + type ) ;
386+ var extension = domain . miniprofiler_currentRequest . miniprofiler ;
387+ var args = Array . prototype . slice . call ( arguments , 0 ) ;
388+ args . unshift ( extension ) ;
389+ timeQueryExtension . apply ( this , args ) ;
390+ }
391+
392+ function timeQueryExtension ( extension , type , query , executeFunction ) {
393+ var time = process . hrtime ( ) ;
394+ var startDate = Date . now ( ) ;
373395
374- var extension = domain . miniprofiler_currentRequest . miniprofiler_extension ;
396+ var params = Array . prototype . slice . call ( arguments , 4 ) ;
375397
376398 if ( ! extension . stepGraph . customTimings [ type ] ) {
377399 extension . stepGraph . customTimings [ type ] = [ ] ;
@@ -422,7 +444,7 @@ function unstep(name, failed) {
422444 // Not profiling
423445 if ( ! domain || ! domain . miniprofiler_currentRequest ) return ;
424446
425- var extension = domain . miniprofiler_currentRequest . miniprofiler_extension ;
447+ var extension = domain . miniprofiler_currentRequest . miniprofiler ;
426448
427449 if ( extension . stepGraph . name != name ) {
428450 throw new Error ( 'profiling stepped out of the wrong function, found [' + name + '] expected [' + extension . stepGraph . name + ']' ) ;
0 commit comments