X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FWorkerBroadcastChannel.ts;h=f3c168bdc8f1057611d1b8768d6d1dde0364c627;hb=728e01f09f2b43946aab50eb2ed9673bf2d2daa3;hp=dba83cbb01fccdd367143906d53f8fd7a4d280bc;hpb=6c1761d470507ea23d186be61b94ca7375c5144a;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/WorkerBroadcastChannel.ts b/src/charging-station/WorkerBroadcastChannel.ts index dba83cbb..f3c168bd 100644 --- a/src/charging-station/WorkerBroadcastChannel.ts +++ b/src/charging-station/WorkerBroadcastChannel.ts @@ -1,13 +1,16 @@ -import { BroadcastChannel } from 'worker_threads'; +import { BroadcastChannel } from 'node:worker_threads'; -import BaseError from '../exception/BaseError'; import type { BroadcastChannelRequest, BroadcastChannelResponse, + JsonType, MessageEvent, -} from '../types/WorkerBroadcastChannel'; +} from '../types'; +import { Utils, logger } from '../utils'; -export default abstract class WorkerBroadcastChannel extends BroadcastChannel { +const moduleName = 'WorkerBroadcastChannel'; + +export abstract class WorkerBroadcastChannel extends BroadcastChannel { protected constructor() { super('worker'); } @@ -20,17 +23,37 @@ export default abstract class WorkerBroadcastChannel extends BroadcastChannel { this.postMessage(response); } - protected isRequest(message: any): boolean { - return Array.isArray(message) && message.length === 3; + protected isRequest(message: JsonType[]): boolean { + return Array.isArray(message) === true && message.length === 3; } - protected isResponse(message: any): boolean { - return Array.isArray(message) && message.length === 2; + protected isResponse(message: JsonType[]): boolean { + return Array.isArray(message) === true && message.length === 2; } - protected validateMessageEvent(messageEvent: MessageEvent): void { + protected validateMessageEvent(messageEvent: MessageEvent): MessageEvent | false { if (Array.isArray(messageEvent.data) === false) { - throw new BaseError('Worker broadcast channel protocol message event data is not an array'); + logger.error( + `${this.logPrefix( + moduleName, + 'validateMessageEvent' + )} Worker broadcast channel protocol message event data is not an array` + ); + return false; + } + if (Utils.validateUUID(messageEvent.data[0]) === false) { + logger.error( + `${this.logPrefix( + moduleName, + 'validateMessageEvent' + )} Worker broadcast channel protocol message event data UUID field is invalid` + ); + return false; } + return messageEvent; } + + private logPrefix = (modName: string, methodName: string): string => { + return Utils.logPrefix(` Worker Broadcast Channel | ${modName}.${methodName}:`); + }; }