forked from freshframework/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv.ts
More file actions
64 lines (51 loc) · 1.78 KB
/
Copy pathkv.ts
File metadata and controls
64 lines (51 loc) · 1.78 KB
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
import { getFile, housekeep, isSupported, saveFile } from "./kvfs.ts";
const IS_CHUNK = /\/chunk-[a-zA-Z0-9]*.js/;
const DEPENDENCIES_SNAP = "dependencies.snap.json";
export const getDependencies = async () => {
const deps = await getFile(DEPENDENCIES_SNAP);
if (!deps) {
return null;
}
const json = await new Response(deps).json();
return new Map<string, string[]>(json);
};
export const saveDependencies = (deps: Map<string, string[]>) =>
saveFile(
DEPENDENCIES_SNAP,
new TextEncoder().encode(
JSON.stringify([...deps.entries()]),
),
);
export const saveSnapshot = async (
filesystem: Map<string, Uint8Array>,
dependencies: Map<string, string[]>,
) => {
if (!isSupported()) return;
// We need to save chunks first, islands/plugins last so we address esm.sh build instabilities
const chunksFirst = [...filesystem.keys()].sort((a, b) => {
const aIsChunk = IS_CHUNK.test(a);
const bIsChunk = IS_CHUNK.test(b);
const cmp = a > b ? 1 : a < b ? -1 : 0;
return aIsChunk && bIsChunk ? cmp : aIsChunk ? -10 : bIsChunk ? 10 : cmp;
});
let start = performance.now();
for (const path of chunksFirst) {
const content = filesystem.get(path);
if (content instanceof ReadableStream) {
console.info("streams are not yet supported on KVFS");
return;
}
if (content) await saveFile(path, content);
}
const deps = new Map<string, string[]>();
for (const dep of chunksFirst) {
deps.set(dep, dependencies.get(dep)!);
}
await saveDependencies(deps);
let dur = (performance.now() - start) / 1e3;
console.log(` 💾 Save bundle to Deno.KV: ${dur.toFixed(2)}s`);
start = performance.now();
await housekeep();
dur = (performance.now() - start) / 1e3;
console.log(` 🧹 Housekeep Deno.KV: ${dur.toFixed(2)}s`);
};