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