All Projects → esamattis → Node Promisepipe

esamattis / Node Promisepipe

Licence: mit
Safely pipe node.js streams while capturing all errors to a single promise

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Node Promisepipe

Node Instagram
Instagram api client for node that support promises.
Stars: ✭ 185 (+134.18%)
Mutual labels:  stream, promise
Scramjet
Simple yet powerful live data computation framework
Stars: ✭ 171 (+116.46%)
Mutual labels:  stream, promise
Write
Write data to the file system, creating any intermediate directories if they don't already exist. Used by flat-cache and many others!
Stars: ✭ 68 (-13.92%)
Mutual labels:  stream, promise
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+8983.54%)
Mutual labels:  stream, promise
futura
Asynchronous Swift made easy. The project was made by Miquido. https://www.miquido.com/
Stars: ✭ 34 (-56.96%)
Mutual labels:  stream, promise
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (-75.95%)
Mutual labels:  stream, promise
Bach
Compose your async functions with elegance.
Stars: ✭ 117 (+48.1%)
Mutual labels:  stream, promise
wise-river
Object streaming the way it should be.
Stars: ✭ 33 (-58.23%)
Mutual labels:  stream, promise
tish
A replacement of shell script with TypeScript, for those who love TypeScript and tired of writing shell script, aiming to emulate shell script in TypeScript.
Stars: ✭ 119 (+50.63%)
Mutual labels:  stream, promise
Download
Download and extract files
Stars: ✭ 1,064 (+1246.84%)
Mutual labels:  stream, promise
Graphql Batch
A query batching executor for the graphql gem
Stars: ✭ 1,164 (+1373.42%)
Mutual labels:  promise
Target Postgres
A Singer.io Target for Postgres
Stars: ✭ 70 (-11.39%)
Mutual labels:  stream
Redis Dataloader
Batching and Caching layer using Redis as the Caching layer
Stars: ✭ 72 (-8.86%)
Mutual labels:  promise
Athenax
SQL-based streaming analytics platform at scale
Stars: ✭ 1,178 (+1391.14%)
Mutual labels:  stream
Fast Dat Parser
Superfast blockchain parser for stats
Stars: ✭ 68 (-13.92%)
Mutual labels:  stream
Smalltalk
Promise-based Alert, Confirm and Prompt replacement
Stars: ✭ 76 (-3.8%)
Mutual labels:  promise
Freeiptv
FreeIPTV • Watch Free IPTV World Wide
Stars: ✭ 68 (-13.92%)
Mutual labels:  stream
Socket.io Rpc
Extend your promises across a network with socket.io
Stars: ✭ 67 (-15.19%)
Mutual labels:  promise
Sake Core
Sake's core interface.
Stars: ✭ 78 (-1.27%)
Mutual labels:  promise
String To Stream
Convert a string into a stream (streams2)
Stars: ✭ 75 (-5.06%)
Mutual labels:  stream

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 error
  • originalError: Original error from the stream
  • message: 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 = require("promisepipe");

promisePipe(
    fs.createReadStream(INPUT_FILE),
    new UpcaseTransform(),
    fs.createWriteStream(OUTPUT_FILE),
).then(function(streams){
    console.log("Done writing to the output file stream!");
}, function(err) {
    console.log("This stream failed:", err.source);
    console.log("Original error was:", err.originalError);
});

or with async-wait

var promisePipe = require("promisepipe");

(async () => {
  try {
    await promisePipe(
      fs.createReadStream(INPUT_FILE),
      new UpcaseTransform(),
      fs.createWriteStream(OUTPUT_FILE)
    );
    console.log("Done writing to the output file stream!");
  } catch (err) {
    console.log("This stream failed:", err.source);
    console.log("Original error was:", err.originalError);
  }
})();

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) {
    // handle the error
}).pipe(new UpcaseTransform()).on("error", function(err) {
    // handle the error
}).pipe(fs.createReadStream(OUTPUT_FILE)).on("error", function(err) {
    // handle the error
})

Handling errors this way can be very cumbersome. promisepipe simplifies error handling by sending the first error occurance into a promise.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].