Skip to content

Commit 5b28a8c

Browse files
committed
refactor: split out sanitize
1 parent 46e38c9 commit 5b28a8c

File tree

2 files changed

+107
-106
lines changed

2 files changed

+107
-106
lines changed

src/cloudflare-worker/handler.ts

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { Router } from "itty-router";
2-
import {
3-
ActivityExtension,
4-
AnimationExtension,
5-
Config,
6-
FontExtension,
7-
Generator,
8-
RemoteStyleExtension,
9-
ThemeExtension,
10-
} from "../core";
2+
import { Config, Generator } from "../core";
113
import { Cache } from "./cache";
124
import demo from "./demo";
135
import Header from "./headers";
14-
import { booleanize, normalize } from "./utils";
6+
import { sanitize } from "./sanitize";
157

168
const router = Router();
179

@@ -22,102 +14,6 @@ router.get("/favicon.ico", async () => {
2214
);
2315
});
2416

25-
function sanitize(config: Record<string, string>): Config {
26-
const sanitized: Config = {
27-
username: "jacoblincool",
28-
site: "us",
29-
width: 500,
30-
height: 200,
31-
css: [],
32-
extensions: [FontExtension, AnimationExtension, ThemeExtension],
33-
font: "baloo_2",
34-
animation: true,
35-
theme: { light: "light", dark: "dark" },
36-
cache: 60,
37-
};
38-
39-
if (!config.username?.trim()) {
40-
throw new Error("Missing username");
41-
}
42-
sanitized.username = config.username.trim();
43-
44-
// #region backward compatibility
45-
if (config.border_radius) {
46-
const size = parseFloat(config.border_radius) ?? 1;
47-
sanitized.css.push(`#background{rx:${size}px}`);
48-
}
49-
50-
if (config.show_rank && booleanize(config.show_rank) === false) {
51-
sanitized.css.push(`#ranking{display:none}`);
52-
}
53-
// #endregion
54-
55-
if (config.site?.trim().toLowerCase() === "cn") {
56-
sanitized.site = "cn";
57-
}
58-
59-
if (config.width?.trim()) {
60-
sanitized.width = parseInt(config.width.trim()) ?? 500;
61-
}
62-
63-
if (config.height?.trim()) {
64-
sanitized.height = parseInt(config.height.trim()) ?? 200;
65-
}
66-
67-
if (config.theme?.trim()) {
68-
const themes = config.theme.trim().split(",");
69-
if (themes.length === 1 || themes[1] === "") {
70-
sanitized.theme = themes[0].trim();
71-
} else {
72-
sanitized.theme = { light: themes[0].trim(), dark: themes[1].trim() };
73-
}
74-
}
75-
76-
if (config.font?.trim()) {
77-
sanitized.font = normalize(config.font.trim());
78-
}
79-
80-
if (config.animation?.trim()) {
81-
sanitized.animation = booleanize(config.animation.trim());
82-
}
83-
84-
if (config.ext === "activity" || config.extension === "activity") {
85-
sanitized.extensions.push(ActivityExtension);
86-
}
87-
88-
if (config.border) {
89-
const size = parseFloat(config.border) ?? 1;
90-
sanitized.extensions.push(() => (generator, data, body, styles) => {
91-
styles.push(
92-
`#background{stroke-width:${size};width:${generator.config.width - size}px;height:${
93-
generator.config.height - size
94-
}px;transform:translate(${size / 2}px,${size / 2}px)}`,
95-
);
96-
});
97-
}
98-
99-
if (config.radius) {
100-
const size = parseFloat(config.radius) ?? 1;
101-
sanitized.css.push(`#background{rx:${size}px}`);
102-
}
103-
104-
if (config.hide) {
105-
const targets = config.hide.split(",").map((x) => x.trim());
106-
sanitized.css.push(...targets.map((x) => `#${x}{display:none}`));
107-
}
108-
109-
if (config.sheets) {
110-
sanitized.sheets = config.sheets.split(",").map((x) => x.trim());
111-
sanitized.extensions.push(RemoteStyleExtension);
112-
}
113-
114-
if (config.cache && parseInt(config.cache) >= 0 && parseInt(config.cache) <= 60 * 60 * 24 * 7) {
115-
sanitized.cache = parseInt(config.cache);
116-
}
117-
118-
return sanitized;
119-
}
120-
12117
async function generate(config: Record<string, string>): Promise<Response> {
12218
let sanitized: Config;
12319
try {

src/cloudflare-worker/sanitize.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import {
2+
ActivityExtension,
3+
AnimationExtension,
4+
Config,
5+
FontExtension,
6+
RemoteStyleExtension,
7+
ThemeExtension,
8+
} from "../core";
9+
import { booleanize, normalize } from "./utils";
10+
11+
export function sanitize(config: Record<string, string>): Config {
12+
const sanitized: Config = {
13+
username: "jacoblincool",
14+
site: "us",
15+
width: 500,
16+
height: 200,
17+
css: [],
18+
extensions: [FontExtension, AnimationExtension, ThemeExtension],
19+
font: "baloo_2",
20+
animation: true,
21+
theme: { light: "light", dark: "dark" },
22+
cache: 60,
23+
};
24+
25+
if (!config.username?.trim()) {
26+
throw new Error("Missing username");
27+
}
28+
sanitized.username = config.username.trim();
29+
30+
// #region backward compatibility
31+
if (config.border_radius) {
32+
const size = parseFloat(config.border_radius) ?? 1;
33+
sanitized.css.push(`#background{rx:${size}px}`);
34+
}
35+
36+
if (config.show_rank && booleanize(config.show_rank) === false) {
37+
sanitized.css.push(`#ranking{display:none}`);
38+
}
39+
// #endregion
40+
41+
if (config.site?.trim().toLowerCase() === "cn") {
42+
sanitized.site = "cn";
43+
}
44+
45+
if (config.width?.trim()) {
46+
sanitized.width = parseInt(config.width.trim()) ?? 500;
47+
}
48+
49+
if (config.height?.trim()) {
50+
sanitized.height = parseInt(config.height.trim()) ?? 200;
51+
}
52+
53+
if (config.theme?.trim()) {
54+
const themes = config.theme.trim().split(",");
55+
if (themes.length === 1 || themes[1] === "") {
56+
sanitized.theme = themes[0].trim();
57+
} else {
58+
sanitized.theme = { light: themes[0].trim(), dark: themes[1].trim() };
59+
}
60+
}
61+
62+
if (config.font?.trim()) {
63+
sanitized.font = normalize(config.font.trim());
64+
}
65+
66+
if (config.animation?.trim()) {
67+
sanitized.animation = booleanize(config.animation.trim());
68+
}
69+
70+
if (config.ext === "activity" || config.extension === "activity") {
71+
sanitized.extensions.push(ActivityExtension);
72+
}
73+
74+
if (config.border) {
75+
const size = parseFloat(config.border) ?? 1;
76+
sanitized.extensions.push(() => (generator, data, body, styles) => {
77+
styles.push(
78+
`#background{stroke-width:${size};width:${generator.config.width - size}px;height:${
79+
generator.config.height - size
80+
}px;transform:translate(${size / 2}px,${size / 2}px)}`,
81+
);
82+
});
83+
}
84+
85+
if (config.radius) {
86+
const size = parseFloat(config.radius) ?? 4;
87+
sanitized.css.push(`#background{rx:${size}px}`);
88+
}
89+
90+
if (config.hide) {
91+
const targets = config.hide.split(",").map((x) => x.trim());
92+
sanitized.css.push(...targets.map((x) => `#${x}{display:none}`));
93+
}
94+
95+
if (config.sheets) {
96+
sanitized.sheets = config.sheets.split(",").map((x) => x.trim());
97+
sanitized.extensions.push(RemoteStyleExtension);
98+
}
99+
100+
if (config.cache && parseInt(config.cache) >= 0 && parseInt(config.cache) <= 60 * 60 * 24 * 7) {
101+
sanitized.cache = parseInt(config.cache);
102+
}
103+
104+
return sanitized;
105+
}

0 commit comments

Comments
 (0)