1 import type { IncomingMessage
} from
'http';
2 import type internal from
'stream';
4 import { StatusCodes
} from
'http-status-codes';
5 import WebSocket
, { type RawData
, WebSocketServer
} from
'ws';
7 import type { UIServerConfiguration
} from
'../../types/ConfigurationData';
8 import type { ProtocolRequest
, ProtocolResponse
} from
'../../types/UIProtocol';
9 import { WebSocketCloseEventStatusCode
} from
'../../types/WebSocket';
10 import logger from
'../../utils/Logger';
11 import Utils from
'../../utils/Utils';
12 import { AbstractUIServer
} from
'./AbstractUIServer';
13 import { UIServerUtils
} from
'./UIServerUtils';
15 const moduleName
= 'UIWebSocketServer';
17 export default class UIWebSocketServer
extends AbstractUIServer
{
18 private readonly webSocketServer
: WebSocketServer
;
20 public constructor(protected readonly uiServerConfiguration
: UIServerConfiguration
) {
21 super(uiServerConfiguration
);
22 this.webSocketServer
= new WebSocketServer({
23 handleProtocols
: UIServerUtils
.handleProtocols
,
28 public start(): void {
29 // eslint-disable-next-line @typescript-eslint/no-unused-vars
30 this.webSocketServer
.on('connection', (ws
: WebSocket
, req
: IncomingMessage
): void => {
31 if (UIServerUtils
.isProtocolAndVersionSupported(ws
.protocol
) === false) {
35 'start.server.onconnection'
36 )} Unsupported UI protocol version: '${ws.protocol}'`
38 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_PROTOCOL_ERROR
);
40 const [, version
] = UIServerUtils
.getProtocolAndVersion(ws
.protocol
);
41 this.registerProtocolVersionUIService(version
);
42 ws
.on('message', (rawData
) => {
43 const request
= this.validateRawDataRequest(rawData
);
44 if (request
=== false) {
45 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_INVALID_PAYLOAD
);
48 const [requestId
] = request
as ProtocolRequest
;
49 this.responseHandlers
.set(requestId
, ws
);
52 .requestHandler(request
)
54 /* Error caught by AbstractUIService */
57 ws
.on('error', (error
) => {
58 logger
.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error
);
60 ws
.on('close', (code
, reason
) => {
65 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
67 )}' - '${reason.toString()}'`
73 (req
: IncomingMessage
, socket
: internal
.Duplex
, head
: Buffer
): void => {
74 this.authenticate(req
, (err
) => {
76 socket
.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
80 this.webSocketServer
.handleUpgrade(req
, socket
, head
, (ws
: WebSocket
) => {
81 this.webSocketServer
.emit('connection', ws
, req
);
86 this.startHttpServer();
89 public sendRequest(request
: ProtocolRequest
): void {
90 this.broadcastToClients(JSON
.stringify(request
));
93 public sendResponse(response
: ProtocolResponse
): void {
94 const responseId
= response
[0];
96 if (this.responseHandlers
.has(responseId
)) {
97 const ws
= this.responseHandlers
.get(responseId
) as WebSocket
;
98 if (ws
?.readyState
=== WebSocket
.OPEN
) {
99 ws
.send(JSON
.stringify(response
));
105 )} Error at sending response id '${responseId}', WebSocket is not open: ${
115 )} Response for unknown request id: ${responseId}`
123 )} Error at sending response id '${responseId}':`,
127 this.responseHandlers
.delete(responseId
);
131 public logPrefix(modName
?: string, methodName
?: string, prefixSuffix
?: string): string {
132 const logMsgPrefix
= prefixSuffix
133 ? `UI WebSocket Server ${prefixSuffix}`
134 : 'UI WebSocket Server';
136 modName
&& methodName
? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
137 return Utils
.logPrefix(logMsg
);
140 private broadcastToClients(message
: string): void {
141 for (const client
of this.webSocketServer
.clients
) {
142 if (client
?.readyState
=== WebSocket
.OPEN
) {
143 client
.send(message
);
148 private validateRawDataRequest(rawData
: RawData
): ProtocolRequest
| false {
150 // `${this.logPrefix(
152 // 'validateRawDataRequest'
153 // )} Raw data received in string format: ${rawData.toString()}`
156 const request
= JSON
.parse(rawData
.toString()) as ProtocolRequest
;
158 if (Array.isArray(request
) === false) {
162 'validateRawDataRequest'
163 )} UI protocol request is not an array:`,
169 if (request
.length
!== 3) {
171 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
177 if (Utils
.validateUUID(request
[0]) === false) {
181 'validateRawDataRequest'
182 )} UI protocol request UUID field is invalid:`,