| 139 | } |
| 140 | |
| 141 | private async write(url: string, text: string): Promise<void> { |
| 142 | await mkdir(this.cacheDir, { recursive: true }); |
| 143 | |
| 144 | const file = this.cacheFile(url); |
| 145 | const existing = await readFile(file, "utf8").catch(() => null); |
| 146 | if (existing !== text) { |
| 147 | await writeFile(file, text, "utf8"); |
| 148 | } |
| 149 | |
| 150 | const index = await this.readIndex(); |
| 151 | if (index[this.cacheKey(url)] !== url) { |
| 152 | index[this.cacheKey(url)] = url; |
| 153 | await writeFile( |
| 154 | this.indexFile(), |
| 155 | `${JSON.stringify(sortKeys(index), null, 2)}\n`, |
| 156 | "utf8" |
| 157 | ); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private async read(url: string): Promise<{ text: string; cachedAt?: string } | null> { |
| 162 | const file = this.cacheFile(url); |