4343 DIR_MODE = 448 /* 0o700 */ ,
4444 FILE_MODE = 384 /* 0o600 */ ,
4545
46- EVENT = 'exit' ,
46+ EXIT = 'exit' ,
47+
48+ SIGINT = 'SIGINT' ,
4749
4850 // this will hold the objects need to be removed on exit
4951 _removeObjects = [ ] ;
@@ -101,7 +103,7 @@ function _isUndefined(obj) {
101103 */
102104function _parseArguments ( options , callback ) {
103105 /* istanbul ignore else */
104- if ( typeof options == 'function' ) {
106+ if ( typeof options === 'function' ) {
105107 return [ { } , options ] ;
106108 }
107109
@@ -132,7 +134,7 @@ function _generateTmpName(opts) {
132134 var template = opts . template ;
133135 // make sure that we prepend the tmp path if none was given
134136 /* istanbul ignore else */
135- if ( path . basename ( template ) == template )
137+ if ( path . basename ( template ) === template )
136138 template = path . join ( opts . dir || tmpDir , template ) ;
137139 return template . replace ( TEMPLATE_PATTERN , _randomChars ( 6 ) ) ;
138140 }
@@ -479,7 +481,7 @@ function _prepareRemoveCallback(removeFunction, arg, cleanupCallbackSync) {
479481
480482 called = true ;
481483 // sync?
482- if ( removeFunction . length == 1 ) {
484+ if ( removeFunction . length === 1 ) {
483485 try {
484486 removeFunction ( arg ) ;
485487 return next ( null ) ;
@@ -550,7 +552,7 @@ function isENOENT(error) {
550552 * error.errno n/a
551553 */
552554function isExpectedError ( error , code , errno ) {
553- return error . code == code || error . code == errno ;
555+ return error . code === code || error . code = == errno ;
554556}
555557
556558/**
@@ -569,60 +571,87 @@ function setGracefulCleanup() {
569571 * @returns {Boolean } true whether listener is a legacy listener
570572 */
571573function _is_legacy_listener ( listener ) {
572- return ( listener . name == '_exit' || listener . name == '_uncaughtExceptionThrown' )
574+ return ( listener . name === '_exit' || listener . name = == '_uncaughtExceptionThrown' )
573575 && listener . toString ( ) . indexOf ( '_garbageCollector();' ) > - 1 ;
574576}
575577
576578/**
577- * Safely install process exit listeners.
578- *
579+ * Safely install SIGINT listener.
580+ *
581+ * NOTE: this will only work on OSX and Linux.
582+ *
579583 * @private
580584 */
581- function _safely_install_listener ( ) {
582- var listeners = process . listeners ( EVENT ) ;
585+ function _safely_install_sigint_listener ( ) {
583586
584- // collect any existing listeners
585- var existingListeners = [ ] ;
586- for ( var i = 0 , length = listeners . length ; i < length ; i ++ ) {
587- var lstnr = listeners [ i ] ;
587+ const listeners = process . listeners ( SIGINT ) ;
588+ const existingListeners = [ ] ;
589+ for ( let i = 0 , length = listeners . length ; i < length ; i ++ ) {
590+ const lstnr = listeners [ i ] ;
588591 /* istanbul ignore else */
589- if ( lstnr . name == '_tmp$safe_listener' || _is_legacy_listener ( lstnr ) ) {
590- // we must forget about the uncaughtException listener
591- if ( lstnr . name != '_uncaughtExceptionThrown' ) existingListeners . push ( lstnr ) ;
592- process . removeListener ( EVENT , lstnr ) ;
592+ if ( lstnr . name === '_tmp$sigint_listener' ) {
593+ existingListeners . push ( lstnr ) ;
594+ process . removeListener ( SIGINT , lstnr ) ;
593595 }
594596 }
595-
596- // windows does not support signals
597- // it'd never had won if it wasn't a major PITA
598- // with node v8.x and win 10 this is no longer an issue
599- if ( process . platform == 'win32' ) {
600- var rl = require ( 'readline' ) . createInterface ( {
601- input : process . stdin ,
602- output : process . stdout
603- } ) ;
604-
605- rl . on ( 'SIGINT' , function ( ) {
606- process . emit ( 'SIGINT' ) ;
607- } ) ;
608- }
609-
610- process . on ( 'SIGINT' , function ( ) {
611- process . exit ( 0 ) ;
597+ process . on ( SIGINT , function _tmp$sigint_listener ( doExit ) {
598+ for ( let i = 0 , length = existingListeners . length ; i < length ; i ++ ) {
599+ // let the existing listener do the garbage collection (e.g. jest sandbox)
600+ try {
601+ existingListeners [ i ] ( false ) ;
602+ } catch ( err ) {
603+ // ignore
604+ }
605+ }
606+ try {
607+ // force the garbage collector even it is called again in the exit listener
608+ _garbageCollector ( ) ;
609+ } finally {
610+ if ( ! ! doExit ) {
611+ process . exit ( 0 ) ;
612+ }
613+ }
612614 } ) ;
615+ }
616+
617+ /**
618+ * Safely install process exit listener.
619+ *
620+ * @private
621+ */
622+ function _safely_install_exit_listener ( ) {
623+ const listeners = process . listeners ( EXIT ) ;
613624
614- process . addListener ( EVENT , function _tmp$safe_listener ( data ) {
625+ // collect any existing listeners
626+ const existingListeners = [ ] ;
627+ for ( let i = 0 , length = listeners . length ; i < length ; i ++ ) {
628+ const lstnr = listeners [ i ] ;
615629 /* istanbul ignore else */
616- if ( existingListeners . length ) {
617- for ( var i = 0 , length = existingListeners . length ; i < length ; i ++ ) {
630+ // TODO: remove support for legacy listeners once release 1.0.0 is out
631+ if ( lstnr . name === '_tmp$safe_listener' || _is_legacy_listener ( lstnr ) ) {
632+ // we must forget about the uncaughtException listener, hopefully it is ours
633+ if ( lstnr . name !== '_uncaughtExceptionThrown' ) {
634+ existingListeners . push ( lstnr ) ;
635+ }
636+ process . removeListener ( EXIT , lstnr ) ;
637+ }
638+ }
639+ // TODO: what was the data parameter good for?
640+ process . addListener ( EXIT , function _tmp$safe_listener ( data ) {
641+ for ( let i = 0 , length = existingListeners . length ; i < length ; i ++ ) {
642+ // let the existing listener do the garbage collection (e.g. jest sandbox)
643+ try {
618644 existingListeners [ i ] ( data ) ;
645+ } catch ( err ) {
646+ // ignore
619647 }
620648 }
621649 _garbageCollector ( ) ;
622650 } ) ;
623651}
624652
625- _safely_install_listener ( ) ;
653+ _safely_install_exit_listener ( ) ;
654+ _safely_install_sigint_listener ( ) ;
626655
627656/**
628657 * Configuration options.
0 commit comments