import { StatusCodes } from 'http-status-codes'
import { type RawData, WebSocket, WebSocketServer } from 'ws'
-import { AbstractUIServer } from './AbstractUIServer.js'
-import {
- getProtocolAndVersion,
- handleProtocols,
- isProtocolAndVersionSupported
-} from './UIServerUtils.js'
import {
+ MapStringifyFormat,
type ProtocolRequest,
type ProtocolResponse,
type UIServerConfiguration,
} from '../../types/index.js'
import {
Constants,
- JSONStringifyWithMapSupport,
getWebSocketCloseEventStatusString,
isNotEmptyString,
- logPrefix,
+ JSONStringify,
logger,
+ logPrefix,
validateUUID
} from '../../utils/index.js'
+import { AbstractUIServer } from './AbstractUIServer.js'
+import {
+ getProtocolAndVersion,
+ handleProtocols,
+ isProtocolAndVersionSupported
+} from './UIServerUtils.js'
const moduleName = 'UIWebSocketServer'
}
})
this.httpServer.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer): void => {
+ const onSocketError = (error: Error): void => {
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'start.httpServer.on.upgrade'
+ )} Socket error at connection upgrade event handling:`,
+ error
+ )
+ }
+ socket.on('error', onSocketError)
this.authenticate(req, err => {
if (err != null) {
socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`)
`${this.logPrefix(
moduleName,
'start.httpServer.on.upgrade'
- )} Error at handling connection upgrade:`,
+ )} Error at connection upgrade event handling:`,
error
)
}
})
+ socket.removeListener('error', onSocketError)
})
this.startHttpServer()
}
if (this.hasResponseHandler(responseId)) {
const ws = this.responseHandlers.get(responseId) as WebSocket
if (ws.readyState === WebSocket.OPEN) {
- ws.send(JSONStringifyWithMapSupport(response))
+ ws.send(JSONStringify(response, undefined, MapStringifyFormat.object))
} else {
logger.error(
`${this.logPrefix(