# writable-stream-and-promise

> Pipe a stream into a Promise.

Latest version **1.0.15** (published 2020-12-25) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install writable-stream-and-promise
pnpm add writable-stream-and-promise
yarn add writable-stream-and-promise
bun add writable-stream-and-promise
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.15 |
| Published | 2020-12-25 |
| First published | 2019-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Raymond Lam |
| Maintainers | raymond-lam |
| Keywords | stream, promise, writable |

## Links

- npm: https://www.npmjs.com/package/writable-stream-and-promise
- Repository: https://github.com/raymond-lam/writable-stream-and-promise
- Homepage: https://github.com/raymond-lam/writable-stream-and-promise#readme
- Issues: https://github.com/raymond-lam/writable-stream-and-promise/issues
- npm.io page: https://npm.io/package/writable-stream-and-promise

## 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

- 1.0.15 (latest) — 2020-12-25
- 1.0.14 — 2020-12-25
- 1.0.13 — 2020-12-25
- 1.0.12 — 2020-07-18
- 1.0.11 — 2020-07-05
- 1.0.10 — 2020-04-05
- 1.0.9 — 2020-03-14
- 1.0.8 — 2020-02-09
- 1.0.7 — 2019-12-29
- 1.0.6 — 2019-10-27
- 1.0.5 — 2019-09-02
- 1.0.4 — 2019-08-03
- 1.0.3 — 2019-05-05
- 1.0.2 — 2019-05-05
- 1.0.1 — 2019-02-25
- … 1 more at https://npm.io/package/writable-stream-and-promise/versions

## README

writable-stream-and-promise
===========================

[![npm](https://img.shields.io/npm/v/writable-stream-and-promise)](https://www.npmjs.com/package/writable-stream-and-promise)
[![CI](https://github.com/raymond-lam/writable-stream-and-promise/workflows/CI/badge.svg?branch=master)](https://github.com/raymond-lam/writable-stream-and-promise/actions)
[![Coverage Status](https://coveralls.io/repos/github/raymond-lam/writable-stream-and-promise/badge.svg?branch=master)](https://coveralls.io/github/raymond-lam/writable-stream-and-promise?branch=master)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)

Pipe a [stream](https://nodejs.org/api/stream.htm) into a Promise.

## Usage

### Example

```javascript
const {createReadStream} = require('fs');
const {writableStreamAndPromise} = require('writable-stream-and-promise');
const {createGunzip} = require('zlib');

const processZipFile = async path => {
  const [writable, promise] = writableStreamAndPromise();
  createReadStream(path).pipe(createGunzip()).pipe(writable);
  const contents = await promise;

  // ... process contents ...

  return processedContents;
};
```

### API

#### writableStreamAndPromise([options])

Returns a 2-tuple (Array): a [Writable](https://nodejs.org/api/stream.html#stream_writable_streams) [Buffer](https://nodejs.org/api/buffer.html) stream and a Promise. When the stream ends, the Promise resolves to a Buffer containing all the chunks written to the stream. The Promise rejects if the stream emits an error or when the stream is destroyed. Strings written to the stream will be decoded according to the given encoding or `defaultEncoding`. The stream does not destroy itself automatically after ending. The stream will emit a `'close'` event when it is destroyed.

- `options`
  - `defaultEncoding` The default encoding that is used when no encoding is specified when writing a String. The default is `'utf8'`.
  - `highWaterMark` Writes are completed immediately and synchronously, so this parameter is only really relevant if the stream is [cork](https://nodejs.org/api/stream.html#stream_writable_cork)ed. `highWaterMark` indicates the number of bytes that the stream should buffer while corked. The default is 16384 (16 kb). 
- Returns: A 2-tuple (Array of length 2), the first element a [Writable](https://nodejs.org/api/stream.html#stream_writable_streams) stream, and the second element a Promise.

#### writableObjectStreamAndPromise([options])

Returns a 2-tuple (Array): a [Writable](https://nodejs.org/api/stream.html#stream_writable_streams) [object stream](https://nodejs.org/api/stream.html#stream_object_mode) and a Promise. When the stream ends, the Promise resolves to an Array containing all the chunks written to the stream. The Promise rejects if the stream emits an error or when the stream is destroyed. The stream does not destroy itself automatically after ending. The stream will emit a `'close'` event when it is destroyed.

- options
  - `highWaterMark` Writes are completed immediately and synchronously, so this parameters is only really relevant if the stream is [cork](https://nodejs.org/api/stream.html#stream_writable_cork)ed. `highWaterMark` indicates the number of objects that the stream should buffer while corked. The default is 16.
- Returns: A 2-tuple (Array of length 2), the first element a [Writable](https://nodejs.org/api/stream.html#stream_writable_streams) stream, and the second element a Promise.

## Author

Raymond Lam (ray@lam-ray.com)

## License

MIT

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