# promise-duplex

> Return promise for duplex stream

Latest version **8.0.0** (published 2024-06-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-duplex
pnpm add promise-duplex
yarn add promise-duplex
bun add promise-duplex
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2024-06-21 |
| First published | 2017-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.0.0 |
| Dependencies | 2 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Piotr Roszatycki |
| Keywords | promise, duplex, stream |

## Links

- npm: https://www.npmjs.com/package/promise-duplex
- Repository: https://github.com/dex4er/js-promise-duplex
- Homepage: http://github.com/dex4er/js-promise-duplex
- Issues: https://github.com/dex4er/js-promise-duplex/issues
- npm.io page: https://npm.io/package/promise-duplex

## Dependencies (2)

- [promise-readable](https://npm.io/package/promise-readable.md) ^8.0.1
- [promise-writable](https://npm.io/package/promise-writable.md) ^8.0.0

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

- 8.0.0 (latest) — 2024-06-21
- 7.0.0 — 2024-06-21
- 6.0.0 — 2020-08-10
- 5.0.3 — 2019-12-25
- 5.0.2 — 2019-10-07
- 5.0.1 — 2019-10-07
- 5.0.0 — 2019-07-15
- 4.0.3 — 2019-06-04
- 4.0.2 — 2019-05-10
- 4.0.1 — 2019-05-10
- 4.0.0 — 2019-05-10
- 3.0.3 — 2018-03-12
- 3.0.2 — 2018-02-05
- 3.0.1 — 2018-02-05
- 3.0.0 — 2018-02-04
- … 13 more at https://npm.io/package/promise-duplex/versions

## README

# promise-duplex

<!-- markdownlint-disable MD013 -->

[![GitHub](https://img.shields.io/github/v/release/dex4er/js-promise-duplex?display_name=tag&sort=semver)](https://github.com/dex4er/js-promise-duplex)
[![CI](https://github.com/dex4er/js-promise-duplex/actions/workflows/ci.yaml/badge.svg)](https://github.com/dex4er/js-promise-duplex/actions/workflows/ci.yaml)
[![Trunk Check](https://github.com/dex4er/js-promise-duplex/actions/workflows/trunk.yaml/badge.svg)](https://github.com/dex4er/js-promise-duplex/actions/workflows/trunk.yaml)
[![Coverage Status](https://coveralls.io/repos/github/dex4er/js-promise-duplex/badge.svg)](https://coveralls.io/github/dex4er/js-promise-duplex)
[![npm](https://img.shields.io/npm/v/promise-duplex.svg)](https://www.npmjs.com/package/promise-duplex)

<!-- markdownlint-enable MD013 -->

This module allows to convert
[`Duplex`](https://nodejs.org/api/stream.html#stream_class_stream_duplex) stream
into its promisified version, which returns
[`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
object fulfilled when stream's events occurred.

The module combines
[`promise-readable`](https://www.npmjs.com/package/promise-readable) and
[`promise-`](https://www.npmjs.com/package/promise-) in one.

## Requirements

This module requires Node >= 16.

## Installation

```shell
npm install promise-duplex
```

## Usage

```js
import PromiseDuplex from "promise-duplex"
```

### constructor

```js
const promiseDuplex = new PromiseDuplex(stream)
```

`PromiseDuplex` object requires `Duplex` object to work.

_Example:_

```js
import net from "node:net"
import PromiseDuplex from "promise-duplex"

const stream = new net.Socket()

const promiseDuplex = new PromiseDuplex(stream)
```

### stream

```js
const stream = promiseDuplex.stream
```

Original stream object.

_Example:_

```js
console.log(promiseDuplex.stream.localAddress)
```

### read

```js
const chunk = await promiseDuplex.read(chunkSize)
```

Check
[`PromiseReadable.read`](https://www.npmjs.com/package/promise-readable#read)
for details.

### readAll

```js
const content = await promiseDuplex.readAll()
```

Check
[`PromiseReadable.readAll`](https://www.npmjs.com/package/promise-readable#readall)
for details.

### iterate

```js
for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}
```

Check
[`PromiseReadable.iterate`](https://www.npmjs.com/package/promise-readable#iterate)
for details.

### Symbol.asyncIterator

```js
for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}
```

Check
[`PromiseReadable[Symbol.asyncIterator]`](https://www.npmjs.com/package/promise-readable#symbolasynciterator)
for details.

### setEncoding

```js
promiseDuplex = await promiseDuplex.setEncoding(encoding)
```

Check
[`PromiseReadable.setEncoding`](https://www.npmjs.com/package/promise-readable#setencoding)
for details.

### write

```js
await promiseDuplex.write(chunk)
```

Check
[`PromiseWritable.write`](https://www.npmjs.com/package/promise-#write)
for details.

### writeAll

```js
await promiseDuplex.writeAll(content, chunkSize)
```

Check
[`PromiseWritable.writeAll`](https://www.npmjs.com/package/promise-#writeall)
for details.

### end

```js
await promiseDuplex.end()
```

Check
[`PromiseWritable.once`](https://www.npmjs.com/package/promise-#end)
for details.

### once

```js
const result = await promiseDuplex.once(event)
```

Check
[`PromiseReadable.once`](https://www.npmjs.com/package/promise-readable#once)
and
[`PromiseWritable.once`](https://www.npmjs.com/package/promise-#once)
for details.

### destroy

```js
promiseDuplex = promiseDuplex.destroy()
```

This method calls `destroy` method on stream and cleans up all own handlers.

The method returns this object.

## See also

[`PromiseReadable`](https://www.npmjs.com/package/promise-readable),
[`PromiseWritable`](https://www.npmjs.com/package/promise-),
[`PromiseSocket`](https://www.npmjs.com/package/promise-socket),
[`PromisePiping`](https://www.npmjs.com/package/promise-piping).

## License

Copyright (c) 2017-2024 Piotr Roszatycki <piotr.roszatycki@gmail.com>

[MIT](https://opensource.org/licenses/MIT)

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