lambert-server: remove unused code

This commit is contained in:
Rory&
2026-03-14 03:45:17 +01:00
parent 128c004508
commit 27cb026967
3 changed files with 2 additions and 59 deletions

View File

@@ -1,10 +1,5 @@
import express, { Application, NextFunction, Request, Response, Router } from "express";
import { traverseDirectory } from "./Utils";
import express, { Application, Router } from "express";
import { Server as HTTPServer } from "http";
import { HTTPError } from "./HTTPError";
// import "express-async-errors";
import bodyParser from "body-parser";
// import helmet from "helmet";
import http from "http";
declare global {
@@ -21,7 +16,6 @@ export type ServerOptions = {
host: string;
production: boolean;
serverInitLogging: boolean;
// errorHandler?: { (err: Error, req: Request, res: Response, next: NextFunction): any };
jsonBody: boolean;
server: http.Server;
app: Application;
@@ -48,7 +42,6 @@ export class Server {
if (!opts.host) opts.host = "0.0.0.0";
if (opts.production == null) opts.production = false;
if (opts.serverInitLogging == null) opts.serverInitLogging = true;
// if (opts.errorHandler == null) opts.errorHandler = this.errorHandler;
if (opts.jsonBody == null) opts.jsonBody = true;
if (opts.server) this.http = opts.server;
@@ -58,40 +51,6 @@ export class Server {
else this.app = express();
}
// protected secureExpress() {
// this.app.use(helmet.contentSecurityPolicy());
// this.app.use(helmet.expectCt);
// this.app.use(helmet.originAgentCluster());
// this.app.use(helmet.referrerPolicy({ policy: "same-origin" }));
// this.app.use(helmet.hidePoweredBy());
// this.app.use(helmet.noSniff());
// this.app.use(helmet.dnsPrefetchControl({ allow: true }));
// this.app.use(helmet.ieNoOpen());
// this.app.use(helmet.frameguard({ action: "deny" }));
// this.app.use(helmet.permittedCrossDomainPolicies({ permittedPolicies: "none" }));
// }
public errorHandler = (error: Error, req: Request, res: Response, next: NextFunction) => {
try {
let code;
let message = error?.toString();
if (error instanceof HTTPError && error.code) code = error.code || 400;
else {
console.error(error);
if (this.options.production) {
message = "Internal Server Error";
}
code = 500;
}
res.status(code).json({ success: false, code: code, error: true, message });
} catch (e) {
console.error(e);
return res.status(500).json({ success: false, code: 500, error: true, message: "Internal Server Error" });
}
};
async start() {
const server = this.http || this.app;
if (!server.listening) {
@@ -102,18 +61,6 @@ export class Server {
}
}
async registerRoutes(root: string) {
this.app.use((req, res, next) => {
req.server = this;
next();
});
if (this.options.jsonBody) this.app.use(bodyParser.json());
const result = await traverseDirectory({ dirname: root, recursive: true }, this.registerRoute.bind(this, root));
// if (this.options.errorHandler) this.app.use(this.options.errorHandler);
// if (this.options.production) this.secureExpress();
return result;
}
registerRoute(root: string, file: string): Router | undefined {
if (root.endsWith("/") || root.endsWith("\\")) root = root.slice(0, -1); // removes slash at the end of the root dir
let path = file.replace(root, ""); // remove root from path and
@@ -128,7 +75,6 @@ export class Server {
if (router.default) router = router.default;
if (!router || router?.prototype?.constructor?.name !== "router") throw `File doesn't export any default router`;
// if (this.options.errorHandler) router.use(this.options.errorHandler);
this.app.use(path, <Router>router);
if (this.options.serverInitLogging && process.env.LOG_ROUTES !== "false") console.log(`[Server] Route ${path} registered`);

View File

@@ -16,7 +16,7 @@ export async function traverseDirectory<T>(options: traverseDirectoryOptions, ac
const routes = fs.readdirSync(options.dirname);
const promises = <Promise<T | T[] | undefined>[]>routes
.sort((a, b) => (a.startsWith("#") ? 1 : -1)) // load #parameter routes last
.sort((a, _) => (a.startsWith("#") ? 1 : -1)) // load #parameter routes last
.map(async (file) => {
const path = options.dirname + file;
const stat = fs.lstatSync(path);

View File

@@ -1,6 +1,3 @@
import { NextFunction, Request, Response } from "express";
import { HTTPError } from ".";
const OPTIONAL_PREFIX = "$";
const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;