* Reads contents from the local map, if any exist and the file hasn't * changed on disk since hydration, or loads from the file system and * hydrates the cache for the particular filepath * * @param path The filepath of the contents to read * @returns Promise of the contents separated
(path: string, encoded: boolean)
| 94 | * @returns Promise of the contents separated by newlines |
| 95 | */ |
| 96 | public async read(path: string, encoded: boolean): Promise<string[]> { |
| 97 | const mtime = await this.getMtime(path); |
| 98 | |
| 99 | if (this.fsMap.has(path) && this.mtimes.get(path) === mtime) { |
| 100 | return this.fsMap.get(path) as string[]; |
| 101 | } |
| 102 | const contents = (await readFile(path).catch(() => '')).toString(); |
| 103 | const decoded = |
| 104 | encoded && contents.length |
| 105 | ? await decrypt(contents, this.currentAESKey) |
| 106 | : contents; |
| 107 | const splitContents = decoded.length ? decoded.split('\n') : []; |
| 108 | |
| 109 | this.fsMap.set(path, splitContents); |
| 110 | if (mtime !== null) { |
| 111 | this.mtimes.set(path, mtime); |
| 112 | } else { |
| 113 | this.mtimes.delete(path); |
| 114 | } |
| 115 | |
| 116 | return splitContents; |
| 117 | } |
| 118 | |
| 119 | protected async getMtime(path: string): Promise<number | null> { |
| 120 | return stat(path).then( |