UI Server: dedupe some code in helpers
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Aug 2022 10:09:23 +0000 (12:09 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Aug 2022 10:09:23 +0000 (12:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
rollup.config.mjs
src/charging-station/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/UIServiceWorkerBroadcastChannel.ts
src/charging-station/WorkerBroadcastChannel.ts
src/charging-station/ui-server/AbstractUIServer.ts
src/charging-station/ui-server/UIHttpServer.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts

index 2dc4f5c7de531aabb599f99bcb800ec778babd82..effe368d667737a6ec2e3e38e389e20c7958ad3e 100644 (file)
@@ -43,6 +43,7 @@ export default {
     'crypto',
     'fs',
     'http',
+    'http-status-codes',
     'mnemonist/lru-map-with-delete',
     'moment',
     'mongodb',
index 9de2cfb2784dc75965403734ccb97b327d940bed..42448275432337772fe80a691a5364840680f068 100644 (file)
@@ -38,9 +38,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca
     if (this.isResponse(messageEvent.data)) {
       return;
     }
-    if (Array.isArray(messageEvent.data) === false) {
-      throw new BaseError('Worker broadcast channel protocol request is not an array');
-    }
+    this.validateMessageEvent(messageEvent);
 
     const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest;
 
index a94f2b31d263fa086153984c61d36a8ddffb4aa0..3916f626ee6896a18c9e2898d7fb1da9c2e43456 100644 (file)
@@ -1,4 +1,3 @@
-import BaseError from '../exception/BaseError';
 import { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel';
 import logger from '../utils/Logger';
 import type AbstractUIService from './ui-server/ui-services/AbstractUIService';
@@ -20,9 +19,7 @@ export default class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChan
     if (this.isRequest(messageEvent.data)) {
       return;
     }
-    if (Array.isArray(messageEvent.data) === false) {
-      throw new BaseError('Worker broadcast channel protocol response is not an array');
-    }
+    this.validateMessageEvent(messageEvent);
     const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse;
 
     this.uiService.sendResponse(uuid, responsePayload);
index 12a877c12add623365c0c8c6c6068c62841000d2..ea89dc3dbf65f093f0a0bbfacdfdb32aab3c5f62 100644 (file)
@@ -1,6 +1,11 @@
 import { BroadcastChannel } from 'worker_threads';
 
-import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel';
+import BaseError from '../exception/BaseError';
+import {
+  BroadcastChannelRequest,
+  BroadcastChannelResponse,
+  MessageEvent,
+} from '../types/WorkerBroadcastChannel';
 
 export default abstract class WorkerBroadcastChannel extends BroadcastChannel {
   protected constructor() {
@@ -22,4 +27,10 @@ export default abstract class WorkerBroadcastChannel extends BroadcastChannel {
   protected isResponse(message: any): boolean {
     return Array.isArray(message) && message.length === 2;
   }
+
+  protected validateMessageEvent(messageEvent: MessageEvent): void {
+    if (Array.isArray(messageEvent.data) === false) {
+      throw new BaseError('Worker broadcast channel protocol message event data is not an array');
+    }
+  }
 }
index 65847d85d4f313a92880c99fc6bfcb2f98a3cfbf..b8e7c46cdf879b1e5d28d65c653085e90c40cf89 100644 (file)
@@ -3,7 +3,14 @@ import { Server as HttpServer } from 'http';
 import WebSocket from 'ws';
 
 import { ChargingStationData } from '../../types/ChargingStationWorker';
-import { ProtocolVersion } from '../../types/UIProtocol';
+import {
+  ProcedureName,
+  ProtocolRequest,
+  ProtocolResponse,
+  ProtocolVersion,
+  RequestPayload,
+  ResponsePayload,
+} from '../../types/UIProtocol';
 import type AbstractUIService from './ui-services/AbstractUIService';
 
 export abstract class AbstractUIServer {
@@ -16,6 +23,18 @@ export abstract class AbstractUIServer {
     this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
   }
 
+  public buildProtocolRequest(
+    id: string,
+    procedureName: ProcedureName,
+    requestPayload: RequestPayload
+  ): string {
+    return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest);
+  }
+
+  public buildProtocolResponse(id: string, responsePayload: ResponsePayload): string {
+    return JSON.stringify([id, responsePayload] as ProtocolResponse);
+  }
+
   public abstract start(): void;
   public abstract stop(): void;
   public abstract sendRequest(request: string): void;
index a9b620635d03df20db1898e6894fb94be00c8993..6fb8a6add2550be30898b0f43fab18eb39153d8a 100644 (file)
@@ -7,11 +7,9 @@ import { ServerOptions } from '../../types/ConfigurationData';
 import {
   ProcedureName,
   Protocol,
-  ProtocolRequest,
   ProtocolResponse,
   ProtocolVersion,
   RequestPayload,
-  ResponsePayload,
   ResponseStatus,
 } from '../../types/UIProtocol';
 import Configuration from '../../utils/Configuration';
@@ -101,9 +99,11 @@ export default class UIHttpServer extends AbstractUIServer {
             const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
             this.uiServices
               .get(version)
-              .requestHandler(this.buildRequest(uuid, procedureName, body ?? {}))
+              .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
               .catch(() => {
-                this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE }));
+                this.sendResponse(
+                  this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE })
+                );
               });
           });
       } else {
@@ -114,22 +114,10 @@ export default class UIHttpServer extends AbstractUIServer {
         `${this.logPrefix(moduleName, 'requestListener')} Handle HTTP request error:`,
         error
       );
-      this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE }));
+      this.sendResponse(this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE }));
     }
   }
 
