-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
95 lines (82 loc) · 2.57 KB
/
index.js
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
import {
getAssetFromKV,
mapRequestToAsset,
} from "@cloudflare/kv-asset-handler";
/**
* The DEBUG flag will do two things that help during development:
* 1. we will skip caching on the edge, which makes it easier to
* debug.
* 2. we will return an error message on exception in your Response rather
* than the default 404.html page.
*/
const DEBUG = false;
addEventListener("fetch", (event) => {
try {
event.respondWith(handleEvent(event));
} catch (e) {
if (DEBUG) {
return event.respondWith(
new Response(e.message || e.toString(), {
status: 500,
})
);
}
event.respondWith(new Response("Internal Error", { status: 500 }));
}
});
async function handleEvent(event) {
const url = new URL(event.request.url);
let options = {};
try {
if (DEBUG) {
// customize caching
options.cacheControl = {
bypassCache: true,
};
}
const page = await getAssetFromKV(event, options);
// allow headers to be altered
let response = new Response(page.body, page);
response.headers.set("X-XSS-Protection", "1; mode=block");
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set("X-Frame-Options", "DENY");
response.headers.set("Referrer-Policy", "unsafe-url");
if (/\.avif$/.test(url)) {
response.headers.set("Content-Type", "image/avif");
response.headers.set("Content-Disposition", "inline");
}
if (
/\.(avif|bmp|css|gif|jpg|jpeg|js|png|svg|tif|tiff|webp)(\?.*)?$/.test(url)
) {
response.headers.set(
"Cache-Control",
"public, max-age=31536000, immutable"
);
}
const statsRequest = new Request(event.request);
// Offload stats from the main thread
statsRequest.headers.set("X-Original-Status-Code", response.status);
statsRequest.headers.set("X-Original-Url", url);
statsRequest.headers.set(
"X-Original-Ip",
event.request.headers.get("cf-connecting-ip")
);
event.waitUntil(fetch("https://dashflare.mre.workers.dev", statsRequest));
return response;
} catch (e) {
// if an error is thrown try to serve the asset at 404.html
if (!DEBUG) {
try {
let notFoundResponse = await getAssetFromKV(event, {
mapRequestToAsset: (req) =>
new Request(`${new URL(req.url).origin}/404.html`, req),
});
return new Response(notFoundResponse.body, {
...notFoundResponse,
status: 404,
});
} catch (e) {}
}
return new Response(e.message || e.toString(), { status: 500 });
}
}