X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FAuthorizedTagsCache.ts;h=a3c8e5cdc42c7bfc1f9e978bb59067f856783fd3;hb=5a2a53cfa256a32aa342d4dee48492118065e84a;hp=4142ee251f354ed31100c530a5732e25e1f3c013;hpb=8114d10e3893e96bb725ce2fca9744429ee4b75b;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/AuthorizedTagsCache.ts b/src/charging-station/AuthorizedTagsCache.ts index 4142ee25..a3c8e5cd 100644 --- a/src/charging-station/AuthorizedTagsCache.ts +++ b/src/charging-station/AuthorizedTagsCache.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import fs from 'node:fs'; import { FileType } from '../types/FileType'; import FileUtils from '../utils/FileUtils'; @@ -8,49 +8,46 @@ import Utils from '../utils/Utils'; export default class AuthorizedTagsCache { private static instance: AuthorizedTagsCache | null = null; private readonly tagsCaches: Map; - private readonly FSWatchers: Map; + private readonly FSWatchers: Map; private constructor() { this.tagsCaches = new Map(); - this.FSWatchers = new Map(); + this.FSWatchers = new Map(); } public static getInstance(): AuthorizedTagsCache { - if (!AuthorizedTagsCache.instance) { + if (AuthorizedTagsCache.instance === null) { AuthorizedTagsCache.instance = new AuthorizedTagsCache(); } return AuthorizedTagsCache.instance; } - public getAuthorizedTags(file: string): string[] { - if (!this.hasTags(file)) { + public getAuthorizedTags(file: string): string[] | undefined { + if (this.hasTags(file) === false) { this.setTags(file, this.getAuthorizedTagsFromFile(file)); // Monitor authorization file - !this.FSWatchers.has(file) && + this.FSWatchers.has(file) === false && this.FSWatchers.set( file, FileUtils.watchJsonFile( - this.logPrefix(file), - FileType.Authorization, file, - null, + FileType.Authorization, + this.logPrefix(file), + undefined, (event, filename) => { - if (filename && event === 'change') { + if (Utils.isNotEmptyString(filename) && event === 'change') { try { logger.debug( - this.logPrefix(file) + - ' ' + - FileType.Authorization + - ' file have changed, reload' + `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload` ); this.deleteTags(file); this.deleteFSWatcher(file); } catch (error) { FileUtils.handleFileException( - this.logPrefix(file), - FileType.Authorization, file, + FileType.Authorization, error as NodeJS.ErrnoException, + this.logPrefix(file), { throwError: false, } @@ -64,6 +61,10 @@ export default class AuthorizedTagsCache { return this.getTags(file); } + public deleteAuthorizedTags(file: string): boolean { + return this.deleteTags(file); + } + private hasTags(file: string): boolean { return this.tagsCaches.has(file); } @@ -72,7 +73,7 @@ export default class AuthorizedTagsCache { return this.tagsCaches.set(file, tags); } - private getTags(file: string): string[] { + private getTags(file: string): string[] | undefined { return this.tagsCaches.get(file); } @@ -81,7 +82,7 @@ export default class AuthorizedTagsCache { } private deleteFSWatcher(file: string): boolean { - this.FSWatchers.get(file).close(); + this.FSWatchers.get(file)?.close(); return this.FSWatchers.delete(file); } @@ -93,19 +94,19 @@ export default class AuthorizedTagsCache { authorizedTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; } catch (error) { FileUtils.handleFileException( - this.logPrefix(file), - FileType.Authorization, file, - error as NodeJS.ErrnoException + FileType.Authorization, + error as NodeJS.ErrnoException, + this.logPrefix(file) ); } } else { - logger.info(this.logPrefix(file) + ' No authorization file given)'); + logger.info(`${this.logPrefix(file)} No authorization file given`); } return authorizedTags; } - private logPrefix(file: string): string { + private logPrefix = (file: string): string => { return Utils.logPrefix(` Authorized tags cache for authorization file '${file}' |`); - } + }; }