forked from idrinth-api-bench/issues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.ts
56 lines (54 loc) · 1.27 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
writeFileSync,
readFileSync,
existsSync,
mkdirSync,
rmdirSync,
} from 'fs';
import fsExtra from 'fs-extra/esm';
import {
tmpdir,
} from 'os';
import {
createHash,
} from 'crypto';
import {
sep,
} from 'path';
import language from './helper/language.js';
import {
FRAMEWORK_ROOT,
} from './constants.js';
const hash = createHash('sha256',)
.update(FRAMEWORK_ROOT,)
.digest('hex',);
const id = hash + process.pid;
const cachefolder: string = tmpdir() + sep + 'api-bench' + sep + '_' + id;
export default {
get(key: string, defaulted: string,): string {
if (! key.match(/^[a-z0-9.]+$/u,)) {
throw new Error(language('invalid_key', key,),);
}
if (! existsSync(cachefolder + sep + key,)) {
return defaulted;
}
return readFileSync(cachefolder + sep + key,) + '';
},
set(key: string, value: string,): void {
if (! key.match(/^[a-z0-9.]+$/u,)) {
throw new Error(language('invalid_key', key,),);
}
if (! existsSync(cachefolder,)) {
mkdirSync(cachefolder, {
recursive: true,
},);
}
writeFileSync(cachefolder + sep + key, value,);
},
async clean(): Promise<void> {
if (existsSync(cachefolder,)) {
await fsExtra.emptyDir(cachefolder,);
rmdirSync(cachefolder,);
}
},
};