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'
10 type ProtocolResponse
,
11 type UIServerConfiguration
,
12 WebSocketCloseEventStatusCode
,
13 } from
'../../types/index.js'
16 getWebSocketCloseEventStatusString
,
22 } from
'../../utils/index.js'
23 import { AbstractUIServer
} from
'./AbstractUIServer.js'
25 getProtocolAndVersion
,
27 isProtocolAndVersionSupported
,
28 } from
'./UIServerUtils.js'
30 const moduleName
= 'UIWebSocketServer'
32 export class UIWebSocketServer
extends AbstractUIServer
{
33 private readonly webSocketServer
: WebSocketServer
35 public logPrefix
= (modName
?: string, methodName
?: string, prefixSuffix
?: string): string => {
37 prefixSuffix
!= null ? `UI WebSocket Server ${prefixSuffix}` : 'UI WebSocket Server'
39 isNotEmptyString(modName
) && isNotEmptyString(methodName
)
40 ? ` ${logMsgPrefix} | ${modName}.${methodName}:`
41 : ` ${logMsgPrefix} |`
42 return logPrefix(logMsg
)
45 public constructor (protected override
readonly uiServerConfiguration
: UIServerConfiguration
) {
46 super(uiServerConfiguration
)
47 this.webSocketServer
= new WebSocketServer({
53 private broadcastToClients (message
: string): void {
54 for (const client
of this.webSocketServer
.clients
) {
55 if (client
.readyState
=== WebSocket
.OPEN
) {
61 private validateRawDataRequest (rawData
: RawData
): false | ProtocolRequest
{
65 // 'validateRawDataRequest'
66 // // eslint-disable-next-line @typescript-eslint/no-base-to-string
67 // )} Raw data received in string format: ${rawData.toString()}`
70 let request
: ProtocolRequest
72 // eslint-disable-next-line @typescript-eslint/no-base-to-string
73 request
= JSON
.parse(rawData
.toString()) as ProtocolRequest
78 'validateRawDataRequest'
79 // eslint-disable-next-line @typescript-eslint/no-base-to-string
80 )} UI protocol request is not valid JSON: ${rawData.toString()}`
85 if (!Array.isArray(request
)) {
89 'validateRawDataRequest'
90 )} UI protocol request is not an array:`,
96 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
97 if (request
.length
!== 3) {
99 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
105 if (!validateUUID(request
[0])) {
109 'validateRawDataRequest'
110 )} UI protocol request UUID field is invalid:`,
119 public sendRequest (request
: ProtocolRequest
): void {
120 this.broadcastToClients(JSON
.stringify(request
))
123 public sendResponse (response
: ProtocolResponse
): void {
124 const responseId
= response
[0]
126 if (this.hasResponseHandler(responseId
)) {
127 const ws
= this.responseHandlers
.get(responseId
) as WebSocket
128 if (ws
.readyState
=== WebSocket
.OPEN
) {
129 ws
.send(JSONStringify(response
, undefined, MapStringifyFormat
.object
))
135 )} Error at sending response id '${responseId}', WebSocket is not open: ${ws.readyState.toString()}`
143 )} Response for unknown request id: ${responseId}`
151 )} Error at sending response id '${responseId}':`,
155 this.responseHandlers
.delete(responseId
)
159 public start (): void {
160 this.webSocketServer
.on('connection', (ws
: WebSocket
, _req
: IncomingMessage
): void => {
161 if (!isProtocolAndVersionSupported(ws
.protocol
)) {
165 'start.server.onconnection'
166 )} Unsupported UI protocol version: '${ws.protocol}'`
168 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_PROTOCOL_ERROR
)
170 const [, version
] = getProtocolAndVersion(ws
.protocol
)
171 this.registerProtocolVersionUIService(version
)
172 ws
.on('message', rawData
=> {
173 const request
= this.validateRawDataRequest(rawData
)
174 if (request
=== false) {
175 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_INVALID_PAYLOAD
)
178 const [requestId
] = request
179 this.responseHandlers
.set(requestId
, ws
)
182 ?.requestHandler(request
)
183 .then((protocolResponse
?: ProtocolResponse
) => {
184 if (protocolResponse
!= null) {
185 this.sendResponse(protocolResponse
)
189 .catch(Constants
.EMPTY_FUNCTION
)
191 ws
.on('error', error
=> {
192 logger
.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error
)
194 ws
.on('close', (code
, reason
) => {
199 )} WebSocket closed: '${getWebSocketCloseEventStatusString(
201 )}' - '${reason.toString()}'`
205 this.httpServer
.on('connect', (req
: IncomingMessage
, socket
: Duplex
, _head
: Buffer
) => {
206 if (req
.headers
.connection
!== 'Upgrade' || req
.headers
.upgrade
!== 'websocket') {
207 socket
.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST.toString()} Bad Request\r\n\r\n`)
211 this.httpServer
.on('upgrade', (req
: IncomingMessage
, socket
: Duplex
, head
: Buffer
): void => {
212 const onSocketError
= (error
: Error): void => {
216 'start.httpServer.on.upgrade'
217 )} Socket error at connection upgrade event handling:`,
221 socket
.on('error', onSocketError
)
222 this.authenticate(req
, err
=> {
224 socket
.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED.toString()} Unauthorized\r\n\r\n`)
229 this.webSocketServer
.handleUpgrade(req
, socket
, head
, (ws
: WebSocket
) => {
230 this.webSocketServer
.emit('connection', ws
, req
)
236 'start.httpServer.on.upgrade'
237 )} Error at connection upgrade event handling:`,
242 socket
.removeListener('error', onSocketError
)
244 this.startHttpServer()