justrun-ws 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +1,29 @@
1
1
  export class DataWrapper {
2
2
  constructor(a, needParse) {
3
3
  if (needParse) {
4
- this.x = a;
4
+ this._ = a;
5
5
  }
6
6
  else {
7
- this.y = a;
7
+ this.A = a;
8
8
  }
9
9
  }
10
10
  get data() {
11
- if (!this.y) {
12
- this.y = this.decode(this.x);
11
+ if (!this.A) {
12
+ this.A = this.decode(this._);
13
13
  }
14
- return this.y;
14
+ return this.A;
15
15
  }
16
16
  set data(b) {
17
- this.y = b;
18
- this.x = undefined;
17
+ this.A = b;
18
+ this._ = undefined;
19
19
  }
20
20
  get buff() {
21
- if (!this.x) {
22
- this.x = this.encode(this.y);
21
+ if (!this._) {
22
+ this._ = this.encode(this.A);
23
23
  }
24
- return this.x;
24
+ return this._;
25
25
  }
26
- x;
27
- y;
26
+ _;
27
+ A;
28
28
  }
29
29
  //# sourceMappingURL=DataWrapper.js.map
@@ -1,52 +1,52 @@
1
1
  export class ListNode {
2
2
  constructor() {
3
- this.e = this;
4
- this.i = this;
3
+ this.b = this;
4
+ this.j = this;
5
5
  }
6
6
  getPrev() {
7
- return this.e;
7
+ return this.b;
8
8
  }
9
9
  getNext() {
10
- return this.i;
10
+ return this.j;
11
11
  }
12
12
  insertBefore(node) {
13
- this.i = node;
14
- this.e = node.e;
15
- node.e = this;
16
- this.e.i = this;
13
+ this.j = node;
14
+ this.b = node.b;
15
+ node.b = this;
16
+ this.b.j = this;
17
17
  }
18
18
  insertAfter(node) {
19
- this.e = node;
20
- this.i = node.i;
21
- node.i = this;
22
- this.i.e = this;
19
+ this.b = node;
20
+ this.j = node.j;
21
+ node.j = this;
22
+ this.j.b = this;
23
23
  }
24
24
  remove() {
25
- this.e.i = this.i;
26
- this.i.e = this.e;
25
+ this.b.j = this.j;
26
+ this.j.b = this.b;
27
27
  }
28
- e;
29
- i;
28
+ b;
29
+ j;
30
30
  }
31
31
  export class c {
32
32
  constructor() {
33
- this._ = new ListNode();
33
+ this.D = new ListNode();
34
34
  }
35
- get f() {
36
- return this._.getNext();
35
+ get e() {
36
+ return this.D.getNext();
37
37
  }
38
38
  get a() {
39
- return this._.getPrev();
39
+ return this.D.getPrev();
40
40
  }
41
- P(node) {
42
- return node === this._;
41
+ Y(node) {
42
+ return node === this.D;
43
43
  }
44
- Q(node) {
45
- node.insertAfter(this._);
44
+ Z(node) {
45
+ node.insertAfter(this.D);
46
46
  }
47
- a6(node) {
48
- node.insertBefore(this._);
47
+ ar(node) {
48
+ node.insertBefore(this.D);
49
49
  }
50
- _;
50
+ D;
51
51
  }
52
52
  //# sourceMappingURL=List.js.map
@@ -1,18 +1,18 @@
1
- export const j = (f) => {
2
- return new Promise((d) => setTimeout(d, f));
1
+ export const i = (d) => {
2
+ return new Promise((f) => setTimeout(f, d));
3
3
  };
4
4
  export const l = () => { };
5
- export const k = (i = 0x100000000) => {
6
- return (Math.random() * i) >>> 0;
5
+ export const j = (e = 0x100000000) => {
6
+ return (Math.random() * e) >>> 0;
7
7
  };
8
- export const m = (e) => {
9
- let c = 5381, a = 0;
10
- while (a < e.length) {
11
- c = ((c << 5) + c) + e.charCodeAt(a++);
8
+ export const k = (c) => {
9
+ let hash = 5381, a = 0;
10
+ while (a < c.length) {
11
+ hash = ((hash << 5) + hash) + c.charCodeAt(a++);
12
12
  }
13
- return c >>> 0;
13
+ return hash >>> 0;
14
14
  };
15
- export const y = (h) => {
15
+ export const z = (h) => {
16
16
  let a = 0;
17
17
  let b = 0;
18
18
  while (a < h.length) {
@@ -1,22 +1,22 @@
1
1
  import { NetErrorString } from '../net/NetErrorString';
2
2
  import { b } from './common';
3
- const c = new Error(NetErrorString.g);
4
- export class n {
5
- constructor(wrapperCtor, d, e = true) {
6
- this.l = wrapperCtor;
7
- this.v = d;
8
- this.w = e;
3
+ const d = new Error(NetErrorString.g);
4
+ export class i {
5
+ constructor(wrapperCtor, e, f = true) {
6
+ this.n = wrapperCtor;
7
+ this.v = e;
8
+ this.w = f;
9
9
  }
10
10
  get pkgType() {
11
- return this.l.pkgType;
11
+ return this.n.pkgType;
12
12
  }
13
- get m() {
14
- return this.l.name;
13
+ get k() {
14
+ return this.n.name;
15
15
  }
16
- async $(conn, a) {
16
+ async y(conn, a) {
17
17
  const { buff, bodyOffset } = a;
18
- const h = buff.subarray(bodyOffset);
19
- const message = new this.l(h, true);
18
+ const body = buff.subarray(bodyOffset);
19
+ const message = new this.n(body, true);
20
20
  if (conn.context !== undefined || !this.w) {
21
21
  try {
22
22
  await this.v(conn, message);
@@ -28,13 +28,13 @@ export class n {
28
28
  }
29
29
  }
30
30
  else {
31
- b(conn, c);
31
+ b(conn, d);
32
32
  }
33
33
  if (conn.context === undefined) {
34
34
  await conn.close();
35
35
  }
36
36
  }
37
- async r(conn, message) {
37
+ async q(conn, message) {
38
38
  if (conn.context !== undefined || !this.w) {
39
39
  try {
40
40
  return await this.v(conn, message);
@@ -46,29 +46,29 @@ export class n {
46
46
  }
47
47
  }
48
48
  else {
49
- b(conn, c);
49
+ b(conn, d);
50
50
  }
51
51
  }
52
- l;
52
+ n;
53
53
  v;
54
54
  w;
55
55
  }
56
- export class o {
57
- constructor(wrapperCtor, d, e = true) {
58
- this.l = wrapperCtor;
59
- this.v = d;
60
- this.w = e;
56
+ export class j {
57
+ constructor(wrapperCtor, e, f = true) {
58
+ this.n = wrapperCtor;
59
+ this.v = e;
60
+ this.w = f;
61
61
  }
62
62
  get pkgType() {
63
- return this.l.pkgType;
63
+ return this.n.pkgType;
64
64
  }
65
- get m() {
66
- return this.l.name;
65
+ get k() {
66
+ return this.n.name;
67
67
  }
68
- async $(conn, a) {
68
+ async y(conn, a) {
69
69
  const { buff, requestId, bodyOffset } = a;
70
- const h = buff.subarray(bodyOffset);
71
- const request = new this.l(h, true);
70
+ const body = buff.subarray(bodyOffset);
71
+ const request = new this.n(body, true);
72
72
  let error;
73
73
  if (conn.context !== undefined || !this.w) {
74
74
  try {
@@ -84,7 +84,7 @@ export class o {
84
84
  }
85
85
  }
86
86
  else {
87
- error = b(conn, c);
87
+ error = b(conn, d);
88
88
  }
89
89
  try {
90
90
  if (!error) {
@@ -101,7 +101,7 @@ export class o {
101
101
  await conn.close(err);
102
102
  }
103
103
  }
104
- async r(conn, request) {
104
+ async q(conn, request) {
105
105
  if (conn.context !== undefined || !this.w) {
106
106
  try {
107
107
  return await this.v(conn, request);
@@ -115,9 +115,9 @@ export class o {
115
115
  }
116
116
  }
117
117
  }
118
- throw b(conn, c);
118
+ throw b(conn, d);
119
119
  }
120
- l;
120
+ n;
121
121
  v;
122
122
  w;
123
123
  }
package/lib/index.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  // Generated by dts-bundle-generator v9.5.1
2
2
 
3
+ export interface ISocket {
4
+ readonly localAddress?: string;
5
+ readonly localPort?: number;
6
+ readonly remoteAddress?: string;
7
+ readonly remoteFamily?: string;
8
+ readonly remotePort?: number;
9
+ }
3
10
  export interface IWrapper<DataT> {
4
11
  get buff(): Uint8Array;
5
12
  get data(): DataT;
@@ -46,11 +53,7 @@ export type RequestWrapperCtor<RequestT, ResponseT, WrapperT extends RequestWrap
46
53
  export interface IConn<ConnContextT = void> {
47
54
  context?: ConnContextT | undefined;
48
55
  readonly id: number;
49
- readonly localAddress?: string;
50
- readonly localPort?: number;
51
- readonly remoteAddress?: string;
52
- readonly remoteFamily?: string;
53
- readonly remotePort?: number;
56
+ readonly socket?: ISocket;
54
57
  sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
55
58
  sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<void>;
56
59
  sendErrorResponse<ErrorT>(error: ErrorWrapper<ErrorT>, requestId: number): Promise<void>;
@@ -116,11 +119,6 @@ export declare class CommonError extends JsonError<string> {
116
119
  export interface DoneResp<ResultT extends string | number | boolean = boolean> {
117
120
  result: ResultT;
118
121
  }
119
- export interface IAuthResp {
120
- cid: string;
121
- token: string;
122
- shard?: number;
123
- }
124
122
  export declare const handleUncaughtErrorWith: (handler: (conn: IConn<unknown>, err: Error) => ErrorWrapper<unknown>) => void;
125
123
  export declare enum NetErrorString {
126
124
  AuthTimeout = "AuthTimeout",
@@ -134,9 +132,14 @@ export declare enum NetErrorString {
134
132
  Timeout = "Timeout",
135
133
  UnknownError = "UnknownError"
136
134
  }
137
- export declare const createNumPkgTypeClient: (getConnStr: (shard?: number) => string, timeoutMonitor?: TimeoutMonitor) => WsClient<number>;
135
+ export interface IAuthProvider {
136
+ init<PkgTypeT extends number | string>(proxyPrivatePkgType: PkgTypeT, client: WsClient<PkgTypeT>): Promise<void>;
137
+ preConnect(): Promise<string>;
138
+ }
139
+ export declare const createNumPkgTypeClient: (connUrl: string, timeoutMonitor?: TimeoutMonitor) => WsClient<number>;
138
140
  export declare class WsClient<PkgTypeT extends number | string> {
139
- constructor(getConnStr: (shard?: number) => string, opts: TypedPkgHubOpts<PkgTypeT>);
141
+ constructor(connUrl: string, opts: TypedPkgHubOpts<PkgTypeT>);
142
+ enableAuthenticate(proxyPrivatePkgType: PkgTypeT, authProvider: IAuthProvider): void;
140
143
  registerError<ErrorT>(pkgType: PkgTypeT, wrapperCtor: ErrorWrapperCtor<ErrorT>): void;
141
144
  registerMessage<MessageT>(pkgType: PkgTypeT, wrapperCtor: MessageWrapperCtor<MessageT>): void;
142
145
  registerRequest<RequestT, ResponseT>(pkgType: PkgTypeT, wrapperCtor: RequestWrapperCtor<RequestT, ResponseT>): void;
@@ -144,22 +147,42 @@ export declare class WsClient<PkgTypeT extends number | string> {
144
147
  handleRequestWith<RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT>>(wrapperCtor: RequestWrapperCtor<RequestT, ResponseT, WrapperT>, handler: (request: RequestT) => ResponseT | Promise<ResponseT>): void;
145
148
  sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
146
149
  sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
147
- sendCustomAuthRequest<RequestT, ResponseT extends IAuthResp>(_request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
148
- resetAuthState(): void;
149
- enableAuthenticate(proxyAuthPkgType: PkgTypeT, authStatusCallback: (cid: string) => void, allowAnonymous?: true): void;
150
- reconnect(delay?: number): void;
151
- onConnectionStatus(listener: (isOpen: boolean, active: boolean, err?: Error) => void): void;
150
+ startConnect(delay?: number): Promise<void>;
151
+ onConnectionStatus(listener: (isOpen: boolean, conn: IConn<string>, active: boolean, err?: Error) => void): void;
152
152
  private connect;
153
153
  private onConnOpen;
154
154
  private onConnClose;
155
+ private _connUrl;
155
156
  private _pkgHub;
156
157
  private _conn;
157
- private _cid;
158
158
  private _connState;
159
- private _getConnStr;
160
- private _originConnect;
159
+ private _authProvider?;
161
160
  private _connectDelay?;
162
161
  private _listenerArr?;
163
162
  }
163
+ 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);
165
+ init<PkgTypeT extends number | string>(proxyPrivatePkgType: PkgTypeT, client: WsClient<PkgTypeT>): Promise<void>;
166
+ preConnect(): Promise<string>;
167
+ authWithCredential(type: "signin" | "signup", id: string, hash: string, extra: string): Promise<void>;
168
+ authWithOAuth2(returnTo: string, oauthUrl: string, clientId: string, scope: string): void;
169
+ resetAuthState(): Promise<void>;
170
+ resetKickedState(forceKick: boolean): void;
171
+ private onConnStatus;
172
+ private extendToken;
173
+ private onSysMessage;
174
+ private readAuthInfo;
175
+ private sendAuthRequest;
176
+ private _cid;
177
+ private _sid;
178
+ private _allowAnonymous;
179
+ private _authUrl;
180
+ private _kickedState;
181
+ private _currConnAddr;
182
+ private _conn?;
183
+ private _extendTokenTimer?;
184
+ private _getConnUrl;
185
+ private _authStateCb;
186
+ }
164
187
 
165
188
  export {};
package/lib/index.js CHANGED
@@ -3,5 +3,6 @@ export { JsonError, JsonMessage, JsonRequest } from './net/JsonPackageWrapper';
3
3
  export { NetErrorString } from './net/NetErrorString';
4
4
  export { ErrorWrapper, EventWrapper, MessageWrapper, RequestWrapper } from './net/PackageWrapper';
5
5
  export { TimeoutMonitor } from './net/TimeoutMonitor';
6
+ export { JustrunAuthProvider } from './JustRunAuthProvider';
6
7
  export { createNumPkgTypeClient, WsClient } from './WsClient';
7
8
  //# sourceMappingURL=index.js.map
@@ -1,57 +1,57 @@
1
- import { y } from '../common/utils';
1
+ import { z } from '../common/utils';
2
2
  import { NetErrorString } from './NetErrorString';
3
- export class r {
4
- o() {
3
+ export class q {
4
+ b() {
5
5
  return null;
6
6
  }
7
- P(D) {
8
- this.D = D;
7
+ I(H) {
8
+ this.H = H;
9
9
  }
10
- async Q(t) {
10
+ async J(t) {
11
11
  return false;
12
12
  }
13
- async R() {
13
+ async L() {
14
14
  return false;
15
15
  }
16
- async c(e) {
17
- const f = new WebSocket(e);
18
- return new Promise((d, i) => {
19
- f.onerror = (k) => {
20
- i(new Error(NetErrorString.m));
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));
21
21
  };
22
- f.onopen = () => {
23
- d(this.a4(f, true));
22
+ g.onopen = () => {
23
+ f(this.am(g, true));
24
24
  };
25
25
  });
26
26
  }
27
- a4 = (f, q) => {
28
- const b = new s(f);
29
- const { conn, E, n } = this.D(b, q);
30
- f.onmessage = (j) => {
31
- j.data.arrayBuffer().then((buff) => {
32
- E(new Uint8Array(buff));
27
+ am = (g, p) => {
28
+ const c = new s(g);
29
+ const { conn, A, o } = this.H(c, p);
30
+ g.onmessage = (k) => {
31
+ k.data.arrayBuffer().then((buff) => {
32
+ A(new Uint8Array(buff));
33
33
  });
34
34
  };
35
- f.onerror = (k) => {
36
- n(new Error(NetErrorString.m));
35
+ g.onerror = (r) => {
36
+ o(new Error(NetErrorString.l));
37
37
  };
38
- f.onclose = (j) => {
39
- if (j.code === 1000) {
40
- n();
38
+ g.onclose = (k) => {
39
+ if (k.code === 1000) {
40
+ o();
41
41
  }
42
42
  else {
43
- n(new Error(j.reason));
43
+ o(new Error(k.reason));
44
44
  }
45
45
  };
46
46
  return conn;
47
47
  };
48
- D(a, u) {
48
+ H(a, u) {
49
49
  throw new Error('Need to call "setConnWrapperMaker" before using');
50
50
  }
51
51
  }
52
52
  class s {
53
- constructor(f) {
54
- this.g = f;
53
+ constructor(g) {
54
+ this.i = g;
55
55
  }
56
56
  get localAddress() {
57
57
  return;
@@ -68,36 +68,36 @@ class s {
68
68
  get remotePort() {
69
69
  return;
70
70
  }
71
- async N(buff) {
72
- if (this.g) {
73
- return this.g.send(buff);
71
+ async D(buff) {
72
+ if (this.i) {
73
+ return this.i.send(buff);
74
74
  }
75
- throw new Error(NetErrorString.l);
75
+ throw new Error(NetErrorString.m);
76
76
  }
77
- async p(h) {
78
- if (this.g) {
79
- const buff = y(h);
80
- return this.g.send(buff);
77
+ async n(h) {
78
+ if (this.i) {
79
+ const buff = z(h);
80
+ return this.i.send(buff);
81
81
  }
82
- throw new Error(NetErrorString.l);
82
+ throw new Error(NetErrorString.m);
83
83
  }
84
84
  async close(err) {
85
- const { g } = this;
86
- if (g) {
87
- this.g = undefined;
85
+ const { i } = this;
86
+ if (i) {
87
+ this.i = undefined;
88
88
  if (!err) {
89
- if (g.readyState === WebSocket.OPEN) {
90
- g.close(1000);
89
+ if (i.readyState === WebSocket.OPEN) {
90
+ i.close(1000);
91
91
  }
92
92
  }
93
93
  else {
94
- g.close(3000, err.message);
94
+ i.close(3000, err.message);
95
95
  }
96
96
  }
97
97
  }
98
- O() {
99
- return !this.g;
98
+ G() {
99
+ return !this.i;
100
100
  }
101
- g;
101
+ i;
102
102
  }
103
103
  //# sourceMappingURL=BrowserWsNet.js.map
@@ -1,6 +1,6 @@
1
- export class g {
2
- constructor(e) {
3
- this.a1 = e;
1
+ export class h {
2
+ constructor(c) {
3
+ this.a9 = c;
4
4
  }
5
5
  get id() {
6
6
  return this.a?.id ?? -1;
@@ -19,16 +19,16 @@ export class g {
19
19
  remoteFamily;
20
20
  remotePort;
21
21
  async sendErrorResponse(error, requestId) {
22
- return (this.a ?? await this.c()).sendErrorResponse(error, requestId);
22
+ return (this.a ?? await this.d()).sendErrorResponse(error, requestId);
23
23
  }
24
24
  async sendResponse(request, requestId) {
25
- return (this.a ?? await this.c()).sendResponse(request, requestId);
25
+ return (this.a ?? await this.d()).sendResponse(request, requestId);
26
26
  }
27
27
  async sendMessage(message) {
28
- return (this.a ?? await this.c()).sendMessage(message);
28
+ return (this.a ?? await this.d()).sendMessage(message);
29
29
  }
30
30
  async sendRequest(request) {
31
- return (this.a ?? await this.c()).sendRequest(request);
31
+ return (this.a ?? await this.d()).sendRequest(request);
32
32
  }
33
33
  async close(err) {
34
34
  const { a } = this;
@@ -37,41 +37,41 @@ export class g {
37
37
  await a.close(err);
38
38
  return;
39
39
  }
40
- if (this.p) {
41
- await this.p.then((conn) => {
40
+ if (this.q) {
41
+ await this.q.then((conn) => {
42
42
  this.a = undefined;
43
43
  conn.close(err);
44
44
  }, () => this.a = undefined);
45
45
  }
46
46
  }
47
- async reconnect() {
47
+ async an() {
48
48
  if (!this.a) {
49
- await this.c();
49
+ await this.d();
50
50
  }
51
51
  }
52
- a5() {
52
+ ao() {
53
53
  this.a = undefined;
54
54
  }
55
- async c() {
56
- let { p } = this;
57
- if (!p) {
58
- p = this.p = new Promise((d, i) => {
59
- this.a1().then((conn) => {
55
+ async d() {
56
+ let { q } = this;
57
+ if (!q) {
58
+ q = this.q = new Promise((f, j) => {
59
+ this.a9().then((conn) => {
60
60
  if (conn) {
61
- d(conn);
61
+ f(conn);
62
62
  this.a = conn;
63
- this.p = undefined;
63
+ this.q = undefined;
64
64
  }
65
65
  else {
66
- i(new Error('Connect failed'));
66
+ j(new Error('Connect failed'));
67
67
  }
68
68
  });
69
69
  });
70
70
  }
71
- return await p;
71
+ return await q;
72
72
  }
73
- a1;
73
+ a9;
74
74
  a;
75
- p;
75
+ q;
76
76
  }
77
77
  //# sourceMappingURL=LazyConn.js.map
@@ -1,14 +1,14 @@
1
1
  export var NetErrorString;
2
2
  (function (NetErrorString) {
3
- NetErrorString["I"] = "AuthTimeout";
3
+ NetErrorString["H"] = "AuthTimeout";
4
4
  NetErrorString["a"] = "BadRequest";
5
- NetErrorString["k"] = "ConnectFailed";
6
- NetErrorString["a1"] = "ConnectionClosed";
5
+ NetErrorString["p"] = "ConnectFailed";
6
+ NetErrorString["Y"] = "ConnectionClosed";
7
7
  NetErrorString["b"] = "InvalidConnString";
8
- NetErrorString["l"] = "InvalidConnection";
9
- NetErrorString["D"] = "InvalidPackage";
8
+ NetErrorString["m"] = "InvalidConnection";
9
+ NetErrorString["$"] = "InvalidPackage";
10
10
  NetErrorString["g"] = "NotAuthenticated";
11
- NetErrorString["a2"] = "Timeout";
12
- NetErrorString["m"] = "UnknownError";
11
+ NetErrorString["Z"] = "Timeout";
12
+ NetErrorString["l"] = "UnknownError";
13
13
  })(NetErrorString || (NetErrorString = {}));
14
14
  //# sourceMappingURL=NetErrorString.js.map