| 1 | import fs from 'node:fs'; |
| 2 | |
| 3 | import { FileType } from '../types'; |
| 4 | import { FileUtils, Utils, logger } from '../utils'; |
| 5 | |
| 6 | export class AuthorizedTagsCache { |
| 7 | private static instance: AuthorizedTagsCache | null = null; |
| 8 | private readonly tagsCaches: Map<string, string[]>; |
| 9 | private readonly FSWatchers: Map<string, fs.FSWatcher | undefined>; |
| 10 | |
| 11 | private constructor() { |
| 12 | this.tagsCaches = new Map<string, string[]>(); |
| 13 | this.FSWatchers = new Map<string, fs.FSWatcher | undefined>(); |
| 14 | } |
| 15 | |
| 16 | public static getInstance(): AuthorizedTagsCache { |
| 17 | if (AuthorizedTagsCache.instance === null) { |
| 18 | AuthorizedTagsCache.instance = new AuthorizedTagsCache(); |
| 19 | } |
| 20 | return AuthorizedTagsCache.instance; |
| 21 | } |
| 22 | |
| 23 | public getAuthorizedTags(file: string): string[] | undefined { |
| 24 | if (this.hasTags(file) === false) { |
| 25 | this.setTags(file, this.getAuthorizedTagsFromFile(file)); |
| 26 | // Monitor authorization file |
| 27 | this.FSWatchers.has(file) === false && |
| 28 | this.FSWatchers.set( |
| 29 | file, |
| 30 | FileUtils.watchJsonFile( |
| 31 | file, |
| 32 | FileType.Authorization, |
| 33 | this.logPrefix(file), |
| 34 | undefined, |
| 35 | (event, filename) => { |
| 36 | if (Utils.isNotEmptyString(filename) && event === 'change') { |
| 37 | try { |
| 38 | logger.debug( |
| 39 | `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload` |
| 40 | ); |
| 41 | this.deleteTags(file); |
| 42 | this.deleteFSWatcher(file); |
| 43 | } catch (error) { |
| 44 | FileUtils.handleFileException( |
| 45 | file, |
| 46 | FileType.Authorization, |
| 47 | error as NodeJS.ErrnoException, |
| 48 | this.logPrefix(file), |
| 49 | { |
| 50 | throwError: false, |
| 51 | } |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | ) |
| 57 | ); |
| 58 | } |
| 59 | return this.getTags(file); |
| 60 | } |
| 61 | |
| 62 | public deleteAuthorizedTags(file: string): boolean { |
| 63 | return this.deleteTags(file); |
| 64 | } |
| 65 | |
| 66 | private hasTags(file: string): boolean { |
| 67 | return this.tagsCaches.has(file); |
| 68 | } |
| 69 | |
| 70 | private setTags(file: string, tags: string[]) { |
| 71 | return this.tagsCaches.set(file, tags); |
| 72 | } |
| 73 | |
| 74 | private getTags(file: string): string[] | undefined { |
| 75 | return this.tagsCaches.get(file); |
| 76 | } |
| 77 | |
| 78 | private deleteTags(file: string): boolean { |
| 79 | return this.tagsCaches.delete(file); |
| 80 | } |
| 81 | |
| 82 | private deleteFSWatcher(file: string): boolean { |
| 83 | this.FSWatchers.get(file)?.close(); |
| 84 | return this.FSWatchers.delete(file); |
| 85 | } |
| 86 | |
| 87 | private getAuthorizedTagsFromFile(file: string): string[] { |
| 88 | let authorizedTags: string[] = []; |
| 89 | if (file) { |
| 90 | try { |
| 91 | // Load authorization file |
| 92 | authorizedTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[]; |
| 93 | } catch (error) { |
| 94 | FileUtils.handleFileException( |
| 95 | file, |
| 96 | FileType.Authorization, |
| 97 | error as NodeJS.ErrnoException, |
| 98 | this.logPrefix(file) |
| 99 | ); |
| 100 | } |
| 101 | } else { |
| 102 | logger.info(`${this.logPrefix(file)} No authorization file given`); |
| 103 | } |
| 104 | return authorizedTags; |
| 105 | } |
| 106 | |
| 107 | private logPrefix = (file: string): string => { |
| 108 | return Utils.logPrefix(` Authorized tags cache for authorization file '${file}' |`); |
| 109 | }; |
| 110 | } |