Code coverage report for lib/stream.js

Statements: 96.61% (57 / 59)      Branches: 69.57% (16 / 23)      Functions: 100% (8 / 8)      Lines: 100% (56 / 56)      Ignored: none     

All files » lib/ » stream.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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111    946   946 946   946 946 946 946 946 946     946             946 19015     946 324   946 453 453 20         324   946 20 20       324       324 324 324     324 946 120 120   120       946 100 100   100       946 2 2 1       324 324     946 322 322   322 322   322 322   322 322   322     324 324   324   324     324        
(function () { 'use strict';
 
module.exports = Stream;
 
const EE = require('events').EventEmitter;
const util = require('util');
 
util.inherits(Stream, EE);
Stream.Readable = require('_stream_readable');
Stream.Writable = require('_stream_writable');
Stream.Duplex = require('_stream_duplex');
Stream.Transform = require('_stream_transform');
Stream.PassThrough = require('_stream_passthrough');
 
// Backwards-compat with node 0.4.x
Stream.Stream = Stream;
 
 
 
// old-style streams.  Note that the pipe method (the only relevant
// part of this class) is overridden in the Readable class.
 
function Stream() {
  EE.call(this);
}
 
Stream.prototype.pipe = function(dest, options) {
  var source = this;
 
  function ondata(chunk) {
    Eif (dest.writable) {
      if (false === dest.write(chunk) && source.pause) {
        source.pause();
      }
    }
  }
 
  source.on('data', ondata);
 
  function ondrain() {
    Eif (source.readable && source.resume) {
      source.resume();
    }
  }
 
  dest.on('drain', ondrain);
 
  // If the 'end' option is not supplied, dest.end() will be called when
  // source gets the 'end' or 'close' events.  Only dest.end() once.
  Eif (!dest._isStdio && (!options || options.end !== false)) {
    source.on('end', onend);
    source.on('close', onclose);
  }
 
  var didOnEnd = false;
  function onend() {
    Iif (didOnEnd) return;
    didOnEnd = true;
 
    dest.end();
  }
 
 
  function onclose() {
    Iif (didOnEnd) return;
    didOnEnd = true;
 
    Eif (typeof dest.destroy === 'function') dest.destroy();
  }
 
  // don't leave dangling pipes when there are errors.
  function onerror(er) {
    cleanup();
    if (EE.listenerCount(this, 'error') === 0) {
      throw er; // Unhandled stream error in pipe.
    }
  }
 
  source.on('error', onerror);
  dest.on('error', onerror);
 
  // remove all the event listeners that were added.
  function cleanup() {
    source.removeListener('data', ondata);
    dest.removeListener('drain', ondrain);
 
    source.removeListener('end', onend);
    source.removeListener('close', onclose);
 
    source.removeListener('error', onerror);
    dest.removeListener('error', onerror);
 
    source.removeListener('end', cleanup);
    source.removeListener('close', cleanup);
 
    dest.removeListener('close', cleanup);
  }
 
  source.on('end', cleanup);
  source.on('close', cleanup);
 
  dest.on('close', cleanup);
 
  dest.emit('pipe', source);
 
  // Allow for unix-like usage: A.pipe(B).pipe(C)
  return dest;
};
 
}());