1 import type { IncomingMessage
} from
'node:http';
2 import type { Duplex
} from
'node:stream';
4 import { StatusCodes
} from
'http-status-codes';
5 import { type RawData
, WebSocket
, WebSocketServer
} from
'ws';
7 import { AbstractUIServer
} from
'./AbstractUIServer';
8 import { UIServerUtils
} from
'./UIServerUtils';
11 type ProtocolResponse
,
12 type UIServerConfiguration
,
13 WebSocketCloseEventStatusCode
,
17 getWebSocketCloseEventStatusString
,
25 const moduleName
= 'UIWebSocketServer';
27 export class UIWebSocketServer
extends AbstractUIServer
{
28 private readonly webSocketServer
: WebSocketServer
;
30 public constructor(protected readonly uiServerConfiguration
: UIServerConfiguration
) {
31 super(uiServerConfiguration
);
32 this.webSocketServer
= new WebSocketServer({
33 handleProtocols
: UIServerUtils
.handleProtocols
,
38 public start(): void {
39 // eslint-disable-next-line @typescript-eslint/no-unused-vars
40 this.webSocketServer
.on('connection', (ws
: WebSocket
, req
: IncomingMessage
): void => {
41 if (UIServerUtils
.isProtocolAndVersionSupported(ws
.protocol
) === false) {
45 'start.server.onconnection',
46 )} Unsupported UI protocol version: '${ws.protocol}'`,
48 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_PROTOCOL_ERROR
);
50 const [, version
] = UIServerUtils
.getProtocolAndVersion(ws
.protocol
);
51 this.registerProtocolVersionUIService(version
);
52 ws
.on('message', (rawData
) => {
53 const request
= this.validateRawDataRequest(rawData
);
54 if (request
=== false) {
55 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_INVALID_PAYLOAD
);
58 const [requestId
] = request
as ProtocolRequest
;
59 this.responseHandlers
.set(requestId
, ws
);
62 ?.requestHandler(request
)
63 .then((protocolResponse
?: ProtocolResponse
) => {
64 if (!isNullOrUndefined(protocolResponse
)) {
65 this.sendResponse(protocolResponse
!);
68 .catch(Constants
.EMPTY_FUNCTION
);
70 ws
.on('error', (error
) => {
71 logger
.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error
);
73 ws
.on('close', (code
, reason
) => {
78 )} WebSocket closed: '${getWebSocketCloseEventStatusString(
80 )}' - '${reason.toString()}'`,
84 // eslint-disable-next-line @typescript-eslint/no-unused-vars
85 this.httpServer
.on('connect', (req
: IncomingMessage
, socket
: Duplex
, head
: Buffer
) => {
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`);
91 this.httpServer
.on('upgrade', (req
: IncomingMessage
, socket
: Duplex
, head
: Buffer
): void => {
92 this.authenticate(req
, (err
) => {
94 socket
.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
99 this.webSocketServer
.handleUpgrade(req
, socket
, head
, (ws
: WebSocket
) => {
100 this.webSocketServer
.emit('connection', ws
, req
);
106 'start.httpServer.on.upgrade',
107 )} Error at handling connection upgrade:`,
113 this.startHttpServer();
116 public sendRequest(request
: ProtocolRequest
): void {
117 this.broadcastToClients(JSON
.stringify(request
));
120 public sendResponse(response
: ProtocolResponse
): void {
121 const responseId
= response
[0];
123 if (this.hasResponseHandler(responseId
)) {
124 const ws
= this.responseHandlers
.get(responseId
) as WebSocket
;
125 if (ws
?.readyState
=== WebSocket
.OPEN
) {
126 ws
.send(JSON
.stringify(response
));
132 )} Error at sending response id '${responseId}', WebSocket is not open: ${ws?.readyState}`,
140 )} Response for unknown request id: ${responseId}`,
148 )} Error at sending response id '${responseId}':`,
152 this.responseHandlers
.delete(responseId
);
156 public logPrefix
= (modName
?: string, methodName
?: string, prefixSuffix
?: string): string => {
157 const logMsgPrefix
= prefixSuffix
158 ? `UI WebSocket Server ${prefixSuffix}`
159 : 'UI WebSocket Server';
161 isNotEmptyString(modName
) && isNotEmptyString(methodName
)
162 ? ` ${logMsgPrefix} | ${modName}.${methodName}:`
163 : ` ${logMsgPrefix} |`;
164 return logPrefix(logMsg
);
167 private broadcastToClients(message
: string): void {
168 for (const client
of this.webSocketServer
.clients
) {
169 if (client
?.readyState
=== WebSocket
.OPEN
) {
170 client
.send(message
);
175 private validateRawDataRequest(rawData
: RawData
): ProtocolRequest
| false {
177 // `${this.logPrefix(
179 // 'validateRawDataRequest',
180 // // eslint-disable-next-line @typescript-eslint/no-base-to-string
181 // )} Raw data received in string format: ${rawData.toString()}`,
184 // eslint-disable-next-line @typescript-eslint/no-base-to-string
185 const request
= JSON
.parse(rawData
.toString()) as ProtocolRequest
;
187 if (Array.isArray(request
) === false) {
191 'validateRawDataRequest',
192 )} UI protocol request is not an array:`,
198 if (request
.length
!== 3) {
200 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
206 if (validateUUID(request
[0]) === false) {
210 'validateRawDataRequest',
211 )} UI protocol request UUID field is invalid:`,