From: Jérôme Benoit Date: Fri, 3 Jun 2022 19:12:41 +0000 (+0200) Subject: Prepare the code for ESM support X-Git-Tag: v1.1.61~11 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0d8140bd623b67d6d7fddeda288fd945af0cb168;p=e-mobility-charging-stations-simulator.git Prepare the code for ESM support Signed-off-by: Jérôme Benoit --- diff --git a/mikro-orm.config-template.ts b/mikro-orm.config-template.ts index ab28c40a..5f318c13 100644 --- a/mikro-orm.config-template.ts +++ b/mikro-orm.config-template.ts @@ -2,6 +2,7 @@ import Constants from './src/utils/Constants'; import { PerformanceData } from './src/types/orm/entities/PerformanceData'; import { PerformanceRecord } from './src/types/orm/entities/PerformanceRecord'; import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; +import { fileURLToPath } from 'url'; import path from 'path'; export default { @@ -9,7 +10,7 @@ export default { entities: [PerformanceRecord, PerformanceData], type: 'sqlite', clientUrl: `file://${path.join( - path.resolve(__dirname), + path.resolve(path.dirname(fileURLToPath(import.meta.url))), `${Constants.DEFAULT_PERFORMANCE_RECORDS_DB_NAME}.db` )}`, }; diff --git a/rollup.config.mjs b/rollup.config.mjs index be738ef7..7f0e5774 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -40,7 +40,7 @@ export default { 'uuid', 'ws', 'winston-daily-rotate-file', - 'winston/lib/winston/transports', + 'winston/lib/winston/transports/index.js', 'winston', 'worker_threads', ], diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index cf7208ef..1ce68ba2 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -20,6 +20,7 @@ import Utils from '../utils/Utils'; import WorkerAbstract from '../worker/WorkerAbstract'; import WorkerFactory from '../worker/WorkerFactory'; import chalk from 'chalk'; +import { fileURLToPath } from 'url'; import { isMainThread } from 'worker_threads'; import path from 'path'; import { version } from '../../package.json'; @@ -38,7 +39,7 @@ export default class Bootstrap { private constructor() { this.started = false; this.workerScript = path.join( - path.resolve(__dirname, '../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'charging-station', 'ChargingStationWorker.js' ); @@ -188,7 +189,7 @@ export default class Bootstrap { const workerData: ChargingStationWorkerData = { index, templateFile: path.join( - path.resolve(__dirname, '../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'assets', 'station-templates', path.basename(stationTemplateUrl.file) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 14bb05a1..e114da41 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -39,8 +39,9 @@ import { StopTransactionRequest, StopTransactionResponse, } from '../types/ocpp/Transaction'; +import { URL, fileURLToPath } from 'url'; import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket'; -import WebSocket, { Data, OPEN, RawData } from 'ws'; +import WebSocket, { Data, RawData } from 'ws'; import AutomaticTransactionGenerator from './AutomaticTransactionGenerator'; import { AutomaticTransactionGeneratorConfiguration } from '../types/AutomaticTransactionGenerator'; @@ -72,7 +73,6 @@ import OCPPRequestService from './ocpp/OCPPRequestService'; import { OCPPVersion } from '../types/ocpp/OCPPVersion'; import PerformanceStatistics from '../performance/PerformanceStatistics'; import { SupervisionUrlDistribution } from '../types/ConfigurationData'; -import { URL } from 'url'; import Utils from '../utils/Utils'; import crypto from 'crypto'; import fs from 'fs'; @@ -180,7 +180,7 @@ export default class ChargingStation { } public isWebSocketConnectionOpened(): boolean { - return this?.wsConnection?.readyState === OPEN; + return this?.wsConnection?.readyState === WebSocket.OPEN; } public getRegistrationStatus(): RegistrationStatus { @@ -877,7 +877,7 @@ export default class ChargingStation { this.hashId = ChargingStationUtils.getHashId(this.index, this.getTemplateFromFile()); logger.info(`${this.logPrefix()} Charging station hashId '${this.hashId}'`); this.configurationFile = path.join( - path.resolve(__dirname, '../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'assets', 'configurations', this.hashId + '.json' diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 9eee3466..ffe90a77 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -22,6 +22,7 @@ import Utils from '../utils/Utils'; import { WebSocketCloseEventStatusString } from '../types/WebSocket'; import { WorkerProcessType } from '../types/Worker'; import crypto from 'crypto'; +import { fileURLToPath } from 'url'; import fs from 'fs'; import logger from '../utils/Logger'; import moment from 'moment'; @@ -545,7 +546,7 @@ export class ChargingStationUtils { return ( stationInfo.authorizationFile && path.join( - path.resolve(__dirname, '../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'assets', path.basename(stationInfo.authorizationFile) ) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 1e66f4af..67967fd9 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -58,6 +58,7 @@ import { OCPP16StandardParametersKey, OCPP16SupportedFeatureProfiles, } from '../../../types/ocpp/1.6/Configuration'; +import { URL, fileURLToPath } from 'url'; import type ChargingStation from '../../ChargingStation'; import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; @@ -73,7 +74,6 @@ import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; import OCPPError from '../../../exception/OCPPError'; import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; -import { URL } from 'url'; import Utils from '../../../utils/Utils'; import fs from 'fs'; import logger from '../../../utils/Logger'; @@ -881,7 +881,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer let ftpClient: Client; try { const logFiles = fs - .readdirSync(path.resolve(__dirname, '../../../../')) + .readdirSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../')) .filter((file) => file.endsWith('.log')) .map((file) => path.join('./', file)); const diagnosticsArchive = chargingStation.stationInfo.chargingStationId + '_logs.tar.gz'; @@ -910,7 +910,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer }); }); uploadResponse = await ftpClient.uploadFrom( - path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive), + path.join( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../../'), + diagnosticsArchive + ), uri.pathname + diagnosticsArchive ); if (uploadResponse.code === 226) { diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index 92860638..1c8ed1b5 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -1,12 +1,12 @@ import AbstractUIService from './ui-services/AbstractUIService'; import { Server as HttpServer } from 'http'; import { ProtocolVersion } from '../../types/UIProtocol'; -import { Server as WSServer } from 'ws'; +import WebSocket from 'ws'; export abstract class AbstractUIServer { public readonly chargingStations: Set; protected readonly uiServices: Map; - protected server: WSServer | HttpServer; + protected server: WebSocket.Server | HttpServer; public constructor() { this.chargingStations = new Set(); diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index f71f404b..c67da00e 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -1,5 +1,4 @@ import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; -import WebSocket, { OPEN, Server } from 'ws'; import { AbstractUIServer } from './AbstractUIServer'; import Configuration from '../../utils/Configuration'; @@ -7,12 +6,13 @@ import { IncomingMessage } from 'http'; import { ServerOptions } from '../../types/ConfigurationData'; import UIServiceFactory from './ui-services/UIServiceFactory'; import Utils from '../../utils/Utils'; +import WebSocket from 'ws'; import logger from '../../utils/Logger'; export default class UIWebSocketServer extends AbstractUIServer { public constructor(options?: ServerOptions) { super(); - this.server = new Server(options ?? Configuration.getUIServer().options); + this.server = new WebSocket.Server(options ?? Configuration.getUIServer().options); } public start(): void { @@ -52,8 +52,8 @@ export default class UIWebSocketServer extends AbstractUIServer { } private broadcastToClients(message: string): void { - for (const client of (this.server as Server).clients) { - if (client?.readyState === OPEN) { + for (const client of (this.server as WebSocket.Server).clients) { + if (client?.readyState === WebSocket.OPEN) { client.send(message); } } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 491c377e..716e5309 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -15,12 +15,13 @@ import type { WorkerChoiceStrategy } from 'poolifier'; import WorkerConstants from '../worker/WorkerConstants'; import { WorkerProcessType } from '../types/Worker'; import chalk from 'chalk'; +import { fileURLToPath } from 'url'; import fs from 'fs'; import path from 'path'; export default class Configuration { private static configurationFile = path.join( - path.resolve(__dirname, '../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'assets', 'config.json' ); @@ -370,11 +371,14 @@ export default class Configuration { switch (storageType) { case StorageType.JSON_FILE: return `file://${path.join( - path.resolve(__dirname, '../../'), + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'), Constants.DEFAULT_PERFORMANCE_RECORDS_FILENAME )}`; case StorageType.SQLITE: - return `file://${path.join(path.resolve(__dirname, '../../'), SQLiteFileName)}`; + return `file://${path.join( + path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../'), + SQLiteFileName + )}`; default: throw new Error(`Performance storage URI is mandatory with storage type '${storageType}'`); } diff --git a/src/utils/Logger.ts b/src/utils/Logger.ts index 5a98ed99..aba00c74 100644 --- a/src/utils/Logger.ts +++ b/src/utils/Logger.ts @@ -1,9 +1,9 @@ -import { Console, File } from 'winston/lib/winston/transports'; import { Logger, createLogger, format, transport } from 'winston'; import Configuration from './Configuration'; import DailyRotateFile from 'winston-daily-rotate-file'; import { Format } from 'logform'; +import TransportType from 'winston/lib/winston/transports/index.js'; import Utils from './Utils'; let transports: transport[]; @@ -30,8 +30,8 @@ if (Configuration.getLogRotate()) { ]; } else { transports = [ - new File({ filename: Configuration.getLogErrorFile(), level: 'error' }), - new File({ filename: Configuration.getLogFile() }), + new TransportType.File({ filename: Configuration.getLogErrorFile(), level: 'error' }), + new TransportType.File({ filename: Configuration.getLogFile() }), ]; } @@ -47,7 +47,7 @@ const logger: Logger = createLogger({ // if (Configuration.getLogConsole()) { logger.add( - new Console({ + new TransportType.Console({ format: format.combine( format.splat(), (format[Configuration.getLogFormat()] as () => Format)()