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.

74 lines
2.4 KiB

1 month 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. Note that `pump` attaches error handlers to the streams to do internal error handling, so if `s2` emits an
  38. error in the above scenario, it will not trigger a `proccess.on('uncaughtException')` if you do not listen for it.
  39. If you want to return a stream that combines *both* s1 and s2 to a single stream use
  40. [pumpify](https://github.com/mafintosh/pumpify) instead.
  41. ## License
  42. MIT
  43. ## Related
  44. `pump` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one.
  45. ## For enterprise
  46. Available as part of the Tidelift Subscription.
  47. The maintainers of pump and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-pump?utm_source=npm-pump&utm_medium=referral&utm_campaign=enterprise)