oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/js/canonical.txt
1.1 KiB, 1 run
created by r1870400018:12000, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | import { readFile, writeFile } from 'fs/promises'; |
| 2 | import { join } from 'path'; |
| 3 | |
| 4 | const DEFAULT_ENCODING = 'utf-8'; |
| 5 | |
| 6 | class ConfigStore { |
| 7 | constructor(basePath) { |
| 8 | this.basePath = basePath; |
| 9 | this.cache = new Map(); |
| 10 | } |
| 11 | |
| 12 | async load(name) { |
| 13 | const filePath = join(this.basePath, `${name}.json`); |
| 14 | try { |
| 15 | const raw = await readFile(filePath, DEFAULT_ENCODING); |
| 16 | const data = JSON.parse(raw); |
| 17 | this.cache.set(name, data); |
| 18 | return data; |
| 19 | } catch (err) { |
| 20 | if (err.code === 'ENOENT') { |
| 21 | return null; |
| 22 | } |
| 23 | throw err; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | async save(name, data) { |
| 28 | const filePath = join(this.basePath, `${name}.json`); |
| 29 | const raw = JSON.stringify(data, null, 2); |
| 30 | await writeFile(filePath, raw, DEFAULT_ENCODING); |
| 31 | this.cache.set(name, data); |
| 32 | } |
| 33 | |
| 34 | get(name) { |
| 35 | return this.cache.get(name) ?? null; |
| 36 | } |
| 37 | |
| 38 | has(name) { |
| 39 | return this.cache.has(name); |
| 40 | } |
| 41 | |
| 42 | keys() { |
| 43 | return [...this.cache.keys()]; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | export default ConfigStore; |