You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7 KiB

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