-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
60 lines (57 loc) · 2.17 KB
/
mod.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
import { Handler, RequestEvent } from "https://deno.land/x/[email protected]/mod.ts";
import * as base64 from "https://deno.land/[email protected]/encoding/base64.ts";
const encoder = new TextEncoder();
const def = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
export async function entityTag(entity: Uint8Array) {
if (!entity) return def;
if (entity.length === 0) return def;
const digest = await crypto.subtle.digest("SHA-1", entity);
const hash = base64.encode(digest).substring(0, 27);
return `"${entity.length.toString(16)}-${hash}"`;
}
export const etag = <T extends RequestEvent = RequestEvent>(
opts: { weak?: boolean } = {},
): Handler<T> =>
async (rev, next) => {
const weak = opts.weak !== false;
const { response, request, respondWith } = rev;
const sendEtag = async function (body: any) {
try {
let fname = "noop";
if (typeof body === "object") {
if (body instanceof Response) return respondWith(body);
if (
body instanceof ReadableStream ||
body instanceof FormData ||
body instanceof Blob ||
typeof (body as unknown as Deno.Reader).read === "function"
) {
return respondWith(new Response(body, rev.responseInit));
} else if (body instanceof Uint8Array) {
fname = "Uint8Array";
} else {
body = JSON.stringify(body);
fname = "json";
}
}
if (!response.header("ETag")) {
const etag = await entityTag(
fname === "Uint8Array" ? body : encoder.encode(body),
);
response.header("ETag", weak ? `W/${etag}` : etag);
}
if (request.headers.get("if-none-match") === response.header("ETag")) {
response.status(304);
return respondWith(new Response(void 0, rev.responseInit));
}
if (fname === "json") {
response.header("content-type", "application/json; charset=utf-8");
}
return respondWith(new Response(body, rev.responseInit));
} catch (_e) {
return respondWith(new Response(body, rev.responseInit));
}
};
rev.response.send = sendEtag as any;
return next();
};