feat: add etag to static files (#95)

This commit is contained in:
Leo Kettmeir
2022-03-03 14:18:29 +01:00
committed by GitHub
parent 38ac19a042
commit d441bf2489
3 changed files with 48 additions and 9 deletions
+30 -9
View File
@@ -42,6 +42,8 @@ interface StaticFile {
size: number; size: number;
/** The content-type of the file. */ /** The content-type of the file. */
contentType: string; contentType: string;
/** Hash of the file contents. */
etag: string;
} }
export class ServerContext { export class ServerContext {
@@ -192,17 +194,27 @@ export class ServerContext {
includeDirs: false, includeDirs: false,
followSymlinks: false, followSymlinks: false,
}); });
const encoder = new TextEncoder();
for await (const entry of entires) { for await (const entry of entires) {
const localUrl = toFileUrl(entry.path); const localUrl = toFileUrl(entry.path);
const path = localUrl.href.substring(staticFolder.href.length); const path = localUrl.href.substring(staticFolder.href.length);
const stat = await Deno.stat(localUrl); const stat = await Deno.stat(localUrl);
const contentType = mediaTypeLookup(extname(path)) ?? const contentType = mediaTypeLookup(extname(path)) ??
"application/octet-stream"; "application/octet-stream";
const etag = await crypto.subtle.digest(
"SHA-1",
encoder.encode(BUILD_ID + path),
).then((hash) =>
Array.from(new Uint8Array(hash))
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("")
);
const staticFile: StaticFile = { const staticFile: StaticFile = {
localUrl, localUrl,
path, path,
size: stat.size, size: stat.size,
contentType, contentType,
etag,
}; };
staticFiles.push(staticFile); staticFiles.push(staticFile);
} }
@@ -253,12 +265,15 @@ export class ServerContext {
const routes: router.Routes = {}; const routes: router.Routes = {};
// Add the static file routes. // Add the static file routes.
for (const { localUrl, path, size, contentType } of this.#staticFiles) { for (
const { localUrl, path, size, contentType, etag } of this.#staticFiles
) {
const route = sanitizePathToRegex(path); const route = sanitizePathToRegex(path);
routes[`GET@${route}`] = this.#staticFileHandler( routes[`GET@${route}`] = this.#staticFileHandler(
localUrl, localUrl,
size, size,
contentType, contentType,
etag,
); );
} }
@@ -420,15 +435,21 @@ export class ServerContext {
localUrl: URL, localUrl: URL,
size: number, size: number,
contentType: string, contentType: string,
etag: string,
): router.MatchHandler { ): router.MatchHandler {
return async (_req: Request) => { return async (req: Request) => {
const resp = await fetch(localUrl); if (req.headers.get("if-none-match") === etag) {
return new Response(resp.body, { return new Response(null, { status: 304 });
headers: { } else {
"content-type": contentType, const resp = await fetch(localUrl);
"content-length": String(size), return new Response(resp.body, {
}, headers: {
}); "content-type": contentType,
"content-length": String(size),
"etag": etag,
},
});
}
}; };
} }
+1
View File
@@ -0,0 +1 @@
bar
+17
View File
@@ -182,3 +182,20 @@ Deno.test("/foo/:path*", async () => {
const body = await resp.text(); const body = await resp.text();
assert(body.includes("bar/baz")); assert(body.includes("bar/baz"));
}); });
Deno.test("static file", async () => {
const resp = await router(new Request("https://fresh.deno.dev/foo.txt"));
assertEquals(resp.status, 200);
const body = await resp.text();
assert(body.startsWith("bar"));
assert(resp.headers.get("etag"));
const resp2 = await router(
new Request("https://fresh.deno.dev/foo.txt", {
headers: {
"if-none-match": resp.headers.get("etag")!,
},
}),
);
assertEquals(resp2.status, 304);
});