Skip to content

Commit a12553f

Browse files
committed
Convert backend to ESM
- About 5 years overdue - Remove eslint, use bomejs instead
1 parent 5b6ca1b commit a12553f

File tree

89 files changed

+4795
-5103
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+4795
-5103
lines changed

backend/.eslintrc.json

Lines changed: 0 additions & 73 deletions
This file was deleted.

backend/.prettierrc

Lines changed: 0 additions & 11 deletions
This file was deleted.

backend/app.js

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
const express = require('express');
2-
const bodyParser = require('body-parser');
3-
const fileUpload = require('express-fileupload');
4-
const compression = require('compression');
5-
const config = require('./lib/config');
6-
const log = require('./logger').express;
1+
import bodyParser from "body-parser";
2+
import compression from "compression";
3+
import express from "express";
4+
import fileUpload from "express-fileupload";
5+
import { isDebugMode } from "./lib/config.js";
6+
import cors from "./lib/express/cors.js";
7+
import jwt from "./lib/express/jwt.js";
8+
import { express as logger } from "./logger.js";
9+
import mainRoutes from "./routes/main.js";
710

811
/**
912
* App
1013
*/
1114
const app = express();
1215
app.use(fileUpload());
1316
app.use(bodyParser.json());
14-
app.use(bodyParser.urlencoded({extended: true}));
17+
app.use(bodyParser.urlencoded({ extended: true }));
1518

1619
// Gzip
1720
app.use(compression());
@@ -20,71 +23,70 @@ app.use(compression());
2023
* General Logging, BEFORE routes
2124
*/
2225

