From 291b5ec8b20005de53c4de3eba0e4cd1b068006c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 7 Nov 2023 20:10:37 +0100 Subject: [PATCH] refactor(simulator): cleanup JSON type usage in OCPP payload validation code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 5 ++--- .../ocpp/1.6/OCPP16RequestService.ts | 4 ++-- .../ocpp/1.6/OCPP16ResponseService.ts | 7 +++--- .../ocpp/2.0/OCPP20IncomingRequestService.ts | 5 ++--- .../ocpp/2.0/OCPP20RequestService.ts | 4 ++-- .../ocpp/2.0/OCPP20ResponseService.ts | 7 +++--- .../ocpp/OCPPIncomingRequestService.ts | 9 ++++---- .../ocpp/OCPPRequestService.ts | 22 +++++++++---------- .../ocpp/OCPPResponseService.ts | 20 ++++++----------- 9 files changed, 36 insertions(+), 47 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 4b565eb9..1f1366fa 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -35,7 +35,6 @@ import { type GetDiagnosticsRequest, type GetDiagnosticsResponse, type IncomingRequestHandler, - type JsonObject, type JsonType, OCPP16AuthorizationStatus, OCPP16AvailabilityType, @@ -109,7 +108,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService'; const moduleName = 'OCPP16IncomingRequestService'; export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { - protected jsonSchemas: Map>; + protected jsonSchemas: Map>; private incomingRequestHandlers: Map; public constructor() { @@ -187,7 +186,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { this.handleRequestCancelReservation.bind(this) as unknown as IncomingRequestHandler, ], ]); - this.jsonSchemas = new Map>([ + this.jsonSchemas = new Map>([ [ OCPP16IncomingRequestCommand.RESET, OCPP16ServiceUtils.parseJsonSchemaFile( diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 5c1c129f..d731071d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -32,14 +32,14 @@ import type { OCPPResponseService } from '../OCPPResponseService'; const moduleName = 'OCPP16RequestService'; export class OCPP16RequestService extends OCPPRequestService { - protected jsonSchemas: Map>; + protected jsonSchemas: Map>; public constructor(ocppResponseService: OCPPResponseService) { // if (new.target?.name === moduleName) { // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); // } super(OCPPVersion.VERSION_16, ocppResponseService); - this.jsonSchemas = new Map>([ + this.jsonSchemas = new Map>([ [ OCPP16RequestCommand.AUTHORIZE, OCPP16ServiceUtils.parseJsonSchemaFile( diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index b634562b..d2e19375 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -21,7 +21,6 @@ import { type GenericResponse, type GetConfigurationResponse, type GetDiagnosticsResponse, - type JsonObject, type JsonType, OCPP16AuthorizationStatus, type OCPP16AuthorizeRequest, @@ -68,11 +67,11 @@ const moduleName = 'OCPP16ResponseService'; export class OCPP16ResponseService extends OCPPResponseService { public jsonIncomingRequestResponseSchemas: Map< OCPP16IncomingRequestCommand, - JSONSchemaType + JSONSchemaType >; private responseHandlers: Map; - private jsonSchemas: Map>; + private jsonSchemas: Map>; public constructor() { // if (new.target?.name === moduleName) { @@ -109,7 +108,7 @@ export class OCPP16ResponseService extends OCPPResponseService { this.emptyResponseHandler.bind(this) as ResponseHandler, ], ]); - this.jsonSchemas = new Map>([ + this.jsonSchemas = new Map>([ [ OCPP16RequestCommand.BOOT_NOTIFICATION, OCPP16ServiceUtils.parseJsonSchemaFile( diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index b5790b56..6344d36d 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -8,7 +8,6 @@ import { OCPPError } from '../../../exception'; import { ErrorType, type IncomingRequestHandler, - type JsonObject, type JsonType, type OCPP20ClearCacheRequest, OCPP20IncomingRequestCommand, @@ -20,7 +19,7 @@ import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService'; const moduleName = 'OCPP20IncomingRequestService'; export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { - protected jsonSchemas: Map>; + protected jsonSchemas: Map>; private incomingRequestHandlers: Map; public constructor() { @@ -31,7 +30,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { this.incomingRequestHandlers = new Map([ [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)], ]); - this.jsonSchemas = new Map>([ + this.jsonSchemas = new Map>([ [ OCPP20IncomingRequestCommand.CLEAR_CACHE, OCPP20ServiceUtils.parseJsonSchemaFile( diff --git a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts index e7584236..f9f5c268 100644 --- a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts @@ -24,14 +24,14 @@ import type { OCPPResponseService } from '../OCPPResponseService'; const moduleName = 'OCPP20RequestService'; export class OCPP20RequestService extends OCPPRequestService { - protected jsonSchemas: Map>; + protected jsonSchemas: Map>; public constructor(ocppResponseService: OCPPResponseService) { // if (new.target?.name === moduleName) { // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); // } super(OCPPVersion.VERSION_20, ocppResponseService); - this.jsonSchemas = new Map>([ + this.jsonSchemas = new Map>([ [ OCPP20RequestCommand.BOOT_NOTIFICATION, OCPP20ServiceUtils.parseJsonSchemaFile( diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index cc4370e8..111b114a 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -7,7 +7,6 @@ import { type ChargingStation, addConfigurationKey } from '../../../charging-sta import { OCPPError } from '../../../exception'; import { ErrorType, - type JsonObject, type JsonType, type OCPP20BootNotificationResponse, type OCPP20ClearCacheResponse, @@ -28,11 +27,11 @@ const moduleName = 'OCPP20ResponseService'; export class OCPP20ResponseService extends OCPPResponseService { public jsonIncomingRequestResponseSchemas: Map< OCPP20IncomingRequestCommand, - JSONSchemaType + JSONSchemaType >; private responseHandlers: Map; - private jsonSchemas: Map>; + private jsonSchemas: Map>; public constructor() { // if (new.target?.name === moduleName) { @@ -50,7 +49,7 @@ export class OCPP20ResponseService extends OCPPResponseService { this.emptyResponseHandler.bind(this) as ResponseHandler, ], ]); - this.jsonSchemas = new Map>([ + this.jsonSchemas = new Map>([ [ OCPP20RequestCommand.BOOT_NOTIFICATION, OCPP20ServiceUtils.parseJsonSchemaFile( diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 0c5b5c61..0ccf92fe 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -11,7 +11,6 @@ import type { ClearCacheResponse, HandleErrorParams, IncomingRequestCommand, - JsonObject, JsonType, OCPPVersion, } from '../../types'; @@ -23,8 +22,8 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { private static instance: OCPPIncomingRequestService | null = null; private readonly version: OCPPVersion; private readonly ajv: Ajv; - private jsonValidateFunctions: Map>; - protected abstract jsonSchemas: Map>; + private jsonValidateFunctions: Map>; + protected abstract jsonSchemas: Map>; protected constructor(version: OCPPVersion) { super(moduleName); @@ -34,7 +33,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.jsonValidateFunctions = new Map>(); + this.jsonValidateFunctions = new Map>(); this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < ReqType extends JsonType, // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -94,7 +93,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { return true; } if (this.jsonValidateFunctions.has(commandName) === false) { - this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); + this.jsonValidateFunctions.set(commandName, this.ajv.compile(schema).bind(this)); } const validate = this.jsonValidateFunctions.get(commandName)!; if (validate(payload)) { diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 52a8a6e5..b95e9d23 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -44,8 +44,8 @@ export abstract class OCPPRequestService { private readonly version: OCPPVersion; private readonly ajv: Ajv; private readonly ocppResponseService: OCPPResponseService; - private readonly jsonValidateFunctions: Map>; - protected abstract jsonSchemas: Map>; + private readonly jsonValidateFunctions: Map>; + protected abstract jsonSchemas: Map>; protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) { this.version = version; @@ -54,7 +54,7 @@ export abstract class OCPPRequestService { multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.jsonValidateFunctions = new Map>(); + this.jsonValidateFunctions = new Map>(); this.ocppResponseService = ocppResponseService; this.requestHandler = this.requestHandler.bind(this) as < // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -102,14 +102,14 @@ export abstract class OCPPRequestService { responseCallback: ResponseCallback, errorCallback: ErrorCallback, ) => string; - this.validateRequestPayload = this.validateRequestPayload.bind(this) as ( + this.validateRequestPayload = this.validateRequestPayload.bind(this) as ( chargingStation: ChargingStation, commandName: RequestCommand | IncomingRequestCommand, payload: T, ) => boolean; this.validateIncomingRequestResponsePayload = this.validateIncomingRequestResponsePayload.bind( this, - ) as ( + ) as ( chargingStation: ChargingStation, commandName: RequestCommand | IncomingRequestCommand, payload: T, @@ -198,7 +198,7 @@ export abstract class OCPPRequestService { } } - private validateRequestPayload( + private validateRequestPayload( chargingStation: ChargingStation, commandName: RequestCommand | IncomingRequestCommand, payload: T, @@ -215,7 +215,7 @@ export abstract class OCPPRequestService { if (this.jsonValidateFunctions.has(commandName as RequestCommand) === false) { this.jsonValidateFunctions.set( commandName as RequestCommand, - this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand)!).bind(this), + this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand)!).bind(this), ); } const validate = this.jsonValidateFunctions.get(commandName as RequestCommand)!; @@ -237,7 +237,7 @@ export abstract class OCPPRequestService { ); } - private validateIncomingRequestResponsePayload( + private validateIncomingRequestResponsePayload( chargingStation: ChargingStation, commandName: RequestCommand | IncomingRequestCommand, payload: T, @@ -263,7 +263,7 @@ export abstract class OCPPRequestService { this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set( commandName as IncomingRequestCommand, this.ajv - .compile( + .compile( this.ocppResponseService.jsonIncomingRequestResponseSchemas.get( commandName as IncomingRequestCommand, )!, @@ -475,7 +475,7 @@ export abstract class OCPPRequestService { // Request case MessageType.CALL_MESSAGE: // Build request - this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonObject); + this.validateRequestPayload(chargingStation, commandName, messagePayload as JsonType); chargingStation.requests.set(messageId, [ responseCallback, errorCallback, @@ -495,7 +495,7 @@ export abstract class OCPPRequestService { this.validateIncomingRequestResponsePayload( chargingStation, commandName, - messagePayload as JsonObject, + messagePayload as JsonType, ); messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response); break; diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 46d12fb0..90f92169 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -4,13 +4,7 @@ import ajvFormats from 'ajv-formats'; import { OCPPServiceUtils } from './OCPPServiceUtils'; import type { ChargingStation } from '../../charging-station'; import { OCPPError } from '../../exception'; -import type { - IncomingRequestCommand, - JsonObject, - JsonType, - OCPPVersion, - RequestCommand, -} from '../../types'; +import type { IncomingRequestCommand, JsonType, OCPPVersion, RequestCommand } from '../../types'; import { logger } from '../../utils'; const moduleName = 'OCPPResponseService'; @@ -20,16 +14,16 @@ export abstract class OCPPResponseService { public jsonIncomingRequestResponseValidateFunctions: Map< IncomingRequestCommand, - ValidateFunction + ValidateFunction >; private readonly version: OCPPVersion; private readonly ajv: Ajv; - private jsonRequestValidateFunctions: Map>; + private jsonRequestValidateFunctions: Map>; public abstract jsonIncomingRequestResponseSchemas: Map< IncomingRequestCommand, - JSONSchemaType + JSONSchemaType >; protected constructor(version: OCPPVersion) { @@ -39,10 +33,10 @@ export abstract class OCPPResponseService { multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.jsonRequestValidateFunctions = new Map>(); + this.jsonRequestValidateFunctions = new Map>(); this.jsonIncomingRequestResponseValidateFunctions = new Map< IncomingRequestCommand, - ValidateFunction + ValidateFunction >(); this.responseHandler = this.responseHandler.bind(this) as < ReqType extends JsonType, @@ -80,7 +74,7 @@ export abstract class OCPPResponseService { if (this.jsonRequestValidateFunctions.has(commandName) === false) { this.jsonRequestValidateFunctions.set( commandName, - this.ajv.compile(schema).bind(this), + this.ajv.compile(schema).bind(this), ); } const validate = this.jsonRequestValidateFunctions.get(commandName)!; -- 2.34.1