@@ -14,6 +14,8 @@ var os = require('os');
1414var path = require ( 'path' ) ;
1515var qs = require ( 'querystring' ) ;
1616var url = require ( 'url' ) ;
17+ var utils = require ( './utils.js' ) ;
18+ var uuid = require ( 'node-uuid' ) ;
1719
1820_ . templateSettings = {
1921 interpolate : / \{ ( .+ ?) \} / g
@@ -122,13 +124,6 @@ var includes = {
122124 share : _ . template ( fs . readFileSync ( path . join ( includesDir , 'share.html' ) ) . toString ( ) )
123125} ;
124126
125- // FUNCTIONS
126- if ( typeof String . prototype . startsWith != 'function' ) {
127- String . prototype . startsWith = function ( str ) {
128- return this . slice ( 0 , str . length ) == str ;
129- } ;
130- }
131-
132127function middleware ( f ) {
133128 if ( ! f ) f = function ( ) { return true ; } ;
134129 return function ( req , res , next ) {
@@ -227,7 +222,7 @@ function addProfilingInstrumentation(toInstrument) {
227222 throw new Error ( 'toInstrument must be set' ) ;
228223 }
229224
230- if ( ! isObject ( toInstrument ) ) {
225+ if ( ! utils . isObject ( toInstrument ) ) {
231226 throw new Error ( 'toInstrument must be an object' ) ;
232227 }
233228
@@ -254,7 +249,7 @@ function startProfiling(request, enabled) {
254249 currentRequestExtension . startTime = process . hrtime ( ) ;
255250 currentRequestExtension . stopTime = null ;
256251 currentRequestExtension . stepGraph = makeStep ( request . path , currentRequestExtension . startTime , null ) ;
257- currentRequestExtension . id = makeGuid ( ) ;
252+ currentRequestExtension . id = uuid . v4 ( ) ;
258253 currentRequestExtension . customTimings = { } ;
259254 }
260255 currentRequestExtension . timeQuery = function ( ) {
@@ -363,9 +358,9 @@ function timeQuery(extension, type, query, executeFunction) {
363358 }
364359
365360 var customTiming = {
366- id : makeGuid ( ) ,
361+ id : uuid . v4 ( ) ,
367362 executeType : type ,
368- commandString : htmlEscape ( query ) ,
363+ commandString : utils . htmlEscape ( query ) ,
369364 startTime : time ,
370365 startDate : startDate ,
371366 callStack : new Error ( ) . stack
@@ -374,7 +369,7 @@ function timeQuery(extension, type, query, executeFunction) {
374369
375370 for ( var i = 0 ; i < params . length ; i ++ ) {
376371 var param = params [ i ] ;
377- if ( isFunction ( params [ i ] ) ) {
372+ if ( utils . isFunction ( params [ i ] ) ) {
378373 params [ i ] = function ( ) {
379374 customTiming . stopTime = process . hrtime ( ) ;
380375
@@ -390,15 +385,6 @@ function timeQuery(extension, type, query, executeFunction) {
390385 return ret ;
391386}
392387
393- function htmlEscape ( str ) {
394- return String ( str )
395- . replace ( / & / g, '&' )
396- . replace ( / " / g, '"' )
397- . replace ( / ' / g, ''' )
398- . replace ( / < / g, '<' )
399- . replace ( / > / g, '>' ) ;
400- }
401-
402388function unstep ( name , request , failed ) {
403389 var time = process . hrtime ( ) ;
404390
@@ -415,14 +401,6 @@ function unstep(name, request, failed) {
415401 extension . stepGraph = extension . stepGraph . parent ;
416402}
417403
418- function makeGuid ( ) {
419- // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
420- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
421- var r = Math . random ( ) * 16 | 0 , v = c == 'x' ? r : ( r & 0x3 | 0x8 ) ;
422- return v . toString ( 16 ) ;
423- } ) ;
424- }
425-
426404function describePerformance ( root , request ) {
427405 var ret = { } ;
428406
@@ -461,7 +439,7 @@ function callStack(stack) {
461439}
462440
463441function describeTimings ( timing , root ) {
464- var id = makeGuid ( ) ;
442+ var id = uuid . v4 ( ) ;
465443 var name = timing . name ;
466444 var elapsedMs = diff ( timing . startTime , timing . stopTime ) ;
467445 var sinceRootMs = diff ( root . startTime , timing . startTime ) ;
@@ -513,15 +491,6 @@ function makeStep(name, time, parent){
513491 return { name : name , startTime : time , stopTime : null , parent : parent , steps : [ ] , customTimings : { } } ;
514492}
515493
516- function isObject ( val ) {
517- return Function . prototype . call . bind ( Object . prototype . toString ) ( val ) == '[object Object]' ;
518- }
519-
520- function isFunction ( func ) {
521- var getType = { } ;
522- return getType . toString . call ( func ) === '[object Function]' ;
523- }
524-
525494function instrument ( func , defaultName ) {
526495 var name = func . name || defaultName || 'Unnamed' ;
527496
@@ -533,7 +502,7 @@ function instrument(func, defaultName) {
533502 if ( args ) {
534503 for ( var i = 0 ; i < args . length ; i ++ ) {
535504 var arg = args [ i ] ;
536- if ( arg && isFunction ( arg ) ) {
505+ if ( arg && utils . isFunction ( arg ) ) {
537506 args [ i ] = instrument ( arg , name + ' arg #' + i ) ;
538507 }
539508 }
@@ -579,13 +548,13 @@ function addProfilingImpl(toInstrument) {
579548 continue ;
580549 }
581550
582- if ( isObject ( toWrap ) ) {
551+ if ( utils . isObject ( toWrap ) ) {
583552 addProfilingImpl ( toWrap ) ;
584553
585554 continue ;
586555 }
587556
588- if ( isFunction ( toWrap ) ) {
557+ if ( utils . isFunction ( toWrap ) ) {
589558 var wrappedFunc = instrument ( toWrap , prop ) ;
590559
591560 toInstrument [ prop ] = wrappedFunc ;
0 commit comments