index.mjs 3.46 KB
import e from"querystring";var t=/https?|ftp|gopher|file/;function r(r){"string"==typeof r&&(r=m(r));var a=function(e,t,r){var a=e.auth,o=e.hostname,s=e.protocol||"",h=e.pathname||"",c=e.hash||"",p=e.query||"",n=!1;a=a?encodeURIComponent(a).replace(/%3A/i,":")+"@":"",e.host?n=a+e.host:o&&(n=a+(~o.indexOf(":")?"["+o+"]":o),e.port&&(n+=":"+e.port)),p&&"object"==typeof p&&(p=t.encode(p));var l=e.search||p&&"?"+p||"";return s&&":"!==s.substr(-1)&&(s+=":"),e.slashes||(!s||r.test(s))&&!1!==n?(n="//"+(n||""),h&&"/"!==h[0]&&(h="/"+h)):n||(n=""),c&&"#"!==c[0]&&(c="#"+c),l&&"?"!==l[0]&&(l="?"+l),{protocol:s,host:n,pathname:h=h.replace(/[?#]/g,encodeURIComponent),search:l=l.replace("#","%23"),hash:c}}(r,e,t);return""+a.protocol+a.host+a.pathname+a.search+a.hash}var a="http://",o="w.w",s=a+o,h=/^https?|ftp|gopher|file/,c=/^(.*?)([#?].*)/,p=/^([a-z0-9.+-]*:)(\/{0,3})(.*)/i,n=/^([a-z0-9.+-]*:)?\/\/\/*/i,l=/^([a-z0-9.+-]*:)(\/{0,2})\[(.*)\]$/i;function i(e){try{return decodeURI(e)}catch(t){return e}}function m(t,a,m){void 0===a&&(a=!1),void 0===m&&(m=!1);var u=(t=t.trim()).match(c);t=u?i(u[1]).replace(/\\/g,"/")+u[2]:i(t).replace(/\\/g,"/"),l.test(t)&&"/"!==t.slice(-1)&&(t+="/");var f=!/(^javascript)/.test(t)&&t.match(p),v=n.test(t),g="";f&&(h.test(f[1])||(g=f[1].toLowerCase(),t=""+f[2]+f[3]),f[2]||(v=!1,h.test(f[1])?(g=f[1],t=""+f[3]):t="//"+f[3]),3!==f[2].length&&1!==f[2].length||(g=f[1],t="/"+f[3]));var d,b=t.match(/(:[0-9]+)/),y="";b&&b[1]&&3===b[1].length&&(t=t.replace(y=b[1],y+"00"));var w={},R="",U="";try{d=new URL(t)}catch(e){R=e,g||m||!/^\/\//.test(t)||/^\/\/.+[@.]/.test(t)||(U="/",t=t.substr(1));try{d=new URL(t,s)}catch(e){return w.protocol=g,w.href=g,w}}w.slashes=v&&!U,w.host=d.host===o?"":d.host,w.hostname=d.hostname===o?"":d.hostname.replace(/(\[|\])/g,""),w.protocol=R?g||null:d.protocol,w.search=d.search.replace(/\\/g,"%5C"),w.hash=d.hash.replace(/\\/g,"%5C");var C=t.split("#");!w.search&&~C[0].indexOf("?")&&(w.search="?"),w.hash||""!==C[1]||(w.hash="#"),w.query=a?e.decode(d.search.substr(1)):w.search.substr(1),w.pathname=U+i(d.pathname).replace(/"/g,"%22"),"about:"===w.protocol&&"blank"===w.pathname&&(w.protocol="",w.pathname=""),R&&"/"!==t[0]&&(w.pathname=w.pathname.substr(1)),g&&!h.test(g)&&"/"!==t.slice(-1)&&"/"===w.pathname&&(w.pathname=""),w.path=w.pathname+w.search,w.auth=[d.username,d.password].map(decodeURIComponent).filter(Boolean).join(":"),w.port=d.port,y&&(w.host=w.host.replace(y+"00",y),w.port=w.port.slice(0,-2)),w.href=U?""+w.pathname+w.search+w.hash:r(w);var j=/^(file)/.test(w.href)?["host","hostname"]:[];return Object.keys(w).forEach(function(e){~j.indexOf(e)||(w[e]=w[e]||null)}),w}var u=/^([a-z0-9.+-]*:\/\/\/)([a-z0-9.+-]:\/*)?/i,f=/https?|ftp|gopher|file/;function v(e,t){var o="string"==typeof e?m(e):e;e="object"==typeof e?r(e):e;var h=m(t),c="";o.protocol&&!o.slashes&&(c=o.protocol,e=e.replace(o.protocol,""),c+="/"===t[0]||"/"===e[0]?"/":""),c&&h.protocol&&(c="",h.slashes||(c=h.protocol,t=t.replace(h.protocol,"")));var p=e.match(u);p&&!h.protocol&&(e=e.substr((c=p[1]+(p[2]||"")).length),/^\/\/[^\/]/.test(t)&&(c=c.slice(0,-1)));var n=new URL(e,s+"/"),l=new URL(t,n).toString().replace(s,""),i=h.protocol||o.protocol;return i+=o.slashes||h.slashes?"//":"",!c&&i?l=l.replace(a,i):c&&(l=l.replace(a,"")),f.test(l)||~t.indexOf(".")||"/"===e.slice(-1)||"/"===t.slice(-1)||"/"!==l.slice(-1)||(l=l.slice(0,-1)),c&&(l=c+("/"===l[0]?l.substr(1):l)),l}function g(e,t){return m(v(e,t))}export{m as parse,r as format,v as resolve,g as resolveObject};
//# sourceMappingURL=index.mjs.map