Commit | Line | Data |
---|---|---|
8114d10e JB |
1 | import fs from 'fs'; |
2 | ||
9d7484a4 JB |
3 | import { FileType } from '../types/FileType'; |
4 | import FileUtils from '../utils/FileUtils'; | |
9d7484a4 | 5 | import logger from '../utils/Logger'; |
8114d10e | 6 | import Utils from '../utils/Utils'; |
9d7484a4 JB |
7 | |
8 | export default class AuthorizedTagsCache { | |
9 | private static instance: AuthorizedTagsCache | null = null; | |
10 | private readonly tagsCaches: Map<string, string[]>; | |
11 | private readonly FSWatchers: Map<string, fs.FSWatcher>; | |
12 | ||
13 | private constructor() { | |
14 | this.tagsCaches = new Map<string, string[]>(); | |
15 | this.FSWatchers = new Map<string, fs.FSWatcher>(); | |
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 | ||
25 | public getAuthorizedTags(file: string): string[] { | |
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( | |
33 | this.logPrefix(file), | |
34 | FileType.Authorization, | |
35 | file, | |
36 | null, | |
37 | (event, filename) => { | |
38 | if (filename && event === 'change') { | |
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( | |
47 | this.logPrefix(file), | |
48 | FileType.Authorization, | |
49 | file, | |
50 | error as NodeJS.ErrnoException, | |
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 | ||
76 | private getTags(file: string): string[] { | |
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 { | |
85 | this.FSWatchers.get(file).close(); | |
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( | |
97 | this.logPrefix(file), | |
98 | FileType.Authorization, | |
99 | file, | |
100 | error as NodeJS.ErrnoException | |
101 | ); | |
102 | } | |
103 | } else { | |
44eb6026 | 104 | logger.info(`${this.logPrefix(file)} No authorization file given`); |
9d7484a4 JB |
105 | } |
106 | return authorizedTags; | |
107 | } | |
108 | ||
109 | private logPrefix(file: string): string { | |
110 | return Utils.logPrefix(` Authorized tags cache for authorization file '${file}' |`); | |
111 | } | |
112 | } |