@@ -5,84 +5,50 @@ const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
5
5
6
6
exports . setup = setupProcessWarnings ;
7
7
8
- let options ;
9
- function lazyOption ( name ) {
10
- if ( ! options ) {
11
- options = require ( 'internal/options' ) ;
8
+ // Lazily loaded
9
+ let fs ;
10
+ let fd ;
11
+ let warningFile ;
12
+
13
+ function lazyOption ( ) {
14
+ // This will load `warningFile` only once. If the flag is not set,
15
+ // `warningFile` will be set to an empty string.
16
+ if ( warningFile === undefined ) {
17
+ warningFile = require ( 'internal/options' )
18
+ . getOptionValue ( '--redirect-warnings' ) ;
12
19
}
13
- return options . getOptionValue ( name ) ;
20
+ return warningFile ;
14
21
}
15
22
16
- var cachedFd ;
17
- var acquiringFd = false ;
18
- function nop ( ) { }
19
-
20
- // Lazily loaded
21
- var fs = null ;
22
-
23
23
function writeOut ( message ) {
24
24
if ( console && typeof console . error === 'function' )
25
25
return console . error ( message ) ;
26
26
process . _rawDebug ( message ) ;
27
27
}
28
28
29
- function onClose ( fd ) {
30
- return ( ) => {
31
- if ( fs === null ) fs = require ( 'fs' ) ;
29
+ function writeToFile ( message ) {
30
+ if ( fd === undefined ) {
31
+ fs = require ( 'fs' ) ;
32
32
try {
33
- fs . closeSync ( fd ) ;
34
- } catch { }
35
- } ;
36
- }
37
-
38
- function onOpen ( cb ) {
39
- return ( err , fd ) => {
40
- acquiringFd = false ;
41
- if ( fd !== undefined ) {
42
- cachedFd = fd ;
43
- process . on ( 'exit' , onClose ( fd ) ) ;
44
- }
45
- cb ( err , fd ) ;
46
- process . emit ( '_node_warning_fd_acquired' , err , fd ) ;
47
- } ;
48
- }
49
-
50
- function onAcquired ( message ) {
51
- // make a best effort attempt at writing the message
52
- // to the fd. Errors are ignored at this point.
53
- return ( err , fd ) => {
54
- if ( err )
33
+ fd = fs . openSync ( warningFile , 'a' ) ;
34
+ } catch {
55
35
return writeOut ( message ) ;
56
- if ( fs === null ) fs = require ( 'fs' ) ;
57
- fs . appendFile ( fd , `${ message } \n` , nop ) ;
58
- } ;
59
- }
60
-
61
- function acquireFd ( warningFile , cb ) {
62
- if ( cachedFd === undefined && ! acquiringFd ) {
63
- acquiringFd = true ;
64
- if ( fs === null ) fs = require ( 'fs' ) ;
65
- fs . open ( warningFile , 'a' , onOpen ( cb ) ) ;
66
- } else if ( cachedFd !== undefined && ! acquiringFd ) {
67
- cb ( null , cachedFd ) ;
68
- } else {
69
- process . once ( '_node_warning_fd_acquired' , cb ) ;
70
- }
71
- }
72
-
73
- function output ( message ) {
74
- const warningFile = lazyOption ( '--redirect-warnings' ) ;
75
- if ( warningFile ) {
76
- acquireFd ( warningFile , onAcquired ( message ) ) ;
77
- return ;
36
+ }
37
+ process . on ( 'exit' , ( ) => {
38
+ try {
39
+ fs . closeSync ( fd ) ;
40
+ } catch { }
41
+ } ) ;
78
42
}
79
- writeOut ( message ) ;
43
+ fs . appendFile ( fd , `${ message } \n` , ( err ) => {
44
+ if ( err ) {
45
+ writeOut ( message ) ;
46
+ }
47
+ } ) ;
80
48
}
81
49
82
50
function doEmitWarning ( warning ) {
83
- return ( ) => {
84
- process . emit ( 'warning' , warning ) ;
85
- } ;
51
+ return ( ) => process . emit ( 'warning' , warning ) ;
86
52
}
87
53
88
54
function setupProcessWarnings ( ) {
@@ -107,7 +73,11 @@ function setupProcessWarnings() {
107
73
if ( typeof warning . detail === 'string' ) {
108
74
msg += `\n${ warning . detail } ` ;
109
75
}
110
- output ( msg ) ;
76
+ const warningFile = lazyOption ( ) ;
77
+ if ( warningFile ) {
78
+ return writeToFile ( msg ) ;
79
+ }
80
+ writeOut ( msg ) ;
111
81
} ) ;
112
82
}
113
83
0 commit comments