build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
CommitLineData
01f4001e
JB
1import type { IncomingMessage } from 'node:http';
2import type { Duplex } from 'node:stream';
8114d10e 3
eb3abc4f 4import { StatusCodes } from 'http-status-codes';
d972af76 5import { type RawData, WebSocket, WebSocketServer } from 'ws';
8114d10e 6
4c3c0d59
JB
7import { AbstractUIServer } from './AbstractUIServer';
8import { UIServerUtils } from './UIServerUtils';
268a74bb
JB
9import {
10 type ProtocolRequest,
11 type ProtocolResponse,
12 type UIServerConfiguration,
13 WebSocketCloseEventStatusCode,
14} from '../../types';
9bf0ef23
JB
15import {
16 Constants,
17 getWebSocketCloseEventStatusString,
18 isNotEmptyString,
19 isNullOrUndefined,
20 logPrefix,
21 logger,
22 validateUUID,
23} from '../../utils';
4198ad5c 24
32de5a57
LM
25const moduleName = 'UIWebSocketServer';
26
268a74bb 27export class UIWebSocketServer extends AbstractUIServer {
eb3abc4f
JB
28 private readonly webSocketServer: WebSocketServer;
29
30 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
31 super(uiServerConfiguration);
eb3abc4f 32 this.webSocketServer = new WebSocketServer({
ed3d2808 33 handleProtocols: UIServerUtils.handleProtocols,
eb3abc4f
JB
34 noServer: true,
35 });
4198ad5c
JB
36 }
37
38 public start(): void {
fd3c56d1 39 // eslint-disable-next-line @typescript-eslint/no-unused-vars
eb3abc4f 40 this.webSocketServer.on('connection', (ws: WebSocket, req: IncomingMessage): void => {
ed3d2808 41 if (UIServerUtils.isProtocolAndVersionSupported(ws.protocol) === false) {
a92929f1
JB
42 logger.error(
43 `${this.logPrefix(
44 moduleName,
5edd8ba0
JB
45 'start.server.onconnection',
46 )} Unsupported UI protocol version: '${ws.protocol}'`,
a92929f1 47 );
5e3cb728 48 ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
a92929f1 49 }
ed3d2808 50 const [, version] = UIServerUtils.getProtocolAndVersion(ws.protocol);
143498c8 51 this.registerProtocolVersionUIService(version);
72092cfc 52 ws.on('message', (rawData) => {
5dea4c94
JB
53 const request = this.validateRawDataRequest(rawData);
54 if (request === false) {
55 ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD);
56 return;
57 }
94dc3080
JB
58 const [requestId] = request as ProtocolRequest;
59 this.responseHandlers.set(requestId, ws);
0b22144c
JB
60 this.uiServices
61 .get(version)
62 ?.requestHandler(request)
63 .then((protocolResponse: ProtocolResponse) => {
9bf0ef23 64 if (!isNullOrUndefined(protocolResponse)) {
0b22144c
JB
65 this.sendResponse(protocolResponse);
66 }
67 })
68 .catch(Constants.EMPTY_FUNCTION);
4198ad5c 69 });
72092cfc 70 ws.on('error', (error) => {
5e3cb728
JB
71 logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error);
72 });
73 ws.on('close', (code, reason) => {
74 logger.debug(
75 `${this.logPrefix(
76 moduleName,
5edd8ba0 77 'start.ws.onclose',
9bf0ef23 78 )} WebSocket closed: '${getWebSocketCloseEventStatusString(
5edd8ba0
JB
79 code,
80 )}' - '${reason.toString()}'`,
32de5a57 81 );
4198ad5c
JB
82 });
83 });
cbf9b878 84 // eslint-disable-next-line @typescript-eslint/no-unused-vars
60a74391 85 this.httpServer.on('connect', (req: IncomingMessage, socket: Duplex, head: Buffer) => {
cbf9b878
JB
86 if (req.headers?.connection !== 'Upgrade' || req.headers?.upgrade !== 'websocket') {
87 socket.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST} Bad Request\r\n\r\n`);
88 socket.destroy();
89 }
90 });
60a74391
JB
91 this.httpServer.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer): void => {
92 this.authenticate(req, (err) => {
93 if (err) {
94 socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
95 socket.destroy();
96 return;
97 }
98 try {
99 this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => {
100 this.webSocketServer.emit('connection', ws, req);
101 });
102 } catch (error) {
103 logger.error(
104 `${this.logPrefix(
105 moduleName,
5edd8ba0 106 'start.httpServer.on.upgrade',
60a74391 107 )} Error at handling connection upgrade:`,
5edd8ba0 108 error,
60a74391
JB
109 );
110 }
111 });
112 });
a307349b 113 this.startHttpServer();
4198ad5c
JB
114 }
115
5e3cb728
JB
116 public sendRequest(request: ProtocolRequest): void {
117 this.broadcastToClients(JSON.stringify(request));
02a6943a
JB
118 }
119
5e3cb728 120 public sendResponse(response: ProtocolResponse): void {
94dc3080 121 const responseId = response[0];
976d11ec 122 try {
1ca4a038 123 if (this.hasResponseHandler(responseId)) {
976d11ec
JB
124 const ws = this.responseHandlers.get(responseId) as WebSocket;
125 if (ws?.readyState === WebSocket.OPEN) {
126 ws.send(JSON.stringify(response));
e2c77f10
JB
127 } else {
128 logger.error(
129 `${this.logPrefix(
130 moduleName,
5edd8ba0
JB
131 'sendResponse',
132 )} Error at sending response id '${responseId}', WebSocket is not open: ${ws?.readyState}`,
e2c77f10 133 );
976d11ec 134 }
976d11ec
JB
135 } else {
136 logger.error(
137 `${this.logPrefix(
138 moduleName,
5edd8ba0
JB
139 'sendResponse',
140 )} Response for unknown request id: ${responseId}`,
976d11ec 141 );
94dc3080 142 }
976d11ec 143 } catch (error) {
94dc3080
JB
144 logger.error(
145 `${this.logPrefix(
146 moduleName,
5edd8ba0 147 'sendResponse',
976d11ec 148 )} Error at sending response id '${responseId}':`,
5edd8ba0 149 error,
94dc3080 150 );
e2c77f10
JB
151 } finally {
152 this.responseHandlers.delete(responseId);
94dc3080 153 }
178ac666
JB
154 }
155
8b7072dc 156 public logPrefix = (modName?: string, methodName?: string, prefixSuffix?: string): string => {
0d2cec76
JB
157 const logMsgPrefix = prefixSuffix
158 ? `UI WebSocket Server ${prefixSuffix}`
159 : 'UI WebSocket Server';
32de5a57 160 const logMsg =
9bf0ef23 161 isNotEmptyString(modName) && isNotEmptyString(methodName)
1b271a54
JB
162 ? ` ${logMsgPrefix} | ${modName}.${methodName}:`
163 : ` ${logMsgPrefix} |`;
9bf0ef23 164 return logPrefix(logMsg);
8b7072dc 165 };
178ac666
JB
166
167 private broadcastToClients(message: string): void {
eb3abc4f 168 for (const client of this.webSocketServer.clients) {
0d8140bd 169 if (client?.readyState === WebSocket.OPEN) {
178ac666
JB
170 client.send(message);
171 }
172 }
173 }
5e3cb728 174
5dea4c94 175 private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
5e3cb728
JB
176 // logger.debug(
177 // `${this.logPrefix(
178 // moduleName,
e1d9a0f4
JB
179 // 'validateRawDataRequest',
180 // // eslint-disable-next-line @typescript-eslint/no-base-to-string
181 // )} Raw data received in string format: ${rawData.toString()}`,
5e3cb728
JB
182 // );
183
e1d9a0f4 184 // eslint-disable-next-line @typescript-eslint/no-base-to-string
5e3cb728
JB
185 const request = JSON.parse(rawData.toString()) as ProtocolRequest;
186
187 if (Array.isArray(request) === false) {
5dea4c94
JB
188 logger.error(
189 `${this.logPrefix(
190 moduleName,
5edd8ba0 191 'validateRawDataRequest',
5dea4c94 192 )} UI protocol request is not an array:`,
5edd8ba0 193 request,
5dea4c94
JB
194 );
195 return false;
5e3cb728
JB
196 }
197
198 if (request.length !== 3) {
5dea4c94
JB
199 logger.error(
200 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
5edd8ba0 201 request,
5dea4c94
JB
202 );
203 return false;
204 }
205
9bf0ef23 206 if (validateUUID(request[0]) === false) {
5dea4c94
JB
207 logger.error(
208 `${this.logPrefix(
209 moduleName,
5edd8ba0 210 'validateRawDataRequest',
5dea4c94 211 )} UI protocol request UUID field is invalid:`,
5edd8ba0 212 request,
5dea4c94
JB
213 );
214 return false;
5e3cb728
JB
215 }
216
217 return request;
218 }
4198ad5c 219}