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