@@ -6,38 +6,55 @@ const {
66
77const NOOP = ( ) => undefined ;
88
9- module . exports = ( opts = { } , closeFunctions = [ ] ) => {
10- const options = Object . assign (
11- {
12- timeout : 2000 ,
13- catchPromisesReject : true ,
14- } ,
15- opts
16- ) ;
9+ const DEFAULT_EVENTS = [
10+ "SIGINT" ,
11+ "SIGTERM" ,
12+ "uncaughtException" ,
13+ "unhandledRejection" ,
14+ ] ;
1715
18- let events = [ "SIGINT" , "SIGTERM" , "uncaughtException" ] ;
16+ function validateParameters ( timeout , events , callbacks ) {
17+ if ( typeof timeout !== "number" ) {
18+ throw new TypeError ( "timeout parameter must be number" ) ;
19+ }
20+ if ( ! Array . isArray ( events ) ) {
21+ throw new TypeError ( "events parameter must be an array of strings" ) ;
22+ }
1923
20- if ( options . catchPromisesReject ) {
21- events . push ( "unhandledRejection" ) ;
24+ if ( ! Array . isArray ( callbacks ) ) {
25+ throw new TypeError ( "callbacks parameter must be an array of functions" ) ;
26+ } else {
27+ callbacks . map ( ( cb ) => {
28+ if ( typeof cb !== "function" ) {
29+ throw new TypeError ( "callback must be a function" ) ;
30+ }
31+ } ) ;
2232 }
33+ }
34+
35+ module . exports = function fine (
36+ timeout = 2000 ,
37+ events = DEFAULT_EVENTS ,
38+ callbacks = [ ]
39+ ) {
40+ validateParameters ( timeout , events , callbacks ) ;
41+
2342 for ( const event of events ) {
2443 process . once ( event , ( ) => {
2544 const code = event . match ( "^SIG" ) ? 0 : 1 ;
2645 process . stdout . write ( `[${ event } ] exiting with code ${ code } \n` ) ;
27- if ( Array . isArray ( closeFunctions ) ) {
28- for ( const cb of closeFunctions ) {
29- try {
30- if ( typeof cb === "function" ) {
31- const p = cb ( ) ;
32- if ( isPromise ( p ) ) p . catch ( NOOP ) ;
33- }
34- // eslint-disable-next-line no-empty
35- } catch ( _ ) { }
36- }
46+ for ( const cb of callbacks ) {
47+ try {
48+ if ( typeof cb === "function" ) {
49+ const p = cb ( ) ;
50+ if ( isPromise ( p ) ) p . catch ( NOOP ) ;
51+ }
52+ // eslint-disable-next-line no-empty
53+ } catch ( _ ) { }
3754 }
3855 setTimeout ( ( ) => {
3956 process . exit ( code ) ;
40- } , options . timeout ) . unref ( ) ;
57+ } , timeout ) ;
4158 } ) ;
4259 }
4360} ;
0 commit comments