1 | module.exports = epipeBomb
|
2 |
|
3 | function epipeBomb(stream, callback) {
|
4 | if (stream == null) stream = process.stdout
|
5 | if (callback == null) callback = process.exit
|
6 |
|
7 | function epipeFilter(err) {
|
8 | if (err.code === 'EPIPE') return callback()
|
9 |
|
10 | // If there's more than one error handler (ie, us),
|
11 | // then the error won't be bubbled up anyway
|
12 | if (stream.listeners('error').length <= 1) {
|
13 | stream.removeAllListeners() // Pretend we were never here
|
14 | stream.emit('error', err) // Then emit as if we were never here
|
15 | stream.on('error', epipeFilter) // Then reattach, ready for the next error!
|
16 | }
|
17 | }
|
18 |
|
19 | stream.on('error', epipeFilter)
|
20 | }
|
21 |
|