1 | # pump
|
2 |
|
3 | pump is a small node module that pipes streams together and destroys all of them if one of them closes.
|
4 |
|
5 | npm install pump
|
6 |
|
7 | ## What problem does it solve?
|
8 |
|
9 | When using standard `source.pipe(dest)` source will _not_ be destroyed if dest emits close or an error.
|
10 | You are also not able to provide a callback to tell when then pipe has finished.
|
11 |
|
12 | pump does these two things for you
|
13 |
|
14 | ## Usage
|
15 |
|
16 | Simply pass the streams you want to pipe together to pump and add an optional callback
|
17 |
|
18 | ``` js
|
19 | var pump = require('pump');
|
20 | var fs = require('fs');
|
21 |
|
22 | var source = fs.createReadStream('/dev/random');
|
23 | var dest = fs.createWriteStream('/dev/null');
|
24 |
|
25 | pump(source, dest, function(err) {
|
26 | console.log('pipe finished', err);
|
27 | });
|
28 |
|
29 | setTimeout(function() {
|
30 | dest.destroy(); // when dest is closed pump will destroy source
|
31 | }, 1000);
|
32 | ```
|
33 |
|
34 | You can use pump to pipe more than two streams together as well
|
35 |
|
36 | ``` js
|
37 | var transform = someTransformStream();
|
38 |
|
39 | pump(source, transform, anotherTransform, dest, function(err) {
|
40 | console.log('pipe finished', err);
|
41 | });
|
42 | ```
|
43 |
|
44 | If `source`, `transform`, `anotherTransform` or `dest` closes all of them will be destroyed.
|
45 |
|
46 | ## License
|
47 |
|
48 | MIT
|