import { AsyncResource } from 'node:async_hooks';
-import Ajv, { type JSONSchemaType } from 'ajv';
+import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
import ajvFormats from 'ajv-formats';
import { OCPPConstants } from './OCPPConstants';
private static instance: OCPPIncomingRequestService | null = null;
private readonly version: OCPPVersion;
private readonly ajv: Ajv;
+ private jsonValidateFunctions: Map<IncomingRequestCommand, ValidateFunction<JsonObject>>;
protected abstract jsonSchemas: Map<IncomingRequestCommand, JSONSchemaType<JsonObject>>;
protected constructor(version: OCPPVersion) {
multipleOfPrecision: 2,
});
ajvFormats(this.ajv);
+ this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonObject>>();
this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as <
ReqType extends JsonType,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
if (chargingStation.getOcppStrictCompliance() === false) {
return true;
}
- const validate = this.ajv.compile(schema);
+ if (this.jsonValidateFunctions.has(commandName) === false) {
+ this.jsonValidateFunctions.set(commandName, this.ajv.compile<JsonObject>(schema).bind(this));
+ }
+ const validate = this.jsonValidateFunctions.get(commandName)!;
if (validate(payload)) {
return true;
}
-import Ajv, { type JSONSchemaType } from 'ajv';
+import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
import ajvFormats from 'ajv-formats';
import { OCPPConstants } from './OCPPConstants';
private readonly version: OCPPVersion;
private readonly ajv: Ajv;
private readonly ocppResponseService: OCPPResponseService;
+ private readonly jsonValidateFunctions: Map<RequestCommand, ValidateFunction<JsonObject>>;
protected abstract jsonSchemas: Map<RequestCommand, JSONSchemaType<JsonObject>>;
protected constructor(version: OCPPVersion, ocppResponseService: OCPPResponseService) {
multipleOfPrecision: 2,
});
ajvFormats(this.ajv);
+ this.jsonValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonObject>>();
this.ocppResponseService = ocppResponseService;
this.requestHandler = this.requestHandler.bind(this) as <
// eslint-disable-next-line @typescript-eslint/no-unused-vars
);
return true;
}
- const validate = this.ajv.compile(this.jsonSchemas.get(commandName as RequestCommand)!);
+ 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),
+ );
+ }
+ const validate = this.jsonValidateFunctions.get(commandName as RequestCommand)!;
payload = cloneObject<T>(payload);
OCPPServiceUtils.convertDateToISOString<T>(payload);
if (validate(payload)) {
);
return true;
}
- const validate = this.ajv.compile(
- this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(
+ if (
+ this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.has(
commandName as IncomingRequestCommand,
- )!,
- );
+ ) === false
+ ) {
+ this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set(
+ commandName as IncomingRequestCommand,
+ this.ajv
+ .compile(
+ this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(
+ commandName as IncomingRequestCommand,
+ )!,
+ )
+ .bind(this),
+ );
+ }
+ const validate = this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.get(
+ commandName as IncomingRequestCommand,
+ )!;
payload = cloneObject<T>(payload);
OCPPServiceUtils.convertDateToISOString<T>(payload);
if (validate(payload)) {
-import Ajv, { type JSONSchemaType } from 'ajv';
+import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
import ajvFormats from 'ajv-formats';
import { OCPPServiceUtils } from './OCPPServiceUtils';
export abstract class OCPPResponseService {
private static instance: OCPPResponseService | null = null;
+
+ public jsonIncomingRequestResponseValidateFunctions: Map<
+ IncomingRequestCommand,
+ ValidateFunction<JsonObject>
+ >;
+
private readonly version: OCPPVersion;
private readonly ajv: Ajv;
+ private jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonObject>>;
+
public abstract jsonIncomingRequestResponseSchemas: Map<
IncomingRequestCommand,
JSONSchemaType<JsonObject>
multipleOfPrecision: 2,
});
ajvFormats(this.ajv);
+ this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonObject>>();
+ this.jsonIncomingRequestResponseValidateFunctions = new Map<
+ IncomingRequestCommand,
+ ValidateFunction<JsonObject>
+ >();
this.responseHandler = this.responseHandler.bind(this) as <
ReqType extends JsonType,
ResType extends JsonType,
if (chargingStation.getOcppStrictCompliance() === false) {
return true;
}
- const validate = this.ajv.compile(schema);
+ if (this.jsonRequestValidateFunctions.has(commandName) === false) {
+ this.jsonRequestValidateFunctions.set(
+ commandName,
+ this.ajv.compile<JsonObject>(schema).bind(this),
+ );
+ }
+ const validate = this.jsonRequestValidateFunctions.get(commandName)!;
if (validate(payload)) {
return true;
}