# all files / raven-node/lib/ transports.js

96.08% Statements 49/51
75% Branches 15/20
85.71% Functions 6/7
96.08% Lines 49/51

    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

    1×
    1×

    1×
    1×

    1×

    1×
    1×
    1×
    1×

    1×
    1×
    23×

    23×

    23×
    22×
    22×
    19×
    19×

    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×
    3×

    22×
    22×
    22×

    23×
    23×
    1×
    1×
    1×
    1×

    23×

    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
     'use strict';

    var events = require('events');
    var util = require('util');

    function Transport() {}
    util.inherits(Transport, events.EventEmitter);

    var http = require('http');

    function HTTPTransport(options) {
      this.defaultPort = 80;
      this.transport = http;
      this.options = options || {};
    }
    util.inherits(HTTPTransport, Transport);
    HTTPTransport.prototype.send = function (client, message, headers, eventId, cb) {
      var options = {
        hostname: client.dsn.host,
        path: client.dsn.path + 'api/' + client.dsn.project_id + '/store/',
        headers: headers,
        method: 'POST',
        port: client.dsn.port || this.defaultPort,
        ca: client.ca
      };
      for (var key in this.options) {
        if (this.options.hasOwnProperty(key)) {
          options[key] = this.options[key];
        }
      }
      var req = this.transport.request(options, function (res) {
        res.setEncoding('utf8');
        if (res.statusCode >= 200 && res.statusCode < 300) {
          client.emit('logged', eventId);
          cb && cb(null, eventId);
        } else {
          var reason = res.headers['x-sentry-error'];
          var e = new Error('HTTP Error (' + res.statusCode + '): ' + reason);
          e.response = res;
          e.statusCode = res.statusCode;
          e.reason = reason;
          e.sendMessage = message;
          e.requestHeaders = headers;
          e.eventId = eventId;
          client.emit('error', e);
          cb && cb(e);
        }
        // force the socket to drain
        var noop = function () {};
        res.on('data', noop);
        res.on('end', noop);
      });

      var cbFired = false;
      req.on('error', function (e) {
        client.emit('error', e);
        Eif (!cbFired) {
          cb && cb(e);
          cbFired = true;
        }
      });
      req.end(message);
    };

    var https = require('https');

    function HTTPSTransport(options) {
      this.defaultPort = 443;
      this.transport = https;
      this.options = options || {};
    }
    util.inherits(HTTPSTransport, HTTPTransport);

    module.exports.http = new HTTPTransport();
    module.exports.https = new HTTPSTransport();
    module.exports.Transport = Transport;
    module.exports.HTTPTransport = HTTPTransport;
    module.exports.HTTPSTransport = HTTPSTransport;