-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathbuild_cache.ts
More file actions
134 lines (115 loc) · 3.77 KB
/
build_cache.ts
File metadata and controls
134 lines (115 loc) · 3.77 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as path from "@std/path";
import { getSnapshotPath, type ResolvedFreshConfig } from "./config.ts";
import { DENO_DEPLOYMENT_ID, setBuildId } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
export interface FileSnapshot {
generated: boolean;
hash: string | null;
}
export interface BuildSnapshot {
version: number;
buildId: string;
staticFiles: Record<string, FileSnapshot>;
islands: Record<string, string>;
}
export interface StaticFile {
hash: string | null;
size: number;
readable: ReadableStream<Uint8Array> | Uint8Array;
close(): void;
}
export interface BuildCache {
hasSnapshot: boolean;
readFile(pathname: string): Promise<StaticFile | null>;
getIslandChunkName(islandName: string): string | null;
}
export class ProdBuildCache implements BuildCache {
static fromSnapshot(config: ResolvedFreshConfig, islandCount: number) {
const snapshotPath = getSnapshotPath(config);
const staticFiles = new Map<string, FileSnapshot>();
const islandToChunk = new Map<string, string>();
let hasSnapshot = false;
try {
const content = Deno.readTextFileSync(snapshotPath);
const snapshot = JSON.parse(content) as BuildSnapshot;
hasSnapshot = true;
setBuildId(snapshot.buildId);
const files = Object.keys(snapshot.staticFiles);
for (let i = 0; i < files.length; i++) {
const pathname = files[i];
const info = snapshot.staticFiles[pathname];
staticFiles.set(pathname, info);
}
const islands = Object.keys(snapshot.islands);
for (let i = 0; i < islands.length; i++) {
const pathname = islands[i];
islandToChunk.set(pathname, snapshot.islands[pathname]);
}
if (!DENO_DEPLOYMENT_ID) {
// deno-lint-ignore no-console
console.log(
`Found snapshot at ${colors.cyan(snapshotPath)}`,
);
}
} catch (err) {
if ((err instanceof Deno.errors.NotFound)) {
if (islandCount > 0) {
throw new Error(
`Found ${
colors.green(`${islandCount} islands`)
}, but did not find build snapshot at:\n${
colors.red(snapshotPath)
}.\n\nMaybe your forgot to run ${
colors.cyan("deno task build")
} before starting the production server\nor maybe you wanted to run ${
colors.cyan("deno task dev")
} to spin up a development server instead?\n`,
);
}
} else {
throw err;
}
}
return new ProdBuildCache(config, islandToChunk, staticFiles, hasSnapshot);
}
#islands: Map<string, string>;
#fileInfo: Map<string, FileSnapshot>;
#config: ResolvedFreshConfig;
constructor(
config: ResolvedFreshConfig,
islands: Map<string, string>,
files: Map<string, FileSnapshot>,
public hasSnapshot: boolean,
) {
this.#islands = islands;
this.#fileInfo = files;
this.#config = config;
}
async readFile(pathname: string): Promise<StaticFile | null> {
const info = this.#fileInfo.get(pathname);
if (info === undefined) return null;
const base = info.generated
? this.#config.buildOutDir
: this.#config.staticDir;
const filePath = info.generated
? path.join(base, "static", pathname)
: path.join(base, pathname);
// Check if path resolves outside of intended directory.
if (path.relative(base, filePath).startsWith("..")) {
return null;
}
const [stat, file] = await Promise.all([
Deno.stat(filePath),
Deno.open(filePath),
]);
return {
hash: info.hash,
size: stat.size,
readable: file.readable,
close: () => file.close(),
};
}
getIslandChunkName(islandName: string): string | null {
return this.#islands.get(islandName) ?? null;
}
}