23-
app.disable('x-powered-by');
24-
app.enable('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
25-
app.enable('strict routing');
26+
app.disable("x-powered-by");
27+
app.enable("trust proxy", ["loopback", "linklocal", "uniquelocal"]);
28+
app.enable("strict routing");
2629

2730
// pretty print JSON when not live
28-
if (config.debug()) {
29-
app.set('json spaces', 2);
31+
if (isDebugMode()) {
32+
app.set("json spaces", 2);
3033
}
3134

3235
// CORS for everything
33-
app.use(require('./lib/express/cors'));
36+
app.use(cors);
3437

3538
// General security/cache related headers + server header
36-
app.use(function (req, res, next) {
37-
let x_frame_options = 'DENY';
39+
app.use((_, res, next) => {
40+
let x_frame_options = "DENY";
3841

39-
if (typeof process.env.X_FRAME_OPTIONS !== 'undefined' && process.env.X_FRAME_OPTIONS) {
42+
if (typeof process.env.X_FRAME_OPTIONS !== "undefined" && process.env.X_FRAME_OPTIONS) {
4043
x_frame_options = process.env.X_FRAME_OPTIONS;
4144
}
4245

4346
res.set({
44-
'X-XSS-Protection': '1; mode=block',
45-
'X-Content-Type-Options': 'nosniff',
46-
'X-Frame-Options': x_frame_options,
47-
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
48-
Pragma: 'no-cache',
49-
Expires: 0
47+
"X-XSS-Protection": "1; mode=block",
48+
"X-Content-Type-Options": "nosniff",
49+
"X-Frame-Options": x_frame_options,
50+
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
51+
Pragma: "no-cache",
52+
Expires: 0,
5053
});
5154
next();
5255
});
5356

54-
app.use(require('./lib/express/jwt')());
55-
app.use('/', require('./routes/main'));
57+
app.use(jwt());
58+
app.use("/", mainRoutes);
5659

5760
// production error handler
5861
// no stacktraces leaked to user
59-
// eslint-disable-next-line
60-
app.use(function (err, req, res, next) {
61-
62-
let payload = {
62+
app.use((err, req, res, _) => {
63+
const payload = {
6364
error: {
64-
code: err.status,
65-
message: err.public ? err.message : 'Internal Error'
66-
}
65+
code: err.status,
66+
message: err.public ? err.message : "Internal Error",
67+
},
6768
};
6869

69-
if (config.debug() || (req.baseUrl + req.path).includes('nginx/certificates')) {
70+
if (typeof err.message_i18n !== "undefined") {
71+
payload.error.message_i18n = err.message_i18n;
72+
}
73+
74+
if (isDebugMode() || (req.baseUrl + req.path).includes("nginx/certificates")) {
7075
payload.debug = {
71-
stack: typeof err.stack !== 'undefined' && err.stack ? err.stack.split('\n') : null,
72-
previous: err.previous
76+
stack: typeof err.stack !== "undefined" && err.stack ? err.stack.split("\n") : null,
77+
previous: err.previous,
7378
};
7479
}
7580

7681
// Not every error is worth logging - but this is good for now until it gets annoying.
77-
if (typeof err.stack !== 'undefined' && err.stack) {
78-
if (config.debug()) {
79-
log.debug(err.stack);
80-
} else if (typeof err.public == 'undefined' || !err.public) {
81-
log.warn(err.message);
82+
if (typeof err.stack !== "undefined" && err.stack) {
83+
logger.debug(err.stack);
84+
if (typeof err.public === "undefined" || !err.public) {
85+
logger.warn(err.message);
8286
}
8387
}
8488

85-
res
86-
.status(err.status || 500)
87-
.send(payload);
89+
res.status(err.status || 500).send(payload);
8890
});
8991

90-
module.exports = app;
92+
export default app;

backend/biome.json

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.2.0/schema.json",
3+
"vcs": {
4+
"enabled": true,
5+
"clientKind": "git",
6+
"useIgnoreFile": true
7+
},
8+
"files": {
9+
"ignoreUnknown": false,
10+
"includes": [
11+
"**/*.ts",
12+
"**/*.tsx",
13+
"**/*.js",
14+
"**/*.jsx",
15+
"!**/dist/**/*"
16+
]
17+
},
18+
"formatter": {
19+
"enabled": true,
20+
"indentStyle": "tab",
21+
"indentWidth": 4,
22+
"lineWidth": 120,
23+
"formatWithErrors": true
24+
},
25+
"assist": {
26+
"actions": {
27+
"source": {
28+
"organizeImports": {
29+
"level": "on",
30+
"options": {
31+
"groups": [
32+
":BUN:",
33+
":NODE:",
34+
[
35+
"npm:*",
36+
"npm:*/**"
37+
],
38+
":PACKAGE_WITH_PROTOCOL:",
39+
":URL:",
40+
":PACKAGE:",
41+
[
42+
"/src/*",
43+
"/src/**"
44+
],
45+
[
46+
"/**"
47+
],
48+
[
49+
"#*",
50+
"#*/**"
51+
],
52+
":PATH:"
53+
]
54+
}
55+
}
56+
}
57+
}
58+
},
59+
"linter": {
60+
"enabled": true,
61+
"rules": {
62+
"recommended": true,
63+
"correctness": {
64+
"useUniqueElementIds": "off"
65+
},
66+
"suspicious": {
67+
"noExplicitAny": "off"
68+
},
69+
"performance": {
70+
"noDelete": "off"
71+
},
72+
"nursery": "off",
73+
"a11y": {
74+
"useSemanticElements": "off",
75+
"useValidAnchor": "off"
76+
},
77+
"style": {
78+
"noParameterAssign": "error",
79+
"useAsConstAssertion": "error",
80+
"useDefaultParameterLast": "error",
81+
"useEnumInitializers": "error",
82+
"useSelfClosingElements": "error",
83+
"useSingleVarDeclarator": "error",
84+
"noUnusedTemplateLiteral": "error",
85+
"useNumberNamespace": "error",
86+
"noInferrableTypes": "error",
87+
"noUselessElse": "error"
88+
}
89+
}
90+
}
91+
}

backend/db.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
const config = require('./lib/config');
1+
import knex from "knex";
2+
import {configGet, configHas} from "./lib/config.js";
23

3-
if (!config.has('database')) {
4-
throw new Error('Database config does not exist! Please read the instructions: https://nginxproxymanager.com/setup/');
5-
}
4+
const generateDbConfig = () => {
5+
if (!configHas("database")) {
6+
throw new Error(
7+
"Database config does not exist! Please read the instructions: https://nginxproxymanager.com/setup/",
8+
);
9+
}
10+
11+
const cfg = configGet("database");
612

7-
function generateDbConfig() {
8-
const cfg = config.get('database');
9-
if (cfg.engine === 'knex-native') {
13+
if (cfg.engine === "knex-native") {
1014
return cfg.knex;
1115
}
16+
1217
return {
13-
client: cfg.engine,
18+
client: cfg.engine,
1419
connection: {
15-
host: cfg.host,
16-
user: cfg.user,
20+
host: cfg.host,
21+
user: cfg.user,
1722
password: cfg.password,
1823
database: cfg.name,
19-
port: cfg.port
24+
port: cfg.port,
2025
},
2126
migrations: {
22-
tableName: 'migrations'
23-
}
27+
tableName: "migrations",
28+
},
2429
};
25-
}
30+
};
2631

27-
module.exports = require('knex')(generateDbConfig());
32+
export default knex(generateDbConfig());

0 commit comments

Comments
 (0)