* chore(deps): bump koa-body from 4.2.0 to 6.0.1 Bumps [koa-body](https://github.com/koajs/koa-body) from 4.2.0 to 6.0.1. - [Release notes](https://github.com/koajs/koa-body/releases) - [Changelog](https://github.com/koajs/koa-body/blob/master/CHANGELOG.md) - [Commits](https://github.com/koajs/koa-body/compare/v4.2.0...v6.0.1) --- updated-dependencies: - dependency-name: koa-body dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Update types * test --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tom Moor <tom.moor@gmail.com>
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import crypto from "crypto";
|
|
import { Context } from "koa";
|
|
import Router from "koa-router";
|
|
import env from "@server/env";
|
|
import { AuthenticationError } from "@server/errors";
|
|
import tasks from "@server/queues/tasks";
|
|
|
|
const router = new Router();
|
|
|
|
const cronHandler = async (ctx: Context) => {
|
|
const token =
|
|
ctx.method === "POST" ? ctx.request.body?.token : ctx.query.token;
|
|
const limit =
|
|
(ctx.method === "POST" ? ctx.request.body?.limit : ctx.query.limit) ?? 500;
|
|
|
|
if (!token || typeof token !== "string") {
|
|
throw AuthenticationError("Token is required");
|
|
}
|
|
|
|
if (
|
|
token.length !== env.UTILS_SECRET.length ||
|
|
!crypto.timingSafeEqual(
|
|
Buffer.from(env.UTILS_SECRET),
|
|
Buffer.from(String(token))
|
|
)
|
|
) {
|
|
throw AuthenticationError("Invalid secret token");
|
|
}
|
|
|
|
for (const name in tasks) {
|
|
const TaskClass = tasks[name];
|
|
if (TaskClass.cron) {
|
|
await TaskClass.schedule({ limit });
|
|
}
|
|
}
|
|
|
|
ctx.body = {
|
|
success: true,
|
|
};
|
|
};
|
|
|
|
router.get("cron.:period", cronHandler);
|
|
router.post("cron.:period", cronHandler);
|
|
|
|
// For backwards compatibility
|
|
router.get("utils.gc", cronHandler);
|
|
router.post("utils.gc", cronHandler);
|
|
|
|
export default router;
|