npmtest-emailjs (v0.0.1)

Code coverage report for node-npmtest-emailjs/emailjs/smtp/response.js

Statements: 8.33% (3 / 36)      Branches: 0% (0 / 14)      Functions: 0% (0 / 9)      Lines: 8.33% (3 / 36)      Ignored: none     

All files » node-npmtest-emailjs/emailjs/smtp/ » response.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 831   1                                                                                                                                                     1          
var SMTPError = require('./error');
 
var SMTPResponse = function(stream, timeout, onerror) 
{
  var buffer = '',
 
  notify = function()
  {
    if(buffer.length)
    {
      // parse buffer for response codes
      var line = buffer.replace("\r", '');
        
      if(!line.trim().split(/\n/).pop().match(/^(\d{3})\s/))
          return;
        
      var match = line ? line.match(/(\d+)\s?(.*)/) : null;
 
      stream.emit('response', null, match ? {code:match[1], message:match[2], data:line} : {code:-1, data:line});
      buffer = '';
    }
  },
 
  error = function(err)
  {
    stream.emit('response', SMTPError('connection encountered an error', SMTPError.ERROR, err));
  },
 
  timedout = function(err)
  {
    stream.end();
    stream.emit('response', SMTPError('timedout while connecting to smtp server', SMTPError.TIMEDOUT, err));
  },
 
  watch = function(data)
  {
    //var data = stream.read();
    if (data !== null) {
      var decoded = data.toString();
      var emit		= false;
      var code		= 0;
 
      buffer += decoded;
      notify();
    }
  },
 
  close = function(err)
  {
    stream.emit('response', SMTPError('connection has closed', SMTPError.CONNECTIONCLOSED, err));
  },
 
  end = function(err)
  {
    stream.emit('response', SMTPError('connection has ended', SMTPError.CONNECTIONENDED, err));
  };
 
  this.stop = function(err) {
    stream.removeAllListeners('response');
    //stream.removeListener('readable', watch);
    stream.removeListener('data', watch);
    stream.removeListener('end', end);
    stream.removeListener('close', close);
    stream.removeListener('error', error);
 
    if(err && typeof(onerror) == "function")
      onerror(err);
  };
 
  //stream.on('readable', watch);
  stream.on('data', watch);
  stream.on('end', end);
  stream.on('close', close);
  stream.on('error', error);
  stream.setTimeout(timeout, timedout);
};
 
exports.monitor = function(stream, timeout, onerror) 
{
  return new SMTPResponse(stream, timeout, onerror);
};