justrun-ws 0.1.2 → 0.1.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/bundle.min.js +1 -1
- package/lib/handler/SimpleHandler.js +9 -15
- package/lib/handler/common.js +6 -9
- package/lib/index.d.ts +23 -16
- package/lib/index.js +1 -0
- package/lib/net/hubPkgSerializer.js +2 -2
- package/package.json +1 -1
package/lib/bundle.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e={d:(t,s)=>{for(var r in s)e.o(s,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:s[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{yE:()=>w,sr:()=>n,si:()=>i,N3:()=>d,sk:()=>u,wL:()=>p,p6:()=>b,bM:()=>o,h8:()=>h,Kr:()=>f,Yu:()=>B,Kl:()=>C,kX:()=>v,jG:()=>P});class s{constructor(e,t){t?(this.k=e,this.l=void 0):(this.k=void 0,this.l=e)}get data(){return this.l||(this.l=this.decode(this.k)),this.l}set data(e){this.l=e,this.k=void 0}get buff(){return this.k||(this.k=this.encode(this.l)),this.k}k;l}class r extends s{static context;static registerPkgType(e){let t;try{t=this.pkgType}catch{return void Object.defineProperty(this,"pkgType",{value:e,writable:!1,enumerable:!1})}if(t!==e)throw new Error(`Wrapper ${this.name} is already registered with type ${t}`)}static get pkgType(){throw new Error(`Wrapper ${this.name} need to be registered`)}}class n extends r{isError(){return!0}}class o extends r{isMessage(){return!0}}class i extends r{isEvent(){return!0}}class h extends r{constructor(e,t){super(e,t)}isRequest(){return!0}get resp(){return this.r||(this.r=this.decodeResponse(this.s)),this.r}set resp(e){this.r=e,this.s=void 0}get respBuff(){return this.s||(this.s=this.encodeResponse(this.r)),this.s}set respBuff(e){this.r=void 0,this.s=e}r;s}const a=new TextEncoder,c=new TextDecoder;class d extends n{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}}class u extends o{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}}class p extends h{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}encodeResponse(e){const t=JSON.stringify(e);return a.encode(t)}decodeResponse(e){const t=c.decode(e);return JSON.parse(t)}}class l extends d{}const g={message:"Connection is not authenticated"};class f{constructor(e,t,s=!0){this.g=e,this.m=t,this.n=s}get pkgType(){return this.g.pkgType}get pkgName(){return this.g.name}async run(e,t){const{buff:s,bodyOffset:r}=t,n=s.subarray(r),o=new this.g(n,!0);if(void 0===e.context&&this.n)this.wrapUncaughtError(e,g);else try{await this.m(e,o)}catch(t){t.isError||this.wrapUncaughtError(e,t)}void 0===e.context&&await e.close()}async runLocal(e,t){if(void 0===e.context&&this.n)this.wrapUncaughtError(e,g);else try{return await this.m(e,t)}catch(t){t.isError||this.wrapUncaughtError(e,t)}}wrapUncaughtError(e,t){return((e,t)=>((e,t)=>new l(t.message))(0,t))(0,t)}g;m;n}const y=()=>{},k=e=>{let t=0,s=0;for(;t<e.length;)s+=e[t++].length;const r=new Uint8Array(s);for(s=0;t>0;){const n=e[--t];r.set(n,s),s+=n.length}return r};class w{address(){return null}setConnWrapperMaker(e){this.makeWrapper=e}async startListen(e){return!1}async stopListen(){return!1}async connect(e){const t=new WebSocket(e);return new Promise(((e,s)=>{t.onerror=e=>{s(new Error("WebSocket error"))},t.onopen=()=>{e(this.L(t,!0))}}))}L=(e,t)=>{const s=new m(e),{conn:r,onPkgBuff:n,onClose:o}=this.makeWrapper(s,t);return e.onmessage=e=>{e.data.arrayBuffer().then((e=>{n(new Uint8Array(e))}))},e.onerror=e=>{o(new Error("WebSocket error"))},e.onclose=e=>{1e3===e.code&&o()},r};makeWrapper(e,t){throw new Error('Need to call "setConnWrapperMaker" before using')}}class m{constructor(e){this.d=e}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async sendPkgBuff(e){if(this.d)return this.d.send(e);throw new Error("Invalid socket")}async sendPkgBuffV(e){if(this.d){const t=k(e);return this.d.send(t)}throw new Error("Invalid socket")}async close(e){const{d:t}=this;t&&(this.d=void 0,e?t.close(3e3,e.message):t.readyState===WebSocket.OPEN&&t.close(1e3))}hadClosed(){return!this.d}d}const v=(e,t,s)=>{const r=[t,e],n=(e=>{let t,s=0,r=0;for(;s<e.length;)for(r++,t=e[s++];t>>>=7;)r++;return r})(r),o=new Uint8Array(n);((e,t,s=0)=>{let r,n,o=s;for(r=0;r<e.length;r++){n=e[r];let s=127&n;for(;n>>>=7;)t[o++]=128|s,s=127&n;t[o++]=s}if(o<=t.length)return o-s;throw new Error("Buffer overflow")})(r,o),s.push(o)},P=e=>{const{h:[t,s],k:r}=((e,t,s=0)=>{let r,n,o=s,i=0,h=0;const a=[];for(r=0;r<e;r++){do{n=t[o++],i|=(127&n)<<h,h+=7}while(128&n);if(!(void 0!==n&&h<=35))throw new Error("Parsing failed");a.push(i),i=0,h=0}return{h:a,k:o-s}})(2,e);return{buff:e,pkgType:s,requestId:t,bodyOffset:r}};class b{constructor(e){this.A=e}get id(){return this.b?.id??-1}get context(){return this.b?.context}set context(e){this.b&&(this.b.context=e)}localAddress;localPort;remoteAddress;remoteFamily;remotePort;async sendErrorResponse(e,t){return(this.b??await this.connect()).sendErrorResponse(e,t)}async sendResponse(e,t){return(this.b??await this.connect()).sendResponse(e,t)}async sendMessage(e){return(this.b??await this.connect()).sendMessage(e)}async sendRequest(e){return(this.b??await this.connect()).sendRequest(e)}async close(e){const{b:t}=this;if(t)return this.b=void 0,void await t.close(e);this.i&&await this.i.then((t=>t.close(e).then((()=>this.b=void 0))),(()=>this.b=void 0))}async connect(){let{i:e}=this;return e||(e=this.i=new Promise(((e,t)=>{this.A().then((s=>{s?(e(s),this.b=s,this.i=void 0):t(new Error("Connect failed"))}))}))),await e}A;b;i}class E{constructor(){this.e=this,this.f=this}getPrev(){return this.e}getNext(){return this.f}insertBefore(e){this.f=e,this.e=e.e,e.e=this,this.e.f=this}insertAfter(e){this.e=e,this.f=e.f,e.f=this,this.f.e=this}remove(){this.e.f=this.f,this.f.e=this.e}e;f}class x{constructor(){this.t=new E}get g(){return this.t.getNext()}get a(){return this.t.getPrev()}B(e){return e===this.t}C(e){e.insertAfter(this.t)}U(e){e.insertBefore(this.t)}t}class T extends E{}class B{constructor(e,t,s){if(this.D=e,this.y=t,this.w=s,this.j=0,this.x=0,this.o=[],0===e)return this.insert=y,this.update=y,void(this.remove=y);for(let e=0;e<s;e++)this.o.push(new x)}insert(e){this.x||(this.E=setInterval(this.M,this.D)),this.x++,this.o[this.j].C(e)}update(e){e.remove(),this.o[this.j].C(e)}remove(e){e.remove(),this.x--,this.x||clearInterval(this.E)}M=()=>{const{y:e,w:t,o:s}=this;if(this.j++,this.j===t&&(this.j=0),e){let r=this.j+e;r>=t&&(r-=t);const n=s[r];for(let e=n.g;!n.B(e);e=e.getNext())e.keepAlive()}const r=s[this.j];for(let e=r.g;!r.B(e);e=e.getNext())e.timeout()};D;y;w;j;x;E;o}class C{constructor(e,t){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:n,parsePkgBuff:o}=t;this.responsePkgType=s,this.context=void 0,this.onConnOpen=void 0,this.onConnClose=void 0,this.p=0,this.q=new Map,this.c=new Map,this.$=new Map,this.F=n,this.G=o,this.u=e,this.z=r,this.run=this.N;const i=[];this.F(s,2097152,i),this.H={makePkgBuff:n,responsePkgType:s,j:k(i)},this.c.set(s,this),this.u.setConnWrapperMaker(((e,t)=>{do{this.p++}while(this.q.has(2097151&this.p));const s=new N(this.H,e,2097151&this.p);this.q.set(s.id,s),this.z?.insert(s);const r={conn:s,onPkgBuff:e=>this.O(s,e),onClose:e=>this.P(s,e)};return this.onConnOpen?.(this,s,!!t),r}))}responsePkgType;context;onConnOpen;onConnClose;address(){return this.u.address()}getConn(e){return this.q.get(e)}connect(e){return this.u.connect(e)}async connectLocal(){do{this.p++}while(this.q.has(2097151&this.p));const e=new R(2097151&this.p,this);return this.q.set(e.id,e),e}async startServer(e){return this.u.startListen(e)}async stopServer(){return this.u.stopListen()}sendMessageLocal(e,t){const s=t.constructor.pkgType,r=this.c.get(s);if(r)return r?.runLocal(e,t);throw new Error("Invalid package")}sendRequestLocal(e,t){const s=t.constructor.pkgType,r=this.c.get(s);if(r)return r?.runLocal(e,t);throw new Error("Invalid package")}registerError(e,t){t.registerPkgType(e),this.$.set(e,t);const s=this.c.get(this.responsePkgType);this.c.set(e,s)}registerMessage(e,t){t.registerPkgType(e)}registerRequest(e,t){t.registerPkgType(e)}handleMessageWith(e){const t=e.pkgType;if(typeof t!=typeof this.responsePkgType)throw new Error(`Wrapper ${e.pkgName} has an incompatible pkgType ${typeof t}`);if(this.c.has(t))throw new Error(`Wrapper ${e.pkgName} is already handled by another handler`);return this.c.set(t,e),e}handleRequestWith(e){const t=e.pkgType;if(typeof t!=typeof this.responsePkgType)throw new Error(`Wrapper ${e.pkgName} has an incompatible pkgType ${typeof t}`);if(this.c.has(t))throw new Error(`Wrapper ${e.pkgName} is already handled by another handler`);return this.c.set(t,e),e}removeHandler(e){const t=e.pkgType;return this.c.get(t)===e&&(this.c.delete(t),!0)}O(e,t){this.z?.update(e);const s=this.G(t),r=this.c.get(s.pkgType);r?r.run(e,s):e.close(new Error("Invalid package"))}P(e,t){const s=e.id;this.q.delete(s),this.z?.remove(e),e.onClose(t),this.onConnClose?.(this,e,e.a.hadClosed(),t)}N(e,t){const{buff:s,pkgType:r,requestId:n,bodyOffset:o}=t;if(r===this.responsePkgType){const t=s.subarray(o);e.I(n,t)}else{const t=this.$.get(r);if(t){const r=new t(s.subarray(o),!0);e.Q(n,r)}}}H;p;F;G;u;q;c;$;z}class N extends T{constructor(e,t,s){super(),this.context=void 0,this.id=s,this.a=t,this.v=e,this.h=void 0,this.J=0}context;id;a;get localAddress(){return this.a.localAddress}get localPort(){return this.a.localPort}get remoteAddress(){return this.a.remoteAddress}get remoteFamily(){return this.a.remoteFamily}get remotePort(){return this.a.remotePort}sendMessage(e){const t=e.constructor.pkgType,s=[e.buff];return this.v.makePkgBuff(t,0,s),this.a.sendPkgBuffV(s)}sendRequest(e){return this.h=new Map,this.sendRequest=this.K,this.I=this.R,this.onClose=this.S,this.K(e)}sendErrorResponse(e,t){const s=e.constructor.pkgType,r=[e.buff];return this.v.makePkgBuff(s,t,r),this.a.sendPkgBuffV(r)}sendResponse(e,t){const s=[e.respBuff],{makePkgBuff:r,responsePkgType:n}=this.v;return r(n,t,s),this.a.sendPkgBuffV(s)}async close(e){await this.a.close(e)}I(e,t){2097152===e&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}Q(e,t){const s=this.h.get(e);if(s)return this.h.delete(e),s.g(t)}keepAlive(){this.a.sendPkgBuff(this.v.j).catch(y)}timeout(){this.close(new Error("Connection timeout"))}onClose(e){}async K(e){const t=2097151&this.J++,s=new Promise(((e,s)=>{const r={f:e,g:s};this.h.set(t,r)})),r=e.constructor.pkgType,n=[e.buff];this.v.makePkgBuff(r,t,n),this.a.sendPkgBuffV(n),e.respBuff=await s}R(e,t){const s=this.h.get(e);if(s)return this.h.delete(e),void s.f(t);2097152===e&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}S(e){const t=this.h;if(t.size>0){e||(e=new Error("Connection is closed"));for(const[s,r]of t)t.delete(s),r.g(e)}this.h=void 0}v;h;J}class R{constructor(e,t){this.context=void 0,this.id=e,this._=t}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(e){return this._.sendMessageLocal(this,e)}async sendRequest(e){return this._.sendRequestLocal(this,e)}sendErrorResponse(e,t){throw null}sendResponse(e,t){throw null}async close(e){}_}var M=t.yE,W=t.sr,q=t.si,A=t.N3,O=t.sk,L=t.wL,S=t.p6,j=t.bM,J=t.h8,$=t.Kr,I=t.Yu,U=t.Kl,F=t.kX,z=t.jG;export{M as BrowserWsNet,W as ErrorWrapper,q as EventWrapper,A as JsonError,O as JsonMessage,L as JsonRequest,S as LazyConn,j as MessageWrapper,J as RequestWrapper,$ as SimpleMessageHandler,I as TimeoutMonitor,U as TypedPkgHub,F as makeNumTypePkgBuff,z as parseNumTypePkgBuff};
|
|
1
|
+
var e={d:(t,s)=>{for(var r in s)e.o(s,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:s[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{yE:()=>v,EK:()=>l,sr:()=>n,si:()=>i,N3:()=>d,sk:()=>u,wL:()=>p,p6:()=>x,bM:()=>o,h8:()=>h,Kr:()=>k,Yu:()=>N,Kl:()=>R,mx:()=>y,kX:()=>b,jG:()=>E});class s{constructor(e,t){t?(this.k=e,this.l=void 0):(this.k=void 0,this.l=e)}get data(){return this.l||(this.l=this.decode(this.k)),this.l}set data(e){this.l=e,this.k=void 0}get buff(){return this.k||(this.k=this.encode(this.l)),this.k}k;l}class r extends s{static context;static registerPkgType(e){let t;try{t=this.pkgType}catch{return void Object.defineProperty(this,"pkgType",{value:e,writable:!1,enumerable:!1})}if(t!==e)throw new Error(`Wrapper ${this.name} is already registered with type ${t}`)}static get pkgType(){throw new Error(`Wrapper ${this.name} need to be registered`)}}class n extends r{isError(){return!0}}class o extends r{isMessage(){return!0}}class i extends r{isEvent(){return!0}}class h extends r{constructor(e,t){super(e,t)}isRequest(){return!0}get resp(){return this.r||(this.r=this.decodeResponse(this.s)),this.r}set resp(e){this.r=e,this.s=void 0}get respBuff(){return this.s||(this.s=this.encodeResponse(this.r)),this.s}set respBuff(e){this.r=void 0,this.s=e}r;s}const a=new TextEncoder,c=new TextDecoder;class d extends n{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}}class u extends o{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}}class p extends h{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}encodeResponse(e){const t=JSON.stringify(e);return a.encode(t)}decodeResponse(e){const t=c.decode(e);return JSON.parse(t)}}class l extends d{}const g=new Error("Connection is not authenticated");let f=(e,t)=>new l(t.message);const y=e=>{f=e};class k{constructor(e,t,s=!0){this.g=e,this.m=t,this.n=s}get pkgType(){return this.g.pkgType}get pkgName(){return this.g.name}async run(e,t){const{buff:s,bodyOffset:r}=t,n=s.subarray(r),o=new this.g(n,!0);if(void 0===e.context&&this.n)f(e,g);else try{await this.m(e,o)}catch(t){t.isError||f(e,t)}void 0===e.context&&await e.close()}async runLocal(e,t){if(void 0===e.context&&this.n)f(e,g);else try{return await this.m(e,t)}catch(t){t.isError||f(e,t)}}g;m;n}const w=()=>{},m=e=>{let t=0,s=0;for(;t<e.length;)s+=e[t++].length;const r=new Uint8Array(s);for(s=0;t>0;){const n=e[--t];r.set(n,s),s+=n.length}return r};class v{address(){return null}setConnWrapperMaker(e){this.makeWrapper=e}async startListen(e){return!1}async stopListen(){return!1}async connect(e){const t=new WebSocket(e);return new Promise(((e,s)=>{t.onerror=e=>{s(new Error("WebSocket error"))},t.onopen=()=>{e(this.L(t,!0))}}))}L=(e,t)=>{const s=new P(e),{conn:r,onPkgBuff:n,onClose:o}=this.makeWrapper(s,t);return e.onmessage=e=>{e.data.arrayBuffer().then((e=>{n(new Uint8Array(e))}))},e.onerror=e=>{o(new Error("WebSocket error"))},e.onclose=e=>{1e3===e.code&&o()},r};makeWrapper(e,t){throw new Error('Need to call "setConnWrapperMaker" before using')}}class P{constructor(e){this.d=e}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async sendPkgBuff(e){if(this.d)return this.d.send(e);throw new Error("Invalid socket")}async sendPkgBuffV(e){if(this.d){const t=m(e);return this.d.send(t)}throw new Error("Invalid socket")}async close(e){const{d:t}=this;t&&(this.d=void 0,e?t.close(3e3,e.message):t.readyState===WebSocket.OPEN&&t.close(1e3))}hadClosed(){return!this.d}d}const b=(e,t,s)=>{const r=[t,e],n=(e=>{let t,s=0,r=0;for(;s<e.length;)for(r++,t=e[s++];t>>>=7;)r++;return r})(r),o=new Uint8Array(n);((e,t,s=0)=>{let r,n,o=s;for(r=0;r<e.length;r++){n=e[r];let s=127&n;for(;n>>>=7;)t[o++]=128|s,s=127&n;t[o++]=s}if(o<=t.length)return o-s;throw new Error("Buffer overflow")})(r,o),s.push(o)},E=e=>{const{h:[t,s],k:r}=((e,t,s=0)=>{let r,n,o=s,i=0,h=0;const a=[];for(r=0;r<e;r++){do{n=t[o++],i|=(127&n)<<h,h+=7}while(128&n);if(!(void 0!==n&&h<=35))throw new Error("Parsing failed");a.push(i),i=0,h=0}return{h:a,k:o-s}})(2,e);return{buff:e,pkgType:s,requestId:t,bodyOffset:r}};class x{constructor(e){this.A=e}get id(){return this.b?.id??-1}get context(){return this.b?.context}set context(e){this.b&&(this.b.context=e)}localAddress;localPort;remoteAddress;remoteFamily;remotePort;async sendErrorResponse(e,t){return(this.b??await this.connect()).sendErrorResponse(e,t)}async sendResponse(e,t){return(this.b??await this.connect()).sendResponse(e,t)}async sendMessage(e){return(this.b??await this.connect()).sendMessage(e)}async sendRequest(e){return(this.b??await this.connect()).sendRequest(e)}async close(e){const{b:t}=this;if(t)return this.b=void 0,void await t.close(e);this.i&&await this.i.then((t=>t.close(e).then((()=>this.b=void 0))),(()=>this.b=void 0))}async connect(){let{i:e}=this;return e||(e=this.i=new Promise(((e,t)=>{this.A().then((s=>{s?(e(s),this.b=s,this.i=void 0):t(new Error("Connect failed"))}))}))),await e}A;b;i}class T{constructor(){this.e=this,this.f=this}getPrev(){return this.e}getNext(){return this.f}insertBefore(e){this.f=e,this.e=e.e,e.e=this,this.e.f=this}insertAfter(e){this.e=e,this.f=e.f,e.f=this,this.f.e=this}remove(){this.e.f=this.f,this.f.e=this.e}e;f}class B{constructor(){this.t=new T}get g(){return this.t.getNext()}get a(){return this.t.getPrev()}B(e){return e===this.t}C(e){e.insertAfter(this.t)}U(e){e.insertBefore(this.t)}t}class C extends T{}class N{constructor(e,t,s){if(this.D=e,this.y=t,this.w=s,this.j=0,this.x=0,this.o=[],0===e)return this.insert=w,this.update=w,void(this.remove=w);for(let e=0;e<s;e++)this.o.push(new B)}insert(e){this.x||(this.E=setInterval(this.M,this.D)),this.x++,this.o[this.j].C(e)}update(e){e.remove(),this.o[this.j].C(e)}remove(e){e.remove(),this.x--,this.x||clearInterval(this.E)}M=()=>{const{y:e,w:t,o:s}=this;if(this.j++,this.j===t&&(this.j=0),e){let r=this.j+e;r>=t&&(r-=t);const n=s[r];for(let e=n.g;!n.B(e);e=e.getNext())e.keepAlive()}const r=s[this.j];for(let e=r.g;!r.B(e);e=e.getNext())e.timeout()};D;y;w;j;x;E;o}class R{constructor(e,t){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:n,parsePkgBuff:o}=t;this.responsePkgType=s,this.context=void 0,this.onConnOpen=void 0,this.onConnClose=void 0,this.p=0,this.q=new Map,this.c=new Map,this.$=new Map,this.F=n,this.G=o,this.u=e,this.z=r,this.run=this.N;const i=[];this.F(s,2097152,i),this.H={makePkgBuff:n,responsePkgType:s,j:m(i)},this.c.set(s,this),this.u.setConnWrapperMaker(((e,t)=>{do{this.p++}while(this.q.has(2097151&this.p));const s=new M(this.H,e,2097151&this.p);this.q.set(s.id,s),this.z?.insert(s);const r={conn:s,onPkgBuff:e=>this.O(s,e),onClose:e=>this.P(s,e)};return this.onConnOpen?.(this,s,!!t),r}))}responsePkgType;context;onConnOpen;onConnClose;address(){return this.u.address()}getConn(e){return this.q.get(e)}connect(e){return this.u.connect(e)}async connectLocal(){do{this.p++}while(this.q.has(2097151&this.p));const e=new W(2097151&this.p,this);return this.q.set(e.id,e),e}async startServer(e){return this.u.startListen(e)}async stopServer(){return this.u.stopListen()}sendMessageLocal(e,t){const s=t.constructor.pkgType,r=this.c.get(s);if(r)return r?.runLocal(e,t);throw new Error("Invalid package")}sendRequestLocal(e,t){const s=t.constructor.pkgType,r=this.c.get(s);if(r)return r?.runLocal(e,t);throw new Error("Invalid package")}registerError(e,t){t.registerPkgType(e),this.$.set(e,t);const s=this.c.get(this.responsePkgType);this.c.set(e,s)}registerMessage(e,t){t.registerPkgType(e)}registerRequest(e,t){t.registerPkgType(e)}handleMessageWith(e){const t=e.pkgType;if(typeof t!=typeof this.responsePkgType)throw new Error(`Wrapper ${e.pkgName} has an incompatible pkgType ${typeof t}`);if(this.c.has(t))throw new Error(`Wrapper ${e.pkgName} is already handled by another handler`);return this.c.set(t,e),e}handleRequestWith(e){const t=e.pkgType;if(typeof t!=typeof this.responsePkgType)throw new Error(`Wrapper ${e.pkgName} has an incompatible pkgType ${typeof t}`);if(this.c.has(t))throw new Error(`Wrapper ${e.pkgName} is already handled by another handler`);return this.c.set(t,e),e}removeHandler(e){const t=e.pkgType;return this.c.get(t)===e&&(this.c.delete(t),!0)}O(e,t){this.z?.update(e);const s=this.G(t),r=this.c.get(s.pkgType);r?r.run(e,s):e.close(new Error("Invalid package"))}P(e,t){const s=e.id;this.q.delete(s),this.z?.remove(e),e.onClose(t),this.onConnClose?.(this,e,e.a.hadClosed(),t)}N(e,t){const{buff:s,pkgType:r,requestId:n,bodyOffset:o}=t;if(r===this.responsePkgType){const t=s.subarray(o);e.I(n,t)}else{const t=this.$.get(r);if(t){const r=new t(s.subarray(o),!0);e.Q(n,r)}}}H;p;F;G;u;q;c;$;z}class M extends C{constructor(e,t,s){super(),this.context=void 0,this.id=s,this.a=t,this.v=e,this.h=void 0,this.J=0}context;id;a;get localAddress(){return this.a.localAddress}get localPort(){return this.a.localPort}get remoteAddress(){return this.a.remoteAddress}get remoteFamily(){return this.a.remoteFamily}get remotePort(){return this.a.remotePort}sendMessage(e){const t=e.constructor.pkgType,s=[e.buff];return this.v.makePkgBuff(t,0,s),this.a.sendPkgBuffV(s)}sendRequest(e){return this.h=new Map,this.sendRequest=this.K,this.I=this.R,this.onClose=this.S,this.K(e)}sendErrorResponse(e,t){const s=e.constructor.pkgType,r=[e.buff];return this.v.makePkgBuff(s,t,r),this.a.sendPkgBuffV(r)}sendResponse(e,t){const s=[e.respBuff],{makePkgBuff:r,responsePkgType:n}=this.v;return r(n,t,s),this.a.sendPkgBuffV(s)}async close(e){await this.a.close(e)}I(e,t){2097152===e&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}Q(e,t){const s=this.h.get(e);if(s)return this.h.delete(e),s.g(t)}keepAlive(){this.a.sendPkgBuff(this.v.j).catch(w)}timeout(){this.close(new Error("Connection timeout"))}onClose(e){}async K(e){const t=2097151&this.J++,s=new Promise(((e,s)=>{const r={f:e,g:s};this.h.set(t,r)})),r=e.constructor.pkgType,n=[e.buff];this.v.makePkgBuff(r,t,n),this.a.sendPkgBuffV(n),e.respBuff=await s}R(e,t){const s=this.h.get(e);if(s)return this.h.delete(e),void s.f(t);2097152===e&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}S(e){const t=this.h;if(t.size>0){e||(e=new Error("Connection is closed"));for(const[s,r]of t)t.delete(s),r.g(e)}this.h=void 0}v;h;J}class W{constructor(e,t){this.context=void 0,this.id=e,this._=t}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(e){return this._.sendMessageLocal(this,e)}async sendRequest(e){return this._.sendRequestLocal(this,e)}sendErrorResponse(e,t){throw null}sendResponse(e,t){throw null}async close(e){}_}var q=t.yE,A=t.EK,O=t.sr,L=t.si,S=t.N3,j=t.sk,J=t.wL,$=t.p6,I=t.bM,K=t.h8,F=t.Kr,z=t.Yu,H=t.Kl,G=t.mx,U=t.kX,V=t.jG;export{q as BrowserWsNet,A as CommonError,O as ErrorWrapper,L as EventWrapper,S as JsonError,j as JsonMessage,J as JsonRequest,$ as LazyConn,I as MessageWrapper,K as RequestWrapper,F as SimpleMessageHandler,z as TimeoutMonitor,H as TypedPkgHub,G as handleUncaughtErrorWith,U as makeNumTypePkgBuff,V as parseNumTypePkgBuff};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c, a } from './common';
|
|
2
2
|
export class SimpleMessageHandler {
|
|
3
3
|
constructor(wrapperCtor, d, e = true) {
|
|
4
4
|
this.g = wrapperCtor;
|
|
@@ -21,12 +21,12 @@ export class SimpleMessageHandler {
|
|
|
21
21
|
}
|
|
22
22
|
catch (err) {
|
|
23
23
|
if (!err.isError) {
|
|
24
|
-
|
|
24
|
+
a(conn, err);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
|
-
|
|
29
|
+
a(conn, c);
|
|
30
30
|
}
|
|
31
31
|
if (conn.context === undefined) {
|
|
32
32
|
await conn.close();
|
|
@@ -39,17 +39,14 @@ export class SimpleMessageHandler {
|
|
|
39
39
|
}
|
|
40
40
|
catch (err) {
|
|
41
41
|
if (!err.isError) {
|
|
42
|
-
|
|
42
|
+
a(conn, err);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
else {
|
|
47
|
-
|
|
47
|
+
a(conn, c);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
wrapUncaughtError(conn, err) {
|
|
51
|
-
return c(conn, err);
|
|
52
|
-
}
|
|
53
50
|
g;
|
|
54
51
|
m;
|
|
55
52
|
n;
|
|
@@ -80,12 +77,12 @@ export class h {
|
|
|
80
77
|
error = err;
|
|
81
78
|
}
|
|
82
79
|
else {
|
|
83
|
-
error =
|
|
80
|
+
error = a(conn, err);
|
|
84
81
|
}
|
|
85
82
|
}
|
|
86
83
|
}
|
|
87
84
|
else {
|
|
88
|
-
error =
|
|
85
|
+
error = a(conn, c);
|
|
89
86
|
}
|
|
90
87
|
try {
|
|
91
88
|
if (!error) {
|
|
@@ -112,14 +109,11 @@ export class h {
|
|
|
112
109
|
throw err;
|
|
113
110
|
}
|
|
114
111
|
else {
|
|
115
|
-
throw
|
|
112
|
+
throw a(conn, err);
|
|
116
113
|
}
|
|
117
114
|
}
|
|
118
115
|
}
|
|
119
|
-
throw
|
|
120
|
-
}
|
|
121
|
-
wrapUncaughtError(conn, err) {
|
|
122
|
-
return c(conn, err);
|
|
116
|
+
throw a(conn, c);
|
|
123
117
|
}
|
|
124
118
|
g;
|
|
125
119
|
m;
|
package/lib/handler/common.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { JsonError } from '../net/JsonPackageWrapper';
|
|
2
|
-
export class
|
|
2
|
+
export class CommonError extends JsonError {
|
|
3
3
|
}
|
|
4
|
-
export const
|
|
5
|
-
let
|
|
6
|
-
return new
|
|
4
|
+
export const c = new Error('Connection is not authenticated');
|
|
5
|
+
export let a = (b, err) => {
|
|
6
|
+
return new CommonError(err.message);
|
|
7
7
|
};
|
|
8
|
-
export const
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
export const h = (handler) => {
|
|
12
|
-
e = handler;
|
|
8
|
+
export const handleUncaughtErrorWith = (handler) => {
|
|
9
|
+
a = handler;
|
|
13
10
|
};
|
|
14
11
|
//# sourceMappingURL=common.js.map
|
package/lib/index.d.ts
CHANGED
|
@@ -104,7 +104,6 @@ export interface IPkgHandler<PkgTypeT extends number | string, ConnContextT = vo
|
|
|
104
104
|
get pkgType(): PkgTypeT;
|
|
105
105
|
get pkgName(): string;
|
|
106
106
|
run(conn: IConn<ConnContextT>, parseResult: ParseResult<PkgTypeT>): void | Promise<void>;
|
|
107
|
-
wrapUncaughtError(conn: IConn<unknown>, err: Error): ErrorWrapper<unknown>;
|
|
108
107
|
}
|
|
109
108
|
export interface IMessageHandler<PkgTypeT extends number | string, MessageT, WrapperT extends MessageWrapper<MessageT>, ConnContextT = void> extends IPkgHandler<PkgTypeT, ConnContextT> {
|
|
110
109
|
runLocal(conn: IConn<ConnContextT>, message: WrapperT): void | Promise<void>;
|
|
@@ -112,6 +111,29 @@ export interface IMessageHandler<PkgTypeT extends number | string, MessageT, Wra
|
|
|
112
111
|
export interface IRequestHandler<PkgTypeT extends number | string, RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT>, ConnContextT = void> extends IPkgHandler<PkgTypeT, ConnContextT> {
|
|
113
112
|
runLocal(conn: IConn<ConnContextT>, request: WrapperT): void | Promise<void>;
|
|
114
113
|
}
|
|
114
|
+
export declare abstract class JsonError<ErrorT> extends ErrorWrapper<ErrorT> {
|
|
115
|
+
protected encode(data: ErrorT): Uint8Array;
|
|
116
|
+
protected decode(buffer: Uint8Array): ErrorT;
|
|
117
|
+
}
|
|
118
|
+
export declare abstract class JsonMessage<MessageT> extends MessageWrapper<MessageT> {
|
|
119
|
+
protected encode(data: MessageT): Uint8Array;
|
|
120
|
+
protected decode(buffer: Uint8Array): MessageT;
|
|
121
|
+
}
|
|
122
|
+
export declare abstract class JsonRequest<RequestT, ResponseT> extends RequestWrapper<RequestT, ResponseT> {
|
|
123
|
+
protected encode(data: RequestT): Uint8Array;
|
|
124
|
+
protected decode(buffer: Uint8Array): RequestT;
|
|
125
|
+
protected encodeResponse(response: ResponseT): Uint8Array;
|
|
126
|
+
protected decodeResponse(buffer: Uint8Array): ResponseT;
|
|
127
|
+
}
|
|
128
|
+
export declare class CommonError extends JsonError<string> {
|
|
129
|
+
}
|
|
130
|
+
export interface DoneResp<ResultT extends string | number | boolean = boolean> {
|
|
131
|
+
result: ResultT;
|
|
132
|
+
}
|
|
133
|
+
export interface ClientPkg {
|
|
134
|
+
cid: string;
|
|
135
|
+
}
|
|
136
|
+
export declare const handleUncaughtErrorWith: (handler: (conn: IConn<unknown>, err: Error) => ErrorWrapper<unknown>) => void;
|
|
115
137
|
export type HandleFunc<WrapperT, ConnContextT> = (conn: IConn<ConnContextT>, wrapper: WrapperT) => void | Promise<void>;
|
|
116
138
|
export declare class SimpleMessageHandler<PkgTypeT extends number | string, MessageT, WrapperT extends MessageWrapper<MessageT>, ConnContextT> implements IMessageHandler<PkgTypeT, MessageT, WrapperT, ConnContextT> {
|
|
117
139
|
constructor(wrapperCtor: MessageWrapperCtor<MessageT, WrapperT>, handleFunc: HandleFunc<WrapperT, ConnContextT>, checkContext?: boolean);
|
|
@@ -119,7 +141,6 @@ export declare class SimpleMessageHandler<PkgTypeT extends number | string, Mess
|
|
|
119
141
|
get pkgName(): string;
|
|
120
142
|
run(conn: IConn<ConnContextT>, parseResult: ParseResult<PkgTypeT>): Promise<void>;
|
|
121
143
|
runLocal(conn: IConn<ConnContextT>, message: WrapperT): Promise<void>;
|
|
122
|
-
wrapUncaughtError(conn: IConn<unknown>, err: Error): ErrorWrapper<unknown>;
|
|
123
144
|
private _wrapperCtor;
|
|
124
145
|
private _handleFunc;
|
|
125
146
|
private _checkContext;
|
|
@@ -135,20 +156,6 @@ export declare class BrowserWsNet<ConnWrapperT> implements INet<ConnWrapperT> {
|
|
|
135
156
|
}
|
|
136
157
|
export declare const makeNumTypePkgBuff: (pkgType: number, requestId: number, encoded: Uint8Array[]) => void;
|
|
137
158
|
export declare const parseNumTypePkgBuff: (buff: Uint8Array) => ParseResult<number>;
|
|
138
|
-
export declare abstract class JsonError<ErrorT> extends ErrorWrapper<ErrorT> {
|
|
139
|
-
protected encode(data: ErrorT): Uint8Array;
|
|
140
|
-
protected decode(buffer: Uint8Array): ErrorT;
|
|
141
|
-
}
|
|
142
|
-
export declare abstract class JsonMessage<MessageT> extends MessageWrapper<MessageT> {
|
|
143
|
-
protected encode(data: MessageT): Uint8Array;
|
|
144
|
-
protected decode(buffer: Uint8Array): MessageT;
|
|
145
|
-
}
|
|
146
|
-
export declare abstract class JsonRequest<RequestT, ResponseT> extends RequestWrapper<RequestT, ResponseT> {
|
|
147
|
-
protected encode(data: RequestT): Uint8Array;
|
|
148
|
-
protected decode(buffer: Uint8Array): RequestT;
|
|
149
|
-
protected encodeResponse(response: ResponseT): Uint8Array;
|
|
150
|
-
protected decodeResponse(buffer: Uint8Array): ResponseT;
|
|
151
|
-
}
|
|
152
159
|
export declare class LazyConn<ConnContextT> implements IConn<ConnContextT> {
|
|
153
160
|
constructor(connectFunc: () => Promise<IConn<ConnContextT> | undefined>);
|
|
154
161
|
get id(): number;
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { CommonError, handleUncaughtErrorWith } from './handler/common';
|
|
1
2
|
export { SimpleMessageHandler } from './handler/SimpleHandler';
|
|
2
3
|
export { BrowserWsNet } from './net/BrowserWsNet';
|
|
3
4
|
export { makeNumTypePkgBuff, parseNumTypePkgBuff } from './net/hubPkgSerializer';
|
|
@@ -8,7 +8,7 @@ export const makeNumTypePkgBuff = (pkgType, requestId, encoded) => {
|
|
|
8
8
|
};
|
|
9
9
|
export const parseNumTypePkgBuff = (buff) => {
|
|
10
10
|
const { h: [requestId, pkgType], k: bodyOffset } = l(2, buff);
|
|
11
|
-
const
|
|
12
|
-
return
|
|
11
|
+
const result = { buff, pkgType, requestId, bodyOffset };
|
|
12
|
+
return result;
|
|
13
13
|
};
|
|
14
14
|
//# sourceMappingURL=hubPkgSerializer.js.map
|