Page MenuHomeSealhub

mount.ts
No OneTemporary

mount.ts

/* eslint-disable @typescript-eslint/no-explicit-any */
import Router from "@koa/router";
import { FileManager } from "@sealcode/file-manager";
import Koa, { Context } from "koa";
import { Middlewares as SealiousMiddlewares } from "sealious";
import { Mountable } from "./page/mountable.js";
async function handleHtmlPromise(ctx: Context, next: Koa.Next) {
await next();
if (ctx.body instanceof Promise) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
ctx.body = await ctx.body;
}
ctx.set("content-type", "text/html;charset=utf-8");
}
export function mount(
router: Router,
url: string | { rawURL: string },
mountable: Mountable,
use_dummy_app = false,
file_manager: FileManager | undefined = use_dummy_app
? new FileManager("/tmp", "/uploaded_files")
: undefined
): void {
const raw_url = typeof url === "string" ? url : url.rawURL;
const args = use_dummy_app
? [
async (ctx: Context, next: any) => {
// this dummy is a temporary solution it's ilustrating the ways that
// sealgen is coupled with sealious. Definitely room for improvement in this
// regard
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
ctx.$app = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Logger: new Proxy(
{},
{
get: (_target: any, prop: any) => {
if (prop === "info") {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
} else {
// eslint-disable-next-line @typescript-eslint/no-empty-function
return () => {};
}
},
}
),
getString: (x: string) => x,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any;
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await next();
},
SealiousMiddlewares.parseBody(file_manager),
handleHtmlPromise,
]
: [
SealiousMiddlewares.extractContext(),
SealiousMiddlewares.parseBody(file_manager),
handleHtmlPromise,
];
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
router.use(raw_url, ...args);
mountable.init();
// to automatically add trailing slashes:
router.get(raw_url.slice(0, -1), async (ctx, next) => {
const url2 = ctx.URL;
if (!url2.pathname.endsWith("/")) {
url2.pathname += "/";
ctx.redirect(url2.href);
}
await next();
});
router.use(raw_url, async (ctx, next) => {
ctx.set("content-type", "text/html;charset=utf-8");
const access_result = await mountable.canAccess(ctx);
if (!access_result.canAccess) {
ctx.status = 403;
ctx.body = access_result.message || "no access";
return;
}
await next();
});
mountable.mount(router, raw_url);
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (18 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557026
Default Alt Text
mount.ts (2 KB)

Event Timeline