UNPKG

4.7 kBJavaScriptView Raw
1var xfetch=(()=>{var T=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var v=e=>T(e,"__esModule",{value:!0});var C=(e,t)=>{for(var o in t)T(e,o,{get:t[o],enumerable:!0})},D=(e,t,o,u)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of _(t))!S.call(e,l)&&(o||l!=="default")&&T(e,l,{get:()=>t[l],enumerable:!(u=A(t,l))||u.enumerable});return e};var N=(e=>(t,o)=>e&&e.get(t)||(o=D(v({}),t,1),e&&e.set(t,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var I={};C(I,{fetchHook:()=>L,xhrHook:()=>j});var X=e=>{if(/^https?/.test(e))return e;let t=window.location.protocol+"//"+window.location.host;return e[0]==="/"?t+e:t+window.location.pathname+"/"+e},E=e=>[...e.entries()].reduce((t,[o,u])=>(t[o]=u,t),{}),U=e=>new URL(e),P=(e,t,o=null)=>e.reduce((u,l)=>l.call(o,u),t);function L({namespace:e=globalThis||window,fetch:t=e.fetch}={}){if(e==null&&(e=globalThis||window),typeof e!="object")throw new Error("[xfetch-hook] - `namespace` should be an object");if(typeof t!="function")throw new Error("[xfetch-hook] - `fetch` should be a function");if(typeof t.onRequest=="function")return t.stopIntercepting;let o="fetch",u=[];async function l(...n){let a=[],d=[],p=new Request(...n),b=null,w=null,R=null,x=null;for(let y of u){let i=await y({request:p,fetch:t,get url(){return R||(R=U(p.url))},get headers(){return x||(x=E(p.headers))}});!i||(i.request instanceof Request&&(p=i.request),i.response instanceof Response&&(b=i.response),typeof i.listen=="function"&&a.push(i.listen),typeof i.as=="string"&&(w=i.as,typeof i.transformResponse=="function"&&d.push(i.transformResponse)))}async function m(y){let i=y,g=w&&typeof y[w]=="function",f=await(g&&y.clone()[w]());if(d.length>0){let r=P(d,f);typeof r!="string"&&(r=JSON.stringify(r)),i=new Response(r,{status:y.status,statusText:y.statusText,headers:y.headers})}return a.forEach(r=>{let h=i.clone();g?h[w]().then(r.bind(null,h)):r(h)}),i}return b?m(b):t(p).then(m)}Object.defineProperty(l,"name",{value:o,configurable:!0});function H(){e[o]=l}H();function q(){e[o]=t}function M(n){u=u.filter(a=>a!==n)}function s(n){u.push(n)}return l.stopProxying=q,l.onRequest=function(a){if(typeof a!="function")throw new Error("[xfetch-hook] - `onRequest`: argument must be a function");let d=()=>M(a);return u.find(b=>b===a)||s(a),d},q}function j({namespace:e=globalThis||window}={}){if(e==null&&(e=globalThis||window),typeof e!="object")throw new Error("[xfetch-hook] - `namespace` should be an object");let t=e.XMLHttpRequest;if(typeof t!="function")throw new Error("[xfetch-hook] - `XMLHttpRequest` should be a function.");let o=[];function u(){let s=new t,n=this,a,d,p,b,w,R,x,m,y=[],i=[];this.onreadystatechange=null,s.onreadystatechange=function(){if(p&&this.readyState==4){let f=s.responseType;try{n.response=m||(m=P(y,s.response,n)),(f===""||f==="text")&&(n.responseText=s.responseText),i.forEach(r=>r.call(n,n.response,n))}catch(r){console.warn("Error in proxied xfetch-hook package",r.message)}}if(n.onreadystatechange)return n.onreadystatechange()},["status","statusText","readyState","responseXML","upload"].forEach(function(f){Object.defineProperty(n,f,{get:function(){return s[f]}})}),["ontimeout, timeout","withCredentials","onload","onerror","onprogress","responseType"].forEach(function(f){Object.defineProperty(n,f,{get:function(){return s[f]},set:function(r){f==="onload"&&typeof r=="function"&&(r=r.bind(n)),s[f]=r}})}),["addEventListener","abort","getAllkeyResponseHeaders","getAllResponseHeaders","getResponseHeader","overrideMimeType"].forEach(function(f){Object.defineProperty(n,f,{value:function(){return s[f].apply(s,arguments)}})});let g=new Headers;n.setRequestHeader=function(r,h){g.append(r,h)},n.open=function(r,h,c,O,k){a=r,d=new URL(X(h)),p=c!==!1,b=O,w=k,s.open(a,d.toString(),p,b,w)},n.send=async function(r){if(R=r,p){for(let h of o){let c=await h({method:a,url:d,body:R,get headers(){return x||(x=E(g))}});!c||(c.method&&(a=c.method),c.url&&(d=c.url.href||c.url),c.body!==void 0&&(R=c.body),c.headers instanceof Headers&&(g=c.headers),typeof c.transformResponse=="function"&&y.push(c.transformResponse),typeof c.listen=="function"&&i.push(c.listen))}s.open(a,d,p,b,w);for(let h in g.entries())s.setRequestHeader(h[0],h[1]);s.send(R)}}}Object.defineProperty(u,"name",{value:"XMLHttpRequest",configurable:!0});function l(){e.XMLHttpRequest=u}l();function H(){e.XMLHttpRequest=t}function q(s){o=o.filter(n=>n!==s)}function M(s){o.push(s)}return u.stopProxying=H,u.onRequest=function(n){if(typeof n!="function")throw new Error("[onRequest] - Argument must be a function");let a=()=>q(n);return o.find(p=>p===n)||M(n),a},H}return N(I);})();