@litert/televoke 0.4.4 → 0.4.6
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/CHANGES.md +12 -0
- package/lib/Client/Common.d.ts +1 -1
- package/lib/Client/Common.d.ts.map +1 -1
- package/lib/Client/HttpClient.d.ts.map +1 -1
- package/lib/Client/HttpClient.js +17 -9
- package/lib/Client/HttpClient.js.map +1 -1
- package/lib/Client/HttpsClient.d.ts.map +1 -1
- package/lib/Client/HttpsClient.js +17 -9
- package/lib/Client/HttpsClient.js.map +1 -1
- package/lib/Client/TCPClient.d.ts +5 -0
- package/lib/Client/TCPClient.d.ts.map +1 -1
- package/lib/Client/TCPClient.js +8 -4
- package/lib/Client/TCPClient.js.map +1 -1
- package/lib/Client/TLSClient.d.ts.map +1 -1
- package/lib/Client/TLSClient.js +3 -4
- package/lib/Client/TLSClient.js.map +1 -1
- package/lib/Client/index.js +5 -1
- package/lib/Client/index.js.map +1 -1
- package/lib/Common/API.d.ts +1 -1
- package/lib/Common/API.d.ts.map +1 -1
- package/lib/Common/Request.d.ts +1 -1
- package/lib/Common/Request.d.ts.map +1 -1
- package/lib/Common/Response.js +1 -1
- package/lib/Common/Response.js.map +1 -1
- package/lib/Common/index.js +5 -1
- package/lib/Common/index.js.map +1 -1
- package/lib/Encoder/Decoder.d.ts.map +1 -1
- package/lib/Encoder/Decoder.js +23 -13
- package/lib/Encoder/Decoder.js.map +1 -1
- package/lib/Encoder/Encoder.d.ts.map +1 -1
- package/lib/Encoder/Encoder.js +4 -4
- package/lib/Encoder/Encoder.js.map +1 -1
- package/lib/Server/Common/Gateway.d.ts +3 -1
- package/lib/Server/Common/Gateway.d.ts.map +1 -1
- package/lib/Server/Common/Request.d.ts +2 -1
- package/lib/Server/Common/Request.d.ts.map +1 -1
- package/lib/Server/Common/Router.d.ts +2 -2
- package/lib/Server/Common/Router.d.ts.map +1 -1
- package/lib/Server/Common/Server.d.ts +6 -0
- package/lib/Server/Common/Server.d.ts.map +1 -1
- package/lib/Server/Common/index.js +5 -1
- package/lib/Server/Common/index.js.map +1 -1
- package/lib/Server/Errors.d.ts +1 -0
- package/lib/Server/Errors.d.ts.map +1 -1
- package/lib/Server/Errors.js +7 -1
- package/lib/Server/Errors.js.map +1 -1
- package/lib/Server/Gateways/Http.d.ts +1 -1
- package/lib/Server/Gateways/Http.d.ts.map +1 -1
- package/lib/Server/Gateways/Http.js +30 -13
- package/lib/Server/Gateways/Http.js.map +1 -1
- package/lib/Server/Gateways/TCP.d.ts +6 -1
- package/lib/Server/Gateways/TCP.d.ts.map +1 -1
- package/lib/Server/Gateways/TCP.js +19 -9
- package/lib/Server/Gateways/TCP.js.map +1 -1
- package/lib/Server/Server.d.ts.map +1 -1
- package/lib/Server/Server.js +30 -16
- package/lib/Server/Server.js.map +1 -1
- package/lib/Server/index.js +5 -1
- package/lib/Server/index.js.map +1 -1
- package/lib/index.js +5 -1
- package/lib/index.js.map +1 -1
- package/package.json +7 -9
- package/src/lib/Client/Common.ts +2 -1
- package/src/lib/Client/HttpClient.ts +19 -16
- package/src/lib/Client/HttpsClient.ts +19 -16
- package/src/lib/Client/TCPClient.ts +16 -12
- package/src/lib/Client/TLSClient.ts +12 -13
- package/src/lib/Common/Request.ts +1 -1
- package/src/lib/Encoder/Decoder.ts +28 -13
- package/src/lib/Encoder/Encoder.ts +4 -5
- package/src/lib/Server/Common/Gateway.ts +4 -0
- package/src/lib/Server/Common/Request.ts +3 -1
- package/src/lib/Server/Common/Router.ts +7 -2
- package/src/lib/Server/Common/Server.ts +7 -0
- package/src/lib/Server/Errors.ts +7 -0
- package/src/lib/Server/Gateways/Http.ts +37 -13
- package/src/lib/Server/Gateways/TCP.ts +21 -10
- package/src/lib/Server/Server.ts +38 -16
- package/src/lib/Server/SimpleRouter.ts +1 -1
|
@@ -39,7 +39,7 @@ interface IClient {
|
|
|
39
39
|
|
|
40
40
|
socket: $Net.Socket;
|
|
41
41
|
|
|
42
|
-
decoder: G.IDecoder<
|
|
42
|
+
decoder: G.IDecoder<C.IRequest>;
|
|
43
43
|
|
|
44
44
|
status: EClientStatus;
|
|
45
45
|
|
|
@@ -48,7 +48,7 @@ interface IClient {
|
|
|
48
48
|
|
|
49
49
|
class TCPGateway implements C.IGateway {
|
|
50
50
|
|
|
51
|
-
private _socket: $Net.Server = $Net.createServer(this._onConnect.bind(this));
|
|
51
|
+
private readonly _socket: $Net.Server = $Net.createServer(this._onConnect.bind(this));
|
|
52
52
|
|
|
53
53
|
private _status: EStatus = EStatus.IDLE;
|
|
54
54
|
|
|
@@ -64,16 +64,17 @@ class TCPGateway implements C.IGateway {
|
|
|
64
64
|
) => void;
|
|
65
65
|
|
|
66
66
|
public constructor(
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
public host: string,
|
|
68
|
+
public port: number,
|
|
69
|
+
private readonly _backlog?: number,
|
|
70
|
+
) {}
|
|
70
71
|
|
|
71
72
|
private _onConnect(socket: $Net.Socket): void {
|
|
72
73
|
|
|
73
74
|
const clientId = this._clientIdCounter++;
|
|
74
75
|
|
|
75
76
|
const encoder = createEncoder();
|
|
76
|
-
const decoder = createDecoder<
|
|
77
|
+
const decoder = createDecoder<C.IRequest>();
|
|
77
78
|
const client = {
|
|
78
79
|
decoder,
|
|
79
80
|
socket,
|
|
@@ -93,6 +94,7 @@ class TCPGateway implements C.IGateway {
|
|
|
93
94
|
decoder.onData = (data) => {
|
|
94
95
|
|
|
95
96
|
client.loads++;
|
|
97
|
+
data.ip = (socket.address() as any).address;
|
|
96
98
|
|
|
97
99
|
this.onRequest(
|
|
98
100
|
data,
|
|
@@ -141,11 +143,15 @@ class TCPGateway implements C.IGateway {
|
|
|
141
143
|
|
|
142
144
|
return new Promise((resolve, reject) => {
|
|
143
145
|
|
|
144
|
-
this._socket.listen(this.
|
|
146
|
+
this._socket.listen(this.port, this.host, this._backlog)
|
|
145
147
|
.once('listening', () => {
|
|
146
148
|
|
|
147
149
|
this._socket.removeAllListeners('error');
|
|
148
150
|
this._status = EStatus.WORKING;
|
|
151
|
+
if (this.port === 0) {
|
|
152
|
+
|
|
153
|
+
this.port = (this._socket.address() as $Net.AddressInfo).port;
|
|
154
|
+
}
|
|
149
155
|
resolve();
|
|
150
156
|
})
|
|
151
157
|
.once('error', (e) => {
|
|
@@ -178,7 +184,7 @@ class TCPGateway implements C.IGateway {
|
|
|
178
184
|
case EStatus.WORKING:
|
|
179
185
|
return new Promise((resolve) => {
|
|
180
186
|
|
|
181
|
-
this._socket.close(() => resolve());
|
|
187
|
+
this._socket.close(() => { resolve(); });
|
|
182
188
|
|
|
183
189
|
for (const clientId in this._clients) {
|
|
184
190
|
|
|
@@ -204,7 +210,12 @@ class TCPGateway implements C.IGateway {
|
|
|
204
210
|
}
|
|
205
211
|
}
|
|
206
212
|
|
|
207
|
-
|
|
213
|
+
/**
|
|
214
|
+
* Create a gateway of TCP-base televoke/1 protocol.
|
|
215
|
+
*
|
|
216
|
+
* @deprecated This legacy TCP-based televoke/1 protocol is no longer maintained, and will be removed in v2.0.0
|
|
217
|
+
*/
|
|
218
|
+
export function createTCPGateway(host: string, port: number, backlog?: number): C.IGateway {
|
|
208
219
|
|
|
209
|
-
return new TCPGateway(host, port);
|
|
220
|
+
return new TCPGateway(host, port, backlog);
|
|
210
221
|
}
|
package/src/lib/Server/Server.ts
CHANGED
|
@@ -35,6 +35,18 @@ class Server extends Events.EventEmitter<C.IServerEvents> implements C.IServer {
|
|
|
35
35
|
|
|
36
36
|
private _status: EStatus = EStatus.IDLE;
|
|
37
37
|
|
|
38
|
+
public getGateway(name: string): C.IGateway {
|
|
39
|
+
|
|
40
|
+
const ret = this._gateways[name];
|
|
41
|
+
|
|
42
|
+
if (!ret) {
|
|
43
|
+
|
|
44
|
+
throw new E.E_GATEWAY_NOT_FOUND({ name });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return ret;
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
public addGateway(name: string, gateway: C.IGateway): this {
|
|
39
51
|
|
|
40
52
|
if (this._status !== EStatus.IDLE) {
|
|
@@ -44,7 +56,7 @@ class Server extends Events.EventEmitter<C.IServerEvents> implements C.IServer {
|
|
|
44
56
|
|
|
45
57
|
this._gateways[name] = gateway;
|
|
46
58
|
|
|
47
|
-
if (gateway.onError
|
|
59
|
+
if (gateway.onError ?? gateway.onRequest) {
|
|
48
60
|
|
|
49
61
|
throw new E.E_GATEWAY_BUSY();
|
|
50
62
|
}
|
|
@@ -60,24 +72,26 @@ class Server extends Events.EventEmitter<C.IServerEvents> implements C.IServer {
|
|
|
60
72
|
|
|
61
73
|
if (!handler) {
|
|
62
74
|
|
|
63
|
-
|
|
75
|
+
reply({
|
|
64
76
|
rid: req.rid,
|
|
65
77
|
srt: req.srt,
|
|
66
78
|
sst: Date.now(),
|
|
67
79
|
code: G.EResponseCode.API_NOT_FOUND,
|
|
68
80
|
body: null
|
|
69
81
|
});
|
|
82
|
+
return;
|
|
70
83
|
}
|
|
71
84
|
|
|
72
85
|
if (!this._router.validate(req, handler)) {
|
|
73
86
|
|
|
74
|
-
|
|
87
|
+
reply({
|
|
75
88
|
rid: req.rid,
|
|
76
89
|
srt: req.srt,
|
|
77
90
|
sst: Date.now(),
|
|
78
91
|
code: G.EResponseCode.MALFORMED_ARGUMENTS,
|
|
79
92
|
body: null
|
|
80
93
|
});
|
|
94
|
+
return;
|
|
81
95
|
}
|
|
82
96
|
|
|
83
97
|
let resultPr: any;
|
|
@@ -101,21 +115,29 @@ class Server extends Events.EventEmitter<C.IServerEvents> implements C.IServer {
|
|
|
101
115
|
|
|
102
116
|
if (resultPr instanceof Promise) {
|
|
103
117
|
|
|
104
|
-
resultPr.then(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
resultPr.then(
|
|
119
|
+
(body) => {
|
|
120
|
+
reply({
|
|
121
|
+
rid: req.rid,
|
|
122
|
+
srt: req.srt,
|
|
123
|
+
sst: Date.now(),
|
|
124
|
+
code: G.EResponseCode.OK,
|
|
125
|
+
body: body ?? null
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
(err) => {
|
|
129
|
+
reply({
|
|
130
|
+
rid: req.rid,
|
|
131
|
+
srt: req.srt,
|
|
132
|
+
sst: Date.now(),
|
|
133
|
+
code: G.EResponseCode.FAILURE,
|
|
134
|
+
body: err ?? null
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
);
|
|
117
138
|
}
|
|
118
139
|
else {
|
|
140
|
+
|
|
119
141
|
reply({
|
|
120
142
|
rid: req.rid,
|
|
121
143
|
srt: req.srt,
|