mirror of
https://github.com/rikkaneko/paste.git
synced 2025-08-03 12:40:07 +01:00
Bypass script to get cached response faster
Use `ctx.waitUntil` to avoid blocking on updating index Reply 404 for /favicon.ico Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
parent
7c4aacd952
commit
a26f627149
1 changed files with 29 additions and 9 deletions
38
src/index.ts
38
src/index.ts
|
@ -74,12 +74,31 @@ export default {
|
||||||
const {pathname} = new URL(url);
|
const {pathname} = new URL(url);
|
||||||
const path = pathname.replace(/\/+$/, "") || "/";
|
const path = pathname.replace(/\/+$/, "") || "/";
|
||||||
let cache = caches.default;
|
let cache = caches.default;
|
||||||
|
// Bypass script to get cached response faster
|
||||||
|
{
|
||||||
|
if (method == "GET") {
|
||||||
|
let cached = await cache.match(url);
|
||||||
|
if (cached !== undefined) {
|
||||||
|
let {readable, writable} = new TransformStream();
|
||||||
|
cached.body!.pipeTo(writable);
|
||||||
|
return new Response(readable, cached);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
const s3 = new AwsClient({
|
const s3 = new AwsClient({
|
||||||
accessKeyId: env.AWS_ACCESS_KEY_ID,
|
accessKeyId: env.AWS_ACCESS_KEY_ID,
|
||||||
secretAccessKey: env.AWS_SECRET_ACCESS_KEY
|
secretAccessKey: env.AWS_SECRET_ACCESS_KEY
|
||||||
});
|
});
|
||||||
|
|
||||||
// Special path
|
// Special path
|
||||||
|
if (path === "/favicon.ico" && method == "GET") {
|
||||||
|
return new Response(null, {
|
||||||
|
status: 404
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (path === "/api" && method == "GET") {
|
if (path === "/api" && method == "GET") {
|
||||||
return new Response(API_SPEC_TEXT);
|
return new Response(API_SPEC_TEXT);
|
||||||
}
|
}
|
||||||
|
@ -169,11 +188,10 @@ export default {
|
||||||
last_modified: Date.now()
|
last_modified: Date.now()
|
||||||
};
|
};
|
||||||
|
|
||||||
const p1 = env.PASTE_INDEX.get("__count__").then(counter => {
|
ctx.waitUntil(env.PASTE_INDEX.get("__count__").then(counter => {
|
||||||
env.PASTE_INDEX.put("__count__", (Number(counter ?? "0") + 1).toString());
|
env.PASTE_INDEX.put("__count__", (Number(counter ?? "0") + 1).toString());
|
||||||
});
|
}));
|
||||||
const p2 = env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor));
|
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor)));
|
||||||
await Promise.all([p1, p2]);
|
|
||||||
return new Response(get_paste_info(uuid, descriptor));
|
return new Response(get_paste_info(uuid, descriptor));
|
||||||
} else {
|
} else {
|
||||||
return new Response("Unable to upload the paste.\n", {
|
return new Response("Unable to upload the paste.\n", {
|
||||||
|
@ -260,7 +278,8 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
// res.body cannot be read twice
|
// res.body cannot be read twice
|
||||||
await cache.put(request.url, res.clone());
|
// Do not block when writing to cache
|
||||||
|
ctx.waitUntil(cache.put(url, res.clone()));
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,12 +302,13 @@ export default {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
await env.PASTE_INDEX.delete(uuid);
|
ctx.waitUntil(env.PASTE_INDEX.delete(uuid));
|
||||||
const counter = await env.PASTE_INDEX.get("__count__") || "1";
|
ctx.waitUntil(env.PASTE_INDEX.get("__count__").then(counter => {
|
||||||
await env.PASTE_INDEX.put("__count__", (Number(counter) - 1).toString());
|
env.PASTE_INDEX.put("__count__", (Number(counter ?? "1") - 1).toString())
|
||||||
|
}));
|
||||||
|
|
||||||
// Invalidate CF cache
|
// Invalidate CF cache
|
||||||
await cache.delete(request.url);
|
ctx.waitUntil(cache.delete(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", {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue