justrun-ws 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/WsClient.js +3 -3
- package/lib/bundle.min.js +1 -1
- package/lib/handler/SimpleHandler.js +2 -2
- package/lib/net/NetErrorString.js +1 -1
- package/lib/net/TypedPkgHub.js +24 -19
- package/package.json +2 -2
package/lib/WsClient.js
CHANGED
|
@@ -20,15 +20,15 @@ export const createNumPkgTypeClient = (getConnStr, timeoutMonitor) => {
|
|
|
20
20
|
export class WsClient {
|
|
21
21
|
constructor(getConnStr, w) {
|
|
22
22
|
const F = new r();
|
|
23
|
-
const
|
|
23
|
+
const D = this.d = new v(F, w);
|
|
24
24
|
this.h = '';
|
|
25
25
|
this.j = [];
|
|
26
26
|
this.q = 0;
|
|
27
27
|
this.a = new g(() => this.c());
|
|
28
28
|
this.H = getConnStr;
|
|
29
29
|
this.O = this.c;
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
D.K = this.K.bind(this);
|
|
31
|
+
D.B = this.B.bind(this);
|
|
32
32
|
}
|
|
33
33
|
registerError(pkgType, wrapperCtor) {
|
|
34
34
|
return this.d.registerError(pkgType, wrapperCtor);
|
package/lib/bundle.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{EK:()=>p,sr:()=>i,si:()=>o,N3:()=>u,sk:()=>d,wL:()=>l,bM:()=>n,Lm:()=>y,h8:()=>h,Yu:()=>b,I9:()=>I,iU:()=>$,mx:()=>f});class s{constructor(t,e){e?this.x=t:this.y=t}get data(){return this.y||(this.y=this.decode(this.x)),this.y}set data(t){this.y=t,this.x=void 0}get buff(){return this.x||(this.x=this.encode(this.y)),this.x}x;y}class r extends s{static context;static registerPkgType(t){let e;try{e=this.pkgType}catch{return void Object.defineProperty(this,"pkgType",{value:t,writable:!1,enumerable:!1})}if(e!==t)throw new Error(`Wrapper ${this.name} is already registered with type ${e}`)}static get pkgType(){throw new Error(`Wrapper ${this.name} need to be registered`)}}class i extends r{isError(){return!0}}class n extends r{isMessage(){return!0}}class o extends r{isEvent(){return!0}}class h extends r{constructor(t,e){super(t,e)}isRequest(){return!0}get resp(){return this.z||(this.z=this.decodeResponse(this.$)),this.z}set resp(t){this.z=t,this.$=void 0}get respBuff(){return this.$||(this.$=this.encodeResponse(this.z)),this.$}set respBuff(t){this.z=void 0,this.$=t}z;$}const a=new TextEncoder,c=new TextDecoder;class u extends i{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}}class d extends n{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}}class l extends h{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}encodeResponse(t){const e=JSON.stringify(t);return a.encode(e)}decodeResponse(t){const e=c.decode(t);return JSON.parse(e)}}class p extends u{}let g=(t,e)=>new p(e.message);const f=t=>{g=t};var y;!function(t){t.I="AuthTimeout",t.a="BadRequest",t.k="ConnectFailed",t.a1="ConnectionClosed",t.b="InvalidConnString",t.l="InvalidConnection",t.D="InvalidPackage",t.g="NotAuthenticated",t.a2="Timeout",t.m="UnknownError"}(y||(y={}));class w{constructor(){this.e=this,this.i=this}getPrev(){return this.e}getNext(){return this.i}insertBefore(t){this.i=t,this.e=t.e,t.e=this,this.e.i=this}insertAfter(t){this.e=t,this.i=t.i,t.i=this,this.i.e=this}remove(){this.e.i=this.i,this.i.e=this.e}e;i}class m{constructor(){this._=new w}get f(){return this._.getNext()}get a(){return this._.getPrev()}P(t){return t===this._}Q(t){t.insertAfter(this._)}a6(t){t.insertBefore(this._)}_}const k=()=>{},v=t=>{let e=0,s=0;for(;e<t.length;)s+=t[e++].length;const r=new Uint8Array(s);for(s=0;e>0;){const i=t[--e];r.set(i,s),s+=i.length}return r};class E extends w{}class b{constructor(t,e,s){if(this.R=t,this.I=e,this.E=s,this.o=0,this.F=0,this.r=[],0===t)return this.insert=k,this.update=k,void(this.remove=k);for(let t=0;t<s;t++)this.r.push(new m)}insert(t){this.F||(this.S=setInterval(this.a2,this.R)),this.F++,this.r[this.o].Q(t)}update(t){t.remove(),this.r[this.o].Q(t)}remove(t){t.remove(),this.F--,this.F||clearInterval(this.S)}a2=()=>{const{I:t,E:e,r:s}=this;if(this.o++,this.o===e&&(this.o=0),t){let r=this.o+t;r>=e&&(r-=e);const i=s[r];for(let t=i.f;!i.P(t);t=t.getNext())t.keepAlive()}const r=s[this.o];for(let t=r.f;!r.P(t);t=t.getNext())t.timeout()};R;I;E;o;F;S;r}class P{constructor(t,e){this.W=new Promise((t=>{this.G=t})),this.N=e,t>0?this.u=setTimeout((()=>{this.u=void 0,this.N?.(),this.G()}),t):this.G()}a3(t){this.u&&clearTimeout(this.u),t>0?this.u=setTimeout((()=>{this.u=void 0,this.N?.(),this.G()}),t):(this.u=void 0,this.G())}async Y(){await this.W}G;W;u;N}const T=new Error(y.g);class x{constructor(t,e,s=!0){this.l=t,this.v=e,this.w=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async $(t,e){const{buff:s,bodyOffset:r}=e,i=s.subarray(r),n=new this.l(i,!0);if(void 0===t.context&&this.w)g(t,T);else try{await this.v(t,n)}catch(e){e.isError||g(t,e)}void 0===t.context&&await t.close()}async r(t,e){if(void 0===t.context&&this.w)g(t,T);else try{return await this.v(t,e)}catch(e){e.isError||g(t,e)}}l;v;w}class R{constructor(t,e,s=!0){this.l=t,this.v=e,this.w=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async $(t,e){const{buff:s,requestId:r,bodyOffset:i}=e,n=s.subarray(i),o=new this.l(n,!0);let h;if(void 0===t.context&&this.w)h=g(t,T);else try{await this.v(t,o)}catch(e){h=e.isError?e:g(t,e)}try{h?await t.sendErrorResponse(h,r):await t.sendResponse(o,r),void 0===t.context&&await t.close()}catch(e){await t.close(e)}}async r(t,e){if(void 0!==t.context||!this.w)try{return await this.v(t,e)}catch(e){throw e.isError?e:g(t,e)}throw g(t,T)}l;v;w}class A{o(){return null}P(t){this.D=t}async Q(t){return!1}async R(){return!1}async c(t){const e=new WebSocket(t);return new Promise(((t,s)=>{e.onerror=t=>{s(new Error(y.m))},e.onopen=()=>{t(this.a4(e,!0))}}))}a4=(t,e)=>{const s=new q(t),{conn:r,E:i,n}=this.D(s,e);return t.onmessage=t=>{t.data.arrayBuffer().then((t=>{i(new Uint8Array(t))}))},t.onerror=t=>{n(new Error(y.m))},t.onclose=t=>{1e3===t.code?n():n(new Error(t.reason))},r};D(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class q{constructor(t){this.g=t}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async N(t){if(this.g)return this.g.send(t);throw new Error(y.l)}async p(t){if(this.g){const e=v(t);return this.g.send(e)}throw new Error(y.l)}async close(t){const{g:e}=this;e&&(this.g=void 0,t?e.close(3e3,t.message):e.readyState===WebSocket.OPEN&&e.close(1e3))}O(){return!this.g}g}const M=(t,e,s)=>{const r=[e,t],i=(t=>{let e,s=0,r=0;for(;s<t.length;)for(r++,e=t[s++];e>>>=7;)r++;return r})(r),n=new Uint8Array(i);((t,e,s=0)=>{let r,i,n=s;for(r=0;r<t.length;r++){i=t[r];let s=127&i;for(;i>>>=7;)e[n++]=128|s,s=127&i;e[n++]=s}if(n<=e.length)return n-s;throw new Error("Buffer overflow")})(r,n),s.push(n)},N=t=>{const{h:[e,s],k:r}=((t,e,s=0)=>{let r,i,n=s,o=0,h=0;const a=[];for(r=0;r<t;r++){do{i=e[n++],o|=(127&i)<<h,h+=7}while(128&i);if(!(void 0!==i&&h<=35))throw new Error("Parsing failed");a.push(o),o=0,h=0}return{h:a,k:n-s}})(2,t);return{buff:t,pkgType:s,requestId:e,bodyOffset:r}};class B{constructor(t){this.a1=t}get id(){return this.a?.id??-1}get context(){return this.a?.context}set context(t){this.a&&(this.a.context=t)}localAddress;localPort;remoteAddress;remoteFamily;remotePort;async sendErrorResponse(t,e){return(this.a??await this.c()).sendErrorResponse(t,e)}async sendResponse(t,e){return(this.a??await this.c()).sendResponse(t,e)}async sendMessage(t){return(this.a??await this.c()).sendMessage(t)}async sendRequest(t){return(this.a??await this.c()).sendRequest(t)}async close(t){const{a:e}=this;if(e)return this.a=void 0,void await e.close(t);this.p&&await this.p.then((e=>{this.a=void 0,e.close(t)}),(()=>this.a=void 0))}async reconnect(){this.a||await this.c()}a5(){this.a=void 0}async c(){let{p:t}=this;return t||(t=this.p=new Promise(((t,e)=>{this.a1().then((s=>{s?(t(s),this.a=s,this.p=void 0):e(new Error("Connect failed"))}))}))),await t}a1;a;p}class W{constructor(t,e){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:i,parsePkgBuff:n}=e;this.responsePkgType=s,this.s=0,this.t=new Map,this.f=new Map,this.L=new Map,this.T=i,this.U=n,this.A=t,this.J=r,this.$=this.a7;const o=[];this.T(s,2097152,o),this.V={makePkgBuff:i,responsePkgType:s,u:v(o)},this.f.set(s,this),this.A.P(((t,e)=>{do{this.s++}while(this.t.has(2097151&this.s));const s=new O(this.V,t,2097151&this.s);this.t.set(s.id,s),this.J?.insert(s);const r={conn:s,E:t=>this.a8(s,t),n:t=>this.a9(s,t)};return this.K?.(this,s,!!e),r}))}responsePkgType;context;K;B;o(){return this.A.o()}a3(t){return this.t.get(t)}c(t){return this.A.c(t)}async a4(){do{this.s++}while(this.t.has(2097151&this.s));const t=new C(2097151&this.s,this);return this.t.set(t.id,t),t}async a5(t){return this.A.Q(t)}async a6(){return this.A.R()}ab(t,e){const s=e.constructor.pkgType,r=this.f.get(s);if(r)return r?.r(t,e);throw new Error(y.D)}ac(t,e){const s=e.constructor.pkgType,r=this.f.get(s);if(r)return r?.r(t,e);throw new Error(y.D)}registerError(t,e){e.registerPkgType(t),this.L.set(t,e);const s=this.f.get(this.responsePkgType);this.f.set(t,s)}registerMessage(t,e){e.registerPkgType(t)}registerRequest(t,e){e.registerPkgType(t)}handleMessageWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.m} has an incompatible pkgType ${typeof e}`);if(this.f.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.f.set(e,t),t}handleRequestWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.m} has an incompatible pkgType ${typeof e}`);if(this.f.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.f.set(e,t),t}ao(t){const e=t.pkgType;return this.f.get(e)===t&&(this.f.delete(e),!0)}a8(t,e){this.J?.update(t);const s=this.U(e),r=this.f.get(s.pkgType);r?r.$(t,s):t.close(new Error(y.D))}a9(t,e){const s=t.id;this.t.delete(s),this.J?.remove(t),t.n(e),this.B?.(this,t,t.b.O(),e)}a7(t,e){const{buff:s,pkgType:r,requestId:i,bodyOffset:n}=e;if(r===this.responsePkgType){const e=s.subarray(n);t.X(i,e)}else{const e=this.L.get(r);if(e){const r=new e(s.subarray(n),!0);t.ag(i,r)}}}V;s;T;U;A;t;f;L;J}class O extends E{constructor(t,e,s){super(),this.id=s,this.b=e,this.C=t,this.Z=0}context;id;b;get localAddress(){return this.b.localAddress}get localPort(){return this.b.localPort}get remoteAddress(){return this.b.remoteAddress}get remoteFamily(){return this.b.remoteFamily}get remotePort(){return this.b.remotePort}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.C.makePkgBuff(e,0,s),this.b.p(s)}sendRequest(t){return this.k=new Map,this.sendRequest=this.a0,this.X=this.ah,this.n=this.ai,this.a0(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,r=[t.buff];return this.C.makePkgBuff(s,e,r),this.b.p(r)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:r,responsePkgType:i}=this.C;return r(i,e,s),this.b.p(s)}async close(t){await this.b.close(t)}X(t,e){2097152===t&&void 0===this.context&&this.close(new Error(y.I))}ag(t,e){const s=this.k.get(t);if(s)return this.k.delete(t),s.i(e)}keepAlive(){this.b.N(this.C.u).catch(k)}timeout(){this.close(new Error(y.a2))}n(t){}async a0(t){const e=2097151&this.Z++,s=t.constructor.pkgType,r=[t.buff];this.C.makePkgBuff(s,e,r);const i=new Promise(((t,s)=>{const i={d:t,i:s};this.k.set(e,i),this.b.p(r).catch((t=>{this.k.delete(e),s(t)}))}));t.respBuff=await i}ah(t,e){const s=this.k.get(t);if(s)return this.k.delete(t),void s.d(e);2097152===t&&void 0===this.context&&this.close(new Error(y.I))}ai(t){const e=this.k;if(e.size>0){t||(t=new Error(y.a1));for(const[s,r]of e)e.delete(s),r.i(t)}this.k=void 0}C;k;Z}class C{constructor(t,e){this.id=t,this.M=e}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(t){return this.M.ab(this,t)}async sendRequest(t){return this.M.ac(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}M}class S extends l{}const $=(t,e)=>new I(t,{responsePkgType:0,makePkgBuff:M,parsePkgBuff:N,timeoutMonitor:e});class I{constructor(t,e){const s=new A,r=this.d=new W(s,e);this.h="",this.j=[],this.q=0,this.a=new B((()=>this.c())),this.H=t,this.O=this.c,r.K=this.K.bind(this),r.B=this.B.bind(this)}registerError(t,e){return this.d.registerError(t,e)}registerMessage(t,e){return this.d.registerMessage(t,e)}registerRequest(t,e){return this.d.registerRequest(t,e)}handleMessageWith(t,e){this.d.handleMessageWith(new x(t,((t,s)=>e(s.data)),!1))}handleRequestWith(t,e){this.d.handleRequestWith(new R(t,(async(t,s)=>{s.resp=await e(s.data)}),!1))}sendMessage(t){return this.a.sendMessage(t)}async sendRequest(t){return await this.a.sendRequest(t),t.resp}async sendCustomAuthRequest(t){throw new Error('Need to call "enableAuthenticate" before using sendCustomAuthRequest')}resetAuthState(){}enableAuthenticate(t,e,s){const{O:r,H:i,d:n,a:o}=this;if(this.c===r){n.registerRequest(t,S);let r,h="";this.c=async()=>{for(this.q=-1,this.m&&await this.m.Y();;){const t=i(r);let o;try{o=await n.c(t)}catch{return void this.B(n,this.a,!0,new Error(y.k))}if(this.h||s){const t=new S({cid:this.h,token:h});try{await o.sendRequest(t);const{resp:s}=t;if(this.h=s.cid,h=s.token,r=s.shard,r&&r<0){r=-r-1,o.close();continue}e(this.h)}catch(t){return t instanceof p&&"HitSessionLimit"===t.data&&(this.h="",h="",r=void 0,e(this.h)),void o.close(t)}}return o}},this.sendCustomAuthRequest=async t=>{await o.sendRequest(t);const{resp:s}=t;return this.h=s.cid,h=s.token,r=s.shard,r&&r<0&&(r=-r-1,o.close()),e(this.h),s},this.resetAuthState=()=>{this.h&&(this.h="",h="",r=void 0,o.close(),e(this.h))}}}reconnect(t){const{a:e,q:s}=this;s>0||(this.m?this.m.a3(t??0):t&&(this.m=new P(t,(()=>this.m=void 0))),s||e.reconnect())}onConnectionStatus(t){let{j:e}=this;e||(this.j=e=[]),e.push(t)}async c(){const{H:t,d:e,m:s}=this;this.q=-1,s&&await s.Y();const r=t();try{return e.c(r)}catch{this.B(e,this.a,!0,new Error(y.k))}}K(t,e,s){if(this.q=1,e.context="",this.j)for(const t of this.j)t(!0,s)}B(t,e,s,r){if(this.q=0,this.a.a5(),s||this.resetAuthState(),this.j)for(const t of this.j)t(!1,s,r)}d;a;h;q;H;O;m;j}var J=e.EK,F=e.sr,j=e.si,L=e.N3,U=e.sk,z=e.wL,D=e.bM,K=e.Lm,_=e.h8,G=e.Yu,H=e.I9,Q=e.iU,Y=e.mx;export{J as CommonError,F as ErrorWrapper,j as EventWrapper,L as JsonError,U as JsonMessage,z as JsonRequest,D as MessageWrapper,K as NetErrorString,_ as RequestWrapper,G as TimeoutMonitor,H as WsClient,Q as createNumPkgTypeClient,Y as handleUncaughtErrorWith};
|
|
1
|
+
class t{constructor(t,e){e?this.x=t:this.y=t}get data(){return this.y||(this.y=this.decode(this.x)),this.y}set data(t){this.y=t,this.x=void 0}get buff(){return this.x||(this.x=this.encode(this.y)),this.x}x;y}class e extends t{static context;static registerPkgType(t){let e;try{e=this.pkgType}catch{return void Object.defineProperty(this,"pkgType",{value:t,writable:!1,enumerable:!1})}if(e!==t)throw new Error(`Wrapper ${this.name} is already registered with type ${e}`)}static get pkgType(){throw new Error(`Wrapper ${this.name} need to be registered`)}}class s extends e{isError(){return!0}}class r extends e{isMessage(){return!0}}class i extends e{isEvent(){return!0}}class n extends e{constructor(t,e){super(t,e)}isRequest(){return!0}get resp(){return this.z||(this.z=this.decodeResponse(this.$)),this.z}set resp(t){this.z=t,this.$=void 0}get respBuff(){return this.$||(this.$=this.encodeResponse(this.z)),this.$}set respBuff(t){this.z=void 0,this.$=t}z;$}const o=new TextEncoder,h=new TextDecoder;class a extends s{encode(t){const e=JSON.stringify(t);return o.encode(e)}decode(t){const e=h.decode(t);return JSON.parse(e)}}class c extends r{encode(t){const e=JSON.stringify(t);return o.encode(e)}decode(t){const e=h.decode(t);return JSON.parse(e)}}class d extends n{encode(t){const e=JSON.stringify(t);return o.encode(e)}decode(t){const e=h.decode(t);return JSON.parse(e)}encodeResponse(t){const e=JSON.stringify(t);return o.encode(e)}decodeResponse(t){const e=h.decode(t);return JSON.parse(e)}}class u extends a{}let l=(t,e)=>new u(e.message);const p=t=>{l=t};var g;!function(t){t.I="AuthTimeout",t.a="BadRequest",t.k="ConnectFailed",t.a1="ConnectionClosed",t.b="InvalidConnString",t.l="InvalidConnection",t.z="InvalidPackage",t.g="NotAuthenticated",t.a2="Timeout",t.m="UnknownError"}(g||(g={}));class f{constructor(){this.e=this,this.i=this}getPrev(){return this.e}getNext(){return this.i}insertBefore(t){this.i=t,this.e=t.e,t.e=this,this.e.i=this}insertAfter(t){this.e=t,this.i=t.i,t.i=this,this.i.e=this}remove(){this.e.i=this.i,this.i.e=this.e}e;i}class w{constructor(){this._=new f}get f(){return this._.getNext()}get a(){return this._.getPrev()}P(t){return t===this._}Q(t){t.insertAfter(this._)}a6(t){t.insertBefore(this._)}_}const y=()=>{},m=t=>{let e=0,s=0;for(;e<t.length;)s+=t[e++].length;const r=new Uint8Array(s);for(s=0;e>0;){const i=t[--e];r.set(i,s),s+=i.length}return r};class k extends f{}class v{constructor(t,e,s){if(this.R=t,this.I=e,this.E=s,this.o=0,this.F=0,this.r=[],0===t)return this.insert=y,this.update=y,void(this.remove=y);for(let t=0;t<s;t++)this.r.push(new w)}insert(t){this.F||(this.S=setInterval(this.a2,this.R)),this.F++,this.r[this.o].Q(t)}update(t){t.remove(),this.r[this.o].Q(t)}remove(t){t.remove(),this.F--,this.F||clearInterval(this.S)}a2=()=>{const{I:t,E:e,r:s}=this;if(this.o++,this.o===e&&(this.o=0),t){let r=this.o+t;r>=e&&(r-=e);const i=s[r];for(let t=i.f;!i.P(t);t=t.getNext())t.keepAlive()}const r=s[this.o];for(let t=r.f;!r.P(t);t=t.getNext())t.timeout()};R;I;E;o;F;S;r}class b{constructor(t,e){this.W=new Promise((t=>{this.G=t})),this.N=e,t>0?this.u=setTimeout((()=>{this.u=void 0,this.N?.(),this.G()}),t):this.G()}a3(t){this.u&&clearTimeout(this.u),t>0?this.u=setTimeout((()=>{this.u=void 0,this.N?.(),this.G()}),t):(this.u=void 0,this.G())}async Y(){await this.W}G;W;u;N}const E=new Error(g.g);class P{constructor(t,e,s=!0){this.l=t,this.v=e,this.w=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async _(t,e){const{buff:s,bodyOffset:r}=e,i=s.subarray(r),n=new this.l(i,!0);if(void 0===t.context&&this.w)l(t,E);else try{await this.v(t,n)}catch(e){e.isError||l(t,e)}void 0===t.context&&await t.close()}async r(t,e){if(void 0===t.context&&this.w)l(t,E);else try{return await this.v(t,e)}catch(e){e.isError||l(t,e)}}l;v;w}class T{constructor(t,e,s=!0){this.l=t,this.v=e,this.w=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async _(t,e){const{buff:s,requestId:r,bodyOffset:i}=e,n=s.subarray(i),o=new this.l(n,!0);let h;if(void 0===t.context&&this.w)h=l(t,E);else try{await this.v(t,o)}catch(e){h=e.isError?e:l(t,e)}try{h?await t.sendErrorResponse(h,r):await t.sendResponse(o,r),void 0===t.context&&await t.close()}catch(e){await t.close(e)}}async r(t,e){if(void 0!==t.context||!this.w)try{return await this.v(t,e)}catch(e){throw e.isError?e:l(t,e)}throw l(t,E)}l;v;w}class x{o(){return null}P(t){this.D=t}async Q(t){return!1}async R(){return!1}async c(t){const e=new WebSocket(t);return new Promise(((t,s)=>{e.onerror=t=>{s(new Error(g.m))},e.onopen=()=>{t(this.a4(e,!0))}}))}a4=(t,e)=>{const s=new R(t),{conn:r,E:i,n}=this.D(s,e);return t.onmessage=t=>{t.data.arrayBuffer().then((t=>{i(new Uint8Array(t))}))},t.onerror=t=>{n(new Error(g.m))},t.onclose=t=>{1e3===t.code?n():n(new Error(t.reason))},r};D(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class R{constructor(t){this.g=t}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async N(t){if(this.g)return this.g.send(t);throw new Error(g.l)}async p(t){if(this.g){const e=m(t);return this.g.send(e)}throw new Error(g.l)}async close(t){const{g:e}=this;e&&(this.g=void 0,t?e.close(3e3,t.message):e.readyState===WebSocket.OPEN&&e.close(1e3))}O(){return!this.g}g}const A=(t,e,s)=>{const r=[e,t],i=(t=>{let e,s=0,r=0;for(;s<t.length;)for(r++,e=t[s++];e>>>=7;)r++;return r})(r),n=new Uint8Array(i);((t,e,s=0)=>{let r,i,n=s;for(r=0;r<t.length;r++){i=t[r];let s=127&i;for(;i>>>=7;)e[n++]=128|s,s=127&i;e[n++]=s}if(n<=e.length)return n-s;throw new Error("Buffer overflow")})(r,n),s.push(n)},q=t=>{const{h:[e,s],k:r}=((t,e,s=0)=>{let r,i,n=s,o=0,h=0;const a=[];for(r=0;r<t;r++){do{i=e[n++],o|=(127&i)<<h,h+=7}while(128&i);if(!(void 0!==i&&h<=35))throw new Error("Parsing failed");a.push(o),o=0,h=0}return{h:a,k:n-s}})(2,t);return{buff:t,pkgType:s,requestId:e,bodyOffset:r}};class B{constructor(t){this.a1=t}get id(){return this.a?.id??-1}get context(){return this.a?.context}set context(t){this.a&&(this.a.context=t)}localAddress;localPort;remoteAddress;remoteFamily;remotePort;async sendErrorResponse(t,e){return(this.a??await this.c()).sendErrorResponse(t,e)}async sendResponse(t,e){return(this.a??await this.c()).sendResponse(t,e)}async sendMessage(t){return(this.a??await this.c()).sendMessage(t)}async sendRequest(t){return(this.a??await this.c()).sendRequest(t)}async close(t){const{a:e}=this;if(e)return this.a=void 0,void await e.close(t);this.p&&await this.p.then((e=>{this.a=void 0,e.close(t)}),(()=>this.a=void 0))}async reconnect(){this.a||await this.c()}a5(){this.a=void 0}async c(){let{p:t}=this;return t||(t=this.p=new Promise(((t,e)=>{this.a1().then((s=>{s?(t(s),this.a=s,this.p=void 0):e(new Error("Connect failed"))}))}))),await t}a1;a;p}class M{constructor(t,e){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:i,parsePkgBuff:n}=e;this.responsePkgType=s,this.s=0,this.t=new Map,this.f=new Map,this.L=new Map,this.T=i,this.U=n,this.A=t,this.J=r,this._=this.a7;const o=[];this.T(s,2097152,o),this.V={makePkgBuff:i,responsePkgType:s,u:m(o)},this.f.set(s,this),this.A.P(((t,e)=>{do{this.s++}while(this.t.has(2097151&this.s));const s=new N(this.V,t,2097151&this.s);this.t.set(s.id,s),this.J?.insert(s);const r={conn:s,E:t=>this.a8(s,t),n:t=>this.a9(s,t)};return this.K?.(this,s,!!e),r}))}responsePkgType;context;K;B;o(){return this.A.o()}a3(t){return this.t.get(t)}c(t){return this.A.c(t)}async a4(){do{this.s++}while(this.t.has(2097151&this.s));const t=new S(2097151&this.s,this);return this.t.set(t.id,t),t}async a5(t){return this.A.Q(t)}async a6(){return this.A.R()}ab(t,e){const s=e.constructor.pkgType,r=this.f.get(s);if(r)return r?.r(t,e);throw new Error(g.z)}ac(t,e){const s=e.constructor.pkgType,r=this.f.get(s);if(r)return r?.r(t,e);throw new Error(g.z)}registerError(t,e){e.registerPkgType(t),this.L.set(t,e);const s=this.f.get(this.responsePkgType);this.f.set(t,s)}registerMessage(t,e){e.registerPkgType(t)}registerRequest(t,e){e.registerPkgType(t)}handleMessageWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.m} has an incompatible pkgType ${typeof e}`);if(this.f.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.f.set(e,t),t}handleRequestWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.m} has an incompatible pkgType ${typeof e}`);if(this.f.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.f.set(e,t),t}ao(t){const e=t.pkgType;return this.f.get(e)===t&&(this.f.delete(e),!0)}a8(t,e){this.J?.update(t);const s=this.U(e),r=this.f.get(s.pkgType);r?r._(t,s):t.close(new Error(g.z))}a9(t,e){const s=t.id;this.t.delete(s),this.J?.remove(t),t.n(e),this.B?.(this,t,t.b.O(),e)}a7(t,e){try{const{buff:s,pkgType:r,requestId:i,bodyOffset:n}=e;if(r===this.responsePkgType){const e=s.subarray(n);t.X(i,e)}else{const e=this.L.get(r);if(e){const r=new e(s.subarray(n),!0);t.ag(i,r)}}}catch{t.close(new Error(g.z))}}V;s;T;U;A;t;f;L;J}class N extends k{constructor(t,e,s){super(),this.id=s,this.b=e,this.C=t,this.Z=0}context;id;b;get localAddress(){return this.b.localAddress}get localPort(){return this.b.localPort}get remoteAddress(){return this.b.remoteAddress}get remoteFamily(){return this.b.remoteFamily}get remotePort(){return this.b.remotePort}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.C.makePkgBuff(e,0,s),this.b.p(s)}sendRequest(t){return this.k=new Map,this.sendRequest=this.a0,this.X=this.ah,this.n=this.ai,this.a0(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,r=[t.buff];return this.C.makePkgBuff(s,e,r),this.b.p(r)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:r,responsePkgType:i}=this.C;return r(i,e,s),this.b.p(s)}async close(t){await this.b.close(t)}X(t,e){2097152===t&&void 0===this.context&&this.close(new Error(g.I))}ag(t,e){const s=this.k.get(t);if(s)return this.k.delete(t),s.i(e)}keepAlive(){this.b.N(this.C.u).catch(y)}timeout(){this.close(new Error(g.a2))}n(t){}async a0(t){const e=2097151&this.Z++,s=t.constructor.pkgType,r=[t.buff];this.C.makePkgBuff(s,e,r);const i=new Promise(((t,s)=>{const i={d:t,i:s};this.k.set(e,i),this.b.p(r).catch((t=>{this.k.delete(e),s(t)}))}));t.respBuff=await i}ah(t,e){const s=this.k.get(t);if(s)return this.k.delete(t),void s.d(e);2097152===t&&void 0===this.context&&this.close(new Error(g.I))}ai(t){const e=this.k;if(e.size>0){t||(t=new Error(g.a1));for(const[s,r]of e)e.delete(s),r.i(t)}this.k=void 0}C;k;Z}class S{constructor(t,e){this.id=t,this.M=e}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(t){return this.M.ab(this,t)}async sendRequest(t){return this.M.ac(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}M}class O extends d{}const C=(t,e)=>new W(t,{responsePkgType:0,makePkgBuff:A,parsePkgBuff:q,timeoutMonitor:e});class W{constructor(t,e){const s=new x,r=this.d=new M(s,e);this.h="",this.j=[],this.q=0,this.a=new B((()=>this.c())),this.H=t,this.O=this.c,r.K=this.K.bind(this),r.B=this.B.bind(this)}registerError(t,e){return this.d.registerError(t,e)}registerMessage(t,e){return this.d.registerMessage(t,e)}registerRequest(t,e){return this.d.registerRequest(t,e)}handleMessageWith(t,e){this.d.handleMessageWith(new P(t,((t,s)=>e(s.data)),!1))}handleRequestWith(t,e){this.d.handleRequestWith(new T(t,(async(t,s)=>{s.resp=await e(s.data)}),!1))}sendMessage(t){return this.a.sendMessage(t)}async sendRequest(t){return await this.a.sendRequest(t),t.resp}async sendCustomAuthRequest(t){throw new Error('Need to call "enableAuthenticate" before using sendCustomAuthRequest')}resetAuthState(){}enableAuthenticate(t,e,s){const{O:r,H:i,d:n,a:o}=this;if(this.c===r){n.registerRequest(t,O);let r,h="";this.c=async()=>{for(this.q=-1,this.m&&await this.m.Y();;){const t=i(r);let o;try{o=await n.c(t)}catch{return void this.B(n,this.a,!0,new Error(g.k))}if(this.h||s){const t=new O({cid:this.h,token:h});try{await o.sendRequest(t);const{resp:s}=t;if(this.h=s.cid,h=s.token,r=s.shard,r&&r<0){r=-r-1,o.close();continue}e(this.h)}catch(t){return t instanceof u&&"HitSessionLimit"===t.data&&(this.h="",h="",r=void 0,e(this.h)),void o.close(t)}}return o}},this.sendCustomAuthRequest=async t=>{await o.sendRequest(t);const{resp:s}=t;return this.h=s.cid,h=s.token,r=s.shard,r&&r<0&&(r=-r-1,o.close()),e(this.h),s},this.resetAuthState=()=>{this.h&&(this.h="",h="",r=void 0,o.close(),e(this.h))}}}reconnect(t){const{a:e,q:s}=this;s>0||(this.m?this.m.a3(t??0):t&&(this.m=new b(t,(()=>this.m=void 0))),s||e.reconnect())}onConnectionStatus(t){let{j:e}=this;e||(this.j=e=[]),e.push(t)}async c(){const{H:t,d:e,m:s}=this;this.q=-1,s&&await s.Y();const r=t();try{return e.c(r)}catch{this.B(e,this.a,!0,new Error(g.k))}}K(t,e,s){if(this.q=1,e.context="",this.j)for(const t of this.j)t(!0,s)}B(t,e,s,r){if(this.q=0,this.a.a5(),s||this.resetAuthState(),this.j)for(const t of this.j)t(!1,s,r)}d;a;h;q;H;O;m;j}export{u as CommonError,s as ErrorWrapper,i as EventWrapper,a as JsonError,c as JsonMessage,d as JsonRequest,r as MessageWrapper,g as NetErrorString,n as RequestWrapper,v as TimeoutMonitor,W as WsClient,C as createNumPkgTypeClient,p as handleUncaughtErrorWith};
|
|
@@ -13,7 +13,7 @@ export class n {
|
|
|
13
13
|
get m() {
|
|
14
14
|
return this.l.name;
|
|
15
15
|
}
|
|
16
|
-
async
|
|
16
|
+
async _(conn, a) {
|
|
17
17
|
const { buff, bodyOffset } = a;
|
|
18
18
|
const h = buff.subarray(bodyOffset);
|
|
19
19
|
const message = new this.l(h, true);
|
|
@@ -65,7 +65,7 @@ export class o {
|
|
|
65
65
|
get m() {
|
|
66
66
|
return this.l.name;
|
|
67
67
|
}
|
|
68
|
-
async
|
|
68
|
+
async _(conn, a) {
|
|
69
69
|
const { buff, requestId, bodyOffset } = a;
|
|
70
70
|
const h = buff.subarray(bodyOffset);
|
|
71
71
|
const request = new this.l(h, true);
|
|
@@ -6,7 +6,7 @@ export var NetErrorString;
|
|
|
6
6
|
NetErrorString["a1"] = "ConnectionClosed";
|
|
7
7
|
NetErrorString["b"] = "InvalidConnString";
|
|
8
8
|
NetErrorString["l"] = "InvalidConnection";
|
|
9
|
-
NetErrorString["
|
|
9
|
+
NetErrorString["z"] = "InvalidPackage";
|
|
10
10
|
NetErrorString["g"] = "NotAuthenticated";
|
|
11
11
|
NetErrorString["a2"] = "Timeout";
|
|
12
12
|
NetErrorString["m"] = "UnknownError";
|
package/lib/net/TypedPkgHub.js
CHANGED
|
@@ -14,7 +14,7 @@ export class v {
|
|
|
14
14
|
this.A = F;
|
|
15
15
|
this.J = timeoutMonitor;
|
|
16
16
|
const S = this;
|
|
17
|
-
S
|
|
17
|
+
S._ = this.a7;
|
|
18
18
|
const u = [];
|
|
19
19
|
this.T(responsePkgType, 0x200000, u);
|
|
20
20
|
this.V = {
|
|
@@ -46,8 +46,8 @@ export class v {
|
|
|
46
46
|
o() {
|
|
47
47
|
return this.A.o();
|
|
48
48
|
}
|
|
49
|
-
a3(
|
|
50
|
-
return this.t.get(
|
|
49
|
+
a3($) {
|
|
50
|
+
return this.t.get($);
|
|
51
51
|
}
|
|
52
52
|
c(e) {
|
|
53
53
|
return this.A.c(e);
|
|
@@ -72,7 +72,7 @@ export class v {
|
|
|
72
72
|
if (handler) {
|
|
73
73
|
return handler?.r(conn, message);
|
|
74
74
|
}
|
|
75
|
-
throw new Error(NetErrorString.
|
|
75
|
+
throw new Error(NetErrorString.z);
|
|
76
76
|
}
|
|
77
77
|
ac(conn, request) {
|
|
78
78
|
const pkgType = request.constructor.pkgType;
|
|
@@ -80,7 +80,7 @@ export class v {
|
|
|
80
80
|
if (handler) {
|
|
81
81
|
return handler?.r(conn, request);
|
|
82
82
|
}
|
|
83
|
-
throw new Error(NetErrorString.
|
|
83
|
+
throw new Error(NetErrorString.z);
|
|
84
84
|
}
|
|
85
85
|
registerError(pkgType, wrapperCtor) {
|
|
86
86
|
wrapperCtor.registerPkgType(pkgType);
|
|
@@ -130,32 +130,37 @@ export class v {
|
|
|
130
130
|
const a = this.U(buff);
|
|
131
131
|
const handler = this.f.get(a.pkgType);
|
|
132
132
|
if (handler) {
|
|
133
|
-
handler
|
|
133
|
+
handler._(conn, a);
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
136
|
-
conn.close(new Error(NetErrorString.
|
|
136
|
+
conn.close(new Error(NetErrorString.z));
|
|
137
137
|
}
|
|
138
138
|
a9(conn, err) {
|
|
139
|
-
const
|
|
140
|
-
this.t.delete(
|
|
139
|
+
const $ = conn.id;
|
|
140
|
+
this.t.delete($);
|
|
141
141
|
this.J?.remove(conn);
|
|
142
142
|
conn.n(err);
|
|
143
143
|
this.B?.(this, conn, conn.b.O(), err);
|
|
144
144
|
}
|
|
145
145
|
a7(conn, a) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
conn.X(requestId, h);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
const Y = this.L.get(pkgType);
|
|
153
|
-
if (Y) {
|
|
146
|
+
try {
|
|
147
|
+
const { buff, pkgType, requestId, bodyOffset } = a;
|
|
148
|
+
if (pkgType === this.responsePkgType) {
|
|
154
149
|
const h = buff.subarray(bodyOffset);
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
conn.X(requestId, h);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const Y = this.L.get(pkgType);
|
|
154
|
+
if (Y) {
|
|
155
|
+
const h = buff.subarray(bodyOffset);
|
|
156
|
+
const af = new Y(h, true);
|
|
157
|
+
conn.ag(requestId, af);
|
|
158
|
+
}
|
|
157
159
|
}
|
|
158
160
|
}
|
|
161
|
+
catch {
|
|
162
|
+
conn.close(new Error(NetErrorString.z));
|
|
163
|
+
}
|
|
159
164
|
}
|
|
160
165
|
V;
|
|
161
166
|
s;
|