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.
@@ -1,94 +1,92 @@
1
- import { l, i } from '../common/utils';
1
+ import { y, l } from '../common/utils';
2
+ import { NetErrorString } from './NetErrorString';
2
3
  import { MonitorConn } from './TimeoutMonitor';
3
- export class TypedPkgHub {
4
- constructor(s, m) {
5
- const { responsePkgType, timeoutMonitor, makePkgBuff, parsePkgBuff } = m;
4
+ export class v {
5
+ constructor(F, w) {
6
+ const { responsePkgType, timeoutMonitor, makePkgBuff, parsePkgBuff } = w;
6
7
  this.responsePkgType = responsePkgType;
7
- this.context = undefined;
8
- this.onConnOpen = undefined;
9
- this.onConnClose = undefined;
10
- this.p = 0;
11
- this.q = new Map();
12
- this.c = new Map();
13
- this.$ = new Map();
14
- this.F = makePkgBuff;
15
- this.G = parsePkgBuff;
16
- this.u = s;
17
- this.z = timeoutMonitor;
18
- const o = this;
19
- o.run = this.N;
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
- j: l(j),
23
+ u: y(u),
26
24
  };
27
- this.c.set(responsePkgType, o);
28
- this.u.setConnWrapperMaker((a, isActive) => {
25
+ this.f.set(responsePkgType, S);
26
+ this.A.P((b, q) => {
29
27
  do {
30
- this.p++;
31
- } while (this.q.has(this.p & 0x1fffff));
32
- const conn = new k(this.H, a, this.p & 0x1fffff);
33
- this.q.set(conn.id, conn);
34
- this.z?.insert(conn);
35
- const e = {
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
- onPkgBuff: (buff) => this.O(conn, buff),
38
- onClose: (err) => this.P(conn, err),
35
+ E: (buff) => this.a8(conn, buff),
36
+ n: (err) => this.a9(conn, err),
39
37
  };
40
- this.onConnOpen?.(this, conn, !!isActive);
41
- return e;
38
+ this.K?.(this, conn, !!q);
39
+ return g;
42
40
  });
43
41
  }
44
42
  responsePkgType;
45
43
  context;
46
- onConnOpen;
47
- onConnClose;
48
- address() {
49
- return this.u.address();
44
+ K;
45
+ B;
46
+ o() {
47
+ return this.A.o();
50
48
  }
51
- getConn(connId) {
52
- return this.q.get(connId);
49
+ a3(z) {
50
+ return this.t.get(z);
53
51
  }
54
- connect(connStr) {
55
- return this.u.connect(connStr);
52
+ c(e) {
53
+ return this.A.c(e);
56
54
  }
57
- async connectLocal() {
55
+ async a4() {
58
56
  do {
59
- this.p++;
60
- } while (this.q.has(this.p & 0x1fffff));
61
- const conn = new t(this.p & 0x1fffff, this);
62
- this.q.set(conn.id, conn);
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 startServer(connStr) {
66
- return this.u.startListen(connStr);
63
+ async a5(e) {
64
+ return this.A.Q(e);
67
65
  }
68
- async stopServer() {
69
- return this.u.stopListen();
66
+ async a6() {
67
+ return this.A.R();
70
68
  }
71
- sendMessageLocal(conn, message) {
69
+ ab(conn, message) {
72
70
  const pkgType = message.constructor.pkgType;
73
- const handler = this.c.get(pkgType);
71
+ const handler = this.f.get(pkgType);
74
72
  if (handler) {
75
- return handler?.runLocal(conn, message);
73
+ return handler?.r(conn, message);
76
74
  }
77
- throw new Error('Invalid package');
75
+ throw new Error(NetErrorString.D);
78
76
  }
79
- sendRequestLocal(conn, request) {
77
+ ac(conn, request) {
80
78
  const pkgType = request.constructor.pkgType;
81
- const handler = this.c.get(pkgType);
79
+ const handler = this.f.get(pkgType);
82
80
  if (handler) {
83
- return handler?.runLocal(conn, request);
81
+ return handler?.r(conn, request);
84
82
  }
85
- throw new Error('Invalid package');
83
+ throw new Error(NetErrorString.D);
86
84
  }
87
85
  registerError(pkgType, wrapperCtor) {
88
86
  wrapperCtor.registerPkgType(pkgType);
89
- this.$.set(pkgType, wrapperCtor);
90
- const w = this.c.get(this.responsePkgType);
91
- this.c.set(pkgType, w);
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.pkgName} has an incompatible pkgType ${typeof pkgType}`);
100
+ throw new Error(`Wrapper ${handler.m} has an incompatible pkgType ${typeof pkgType}`);
103
101
  }
104
- if (this.c.has(pkgType)) {
105
- throw new Error(`Wrapper ${handler.pkgName} is already handled by another handler`);
102
+ if (this.f.has(pkgType)) {
103
+ throw new Error(`Wrapper ${handler.m} is already handled by another handler`);
106
104
  }
107
- this.c.set(pkgType, handler);
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.pkgName} has an incompatible pkgType ${typeof pkgType}`);
111
+ throw new Error(`Wrapper ${handler.m} has an incompatible pkgType ${typeof pkgType}`);
114
112
  }
115
- if (this.c.has(pkgType)) {
116
- throw new Error(`Wrapper ${handler.pkgName} is already handled by another handler`);
113
+ if (this.f.has(pkgType)) {
114
+ throw new Error(`Wrapper ${handler.m} is already handled by another handler`);
117
115
  }
118
- this.c.set(pkgType, handler);
116
+ this.f.set(pkgType, handler);
119
117
  return handler;
120
118
  }
121
- removeHandler(handler) {
119
+ ao(handler) {
122
120
  const pkgType = handler.pkgType;
123
- const x = this.c.get(pkgType);
124
- if (x === handler) {
125
- this.c.delete(pkgType);
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
- O(conn, buff) {
131
- this.z?.update(conn);
132
- const parseResult = this.G(buff);
133
- const handler = this.c.get(parseResult.pkgType);
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.run(conn, parseResult);
133
+ handler.$(conn, a);
136
134
  return;
137
135
  }
138
- conn.close(new Error('Invalid package'));
136
+ conn.close(new Error(NetErrorString.D));
139
137
  }
140
- P(conn, err) {
141
- const connId = conn.id;
142
- this.q.delete(connId);
143
- this.z?.remove(conn);
144
- conn.onClose(err);
145
- this.onConnClose?.(this, conn, conn.a.hadClosed(), err);
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
- N(conn, parseResult) {
148
- const { buff, pkgType, requestId, bodyOffset } = parseResult;
145
+ a7(conn, a) {
146
+ const { buff, pkgType, requestId, bodyOffset } = a;
149
147
  if (pkgType === this.responsePkgType) {
150
- const b = buff.subarray(bodyOffset);
151
- conn.I(requestId, b);
148
+ const h = buff.subarray(bodyOffset);
149
+ conn.X(requestId, h);
152
150
  }
153
151
  else {
154
- const r = this.$.get(pkgType);
155
- if (r) {
156
- const b = buff.subarray(bodyOffset);
157
- const y = new r(b, true);
158
- conn.Q(requestId, y);
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
- H;
163
- p;
164
- F;
165
- G;
166
- u;
167
- q;
168
- c;
169
- $;
170
- z;
160
+ V;
161
+ s;
162
+ T;
163
+ U;
164
+ A;
165
+ t;
166
+ f;
167
+ L;
168
+ J;
171
169
  }
172
- class k extends MonitorConn {
173
- constructor(m, a, id) {
170
+ class x extends MonitorConn {
171
+ constructor(w, b, id) {
174
172
  super();
175
- this.context = undefined;
176
173
  this.id = id;
177
- this.a = a;
178
- this.v = m;
179
- this.h = undefined;
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
- a;
180
+ b;
185
181
  get localAddress() {
186
- return this.a.localAddress;
182
+ return this.b.localAddress;
187
183
  }
188
184
  get localPort() {
189
- return this.a.localPort;
185
+ return this.b.localPort;
190
186
  }
191
187
  get remoteAddress() {
192
- return this.a.remoteAddress;
188
+ return this.b.remoteAddress;
193
189
  }
194
190
  get remoteFamily() {
195
- return this.a.remoteFamily;
191
+ return this.b.remoteFamily;
196
192
  }
197
193
  get remotePort() {
198
- return this.a.remotePort;
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.v.makePkgBuff(pkgType, 0, encoded);
204
- return this.a.sendPkgBuffV(encoded);
199
+ this.C.makePkgBuff(pkgType, 0, encoded);
200
+ return this.b.p(encoded);
205
201
  }
206
202
  sendRequest(request) {
207
- this.h = new Map();
208
- this.sendRequest = this.K;
209
- this.I = this.R;
210
- this.onClose = this.S;
211
- return this.K(request);
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.v.makePkgBuff(pkgType, requestId, encoded);
217
- return this.a.sendPkgBuffV(encoded);
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.v;
217
+ const { makePkgBuff, responsePkgType } = this.C;
222
218
  makePkgBuff(responsePkgType, requestId, encoded);
223
- return this.a.sendPkgBuffV(encoded);
219
+ return this.b.p(encoded);
224
220
  }
225
221
  async close(err) {
226
- await this.a.close(err);
222
+ await this.b.close(err);
227
223
  }
228
- I(requestId, L) {
224
+ X(requestId, ap) {
229
225
  if (requestId === 0x200000 && this.context === undefined) {
230
- this.close(new Error('Authenticating timeout'));
226
+ this.close(new Error(NetErrorString.I));
231
227
  }
232
228
  }
233
- Q(requestId, error) {
234
- const d = this.h.get(requestId);
235
- if (d) {
236
- this.h.delete(requestId);
237
- return d.g(error);
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.a.sendPkgBuff(this.v.j).catch(i);
237
+ this.b.N(this.C.u).catch(l);
242
238
  }
243
239
  timeout() {
244
- this.close(new Error('Connection timeout'));
240
+ this.close(new Error(NetErrorString.a2));
245
241
  }
246
- onClose(A) {
242
+ n(aj) {
247
243
  }
248
- async K(request) {
249
- const requestId = (this.J++) & 0x1fffff;
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.v.makePkgBuff(pkgType, requestId, encoded);
257
- this.a.sendPkgBuffV(encoded);
258
- request.respBuff = await B;
259
- }
260
- R(requestId, b) {
261
- const d = this.h.get(requestId);
262
- if (d) {
263
- this.h.delete(requestId);
264
- d.f(b);
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('Authenticating timeout'));
267
+ this.close(new Error(NetErrorString.I));
269
268
  }
270
269
  }
271
- S(err) {
272
- const n = this.h;
273
- if (n.size > 0) {
270
+ ai(err) {
271
+ const H = this.k;
272
+ if (H.size > 0) {
274
273
  if (!err) {
275
- err = new Error('Connection is closed');
274
+ err = new Error(NetErrorString.a1);
276
275
  }
277
- for (const [requestId, d] of n) {
278
- n.delete(requestId);
279
- d.g(err);
276
+ for (const [requestId, j] of H) {
277
+ H.delete(requestId);
278
+ j.i(err);
280
279
  }
281
280
  }
282
- this.h = undefined;
281
+ this.k = undefined;
283
282
  }
284
- v;
285
- h;
286
- J;
283
+ C;
284
+ k;
285
+ Z;
287
286
  }
288
- class t {
289
- constructor(id, D) {
290
- this.context = undefined;
287
+ class aa {
288
+ constructor(id, am) {
291
289
  this.id = id;
292
- this._ = D;
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._.sendMessageLocal(this, message);
302
+ return this.M.ab(this, message);
305
303
  }
306
304
  async sendRequest(request) {
307
- return this._.sendRequestLocal(this, request);
305
+ return this.M.ac(this, request);
308
306
  }
309
- sendErrorResponse(M, E) {
307
+ sendErrorResponse(aq, an) {
310
308
  throw null;
311
309
  }
312
- sendResponse(T, E) {
310
+ sendResponse(_request, an) {
313
311
  throw null;
314
312
  }
315
- async close(A) {
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 makeNumTypePkgBuff = (pkgType, requestId, encoded) => {
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 parseNumTypePkgBuff = (buff) => {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "justrun-ws",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "scripts": {
5
5
  "start": "webpack serve"
6
6
  },