@litert/televoke 1.1.0 → 1.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.
- package/CHANGES.md +25 -0
- package/docs/README.md +6 -0
- package/docs/Televoke_v2.1.md +499 -0
- package/docs/Televoke_v2.md +1 -1
- package/lib/client/Client.Constants.d.ts +5 -0
- package/lib/client/Client.Constants.d.ts.map +1 -0
- package/lib/client/Client.Constants.js +9 -0
- package/lib/client/Client.Constants.js.map +1 -0
- package/lib/client/Client.decl.d.ts +32 -2
- package/lib/client/Client.decl.d.ts.map +1 -1
- package/lib/client/Client.decl.js +1 -1
- package/lib/client/JsonApiClient.impl.d.ts +1 -1
- package/lib/client/JsonApiClient.impl.d.ts.map +1 -1
- package/lib/client/JsonApiClient.impl.js +30 -5
- package/lib/client/JsonApiClient.impl.js.map +1 -1
- package/lib/client/index.d.ts +1 -1
- package/lib/client/index.js +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/server/LegacyServerChannel.impl.d.ts +2 -2
- package/lib/server/LegacyServerChannel.impl.d.ts.map +1 -1
- package/lib/server/LegacyServerChannel.impl.js +1 -1
- package/lib/server/Server.decl.d.ts +18 -3
- package/lib/server/Server.decl.d.ts.map +1 -1
- package/lib/server/Server.decl.js +1 -1
- package/lib/server/Server.impl.d.ts +1 -1
- package/lib/server/Server.impl.d.ts.map +1 -1
- package/lib/server/Server.impl.js +5 -4
- package/lib/server/Server.impl.js.map +1 -1
- package/lib/server/ServerChannel.d.ts +1 -1
- package/lib/server/ServerChannel.js +1 -1
- package/lib/server/SimpleJsonApiRouter.d.ts +2 -2
- package/lib/server/SimpleJsonApiRouter.d.ts.map +1 -1
- package/lib/server/SimpleJsonApiRouter.js +9 -4
- package/lib/server/SimpleJsonApiRouter.js.map +1 -1
- package/lib/server/index.d.ts +1 -1
- package/lib/server/index.js +1 -1
- package/lib/shared/BinaryStream.d.ts +1 -1
- package/lib/shared/BinaryStream.js +1 -1
- package/lib/shared/BinaryStreamManager.d.ts +1 -1
- package/lib/shared/BinaryStreamManager.js +1 -1
- package/lib/shared/Channel.impl.d.ts +2 -2
- package/lib/shared/Channel.impl.d.ts.map +1 -1
- package/lib/shared/Channel.impl.js +23 -11
- package/lib/shared/Channel.impl.js.map +1 -1
- package/lib/shared/Constants.d.ts +1 -1
- package/lib/shared/Constants.js +1 -1
- package/lib/shared/Encodings/index.d.ts +1 -1
- package/lib/shared/Encodings/index.js +1 -1
- package/lib/shared/Encodings/v1/EncoderV1.d.ts +1 -1
- package/lib/shared/Encodings/v1/EncoderV1.js +1 -1
- package/lib/shared/Encodings/v1/Protocol.v1.decls.d.ts +1 -1
- package/lib/shared/Encodings/v1/Protocol.v1.decls.js +1 -1
- package/lib/shared/Encodings/v1/index.d.ts +1 -1
- package/lib/shared/Encodings/v1/index.js +1 -1
- package/lib/shared/Encodings/v2/Constants.v2.d.ts +14 -1
- package/lib/shared/Encodings/v2/Constants.v2.d.ts.map +1 -1
- package/lib/shared/Encodings/v2/Constants.v2.js +17 -2
- package/lib/shared/Encodings/v2/Constants.v2.js.map +1 -1
- package/lib/shared/Encodings/v2/DecoderV2.d.ts +1 -1
- package/lib/shared/Encodings/v2/DecoderV2.d.ts.map +1 -1
- package/lib/shared/Encodings/v2/DecoderV2.js +81 -7
- package/lib/shared/Encodings/v2/DecoderV2.js.map +1 -1
- package/lib/shared/Encodings/v2/EncoderV2.d.ts +3 -2
- package/lib/shared/Encodings/v2/EncoderV2.d.ts.map +1 -1
- package/lib/shared/Encodings/v2/EncoderV2.js +104 -25
- package/lib/shared/Encodings/v2/EncoderV2.js.map +1 -1
- package/lib/shared/Encodings/v2/Encoding.v2.decl.d.ts +70 -40
- package/lib/shared/Encodings/v2/Encoding.v2.decl.d.ts.map +1 -1
- package/lib/shared/Encodings/v2/Encoding.v2.decl.js +1 -1
- package/lib/shared/Encodings/v2/index.d.ts +1 -1
- package/lib/shared/Encodings/v2/index.js +1 -1
- package/lib/shared/Errors.d.ts +2 -1
- package/lib/shared/Errors.d.ts.map +1 -1
- package/lib/shared/Errors.js +2 -1
- package/lib/shared/Errors.js.map +1 -1
- package/lib/shared/Shared.decl.d.ts +17 -17
- package/lib/shared/Shared.decl.d.ts.map +1 -1
- package/lib/shared/Shared.decl.js +1 -1
- package/lib/shared/Utils.d.ts +2 -2
- package/lib/shared/Utils.d.ts.map +1 -1
- package/lib/shared/Utils.js +1 -1
- package/lib/shared/Utils.js.map +1 -1
- package/lib/shared/index.d.ts +1 -1
- package/lib/shared/index.js +1 -1
- package/lib/transporters/Transporter.decl.d.ts +1 -1
- package/lib/transporters/Transporter.decl.js +1 -1
- package/lib/transporters/http-listener/AbstractHttpListener.d.ts +2 -2
- package/lib/transporters/http-listener/AbstractHttpListener.d.ts.map +1 -1
- package/lib/transporters/http-listener/AbstractHttpListener.js +10 -6
- package/lib/transporters/http-listener/AbstractHttpListener.js.map +1 -1
- package/lib/transporters/http-listener/Http.Decl.d.ts +1 -5
- package/lib/transporters/http-listener/Http.Decl.d.ts.map +1 -1
- package/lib/transporters/http-listener/Http.Decl.js +2 -22
- package/lib/transporters/http-listener/Http.Decl.js.map +1 -1
- package/lib/transporters/http-listener/HttpListener.d.ts +1 -1
- package/lib/transporters/http-listener/HttpListener.d.ts.map +1 -1
- package/lib/transporters/http-listener/HttpListener.js +4 -2
- package/lib/transporters/http-listener/HttpListener.js.map +1 -1
- package/lib/transporters/http-listener/HttpUnixSocketListener.d.ts +1 -1
- package/lib/transporters/http-listener/HttpUnixSocketListener.d.ts.map +1 -1
- package/lib/transporters/http-listener/HttpUnixSocketListener.js +4 -2
- package/lib/transporters/http-listener/HttpUnixSocketListener.js.map +1 -1
- package/lib/transporters/http-listener/HttpsListener.d.ts +1 -1
- package/lib/transporters/http-listener/HttpsListener.d.ts.map +1 -1
- package/lib/transporters/http-listener/HttpsListener.js +6 -4
- package/lib/transporters/http-listener/HttpsListener.js.map +1 -1
- package/lib/transporters/http-listener/index.d.ts +1 -1
- package/lib/transporters/http-listener/index.js +1 -1
- package/lib/transporters/index.d.ts +1 -1
- package/lib/transporters/index.js +1 -1
- package/lib/transporters/legacy-http/LegacyHttp.Client.Native.d.ts +12 -2
- package/lib/transporters/legacy-http/LegacyHttp.Client.Native.d.ts.map +1 -1
- package/lib/transporters/legacy-http/LegacyHttp.Client.Native.js +33 -7
- package/lib/transporters/legacy-http/LegacyHttp.Client.Native.js.map +1 -1
- package/lib/transporters/legacy-http/LegacyHttp.Server.d.ts +29 -1
- package/lib/transporters/legacy-http/LegacyHttp.Server.d.ts.map +1 -1
- package/lib/transporters/legacy-http/LegacyHttp.Server.js +35 -10
- package/lib/transporters/legacy-http/LegacyHttp.Server.js.map +1 -1
- package/lib/transporters/legacy-http/LegacyHttp.Transporter.d.ts +1 -1
- package/lib/transporters/legacy-http/LegacyHttp.Transporter.js +1 -1
- package/lib/transporters/legacy-http/index.d.ts +1 -1
- package/lib/transporters/legacy-http/index.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tcp.Client.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tcp.Client.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tcp.Server.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tcp.Server.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tls.Client.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tls.Client.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tls.Server.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.Tls.Server.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.Transporter.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.Transporter.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.UnixSocket.Client.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.UnixSocket.Client.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.UnixSocket.Server.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.UnixSocket.Server.js +1 -1
- package/lib/transporters/lwdfx/LwDFX.decl.d.ts +1 -1
- package/lib/transporters/lwdfx/LwDFX.decl.js +1 -1
- package/lib/transporters/lwdfx/index.d.ts +1 -1
- package/lib/transporters/lwdfx/index.js +1 -1
- package/lib/transporters/memory/Memory.Impl.d.ts +1 -1
- package/lib/transporters/memory/Memory.Impl.js +1 -1
- package/lib/transporters/memory/Memory.Impl.js.map +1 -1
- package/lib/transporters/memory/index.d.ts +1 -1
- package/lib/transporters/memory/index.js +1 -1
- package/lib/transporters/websocket/WebSocket.Client.d.ts +18 -3
- package/lib/transporters/websocket/WebSocket.Client.d.ts.map +1 -1
- package/lib/transporters/websocket/WebSocket.Client.js +21 -1
- package/lib/transporters/websocket/WebSocket.Client.js.map +1 -1
- package/lib/transporters/websocket/WebSocket.Server.d.ts +24 -2
- package/lib/transporters/websocket/WebSocket.Server.d.ts.map +1 -1
- package/lib/transporters/websocket/WebSocket.Server.js +29 -10
- package/lib/transporters/websocket/WebSocket.Server.js.map +1 -1
- package/lib/transporters/websocket/WebSocket.Transporter.d.ts +1 -1
- package/lib/transporters/websocket/WebSocket.Transporter.js +1 -1
- package/lib/transporters/websocket/WebSocket.decl.d.ts +1 -1
- package/lib/transporters/websocket/WebSocket.decl.js +1 -1
- package/lib/transporters/websocket/index.d.ts +1 -1
- package/lib/transporters/websocket/index.js +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Client.d.ts +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Client.js +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Constants.d.ts +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Constants.js +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Errors.d.ts +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Errors.d.ts.map +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Errors.js +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Server.d.ts +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Server.js +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Transporter.d.ts +1 -1
- package/lib/transporters/worker-thread/WorkerThread.Transporter.js +1 -1
- package/lib/transporters/worker-thread/index.d.ts +1 -1
- package/lib/transporters/worker-thread/index.js +1 -1
- package/package.json +5 -5
- package/src/lib/client/Client.Constants.ts +4 -0
- package/src/lib/client/Client.decl.ts +42 -2
- package/src/lib/client/JsonApiClient.impl.ts +59 -8
- package/src/lib/client/index.ts +1 -1
- package/src/lib/index.ts +1 -1
- package/src/lib/server/LegacyServerChannel.impl.ts +2 -2
- package/src/lib/server/Server.decl.ts +23 -2
- package/src/lib/server/Server.impl.ts +5 -4
- package/src/lib/server/ServerChannel.ts +1 -1
- package/src/lib/server/SimpleJsonApiRouter.ts +17 -4
- package/src/lib/server/index.ts +1 -1
- package/src/lib/shared/BinaryStream.ts +1 -1
- package/src/lib/shared/BinaryStreamManager.ts +1 -1
- package/src/lib/shared/Channel.impl.ts +43 -23
- package/src/lib/shared/Constants.ts +1 -1
- package/src/lib/shared/Encodings/index.ts +1 -1
- package/src/lib/shared/Encodings/v1/EncoderV1.ts +1 -1
- package/src/lib/shared/Encodings/v1/Protocol.v1.decls.ts +1 -1
- package/src/lib/shared/Encodings/v1/index.ts +1 -1
- package/src/lib/shared/Encodings/v2/Constants.v2.ts +20 -1
- package/src/lib/shared/Encodings/v2/DecoderV2.ts +137 -15
- package/src/lib/shared/Encodings/v2/EncoderV2.ts +160 -36
- package/src/lib/shared/Encodings/v2/Encoding.v2.decl.ts +96 -44
- package/src/lib/shared/Encodings/v2/index.ts +1 -1
- package/src/lib/shared/Errors.ts +2 -1
- package/src/lib/shared/Shared.decl.ts +23 -18
- package/src/lib/shared/Utils.ts +3 -3
- package/src/lib/shared/index.ts +1 -1
- package/src/lib/transporters/Transporter.decl.ts +1 -1
- package/src/lib/transporters/http-listener/AbstractHttpListener.ts +14 -6
- package/src/lib/transporters/http-listener/Http.Decl.ts +1 -27
- package/src/lib/transporters/http-listener/HttpListener.ts +10 -2
- package/src/lib/transporters/http-listener/HttpUnixSocketListener.ts +10 -2
- package/src/lib/transporters/http-listener/HttpsListener.ts +12 -4
- package/src/lib/transporters/http-listener/index.ts +1 -1
- package/src/lib/transporters/index.ts +1 -1
- package/src/lib/transporters/legacy-http/LegacyHttp.Client.Native.ts +60 -10
- package/src/lib/transporters/legacy-http/LegacyHttp.Server.ts +58 -10
- package/src/lib/transporters/legacy-http/LegacyHttp.Transporter.ts +1 -1
- package/src/lib/transporters/legacy-http/index.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.Tcp.Client.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.Tcp.Server.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.Tls.Client.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.Tls.Server.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.Transporter.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.UnixSocket.Client.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.UnixSocket.Server.ts +1 -1
- package/src/lib/transporters/lwdfx/LwDFX.decl.ts +1 -1
- package/src/lib/transporters/lwdfx/index.ts +1 -1
- package/src/lib/transporters/memory/Memory.Impl.ts +4 -4
- package/src/lib/transporters/memory/index.ts +1 -1
- package/src/lib/transporters/websocket/WebSocket.Client.ts +52 -7
- package/src/lib/transporters/websocket/WebSocket.Server.ts +59 -12
- package/src/lib/transporters/websocket/WebSocket.Transporter.ts +1 -1
- package/src/lib/transporters/websocket/WebSocket.decl.ts +1 -1
- package/src/lib/transporters/websocket/index.ts +1 -1
- package/src/lib/transporters/worker-thread/WorkerThread.Client.ts +1 -1
- package/src/lib/transporters/worker-thread/WorkerThread.Constants.ts +1 -1
- package/src/lib/transporters/worker-thread/WorkerThread.Errors.ts +1 -1
- package/src/lib/transporters/worker-thread/WorkerThread.Server.ts +1 -1
- package/src/lib/transporters/worker-thread/WorkerThread.Transporter.ts +1 -1
- package/src/lib/transporters/worker-thread/index.ts +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2025 Angus.Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -21,15 +21,29 @@ import * as Shared from '../../shared';
|
|
|
21
21
|
import * as v1 from '../../shared/Encodings/v1';
|
|
22
22
|
import { EventEmitter } from 'node:events';
|
|
23
23
|
|
|
24
|
-
const
|
|
25
|
-
const
|
|
24
|
+
const H_HDR_NAME_CONTENT_LENGTH = 'content-length';
|
|
25
|
+
const H_HDR_NAME_CONTENT_TYPE = 'content-type';
|
|
26
|
+
const H_HDR_NAME_TV_VER = 'x-tv-ver';
|
|
26
27
|
|
|
27
28
|
const DEFAULT_MAX_CONNECTIONS = 100;
|
|
28
29
|
const DEFAULT_TIMEOUT = 30_000;
|
|
29
30
|
|
|
30
31
|
const disabledStreamManager = Shared.createDisabledStreamManagerFactory()(null as any);
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
export interface ILegacyHttpClient<T extends Shared.IObject> extends C.IClient<T> {
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Setup the headers for the every HTTP request.
|
|
37
|
+
*
|
|
38
|
+
* @param newHeaders The new headers to set.
|
|
39
|
+
* @param append Whether to append the new headers to existing headers, or replace all headers. [Default: true]
|
|
40
|
+
*/
|
|
41
|
+
setHeaders(newHeaders: $Http.OutgoingHttpHeaders, append?: boolean): void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const H_HDR_VAL_JSON_MIME = 'application/json';
|
|
45
|
+
|
|
46
|
+
class TvLegacyHttpClient extends EventEmitter implements ILegacyHttpClient<any> {
|
|
33
47
|
|
|
34
48
|
public onError!: any;
|
|
35
49
|
|
|
@@ -42,6 +56,8 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
|
|
|
42
56
|
) {
|
|
43
57
|
|
|
44
58
|
super();
|
|
59
|
+
|
|
60
|
+
this._opts.headers ??= {};
|
|
45
61
|
}
|
|
46
62
|
|
|
47
63
|
public get timeout(): number {
|
|
@@ -62,6 +78,18 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
|
|
|
62
78
|
|
|
63
79
|
public readonly transporter = null;
|
|
64
80
|
|
|
81
|
+
public setHeaders(newHeaders: $Http.OutgoingHttpHeaders, append: boolean = true): void {
|
|
82
|
+
|
|
83
|
+
if (append) {
|
|
84
|
+
|
|
85
|
+
Object.assign(this._opts.headers!, newHeaders);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
|
|
89
|
+
this._opts.headers = newHeaders;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
65
93
|
public sendBinaryChunk(): Promise<void> {
|
|
66
94
|
|
|
67
95
|
return Promise.reject(new Shared.errors.cmd_not_impl());
|
|
@@ -96,6 +124,25 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
|
|
|
96
124
|
}
|
|
97
125
|
}
|
|
98
126
|
|
|
127
|
+
public async callApi(api: any, args: any[]): Promise<any> {
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
|
|
131
|
+
return await this._invoke(api, ...args);
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
|
|
135
|
+
if (this._retryOnConnReset && (e as any)?.code === 'ECONNRESET') {
|
|
136
|
+
|
|
137
|
+
return this._invoke(api, ...args);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
|
|
141
|
+
throw e;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
99
146
|
private _invoke(api: any, ...args: any[]): Promise<any> {
|
|
100
147
|
|
|
101
148
|
const CST = Date.now();
|
|
@@ -124,8 +171,9 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
|
|
|
124
171
|
method: 'POST',
|
|
125
172
|
headers: {
|
|
126
173
|
...this._opts.headers,
|
|
127
|
-
[
|
|
128
|
-
[
|
|
174
|
+
[H_HDR_NAME_CONTENT_TYPE]: H_HDR_VAL_JSON_MIME,
|
|
175
|
+
[H_HDR_NAME_CONTENT_LENGTH]: Buffer.byteLength(content),
|
|
176
|
+
[H_HDR_NAME_TV_VER]: 1
|
|
129
177
|
},
|
|
130
178
|
}, (resp) => {
|
|
131
179
|
|
|
@@ -139,14 +187,14 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
|
|
|
139
187
|
return;
|
|
140
188
|
}
|
|
141
189
|
|
|
142
|
-
if (!resp.headers[
|
|
190
|
+
if (!resp.headers[H_HDR_NAME_CONTENT_LENGTH]) {
|
|
143
191
|
|
|
144
192
|
resp.socket.destroy();
|
|
145
193
|
reject(new Shared.errors.invalid_response({ reason: 'missing_content_length' }));
|
|
146
194
|
return;
|
|
147
195
|
}
|
|
148
196
|
|
|
149
|
-
const length = parseInt(resp.headers[
|
|
197
|
+
const length = parseInt(resp.headers[H_HDR_NAME_CONTENT_LENGTH]);
|
|
150
198
|
|
|
151
199
|
if (!Number.isSafeInteger(length) || length > v1.MAX_PACKET_SIZE) { // Maximum request packet is 64MB
|
|
152
200
|
|
|
@@ -183,7 +231,7 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
|
|
|
183
231
|
|
|
184
232
|
try {
|
|
185
233
|
|
|
186
|
-
data = JSON.parse(buf as
|
|
234
|
+
data = JSON.parse(buf as unknown as string);
|
|
187
235
|
}
|
|
188
236
|
catch (e) {
|
|
189
237
|
|
|
@@ -312,7 +360,9 @@ export interface IHttpClientUnixSocketOptions extends IHttpClientOptionsBase {
|
|
|
312
360
|
|
|
313
361
|
export type IHttpClientOptions = IHttpClientNetworkOptions | IHttpClientUnixSocketOptions;
|
|
314
362
|
|
|
315
|
-
export function createLegacyHttpClient<TAPIs extends Shared.IObject>(
|
|
363
|
+
export function createLegacyHttpClient<TAPIs extends Shared.IObject>(
|
|
364
|
+
opts: IHttpClientOptions,
|
|
365
|
+
): ILegacyHttpClient<TAPIs> {
|
|
316
366
|
|
|
317
367
|
return new TvLegacyHttpClient(
|
|
318
368
|
opts.https ? $Https.request : $Http.request,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2025 Angus.Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -57,10 +57,28 @@ export interface IHttpGatewayOptions {
|
|
|
57
57
|
backlog?: number;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
export interface IRegisterListenerOptions {
|
|
61
|
+
|
|
62
|
+
onErrorCallback: (e: Error) => void;
|
|
63
|
+
|
|
64
|
+
onRequestCallback: (req: Http.IncomingMessage, resp: Http.ServerResponse) => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface IRegisterListenerResult {
|
|
68
|
+
|
|
69
|
+
stop?: () => void | Promise<void>;
|
|
70
|
+
|
|
71
|
+
start?: () => void | Promise<void>;
|
|
72
|
+
|
|
73
|
+
readonly running: boolean;
|
|
74
|
+
}
|
|
75
|
+
|
|
60
76
|
class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
|
|
61
77
|
|
|
78
|
+
private readonly _listener: IRegisterListenerResult;
|
|
79
|
+
|
|
62
80
|
public constructor(
|
|
63
|
-
|
|
81
|
+
registerListener: (opts: IRegisterListenerOptions) => IRegisterListenerResult,
|
|
64
82
|
private readonly _server: dT.IServer,
|
|
65
83
|
) {
|
|
66
84
|
|
|
@@ -71,9 +89,10 @@ class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
|
|
|
71
89
|
throw new TypeError('Legacy HTTP gateway only supports JSON encoding');
|
|
72
90
|
}
|
|
73
91
|
|
|
74
|
-
this._listener
|
|
75
|
-
|
|
76
|
-
|
|
92
|
+
this._listener = registerListener({
|
|
93
|
+
onErrorCallback: (e) => this.emit('error', e),
|
|
94
|
+
onRequestCallback: this._onRequest,
|
|
95
|
+
});
|
|
77
96
|
}
|
|
78
97
|
|
|
79
98
|
private _sendResponse(resp: Http.ServerResponse, data: string): void {
|
|
@@ -157,7 +176,7 @@ class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
|
|
|
157
176
|
|
|
158
177
|
let input: any;
|
|
159
178
|
|
|
160
|
-
if (offset !== length
|
|
179
|
+
if (offset !== length) {
|
|
161
180
|
|
|
162
181
|
refuseBadRequest(resp);
|
|
163
182
|
return;
|
|
@@ -165,7 +184,7 @@ class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
|
|
|
165
184
|
|
|
166
185
|
try {
|
|
167
186
|
|
|
168
|
-
input = JSON.parse(buf as
|
|
187
|
+
input = JSON.parse(buf as unknown as string);
|
|
169
188
|
}
|
|
170
189
|
catch (e) {
|
|
171
190
|
|
|
@@ -269,7 +288,7 @@ class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
|
|
|
269
288
|
return;
|
|
270
289
|
}
|
|
271
290
|
|
|
272
|
-
await this._listener.start();
|
|
291
|
+
await this._listener.start?.();
|
|
273
292
|
}
|
|
274
293
|
|
|
275
294
|
public async stop(): Promise<void> {
|
|
@@ -279,14 +298,43 @@ class LegacyHttpGateway extends EventEmitter implements dT.IGateway {
|
|
|
279
298
|
return Promise.resolve();
|
|
280
299
|
}
|
|
281
300
|
|
|
282
|
-
await this._listener.stop();
|
|
301
|
+
await this._listener.stop?.();
|
|
283
302
|
}
|
|
284
303
|
}
|
|
285
304
|
|
|
305
|
+
/**
|
|
306
|
+
* Create a legacy HTTP gateway, binding to a built-in simple HTTP server.
|
|
307
|
+
*
|
|
308
|
+
* > When using built-in HTTP server, the api will ignore headers, path and query string in the URL.
|
|
309
|
+
*
|
|
310
|
+
* @param listener The built-in HTTP listener to bind to.
|
|
311
|
+
* @param server The server to process the requests.
|
|
312
|
+
*/
|
|
286
313
|
export function createLegacyHttpGateway(
|
|
287
314
|
listener: Listener.IHttpListener,
|
|
288
315
|
server: dT.IServer
|
|
289
316
|
): dT.IGateway {
|
|
290
317
|
|
|
291
|
-
return new LegacyHttpGateway(
|
|
318
|
+
return new LegacyHttpGateway((o) => {
|
|
319
|
+
listener.on('error', o.onErrorCallback);
|
|
320
|
+
listener.setLegacyApiProcessor(o.onRequestCallback);
|
|
321
|
+
return listener;
|
|
322
|
+
}, server);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Create a legacy HTTP gateway, binding to a custom HTTP server.
|
|
327
|
+
*
|
|
328
|
+
* > When using a custom HTTP server, it's able to preprocess the request before passing to the server, like
|
|
329
|
+
* > authentication, rate limiting, etc.
|
|
330
|
+
*
|
|
331
|
+
* @param registerListener The function to register the listener to the custom HTTP server.
|
|
332
|
+
* @param server The server to process the requests.
|
|
333
|
+
*/
|
|
334
|
+
export function createCustomLegacyHttpGateway(
|
|
335
|
+
registerListener: (opts: IRegisterListenerOptions) => IRegisterListenerResult,
|
|
336
|
+
server: dT.IServer
|
|
337
|
+
): dT.IGateway {
|
|
338
|
+
|
|
339
|
+
return new LegacyHttpGateway(registerListener, server);
|
|
292
340
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2025 Angus.Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -33,9 +33,9 @@ function createRandomName(): string {
|
|
|
33
33
|
|
|
34
34
|
interface IMemoryExchangeEvents extends Shared.IDefaultEvents {
|
|
35
35
|
|
|
36
|
-
['data_a'](data:
|
|
36
|
+
['data_a'](data: unknown): void;
|
|
37
37
|
|
|
38
|
-
['data_b'](data:
|
|
38
|
+
['data_b'](data: unknown): void;
|
|
39
39
|
|
|
40
40
|
['end_a'](): void;
|
|
41
41
|
|
|
@@ -57,7 +57,7 @@ class MemoryExchange extends EventEmitter implements Shared.IEventListener<IMemo
|
|
|
57
57
|
super();
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
public send(targetEndpoint: 'a' | 'b', data:
|
|
60
|
+
public send(targetEndpoint: 'a' | 'b', data: unknown): void {
|
|
61
61
|
|
|
62
62
|
setImmediate(() => this.emit('data_' + targetEndpoint, data));
|
|
63
63
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2025 Angus.Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -15,13 +15,28 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import * as LibWS from '@litert/websocket';
|
|
18
|
+
import type * as NodeHttp from 'node:http';
|
|
18
19
|
import * as dWS from './WebSocket.decl';
|
|
19
20
|
import type * as dT from '../Transporter.decl';
|
|
20
21
|
import { WebSocketTransporter } from './WebSocket.Transporter';
|
|
21
22
|
|
|
22
|
-
class WsConnector implements
|
|
23
|
+
class WsConnector implements IWsConnector {
|
|
23
24
|
|
|
24
|
-
public constructor(private readonly _opts: LibWS.IWsConnectOptions) {
|
|
25
|
+
public constructor(private readonly _opts: LibWS.IWsConnectOptions) {
|
|
26
|
+
this._opts.headers ??= {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public setHeaders(newHeaders: NodeHttp.OutgoingHttpHeaders, append: boolean = true): void {
|
|
30
|
+
|
|
31
|
+
if (append) {
|
|
32
|
+
|
|
33
|
+
Object.assign(this._opts.headers!, newHeaders);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
|
|
37
|
+
this._opts.headers = newHeaders;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
25
40
|
|
|
26
41
|
public async connect(): Promise<dT.ITransporter> {
|
|
27
42
|
|
|
@@ -41,9 +56,23 @@ class WsConnector implements dT.IConnector {
|
|
|
41
56
|
}
|
|
42
57
|
}
|
|
43
58
|
|
|
44
|
-
class WssConnector implements
|
|
59
|
+
class WssConnector implements IWsConnector {
|
|
60
|
+
|
|
61
|
+
public constructor(private readonly _opts: LibWS.IWssConnectOptions) {
|
|
62
|
+
this._opts.headers ??= {};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public setHeaders(newHeaders: NodeHttp.OutgoingHttpHeaders, append: boolean = true): void {
|
|
45
66
|
|
|
46
|
-
|
|
67
|
+
if (append) {
|
|
68
|
+
|
|
69
|
+
Object.assign(this._opts.headers!, newHeaders);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
|
|
73
|
+
this._opts.headers = newHeaders;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
47
76
|
|
|
48
77
|
public async connect(): Promise<dT.ITransporter> {
|
|
49
78
|
|
|
@@ -63,13 +92,29 @@ class WssConnector implements dT.IConnector {
|
|
|
63
92
|
}
|
|
64
93
|
}
|
|
65
94
|
|
|
66
|
-
export
|
|
95
|
+
export interface IWsConnector extends dT.IConnector {
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Setup the custom headers for websocket negotiation on each connection.
|
|
99
|
+
*
|
|
100
|
+
* **WARNING:**
|
|
101
|
+
* **In browser environment, the Websocket class cannot send custom headers.**
|
|
102
|
+
* **Considering the compatibility, custom headers in Websocket protocol are not recommended to use.**
|
|
103
|
+
* ***Don't use this feature unless you don't care about the compatibility in browser environment.***
|
|
104
|
+
*
|
|
105
|
+
* @param newHeaders The new headers to set.
|
|
106
|
+
* @param append Whether to append the new headers to existing headers, or replace all headers. [Default: true]
|
|
107
|
+
*/
|
|
108
|
+
setHeaders(newHeaders: NodeHttp.OutgoingHttpHeaders, append?: boolean): void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function createWsConnector(opts: LibWS.IWsConnectOptions): IWsConnector {
|
|
67
112
|
|
|
68
113
|
opts.frameReceiveMode = LibWS.EFrameReceiveMode.LITE;
|
|
69
114
|
return new WsConnector(opts);
|
|
70
115
|
}
|
|
71
116
|
|
|
72
|
-
export function createWssConnector(opts: LibWS.IWssConnectOptions):
|
|
117
|
+
export function createWssConnector(opts: LibWS.IWssConnectOptions): IWsConnector {
|
|
73
118
|
|
|
74
119
|
opts.frameReceiveMode = LibWS.EFrameReceiveMode.LITE;
|
|
75
120
|
return new WssConnector(opts);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright
|
|
2
|
+
* Copyright 2025 Angus.Fenying <fenying@litert.org>
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -15,27 +15,48 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import * as LibWS from '@litert/websocket';
|
|
18
|
+
import type * as NodeHttp from 'node:http';
|
|
19
|
+
import type * as NodeNet from 'node:net';
|
|
18
20
|
import * as dWS from './WebSocket.decl';
|
|
19
21
|
import type * as Listener from '../http-listener';
|
|
20
22
|
import type * as dT from '../Transporter.decl';
|
|
21
23
|
import { EventEmitter } from 'node:events';
|
|
22
24
|
import { WebSocketTransporter } from './WebSocket.Transporter';
|
|
23
25
|
|
|
26
|
+
export interface IRegisterListenerOptions {
|
|
27
|
+
|
|
28
|
+
onErrorCallback: (e: Error) => void;
|
|
29
|
+
|
|
30
|
+
onUpgradeCallback: (req: NodeHttp.IncomingMessage, socket: NodeJS.Socket, head: Buffer) => void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface IRegisterListenerResult {
|
|
34
|
+
|
|
35
|
+
stop?: () => void | Promise<void>;
|
|
36
|
+
|
|
37
|
+
start?: () => void | Promise<void>;
|
|
38
|
+
|
|
39
|
+
readonly running: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
24
42
|
class WebSocketGateway extends EventEmitter implements dT.IGateway {
|
|
25
43
|
|
|
26
44
|
private readonly _wsServer: LibWS.IServer;
|
|
27
45
|
|
|
46
|
+
private readonly _listener: IRegisterListenerResult;
|
|
47
|
+
|
|
28
48
|
public constructor(
|
|
29
|
-
|
|
49
|
+
registerListener: (opts: IRegisterListenerOptions) => IRegisterListenerResult,
|
|
30
50
|
private readonly _server: dT.IServer,
|
|
31
51
|
timeout?: number,
|
|
32
52
|
) {
|
|
33
53
|
|
|
34
54
|
super();
|
|
35
55
|
|
|
36
|
-
this._listener
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
this._listener = registerListener({
|
|
57
|
+
'onErrorCallback': (e) => this.emit('error', e),
|
|
58
|
+
'onUpgradeCallback': this._onUpgrade,
|
|
59
|
+
});
|
|
39
60
|
|
|
40
61
|
this._wsServer = LibWS.createServer({
|
|
41
62
|
frameReceiveMode: LibWS.EFrameReceiveMode.LITE,
|
|
@@ -51,7 +72,7 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
|
|
|
51
72
|
|
|
52
73
|
this._wsServer.reject({
|
|
53
74
|
'request': req,
|
|
54
|
-
'socket': socket as
|
|
75
|
+
'socket': socket as unknown as NodeNet.Socket,
|
|
55
76
|
});
|
|
56
77
|
return;
|
|
57
78
|
}
|
|
@@ -65,14 +86,14 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
|
|
|
65
86
|
|
|
66
87
|
this._wsServer.reject({
|
|
67
88
|
'request': req,
|
|
68
|
-
'socket': socket as
|
|
89
|
+
'socket': socket as unknown as NodeNet.Socket,
|
|
69
90
|
});
|
|
70
91
|
return;
|
|
71
92
|
}
|
|
72
93
|
|
|
73
94
|
const ws = this._wsServer.accept({
|
|
74
95
|
'request': req,
|
|
75
|
-
'socket': socket as
|
|
96
|
+
'socket': socket as unknown as NodeNet.Socket,
|
|
76
97
|
'subProtocol': dWS.WEBSOCKET_SUB_PROTOCOL,
|
|
77
98
|
});
|
|
78
99
|
|
|
@@ -91,7 +112,7 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
|
|
|
91
112
|
return;
|
|
92
113
|
}
|
|
93
114
|
|
|
94
|
-
await this._listener.start();
|
|
115
|
+
await this._listener.start?.();
|
|
95
116
|
}
|
|
96
117
|
|
|
97
118
|
public async stop(): Promise<void> {
|
|
@@ -101,7 +122,7 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
|
|
|
101
122
|
return;
|
|
102
123
|
}
|
|
103
124
|
|
|
104
|
-
await this._listener.stop();
|
|
125
|
+
await this._listener.stop?.();
|
|
105
126
|
}
|
|
106
127
|
}
|
|
107
128
|
|
|
@@ -118,7 +139,9 @@ export interface IWebSocketGatewayOptions {
|
|
|
118
139
|
}
|
|
119
140
|
|
|
120
141
|
/**
|
|
121
|
-
* Create a WebSocket gateway.
|
|
142
|
+
* Create a WebSocket gateway, binding to a simple built-in HTTP(S) listener.
|
|
143
|
+
*
|
|
144
|
+
* > When using built-in HTTP server, the api will ignore other headers, path and query string in the URL.
|
|
122
145
|
*
|
|
123
146
|
* @param listener The HTTP(S) listener object.
|
|
124
147
|
* @param server The televoke server object.
|
|
@@ -129,5 +152,29 @@ export function createWebsocketGateway(
|
|
|
129
152
|
options: IWebSocketGatewayOptions = {}
|
|
130
153
|
): dT.IGateway {
|
|
131
154
|
|
|
132
|
-
return new WebSocketGateway(
|
|
155
|
+
return new WebSocketGateway((o) => {
|
|
156
|
+
|
|
157
|
+
listener.on('error', o.onErrorCallback);
|
|
158
|
+
listener.setUpgradeProcessor(o.onUpgradeCallback);
|
|
159
|
+
|
|
160
|
+
return listener;
|
|
161
|
+
|
|
162
|
+
}, server, options.timeout);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Create a WebSocket gateway, binding to a custom HTTP server.
|
|
167
|
+
*
|
|
168
|
+
* > When using a custom HTTP server, it's able to preprocess the request before passing to the server, like
|
|
169
|
+
* > authentication, rate limiting, custom routing, etc.
|
|
170
|
+
*
|
|
171
|
+
* @param registerListener The function to register the listener to the custom HTTP server.
|
|
172
|
+
* @param server The server to process the requests.
|
|
173
|
+
*/
|
|
174
|
+
export function createCustomWebsocketGateway(
|
|
175
|
+
registerListener: (opts: IRegisterListenerOptions) => IRegisterListenerResult,
|
|
176
|
+
server: dT.IServer
|
|
177
|
+
): dT.IGateway {
|
|
178
|
+
|
|
179
|
+
return new WebSocketGateway(registerListener, server);
|
|
133
180
|
}
|