Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tilfin/promised-lifestream
Creating promisified stream pipeline for Node.js
https://github.com/tilfin/promised-lifestream
nodejs npm-module promise stream
Last synced: 3 months ago
JSON representation
Creating promisified stream pipeline for Node.js
- Host: GitHub
- URL: https://github.com/tilfin/promised-lifestream
- Owner: tilfin
- License: mit
- Created: 2016-09-19T04:37:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-28T13:13:58.000Z (about 6 years ago)
- Last Synced: 2024-10-05T06:55:42.200Z (4 months ago)
- Topics: nodejs, npm-module, promise, stream
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Promised Lifestream
===================[![NPM Version][npm-image]][npm-url]
[![Build Status](https://travis-ci.org/tilfin/promised-lifestream.svg?branch=master)](https://travis-ci.org/tilfin/promised-lifestream)
[![Coverage Status](https://coveralls.io/repos/github/tilfin/promised-lifestream/badge.svg?branch=master)](https://coveralls.io/github/tilfin/promised-lifestream?branch=master)
[![dependencies Status](https://david-dm.org/tilfin/promised-lifestream/status.svg)](https://david-dm.org/tilfin/promised-lifestream)Creating promisified stream pipeline for Node.js
## Feature
* Streams are pipelined and it resolves when last stream have finished.
* If an error occurred at any stream, it rejects with the error.
* If last stream is Readable or Transform, it appends auto-generated writer to streams.
* Last stream must raise `finish` event. `process.stdout` doesn't raise it.## Install
```
$ npm install -save promised-lifestream
```## How to use
**PromisedLifestream(streams, [options])**
* `streams` `>` streams composes pipeline. The first stream must be Readable. The second and any later stream must be Writable. The last stream can be Transform.
* `options` ``
* `needResult` `` Whether the last stream result is resolved or not. Defaults to false## Example
```javascript
'use strict';const fs = require('fs');
const stream = require('stream');
const es = require('event-stream');
const PromisedLifestream = require('promised-lifestream');var i = 0;
PromisedLifestream([
new stream.Readable({
read(size) {
if (++i < 10) {
this.push(String(i));
} else {
this.push(null);
}
}
}),
new stream.PassThrough(),
fs.createWriteStream('out.txt')
])
.then(() => {
const str = fs.readFileSync('out.txt', { encoding: 'ascii' });
console.log(str);
// 123456789
})
.catch(err => {
console.error(err);
});
```### Get last result
```javascript
PromisedLifestream([
es.readArray([1, 2, 3]),
es.map(function (data, callback) {
callback(null, data * 2);
})
], {
needResult: true
})
.then(result => {
console.log(result);
// [2, 4, 6]
})
.catch(err => {
console.error(err);
});
```## License
[MIT](LICENSE)
[npm-image]: https://img.shields.io/npm/v/promised-lifestream.svg
[npm-url]: https://npmjs.org/package/promised-lifestream