UNPKG

1.35 kBMarkdownView Raw
1# EPIPE Bomb
2
3By default, node throws `EPIPE` errors if `process.stdout` is being written to and
4a user runs it through a pipe that gets closed while the process is still outputting
5(eg, the simple case of piping a node app through `head`).
6
7This seemed a little overzealous to me, so I wrote this to suppress such errors.
8
9## Before
10
11#### example.js
12```javascript
13;(function log() {
14 console.log('tick')
15 process.nextTick(log)
16})()
17```
18
19#### Oh the humanity
20
21```shell
22$ node example.js | head
23tick
24tick
25tick
26tick
27tick
28tick
29tick
30tick
31tick
32tick
33
34events.js:66
35 throw arguments[1]; // Unhandled 'error' event
36 ^
37Error: write EPIPE
38 at errnoException (net.js:782:11)
39 at Object.afterWrite (net.js:600:19)
40```
41
42## After
43
44#### example.js
45```javascript
46require('epipebomb')()
47
48;(function log() {
49 console.log('tick')
50 process.nextTick(log)
51})()
52```
53
54#### Oh the joy!
55```shell
56$ node example.js | head
57tick
58tick
59tick
60tick
61tick
62tick
63tick
64tick
65tick
66tick
67```
68
69## CLI usage (Node 4.x and up)
70
71Require `epipebomb/register` from the command line
72
73```shell
74node -r epipebomb/register some-script.js | head
75```
76
77or use `epipebomb` as a drop-in replacement for `node`
78
79```shell
80epipebomb some-script.js | head
81```
82
83## Notes
84
85Only the `EPIPE` error is captured on `process.stdout` - all other errors are thrown as per usual.