Move mx logic to top level functions middleware
Some checks failed
Test, Build, Deploy / Test, Build, and Deploy (push) Failing after 54s
Test, Build, Deploy / Create Sentry Release (push) Has been skipped

This commit is contained in:
2026-04-11 02:26:31 -04:00
parent c92f4d31f2
commit 02f0a299e0
2 changed files with 25 additions and 1 deletions

View File

@@ -166,7 +166,7 @@ export async function loader({
}: {
context: RequestContext;
}): Promise<{ [k: string]: any }> {
if (await context.env.DATA.get("mx"))
if (context.data.mx)
throw new Response(null, {
status: 503,
});

View File

@@ -1,5 +1,7 @@
import getPermissions from "./permissions.js";
import { jsonError } from "./common.js";
import { PrismaClient } from "../generated/prisma/client.js";
import { PrismaD1 } from "@prisma/adapter-d1";
import * as Sentry from "@sentry/cloudflare";
async function constructHTML(context: RequestContext) {
@@ -28,6 +30,27 @@ async function generateTokenHash(token: string) {
.replace(/=/g, "");
}
async function initializePrisma(context: RequestContext) {
const adapter = new PrismaD1(context.env.D1);
context.data.prisma = new PrismaClient({ adapter });
return await context.next();
}
async function mxAndBypassCheck(context: RequestContext) {
if (!(await context.env.DATA.get("mx"))) return await context.next();
const cookies = context.request.headers.get("cookie")?.split(/; *?/);
const isAPI = new URL(context.request.url).pathname.startsWith("/api");
if (!cookies?.length || !cookies.find((c) => c.startsWith("mxb="))) {
if (isAPI) return jsonError("API is undergoing maintenance", 503);
context.data.mx = true;
}
return await context.next();
}
async function refreshAuth(context: RequestContext) {
const { current_user: currentUser } = context.data;
@@ -395,5 +418,6 @@ export const onRequest = [
setTheme,
constructHTML,
setBody,
initializePrisma,
setHeaders,
];