# culvert

> Channel for easy streaming of work between complex logics.

Latest version **0.1.2** (published 2014-07-25) · MIT license · 0 weekly downloads

## Install

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

## 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.1.2 |
| Published | 2014-07-25 |
| First published | 2014-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Tim Caswell |
| Maintainers | creationix |
| Keywords | channel, stream, csp, js-git |

## Links

- npm: https://www.npmjs.com/package/culvert
- Repository: https://github.com/creationix/culvert
- Issues: https://github.com/creationix/culvert/issues
- npm.io page: https://npm.io/package/culvert

## 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.1.2 (latest) — 2014-07-25
- 0.1.1 — 2014-07-19
- 0.1.0 — 2014-07-19

## README

Culvert
=======

Channel for easy streaming of work between complex logics.

This is used in place of streams for CSP style flow.  I use it in js-git for network and file streams.

Usually, you'll want to split sides to create a duplex channel.

```js
var makeChannel = require('culvert');

var serverChannel = makeChannel();
var clientChannel = makeChannel();

function connect(host, port) {

  // This represents the server-side of the duplex pipe
  var socket = {
    put: serverChannel.put,
    drain: serverChannel.drain,
    take: cientChannel.drain
  };

  // When we want to send data to the consumer...
  socket.put(someData);

  // When we want to read from the consumer...
  socket.take(function (err, item) {});

  // Return the client's end of the pipe
  return {
    put: clientChannel.put,
    drain: clientChannel.drain,
    take: serverChannel.take
  };
}
```

If you want/need to preserve back-pressure and honor the buffer limit,
make sure to wait for drain when `put` returns false.

```js
// Start a read
socket.take(onData);

function onData(err, item) {
  if (err) throw err;
  if (item === undefined) {
    // End stream when nothing comes out
    console.log("done");
  }
  else if (socket.put(item)) {
    // If put returned true, keep reading
    socket.take(onData);
  }
  else {
    // Otherwise pause and wait for drain
    socket.drain(onDrain);
  }
}

function onDrain(err) {
  if (err) throw err;
  // Resume reading
  socket.take(onData);
}
```

If you're using continuables and generators, it's much nicer syntax.

```js
var item;
while (item = yield socket.take, item !== undefined) {
  if (!socket.put(item)) yield socket.drain;
}
console.log("done");
```

Also the continuable version won't blow the stack if lots of events come in on the same tick.

## makeChannel(bufferSize, monitor)

Create a new channel.

The optional bufferSize is how many items can be in the queue and still be considered not full.

The optional monitor function will get called with `(type, item)` where `type` is either "put" or "take" and `item` is the value being put or taken.

## channel.put(item) -> more

This is a sync function.  You can add as many items to the channel as you want and it will queue them up.

This returns `true` when the queue is smaller than bufferSize, it returns false if you should wait for drain.

## channel.drain(callback)

Drain is a reusable continuable.  Use this when you want to wait for the buffer to be below the bufferSize mark.

## channel.take(callback)

Take is for reading.  The callback will have the next item.  It may call sync or it may be later.

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