/
serve_dist.ts
63 lines (62 loc) · 2.51 KB
/
serve_dist.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
import { readableStreamFromReader } from "https://deno.land/std@0.136.0/streams/conversion.ts";
import log from "../lib/log.ts";
import { builtinModuleExts, getDeploymentId, regFullVersion } from "./helpers.ts";
import type { AlephConfig } from "./types.ts";
export default {
test: (pathname: string) => {
return pathname.startsWith("/-/") ||
(builtinModuleExts.find((ext) => pathname.endsWith(`.${ext}`)) && !pathname.endsWith(".d.ts")) ||
pathname.endsWith(".css");
},
fetch: async (req: Request): Promise<Response> => {
const config: AlephConfig | undefined = Reflect.get(globalThis, "__ALEPH_CONFIG");
const outputDir = config?.build?.outputDir ?? "dist";
const { pathname, searchParams } = new URL(req.url);
try {
let filePath = `./${outputDir}${pathname}`;
let ctype = "application/javascript; charset=utf-8";
if (
searchParams.has("module") ||
(pathname.startsWith("/-/esm.sh/") && !pathname.endsWith(".js") && !pathname.endsWith(".css"))
) {
filePath += ".js";
}
if (pathname.endsWith(".css") && !searchParams.has("module")) {
ctype = "text/css; charset=utf-8";
}
const headers = new Headers({ "Content-Type": ctype });
const deployId = getDeploymentId();
let etag: string | null = null;
if (deployId) {
etag = `W/${btoa(pathname).replace(/[^a-z0-9]/g, "")}-${deployId}`;
} else {
const stat = await Deno.lstat(filePath);
if (!stat.isFile) {
return new Response("File Not Found", { status: 404 });
}
const { mtime, size } = stat;
if (mtime) {
etag = `W/${mtime.getTime().toString(16)}-${size.toString(16)}`;
headers.append("Last-Modified", new Date(mtime).toUTCString());
}
}
if (etag && req.headers.get("If-None-Match") === etag) {
return new Response(null, { status: 304 });
}
const file = await Deno.open(filePath, { read: true });
if (etag) {
headers.append("Etag", etag);
}
if (searchParams.get("v") || (pathname.startsWith("/-/") && regFullVersion.test(pathname))) {
headers.append("Cache-Control", "public, max-age=31536000, immutable");
}
return new Response(readableStreamFromReader(file), { headers });
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return new Response("File Not Found", { status: 404 });
}
log.error(err);
return new Response("Internal Server Error", { status: 500 });
}
},
};