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