justrun-ws 0.3.2 → 0.3.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/index.d.ts +43 -35
- package/lib/index.min.mjs +1 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -14,12 +14,12 @@ export interface IWrapper<DataT> {
|
|
|
14
14
|
declare abstract class DataWrapper<DataT> implements IWrapper<DataT> {
|
|
15
15
|
constructor(a: DataT | Uint8Array, needParse?: true);
|
|
16
16
|
get data(): DataT;
|
|
17
|
-
set data(
|
|
17
|
+
set data(val: DataT);
|
|
18
18
|
get buff(): Uint8Array;
|
|
19
19
|
protected abstract encode(data: DataT): Uint8Array;
|
|
20
20
|
protected abstract decode(buffer: Uint8Array): DataT;
|
|
21
|
-
private
|
|
22
|
-
private
|
|
21
|
+
private $?;
|
|
22
|
+
private _?;
|
|
23
23
|
}
|
|
24
24
|
declare abstract class PkgWrapper<DataT> extends DataWrapper<DataT> {
|
|
25
25
|
static context: unknown;
|
|
@@ -39,13 +39,13 @@ export declare abstract class RequestWrapper<RequestT, ResponseT> extends PkgWra
|
|
|
39
39
|
constructor(a: RequestT | Uint8Array, needParse?: true);
|
|
40
40
|
isRequest(): boolean;
|
|
41
41
|
get resp(): ResponseT;
|
|
42
|
-
set resp(
|
|
42
|
+
set resp(val: ResponseT);
|
|
43
43
|
get respBuff(): Uint8Array;
|
|
44
|
-
set respBuff(
|
|
44
|
+
set respBuff(val: Uint8Array);
|
|
45
45
|
protected abstract encodeResponse(response: ResponseT): Uint8Array;
|
|
46
46
|
protected abstract decodeResponse(buffer: Uint8Array): ResponseT;
|
|
47
|
-
private
|
|
48
|
-
private
|
|
47
|
+
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;
|
|
@@ -73,26 +73,26 @@ declare class ListNode {
|
|
|
73
73
|
insertBefore<SelfT extends ListNode>(this: SelfT, node: SelfT): void;
|
|
74
74
|
insertAfter<SelfT extends ListNode>(this: SelfT, node: SelfT): void;
|
|
75
75
|
remove(): void;
|
|
76
|
-
private b;
|
|
77
76
|
private j;
|
|
77
|
+
private k;
|
|
78
78
|
}
|
|
79
79
|
declare abstract class MonitorItem extends ListNode {
|
|
80
80
|
abstract warnTimeout(): void;
|
|
81
81
|
abstract onTimeout(): void;
|
|
82
82
|
}
|
|
83
83
|
export declare class TimeoutMonitor {
|
|
84
|
-
constructor(
|
|
84
|
+
constructor(c: number, h: number, d: number);
|
|
85
85
|
insert(item: MonitorItem): void;
|
|
86
86
|
update(item: MonitorItem): void;
|
|
87
87
|
remove(item: MonitorItem): void;
|
|
88
|
-
private
|
|
89
|
-
private
|
|
90
|
-
private
|
|
91
|
-
private
|
|
92
|
-
private
|
|
93
|
-
private
|
|
94
|
-
private
|
|
95
|
-
private
|
|
88
|
+
private ac;
|
|
89
|
+
private a0;
|
|
90
|
+
private M;
|
|
91
|
+
private I;
|
|
92
|
+
private p;
|
|
93
|
+
private J;
|
|
94
|
+
private a1?;
|
|
95
|
+
private t;
|
|
96
96
|
}
|
|
97
97
|
export type TypedPkgHubOpts<PkgTypeT extends number | string> = {
|
|
98
98
|
readonly responsePkgType: PkgTypeT;
|
|
@@ -133,6 +133,14 @@ export declare enum NetErrorString {
|
|
|
133
133
|
Timeout = "Timeout",
|
|
134
134
|
UnknownError = "UnknownError"
|
|
135
135
|
}
|
|
136
|
+
export declare class TimeoutCache<KeyT extends number | string, ValT> {
|
|
137
|
+
constructor(c: number, d: number);
|
|
138
|
+
get(key: KeyT): ValT | undefined;
|
|
139
|
+
set(key: KeyT, val: ValT): void;
|
|
140
|
+
delete(key: KeyT): boolean;
|
|
141
|
+
private b;
|
|
142
|
+
private q;
|
|
143
|
+
}
|
|
136
144
|
export interface IAuthProvider {
|
|
137
145
|
init<PkgTypeT extends number | string>(proxyPrivatePkgType: PkgTypeT, client: WsClient<PkgTypeT>): Promise<void>;
|
|
138
146
|
preConnect(): Promise<string>;
|
|
@@ -144,7 +152,7 @@ declare enum ClientConnState {
|
|
|
144
152
|
Connected = 2
|
|
145
153
|
}
|
|
146
154
|
export declare class WsClient<PkgTypeT extends number | string> {
|
|
147
|
-
constructor(connUrl: string,
|
|
155
|
+
constructor(connUrl: string, t: TypedPkgHubOpts<PkgTypeT>);
|
|
148
156
|
getConnState(): ClientConnState;
|
|
149
157
|
enableAuthenticate(proxyPrivatePkgType: PkgTypeT, authProvider: IAuthProvider): void;
|
|
150
158
|
registerError<ErrorT>(pkgType: PkgTypeT, wrapperCtor: ErrorWrapperCtor<ErrorT>): void;
|
|
@@ -156,37 +164,37 @@ export declare class WsClient<PkgTypeT extends number | string> {
|
|
|
156
164
|
sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
|
|
157
165
|
onConnectionStatus(listener: (isOpen: boolean, conn: IConn<string>, active: boolean, err?: Error) => void): void;
|
|
158
166
|
private g;
|
|
159
|
-
private
|
|
160
|
-
private
|
|
161
|
-
private
|
|
162
|
-
private
|
|
167
|
+
private O;
|
|
168
|
+
private K;
|
|
169
|
+
private ab;
|
|
170
|
+
private m;
|
|
163
171
|
private a;
|
|
164
|
-
private
|
|
165
|
-
private
|
|
172
|
+
private L;
|
|
173
|
+
private Q?;
|
|
166
174
|
private b?;
|
|
167
175
|
}
|
|
168
176
|
export declare class JustrunAuthProvider implements IAuthProvider {
|
|
169
|
-
constructor(w: string,
|
|
177
|
+
constructor(w: string, x: (b: string, k: string, o?: boolean) => string, y: (g: string, k: string, s?: string) => void);
|
|
170
178
|
init<PkgTypeT extends number | string>(proxyPrivatePkgType: PkgTypeT, client: WsClient<PkgTypeT>): Promise<void>;
|
|
171
179
|
preConnect(): Promise<string>;
|
|
172
180
|
authWithCredential(type: "signin" | "signup", id: string, hash: string, extra: string): Promise<void>;
|
|
173
181
|
authWithOAuth2(returnTo: string, oauthUrl: string, clientId: string, scope: string): void;
|
|
174
182
|
resetAuthState(): Promise<void>;
|
|
175
183
|
resetConnState(forceKick: boolean): void;
|
|
176
|
-
private
|
|
177
|
-
private
|
|
178
|
-
private
|
|
179
|
-
private
|
|
180
|
-
private
|
|
184
|
+
private ao;
|
|
185
|
+
private S;
|
|
186
|
+
private ap;
|
|
187
|
+
private X;
|
|
188
|
+
private T;
|
|
181
189
|
private c;
|
|
182
190
|
private d;
|
|
183
|
-
private
|
|
184
|
-
private
|
|
185
|
-
private
|
|
191
|
+
private W;
|
|
192
|
+
private R;
|
|
193
|
+
private z;
|
|
186
194
|
private a?;
|
|
187
195
|
private h?;
|
|
188
|
-
private
|
|
189
|
-
private
|
|
196
|
+
private G?;
|
|
197
|
+
private F;
|
|
190
198
|
private f;
|
|
191
199
|
}
|
|
192
200
|
|
package/lib/index.min.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class t{constructor(t,e){e?this.y=t:this.z=t}get data(){return this.z||(this.z=this.decode(this.y)),this.z}set data(t){this.z=t,this.y=void 0}get buff(){return this.y||(this.y=this.encode(this.z)),this.y}y;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._)),this.$}set resp(t){this.$=t,this._=void 0}get respBuff(){return this._||(this._=this.encodeResponse(this.$)),this._}set respBuff(t){this.$=void 0,this._=t}$;_}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 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 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.b=this,this.j=this}getPrev(){return this.b}getNext(){return this.j}insertBefore(t){this.j=t,this.b=t.b,t.b=this,this.b.j=this}insertAfter(t){this.b=t,this.j=t.j,t.j=this,this.j.b=this}remove(){this.b.j=this.j,this.j.b=this.b}b;j}class w{constructor(){this.A=new f}get e(){return this.A.getNext()}get a(){return this.A.getPrev()}W(t){return t===this.A}X(t){t.insertAfter(this.A)}an(t){t.insertBefore(this.A)}A}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.Y=t,this.K=e,this.G=s,this.o=0,this.H=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.H||(this.Z=setInterval(this.a9,this.Y)),this.H++,this.r[this.o].X(t)}update(t){t.remove(),this.r[this.o].X(t)}remove(t){t.remove(),this.H--,this.H||clearInterval(this.Z)}a9=()=>{const{K:t,G:e,r:s}=this;if(this.o++,this.o===e&&(this.o=0),t){let i=this.o+t;i>=e&&(i-=e);const r=s[i];for(let t=r.e;!r.W(t);t=t.getNext())t.warnTimeout()}const i=s[this.o];for(let t=i.e;!i.W(t);t=t.getNext())t.onTimeout()};Y;K;G;o;H;Z;r}class b{constructor(t,e){this.a6=new Promise(t=>{this.u=t}),this.N=e,t>0?this.k=setTimeout(()=>{this.k=void 0,this.u(!0),e?.()},t):(this.u(!0),e?.())}ap(t){const{k:e,u:s,N:i}=this;e&&clearTimeout(e),t>0?this.k=setTimeout(()=>{this.k=void 0,i?.(),s(!0)},t):(this.k=void 0,s(!0),i?.())}ai(){this.k&&(clearTimeout(this.k),this.k=void 0,this.u(!1))}aq(){return this.a6}u;a6;k;N}class E extends c{}class T{constructor(t,e,s){let i=sessionStorage.getItem("jrSession");i||(i=crypto.randomUUID(),sessionStorage.setItem("jrSession",i)),this.c="",this.d=i,this.U=t,this.P="",this.x=!1,this.D=e,this.f=s,this.Q=this.Q.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=T.prototype;this.authWithCredential=s.authWithCredential,this.authWithOAuth2=s.authWithOAuth2,this.resetAuthState=s.resetAuthState,this.resetConnState=s.resetConnState,e.onConnectionStatus(this.al.bind(this)),e.registerMessage(t,E),e.handleMessageWith(E,this.am.bind(this));const{d:i,f:r}=this,n=this.V();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,D:r}=this;if(s)return i(t,e,s),"";const n=""===s;this.h=void 0;let o=this.V();if(o){const{g:t,b:s,i,j:h}=o,a=Math.round(Date.now()/1e3);if(a<=i)return this.P=o.b,r(s,e,n);if(a<=h&&(o=await this.R("refresh",""),o))return this.P=o.b,r(o.b,e,n)}return this.h="unknown error",i("",""),""}async authWithCredential(t,e,s,i){if(!this.x){this.x=!0;try{await this.R(t,`${e},${s},${i}`)}finally{this.x=!1}}}authWithOAuth2(t,e,s,i){const r=`${e}?client_id=${s}&redirect_uri=${this.U}&scope=${i}&response_type=code`;window.location.href=`${r}&state=${this.d}~${s}~${encodeURI(t)}`}async resetAuthState(){if(!this.x){this.x=!0;try{await this.R("signout","")}finally{this.x=!1}}}resetConnState(t){if(this.h){this.h=t?"":void 0;const{c:e,d:s,f:i}=this;i(e,s)}}al(t,e,s,i){if(t){this.a=e;const t=this.V();if(t){const{i:e}=t;this.E=new b(1e3*e-Date.now(),this.Q)}}else if(this.a=void 0,this.E&&(this.E.ai(),this.E=void 0),!s){const t=this.h=i?.message||"unknown error",{c:e,d:s,f:r}=this;r(e,s,t)}}async Q(){this.E=void 0;const{d:t,D:e}=this,s=await this.R("refresh","");if(s&&this.a){const{b:i,i:r}=s;if(i===this.P){const s=e(i,t),[n,o]=s.split("://");await fetch(`${n.replace("ws","http")}://${o}`),this.E=new b(1e3*r-Date.now(),this.Q)}else this.a&&(this.a.close(),this.a=void 0)}}async am(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)))}V(){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 R(t,e){const s=await fetch(this.U,{method:"POST",body:`${t}:${e}`}).catch(t=>{throw new u(t.message)}),i=await s.text();if(!s.ok)throw new u(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 u(e)}}c;d;U;P;x;a;h;E;D;f}const P=new Error(p.NotAuthenticated);class x{constructor(t,e,s){this.n=t,this.v=e,this.w=!s}get pkgType(){return this.n.pkgType}get k(){return this.n.name}async x(t,e){const{buff:s,bodyOffset:i}=e,r=s.subarray(i),n=new this.n(r,!0);if(void 0===t.context&&this.w)l(t,P);else try{await this.v(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.w)l(t,P);else try{return await this.v(t,e)}catch(e){e.isError||l(t,e)}}n;v;w}class C{constructor(t,e,s){this.n=t,this.v=e,this.w=!s}get pkgType(){return this.n.pkgType}get k(){return this.n.name}async x(t,e){const{buff:s,requestId:i,bodyOffset:r}=e,n=s.subarray(r),o=new this.n(n,!0);let h;if(void 0===t.context&&this.w)h=l(t,P);else try{await this.v(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.w)try{return await this.v(t,e)}catch(e){throw e.isError?e:l(t,e)}throw l(t,P)}n;v;w}class A{b(){return null}init(t){this.F=t}async G(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.aj(e,!0))}})}aj=(t,e)=>{const s=new R(t),{conn:i,$:r,q:n}=this.F(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};F(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class R{constructor(t){this.i=t}get socket(){return{localAddress:"",localPort:0,remoteAddress:"",remoteFamily:"",remotePort:0}}async D(t){if(this.i)return this.i.send(t);throw new Error(p.InvalidConnection)}async n(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 S=(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)},I=t=>{const{h:[e,s],k: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{h:a,k:n-s}})(2,t);return{buff:t,pkgType:s,requestId:e,bodyOffset:i}};class ${constructor(t){this.a7=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.p&&await this.p.then(e=>{this.a=void 0,e.close(t)},()=>this.a=void 0)}async ar(){this.a||await this.g()}ak(){this.a=void 0}async g(){let{p:t}=this;return t||(t=this.p=new Promise((t,e)=>{this.a7().then(s=>{s?(t(s),this.a=s,this.p=void 0):e(new Error("Connect failed"))})})),await t}a7;a;p}class W{constructor(t,e){const{responsePkgType:s,timeoutMonitor:i,makePkgBuff:r,parsePkgBuff:n}=e;this.responsePkgType=s,this.s=0,this.t=new Map,this.h=new Map,this.S=new Map,this.a0=r,this.a1=n,this.B=t,this.L=i,this.x=this.aa;const o=[];this.a0(s,2097152,o),this.a2={makePkgBuff:r,responsePkgType:s,r:k(o)},this.h.set(s,this),this.B.init((t,e)=>{do{this.s++}while(this.t.has(2097151&this.s));const s=new M(this.a2,t,2097151&this.s);this.t.set(s.id,s),this.L?.insert(s);const i={conn:s,$:t=>this.ab(s,t),q:t=>this.ac(s,t)};return this.M?.(this,s,!!e),i})}responsePkgType;context;M;I;b(){return this.B.b()}P(t){return this.t.get(t)}g(t){return this.B.g(t)}async Q(){do{this.s++}while(this.t.has(2097151&this.s));const t=new j(2097151&this.s,this);return this.t.set(t.id,t),t}async R(t){return this.B.G(t)}async U(){return this.B.H()}ad(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.p(t,e);throw new Error(p.InvalidPackage)}ae(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.S.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}ao(t){const e=t.pkgType;return this.h.get(e)===t&&(this.h.delete(e),!0)}ab(t,e){this.L?.update(t);const s=this.a1(e),i=this.h.get(s.pkgType);i?i.x(t,s):t.close(new Error(p.InvalidPackage))}ac(t,e){this.t.delete(t.id),this.L?.remove(t),t.q(e),this.I?.(this,t,t.e.E(),e)}aa(t,e){try{const{buff:s,pkgType:i,requestId:r,bodyOffset:n}=e;if(i===this.responsePkgType){const e=s.subarray(n);t.a3(r,e)}else{const e=this.S.get(i);if(e){const i=new e(s.subarray(n),!0);t.af(r,i)}}}catch{t.close(new Error(p.InvalidPackage))}}a2;s;a0;a1;B;t;h;S;L}class M extends v{constructor(t,e,s){super(),this.id=s,this.e=e,this.C=t,this.a4=0}context;id;e;get socket(){return this.e.socket}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.C.makePkgBuff(e,0,s),this.e.n(s)}sendRequest(t){return this.m=new Map,this.sendRequest=this.a5,this.a3=this.ag,this.q=this.ah,this.a5(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,i=[t.buff];return this.C.makePkgBuff(s,e,i),this.e.n(i)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:i,responsePkgType:r}=this.C;return i(r,e,s),this.e.n(s)}async close(t){await this.e.close(t)}a3(t,e){2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}af(t,e){const s=this.m.get(t);if(s)return this.m.delete(t),s.j(e)}warnTimeout(){this.e.D(this.C.r).catch(y)}onTimeout(){this.close(new Error(p.Timeout))}q(t){}async a5(t){const e=2097151&this.a4++,s=t.constructor.pkgType,i=[t.buff];this.C.makePkgBuff(s,e,i);const r=new Promise((t,s)=>{const r={d:t,j:s};this.m.set(e,r),this.e.n(i).catch(t=>{this.m.delete(e),s(t)})});t.respBuff=await r}ag(t,e){const s=this.m.get(t);if(s)return this.m.delete(t),void s.d(e);2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}ah(t){const e=this.m;if(e.size>0){t||(t=new Error(p.ConnectionClosed));for(const[s,i]of e)e.delete(s),i.j(t)}this.m=void 0}C;m;a4}class j{constructor(t,e){this.id=t,this.T=e}context;id;socket;get remoteFamily(){return"Local"}async sendMessage(t){return this.T.ad(this,t)}async sendRequest(t){return this.T.ae(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}T}const B=(t,e)=>new q(t,{responsePkgType:0,makePkgBuff:S,parsePkgBuff:I,timeoutMonitor:e});var N;!function(t){t[t.NotConnected=0]="NotConnected",t[t.Connecting=1]="Connecting",t[t.Connected=2]="Connected"}(N||(N={}));class q{constructor(t,e){const s=new A,i=this.l=new W(s,e);this.J=N.NotConnected,this.a8=t,this.a=new $(()=>this.g()),i.M=this.M.bind(this),i.I=this.I.bind(this)}getConnState(){return this.J}enableAuthenticate(t,e){this.O=e,e.init(t,this)}registerError(t,e){return this.l.registerError(t,e)}registerMessage(t,e){return this.l.registerMessage(t,e)}registerRequest(t,e){return this.l.registerRequest(t,e)}handleMessageWith(t,e){this.l.handleMessageWith(new x(t,(t,s)=>e(s.data),!0))}handleRequestWith(t,e){this.l.handleRequestWith(new C(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{l:t,O:e}=this;this.J=N.Connecting;try{const s=e?await e.preConnect():this.a8;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.I(t,this.a,!0,s)}}M(t,e,s){if(this.J=N.Connected,e.context="",this.b)for(const t of this.b)t(!0,e,s)}I(t,e,s,i){if(this.J=N.NotConnected,this.a.ak(),this.b)for(const t of this.b)t(!1,e,s,i)}a8;l;a;J;O;b}export{u as CommonError,s as ErrorWrapper,r as EventWrapper,a as JsonError,c as JsonMessage,d as JsonRequest,T as JustrunAuthProvider,i as MessageWrapper,p as NetErrorString,n as RequestWrapper,m as TimeoutMonitor,q as WsClient,B as createNumPkgTypeClient,g as handleUncaughtErrorWith};
|
|
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};
|