pump
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
end-of-stream | 1.4.4 | 2.33 kB | MIT | prod | |
once | 1.4.0 | 1.93 kB | ISC | prod |
Pump is a small and effective Node.js module that serves the purpose of piping streams together and destroys all of them if one closes. This module resolves the issue in Node.js where using source.pipe(dest)
, the source won't be destroyed if dest emits close or an error, and it's not possible to provide a callback when the pipe is finished. Pump handles these two issues seamlessly.
To incorporate Pump in your Node.js project, you first need to install it using npm by running npm install pump
in your terminal. Once you've done this, you can start using Pump in your code by requiring it like this var pump = require('pump')
. The basic usage is quite straightforward - you need to pass the streams you wish to pipe together to pump, and optionally add a callback function. Here's a basic example:
var pump = require('pump')
var fs = require('fs')
var source = fs.createReadStream('/dev/random')
var dest = fs.createWriteStream('/dev/null')
pump(source, dest, function(err) {
console.log('pipe finished', err)
})
setTimeout(function() {
dest.destroy() // when dest is closed pump will destroy source
}, 1000)
In this example, a source stream is created that reads from '/dev/random' and a destination stream is created to write to '/dev/null'. These streams are piped together using pump, and a callback is provided that logs when the pipe is finished. After 1 second, the destination stream is destroyed, which in turn causes pump to destroy the source stream.
Pump can also be used to pipe more than two streams together as well like this:
var transform = someTransformStream()
pump(source, transform, anotherTransform, dest, function(err) {
console.log('pipe finished', err)
})
In this case, if any of source
, transform
, anotherTransform
, or dest
closes, all of them will be annihilated.
The complete documentation and information for Pump can be found in its README file in the GitHub repository (git://github.com/mafintosh/pump.git). The README offers a detailed explanation about what the module does, how to use it with code examples, and the problems it can solve. Further information can be accessed through the provided link to the Mississippi Stream Utility Collection, where you can find related and useful stream modules.