2022-09-24 19:20:52 +08:00
|
|
|
import { renderTopLanguages } from "../src/cards/top-languages-card.js";
|
2022-09-24 20:10:06 +08:00
|
|
|
import { blacklist } from "../src/common/blacklist.js";
|
2022-09-24 16:20:54 +08:00
|
|
|
import {
|
2020-07-21 20:34:58 +08:00
|
|
|
clampValue,
|
|
|
|
CONSTANTS,
|
2022-09-24 19:20:52 +08:00
|
|
|
parseArray,
|
|
|
|
parseBoolean,
|
|
|
|
renderError,
|
|
|
|
} from "../src/common/utils.js";
|
2022-09-24 20:10:06 +08:00
|
|
|
import { fetchTopLanguages } from "../src/fetchers/top-languages-fetcher.js";
|
2022-09-24 19:20:52 +08:00
|
|
|
import { isLocaleAvailable } from "../src/translations.js";
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2022-09-24 16:20:54 +08:00
|
|
|
export default async (req, res) => {
|
2020-07-21 19:33:05 +08:00
|
|
|
const {
|
|
|
|
username,
|
2020-07-23 22:17:04 +08:00
|
|
|
hide,
|
2020-07-21 20:34:58 +08:00
|
|
|
hide_title,
|
2020-07-30 21:49:03 +08:00
|
|
|
hide_border,
|
2020-07-21 19:33:05 +08:00
|
|
|
card_width,
|
|
|
|
title_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
|
|
|
theme,
|
|
|
|
cache_seconds,
|
2020-07-30 21:49:03 +08:00
|
|
|
layout,
|
2020-09-12 21:16:18 +08:00
|
|
|
langs_count,
|
2020-09-27 15:09:44 +08:00
|
|
|
exclude_repo,
|
2023-04-25 14:19:05 +08:00
|
|
|
size_weight,
|
|
|
|
count_weight,
|
2020-09-27 15:40:39 +08:00
|
|
|
custom_title,
|
2020-10-04 16:05:15 +08:00
|
|
|
locale,
|
2021-04-28 02:58:44 +08:00
|
|
|
border_radius,
|
|
|
|
border_color,
|
2023-01-16 19:30:39 +08:00
|
|
|
disable_animations,
|
2023-02-16 09:23:11 +08:00
|
|
|
hide_progress,
|
2020-07-21 19:33:05 +08:00
|
|
|
} = req.query;
|
|
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
|
|
|
|
2020-08-17 16:48:17 +08:00
|
|
|
if (blacklist.includes(username)) {
|
|
|
|
return res.send(renderError("Something went wrong"));
|
|
|
|
}
|
|
|
|
|
2020-10-04 16:05:15 +08:00
|
|
|
if (locale && !isLocaleAvailable(locale)) {
|
2021-07-11 21:58:06 +08:00
|
|
|
return res.send(renderError("Something went wrong", "Locale not found"));
|
2020-10-04 16:05:15 +08:00
|
|
|
}
|
|
|
|
|
2023-06-05 18:10:22 +08:00
|
|
|
if (
|
|
|
|
layout !== undefined &&
|
|
|
|
(typeof layout !== "string" ||
|
|
|
|
!["compact", "normal", "donut", "donut-vertical", "pie"].includes(layout))
|
|
|
|
) {
|
|
|
|
return res.send(
|
|
|
|
renderError("Something went wrong", "Incorrect layout input"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:33:05 +08:00
|
|
|
try {
|
2021-10-10 23:11:40 +08:00
|
|
|
const topLangs = await fetchTopLanguages(
|
2020-09-27 15:09:44 +08:00
|
|
|
username,
|
|
|
|
parseArray(exclude_repo),
|
2023-04-25 14:19:05 +08:00
|
|
|
size_weight,
|
|
|
|
count_weight,
|
2020-09-27 15:09:44 +08:00
|
|
|
);
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2023-06-09 09:44:42 +08:00
|
|
|
let cacheSeconds = clampValue(
|
2022-05-19 03:18:26 +08:00
|
|
|
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
|
|
|
|
CONSTANTS.FOUR_HOURS,
|
2020-09-25 00:08:14 +08:00
|
|
|
CONSTANTS.ONE_DAY,
|
2020-08-13 23:48:26 +08:00
|
|
|
);
|
2023-06-09 09:44:42 +08:00
|
|
|
cacheSeconds = process.env.CACHE_SECONDS
|
|
|
|
? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds
|
|
|
|
: cacheSeconds;
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2022-11-21 17:21:46 +08:00
|
|
|
res.setHeader(
|
|
|
|
"Cache-Control",
|
|
|
|
`max-age=${
|
|
|
|
cacheSeconds / 2
|
|
|
|
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
|
|
|
);
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
return res.send(
|
|
|
|
renderTopLanguages(topLangs, {
|
2020-09-27 15:40:39 +08:00
|
|
|
custom_title,
|
2020-08-13 23:48:26 +08:00
|
|
|
hide_title: parseBoolean(hide_title),
|
|
|
|
hide_border: parseBoolean(hide_border),
|
|
|
|
card_width: parseInt(card_width, 10),
|
|
|
|
hide: parseArray(hide),
|
|
|
|
title_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
|
|
|
theme,
|
|
|
|
layout,
|
2021-05-07 03:01:04 +08:00
|
|
|
langs_count,
|
2021-03-08 00:53:32 +08:00
|
|
|
border_radius,
|
2021-04-28 02:58:44 +08:00
|
|
|
border_color,
|
2020-10-04 16:05:15 +08:00
|
|
|
locale: locale ? locale.toLowerCase() : null,
|
2023-01-16 19:30:39 +08:00
|
|
|
disable_animations: parseBoolean(disable_animations),
|
2023-02-16 09:23:11 +08:00
|
|
|
hide_progress: parseBoolean(hide_progress),
|
2020-09-25 00:08:14 +08:00
|
|
|
}),
|
2020-08-13 23:48:26 +08:00
|
|
|
);
|
|
|
|
} catch (err) {
|
2022-10-19 00:07:38 +08:00
|
|
|
res.setHeader("Cache-Control", `no-cache, no-store, must-revalidate`); // Don't cache error responses.
|
2020-08-13 23:48:26 +08:00
|
|
|
return res.send(renderError(err.message, err.secondaryMessage));
|
|
|
|
}
|
2020-07-21 19:33:05 +08:00
|
|
|
};
|