# promisepipe

> Pipe node.js streams safely with Promises

Latest version **3.0.0** (published 2018-08-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install promisepipe
pnpm add promisepipe
yarn add promisepipe
bun add promisepipe
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2018-08-04 |
| First published | 2013-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 80 |
| Author | Esa-Matti Suuronen |
| Maintainers | esamatti, martin-kolarik |
| Keywords | promise, stream |

## Links

- npm: https://www.npmjs.com/package/promisepipe
- Repository: https://github.com/epeli/node-promisepipe
- Homepage: https://github.com/epeli/node-promisepipe#readme
- Issues: https://github.com/epeli/node-promisepipe/issues
- npm.io page: https://npm.io/package/promisepipe

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2018-08-04
- 2.1.3 — 2018-04-07
- 2.1.2 — 2018-03-08
- 2.1.1 — 2017-11-03
- 2.1.0 — 2017-11-03
- 2.0.0 — 2017-01-11
- 1.0.1 — 2013-10-15
- 1.0.0 — 2013-10-15

## README

# 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())`](https://www.npmjs.com/package/dev-null) to the end, otherwise the promise will never resolve ([#16](https://github.com/epeli/node-promisepipe/issues/16)).

Starting with v3, all streams are destroyed if there's an error to prevent memory leaks.

## Example

```javascript
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

```javascript
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:

```javascript
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:

```javascript
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.

---
_Source: https://npm.io/package/promisepipe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
