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

View File

@@ -3,7 +3,7 @@
"use strict"; "use strict";
require("../tools/exit"); require("../tools/tty");
var fs = require("fs"); var fs = require("fs");
var info = require("../package.json"); var info = require("../package.json");

View File

@@ -1,6 +1,6 @@
"use strict"; "use strict";
require("../tools/exit"); require("../tools/tty");
var assert = require("assert"); var assert = require("assert");
var child_process = require("child_process"); var child_process = require("child_process");

View File

@@ -5,7 +5,7 @@
var site = "https://browserbench.org/JetStream1.1"; var site = "https://browserbench.org/JetStream1.1";
if (typeof phantom == "undefined") { if (typeof phantom == "undefined") {
require("../tools/exit"); require("../tools/tty");
var args = process.argv.slice(2); var args = process.argv.slice(2);
var debug = args.indexOf("--debug"); var debug = args.indexOf("--debug");
if (debug < 0) { if (debug < 0) {

View File

@@ -5,7 +5,7 @@
// bin/uglifyjs s.js -c && bin/uglifyjs s.js -c passes=3 && bin/uglifyjs s.js -c passes=3 -m // bin/uglifyjs s.js -c && bin/uglifyjs s.js -c passes=3 && bin/uglifyjs s.js -c passes=3 -m
// cat s.js | node && node s.js && bin/uglifyjs s.js -c | node && bin/uglifyjs s.js -c passes=3 | node && bin/uglifyjs s.js -c passes=3 -m | node // cat s.js | node && node s.js && bin/uglifyjs s.js -c | node && bin/uglifyjs s.js -c passes=3 | node && bin/uglifyjs s.js -c passes=3 -m | node
require("../../tools/exit"); require("../../tools/tty");
var UglifyJS = require("../.."); var UglifyJS = require("../..");
var randomBytes = require("crypto").randomBytes; var randomBytes = require("crypto").randomBytes;

View File

@@ -1,15 +0,0 @@
// workaround for tty output truncation upon process.exit()
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;
};

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;
};
}