# @basd/pipe

> A simple and versatile utility class for handling streams in both Node.js and the browser.

Latest version **0.0.4** (published 2023-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @basd/pipe
pnpm add @basd/pipe
yarn add @basd/pipe
bun add @basd/pipe
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2023-10-24 |
| First published | 2023-07-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 24.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Basedwon |
| Maintainers | basedwon |
| Keywords | javascript, nodejs, library, pipe, streams, stream-management, stream-processing, data-handling, stream-helper, node-streams, readable-streams, writable-streams, duplex-streams, transform-streams |

## Links

- npm: https://www.npmjs.com/package/@basd/pipe
- Repository: https://gitlab.com/frenware/core/basd/pipe
- Homepage: https://gitlab.com/frenware/core/basd/pipe#readme
- Issues: https://gitlab.com/frenware/core/basd/pipe/issues
- npm.io page: https://npm.io/package/@basd/pipe

## Dependencies (2)

- [@basd/base](https://npm.io/package/@basd/base.md) ^0.0.7
- [readable-stream](https://npm.io/package/readable-stream.md) ^4.4.2

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

- 0.0.4 (latest) — 2023-10-24
- 0.0.3 — 2023-10-06
- 0.0.2 — 2023-09-23
- 0.0.1 — 2023-07-27

## README

# @basd/pipe

> put that in your pipe and stream it

[![npm](https://img.shields.io/npm/v/@basd/pipe?style=flat&logo=npm)](https://www.npmjs.com/package/@basd/pipe)
[![pipeline](https://gitlab.com/frenware/core/basd/pipe/badges/master/pipeline.svg)](https://gitlab.com/frenware/core/basd/pipe/-/pipelines)
[![license](https://img.shields.io/npm/l/@basd/pipe)](https://gitlab.com/frenware/core/basd/pipe/-/blob/master/LICENSE)
[![downloads](https://img.shields.io/npm/dw/@basd/pipe)](https://www.npmjs.com/package/@basd/pipe) 

[![Gitlab](https://img.shields.io/badge/Gitlab%20-%20?logo=gitlab&color=%23383a40)](https://gitlab.com/frenware/core/basd/pipe)
[![Twitter](https://img.shields.io/badge/@basdwon%20-%20?logo=twitter&color=%23383a40)](https://twitter.com/basdwon)
[![Discord](https://img.shields.io/badge/Basedwon%20-%20?logo=discord&color=%23383a40)](https://discordapp.com/users/basedwon)

A simple and versatile utility class for handling streams in both Node.js and the browser. The Pipe class provides a convenient way to work with various stream types, including Readable, Writable, Duplex, Transform, and PassThrough streams. You can also collect readable streams into arrays, buffers and strings. Or pipe streams together to create a pipeline.

## Installation

```
npm install @basd/pipe
```

## Usage

First, require the `Pipe` class from the module:

```js
const Pipe = require('@basd/pipe')
```

You can create different types of streams (Readable, Writable, Duplex, PassThrough) by using the Pipe class constructor and providing the appropriate options:

```js
// Create a duplex stream (as a string)
const myDuplex = new Pipe('duplex')

// Create a readable stream (with an options object)
const myReadable = new Pipe({ type: 'readable' })

// Create a writable stream (without the new)
const myWritable = Pipe('writable')

// Create a pass-through stream (defaults to pass-through)
const myPassThrough = new Pipe()
```

You're basically creating a stream proxy that let's you pass a function and options into the constructor. If the type is a writable stream, the function becomes the write() method; if it's a readable stream, the function becomes the read() method. 

```js
const writer = new Pipe(data => console.log(`got data:`, data))
const reader = new Pipe()
const pipeline = Pipe.line(reader, writer)
pipeline.push('Hello World!') // => `got data: Hello World!`
```

The Pipe class can also be used to set up custom write and read handlers:

```js
const myDuplex = new Pipe(
  'duplex',
  {
    write: (data, encoding, callback) => {
      console.log('Writing:', data)
      callback()
    },
    read: (size) => {
      console.log('Reading:', size)
    }
  }
)
```

You can pipe other Pipe instances or functions into a Pipe instance:

```js
const myDuplex = new Pipe('duplex')
const myReadable = new Pipe('readable')

myDuplex.pipe(myReadable)
```

The `line` method allows you to pipe multiple streams one after the other:

```js
const myDuplex = new Pipe('duplex')
const myReadable = new Pipe('readable')
const myWritable = new Pipe('writable')

myDuplex.line(myReadable, myWritable)
```

You can collect data from a stream:

```js
const myReadable = new Pipe('readable')

myReadable.collect().then((data) => {
  console.log('Collected data:', data)
})
```

And write data to a stream:

```js
const myWritable = new Pipe('writable')

Pipe.writeStream(myWritable, 'Hello, world!').then(() => {
  console.log('Data written.')
})
```

Or read data from a stream:

```js
const myReadable = new Pipe('readable')

Pipe.readStream(myReadable).then((data) => {
  console.log('Read data:', data)
})
```

## Documentation

### Pipe Class Methods

The Pipe class provides several static methods and properties to work with different stream types:

* `Pipe.Duplex`
* `Pipe.Readable`
* `Pipe.Writable`
* `Pipe.Transform`
* `Pipe.PassThrough`
* `Pipe.line(...)`
* `Pipe.collect(stream, format = 'array', bytes = 32)`
* `Pipe.readStream(reader)`
* `Pipe.writeStream(writer, data)`

### Pipe Instance Methods

The Pipe class instances expose a set of methods to interact with the underlying stream:

* `pipe.end(...)`
* `pipe.removeListener(...)`
* `pipe.on(...)`
* `pipe.once(...)`
* `pipe.off(...)`
* `pipe.emit(...)`
* `pipe.write(...)`
* `pipe.read(...)`
* `pipe.push(...)`
* `pipe.pipe(pipe)`
* `pipe.line(...)`
* `pipe.collect()`

### API Reference

[API Reference](/docs/api.md), also see the inline comments in the Pipe class for more detailed information about each method.

## Tests

In order to run the test suite, simply clone the repository and install its dependencies:

```bash
git clone https://gitlab.com/frenware/core/basd/pipe.git
cd basd
npm install
```

To run the tests:

```bash
npm test
```

## Contributing

Thank you! Please see our [contributing guidelines](/docs/contributing.md) for details.

## Donations

If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!

**Bitcoin (BTC):**
```
1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF
```

**Monero (XMR):**
```
46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ
```

## License

basd is [MIT licensed](https://gitlab.com/frenware/core/basd/pipe/-/blob/master/LICENSE).

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