@@ -93,6 +93,32 @@ function initializeLogging(logger: logging.Logger) {
93
93
break ;
94
94
}
95
95
96
- output . write ( color ( entry . message ) + '\n' ) ;
96
+ // If we do console.log(message) or process.stdout.write(message + '\n'), the process might
97
+ // stop before the whole message is written and the stream is flushed. This happens when
98
+ // streams are asynchronous.
99
+ //
100
+ // NodeJS IO streams are different depending on platform and usage. In POSIX environment,
101
+ // for example, they're asynchronous when writing to a pipe, but synchronous when writing
102
+ // to a TTY. In windows, it's the other way around. You can verify which is which with
103
+ // stream.isTTY and platform, but this is not good enough.
104
+ // In the async case, one should wait for the callback before sending more data or
105
+ // continuing the process. In our case it would be rather hard to do (but not impossible).
106
+ //
107
+ // Instead we take the easy way out and simply chunk the message and call the write
108
+ // function while the buffer drain itself asynchronously. With a smaller chunk size than
109
+ // the buffer, we are mostly certain that it works. In this case, the chunk has been picked
110
+ // as half a page size (4096/2 = 2048), minus some bytes for the color formatting.
111
+ // On POSIX it seems the buffer is 2 pages (8192), but just to be sure (could be different
112
+ // by platform).
113
+ //
114
+ // For more details, see https://nodejs.org/api/process.html#process_a_note_on_process_i_o
115
+ const chunkSize = 2000 ; // Small chunk.
116
+ let message = entry . message ;
117
+ while ( message ) {
118
+ const chunk = message . slice ( 0 , chunkSize ) ;
119
+ message = message . slice ( chunkSize ) ;
120
+ output . write ( color ( chunk ) ) ;
121
+ }
122
+ output . write ( '\n' ) ;
97
123
} ) ;
98
124
}
0 commit comments