mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 05:32:09 +03:00
5 lines
7.8 KiB
TypeScript
5 lines
7.8 KiB
TypeScript
|
// Loaded from https://raw.githubusercontent.com/denjucks/dex/master/lib/deps/@jspm/core@1.1.0/nodelibs/url.js
|
||
|
|
||
|
|
||
|
import t from'./punycode.js';import s from'./querystring.js';var h={},e=t,a={isString:function(t){return "string"==typeof t},isObject:function(t){return "object"==typeof t&&null!==t},isNull:function(t){return null===t},isNullOrUndefined:function(t){return null==t}};function r(){this.protocol=null,this.slashes=null,this.auth=null,this.host=null,this.port=null,this.hostname=null,this.hash=null,this.search=null,this.query=null,this.pathname=null,this.path=null,this.href=null;}h.parse=O,h.resolve=function(t,s){return O(t,!1,!0).resolve(s)},h.resolveObject=function(t,s){return t?O(t,!1,!0).resolveObject(s):s},h.format=function(t){a.isString(t)&&(t=O(t));return t instanceof r?t.format():r.prototype.format.call(t)},h.Url=r;var o=/^([a-z0-9.+-]+:)/i,n=/:[0-9]*$/,i=/^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,l=["{","}","|","\\","^","`"].concat(["<",">",'"',"`"," ","\r","\n","\t"]),p=["'"].concat(l),c=["%","/","?",";","#"].concat(p),u=["/","?","#"],f=/^[+a-z0-9A-Z_-]{0,63}$/,m=/^([+a-z0-9A-Z_-]{0,63})(.*)$/,v={javascript:!0,"javascript:":!0},g={javascript:!0,"javascript:":!0},y={http:!0,https:!0,ftp:!0,gopher:!0,file:!0,"http:":!0,"https:":!0,"ftp:":!0,"gopher:":!0,"file:":!0},b=s;function O(t,s,h){if(t&&a.isObject(t)&&t instanceof r)return t;var e=new r;return e.parse(t,s,h),e}r.prototype.parse=function(t,s,h){if(!a.isString(t))throw new TypeError("Parameter 'url' must be a string, not "+typeof t);var r=t.indexOf("?"),n=-1!==r&&r<t.indexOf("#")?"?":"#",l=t.split(n);l[0]=l[0].replace(/\\/g,"/");var O=t=l.join(n);if(O=O.trim(),!h&&1===t.split("#").length){var d=i.exec(O);if(d)return this.path=O,this.href=O,this.pathname=d[1],d[2]?(this.search=d[2],this.query=s?b.parse(this.search.substr(1)):this.search.substr(1)):s&&(this.search="",this.query={}),this}var j=o.exec(O);if(j){var q=(j=j[0]).toLowerCase();this.protocol=q,O=O.substr(j.length);}if(h||j||O.match(/^\/\/[^@\/]+@[^@\/]+/)){var x="//"===O.substr(0,2);!x||j&&g[j]||(O=O.substr(2),this.slashes=!0);}if(!g[j]&&(x||j&&!y[j])){for(var A,C,I=-1,w=0;w<u.length;w++){-1!==(N=O.indexOf(u[w]))&&(-1===I||N<I)&&(I=N);}-1!==(C=-1===I?O.lastIndexOf("@"):O.lastIndexOf("@",I))&&(A=O.slice(0,C),O=O.slice(C+1),this.auth=decodeURIComponent(A)),I=-1;for(w=0;w<c.length;w++){var N;-1!==(N=O.indexOf(c[w]))&&(-1===I||N<I)&&(I=N);}-1===I&&(I=O.length),this.host=O.slice(0,I),O=O.slice(I),this.parseHost(),this.hostname=this.hostname||"";var U="["===this.hostname[0]&&"]"===this.hostname[this.hostname.length-1];if(!U)for(var k=this.hostname.split(/\./),S=(w=0,k.length);w<S;w++){var R=k[w];if(R&&!R.match(f)){for(var $="",z=0,H=R.length;z<H;z++)R.charCodeAt(z)>127?$+="x":$+=R[z];if(!$.match(f)){var L=k.slice(0,w),Z=k.slice(w+1),_=R.match(m);_&&(L.push(_[1]),Z.unshift(_[2])),Z.length&&(O="/"+Z.join(".")+O),this.hostname=L.join(".");break}}}this.hostname.length>255?this.hostname="":this.hostname=this.hostname.toLowerCase(),U||(this.hostname=e.toASCII(this.hostname));var E=this.port?":"+this.port:"",P=this.hostname||"";this.host=P+E,this.href+=this.host,U&&(this.hostname=this.hostname.substr(1,this.hostname.length-2),"/"!==O[0]&&(O="/"+O));}if(!v[q])for(w=0,S=p.length;w<S;w++){var T=p[w];if(-1!==O.indexOf(T)){var B=encodeURIComponent(T);B===T&&(B=escape(T)),O=O.split(T).join(B);}}var D=O.indexOf("#");-1!==D&&(this.hash=O.substr(D),O=O.slice(0,D));var F=O.indexOf("?");if(-1!==F?(this.search=O.substr(F),this.query=O.substr(F+1),s&&(this.query=b.parse(this.query)),O=O.slice(0,F)):s&&(this.search="",this.query={}),O&&(this.pathname=O),y[q]&&this.hostname&&!this.pathname&&(this.pathname="/"),this.pathname||this.search){E=this.pathname||"";var G=this.search||"";this.path=E+G;}return this.href=this.format(),this},r.prototype.format=function(){var t=this.auth||"";t&&(t=(t=encodeURIComponent(t)).replace(/%3A/i,":"),t+="@");var s=this.protocol||"",h=this.pathname||"",e=this.hash||"",r=!1,o="";this.host?r=t+this.host:this.hostname&&(r=t+(-1===this.hostname.indexOf(":")?this.hostname:"["+this.hostname+"]"),this.port&&(r+=":"+this.port)),this.query&&a.isObject(this.query)&&Object.keys(this.query).length&&(o=b.stringify(this.query)
|
||
|
var Url=h.Url;var format=h.format;var resolve=h.resolve;var resolveObject=h.resolveObject;var URL=void 0!==URL?URL:null;export default h;export{URL,Url,format,resolve,resolveObject};
|