improve `process.exit()` workaround (#2741)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 7 Jan 2018 09:53:50 +0000 (17:53 +0800)
committerGitHub <noreply@github.com>
Sun, 7 Jan 2018 09:53:50 +0000 (17:53 +0800)
- use public API
- fix issue with Node.js 0.10 on WIndows

bin/uglifyjs
test/jetstream.js
test/ufuzz.js
tools/exit.js [new file with mode: 0644]

index 718397c..3891713 100755 (executable)
@@ -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");
index 1cc6d4a..7ee09df 100644 (file)
@@ -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) {
index de6c317..d02e9f7 100644 (file)
@@ -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 (file)
index 0000000..17048d8
--- /dev/null
@@ -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;
+};