From: Jérôme Benoit Date: Thu, 30 Nov 2023 18:24:39 +0000 (+0100) Subject: fix: handle invalid hashIds in UI server payload X-Git-Tag: v1.2.29~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3a6ef20ab4c64999fb15ba8df7422bc15c595200;p=e-mobility-charging-stations-simulator.git fix: handle invalid hashIds in UI server payload Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index d0478b99..e0078c19 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -140,7 +140,7 @@ export abstract class AbstractUIService { } public getBroadcastChannelExpectedResponses(uuid: string): number { - return this.broadcastChannelRequests.get(uuid) ?? 0; + return this.broadcastChannelRequests.get(uuid)!; } protected handleProtocolRequest( @@ -166,21 +166,25 @@ export abstract class AbstractUIService { if (hashId !== undefined && this.uiServer.chargingStations.has(hashId) === true) { return hashId; } - const msg = `${this.logPrefix( - moduleName, - 'sendBroadcastChannelRequest', - )} Charging station with hashId '${hashId}' not found`; - if (payload.hashIds?.length === 1) { - throw new BaseError(msg); - } else { - logger.warn(msg); - } + logger.warn( + `${this.logPrefix( + moduleName, + 'sendBroadcastChannelRequest', + )} Charging station with hashId '${hashId}' not found`, + ); }) ?.filter((hashId) => !isNullOrUndefined(hashId)) as string[]; + } else { + delete payload.hashIds; } - const expectedNumberOfResponses = isNotEmptyArray(payload.hashIds) - ? payload.hashIds!.length + const expectedNumberOfResponses = Array.isArray(payload.hashIds) + ? payload.hashIds.length : this.uiServer.chargingStations.size; + if (expectedNumberOfResponses === 0) { + throw new BaseError( + 'hashIds array in the request payload does not contain any valid charging station hashId', + ); + } this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]); this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses); }