From c00efe56f4fb07d27eb1d47915288b027b3ef692 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 28 Dec 2020 05:32:07 +0000 Subject: [PATCH] workaround asynchronous tty bugs on Node.js (#4473) --- bin/uglifyjs | 2 +- test/compress.js | 2 +- test/jetstream.js | 2 +- test/ufuzz/index.js | 2 +- tools/exit.js | 15 --------------- tools/tty.js | 22 ++++++++++++++++++++++ 6 files changed, 26 insertions(+), 19 deletions(-) delete mode 100644 tools/exit.js create mode 100644 tools/tty.js diff --git a/bin/uglifyjs b/bin/uglifyjs index 4d1fb5a2..ed01f986 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -3,7 +3,7 @@ "use strict"; -require("../tools/exit"); +require("../tools/tty"); var fs = require("fs"); var info = require("../package.json"); diff --git a/test/compress.js b/test/compress.js index f35159b0..29ea5a4f 100644 --- a/test/compress.js +++ b/test/compress.js @@ -1,6 +1,6 @@ "use strict"; -require("../tools/exit"); +require("../tools/tty"); var assert = require("assert"); var child_process = require("child_process"); diff --git a/test/jetstream.js b/test/jetstream.js index 1eebdc6b..25cf1290 100644 --- a/test/jetstream.js +++ b/test/jetstream.js @@ -5,7 +5,7 @@ var site = "https://browserbench.org/JetStream1.1"; if (typeof phantom == "undefined") { - require("../tools/exit"); + require("../tools/tty"); var args = process.argv.slice(2); var debug = args.indexOf("--debug"); if (debug < 0) { diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index 2eac44f0..e5bf0083 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -5,7 +5,7 @@ // 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 -require("../../tools/exit"); +require("../../tools/tty"); var UglifyJS = require("../.."); var randomBytes = require("crypto").randomBytes; diff --git a/tools/exit.js b/tools/exit.js deleted file mode 100644 index 4dd7141b..00000000 --- a/tools/exit.js +++ /dev/null @@ -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; -}; diff --git a/tools/tty.js b/tools/tty.js new file mode 100644 index 00000000..395c48ec --- /dev/null +++ b/tools/tty.js @@ -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; + }; +} -- 2.34.1