-
Notifications
You must be signed in to change notification settings - Fork 54
/
hashManager.mjs
58 lines (46 loc) 路 1.67 KB
/
hashManager.mjs
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
57
58
import fs from 'node:fs/promises';
import scraper from './scraper.mjs';
import readJson from './readJson.mjs';
const exportCache = await readJson(new URL('../data/cache/.export.json', import.meta.url));
const locales = await readJson(new URL('../config/locales.json', import.meta.url));
const exportKeyWhitelist = ['Manifest', 'DropChances', 'Patchlogs'];
class HashManager {
constructor() {
this.exportCache = Object.fromEntries(
exportKeyWhitelist.filter((key) => key in exportCache).map((key) => [key, exportCache[key]])
);
}
/**
* Compares DE's endpoint hashs between the cache and the last fetched ones
* @returns {boolean}
*/
isUptodate() {
const oldCacheEntries = Object.entries(exportCache);
const cacheEntries = Object.entries(this.exportCache);
const compareHashs = () =>
Object.entries(this.exportCache).every(([key, { hash }]) => hash === exportCache[key]?.hash);
return oldCacheEntries.length === cacheEntries.length && compareHashs();
}
async saveExportCache() {
await fs.writeFile(
new URL('../data/cache/.export.json', import.meta.url),
JSON.stringify(this.exportCache, undefined, 1)
);
}
async updateExportCache() {
const endpoints = [];
const allLocales = [...locales, 'en'];
for (let i = 0; i < allLocales.length; i += 1) {
const locale = allLocales[i];
endpoints.push(...(await scraper.fetchEndpoints(false, locale)));
}
endpoints
.flat()
.map((endpoint) => endpoint.split('!00_'))
.filter(([key, hash]) => key && hash)
.forEach(([key, hash]) => {
this.exportCache[key] = { hash };
});
}
}
export default new HashManager();