1 | # EPIPE Bomb
|
2 |
|
3 | By default, node throws `EPIPE` errors if `process.stdout` is being written to and
|
4 | a 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 |
|
7 | This 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
|
23 | tick
|
24 | tick
|
25 | tick
|
26 | tick
|
27 | tick
|
28 | tick
|
29 | tick
|
30 | tick
|
31 | tick
|
32 | tick
|
33 |
|
34 | events.js:66
|
35 | throw arguments[1]; // Unhandled 'error' event
|
36 | ^
|
37 | Error: 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
|
46 | require('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
|
57 | tick
|
58 | tick
|
59 | tick
|
60 | tick
|
61 | tick
|
62 | tick
|
63 | tick
|
64 | tick
|
65 | tick
|
66 | tick
|
67 | ```
|
68 |
|
69 | ## CLI usage (Node 4.x and up)
|
70 |
|
71 | Require `epipebomb/register` from the command line
|
72 |
|
73 | ```shell
|
74 | node -r epipebomb/register some-script.js | head
|
75 | ```
|
76 |
|
77 | or use `epipebomb` as a drop-in replacement for `node`
|
78 |
|
79 | ```shell
|
80 | epipebomb some-script.js | head
|
81 | ```
|
82 |
|
83 | ## Notes
|
84 |
|
85 | Only the `EPIPE` error is captured on `process.stdout` - all other errors are thrown as per usual.
|