justrun-ws 0.1.2 → 0.2.0
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 +198 -0
- package/lib/bundle.min.js +1 -1
- package/lib/common/AdjustableTimer.js +42 -0
- package/lib/common/DataWrapper.js +16 -16
- package/lib/common/List.js +27 -27
- package/lib/common/utils.js +19 -19
- package/lib/common/varUintOps.js +51 -51
- package/lib/handler/SimpleHandler.js +49 -55
- package/lib/handler/common.js +6 -9
- package/lib/index.d.ts +48 -135
- package/lib/index.js +2 -5
- package/lib/net/BrowserWsNet.js +48 -45
- package/lib/net/LazyConn.js +41 -30
- package/lib/net/PackageWrapper.js +16 -16
- package/lib/net/TimeoutMonitor.js +40 -40
- package/lib/net/TypedPkgHub.js +167 -164
- package/lib/net/hubPkgSerializer.js +4 -4
- package/package.json +1 -1
package/lib/WsClient.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { b } from './common/AdjustableTimer';
|
|
2
|
+
import { CommonError } from './handler/common';
|
|
3
|
+
import { j, n } from './handler/SimpleHandler';
|
|
4
|
+
import { l } from './net/BrowserWsNet';
|
|
5
|
+
import { o, p } from './net/hubPkgSerializer';
|
|
6
|
+
import { JsonRequest } from './net/JsonPackageWrapper';
|
|
7
|
+
import { g } from './net/LazyConn';
|
|
8
|
+
import { t } from './net/TypedPkgHub';
|
|
9
|
+
class r extends JsonRequest {
|
|
10
|
+
}
|
|
11
|
+
export const createNumPkgTypeClient = (getConnStr, timeoutMonitor) => {
|
|
12
|
+
return new WsClient(getConnStr, {
|
|
13
|
+
responsePkgType: 0,
|
|
14
|
+
makePkgBuff: o,
|
|
15
|
+
parsePkgBuff: p,
|
|
16
|
+
timeoutMonitor,
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
export class WsClient {
|
|
20
|
+
constructor(getConnStr, w) {
|
|
21
|
+
const C = new l();
|
|
22
|
+
const _ = this.f = new t(C, w);
|
|
23
|
+
this.h = '';
|
|
24
|
+
this.k = [];
|
|
25
|
+
this.q = 0;
|
|
26
|
+
this.a = new g(() => this.c());
|
|
27
|
+
this.I = getConnStr;
|
|
28
|
+
this.O = this.c;
|
|
29
|
+
_.F = this.F.bind(this);
|
|
30
|
+
_.G = this.G.bind(this);
|
|
31
|
+
}
|
|
32
|
+
registerError(pkgType, wrapperCtor) {
|
|
33
|
+
return this.f.registerError(pkgType, wrapperCtor);
|
|
34
|
+
}
|
|
35
|
+
registerMessage(pkgType, wrapperCtor) {
|
|
36
|
+
return this.f.registerMessage(pkgType, wrapperCtor);
|
|
37
|
+
}
|
|
38
|
+
registerRequest(pkgType, wrapperCtor) {
|
|
39
|
+
return this.f.registerRequest(pkgType, wrapperCtor);
|
|
40
|
+
}
|
|
41
|
+
handleMessageWith(wrapperCtor, handler) {
|
|
42
|
+
const i = (a, e) => {
|
|
43
|
+
return handler(e.data);
|
|
44
|
+
};
|
|
45
|
+
this.f.handleMessageWith(new j(wrapperCtor, i, false));
|
|
46
|
+
}
|
|
47
|
+
handleRequestWith(wrapperCtor, handler) {
|
|
48
|
+
const i = async (a, e) => {
|
|
49
|
+
e.resp = await handler(e.data);
|
|
50
|
+
};
|
|
51
|
+
this.f.handleRequestWith(new n(wrapperCtor, i, false));
|
|
52
|
+
}
|
|
53
|
+
sendMessage(message) {
|
|
54
|
+
return this.a.sendMessage(message);
|
|
55
|
+
}
|
|
56
|
+
async sendRequest(request) {
|
|
57
|
+
await this.a.sendRequest(request);
|
|
58
|
+
return request.resp;
|
|
59
|
+
}
|
|
60
|
+
async sendCustomAuthRequest(_request) {
|
|
61
|
+
throw new Error('Need to call "enableAuthenticate" before using sendCustomAuthRequest');
|
|
62
|
+
}
|
|
63
|
+
resetAuthState() {
|
|
64
|
+
}
|
|
65
|
+
enableAuthenticate(proxyAuthPkgType, authStatusCallback, allowAnonymous) {
|
|
66
|
+
const { O, I, f, a } = this;
|
|
67
|
+
if (this.c === O) {
|
|
68
|
+
f.registerRequest(proxyAuthPkgType, r);
|
|
69
|
+
let token = '';
|
|
70
|
+
let shard = undefined;
|
|
71
|
+
this.c = async () => {
|
|
72
|
+
this.q = -1;
|
|
73
|
+
if (this.m) {
|
|
74
|
+
await this.m.a0();
|
|
75
|
+
}
|
|
76
|
+
for (;;) {
|
|
77
|
+
const d = I(shard);
|
|
78
|
+
const conn = await f.c(d);
|
|
79
|
+
if (this.h || allowAnonymous) {
|
|
80
|
+
const request = new r({
|
|
81
|
+
cid: this.h,
|
|
82
|
+
token,
|
|
83
|
+
});
|
|
84
|
+
try {
|
|
85
|
+
await conn.sendRequest(request);
|
|
86
|
+
const { resp } = request;
|
|
87
|
+
this.h = resp.cid;
|
|
88
|
+
token = resp.token;
|
|
89
|
+
shard = resp.shard;
|
|
90
|
+
if (shard && shard < 0) {
|
|
91
|
+
shard = -shard - 1;
|
|
92
|
+
conn.close();
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
authStatusCallback(this.h);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
if (err instanceof CommonError) {
|
|
99
|
+
if (err.data === 'HitSessionLimit') {
|
|
100
|
+
this.h = '';
|
|
101
|
+
token = '';
|
|
102
|
+
shard = undefined;
|
|
103
|
+
authStatusCallback(this.h);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
conn.close(err);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return conn;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
this.sendCustomAuthRequest = async (request) => {
|
|
114
|
+
await a.sendRequest(request);
|
|
115
|
+
const { resp } = request;
|
|
116
|
+
this.h = resp.cid;
|
|
117
|
+
token = resp.token;
|
|
118
|
+
shard = resp.shard;
|
|
119
|
+
if (shard && shard < 0) {
|
|
120
|
+
shard = -shard - 1;
|
|
121
|
+
a.close();
|
|
122
|
+
}
|
|
123
|
+
authStatusCallback(this.h);
|
|
124
|
+
return resp;
|
|
125
|
+
};
|
|
126
|
+
this.resetAuthState = () => {
|
|
127
|
+
if (this.h) {
|
|
128
|
+
this.h = '';
|
|
129
|
+
token = '';
|
|
130
|
+
shard = undefined;
|
|
131
|
+
a.close();
|
|
132
|
+
authStatusCallback(this.h);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
reconnect(delay) {
|
|
138
|
+
const { a, q } = this;
|
|
139
|
+
if (q > 0) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (this.m) {
|
|
143
|
+
this.m.a6(delay ?? 0);
|
|
144
|
+
}
|
|
145
|
+
else if (delay) {
|
|
146
|
+
this.m = new b(delay, () => this.m = undefined);
|
|
147
|
+
}
|
|
148
|
+
if (!q) {
|
|
149
|
+
a.reconnect();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
onConnectionStatus(listener) {
|
|
153
|
+
let { k } = this;
|
|
154
|
+
if (!k) {
|
|
155
|
+
this.k = k = [];
|
|
156
|
+
}
|
|
157
|
+
k.push(listener);
|
|
158
|
+
}
|
|
159
|
+
async c() {
|
|
160
|
+
const { I, f, m } = this;
|
|
161
|
+
this.q = -1;
|
|
162
|
+
if (m) {
|
|
163
|
+
await m.a0();
|
|
164
|
+
}
|
|
165
|
+
const d = I();
|
|
166
|
+
return f.c(d);
|
|
167
|
+
}
|
|
168
|
+
F(f, conn, R) {
|
|
169
|
+
this.q = 1;
|
|
170
|
+
conn.context = '';
|
|
171
|
+
if (this.k) {
|
|
172
|
+
for (const listener of this.k) {
|
|
173
|
+
listener(true, R);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
G(f, a, E, err) {
|
|
178
|
+
this.q = 0;
|
|
179
|
+
this.a.aa();
|
|
180
|
+
if (!E) {
|
|
181
|
+
this.resetAuthState();
|
|
182
|
+
}
|
|
183
|
+
if (this.k) {
|
|
184
|
+
for (const listener of this.k) {
|
|
185
|
+
listener(false, E, err);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
f;
|
|
190
|
+
a;
|
|
191
|
+
h;
|
|
192
|
+
q;
|
|
193
|
+
I;
|
|
194
|
+
O;
|
|
195
|
+
m;
|
|
196
|
+
k;
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=WsClient.js.map
|
package/lib/bundle.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e={d:(t,s)=>{for(var r in s)e.o(s,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:s[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{yE:()=>w,sr:()=>n,si:()=>i,N3:()=>d,sk:()=>u,wL:()=>p,p6:()=>b,bM:()=>o,h8:()=>h,Kr:()=>f,Yu:()=>B,Kl:()=>C,kX:()=>v,jG:()=>P});class s{constructor(e,t){t?(this.k=e,this.l=void 0):(this.k=void 0,this.l=e)}get data(){return this.l||(this.l=this.decode(this.k)),this.l}set data(e){this.l=e,this.k=void 0}get buff(){return this.k||(this.k=this.encode(this.l)),this.k}k;l}class r extends s{static context;static registerPkgType(e){let t;try{t=this.pkgType}catch{return void Object.defineProperty(this,"pkgType",{value:e,writable:!1,enumerable:!1})}if(t!==e)throw new Error(`Wrapper ${this.name} is already registered with type ${t}`)}static get pkgType(){throw new Error(`Wrapper ${this.name} need to be registered`)}}class n extends r{isError(){return!0}}class o extends r{isMessage(){return!0}}class i extends r{isEvent(){return!0}}class h extends r{constructor(e,t){super(e,t)}isRequest(){return!0}get resp(){return this.r||(this.r=this.decodeResponse(this.s)),this.r}set resp(e){this.r=e,this.s=void 0}get respBuff(){return this.s||(this.s=this.encodeResponse(this.r)),this.s}set respBuff(e){this.r=void 0,this.s=e}r;s}const a=new TextEncoder,c=new TextDecoder;class d extends n{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}}class u extends o{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}}class p extends h{encode(e){const t=JSON.stringify(e);return a.encode(t)}decode(e){const t=c.decode(e);return JSON.parse(t)}encodeResponse(e){const t=JSON.stringify(e);return a.encode(t)}decodeResponse(e){const t=c.decode(e);return JSON.parse(t)}}class l extends d{}const g={message:"Connection is not authenticated"};class f{constructor(e,t,s=!0){this.g=e,this.m=t,this.n=s}get pkgType(){return this.g.pkgType}get pkgName(){return this.g.name}async run(e,t){const{buff:s,bodyOffset:r}=t,n=s.subarray(r),o=new this.g(n,!0);if(void 0===e.context&&this.n)this.wrapUncaughtError(e,g);else try{await this.m(e,o)}catch(t){t.isError||this.wrapUncaughtError(e,t)}void 0===e.context&&await e.close()}async runLocal(e,t){if(void 0===e.context&&this.n)this.wrapUncaughtError(e,g);else try{return await this.m(e,t)}catch(t){t.isError||this.wrapUncaughtError(e,t)}}wrapUncaughtError(e,t){return((e,t)=>((e,t)=>new l(t.message))(0,t))(0,t)}g;m;n}const y=()=>{},k=e=>{let t=0,s=0;for(;t<e.length;)s+=e[t++].length;const r=new Uint8Array(s);for(s=0;t>0;){const n=e[--t];r.set(n,s),s+=n.length}return r};class w{address(){return null}setConnWrapperMaker(e){this.makeWrapper=e}async startListen(e){return!1}async stopListen(){return!1}async connect(e){const t=new WebSocket(e);return new Promise(((e,s)=>{t.onerror=e=>{s(new Error("WebSocket error"))},t.onopen=()=>{e(this.L(t,!0))}}))}L=(e,t)=>{const s=new m(e),{conn:r,onPkgBuff:n,onClose:o}=this.makeWrapper(s,t);return e.onmessage=e=>{e.data.arrayBuffer().then((e=>{n(new Uint8Array(e))}))},e.onerror=e=>{o(new Error("WebSocket error"))},e.onclose=e=>{1e3===e.code&&o()},r};makeWrapper(e,t){throw new Error('Need to call "setConnWrapperMaker" before using')}}class m{constructor(e){this.d=e}get localAddress(){}get localPort(){}get remoteAddress(){}get remoteFamily(){}get remotePort(){}async sendPkgBuff(e){if(this.d)return this.d.send(e);throw new Error("Invalid socket")}async sendPkgBuffV(e){if(this.d){const t=k(e);return this.d.send(t)}throw new Error("Invalid socket")}async close(e){const{d:t}=this;t&&(this.d=void 0,e?t.close(3e3,e.message):t.readyState===WebSocket.OPEN&&t.close(1e3))}hadClosed(){return!this.d}d}const v=(e,t,s)=>{const r=[t,e],n=(e=>{let t,s=0,r=0;for(;s<e.length;)for(r++,t=e[s++];t>>>=7;)r++;return r})(r),o=new Uint8Array(n);((e,t,s=0)=>{let r,n,o=s;for(r=0;r<e.length;r++){n=e[r];let s=127&n;for(;n>>>=7;)t[o++]=128|s,s=127&n;t[o++]=s}if(o<=t.length)return o-s;throw new Error("Buffer overflow")})(r,o),s.push(o)},P=e=>{const{h:[t,s],k:r}=((e,t,s=0)=>{let r,n,o=s,i=0,h=0;const a=[];for(r=0;r<e;r++){do{n=t[o++],i|=(127&n)<<h,h+=7}while(128&n);if(!(void 0!==n&&h<=35))throw new Error("Parsing failed");a.push(i),i=0,h=0}return{h:a,k:o-s}})(2,e);return{buff:e,pkgType:s,requestId:t,bodyOffset:r}};class b{constructor(e){this.A=e}get id(){return this.b?.id??-1}get context(){return this.b?.context}set context(e){this.b&&(this.b.context=e)}localAddress;localPort;remoteAddress;remoteFamily;remotePort;async sendErrorResponse(e,t){return(this.b??await this.connect()).sendErrorResponse(e,t)}async sendResponse(e,t){return(this.b??await this.connect()).sendResponse(e,t)}async sendMessage(e){return(this.b??await this.connect()).sendMessage(e)}async sendRequest(e){return(this.b??await this.connect()).sendRequest(e)}async close(e){const{b:t}=this;if(t)return this.b=void 0,void await t.close(e);this.i&&await this.i.then((t=>t.close(e).then((()=>this.b=void 0))),(()=>this.b=void 0))}async connect(){let{i:e}=this;return e||(e=this.i=new Promise(((e,t)=>{this.A().then((s=>{s?(e(s),this.b=s,this.i=void 0):t(new Error("Connect failed"))}))}))),await e}A;b;i}class E{constructor(){this.e=this,this.f=this}getPrev(){return this.e}getNext(){return this.f}insertBefore(e){this.f=e,this.e=e.e,e.e=this,this.e.f=this}insertAfter(e){this.e=e,this.f=e.f,e.f=this,this.f.e=this}remove(){this.e.f=this.f,this.f.e=this.e}e;f}class x{constructor(){this.t=new E}get g(){return this.t.getNext()}get a(){return this.t.getPrev()}B(e){return e===this.t}C(e){e.insertAfter(this.t)}U(e){e.insertBefore(this.t)}t}class T extends E{}class B{constructor(e,t,s){if(this.D=e,this.y=t,this.w=s,this.j=0,this.x=0,this.o=[],0===e)return this.insert=y,this.update=y,void(this.remove=y);for(let e=0;e<s;e++)this.o.push(new x)}insert(e){this.x||(this.E=setInterval(this.M,this.D)),this.x++,this.o[this.j].C(e)}update(e){e.remove(),this.o[this.j].C(e)}remove(e){e.remove(),this.x--,this.x||clearInterval(this.E)}M=()=>{const{y:e,w:t,o:s}=this;if(this.j++,this.j===t&&(this.j=0),e){let r=this.j+e;r>=t&&(r-=t);const n=s[r];for(let e=n.g;!n.B(e);e=e.getNext())e.keepAlive()}const r=s[this.j];for(let e=r.g;!r.B(e);e=e.getNext())e.timeout()};D;y;w;j;x;E;o}class C{constructor(e,t){const{responsePkgType:s,timeoutMonitor:r,makePkgBuff:n,parsePkgBuff:o}=t;this.responsePkgType=s,this.context=void 0,this.onConnOpen=void 0,this.onConnClose=void 0,this.p=0,this.q=new Map,this.c=new Map,this.$=new Map,this.F=n,this.G=o,this.u=e,this.z=r,this.run=this.N;const i=[];this.F(s,2097152,i),this.H={makePkgBuff:n,responsePkgType:s,j:k(i)},this.c.set(s,this),this.u.setConnWrapperMaker(((e,t)=>{do{this.p++}while(this.q.has(2097151&this.p));const s=new N(this.H,e,2097151&this.p);this.q.set(s.id,s),this.z?.insert(s);const r={conn:s,onPkgBuff:e=>this.O(s,e),onClose:e=>this.P(s,e)};return this.onConnOpen?.(this,s,!!t),r}))}responsePkgType;context;onConnOpen;onConnClose;address(){return this.u.address()}getConn(e){return this.q.get(e)}connect(e){return this.u.connect(e)}async connectLocal(){do{this.p++}while(this.q.has(2097151&this.p));const e=new R(2097151&this.p,this);return this.q.set(e.id,e),e}async startServer(e){return this.u.startListen(e)}async stopServer(){return this.u.stopListen()}sendMessageLocal(e,t){const s=t.constructor.pkgType,r=this.c.get(s);if(r)return r?.runLocal(e,t);throw new Error("Invalid package")}sendRequestLocal(e,t){const s=t.constructor.pkgType,r=this.c.get(s);if(r)return r?.runLocal(e,t);throw new Error("Invalid package")}registerError(e,t){t.registerPkgType(e),this.$.set(e,t);const s=this.c.get(this.responsePkgType);this.c.set(e,s)}registerMessage(e,t){t.registerPkgType(e)}registerRequest(e,t){t.registerPkgType(e)}handleMessageWith(e){const t=e.pkgType;if(typeof t!=typeof this.responsePkgType)throw new Error(`Wrapper ${e.pkgName} has an incompatible pkgType ${typeof t}`);if(this.c.has(t))throw new Error(`Wrapper ${e.pkgName} is already handled by another handler`);return this.c.set(t,e),e}handleRequestWith(e){const t=e.pkgType;if(typeof t!=typeof this.responsePkgType)throw new Error(`Wrapper ${e.pkgName} has an incompatible pkgType ${typeof t}`);if(this.c.has(t))throw new Error(`Wrapper ${e.pkgName} is already handled by another handler`);return this.c.set(t,e),e}removeHandler(e){const t=e.pkgType;return this.c.get(t)===e&&(this.c.delete(t),!0)}O(e,t){this.z?.update(e);const s=this.G(t),r=this.c.get(s.pkgType);r?r.run(e,s):e.close(new Error("Invalid package"))}P(e,t){const s=e.id;this.q.delete(s),this.z?.remove(e),e.onClose(t),this.onConnClose?.(this,e,e.a.hadClosed(),t)}N(e,t){const{buff:s,pkgType:r,requestId:n,bodyOffset:o}=t;if(r===this.responsePkgType){const t=s.subarray(o);e.I(n,t)}else{const t=this.$.get(r);if(t){const r=new t(s.subarray(o),!0);e.Q(n,r)}}}H;p;F;G;u;q;c;$;z}class N extends T{constructor(e,t,s){super(),this.context=void 0,this.id=s,this.a=t,this.v=e,this.h=void 0,this.J=0}context;id;a;get localAddress(){return this.a.localAddress}get localPort(){return this.a.localPort}get remoteAddress(){return this.a.remoteAddress}get remoteFamily(){return this.a.remoteFamily}get remotePort(){return this.a.remotePort}sendMessage(e){const t=e.constructor.pkgType,s=[e.buff];return this.v.makePkgBuff(t,0,s),this.a.sendPkgBuffV(s)}sendRequest(e){return this.h=new Map,this.sendRequest=this.K,this.I=this.R,this.onClose=this.S,this.K(e)}sendErrorResponse(e,t){const s=e.constructor.pkgType,r=[e.buff];return this.v.makePkgBuff(s,t,r),this.a.sendPkgBuffV(r)}sendResponse(e,t){const s=[e.respBuff],{makePkgBuff:r,responsePkgType:n}=this.v;return r(n,t,s),this.a.sendPkgBuffV(s)}async close(e){await this.a.close(e)}I(e,t){2097152===e&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}Q(e,t){const s=this.h.get(e);if(s)return this.h.delete(e),s.g(t)}keepAlive(){this.a.sendPkgBuff(this.v.j).catch(y)}timeout(){this.close(new Error("Connection timeout"))}onClose(e){}async K(e){const t=2097151&this.J++,s=new Promise(((e,s)=>{const r={f:e,g:s};this.h.set(t,r)})),r=e.constructor.pkgType,n=[e.buff];this.v.makePkgBuff(r,t,n),this.a.sendPkgBuffV(n),e.respBuff=await s}R(e,t){const s=this.h.get(e);if(s)return this.h.delete(e),void s.f(t);2097152===e&&void 0===this.context&&this.close(new Error("Authenticating timeout"))}S(e){const t=this.h;if(t.size>0){e||(e=new Error("Connection is closed"));for(const[s,r]of t)t.delete(s),r.g(e)}this.h=void 0}v;h;J}class R{constructor(e,t){this.context=void 0,this.id=e,this._=t}context;id;localAddress;localPort;remoteAddress;remotePort;get remoteFamily(){return"Local"}async sendMessage(e){return this._.sendMessageLocal(this,e)}async sendRequest(e){return this._.sendRequestLocal(this,e)}sendErrorResponse(e,t){throw null}sendResponse(e,t){throw null}async close(e){}_}var M=t.yE,W=t.sr,q=t.si,A=t.N3,O=t.sk,L=t.wL,S=t.p6,j=t.bM,J=t.h8,$=t.Kr,I=t.Yu,U=t.Kl,F=t.kX,z=t.jG;export{M as BrowserWsNet,W as ErrorWrapper,q as EventWrapper,A as JsonError,O as JsonMessage,L as JsonRequest,S as LazyConn,j as MessageWrapper,J as RequestWrapper,$ as SimpleMessageHandler,I as TimeoutMonitor,U as TypedPkgHub,F as makeNumTypePkgBuff,z as parseNumTypePkgBuff};
|
|
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};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class b {
|
|
2
|
+
constructor(a, c) {
|
|
3
|
+
this.Z = new Promise((f) => {
|
|
4
|
+
this.H = f;
|
|
5
|
+
});
|
|
6
|
+
this.N = c;
|
|
7
|
+
if (a > 0) {
|
|
8
|
+
this.w = setTimeout(() => {
|
|
9
|
+
this.w = undefined;
|
|
10
|
+
this.N?.();
|
|
11
|
+
this.H();
|
|
12
|
+
}, a);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
this.H();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
a6(a) {
|
|
19
|
+
if (this.w) {
|
|
20
|
+
clearTimeout(this.w);
|
|
21
|
+
}
|
|
22
|
+
if (a > 0) {
|
|
23
|
+
this.w = setTimeout(() => {
|
|
24
|
+
this.w = undefined;
|
|
25
|
+
this.N?.();
|
|
26
|
+
this.H();
|
|
27
|
+
}, a);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this.w = undefined;
|
|
31
|
+
this.H();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async a0() {
|
|
35
|
+
await this.Z;
|
|
36
|
+
}
|
|
37
|
+
H;
|
|
38
|
+
Z;
|
|
39
|
+
w;
|
|
40
|
+
N;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=AdjustableTimer.js.map
|
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
export class DataWrapper {
|
|
2
|
-
constructor(
|
|
2
|
+
constructor(a, needParse) {
|
|
3
3
|
if (needParse) {
|
|
4
|
-
this.
|
|
5
|
-
this.
|
|
4
|
+
this.r = a;
|
|
5
|
+
this.s = undefined;
|
|
6
6
|
}
|
|
7
7
|
else {
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
8
|
+
this.r = undefined;
|
|
9
|
+
this.s = a;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
get data() {
|
|
13
|
-
if (!this.
|
|
14
|
-
this.
|
|
13
|
+
if (!this.s) {
|
|
14
|
+
this.s = this.decode(this.r);
|
|
15
15
|
}
|
|
16
|
-
return this.
|
|
16
|
+
return this.s;
|
|
17
17
|
}
|
|
18
|
-
set data(
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
18
|
+
set data(b) {
|
|
19
|
+
this.s = b;
|
|
20
|
+
this.r = undefined;
|
|
21
21
|
}
|
|
22
22
|
get buff() {
|
|
23
|
-
if (!this.
|
|
24
|
-
this.
|
|
23
|
+
if (!this.r) {
|
|
24
|
+
this.r = this.encode(this.s);
|
|
25
25
|
}
|
|
26
|
-
return this.
|
|
26
|
+
return this.r;
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
r;
|
|
29
|
+
s;
|
|
30
30
|
}
|
|
31
31
|
//# sourceMappingURL=DataWrapper.js.map
|
package/lib/common/List.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
export class ListNode {
|
|
2
2
|
constructor() {
|
|
3
|
-
this.
|
|
4
|
-
this.
|
|
3
|
+
this.d = this;
|
|
4
|
+
this.i = this;
|
|
5
5
|
}
|
|
6
6
|
getPrev() {
|
|
7
|
-
return this.
|
|
7
|
+
return this.d;
|
|
8
8
|
}
|
|
9
9
|
getNext() {
|
|
10
|
-
return this.
|
|
10
|
+
return this.i;
|
|
11
11
|
}
|
|
12
12
|
insertBefore(node) {
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
node.
|
|
16
|
-
this.
|
|
13
|
+
this.i = node;
|
|
14
|
+
this.d = node.d;
|
|
15
|
+
node.d = this;
|
|
16
|
+
this.d.i = this;
|
|
17
17
|
}
|
|
18
18
|
insertAfter(node) {
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
node.
|
|
22
|
-
this.
|
|
19
|
+
this.d = node;
|
|
20
|
+
this.i = node.i;
|
|
21
|
+
node.i = this;
|
|
22
|
+
this.i.d = this;
|
|
23
23
|
}
|
|
24
24
|
remove() {
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
25
|
+
this.d.i = this.i;
|
|
26
|
+
this.i.d = this.d;
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
d;
|
|
29
|
+
i;
|
|
30
30
|
}
|
|
31
31
|
export class c {
|
|
32
32
|
constructor() {
|
|
33
|
-
this.
|
|
33
|
+
this._ = new ListNode();
|
|
34
34
|
}
|
|
35
|
-
get
|
|
36
|
-
return this.
|
|
35
|
+
get e() {
|
|
36
|
+
return this._.getNext();
|
|
37
37
|
}
|
|
38
38
|
get a() {
|
|
39
|
-
return this.
|
|
39
|
+
return this._.getPrev();
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
return node === this.
|
|
41
|
+
P(node) {
|
|
42
|
+
return node === this._;
|
|
43
43
|
}
|
|
44
|
-
|
|
45
|
-
node.insertAfter(this.
|
|
44
|
+
Q(node) {
|
|
45
|
+
node.insertAfter(this._);
|
|
46
46
|
}
|
|
47
|
-
|
|
48
|
-
node.insertBefore(this.
|
|
47
|
+
ab(node) {
|
|
48
|
+
node.insertBefore(this._);
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
_;
|
|
51
51
|
}
|
|
52
52
|
//# sourceMappingURL=List.js.map
|
package/lib/common/utils.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
export const j = (
|
|
2
|
-
return new Promise((f) => setTimeout(f,
|
|
1
|
+
export const j = (e) => {
|
|
2
|
+
return new Promise((f) => setTimeout(f, e));
|
|
3
3
|
};
|
|
4
|
-
export const
|
|
5
|
-
export const k = (
|
|
6
|
-
return (Math.random() *
|
|
4
|
+
export const l = () => { };
|
|
5
|
+
export const k = (i = 0x100000000) => {
|
|
6
|
+
return (Math.random() * i) >>> 0;
|
|
7
7
|
};
|
|
8
8
|
export const m = (d) => {
|
|
9
|
-
let c = 5381,
|
|
10
|
-
while (
|
|
11
|
-
c = ((c << 5) + c) + d.charCodeAt(
|
|
9
|
+
let c = 5381, a = 0;
|
|
10
|
+
while (a < d.length) {
|
|
11
|
+
c = ((c << 5) + c) + d.charCodeAt(a++);
|
|
12
12
|
}
|
|
13
13
|
return c >>> 0;
|
|
14
14
|
};
|
|
15
|
-
export const
|
|
16
|
-
let b = 0;
|
|
15
|
+
export const y = (h) => {
|
|
17
16
|
let a = 0;
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
let b = 0;
|
|
18
|
+
while (a < h.length) {
|
|
19
|
+
b += h[a++].length;
|
|
20
20
|
}
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
while (
|
|
24
|
-
const buff =
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const g = new Uint8Array(b);
|
|
22
|
+
b = 0;
|
|
23
|
+
while (a > 0) {
|
|
24
|
+
const buff = h[--a];
|
|
25
|
+
g.set(buff, b);
|
|
26
|
+
b += buff.length;
|
|
27
27
|
}
|
|
28
|
-
return
|
|
28
|
+
return g;
|
|
29
29
|
};
|
|
30
30
|
//# sourceMappingURL=utils.js.map
|
package/lib/common/varUintOps.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
export const o = (
|
|
2
|
-
let
|
|
3
|
-
while (
|
|
4
|
-
|
|
1
|
+
export const o = (b) => {
|
|
2
|
+
let g = 1;
|
|
3
|
+
while (b >>>= 7) {
|
|
4
|
+
g++;
|
|
5
5
|
}
|
|
6
|
-
return
|
|
6
|
+
return g;
|
|
7
7
|
};
|
|
8
8
|
export const j = (i) => {
|
|
9
|
-
let
|
|
10
|
-
let
|
|
11
|
-
let
|
|
12
|
-
while (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
while (
|
|
16
|
-
|
|
9
|
+
let b;
|
|
10
|
+
let a = 0;
|
|
11
|
+
let g = 0;
|
|
12
|
+
while (a < i.length) {
|
|
13
|
+
g++;
|
|
14
|
+
b = i[a++];
|
|
15
|
+
while (b >>>= 7) {
|
|
16
|
+
g++;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
return
|
|
19
|
+
return g;
|
|
20
20
|
};
|
|
21
|
-
export const p = (buffer,
|
|
21
|
+
export const p = (buffer, e = 0) => {
|
|
22
22
|
let c;
|
|
23
|
-
let d =
|
|
24
|
-
let
|
|
25
|
-
let
|
|
23
|
+
let d = e;
|
|
24
|
+
let b = 0;
|
|
25
|
+
let f = 0;
|
|
26
26
|
do {
|
|
27
27
|
c = buffer[d++];
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
b |= (c & 127) << f;
|
|
29
|
+
f += 7;
|
|
30
30
|
} while (c & 128);
|
|
31
|
-
if (c !== undefined &&
|
|
31
|
+
if (c !== undefined && f <= 35) {
|
|
32
32
|
return {
|
|
33
|
-
|
|
34
|
-
k: d -
|
|
33
|
+
b: b,
|
|
34
|
+
k: d - e,
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
else {
|
|
38
38
|
throw new Error('Parsing failed');
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
|
-
export const l = (n, buffer,
|
|
42
|
-
let
|
|
41
|
+
export const l = (n, buffer, e = 0) => {
|
|
42
|
+
let a;
|
|
43
43
|
let c;
|
|
44
|
-
let d =
|
|
45
|
-
let
|
|
46
|
-
let
|
|
44
|
+
let d = e;
|
|
45
|
+
let b = 0;
|
|
46
|
+
let f = 0;
|
|
47
47
|
const h = [];
|
|
48
|
-
for (
|
|
48
|
+
for (a = 0; a < n; a++) {
|
|
49
49
|
do {
|
|
50
50
|
c = buffer[d++];
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
b |= (c & 127) << f;
|
|
52
|
+
f += 7;
|
|
53
53
|
} while (c & 128);
|
|
54
|
-
if (c !== undefined &&
|
|
55
|
-
h.push(
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
if (c !== undefined && f <= 35) {
|
|
55
|
+
h.push(b);
|
|
56
|
+
b = 0;
|
|
57
|
+
f = 0;
|
|
58
58
|
}
|
|
59
59
|
else {
|
|
60
60
|
throw new Error('Parsing failed');
|
|
@@ -62,39 +62,39 @@ export const l = (n, buffer, f = 0) => {
|
|
|
62
62
|
}
|
|
63
63
|
return {
|
|
64
64
|
h,
|
|
65
|
-
k: d -
|
|
65
|
+
k: d - e,
|
|
66
66
|
};
|
|
67
67
|
};
|
|
68
|
-
export const q = (
|
|
69
|
-
let d =
|
|
70
|
-
let c =
|
|
71
|
-
while (
|
|
68
|
+
export const q = (b, buffer, e = 0) => {
|
|
69
|
+
let d = e;
|
|
70
|
+
let c = b & 127;
|
|
71
|
+
while (b >>>= 7) {
|
|
72
72
|
buffer[d++] = c | 128;
|
|
73
|
-
c =
|
|
73
|
+
c = b & 127;
|
|
74
74
|
}
|
|
75
75
|
buffer[d++] = c;
|
|
76
76
|
if (d <= buffer.length) {
|
|
77
|
-
return d -
|
|
77
|
+
return d - e;
|
|
78
78
|
}
|
|
79
79
|
else {
|
|
80
80
|
throw new Error('Buffer overflow');
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
|
-
export const m = (h, buffer,
|
|
84
|
-
let b;
|
|
83
|
+
export const m = (h, buffer, e = 0) => {
|
|
85
84
|
let a;
|
|
86
|
-
let
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
85
|
+
let b;
|
|
86
|
+
let d = e;
|
|
87
|
+
for (a = 0; a < h.length; a++) {
|
|
88
|
+
b = h[a];
|
|
89
|
+
let c = b & 127;
|
|
90
|
+
while (b >>>= 7) {
|
|
91
91
|
buffer[d++] = c | 128;
|
|
92
|
-
c =
|
|
92
|
+
c = b & 127;
|
|
93
93
|
}
|
|
94
94
|
buffer[d++] = c;
|
|
95
95
|
}
|
|
96
96
|
if (d <= buffer.length) {
|
|
97
|
-
return d -
|
|
97
|
+
return d - e;
|
|
98
98
|
}
|
|
99
99
|
else {
|
|
100
100
|
throw new Error('Buffer overflow');
|