@litert/televoke 1.0.0-1 → 1.0.0-3

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.
Files changed (36) hide show
  1. package/docs/Televoke_v2.md +378 -0
  2. package/lib/client/JsonApiClient.impl.js.map +1 -1
  3. package/lib/client/LegacyHttpClient.impl.d.ts +17 -16
  4. package/lib/client/LegacyHttpClient.impl.d.ts.map +1 -1
  5. package/lib/client/LegacyHttpClient.impl.js +24 -10
  6. package/lib/client/LegacyHttpClient.impl.js.map +1 -1
  7. package/lib/server/SimpleJsonApiRouter.js.map +1 -1
  8. package/lib/shared/BinaryStream.js.map +1 -1
  9. package/lib/shared/BinaryStreamManager.js.map +1 -1
  10. package/lib/shared/Channel.impl.js.map +1 -1
  11. package/lib/shared/Encodings/v1/EncoderV1.js.map +1 -1
  12. package/lib/shared/Encodings/v2/DecoderV2.js +1 -1
  13. package/lib/shared/Encodings/v2/DecoderV2.js.map +1 -1
  14. package/lib/shared/Encodings/v2/EncoderV2.js +3 -3
  15. package/lib/shared/Encodings/v2/EncoderV2.js.map +1 -1
  16. package/lib/shared/Utils.js.map +1 -1
  17. package/lib/transporters/http-listener/HttpListener.js.map +1 -1
  18. package/lib/transporters/http-listener/HttpUnixSocketListener.js.map +1 -1
  19. package/lib/transporters/http-listener/HttpsListener.js.map +1 -1
  20. package/lib/transporters/legacy-http/LegacyHttp.Server.js.map +1 -1
  21. package/lib/transporters/legacy-http/LegacyHttp.Transporter.js.map +1 -1
  22. package/lib/transporters/lwdfx/LwDFX.Tcp.Server.js.map +1 -1
  23. package/lib/transporters/lwdfx/LwDFX.Tls.Server.js.map +1 -1
  24. package/lib/transporters/lwdfx/LwDFX.Transporter.js.map +1 -1
  25. package/lib/transporters/lwdfx/LwDFX.UnixSocket.Server.js.map +1 -1
  26. package/lib/transporters/memory/Memory.Impl.js.map +1 -1
  27. package/lib/transporters/websocket/WebSocket.Server.d.ts +11 -1
  28. package/lib/transporters/websocket/WebSocket.Server.d.ts.map +1 -1
  29. package/lib/transporters/websocket/WebSocket.Server.js +4 -3
  30. package/lib/transporters/websocket/WebSocket.Server.js.map +1 -1
  31. package/lib/transporters/websocket/WebSocket.Transporter.js.map +1 -1
  32. package/package.json +7 -7
  33. package/src/lib/client/LegacyHttpClient.impl.ts +50 -30
  34. package/src/lib/shared/Encodings/v2/DecoderV2.ts +1 -1
  35. package/src/lib/shared/Encodings/v2/EncoderV2.ts +3 -3
  36. package/src/lib/transporters/websocket/WebSocket.Server.ts +16 -1
@@ -36,7 +36,8 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
36
36
  private readonly _request: (opts: any, cb: (resp: $Http.IncomingMessage) => void) => $Http.ClientRequest,
37
37
  private readonly _opts: $Https.RequestOptions,
38
38
  private readonly _agent: $Http.Agent,
39
- private readonly _apiNameWrapper?: (name: string) => string
39
+ private readonly _retryOnConnReset: boolean = true,
40
+ private readonly _apiNameWrapper?: (name: string) => string,
40
41
  ) {
41
42
 
42
43
  super();
@@ -75,7 +76,26 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
75
76
  return Promise.reject(new Shared.errors.cmd_not_impl());
76
77
  }
77
78
 
