import { StatusCodes } from 'http-status-codes';
import * as uuid from 'uuid';
-import WebSocket, { RawData, WebSocketServer } from 'ws';
+import WebSocket, { type RawData, WebSocketServer } from 'ws';
import type { UIServerConfiguration } from '../../types/ConfigurationData';
import type { ProtocolRequest, ProtocolResponse } from '../../types/UIProtocol';
public sendResponse(response: ProtocolResponse): void {
const responseId = response[0];
- if (this.responseHandlers.has(responseId)) {
- const ws = this.responseHandlers.get(responseId) as WebSocket;
- if (ws?.readyState === WebSocket.OPEN) {
- ws.send(JSON.stringify(response));
+ try {
+ if (this.responseHandlers.has(responseId)) {
+ const ws = this.responseHandlers.get(responseId) as WebSocket;
+ if (ws?.readyState === WebSocket.OPEN) {
+ ws.send(JSON.stringify(response));
+ }
+ this.responseHandlers.delete(responseId);
+ } else {
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'sendResponse'
+ )} Response for unknown request id: ${responseId}`
+ );
}
- this.responseHandlers.delete(responseId);
- } else {
+ } catch (error) {
logger.error(
`${this.logPrefix(
moduleName,
'sendResponse'
- )} Response for unknown request id: ${responseId}`
+ )} Error at sending response id '${responseId}':`,
+ error
);
}
}
}
}
- private authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
- if (this.isBasicAuthEnabled() === true) {
- if (this.isValidBasicAuth(req) === false) {
- next(new Error('Unauthorized'));
- } else {
- next();
- }
- } else {
- next();
- }
- }
-
private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
// logger.debug(
// `${this.logPrefix(