From 9809567dfc4d5c3f23c7e6424da9a471a964584a Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 7 Jan 2018 17:53:50 +0800 Subject: [PATCH] improve `process.exit()` workaround (#2741) - use public API - fix issue with Node.js 0.10 on WIndows --- bin/uglifyjs | 6 +----- test/jetstream.js | 6 +----- test/ufuzz.js | 6 +----- tools/exit.js | 15 +++++++++++++++ 4 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 tools/exit.js diff --git a/bin/uglifyjs b/bin/uglifyjs index 718397c1..38917137 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -3,11 +3,7 @@ "use strict"; -// workaround for tty output truncation upon process.exit() -[process.stdout, process.stderr].forEach(function(stream){ - if (stream._handle && stream._handle.setBlocking) - stream._handle.setBlocking(true); -}); +require("../tools/exit"); var fs = require("fs"); var info = require("../package.json"); diff --git a/test/jetstream.js b/test/jetstream.js index 1cc6d4a7..7ee09df3 100644 --- a/test/jetstream.js +++ b/test/jetstream.js @@ -5,11 +5,7 @@ var site = "http://browserbench.org/JetStream"; if (typeof phantom == "undefined") { - // workaround for tty output truncation upon process.exit() - [process.stdout, process.stderr].forEach(function(stream){ - if (stream._handle && stream._handle.setBlocking) - stream._handle.setBlocking(true); - }); + require("../tools/exit"); var args = process.argv.slice(2); var debug = args.indexOf("--debug"); if (debug >= 0) { diff --git a/test/ufuzz.js b/test/ufuzz.js index de6c317c..d02e9f76 100644 --- a/test/ufuzz.js +++ b/test/ufuzz.js @@ -6,11 +6,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 -// workaround for tty output truncation upon process.exit() -[process.stdout, process.stderr].forEach(function(stream){ - if (stream._handle && stream._handle.setBlocking) - stream._handle.setBlocking(true); -}); +require("../tools/exit"); var UglifyJS = require(".."); var randomBytes = require("crypto").randomBytes; diff --git a/tools/exit.js b/tools/exit.js new file mode 100644 index 00000000..17048d8e --- /dev/null +++ b/tools/exit.js @@ -0,0 +1,15 @@ +// 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) { + setImmediate(callback); + } else { + exit.apply(process, args); + } + })(); + }); + throw exit; +}; -- 2.34.1