43
43
DIR_MODE = 448 /* 0o700 */ ,
44
44
FILE_MODE = 384 /* 0o600 */ ,
45
45
46
- EVENT = 'exit' ,
46
+ EXIT = 'exit' ,
47
+
48
+ SIGINT = 'SIGINT' ,
47
49
48
50
// this will hold the objects need to be removed on exit
49
51
_removeObjects = [ ] ;
@@ -101,7 +103,7 @@ function _isUndefined(obj) {
101
103
*/
102
104
function _parseArguments ( options , callback ) {
103
105
/* istanbul ignore else */
104
- if ( typeof options == 'function' ) {
106
+ if ( typeof options === 'function' ) {
105
107
return [ { } , options ] ;
106
108
}
107
109
@@ -132,7 +134,7 @@ function _generateTmpName(opts) {
132
134
var template = opts . template ;
133
135
// make sure that we prepend the tmp path if none was given
134
136
/* istanbul ignore else */
135
- if ( path . basename ( template ) == template )
137
+ if ( path . basename ( template ) === template )
136
138
template = path . join ( opts . dir || tmpDir , template ) ;
137
139
return template . replace ( TEMPLATE_PATTERN , _randomChars ( 6 ) ) ;
138
140
}
@@ -479,7 +481,7 @@ function _prepareRemoveCallback(removeFunction, arg, cleanupCallbackSync) {
479
481
480
482
called = true ;
481
483
// sync?
482
- if ( removeFunction . length == 1 ) {
484
+ if ( removeFunction . length === 1 ) {
483
485
try {
484
486
removeFunction ( arg ) ;
485
487
return next ( null ) ;
@@ -550,7 +552,7 @@ function isENOENT(error) {
550
552
* error.errno n/a
551
553
*/
552
554
function isExpectedError ( error , code , errno ) {
553
- return error . code == code || error . code == errno ;
555
+ return error . code === code || error . code = == errno ;
554
556
}
555
557
556
558
/**
@@ -569,60 +571,87 @@ function setGracefulCleanup() {
569
571
* @returns {Boolean } true whether listener is a legacy listener
570
572
*/
571
573
function _is_legacy_listener ( listener ) {
572
- return ( listener . name == '_exit' || listener . name == '_uncaughtExceptionThrown' )
574
+ return ( listener . name === '_exit' || listener . name = == '_uncaughtExceptionThrown' )
573
575
&& listener . toString ( ) . indexOf ( '_garbageCollector();' ) > - 1 ;
574
576
}
575
577
576
578
/**
577
- * Safely install process exit listeners.
578
- *
579
+ * Safely install SIGINT listener.
580
+ *
581
+ * NOTE: this will only work on OSX and Linux.
582
+ *
579
583
* @private
580
584
*/
581
- function _safely_install_listener ( ) {
582
- var listeners = process . listeners ( EVENT ) ;
585
+ function _safely_install_sigint_listener ( ) {
583
586
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 ] ;
588
591
/* 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 ) ;
593
595
}
594
596
}
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
+ }
612
614
} ) ;
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 ) ;
613
624
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 ] ;
615
629
/* 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 {
618
644
existingListeners [ i ] ( data ) ;
645
+ } catch ( err ) {
646
+ // ignore
619
647
}
620
648
}
621
649
_garbageCollector ( ) ;
622
650
} ) ;
623
651
}
624
652
625
- _safely_install_listener ( ) ;
653
+ _safely_install_exit_listener ( ) ;
654
+ _safely_install_sigint_listener ( ) ;
626
655
627
656
/**
628
657
* Configuration options.
0 commit comments