justrun-ws 0.2.0 → 0.2.1
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/WsClient.js +63 -50
- package/lib/bundle.min.js +1 -1
- package/lib/common/AdjustableTimer.js +19 -19
- package/lib/common/DataWrapper.js +12 -14
- package/lib/common/List.js +12 -12
- package/lib/common/utils.js +5 -5
- package/lib/handler/SimpleHandler.js +23 -21
- package/lib/handler/common.js +0 -1
- package/lib/index.d.ts +12 -0
- package/lib/index.js +1 -0
- package/lib/net/BrowserWsNet.js +29 -28
- package/lib/net/LazyConn.js +5 -5
- package/lib/net/NetErrorString.js +14 -0
- package/lib/net/TimeoutMonitor.js +28 -28
- package/lib/net/TypedPkgHub.js +123 -128
- package/lib/net/hubPkgSerializer.js +2 -2
- package/package.json +1 -1
package/lib/WsClient.js
CHANGED
|
@@ -1,54 +1,55 @@
|
|
|
1
1
|
import { b } from './common/AdjustableTimer';
|
|
2
2
|
import { CommonError } from './handler/common';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { n, o } from './handler/SimpleHandler';
|
|
4
|
+
import { r } from './net/BrowserWsNet';
|
|
5
|
+
import { p, s } from './net/hubPkgSerializer';
|
|
6
6
|
import { JsonRequest } from './net/JsonPackageWrapper';
|
|
7
7
|
import { g } from './net/LazyConn';
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import { NetErrorString } from './net/NetErrorString';
|
|
9
|
+
import { v } from './net/TypedPkgHub';
|
|
10
|
+
class l extends JsonRequest {
|
|
10
11
|
}
|
|
11
12
|
export const createNumPkgTypeClient = (getConnStr, timeoutMonitor) => {
|
|
12
13
|
return new WsClient(getConnStr, {
|
|
13
14
|
responsePkgType: 0,
|
|
14
|
-
makePkgBuff:
|
|
15
|
-
parsePkgBuff:
|
|
15
|
+
makePkgBuff: p,
|
|
16
|
+
parsePkgBuff: s,
|
|
16
17
|
timeoutMonitor,
|
|
17
18
|
});
|
|
18
19
|
};
|
|
19
20
|
export class WsClient {
|
|
20
21
|
constructor(getConnStr, w) {
|
|
21
|
-
const
|
|
22
|
-
const _ = this.
|
|
22
|
+
const F = new r();
|
|
23
|
+
const _ = this.d = new v(F, w);
|
|
23
24
|
this.h = '';
|
|
24
|
-
this.
|
|
25
|
+
this.j = [];
|
|
25
26
|
this.q = 0;
|
|
26
27
|
this.a = new g(() => this.c());
|
|
27
|
-
this.
|
|
28
|
+
this.H = getConnStr;
|
|
28
29
|
this.O = this.c;
|
|
29
|
-
_.
|
|
30
|
-
_.
|
|
30
|
+
_.K = this.K.bind(this);
|
|
31
|
+
_.B = this.B.bind(this);
|
|
31
32
|
}
|
|
32
33
|
registerError(pkgType, wrapperCtor) {
|
|
33
|
-
return this.
|
|
34
|
+
return this.d.registerError(pkgType, wrapperCtor);
|
|
34
35
|
}
|
|
35
36
|
registerMessage(pkgType, wrapperCtor) {
|
|
36
|
-
return this.
|
|
37
|
+
return this.d.registerMessage(pkgType, wrapperCtor);
|
|
37
38
|
}
|
|
38
39
|
registerRequest(pkgType, wrapperCtor) {
|
|
39
|
-
return this.
|
|
40
|
+
return this.d.registerRequest(pkgType, wrapperCtor);
|
|
40
41
|
}
|
|
41
42
|
handleMessageWith(wrapperCtor, handler) {
|
|
42
|
-
const i = (a,
|
|
43
|
-
return handler(
|
|
43
|
+
const i = (a, f) => {
|
|
44
|
+
return handler(f.data);
|
|
44
45
|
};
|
|
45
|
-
this.
|
|
46
|
+
this.d.handleMessageWith(new n(wrapperCtor, i, false));
|
|
46
47
|
}
|
|
47
48
|
handleRequestWith(wrapperCtor, handler) {
|
|
48
|
-
const i = async (a,
|
|
49
|
-
|
|
49
|
+
const i = async (a, f) => {
|
|
50
|
+
f.resp = await handler(f.data);
|
|
50
51
|
};
|
|
51
|
-
this.
|
|
52
|
+
this.d.handleRequestWith(new o(wrapperCtor, i, false));
|
|
52
53
|
}
|
|
53
54
|
sendMessage(message) {
|
|
54
55
|
return this.a.sendMessage(message);
|
|
@@ -63,21 +64,28 @@ export class WsClient {
|
|
|
63
64
|
resetAuthState() {
|
|
64
65
|
}
|
|
65
66
|
enableAuthenticate(proxyAuthPkgType, authStatusCallback, allowAnonymous) {
|
|
66
|
-
const { O,
|
|
67
|
+
const { O, H, d, a } = this;
|
|
67
68
|
if (this.c === O) {
|
|
68
|
-
|
|
69
|
+
d.registerRequest(proxyAuthPkgType, l);
|
|
69
70
|
let token = '';
|
|
70
71
|
let shard = undefined;
|
|
71
72
|
this.c = async () => {
|
|
72
73
|
this.q = -1;
|
|
73
74
|
if (this.m) {
|
|
74
|
-
await this.m.
|
|
75
|
+
await this.m.Y();
|
|
75
76
|
}
|
|
76
77
|
for (;;) {
|
|
77
|
-
const
|
|
78
|
-
|
|
78
|
+
const e = H(shard);
|
|
79
|
+
let conn;
|
|
80
|
+
try {
|
|
81
|
+
conn = await d.c(e);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
this.B(d, this.a, true, new Error(NetErrorString.k));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
79
87
|
if (this.h || allowAnonymous) {
|
|
80
|
-
const request = new
|
|
88
|
+
const request = new l({
|
|
81
89
|
cid: this.h,
|
|
82
90
|
token,
|
|
83
91
|
});
|
|
@@ -140,7 +148,7 @@ export class WsClient {
|
|
|
140
148
|
return;
|
|
141
149
|
}
|
|
142
150
|
if (this.m) {
|
|
143
|
-
this.m.
|
|
151
|
+
this.m.a3(delay ?? 0);
|
|
144
152
|
}
|
|
145
153
|
else if (delay) {
|
|
146
154
|
this.m = new b(delay, () => this.m = undefined);
|
|
@@ -150,49 +158,54 @@ export class WsClient {
|
|
|
150
158
|
}
|
|
151
159
|
}
|
|
152
160
|
onConnectionStatus(listener) {
|
|
153
|
-
let {
|
|
154
|
-
if (!
|
|
155
|
-
this.
|
|
161
|
+
let { j } = this;
|
|
162
|
+
if (!j) {
|
|
163
|
+
this.j = j = [];
|
|
156
164
|
}
|
|
157
|
-
|
|
165
|
+
j.push(listener);
|
|
158
166
|
}
|
|
159
167
|
async c() {
|
|
160
|
-
const {
|
|
168
|
+
const { H, d, m } = this;
|
|
161
169
|
this.q = -1;
|
|
162
170
|
if (m) {
|
|
163
|
-
await m.
|
|
171
|
+
await m.Y();
|
|
172
|
+
}
|
|
173
|
+
const e = H();
|
|
174
|
+
try {
|
|
175
|
+
return d.c(e);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
this.B(d, this.a, true, new Error(NetErrorString.k));
|
|
164
179
|
}
|
|
165
|
-
const d = I();
|
|
166
|
-
return f.c(d);
|
|
167
180
|
}
|
|
168
|
-
|
|
181
|
+
K(d, conn, W) {
|
|
169
182
|
this.q = 1;
|
|
170
183
|
conn.context = '';
|
|
171
|
-
if (this.
|
|
172
|
-
for (const listener of this.
|
|
173
|
-
listener(true,
|
|
184
|
+
if (this.j) {
|
|
185
|
+
for (const listener of this.j) {
|
|
186
|
+
listener(true, W);
|
|
174
187
|
}
|
|
175
188
|
}
|
|
176
189
|
}
|
|
177
|
-
|
|
190
|
+
B(d, a, G, err) {
|
|
178
191
|
this.q = 0;
|
|
179
|
-
this.a.
|
|
180
|
-
if (!
|
|
192
|
+
this.a.a5();
|
|
193
|
+
if (!G) {
|
|
181
194
|
this.resetAuthState();
|
|
182
195
|
}
|
|
183
|
-
if (this.
|
|
184
|
-
for (const listener of this.
|
|
185
|
-
listener(false,
|
|
196
|
+
if (this.j) {
|
|
197
|
+
for (const listener of this.j) {
|
|
198
|
+
listener(false, G, err);
|
|
186
199
|
}
|
|
187
200
|
}
|
|
188
201
|
}
|
|
189
|
-
|
|
202
|
+
d;
|
|
190
203
|
a;
|
|
191
204
|
h;
|
|
192
205
|
q;
|
|
193
|
-
|
|
206
|
+
H;
|
|
194
207
|
O;
|
|
195
208
|
m;
|
|
196
|
-
|
|
209
|
+
j;
|
|
197
210
|
}
|
|
198
211
|
//# sourceMappingURL=WsClient.js.map
|
package/lib/bundle.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{EK:()=>p,sr:()=>i,si:()=>o,N3:()=>d,sk:()=>u,wL:()=>l,bM:()=>n,h8:()=>h,Yu:()=>E,I9:()=>I,iU:()=>$,mx:()=>y});class s{constructor(t,e){e?(this.r=t,this.s=void 0):(this.r=void 0,this.s=t)}get data(){return this.s||(this.s=this.decode(this.r)),this.s}set data(t){this.s=t,this.r=void 0}get buff(){return this.r||(this.r=this.encode(this.s)),this.r}r;s}class r extends s{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 i extends r{isError(){return!0}}class n extends r{isMessage(){return!0}}class o extends r{isEvent(){return!0}}class h extends r{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 a=new TextEncoder,c=new TextDecoder;class d extends i{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}}class u extends n{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}}class l extends h{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}encodeResponse(t){const e=JSON.stringify(t);return a.encode(e)}decodeResponse(t){const e=c.decode(t);return JSON.parse(e)}}class p extends d{}const g=new Error("Connection is not authenticated");let f=(t,e)=>new p(e.message);const y=t=>{f=t};class w{constructor(){this.d=this,this.i=this}getPrev(){return this.d}getNext(){return this.i}insertBefore(t){this.i=t,this.d=t.d,t.d=this,this.d.i=this}insertAfter(t){this.d=t,this.i=t.i,t.i=this,this.i.d=this}remove(){this.d.i=this.i,this.i.d=this.d}d;i}class m{constructor(){this._=new w}get e(){return this._.getNext()}get a(){return this._.getPrev()}P(t){return t===this._}Q(t){t.insertAfter(this._)}ab(t){t.insertBefore(this._)}_}const k=()=>{},v=t=>{let e=0,s=0;for(;e<t.length;)s+=t[e++].length;const r=new Uint8Array(s);for(s=0;e>0;){const i=t[--e];r.set(i,s),s+=i.length}return r};class b extends w{}class E{constructor(t,e,s){if(this.R=t,this.J=e,this.D=s,this.o=0,this.E=0,this.t=[],0===t)return this.insert=k,this.update=k,void(this.remove=k);for(let t=0;t<s;t++)this.t.push(new m)}insert(t){this.E||(this.S=setInterval(this.a2,this.R)),this.E++,this.t[this.o].Q(t)}update(t){t.remove(),this.t[this.o].Q(t)}remove(t){t.remove(),this.E--,this.E||clearInterval(this.S)}a2=()=>{const{J:t,D:e,t:s}=this;if(this.o++,this.o===e&&(this.o=0),t){let r=this.o+t;r>=e&&(r-=e);const i=s[r];for(let t=i.e;!i.P(t);t=t.getNext())t.keepAlive()}const r=s[this.o];for(let t=r.e;!r.P(t);t=t.getNext())t.timeout()};R;J;D;o;E;S;t}class P{constructor(t,e){this.Z=new Promise((t=>{this.H=t})),this.N=e,t>0?this.w=setTimeout((()=>{this.w=void 0,this.N?.(),this.H()}),t):this.H()}a6(t){this.w&&clearTimeout(this.w),t>0?this.w=setTimeout((()=>{this.w=void 0,this.N?.(),this.H()}),t):(this.w=void 0,this.H())}async a0(){await this.Z}H;Z;w;N}class x{constructor(t,e,s=!0){this.l=t,this.x=e,this.y=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async $(t,e){const{buff:s,bodyOffset:r}=e,i=s.subarray(r),n=new this.l(i,!0);if(void 0===t.context&&this.y)f(t,g);else try{await this.x(t,n)}catch(e){e.isError||f(t,e)}void 0===t.context&&await t.close()}async r(t,e){if(void 0===t.context&&this.y)f(t,g);else try{return await this.x(t,e)}catch(e){e.isError||f(t,e)}}l;x;y}class T{constructor(t,e,s=!0){this.l=t,this.x=e,this.y=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async $(t,e){const{buff:s,requestId:r,bodyOffset:i}=e,n=s.subarray(i),o=new this.l(n,!0);let h;if(void 0===t.context&&this.y)h=f(t,g);else try{await this.x(t,o)}catch(e){h=e.isError?e:f(t,e)}try{h?await t.sendErrorResponse(h,r):await t.sendResponse(o,r),void 0===t.context&&await t.close()}catch(e){await t.close(e)}}async r(t,e){if(void 0!==t.context||!this.y)try{return await this.x(t,e)}catch(e){throw e.isError?e:f(t,e)}throw f(t,g)}l;x;y}class R{o(){return null}N(t){this.C=t}async O(t){return!1}async P(){return!1}async c(t){const e=new WebSocket(t);return new Promise(((t,s)=>{e.onerror=t=>{s(new Error("WebSocket error"))},e.onopen=()=>{t(this.a9(e,!0))}}))}a9=(t,e)=>{const s=new A(t),{conn:r,D:i,n}=this.C(s,e);return t.onmessage=t=>{t.data.arrayBuffer().then((t=>{i(new Uint8Array(t))}))},t.onerror=t=>{n(new Error("WebSocket error"))},t.onclose=t=>{1e3===t.code?n():n(new Error(t.reason))},r};C(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class A{constructor(t){this.g=t}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async I(t){if(this.g)return this.g.send(t);throw new Error("Invalid socket")}async p(t){if(this.g){const e=v(t);return this.g.send(e)}throw new Error("Invalid socket")}async close(t){const{g:e}=this;e&&(this.g=void 0,t?e.close(3e3,t.message):e.readyState===WebSocket.OPEN&&e.close(1e3))}J(){return!this.g}g}const q=(t,e,s)=>{const r=[e,t],i=(t=>{let e,s=0,r=0;for(;s<t.length;)for(r++,e=t[s++];e>>>=7;)r++;return r})(r),n=new Uint8Array(i);((t,e,s=0)=>{let r,i,n=s;for(r=0;r<t.length;r++){i=t[r];let s=127&i;for(;i>>>=7;)e[n++]=128|s,s=127&i;e[n++]=s}if(n<=e.length)return n-s;throw new Error("Buffer overflow")})(r,n),s.push(n)},M=t=>{const{h:[e,s],k:r}=((t,e,s=0)=>{let r,i,n=s,o=0,h=0;const a=[];for(r=0;r<t;r++){do{i=e[n++],o|=(127&i)<<h,h+=7}while(128&i);if(!(void 0!==i&&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:r}};class W{constructor(t){this.a1=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.c()).sendErrorResponse(t,e)}async sendResponse(t,e){return(this.a??await this.c()).sendResponse(t,e)}async sendMessage(t){return(this.a??await this.c()).sendMessage(t)}async sendRequest(t){return(this.a??await this.c()).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 reconnect(){this.a||await this.c()}aa(){this.a=void 0}async c(){let{p:t}=this;return t||(t=this.p=new Promise(((t,e)=>{this.a1().then((s=>{s?(t(s),this.a=s,this.p=void 0):e(new Error("Connect failed"))}))}))),await t}a1;a;p}class B{constructor(t,e){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:i,parsePkgBuff:n}=e;this.responsePkgType=s,this.context=void 0,this.F=void 0,this.G=void 0,this.u=0,this.v=new Map,this.e=new Map,this.L=new Map,this.T=i,this.U=n,this.A=t,this.K=r,this.$=this.a3;const o=[];this.T(s,2097152,o),this.V={makePkgBuff:i,responsePkgType:s,s:v(o)},this.e.set(s,this),this.A.N(((t,e)=>{do{this.u++}while(this.v.has(2097151&this.u));const s=new N(this.V,t,2097151&this.u);this.v.set(s.id,s),this.K?.insert(s);const r={conn:s,D:t=>this.a4(s,t),n:t=>this.a5(s,t)};return this.F?.(this,s,!!e),r}))}responsePkgType;context;F;G;o(){return this.A.o()}Z(t){return this.v.get(t)}c(t){return this.A.c(t)}async a0(){do{this.u++}while(this.v.has(2097151&this.u));const t=new O(2097151&this.u,this);return this.v.set(t.id,t),t}async a1(t){return this.A.O(t)}async a2(){return this.A.P()}a7(t,e){const s=e.constructor.pkgType,r=this.e.get(s);if(r)return r?.r(t,e);throw new Error("Invalid package")}a8(t,e){const s=e.constructor.pkgType,r=this.e.get(s);if(r)return r?.r(t,e);throw new Error("Invalid package")}registerError(t,e){e.registerPkgType(t),this.L.set(t,e);const s=this.e.get(this.responsePkgType);this.e.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.m} has an incompatible pkgType ${typeof e}`);if(this.e.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.e.set(e,t),t}handleRequestWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.m} has an incompatible pkgType ${typeof e}`);if(this.e.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.e.set(e,t),t}ak(t){const e=t.pkgType;return this.e.get(e)===t&&(this.e.delete(e),!0)}a4(t,e){this.K?.update(t);const s=this.U(e),r=this.e.get(s.pkgType);r?r.$(t,s):t.close(new Error("Invalid package"))}a5(t,e){const s=t.id;this.v.delete(s),this.K?.remove(t),t.n(e),this.G?.(this,t,t.b.J(),e)}a3(t,e){const{buff:s,pkgType:r,requestId:i,bodyOffset:n}=e;if(r===this.responsePkgType){const e=s.subarray(n);t.W(i,e)}else{const e=this.L.get(r);if(e){const r=new e(s.subarray(n),!0);t.ac(i,r)}}}V;u;T;U;A;v;e;L;K}class N extends b{constructor(t,e,s){super(),this.context=void 0,this.id=s,this.b=e,this.B=t,this.j=void 0,this.X=0}context;id;b;get localAddress(){return this.b.localAddress}get localPort(){return this.b.localPort}get remoteAddress(){return this.b.remoteAddress}get remoteFamily(){return this.b.remoteFamily}get remotePort(){return this.b.remotePort}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.B.makePkgBuff(e,0,s),this.b.p(s)}sendRequest(t){return this.j=new Map,this.sendRequest=this.Y,this.W=this.ad,this.n=this.ae,this.Y(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,r=[t.buff];return this.B.makePkgBuff(s,e,r),this.b.p(r)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:r,responsePkgType:i}=this.B;return r(i,e,s),this.b.p(s)}async close(t){await this.b.close(t)}W(t,e){2097152===t&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}ac(t,e){const s=this.j.get(t);if(s)return this.j.delete(t),s.i(e)}keepAlive(){this.b.I(this.B.s).catch(k)}timeout(){this.close(new Error("Connection timeout"))}n(t){}async Y(t){const e=2097151&this.X++,s=t.constructor.pkgType,r=[t.buff];this.B.makePkgBuff(s,e,r);const i=new Promise(((t,s)=>{const i={f:t,i:s};this.j.set(e,i),this.b.p(r).catch((t=>{this.j.delete(e),s(t)}))}));t.respBuff=await i}ad(t,e){const s=this.j.get(t);if(s)return this.j.delete(t),void s.f(e);2097152===t&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}ae(t){const e=this.j;if(e.size>0){t||(t=new Error("Connection is closed"));for(const[s,r]of e)e.delete(s),r.i(t)}this.j=void 0}B;j;X}class O{constructor(t,e){this.context=void 0,this.id=t,this.M=e}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(t){return this.M.a7(this,t)}async sendRequest(t){return this.M.a8(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}M}class S extends l{}const $=(t,e)=>new I(t,{responsePkgType:0,makePkgBuff:q,parsePkgBuff:M,timeoutMonitor:e});class I{constructor(t,e){const s=new R,r=this.f=new B(s,e);this.h="",this.k=[],this.q=0,this.a=new W((()=>this.c())),this.I=t,this.O=this.c,r.F=this.F.bind(this),r.G=this.G.bind(this)}registerError(t,e){return this.f.registerError(t,e)}registerMessage(t,e){return this.f.registerMessage(t,e)}registerRequest(t,e){return this.f.registerRequest(t,e)}handleMessageWith(t,e){this.f.handleMessageWith(new x(t,((t,s)=>e(s.data)),!1))}handleRequestWith(t,e){this.f.handleRequestWith(new T(t,(async(t,s)=>{s.resp=await e(s.data)}),!1))}sendMessage(t){return this.a.sendMessage(t)}async sendRequest(t){return await this.a.sendRequest(t),t.resp}async sendCustomAuthRequest(t){throw new Error('Need to call "enableAuthenticate" before using sendCustomAuthRequest')}resetAuthState(){}enableAuthenticate(t,e,s){const{O:r,I:i,f:n,a:o}=this;if(this.c===r){n.registerRequest(t,S);let r,h="";this.c=async()=>{for(this.q=-1,this.m&&await this.m.a0();;){const t=i(r),o=await n.c(t);if(this.h||s){const t=new S({cid:this.h,token:h});try{await o.sendRequest(t);const{resp:s}=t;if(this.h=s.cid,h=s.token,r=s.shard,r&&r<0){r=-r-1,o.close();continue}e(this.h)}catch(t){return t instanceof p&&"HitSessionLimit"===t.data&&(this.h="",h="",r=void 0,e(this.h)),void o.close(t)}}return o}},this.sendCustomAuthRequest=async t=>{await o.sendRequest(t);const{resp:s}=t;return this.h=s.cid,h=s.token,r=s.shard,r&&r<0&&(r=-r-1,o.close()),e(this.h),s},this.resetAuthState=()=>{this.h&&(this.h="",h="",r=void 0,o.close(),e(this.h))}}}reconnect(t){const{a:e,q:s}=this;s>0||(this.m?this.m.a6(t??0):t&&(this.m=new P(t,(()=>this.m=void 0))),s||e.reconnect())}onConnectionStatus(t){let{k:e}=this;e||(this.k=e=[]),e.push(t)}async c(){const{I:t,f:e,m:s}=this;this.q=-1,s&&await s.a0();const r=t();return e.c(r)}F(t,e,s){if(this.q=1,e.context="",this.k)for(const t of this.k)t(!0,s)}G(t,e,s,r){if(this.q=0,this.a.aa(),s||this.resetAuthState(),this.k)for(const t of this.k)t(!1,s,r)}f;a;h;q;I;O;m;k}var J=e.EK,C=e.sr,j=e.si,F=e.N3,U=e.sk,z=e.wL,L=e.bM,H=e.h8,K=e.Yu,_=e.I9,D=e.iU,G=e.mx;export{J as CommonError,C as ErrorWrapper,j as EventWrapper,F as JsonError,U as JsonMessage,z as JsonRequest,L as MessageWrapper,H as RequestWrapper,K as TimeoutMonitor,_ as WsClient,D as createNumPkgTypeClient,G as handleUncaughtErrorWith};
|
|
1
|
+
var t={d:(e,s)=>{for(var r in s)t.o(s,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:s[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{EK:()=>p,sr:()=>i,si:()=>o,N3:()=>u,sk:()=>d,wL:()=>l,bM:()=>n,Lm:()=>y,h8:()=>h,Yu:()=>b,I9:()=>I,iU:()=>$,mx:()=>f});class s{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 r extends s{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 i extends r{isError(){return!0}}class n extends r{isMessage(){return!0}}class o extends r{isEvent(){return!0}}class h extends r{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 a=new TextEncoder,c=new TextDecoder;class u extends i{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}}class d extends n{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}}class l extends h{encode(t){const e=JSON.stringify(t);return a.encode(e)}decode(t){const e=c.decode(t);return JSON.parse(e)}encodeResponse(t){const e=JSON.stringify(t);return a.encode(e)}decodeResponse(t){const e=c.decode(t);return JSON.parse(e)}}class p extends u{}let g=(t,e)=>new p(e.message);const f=t=>{g=t};var y;!function(t){t.I="AuthTimeout",t.a="BadRequest",t.k="ConnectFailed",t.a1="ConnectionClosed",t.b="InvalidConnString",t.l="InvalidConnection",t.D="InvalidPackage",t.g="NotAuthenticated",t.a2="Timeout",t.m="UnknownError"}(y||(y={}));class w{constructor(){this.e=this,this.i=this}getPrev(){return this.e}getNext(){return this.i}insertBefore(t){this.i=t,this.e=t.e,t.e=this,this.e.i=this}insertAfter(t){this.e=t,this.i=t.i,t.i=this,this.i.e=this}remove(){this.e.i=this.i,this.i.e=this.e}e;i}class m{constructor(){this._=new w}get f(){return this._.getNext()}get a(){return this._.getPrev()}P(t){return t===this._}Q(t){t.insertAfter(this._)}a6(t){t.insertBefore(this._)}_}const k=()=>{},v=t=>{let e=0,s=0;for(;e<t.length;)s+=t[e++].length;const r=new Uint8Array(s);for(s=0;e>0;){const i=t[--e];r.set(i,s),s+=i.length}return r};class E extends w{}class b{constructor(t,e,s){if(this.R=t,this.I=e,this.E=s,this.o=0,this.F=0,this.r=[],0===t)return this.insert=k,this.update=k,void(this.remove=k);for(let t=0;t<s;t++)this.r.push(new m)}insert(t){this.F||(this.S=setInterval(this.a2,this.R)),this.F++,this.r[this.o].Q(t)}update(t){t.remove(),this.r[this.o].Q(t)}remove(t){t.remove(),this.F--,this.F||clearInterval(this.S)}a2=()=>{const{I:t,E:e,r:s}=this;if(this.o++,this.o===e&&(this.o=0),t){let r=this.o+t;r>=e&&(r-=e);const i=s[r];for(let t=i.f;!i.P(t);t=t.getNext())t.keepAlive()}const r=s[this.o];for(let t=r.f;!r.P(t);t=t.getNext())t.timeout()};R;I;E;o;F;S;r}class P{constructor(t,e){this.W=new Promise((t=>{this.G=t})),this.N=e,t>0?this.u=setTimeout((()=>{this.u=void 0,this.N?.(),this.G()}),t):this.G()}a3(t){this.u&&clearTimeout(this.u),t>0?this.u=setTimeout((()=>{this.u=void 0,this.N?.(),this.G()}),t):(this.u=void 0,this.G())}async Y(){await this.W}G;W;u;N}const T=new Error(y.g);class x{constructor(t,e,s=!0){this.l=t,this.v=e,this.w=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async $(t,e){const{buff:s,bodyOffset:r}=e,i=s.subarray(r),n=new this.l(i,!0);if(void 0===t.context&&this.w)g(t,T);else try{await this.v(t,n)}catch(e){e.isError||g(t,e)}void 0===t.context&&await t.close()}async r(t,e){if(void 0===t.context&&this.w)g(t,T);else try{return await this.v(t,e)}catch(e){e.isError||g(t,e)}}l;v;w}class R{constructor(t,e,s=!0){this.l=t,this.v=e,this.w=s}get pkgType(){return this.l.pkgType}get m(){return this.l.name}async $(t,e){const{buff:s,requestId:r,bodyOffset:i}=e,n=s.subarray(i),o=new this.l(n,!0);let h;if(void 0===t.context&&this.w)h=g(t,T);else try{await this.v(t,o)}catch(e){h=e.isError?e:g(t,e)}try{h?await t.sendErrorResponse(h,r):await t.sendResponse(o,r),void 0===t.context&&await t.close()}catch(e){await t.close(e)}}async r(t,e){if(void 0!==t.context||!this.w)try{return await this.v(t,e)}catch(e){throw e.isError?e:g(t,e)}throw g(t,T)}l;v;w}class A{o(){return null}P(t){this.D=t}async Q(t){return!1}async R(){return!1}async c(t){const e=new WebSocket(t);return new Promise(((t,s)=>{e.onerror=t=>{s(new Error(y.m))},e.onopen=()=>{t(this.a4(e,!0))}}))}a4=(t,e)=>{const s=new q(t),{conn:r,E:i,n}=this.D(s,e);return t.onmessage=t=>{t.data.arrayBuffer().then((t=>{i(new Uint8Array(t))}))},t.onerror=t=>{n(new Error(y.m))},t.onclose=t=>{1e3===t.code?n():n(new Error(t.reason))},r};D(t,e){throw new Error('Need to call "setConnWrapperMaker" before using')}}class q{constructor(t){this.g=t}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async N(t){if(this.g)return this.g.send(t);throw new Error(y.l)}async p(t){if(this.g){const e=v(t);return this.g.send(e)}throw new Error(y.l)}async close(t){const{g:e}=this;e&&(this.g=void 0,t?e.close(3e3,t.message):e.readyState===WebSocket.OPEN&&e.close(1e3))}O(){return!this.g}g}const M=(t,e,s)=>{const r=[e,t],i=(t=>{let e,s=0,r=0;for(;s<t.length;)for(r++,e=t[s++];e>>>=7;)r++;return r})(r),n=new Uint8Array(i);((t,e,s=0)=>{let r,i,n=s;for(r=0;r<t.length;r++){i=t[r];let s=127&i;for(;i>>>=7;)e[n++]=128|s,s=127&i;e[n++]=s}if(n<=e.length)return n-s;throw new Error("Buffer overflow")})(r,n),s.push(n)},N=t=>{const{h:[e,s],k:r}=((t,e,s=0)=>{let r,i,n=s,o=0,h=0;const a=[];for(r=0;r<t;r++){do{i=e[n++],o|=(127&i)<<h,h+=7}while(128&i);if(!(void 0!==i&&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:r}};class B{constructor(t){this.a1=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.c()).sendErrorResponse(t,e)}async sendResponse(t,e){return(this.a??await this.c()).sendResponse(t,e)}async sendMessage(t){return(this.a??await this.c()).sendMessage(t)}async sendRequest(t){return(this.a??await this.c()).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 reconnect(){this.a||await this.c()}a5(){this.a=void 0}async c(){let{p:t}=this;return t||(t=this.p=new Promise(((t,e)=>{this.a1().then((s=>{s?(t(s),this.a=s,this.p=void 0):e(new Error("Connect failed"))}))}))),await t}a1;a;p}class W{constructor(t,e){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:i,parsePkgBuff:n}=e;this.responsePkgType=s,this.s=0,this.t=new Map,this.f=new Map,this.L=new Map,this.T=i,this.U=n,this.A=t,this.J=r,this.$=this.a7;const o=[];this.T(s,2097152,o),this.V={makePkgBuff:i,responsePkgType:s,u:v(o)},this.f.set(s,this),this.A.P(((t,e)=>{do{this.s++}while(this.t.has(2097151&this.s));const s=new O(this.V,t,2097151&this.s);this.t.set(s.id,s),this.J?.insert(s);const r={conn:s,E:t=>this.a8(s,t),n:t=>this.a9(s,t)};return this.K?.(this,s,!!e),r}))}responsePkgType;context;K;B;o(){return this.A.o()}a3(t){return this.t.get(t)}c(t){return this.A.c(t)}async a4(){do{this.s++}while(this.t.has(2097151&this.s));const t=new C(2097151&this.s,this);return this.t.set(t.id,t),t}async a5(t){return this.A.Q(t)}async a6(){return this.A.R()}ab(t,e){const s=e.constructor.pkgType,r=this.f.get(s);if(r)return r?.r(t,e);throw new Error(y.D)}ac(t,e){const s=e.constructor.pkgType,r=this.f.get(s);if(r)return r?.r(t,e);throw new Error(y.D)}registerError(t,e){e.registerPkgType(t),this.L.set(t,e);const s=this.f.get(this.responsePkgType);this.f.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.m} has an incompatible pkgType ${typeof e}`);if(this.f.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.f.set(e,t),t}handleRequestWith(t){const e=t.pkgType;if(typeof e!=typeof this.responsePkgType)throw new Error(`Wrapper ${t.m} has an incompatible pkgType ${typeof e}`);if(this.f.has(e))throw new Error(`Wrapper ${t.m} is already handled by another handler`);return this.f.set(e,t),t}ao(t){const e=t.pkgType;return this.f.get(e)===t&&(this.f.delete(e),!0)}a8(t,e){this.J?.update(t);const s=this.U(e),r=this.f.get(s.pkgType);r?r.$(t,s):t.close(new Error(y.D))}a9(t,e){const s=t.id;this.t.delete(s),this.J?.remove(t),t.n(e),this.B?.(this,t,t.b.O(),e)}a7(t,e){const{buff:s,pkgType:r,requestId:i,bodyOffset:n}=e;if(r===this.responsePkgType){const e=s.subarray(n);t.X(i,e)}else{const e=this.L.get(r);if(e){const r=new e(s.subarray(n),!0);t.ag(i,r)}}}V;s;T;U;A;t;f;L;J}class O extends E{constructor(t,e,s){super(),this.id=s,this.b=e,this.C=t,this.Z=0}context;id;b;get localAddress(){return this.b.localAddress}get localPort(){return this.b.localPort}get remoteAddress(){return this.b.remoteAddress}get remoteFamily(){return this.b.remoteFamily}get remotePort(){return this.b.remotePort}sendMessage(t){const e=t.constructor.pkgType,s=[t.buff];return this.C.makePkgBuff(e,0,s),this.b.p(s)}sendRequest(t){return this.k=new Map,this.sendRequest=this.a0,this.X=this.ah,this.n=this.ai,this.a0(t)}sendErrorResponse(t,e){const s=t.constructor.pkgType,r=[t.buff];return this.C.makePkgBuff(s,e,r),this.b.p(r)}sendResponse(t,e){const s=[t.respBuff],{makePkgBuff:r,responsePkgType:i}=this.C;return r(i,e,s),this.b.p(s)}async close(t){await this.b.close(t)}X(t,e){2097152===t&&void 0===this.context&&this.close(new Error(y.I))}ag(t,e){const s=this.k.get(t);if(s)return this.k.delete(t),s.i(e)}keepAlive(){this.b.N(this.C.u).catch(k)}timeout(){this.close(new Error(y.a2))}n(t){}async a0(t){const e=2097151&this.Z++,s=t.constructor.pkgType,r=[t.buff];this.C.makePkgBuff(s,e,r);const i=new Promise(((t,s)=>{const i={d:t,i:s};this.k.set(e,i),this.b.p(r).catch((t=>{this.k.delete(e),s(t)}))}));t.respBuff=await i}ah(t,e){const s=this.k.get(t);if(s)return this.k.delete(t),void s.d(e);2097152===t&&void 0===this.context&&this.close(new Error(y.I))}ai(t){const e=this.k;if(e.size>0){t||(t=new Error(y.a1));for(const[s,r]of e)e.delete(s),r.i(t)}this.k=void 0}C;k;Z}class C{constructor(t,e){this.id=t,this.M=e}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(t){return this.M.ab(this,t)}async sendRequest(t){return this.M.ac(this,t)}sendErrorResponse(t,e){throw null}sendResponse(t,e){throw null}async close(t){}M}class S extends l{}const $=(t,e)=>new I(t,{responsePkgType:0,makePkgBuff:M,parsePkgBuff:N,timeoutMonitor:e});class I{constructor(t,e){const s=new A,r=this.d=new W(s,e);this.h="",this.j=[],this.q=0,this.a=new B((()=>this.c())),this.H=t,this.O=this.c,r.K=this.K.bind(this),r.B=this.B.bind(this)}registerError(t,e){return this.d.registerError(t,e)}registerMessage(t,e){return this.d.registerMessage(t,e)}registerRequest(t,e){return this.d.registerRequest(t,e)}handleMessageWith(t,e){this.d.handleMessageWith(new x(t,((t,s)=>e(s.data)),!1))}handleRequestWith(t,e){this.d.handleRequestWith(new R(t,(async(t,s)=>{s.resp=await e(s.data)}),!1))}sendMessage(t){return this.a.sendMessage(t)}async sendRequest(t){return await this.a.sendRequest(t),t.resp}async sendCustomAuthRequest(t){throw new Error('Need to call "enableAuthenticate" before using sendCustomAuthRequest')}resetAuthState(){}enableAuthenticate(t,e,s){const{O:r,H:i,d:n,a:o}=this;if(this.c===r){n.registerRequest(t,S);let r,h="";this.c=async()=>{for(this.q=-1,this.m&&await this.m.Y();;){const t=i(r);let o;try{o=await n.c(t)}catch{return void this.B(n,this.a,!0,new Error(y.k))}if(this.h||s){const t=new S({cid:this.h,token:h});try{await o.sendRequest(t);const{resp:s}=t;if(this.h=s.cid,h=s.token,r=s.shard,r&&r<0){r=-r-1,o.close();continue}e(this.h)}catch(t){return t instanceof p&&"HitSessionLimit"===t.data&&(this.h="",h="",r=void 0,e(this.h)),void o.close(t)}}return o}},this.sendCustomAuthRequest=async t=>{await o.sendRequest(t);const{resp:s}=t;return this.h=s.cid,h=s.token,r=s.shard,r&&r<0&&(r=-r-1,o.close()),e(this.h),s},this.resetAuthState=()=>{this.h&&(this.h="",h="",r=void 0,o.close(),e(this.h))}}}reconnect(t){const{a:e,q:s}=this;s>0||(this.m?this.m.a3(t??0):t&&(this.m=new P(t,(()=>this.m=void 0))),s||e.reconnect())}onConnectionStatus(t){let{j:e}=this;e||(this.j=e=[]),e.push(t)}async c(){const{H:t,d:e,m:s}=this;this.q=-1,s&&await s.Y();const r=t();try{return e.c(r)}catch{this.B(e,this.a,!0,new Error(y.k))}}K(t,e,s){if(this.q=1,e.context="",this.j)for(const t of this.j)t(!0,s)}B(t,e,s,r){if(this.q=0,this.a.a5(),s||this.resetAuthState(),this.j)for(const t of this.j)t(!1,s,r)}d;a;h;q;H;O;m;j}var J=e.EK,F=e.sr,j=e.si,L=e.N3,U=e.sk,z=e.wL,D=e.bM,K=e.Lm,_=e.h8,G=e.Yu,H=e.I9,Q=e.iU,Y=e.mx;export{J as CommonError,F as ErrorWrapper,j as EventWrapper,L as JsonError,U as JsonMessage,z as JsonRequest,D as MessageWrapper,K as NetErrorString,_ as RequestWrapper,G as TimeoutMonitor,H as WsClient,Q as createNumPkgTypeClient,Y as handleUncaughtErrorWith};
|
|
@@ -1,42 +1,42 @@
|
|
|
1
1
|
export class b {
|
|
2
2
|
constructor(a, c) {
|
|
3
|
-
this.
|
|
4
|
-
this.
|
|
3
|
+
this.W = new Promise((d) => {
|
|
4
|
+
this.G = d;
|
|
5
5
|
});
|
|
6
6
|
this.N = c;
|
|
7
7
|
if (a > 0) {
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
8
|
+
this.u = setTimeout(() => {
|
|
9
|
+
this.u = undefined;
|
|
10
10
|
this.N?.();
|
|
11
|
-
this.
|
|
11
|
+
this.G();
|
|
12
12
|
}, a);
|
|
13
13
|
}
|
|
14
14
|
else {
|
|
15
|
-
this.
|
|
15
|
+
this.G();
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
|
|
19
|
-
if (this.
|
|
20
|
-
clearTimeout(this.
|
|
18
|
+
a3(a) {
|
|
19
|
+
if (this.u) {
|
|
20
|
+
clearTimeout(this.u);
|
|
21
21
|
}
|
|
22
22
|
if (a > 0) {
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
23
|
+
this.u = setTimeout(() => {
|
|
24
|
+
this.u = undefined;
|
|
25
25
|
this.N?.();
|
|
26
|
-
this.
|
|
26
|
+
this.G();
|
|
27
27
|
}, a);
|
|
28
28
|
}
|
|
29
29
|
else {
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
30
|
+
this.u = undefined;
|
|
31
|
+
this.G();
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
async
|
|
35
|
-
await this.
|
|
34
|
+
async Y() {
|
|
35
|
+
await this.W;
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
G;
|
|
38
|
+
W;
|
|
39
|
+
u;
|
|
40
40
|
N;
|
|
41
41
|
}
|
|
42
42
|
//# sourceMappingURL=AdjustableTimer.js.map
|
|
@@ -1,31 +1,29 @@
|
|
|
1
1
|
export class DataWrapper {
|
|
2
2
|
constructor(a, needParse) {
|
|
3
3
|
if (needParse) {
|
|
4
|
-
this.
|
|
5
|
-
this.s = undefined;
|
|
4
|
+
this.x = a;
|
|
6
5
|
}
|
|
7
6
|
else {
|
|
8
|
-
this.
|
|
9
|
-
this.s = a;
|
|
7
|
+
this.y = a;
|
|
10
8
|
}
|
|
11
9
|
}
|
|
12
10
|
get data() {
|
|
13
|
-
if (!this.
|
|
14
|
-
this.
|
|
11
|
+
if (!this.y) {
|
|
12
|
+
this.y = this.decode(this.x);
|
|
15
13
|
}
|
|
16
|
-
return this.
|
|
14
|
+
return this.y;
|
|
17
15
|
}
|
|
18
16
|
set data(b) {
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
17
|
+
this.y = b;
|
|
18
|
+
this.x = undefined;
|
|
21
19
|
}
|
|
22
20
|
get buff() {
|
|
23
|
-
if (!this.
|
|
24
|
-
this.
|
|
21
|
+
if (!this.x) {
|
|
22
|
+
this.x = this.encode(this.y);
|
|
25
23
|
}
|
|
26
|
-
return this.
|
|
24
|
+
return this.x;
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
x;
|
|
27
|
+
y;
|
|
30
28
|
}
|
|
31
29
|
//# sourceMappingURL=DataWrapper.js.map
|
package/lib/common/List.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
export class ListNode {
|
|
2
2
|
constructor() {
|
|
3
|
-
this.
|
|
3
|
+
this.e = this;
|
|
4
4
|
this.i = this;
|
|
5
5
|
}
|
|
6
6
|
getPrev() {
|
|
7
|
-
return this.
|
|
7
|
+
return this.e;
|
|
8
8
|
}
|
|
9
9
|
getNext() {
|
|
10
10
|
return this.i;
|
|
11
11
|
}
|
|
12
12
|
insertBefore(node) {
|
|
13
13
|
this.i = node;
|
|
14
|
-
this.
|
|
15
|
-
node.
|
|
16
|
-
this.
|
|
14
|
+
this.e = node.e;
|
|
15
|
+
node.e = this;
|
|
16
|
+
this.e.i = this;
|
|
17
17
|
}
|
|
18
18
|
insertAfter(node) {
|
|
19
|
-
this.
|
|
19
|
+
this.e = node;
|
|
20
20
|
this.i = node.i;
|
|
21
21
|
node.i = this;
|
|
22
|
-
this.i.
|
|
22
|
+
this.i.e = this;
|
|
23
23
|
}
|
|
24
24
|
remove() {
|
|
25
|
-
this.
|
|
26
|
-
this.i.
|
|
25
|
+
this.e.i = this.i;
|
|
26
|
+
this.i.e = this.e;
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
e;
|
|
29
29
|
i;
|
|
30
30
|
}
|
|
31
31
|
export class c {
|
|
32
32
|
constructor() {
|
|
33
33
|
this._ = new ListNode();
|
|
34
34
|
}
|
|
35
|
-
get
|
|
35
|
+
get f() {
|
|
36
36
|
return this._.getNext();
|
|
37
37
|
}
|
|
38
38
|
get a() {
|
|
@@ -44,7 +44,7 @@ export class c {
|
|
|
44
44
|
Q(node) {
|
|
45
45
|
node.insertAfter(this._);
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
a6(node) {
|
|
48
48
|
node.insertBefore(this._);
|
|
49
49
|
}
|
|
50
50
|
_;
|
package/lib/common/utils.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export const j = (
|
|
2
|
-
return new Promise((
|
|
1
|
+
export const j = (f) => {
|
|
2
|
+
return new Promise((d) => setTimeout(d, f));
|
|
3
3
|
};
|
|
4
4
|
export const l = () => { };
|
|
5
5
|
export const k = (i = 0x100000000) => {
|
|
6
6
|
return (Math.random() * i) >>> 0;
|
|
7
7
|
};
|
|
8
|
-
export const m = (
|
|
8
|
+
export const m = (e) => {
|
|
9
9
|
let c = 5381, a = 0;
|
|
10
|
-
while (a <
|
|
11
|
-
c = ((c << 5) + c) +
|
|
10
|
+
while (a < e.length) {
|
|
11
|
+
c = ((c << 5) + c) + e.charCodeAt(a++);
|
|
12
12
|
}
|
|
13
13
|
return c >>> 0;
|
|
14
14
|
};
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { NetErrorString } from '../net/NetErrorString';
|
|
2
|
+
import { b } from './common';
|
|
3
|
+
const c = new Error(NetErrorString.g);
|
|
4
|
+
export class n {
|
|
5
|
+
constructor(wrapperCtor, d, e = true) {
|
|
4
6
|
this.l = wrapperCtor;
|
|
5
|
-
this.
|
|
6
|
-
this.
|
|
7
|
+
this.v = d;
|
|
8
|
+
this.w = e;
|
|
7
9
|
}
|
|
8
10
|
get pkgType() {
|
|
9
11
|
return this.l.pkgType;
|
|
@@ -15,9 +17,9 @@ export class j {
|
|
|
15
17
|
const { buff, bodyOffset } = a;
|
|
16
18
|
const h = buff.subarray(bodyOffset);
|
|
17
19
|
const message = new this.l(h, true);
|
|
18
|
-
if (conn.context !== undefined || !this.
|
|
20
|
+
if (conn.context !== undefined || !this.w) {
|
|
19
21
|
try {
|
|
20
|
-
await this.
|
|
22
|
+
await this.v(conn, message);
|
|
21
23
|
}
|
|
22
24
|
catch (err) {
|
|
23
25
|
if (!err.isError) {
|
|
@@ -33,9 +35,9 @@ export class j {
|
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
async r(conn, message) {
|
|
36
|
-
if (conn.context !== undefined || !this.
|
|
38
|
+
if (conn.context !== undefined || !this.w) {
|
|
37
39
|
try {
|
|
38
|
-
return await this.
|
|
40
|
+
return await this.v(conn, message);
|
|
39
41
|
}
|
|
40
42
|
catch (err) {
|
|
41
43
|
if (!err.isError) {
|
|
@@ -48,14 +50,14 @@ export class j {
|
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
l;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
v;
|
|
54
|
+
w;
|
|
53
55
|
}
|
|
54
|
-
export class
|
|
55
|
-
constructor(wrapperCtor, d,
|
|
56
|
+
export class o {
|
|
57
|
+
constructor(wrapperCtor, d, e = true) {
|
|
56
58
|
this.l = wrapperCtor;
|
|
57
|
-
this.
|
|
58
|
-
this.
|
|
59
|
+
this.v = d;
|
|
60
|
+
this.w = e;
|
|
59
61
|
}
|
|
60
62
|
get pkgType() {
|
|
61
63
|
return this.l.pkgType;
|
|
@@ -68,9 +70,9 @@ export class n {
|
|
|
68
70
|
const h = buff.subarray(bodyOffset);
|
|
69
71
|
const request = new this.l(h, true);
|
|
70
72
|
let error;
|
|
71
|
-
if (conn.context !== undefined || !this.
|
|
73
|
+
if (conn.context !== undefined || !this.w) {
|
|
72
74
|
try {
|
|
73
|
-
await this.
|
|
75
|
+
await this.v(conn, request);
|
|
74
76
|
}
|
|
75
77
|
catch (err) {
|
|
76
78
|
if (err.isError) {
|
|
@@ -100,9 +102,9 @@ export class n {
|
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
104
|
async r(conn, request) {
|
|
103
|
-
if (conn.context !== undefined || !this.
|
|
105
|
+
if (conn.context !== undefined || !this.w) {
|
|
104
106
|
try {
|
|
105
|
-
return await this.
|
|
107
|
+
return await this.v(conn, request);
|
|
106
108
|
}
|
|
107
109
|
catch (err) {
|
|
108
110
|
if (err.isError) {
|
|
@@ -116,7 +118,7 @@ export class n {
|
|
|
116
118
|
throw b(conn, c);
|
|
117
119
|
}
|
|
118
120
|
l;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
+
v;
|
|
122
|
+
w;
|
|
121
123
|
}
|
|
122
124
|
//# sourceMappingURL=SimpleHandler.js.map
|
package/lib/handler/common.js
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -122,6 +122,18 @@ export interface IAuthResp {
|
|
|
122
122
|
shard?: number;
|
|
123
123
|
}
|
|
124
124
|
export declare const handleUncaughtErrorWith: (handler: (conn: IConn<unknown>, err: Error) => ErrorWrapper<unknown>) => void;
|
|
125
|
+
export declare enum NetErrorString {
|
|
126
|
+
AuthTimeout = "AuthTimeout",
|
|
127
|
+
BadRequest = "BadRequest",
|
|
128
|
+
ConnectFailed = "ConnectFailed",
|
|
129
|
+
ConnectionClosed = "ConnectionClosed",
|
|
130
|
+
InvalidConnString = "InvalidConnString",
|
|
131
|
+
InvalidConnection = "InvalidConnection",
|
|
132
|
+
InvalidPackage = "InvalidPackage",
|
|
133
|
+
NotAuthenticated = "NotAuthenticated",
|
|
134
|
+
Timeout = "Timeout",
|
|
135
|
+
UnknownError = "UnknownError"
|
|
136
|
+
}
|
|
125
137
|
export declare const createNumPkgTypeClient: (getConnStr: (shard?: number) => string, timeoutMonitor?: TimeoutMonitor) => WsClient<number>;
|
|
126
138
|
export declare class WsClient<PkgTypeT extends number | string> {
|
|
127
139
|
constructor(getConnStr: (shard?: number) => string, opts: TypedPkgHubOpts<PkgTypeT>);
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { CommonError, handleUncaughtErrorWith } from './handler/common';
|
|
2
2
|
export { JsonError, JsonMessage, JsonRequest } from './net/JsonPackageWrapper';
|
|
3
|
+
export { NetErrorString } from './net/NetErrorString';
|
|
3
4
|
export { ErrorWrapper, EventWrapper, MessageWrapper, RequestWrapper } from './net/PackageWrapper';
|
|
4
5
|
export { TimeoutMonitor } from './net/TimeoutMonitor';
|
|
5
6
|
export { createNumPkgTypeClient, WsClient } from './WsClient';
|
package/lib/net/BrowserWsNet.js
CHANGED
|
@@ -1,40 +1,41 @@
|
|
|
1
1
|
import { y } from '../common/utils';
|
|
2
|
-
|
|
2
|
+
import { NetErrorString } from './NetErrorString';
|
|
3
|
+
export class r {
|
|
3
4
|
o() {
|
|
4
5
|
return null;
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
-
this.
|
|
7
|
+
P(D) {
|
|
8
|
+
this.D = D;
|
|
8
9
|
}
|
|
9
|
-
async
|
|
10
|
+
async Q(t) {
|
|
10
11
|
return false;
|
|
11
12
|
}
|
|
12
|
-
async
|
|
13
|
+
async R() {
|
|
13
14
|
return false;
|
|
14
15
|
}
|
|
15
|
-
async c(
|
|
16
|
-
const
|
|
17
|
-
return new Promise((
|
|
18
|
-
|
|
19
|
-
i(new Error(
|
|
16
|
+
async c(e) {
|
|
17
|
+
const f = new WebSocket(e);
|
|
18
|
+
return new Promise((d, i) => {
|
|
19
|
+
f.onerror = (k) => {
|
|
20
|
+
i(new Error(NetErrorString.m));
|
|
20
21
|
};
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
f.onopen = () => {
|
|
23
|
+
d(this.a4(f, true));
|
|
23
24
|
};
|
|
24
25
|
});
|
|
25
26
|
}
|
|
26
|
-
|
|
27
|
-
const b = new
|
|
28
|
-
const { conn,
|
|
29
|
-
|
|
27
|
+
a4 = (f, q) => {
|
|
28
|
+
const b = new s(f);
|
|
29
|
+
const { conn, E, n } = this.D(b, q);
|
|
30
|
+
f.onmessage = (j) => {
|
|
30
31
|
j.data.arrayBuffer().then((buff) => {
|
|
31
|
-
|
|
32
|
+
E(new Uint8Array(buff));
|
|
32
33
|
});
|
|
33
34
|
};
|
|
34
|
-
|
|
35
|
-
n(new Error(
|
|
35
|
+
f.onerror = (k) => {
|
|
36
|
+
n(new Error(NetErrorString.m));
|
|
36
37
|
};
|
|
37
|
-
|
|
38
|
+
f.onclose = (j) => {
|
|
38
39
|
if (j.code === 1000) {
|
|
39
40
|
n();
|
|
40
41
|
}
|
|
@@ -44,13 +45,13 @@ export class l {
|
|
|
44
45
|
};
|
|
45
46
|
return conn;
|
|
46
47
|
};
|
|
47
|
-
|
|
48
|
+
D(a, u) {
|
|
48
49
|
throw new Error('Need to call "setConnWrapperMaker" before using');
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
|
-
class
|
|
52
|
-
constructor(
|
|
53
|
-
this.g =
|
|
52
|
+
class s {
|
|
53
|
+
constructor(f) {
|
|
54
|
+
this.g = f;
|
|
54
55
|
}
|
|
55
56
|
get localAddress() {
|
|
56
57
|
return;
|
|
@@ -67,18 +68,18 @@ class m {
|
|
|
67
68
|
get remotePort() {
|
|
68
69
|
return;
|
|
69
70
|
}
|
|
70
|
-
async
|
|
71
|
+
async N(buff) {
|
|
71
72
|
if (this.g) {
|
|
72
73
|
return this.g.send(buff);
|
|
73
74
|
}
|
|
74
|
-
throw new Error(
|
|
75
|
+
throw new Error(NetErrorString.l);
|
|
75
76
|
}
|
|
76
77
|
async p(h) {
|
|
77
78
|
if (this.g) {
|
|
78
79
|
const buff = y(h);
|
|
79
80
|
return this.g.send(buff);
|
|
80
81
|
}
|
|
81
|
-
throw new Error(
|
|
82
|
+
throw new Error(NetErrorString.l);
|
|
82
83
|
}
|
|
83
84
|
async close(err) {
|
|
84
85
|
const { g } = this;
|
|
@@ -94,7 +95,7 @@ class m {
|
|
|
94
95
|
}
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
|
-
|
|
98
|
+
O() {
|
|
98
99
|
return !this.g;
|
|
99
100
|
}
|
|
100
101
|
g;
|
package/lib/net/LazyConn.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export class g {
|
|
2
|
-
constructor(
|
|
3
|
-
this.a1 =
|
|
2
|
+
constructor(e) {
|
|
3
|
+
this.a1 = e;
|
|
4
4
|
}
|
|
5
5
|
get id() {
|
|
6
6
|
return this.a?.id ?? -1;
|
|
@@ -49,16 +49,16 @@ export class g {
|
|
|
49
49
|
await this.c();
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
a5() {
|
|
53
53
|
this.a = undefined;
|
|
54
54
|
}
|
|
55
55
|
async c() {
|
|
56
56
|
let { p } = this;
|
|
57
57
|
if (!p) {
|
|
58
|
-
p = this.p = new Promise((
|
|
58
|
+
p = this.p = new Promise((d, i) => {
|
|
59
59
|
this.a1().then((conn) => {
|
|
60
60
|
if (conn) {
|
|
61
|
-
|
|
61
|
+
d(conn);
|
|
62
62
|
this.a = conn;
|
|
63
63
|
this.p = undefined;
|
|
64
64
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export var NetErrorString;
|
|
2
|
+
(function (NetErrorString) {
|
|
3
|
+
NetErrorString["I"] = "AuthTimeout";
|
|
4
|
+
NetErrorString["a"] = "BadRequest";
|
|
5
|
+
NetErrorString["k"] = "ConnectFailed";
|
|
6
|
+
NetErrorString["a1"] = "ConnectionClosed";
|
|
7
|
+
NetErrorString["b"] = "InvalidConnString";
|
|
8
|
+
NetErrorString["l"] = "InvalidConnection";
|
|
9
|
+
NetErrorString["D"] = "InvalidPackage";
|
|
10
|
+
NetErrorString["g"] = "NotAuthenticated";
|
|
11
|
+
NetErrorString["a2"] = "Timeout";
|
|
12
|
+
NetErrorString["m"] = "UnknownError";
|
|
13
|
+
})(NetErrorString || (NetErrorString = {}));
|
|
14
|
+
//# sourceMappingURL=NetErrorString.js.map
|
|
@@ -3,68 +3,68 @@ import { l } from '../common/utils';
|
|
|
3
3
|
export class MonitorConn extends ListNode {
|
|
4
4
|
}
|
|
5
5
|
export class TimeoutMonitor {
|
|
6
|
-
constructor(
|
|
7
|
-
this.R =
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
6
|
+
constructor(e, h, g) {
|
|
7
|
+
this.R = e;
|
|
8
|
+
this.I = h;
|
|
9
|
+
this.E = g;
|
|
10
10
|
this.o = 0;
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
if (
|
|
11
|
+
this.F = 0;
|
|
12
|
+
this.r = [];
|
|
13
|
+
if (e === 0) {
|
|
14
14
|
this.insert = l;
|
|
15
15
|
this.update = l;
|
|
16
16
|
this.remove = l;
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
for (let a = 0; a < g; a++) {
|
|
20
|
-
this.
|
|
20
|
+
this.r.push(new c());
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
insert(conn) {
|
|
24
|
-
if (!this.
|
|
24
|
+
if (!this.F) {
|
|
25
25
|
this.S = setInterval(this.a2, this.R);
|
|
26
26
|
}
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
27
|
+
this.F++;
|
|
28
|
+
this.r[this.o].Q(conn);
|
|
29
29
|
}
|
|
30
30
|
update(conn) {
|
|
31
31
|
conn.remove();
|
|
32
|
-
this.
|
|
32
|
+
this.r[this.o].Q(conn);
|
|
33
33
|
}
|
|
34
34
|
remove(conn) {
|
|
35
35
|
conn.remove();
|
|
36
|
-
this.
|
|
37
|
-
if (!this.
|
|
36
|
+
this.F--;
|
|
37
|
+
if (!this.F) {
|
|
38
38
|
clearInterval(this.S);
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
a2 = () => {
|
|
42
|
-
const {
|
|
42
|
+
const { I, E, r } = this;
|
|
43
43
|
this.o++;
|
|
44
|
-
if (this.o ===
|
|
44
|
+
if (this.o === E) {
|
|
45
45
|
this.o = 0;
|
|
46
46
|
}
|
|
47
|
-
if (
|
|
48
|
-
let d = this.o +
|
|
49
|
-
if (d >=
|
|
50
|
-
d -=
|
|
47
|
+
if (I) {
|
|
48
|
+
let d = this.o + I;
|
|
49
|
+
if (d >= E) {
|
|
50
|
+
d -= E;
|
|
51
51
|
}
|
|
52
|
-
const b =
|
|
53
|
-
for (let node = b.
|
|
52
|
+
const b = r[d];
|
|
53
|
+
for (let node = b.f; !b.P(node); node = node.getNext()) {
|
|
54
54
|
node.keepAlive();
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
const b =
|
|
58
|
-
for (let node = b.
|
|
57
|
+
const b = r[this.o];
|
|
58
|
+
for (let node = b.f; !b.P(node); node = node.getNext()) {
|
|
59
59
|
node.timeout();
|
|
60
60
|
}
|
|
61
61
|
};
|
|
62
62
|
R;
|
|
63
|
-
|
|
64
|
-
D;
|
|
65
|
-
o;
|
|
63
|
+
I;
|
|
66
64
|
E;
|
|
65
|
+
o;
|
|
66
|
+
F;
|
|
67
67
|
S;
|
|
68
|
-
|
|
68
|
+
r;
|
|
69
69
|
}
|
|
70
70
|
//# sourceMappingURL=TimeoutMonitor.js.map
|
package/lib/net/TypedPkgHub.js
CHANGED
|
@@ -1,94 +1,92 @@
|
|
|
1
1
|
import { y, l } from '../common/utils';
|
|
2
|
+
import { NetErrorString } from './NetErrorString';
|
|
2
3
|
import { MonitorConn } from './TimeoutMonitor';
|
|
3
|
-
export class
|
|
4
|
-
constructor(
|
|
4
|
+
export class v {
|
|
5
|
+
constructor(F, w) {
|
|
5
6
|
const { responsePkgType, timeoutMonitor, makePkgBuff, parsePkgBuff } = w;
|
|
6
7
|
this.responsePkgType = responsePkgType;
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
10
|
-
this.u = 0;
|
|
11
|
-
this.v = new Map();
|
|
12
|
-
this.e = new Map();
|
|
8
|
+
this.s = 0;
|
|
9
|
+
this.t = new Map();
|
|
10
|
+
this.f = new Map();
|
|
13
11
|
this.L = new Map();
|
|
14
12
|
this.T = makePkgBuff;
|
|
15
13
|
this.U = parsePkgBuff;
|
|
16
|
-
this.A =
|
|
17
|
-
this.
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
this.T(responsePkgType, 0x200000,
|
|
14
|
+
this.A = F;
|
|
15
|
+
this.J = timeoutMonitor;
|
|
16
|
+
const S = this;
|
|
17
|
+
S.$ = this.a7;
|
|
18
|
+
const u = [];
|
|
19
|
+
this.T(responsePkgType, 0x200000, u);
|
|
22
20
|
this.V = {
|
|
23
21
|
makePkgBuff: makePkgBuff,
|
|
24
22
|
responsePkgType: responsePkgType,
|
|
25
|
-
|
|
23
|
+
u: y(u),
|
|
26
24
|
};
|
|
27
|
-
this.
|
|
28
|
-
this.A.
|
|
25
|
+
this.f.set(responsePkgType, S);
|
|
26
|
+
this.A.P((b, q) => {
|
|
29
27
|
do {
|
|
30
|
-
this.
|
|
31
|
-
} while (this.
|
|
32
|
-
const conn = new x(this.V, b, this.
|
|
33
|
-
this.
|
|
34
|
-
this.
|
|
28
|
+
this.s++;
|
|
29
|
+
} while (this.t.has(this.s & 0x1fffff));
|
|
30
|
+
const conn = new x(this.V, b, this.s & 0x1fffff);
|
|
31
|
+
this.t.set(conn.id, conn);
|
|
32
|
+
this.J?.insert(conn);
|
|
35
33
|
const g = {
|
|
36
34
|
conn,
|
|
37
|
-
|
|
38
|
-
n: (err) => this.
|
|
35
|
+
E: (buff) => this.a8(conn, buff),
|
|
36
|
+
n: (err) => this.a9(conn, err),
|
|
39
37
|
};
|
|
40
|
-
this.
|
|
38
|
+
this.K?.(this, conn, !!q);
|
|
41
39
|
return g;
|
|
42
40
|
});
|
|
43
41
|
}
|
|
44
42
|
responsePkgType;
|
|
45
43
|
context;
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
K;
|
|
45
|
+
B;
|
|
48
46
|
o() {
|
|
49
47
|
return this.A.o();
|
|
50
48
|
}
|
|
51
|
-
|
|
52
|
-
return this.
|
|
49
|
+
a3(z) {
|
|
50
|
+
return this.t.get(z);
|
|
53
51
|
}
|
|
54
|
-
c(
|
|
55
|
-
return this.A.c(
|
|
52
|
+
c(e) {
|
|
53
|
+
return this.A.c(e);
|
|
56
54
|
}
|
|
57
|
-
async
|
|
55
|
+
async a4() {
|
|
58
56
|
do {
|
|
59
|
-
this.
|
|
60
|
-
} while (this.
|
|
61
|
-
const conn = new
|
|
62
|
-
this.
|
|
57
|
+
this.s++;
|
|
58
|
+
} while (this.t.has(this.s & 0x1fffff));
|
|
59
|
+
const conn = new aa(this.s & 0x1fffff, this);
|
|
60
|
+
this.t.set(conn.id, conn);
|
|
63
61
|
return conn;
|
|
64
62
|
}
|
|
65
|
-
async
|
|
66
|
-
return this.A.
|
|
63
|
+
async a5(e) {
|
|
64
|
+
return this.A.Q(e);
|
|
67
65
|
}
|
|
68
|
-
async
|
|
69
|
-
return this.A.
|
|
66
|
+
async a6() {
|
|
67
|
+
return this.A.R();
|
|
70
68
|
}
|
|
71
|
-
|
|
69
|
+
ab(conn, message) {
|
|
72
70
|
const pkgType = message.constructor.pkgType;
|
|
73
|
-
const handler = this.
|
|
71
|
+
const handler = this.f.get(pkgType);
|
|
74
72
|
if (handler) {
|
|
75
73
|
return handler?.r(conn, message);
|
|
76
74
|
}
|
|
77
|
-
throw new Error(
|
|
75
|
+
throw new Error(NetErrorString.D);
|
|
78
76
|
}
|
|
79
|
-
|
|
77
|
+
ac(conn, request) {
|
|
80
78
|
const pkgType = request.constructor.pkgType;
|
|
81
|
-
const handler = this.
|
|
79
|
+
const handler = this.f.get(pkgType);
|
|
82
80
|
if (handler) {
|
|
83
81
|
return handler?.r(conn, request);
|
|
84
82
|
}
|
|
85
|
-
throw new Error(
|
|
83
|
+
throw new Error(NetErrorString.D);
|
|
86
84
|
}
|
|
87
85
|
registerError(pkgType, wrapperCtor) {
|
|
88
86
|
wrapperCtor.registerPkgType(pkgType);
|
|
89
87
|
this.L.set(pkgType, wrapperCtor);
|
|
90
|
-
const
|
|
91
|
-
this.
|
|
88
|
+
const ad = this.f.get(this.responsePkgType);
|
|
89
|
+
this.f.set(pkgType, ad);
|
|
92
90
|
}
|
|
93
91
|
registerMessage(pkgType, wrapperCtor) {
|
|
94
92
|
wrapperCtor.registerPkgType(pkgType);
|
|
@@ -101,10 +99,10 @@ export class t {
|
|
|
101
99
|
if (typeof pkgType !== typeof this.responsePkgType) {
|
|
102
100
|
throw new Error(`Wrapper ${handler.m} has an incompatible pkgType ${typeof pkgType}`);
|
|
103
101
|
}
|
|
104
|
-
if (this.
|
|
102
|
+
if (this.f.has(pkgType)) {
|
|
105
103
|
throw new Error(`Wrapper ${handler.m} is already handled by another handler`);
|
|
106
104
|
}
|
|
107
|
-
this.
|
|
105
|
+
this.f.set(pkgType, handler);
|
|
108
106
|
return handler;
|
|
109
107
|
}
|
|
110
108
|
handleRequestWith(handler) {
|
|
@@ -112,72 +110,70 @@ export class t {
|
|
|
112
110
|
if (typeof pkgType !== typeof this.responsePkgType) {
|
|
113
111
|
throw new Error(`Wrapper ${handler.m} has an incompatible pkgType ${typeof pkgType}`);
|
|
114
112
|
}
|
|
115
|
-
if (this.
|
|
113
|
+
if (this.f.has(pkgType)) {
|
|
116
114
|
throw new Error(`Wrapper ${handler.m} is already handled by another handler`);
|
|
117
115
|
}
|
|
118
|
-
this.
|
|
116
|
+
this.f.set(pkgType, handler);
|
|
119
117
|
return handler;
|
|
120
118
|
}
|
|
121
|
-
|
|
119
|
+
ao(handler) {
|
|
122
120
|
const pkgType = handler.pkgType;
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
125
|
-
this.
|
|
121
|
+
const ae = this.f.get(pkgType);
|
|
122
|
+
if (ae === handler) {
|
|
123
|
+
this.f.delete(pkgType);
|
|
126
124
|
return true;
|
|
127
125
|
}
|
|
128
126
|
return false;
|
|
129
127
|
}
|
|
130
|
-
|
|
131
|
-
this.
|
|
128
|
+
a8(conn, buff) {
|
|
129
|
+
this.J?.update(conn);
|
|
132
130
|
const a = this.U(buff);
|
|
133
|
-
const handler = this.
|
|
131
|
+
const handler = this.f.get(a.pkgType);
|
|
134
132
|
if (handler) {
|
|
135
133
|
handler.$(conn, a);
|
|
136
134
|
return;
|
|
137
135
|
}
|
|
138
|
-
conn.close(new Error(
|
|
136
|
+
conn.close(new Error(NetErrorString.D));
|
|
139
137
|
}
|
|
140
|
-
|
|
138
|
+
a9(conn, err) {
|
|
141
139
|
const z = conn.id;
|
|
142
|
-
this.
|
|
143
|
-
this.
|
|
140
|
+
this.t.delete(z);
|
|
141
|
+
this.J?.remove(conn);
|
|
144
142
|
conn.n(err);
|
|
145
|
-
this.
|
|
143
|
+
this.B?.(this, conn, conn.b.O(), err);
|
|
146
144
|
}
|
|
147
|
-
|
|
145
|
+
a7(conn, a) {
|
|
148
146
|
const { buff, pkgType, requestId, bodyOffset } = a;
|
|
149
147
|
if (pkgType === this.responsePkgType) {
|
|
150
148
|
const h = buff.subarray(bodyOffset);
|
|
151
|
-
conn.
|
|
149
|
+
conn.X(requestId, h);
|
|
152
150
|
}
|
|
153
151
|
else {
|
|
154
|
-
const
|
|
155
|
-
if (
|
|
152
|
+
const Y = this.L.get(pkgType);
|
|
153
|
+
if (Y) {
|
|
156
154
|
const h = buff.subarray(bodyOffset);
|
|
157
|
-
const
|
|
158
|
-
conn.
|
|
155
|
+
const af = new Y(h, true);
|
|
156
|
+
conn.ag(requestId, af);
|
|
159
157
|
}
|
|
160
158
|
}
|
|
161
159
|
}
|
|
162
160
|
V;
|
|
163
|
-
|
|
161
|
+
s;
|
|
164
162
|
T;
|
|
165
163
|
U;
|
|
166
164
|
A;
|
|
167
|
-
|
|
168
|
-
|
|
165
|
+
t;
|
|
166
|
+
f;
|
|
169
167
|
L;
|
|
170
|
-
|
|
168
|
+
J;
|
|
171
169
|
}
|
|
172
170
|
class x extends MonitorConn {
|
|
173
171
|
constructor(w, b, id) {
|
|
174
172
|
super();
|
|
175
|
-
this.context = undefined;
|
|
176
173
|
this.id = id;
|
|
177
174
|
this.b = b;
|
|
178
|
-
this.
|
|
179
|
-
this.
|
|
180
|
-
this.X = 0;
|
|
175
|
+
this.C = w;
|
|
176
|
+
this.Z = 0;
|
|
181
177
|
}
|
|
182
178
|
context;
|
|
183
179
|
id;
|
|
@@ -200,99 +196,98 @@ class x extends MonitorConn {
|
|
|
200
196
|
sendMessage(message) {
|
|
201
197
|
const pkgType = message.constructor.pkgType;
|
|
202
198
|
const encoded = [message.buff];
|
|
203
|
-
this.
|
|
199
|
+
this.C.makePkgBuff(pkgType, 0, encoded);
|
|
204
200
|
return this.b.p(encoded);
|
|
205
201
|
}
|
|
206
202
|
sendRequest(request) {
|
|
207
|
-
this.
|
|
208
|
-
this.sendRequest = this.
|
|
209
|
-
this.
|
|
210
|
-
this.n = this.
|
|
211
|
-
return this.
|
|
203
|
+
this.k = new Map();
|
|
204
|
+
this.sendRequest = this.a0;
|
|
205
|
+
this.X = this.ah;
|
|
206
|
+
this.n = this.ai;
|
|
207
|
+
return this.a0(request);
|
|
212
208
|
}
|
|
213
209
|
sendErrorResponse(error, requestId) {
|
|
214
210
|
const pkgType = error.constructor.pkgType;
|
|
215
211
|
const encoded = [error.buff];
|
|
216
|
-
this.
|
|
212
|
+
this.C.makePkgBuff(pkgType, requestId, encoded);
|
|
217
213
|
return this.b.p(encoded);
|
|
218
214
|
}
|
|
219
215
|
sendResponse(request, requestId) {
|
|
220
216
|
const encoded = [request.respBuff];
|
|
221
|
-
const { makePkgBuff, responsePkgType } = this.
|
|
217
|
+
const { makePkgBuff, responsePkgType } = this.C;
|
|
222
218
|
makePkgBuff(responsePkgType, requestId, encoded);
|
|
223
219
|
return this.b.p(encoded);
|
|
224
220
|
}
|
|
225
221
|
async close(err) {
|
|
226
222
|
await this.b.close(err);
|
|
227
223
|
}
|
|
228
|
-
|
|
224
|
+
X(requestId, ap) {
|
|
229
225
|
if (requestId === 0x200000 && this.context === undefined) {
|
|
230
|
-
this.close(new Error(
|
|
226
|
+
this.close(new Error(NetErrorString.I));
|
|
231
227
|
}
|
|
232
228
|
}
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
if (
|
|
236
|
-
this.
|
|
237
|
-
return
|
|
229
|
+
ag(requestId, error) {
|
|
230
|
+
const j = this.k.get(requestId);
|
|
231
|
+
if (j) {
|
|
232
|
+
this.k.delete(requestId);
|
|
233
|
+
return j.i(error);
|
|
238
234
|
}
|
|
239
235
|
}
|
|
240
236
|
keepAlive() {
|
|
241
|
-
this.b.
|
|
237
|
+
this.b.N(this.C.u).catch(l);
|
|
242
238
|
}
|
|
243
239
|
timeout() {
|
|
244
|
-
this.close(new Error(
|
|
240
|
+
this.close(new Error(NetErrorString.a2));
|
|
245
241
|
}
|
|
246
|
-
n(
|
|
242
|
+
n(aj) {
|
|
247
243
|
}
|
|
248
|
-
async
|
|
249
|
-
const requestId = (this.
|
|
244
|
+
async a0(request) {
|
|
245
|
+
const requestId = (this.Z++) & 0x1fffff;
|
|
250
246
|
const pkgType = request.constructor.pkgType;
|
|
251
247
|
const encoded = [request.buff];
|
|
252
|
-
this.
|
|
253
|
-
const
|
|
254
|
-
const
|
|
255
|
-
this.
|
|
248
|
+
this.C.makePkgBuff(pkgType, requestId, encoded);
|
|
249
|
+
const ak = new Promise((d, i) => {
|
|
250
|
+
const al = { d, i };
|
|
251
|
+
this.k.set(requestId, al);
|
|
256
252
|
this.b.p(encoded).catch((err) => {
|
|
257
|
-
this.
|
|
253
|
+
this.k.delete(requestId);
|
|
258
254
|
i(err);
|
|
259
255
|
});
|
|
260
256
|
});
|
|
261
|
-
request.respBuff = await
|
|
257
|
+
request.respBuff = await ak;
|
|
262
258
|
}
|
|
263
|
-
|
|
264
|
-
const
|
|
265
|
-
if (
|
|
266
|
-
this.
|
|
267
|
-
|
|
259
|
+
ah(requestId, h) {
|
|
260
|
+
const j = this.k.get(requestId);
|
|
261
|
+
if (j) {
|
|
262
|
+
this.k.delete(requestId);
|
|
263
|
+
j.d(h);
|
|
268
264
|
return;
|
|
269
265
|
}
|
|
270
266
|
if (requestId === 0x200000 && this.context === undefined) {
|
|
271
|
-
this.close(new Error(
|
|
267
|
+
this.close(new Error(NetErrorString.I));
|
|
272
268
|
}
|
|
273
269
|
}
|
|
274
|
-
|
|
275
|
-
const H = this.
|
|
270
|
+
ai(err) {
|
|
271
|
+
const H = this.k;
|
|
276
272
|
if (H.size > 0) {
|
|
277
273
|
if (!err) {
|
|
278
|
-
err = new Error(
|
|
274
|
+
err = new Error(NetErrorString.a1);
|
|
279
275
|
}
|
|
280
|
-
for (const [requestId,
|
|
276
|
+
for (const [requestId, j] of H) {
|
|
281
277
|
H.delete(requestId);
|
|
282
|
-
|
|
278
|
+
j.i(err);
|
|
283
279
|
}
|
|
284
280
|
}
|
|
285
|
-
this.
|
|
281
|
+
this.k = undefined;
|
|
286
282
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
283
|
+
C;
|
|
284
|
+
k;
|
|
285
|
+
Z;
|
|
290
286
|
}
|
|
291
|
-
class
|
|
292
|
-
constructor(id,
|
|
293
|
-
this.context = undefined;
|
|
287
|
+
class aa {
|
|
288
|
+
constructor(id, am) {
|
|
294
289
|
this.id = id;
|
|
295
|
-
this.M =
|
|
290
|
+
this.M = am;
|
|
296
291
|
}
|
|
297
292
|
context;
|
|
298
293
|
id;
|
|
@@ -304,18 +299,18 @@ class a6 {
|
|
|
304
299
|
return 'Local';
|
|
305
300
|
}
|
|
306
301
|
async sendMessage(message) {
|
|
307
|
-
return this.M.
|
|
302
|
+
return this.M.ab(this, message);
|
|
308
303
|
}
|
|
309
304
|
async sendRequest(request) {
|
|
310
|
-
return this.M.
|
|
305
|
+
return this.M.ac(this, request);
|
|
311
306
|
}
|
|
312
|
-
sendErrorResponse(
|
|
307
|
+
sendErrorResponse(aq, an) {
|
|
313
308
|
throw null;
|
|
314
309
|
}
|
|
315
|
-
sendResponse(_request,
|
|
310
|
+
sendResponse(_request, an) {
|
|
316
311
|
throw null;
|
|
317
312
|
}
|
|
318
|
-
async close(
|
|
313
|
+
async close(aj) {
|
|
319
314
|
}
|
|
320
315
|
M;
|
|
321
316
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { j, l, m } from '../common/varUintOps';
|
|
2
|
-
export const
|
|
2
|
+
export const p = (pkgType, requestId, encoded) => {
|
|
3
3
|
const i = [requestId, pkgType];
|
|
4
4
|
const b = j(i);
|
|
5
5
|
const a = new Uint8Array(b);
|
|
6
6
|
m(i, a);
|
|
7
7
|
encoded.push(a);
|
|
8
8
|
};
|
|
9
|
-
export const
|
|
9
|
+
export const s = (buff) => {
|
|
10
10
|
const { h: [requestId, pkgType], k: bodyOffset } = l(2, buff);
|
|
11
11
|
const result = { buff, pkgType, requestId, bodyOffset };
|
|
12
12
|
return result;
|