ff-request-dist/dist/ff-request.umd.cjs
2024-07-23 11:16:38 +08:00

2 lines
12 KiB
JavaScript

(function(v,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(v=typeof globalThis<"u"?globalThis:v||self,p(v["ff-request"]={}))})(this,function(v){"use strict";var ye=Object.defineProperty;var be=(v,p,P)=>p in v?ye(v,p,{enumerable:!0,configurable:!0,writable:!0,value:P}):v[p]=P;var A=(v,p,P)=>(be(v,typeof p!="symbol"?p+"":p,P),P),ue=(v,p,P)=>{if(!p.has(v))throw TypeError("Cannot "+P)};var u=(v,p,P)=>(ue(v,p,"read from private field"),P?P.call(v):p.get(v)),C=(v,p,P)=>{if(p.has(v))throw TypeError("Cannot add the same private member more than once");p instanceof WeakSet?p.add(v):p.set(v,P)},I=(v,p,P,Y)=>(ue(v,p,"write to private field"),Y?Y.call(v,P):p.set(v,P),P);var G,B,q,D,Q,W,L,X,Z,H,K,k,j,M;function p(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function P(t){if(t.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),r=0;r<e.length;r++)e[r]=255;for(var i=0;i<t.length;i++){var l=t.charAt(i),w=l.charCodeAt(0);if(e[w]!==255)throw new TypeError(l+" is ambiguous");e[w]=i}var g=t.length,R=t.charAt(0),V=Math.log(g)/Math.log(256),J=Math.log(256)/Math.log(g);function F(n){if(n instanceof Uint8Array||(ArrayBuffer.isView(n)?n=new Uint8Array(n.buffer,n.byteOffset,n.byteLength):Array.isArray(n)&&(n=Uint8Array.from(n))),!(n instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(n.length===0)return"";for(var s=0,f=0,o=0,h=n.length;o!==h&&n[o]===0;)o++,s++;for(var c=(h-o)*J+1>>>0,d=new Uint8Array(c);o!==h;){for(var S=n[o],O=0,y=c-1;(S!==0||O<f)&&y!==-1;y--,O++)S+=256*d[y]>>>0,d[y]=S%g>>>0,S=S/g>>>0;if(S!==0)throw new Error("Non-zero carry");f=O,o++}for(var a=c-f;a!==c&&d[a]===0;)a++;for(var b=R.repeat(s);a<c;++a)b+=t.charAt(d[a]);return b}function x(n){if(typeof n!="string")throw new TypeError("Expected String");if(n.length===0)return new Uint8Array;for(var s=0,f=0,o=0;n[s]===R;)f++,s++;for(var h=(n.length-s)*V+1>>>0,c=new Uint8Array(h);n[s];){var d=e[n.charCodeAt(s)];if(d===255)return;for(var S=0,O=h-1;(d!==0||S<o)&&O!==-1;O--,S++)d+=g*c[O]>>>0,c[O]=d%256>>>0,d=d/256>>>0;if(d!==0)throw new Error("Non-zero carry");o=S,s++}for(var y=h-o;y!==h&&c[y]===0;)y++;for(var a=new Uint8Array(f+(h-y)),b=f;y!==h;)a[b++]=c[y++];return a}function z(n){var s=x(n);if(s)return s;throw new Error("Non-base"+g+" character")}return{encode:F,decodeUnsafe:x,decode:z}}var Y=P;const ce=p(Y);var ne={};(function(t){(function(e){e([],function(){var r=-1,i=-2,l=-3,w=-4,g=-5,R=function(s,f){f=f||{};var o=f.verbose||!1;o&&console.log("Normalize the JSON Object"),s=typeof s=="string"?this.JSON.parse(s):s,o&&console.log("Creating a empty dictionary");var h={strings:[],integers:[],floats:[]};o&&console.log("Creating the AST");var c=function y(a){o&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(a));var b=typeof a;if(a===null)return{type:"null",index:l};if(typeof a>"u")return{type:"undefined",index:g};if(a instanceof Array){var N=["@"];for(var T in a)a.hasOwnProperty(T)&&N.push(y(a[T]));return N}if(b==="object"){var N=["$"];for(var $ in a)a.hasOwnProperty($)&&(N.push(y($)),N.push(y(a[$])));return N}if(a==="")return{type:"empty",index:w};if(b==="string"){var _=n.call(h.strings,a);return _==-1&&(h.strings.push(J(a)),_=h.strings.length-1),{type:"strings",index:_}}if(b==="number"&&a%1===0){var _=n.call(h.integers,a);return _==-1&&(h.integers.push(x(a)),_=h.integers.length-1),{type:"integers",index:_}}if(b==="number"){var _=n.call(h.floats,a);return _==-1&&(h.floats.push(a),_=h.floats.length-1),{type:"floats",index:_}}if(b==="boolean")return{type:"boolean",index:a?r:i};throw new Error("Unexpected argument of type "+typeof a)}(s),d=h.strings.length,S=h.integers.length;h.floats.length,o&&console.log("Parsing the dictionary");var O=h.strings.join("|");return O+="^"+h.integers.join("|"),O+="^"+h.floats.join("|"),o&&console.log("Parsing the structure"),O+="^"+function y(a){if(o&&console.log("Calling a recursiveParser with "+this.JSON.stringify(a)),a instanceof Array){var b=a.shift();for(var N in a)a.hasOwnProperty(N)&&(b+=y(a[N])+"|");return(b[b.length-1]==="|"?b.slice(0,-1):b)+"]"}var T=a.type,$=a.index;if(T==="strings")return x($);if(T==="integers")return x(d+$);if(T==="floats")return x(d+S+$);if(T==="boolean")return a.index;if(T==="null")return l;if(T==="undefined")return g;if(T==="empty")return w;throw new TypeError("The item is alien!")}(c),o&&console.log("Ending parser"),f.debug?{dictionary:h,ast:c,packed:O}:O},V=function(s,f){f=f||{};var o=s.split("^");f.verbose&&console.log("Building dictionary");var h=[],c=o[0];if(c!==""){c=c.split("|"),f.verbose&&console.log("Parse the strings dictionary");for(var d=0,S=c.length;d<S;d++)h.push(F(c[d]))}if(c=o[1],c!==""){c=c.split("|"),f.verbose&&console.log("Parse the integers dictionary");for(var d=0,S=c.length;d<S;d++)h.push(z(c[d]))}if(c=o[2],c!==""){c=c.split("|"),f.verbose&&console.log("Parse the floats dictionary");for(var d=0,S=c.length;d<S;d++)h.push(parseFloat(c[d]))}c=null,f.verbose&&console.log("Tokenizing the structure");for(var O="",y=[],a=o[3].length,d=0;d<a;d++){var b=o[3].charAt(d);b==="|"||b==="$"||b==="@"||b==="]"?(O&&(y.push(z(O)),O=""),b!=="|"&&y.push(b)):O+=b}var N=y.length,T=0;return f.verbose&&console.log("Starting recursive parser"),function $(){var _=y[T++];if(f.verbose&&console.log("Reading collection type "+(_==="$"?"object":"Array")),_==="@"){for(var E=[];T<N;T++){var m=y[T];if(f.verbose&&console.log("Read "+m+" symbol"),m==="]")return E;if(m==="@"||m==="$")E.push($());else switch(m){case r:E.push(!0);break;case i:E.push(!1);break;case l:E.push(null);break;case g:E.push(void 0);break;case w:E.push("");break;default:E.push(h[m])}}return f.verbose&&console.log("Parsed "+this.JSON.stringify(E)),E}if(_==="$"){for(var E={};T<N;T++){var U=y[T];if(U==="]")return E;U===w?U="":U=h[U];var m=y[++T];if(m==="@"||m==="$")E[U]=$();else switch(m){case r:E[U]=!0;break;case i:E[U]=!1;break;case l:E[U]=null;break;case g:E[U]=void 0;break;case w:E[U]="";break;default:E[U]=h[m]}}return f.verbose&&console.log("Parsed "+this.JSON.stringify(E)),E}throw new TypeError("Bad token "+_+" isn't a type")}()},J=function(s){return typeof s!="string"?s:s.replace(/[\+ \|\^\%]/g,function(f){return{" ":"+","+":"%2B","|":"%7C","^":"%5E","%":"%25"}[f]})},F=function(s){return typeof s!="string"?s:s.replace(/\+|%2B|%7C|%5E|%25/g,function(f){return{"+":" ","%2B":"+","%7C":"|","%5E":"^","%25":"%"}[f]})},x=function(s){return Number.prototype.toString.call(s,36).toUpperCase()},z=function(s){return parseInt(s,36)},n=Array.prototype.indexOf||function(s,f){for(var o=f||0,h=this.length;o<h;o++)if(this[o]===s)return o;return-1};return{JSON,pack:R,unpack:V}})})(function(e,r){var i=r();for(var l in i)t[l]=i[l]})})(ne);const se=p(ne);class ee{constructor(e,r,i,l,w){A(this,"code",0);A(this,"message","操作成功");A(this,"data",null);A(this,"url","");A(this,"res","");C(this,G,null);C(this,B,!0);C(this,q,!0);C(this,D,{get:(e,r)=>u(this,Q).call(this,r),ownKeys:()=>Object.keys(u(this,D).getPrototypeOf()||{}),getPrototypeOf:()=>u(this,B)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:u(this,D).getPrototypeOf()})});C(this,Q,e=>e==="$echoMsg"?u(this,L):e==="msg"?u(this,W):e==="then"||e==="resp"?(I(this,q,!0),I(this,B,e==="resp"),u(this,X)):e==="catch"?u(this,Z):e==="IS_ECHO_MSG"?u(this,q):Reflect.get(this,e));C(this,W,(e=i=>i,r=!1)=>(u(this,L).call(this),I(this,B,r),Promise.resolve(e==null?void 0:e(u(this,D).getPrototypeOf()))));C(this,L,()=>{u(this,q)&&(I(this,q,!1),le.onMsg(this.code,this.message))});C(this,X,e=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(u(this,D).getPrototypeOf())):Promise.reject(u(this,G)));C(this,Z,e=>Promise.resolve(e(u(this,G))));return this.code=e,this.message=r,this.data=i,this.url=l,this.res=w,I(this,G,new Proxy(this,u(this,D)))}}G=new WeakMap,B=new WeakMap,q=new WeakMap,D=new WeakMap,Q=new WeakMap,W=new WeakMap,L=new WeakMap,X=new WeakMap,Z=new WeakMap;const te=(t,e,r,i="/",l="")=>new ee(t,e,r,i,l);var ie=["utf8","utf-8","unicode-1-1-utf-8"];function ge(t){if(ie.indexOf(t)<0&&typeof t<"u"&&t!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var r=unescape(encodeURIComponent(e)),i=new Uint8Array(r.length);const l=r.split("");for(let w=0;w<l.length;w++)i[w]=l[w].charCodeAt(0);return i}}function de(t){if(ie.indexOf(t)<0&&typeof t<"u"&&t!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.decode=function(e,r){if(typeof e>"u")return"";var i=typeof r<"u"&&i in r?r.stream:!1;if(typeof i!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var l=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),w=new Array(l.length);for(let g=0;g<l.length;g++)w[g]=String.fromCharCode(l[g]);return decodeURIComponent(escape(w.join("")))}else throw new TypeError("passed argument must be an array buffer view")}}var oe={TextEncoder:ge,TextDecoder:de};const ve=new oe.TextEncoder("utf8"),pe=new oe.TextDecoder("utf8"),ae=t=>{let e=5381;for(let r=0;r<t.length;r++)e=e*33^t.charCodeAt(r);return(e>>>0).toString(16)},we=(t,e)=>t.replace(new RegExp(`^${e}+|${e}+$`,"g"),""),fe=t=>ve.encode(t||""),he=t=>pe.decode(new DataView(t),{});Promise.withResolvers||(Promise.withResolvers=function(){let t,e;return{promise:new Promise((i,l)=>{t=i,e=l}),resolve:t,reject:e}});class re{constructor(e,r,i=({})=>Promise.reject({code:-1,msg:"请初始化HTTP请求方法主体"})){C(this,H,void 0);C(this,K,void 0);C(this,k,void 0);C(this,j,{"#":new Map,$:new Map});C(this,M,new Map);A(this,"request",({url:e="/",method:r="GET",...i},l=!1)=>{var x,z;const w=r.toUpperCase(),g=ae(JSON.stringify([e,w,i])),R=(x=e==null?void 0:e.includes)!=null&&x.call(e,"/_/")?"#":"$",{promise:V,resolve:J,reject:F}=Promise.withResolvers();return u(this,j)[R].has(g)?(console.log("缓存命中",e),J(u(this,j)[R].get(g))):(u(this,M).has(g)?u(this,M).get(g).push([J,F]):u(this,M).set(g,[[J,F]]),(z=u(this,M).get(g))!=null&&z.some(n=>n)&&u(this,H).call(this,{url:e,method:w,...i}).then(n=>{if(![0,1].includes(n.code))throw te(n.code,n.msg,n.data,e,n==null?void 0:n.res);return[n.code,n.msg,n.data,e,n==null?void 0:n.res]}).then(n=>((R=="#"||l&&w==="GET")&&u(this,j)[R].set(g,n),n)).then(n=>{var s;return(s=u(this,M).get(g))==null?void 0:s.forEach((f,o,h)=>{f&&f[0](n),delete h[o]})}).catch(n=>{var s;u(this,j)[R].delete(g),(s=u(this,M).get(g))==null||s.forEach((f,o,h)=>{f&&f[1](n instanceof ee?n:te(-1,n,null,e)),delete h[o]})}).finally(()=>u(this,M).delete(g))),new Proxy(V,{get:(n,s)=>(...f)=>n.then(o=>te(...o)[s](...f)).catch(o=>{if(!o instanceof ee)throw o;return typeof o[s]=="function"?o[s](...f):o[s]})})});A(this,"get",(e,r={})=>this.request({url:e,method:"GET",params:r}));A(this,"post",(e,r={},i={})=>this.request({url:e,method:"POST",data:r,params:i}));A(this,"put",(e,r={},i={})=>this.request({url:e,method:"PUT",data:r,params:i}));A(this,"del",(e,r={},i={})=>this.request({url:e,method:"DELETE",data:r,params:i}));A(this,"download",(e,r)=>{});A(this,"cache",(e,r={})=>this.request({url:e,method:"GET",params:r},!0));A(this,"list",(e,r={})=>this.get(`/api/${e}/${this.encode(r)}`));A(this,"getAppInfo",()=>{});A(this,"refreshCache",(e=!1)=>{e&&u(this,j)["#"].clear(),u(this,j).$.clear()});A(this,"decode",(e="",r={})=>{if(e===void 0||e===""||e===null)return r;try{const i=u(this,K).decode(e).buffer;return se.unpack(he(i))}catch{return console.warn("core.decode",e),r}});A(this,"encode",(e={})=>{const r=JSON.stringify(e,(i,l)=>l===void 0?null:l);return u(this,K).encode(fe(se.pack(r)))});I(this,k,e),I(this,H,i),I(this,K,ce(r))}}H=new WeakMap,K=new WeakMap,k=new WeakMap,j=new WeakMap,M=new WeakMap;const le=re;re.onUnhandledRejection=t=>{var e,r,i,l,w,g;t!=null&&t.IS_ECHO_MSG?((e=t==null?void 0:t.$echoMsg)==null||e.call(t),(r=t==null?void 0:t.preventDefault)==null||r.call(t)):(i=t.reason)!=null&&i.IS_ECHO_MSG&&((w=(l=t.reason)==null?void 0:l.$echoMsg)==null||w.call(l),(g=t==null?void 0:t.preventDefault)==null||g.call(t))},re.onMsg=(t,e)=>[0,1].includes(t)?console.log(e):console.warn(e),v.default=le,v.signature=ae,v.str2uint8array=fe,v.trim=we,v.uint8array2str=he,Object.defineProperties(v,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});