Giter Site home page Giter Site logo

jedwards1211 / node-promisepipe Goto Github PK

View Code? Open in Web Editor NEW

This project forked from esamattis/node-promisepipe

0.0 2.0 0.0 32 KB

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

Home Page: https://npmjs.org/package/promisepipe

License: MIT License

Logos 0.19% JavaScript 99.81%

node-promisepipe's Introduction

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 error
  • originalError: Original error from the stream
  • message: The error message from original error

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) {
    // 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
})

Which is imo repeative and cumbersome (at least when you want to use promises).

node-promisepipe's People

Contributors

esamattis avatar graingert avatar wildskyf avatar marekventur avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.