# duplex-to

> Wraps a duplex stream and hides the readable or the writable interface

Latest version **2.0.0** (published 2022-10-06) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-10-06 |
| First published | 2019-11-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/duplex-to) |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | duplex, readable, stream, writable |

## Links

- npm: https://www.npmjs.com/package/duplex-to
- Repository: https://github.com/bergos/duplex-to
- Issues: https://github.com/bergos/duplex-to/issues
- npm.io page: https://npm.io/package/duplex-to

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

- 2.0.0 (latest) — 2022-10-06
- 1.0.1 — 2021-02-25
- 1.0.0 — 2019-11-07

## README

# duplex-to
[![build status](https://img.shields.io/github/workflow/status/bergos/duplex-to/Test)](https://github.com/bergos/duplex-to/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/duplex-to.svg)](https://www.npmjs.com/package/duplex-to)

`duplex-to` wraps a duplex stream with a `Proxy` and hides the readable or the writable interface.
Hidding part of the interface can be useful in cases where errors are thrown or the code behaves different based on the interface type. 
This package allows to show only one part of a duplex stream for those cases.

## Usage

### readable

The `readable` function wraps a duplex stream to show only the readable interface.
It can be loaded either by path or from the main module by property:

```js
import readable from 'duplex-to/readable.js'
import { readable } from 'duplex-to'
```

The function is a factory which returns the wrapped stream.
The stream which should be wrapped must be given as argument:

```js
const readableStream = readable(duplexStream)
````

### writable

The `writable` function wraps a duplex stream to show only the writable interface.
It can be loaded either by path or from the main module by property:

```js
import writable from 'duplex-to/writable.js'
import { writable } from 'duplex-to'
```

The function is a factory which returns the wrapped stream.
The stream which should be wrapped must be given as argument:

```js
const writableStream = writable(duplexStream)
````

## Example

The following examples creates a `PassThrough` duplex stream, which is used to write a text string and allows to access it via the readable stream interface.
The function `noWritablesAccepted` accepts only readable streams and writes the data from the stream to `stdout`.
Passing the `PassThrough` object to the function would throw an error, but with the wrapper only the readable part is visible to the function.

```js
import duplexToReadable from 'duplex-to/readable.js'
import { isWritableStream } from 'is-stream'
import { PassThrough } from 'readable-stream'

// dummy function which
//   - doesn't accept streams with writable interface
//   - just writes the incoming data to stdout 
function noWritablesAccepted (stream) {
  if (isWritableStream(stream)) {
    throw new Error('no writable streams supported')
  }

  stream.on('data', chunk => process.stdout.write(chunk))
}

const stream = new PassThrough()
const readable = duplexToReadable(stream)

// the next line would throw an error if it would be called with stream
noWritablesAccepted(readable)

stream.write('Hello ')
stream.end('World!\n')
```

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