+ responsePayload = {
+ status: ResponseStatus.FAILURE,
+ command,
+ requestPayload,
+ responsePayload,
+ errorMessage: (error as Error).message,
+ errorStack: (error as Error).stack,
+ };
+ }
+
+ if (responsePayload !== undefined) {
+ // Send the response
+ this.uiServer.sendResponse(this.buildProtocolResponse(messageId ?? 'error', responsePayload));
+ }
+ }
+
+ public sendRequest(
+ messageId: string,
+ procedureName: ProcedureName,
+ requestPayload: RequestPayload
+ ): void {
+ this.uiServer.sendRequest(this.buildProtocolRequest(messageId, procedureName, requestPayload));
+ }
+
+ public sendResponse(messageId: string, responsePayload: ResponsePayload): void {
+ this.uiServer.sendResponse(this.buildProtocolResponse(messageId, responsePayload));
+ }
+
+ public logPrefix(modName: string, methodName: string): string {
+ return this.uiServer.logPrefix(modName, methodName);
+ }
+
+ private buildProtocolRequest(
+ messageId: string,
+ procedureName: ProcedureName,
+ requestPayload: RequestPayload
+ ): string {
+ return JSON.stringify([messageId, procedureName, requestPayload] as ProtocolRequest);
+ }
+
+ private buildProtocolResponse(messageId: string, responsePayload: ResponsePayload): string {
+ return JSON.stringify([messageId, responsePayload] as ProtocolResponse);
+ }
+
+ // Validate the raw data received from the WebSocket
+ // TODO: should probably be moved to the ws verify clients callback
+ private requestValidation(rawData: RawData): ProtocolRequest {
+ // logger.debug(
+ // `${this.uiServer.logPrefix(
+ // moduleName,
+ // 'dataValidation'
+ // )} Raw data received: ${rawData.toString()}`
+ // );
+
+ const data = JSON.parse(rawData.toString()) as JsonType[];
+
+ if (Utils.isIterable(data) === false) {
+ throw new BaseError('UI protocol request is not iterable');