/
cache.ts
82 lines (67 loc) 路 1.96 KB
/
cache.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// deno-lint-ignore-file no-explicit-any
import { emptyDir, ensureDir } from "../deps/fs.ts";
import { posix } from "../deps/path.ts";
import { crypto } from "../deps/crypto.ts";
import { encode } from "../deps/hex.ts";
export interface Options {
/** The folder to load the files from */
folder?: string;
}
/**
* Class to cache the content transformations (like imagick manipulations)
*/
export default class Cache {
#folder: string;
constructor(options: Options = {}) {
this.#folder = options.folder || "_cache";
}
async set(
content: string | Uint8Array,
key: any,
result: string | Uint8Array,
): Promise<void> {
const [dir, file] = await paths(content, key);
await ensureDir(posix.join(this.#folder, dir));
if (typeof result === "string") {
await Deno.writeTextFile(posix.join(this.#folder, dir, file), result);
} else {
await Deno.writeFile(posix.join(this.#folder, dir, file), result);
}
}
async get(content: any, key: any): Promise<Uint8Array | undefined> {
const [dir, file] = await paths(content, key);
try {
return await Deno.readFile(posix.join(this.#folder, dir, file));
} catch {
// Ignore
}
}
async getText(content: any, key: any): Promise<string | undefined> {
const [dir, file] = await paths(content, key);
try {
return await Deno.readTextFile(posix.join(this.#folder, dir, file));
} catch {
// Ignore
}
}
async clear(): Promise<void> {
await emptyDir(this.#folder);
}
}
function paths(
content: string | Uint8Array,
key: any,
): Promise<[string, string]> {
return Promise.all([
hash(content),
hash(JSON.stringify(key)),
]);
}
async function hash(content: string | Uint8Array): Promise<string> {
const hash = await crypto.subtle.digest(
"MD5",
typeof content === "string" ? new TextEncoder().encode(content) : content,
);
const hex = encode(new Uint8Array(hash));
return new TextDecoder().decode(hex);
}