All files / app/lib chain.js

93.33% Statements 28/30
85.71% Branches 6/7
100% Functions 1/1
93.33% Lines 28/30

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 23 24 25 26 27 28 29 3021x 21x 21x 21x 21x 8x 8x 8x 8x 8x 7x 7x 8x 8x 8x 8x 8x 1x 1x 1x 1x     1x 8x 8x 8x 8x 8x 21x
const duplexer3 = require('duplexer3');
const Streamz = require('streamz');
const toStream = require('./tostream');
 
module.exports = function(fn) {
  const inStream = Streamz();
  const outStream = Streamz();
 
  if (fn.length > 1)
    fn(inStream,outStream);
  else
    toStream(fn(inStream)).pipe(outStream);
 
  const stream = duplexer3({objectMode: true},inStream,outStream);
 
  // Mirror error and promise behaviour from streamz
  stream.on('error',e => {
    if (stream._events.error.length < 2) {
      const pipes = stream._readableState.pipes;
      if (pipes)
        [].concat(pipes).forEach(child => child.emit('error',e));
      else
        throw e;
    }
  });
 
  stream.promise = Streamz.prototype.promise;
 
  return stream;
};