diff --git a/src/util/util/lambert-server/Server.ts b/src/util/util/lambert-server/Server.ts index 481c0f658..e9cb675b4 100644 --- a/src/util/util/lambert-server/Server.ts +++ b/src/util/util/lambert-server/Server.ts @@ -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); if (this.options.serverInitLogging && process.env.LOG_ROUTES !== "false") console.log(`[Server] Route ${path} registered`); diff --git a/src/util/util/lambert-server/Utils.ts b/src/util/util/lambert-server/Utils.ts index 32f4b16cb..07f8eadca 100644 --- a/src/util/util/lambert-server/Utils.ts +++ b/src/util/util/lambert-server/Utils.ts @@ -16,7 +16,7 @@ export async function traverseDirectory(options: traverseDirectoryOptions, ac const routes = fs.readdirSync(options.dirname); const promises = []>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); diff --git a/src/util/util/lambert-server/check.ts b/src/util/util/lambert-server/check.ts index 864e5fac0..561a584ce 100644 --- a/src/util/util/lambert-server/check.ts +++ b/src/util/util/lambert-server/check.ts @@ -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,}))$/;