From 4bff9050c868e12086f4f3e9da315552361d79ec Mon Sep 17 00:00:00 2001 From: saqqdy Date: Tue, 30 Nov 2021 14:43:52 +0800 Subject: [PATCH] use http module --- ...29e370.js => polyfills-legacy.a3a493e7.js} | 2 +- app/www/index.html | 2 +- bin/core/api/getUserToken.ts | 2 +- bin/core/request.ts | 170 ++++++++++++++---- docs/changelog.md | 1 + lib/core/api/getUserToken.js | 2 +- lib/core/request.js | 125 ++++++++----- package.json | 4 +- script/lib.js | 3 +- 9 files changed, 232 insertions(+), 79 deletions(-) rename app/www/assets/{polyfills-legacy.5229e370.js => polyfills-legacy.a3a493e7.js} (96%) diff --git a/app/www/assets/polyfills-legacy.5229e370.js b/app/www/assets/polyfills-legacy.a3a493e7.js similarity index 96% rename from app/www/assets/polyfills-legacy.5229e370.js rename to app/www/assets/polyfills-legacy.a3a493e7.js index 24842f64..e37cac6d 100644 --- a/app/www/assets/polyfills-legacy.5229e370.js +++ b/app/www/assets/polyfills-legacy.a3a493e7.js @@ -1 +1 @@ -!function(){"use strict";var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},r=function(t){return t&&t.Math==Math&&t},e=r("object"==typeof globalThis&&globalThis)||r("object"==typeof window&&window)||r("object"==typeof self&&self)||r("object"==typeof t&&t)||function(){return this}()||Function("return this")(),n={},o=function(t){try{return!!t()}catch(r){return!0}},i=!o((function(){return 7!=Object.defineProperty({},1,{get:function(){return 7}})[1]})),a=Function.prototype.call,u=a.bind?a.bind(a):function(){return a.apply(a,arguments)},f={},c={}.propertyIsEnumerable,s=Object.getOwnPropertyDescriptor,l=s&&!c.call({1:2},1);f.f=l?function(t){var r=s(this,t);return!!r&&r.enumerable}:c;var h,p,v=function(t,r){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:r}},d=Function.prototype,g=d.bind,y=d.call,m=g&&g.bind(y),b=g?function(t){return t&&m(y,t)}:function(t){return t&&function(){return y.apply(t,arguments)}},w=b,x=w({}.toString),E=w("".slice),A=function(t){return E(x(t),8,-1)},S=b,O=o,T=A,R=e.Object,I=S("".split),j=O((function(){return!R("z").propertyIsEnumerable(0)}))?function(t){return"String"==T(t)?I(t,""):R(t)}:R,L=e.TypeError,P=function(t){if(null==t)throw L("Can't call method on "+t);return t},k=j,M=P,U=function(t){return k(M(t))},_=function(t){return"function"==typeof t},N=_,F=function(t){return"object"==typeof t?null!==t:N(t)},C=e,B=_,D=function(t){return B(t)?t:void 0},z=function(t,r){return arguments.length<2?D(C[t]):C[t]&&C[t][r]},W=b({}.isPrototypeOf),q=z("navigator","userAgent")||"",G=e,Y=q,V=G.process,$=G.Deno,J=V&&V.versions||$&&$.version,K=J&&J.v8;K&&(p=(h=K.split("."))[0]>0&&h[0]<4?1:+(h[0]+h[1])),!p&&Y&&(!(h=Y.match(/Edge\/(\d+)/))||h[1]>=74)&&(h=Y.match(/Chrome\/(\d+)/))&&(p=+h[1]);var H=p,X=H,Q=o,Z=!!Object.getOwnPropertySymbols&&!Q((function(){var t=Symbol();return!String(t)||!(Object(t)instanceof Symbol)||!Symbol.sham&&X&&X<41})),tt=Z&&!Symbol.sham&&"symbol"==typeof Symbol.iterator,rt=z,et=_,nt=W,ot=tt,it=e.Object,at=ot?function(t){return"symbol"==typeof t}:function(t){var r=rt("Symbol");return et(r)&&nt(r.prototype,it(t))},ut=e.String,ft=function(t){try{return ut(t)}catch(r){return"Object"}},ct=_,st=ft,lt=e.TypeError,ht=function(t){if(ct(t))return t;throw lt(st(t)+" is not a function")},pt=ht,vt=function(t,r){var e=t[r];return null==e?void 0:pt(e)},dt=u,gt=_,yt=F,mt=e.TypeError,bt={exports:{}},wt=e,xt=Object.defineProperty,Et=function(t,r){try{xt(wt,t,{value:r,configurable:!0,writable:!0})}catch(e){wt[t]=r}return r},At=Et,St="__core-js_shared__",Ot=e[St]||At(St,{}),Tt=Ot;(bt.exports=function(t,r){return Tt[t]||(Tt[t]=void 0!==r?r:{})})("versions",[]).push({version:"3.19.1",mode:"global",copyright:"© 2021 Denis Pushkarev (zloirock.ru)"});var Rt=P,It=e.Object,jt=function(t){return It(Rt(t))},Lt=jt,Pt=b({}.hasOwnProperty),kt=Object.hasOwn||function(t,r){return Pt(Lt(t),r)},Mt=b,Ut=0,_t=Math.random(),Nt=Mt(1..toString),Ft=function(t){return"Symbol("+(void 0===t?"":t)+")_"+Nt(++Ut+_t,36)},Ct=e,Bt=bt.exports,Dt=kt,zt=Ft,Wt=Z,qt=tt,Gt=Bt("wks"),Yt=Ct.Symbol,Vt=Yt&&Yt.for,$t=qt?Yt:Yt&&Yt.withoutSetter||zt,Jt=function(t){if(!Dt(Gt,t)||!Wt&&"string"!=typeof Gt[t]){var r="Symbol."+t;Wt&&Dt(Yt,t)?Gt[t]=Yt[t]:Gt[t]=qt&&Vt?Vt(r):$t(r)}return Gt[t]},Kt=u,Ht=F,Xt=at,Qt=vt,Zt=function(t,r){var e,n;if("string"===r&>(e=t.toString)&&!yt(n=dt(e,t)))return n;if(gt(e=t.valueOf)&&!yt(n=dt(e,t)))return n;if("string"!==r&>(e=t.toString)&&!yt(n=dt(e,t)))return n;throw mt("Can't convert object to primitive value")},tr=Jt,rr=e.TypeError,er=tr("toPrimitive"),nr=function(t,r){if(!Ht(t)||Xt(t))return t;var e,n=Qt(t,er);if(n){if(void 0===r&&(r="default"),e=Kt(n,t,r),!Ht(e)||Xt(e))return e;throw rr("Can't convert object to primitive value")}return void 0===r&&(r="number"),Zt(t,r)},or=nr,ir=at,ar=function(t){var r=or(t,"string");return ir(r)?r:r+""},ur=F,fr=e.document,cr=ur(fr)&&ur(fr.createElement),sr=function(t){return cr?fr.createElement(t):{}},lr=sr,hr=!i&&!o((function(){return 7!=Object.defineProperty(lr("div"),"a",{get:function(){return 7}}).a})),pr=i,vr=u,dr=f,gr=v,yr=U,mr=ar,br=kt,wr=hr,xr=Object.getOwnPropertyDescriptor;n.f=pr?xr:function(t,r){if(t=yr(t),r=mr(r),wr)try{return xr(t,r)}catch(e){}if(br(t,r))return gr(!vr(dr.f,t,r),t[r])};var Er={},Ar=e,Sr=F,Or=Ar.String,Tr=Ar.TypeError,Rr=function(t){if(Sr(t))return t;throw Tr(Or(t)+" is not an object")},Ir=i,jr=hr,Lr=Rr,Pr=ar,kr=e.TypeError,Mr=Object.defineProperty;Er.f=Ir?Mr:function(t,r,e){if(Lr(t),r=Pr(r),Lr(e),jr)try{return Mr(t,r,e)}catch(n){}if("get"in e||"set"in e)throw kr("Accessors not supported");return"value"in e&&(t[r]=e.value),t};var Ur=Er,_r=v,Nr=i?function(t,r,e){return Ur.f(t,r,_r(1,e))}:function(t,r,e){return t[r]=e,t},Fr={exports:{}},Cr=_,Br=Ot,Dr=b(Function.toString);Cr(Br.inspectSource)||(Br.inspectSource=function(t){return Dr(t)});var zr,Wr,qr,Gr=Br.inspectSource,Yr=_,Vr=Gr,$r=e.WeakMap,Jr=Yr($r)&&/native code/.test(Vr($r)),Kr=bt.exports,Hr=Ft,Xr=Kr("keys"),Qr=function(t){return Xr[t]||(Xr[t]=Hr(t))},Zr={},te=Jr,re=e,ee=b,ne=F,oe=Nr,ie=kt,ae=Ot,ue=Qr,fe=Zr,ce="Object already initialized",se=re.TypeError,le=re.WeakMap;if(te||ae.state){var he=ae.state||(ae.state=new le),pe=ee(he.get),ve=ee(he.has),de=ee(he.set);zr=function(t,r){if(ve(he,t))throw new se(ce);return r.facade=t,de(he,t,r),r},Wr=function(t){return pe(he,t)||{}},qr=function(t){return ve(he,t)}}else{var ge=ue("state");fe[ge]=!0,zr=function(t,r){if(ie(t,ge))throw new se(ce);return r.facade=t,oe(t,ge,r),r},Wr=function(t){return ie(t,ge)?t[ge]:{}},qr=function(t){return ie(t,ge)}}var ye={set:zr,get:Wr,has:qr,enforce:function(t){return qr(t)?Wr(t):zr(t,{})},getterFor:function(t){return function(r){var e;if(!ne(r)||(e=Wr(r)).type!==t)throw se("Incompatible receiver, "+t+" required");return e}}},me=i,be=kt,we=Function.prototype,xe=me&&Object.getOwnPropertyDescriptor,Ee=be(we,"name"),Ae={EXISTS:Ee,PROPER:Ee&&"something"===function(){}.name,CONFIGURABLE:Ee&&(!me||me&&xe(we,"name").configurable)},Se=e,Oe=_,Te=kt,Re=Nr,Ie=Et,je=Gr,Le=Ae.CONFIGURABLE,Pe=ye.get,ke=ye.enforce,Me=String(String).split("String");(Fr.exports=function(t,r,e,n){var o,i=!!n&&!!n.unsafe,a=!!n&&!!n.enumerable,u=!!n&&!!n.noTargetGet,f=n&&void 0!==n.name?n.name:r;Oe(e)&&("Symbol("===String(f).slice(0,7)&&(f="["+String(f).replace(/^Symbol\(([^)]*)\)/,"$1")+"]"),(!Te(e,"name")||Le&&e.name!==f)&&Re(e,"name",f),(o=ke(e)).source||(o.source=Me.join("string"==typeof f?f:""))),t!==Se?(i?!u&&t[r]&&(a=!0):delete t[r],a?t[r]=e:Re(t,r,e)):a?t[r]=e:Ie(r,e)})(Function.prototype,"toString",(function(){return Oe(this)&&Pe(this).source||je(this)}));var Ue={},_e=Math.ceil,Ne=Math.floor,Fe=function(t){var r=+t;return r!=r||0===r?0:(r>0?Ne:_e)(r)},Ce=Fe,Be=Math.max,De=Math.min,ze=function(t,r){var e=Ce(t);return e<0?Be(e+r,0):De(e,r)},We=Fe,qe=Math.min,Ge=function(t){return t>0?qe(We(t),9007199254740991):0},Ye=Ge,Ve=function(t){return Ye(t.length)},$e=U,Je=ze,Ke=Ve,He=function(t){return function(r,e,n){var o,i=$e(r),a=Ke(i),u=Je(n,a);if(t&&e!=e){for(;a>u;)if((o=i[u++])!=o)return!0}else for(;a>u;u++)if((t||u in i)&&i[u]===e)return t||u||0;return!t&&-1}},Xe={includes:He(!0),indexOf:He(!1)},Qe=kt,Ze=U,tn=Xe.indexOf,rn=Zr,en=b([].push),nn=function(t,r){var e,n=Ze(t),o=0,i=[];for(e in n)!Qe(rn,e)&&Qe(n,e)&&en(i,e);for(;r.length>o;)Qe(n,e=r[o++])&&(~tn(i,e)||en(i,e));return i},on=["constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"],an=nn,un=on.concat("length","prototype");Ue.f=Object.getOwnPropertyNames||function(t){return an(t,un)};var fn={};fn.f=Object.getOwnPropertySymbols;var cn=z,sn=Ue,ln=fn,hn=Rr,pn=b([].concat),vn=cn("Reflect","ownKeys")||function(t){var r=sn.f(hn(t)),e=ln.f;return e?pn(r,e(t)):r},dn=kt,gn=vn,yn=n,mn=Er,bn=function(t,r){for(var e=gn(r),n=mn.f,o=yn.f,i=0;ii;i++)if((u=g(t[i]))&&$o(Zo,u))return u;return new Qo(!1)}n=Jo(t,o)}for(f=n.next;!(c=Wo(f,n)).done;){try{u=g(c.value)}catch(y){Ho(n,"throw",y)}if("object"==typeof u&&u&&$o(Zo,u))return u}return new Qo(!1)},ri=Jt("iterator"),ei=!1;try{var ni=0,oi={next:function(){return{done:!!ni++}},return:function(){ei=!0}};oi[ri]=function(){return this},Array.from(oi,(function(){throw 2}))}catch(e_){}var ii,ai,ui,fi,ci=function(t,r){if(!r&&!ei)return!1;var e=!1;try{var n={};n[ri]=function(){return{next:function(){return{done:e=!0}}}},t(n)}catch(e_){}return e},si=b,li=o,hi=_,pi=So,vi=Gr,di=function(){},gi=[],yi=z("Reflect","construct"),mi=/^\s*(?:class|function)\b/,bi=si(mi.exec),wi=!mi.exec(di),xi=function(t){if(!hi(t))return!1;try{return yi(di,gi,t),!0}catch(e_){return!1}},Ei=!yi||li((function(){var t;return xi(xi.call)||!xi(Object)||!xi((function(){t=!0}))||t}))?function(t){if(!hi(t))return!1;switch(pi(t)){case"AsyncFunction":case"GeneratorFunction":case"AsyncGeneratorFunction":return!1}return wi||!!bi(mi,vi(t))}:xi,Ai=Ei,Si=ft,Oi=e.TypeError,Ti=function(t){if(Ai(t))return t;throw Oi(Si(t)+" is not a constructor")},Ri=Rr,Ii=Ti,ji=Jt("species"),Li=function(t,r){var e,n=Ri(t).constructor;return void 0===n||null==(e=Ri(n)[ji])?r:Ii(e)},Pi=Function.prototype,ki=Pi.apply,Mi=Pi.bind,Ui=Pi.call,_i="object"==typeof Reflect&&Reflect.apply||(Mi?Ui.bind(ki):function(){return Ui.apply(ki,arguments)}),Ni=z("document","documentElement"),Fi=b([].slice),Ci=/(?:ipad|iphone|ipod).*applewebkit/i.test(q),Bi="process"==A(e.process),Di=e,zi=_i,Wi=fo,qi=_,Gi=kt,Yi=o,Vi=Ni,$i=Fi,Ji=sr,Ki=Ci,Hi=Bi,Xi=Di.setImmediate,Qi=Di.clearImmediate,Zi=Di.process,ta=Di.Dispatch,ra=Di.Function,ea=Di.MessageChannel,na=Di.String,oa=0,ia={},aa="onreadystatechange";try{ii=Di.location}catch(e_){}var ua=function(t){if(Gi(ia,t)){var r=ia[t];delete ia[t],r()}},fa=function(t){return function(){ua(t)}},ca=function(t){ua(t.data)},sa=function(t){Di.postMessage(na(t),ii.protocol+"//"+ii.host)};Xi&&Qi||(Xi=function(t){var r=$i(arguments,1);return ia[++oa]=function(){zi(qi(t)?t:ra(t),void 0,r)},ai(oa),oa},Qi=function(t){delete ia[t]},Hi?ai=function(t){Zi.nextTick(fa(t))}:ta&&ta.now?ai=function(t){ta.now(fa(t))}:ea&&!Ki?(fi=(ui=new ea).port2,ui.port1.onmessage=ca,ai=Wi(fi.postMessage,fi)):Di.addEventListener&&qi(Di.postMessage)&&!Di.importScripts&&ii&&"file:"!==ii.protocol&&!Yi(sa)?(ai=sa,Di.addEventListener("message",ca,!1)):ai=aa in Ji("script")?function(t){Vi.appendChild(Ji("script")).onreadystatechange=function(){Vi.removeChild(this),ua(t)}}:function(t){setTimeout(fa(t),0)});var la,ha,pa,va,da,ga,ya,ma,ba={set:Xi,clear:Qi},wa=e,xa=/ipad|iphone|ipod/i.test(q)&&void 0!==wa.Pebble,Ea=/web0s(?!.*chrome)/i.test(q),Aa=e,Sa=fo,Oa=n.f,Ta=ba.set,Ra=Ci,Ia=xa,ja=Ea,La=Bi,Pa=Aa.MutationObserver||Aa.WebKitMutationObserver,ka=Aa.document,Ma=Aa.process,Ua=Aa.Promise,_a=Oa(Aa,"queueMicrotask"),Na=_a&&_a.value;Na||(la=function(){var t,r;for(La&&(t=Ma.domain)&&t.exit();ha;){r=ha.fn,ha=ha.next;try{r()}catch(e_){throw ha?va():pa=void 0,e_}}pa=void 0,t&&t.enter()},Ra||La||ja||!Pa||!ka?!Ia&&Ua&&Ua.resolve?((ya=Ua.resolve(void 0)).constructor=Ua,ma=Sa(ya.then,ya),va=function(){ma(la)}):La?va=function(){Ma.nextTick(la)}:(Ta=Sa(Ta,Aa),va=function(){Ta(la)}):(da=!0,ga=ka.createTextNode(""),new Pa(la).observe(ga,{characterData:!0}),va=function(){ga.data=da=!da}));var Fa=Na||function(t){var r={fn:t,next:void 0};pa&&(pa.next=r),ha||(ha=r,va()),pa=r},Ca={},Ba=ht,Da=function(t){var r,e;this.promise=new t((function(t,n){if(void 0!==r||void 0!==e)throw TypeError("Bad Promise constructor");r=t,e=n})),this.resolve=Ba(r),this.reject=Ba(e)};Ca.f=function(t){return new Da(t)};var za,Wa,qa,Ga,Ya=Rr,Va=F,$a=Ca,Ja=function(t,r){if(Ya(t),Va(r)&&r.constructor===t)return r;var e=$a.f(t);return(0,e.resolve)(r),e.promise},Ka=e,Ha="object"==typeof window,Xa=Nn,Qa=e,Za=z,tu=u,ru=Fn,eu=Fr.exports,nu=Bn,ou=$n,iu=Xn,au=eo,uu=ht,fu=_,cu=F,su=io,lu=Gr,hu=ti,pu=ci,vu=Li,du=ba.set,gu=Fa,yu=Ja,mu=function(t,r){var e=Ka.console;e&&e.error&&(1==arguments.length?e.error(t):e.error(t,r))},bu=Ca,wu=function(t){try{return{error:!1,value:t()}}catch(e_){return{error:!0,value:e_}}},xu=ye,Eu=In,Au=Ha,Su=Bi,Ou=H,Tu=Jt("species"),Ru="Promise",Iu=xu.get,ju=xu.set,Lu=xu.getterFor(Ru),Pu=ru&&ru.prototype,ku=ru,Mu=Pu,Uu=Qa.TypeError,_u=Qa.document,Nu=Qa.process,Fu=bu.f,Cu=Fu,Bu=!!(_u&&_u.createEvent&&Qa.dispatchEvent),Du=fu(Qa.PromiseRejectionEvent),zu="unhandledrejection",Wu=!1,qu=Eu(Ru,(function(){var t=lu(ku),r=t!==String(ku);if(!r&&66===Ou)return!0;if(Ou>=51&&/native code/.test(t))return!1;var e=new ku((function(t){t(1)})),n=function(t){t((function(){}),(function(){}))};return(e.constructor={})[Tu]=n,!(Wu=e.then((function(){}))instanceof n)||!r&&Au&&!Du})),Gu=qu||!pu((function(t){ku.all(t).catch((function(){}))})),Yu=function(t){var r;return!(!cu(t)||!fu(r=t.then))&&r},Vu=function(t,r){if(!t.notified){t.notified=!0;var e=t.reactions;gu((function(){for(var n=t.value,o=1==t.state,i=0;e.length>i;){var a,u,f,c=e[i++],s=o?c.ok:c.fail,l=c.resolve,h=c.reject,p=c.domain;try{s?(o||(2===t.rejection&&Hu(t),t.rejection=1),!0===s?a=n:(p&&p.enter(),a=s(n),p&&(p.exit(),f=!0)),a===c.promise?h(Uu("Promise-chain cycle")):(u=Yu(a))?tu(u,a,l,h):l(a)):h(n)}catch(e_){p&&!f&&p.exit(),h(e_)}}t.reactions=[],t.notified=!1,r&&!t.rejection&&Ju(t)}))}},$u=function(t,r,e){var n,o;Bu?((n=_u.createEvent("Event")).promise=r,n.reason=e,n.initEvent(t,!1,!0),Qa.dispatchEvent(n)):n={promise:r,reason:e},!Du&&(o=Qa["on"+t])?o(n):t===zu&&mu("Unhandled promise rejection",e)},Ju=function(t){tu(du,Qa,(function(){var r,e=t.facade,n=t.value;if(Ku(t)&&(r=wu((function(){Su?Nu.emit("unhandledRejection",n,e):$u(zu,e,n)})),t.rejection=Su||Ku(t)?2:1,r.error))throw r.value}))},Ku=function(t){return 1!==t.rejection&&!t.parent},Hu=function(t){tu(du,Qa,(function(){var r=t.facade;Su?Nu.emit("rejectionHandled",r):$u("rejectionhandled",r,t.value)}))},Xu=function(t,r,e){return function(n){t(r,n,e)}},Qu=function(t,r,e){t.done||(t.done=!0,e&&(t=e),t.value=r,t.state=2,Vu(t,!0))},Zu=function(t,r,e){if(!t.done){t.done=!0,e&&(t=e);try{if(t.facade===r)throw Uu("Promise can't be resolved itself");var n=Yu(r);n?gu((function(){var e={done:!1};try{tu(n,r,Xu(Zu,e,t),Xu(Qu,e,t))}catch(e_){Qu(e,e_,t)}})):(t.value=r,t.state=1,Vu(t,!1))}catch(e_){Qu({done:!1},e_,t)}}};if(qu&&(Mu=(ku=function(t){su(this,Mu),uu(t),tu(za,this);var r=Iu(this);try{t(Xu(Zu,r),Xu(Qu,r))}catch(e_){Qu(r,e_)}}).prototype,(za=function(t){ju(this,{type:Ru,done:!1,notified:!1,parent:!1,reactions:[],rejection:!1,state:0,value:void 0})}).prototype=nu(Mu,{then:function(t,r){var e=Lu(this),n=e.reactions,o=Fu(vu(this,ku));return o.ok=!fu(t)||t,o.fail=fu(r)&&r,o.domain=Su?Nu.domain:void 0,e.parent=!0,n[n.length]=o,0!=e.state&&Vu(e,!1),o.promise},catch:function(t){return this.then(void 0,t)}}),Wa=function(){var t=new za,r=Iu(t);this.promise=t,this.resolve=Xu(Zu,r),this.reject=Xu(Qu,r)},bu.f=Fu=function(t){return t===ku||t===qa?new Wa(t):Cu(t)},fu(ru)&&Pu!==Object.prototype)){Ga=Pu.then,Wu||(eu(Pu,"then",(function(t,r){var e=this;return new ku((function(t,r){tu(Ga,e,t,r)})).then(t,r)}),{unsafe:!0}),eu(Pu,"catch",Mu.catch,{unsafe:!0}));try{delete Pu.constructor}catch(e_){}ou&&ou(Pu,Mu)}Xa({global:!0,wrap:!0,forced:qu},{Promise:ku}),iu(ku,Ru,!1),au(Ru),qa=Za(Ru),Xa({target:Ru,stat:!0,forced:qu},{reject:function(t){var r=Fu(this);return tu(r.reject,void 0,t),r.promise}}),Xa({target:Ru,stat:!0,forced:qu},{resolve:function(t){return yu(this,t)}}),Xa({target:Ru,stat:!0,forced:Gu},{all:function(t){var r=this,e=Fu(r),n=e.resolve,o=e.reject,i=wu((function(){var e=uu(r.resolve),i=[],a=0,u=1;hu(t,(function(t){var f=a++,c=!1;u++,tu(e,r,t).then((function(t){c||(c=!0,i[f]=t,--u||n(i))}),o)})),--u||n(i)}));return i.error&&o(i.value),e.promise},race:function(t){var r=this,e=Fu(r),n=e.reject,o=wu((function(){var o=uu(r.resolve);hu(t,(function(t){tu(o,r,t).then(e.resolve,n)}))}));return o.error&&n(o.value),e.promise}});var tf,rf=nn,ef=on,nf=Object.keys||function(t){return rf(t,ef)},of=Er,af=Rr,uf=U,ff=nf,cf=i?Object.defineProperties:function(t,r){af(t);for(var e,n=uf(r),o=ff(r),i=o.length,a=0;i>a;)of.f(t,e=o[a++],n[e]);return t},sf=Rr,lf=cf,hf=on,pf=Zr,vf=Ni,df=sr,gf=Qr("IE_PROTO"),yf=function(){},mf=function(t){return" - + diff --git a/bin/core/api/getUserToken.ts b/bin/core/api/getUserToken.ts index c3d1246a..8a3da6e7 100644 --- a/bin/core/api/getUserToken.ts +++ b/bin/core/api/getUserToken.ts @@ -25,7 +25,7 @@ async function getUserToken(): Promise { } const fetchData = - ((await request({ url: `${config.api}?name=${user}` })) + ((await request.get({ url: `${config.api}?name=${user}` })) .data as FetchDataType) || null // 没有查到用户信息或者没有设置token if (!fetchData) { diff --git a/bin/core/request.ts b/bin/core/request.ts index 082422b5..02aec713 100644 --- a/bin/core/request.ts +++ b/bin/core/request.ts @@ -1,37 +1,145 @@ const https = require('https') +const http = require('http') +const zlib = require('zlib') const { URL } = require('url') +const qs = require('qs') -function request(options: any) { - const url = new URL(options.url) - return new Promise((resolve, reject) => { - const req = https.request( - { - hostname: url.hostname, - port: url.port, - path: `${url.pathname}${url.search}`, - method: 'GET', - ...options - }, - (res: any) => { - res.on('data', (buf: any) => { - const data = Buffer.from(buf).toString() - let result = null - try { - result = JSON.parse(data) - } catch { - // - } - if (result.code === 0) { - resolve(result) - } else { - reject(result) - } - }) +class Request { + cookies: string[] = [] + constructor() { + // if (cookie) { + // this.setCookie(cookie) + // } + } + /** + * 获取请求头 + * + * @param host - 请求的域名 + * @param propsData - 请求数据 + * @returns headers - 请求头 + */ + public getHeaders(host: string, postData?: string) { + const headers: { [prop: string]: string | number | boolean } = { + Host: host, + rejectUnauthorized: false, + Pragma: 'no-cache', + Connection: 'keep-alive', + 'Cache-Control': 'no-cache', + 'Content-Type': 'application/x-www-form-urlencoded', + 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2', + Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', + 'User-Agent': + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36' + } + if (this.cookies.length) { + headers.Cookie = this.cookies.join('; ') + } + if (postData) { + headers['Content-Length'] = Buffer.byteLength(postData) + } + return headers + } + /** + * 设置cookie + * + * @param cookie - cookie + * @returns this - 当前类 + */ + public setCookie(cookie: string) { + const cookies = cookie.split(';') + for (let item of cookies) { + item = item.replace(/^\s/, '') + this.cookies.push(item) + } + return this + } + /** + * 发起请求 + * + * @param method - 请求方法:'GET' | 'POST' | 'DELETE' | 'OPTIONS' + * @param url - 请求链接 + * @param params - 请求参数 + * @returns Promise - 请求结果 + */ + public request( + method: 'GET' | 'POST' | 'DELETE' | 'OPTIONS', + url: string, + params?: object + ) { + const postData = qs.stringify(params || {}, { + arrayFormat: 'indices', + allowDots: true + }) + const urlObj = new URL(url) + const options = { + hostname: urlObj.hostname, + port: urlObj.port, + path: urlObj.pathname + urlObj.search, + method, + headers: this.getHeaders(urlObj.host, postData) + } + return new Promise((resolve, reject) => { + const req = (urlObj.protocol == 'http:' ? http : https).request( + options, + (res: any) => { + const chunks: any[] = [] + res.on('data', (buf: any) => { + // const data = JSON.parse(Buffer.from(buf).toString()) + chunks.push(buf) + }) + res.on('end', () => { + const buffer = Buffer.concat(chunks) + const encoding = res.headers['content-encoding'] + if (encoding === 'gzip') { + zlib.gunzip( + buffer, + function (err: Error, decoded: any) { + resolve(decoded.toString()) + } + ) + } else if (encoding === 'deflate') { + zlib.inflate( + buffer, + function (err: Error, decoded: any) { + resolve(decoded.toString()) + } + ) + } else { + resolve(JSON.parse(buffer.toString())) + } + }) + } + ) + req.on('error', (err: Error) => { + reject(err) + }) + if (postData) { + req.write(postData) } - ) - req.on('error', reject) - req.end() - }) + req.end() + }) + } + /** + * get方法 + * + * @param option - 参数 + * @param option.url - 请求链接 + * @returns Promise - 请求结果 + */ + public async get({ url }: any) { + return await this.request('GET', url) + } + /** + * post方法 + * + * @param option - 参数 + * @param option.url - 请求链接 + * @param option.params - 请求参数 + * @returns Promise - 请求结果 + */ + public async post({ url, params }: any) { + return await this.request('POST', url, params) + } } -module.exports = request +module.exports = new Request() diff --git a/docs/changelog.md b/docs/changelog.md index bd8d25e6..d740c806 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,7 @@ ## 2021.11.30 v2.14.4 更新日志 1. 替换`curl`脚本,处理win7不能执行curl的兼容问题 +2. 封装`request`方法 ## 2021.11.28 v2.14.3 更新日志 diff --git a/lib/core/api/getUserToken.js b/lib/core/api/getUserToken.js index 550dd17e..3992963c 100644 --- a/lib/core/api/getUserToken.js +++ b/lib/core/api/getUserToken.js @@ -16,7 +16,7 @@ async function getUserToken() { sh.echo(error("\u8BF7\u8BBE\u7F6E\u672C\u5730git\u7528\u6237\u540D")); process.exit(1); } - const fetchData = (await request({ url: `${config.api}?name=${user}` })).data || null; + const fetchData = (await request.get({ url: `${config.api}?name=${user}` })).data || null; if (!fetchData) { sh.echo(error("\u6CA1\u6709\u627E\u5230\u7528\u6237\uFF0C\u8BF7\u8054\u7CFB\u7BA1\u7406\u5458")); process.exit(1); diff --git a/lib/core/request.js b/lib/core/request.js index e9f96f8f..f34cbd5d 100644 --- a/lib/core/request.js +++ b/lib/core/request.js @@ -1,48 +1,91 @@ 'use strict'; -var __defProp = Object.defineProperty; -var __getOwnPropSymbols = Object.getOwnPropertySymbols; -var __hasOwnProp = Object.prototype.hasOwnProperty; -var __propIsEnum = Object.prototype.propertyIsEnumerable; -var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; -var __spreadValues = (a, b) => { - for (var prop in b || (b = {})) - if (__hasOwnProp.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - if (__getOwnPropSymbols) - for (var prop of __getOwnPropSymbols(b)) { - if (__propIsEnum.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - } - return a; -}; const https = require("https"); +const http = require("http"); +const zlib = require("zlib"); const { URL } = require("url"); -function request(options) { - const url = new URL(options.url); - return new Promise((resolve, reject) => { - const req = https.request(__spreadValues({ - hostname: url.hostname, - port: url.port, - path: `${url.pathname}${url.search}`, - method: "GET" - }, options), (res) => { - res.on("data", (buf) => { - const data = Buffer.from(buf).toString(); - let result = null; - try { - result = JSON.parse(data); - } catch (e) { - } - if (result.code === 0) { - resolve(result); - } else { - reject(result); - } +const qs = require("qs"); +class Request { + constructor() { + this.cookies = []; + } + getHeaders(host, postData) { + const headers = { + Host: host, + rejectUnauthorized: false, + Pragma: "no-cache", + Connection: "keep-alive", + "Cache-Control": "no-cache", + "Content-Type": "application/x-www-form-urlencoded", + "Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2", + Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36" + }; + if (this.cookies.length) { + headers.Cookie = this.cookies.join("; "); + } + if (postData) { + headers["Content-Length"] = Buffer.byteLength(postData); + } + return headers; + } + setCookie(cookie) { + const cookies = cookie.split(";"); + for (let item of cookies) { + item = item.replace(/^\s/, ""); + this.cookies.push(item); + } + return this; + } + request(method, url, params) { + const postData = qs.stringify(params || {}, { + arrayFormat: "indices", + allowDots: true + }); + const urlObj = new URL(url); + const options = { + hostname: urlObj.hostname, + port: urlObj.port, + path: urlObj.pathname + urlObj.search, + method, + headers: this.getHeaders(urlObj.host, postData) + }; + return new Promise((resolve, reject) => { + const req = (urlObj.protocol == "http:" ? http : https).request(options, (res) => { + const chunks = []; + res.on("data", (buf) => { + chunks.push(buf); + }); + res.on("end", () => { + const buffer = Buffer.concat(chunks); + const encoding = res.headers["content-encoding"]; + if (encoding === "gzip") { + zlib.gunzip(buffer, function(err, decoded) { + resolve(decoded.toString()); + }); + } else if (encoding === "deflate") { + zlib.inflate(buffer, function(err, decoded) { + resolve(decoded.toString()); + }); + } else { + resolve(JSON.parse(buffer.toString())); + } + }); + }); + req.on("error", (err) => { + reject(err); }); + if (postData) { + req.write(postData); + } + req.end(); }); - req.on("error", reject); - req.end(); - }); + } + async get({ url }) { + return await this.request("GET", url); + } + async post({ url, params }) { + return await this.request("POST", url, params); + } } -module.exports = request; +module.exports = new Request(); diff --git a/package.json b/package.json index 88d9c4ed..4782b335 100644 --- a/package.json +++ b/package.json @@ -65,12 +65,14 @@ "node-pty": "^0.10.1", "ora": "^5.4.1", "os": "^0.1.2", + "qs": "^6.10.1", "shelljs": "^0.8.4", "slash": "^3.0.0", "socket.io": "4.4.0", "text-table": "^0.2.0", "tracer": "1.1.5", - "uuid": "^8.3.2" + "uuid": "^8.3.2", + "zlib": "^1.0.5" }, "devDependencies": { "@microsoft/api-extractor": "^8.0.0", diff --git a/script/lib.js b/script/lib.js index dfb307cd..820e9be3 100644 --- a/script/lib.js +++ b/script/lib.js @@ -3,7 +3,7 @@ * @Author: * @LastEditors: saqqdy * @Date: 2021-07-22 22:39:42 - * @LastEditTime: 2021-07-23 00:00:12 + * @LastEditTime: 2021-11-30 14:41:08 */ const fs = require('fs') const path = require('path') @@ -23,7 +23,6 @@ const readDir = entry => { }) } const getInfo = url => { - console.log(url, url.replace(/^bin\//, 'lib/')) builder .build({ entryPoints: [url],