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