All files / app/lib tostream.js

100% Statements 23/23
100% Branches 11/11
100% Functions 1/1
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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