# Code coverage report for lib/url.js

## Statements: 90.48% (19 / 21) Branches: 80% (16 / 20) Functions: 100% (1 / 1) Lines: 95% (19 / 20) Ignored: none

All files » 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

    1

    1

    1
    8

    8

    8
    8
    2
    2

    8
    3
    3
    3

    8
    8

    8

    1

    8

    8

    /**
     * Module dependencies.
     */

    var url = require('url')
      , 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;

      Iif (null == url) url = location.protocol + '//' + location.hostname;

      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;
    }