justrun-ws 0.3.1 → 0.3.2
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 +46 -45
- package/lib/index.min.mjs +1 -0
- package/package.json +3 -3
- package/lib/JustRunAuthProvider.js +0 -231
- package/lib/WsClient.js +0 -114
- package/lib/bundle.min.js +0 -1
- package/lib/common/AdjustableTimer.js +0 -52
- package/lib/common/DataWrapper.js +0 -29
- package/lib/common/List.js +0 -52
- package/lib/common/utils.js +0 -30
- package/lib/common/varUintOps.js +0 -103
- package/lib/handler/SimpleHandler.js +0 -124
- package/lib/handler/common.js +0 -10
- package/lib/index.js +0 -8
- package/lib/net/BrowserWsNet.js +0 -97
- package/lib/net/JsonPackageWrapper.js +0 -43
- package/lib/net/LazyConn.js +0 -77
- package/lib/net/NetErrorString.js +0 -15
- package/lib/net/PackageWrapper.js +0 -71
- package/lib/net/TimeoutMonitor.js +0 -70
- package/lib/net/TypedPkgHub.js +0 -307
- package/lib/net/hubPkgSerializer.js +0 -14
- package/lib/net/interfaces.js +0 -2
package/lib/index.d.ts
CHANGED
|
@@ -12,14 +12,14 @@ export interface IWrapper<DataT> {
|
|
|
12
12
|
get data(): DataT;
|
|
13
13
|
}
|
|
14
14
|
declare abstract class DataWrapper<DataT> implements IWrapper<DataT> {
|
|
15
|
-
constructor(
|
|
15
|
+
constructor(a: DataT | Uint8Array, needParse?: true);
|
|
16
16
|
get data(): DataT;
|
|
17
|
-
set data(
|
|
17
|
+
set data(b: 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 y?;
|
|
22
|
+
private z?;
|
|
23
23
|
}
|
|
24
24
|
declare abstract class PkgWrapper<DataT> extends DataWrapper<DataT> {
|
|
25
25
|
static context: unknown;
|
|
@@ -36,16 +36,16 @@ export declare abstract class EventWrapper<EventT> extends PkgWrapper<EventT> {
|
|
|
36
36
|
isEvent(): boolean;
|
|
37
37
|
}
|
|
38
38
|
export declare abstract class RequestWrapper<RequestT, ResponseT> extends PkgWrapper<RequestT> {
|
|
39
|
-
constructor(
|
|
39
|
+
constructor(a: RequestT | Uint8Array, needParse?: true);
|
|
40
40
|
isRequest(): boolean;
|
|
41
41
|
get resp(): ResponseT;
|
|
42
|
-
set resp(
|
|
42
|
+
set resp(b: ResponseT);
|
|
43
43
|
get respBuff(): Uint8Array;
|
|
44
|
-
set respBuff(
|
|
44
|
+
set respBuff(b: Uint8Array);
|
|
45
45
|
protected abstract encodeResponse(response: ResponseT): Uint8Array;
|
|
46
46
|
protected abstract decodeResponse(buffer: Uint8Array): ResponseT;
|
|
47
|
-
private
|
|
48
|
-
private
|
|
47
|
+
private $?;
|
|
48
|
+
private _?;
|
|
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
|
|
77
|
-
private
|
|
76
|
+
private b;
|
|
77
|
+
private j;
|
|
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(f: number, h: number, g: 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 a9;
|
|
89
|
+
private Y;
|
|
90
|
+
private K;
|
|
91
|
+
private G;
|
|
92
|
+
private o;
|
|
93
|
+
private H;
|
|
94
|
+
private Z?;
|
|
95
|
+
private r;
|
|
96
96
|
}
|
|
97
97
|
export type TypedPkgHubOpts<PkgTypeT extends number | string> = {
|
|
98
98
|
readonly responsePkgType: PkgTypeT;
|
|
@@ -144,7 +144,7 @@ declare enum ClientConnState {
|
|
|
144
144
|
Connected = 2
|
|
145
145
|
}
|
|
146
146
|
export declare class WsClient<PkgTypeT extends number | string> {
|
|
147
|
-
constructor(connUrl: string,
|
|
147
|
+
constructor(connUrl: string, v: TypedPkgHubOpts<PkgTypeT>);
|
|
148
148
|
getConnState(): ClientConnState;
|
|
149
149
|
enableAuthenticate(proxyPrivatePkgType: PkgTypeT, authProvider: IAuthProvider): void;
|
|
150
150
|
registerError<ErrorT>(pkgType: PkgTypeT, wrapperCtor: ErrorWrapperCtor<ErrorT>): void;
|
|
@@ -155,38 +155,39 @@ export declare class WsClient<PkgTypeT extends number | string> {
|
|
|
155
155
|
sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
|
|
156
156
|
sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
|
|
157
157
|
onConnectionStatus(listener: (isOpen: boolean, conn: IConn<string>, active: boolean, err?: Error) => void): void;
|
|
158
|
-
private
|
|
159
|
-
private
|
|
160
|
-
private
|
|
161
|
-
private
|
|
162
|
-
private
|
|
163
|
-
private
|
|
164
|
-
private
|
|
165
|
-
private
|
|
166
|
-
private
|
|
158
|
+
private g;
|
|
159
|
+
private M;
|
|
160
|
+
private I;
|
|
161
|
+
private a8;
|
|
162
|
+
private l;
|
|
163
|
+
private a;
|
|
164
|
+
private J;
|
|
165
|
+
private O?;
|
|
166
|
+
private b?;
|
|
167
167
|
}
|
|
168
168
|
export declare class JustrunAuthProvider implements IAuthProvider {
|
|
169
|
-
constructor(
|
|
169
|
+
constructor(w: string, y: (b: string, k: string, o?: boolean) => string, z: (g: string, k: string, s?: string) => void);
|
|
170
170
|
init<PkgTypeT extends number | string>(proxyPrivatePkgType: PkgTypeT, client: WsClient<PkgTypeT>): Promise<void>;
|
|
171
171
|
preConnect(): Promise<string>;
|
|
172
172
|
authWithCredential(type: "signin" | "signup", id: string, hash: string, extra: string): Promise<void>;
|
|
173
173
|
authWithOAuth2(returnTo: string, oauthUrl: string, clientId: string, scope: string): void;
|
|
174
174
|
resetAuthState(): Promise<void>;
|
|
175
175
|
resetConnState(forceKick: boolean): void;
|
|
176
|
-
private
|
|
177
|
-
private
|
|
178
|
-
private
|
|
179
|
-
private
|
|
180
|
-
private
|
|
181
|
-
private
|
|
182
|
-
private
|
|
183
|
-
private
|
|
184
|
-
private
|
|
185
|
-
private
|
|
186
|
-
private
|
|
187
|
-
private
|
|
188
|
-
private
|
|
189
|
-
private
|
|
176
|
+
private al;
|
|
177
|
+
private Q;
|
|
178
|
+
private am;
|
|
179
|
+
private V;
|
|
180
|
+
private R;
|
|
181
|
+
private c;
|
|
182
|
+
private d;
|
|
183
|
+
private U;
|
|
184
|
+
private P;
|
|
185
|
+
private x;
|
|
186
|
+
private a?;
|
|
187
|
+
private h?;
|
|
188
|
+
private E?;
|
|
189
|
+
private D;
|
|
190
|
+
private f;
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
export {};
|
|
@@ -0,0 +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};
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "justrun-ws",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"start": "webpack serve"
|
|
6
6
|
},
|
|
7
7
|
"files": [
|
|
8
8
|
"./lib/*"
|
|
9
9
|
],
|
|
10
|
-
"module": "./lib/index.
|
|
10
|
+
"module": "./lib/index.min.mjs",
|
|
11
11
|
"typings": "./lib/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"default": "./lib/index.
|
|
14
|
+
"default": "./lib/index.min.mjs",
|
|
15
15
|
"types": "./lib/index.d.ts"
|
|
16
16
|
}
|
|
17
17
|
},
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import { m } from './common/AdjustableTimer';
|
|
2
|
-
import { CommonError } from './handler/common';
|
|
3
|
-
import { JsonMessage } from './net/JsonPackageWrapper';
|
|
4
|
-
class u extends JsonMessage {
|
|
5
|
-
}
|
|
6
|
-
export class JustrunAuthProvider {
|
|
7
|
-
constructor(w, x, y) {
|
|
8
|
-
let k = sessionStorage.getItem('jrSession');
|
|
9
|
-
if (!k) {
|
|
10
|
-
k = crypto.randomUUID();
|
|
11
|
-
sessionStorage.setItem('jrSession', k);
|
|
12
|
-
}
|
|
13
|
-
this.c = '';
|
|
14
|
-
this.d = k;
|
|
15
|
-
this.T = w;
|
|
16
|
-
this.O = '';
|
|
17
|
-
this.C = x;
|
|
18
|
-
this.f = y;
|
|
19
|
-
this.P = this.P.bind(this);
|
|
20
|
-
const p = () => {
|
|
21
|
-
throw new Error('JustrunAuthProvider shall be enabled before use');
|
|
22
|
-
};
|
|
23
|
-
this.authWithCredential = p;
|
|
24
|
-
this.authWithOAuth2 = p;
|
|
25
|
-
this.resetAuthState = p;
|
|
26
|
-
this.resetConnState = p;
|
|
27
|
-
}
|
|
28
|
-
async init(proxyPrivatePkgType, client) {
|
|
29
|
-
const q = JustrunAuthProvider.prototype;
|
|
30
|
-
this.authWithCredential = q.authWithCredential;
|
|
31
|
-
this.authWithOAuth2 = q.authWithOAuth2;
|
|
32
|
-
this.resetAuthState = q.resetAuthState;
|
|
33
|
-
this.resetConnState = q.resetConnState;
|
|
34
|
-
client.onConnectionStatus(this.ak.bind(this));
|
|
35
|
-
client.registerMessage(proxyPrivatePkgType, u);
|
|
36
|
-
client.handleMessageWith(u, this.al.bind(this));
|
|
37
|
-
const { d, f } = this;
|
|
38
|
-
const e = this.U();
|
|
39
|
-
if (e) {
|
|
40
|
-
const { g, j } = e;
|
|
41
|
-
const now = Math.round(Date.now() / 1000);
|
|
42
|
-
if (now <= j) {
|
|
43
|
-
f(g, d);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
f('', '');
|
|
48
|
-
}
|
|
49
|
-
async preConnect() {
|
|
50
|
-
const { c, d, h, f, C } = this;
|
|
51
|
-
if (h) {
|
|
52
|
-
f(c, d, h);
|
|
53
|
-
return '';
|
|
54
|
-
}
|
|
55
|
-
const o = h === '';
|
|
56
|
-
this.h = undefined;
|
|
57
|
-
let e = this.U();
|
|
58
|
-
if (e) {
|
|
59
|
-
const { g, b, i, j } = e;
|
|
60
|
-
const now = Math.round(Date.now() / 1000);
|
|
61
|
-
if (now <= i) {
|
|
62
|
-
this.O = e.b;
|
|
63
|
-
return C(b, d, o);
|
|
64
|
-
}
|
|
65
|
-
if (now <= j) {
|
|
66
|
-
e = await this.Q('refresh', '');
|
|
67
|
-
if (e) {
|
|
68
|
-
this.O = e.b;
|
|
69
|
-
return C(e.b, d, o);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
this.h = 'unknown error';
|
|
74
|
-
f('', '');
|
|
75
|
-
return '';
|
|
76
|
-
}
|
|
77
|
-
async authWithCredential(type, id, hash, extra) {
|
|
78
|
-
await this.Q(type, `${id},${hash},${extra}`);
|
|
79
|
-
}
|
|
80
|
-
authWithOAuth2(returnTo, oauthUrl, clientId, scope) {
|
|
81
|
-
const z = this.T;
|
|
82
|
-
const t = `${oauthUrl}?client_id=${clientId}&redirect_uri=${z}&scope=${scope}&response_type=code`;
|
|
83
|
-
window.location.href = `${t}&state=${this.d}~${clientId}~${encodeURI(returnTo)}`;
|
|
84
|
-
}
|
|
85
|
-
async resetAuthState() {
|
|
86
|
-
await this.Q('signout', '');
|
|
87
|
-
}
|
|
88
|
-
resetConnState(forceKick) {
|
|
89
|
-
if (this.h) {
|
|
90
|
-
this.h = forceKick ? '' : undefined;
|
|
91
|
-
const { c, d, f } = this;
|
|
92
|
-
f(c, d);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
ak(isOpen, conn, active, err) {
|
|
96
|
-
if (isOpen) {
|
|
97
|
-
this.a = conn;
|
|
98
|
-
const e = this.U();
|
|
99
|
-
if (e) {
|
|
100
|
-
const { i } = e;
|
|
101
|
-
this.D = new m(i * 1000 - Date.now(), this.P);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
this.a = undefined;
|
|
106
|
-
if (this.D) {
|
|
107
|
-
this.D.ah();
|
|
108
|
-
this.D = undefined;
|
|
109
|
-
}
|
|
110
|
-
if (!active) {
|
|
111
|
-
const s = this.h = err?.message || 'unknown error';
|
|
112
|
-
const { c, d, f } = this;
|
|
113
|
-
f(c, d, s);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async P() {
|
|
118
|
-
this.D = undefined;
|
|
119
|
-
const { d, C } = this;
|
|
120
|
-
const e = await this.Q('refresh', '');
|
|
121
|
-
if (e && this.a) {
|
|
122
|
-
const { b, i } = e;
|
|
123
|
-
if (b === this.O) {
|
|
124
|
-
const t = C(b, d);
|
|
125
|
-
const [$, _] = t.split('://');
|
|
126
|
-
await fetch(`${$.replace('ws', 'http')}://${_}`);
|
|
127
|
-
this.D = new m(i * 1000 - Date.now(), this.P);
|
|
128
|
-
}
|
|
129
|
-
else if (this.a) {
|
|
130
|
-
this.a.close();
|
|
131
|
-
this.a = undefined;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
async al(message) {
|
|
136
|
-
const { c, d, a } = this;
|
|
137
|
-
const { type, n } = message;
|
|
138
|
-
if (type === 'kick' && a) {
|
|
139
|
-
a.close();
|
|
140
|
-
this.a = undefined;
|
|
141
|
-
if (n) {
|
|
142
|
-
this.h = n;
|
|
143
|
-
this.f(c, d, n);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
U() {
|
|
148
|
-
const v = localStorage.getItem('jrAuth') || undefined;
|
|
149
|
-
if (v) {
|
|
150
|
-
const [g, b, i, j] = v.split(',');
|
|
151
|
-
const { c, a } = this;
|
|
152
|
-
if (c && g !== c && a) {
|
|
153
|
-
a.close();
|
|
154
|
-
this.a = undefined;
|
|
155
|
-
}
|
|
156
|
-
this.c = g;
|
|
157
|
-
return {
|
|
158
|
-
g,
|
|
159
|
-
b,
|
|
160
|
-
i: parseInt(i),
|
|
161
|
-
j: parseInt(j),
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
async Q(type, A) {
|
|
166
|
-
const resp = await fetch(this.T, {
|
|
167
|
-
method: 'POST',
|
|
168
|
-
body: `${type}:${A}`,
|
|
169
|
-
}).catch((err) => {
|
|
170
|
-
throw new CommonError(err.message);
|
|
171
|
-
});
|
|
172
|
-
const l = await resp.text();
|
|
173
|
-
if (!resp.ok) {
|
|
174
|
-
throw new CommonError(l);
|
|
175
|
-
}
|
|
176
|
-
if (l.startsWith('auth:')) {
|
|
177
|
-
const r = l.substring(5);
|
|
178
|
-
localStorage.setItem('jrAuth', r);
|
|
179
|
-
const [g, b, i, j] = r.split(',');
|
|
180
|
-
const { c, a } = this;
|
|
181
|
-
if (c !== g) {
|
|
182
|
-
if (c && a) {
|
|
183
|
-
a.close();
|
|
184
|
-
this.a = undefined;
|
|
185
|
-
}
|
|
186
|
-
this.c = g;
|
|
187
|
-
this.f(g, this.d, this.h);
|
|
188
|
-
}
|
|
189
|
-
return {
|
|
190
|
-
g,
|
|
191
|
-
b,
|
|
192
|
-
i: parseInt(i),
|
|
193
|
-
j: parseInt(j),
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
else if (l.startsWith('clear:')) {
|
|
197
|
-
localStorage.removeItem('jrAuth');
|
|
198
|
-
this.c = '';
|
|
199
|
-
this.h = undefined;
|
|
200
|
-
this.f('', '');
|
|
201
|
-
if (this.a) {
|
|
202
|
-
this.a.close();
|
|
203
|
-
this.a = undefined;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else if (l.startsWith('error:')) {
|
|
207
|
-
if (type === 'refresh' || type === 'signout') {
|
|
208
|
-
localStorage.removeItem('jrAuth');
|
|
209
|
-
this.c = '';
|
|
210
|
-
this.h = undefined;
|
|
211
|
-
this.f('', '');
|
|
212
|
-
if (this.a) {
|
|
213
|
-
this.a.close();
|
|
214
|
-
this.a = undefined;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
const r = l.substring(6);
|
|
218
|
-
throw new CommonError(r);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
c;
|
|
222
|
-
d;
|
|
223
|
-
T;
|
|
224
|
-
O;
|
|
225
|
-
a;
|
|
226
|
-
h;
|
|
227
|
-
D;
|
|
228
|
-
C;
|
|
229
|
-
f;
|
|
230
|
-
}
|
|
231
|
-
//# sourceMappingURL=JustRunAuthProvider.js.map
|
package/lib/WsClient.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { h, i } from './handler/SimpleHandler';
|
|
2
|
-
import { m } from './net/BrowserWsNet';
|
|
3
|
-
import { n, o } from './net/hubPkgSerializer';
|
|
4
|
-
import { e } from './net/LazyConn';
|
|
5
|
-
import { NetErrorString } from './net/NetErrorString';
|
|
6
|
-
import { u } from './net/TypedPkgHub';
|
|
7
|
-
export const createNumPkgTypeClient = (connUrl, timeoutMonitor) => {
|
|
8
|
-
return new WsClient(connUrl, {
|
|
9
|
-
responsePkgType: 0,
|
|
10
|
-
makePkgBuff: n,
|
|
11
|
-
parsePkgBuff: o,
|
|
12
|
-
timeoutMonitor,
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
export var ClientConnState;
|
|
16
|
-
(function (ClientConnState) {
|
|
17
|
-
ClientConnState[ClientConnState["NotConnected"] = 0] = "NotConnected";
|
|
18
|
-
ClientConnState[ClientConnState["Connecting"] = 1] = "Connecting";
|
|
19
|
-
ClientConnState[ClientConnState["Connected"] = 2] = "Connected";
|
|
20
|
-
})(ClientConnState || (ClientConnState = {}));
|
|
21
|
-
export class WsClient {
|
|
22
|
-
constructor(connUrl, v) {
|
|
23
|
-
const C = new m();
|
|
24
|
-
const $ = this.l = new u(C, v);
|
|
25
|
-
this.I = ClientConnState.NotConnected;
|
|
26
|
-
this.a7 = connUrl;
|
|
27
|
-
this.a = new e(() => this.g());
|
|
28
|
-
$.L = this.L.bind(this);
|
|
29
|
-
$.H = this.H.bind(this);
|
|
30
|
-
}
|
|
31
|
-
getConnState() {
|
|
32
|
-
return this.I;
|
|
33
|
-
}
|
|
34
|
-
enableAuthenticate(proxyPrivatePkgType, authProvider) {
|
|
35
|
-
this.N = authProvider;
|
|
36
|
-
authProvider.init(proxyPrivatePkgType, this);
|
|
37
|
-
}
|
|
38
|
-
registerError(pkgType, wrapperCtor) {
|
|
39
|
-
return this.l.registerError(pkgType, wrapperCtor);
|
|
40
|
-
}
|
|
41
|
-
registerMessage(pkgType, wrapperCtor) {
|
|
42
|
-
return this.l.registerMessage(pkgType, wrapperCtor);
|
|
43
|
-
}
|
|
44
|
-
registerRequest(pkgType, wrapperCtor) {
|
|
45
|
-
return this.l.registerRequest(pkgType, wrapperCtor);
|
|
46
|
-
}
|
|
47
|
-
handleMessageWith(wrapperCtor, handler) {
|
|
48
|
-
const f = (a, d) => {
|
|
49
|
-
return handler(d.data);
|
|
50
|
-
};
|
|
51
|
-
this.l.handleMessageWith(new h(wrapperCtor, f, true));
|
|
52
|
-
}
|
|
53
|
-
handleRequestWith(wrapperCtor, handler) {
|
|
54
|
-
const f = async (a, d) => {
|
|
55
|
-
d.resp = await handler(d.data);
|
|
56
|
-
};
|
|
57
|
-
this.l.handleRequestWith(new i(wrapperCtor, f, true));
|
|
58
|
-
}
|
|
59
|
-
sendMessage(message) {
|
|
60
|
-
return this.a.sendMessage(message);
|
|
61
|
-
}
|
|
62
|
-
async sendRequest(request) {
|
|
63
|
-
await this.a.sendRequest(request);
|
|
64
|
-
return request.resp;
|
|
65
|
-
}
|
|
66
|
-
onConnectionStatus(listener) {
|
|
67
|
-
let { b } = this;
|
|
68
|
-
if (!b) {
|
|
69
|
-
this.b = b = [];
|
|
70
|
-
}
|
|
71
|
-
b.push(listener);
|
|
72
|
-
}
|
|
73
|
-
async g() {
|
|
74
|
-
const { l, N } = this;
|
|
75
|
-
this.I = ClientConnState.Connecting;
|
|
76
|
-
try {
|
|
77
|
-
const c = N ? await N.preConnect() : this.a7;
|
|
78
|
-
if (!c) {
|
|
79
|
-
throw new Error(NetErrorString.UnknownError);
|
|
80
|
-
}
|
|
81
|
-
const conn = await l.g(c);
|
|
82
|
-
return conn;
|
|
83
|
-
}
|
|
84
|
-
catch (err) {
|
|
85
|
-
const error = err instanceof Error ? err : new Error(NetErrorString.UnknownError);
|
|
86
|
-
this.H(l, this.a, true, error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
L(l, conn, M) {
|
|
90
|
-
this.I = ClientConnState.Connected;
|
|
91
|
-
conn.context = '';
|
|
92
|
-
if (this.b) {
|
|
93
|
-
for (const listener of this.b) {
|
|
94
|
-
listener(true, conn, M);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
H(l, conn, O, err) {
|
|
99
|
-
this.I = ClientConnState.NotConnected;
|
|
100
|
-
this.a.aj();
|
|
101
|
-
if (this.b) {
|
|
102
|
-
for (const listener of this.b) {
|
|
103
|
-
listener(false, conn, O, err);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
a7;
|
|
108
|
-
l;
|
|
109
|
-
a;
|
|
110
|
-
I;
|
|
111
|
-
N;
|
|
112
|
-
b;
|
|
113
|
-
}
|
|
114
|
-
//# sourceMappingURL=WsClient.js.map
|
package/lib/bundle.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
class t{constructor(t,e){e?this.x=t:this.y=t}get data(){return this.y||(this.y=this.decode(this.x)),this.y}set data(t){this.y=t,this.x=void 0}get buff(){return this.x||(this.x=this.encode(this.y)),this.x}x;y}class e extends t{static context;static registerPkgType(t){let e;try{e=this.pkgType}catch{return void Object.defineProperty(this,"pkgType",{value:t,writable:!1,enumerable:!1})}if(e!==t)throw new Error(`Wrapper ${this.name} is already registered with type ${e}`)}static get pkgType(){throw new Error(`Wrapper ${this.name} need to be registered`)}}class s extends e{isError(){return!0}}class 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.z||(this.z=this.decodeResponse(this.$)),this.z}set resp(t){this.z=t,this.$=void 0}get respBuff(){return this.$||(this.$=this.encodeResponse(this.z)),this.$}set respBuff(t){this.z=void 0,this.$=t}z;$}const o=new TextEncoder,h=new TextDecoder;class a extends s{encode(t){const e=JSON.stringify(t);return o.encode(e)}decode(t){const e=h.decode(t);return JSON.parse(e)}}class c extends 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._=new f}get e(){return this._.getNext()}get a(){return this._.getPrev()}V(t){return t===this._}W(t){t.insertAfter(this._)}am(t){t.insertBefore(this._)}_}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.X=t,this.J=e,this.F=s,this.p=0,this.G=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.G||(this.Y=setInterval(this.a8,this.X)),this.G++,this.r[this.p].W(t)}update(t){t.remove(),this.r[this.p].W(t)}remove(t){t.remove(),this.G--,this.G||clearInterval(this.Y)}a8=()=>{const{J:t,F:e,r: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.e;!r.V(t);t=t.getNext())t.warnTimeout()}const i=s[this.p];for(let t=i.e;!i.V(t);t=t.getNext())t.onTimeout()};X;J;F;p;G;Y;r}class b{constructor(t,e){this.a5=new Promise(t=>{this.u=t}),this.M=e,t>0?this.k=setTimeout(()=>{this.k=void 0,this.u(!0),e?.()},t):(this.u(!0),e?.())}ao(t){const{k:e,u:s,M: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?.())}ah(){this.k&&(clearTimeout(this.k),this.k=void 0,this.u(!1))}ap(){return this.a5}u;a5;k;M}class T extends c{}class E{constructor(t,e,s){let i=sessionStorage.getItem("jrSession");i||(i=crypto.randomUUID(),sessionStorage.setItem("jrSession",i)),this.c="",this.d=i,this.T=t,this.O="",this.C=e,this.f=s,this.P=this.P.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=E.prototype;this.authWithCredential=s.authWithCredential,this.authWithOAuth2=s.authWithOAuth2,this.resetAuthState=s.resetAuthState,this.resetConnState=s.resetConnState,e.onConnectionStatus(this.ak.bind(this)),e.registerMessage(t,T),e.handleMessageWith(T,this.al.bind(this));const{d:i,f:r}=this,n=this.U();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,C:r}=this;if(s)return i(t,e,s),"";const n=""===s;this.h=void 0;let o=this.U();if(o){const{g:t,b:s,i,j:h}=o,a=Math.round(Date.now()/1e3);if(a<=i)return this.O=o.b,r(s,e,n);if(a<=h&&(o=await this.Q("refresh",""),o))return this.O=o.b,r(o.b,e,n)}return this.h="unknown error",i("",""),""}async authWithCredential(t,e,s,i){await this.Q(t,`${e},${s},${i}`)}authWithOAuth2(t,e,s,i){const r=`${e}?client_id=${s}&redirect_uri=${this.T}&scope=${i}&response_type=code`;window.location.href=`${r}&state=${this.d}~${s}~${encodeURI(t)}`}async resetAuthState(){await this.Q("signout","")}resetConnState(t){if(this.h){this.h=t?"":void 0;const{c:e,d:s,f:i}=this;i(e,s)}}ak(t,e,s,i){if(t){this.a=e;const t=this.U();if(t){const{i:e}=t;this.D=new b(1e3*e-Date.now(),this.P)}}else if(this.a=void 0,this.D&&(this.D.ah(),this.D=void 0),!s){const t=this.h=i?.message||"unknown error",{c:e,d:s,f:r}=this;r(e,s,t)}}async P(){this.D=void 0;const{d:t,C:e}=this,s=await this.Q("refresh","");if(s&&this.a){const{b:i,i:r}=s;if(i===this.O){const s=e(i,t),[n,o]=s.split("://");await fetch(`${n.replace("ws","http")}://${o}`),this.D=new b(1e3*r-Date.now(),this.P)}else this.a&&(this.a.close(),this.a=void 0)}}async al(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)))}U(){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 Q(t,e){const s=await fetch(this.T,{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;T;O;a;h;D;C;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 q(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 q(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.E=t}async I(t){return!1}async J(){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.ai(e,!0))}})}ai=(t,e)=>{const s=new I(t),{conn:i,_:r,o:n}=this.E(s,e);return t.onmessage=t=>{t.data.arrayBuffer().then(t=>{r(new Uint8Array(t))})},t.onerror=t=>{n(new Error(p.UnknownError))},t.onclose=t=>{1e3===t.code?n():n(new Error(t.reason))},i};E(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 F(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))}G(){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)},R=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.a6=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.q&&await this.q.then(e=>{this.a=void 0,e.close(t)},()=>this.a=void 0)}async aq(){this.a||await this.g()}aj(){this.a=void 0}async g(){let{q:t}=this;return t||(t=this.q=new Promise((t,e)=>{this.a6().then(s=>{s?(t(s),this.a=s,this.q=void 0):e(new Error("Connect failed"))})})),await t}a6;a;q}class q{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.R=new Map,this.Z=r,this.a0=n,this.A=t,this.K=i,this.x=this.a9;const o=[];this.Z(s,2097152,o),this.a1={makePkgBuff:r,responsePkgType:s,r:k(o)},this.h.set(s,this),this.A.init((t,e)=>{do{this.s++}while(this.t.has(2097151&this.s));const s=new W(this.a1,t,2097151&this.s);this.t.set(s.id,s),this.K?.insert(s);const i={conn:s,_:t=>this.aa(s,t),o:t=>this.ab(s,t)};return this.L?.(this,s,!!e),i})}responsePkgType;context;L;H;b(){return this.A.b()}P(t){return this.t.get(t)}g(t){return this.A.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 T(t){return this.A.I(t)}async U(){return this.A.J()}ac(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.q(t,e);throw new Error(p.InvalidPackage)}ad(t,e){const s=e.constructor.pkgType,i=this.h.get(s);if(i)return i?.q(t,e);throw new Error(p.InvalidPackage)}registerError(t,e){e.registerPkgType(t),this.R.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}an(t){const e=t.pkgType;return this.h.get(e)===t&&(this.h.delete(e),!0)}aa(t,e){this.K?.update(t);const s=this.a0(e),i=this.h.get(s.pkgType);i?i.x(t,s):t.close(new Error(p.InvalidPackage))}ab(t,e){const s=t.id;this.t.delete(s),this.K?.remove(t),t.o(e),this.H?.(this,t,t.e.G(),e)}a9(t,e){try{const{buff:s,pkgType:i,requestId:r,bodyOffset:n}=e;if(i===this.responsePkgType){const e=s.subarray(n);t.a2(r,e)}else{const e=this.R.get(i);if(e){const i=new e(s.subarray(n),!0);t.ae(r,i)}}}catch{t.close(new Error(p.InvalidPackage))}}a1;s;Z;a0;A;t;h;R;K}class W extends v{constructor(t,e,s){super(),this.id=s,this.e=e,this.B=t,this.a3=0}context;id;e;get socket(){return this.e.socket}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.B.makePkgBuff(e,0,s),this.e.n(s)}sendRequest(t){return this.m=new Map,this.sendRequest=this.a4,this.a2=this.af,this.o=this.ag,this.a4(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,i=[t.buff];return this.B.makePkgBuff(s,e,i),this.e.n(i)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:i,responsePkgType:r}=this.B;return i(r,e,s),this.e.n(s)}async close(t){await this.e.close(t)}a2(t,e){2097152===t&&void 0===this.context&&this.close(new Error(p.AuthTimeout))}ae(t,e){const s=this.m.get(t);if(s)return this.m.delete(t),s.j(e)}warnTimeout(){this.e.F(this.B.r).catch(y)}onTimeout(){this.close(new Error(p.Timeout))}o(t){}async a4(t){const e=2097151&this.a3++,s=t.constructor.pkgType,i=[t.buff];this.B.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}af(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))}ag(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}B;m;a3}class j{constructor(t,e){this.id=t,this.S=e}context;id;socket;get remoteFamily(){return"Local"}async sendMessage(t){return this.S.ac(this,t)}async sendRequest(t){return this.S.ad(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}S}const M=(t,e)=>new N(t,{responsePkgType:0,makePkgBuff:S,parsePkgBuff:R,timeoutMonitor:e});var B;!function(t){t[t.NotConnected=0]="NotConnected",t[t.Connecting=1]="Connecting",t[t.Connected=2]="Connected"}(B||(B={}));class N{constructor(t,e){const s=new A,i=this.l=new q(s,e);this.I=B.NotConnected,this.a7=t,this.a=new $(()=>this.g()),i.L=this.L.bind(this),i.H=this.H.bind(this)}getConnState(){return this.I}enableAuthenticate(t,e){this.N=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,N:e}=this;this.I=B.Connecting;try{const s=e?await e.preConnect():this.a7;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.H(t,this.a,!0,s)}}L(t,e,s){if(this.I=B.Connected,e.context="",this.b)for(const t of this.b)t(!0,e,s)}H(t,e,s,i){if(this.I=B.NotConnected,this.a.aj(),this.b)for(const t of this.b)t(!1,e,s,i)}a7;l;a;I;N;b}export{u as CommonError,s as ErrorWrapper,r as EventWrapper,a as JsonError,c as JsonMessage,d as JsonRequest,E as JustrunAuthProvider,i as MessageWrapper,p as NetErrorString,n as RequestWrapper,m as TimeoutMonitor,N as WsClient,M as createNumPkgTypeClient,g as handleUncaughtErrorWith};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export class m {
|
|
2
|
-
constructor(a, onTimeout) {
|
|
3
|
-
this.a5 = new Promise((d) => {
|
|
4
|
-
this.u = d;
|
|
5
|
-
});
|
|
6
|
-
this.M = onTimeout;
|
|
7
|
-
if (a > 0) {
|
|
8
|
-
this.k = setTimeout(() => {
|
|
9
|
-
this.k = undefined;
|
|
10
|
-
this.u(true);
|
|
11
|
-
onTimeout?.();
|
|
12
|
-
}, a);
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
this.u(true);
|
|
16
|
-
onTimeout?.();
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
ao(a) {
|
|
20
|
-
const { k, u, M } = this;
|
|
21
|
-
if (k) {
|
|
22
|
-
clearTimeout(k);
|
|
23
|
-
}
|
|
24
|
-
if (a > 0) {
|
|
25
|
-
this.k = setTimeout(() => {
|
|
26
|
-
this.k = undefined;
|
|
27
|
-
M?.();
|
|
28
|
-
u(true);
|
|
29
|
-
}, a);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
this.k = undefined;
|
|
33
|
-
u(true);
|
|
34
|
-
M?.();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
ah() {
|
|
38
|
-
if (this.k) {
|
|
39
|
-
clearTimeout(this.k);
|
|
40
|
-
this.k = undefined;
|
|
41
|
-
this.u(false);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
ap() {
|
|
45
|
-
return this.a5;
|
|
46
|
-
}
|
|
47
|
-
u;
|
|
48
|
-
a5;
|
|
49
|
-
k;
|
|
50
|
-
M;
|
|
51
|
-
}
|
|
52
|
-
//# sourceMappingURL=AdjustableTimer.js.map
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export class DataWrapper {
|
|
2
|
-
constructor(a, needParse) {
|
|
3
|
-
if (needParse) {
|
|
4
|
-
this.x = a;
|
|
5
|
-
}
|
|
6
|
-
else {
|
|
7
|
-
this.y = a;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
get data() {
|
|
11
|
-
if (!this.y) {
|
|
12
|
-
this.y = this.decode(this.x);
|
|
13
|
-
}
|
|
14
|
-
return this.y;
|
|
15
|
-
}
|
|
16
|
-
set data(b) {
|
|
17
|
-
this.y = b;
|
|
18
|
-
this.x = undefined;
|
|
19
|
-
}
|
|
20
|
-
get buff() {
|
|
21
|
-
if (!this.x) {
|
|
22
|
-
this.x = this.encode(this.y);
|
|
23
|
-
}
|
|
24
|
-
return this.x;
|
|
25
|
-
}
|
|
26
|
-
x;
|
|
27
|
-
y;
|
|
28
|
-
}
|
|
29
|
-
//# sourceMappingURL=DataWrapper.js.map
|