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.
@@ -1,42 +1,52 @@
1
- export class g {
2
- constructor(a, b) {
3
- this.a8 = new Promise((f) => {
4
- this.L = f;
1
+ export class m {
2
+ constructor(a, onTimeout) {
3
+ this.a5 = new Promise((d) => {
4
+ this.u = d;
5
5
  });
6
- this.V = b;
6
+ this.M = onTimeout;
7
7
  if (a > 0) {
8
- this.u = setTimeout(() => {
9
- this.u = undefined;
10
- this.V?.();
11
- this.L();
8
+ this.k = setTimeout(() => {
9
+ this.k = undefined;
10
+ this.u(true);
11
+ onTimeout?.();
12
12
  }, a);
13
13
  }
14
14
  else {
15
- this.L();
15
+ this.u(true);
16
+ onTimeout?.();
16
17
  }
17
18
  }
18
- ak(a) {
19
- if (this.u) {
20
- clearTimeout(this.u);
19
+ ao(a) {
20
+ const { k, u, M } = this;
21
+ if (k) {
22
+ clearTimeout(k);
21
23
  }
22
24
  if (a > 0) {
23
- this.u = setTimeout(() => {
24
- this.u = undefined;
25
- this.V?.();
26
- this.L();
25
+ this.k = setTimeout(() => {
26
+ this.k = undefined;
27
+ M?.();
28
+ u(true);
27
29
  }, a);
28
30
  }
29
31
  else {
30
- this.u = undefined;
31
- this.L();
32
+ this.k = undefined;
33
+ u(true);
34
+ M?.();
32
35
  }
33
36
  }
34
- async al() {
35
- await this.a8;
37
+ ah() {
38
+ if (this.k) {
39
+ clearTimeout(this.k);
40
+ this.k = undefined;
41
+ this.u(false);
42
+ }
43
+ }
44
+ ap() {
45
+ return this.a5;
36
46
  }
37
- L;
38
- a8;
39
47
  u;
40
- V;
48
+ a5;
49
+ k;
50
+ M;
41
51
  }
42
52
  //# sourceMappingURL=AdjustableTimer.js.map
@@ -1,29 +1,29 @@
1
1
  export class DataWrapper {
2
2
  constructor(a, needParse) {
3
3
  if (needParse) {
4
- this._ = a;
4
+ this.x = a;
5
5
  }
6
6
  else {
7
- this.A = a;
7
+ this.y = a;
8
8
  }
9
9
  }
10
10
  get data() {
11
- if (!this.A) {
12
- this.A = this.decode(this._);
11
+ if (!this.y) {
12
+ this.y = this.decode(this.x);
13
13
  }
14
- return this.A;
14
+ return this.y;
15
15
  }
16
16
  set data(b) {
17
- this.A = b;
18
- this._ = undefined;
17
+ this.y = b;
18
+ this.x = undefined;
19
19
  }
20
20
  get buff() {
21
- if (!this._) {
22
- this._ = this.encode(this.A);
21
+ if (!this.x) {
22
+ this.x = this.encode(this.y);
23
23
  }
24
- return this._;
24
+ return this.x;
25
25
  }
26
- _;
27
- A;
26
+ x;
27
+ y;
28
28
  }
29
29
  //# sourceMappingURL=DataWrapper.js.map
@@ -30,23 +30,23 @@ export class ListNode {
30
30
  }
31
31
  export class c {
32
32
  constructor() {
33
- this.D = new ListNode();
33
+ this._ = new ListNode();
34
34
  }
35
35
  get e() {
36
- return this.D.getNext();
36
+ return this._.getNext();
37
37
  }
38
38
  get a() {
39
- return this.D.getPrev();
39
+ return this._.getPrev();
40
40
  }
41
- Y(node) {
42
- return node === this.D;
41
+ V(node) {
42
+ return node === this._;
43
43
  }
44
- Z(node) {
45
- node.insertAfter(this.D);
44
+ W(node) {
45
+ node.insertAfter(this._);
46
46
  }
47
- ar(node) {
48
- node.insertBefore(this.D);
47
+ am(node) {
48
+ node.insertBefore(this._);
49
49
  }
50
- D;
50
+ _;
51
51
  }
52
52
  //# sourceMappingURL=List.js.map
@@ -1,9 +1,9 @@
1
- export const i = (d) => {
2
- return new Promise((f) => setTimeout(f, d));
1
+ export const i = (e) => {
2
+ return new Promise((d) => setTimeout(d, e));
3
3
  };
4
4
  export const l = () => { };
5
- export const j = (e = 0x100000000) => {
6
- return (Math.random() * e) >>> 0;
5
+ export const j = (g = 0x100000000) => {
6
+ return (Math.random() * g) >>> 0;
7
7
  };
8
8
  export const k = (c) => {
9
9
  let hash = 5381, a = 0;
@@ -12,19 +12,19 @@ export const k = (c) => {
12
12
  }
13
13
  return hash >>> 0;
14
14
  };
15
- export const z = (h) => {
15
+ export const y = (h) => {
16
16
  let a = 0;
17
17
  let b = 0;
18
18
  while (a < h.length) {
19
19
  b += h[a++].length;
20
20
  }
21
- const g = new Uint8Array(b);
21
+ const f = new Uint8Array(b);
22
22
  b = 0;
23
23
  while (a > 0) {
24
24
  const buff = h[--a];
25
- g.set(buff, b);
25
+ f.set(buff, b);
26
26
  b += buff.length;
27
27
  }
28
- return g;
28
+ return f;
29
29
  };
30
30
  //# sourceMappingURL=utils.js.map
@@ -1,34 +1,34 @@
1
1
  export const o = (b) => {
2
- let g = 1;
2
+ let f = 1;
3
3
  while (b >>>= 7) {
4
- g++;
4
+ f++;
5
5
  }
6
- return g;
6
+ return f;
7
7
  };
8
8
  export const j = (i) => {
9
9
  let b;
10
10
  let a = 0;
11
- let g = 0;
11
+ let f = 0;
12
12
  while (a < i.length) {
13
- g++;
13
+ f++;
14
14
  b = i[a++];
15
15
  while (b >>>= 7) {
16
- g++;
16
+ f++;
17
17
  }
18
18
  }
19
- return g;
19
+ return f;
20
20
  };
21
21
  export const p = (buffer, e = 0) => {
22
22
  let c;
23
23
  let d = e;
24
24
  let b = 0;
25
- let f = 0;
25
+ let g = 0;
26
26
  do {
27
27
  c = buffer[d++];
28
- b |= (c & 127) << f;
29
- f += 7;
28
+ b |= (c & 127) << g;
29
+ g += 7;
30
30
  } while (c & 128);
31
- if (c !== undefined && f <= 35) {
31
+ if (c !== undefined && g <= 35) {
32
32
  return {
33
33
  b: b,
34
34
  k: d - e,
@@ -43,18 +43,18 @@ export const l = (n, buffer, e = 0) => {
43
43
  let c;
44
44
  let d = e;
45
45
  let b = 0;
46
- let f = 0;
46
+ let g = 0;
47
47
  const h = [];
48
48
  for (a = 0; a < n; a++) {
49
49
  do {
50
50
  c = buffer[d++];
51
- b |= (c & 127) << f;
52
- f += 7;
51
+ b |= (c & 127) << g;
52
+ g += 7;
53
53
  } while (c & 128);
54
- if (c !== undefined && f <= 35) {
54
+ if (c !== undefined && g <= 35) {
55
55
  h.push(b);
56
56
  b = 0;
57
- f = 0;
57
+ g = 0;
58
58
  }
59
59
  else {
60
60
  throw new Error('Parsing failed');
@@ -1,11 +1,11 @@
1
1
  import { NetErrorString } from '../net/NetErrorString';
2
2
  import { b } from './common';
3
- const d = new Error(NetErrorString.g);
4
- export class i {
5
- constructor(wrapperCtor, e, f = true) {
3
+ const c = new Error(NetErrorString.NotAuthenticated);
4
+ export class h {
5
+ constructor(wrapperCtor, e, f) {
6
6
  this.n = wrapperCtor;
7
7
  this.v = e;
8
- this.w = f;
8
+ this.w = !f;
9
9
  }
10
10
  get pkgType() {
11
11
  return this.n.pkgType;
@@ -13,7 +13,7 @@ export class i {
13
13
  get k() {
14
14
  return this.n.name;
15
15
  }
16
- async y(conn, a) {
16
+ async x(conn, a) {
17
17
  const { buff, bodyOffset } = a;
18
18
  const body = buff.subarray(bodyOffset);
19
19
  const message = new this.n(body, true);
@@ -28,7 +28,7 @@ export class i {
28
28
  }
29
29
  }
30
30
  else {
31
- b(conn, d);
31
+ b(conn, c);
32
32
  }
33
33
  if (conn.context === undefined) {
34
34
  await conn.close();
@@ -46,18 +46,18 @@ export class i {
46
46
  }
47
47
  }
48
48
  else {
49
- b(conn, d);
49
+ b(conn, c);
50
50
  }
51
51
  }
52
52
  n;
53
53
  v;
54
54
  w;
55
55
  }
56
- export class j {
57
- constructor(wrapperCtor, e, f = true) {
56
+ export class i {
57
+ constructor(wrapperCtor, e, f) {
58
58
  this.n = wrapperCtor;
59
59
  this.v = e;
60
- this.w = f;
60
+ this.w = !f;
61
61
  }
62
62
  get pkgType() {
63
63
  return this.n.pkgType;
@@ -65,7 +65,7 @@ export class j {
65
65
  get k() {
66
66
  return this.n.name;
67
67
  }
68
- async y(conn, a) {
68
+ async x(conn, a) {
69
69
  const { buff, requestId, bodyOffset } = a;
70
70
  const body = buff.subarray(bodyOffset);
71
71
  const request = new this.n(body, true);
@@ -84,7 +84,7 @@ export class j {
84
84
  }
85
85
  }
86
86
  else {
87
- error = b(conn, d);
87
+ error = b(conn, c);
88
88
  }
89
89
  try {
90
90
  if (!error) {
@@ -97,8 +97,8 @@ export class j {
97
97
  await conn.close();
98
98
  }
99
99
  }
100
- catch (err) {
101
- await conn.close(err);
100
+ catch {
101
+ return;
102
102
  }
103
103
  }
104
104
  async q(conn, request) {
@@ -115,7 +115,7 @@ export class j {
115
115
  }
116
116
  }
117
117
  }
118
- throw b(conn, d);
118
+ throw b(conn, c);
119
119
  }
120
120
  n;
121
121
  v;
package/lib/index.d.ts CHANGED
@@ -76,18 +76,18 @@ declare class ListNode {
76
76
  private _prev;
77
77
  private _next;
78
78
  }
79
- declare abstract class MonitorConn extends ListNode {
80
- abstract keepAlive(): void;
81
- abstract timeout(): void;
79
+ declare abstract class MonitorItem extends ListNode {
80
+ abstract warnTimeout(): void;
81
+ abstract onTimeout(): void;
82
82
  }
83
83
  export declare class TimeoutMonitor {
84
- constructor(tickMilSec: number, aliveTick: number, timeoutTick: number);
85
- insert(conn: MonitorConn): void;
86
- update(conn: MonitorConn): void;
87
- remove(conn: MonitorConn): void;
84
+ constructor(tickMilSec: number, warningTick: number, timeoutTick: number);
85
+ insert(item: MonitorItem): void;
86
+ update(item: MonitorItem): void;
87
+ remove(item: MonitorItem): void;
88
88
  private check;
89
89
  private _tickMilSec;
90
- private _aliveTick;
90
+ private _warningTick;
91
91
  private _timeoutTick;
92
92
  private _currentTick;
93
93
  private _monitorCount;
@@ -129,6 +129,7 @@ export declare enum NetErrorString {
129
129
  InvalidConnection = "InvalidConnection",
130
130
  InvalidPackage = "InvalidPackage",
131
131
  NotAuthenticated = "NotAuthenticated",
132
+ NotFound = "NotFound",
132
133
  Timeout = "Timeout",
133
134
  UnknownError = "UnknownError"
134
135
  }
@@ -137,8 +138,14 @@ export interface IAuthProvider {
137
138
  preConnect(): Promise<string>;
138
139
  }
139
140
  export declare const createNumPkgTypeClient: (connUrl: string, timeoutMonitor?: TimeoutMonitor) => WsClient<number>;
141
+ declare enum ClientConnState {
142
+ NotConnected = 0,
143
+ Connecting = 1,
144
+ Connected = 2
145
+ }
140
146
  export declare class WsClient<PkgTypeT extends number | string> {
141
147
  constructor(connUrl: string, opts: TypedPkgHubOpts<PkgTypeT>);
148
+ getConnState(): ClientConnState;
142
149
  enableAuthenticate(proxyPrivatePkgType: PkgTypeT, authProvider: IAuthProvider): void;
143
150
  registerError<ErrorT>(pkgType: PkgTypeT, wrapperCtor: ErrorWrapperCtor<ErrorT>): void;
144
151
  registerMessage<MessageT>(pkgType: PkgTypeT, wrapperCtor: MessageWrapperCtor<MessageT>): void;
@@ -147,7 +154,6 @@ export declare class WsClient<PkgTypeT extends number | string> {
147
154
  handleRequestWith<RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT>>(wrapperCtor: RequestWrapperCtor<RequestT, ResponseT, WrapperT>, handler: (request: RequestT) => ResponseT | Promise<ResponseT>): void;
148
155
  sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
149
156
  sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
150
- startConnect(delay?: number): Promise<void>;
151
157
  onConnectionStatus(listener: (isOpen: boolean, conn: IConn<string>, active: boolean, err?: Error) => void): void;
152
158
  private connect;
153
159
  private onConnOpen;
@@ -157,17 +163,16 @@ export declare class WsClient<PkgTypeT extends number | string> {
157
163
  private _conn;
158
164
  private _connState;
159
165
  private _authProvider?;
160
- private _connectDelay?;
161
166
  private _listenerArr?;
162
167
  }
163
168
  export declare class JustrunAuthProvider implements IAuthProvider {
164
- constructor(allowAnonymous: boolean, authUrl: string, getConnUrl: (address: string, sid: string, kick?: boolean) => string, authStateCb: (cid: string, sid: string) => void);
169
+ constructor(authUrl: string, getConnUrl: (address: string, sid: string, kick?: boolean) => string, authStateCb: (cid: string, sid: string, connErr?: string) => void);
165
170
  init<PkgTypeT extends number | string>(proxyPrivatePkgType: PkgTypeT, client: WsClient<PkgTypeT>): Promise<void>;
166
171
  preConnect(): Promise<string>;
167
172
  authWithCredential(type: "signin" | "signup", id: string, hash: string, extra: string): Promise<void>;
168
173
  authWithOAuth2(returnTo: string, oauthUrl: string, clientId: string, scope: string): void;
169
174
  resetAuthState(): Promise<void>;
170
- resetKickedState(forceKick: boolean): void;
175
+ resetConnState(forceKick: boolean): void;
171
176
  private onConnStatus;
172
177
  private extendToken;
173
178
  private onSysMessage;
@@ -175,11 +180,10 @@ export declare class JustrunAuthProvider implements IAuthProvider {
175
180
  private sendAuthRequest;
176
181
  private _cid;
177
182
  private _sid;
178
- private _allowAnonymous;
179
183
  private _authUrl;
180
- private _kickedState;
181
184
  private _currConnAddr;
182
185
  private _conn?;
186
+ private _connErr?;
183
187
  private _extendTokenTimer?;
184
188
  private _getConnUrl;
185
189
  private _authStateCb;
@@ -1,41 +1,41 @@
1
- import { z } from '../common/utils';
1
+ import { y } from '../common/utils';
2
2
  import { NetErrorString } from './NetErrorString';
3
- export class q {
3
+ export class m {
4
4
  b() {
5
5
  return null;
6
6
  }
7
- I(H) {
8
- this.H = H;
7
+ init(E) {
8
+ this.E = E;
9
9
  }
10
- async J(t) {
10
+ async I(r) {
11
11
  return false;
12
12
  }
13
- async L() {
13
+ async J() {
14
14
  return false;
15
15
  }
16
- async d(e) {
17
- const g = new WebSocket(e);
18
- return new Promise((f, j) => {
19
- g.onerror = (r) => {
20
- j(new Error(NetErrorString.l));
16
+ async g(c) {
17
+ const f = new WebSocket(c);
18
+ return new Promise((d, j) => {
19
+ f.onerror = (l) => {
20
+ j(new Error(NetErrorString.UnknownError));
21
21
  };
22
- g.onopen = () => {
23
- f(this.am(g, true));
22
+ f.onopen = () => {
23
+ d(this.ai(f, true));
24
24
  };
25
25
  });
26
26
  }
27
- am = (g, p) => {
28
- const c = new s(g);
29
- const { conn, A, o } = this.H(c, p);
30
- g.onmessage = (k) => {
27
+ ai = (f, p) => {
28
+ const e = new q(f);
29
+ const { conn, _, o } = this.E(e, p);
30
+ f.onmessage = (k) => {
31
31
  k.data.arrayBuffer().then((buff) => {
32
- A(new Uint8Array(buff));
32
+ _(new Uint8Array(buff));
33
33
  });
34
34
  };
35
- g.onerror = (r) => {
36
- o(new Error(NetErrorString.l));
35
+ f.onerror = (l) => {
36
+ o(new Error(NetErrorString.UnknownError));
37
37
  };
38
- g.onclose = (k) => {
38
+ f.onclose = (k) => {
39
39
  if (k.code === 1000) {
40
40
  o();
41
41
  }
@@ -45,53 +45,47 @@ export class q {
45
45
  };
46
46
  return conn;
47
47
  };
48
- H(a, u) {
48
+ E(a, s) {
49
49
  throw new Error('Need to call "setConnWrapperMaker" before using');
50
50
  }
51
51
  }
52
- class s {
53
- constructor(g) {
54
- this.i = g;
55
- }
56
- get localAddress() {
57
- return;
58
- }
59
- get localPort() {
60
- return;
61
- }
62
- get remoteAddress() {
63
- return;
64
- }
65
- get remoteFamily() {
66
- return;
67
- }
68
- get remotePort() {
69
- return;
52
+ class q {
53
+ constructor(f) {
54
+ this.i = f;
55
+ }
56
+ get socket() {
57
+ return {
58
+ localAddress: '',
59
+ localPort: 0,
60
+ remoteAddress: '',
61
+ remoteFamily: '',
62
+ remotePort: 0,
63
+ };
70
64
  }
71
- async D(buff) {
65
+ async F(buff) {
72
66
  if (this.i) {
73
67
  return this.i.send(buff);
74
68
  }
75
- throw new Error(NetErrorString.m);
69
+ throw new Error(NetErrorString.InvalidConnection);
76
70
  }
77
71
  async n(h) {
78
72
  if (this.i) {
79
- const buff = z(h);
73
+ const buff = y(h);
80
74
  return this.i.send(buff);
81
75
  }
82
- throw new Error(NetErrorString.m);
76
+ throw new Error(NetErrorString.InvalidConnection);
83
77
  }
84
78
  async close(err) {
85
79
  const { i } = this;
86
80
  if (i) {
87
81
  this.i = undefined;
88
82
  if (!err) {
89
- if (i.readyState === WebSocket.OPEN) {
83
+ if (i.readyState <= WebSocket.OPEN) {
90
84
  i.close(1000);
91
85
  }
92
86
  }
93
87
  else {
94
- i.close(3000, err.message);
88
+ i.close(1006, err.message);
95
89
  }
96
90
  }
97
91
  }
@@ -1,6 +1,6 @@
1
- export class h {
1
+ export class e {
2
2
  constructor(c) {
3
- this.a9 = c;
3
+ this.a6 = c;
4
4
  }
5
5
  get id() {
6
6
  return this.a?.id ?? -1;
@@ -19,16 +19,16 @@ export class h {
19
19
  remoteFamily;
20
20
  remotePort;
21
21
  async sendErrorResponse(error, requestId) {
22
- return (this.a ?? await this.d()).sendErrorResponse(error, requestId);
22
+ return (this.a ?? await this.g()).sendErrorResponse(error, requestId);
23
23
  }
24
24
  async sendResponse(request, requestId) {
25
- return (this.a ?? await this.d()).sendResponse(request, requestId);
25
+ return (this.a ?? await this.g()).sendResponse(request, requestId);
26
26
  }
27
27
  async sendMessage(message) {
28
- return (this.a ?? await this.d()).sendMessage(message);
28
+ return (this.a ?? await this.g()).sendMessage(message);
29
29
  }
30
30
  async sendRequest(request) {
31
- return (this.a ?? await this.d()).sendRequest(request);
31
+ return (this.a ?? await this.g()).sendRequest(request);
32
32
  }
33
33
  async close(err) {
34
34
  const { a } = this;
@@ -44,21 +44,21 @@ export class h {
44
44
  }, () => this.a = undefined);
45
45
  }
46
46
  }
47
- async an() {
47
+ async aq() {
48
48
  if (!this.a) {
49
- await this.d();
49
+ await this.g();
50
50
  }
51
51
  }
52
- ao() {
52
+ aj() {
53
53
  this.a = undefined;
54
54
  }
55
- async d() {
55
+ async g() {
56
56
  let { q } = this;
57
57
  if (!q) {
58
- q = this.q = new Promise((f, j) => {
59
- this.a9().then((conn) => {
58
+ q = this.q = new Promise((d, j) => {
59
+ this.a6().then((conn) => {
60
60
  if (conn) {
61
- f(conn);
61
+ d(conn);
62
62
  this.a = conn;
63
63
  this.q = undefined;
64
64
  }
@@ -70,7 +70,7 @@ export class h {
70
70
  }
71
71
  return await q;
72
72
  }
73
- a9;
73
+ a6;
74
74
  a;
75
75
  q;
76
76
  }
@@ -1,14 +1,15 @@
1
1
  export var NetErrorString;
2
2
  (function (NetErrorString) {
3
- NetErrorString["H"] = "AuthTimeout";
4
- NetErrorString["a"] = "BadRequest";
5
- NetErrorString["p"] = "ConnectFailed";
6
- NetErrorString["Y"] = "ConnectionClosed";
7
- NetErrorString["b"] = "InvalidConnString";
8
- NetErrorString["m"] = "InvalidConnection";
9
- NetErrorString["$"] = "InvalidPackage";
10
- NetErrorString["g"] = "NotAuthenticated";
11
- NetErrorString["Z"] = "Timeout";
12
- NetErrorString["l"] = "UnknownError";
3
+ NetErrorString["AuthTimeout"] = "AuthTimeout";
4
+ NetErrorString["BadRequest"] = "BadRequest";
5
+ NetErrorString["ConnectFailed"] = "ConnectFailed";
6
+ NetErrorString["ConnectionClosed"] = "ConnectionClosed";
7
+ NetErrorString["InvalidConnString"] = "InvalidConnString";
8
+ NetErrorString["InvalidConnection"] = "InvalidConnection";
9
+ NetErrorString["InvalidPackage"] = "InvalidPackage";
10
+ NetErrorString["NotAuthenticated"] = "NotAuthenticated";
11
+ NetErrorString["NotFound"] = "NotFound";
12
+ NetErrorString["Timeout"] = "Timeout";
13
+ NetErrorString["UnknownError"] = "UnknownError";
13
14
  })(NetErrorString || (NetErrorString = {}));
14
15
  //# sourceMappingURL=NetErrorString.js.map