78
- public invoke(api: any, ...args: any[]): Promise<any> {
79
+ public async invoke(api: any, ...args: any[]): Promise<any> {
80
+
81
+ try {
82
+
83
+ return await this._invoke(api, ...args);
84
+ }
85
+ catch (e) {
86
+
87
+ if (this._retryOnConnReset && (e as any)?.code === 'ECONNRESET') {
88
+
89
+ return this._invoke(api, ...args);
90
+ }
91
+ else {
92
+
93
+ throw e;
94
+ }
95
+ }
96
+ }
97
+
98
+ private _invoke(api: any, ...args: any[]): Promise<any> {
79
99
 
80
100
  const CST = Date.now();
81
101
 
@@ -84,7 +104,7 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
84
104
  rid: 0,
85
105
  cst: CST,
86
106
  args,
87
- api: this._apiNameWrapper ? this._apiNameWrapper(api) : api
107
+ api: this._apiNameWrapper?.(api) ?? api
88
108
  });
89
109
 
90
110
  return new Promise((resolve, reject) => {
@@ -207,7 +227,7 @@ class TvLegacyHttpClient extends EventEmitter implements C.IClient<any> {
207
227
  }
208
228
  }
209
229
 
210
- export interface IHttpClientNetworkOptions extends $Https.RequestOptions {
230
+ interface IHttpClientOptionsBase extends $Https.RequestOptions {
211
231
 
212
232
  /**
213
233
  * Whether to use HTTPS.
@@ -217,44 +237,43 @@ export interface IHttpClientNetworkOptions extends $Https.RequestOptions {
217
237
  https?: boolean;
218
238
 
219
239
  /**
220
- * The hostname of HTTP server.
240
+ * Wrap the API name before sending to server.
221
241
  *
222
- * @default 'localhost'
242
+ * @default null
223
243
  */
224
- hostname: string;
244
+ apiNameWrapper?: (name: string) => string;
225
245
 
226
246
  /**
227
- * The port of HTTP server.
247
+ * Whether to retry on connection reset.
228
248
  *
229
- * @default 80 or 443
249
+ * @default true
230
250
  */
231
- port?: number;
251
+ retryOnConnReset?: boolean;
252
+ }
253
+
254
+ export interface IHttpClientNetworkOptions extends IHttpClientOptionsBase {
232
255
 
233
256
  /**
234
- * The path to the RPC entry.
257
+ * The hostname of HTTP server.
235
258
  *
236
- * @default '/'
259
+ * @default 'localhost'
237
260
  */
238
- path?: string;
239
-
240
- apiNameWrapper?: (name: string) => string;
241
- }
242
-
243
- export interface IHttpClientUnixSocketOptions extends $Https.RequestOptions {
261
+ hostname: string;
244
262
 
245
263
  /**
246
- * Whether to use HTTPS.
264
+ * The port of HTTP server.
247
265
  *
248
- * > Always `false` for Unix domain socket.
266
+ * @default 80 or 443
249
267
  */
250
- https?: false;
268
+ port?: number;
269
+ }
270
+
271
+ export interface IHttpClientUnixSocketOptions extends IHttpClientOptionsBase {
251
272
 
252
273
  /**
253
274
  * The path of Unix domain socket to connect to.
254
275
  */
255
276
  socketPath: string;
256
-
257
- apiNameWrapper?: (name: string) => string;
258
277
  }
259
278
 
260
279
  export type IHttpClientOptions = IHttpClientNetworkOptions | IHttpClientUnixSocketOptions;
@@ -265,14 +284,15 @@ export function createLegacyHttpClient<TAPIs extends Shared.IObject>(opts: IHttp
265
284
  opts.https ? $Https.request : $Http.request,
266
285
  opts,
267
286
  opts.https ? new $Https.Agent({
268
- maxSockets: 0,
269
- keepAlive: true,
270
- keepAliveMsecs: DEFAULT_TIMEOUT
287
+ 'maxSockets': 0,
288
+ 'keepAlive': true,
289
+ 'keepAliveMsecs': DEFAULT_TIMEOUT
271
290
  }) : new $Http.Agent({
272
- maxSockets: 0,
273
- keepAlive: true,
274
- keepAliveMsecs: DEFAULT_TIMEOUT
291
+ 'maxSockets': 0,
292
+ 'keepAlive': true,
293
+ 'keepAliveMsecs': DEFAULT_TIMEOUT
275
294
  }),
276
- opts.apiNameWrapper
295
+ opts.retryOnConnReset,
296
+ opts.apiNameWrapper,
277
297
  );
278
298
  }
@@ -234,7 +234,7 @@ class TvPushMessageRequestDecoder implements IPacketDecoder {
234
234
  'cmd': CSv2.ECommand.PUSH_MESSAGE,
235
235
  'typ': CSv2.EPacketType.REQUEST,
236
236
  'seq': ctx.seq,
237
- 'ct': readVarBuffer16(ctx)
237
+ 'ct': readVarBuffer(ctx)
238
238
  } satisfies dEnc2.IPushMessageRequestPacket;
239
239
  }
