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
|
```
|
6
|
npm install pump
|
7
|
```
|
8
|
|
9
|
[![build status](http://img.shields.io/travis/mafintosh/pump.svg?style=flat)](http://travis-ci.org/mafintosh/pump)
|
10
|
|
11
|
## What problem does it solve?
|
12
|
|
13
|
When using standard `source.pipe(dest)` source will _not_ be destroyed if dest emits close or an error.
|
14
|
You are also not able to provide a callback to tell when then pipe has finished.
|
15
|
|
16
|
pump does these two things for you
|
17
|
|
18
|
## Usage
|
19
|
|
20
|
Simply pass the streams you want to pipe together to pump and add an optional callback
|
21
|
|
22
|
``` js
|
23
|
var pump = require('pump')
|
24
|
var fs = require('fs')
|
25
|
|
26
|
var source = fs.createReadStream('/dev/random')
|
27
|
var dest = fs.createWriteStream('/dev/null')
|
28
|
|
29
|
pump(source, dest, function(err) {
|
30
|
console.log('pipe finished', err)
|
31
|
})
|
32
|
|
33
|
setTimeout(function() {
|
34
|
dest.destroy() // when dest is closed pump will destroy source
|
35
|
}, 1000)
|
36
|
```
|
37
|
|
38
|
You can use pump to pipe more than two streams together as well
|
39
|
|
40
|
``` js
|
41
|
var transform = someTransformStream()
|
42
|
|
43
|
pump(source, transform, anotherTransform, dest, function(err) {
|
44
|
console.log('pipe finished', err)
|
45
|
})
|
46
|
```
|
47
|
|
48
|
If `source`, `transform`, `anotherTransform` or `dest` closes all of them will be destroyed.
|
49
|
|
50
|
Similarly to `stream.pipe()`, `pump()` returns the last stream passed in, so you can do:
|
51
|
|
52
|
```
|
53
|
return pump(s1, s2) // returns s2
|
54
|
```
|
55
|
|
56
|
If you want to return a stream that combines *both* s1 and s2 to a single stream use
|
57
|
[pumpify](https://github.com/mafintosh/pumpify) instead.
|
58
|
|
59
|
## License
|
60
|
|
61
|
MIT
|
62
|
|
63
|
## Related
|
64
|
|
65
|
`pump` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one.
|