aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-12-28 05:32:07 +0000
committerGitHub <noreply@github.com>2020-12-28 13:32:07 +0800
commitc00efe56f4fb07d27eb1d47915288b027b3ef692 (patch)
tree6649bb0c894d8d188c2d4706e135edec82cdea92
parent28bcdbd7df403a4dca8bd2b7f327b65c8491235d (diff)
downloadtracifyjs-c00efe56f4fb07d27eb1d47915288b027b3ef692.tar.gz
tracifyjs-c00efe56f4fb07d27eb1d47915288b027b3ef692.zip
workaround asynchronous tty bugs on Node.js (#4473)
-rwxr-xr-xbin/uglifyjs2
-rw-r--r--test/compress.js2
-rw-r--r--test/jetstream.js2
-rw-r--r--test/ufuzz/index.js2
-rw-r--r--tools/exit.js15
-rw-r--r--tools/tty.js22
6 files changed, 26 insertions, 19 deletions
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;
+ };
+}