11'use strict' ;
2-
3- const { emitExperimentalWarning } = require ( 'internal/util' ) ;
2+ const {
3+ convertToValidSignal,
4+ emitExperimentalWarning
5+ } = require ( 'internal/util' ) ;
46const {
57 ERR_INVALID_ARG_TYPE ,
68 ERR_SYNTHETIC
79} = require ( 'internal/errors' ) . codes ;
810
9- const REPORTEVENTS = 1 ;
10- const REPORTSIGNAL = 2 ;
11- const REPORTFILENAME = 3 ;
12- const REPORTPATH = 4 ;
13- const REPORTVERBOSE = 5 ;
14-
1511// If report is enabled, extract the binding and
1612// wrap the APIs with thin layers, with some error checks.
1713// user options can come in from CLI / ENV / API.
@@ -35,68 +31,56 @@ let config = {
3531const report = {
3632 setDiagnosticReportOptions ( options ) {
3733 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 = { } ;
4136
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 = { } ;
4639
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 ) ;
9860
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 ) ;
9967
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+ } ,
10084 triggerReport ( file , err ) {
10185 emitExperimentalWarning ( 'report' ) ;
10286 if ( err == null ) {
@@ -133,23 +117,6 @@ function handleSignal(signo) {
133117 nr . onUserSignal ( signo ) ;
134118}
135119
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-
153120module . exports = {
154121 config,
155122 handleSignal,
0 commit comments