justrun-ws 0.3.1 → 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 +51 -42
- 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
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;
|
|
@@ -36,7 +36,7 @@ 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
42
|
set resp(val: ResponseT);
|
|
@@ -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
|
|
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
|
|
77
|
-
private
|
|
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;
|
|
@@ -155,38 +163,39 @@ export declare class WsClient<PkgTypeT extends number | string> {
|
|
|
155
163
|
sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
|
|
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
|
-
private
|
|
159
|
-
private
|
|
160
|
-
private
|
|
161
|
-
private
|
|
162
|
-
private
|
|
163
|
-
private
|
|
164
|
-
private
|
|
165
|
-
private
|
|
166
|
-
private
|
|
166
|
+
private g;
|
|
167
|
+
private O;
|
|
168
|
+
private K;
|
|
169
|
+
private ab;
|
|
170
|
+
private m;
|
|
171
|
+
private a;
|
|
172
|
+
private L;
|
|
173
|
+
private Q?;
|
|
174
|
+
private b?;
|
|
167
175
|
}
|
|
168
176
|
export declare class JustrunAuthProvider implements IAuthProvider {
|
|
169
|
-
constructor(
|
|
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
|
|
181
|
-
private
|
|
182
|
-
private
|
|
183
|
-
private
|
|
184
|
-
private
|
|
185
|
-
private
|
|
186
|
-
private
|
|
187
|
-
private
|
|
188
|
-
private
|
|
189
|
-
private
|
|
184
|
+
private ao;
|
|
185
|
+
private S;
|
|
186
|
+
private ap;
|
|
187
|
+
private X;
|
|
188
|
+
private T;
|
|
189
|
+
private c;
|
|
190
|
+
private d;
|
|
191
|
+
private W;
|
|
192
|
+
private R;
|
|
193
|
+
private z;
|
|
194
|
+
private a?;
|
|
195
|
+
private h?;
|
|
196
|
+
private G?;
|
|
197
|
+
private F;
|
|
198
|
+
private f;
|
|
190
199
|
}
|
|
191
200
|
|
|
192
201
|
export {};
|
|
@@ -0,0 +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};
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "justrun-ws",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
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
|