promisePipe
Safely pipe node.js streams while capturing all errors to a single promise.
Install
npm install promisepipe
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
Note: the last stream in the chain needs to be a writable stream, not a duplex/transform stream. If you use a 3rd party library which returns deplux streams instead of writable streams, you'll need to add something like .pipe(devnull())
to the end, otherwise the promise will never resolve (#16).
Starting with v3, all streams are destroyed if there's an error to prevent memory leaks.
Example
var promisePipe = ; ;
or with async-wait
var promisePipe = ; async { try await ; console; catch err console; console; };
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
It might crash your program at any time. You must handle the errors from each stream manually like this:
fs
Handling errors this way can be very cumbersome. promisepipe
simplifies
error handling by sending the first error occurance into a promise.