1
1
'use strict' ;
2
-
3
- const { emitExperimentalWarning } = require ( 'internal/util' ) ;
2
+ const {
3
+ convertToValidSignal,
4
+ emitExperimentalWarning
5
+ } = require ( 'internal/util' ) ;
4
6
const {
5
7
ERR_INVALID_ARG_TYPE ,
6
8
ERR_SYNTHETIC
7
9
} = require ( 'internal/errors' ) . codes ;
8
10
9
- const REPORTEVENTS = 1 ;
10
- const REPORTSIGNAL = 2 ;
11
- const REPORTFILENAME = 3 ;
12
- const REPORTPATH = 4 ;
13
- const REPORTVERBOSE = 5 ;
14
-
15
11
// If report is enabled, extract the binding and
16
12
// wrap the APIs with thin layers, with some error checks.
17
13
// user options can come in from CLI / ENV / API.
@@ -35,68 +31,56 @@ let config = {
35
31
const report = {
36
32
setDiagnosticReportOptions ( options ) {
37
33
emitExperimentalWarning ( 'report' ) ;
38
- // Reuse the null and undefined checks. Save
39
- // space when dealing with large number of arguments.
40
- const list = parseOptions ( options ) ;
34
+ const previousConfig = config ;
35
+ const newConfig = { } ;
41
36
42
- // Flush the stale entries from report, as
43
- // we are refreshing it, items that the users did not
44
- // touch may be hanging around stale otherwise.
45
- config = { } ;
37
+ if ( options === null || typeof options !== 'object' )
38
+ options = { } ;
46
39
47
- // The parseOption method returns an array that include
48
- // the indices at which valid params are present.
49
- list . forEach ( ( i ) => {
50
- switch ( i ) {
51
- case REPORTEVENTS :
52
- if ( Array . isArray ( options . events ) )
53
- config . events = options . events ;
54
- else
55
- throw new ERR_INVALID_ARG_TYPE ( 'events' ,
56
- 'Array' ,
57
- options . events ) ;
58
- break ;
59
- case REPORTSIGNAL :
60
- if ( typeof options . signal !== 'string' ) {
61
- throw new ERR_INVALID_ARG_TYPE ( 'signal' ,
62
- 'String' ,
63
- options . signal ) ;
64
- }
65
- process . removeListener ( config . signal , handleSignal ) ;
66
- if ( config . events . includes ( 'signal' ) )
67
- process . on ( options . signal , handleSignal ) ;
68
- config . signal = options . signal ;
69
- break ;
70
- case REPORTFILENAME :
71
- if ( typeof options . filename !== 'string' ) {
72
- throw new ERR_INVALID_ARG_TYPE ( 'filename' ,
73
- 'String' ,
74
- options . filename ) ;
75
- }
76
- config . filename = options . filename ;
77
- break ;
78
- case REPORTPATH :
79
- if ( typeof options . path !== 'string' )
80
- throw new ERR_INVALID_ARG_TYPE ( 'path' , 'String' , options . path ) ;
81
- config . path = options . path ;
82
- break ;
83
- case REPORTVERBOSE :
84
- if ( typeof options . verbose !== 'string' &&
85
- typeof options . verbose !== 'boolean' ) {
86
- throw new ERR_INVALID_ARG_TYPE ( 'verbose' ,
87
- 'Booelan | String' +
88
- ' (true|false|yes|no)' ,
89
- options . verbose ) ;
90
- }
91
- config . verbose = options . verbose ;
92
- break ;
93
- }
94
- } ) ;
95
- // Upload this new config to C++ land
96
- nr . syncConfig ( config , true ) ;
97
- } ,
40
+ if ( Array . isArray ( options . events ) )
41
+ newConfig . events = options . events . slice ( ) ;
42
+ else if ( options . events === undefined )
43
+ newConfig . events = [ ] ;
44
+ else
45
+ throw new ERR_INVALID_ARG_TYPE ( 'events' , 'Array' , options . events ) ;
46
+
47
+ if ( typeof options . filename === 'string' )
48
+ newConfig . filename = options . filename ;
49
+ else if ( options . filename === undefined )
50
+ newConfig . filename = '' ;
51
+ else
52
+ throw new ERR_INVALID_ARG_TYPE ( 'filename' , 'string' , options . filename ) ;
53
+
54
+ if ( typeof options . path === 'string' )
55
+ newConfig . path = options . path ;
56
+ else if ( options . path === undefined )
57
+ newConfig . path = '' ;
58
+ else
59
+ throw new ERR_INVALID_ARG_TYPE ( 'path' , 'string' , options . path ) ;
98
60
61
+ if ( typeof options . verbose === 'boolean' )
62
+ newConfig . verbose = options . verbose ;
63
+ else if ( options . verbose === undefined )
64
+ newConfig . verbose = false ;
65
+ else
66
+ throw new ERR_INVALID_ARG_TYPE ( 'verbose' , 'boolean' , options . verbose ) ;
99
67
68
+ if ( typeof options . signal === 'string' )
69
+ newConfig . signal = convertToValidSignal ( options . signal ) ;
70
+ else if ( options . signal === undefined )
71
+ newConfig . signal = 'SIGUSR2' ;
72
+ else
73
+ throw new ERR_INVALID_ARG_TYPE ( 'signal' , 'string' , options . signal ) ;
74
+
75
+ if ( previousConfig . signal )
76
+ process . removeListener ( previousConfig . signal , handleSignal ) ;
77
+
78
+ if ( newConfig . events . includes ( 'signal' ) )
79
+ process . on ( newConfig . signal , handleSignal ) ;
80
+
81
+ config = newConfig ;
82
+ nr . syncConfig ( config , true ) ;
83
+ } ,
100
84
triggerReport ( file , err ) {
101
85
emitExperimentalWarning ( 'report' ) ;
102
86
if ( err == null ) {
@@ -133,23 +117,6 @@ function handleSignal(signo) {
133
117
nr . onUserSignal ( signo ) ;
134
118
}
135
119
136
- function parseOptions ( obj ) {
137
- const list = [ ] ;
138
- if ( obj == null )
139
- return list ;
140
- if ( obj . events != null )
141
- list . push ( REPORTEVENTS ) ;
142
- if ( obj . signal != null )
143
- list . push ( REPORTSIGNAL ) ;
144
- if ( obj . filename != null )
145
- list . push ( REPORTFILENAME ) ;
146
- if ( obj . path != null )
147
- list . push ( REPORTPATH ) ;
148
- if ( obj . verbose != null )
149
- list . push ( REPORTVERBOSE ) ;
150
- return list ;
151
- }
152
-
153
120
module . exports = {
154
121
config,
155
122
handleSignal,
0 commit comments