22
33//Disable trace handling inside trace handler to avoid recursion
44//Set to true to prevent action till loading is complete -- set to false at end
5- var reentrantDisable = true ;
5+ let reentrantDisable = true ;
66
7- var process = require ( 'process' ) ;
8-
9- var lazyloadDone = false ;
10- var path ;
11- var fs ;
7+ let lazyloadDone = false ;
8+ let path ;
9+ let fs ;
1210
1311///////////////////////////////
1412//Trace calls
@@ -21,7 +19,7 @@ var fs;
2119*@result 're-entrant' 'disabled', 'no-sample', 'fail', 'success'
2220*/
2321function emitTrace ( emitKind , optInfo ) {
24- var res = emitTrace_helper ( emitKind , optInfo ) ;
22+ const res = emitTrace_helper ( emitKind , optInfo ) ;
2523 if ( res . flag === 'success' ) {
2624 //This is an intentional programatic breakpoint
2725 // -- it is only triggerable in --replay-debug mode
@@ -32,7 +30,7 @@ function emitTrace(emitKind, optInfo) {
3230exports . emitTrace = emitTrace ;
3331
3432function buildTraceResult ( flagv , actionv ) {
35- var realAction = actionv || function ( ) { } ;
33+ const realAction = actionv || function ( ) { } ;
3634 return { flag : flagv , action : realAction } ;
3735}
3836
@@ -60,7 +58,7 @@ function emitTrace_helper(emitKind, optInfo) {
6058
6159 //Process a synchronous write action for a unique trace bin
6260 // -- otherwise a async action on a multiple trace bin
63- var sampleRes = buildTraceResult ( 'no-sample' ) ;
61+ let sampleRes = buildTraceResult ( 'no-sample' ) ;
6462 if ( emitKind === 'emitOnExit' ||
6563 emitKind === 'emitOnException' ||
6664 emitKind === 'emitOnSigInt' ) {
@@ -86,13 +84,13 @@ function emitTrace_helper(emitKind, optInfo) {
8684//Helpers for trace calls
8785function emitSyncTraceKind ( emitKind , optInfo ) {
8886 //build up trace name
89- var traceName = emitKind ;
87+ let traceName = emitKind ;
9088 if ( emitKind === 'emitOnExit' ) {
9189 traceName += ( '_code-' + optInfo ) ;
9290 }
9391
9492 //invoke the trace writer and remote manager if needed
95- var resolvedPath = createTraceLogTarget ( traceName ) ;
93+ const resolvedPath = createTraceLogTarget ( traceName ) ;
9694 if ( ! resolvedPath ) {
9795 return buildTraceResult ( 'fail' ) ;
9896 }
@@ -116,19 +114,19 @@ function emitSyncTraceKind(emitKind, optInfo) {
116114
117115function emitAsyncTraceKind ( emitKind ) {
118116 //get trace stack and check if we want to emit
119- var stk = generateFuzzyStack ( emitKind ) ;
120- var entry = checkBinShouldEmit ( stk ) ;
117+ const stk = generateFuzzyStack ( emitKind ) ;
118+ const entry = checkBinShouldEmit ( stk ) ;
121119 if ( ! entry ) {
122120 return buildTraceResult ( 'no-sample' ) ;
123121 }
124122
125123 //build up trace name
126- var traceBucketName = `${ emitKind } _${ stk . fbin } _bucket-${ entry . bucketId } ` ;
127- var traceDirName = `trace-${ entry . traceCtr } ` ;
128- var traceName = path . join ( traceBucketName , traceDirName ) ;
124+ const traceBucketName = `${ emitKind } _${ stk . fbin } _bucket-${ entry . bucketId } ` ;
125+ const traceDirName = `trace-${ entry . traceCtr } ` ;
126+ const traceName = path . join ( traceBucketName , traceDirName ) ;
129127
130128 //invoke the trace writer and remote manager if needed
131- var resolvedPath = createTraceLogTarget ( traceName ) ;
129+ const resolvedPath = createTraceLogTarget ( traceName ) ;
132130 if ( ! resolvedPath ) {
133131 return buildTraceResult ( 'fail' ) ;
134132 }
@@ -154,17 +152,17 @@ function emitAsyncTraceKind(emitKind) {
154152
155153//create a directory for emitting the trace (if possible) and return it
156154function createTraceLogTarget ( tracename ) {
157- var traceRootDir = emitOptions . localTraceDirectory ||
155+ const traceRootDir = emitOptions . localTraceDirectory ||
158156 path . dirname ( process . mainModule . filename ) ;
159157
160158 // Add the PID to the trace name
161159 tracename = `${ tracename } _pid${ process . pid } ` ;
162160
163- var resolvedTracePath =
161+ const resolvedTracePath =
164162 path . resolve ( traceRootDir , '_diagnosticTraces' , tracename ) ;
165163
166164 //ensure directory exists and is empty...
167- var ok = ensureTraceTarget ( resolvedTracePath ) ;
165+ const ok = ensureTraceTarget ( resolvedTracePath ) ;
168166 if ( ! ok ) {
169167 return undefined ;
170168 }
@@ -179,7 +177,7 @@ function ensureTraceTarget(pth) {
179177 return false ;
180178 }
181179
182- var okdir = createTargetDirectory ( pth ) ;
180+ const okdir = createTargetDirectory ( pth ) ;
183181 if ( ! okdir ) {
184182 return false ;
185183 }
@@ -189,7 +187,7 @@ function ensureTraceTarget(pth) {
189187
190188function createTargetDirectory ( pth ) {
191189 //see if it just exists and, if so, just return true
192- var accessok = fs . constants . R_OK | fs . constants . W_OK | fs . constants . X_OK ;
190+ const accessok = fs . constants . R_OK | fs . constants . W_OK | fs . constants . X_OK ;
193191 try {
194192 fs . accessSync ( pth , accessok ) ;
195193 if ( fs . statSync ( pth ) . isDirectory ( ) ) {
@@ -198,9 +196,9 @@ function createTargetDirectory(pth) {
198196 } catch ( ei ) { }
199197
200198 //walk up the directory to see where the first valid part of the path is
201- var prefixPath = pth ;
202- var suffixPaths = [ ] ;
203- var baseFound = false ;
199+ let prefixPath = pth ;
200+ const suffixPaths = [ ] ;
201+ let baseFound = false ;
204202 do {
205203 //check for bad prefix
206204 if ( prefixPath === path . dirname ( prefixPath ) ) {
@@ -233,14 +231,14 @@ function createTargetDirectory(pth) {
233231
234232function deleteTargetDirectoryContents ( pth ) {
235233 try {
236- var items = fs . readdirSync ( pth ) ;
234+ const items = fs . readdirSync ( pth ) ;
237235 for ( var i = 0 ; i < items . length ; i ++ ) {
238- var fpath = path . resolve ( pth , items [ i ] ) ;
239- var stats = fs . lstatSync ( fpath ) ;
236+ const fpath = path . resolve ( pth , items [ i ] ) ;
237+ const stats = fs . lstatSync ( fpath ) ;
240238 if ( stats . isFile ( ) ) {
241239 fs . unlinkSync ( fpath ) ;
242240 } else if ( stats . isDirectory ( ) ) {
243- var recok = deleteTargetDirectoryContents ( fpath ) ;
241+ const recok = deleteTargetDirectoryContents ( fpath ) ;
244242 if ( ! recok ) {
245243 return false ;
246244 }
@@ -262,7 +260,7 @@ function deleteTargetDirectoryContents(pth) {
262260function updateGlobalSampleStats ( eventKind ) {
263261 currentSampleRate [ eventKind ] *= emitOptions . backoffFactors [ eventKind ] ;
264262
265- var updateTime = new Date ( ) ;
263+ const updateTime = new Date ( ) ;
266264 emitMinTimeValue . emitOnLogWarn = new Date ( updateTime ) ;
267265 emitMinTimeValue . emitOnLogError = new Date ( updateTime ) ;
268266
@@ -274,7 +272,7 @@ function updateGlobalSampleStats(eventKind) {
274272///////////////////////////////
275273//Trace emit manager code
276274
277- var emitOptions = {
275+ const emitOptions = {
278276 emitOnExit : 'error' , //emit a trace on exit -- off, error, all
279277 emitOnException : true , //emit a trace on uncaught execption
280278 emitOnSigInt : true , //emit a trace on sigint
@@ -313,30 +311,30 @@ var emitOptions = {
313311 remoteTraceManagerObj : undefined //manager object for remote trace support
314312} ;
315313
316- var callStackEmitInfoMap = new Map ( ) ;
317- var bucketCtr = 0 ;
314+ const callStackEmitInfoMap = new Map ( ) ;
315+ let bucketCtr = 0 ;
318316
319- var emitMinTimeValue = {
317+ const emitMinTimeValue = {
320318 emitOnLogWarn : new Date ( 0 ) ,
321319 emitOnLogError : new Date ( 0 ) ,
322320 emitOnAssert : new Date ( 0 )
323321} ;
324322
325- var currentSampleRate = { } ;
326- for ( var srp in emitOptions . initialRates ) {
323+ const currentSampleRate = { } ;
324+ for ( const srp in emitOptions . initialRates ) {
327325 currentSampleRate [ srp ] = emitOptions . initialRates [ srp ] ;
328326}
329327
330328/*
331329*Update emitOptions from the given options object
332330*/
333331function setOptions ( optionsObj ) {
334- for ( var opt in optionsObj ) {
332+ for ( const opt in optionsObj ) {
335333 //TODO: need more error checking on the validity of the option values
336334 emitOptions [ opt ] = optionsObj [ opt ] ;
337335 }
338336
339- for ( var srp in emitOptions . initialRates ) {
337+ for ( const srp in emitOptions . initialRates ) {
340338 currentSampleRate [ srp ] = emitOptions . initialRates [ srp ] ;
341339 }
342340}
@@ -363,7 +361,7 @@ function checkGlobalShouldEmit(emitKind, optInfo) {
363361 return false ;
364362 }
365363
366- var sampleInterval = new Date ( ) - emitMinTimeValue [ emitKind ] ;
364+ const sampleInterval = new Date ( ) - emitMinTimeValue [ emitKind ] ;
367365
368366 //Don't sample too often no matter what (or we can basically live lock)
369367 if ( sampleInterval < emitOptions . globalMinInterval ) {
@@ -372,8 +370,8 @@ function checkGlobalShouldEmit(emitKind, optInfo) {
372370
373371 //Relax our global rate if it has been a while
374372 if ( sampleInterval >= emitOptions . globalBackoffCancelInterval ) {
375- var currRate = currentSampleRate [ emitKind ] ;
376- var blRate = emitOptions . baselineRates [ emitKind ] ;
373+ const currRate = currentSampleRate [ emitKind ] ;
374+ const blRate = emitOptions . baselineRates [ emitKind ] ;
377375 currentSampleRate [ emitKind ] = Math . max ( currRate , blRate ) ;
378376 }
379377
@@ -388,15 +386,15 @@ function checkGlobalShouldEmit(emitKind, optInfo) {
388386*@result if we want to sample return the entry otherwise undefined
389387*/
390388function checkBinShouldEmit ( fuzzyStack ) {
391- var entry = resolveStackEntry ( fuzzyStack ) ;
389+ const entry = resolveStackEntry ( fuzzyStack ) ;
392390
393391 //stop sampling after max sampled values -- e.g. we don't need 100 repros
394392 if ( entry . traceCtr > emitOptions . binMaxSampled ) {
395393 return undefined ;
396394 }
397395
398396 //check if we want to sample on this entry -- we don't need every hit on this
399- var sampleProb = Math . pow ( emitOptions . binBackoffFactor , entry . traceCtr ) ;
397+ const sampleProb = Math . pow ( emitOptions . binBackoffFactor , entry . traceCtr ) ;
400398 return ( Math . random ( ) < sampleProb ) ? entry : undefined ;
401399}
402400
@@ -416,7 +414,7 @@ function resolveStackEntry(fuzzyStack) {
416414 if ( ! callStackEmitInfoMap . has ( fuzzyStack . hash ) ) {
417415 callStackEmitInfoMap . set ( fuzzyStack . hash , [ ] ) ;
418416 }
419- var stackList = callStackEmitInfoMap . get ( fuzzyStack . hash ) ;
417+ const stackList = callStackEmitInfoMap . get ( fuzzyStack . hash ) ;
420418
421419 for ( var i = 0 ; i < stackList . length ; ++ i ) {
422420 if ( eqFuzzyStacks ( fuzzyStack , stackList [ i ] . stack ) ) {
@@ -450,7 +448,7 @@ function directIsAbsoluteW32(pth) {
450448 if ( len === 0 )
451449 return false ;
452450
453- var code = pth . charCodeAt ( 0 ) ;
451+ let code = pth . charCodeAt ( 0 ) ;
454452 if ( code === 47 /*/*/ || code === 92 /*\*/ ) {
455453 return true ;
456454 } else if ( ( code >= 65 /*A*/ && code <= 90 /*Z*/ ) ||
@@ -470,7 +468,7 @@ function directIsAbsolutePosix(pth) {
470468 return pth . length > 0 && pth . charCodeAt ( 0 ) === 47 /*/*/ ;
471469}
472470
473- var directIsAbsolute = ( process . platform === 'win32' ) ?
471+ const directIsAbsolute = ( process . platform === 'win32' ) ?
474472 directIsAbsoluteW32 :
475473 directIsAbsolutePosix ;
476474
@@ -479,7 +477,7 @@ var directIsAbsolute = (process.platform === 'win32') ?
479477*/
480478function generateFuzzyStack ( eventKind ) {
481479 //Create an array of the file/lines for user space code in the call stack
482- var errstk = new Error ( )
480+ let errstk = new Error ( )
483481 . stack
484482 . split ( '\n' )
485483 . slice ( 1 )
@@ -490,14 +488,14 @@ function generateFuzzyStack(eventKind) {
490488 return directIsAbsolute ( frame ) ;
491489 } ) ;
492490
493- var lastframe = errstk [ errstk . length - 1 ] ;
494- var fname = lastframe . substr ( lastframe . lastIndexOf ( path . sep ) + 1 )
491+ const lastframe = errstk [ errstk . length - 1 ] ;
492+ const fname = lastframe . substr ( lastframe . lastIndexOf ( path . sep ) + 1 )
495493 . replace ( '.js:' , '_line-' )
496494 . replace ( ':' , '_column-' ) ;
497495
498496 //Identify which frames are recursive (appear multiple times in the stack)
499- var recFrames = new Map ( ) ;
500- var hasRecFrames = false ;
497+ const recFrames = new Map ( ) ;
498+ let hasRecFrames = false ;
501499 for ( var i = 0 ; i < errstk . length ; ++ i ) {
502500 if ( recFrames . has ( errstk [ i ] ) ) {
503501 hasRecFrames = true ;
@@ -509,13 +507,13 @@ function generateFuzzyStack(eventKind) {
509507
510508 if ( hasRecFrames ) {
511509 //Compress any recursive frames
512- var cpos = 0 ;
513- var fpos = 0 ;
510+ let cpos = 0 ;
511+ let fpos = 0 ;
514512 while ( fpos < errstk . length ) {
515513 if ( recFrames . get ( errstk [ fpos ] ) ) {
516- var recArray = [ ] ;
517- var spanpos = fpos ;
518- var spanend = errstk . lastIndexOf ( errstk [ fpos ] ) ;
514+ const recArray = [ ] ;
515+ let spanpos = fpos ;
516+ let spanend = errstk . lastIndexOf ( errstk [ fpos ] ) ;
519517 while ( spanpos <= spanend ) {
520518 if ( recArray . indexOf ( errstk [ spanpos ] ) === - 1 ) {
521519 recArray . push ( errstk [ spanpos ] ) ;
@@ -540,7 +538,7 @@ function generateFuzzyStack(eventKind) {
540538 errstk = errstk . slice ( 0 , cpos ) ;
541539 }
542540
543- var chash = 5381 ;
541+ let chash = 5381 ;
544542 for ( i = 0 ; i < errstk . length ; ++ i ) {
545543 if ( Array . isArray ( errstk [ i ] ) ) {
546544 for ( var j = 0 ; j < errstk [ i ] . length ; ++ j ) {
0 commit comments