# stream-wrapper

> Wrap read/write/transform functions into streams

Latest version **0.1.2** (published 2013-08-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install stream-wrapper
pnpm add stream-wrapper
yarn add stream-wrapper
bun add stream-wrapper
```

## 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 | 2013-08-24 |
| First published | 2013-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Mathias Buus Madsen |
| Maintainers | mafintosh |
| Keywords | streams2, stream, wrap, functional |

## Links

- npm: https://www.npmjs.com/package/stream-wrapper
- Repository: https://github.com/mafintosh/stream-wrapper
- Issues: https://github.com/mafintosh/stream-wrapper/issues
- npm.io page: https://npm.io/package/stream-wrapper

## Dependencies (2)

- [xtend](https://npm.io/package/xtend.md) ~2.0.6
- [readable-stream](https://npm.io/package/readable-stream.md) ~1.0.15

## 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) — 2013-08-24
- 0.1.1 — 2013-08-24
- 0.1.0 — 2013-08-24

## README

# stream-wrapper

Drop-in replacement for the core [stream](http://nodejs.org/api/stream.html) module
that allows you to wrap `read`/`write`/`transform` functions into streams.

	npm install stream-wrapper

All streams create are stream2 streams

## Readable Stream

``` js
var stream = require('stream-wrapper');

var rs = stream.readable(function(size) {
	this.push(new Buffer('hello world'));
});
```

If you don't have a `read` function just omit it

``` js
var rs = stream.readable();
rs.push(new Buffer('hello world'));
```

The Readable prototype is exposed through `stream.Readable`.

## Writable Stream

``` js
var stream = require('stream-wrapper');

var ws = stream.writable(function(chunk, enc, callback) {
	console.log('writing', chunk);
	callback();
});
```

The Writable prototype is exposed through `stream.Writable`.

## Duplex Stream

``` js
var stream = require('stream-wrapper');

var ds = stream.duplex(function() {
	// read function
	this.push(new Buffer('hello world'));
}, function write(chunk, enc, callback) {
	// write function
	console.log('writing', chunk);
	callback();
});
```

The Duplex prototype is exposed through `stream.Duplex`

## Transform Stream

``` js
var stream = require('stream-wrapper');

var ts = stream.transform(function(chunk, enc, callback) {
	this.push(chunk);
	callback();
});
```

If you want to add a flush function pass it as the second parameter

``` js
var ts = stream.transform(function(chunk, enc, callback) {
	// transform function
	this.push(chunk);
	callback();
}, function(callback) {
	// flush function
	console.log('now flushing...');
	callback();
});
```

The Transform prototype is exposed through `stream.Transform`

## Stream options

If you want to pass stream options (like `objectMode`) pass them as the first
parameter to `readable`, `writable`, `duplex` or `transform`

``` js
var rs = stream.readable({objectMode:true}, function() {
	this.push({message:'i am an object'});
});
```

## Stream defaults

You can change the default options for the stream by calling `defaults`

``` js
// all streams created have objectMode enabled
stream = stream.defaults({objectMode:true});
```

## Stream.destroy

All streams have a `.destroy` method implemented per default that when called
emits `close` and sets `stream.destroyed = true`.

``` js
var rs = stream.readable();

rs.on('close', function() {
	// rs.destroyed === true
	console.log('someone called destroy');
});

rs.destroy();
```

# License

MIT

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