1 | const Streamz = require('streamz');
|
2 | const Promise = require('bluebird');
|
3 |
|
4 | function toStream(data) {
|
5 | const stream = Streamz();
|
6 |
|
7 | if (typeof data == 'function')
|
8 | data = Promise.try(data.bind(stream));
|
9 |
|
10 | Promise.resolve(data)
|
11 | .then(d => {
|
12 | if (d && typeof d.pipe == 'function')
|
13 | return d.pipe(stream);
|
14 | else if (d !== undefined)
|
15 | [].concat(d).forEach(d => stream.write(d));
|
16 | stream.end();
|
17 | })
|
18 | .catch(e => stream.emit('error',e));
|
19 |
|
20 | return stream;
|
21 | }
|
22 |
|
23 | module.exports = toStream; |
\ | No newline at end of file |