arnaucube
arnaucube/raspberryGPIOhtmlserverPublic Self-hosted repository
raspberryGPIOhtmlserver/node_modules/socket.io-client/lib/url.js
node_modules/socket.io-client/lib/url.jsRaw
 1
 2/**
 3 * Module dependencies.
 4 */
 5
 6var parseuri = require('parseuri');
 7var debug = require('debug')('socket.io-client:url');
 8
 9/**
10 * Module exports.
11 */
12
13module.exports = url;
14
15/**
16 * URL parser.
17 *
18 * @param {String} url
19 * @param {Object} An object meant to mimic window.location.
20 *                 Defaults to window.location.
21 * @api public
22 */
23
24function url(uri, loc){
25  var obj = uri;
26
27  // default to window.location
28  var loc = loc || global.location;
29  if (null == uri) uri = loc.protocol + '//' + loc.hostname;
30
31  // relative path support
32  if ('string' == typeof uri) {
33    if ('/' == uri.charAt(0)) {
34      if ('/' == uri.charAt(1)) {
35        uri = loc.protocol + uri;
36      } else {
37        uri = loc.hostname + uri;
38      }
39    }
40
41    if (!/^(https?|wss?):\/\//.test(uri)) {
42      debug('protocol-less url %s', uri);
43      if ('undefined' != typeof loc) {
44        uri = loc.protocol + '//' + uri;
45      } else {
46        uri = 'https://' + uri;
47      }
48    }
49
50    // parse
51    debug('parse %s', uri);
52    obj = parseuri(uri);
53  }
54
55  // make sure we treat `localhost:80` and `localhost` equally
56  if (!obj.port) {
57    if (/^(http|ws)$/.test(obj.protocol)) {
58      obj.port = '80';
59    }
60    else if (/^(http|ws)s$/.test(obj.protocol)) {
61      obj.port = '443';
62    }
63  }
64
65  obj.path = obj.path || '/';
66
67  // define unique id
68  obj.id = obj.protocol + '://' + obj.host + ':' + obj.port;
69  // define href
70  obj.href = obj.protocol + '://' + obj.host + (loc && loc.port == obj.port ? '' : (':' + obj.port));
71
72  return obj;
73}