justrun-ws 0.1.2 → 0.2.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.
- package/lib/WsClient.js +198 -0
- package/lib/bundle.min.js +1 -1
- package/lib/common/AdjustableTimer.js +42 -0
- package/lib/common/DataWrapper.js +16 -16
- package/lib/common/List.js +27 -27
- package/lib/common/utils.js +19 -19
- package/lib/common/varUintOps.js +51 -51
- package/lib/handler/SimpleHandler.js +49 -55
- package/lib/handler/common.js +6 -9
- package/lib/index.d.ts +48 -135
- package/lib/index.js +2 -5
- package/lib/net/BrowserWsNet.js +48 -45
- package/lib/net/LazyConn.js +41 -30
- package/lib/net/PackageWrapper.js +16 -16
- package/lib/net/TimeoutMonitor.js +40 -40
- package/lib/net/TypedPkgHub.js +167 -164
- package/lib/net/hubPkgSerializer.js +4 -4
- package/package.json +1 -1
|
@@ -1,91 +1,88 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export class
|
|
3
|
-
constructor(wrapperCtor, d,
|
|
4
|
-
this.
|
|
5
|
-
this.
|
|
6
|
-
this.
|
|
1
|
+
import { c, b } from './common';
|
|
2
|
+
export class j {
|
|
3
|
+
constructor(wrapperCtor, d, f = true) {
|
|
4
|
+
this.l = wrapperCtor;
|
|
5
|
+
this.x = d;
|
|
6
|
+
this.y = f;
|
|
7
7
|
}
|
|
8
8
|
get pkgType() {
|
|
9
|
-
return this.
|
|
9
|
+
return this.l.pkgType;
|
|
10
10
|
}
|
|
11
|
-
get
|
|
12
|
-
return this.
|
|
11
|
+
get m() {
|
|
12
|
+
return this.l.name;
|
|
13
13
|
}
|
|
14
|
-
async
|
|
15
|
-
const { buff, bodyOffset } =
|
|
16
|
-
const
|
|
17
|
-
const message = new this.
|
|
18
|
-
if (conn.context !== undefined || !this.
|
|
14
|
+
async $(conn, a) {
|
|
15
|
+
const { buff, bodyOffset } = a;
|
|
16
|
+
const h = buff.subarray(bodyOffset);
|
|
17
|
+
const message = new this.l(h, true);
|
|
18
|
+
if (conn.context !== undefined || !this.y) {
|
|
19
19
|
try {
|
|
20
|
-
await this.
|
|
20
|
+
await this.x(conn, message);
|
|
21
21
|
}
|
|
22
22
|
catch (err) {
|
|
23
23
|
if (!err.isError) {
|
|
24
|
-
|
|
24
|
+
b(conn, err);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
|
-
|
|
29
|
+
b(conn, c);
|
|
30
30
|
}
|
|
31
31
|
if (conn.context === undefined) {
|
|
32
32
|
await conn.close();
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
async
|
|
36
|
-
if (conn.context !== undefined || !this.
|
|
35
|
+
async r(conn, message) {
|
|
36
|
+
if (conn.context !== undefined || !this.y) {
|
|
37
37
|
try {
|
|
38
|
-
return await this.
|
|
38
|
+
return await this.x(conn, message);
|
|
39
39
|
}
|
|
40
40
|
catch (err) {
|
|
41
41
|
if (!err.isError) {
|
|
42
|
-
|
|
42
|
+
b(conn, err);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
else {
|
|
47
|
-
|
|
47
|
+
b(conn, c);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
g;
|
|
54
|
-
m;
|
|
55
|
-
n;
|
|
50
|
+
l;
|
|
51
|
+
x;
|
|
52
|
+
y;
|
|
56
53
|
}
|
|
57
|
-
export class
|
|
58
|
-
constructor(wrapperCtor, d,
|
|
59
|
-
this.
|
|
60
|
-
this.
|
|
61
|
-
this.
|
|
54
|
+
export class n {
|
|
55
|
+
constructor(wrapperCtor, d, f = true) {
|
|
56
|
+
this.l = wrapperCtor;
|
|
57
|
+
this.x = d;
|
|
58
|
+
this.y = f;
|
|
62
59
|
}
|
|
63
60
|
get pkgType() {
|
|
64
|
-
return this.
|
|
61
|
+
return this.l.pkgType;
|
|
65
62
|
}
|
|
66
|
-
get
|
|
67
|
-
return this.
|
|
63
|
+
get m() {
|
|
64
|
+
return this.l.name;
|
|
68
65
|
}
|
|
69
|
-
async
|
|
70
|
-
const { buff, requestId, bodyOffset } =
|
|
71
|
-
const
|
|
72
|
-
const request = new this.
|
|
66
|
+
async $(conn, a) {
|
|
67
|
+
const { buff, requestId, bodyOffset } = a;
|
|
68
|
+
const h = buff.subarray(bodyOffset);
|
|
69
|
+
const request = new this.l(h, true);
|
|
73
70
|
let error;
|
|
74
|
-
if (conn.context !== undefined || !this.
|
|
71
|
+
if (conn.context !== undefined || !this.y) {
|
|
75
72
|
try {
|
|
76
|
-
await this.
|
|
73
|
+
await this.x(conn, request);
|
|
77
74
|
}
|
|
78
75
|
catch (err) {
|
|
79
76
|
if (err.isError) {
|
|
80
77
|
error = err;
|
|
81
78
|
}
|
|
82
79
|
else {
|
|
83
|
-
error =
|
|
80
|
+
error = b(conn, err);
|
|
84
81
|
}
|
|
85
82
|
}
|
|
86
83
|
}
|
|
87
84
|
else {
|
|
88
|
-
error =
|
|
85
|
+
error = b(conn, c);
|
|
89
86
|
}
|
|
90
87
|
try {
|
|
91
88
|
if (!error) {
|
|
@@ -102,27 +99,24 @@ export class h {
|
|
|
102
99
|
await conn.close(err);
|
|
103
100
|
}
|
|
104
101
|
}
|
|
105
|
-
async
|
|
106
|
-
if (conn.context !== undefined || !this.
|
|
102
|
+
async r(conn, request) {
|
|
103
|
+
if (conn.context !== undefined || !this.y) {
|
|
107
104
|
try {
|
|
108
|
-
return await this.
|
|
105
|
+
return await this.x(conn, request);
|
|
109
106
|
}
|
|
110
107
|
catch (err) {
|
|
111
108
|
if (err.isError) {
|
|
112
109
|
throw err;
|
|
113
110
|
}
|
|
114
111
|
else {
|
|
115
|
-
throw
|
|
112
|
+
throw b(conn, err);
|
|
116
113
|
}
|
|
117
114
|
}
|
|
118
115
|
}
|
|
119
|
-
throw
|
|
120
|
-
}
|
|
121
|
-
wrapUncaughtError(conn, err) {
|
|
122
|
-
return c(conn, err);
|
|
116
|
+
throw b(conn, c);
|
|
123
117
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
118
|
+
l;
|
|
119
|
+
x;
|
|
120
|
+
y;
|
|
127
121
|
}
|
|
128
122
|
//# sourceMappingURL=SimpleHandler.js.map
|
package/lib/handler/common.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { JsonError } from '../net/JsonPackageWrapper';
|
|
2
|
-
export class
|
|
2
|
+
export class CommonError extends JsonError {
|
|
3
3
|
}
|
|
4
|
-
export const
|
|
5
|
-
let
|
|
6
|
-
return new
|
|
4
|
+
export const c = new Error('Connection is not authenticated');
|
|
5
|
+
export let b = (a, err) => {
|
|
6
|
+
return new CommonError(err.message);
|
|
7
7
|
};
|
|
8
|
-
export const
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
export const h = (handler) => {
|
|
12
|
-
e = handler;
|
|
8
|
+
export const handleUncaughtErrorWith = (handler) => {
|
|
9
|
+
b = handler;
|
|
13
10
|
};
|
|
14
11
|
//# sourceMappingURL=common.js.map
|
package/lib/index.d.ts
CHANGED
|
@@ -43,34 +43,6 @@ export declare abstract class RequestWrapper<RequestT, ResponseT> extends PkgWra
|
|
|
43
43
|
export type ErrorWrapperCtor<ErrorT, WrapperT extends ErrorWrapper<ErrorT> = ErrorWrapper<ErrorT>> = new (error: ErrorT | Uint8Array, needParse?: true) => WrapperT;
|
|
44
44
|
export type MessageWrapperCtor<MessageT, WrapperT extends MessageWrapper<MessageT> = MessageWrapper<MessageT>> = new (message: MessageT | Uint8Array, needParse?: true) => WrapperT;
|
|
45
45
|
export type RequestWrapperCtor<RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT> = RequestWrapper<RequestT, ResponseT>> = new (request: RequestT | Uint8Array, needParse?: true, _response?: ResponseT) => WrapperT;
|
|
46
|
-
export interface AddressInfo {
|
|
47
|
-
address: string;
|
|
48
|
-
family: string;
|
|
49
|
-
port: number;
|
|
50
|
-
}
|
|
51
|
-
export interface IRawPkgConn {
|
|
52
|
-
readonly localAddress?: string;
|
|
53
|
-
readonly localPort?: number;
|
|
54
|
-
readonly remoteAddress?: string;
|
|
55
|
-
readonly remoteFamily?: string;
|
|
56
|
-
readonly remotePort?: number;
|
|
57
|
-
sendPkgBuff(buff: Uint8Array): Promise<void>;
|
|
58
|
-
sendPkgBuffV(buffVector: Uint8Array[]): Promise<void>;
|
|
59
|
-
close(err?: Error): Promise<void>;
|
|
60
|
-
hadClosed(): boolean;
|
|
61
|
-
}
|
|
62
|
-
export interface IPkgConnWrapper<ConnWrapperT> {
|
|
63
|
-
readonly conn: ConnWrapperT;
|
|
64
|
-
onPkgBuff: (buff: Uint8Array) => void;
|
|
65
|
-
onClose: (err?: Error) => void;
|
|
66
|
-
}
|
|
67
|
-
export interface INet<ConnWrapperT> {
|
|
68
|
-
address(): AddressInfo | null;
|
|
69
|
-
setConnWrapperMaker(makeWrapper: (conn: IRawPkgConn, isActive?: boolean) => IPkgConnWrapper<ConnWrapperT>): void;
|
|
70
|
-
startListen(connStr?: string): Promise<boolean>;
|
|
71
|
-
stopListen(): Promise<boolean>;
|
|
72
|
-
connect(connStr: string): Promise<ConnWrapperT>;
|
|
73
|
-
}
|
|
74
46
|
export interface IConn<ConnContextT = void> {
|
|
75
47
|
context?: ConnContextT | undefined;
|
|
76
48
|
readonly id: number;
|
|
@@ -85,90 +57,12 @@ export interface IConn<ConnContextT = void> {
|
|
|
85
57
|
sendResponse<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>, requestId: number): Promise<void>;
|
|
86
58
|
close(err?: Error): Promise<void>;
|
|
87
59
|
}
|
|
88
|
-
export interface IPkgHub<ConnContextT = void, HubContextT = void> {
|
|
89
|
-
context?: HubContextT | undefined;
|
|
90
|
-
address(): AddressInfo | null;
|
|
91
|
-
getConn(connId: number): IConn<ConnContextT> | undefined;
|
|
92
|
-
connect(connStr: string): Promise<IConn<ConnContextT>>;
|
|
93
|
-
connectLocal(): Promise<IConn<ConnContextT>>;
|
|
94
|
-
startServer(connStr?: string): Promise<boolean>;
|
|
95
|
-
stopServer(): Promise<boolean>;
|
|
96
|
-
}
|
|
97
60
|
export type ParseResult<PkgTypeT extends number | string> = {
|
|
98
61
|
buff: Uint8Array;
|
|
99
62
|
pkgType: PkgTypeT;
|
|
100
63
|
requestId: number;
|
|
101
64
|
bodyOffset: number;
|
|
102
65
|
};
|
|
103
|
-
export interface IPkgHandler<PkgTypeT extends number | string, ConnContextT = void> {
|
|
104
|
-
get pkgType(): PkgTypeT;
|
|
105
|
-
get pkgName(): string;
|
|
106
|
-
run(conn: IConn<ConnContextT>, parseResult: ParseResult<PkgTypeT>): void | Promise<void>;
|
|
107
|
-
wrapUncaughtError(conn: IConn<unknown>, err: Error): ErrorWrapper<unknown>;
|
|
108
|
-
}
|
|
109
|
-
export interface IMessageHandler<PkgTypeT extends number | string, MessageT, WrapperT extends MessageWrapper<MessageT>, ConnContextT = void> extends IPkgHandler<PkgTypeT, ConnContextT> {
|
|
110
|
-
runLocal(conn: IConn<ConnContextT>, message: WrapperT): void | Promise<void>;
|
|
111
|
-
}
|
|
112
|
-
export interface IRequestHandler<PkgTypeT extends number | string, RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT>, ConnContextT = void> extends IPkgHandler<PkgTypeT, ConnContextT> {
|
|
113
|
-
runLocal(conn: IConn<ConnContextT>, request: WrapperT): void | Promise<void>;
|
|
114
|
-
}
|
|
115
|
-
export type HandleFunc<WrapperT, ConnContextT> = (conn: IConn<ConnContextT>, wrapper: WrapperT) => void | Promise<void>;
|
|
116
|
-
export declare class SimpleMessageHandler<PkgTypeT extends number | string, MessageT, WrapperT extends MessageWrapper<MessageT>, ConnContextT> implements IMessageHandler<PkgTypeT, MessageT, WrapperT, ConnContextT> {
|
|
117
|
-
constructor(wrapperCtor: MessageWrapperCtor<MessageT, WrapperT>, handleFunc: HandleFunc<WrapperT, ConnContextT>, checkContext?: boolean);
|
|
118
|
-
get pkgType(): PkgTypeT;
|
|
119
|
-
get pkgName(): string;
|
|
120
|
-
run(conn: IConn<ConnContextT>, parseResult: ParseResult<PkgTypeT>): Promise<void>;
|
|
121
|
-
runLocal(conn: IConn<ConnContextT>, message: WrapperT): Promise<void>;
|
|
122
|
-
wrapUncaughtError(conn: IConn<unknown>, err: Error): ErrorWrapper<unknown>;
|
|
123
|
-
private _wrapperCtor;
|
|
124
|
-
private _handleFunc;
|
|
125
|
-
private _checkContext;
|
|
126
|
-
}
|
|
127
|
-
export declare class BrowserWsNet<ConnWrapperT> implements INet<ConnWrapperT> {
|
|
128
|
-
address(): AddressInfo | null;
|
|
129
|
-
setConnWrapperMaker(makeWrapper: (conn: IRawPkgConn, isActive?: boolean) => IPkgConnWrapper<ConnWrapperT>): void;
|
|
130
|
-
startListen(_connStr?: string): Promise<boolean>;
|
|
131
|
-
stopListen(): Promise<boolean>;
|
|
132
|
-
connect(connStr: string): Promise<ConnWrapperT>;
|
|
133
|
-
private setupConn;
|
|
134
|
-
private makeWrapper;
|
|
135
|
-
}
|
|
136
|
-
export declare const makeNumTypePkgBuff: (pkgType: number, requestId: number, encoded: Uint8Array[]) => void;
|
|
137
|
-
export declare const parseNumTypePkgBuff: (buff: Uint8Array) => ParseResult<number>;
|
|
138
|
-
export declare abstract class JsonError<ErrorT> extends ErrorWrapper<ErrorT> {
|
|
139
|
-
protected encode(data: ErrorT): Uint8Array;
|
|
140
|
-
protected decode(buffer: Uint8Array): ErrorT;
|
|
141
|
-
}
|
|
142
|
-
export declare abstract class JsonMessage<MessageT> extends MessageWrapper<MessageT> {
|
|
143
|
-
protected encode(data: MessageT): Uint8Array;
|
|
144
|
-
protected decode(buffer: Uint8Array): MessageT;
|
|
145
|
-
}
|
|
146
|
-
export declare abstract class JsonRequest<RequestT, ResponseT> extends RequestWrapper<RequestT, ResponseT> {
|
|
147
|
-
protected encode(data: RequestT): Uint8Array;
|
|
148
|
-
protected decode(buffer: Uint8Array): RequestT;
|
|
149
|
-
protected encodeResponse(response: ResponseT): Uint8Array;
|
|
150
|
-
protected decodeResponse(buffer: Uint8Array): ResponseT;
|
|
151
|
-
}
|
|
152
|
-
export declare class LazyConn<ConnContextT> implements IConn<ConnContextT> {
|
|
153
|
-
constructor(connectFunc: () => Promise<IConn<ConnContextT> | undefined>);
|
|
154
|
-
get id(): number;
|
|
155
|
-
get context(): ConnContextT | undefined;
|
|
156
|
-
set context(val: ConnContextT | undefined);
|
|
157
|
-
localAddress?: string | undefined;
|
|
158
|
-
localPort?: number | undefined;
|
|
159
|
-
remoteAddress?: string | undefined;
|
|
160
|
-
remoteFamily?: string | undefined;
|
|
161
|
-
remotePort?: number | undefined;
|
|
162
|
-
sendErrorResponse<ErrorT>(error: ErrorWrapper<ErrorT>, requestId: number): Promise<void>;
|
|
163
|
-
sendResponse<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>, requestId: number): Promise<void>;
|
|
164
|
-
sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
|
|
165
|
-
sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<void>;
|
|
166
|
-
close(err?: Error): Promise<void>;
|
|
167
|
-
private connect;
|
|
168
|
-
private _connectFunc;
|
|
169
|
-
private _conn?;
|
|
170
|
-
private _waitingConn?;
|
|
171
|
-
}
|
|
172
66
|
declare class ListNode {
|
|
173
67
|
constructor();
|
|
174
68
|
getPrev<SelfT extends ListNode>(this: SelfT): SelfT;
|
|
@@ -203,38 +97,57 @@ export type TypedPkgHubOpts<PkgTypeT extends number | string> = {
|
|
|
203
97
|
makePkgBuff: (pkgType: PkgTypeT, requestId: number, encoded: Uint8Array[]) => void;
|
|
204
98
|
parsePkgBuff: (buff: Uint8Array) => ParseResult<PkgTypeT>;
|
|
205
99
|
};
|
|
206
|
-
export declare class
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
100
|
+
export declare abstract class JsonError<ErrorT> extends ErrorWrapper<ErrorT> {
|
|
101
|
+
protected encode(data: ErrorT): Uint8Array;
|
|
102
|
+
protected decode(buffer: Uint8Array): ErrorT;
|
|
103
|
+
}
|
|
104
|
+
export declare abstract class JsonMessage<MessageT> extends MessageWrapper<MessageT> {
|
|
105
|
+
protected encode(data: MessageT): Uint8Array;
|
|
106
|
+
protected decode(buffer: Uint8Array): MessageT;
|
|
107
|
+
}
|
|
108
|
+
export declare abstract class JsonRequest<RequestT, ResponseT> extends RequestWrapper<RequestT, ResponseT> {
|
|
109
|
+
protected encode(data: RequestT): Uint8Array;
|
|
110
|
+
protected decode(buffer: Uint8Array): RequestT;
|
|
111
|
+
protected encodeResponse(response: ResponseT): Uint8Array;
|
|
112
|
+
protected decodeResponse(buffer: Uint8Array): ResponseT;
|
|
113
|
+
}
|
|
114
|
+
export declare class CommonError extends JsonError<string> {
|
|
115
|
+
}
|
|
116
|
+
export interface DoneResp<ResultT extends string | number | boolean = boolean> {
|
|
117
|
+
result: ResultT;
|
|
118
|
+
}
|
|
119
|
+
export interface IAuthResp {
|
|
120
|
+
cid: string;
|
|
121
|
+
token: string;
|
|
122
|
+
shard?: number;
|
|
123
|
+
}
|
|
124
|
+
export declare const handleUncaughtErrorWith: (handler: (conn: IConn<unknown>, err: Error) => ErrorWrapper<unknown>) => void;
|
|
125
|
+
export declare const createNumPkgTypeClient: (getConnStr: (shard?: number) => string, timeoutMonitor?: TimeoutMonitor) => WsClient<number>;
|
|
126
|
+
export declare class WsClient<PkgTypeT extends number | string> {
|
|
127
|
+
constructor(getConnStr: (shard?: number) => string, opts: TypedPkgHubOpts<PkgTypeT>);
|
|
220
128
|
registerError<ErrorT>(pkgType: PkgTypeT, wrapperCtor: ErrorWrapperCtor<ErrorT>): void;
|
|
221
129
|
registerMessage<MessageT>(pkgType: PkgTypeT, wrapperCtor: MessageWrapperCtor<MessageT>): void;
|
|
222
130
|
registerRequest<RequestT, ResponseT>(pkgType: PkgTypeT, wrapperCtor: RequestWrapperCtor<RequestT, ResponseT>): void;
|
|
223
|
-
handleMessageWith<MessageT, WrapperT extends MessageWrapper<MessageT>>(
|
|
224
|
-
handleRequestWith<RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT>>(
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
private
|
|
233
|
-
private
|
|
234
|
-
private
|
|
235
|
-
private
|
|
236
|
-
private
|
|
237
|
-
private
|
|
131
|
+
handleMessageWith<MessageT, WrapperT extends MessageWrapper<MessageT>>(wrapperCtor: MessageWrapperCtor<MessageT, WrapperT>, handler: (message: MessageT) => void | Promise<void>): void;
|
|
132
|
+
handleRequestWith<RequestT, ResponseT, WrapperT extends RequestWrapper<RequestT, ResponseT>>(wrapperCtor: RequestWrapperCtor<RequestT, ResponseT, WrapperT>, handler: (request: RequestT) => ResponseT | Promise<ResponseT>): void;
|
|
133
|
+
sendMessage<MessageT>(message: MessageWrapper<MessageT>): Promise<void>;
|
|
134
|
+
sendRequest<RequestT, ResponseT>(request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
|
|
135
|
+
sendCustomAuthRequest<RequestT, ResponseT extends IAuthResp>(_request: RequestWrapper<RequestT, ResponseT>): Promise<ResponseT>;
|
|
136
|
+
resetAuthState(): void;
|
|
137
|
+
enableAuthenticate(proxyAuthPkgType: PkgTypeT, authStatusCallback: (cid: string) => void, allowAnonymous?: true): void;
|
|
138
|
+
reconnect(delay?: number): void;
|
|
139
|
+
onConnectionStatus(listener: (isOpen: boolean, active: boolean, err?: Error) => void): void;
|
|
140
|
+
private connect;
|
|
141
|
+
private onConnOpen;
|
|
142
|
+
private onConnClose;
|
|
143
|
+
private _pkgHub;
|
|
144
|
+
private _conn;
|
|
145
|
+
private _cid;
|
|
146
|
+
private _connState;
|
|
147
|
+
private _getConnStr;
|
|
148
|
+
private _originConnect;
|
|
149
|
+
private _connectDelay?;
|
|
150
|
+
private _listenerArr?;
|
|
238
151
|
}
|
|
239
152
|
|
|
240
153
|
export {};
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { BrowserWsNet } from './net/BrowserWsNet';
|
|
3
|
-
export { makeNumTypePkgBuff, parseNumTypePkgBuff } from './net/hubPkgSerializer';
|
|
1
|
+
export { CommonError, handleUncaughtErrorWith } from './handler/common';
|
|
4
2
|
export { JsonError, JsonMessage, JsonRequest } from './net/JsonPackageWrapper';
|
|
5
|
-
export { LazyConn } from './net/LazyConn';
|
|
6
3
|
export { ErrorWrapper, EventWrapper, MessageWrapper, RequestWrapper } from './net/PackageWrapper';
|
|
7
4
|
export { TimeoutMonitor } from './net/TimeoutMonitor';
|
|
8
|
-
export {
|
|
5
|
+
export { createNumPkgTypeClient, WsClient } from './WsClient';
|
|
9
6
|
//# sourceMappingURL=index.js.map
|
package/lib/net/BrowserWsNet.js
CHANGED
|
@@ -1,53 +1,56 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export class
|
|
3
|
-
|
|
1
|
+
import { y } from '../common/utils';
|
|
2
|
+
export class l {
|
|
3
|
+
o() {
|
|
4
4
|
return null;
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
this.
|
|
6
|
+
N(C) {
|
|
7
|
+
this.C = C;
|
|
8
8
|
}
|
|
9
|
-
async
|
|
9
|
+
async O(r) {
|
|
10
10
|
return false;
|
|
11
11
|
}
|
|
12
|
-
async
|
|
12
|
+
async P() {
|
|
13
13
|
return false;
|
|
14
14
|
}
|
|
15
|
-
async
|
|
16
|
-
const
|
|
17
|
-
return new Promise((f,
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
async c(d) {
|
|
16
|
+
const e = new WebSocket(d);
|
|
17
|
+
return new Promise((f, i) => {
|
|
18
|
+
e.onerror = (k) => {
|
|
19
|
+
i(new Error('WebSocket error'));
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
f(this.
|
|
21
|
+
e.onopen = () => {
|
|
22
|
+
f(this.a9(e, true));
|
|
23
23
|
};
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
const { conn,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
a9 = (e, q) => {
|
|
27
|
+
const b = new m(e);
|
|
28
|
+
const { conn, D, n } = this.C(b, q);
|
|
29
|
+
e.onmessage = (j) => {
|
|
30
|
+
j.data.arrayBuffer().then((buff) => {
|
|
31
|
+
D(new Uint8Array(buff));
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
e.onerror = (k) => {
|
|
35
|
+
n(new Error('WebSocket error'));
|
|
36
36
|
};
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
|
|
37
|
+
e.onclose = (j) => {
|
|
38
|
+
if (j.code === 1000) {
|
|
39
|
+
n();
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
n(new Error(j.reason));
|
|
40
43
|
}
|
|
41
44
|
};
|
|
42
45
|
return conn;
|
|
43
46
|
};
|
|
44
|
-
|
|
47
|
+
C(a, s) {
|
|
45
48
|
throw new Error('Need to call "setConnWrapperMaker" before using');
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
|
-
class
|
|
49
|
-
constructor(
|
|
50
|
-
this.
|
|
51
|
+
class m {
|
|
52
|
+
constructor(e) {
|
|
53
|
+
this.g = e;
|
|
51
54
|
}
|
|
52
55
|
get localAddress() {
|
|
53
56
|
return;
|
|
@@ -64,36 +67,36 @@ class i {
|
|
|
64
67
|
get remotePort() {
|
|
65
68
|
return;
|
|
66
69
|
}
|
|
67
|
-
async
|
|
68
|
-
if (this.
|
|
69
|
-
return this.
|
|
70
|
+
async I(buff) {
|
|
71
|
+
if (this.g) {
|
|
72
|
+
return this.g.send(buff);
|
|
70
73
|
}
|
|
71
74
|
throw new Error('Invalid socket');
|
|
72
75
|
}
|
|
73
|
-
async
|
|
74
|
-
if (this.
|
|
75
|
-
const buff =
|
|
76
|
-
return this.
|
|
76
|
+
async p(h) {
|
|
77
|
+
if (this.g) {
|
|
78
|
+
const buff = y(h);
|
|
79
|
+
return this.g.send(buff);
|
|
77
80
|
}
|
|
78
81
|
throw new Error('Invalid socket');
|
|
79
82
|
}
|
|
80
83
|
async close(err) {
|
|
81
|
-
const {
|
|
82
|
-
if (
|
|
83
|
-
this.
|
|
84
|
+
const { g } = this;
|
|
85
|
+
if (g) {
|
|
86
|
+
this.g = undefined;
|
|
84
87
|
if (!err) {
|
|
85
|
-
if (
|
|
86
|
-
|
|
88
|
+
if (g.readyState === WebSocket.OPEN) {
|
|
89
|
+
g.close(1000);
|
|
87
90
|
}
|
|
88
91
|
}
|
|
89
92
|
else {
|
|
90
|
-
|
|
93
|
+
g.close(3000, err.message);
|
|
91
94
|
}
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
|
-
|
|
95
|
-
return !this.
|
|
97
|
+
J() {
|
|
98
|
+
return !this.g;
|
|
96
99
|
}
|
|
97
|
-
|
|
100
|
+
g;
|
|
98
101
|
}
|
|
99
102
|
//# sourceMappingURL=BrowserWsNet.js.map
|
package/lib/net/LazyConn.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export class
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
1
|
+
export class g {
|
|
2
|
+
constructor(d) {
|
|
3
|
+
this.a1 = d;
|
|
4
4
|
}
|
|
5
5
|
get id() {
|
|
6
|
-
return this.
|
|
6
|
+
return this.a?.id ?? -1;
|
|
7
7
|
}
|
|
8
8
|
get context() {
|
|
9
|
-
return this.
|
|
9
|
+
return this.a?.context;
|
|
10
10
|
}
|
|
11
|
-
set context(
|
|
12
|
-
if (this.
|
|
13
|
-
this.
|
|
11
|
+
set context(b) {
|
|
12
|
+
if (this.a) {
|
|
13
|
+
this.a.context = b;
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
localAddress;
|
|
@@ -19,48 +19,59 @@ export class LazyConn {
|
|
|
19
19
|
remoteFamily;
|
|
20
20
|
remotePort;
|
|
21
21
|
async sendErrorResponse(error, requestId) {
|
|
22
|
-
return (this.
|
|
22
|
+
return (this.a ?? await this.c()).sendErrorResponse(error, requestId);
|
|
23
23
|
}
|
|
24
24
|
async sendResponse(request, requestId) {
|
|
25
|
-
return (this.
|
|
25
|
+
return (this.a ?? await this.c()).sendResponse(request, requestId);
|
|
26
26
|
}
|
|
27
27
|
async sendMessage(message) {
|
|
28
|
-
return (this.
|
|
28
|
+
return (this.a ?? await this.c()).sendMessage(message);
|
|
29
29
|
}
|
|
30
30
|
async sendRequest(request) {
|
|
31
|
-
return (this.
|
|
31
|
+
return (this.a ?? await this.c()).sendRequest(request);
|
|
32
32
|
}
|
|
33
33
|
async close(err) {
|
|
34
|
-
const {
|
|
35
|
-
if (
|
|
36
|
-
this.
|
|
37
|
-
await
|
|
34
|
+
const { a } = this;
|
|
35
|
+
if (a) {
|
|
36
|
+
this.a = undefined;
|
|
37
|
+
await a.close(err);
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
|
-
if (this.
|
|
41
|
-
await this.
|
|
40
|
+
if (this.p) {
|
|
41
|
+
await this.p.then((conn) => {
|
|
42
|
+
this.a = undefined;
|
|
43
|
+
conn.close(err);
|
|
44
|
+
}, () => this.a = undefined);
|
|
42
45
|
}
|
|
43
46
|
}
|
|
44
|
-
async
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
async reconnect() {
|
|
48
|
+
if (!this.a) {
|
|
49
|
+
await this.c();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
aa() {
|
|
53
|
+
this.a = undefined;
|
|
54
|
+
}
|
|
55
|
+
async c() {
|
|
56
|
+
let { p } = this;
|
|
57
|
+
if (!p) {
|
|
58
|
+
p = this.p = new Promise((f, i) => {
|
|
59
|
+
this.a1().then((conn) => {
|
|
49
60
|
if (conn) {
|
|
50
61
|
f(conn);
|
|
51
|
-
this.
|
|
52
|
-
this.
|
|
62
|
+
this.a = conn;
|
|
63
|
+
this.p = undefined;
|
|
53
64
|
}
|
|
54
65
|
else {
|
|
55
|
-
|
|
66
|
+
i(new Error('Connect failed'));
|
|
56
67
|
}
|
|
57
68
|
});
|
|
58
69
|
});
|
|
59
70
|
}
|
|
60
|
-
return await
|
|
71
|
+
return await p;
|
|
61
72
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
73
|
+
a1;
|
|
74
|
+
a;
|
|
75
|
+
p;
|
|
65
76
|
}
|
|
66
77
|
//# sourceMappingURL=LazyConn.js.map
|