workaround asynchronous tty bugs on Node.js (#4473)

This commit is contained in:
Alex Lam S.L
2020-12-28 05:32:07 +00:00
committed by GitHub
parent 28bcdbd7df
commit c00efe56f4
6 changed files with 26 additions and 19 deletions

22
tools/tty.js Normal file
View File

@@ -0,0 +1,22 @@
// workaround for tty output truncation on Node.js
try {
// prevent buffer overflow and other asynchronous bugs
process.stdout._handle.setBlocking(true);
process.stderr._handle.setBlocking(true);
} catch (e) {
// ensure output buffers are flushed before process termination
var exit = process.exit;
process.exit = function() {
var args = [].slice.call(arguments);
process.once("uncaughtException", function() {
(function callback() {
if (process.stdout.bufferSize || process.stderr.bufferSize) {
setTimeout(callback, 1);
} else {
exit.apply(process, args);
}
})();
});
throw exit;
};
}