memstreams
Version:
Memory Streams library for JavaScript based on readable-stream
91 lines (67 loc) • 2.98 kB
Markdown
# Memory Stream
[](http://travis-ci.org/taoyuan/memstreams)
[](https://www.npmjs.com/package/memstreams)
[](https://github.com/taoyuan/memstreams/network/alerts)
[](https://codeclimate.com/github/taoyuan/memstreams)
[](https://codeclimate.com/github/taoyuan/memstreams)
[](https://greenkeeper.io/)
[](https://greenkeeper.io/)



> Memory Streams library for JavaScript based on readable-stream
> This library is forked from
> [stream-mock](https://github.com/b4nst/stream-mock)
## Features
- Create a
[readable stream](https://nodejs.org/api/stream.html#stream_readable_streams)
from any iterable.
- Create a
[writable stream](https://nodejs.org/api/stream.html#stream_writable_streams)
that puts its data at your disposal.
- Create a
[duplex stream](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams)
that combines a readable and writable stream together.
- Can operate both in
[object](https://nodejs.org/api/stream.html#stream_object_mode) and normal (
[Buffer](https://nodejs.org/api/buffer.html#buffer_buf_length) ) mode.
- Forward writable to readable through event `write` to replace `queue`
## Quick start
```shell
yarn add memstreams
```
Or, if you are more a `npm` person
```shell
npm i memstreams
```
### Basic usage
You are building an awesome brand new
[Transform stream](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams)
that rounds all your values.
`rounder.ts`
```javascript
import {Transform} from 'stream';
export class Rounder extends Transform {
_transform(chunk, encoding, callback) {
this.push(Math.round(chunk));
callback();
}
}
```
Now you need / want to test it.
`example.ts`
```typescript
import {ObjectReader, ObjectWriter} from 'memstreams';
import {Rounder} from './rounder';
const input = [1.2, 2.6, 3.7];
const transform = new Rounder({objectMode: true});
const reader = new ObjectReader(input, {autoEnd: true});
const writer = new ObjectWriter();
reader.pipe(transform).pipe(writer);
writer.on('finish', () => {
console.log(writer.data);
});
```
---
## License
MIT