Code coverage report for src/index.js

Statements: 97.73% (43 / 44)      Branches: 100% (20 / 20)      Functions: 90% (9 / 10)      Lines: 97.73% (43 / 44)      Ignored: none     

All files » src/ » index.js
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 881         1     1 14     14 2         12 3 3 3   9   12       12   2   10 2       8     8 8   8 6 2   6 2       8     8 8   8 8   8 24   8 8       1 44 44 52 52 44     44     1 16     1    
var Stream = require('stream')
  , util = require('util')
;
 
// Inherit of Readable stream
util.inherits(Duplexer, Stream.Duplex);
 
// Constructor
function Duplexer(options, writableStream, readableStream) {
  var _self = this;
 
  // Ensure new were used
  if (!(this instanceof Duplexer)) {
    return new (Duplexer.bind.apply(Duplexer,
      [Duplexer].concat([].slice.call(arguments,0))));
  }
 
  // Mapping args
  if(options instanceof Stream) {
    readableStream = writableStream;
    writableStream = options;
    options = {};
  } else {
    options = options || {};
  }
  options.reemitErrors = 'boolean' === typeof options.reemitErrors
    ? options.reemitErrors : true;
 
  // Checking arguments
  if(!(writableStream instanceof Stream.Writable
    || writableStream instanceof Stream.Duplex)) {
    throw new Error('The writable stream must be an instanceof Writable or Duplex.');
  }
  if(!(readableStream instanceof Stream.Readable)) {
    throw new Error('The readable stream must be an instanceof Readable.');
  }
 
  // Parent constructor
  Stream.Duplex.call(this, options);
 
  // Save streams refs
  this._writable = writableStream;
  this._readable = readableStream;
 
  if(options.reemitErrors) {
    this._writable.on('error', function(err) {
      _self.emit('error', err);
    });
    this._readable.on('error', function(err) {
      _self.emit('error', err);
    });
  }
 
  this._writable.on("drain", function() {
    _self.emit("drain");
  })
  this.once('finish', function() {
    _self._writable.end();
  });
  this._writable.once('finish', function() {
    _self.end();
  });
  this._readable.on('readable', function() {
    _self.read(0);
  });
  this._readable.once('end', function() {
    _self.push(null);
  });
}
 
Duplexer.prototype._read = function(n) {
  var _self = this, chunk;
  do {
    chunk = _self._readable.read(n);
    if(null === chunk || !_self.push(chunk)) {
      break;
    }
  } while(null !== chunk);
  return _self.push('');
};
 
Duplexer.prototype._write = function(chunk, encoding, callback) {
  return this._writable.write(chunk, encoding, callback);
};
 
module.exports = Duplexer;