build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
index 6d6dc6df9fcd6d66a77ec3455d1f0ef76787c08e..16477146f0c638fbd1776213df0d336e3fa9f62d 100644 (file)
@@ -14,6 +14,7 @@ import {
 } from '../../types/index.js'
 import {
   Constants,
+  JSONStringifyWithMapSupport,
   getWebSocketCloseEventStatusString,
   isNotEmptyString,
   logPrefix,
@@ -47,13 +48,13 @@ export class UIWebSocketServer extends AbstractUIServer {
       }
       const [, version] = UIServerUtils.getProtocolAndVersion(ws.protocol)
       this.registerProtocolVersionUIService(version)
-      ws.on('message', (rawData) => {
+      ws.on('message', rawData => {
         const request = this.validateRawDataRequest(rawData)
         if (request === false) {
           ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD)
           return
         }
-        const [requestId] = request as ProtocolRequest
+        const [requestId] = request
         this.responseHandlers.set(requestId, ws)
         this.uiServices
           .get(version)
@@ -65,7 +66,7 @@ export class UIWebSocketServer extends AbstractUIServer {
           })
           .catch(Constants.EMPTY_FUNCTION)
       })
-      ws.on('error', (error) => {
+      ws.on('error', error => {
         logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error)
       })
       ws.on('close', (code, reason) => {
@@ -86,7 +87,7 @@ export class UIWebSocketServer extends AbstractUIServer {
       }
     })
     this.httpServer.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer): void => {
-      this.authenticate(req, (err) => {
+      this.authenticate(req, err => {
         if (err != null) {
           socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`)
           socket.destroy()
@@ -120,7 +121,7 @@ export class UIWebSocketServer extends AbstractUIServer {
       if (this.hasResponseHandler(responseId)) {
         const ws = this.responseHandlers.get(responseId) as WebSocket
         if (ws.readyState === WebSocket.OPEN) {
-          ws.send(JSON.stringify(response))
+          ws.send(JSONStringifyWithMapSupport(response))
         } else {
           logger.error(
             `${this.logPrefix(
@@ -179,8 +180,20 @@ export class UIWebSocketServer extends AbstractUIServer {
     //   )} Raw data received in string format: ${rawData.toString()}`
     // )
 
-    // eslint-disable-next-line @typescript-eslint/no-base-to-string
-    const request = JSON.parse(rawData.toString()) as ProtocolRequest
+    let request: ProtocolRequest
+    try {
+      // eslint-disable-next-line @typescript-eslint/no-base-to-string
+      request = JSON.parse(rawData.toString()) as ProtocolRequest
+    } catch (error) {
+      logger.error(
+        `${this.logPrefix(
+          moduleName,
+          'validateRawDataRequest'
+          // eslint-disable-next-line @typescript-eslint/no-base-to-string
+        )} UI protocol request is not valid JSON: ${rawData.toString()}`
+      )
+      return false
+    }
 
     if (!Array.isArray(request)) {
       logger.error(