Comparing version 0.0.1 to 0.1.0
@@ -1,15 +0,21 @@ | ||
module.exports = epipeFilter | ||
module.exports = epipeBomb | ||
function epipeFilter(err) { | ||
if (err.code === 'EPIPE') return process.exit() | ||
function epipeBomb(stream, callback) { | ||
if (stream == null) stream = process.stdout | ||
if (callback == null) callback = process.exit | ||
// If there's more than one error handler (ie, us), | ||
// then the error won't be bubbled up anyway | ||
if (process.stdout.listeners('error').length <= 1) { | ||
process.stdout.removeAllListeners() // Pretend we were never here | ||
process.stdout.emit('error', err) // Then emit as if we were never here | ||
process.stdout.on('error', epipeFilter) // Then reattach, ready for the next error! | ||
function epipeFilter(err) { | ||
if (err.code === 'EPIPE') return callback() | ||
// If there's more than one error handler (ie, us), | ||
// then the error won't be bubbled up anyway | ||
if (stream.listeners('error').length <= 1) { | ||
stream.removeAllListeners() // Pretend we were never here | ||
stream.emit('error', err) // Then emit as if we were never here | ||
stream.on('error', epipeFilter) // Then reattach, ready for the next error! | ||
} | ||
} | ||
stream.on('error', epipeFilter) | ||
} | ||
process.stdout.on('error', epipeFilter) |
// Without this line, you'll get an EPIPE error on: | ||
// 'node example.js | head -1' | ||
require('./epipebomb.js') | ||
require('./epipebomb.js')() | ||
for (var i = 0; i < 100; i++) console.log(i) |
@@ -5,3 +5,3 @@ { | ||
"description": "Destroy EPIPE errors when stdout runs through a truncated pipe", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"homepage": "https://github.com/mhart/epipebomb", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -0,1 +1,9 @@ | ||
# EPIPE Bomb | ||
By default, node throws `EPIPE` errors if `process.stdout` is being written to and | ||
a user runs it through a pipe that gets closed while the process is still outputting | ||
(eg, the simple case of piping a node app through `head`). | ||
This seemed a little overzealous to me, so I wrote this to suppress such errors. | ||
## Before | ||
@@ -26,3 +34,3 @@ | ||
```javascript | ||
require('epipebomb') | ||
require('epipebomb')() | ||
@@ -32,3 +40,3 @@ for (var i = 0; i < 100; i++) console.log(i) | ||
#### Oh the joy | ||
#### Oh the joy! | ||
```shell | ||
@@ -41,2 +49,2 @@ $ node example.js | head -1 | ||
Only the `EPIPE` error is captured on `process.stdout` - all other errors are thrown as per usual. | ||
Only the `EPIPE` error is captured on `process.stdout` - all other errors are thrown as per usual. |
2215
20
48