From: Jérôme Benoit Date: Sat, 18 Mar 2023 16:45:29 +0000 (+0100) Subject: fix: brown paper bag issue at referencing the same literal object instance X-Git-Tag: v1.1.96~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=abe9e9dd3d72acc384fa2d3acd434f0053d8c61e;p=e-mobility-charging-stations-simulator.git fix: brown paper bag issue at referencing the same literal object instance Signed-off-by: Jérôme Benoit --- diff --git a/docker/start.sh b/docker/start.sh index caf30846..fa3b0b95 100755 --- a/docker/start.sh +++ b/docker/start.sh @@ -1,4 +1,4 @@ #!/usr/bin/env sh -node -r source-map-support/register dist/start.cjs & +node -r source-map-support/register dist/start.mjs & node webui/start.js diff --git a/manifest-cf-template.yml b/manifest-cf-template.yml index e1420eb8..a7d07501 100644 --- a/manifest-cf-template.yml +++ b/manifest-cf-template.yml @@ -9,7 +9,7 @@ applications: routes: - route: e-mobility-charging-stations-simulator.cfapps.sap.hana.ondemand.com health-check-type: process - command: node -r source-map-support/register dist/start.cjs + command: node -r source-map-support/register dist/start.mjs env: # OPTIMIZE_MEMORY: true NODE_OPTIONS: --stack-trace-limit=1024 --max-old-space-size=768 diff --git a/package.json b/package.json index 57f12e9d..6bcc3372 100644 --- a/package.json +++ b/package.json @@ -42,17 +42,17 @@ "scripts": { "prepare": "node prepare.js", "build-requirements": "node build-requirements.js", - "start": "npm run build && cross-env NODE_ENV=production node -r source-map-support/register dist/start.cjs", - "start:esm": "npm run build && cross-env NODE_ENV=production node -r source-map-support/register dist/start.mjs", - "start:dev": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.cjs", - "start:dev:debug": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.cjs", - "start:dev:esm": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.mjs", - "start:dev:esm:debug": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.mjs", + "start": "npm run build && cross-env NODE_ENV=production node -r source-map-support/register dist/start.mjs", + "start:cjs": "npm run build && cross-env NODE_ENV=production node -r source-map-support/register dist/start.cjs", + "start:dev": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.mjs", + "start:dev:debug": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.mjs", + "start:dev:cjs": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register dist/start.cjs", + "start:dev:cjs:debug": "npm run build:dev && cross-env NODE_ENV=development node -r source-map-support/register --inspect dist/start.cjs", "start:prof": "cross-env NODE_ENV=production node -r source-map-support/register --prof dist/start.cjs", - "start:doctorprof": "cross-env NODE_ENV=production clinic doctor -- node -r source-map-support/register dist/start.cjs", - "start:flameprof": "cross-env NODE_ENV=production clinic flame -- node -r source-map-support/register dist/start.cjs", - "start:bubbleprof": "cross-env NODE_ENV=production clinic bubbleprof -- node -r source-map-support/register dist/start.cjs", - "start:heapprofiler": "cross-env NODE_ENV=production clinic heapprofiler -- node -r source-map-support/register dist/start.cjs", + "start:doctorprof": "cross-env NODE_ENV=production clinic doctor -- node -r source-map-support/register dist/start.mjs", + "start:flameprof": "cross-env NODE_ENV=production clinic flame -- node -r source-map-support/register dist/start.mjs", + "start:bubbleprof": "cross-env NODE_ENV=production clinic bubbleprof -- node -r source-map-support/register dist/start.mjs", + "start:heapprofiler": "cross-env NODE_ENV=production clinic heapprofiler -- node -r source-map-support/register dist/start.mjs", "rollup": "npm run build-requirements && rollup --config", "build": "npm run rollup", "build:dev": "npm run rollup -- --environment BUILD:development", diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 9125c377..c24383e9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -74,7 +74,6 @@ import { RegistrationStatusEnumType, RequestCommand, type Response, - type ResponseCallback, StandardParametersKey, type StatusNotificationRequest, type StatusNotificationResponse, @@ -638,7 +637,7 @@ export class ChargingStation { } public openWSConnection( - options: WsOptions = this.stationInfo?.wsOptions ?? Constants.EMPTY_OBJECT, + options: WsOptions = this.stationInfo?.wsOptions ?? {}, params: { closeOpened?: boolean; terminateOpened?: boolean } = { closeOpened: false, terminateOpened: false, @@ -907,7 +906,7 @@ export class ChargingStation { }, reset: true, }, - stationTemplate?.firmwareUpgrade ?? Constants.EMPTY_OBJECT + stationTemplate?.firmwareUpgrade ?? {} ); stationInfo.resetTime = !Utils.isNullOrUndefined(stationTemplate?.resetTime) ? stationTemplate.resetTime * 1000 @@ -1373,7 +1372,7 @@ export class ChargingStation { fs.mkdirSync(path.dirname(this.configurationFile), { recursive: true }); } const configurationData: ChargingStationConfiguration = - Utils.cloneObject(this.getConfigurationFromFile()) ?? Constants.EMPTY_OBJECT; + Utils.cloneObject(this.getConfigurationFromFile()) ?? {}; this.ocppConfiguration?.configurationKey && (configurationData.configurationKey = this.ocppConfiguration.configurationKey); this.stationInfo && (configurationData.stationInfo = this.stationInfo); @@ -2052,7 +2051,7 @@ export class ChargingStation { ); this.openWSConnection( { - ...(this.stationInfo?.wsOptions ?? Constants.EMPTY_OBJECT), + ...(this.stationInfo?.wsOptions ?? {}), handshakeTimeout: reconnectTimeout, }, { closeOpened: true } diff --git a/src/charging-station/ChargingStationConfigurationUtils.ts b/src/charging-station/ChargingStationConfigurationUtils.ts index 5e14e458..f3f043cf 100644 --- a/src/charging-station/ChargingStationConfigurationUtils.ts +++ b/src/charging-station/ChargingStationConfigurationUtils.ts @@ -35,7 +35,7 @@ export class ChargingStationConfigurationUtils { }, params: AddConfigurationKeyParams = { overwrite: false, save: false } ): void { - options = options ?? (Constants.EMPTY_OBJECT as ConfigurationKeyOptions); + options = options ?? ({} as ConfigurationKeyOptions); options.readonly = options?.readonly ?? false; options.visible = options?.visible ?? true; options.reboot = options?.reboot ?? false; diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 957aee90..efa8921c 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -263,7 +263,7 @@ export class ChargingStationUtils { randomSerialNumber: true, } ): void { - params = params ?? Constants.EMPTY_OBJECT; + params = params ?? {}; params.randomSerialNumberUpperCase = params?.randomSerialNumberUpperCase ?? true; params.randomSerialNumber = params?.randomSerialNumber ?? true; const serialNumberSuffix = params?.randomSerialNumber diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 76bd56c2..18973792 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -353,7 +353,7 @@ export class OCPP16ResponseService extends OCPPResponseService { chargingStation, OCPP16StandardParametersKey.HeartbeatInterval, payload.interval.toString(), - Constants.EMPTY_OBJECT, + {}, { overwrite: true, save: true } ); ChargingStationConfigurationUtils.addConfigurationKey( diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index 907cee32..50f6b925 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -196,8 +196,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT ); - let powerPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = - Constants.EMPTY_OBJECT; + let powerPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = {}; if (chargingStation.getNumberOfPhases() === 3) { powerPerPhaseSampledValueTemplates = { L1: OCPP16ServiceUtils.getSampledValueTemplate( @@ -234,7 +233,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { powerSampledValueTemplate.measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER } measurand value`; - const powerMeasurandValues = Constants.EMPTY_OBJECT as MeasurandValues; + const powerMeasurandValues = {} as MeasurandValues; const unitDivider = powerSampledValueTemplate?.unit === MeterValueUnit.KILO_WATT ? 1000 : 1; const connectorMaximumAvailablePower = chargingStation.getConnectorMaximumAvailablePower(connectorId); @@ -404,8 +403,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT ); - let currentPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = - Constants.EMPTY_OBJECT; + let currentPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = {}; if (chargingStation.getNumberOfPhases() === 3) { currentPerPhaseSampledValueTemplates = { L1: OCPP16ServiceUtils.getSampledValueTemplate( @@ -442,7 +440,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { currentSampledValueTemplate.measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER } measurand value`; - const currentMeasurandValues: MeasurandValues = Constants.EMPTY_OBJECT as MeasurandValues; + const currentMeasurandValues: MeasurandValues = {} as MeasurandValues; const connectorMaximumAvailablePower = chargingStation.getConnectorMaximumAvailablePower(connectorId); let connectorMaximumAmperage: number; diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 129d8e52..971d5b83 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -161,7 +161,7 @@ export class OCPP20ResponseService extends OCPPResponseService { chargingStation, OCPP20OptionalVariableName.HeartbeatInterval, payload.interval.toString(), - Constants.EMPTY_OBJECT, + {}, { overwrite: true, save: true } ); chargingStation.heartbeatSetInterval diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 956b8d36..69829ff1 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -226,7 +226,7 @@ export class PerformanceStatistics { const entryName = entry.name; // Initialize command statistics if (!this.statistics.statisticsData.has(entryName)) { - this.statistics.statisticsData.set(entryName, Constants.EMPTY_OBJECT); + this.statistics.statisticsData.set(entryName, {}); } // Update current statistics this.statistics.updatedAt = new Date(); diff --git a/src/utils/Constants.ts b/src/utils/Constants.ts index 67b40146..c42e15be 100644 --- a/src/utils/Constants.ts +++ b/src/utils/Constants.ts @@ -47,7 +47,6 @@ export class Constants { static readonly MAX_RANDOM_INTEGER = 281474976710654; - static EMPTY_OBJECT = {}; static readonly EMPTY_FREEZED_OBJECT = Object.freeze({}); static readonly EMPTY_FUNCTION = Object.freeze(() => { /* This is intentional */ diff --git a/src/worker/WorkerAbstract.ts b/src/worker/WorkerAbstract.ts index 2803eea4..3dd9a782 100644 --- a/src/worker/WorkerAbstract.ts +++ b/src/worker/WorkerAbstract.ts @@ -23,7 +23,7 @@ export abstract class WorkerAbstract { poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, - poolOptions: WorkerConstants.EMPTY_OBJECT, + poolOptions: {}, messageHandler: WorkerConstants.EMPTY_FUNCTION, } ) { diff --git a/src/worker/WorkerConstants.ts b/src/worker/WorkerConstants.ts index 12bb70ac..72d436d4 100644 --- a/src/worker/WorkerConstants.ts +++ b/src/worker/WorkerConstants.ts @@ -1,5 +1,4 @@ export class WorkerConstants { - public static EMPTY_OBJECT = {}; public static readonly EMPTY_FUNCTION = Object.freeze(() => { /* This is intentional */ }); diff --git a/src/worker/WorkerFactory.ts b/src/worker/WorkerFactory.ts index a5b401a3..0a532681 100644 --- a/src/worker/WorkerFactory.ts +++ b/src/worker/WorkerFactory.ts @@ -22,13 +22,12 @@ export class WorkerFactory { if (!isMainThread) { throw new Error('Cannot get a worker implementation outside the main thread'); } - workerOptions = workerOptions ?? (WorkerConstants.EMPTY_OBJECT as WorkerOptions); + workerOptions = workerOptions ?? ({} as WorkerOptions); workerOptions.workerStartDelay = workerOptions?.workerStartDelay ?? WorkerConstants.DEFAULT_WORKER_START_DELAY; workerOptions.elementStartDelay = workerOptions?.elementStartDelay ?? WorkerConstants.DEFAULT_ELEMENT_START_DELAY; - workerOptions.poolOptions = - workerOptions?.poolOptions ?? (WorkerConstants.EMPTY_OBJECT as PoolOptions); + workerOptions.poolOptions = workerOptions?.poolOptions ?? ({} as PoolOptions); workerOptions?.messageHandler && (workerOptions.poolOptions.messageHandler = workerOptions.messageHandler); let workerImplementation: WorkerAbstract | null = null;