MCPcopy Create free account
hub / github.com/CloudBoost/cloudboost / url

Function url

sdk/src/CloudSocketClientLib.js:5781–5837  ·  view source on GitHub ↗

* URL parser. * * @param {String} url * @param {Object} An object meant to mimic window.location. * Defaults to window.location. * @api public

(uri, loc)

Source from the content-addressed store, hash-verified

5779 */
5780
5781 function url(uri, loc) {
5782 var obj = uri;
5783
5784 // default to window.location
5785 var loc = loc || global.location;
5786 if (null == uri)
5787 uri = loc.protocol + '//' + loc.host;
5788
5789 // relative path support
5790 if ('string' == typeof uri) {
5791 if ('/' == uri.charAt(0)) {
5792 if ('/' == uri.charAt(1)) {
5793 uri = loc.protocol + uri;
5794 } else {
5795 uri = loc.host + uri;
5796 }
5797 }
5798
5799 if (!/^(https?|wss?):\/\//.test(uri)) {
5800 debug('protocol-less url %s', uri);
5801 if ('undefined' != typeof loc) {
5802 uri = loc.protocol + '//' + uri;
5803 } else {
5804 uri = 'https://' + uri;
5805 }
5806 }
5807
5808 // parse
5809 debug('parse %s', uri);
5810 obj = parseuri(uri);
5811 }
5812
5813 // make sure we treat `localhost:80` and `localhost` equally
5814 if (!obj.port) {
5815 if (/^(http|ws)$/.test(obj.protocol)) {
5816 obj.port = '80';
5817 } else if (/^(http|ws)s$/.test(obj.protocol)) {
5818 obj.port = '443';
5819 }
5820 }
5821
5822 obj.path = obj.path || '/';
5823
5824 var ipv6 = obj.host.indexOf(':') !== -1;
5825 var host = ipv6
5826 ? '[' + obj.host + ']'
5827 : obj.host;
5828
5829 // define unique id
5830 obj.id = obj.protocol + '://' + host + ':' + obj.port;
5831 // define href
5832 obj.href = obj.protocol + '://' + host + (loc && loc.port == obj.port
5833 ? ''
5834 : (':' + obj.port));
5835
5836 return obj;
5837 }
5838

Callers 1

lookupFunction · 0.70

Calls 1

debugFunction · 0.70

Tested by

no test coverage detected