-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
183 lines (150 loc) · 4.51 KB
/
server.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { dirname } from "std/path/dirname.ts";
import { contentType } from "std/media_types/mod.ts";
import { extname } from "std/path/extname.ts";
import { join } from "std/path/join.ts";
import { createHash } from "npm:sha256-uint8array";
const emblemFileNames = [
"a.png",
"b.png",
"c.png",
];
async function pickRandomEmblem() {
const filename =
emblemFileNames[Math.floor(Math.random() * emblemFileNames.length)];
const file = await Deno.open(
join("willowtest", "named_assets", "emblems", filename),
);
return file.readable;
}
Deno.serve(async (req) => {
// Check built assets for matching pathname.
const url = new URL(req.url);
if (url.pathname === "/emblem.png") {
return new Response(await pickRandomEmblem(), {
headers: {
"Content-Type": "image/png",
},
});
}
try {
const extension = extname(url.pathname);
// Special carve-out for RSS feeds.
if (extension === ".xml") {
const filePath = join(
".",
"willowtest",
"build",
url.pathname,
);
const etag = createHash().update(await Deno.readTextFile(filePath))
.digest("hex");
const ifNoneMatch = req.headers.get("If-None-Match");
if (
ifNoneMatch && (ifNoneMatch === `W/${etag}` || ifNoneMatch === etag)
) {
console.log(304, "RSS", url.pathname);
return new Response(undefined, {
status: 304,
});
}
const file = await Deno.open(filePath);
console.log(200, "RSS", url.pathname);
return new Response(file.readable, {
headers: {
"Content-Type": "application/rss+xml",
"ETag": etag,
"Cache-Control": "s-max-age=3600,max-age=3600,public",
},
});
}
const longCacheDirs = ["/assets", "/previews"];
// Content-addressed items get a long-lived cache policy
if (longCacheDirs.includes(dirname(url.pathname))) {
const filePath = join(
".",
"willowtest",
"build",
url.pathname,
);
const file = await Deno.open(filePath);
const contentKind = contentType(extension) || "text/plain";
console.log(200, "content-addressed", url.pathname);
return new Response(file.readable, {
headers: {
"Content-Type": contentKind,
"Cache-Control": "s-max-age=31536000,max-age=31536000,public",
},
});
}
// All HTML assets have reasonable cache policies
// And ETags for clients to revalidate with
const filePath = !extension
? join(
".",
"willowtest",
"build",
url.pathname,
"index.html",
)
: join(".", "willowtest", "build", url.pathname);
const etagPath = !extension
? join(".", "willowtest", "build", url.pathname, "etag")
: join(".", "willowtest", "build", dirname(url.pathname), "etag");
try {
const etag = await Deno.readTextFile(etagPath);
const ifNoneMatch = req.headers.get("If-None-Match");
if (
ifNoneMatch && (ifNoneMatch === `W/${etag}` || ifNoneMatch === etag)
) {
console.log(304, url.pathname);
return new Response(undefined, {
status: 304,
});
}
try {
const fileBytes = await Deno.readFile(filePath);
const contentKind = extension === ""
? "text/html"
: contentType(extension) || "text/plain";
return new Response(fileBytes, {
headers: {
"Content-Type": contentKind,
"ETag": etag,
"Cache-Control": "s-max-age=3600,max-age=3600,public",
},
});
} catch {
console.log(404, url.pathname);
// No file at this path.
return new Response("Page not found", {
status: 404,
});
}
} catch {
// No etag.
try {
const fileBytes = await Deno.readFile(filePath);
const contentKind = extension === ""
? "text/html"
: contentType(extension) || "text/plain";
return new Response(fileBytes, {
headers: {
"Content-Type": contentKind,
"Cache-Control": "s-max-age=3600,max-age=3600,public",
},
});
} catch {
console.log(404, url.pathname);
// No file at this path.
return new Response("Page not found", {
status: 404,
});
}
}
} catch {
// Make a nicer 404 page with the macro system.
return new Response("Page not found", {
status: 404,
});
}
});