What is promisepipe?
The 'promisepipe' npm package is designed to facilitate the piping of streams with the added benefit of returning a promise. This can be particularly useful for handling asynchronous stream operations in a more manageable and readable way.
What are promisepipe's main functionalities?
Basic Stream Piping
This feature allows you to pipe a readable stream to a writable stream and returns a promise that resolves when the piping is complete. In this example, a file is read from 'source.txt' and written to 'destination.txt'.
const fs = require('fs');
const promisepipe = require('promisepipe');
async function pipeStreams() {
const readStream = fs.createReadStream('source.txt');
const writeStream = fs.createWriteStream('destination.txt');
await promisepipe(readStream, writeStream);
console.log('Piping complete');
}
pipeStreams();
Handling Multiple Streams
This feature allows you to pipe multiple streams in sequence. In this example, a file is read from 'source.txt', compressed using gzip, and then written to 'destination.txt.gz'.
const fs = require('fs');
const zlib = require('zlib');
const promisepipe = require('promisepipe');
async function pipeMultipleStreams() {
const readStream = fs.createReadStream('source.txt');
const gzipStream = zlib.createGzip();
const writeStream = fs.createWriteStream('destination.txt.gz');
await promisepipe(readStream, gzipStream, writeStream);
console.log('Piping and compression complete');
}
pipeMultipleStreams();
Other packages similar to promisepipe
pump
The 'pump' package is used to pipe streams together and handle errors properly. It is similar to 'promisepipe' but does not return a promise. Instead, it uses a callback to signal completion or errors.
promisePipe
Safely pipe node.js streams while capturing all errors to a single promise.
API
promisePipe(<readable stream>, [transform streams...], <writeable stream>)
It returns a native promise. On success the resolved value will be an array of the
streams passed in. When rejected an error object is created with following
keys:
source
: The stream that caused the errororiginalError
: Original error from the streammessage
: The error message from original error
Notice that the promisePipe
function installs handlers for some events and cancels
them upon return, and this may include any event handler you installed before calling
this function.
Example
var promisePipe = require("promisepipe");
promisePipe(
fs.createReadStream(INPUT_FILE),
new UpcaseTransform(),
fs.createWriteStream(OUTPUT_FILE),
).then(function(streams){
console.log("Yay, all streams are now closed/ended/finished!");
}, function(err) {
console.log("This stream failed:", err.source);
console.log("Original error was:", err.originalError);
});
Install
npm install promisepipe
Why?
Stream piping in node.js is cool, but error handling is not because streams
do not bubble errors to the target streams.
For example if the previous example is written like this:
fs.createReadStream(INPUT_FILE)
.pipe(new UpcaseTransform())
.pipe(fs.createReadStream(OUTPUT_FILE))
It might crash your program at any time. You must handle the errors
from each stream manually like this:
fs.createReadStream(INPUT_FILE).on("error", function(err) {
}).pipe(new UpcaseTransform()).on("error", function(err) {
}).pipe(fs.createReadStream(OUTPUT_FILE)).on("error", function(err) {
})
Which is imo repeative and cumbersome (at least when you want to use promises).