Enable Cloudflare cache

Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
Joe Ma 2022-06-02 14:13:54 +08:00
parent 29a6da681e
commit c9e3376d21
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
2 changed files with 47 additions and 25 deletions

View file

@ -24,8 +24,7 @@
</head> </head>
<body> <body>
<h3>Paste service - <a href="https://paste.nekoul.com">paste.nekoul.com</a></h3> <h2>Paste Service</h2>
<a href="https://nekoul.com">[Homepage]</a><a href="https://paste.nekoul.com/api">[API]</a>
<h4>Upload file</h4> <h4>Upload file</h4>
<form action="https://paste.nekoul.com" method="POST" enctype=multipart/form-data> <form action="https://paste.nekoul.com" method="POST" enctype=multipart/form-data>
<div> <div>
@ -63,6 +62,8 @@
document.getElementById("text_input").addEventListener("input", update_textarea, false); document.getElementById("text_input").addEventListener("input", update_textarea, false);
</script> </script>
<br><p>&copy; 2022 rikkaneko</p> <br>
<a href="https://nekoul.com">[Homepage]</a><a href="https://paste.nekoul.com/api">[API]</a>
<p>&copy; 2022 rikkaneko</p>
</body> </body>
</html> </html>

View file

@ -86,7 +86,11 @@ export default {
switch (method) { switch (method) {
// Fetch the HTML for uploading text/file // Fetch the HTML for uploading text/file
case "GET": { case "GET": {
return await fetch(PASTE_INDEX_HTML_URL).then(value => { return await fetch(PASTE_INDEX_HTML_URL, {
cf: {
cacheEverything: true
}
}).then(value => {
let res = new Response(value.body, value); let res = new Response(value.body, value);
// Add the correct content-type to response header // Add the correct content-type to response header
res.headers.set("content-type", "text/html; charset=UTF-8"); res.headers.set("content-type", "text/html; charset=UTF-8");
@ -208,32 +212,45 @@ export default {
switch (method) { switch (method) {
// Fetch the paste by uuid // Fetch the paste by uuid
case "GET": { case "GET": {
let res = await s3.fetch(`${env.ENDPOINT}/${uuid}`, { // Enable CF cache for authorized request
method: "GET" let cache = caches.default;
}); // Match in existing cache
// Stream request let res = await cache.match(request.url);
let {readable, writable} = new TransformStream(); if (res === undefined) {
if (res.body === null) { // Fetch form origin if not hit cache
// UUID exists in index but not found in remote object storage service let origin = await s3.fetch(`${env.ENDPOINT}/${uuid}`, {
return new Response("Internal server error.\n", { method: "GET"
status: 500
}); });
}
// Streaming request
res.body.pipeTo(writable);
// Handle response format res = new Response(origin.body, origin);
// Direct download // Remove x-amz-* headers
if (option === "download") { for (let [key, value] of res.headers.entries()) {
return new Response(readable, { if (key.startsWith("x-amz")) {
headers: { res.headers.delete(key);
"Content-Disposition": `attachment; filename="${encodeURIComponent(descriptor.title ?? uuid)}"`
} }
}); }
if (!res.ok) {
// UUID exists in index but not found in remote object storage service
return new Response("Internal server error.\n", {
status: 500
});
}
res.headers.append("Cache-Control", "max-age=3600");
if (option === "download") {
res.headers.append("Content-Disposition",
`attachment; filename="${encodeURIComponent(descriptor.title ?? uuid)}"`);
}
// res.body cannot be read twice
await cache.put(request.url, res.clone());
return res;
} }
// Default format // Cache hit
return new Response(readable); return res;
} }
// Delete paste by uuid // Delete paste by uuid
@ -252,6 +269,10 @@ export default {
await env.PASTE_INDEX.delete(uuid); await env.PASTE_INDEX.delete(uuid);
let counter = await env.PASTE_INDEX.get("__count__") || "1"; let counter = await env.PASTE_INDEX.get("__count__") || "1";
await env.PASTE_INDEX.put("__count__", (Number(counter) - 1).toString()); await env.PASTE_INDEX.put("__count__", (Number(counter) - 1).toString());
// Invalidate CF cache
let cache = caches.default;
await cache.delete(request.url);
return new Response("OK\n"); return new Response("OK\n");
} else { } else {
return new Response("Unable to process such request.\n", { return new Response("Unable to process such request.\n", {