240
240
  }
@@ -174,7 +174,7 @@ class PushMessageRequestEncoder implements IPacketEncoder {
174
174
 
175
175
  public encode(packet: dEnc2.IPushMessageRequestPacket): v2.IDataChunkArray {
176
176
 
177
- const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 2);
177
+ const leadSeg = Buffer.allocUnsafe(CS.HEADER_SIZE + 4);
178
178
 
179
179
  writePacketHeader(leadSeg, packet.cmd, packet.typ, packet.seq);
180
180
 
@@ -187,12 +187,12 @@ class PushMessageRequestEncoder implements IPacketEncoder {
187
187
  bodyLen += Buffer.byteLength(p);
188
188
  }
189
189
 
190
- leadSeg.writeUInt16LE(bodyLen, CS.HEADER_SIZE);
190
+ leadSeg.writeUInt32LE(bodyLen, CS.HEADER_SIZE);
191
191
 
192
192
  return [leadSeg, ...packet.ct];
193
193
  }
194
194
 
195
- leadSeg.writeUInt16LE(Buffer.byteLength(packet.ct), CS.HEADER_SIZE);
195
+ leadSeg.writeUInt32LE(Buffer.byteLength(packet.ct), CS.HEADER_SIZE);
196
196
 
197
197
  return [leadSeg, packet.ct];
198
198
  }
@@ -28,6 +28,7 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
28
28
  public constructor(
29
29
  private readonly _listener: Listener.IHttpListener,
30
30
  private readonly _server: dT.IServer,
31
+ timeout?: number,
31
32
  ) {
32
33
 
33
34
  super();
@@ -38,6 +39,7 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
38
39
 
39
40
  this._wsServer = LibWS.createServer({
40
41
  liteFrameMode: true,
42
+ timeout,
41
43
  });
42
44
  }
43
45
 
@@ -103,6 +105,18 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
103
105
  }
104
106
  }
105
107
 
108
+ export interface IWebSocketGatewayOptions {
109
+
110
+ /**
111
+ * The timeout of WebSocket connections.
112
+ *
113
+ * > Set to 0 to disable timeout.
114
+ *
115
+ * @default 60000
116
+ */
117
+ timeout?: number;
118
+ }
119
+
106
120
  /**
107
121
  * Create a WebSocket gateway.
108
122
  *
@@ -112,7 +126,8 @@ class WebSocketGateway extends EventEmitter implements dT.IGateway {
112
126
  export function createWebsocketGateway(
113
127
  listener: Listener.IHttpListener,
114
128
  server: dT.IServer,
129
+ options: IWebSocketGatewayOptions = {}
115
130
  ): dT.IGateway {
116
131
 
117
- return new WebSocketGateway(listener, server);
132
+ return new WebSocketGateway(listener, server, options.timeout);
118
133
  }