# Code coverage report for socket.io-client/lib/url.js

## Statements: 95.45% (21 / 22) Branches: 85% (17 / 20) Functions: 100% (1 / 1) Lines: 95.24% (20 / 21) Ignored: none

All files » socket.io-client/lib/ » url.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

    1
    1

    1

    1
    9

    9

    9
    9
    2
    2

    9
    4
    4
    4

    9
    9

    9

    1

    9

    9

    /**
     * Module dependencies.
     */

    var url = require('url');
    var debug = require('debug')('socket.io-client:url');

    /**
     * Module exports.
     */

    module.exports = parse;

    /**
     * URL parser.
     *
     * @param {String} url
     * @api public
     */

    function parse(uri){
      var obj = uri;

      // default to window's location
      if (null == uri) uri = location.protocol + '//' + location.host;

      // parse string
      Eif ('string' == typeof uri) {
        if ('/' == uri.charAt(0)) {
          Eif ('undefined' != typeof location) {
            uri = location.hostname + uri;
          }
        }

        // allow for `localhost:3000`
        if (!/^(https?|wss?):\/\//.test(uri)) {
          debug('protocol-less url %s', uri);
          Eif ('undefined' != typeof location) {
            uri = location.protocol + '//' + uri;
          } else {
            uri = 'https://' + uri;
          }
        }

        // parse
        debug('parse %s', uri);
        obj = url.parse(uri);
      }

      // make sure we treat `localhost:80` and `localhost` equally
      if ((/(http|ws):/.test(obj.protocol) && 80 == obj.port) ||
         (/(http|ws)s:/.test(obj.protocol) && 443 == obj.port)) {
        delete obj.port;
      }

      // define unique id
      obj.id = obj.protocol + obj.hostname + (obj.port ? (':' + obj.port) : '');

      return obj;
    }