@@ -5,84 +5,50 @@ const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
55
66exports . setup = setupProcessWarnings ;
77
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' ) ;
1219 }
13- return options . getOptionValue ( name ) ;
20+ return warningFile ;
1421}
1522
16- var cachedFd ;
17- var acquiringFd = false ;
18- function nop ( ) { }
19-
20- // Lazily loaded
21- var fs = null ;
22-
2323function writeOut ( message ) {
2424 if ( console && typeof console . error === 'function' )
2525 return console . error ( message ) ;
2626 process . _rawDebug ( message ) ;
2727}
2828
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' ) ;
3232 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 {
5535 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+ } ) ;
7842 }
79- writeOut ( message ) ;
43+ fs . appendFile ( fd , `${ message } \n` , ( err ) => {
44+ if ( err ) {
45+ writeOut ( message ) ;
46+ }
47+ } ) ;
8048}
8149
8250function doEmitWarning ( warning ) {
83- return ( ) => {
84- process . emit ( 'warning' , warning ) ;
85- } ;
51+ return ( ) => process . emit ( 'warning' , warning ) ;
8652}
8753
8854function setupProcessWarnings ( ) {
@@ -107,7 +73,11 @@ function setupProcessWarnings() {
10773 if ( typeof warning . detail === 'string' ) {
10874 msg += `\n${ warning . detail } ` ;
10975 }
110- output ( msg ) ;
76+ const warningFile = lazyOption ( ) ;
77+ if ( warningFile ) {
78+ return writeToFile ( msg ) ;
79+ }
80+ writeOut ( msg ) ;
11181 } ) ;
11282 }
11383
0 commit comments