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