| 1 | import chalk from 'chalk' |
| 2 | import { existsSync, type FSWatcher, readFileSync, watch } from 'node:fs' |
| 3 | import { dirname, join } from 'node:path' |
| 4 | import { env } from 'node:process' |
| 5 | import { fileURLToPath } from 'node:url' |
| 6 | import { has, mergeDeepRight, once } from 'rambda' |
| 7 | |
| 8 | import { |
| 9 | ApplicationProtocol, |
| 10 | ApplicationProtocolVersion, |
| 11 | type ConfigurationData, |
| 12 | ConfigurationSection, |
| 13 | FileType, |
| 14 | type LogConfiguration, |
| 15 | type StationTemplateUrl, |
| 16 | type StorageConfiguration, |
| 17 | StorageType, |
| 18 | SupervisionUrlDistribution, |
| 19 | type UIServerConfiguration, |
| 20 | type WorkerConfiguration, |
| 21 | } from '../types/index.js' |
| 22 | import { |
| 23 | DEFAULT_ELEMENT_ADD_DELAY, |
| 24 | DEFAULT_POOL_MAX_SIZE, |
| 25 | DEFAULT_POOL_MIN_SIZE, |
| 26 | DEFAULT_WORKER_START_DELAY, |
| 27 | WorkerProcessType, |
| 28 | } from '../worker/index.js' |
| 29 | import { |
| 30 | buildPerformanceUriFilePath, |
| 31 | checkWorkerElementsPerWorker, |
| 32 | checkWorkerProcessType, |
| 33 | getDefaultPerformanceStorageUri, |
| 34 | handleFileException, |
| 35 | logPrefix, |
| 36 | } from './ConfigurationUtils.js' |
| 37 | import { Constants } from './Constants.js' |
| 38 | import { isCFEnvironment } from './Utils.js' |
| 39 | |
| 40 | type ConfigurationSectionType = |
| 41 | | LogConfiguration |
| 42 | | StorageConfiguration |
| 43 | | UIServerConfiguration |
| 44 | | WorkerConfiguration |
| 45 | |
| 46 | const defaultUIServerConfiguration: UIServerConfiguration = { |
| 47 | enabled: false, |
| 48 | options: { |
| 49 | host: Constants.DEFAULT_UI_SERVER_HOST, |
| 50 | port: Constants.DEFAULT_UI_SERVER_PORT, |
| 51 | }, |
| 52 | type: ApplicationProtocol.WS, |
| 53 | version: ApplicationProtocolVersion.VERSION_11, |
| 54 | } |
| 55 | |
| 56 | const defaultStorageConfiguration: StorageConfiguration = { |
| 57 | enabled: true, |
| 58 | type: StorageType.NONE, |
| 59 | } |
| 60 | |
| 61 | const defaultLogConfiguration: LogConfiguration = { |
| 62 | enabled: true, |
| 63 | errorFile: 'logs/error.log', |
| 64 | file: 'logs/combined.log', |
| 65 | format: 'simple', |
| 66 | level: 'info', |
| 67 | rotate: true, |
| 68 | statisticsInterval: Constants.DEFAULT_LOG_STATISTICS_INTERVAL, |
| 69 | } |
| 70 | |
| 71 | const defaultWorkerConfiguration: WorkerConfiguration = { |
| 72 | elementAddDelay: DEFAULT_ELEMENT_ADD_DELAY, |
| 73 | elementsPerWorker: 'auto', |
| 74 | poolMaxSize: DEFAULT_POOL_MAX_SIZE, |
| 75 | poolMinSize: DEFAULT_POOL_MIN_SIZE, |
| 76 | processType: WorkerProcessType.workerSet, |
| 77 | startDelay: DEFAULT_WORKER_START_DELAY, |
| 78 | } |
| 79 | |
| 80 | // eslint-disable-next-line @typescript-eslint/no-extraneous-class |
| 81 | export class Configuration { |
| 82 | public static configurationChangeCallback?: () => Promise<void> |
| 83 | |
| 84 | private static configurationData?: ConfigurationData |
| 85 | private static configurationFile: string | undefined |
| 86 | private static configurationFileReloading = false |
| 87 | private static configurationFileWatcher?: FSWatcher |
| 88 | private static configurationSectionCache: Map<ConfigurationSection, ConfigurationSectionType> |
| 89 | |
| 90 | static { |
| 91 | const configurationFile = join(dirname(fileURLToPath(import.meta.url)), 'assets', 'config.json') |
| 92 | if (existsSync(configurationFile)) { |
| 93 | Configuration.configurationFile = configurationFile |
| 94 | } else { |
| 95 | console.error( |
| 96 | `${chalk.green(logPrefix())} ${chalk.red( |
| 97 | "Configuration file './src/assets/config.json' not found, using default configuration" |
| 98 | )}` |
| 99 | ) |
| 100 | Configuration.configurationData = { |
| 101 | log: defaultLogConfiguration, |
| 102 | performanceStorage: defaultStorageConfiguration, |
| 103 | stationTemplateUrls: [ |
| 104 | { |
| 105 | file: 'siemens.station-template.json', |
| 106 | numberOfStations: 1, |
| 107 | }, |
| 108 | ], |
| 109 | supervisionUrlDistribution: SupervisionUrlDistribution.ROUND_ROBIN, |
| 110 | supervisionUrls: 'ws://localhost:8180/steve/websocket/CentralSystemService', |
| 111 | uiServer: defaultUIServerConfiguration, |
| 112 | worker: defaultWorkerConfiguration, |
| 113 | } |
| 114 | } |
| 115 | Configuration.configurationSectionCache = new Map< |
| 116 | ConfigurationSection, |
| 117 | ConfigurationSectionType |
| 118 | >([ |
| 119 | [ConfigurationSection.log, Configuration.buildLogSection()], |
| 120 | [ConfigurationSection.performanceStorage, Configuration.buildPerformanceStorageSection()], |
| 121 | [ConfigurationSection.uiServer, Configuration.buildUIServerSection()], |
| 122 | [ConfigurationSection.worker, Configuration.buildWorkerSection()], |
| 123 | ]) |
| 124 | } |
| 125 | |
| 126 | private constructor () { |
| 127 | // This is intentional |
| 128 | } |
| 129 | |
| 130 | private static buildLogSection (): LogConfiguration { |
| 131 | const deprecatedLogConfiguration: LogConfiguration = { |
| 132 | ...(has('logEnabled', Configuration.getConfigurationData()) && { |
| 133 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 134 | enabled: Configuration.getConfigurationData()?.logEnabled, |
| 135 | }), |
| 136 | ...(has('logFile', Configuration.getConfigurationData()) && { |
| 137 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 138 | file: Configuration.getConfigurationData()?.logFile, |
| 139 | }), |
| 140 | ...(has('logErrorFile', Configuration.getConfigurationData()) && { |
| 141 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 142 | errorFile: Configuration.getConfigurationData()?.logErrorFile, |
| 143 | }), |
| 144 | ...(has('logStatisticsInterval', Configuration.getConfigurationData()) && { |
| 145 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 146 | statisticsInterval: Configuration.getConfigurationData()?.logStatisticsInterval, |
| 147 | }), |
| 148 | ...(has('logLevel', Configuration.getConfigurationData()) && { |
| 149 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 150 | level: Configuration.getConfigurationData()?.logLevel, |
| 151 | }), |
| 152 | ...(has('logConsole', Configuration.getConfigurationData()) && { |
| 153 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 154 | console: Configuration.getConfigurationData()?.logConsole, |
| 155 | }), |
| 156 | ...(has('logFormat', Configuration.getConfigurationData()) && { |
| 157 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 158 | format: Configuration.getConfigurationData()?.logFormat, |
| 159 | }), |
| 160 | ...(has('logRotate', Configuration.getConfigurationData()) && { |
| 161 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 162 | rotate: Configuration.getConfigurationData()?.logRotate, |
| 163 | }), |
| 164 | ...(has('logMaxFiles', Configuration.getConfigurationData()) && { |
| 165 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 166 | maxFiles: Configuration.getConfigurationData()?.logMaxFiles, |
| 167 | }), |
| 168 | ...(has('logMaxSize', Configuration.getConfigurationData()) && { |
| 169 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 170 | maxSize: Configuration.getConfigurationData()?.logMaxSize, |
| 171 | }), |
| 172 | } |
| 173 | const logConfiguration: LogConfiguration = { |
| 174 | ...defaultLogConfiguration, |
| 175 | ...deprecatedLogConfiguration, |
| 176 | ...(has(ConfigurationSection.log, Configuration.getConfigurationData()) && |
| 177 | Configuration.getConfigurationData()?.log), |
| 178 | } |
| 179 | return logConfiguration |
| 180 | } |
| 181 | |
| 182 | private static buildPerformanceStorageSection (): StorageConfiguration { |
| 183 | let storageConfiguration: StorageConfiguration |
| 184 | switch (Configuration.getConfigurationData()?.performanceStorage?.type) { |
| 185 | case StorageType.JSON_FILE: |
| 186 | storageConfiguration = { |
| 187 | enabled: false, |
| 188 | type: StorageType.JSON_FILE, |
| 189 | uri: getDefaultPerformanceStorageUri(StorageType.JSON_FILE), |
| 190 | } |
| 191 | break |
| 192 | case StorageType.SQLITE: |
| 193 | storageConfiguration = { |
| 194 | enabled: false, |
| 195 | type: StorageType.SQLITE, |
| 196 | uri: getDefaultPerformanceStorageUri(StorageType.SQLITE), |
| 197 | } |
| 198 | break |
| 199 | case StorageType.NONE: |
| 200 | default: |
| 201 | storageConfiguration = defaultStorageConfiguration |
| 202 | break |
| 203 | } |
| 204 | if (has(ConfigurationSection.performanceStorage, Configuration.getConfigurationData())) { |
| 205 | storageConfiguration = { |
| 206 | ...storageConfiguration, |
| 207 | ...Configuration.getConfigurationData()?.performanceStorage, |
| 208 | ...((Configuration.getConfigurationData()?.performanceStorage?.type === |
| 209 | StorageType.JSON_FILE || |
| 210 | Configuration.getConfigurationData()?.performanceStorage?.type === StorageType.SQLITE) && |
| 211 | Configuration.getConfigurationData()?.performanceStorage?.uri != null && { |
| 212 | uri: buildPerformanceUriFilePath( |
| 213 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 214 | new URL(Configuration.getConfigurationData()!.performanceStorage!.uri!).pathname |
| 215 | ), |
| 216 | }), |
| 217 | } |
| 218 | } |
| 219 | return storageConfiguration |
| 220 | } |
| 221 | |
| 222 | private static buildUIServerSection (): UIServerConfiguration { |
| 223 | let uiServerConfiguration: UIServerConfiguration = defaultUIServerConfiguration |
| 224 | if (has(ConfigurationSection.uiServer, Configuration.getConfigurationData())) { |
| 225 | uiServerConfiguration = mergeDeepRight( |
| 226 | uiServerConfiguration, |
| 227 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 228 | Configuration.getConfigurationData()!.uiServer! |
| 229 | ) |
| 230 | } |
| 231 | if (isCFEnvironment()) { |
| 232 | delete uiServerConfiguration.options?.host |
| 233 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 234 | uiServerConfiguration.options!.port = Number.parseInt(env.PORT!) |
| 235 | } |
| 236 | return uiServerConfiguration |
| 237 | } |
| 238 | |
| 239 | private static buildWorkerSection (): WorkerConfiguration { |
| 240 | const deprecatedWorkerConfiguration: WorkerConfiguration = { |
| 241 | ...(has('workerProcess', Configuration.getConfigurationData()) && { |
| 242 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 243 | processType: Configuration.getConfigurationData()?.workerProcess, |
| 244 | }), |
| 245 | ...(has('workerStartDelay', Configuration.getConfigurationData()) && { |
| 246 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 247 | startDelay: Configuration.getConfigurationData()?.workerStartDelay, |
| 248 | }), |
| 249 | ...(has('chargingStationsPerWorker', Configuration.getConfigurationData()) && { |
| 250 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 251 | elementsPerWorker: Configuration.getConfigurationData()?.chargingStationsPerWorker, |
| 252 | }), |
| 253 | ...(has('elementAddDelay', Configuration.getConfigurationData()) && { |
| 254 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 255 | elementAddDelay: Configuration.getConfigurationData()?.elementAddDelay, |
| 256 | }), |
| 257 | ...(has('elementStartDelay', Configuration.getConfigurationData()?.worker) && { |
| 258 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 259 | elementAddDelay: Configuration.getConfigurationData()?.worker?.elementStartDelay, |
| 260 | }), |
| 261 | ...(has('workerPoolMinSize', Configuration.getConfigurationData()) && { |
| 262 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 263 | poolMinSize: Configuration.getConfigurationData()?.workerPoolMinSize, |
| 264 | }), |
| 265 | ...(has('workerPoolMaxSize', Configuration.getConfigurationData()) && { |
| 266 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 267 | poolMaxSize: Configuration.getConfigurationData()?.workerPoolMaxSize, |
| 268 | }), |
| 269 | } |
| 270 | has('workerPoolStrategy', Configuration.getConfigurationData()) && |
| 271 | // eslint-disable-next-line @typescript-eslint/no-deprecated |
| 272 | delete Configuration.getConfigurationData()?.workerPoolStrategy |
| 273 | const workerConfiguration: WorkerConfiguration = { |
| 274 | ...defaultWorkerConfiguration, |
| 275 | ...deprecatedWorkerConfiguration, |
| 276 | ...(has(ConfigurationSection.worker, Configuration.getConfigurationData()) && |
| 277 | Configuration.getConfigurationData()?.worker), |
| 278 | } |
| 279 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 280 | checkWorkerProcessType(workerConfiguration.processType!) |
| 281 | checkWorkerElementsPerWorker(workerConfiguration.elementsPerWorker) |
| 282 | return workerConfiguration |
| 283 | } |
| 284 | |
| 285 | private static cacheConfigurationSection (sectionName: ConfigurationSection): void { |
| 286 | switch (sectionName) { |
| 287 | case ConfigurationSection.log: |
| 288 | Configuration.configurationSectionCache.set(sectionName, Configuration.buildLogSection()) |
| 289 | break |
| 290 | case ConfigurationSection.performanceStorage: |
| 291 | Configuration.configurationSectionCache.set( |
| 292 | sectionName, |
| 293 | Configuration.buildPerformanceStorageSection() |
| 294 | ) |
| 295 | break |
| 296 | case ConfigurationSection.uiServer: |
| 297 | Configuration.configurationSectionCache.set( |
| 298 | sectionName, |
| 299 | Configuration.buildUIServerSection() |
| 300 | ) |
| 301 | break |
| 302 | case ConfigurationSection.worker: |
| 303 | Configuration.configurationSectionCache.set(sectionName, Configuration.buildWorkerSection()) |
| 304 | break |
| 305 | default: |
| 306 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 307 | throw new Error(`Unknown configuration section '${sectionName}'`) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private static checkDeprecatedConfigurationKeys (): void { |
| 312 | // connection timeout |
| 313 | Configuration.warnDeprecatedConfigurationKey( |
| 314 | 'autoReconnectTimeout', |
| 315 | undefined, |
| 316 | "Use 'ConnectionTimeOut' OCPP parameter in charging station template instead" |
| 317 | ) |
| 318 | Configuration.warnDeprecatedConfigurationKey( |
| 319 | 'connectionTimeout', |
| 320 | undefined, |
| 321 | "Use 'ConnectionTimeOut' OCPP parameter in charging station template instead" |
| 322 | ) |
| 323 | // connection retries |
| 324 | Configuration.warnDeprecatedConfigurationKey( |
| 325 | 'autoReconnectMaxRetries', |
| 326 | undefined, |
| 327 | 'Use it in charging station template instead' |
| 328 | ) |
| 329 | // station template url(s) |
| 330 | Configuration.warnDeprecatedConfigurationKey( |
| 331 | 'stationTemplateURLs', |
| 332 | undefined, |
| 333 | "Use 'stationTemplateUrls' instead" |
| 334 | ) |
| 335 | Configuration.getConfigurationData()?.['stationTemplateURLs' as keyof ConfigurationData] != |
| 336 | null && |
| 337 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 338 | (Configuration.getConfigurationData()!.stationTemplateUrls = |
| 339 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 340 | Configuration.getConfigurationData()![ |
| 341 | 'stationTemplateURLs' as keyof ConfigurationData |
| 342 | ] as StationTemplateUrl[]) |
| 343 | Configuration.getConfigurationData()?.stationTemplateUrls.forEach( |
| 344 | (stationTemplateUrl: StationTemplateUrl) => { |
| 345 | if (stationTemplateUrl['numberOfStation' as keyof StationTemplateUrl] != null) { |
| 346 | console.error( |
| 347 | `${chalk.green(logPrefix())} ${chalk.red( |
| 348 | `Deprecated configuration key 'numberOfStation' usage for template file '${stationTemplateUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead` |
| 349 | )}` |
| 350 | ) |
| 351 | } |
| 352 | } |
| 353 | ) |
| 354 | // supervision url(s) |
| 355 | Configuration.warnDeprecatedConfigurationKey( |
| 356 | 'supervisionURLs', |
| 357 | undefined, |
| 358 | "Use 'supervisionUrls' instead" |
| 359 | ) |
| 360 | // supervision urls distribution |
| 361 | Configuration.warnDeprecatedConfigurationKey( |
| 362 | 'distributeStationToTenantEqually', |
| 363 | undefined, |
| 364 | "Use 'supervisionUrlDistribution' instead" |
| 365 | ) |
| 366 | Configuration.warnDeprecatedConfigurationKey( |
| 367 | 'distributeStationsToTenantsEqually', |
| 368 | undefined, |
| 369 | "Use 'supervisionUrlDistribution' instead" |
| 370 | ) |
| 371 | // worker section |
| 372 | Configuration.warnDeprecatedConfigurationKey( |
| 373 | 'useWorkerPool', |
| 374 | undefined, |
| 375 | `Use '${ConfigurationSection.worker}' section to define the type of worker process model instead` |
| 376 | ) |
| 377 | Configuration.warnDeprecatedConfigurationKey( |
| 378 | 'workerProcess', |
| 379 | undefined, |
| 380 | `Use '${ConfigurationSection.worker}' section to define the type of worker process model instead` |
| 381 | ) |
| 382 | Configuration.warnDeprecatedConfigurationKey( |
| 383 | 'workerStartDelay', |
| 384 | undefined, |
| 385 | `Use '${ConfigurationSection.worker}' section to define the worker start delay instead` |
| 386 | ) |
| 387 | Configuration.warnDeprecatedConfigurationKey( |
| 388 | 'chargingStationsPerWorker', |
| 389 | undefined, |
| 390 | `Use '${ConfigurationSection.worker}' section to define the number of element(s) per worker instead` |
| 391 | ) |
| 392 | Configuration.warnDeprecatedConfigurationKey( |
| 393 | 'elementAddDelay', |
| 394 | undefined, |
| 395 | `Use '${ConfigurationSection.worker}' section to define the worker's element add delay instead` |
| 396 | ) |
| 397 | Configuration.warnDeprecatedConfigurationKey( |
| 398 | 'workerPoolMinSize', |
| 399 | undefined, |
| 400 | `Use '${ConfigurationSection.worker}' section to define the worker pool minimum size instead` |
| 401 | ) |
| 402 | Configuration.warnDeprecatedConfigurationKey( |
| 403 | 'workerPoolSize', |
| 404 | undefined, |
| 405 | `Use '${ConfigurationSection.worker}' section to define the worker pool maximum size instead` |
| 406 | ) |
| 407 | Configuration.warnDeprecatedConfigurationKey( |
| 408 | 'workerPoolMaxSize', |
| 409 | undefined, |
| 410 | `Use '${ConfigurationSection.worker}' section to define the worker pool maximum size instead` |
| 411 | ) |
| 412 | Configuration.warnDeprecatedConfigurationKey( |
| 413 | 'workerPoolStrategy', |
| 414 | undefined, |
| 415 | `Use '${ConfigurationSection.worker}' section to define the worker pool strategy instead` |
| 416 | ) |
| 417 | Configuration.warnDeprecatedConfigurationKey( |
| 418 | 'poolStrategy', |
| 419 | ConfigurationSection.worker, |
| 420 | 'Not publicly exposed to end users' |
| 421 | ) |
| 422 | Configuration.warnDeprecatedConfigurationKey( |
| 423 | 'elementStartDelay', |
| 424 | ConfigurationSection.worker, |
| 425 | "Use 'elementAddDelay' instead" |
| 426 | ) |
| 427 | if ( |
| 428 | Configuration.getConfigurationData()?.worker?.processType === |
| 429 | ('staticPool' as WorkerProcessType) |
| 430 | ) { |
| 431 | console.error( |
| 432 | `${chalk.green(logPrefix())} ${chalk.red( |
| 433 | `Deprecated configuration 'staticPool' value usage in worker section 'processType' field. Use '${WorkerProcessType.fixedPool}' value instead` |
| 434 | )}` |
| 435 | ) |
| 436 | } |
| 437 | // log section |
| 438 | Configuration.warnDeprecatedConfigurationKey( |
| 439 | 'logEnabled', |
| 440 | undefined, |
| 441 | `Use '${ConfigurationSection.log}' section to define the logging enablement instead` |
| 442 | ) |
| 443 | Configuration.warnDeprecatedConfigurationKey( |
| 444 | 'logFile', |
| 445 | undefined, |
| 446 | `Use '${ConfigurationSection.log}' section to define the log file instead` |
| 447 | ) |
| 448 | Configuration.warnDeprecatedConfigurationKey( |
| 449 | 'logErrorFile', |
| 450 | undefined, |
| 451 | `Use '${ConfigurationSection.log}' section to define the log error file instead` |
| 452 | ) |
| 453 | Configuration.warnDeprecatedConfigurationKey( |
| 454 | 'logConsole', |
| 455 | undefined, |
| 456 | `Use '${ConfigurationSection.log}' section to define the console logging enablement instead` |
| 457 | ) |
| 458 | Configuration.warnDeprecatedConfigurationKey( |
| 459 | 'logStatisticsInterval', |
| 460 | undefined, |
| 461 | `Use '${ConfigurationSection.log}' section to define the log statistics interval instead` |
| 462 | ) |
| 463 | Configuration.warnDeprecatedConfigurationKey( |
| 464 | 'logLevel', |
| 465 | undefined, |
| 466 | `Use '${ConfigurationSection.log}' section to define the log level instead` |
| 467 | ) |
| 468 | Configuration.warnDeprecatedConfigurationKey( |
| 469 | 'logFormat', |
| 470 | undefined, |
| 471 | `Use '${ConfigurationSection.log}' section to define the log format instead` |
| 472 | ) |
| 473 | Configuration.warnDeprecatedConfigurationKey( |
| 474 | 'logRotate', |
| 475 | undefined, |
| 476 | `Use '${ConfigurationSection.log}' section to define the log rotation enablement instead` |
| 477 | ) |
| 478 | Configuration.warnDeprecatedConfigurationKey( |
| 479 | 'logMaxFiles', |
| 480 | undefined, |
| 481 | `Use '${ConfigurationSection.log}' section to define the log maximum files instead` |
| 482 | ) |
| 483 | Configuration.warnDeprecatedConfigurationKey( |
| 484 | 'logMaxSize', |
| 485 | undefined, |
| 486 | `Use '${ConfigurationSection.log}' section to define the log maximum size instead` |
| 487 | ) |
| 488 | // performanceStorage section |
| 489 | Configuration.warnDeprecatedConfigurationKey( |
| 490 | 'URI', |
| 491 | ConfigurationSection.performanceStorage, |
| 492 | "Use 'uri' instead" |
| 493 | ) |
| 494 | // uiServer section |
| 495 | if (has(Configuration.getConfigurationData(), 'uiWebSocketServer')) { |
| 496 | console.error( |
| 497 | `${chalk.green(logPrefix())} ${chalk.red( |
| 498 | `Deprecated configuration section 'uiWebSocketServer' usage. Use '${ConfigurationSection.uiServer}' instead` |
| 499 | )}` |
| 500 | ) |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | public static getConfigurationData (): ConfigurationData | undefined { |
| 505 | if ( |
| 506 | Configuration.configurationData == null && |
| 507 | Configuration.configurationFile != null && |
| 508 | Configuration.configurationFile.length > 0 |
| 509 | ) { |
| 510 | try { |
| 511 | Configuration.configurationData = JSON.parse( |
| 512 | readFileSync(Configuration.configurationFile, 'utf8') |
| 513 | ) as ConfigurationData |
| 514 | if (Configuration.configurationFileWatcher == null) { |
| 515 | Configuration.configurationFileWatcher = Configuration.getConfigurationFileWatcher() |
| 516 | } |
| 517 | } catch (error) { |
| 518 | handleFileException( |
| 519 | Configuration.configurationFile, |
| 520 | FileType.Configuration, |
| 521 | error as NodeJS.ErrnoException, |
| 522 | logPrefix() |
| 523 | ) |
| 524 | } |
| 525 | } |
| 526 | return Configuration.configurationData |
| 527 | } |
| 528 | |
| 529 | private static getConfigurationFileWatcher (): FSWatcher | undefined { |
| 530 | if (Configuration.configurationFile == null || Configuration.configurationFile.length === 0) { |
| 531 | return |
| 532 | } |
| 533 | try { |
| 534 | return watch(Configuration.configurationFile, (event, filename): void => { |
| 535 | if ( |
| 536 | !Configuration.configurationFileReloading && |
| 537 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 538 | filename!.trim().length > 0 && |
| 539 | event === 'change' |
| 540 | ) { |
| 541 | Configuration.configurationFileReloading = true |
| 542 | const consoleWarnOnce = once(console.warn) |
| 543 | consoleWarnOnce( |
| 544 | `${chalk.green(logPrefix())} ${chalk.yellow( |
| 545 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 546 | `${FileType.Configuration} ${this.configurationFile} file have changed, reload` |
| 547 | )}` |
| 548 | ) |
| 549 | delete Configuration.configurationData |
| 550 | Configuration.configurationSectionCache.clear() |
| 551 | if (Configuration.configurationChangeCallback != null) { |
| 552 | Configuration.configurationChangeCallback() |
| 553 | .finally(() => { |
| 554 | Configuration.configurationFileReloading = false |
| 555 | }) |
| 556 | .catch((error: unknown) => { |
| 557 | throw typeof error === 'string' ? new Error(error) : error |
| 558 | }) |
| 559 | } else { |
| 560 | Configuration.configurationFileReloading = false |
| 561 | } |
| 562 | } |
| 563 | }) |
| 564 | } catch (error) { |
| 565 | handleFileException( |
| 566 | Configuration.configurationFile, |
| 567 | FileType.Configuration, |
| 568 | error as NodeJS.ErrnoException, |
| 569 | logPrefix() |
| 570 | ) |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters |
| 575 | public static getConfigurationSection<T extends ConfigurationSectionType>( |
| 576 | sectionName: ConfigurationSection |
| 577 | ): T { |
| 578 | if (!Configuration.isConfigurationSectionCached(sectionName)) { |
| 579 | Configuration.cacheConfigurationSection(sectionName) |
| 580 | } |
| 581 | return Configuration.configurationSectionCache.get(sectionName) as T |
| 582 | } |
| 583 | |
| 584 | public static getStationTemplateUrls (): StationTemplateUrl[] | undefined { |
| 585 | const checkDeprecatedConfigurationKeysOnce = once( |
| 586 | Configuration.checkDeprecatedConfigurationKeys.bind(Configuration) |
| 587 | ) |
| 588 | checkDeprecatedConfigurationKeysOnce() |
| 589 | return Configuration.getConfigurationData()?.stationTemplateUrls |
| 590 | } |
| 591 | |
| 592 | public static getSupervisionUrlDistribution (): SupervisionUrlDistribution | undefined { |
| 593 | return has(Configuration.getConfigurationData(), 'supervisionUrlDistribution') |
| 594 | ? Configuration.getConfigurationData()?.supervisionUrlDistribution |
| 595 | : SupervisionUrlDistribution.ROUND_ROBIN |
| 596 | } |
| 597 | |
| 598 | public static getSupervisionUrls (): string | string[] | undefined { |
| 599 | if ( |
| 600 | Configuration.getConfigurationData()?.['supervisionURLs' as keyof ConfigurationData] != null |
| 601 | ) { |
| 602 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 603 | Configuration.getConfigurationData()!.supervisionUrls = Configuration.getConfigurationData()![ |
| 604 | 'supervisionURLs' as keyof ConfigurationData |
| 605 | ] as string | string[] |
| 606 | } |
| 607 | return Configuration.getConfigurationData()?.supervisionUrls |
| 608 | } |
| 609 | |
| 610 | private static isConfigurationSectionCached (sectionName: ConfigurationSection): boolean { |
| 611 | return Configuration.configurationSectionCache.has(sectionName) |
| 612 | } |
| 613 | |
| 614 | private static warnDeprecatedConfigurationKey ( |
| 615 | key: string, |
| 616 | configurationSection?: ConfigurationSection, |
| 617 | logMsgToAppend = '' |
| 618 | ): void { |
| 619 | if ( |
| 620 | configurationSection != null && |
| 621 | Configuration.getConfigurationData()?.[configurationSection as keyof ConfigurationData] != |
| 622 | null && |
| 623 | ( |
| 624 | Configuration.getConfigurationData()?.[ |
| 625 | configurationSection as keyof ConfigurationData |
| 626 | ] as Record<string, unknown> |
| 627 | )[key] != null |
| 628 | ) { |
| 629 | console.error( |
| 630 | `${chalk.green(logPrefix())} ${chalk.red( |
| 631 | `Deprecated configuration key '${key}' usage in section '${configurationSection}'${ |
| 632 | logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : '' |
| 633 | }` |
| 634 | )}` |
| 635 | ) |
| 636 | } else if (Configuration.getConfigurationData()?.[key as keyof ConfigurationData] != null) { |
| 637 | console.error( |
| 638 | `${chalk.green(logPrefix())} ${chalk.red( |
| 639 | `Deprecated configuration key '${key}' usage${ |
| 640 | logMsgToAppend.trim().length > 0 ? `. ${logMsgToAppend}` : '' |
| 641 | }` |
| 642 | )}` |
| 643 | ) |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | public static workerDynamicPoolInUse (): boolean { |
| 648 | return ( |
| 649 | Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker) |
| 650 | .processType === WorkerProcessType.dynamicPool |
| 651 | ) |
| 652 | } |
| 653 | |
| 654 | public static workerPoolInUse (): boolean { |
| 655 | return [WorkerProcessType.dynamicPool, WorkerProcessType.fixedPool].includes( |
| 656 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 657 | Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker) |
| 658 | .processType! |
| 659 | ) |
| 660 | } |
| 661 | } |