justrun-ws 0.3.3 → 0.3.5

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/index.d.ts CHANGED
@@ -18,8 +18,8 @@ declare abstract class DataWrapper<DataT> implements IWrapper<DataT> {
18
18
  get buff(): Uint8Array;
19
19
  protected abstract encode(data: DataT): Uint8Array;
20
20
  protected abstract decode(buffer: Uint8Array): DataT;
21
+ private z?;
21
22
  private $?;
22
- private _?;
23
23
  }
24
24
  declare abstract class PkgWrapper<DataT> extends DataWrapper<DataT> {
25
25
  static context: unknown;
@@ -44,8 +44,8 @@ export declare abstract class RequestWrapper<RequestT, ResponseT> extends PkgWra
44
44
  set respBuff(val: Uint8Array);
45
45
  protected abstract encodeResponse(response: ResponseT): Uint8Array;
46
46
  protected abstract decodeResponse(buffer: Uint8Array): ResponseT;
47
+ private _?;
47
48
  private A?;
48
- private B?;
49
49
  }
50
50
  export type ErrorWrapperCtor<ErrorT, WrapperT extends ErrorWrapper<ErrorT> = ErrorWrapper<ErrorT>> = new (error: ErrorT | Uint8Array, needParse?: true) => WrapperT;
51
51
  export type MessageWrapperCtor<MessageT, WrapperT extends MessageWrapper<MessageT> = MessageWrapper<MessageT>> = new (message: MessageT | Uint8Array, needParse?: true) => WrapperT;
@@ -85,13 +85,13 @@ export declare class TimeoutMonitor {
85
85
  insert(item: MonitorItem): void;
86
86
  update(item: MonitorItem): void;
87
87
  remove(item: MonitorItem): void;
88
- private ac;
89
- private a0;
90
- private M;
91
- private I;
88
+ private ab;
89
+ private Z;
90
+ private L;
91
+ private H;
92
92
  private p;
93
- private J;
94
- private a1?;
93
+ private I;
94
+ private a0?;
95
95
  private t;
96
96
  }
