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

100% Statements 65/65
93.15% Branches 68/73
100% Functions 5/5
100% Lines 64/64

    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
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164

    1×
    1×
    1×

    1×

    1×
    7×
    7×

    7×

    1×
    23×
    23×
    23×
    23×

    23×

    23×
    23×
    7×
    7×
    4×
    4×

    23×
    1×
    1×

    23×
    23×
    23×
    23×

    23×

    1×
    31×

    31×

    31×

    31×

    31×

    31×

    31×

    31×

    31×

    31×
    31×
    9×
    8×

    31×

    1×

    31×

    31×

    31×
    31×
    30×
    30×
    1×
    29×
    5×
    1×
    3×
    3×

    4×
    11×
    8×

    30×
    30×
    8×

    30×

    31×

     'use strict';

    var cookie = require('cookie');
    var urlParser = require('url');
    var stringify = require('json-stringify-safe');

    var utils = require('./utils');

    module.exports.parseText = function parseText(message, kwargs) {
      kwargs = kwargs || {};
      kwargs.message = message;

      return kwargs;
    };

    module.exports.parseError = function parseError(err, kwargs, cb) {
      utils.parseStack(err, function (frames) {
        var name = err.name + '';
        Eif (typeof kwargs.message === 'undefined') {
          kwargs.message = name + ': ' + (err.message || '<no message>');
        }
        kwargs.exception = [{
          type: name,
          value: err.message,
          stacktrace: {
            frames: frames
          }
        }];

        // Save additional error properties to `extra` under the error type (e.g. `extra.AttributeError`)
        var extraErrorProps;
        for (var key in err) {
          Eif (err.hasOwnProperty(key)) {
            if (key !== 'name' && key !== 'message' && key !== 'stack') {
              extraErrorProps = extraErrorProps || {};
              extraErrorProps[key] = err[key];
            }
          }
        }
        if (extraErrorProps) {
          kwargs.extra = kwargs.extra || {};
          kwargs.extra[name] = extraErrorProps;
        }

        for (var n = frames.length - 1; n >= 0; n--) {
          Eif (frames[n].in_app) {
            kwargs.culprit = kwargs.culprit || utils.getCulprit(frames[n]);
            break;
          }
        }

        cb(kwargs);
      });
    };

    module.exports.parseRequest = function parseRequest(req, parseUser) {
      var kwargs = {};

      // headers:
      //   node, express: req.headers
      //   koa: req.header
      var headers = req.headers || req.header || {};

      // method:
      //   node, express, koa: req.method
      var method = req.method;

      // host:
      //   express: req.hostname in > 4 and req.host in < 4
      //   koa: req.host
      //   node: req.headers.host
      var host = req.hostname || req.host || headers.host || '<no host>';

      // protocol:
      //   node: <n/a>
      //   express, koa: req.protocol
      var protocol = req.protocol === 'https' || req.secure || (req.socket || {}).encrypted ? 'https' : 'http';

      // url (including path and query string):
      //   node, express: req.originalUrl
      //   koa: req.url
      var originalUrl = req.originalUrl || req.url;

      // absolute url
      var absoluteUrl = protocol + '://' + host + originalUrl;

      // query string:
      //   node: req.url (raw)
      //   express, koa: req.query
      var query = req.query || urlParser.parse(originalUrl || '', true).query;

      // cookies:
      //   node, express, koa: req.headers.cookie
      var cookies = cookie.parse(headers.cookie || '');

      // body data:
      //   node, express, koa: req.body
      var data = req.body;
      if (['GET', 'HEAD'].indexOf(method) === -1) {
        if (typeof data === 'undefined') {
          data = '<unavailable>';
        }
      }

      if (data && typeof data !== 'string' && {}.toString.call(data) !== '[object String]') {
        // Make sure the request body is a string
        data = stringify(data);
      }

      // http interface
      var http = {
        method: method,
        query_string: query,
        headers: headers,
        cookies: cookies,
        data: data,
        url: absoluteUrl
      };

      // expose http interface
      kwargs.request = http;

      // user: typically found on req.user in express/passport patterns
      // five cases for parseUser value:
      //   absent: grab only id, username, email from req.user
      //   false: capture nothing
      //   true: capture all keys from req.user
      //   array: provided whitelisted keys to grab from req.user
      //   function :: req -> user: custom parsing function
      if (parseUser == null) parseUser = ['id', 'username', 'email'];
      if (parseUser) {
        var user = {};
        if (typeof parseUser === 'function') {
          user = parseUser(req);
        } else if (req.user) {
          if (parseUser === true) {
            for (var key in req.user) {
              Eif ({}.hasOwnProperty.call(req.user, key)) {
                user[key] = req.user[key];
              }
            }
          } else {
            parseUser.forEach(function (fieldName) {
              if ({}.hasOwnProperty.call(req.user, fieldName)) {
                user[fieldName] = req.user[fieldName];
              }
            });
          }
        }

        // client ip:
        //   node: req.connection.remoteAddress
        //   express, koa: req.ip
        var ip = req.ip || req.connection && req.connection.remoteAddress;
        if (ip) {
          user.ip_address = ip;
        }

        kwargs.user = user;
      }

      return kwargs;
    };