justrun-ws 0.1.3 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/WsClient.js +211 -0
- package/lib/bundle.min.js +1 -1
- package/lib/common/AdjustableTimer.js +42 -0
- package/lib/common/DataWrapper.js +14 -16
- package/lib/common/List.js +21 -21
- package/lib/common/utils.js +20 -20
- package/lib/common/varUintOps.js +51 -51
- package/lib/handler/SimpleHandler.js +49 -47
- package/lib/handler/common.js +2 -3
- package/lib/index.d.ts +60 -142
- package/lib/index.js +2 -5
- package/lib/net/BrowserWsNet.js +51 -47
- package/lib/net/LazyConn.js +42 -31
- package/lib/net/NetErrorString.js +14 -0
- package/lib/net/PackageWrapper.js +16 -16
- package/lib/net/TimeoutMonitor.js +39 -39
- package/lib/net/TypedPkgHub.js +173 -175
- package/lib/net/hubPkgSerializer.js +2 -2
- package/package.json +1 -1
package/lib/net/TypedPkgHub.js
CHANGED
|
@@ -1,94 +1,92 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { y, l } from '../common/utils';
|
|
2
|
+
import { NetErrorString } from './NetErrorString';
|
|
2
3
|
import { MonitorConn } from './TimeoutMonitor';
|
|
3
|
-
export class
|
|
4
|
-
constructor(
|
|
5
|
-
const { responsePkgType, timeoutMonitor, makePkgBuff, parsePkgBuff } =
|
|
4
|
+
export class v {
|
|
5
|
+
constructor(F, w) {
|
|
6
|
+
const { responsePkgType, timeoutMonitor, makePkgBuff, parsePkgBuff } = w;
|
|
6
7
|
this.responsePkgType = responsePkgType;
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const j = [];
|
|
21
|
-
this.F(responsePkgType, 0x200000, j);
|
|
22
|
-
this.H = {
|
|
8
|
+
this.s = 0;
|
|
9
|
+
this.t = new Map();
|
|
10
|
+
this.f = new Map();
|
|
11
|
+
this.L = new Map();
|
|
12
|
+
this.T = makePkgBuff;
|
|
13
|
+
this.U = parsePkgBuff;
|
|
14
|
+
this.A = F;
|
|
15
|
+
this.J = timeoutMonitor;
|
|
16
|
+
const S = this;
|
|
17
|
+
S.$ = this.a7;
|
|
18
|
+
const u = [];
|
|
19
|
+
this.T(responsePkgType, 0x200000, u);
|
|
20
|
+
this.V = {
|
|
23
21
|
makePkgBuff: makePkgBuff,
|
|
24
22
|
responsePkgType: responsePkgType,
|
|
25
|
-
|
|
23
|
+
u: y(u),
|
|
26
24
|
};
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
25
|
+
this.f.set(responsePkgType, S);
|
|
26
|
+
this.A.P((b, q) => {
|
|
29
27
|
do {
|
|
30
|
-
this.
|
|
31
|
-
} while (this.
|
|
32
|
-
const conn = new
|
|
33
|
-
this.
|
|
34
|
-
this.
|
|
35
|
-
const
|
|
28
|
+
this.s++;
|
|
29
|
+
} while (this.t.has(this.s & 0x1fffff));
|
|
30
|
+
const conn = new x(this.V, b, this.s & 0x1fffff);
|
|
31
|
+
this.t.set(conn.id, conn);
|
|
32
|
+
this.J?.insert(conn);
|
|
33
|
+
const g = {
|
|
36
34
|
conn,
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
E: (buff) => this.a8(conn, buff),
|
|
36
|
+
n: (err) => this.a9(conn, err),
|
|
39
37
|
};
|
|
40
|
-
this.
|
|
41
|
-
return
|
|
38
|
+
this.K?.(this, conn, !!q);
|
|
39
|
+
return g;
|
|
42
40
|
});
|
|
43
41
|
}
|
|
44
42
|
responsePkgType;
|
|
45
43
|
context;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return this.
|
|
44
|
+
K;
|
|
45
|
+
B;
|
|
46
|
+
o() {
|
|
47
|
+
return this.A.o();
|
|
50
48
|
}
|
|
51
|
-
|
|
52
|
-
return this.
|
|
49
|
+
a3(z) {
|
|
50
|
+
return this.t.get(z);
|
|
53
51
|
}
|
|
54
|
-
|
|
55
|
-
return this.
|
|
52
|
+
c(e) {
|
|
53
|
+
return this.A.c(e);
|
|
56
54
|
}
|
|
57
|
-
async
|
|
55
|
+
async a4() {
|
|
58
56
|
do {
|
|
59
|
-
this.
|
|
60
|
-
} while (this.
|
|
61
|
-
const conn = new
|
|
62
|
-
this.
|
|
57
|
+
this.s++;
|
|
58
|
+
} while (this.t.has(this.s & 0x1fffff));
|
|
59
|
+
const conn = new aa(this.s & 0x1fffff, this);
|
|
60
|
+
this.t.set(conn.id, conn);
|
|
63
61
|
return conn;
|
|
64
62
|
}
|
|
65
|
-
async
|
|
66
|
-
return this.
|
|
63
|
+
async a5(e) {
|
|
64
|
+
return this.A.Q(e);
|
|
67
65
|
}
|
|
68
|
-
async
|
|
69
|
-
return this.
|
|
66
|
+
async a6() {
|
|
67
|
+
return this.A.R();
|
|
70
68
|
}
|
|
71
|
-
|
|
69
|
+
ab(conn, message) {
|
|
72
70
|
const pkgType = message.constructor.pkgType;
|
|
73
|
-
const handler = this.
|
|
71
|
+
const handler = this.f.get(pkgType);
|
|
74
72
|
if (handler) {
|
|
75
|
-
return handler?.
|
|
73
|
+
return handler?.r(conn, message);
|
|
76
74
|
}
|
|
77
|
-
throw new Error(
|
|
75
|
+
throw new Error(NetErrorString.D);
|
|
78
76
|
}
|
|
79
|
-
|
|
77
|
+
ac(conn, request) {
|
|
80
78
|
const pkgType = request.constructor.pkgType;
|
|
81
|
-
const handler = this.
|
|
79
|
+
const handler = this.f.get(pkgType);
|
|
82
80
|
if (handler) {
|
|
83
|
-
return handler?.
|
|
81
|
+
return handler?.r(conn, request);
|
|
84
82
|
}
|
|
85
|
-
throw new Error(
|
|
83
|
+
throw new Error(NetErrorString.D);
|
|
86
84
|
}
|
|
87
85
|
registerError(pkgType, wrapperCtor) {
|
|
88
86
|
wrapperCtor.registerPkgType(pkgType);
|
|
89
|
-
this
|
|
90
|
-
const
|
|
91
|
-
this.
|
|
87
|
+
this.L.set(pkgType, wrapperCtor);
|
|
88
|
+
const ad = this.f.get(this.responsePkgType);
|
|
89
|
+
this.f.set(pkgType, ad);
|
|
92
90
|
}
|
|
93
91
|
registerMessage(pkgType, wrapperCtor) {
|
|
94
92
|
wrapperCtor.registerPkgType(pkgType);
|
|
@@ -99,197 +97,197 @@ export class TypedPkgHub {
|
|
|
99
97
|
handleMessageWith(handler) {
|
|
100
98
|
const pkgType = handler.pkgType;
|
|
101
99
|
if (typeof pkgType !== typeof this.responsePkgType) {
|
|
102
|
-
throw new Error(`Wrapper ${handler.
|
|
100
|
+
throw new Error(`Wrapper ${handler.m} has an incompatible pkgType ${typeof pkgType}`);
|
|
103
101
|
}
|
|
104
|
-
if (this.
|
|
105
|
-
throw new Error(`Wrapper ${handler.
|
|
102
|
+
if (this.f.has(pkgType)) {
|
|
103
|
+
throw new Error(`Wrapper ${handler.m} is already handled by another handler`);
|
|
106
104
|
}
|
|
107
|
-
this.
|
|
105
|
+
this.f.set(pkgType, handler);
|
|
108
106
|
return handler;
|
|
109
107
|
}
|
|
110
108
|
handleRequestWith(handler) {
|
|
111
109
|
const pkgType = handler.pkgType;
|
|
112
110
|
if (typeof pkgType !== typeof this.responsePkgType) {
|
|
113
|
-
throw new Error(`Wrapper ${handler.
|
|
111
|
+
throw new Error(`Wrapper ${handler.m} has an incompatible pkgType ${typeof pkgType}`);
|
|
114
112
|
}
|
|
115
|
-
if (this.
|
|
116
|
-
throw new Error(`Wrapper ${handler.
|
|
113
|
+
if (this.f.has(pkgType)) {
|
|
114
|
+
throw new Error(`Wrapper ${handler.m} is already handled by another handler`);
|
|
117
115
|
}
|
|
118
|
-
this.
|
|
116
|
+
this.f.set(pkgType, handler);
|
|
119
117
|
return handler;
|
|
120
118
|
}
|
|
121
|
-
|
|
119
|
+
ao(handler) {
|
|
122
120
|
const pkgType = handler.pkgType;
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
125
|
-
this.
|
|
121
|
+
const ae = this.f.get(pkgType);
|
|
122
|
+
if (ae === handler) {
|
|
123
|
+
this.f.delete(pkgType);
|
|
126
124
|
return true;
|
|
127
125
|
}
|
|
128
126
|
return false;
|
|
129
127
|
}
|
|
130
|
-
|
|
131
|
-
this.
|
|
132
|
-
const
|
|
133
|
-
const handler = this.
|
|
128
|
+
a8(conn, buff) {
|
|
129
|
+
this.J?.update(conn);
|
|
130
|
+
const a = this.U(buff);
|
|
131
|
+
const handler = this.f.get(a.pkgType);
|
|
134
132
|
if (handler) {
|
|
135
|
-
handler
|
|
133
|
+
handler.$(conn, a);
|
|
136
134
|
return;
|
|
137
135
|
}
|
|
138
|
-
conn.close(new Error(
|
|
136
|
+
conn.close(new Error(NetErrorString.D));
|
|
139
137
|
}
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
this.
|
|
143
|
-
this.
|
|
144
|
-
conn.
|
|
145
|
-
this.
|
|
138
|
+
a9(conn, err) {
|
|
139
|
+
const z = conn.id;
|
|
140
|
+
this.t.delete(z);
|
|
141
|
+
this.J?.remove(conn);
|
|
142
|
+
conn.n(err);
|
|
143
|
+
this.B?.(this, conn, conn.b.O(), err);
|
|
146
144
|
}
|
|
147
|
-
|
|
148
|
-
const { buff, pkgType, requestId, bodyOffset } =
|
|
145
|
+
a7(conn, a) {
|
|
146
|
+
const { buff, pkgType, requestId, bodyOffset } = a;
|
|
149
147
|
if (pkgType === this.responsePkgType) {
|
|
150
|
-
const
|
|
151
|
-
conn.
|
|
148
|
+
const h = buff.subarray(bodyOffset);
|
|
149
|
+
conn.X(requestId, h);
|
|
152
150
|
}
|
|
153
151
|
else {
|
|
154
|
-
const
|
|
155
|
-
if (
|
|
156
|
-
const
|
|
157
|
-
const
|
|
158
|
-
conn.
|
|
152
|
+
const Y = this.L.get(pkgType);
|
|
153
|
+
if (Y) {
|
|
154
|
+
const h = buff.subarray(bodyOffset);
|
|
155
|
+
const af = new Y(h, true);
|
|
156
|
+
conn.ag(requestId, af);
|
|
159
157
|
}
|
|
160
158
|
}
|
|
161
159
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
160
|
+
V;
|
|
161
|
+
s;
|
|
162
|
+
T;
|
|
163
|
+
U;
|
|
164
|
+
A;
|
|
165
|
+
t;
|
|
166
|
+
f;
|
|
167
|
+
L;
|
|
168
|
+
J;
|
|
171
169
|
}
|
|
172
|
-
class
|
|
173
|
-
constructor(
|
|
170
|
+
class x extends MonitorConn {
|
|
171
|
+
constructor(w, b, id) {
|
|
174
172
|
super();
|
|
175
|
-
this.context = undefined;
|
|
176
173
|
this.id = id;
|
|
177
|
-
this.
|
|
178
|
-
this.
|
|
179
|
-
this.
|
|
180
|
-
this.J = 0;
|
|
174
|
+
this.b = b;
|
|
175
|
+
this.C = w;
|
|
176
|
+
this.Z = 0;
|
|
181
177
|
}
|
|
182
178
|
context;
|
|
183
179
|
id;
|
|
184
|
-
|
|
180
|
+
b;
|
|
185
181
|
get localAddress() {
|
|
186
|
-
return this.
|
|
182
|
+
return this.b.localAddress;
|
|
187
183
|
}
|
|
188
184
|
get localPort() {
|
|
189
|
-
return this.
|
|
185
|
+
return this.b.localPort;
|
|
190
186
|
}
|
|
191
187
|
get remoteAddress() {
|
|
192
|
-
return this.
|
|
188
|
+
return this.b.remoteAddress;
|
|
193
189
|
}
|
|
194
190
|
get remoteFamily() {
|
|
195
|
-
return this.
|
|
191
|
+
return this.b.remoteFamily;
|
|
196
192
|
}
|
|
197
193
|
get remotePort() {
|
|
198
|
-
return this.
|
|
194
|
+
return this.b.remotePort;
|
|
199
195
|
}
|
|
200
196
|
sendMessage(message) {
|
|
201
197
|
const pkgType = message.constructor.pkgType;
|
|
202
198
|
const encoded = [message.buff];
|
|
203
|
-
this.
|
|
204
|
-
return this.
|
|
199
|
+
this.C.makePkgBuff(pkgType, 0, encoded);
|
|
200
|
+
return this.b.p(encoded);
|
|
205
201
|
}
|
|
206
202
|
sendRequest(request) {
|
|
207
|
-
this.
|
|
208
|
-
this.sendRequest = this.
|
|
209
|
-
this.
|
|
210
|
-
this.
|
|
211
|
-
return this.
|
|
203
|
+
this.k = new Map();
|
|
204
|
+
this.sendRequest = this.a0;
|
|
205
|
+
this.X = this.ah;
|
|
206
|
+
this.n = this.ai;
|
|
207
|
+
return this.a0(request);
|
|
212
208
|
}
|
|
213
209
|
sendErrorResponse(error, requestId) {
|
|
214
210
|
const pkgType = error.constructor.pkgType;
|
|
215
211
|
const encoded = [error.buff];
|
|
216
|
-
this.
|
|
217
|
-
return this.
|
|
212
|
+
this.C.makePkgBuff(pkgType, requestId, encoded);
|
|
213
|
+
return this.b.p(encoded);
|
|
218
214
|
}
|
|
219
215
|
sendResponse(request, requestId) {
|
|
220
216
|
const encoded = [request.respBuff];
|
|
221
|
-
const { makePkgBuff, responsePkgType } = this.
|
|
217
|
+
const { makePkgBuff, responsePkgType } = this.C;
|
|
222
218
|
makePkgBuff(responsePkgType, requestId, encoded);
|
|
223
|
-
return this.
|
|
219
|
+
return this.b.p(encoded);
|
|
224
220
|
}
|
|
225
221
|
async close(err) {
|
|
226
|
-
await this.
|
|
222
|
+
await this.b.close(err);
|
|
227
223
|
}
|
|
228
|
-
|
|
224
|
+
X(requestId, ap) {
|
|
229
225
|
if (requestId === 0x200000 && this.context === undefined) {
|
|
230
|
-
this.close(new Error(
|
|
226
|
+
this.close(new Error(NetErrorString.I));
|
|
231
227
|
}
|
|
232
228
|
}
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
if (
|
|
236
|
-
this.
|
|
237
|
-
return
|
|
229
|
+
ag(requestId, error) {
|
|
230
|
+
const j = this.k.get(requestId);
|
|
231
|
+
if (j) {
|
|
232
|
+
this.k.delete(requestId);
|
|
233
|
+
return j.i(error);
|
|
238
234
|
}
|
|
239
235
|
}
|
|
240
236
|
keepAlive() {
|
|
241
|
-
this.
|
|
237
|
+
this.b.N(this.C.u).catch(l);
|
|
242
238
|
}
|
|
243
239
|
timeout() {
|
|
244
|
-
this.close(new Error(
|
|
240
|
+
this.close(new Error(NetErrorString.a2));
|
|
245
241
|
}
|
|
246
|
-
|
|
242
|
+
n(aj) {
|
|
247
243
|
}
|
|
248
|
-
async
|
|
249
|
-
const requestId = (this.
|
|
250
|
-
const B = new Promise((f, g) => {
|
|
251
|
-
const C = { f, g };
|
|
252
|
-
this.h.set(requestId, C);
|
|
253
|
-
});
|
|
244
|
+
async a0(request) {
|
|
245
|
+
const requestId = (this.Z++) & 0x1fffff;
|
|
254
246
|
const pkgType = request.constructor.pkgType;
|
|
255
247
|
const encoded = [request.buff];
|
|
256
|
-
this.
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
248
|
+
this.C.makePkgBuff(pkgType, requestId, encoded);
|
|
249
|
+
const ak = new Promise((d, i) => {
|
|
250
|
+
const al = { d, i };
|
|
251
|
+
this.k.set(requestId, al);
|
|
252
|
+
this.b.p(encoded).catch((err) => {
|
|
253
|
+
this.k.delete(requestId);
|
|
254
|
+
i(err);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
request.respBuff = await ak;
|
|
258
|
+
}
|
|
259
|
+
ah(requestId, h) {
|
|
260
|
+
const j = this.k.get(requestId);
|
|
261
|
+
if (j) {
|
|
262
|
+
this.k.delete(requestId);
|
|
263
|
+
j.d(h);
|
|
265
264
|
return;
|
|
266
265
|
}
|
|
267
266
|
if (requestId === 0x200000 && this.context === undefined) {
|
|
268
|
-
this.close(new Error(
|
|
267
|
+
this.close(new Error(NetErrorString.I));
|
|
269
268
|
}
|
|
270
269
|
}
|
|
271
|
-
|
|
272
|
-
const
|
|
273
|
-
if (
|
|
270
|
+
ai(err) {
|
|
271
|
+
const H = this.k;
|
|
272
|
+
if (H.size > 0) {
|
|
274
273
|
if (!err) {
|
|
275
|
-
err = new Error(
|
|
274
|
+
err = new Error(NetErrorString.a1);
|
|
276
275
|
}
|
|
277
|
-
for (const [requestId,
|
|
278
|
-
|
|
279
|
-
|
|
276
|
+
for (const [requestId, j] of H) {
|
|
277
|
+
H.delete(requestId);
|
|
278
|
+
j.i(err);
|
|
280
279
|
}
|
|
281
280
|
}
|
|
282
|
-
this.
|
|
281
|
+
this.k = undefined;
|
|
283
282
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
283
|
+
C;
|
|
284
|
+
k;
|
|
285
|
+
Z;
|
|
287
286
|
}
|
|
288
|
-
class
|
|
289
|
-
constructor(id,
|
|
290
|
-
this.context = undefined;
|
|
287
|
+
class aa {
|
|
288
|
+
constructor(id, am) {
|
|
291
289
|
this.id = id;
|
|
292
|
-
this.
|
|
290
|
+
this.M = am;
|
|
293
291
|
}
|
|
294
292
|
context;
|
|
295
293
|
id;
|
|
@@ -301,19 +299,19 @@ class t {
|
|
|
301
299
|
return 'Local';
|
|
302
300
|
}
|
|
303
301
|
async sendMessage(message) {
|
|
304
|
-
return this.
|
|
302
|
+
return this.M.ab(this, message);
|
|
305
303
|
}
|
|
306
304
|
async sendRequest(request) {
|
|
307
|
-
return this.
|
|
305
|
+
return this.M.ac(this, request);
|
|
308
306
|
}
|
|
309
|
-
sendErrorResponse(
|
|
307
|
+
sendErrorResponse(aq, an) {
|
|
310
308
|
throw null;
|
|
311
309
|
}
|
|
312
|
-
sendResponse(
|
|
310
|
+
sendResponse(_request, an) {
|
|
313
311
|
throw null;
|
|
314
312
|
}
|
|
315
|
-
async close(
|
|
313
|
+
async close(aj) {
|
|
316
314
|
}
|
|
317
|
-
|
|
315
|
+
M;
|
|
318
316
|
}
|
|
319
317
|
//# sourceMappingURL=TypedPkgHub.js.map
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { j, l, m } from '../common/varUintOps';
|
|
2
|
-
export const
|
|
2
|
+
export const p = (pkgType, requestId, encoded) => {
|
|
3
3
|
const i = [requestId, pkgType];
|
|
4
4
|
const b = j(i);
|
|
5
5
|
const a = new Uint8Array(b);
|
|
6
6
|
m(i, a);
|
|
7
7
|
encoded.push(a);
|
|
8
8
|
};
|
|
9
|
-
export const
|
|
9
|
+
export const s = (buff) => {
|
|
10
10
|
const { h: [requestId, pkgType], k: bodyOffset } = l(2, buff);
|
|
11
11
|
const result = { buff, pkgType, requestId, bodyOffset };
|
|
12
12
|
return result;
|