feat: proxy cache control

This commit is contained in:
dsrkafuu 2022-03-13 20:08:22 +08:00
parent 0481c707b2
commit d49aea7eb0
4 changed files with 30 additions and 22 deletions

View File

@ -1,4 +1,4 @@
# Moe Counter
# Moe Counter CF
Fork of Moe Counter for fast global access powered by Cloudflare Workers.
@ -6,22 +6,22 @@ Fork of Moe Counter for fast global access powered by Cloudflare Workers.
## Demo
[![Gelbooru](https://count.dsrkafuu.net/dsrkafuu:demo?theme=gelbooru)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=gelbooru)
[![Gelbooru](https://count.dsrkafuu.net/dsrkafuu:demo?proxy=1)](https://count.dsrkafuu.net/dsrkafuu:demo)
<details>
<summary>More Themes and Customizations</summary>
**A-SOUL (with `theme=asoul&length=10`)**
[![A-SOUL](https://count.dsrkafuu.net/dsrkafuu:demo?theme=asoul&length=10&add=0)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=asoul&length=10)
[![A-SOUL](https://count.dsrkafuu.net/dsrkafuu:demo?theme=asoul&length=10&add=0&proxy=1)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=asoul&length=10)
**Moebooru (with `theme=moebooru&length=auto`)**
[![Moebooru](https://count.dsrkafuu.net/dsrkafuu:demo?theme=moebooru&length=auto&add=0)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=moebooru&length=auto)
[![Moebooru](https://count.dsrkafuu.net/dsrkafuu:demo?theme=moebooru&length=auto&add=0&proxy=1)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=moebooru&length=auto)
**Rule 34 (with `theme=rule34`)**
[![Rule 34](https://count.dsrkafuu.net/dsrkafuu:demo?theme=rule34&add=0)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=rule34)
[![Rule 34](https://count.dsrkafuu.net/dsrkafuu:demo?theme=rule34&add=0&proxy=1)](https://count.dsrkafuu.net/dsrkafuu:demo?theme=rule34)
</details>
@ -31,13 +31,14 @@ Fork of Moe Counter for fast global access powered by Cloudflare Workers.
```
https://count.dsrkafuu.net/{id}
https://count.dsrkafuu.net/{id}?theme={asoul,gelbooru,moebooru,rule34}&length={1-10,auto}&add={0,1}
https://count.dsrkafuu.net/{id}?theme={asoul,gelbooru,moebooru,rule34}&length={1-10,auto}&add={0,1}&proxy={0,1}
```
1. ID: Any custom string between 1-256 chars (`a-zA-Z0-9:.@_-` allowed)
2. Themes: `asoul`, `gelbooru`, `moebooru`, `rule34` and two other themes (default: `gelbooru`)
3. Length: Number between `1-10` (default: `7`) or string `auto`
4. Add: Controls whether make the counter count or not (default: `1`)
1. `{id}`: Any custom string between 1-256 chars (`a-zA-Z0-9:.@_-` allowed)
2. `{&theme}`: `asoul`, `gelbooru`, `moebooru`, `rule34` and two other themes (default: `gelbooru`)
3. `{&length}`: Number between `1-10` (default: `7`) or string `auto`
4. `{&add}`: Controls whether make the counter count or not (default: `1`)
5. `{&proxy}`: Should be used when showing images through proxies which break `no-cache` behavior, e.g. GitHub's image proxy, **DO NOT** use it when showing the counter directly in your website (default: `0`)
Recommend to use `user:usage` like string as ID for better management and potential issue tracking.
@ -76,6 +77,6 @@ DELETE is not enabled in public counter, create a issue if you need to use it.
This project and all contributors shall not be responsible for any dispute or loss caused by using this project.
This project is released under the `MIT` License, for more information read the [License](https://github.com/dsrkafuu/moe-counter/blob/master/LICENSE).
This project is released under the `MIT` License, for more information read the [License](https://github.com/dsrkafuu/moe-counter-cf/blob/master/LICENSE).
**Copyright (c) 2020 journey-ad, 2022 DSRKafuU**

View File

@ -1,6 +1,6 @@
{
"private": true,
"name": "moe-counter",
"name": "moe-counter-cf",
"version": "2.1.0",
"description": "Fork of Moe Counter for fast global access powered by Cloudflare Workers.",
"author": "DSRKafuU <dsrkafuu@outlook.com> (https://dsrkafuu.net)",

View File

@ -42,7 +42,7 @@ function genImage(count, theme, length) {
*/
export async function get(req, event) {
const id = validateID(req.params.id);
let { theme, length, add } = req.query;
let { theme, length, add, proxy } = req.query;
if (!themes[theme]) {
theme = 'gelbooru';
}
@ -53,18 +53,25 @@ export async function get(req, event) {
_length = 7;
}
// get times from KV
const count = (Number.parseInt(await KV.get(id)) || 0) + 1;
const image = genImage(count, theme, _length);
// set time asynchronously (no await)
// get times from KV and set time asynchronously (no await)
const count = Number.parseInt(await KV.get(id)) || 0;
let image;
if (add !== '0') {
event.waitUntil(KV.put(id, count.toString()));
image = genImage(count + 1, theme, _length);
event.waitUntil(KV.put(id, (count + 1).toString()));
} else {
image = genImage(count, theme, _length);
}
const headers = {
'Content-Type': 'image/svg+xml; charset=utf-8',
};
// make sure image refreshes after some url proxys like GitHub's Camo
if (proxy === '1') {
headers['Cache-Control'] = 'max-age=0, no-cache, no-store, must-revalidate';
}
return await genResponse(req, image, {
status: 200,
headers: {
'Content-Type': 'image/svg+xml; charset=utf-8',
},
headers,
});
}

View File

@ -1,4 +1,4 @@
name = "moe-counter"
name = "moe-counter-cf"
type = "javascript"
account_id = "<CF_ACCOUNT_ID>"
zone_id = "<CF_ZONE_ID>"