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