Move mx logic to top level functions middleware
This commit is contained in:
@@ -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,
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user