-  private buildRequest(
-    id: string,
-    procedureName: ProcedureName,
-    requestPayload: RequestPayload
-  ): string {
-    return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest);
-  }
-
-  private buildResponse(id: string, responsePayload: ResponsePayload): string {
-    return JSON.stringify([id, responsePayload] as ProtocolResponse);
-  }
-
   private responseStatusToStatusCode(status: ResponseStatus): StatusCodes {
     switch (status) {
       case ResponseStatus.SUCCESS:
index 24307992872011f53053977131a93b0b4cb826ad..2c541cde9f993ebd1c2ea2ca66a8d551bed5a4a6 100644 (file)
@@ -7,7 +7,6 @@ import {
   ProcedureName,
   ProtocolRequest,
   ProtocolRequestHandler,
-  ProtocolResponse,
   ProtocolVersion,
   RequestPayload,
   ResponsePayload,
@@ -80,29 +79,19 @@ export default abstract class AbstractUIService {
     procedureName: ProcedureName,
     requestPayload: RequestPayload
   ): void {
-    this.uiServer.sendRequest(this.buildProtocolRequest(messageId, procedureName, requestPayload));
+    this.uiServer.sendRequest(
+      this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
+    );
   }
 
   public sendResponse(messageId: string, responsePayload: ResponsePayload): void {
-    this.uiServer.sendResponse(this.buildProtocolResponse(messageId, responsePayload));
+    this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload));
   }
 
   public logPrefix(modName: string, methodName: string): string {
     return this.uiServer.logPrefix(modName, methodName);
   }
 
-  private buildProtocolRequest(
-    messageId: string,
-    procedureName: ProcedureName,
-    requestPayload: RequestPayload
-  ): string {
-    return JSON.stringify([messageId, procedureName, requestPayload] as ProtocolRequest);
-  }
-
-  private buildProtocolResponse(messageId: string, responsePayload: ResponsePayload): string {
-    return JSON.stringify([messageId, responsePayload] as ProtocolResponse);
-  }
-
   // Validate the raw data received from the UI server
   private requestValidation(rawData: RawData | JsonType): ProtocolRequest {
     // logger.debug(