index.js (1233B)
1 /** 2 * Parses an URI 3 * 4 * @author Steven Levithan <stevenlevithan.com> (MIT license) 5 * @api private 6 */ 7 8 var re = /^(?:(?![^:@]+:[^:@\/]*@)(http|https|ws|wss):\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?((?:[a-f0-9]{0,4}:){2,7}[a-f0-9]{0,4}|[^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/; 9 10 var parts = [ 11 'source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor' 12 ]; 13 14 module.exports = function parseuri(str) { 15 var src = str, 16 b = str.indexOf('['), 17 e = str.indexOf(']'); 18 19 if (b != -1 && e != -1) { 20 str = str.substring(0, b) + str.substring(b, e).replace(/:/g, ';') + str.substring(e, str.length); 21 } 22 23 var m = re.exec(str || ''), 24 uri = {}, 25 i = 14; 26 27 while (i--) { 28 uri[parts[i]] = m[i] || ''; 29 } 30 31 if (b != -1 && e != -1) { 32 uri.source = src; 33 uri.host = uri.host.substring(1, uri.host.length - 1).replace(/;/g, ':'); 34 uri.authority = uri.authority.replace('[', '').replace(']', '').replace(/;/g, ':'); 35 uri.ipv6uri = true; 36 } 37 38 return uri; 39 };