97
97
  export type TypedPkgHubOpts<PkgTypeT extends number | string> = {
@@ -164,13 +164,13 @@ export declare class WsClient<PkgTypeT extends number | string> {
164
164
  sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
165
165
  onConnectionStatus(listener: (isOpen: boolean, conn: IConn<string>, active: boolean, err?: Error) => void): void;
166
166
  private g;
167
- private O;
168
- private K;
169
- private ab;
167
+ private N;
168
+ private J;
169
+ private aa;
170
170
  private m;
171
171
  private a;
172
- private L;
173
- private Q?;
172
+ private K;
173
+ private P?;
174
174
  private b?;
175
175
  }
176
176
  export declare class JustrunAuthProvider implements IAuthProvider {
@@ -181,20 +181,19 @@ export declare class JustrunAuthProvider implements IAuthProvider {
181
181
  authWithOAuth2(returnTo: string, oauthUrl: string, clientId: string, scope: string): void;
182
182
  resetAuthState(): Promise<void>;
183
183
  resetConnState(forceKick: boolean): void;
184
+ private an;
185
+ private R;
184
186
  private ao;
187
+ private W;
185
188
  private S;
186
- private ap;
187
- private X;
188
- private T;
189
189
  private c;
190
190
  private d;
191
- private W;
192
- private R;
193
- private z;
191
+ private V;
192
+ private Q;
194
193
  private a?;
195
194
  private h?;
196
- private G?;
197
- private F;
195
+ private F?;
196
+ private E;
198
197
  private f;
199
198
  }
200
199
 
package/lib/index.min.mjs CHANGED
@@ -1 +1 @@
1
- class t{constructor(t,e){e?this.$=t:this._=t}get data(){return this._||(this._=this.decode(this.$)),this._}set data(t){this._=t,this.$=void 0}get buff(){return this.$||(this.$=this.encode(this._)),this.$}$;_}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 i extends e{isMessage(){return!0}}class r extends e{isEvent(){return!0}}class n extends e{constructor(t,e){super(t,e)}isRequest(){return!0}get resp(){return this.A||(this.A=this.decodeResponse(this.B)),this.A}set resp(t){this.A=t,this.B=void 0}get respBuff(){return this.B||(this.B=this.encodeResponse(this.A)),this.B}set respBuff(t){this.A=void 0,this.B=t}A;B}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 i{encode(t){const e=JSON.stringify(t);return o.encode(e)}decode(t){const e=h.decode(t);return JSON.parse(e)}}class u 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 d extends a{}let l=(t,e)=>new d(e.message);const g=t=>{l=t};var p;!function(t){t.AuthTimeout="AuthTimeout",t.BadRequest="BadRequest",t.ConnectFailed="ConnectFailed",t.ConnectionClosed="ConnectionClosed",t.InvalidConnString="InvalidConnString",t.InvalidConnection="InvalidConnection",t.InvalidPackage="InvalidPackage",t.NotAuthenticated="NotAuthenticated",t.NotFound="NotFound",t.Timeout="Timeout",t.UnknownError="UnknownError"}(p||(p={}));class f{constructor(){this.j=this,this.k=this}getPrev(){return this.j}getNext(){return this.k}insertBefore(t){this.k=t,this.j=t.j,t.j=this,this.j.k=this}insertAfter(t){this.j=t,this.k=t.k,t.k=this,this.k.j=this}remove(){this.j.k=this.k,this.k.j=this.j}j;k}class w{constructor(){this.C=new f}get g(){return this.C.getNext()}get a(){return this.C.getPrev()}Y(t){return t===this.C}Z(t){t.insertAfter(this.C)}aq(t){t.insertBefore(this.C)}C}const y=()=>{},k=t=>{let e=0,s=0;for(;e<t.length;)s+=t[e++].length;const i=new Uint8Array(s);for(s=0;e>0;){const r=t[--e];i.set(r,s),s+=r.length}return i};class v extends f{}class m{constructor(t,e,s){if(this.a0=t,this.M=e,this.I=s,this.p=0,this.J=0,this.t=[],0===t)return this.insert=y,this.update=y,void(this.remove=y);for(let t=0;t<s;t++)this.t.push(new w)}insert(t){this.J||(this.a1=setInterval(this.ac,this.a0)),this.J++,this.t[this.p].Z(t)}update(t){t.remove(),this.t[this.p].Z(t)}remove(t){t.remove(),this.J--,this.J||clearInterval(this.a1)}ac=()=>{const{M:t,I:e,t:s}=this;if(this.p++,this.p===e&&(this.p=0),t){let i=this.p+t;i>=e&&(i-=e);const r=s[i];for(let t=r.g;!r.Y(t);t=t.getNext())t.warnTimeout()}const i=s[this.p];for(let t=i.g;!i.Y(t);t=t.getNext())t.onTimeout()};a0;M;I;p;J;a1;t}class b extends v{constructor(t,e,s){super(),this.b=t,this.a8=e,this.val=s}val;warnTimeout(){}onTimeout(){this.b.delete(this.a8)}b;a8}class T{constructor(t,e){this.b=new Map,this.q=new m(t,0,e)}get(t){const{b:e,q:s}=this,i=e.get(t);if(i)return s.update(i),i.val}set(t,e){const{b:s,q:i}=this;let r=s.get(t);r?(r.val=e,i.update(r)):(r=new b(s,t,e),s.set(t,r),i.insert(r))}delete(t){const{b:e,q:s}=this,i=e.get(t);return!!i&&(s.remove(i),e.delete(t))}b;q}class E{constructor(t,e){this.a9=new Promise(t=>{this.w=t}),this.P=e,t>0?this.l=setTimeout(()=>{this.l=void 0,this.w(!0),e?.()},t):(this.w(!0),e?.())}at(t){const{l:e,w:s,P:i}=this;e&&clearTimeout(e),t>0?this.l=setTimeout(()=>{this.l=void 0,i?.(),s(!0)},t):(this.l=void 0,s(!0),i?.())}al(){this.l&&(clearTimeout(this.l),this.l=void 0,this.w(!1))}au(){return this.a9}w;a9;l;P}class C extends c{}class P{constructor(t,e,s){let i=sessionStorage.getItem("jrSession");i||(i=crypto.randomUUID(),sessionStorage.setItem("jrSession",i)),this.c="",this.d=i,this.W=t,this.R="",this.z=!1,this.F=e,this.f=s,this.S=this.S.bind(this);const r=()=>{throw new Error("JustrunAuthProvider shall be enabled before use")};this.authWithCredential=r,this.authWithOAuth2=r,this.resetAuthState=r,this.resetConnState=r}async init(t,e){const s=P.prototype;this.authWithCredential=s.authWithCredential,this.authWithOAuth2=s.authWithOAuth2,this.resetAuthState=s.resetAuthState,this.resetConnState=s.resetConnState,e.onConnectionStatus(this.ao.bind(this)),e.registerMessage(t,C),e.handleMessageWith(C,this.ap.bind(this));const{d:i,f:r}=this,n=this.X();if(n){const{g:t,j:e}=n;if(Math.round(Date.now()/1e3)<=e)return void r(t,i)}r("","")}async preConnect(){const{c:t,d:e,h:s,f:i,F:r}=this;if(s)return i(t,e,s),"";const n=""===s;this.h=void 0;let o=this.X();if(o){const{g:t,b:s,i,j:h}=o,a=Math.round(Date.now()/1e3);if(a<=i)return this.R=o.b,r(s,e,n);if(a<=h&&(o=await this.T("refresh",""),o))return this.R=o.b,r(o.b,e,n)}return this.h="unknown error",i("",""),""}async authWithCredential(t,e,s,i){if(!this.z){this.z=!0;try{await this.T(t,`${e},${s},${i}`)}finally{this.z=!1}}}authWithOAuth2(t,e,s,i){const r=`${e}?client_id=${s}&redirect_uri=${this.W}&scope=${i}&response_type=code`;window.location.href=`${r}&state=${this.d}~${s}~${encodeURI(t)}`}async resetAuthState(){if(!this.z){this.z=!0;try{await this.T("signout","")}finally{this.z=!1}}}resetConnState(t){if(this.h){this.h=t?"":void 0;const{c:e,d:s,f:i}=this;i(e,s)}}ao(t,e,s,i){if(t){this.a=e;const t=this.X();if(t){const{i:e}=t;this.G=new E(1e3*e-Date.now(),this.S)}}else if(this.a=void 0,this.G&&(this.G.al(),this.G=void 0),!s){const t=this.h=i?.message||"unknown error",{c:e,d:s,f:r}=this;r(e,s,t)}}async S(){this.G=void 0;const{d:t,F:e}=this,s=await this.T("refresh","");if(s&&this.a){const{b:i,i:r}=s;if(i===this.R){const s=e(i,t),[n,o]=s.split("://");await fetch(`${n.replace("ws","http")}://${o}`),this.G=new E(1e3*r-Date.now(),this.S)}else this.a&&(this.a.close(),this.a=void 0)}}async ap(t){const{c:e,d:s,a:i}=this,{type:r,n}=t;"kick"===r&&i&&(i.close(),this.a=void 0,n&&(this.h=n,this.f(e,s,n)))}X(){const t=localStorage.getItem("jrAuth")||void 0;if(t){const[e,s,i,r]=t.split(","),{c:n,a:o}=this;return n&&e!==n&&o&&(o.close(),this.a=void 0),this.c=e,{g:e,b:s,i:parseInt(i),j:parseInt(r)}}}async T(t,e){const s=await fetch(this.W,{method:"POST",body:`${t}:${e}`}).catch(t=>{throw new d(t.message)}),i=await s.text();if(!s.ok)throw new d(i);if(i.startsWith("auth:")){const t=i.substring(5);localStorage.setItem("jrAuth",t);const[e,s,r,n]=t.split(","),{c:o,a:h}=this;return o!==e&&(o&&h&&(h.close(),this.a=void 0),this.c=e,this.f(e,this.d,this.h)),{g:e,b:s,i:parseInt(r),j:parseInt(n)}}if(i.startsWith("clear:"))localStorage.removeItem("jrAuth"),this.c="",this.h=void 0,this.f("",""),this.a&&(this.a.close(),this.a=void 0);else if(i.startsWith("error:")){"refresh"!==t&&"signout"!==t||(localStorage.removeItem("jrAuth"),this.c="",this.h=void 0,this.f("",""),this.a&&(this.a.close(),this.a=void 0));const e=i.substring(6);throw new d(e)}}c;d;W;R;z;a;h;G;F;f}const x=new Error(p.NotAuthenticated);class S{constructor(t,e,s){this.o=t,this.x=e,this.y=!s}get pkgType(){return this.o.pkgType}get k(){return this.o.name}async w(t,e){const{buff:s,bodyOffset:i}=e,r=s.subarray(i),n=new this.o(r,!0);if(void 0===t.context&&this.y)l(t,x);else try{await this.x(t,n)}catch(e){e.isError||l(t,e)}void 0===t.context&&await t.close()}async p(t,e){if(void 0===t.context&&this.y)l(t,x);else try{return await this.x(t,e)}catch(e){e.isError||l(t,e)}}o;x;y}class A{constructor(t,e,s){this.o=t,this.x=e,this.y=!s}get pkgType(){return this.o.pkgType}get k(){return this.o.name}async w(t,e){const{buff:s,requestId:i,bodyOffset:r}=e,n=s.subarray(r),o=new this.o(n,!0);let h;if(void 0===t.context&&this.y)h=l(t,x);else try{await this.x(t,o)}catch(e){h=e.isError?e:l(t,e)}try{h?await t.sendErrorResponse(h,i):await t.sendResponse(o,i),void 0===t.context&&await t.close()}catch{return}}async p(t,e){if(void 0!==t.context||!this.y)try{return await this.x(t,e)}catch(e){throw e.isError?e:l(t,e)}throw l(t,x)}o;x;y}class R{b(){return null}init(t){this.H=t}async F(t){return!1}async G(){return!1}async g(t){const e=new WebSocket(t);return new Promise((t,s)=>{e.onerror=t=>{s(new Error(p.UnknownError))},e.onopen=()=>{t(this.am(e,!0))}})}am=(t,e)=>{const s=new $(t),{conn:i,$:r,s:n}=this.H(s,e);t.onmessage=t=>{t.data.arrayBuffer().then(t=>{r(new Uint8Array(t))})};let o=!0;return t.onerror=t=>{o=!1,n(new Error(p.UnknownError))},t.onclose=t=>{o&&n(1e3===t.code?void 0:new Error(t.reason||"unknown error"))},i};H(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class ${constructor(t){this.i=t}get socket(){return{localAddress:"",localPort:0,remoteAddress:"",remoteFamily:"",remotePort:0}}async B(t){if(this.i)return this.i.send(t);throw new Error(p.InvalidConnection)}async m(t){if(this.i){const e=k(t);return this.i.send(e)}throw new Error(p.InvalidConnection)}async close(t){const{i:e}=this;e&&(this.i=void 0,t?e.close(1006,t.message):e.readyState<=WebSocket.OPEN&&e.close(1e3))}C(){return!this.i}i}const I=(t,e,s)=>{const i=[e,t],r=(t=>{let e,s=0,i=0;for(;s<t.length;)for(i++,e=t[s++];e>>>=7;)i++;return i})(i),n=new Uint8Array(r);((t,e,s=0)=>{let i,r,n=s;for(i=0;i<t.length;i++){r=t[i];let s=127&r;for(;r>>>=7;)e[n++]=128|s,s=127&r;e[n++]=s}if(n<=e.length)return n-s;throw new Error("Buffer overflow")})(i,n),s.push(n)},W=t=>{const{g:[e,s],j:i}=((t,e,s=0)=>{let i,r,n=s,o=0,h=0;const a=[];for(i=0;i<t;i++){do{r=e[n++],o|=(127&r)<<h,h+=7}while(128&r);if(!(void 0!==r&&h<=35))throw new Error("Parsing failed");a.push(o),o=0,h=0}return{g:a,j:n-s}})(2,t);return{buff:t,pkgType:s,requestId:e,bodyOffset:i}};class j{constructor(t){this.aa=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.g()).sendErrorResponse(t,e)}async sendResponse(t,e){return(this.a??await this.g()).sendResponse(t,e)}async sendMessage(t){return(this.a??await this.g()).sendMessage(t)}async sendRequest(t){return(this.a??await this.g()).sendRequest(t)}async close(t){const{a:e}=this;if(e)return this.a=void 0,void await e.close(t);this.r&&await this.r.then(e=>{this.a=void 0,e.close(t)},()=>this.a=void 0)}async av(){this.a||await this.g()}an(){this.a=void 0}async g(){let{r:t}=this;return t||(t=this.r=new Promise((t,e)=>{this.aa().then(s=>{s?(t(s),this.a=s,this.r=void 0):e(new Error("Connect failed"))})})),await t}aa;a;r}class B{constructor(t,e){const{responsePkgType:s,timeoutMonitor:i,makePkgBuff:r,parsePkgBuff:n}=e;this.responsePkgType=s,this.u=0,this.v=new Map,this.h=new Map,this.U=new Map,this.a2=r,this.a3=n,this.D=t,this.N=i,this.w=this.ad;const o=[];this.a2(s,2097152,o),this.a4={makePkgBuff:r,responsePkgType:s,q:k(o)},this.h.set(s,this),this.D.init((t,e)=>{do{this.u++}while(this.v.has(2097151&this.u));const s=new M(this.a4,t,2097151&this.u);this.v.set(s.id,s),this.N?.insert(s);const i={conn:s,$:t=>this.ae(s,t),s:t=>this.af(s,t)};return this.O?.(this,s,!!e),i})}responsePkgType;context;O;K;b(){return this.D.b()}P(t){return this.v.get(t)}g(t){return this.D.g(t)}async Q(){do{this.u++}while(this.v.has(2097151&this.u));const t=new N(2097151&this.u,this);return this.v.set(t.id,t),t}async R(t){return this.D.F(t)}async S(){return this.D.G()}ag(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.p(t,e);throw new Error(p.InvalidPackage)}ah(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.p(t,e);throw new Error(p.InvalidPackage)}registerError(t,e){e.registerPkgType(t),this.U.set(t,e);const s=this.h.get(this.responsePkgType);this.h.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.k} has an incompatible pkgType ${typeof e}`);if(this.h.has(e))throw new Error(`Wrapper ${t.k} is already handled by another handler`);return this.h.set(e,t),t}handleRequestWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.k} has an incompatible pkgType ${typeof e}`);if(this.h.has(e))throw new Error(`Wrapper ${t.k} is already handled by another handler`);return this.h.set(e,t),t}ar(t){const e=t.pkgType;return this.h.get(e)===t&&(this.h.delete(e),!0)}ae(t,e){this.N?.update(t);const s=this.a3(e),i=this.h.get(s.pkgType);i?i.w(t,s):t.close(new Error(p.InvalidPackage))}af(t,e){this.v.delete(t.id),this.N?.remove(t),t.s(e),this.K?.(this,t,t.e.C(),e)}ad(t,e){try{const{buff:s,pkgType:i,requestId:r,bodyOffset:n}=e;if(i===this.responsePkgType){const e=s.subarray(n);t.a5(r,e)}else{const e=this.U.get(i);if(e){const i=new e(s.subarray(n),!0);t.ai(r,i)}}}catch{t.close(new Error(p.InvalidPackage))}}a4;u;a2;a3;D;v;h;U;N}class M extends v{constructor(t,e,s){super(),this.id=s,this.e=e,this.E=t,this.a6=0}context;id;e;get socket(){return this.e.socket}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.E.makePkgBuff(e,0,s),this.e.m(s)}sendRequest(t){return this.n=new Map,this.sendRequest=this.a7,this.a5=this.aj,this.s=this.ak,this.a7(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,i=[t.buff];return this.E.makePkgBuff(s,e,i),this.e.m(i)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:i,responsePkgType:r}=this.E;return i(r,e,s),this.e.m(s)}async close(t){await this.e.close(t)}a5(t,e){2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}ai(t,e){const s=this.n.get(t);if(s)return this.n.delete(t),s.j(e)}warnTimeout(){this.e.B(this.E.q).catch(y)}onTimeout(){this.close(new Error(p.Timeout))}s(t){}async a7(t){const e=2097151&this.a6++,s=t.constructor.pkgType,i=[t.buff];this.E.makePkgBuff(s,e,i);const r=new Promise((t,s)=>{const r={d:t,j:s};this.n.set(e,r),this.e.m(i).catch(t=>{this.n.delete(e),s(t)})});t.respBuff=await r}aj(t,e){const s=this.n.get(t);if(s)return this.n.delete(t),void s.d(e);2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}ak(t){const e=this.n;if(e.size>0){t||(t=new Error(p.ConnectionClosed));for(const[s,i]of e)e.delete(s),i.j(t)}this.n=void 0}E;n;a6}class N{constructor(t,e){this.id=t,this.V=e}context;id;socket;get remoteFamily(){return"Local"}async sendMessage(t){return this.V.ag(this,t)}async sendRequest(t){return this.V.ah(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}V}const q=(t,e)=>new U(t,{responsePkgType:0,makePkgBuff:I,parsePkgBuff:W,timeoutMonitor:e});var O;!function(t){t[t.NotConnected=0]="NotConnected",t[t.Connecting=1]="Connecting",t[t.Connected=2]="Connected"}(O||(O={}));class U{constructor(t,e){const s=new R,i=this.m=new B(s,e);this.L=O.NotConnected,this.ab=t,this.a=new j(()=>this.g()),i.O=this.O.bind(this),i.K=this.K.bind(this)}getConnState(){return this.L}enableAuthenticate(t,e){this.Q=e,e.init(t,this)}registerError(t,e){return this.m.registerError(t,e)}registerMessage(t,e){return this.m.registerMessage(t,e)}registerRequest(t,e){return this.m.registerRequest(t,e)}handleMessageWith(t,e){this.m.handleMessageWith(new S(t,(t,s)=>e(s.data),!0))}handleRequestWith(t,e){this.m.handleRequestWith(new A(t,async(t,s)=>{s.resp=await e(s.data)},!0))}sendMessage(t){return this.a.sendMessage(t)}async sendRequest(t){return await this.a.sendRequest(t),t.resp}onConnectionStatus(t){let{b:e}=this;e||(this.b=e=[]),e.push(t)}async g(){const{m:t,Q:e}=this;this.L=O.Connecting;try{const s=e?await e.preConnect():this.ab;if(!s)throw new Error(p.UnknownError);return await t.g(s)}catch(e){const s=e instanceof Error?e:new Error(p.UnknownError);this.K(t,this.a,!0,s)}}O(t,e,s){if(this.L=O.Connected,e.context="",this.b)for(const t of this.b)t(!0,e,s)}K(t,e,s,i){if(this.L=O.NotConnected,this.a.an(),this.b)for(const t of this.b)t(!1,e,s,i)}ab;m;a;L;Q;b}export{d as CommonError,s as ErrorWrapper,r as EventWrapper,a as JsonError,c as JsonMessage,u as JsonRequest,P as JustrunAuthProvider,i as MessageWrapper,p as NetErrorString,n as RequestWrapper,T as TimeoutCache,m as TimeoutMonitor,U as WsClient,q as createNumPkgTypeClient,g as handleUncaughtErrorWith};
1
+ class t{constructor(t,e){e?this.z=t:this.$=t}get data(){return this.$||(this.$=this.decode(this.z)),this.$}set data(t){this.$=t,this.z=void 0}get buff(){return this.z||(this.z=this.encode(this.$)),this.z}z;$}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 i extends e{isMessage(){return!0}}class r extends e{isEvent(){return!0}}class n extends e{constructor(t,e){super(t,e)}isRequest(){return!0}get resp(){return this._||(this._=this.decodeResponse(this.A)),this._}set resp(t){this._=t,this.A=void 0}get respBuff(){return this.A||(this.A=this.encodeResponse(this._)),this.A}set respBuff(t){this._=void 0,this.A=t}_;A}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 i{encode(t){const e=JSON.stringify(t);return o.encode(e)}decode(t){const e=h.decode(t);return JSON.parse(e)}}class u 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 d extends a{}let l=(t,e)=>new d(e.message);const g=t=>{l=t};var p;!function(t){t.AuthTimeout="AuthTimeout",t.BadRequest="BadRequest",t.ConnectFailed="ConnectFailed",t.ConnectionClosed="ConnectionClosed",t.InvalidConnString="InvalidConnString",t.InvalidConnection="InvalidConnection",t.InvalidPackage="InvalidPackage",t.NotAuthenticated="NotAuthenticated",t.NotFound="NotFound",t.Timeout="Timeout",t.UnknownError="UnknownError"}(p||(p={}));class f{constructor(){this.j=this,this.k=this}getPrev(){return this.j}getNext(){return this.k}insertBefore(t){this.k=t,this.j=t.j,t.j=this,this.j.k=this}insertAfter(t){this.j=t,this.k=t.k,t.k=this,this.k.j=this}remove(){this.j.k=this.k,this.k.j=this.j}j;k}class w{constructor(){this.B=new f}get g(){return this.B.getNext()}get a(){return this.B.getPrev()}X(t){return t===this.B}Y(t){t.insertAfter(this.B)}ap(t){t.insertBefore(this.B)}B}const y=()=>{},k=t=>{let e=0,s=0;for(;e<t.length;)s+=t[e++].length;const i=new Uint8Array(s);for(s=0;e>0;){const r=t[--e];i.set(r,s),s+=r.length}return i};class v extends f{}class m{constructor(t,e,s){if(this.Z=t,this.L=e,this.H=s,this.p=0,this.I=0,this.t=[],0===t)return this.insert=y,this.update=y,void(this.remove=y);for(let t=0;t<s;t++)this.t.push(new w)}insert(t){this.I||(this.a0=setInterval(this.ab,this.Z)),this.I++,this.t[this.p].Y(t)}update(t){t.remove(),this.t[this.p].Y(t)}remove(t){t.remove(),this.I--,this.I||clearInterval(this.a0)}ab=()=>{const{L:t,H:e,t:s}=this;if(this.p++,this.p===e&&(this.p=0),t){let i=this.p+t;i>=e&&(i-=e);const r=s[i];for(let t=r.g;!r.X(t);t=t.getNext())t.warnTimeout()}const i=s[this.p];for(let t=i.g;!i.X(t);t=t.getNext())t.onTimeout()};Z;L;H;p;I;a0;t}class b extends v{constructor(t,e,s){super(),this.b=t,this.a7=e,this.val=s}val;warnTimeout(){}onTimeout(){this.b.delete(this.a7)}b;a7}class T{constructor(t,e){this.b=new Map,this.q=new m(t,0,e)}get(t){const{b:e,q:s}=this,i=e.get(t);if(i)return s.update(i),i.val}set(t,e){const{b:s,q:i}=this;let r=s.get(t);r?(r.val=e,i.update(r)):(r=new b(s,t,e),s.set(t,r),i.insert(r))}delete(t){const{b:e,q:s}=this,i=e.get(t);return!!i&&(s.remove(i),e.delete(t))}b;q}class E{constructor(t,e){this.a8=new Promise(t=>{this.w=t}),this.O=e,t>0?this.l=setTimeout(()=>{this.l=void 0,this.w(!0),e?.()},t):(this.w(!0),e?.())}ar(t){const{l:e,w:s,O:i}=this;e&&clearTimeout(e),t>0?this.l=setTimeout(()=>{this.l=void 0,i?.(),s(!0)},t):(this.l=void 0,s(!0),i?.())}ak(){this.l&&(clearTimeout(this.l),this.l=void 0,this.w(!1))}at(){return this.a8}w;a8;l;O}class P extends c{}class x{constructor(t,e,s){let i=sessionStorage.getItem("jrSession");i||(i=crypto.randomUUID(),sessionStorage.setItem("jrSession",i)),this.c="",this.d=i,this.V=t,this.Q="",this.E=e,this.f=s,this.R=this.R.bind(this);const r=()=>{throw new Error("JustrunAuthProvider shall be enabled before use")};this.authWithCredential=r,this.authWithOAuth2=r,this.resetAuthState=r,this.resetConnState=r}async init(t,e){const s=x.prototype;this.authWithCredential=s.authWithCredential,this.authWithOAuth2=s.authWithOAuth2,this.resetAuthState=s.resetAuthState,this.resetConnState=s.resetConnState,e.onConnectionStatus(this.an.bind(this)),e.registerMessage(t,P),e.handleMessageWith(P,this.ao.bind(this));const{d:i,f:r}=this,n=this.W();if(n){const{g:t,j:e}=n;if(Math.round(Date.now()/1e3)<=e)return void r(t,i)}r("","")}async preConnect(){const{c:t,d:e,h:s,f:i,E:r}=this;if(s)return i(t,e,s),"";const n=""===s;this.h=void 0;let o=this.W();if(o){const{g:t,b:s,i,j:h}=o,a=Math.round(Date.now()/1e3);if(a<=i)return this.Q=o.b,r(s,e,n);if(a<=h&&(o=await this.S("refresh",""),o))return this.Q=o.b,r(o.b,e,n)}return this.h="unknown error",i("",""),""}async authWithCredential(t,e,s,i){await this.S(t,`${e},${s},${i}`)}authWithOAuth2(t,e,s,i){const r=`${e}?client_id=${s}&redirect_uri=${this.V}&scope=${i}&response_type=code`;window.location.href=`${r}&state=${this.d}~${s}~${encodeURI(t)}`}async resetAuthState(){await this.S("signout","")}resetConnState(t){if(this.h){this.h=t?"":void 0;const{c:e,d:s,f:i}=this;i(e,s)}}an(t,e,s,i){if(t){this.a=e;const t=this.W();if(t){const{i:e}=t;this.F=new E(1e3*e-Date.now(),this.R)}}else if(this.a=void 0,this.F&&(this.F.ak(),this.F=void 0),!s){const t=this.h=i?.message||"unknown error",{c:e,d:s,f:r}=this;r(e,s,t)}}async R(){this.F=void 0;const{d:t,E:e}=this,s=await this.S("refresh","");if(s&&this.a){const{b:i,i:r}=s;if(i===this.Q){const s=e(i,t),[n,o]=s.split("://");await fetch(`${n.replace("ws","http")}://${o}`),this.F=new E(1e3*r-Date.now(),this.R)}else this.a&&(this.a.close(),this.a=void 0)}}async ao(t){const{c:e,d:s,a:i}=this,{type:r,n}=t;"kick"===r&&i&&(i.close(),this.a=void 0,n&&(this.h=n,this.f(e,s,n)))}W(){const t=localStorage.getItem("jrAuth")||void 0;if(t){const[e,s,i,r]=t.split(","),{c:n,a:o}=this;return n&&e!==n&&o&&(o.close(),this.a=void 0),this.c=e,{g:e,b:s,i:parseInt(i),j:parseInt(r)}}}async S(t,e){const s=await fetch(this.V,{method:"POST",body:`${t}:${e}`}).catch(t=>{throw new d(t.message)}),i=await s.text();if(!s.ok)throw new d(i);if(i.startsWith("auth:")){const t=i.substring(5);localStorage.setItem("jrAuth",t);const[e,s,r,n]=t.split(","),{c:o,a:h}=this;return o!==e&&(o&&h&&(h.close(),this.a=void 0),this.c=e,this.f(e,this.d,this.h)),{g:e,b:s,i:parseInt(r),j:parseInt(n)}}if(i.startsWith("clear:"))localStorage.removeItem("jrAuth"),this.c="",this.h=void 0,this.f("",""),this.a&&(this.a.close(),this.a=void 0);else if(i.startsWith("error:")){"refresh"!==t&&"signout"!==t||(localStorage.removeItem("jrAuth"),this.c="",this.h=void 0,this.f("",""),this.a&&(this.a.close(),this.a=void 0));const e=i.substring(6);throw new d(e)}}c;d;V;Q;a;h;F;E;f}const C=new Error(p.NotAuthenticated);class S{constructor(t,e,s){this.o=t,this.x=e,this.y=!s}get pkgType(){return this.o.pkgType}get k(){return this.o.name}async w(t,e){const{buff:s,bodyOffset:i}=e,r=s.subarray(i),n=new this.o(r,!0);if(void 0===t.context&&this.y)l(t,C);else try{await this.x(t,n)}catch(e){e.isError||l(t,e)}void 0===t.context&&await t.close()}async p(t,e){if(void 0===t.context&&this.y)l(t,C);else try{return await this.x(t,e)}catch(e){e.isError||l(t,e)}}o;x;y}class A{constructor(t,e,s){this.o=t,this.x=e,this.y=!s}get pkgType(){return this.o.pkgType}get k(){return this.o.name}async w(t,e){const{buff:s,requestId:i,bodyOffset:r}=e,n=s.subarray(r),o=new this.o(n,!0);let h;if(void 0===t.context&&this.y)h=l(t,C);else try{await this.x(t,o)}catch(e){h=e.isError?e:l(t,e)}try{h?await t.sendErrorResponse(h,i):await t.sendResponse(o,i),void 0===t.context&&await t.close()}catch{return}}async p(t,e){if(void 0!==t.context||!this.y)try{return await this.x(t,e)}catch(e){throw e.isError?e:l(t,e)}throw l(t,C)}o;x;y}class R{b(){return null}init(t){this.G=t}async F(t){return!1}async H(){return!1}async g(t){const e=new WebSocket(t);return new Promise((t,s)=>{e.onerror=t=>{s(new Error(p.UnknownError))},e.onopen=()=>{t(this.al(e,!0))}})}al=(t,e)=>{const s=new I(t),{conn:i,$:r,s:n}=this.G(s,e);t.onmessage=t=>{t.data.arrayBuffer().then(t=>{r(new Uint8Array(t))})};let o=!0;return t.onerror=t=>{o=!1,n(new Error(p.UnknownError))},t.onclose=t=>{o&&n(1e3===t.code?void 0:new Error(t.reason||"unknown error"))},i};G(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class I{constructor(t){this.i=t}get socket(){return{localAddress:"",localPort:0,remoteAddress:"",remoteFamily:"",remotePort:0}}async B(t){if(this.i)return this.i.send(t);throw new Error(p.InvalidConnection)}async m(t){if(this.i){const e=k(t);return this.i.send(e)}throw new Error(p.InvalidConnection)}async close(t){const{i:e}=this;e&&(this.i=void 0,t?e.close(1006,t.message):e.readyState<=WebSocket.OPEN&&e.close(1e3))}E(){return!this.i}i}const $=(t,e,s)=>{const i=[e,t],r=(t=>{let e,s=0,i=0;for(;s<t.length;)for(i++,e=t[s++];e>>>=7;)i++;return i})(i),n=new Uint8Array(r);((t,e,s=0)=>{let i,r,n=s;for(i=0;i<t.length;i++){r=t[i];let s=127&r;for(;r>>>=7;)e[n++]=128|s,s=127&r;e[n++]=s}if(n<=e.length)return n-s;throw new Error("Buffer overflow")})(i,n),s.push(n)},W=t=>{const{g:[e,s],j:i}=((t,e,s=0)=>{let i,r,n=s,o=0,h=0;const a=[];for(i=0;i<t;i++){do{r=e[n++],o|=(127&r)<<h,h+=7}while(128&r);if(!(void 0!==r&&h<=35))throw new Error("Parsing failed");a.push(o),o=0,h=0}return{g:a,j:n-s}})(2,t);return{buff:t,pkgType:s,requestId:e,bodyOffset:i}};class j{constructor(t){this.a9=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.g()).sendErrorResponse(t,e)}async sendResponse(t,e){return(this.a??await this.g()).sendResponse(t,e)}async sendMessage(t){return(this.a??await this.g()).sendMessage(t)}async sendRequest(t){return(this.a??await this.g()).sendRequest(t)}async close(t){const{a:e}=this;if(e)return this.a=void 0,void await e.close(t);this.r&&await this.r.then(e=>{this.a=void 0,e.close(t)},()=>this.a=void 0)}async au(){this.a||await this.g()}am(){this.a=void 0}async g(){let{r:t}=this;return t||(t=this.r=new Promise((t,e)=>{this.a9().then(s=>{s?(t(s),this.a=s,this.r=void 0):e(new Error("Connect failed"))})})),await t}a9;a;r}class M{constructor(t,e){const{responsePkgType:s,timeoutMonitor:i,makePkgBuff:r,parsePkgBuff:n}=e;this.responsePkgType=s,this.u=0,this.v=new Map,this.h=new Map,this.T=new Map,this.a1=r,this.a2=n,this.C=t,this.M=i,this.w=this.ac;const o=[];this.a1(s,2097152,o),this.a3={makePkgBuff:r,responsePkgType:s,q:k(o)},this.h.set(s,this),this.C.init((t,e)=>{do{this.u++}while(this.v.has(2097151&this.u));const s=new B(this.a3,t,2097151&this.u);this.v.set(s.id,s),this.M?.insert(s);const i={conn:s,$:t=>this.ad(s,t),s:t=>this.ae(s,t)};return this.N?.(this,s,!!e),i})}responsePkgType;context;N;J;b(){return this.C.b()}P(t){return this.v.get(t)}g(t){return this.C.g(t)}async Q(){do{this.u++}while(this.v.has(2097151&this.u));const t=new N(2097151&this.u,this);return this.v.set(t.id,t),t}async R(t){return this.C.F(t)}async S(){return this.C.H()}af(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.p(t,e);throw new Error(p.InvalidPackage)}ag(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.p(t,e);throw new Error(p.InvalidPackage)}registerError(t,e){e.registerPkgType(t),this.T.set(t,e);const s=this.h.get(this.responsePkgType);this.h.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.k} has an incompatible pkgType ${typeof e}`);if(this.h.has(e))throw new Error(`Wrapper ${t.k} is already handled by another handler`);return this.h.set(e,t),t}handleRequestWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.k} has an incompatible pkgType ${typeof e}`);if(this.h.has(e))throw new Error(`Wrapper ${t.k} is already handled by another handler`);return this.h.set(e,t),t}aq(t){const e=t.pkgType;return this.h.get(e)===t&&(this.h.delete(e),!0)}ad(t,e){this.M?.update(t);const s=this.a2(e),i=this.h.get(s.pkgType);i?i.w(t,s):t.close(new Error(p.InvalidPackage))}ae(t,e){this.v.delete(t.id),this.M?.remove(t),t.s(e),this.J?.(this,t,t.e.E(),e)}ac(t,e){try{const{buff:s,pkgType:i,requestId:r,bodyOffset:n}=e;if(i===this.responsePkgType){const e=s.subarray(n);t.a4(r,e)}else{const e=this.T.get(i);if(e){const i=new e(s.subarray(n),!0);t.ah(r,i)}}}catch{t.close(new Error(p.InvalidPackage))}}a3;u;a1;a2;C;v;h;T;M}class B extends v{constructor(t,e,s){super(),this.id=s,this.e=e,this.D=t,this.a5=0}context;id;e;get socket(){return this.e.socket}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.D.makePkgBuff(e,0,s),this.e.m(s)}sendRequest(t){return this.n=new Map,this.sendRequest=this.a6,this.a4=this.ai,this.s=this.aj,this.a6(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,i=[t.buff];return this.D.makePkgBuff(s,e,i),this.e.m(i)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:i,responsePkgType:r}=this.D;return i(r,e,s),this.e.m(s)}async close(t){await this.e.close(t)}a4(t,e){2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}ah(t,e){const s=this.n.get(t);if(s)return this.n.delete(t),s.j(e)}warnTimeout(){this.e.B(this.D.q).catch(y)}onTimeout(){this.close(new Error(p.Timeout))}s(t){}async a6(t){const e=2097151&this.a5++,s=t.constructor.pkgType,i=[t.buff];this.D.makePkgBuff(s,e,i);const r=new Promise((t,s)=>{const r={d:t,j:s};this.n.set(e,r),this.e.m(i).catch(t=>{this.n.delete(e),s(t)})});t.respBuff=await r}ai(t,e){const s=this.n.get(t);if(s)return this.n.delete(t),void s.d(e);2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}aj(t){const e=this.n;if(e.size>0){t||(t=new Error(p.ConnectionClosed));for(const[s,i]of e)e.delete(s),i.j(t)}this.n=void 0}D;n;a5}class N{constructor(t,e){this.id=t,this.U=e}context;id;socket;get remoteFamily(){return"Local"}async sendMessage(t){return this.U.af(this,t)}async sendRequest(t){return this.U.ag(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}U}const q=(t,e)=>new F(t,{responsePkgType:0,makePkgBuff:$,parsePkgBuff:W,timeoutMonitor:e});var O;!function(t){t[t.NotConnected=0]="NotConnected",t[t.Connecting=1]="Connecting",t[t.Connected=2]="Connected"}(O||(O={}));class F{constructor(t,e){const s=new R,i=this.m=new M(s,e);this.K=O.NotConnected,this.aa=t,this.a=new j(()=>this.g()),i.N=this.N.bind(this),i.J=this.J.bind(this)}getConnState(){return this.K}enableAuthenticate(t,e){this.P=e,e.init(t,this)}registerError(t,e){return this.m.registerError(t,e)}registerMessage(t,e){return this.m.registerMessage(t,e)}registerRequest(t,e){return this.m.registerRequest(t,e)}handleMessageWith(t,e){this.m.handleMessageWith(new S(t,(t,s)=>e(s.data),!0))}handleRequestWith(t,e){this.m.handleRequestWith(new A(t,async(t,s)=>{s.resp=await e(s.data)},!0))}sendMessage(t){return this.a.sendMessage(t)}async sendRequest(t){return await this.a.sendRequest(t),t.resp}onConnectionStatus(t){let{b:e}=this;e||(this.b=e=[]),e.push(t)}async g(){const{m:t,P:e}=this;this.K=O.Connecting;try{const s=e?await e.preConnect():this.aa;if(!s)throw new Error(p.UnknownError);return await t.g(s)}catch(e){const s=e instanceof Error?e:new Error(p.UnknownError);this.J(t,this.a,!0,s)}}N(t,e,s){if(this.K=O.Connected,e.context="",this.b)for(const t of this.b)t(!0,e,s)}J(t,e,s,i){if(this.K=O.NotConnected,this.a.am(),this.b)for(const t of this.b)t(!1,e,s,i)}aa;m;a;K;P;b}export{d as CommonError,s as ErrorWrapper,r as EventWrapper,a as JsonError,c as JsonMessage,u as JsonRequest,x as JustrunAuthProvider,i as MessageWrapper,p as NetErrorString,n as RequestWrapper,T as TimeoutCache,m as TimeoutMonitor,F as WsClient,q as createNumPkgTypeClient,g as handleUncaughtErrorWith};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "justrun-ws",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "scripts": {
5
5
  "start": "webpack serve"
6
6
  },
@@ -30,7 +30,7 @@
30
30
  "eslint": "^9.21.0",
31
31
  "eslint-plugin-import": "^2.31.0",
32
32
  "globals": "^16.5.0",
33
- "minify-ts": "^1.2.5",
33
+ "minify-ts": "^1.2.7",
34
34
  "ts-loader": "^9.5.2",
35
35
  "webpack": "^5.98.0",
36
36
  "webpack-cli": "^6.0.1"