From 42c89146f5b56d3cf215a77bc65de5b7884c1a58 Mon Sep 17 00:00:00 2001
From: Alexander Imfeld <alexander.imfeld@netcetera.com>
Date: Tue, 26 Nov 2013 09:12:45 +0100
Subject: [PATCH] Use callback from empty write instead of drain event but
 don't prevent further writing (inspired by @chrisgladd)

---
 README.md                   | 4 ----
 lib/exit.js                 | 4 +---
 test/exit_test.js           | 4 +---
 test/fixtures/log-broken.js | 3 ---
 test/fixtures/log.js        | 3 ---
 5 files changed, 2 insertions(+), 16 deletions(-)

diff --git a/README.md b/README.md
index a022301..01a6764 100644
--- a/README.md
+++ b/README.md
@@ -22,10 +22,6 @@ console.error("yay");
 
 // process.exit(5);
 exit(5);
-
-// These lines shouldn't appear in the output.
-console.log("wtf");
-console.error("bro");
 ```
 
 ## Don't believe me? Try it for yourself.
diff --git a/lib/exit.js b/lib/exit.js
index 3c411ed..203da6b 100644
--- a/lib/exit.js
+++ b/lib/exit.js
@@ -18,13 +18,11 @@ module.exports = function exit(exitCode, streams) {
     }
   }
   streams.forEach(function(stream) {
-    // Prevent further writing.
-    stream.write = function() {};
     // Count drained streams now, but monitor non-drained streams.
     if (stream.bufferSize === 0) {
       drainCount++;
     } else {
-      stream.once('drain', function() {
+      stream.write('', 'utf-8', function() {
         drainCount++;
         tryToExit();
       });
diff --git a/test/exit_test.js b/test/exit_test.js
index a91afb9..835618a 100644
--- a/test/exit_test.js
+++ b/test/exit_test.js
@@ -83,15 +83,13 @@ function buildTests() {
       pipes.forEach(function(pipe) {
         var command = 'node log.js 0 ' + count + output + ' 2>&1' + pipe;
         exports['exit']['output (' + command + ')'] = function(test) {
-          test.expect(2);
+          test.expect(1);
           run(command, function(code, actual) {
             var expected = fixture(count + output.replace(/ /g, '-') + '.txt');
             // Sometimes, the actual file lines are out of order on Windows.
             // But since the point of this lib is to drain the buffer and not
             // guarantee output order, we only test the length.
             test.equal(actual.length, expected.length, 'should be the same length.');
-            // The "fail" lines in log.js should NOT be output!
-            test.ok(actual.indexOf('fail') === -1, 'should not output after exit is called.');
             test.done();
           });
         };
diff --git a/test/fixtures/log-broken.js b/test/fixtures/log-broken.js
index 74c8f12..b6b6599 100644
--- a/test/fixtures/log-broken.js
+++ b/test/fixtures/log-broken.js
@@ -18,6 +18,3 @@ for (var i = 0; i < max; i++) {
 }
 
 process.exit(errorCode);
-
-stdout('fail');
-stderr('fail');
diff --git a/test/fixtures/log.js b/test/fixtures/log.js
index 8a9ed9a..6ba8893 100644
--- a/test/fixtures/log.js
+++ b/test/fixtures/log.js
@@ -20,6 +20,3 @@ for (var i = 0; i < max; i++) {
 }
 
 exit(errorCode);
-
-stdout('fail');
-stderr('fail');