server-ts: Address review

This commit is contained in:
Elian Doran 2024-04-03 19:22:49 +03:00
parent f857b8a9bb
commit 5d452a1525
No known key found for this signature in database
8 changed files with 20 additions and 22 deletions

View File

@ -11,7 +11,7 @@ import protectedSessionService = require('../../services/protected_session');
import blobService = require('../../services/blob'); import blobService = require('../../services/blob');
import Becca, { ConstructorData } from '../becca-interface'; import Becca, { ConstructorData } from '../becca-interface';
let becca: Becca | null = null; let becca: Becca;
interface ContentOpts { interface ContentOpts {
forceSave?: boolean; forceSave?: boolean;

View File

@ -153,7 +153,7 @@ function buildRewardMap(note: BNote) {
const mimeCache: Record<string, string> = {}; const mimeCache: Record<string, string> = {};
function trimMime(mime?: string) { function trimMime(mime: string) {
if (!mime || mime === 'text/html') { if (!mime || mime === 'text/html') {
return; return;
} }

View File

@ -7,13 +7,11 @@ import utils = require('./utils');
import passwordEncryptionService = require('./encryption/password_encryption'); import passwordEncryptionService = require('./encryption/password_encryption');
import config = require('./config'); import config = require('./config');
import passwordService = require('./encryption/password'); import passwordService = require('./encryption/password');
import type { Request } from 'express';
const noAuthentication = config.General && config.General.noAuthentication === true; const noAuthentication = config.General && config.General.noAuthentication === true;
// TODO: We are using custom types for request & response because couldn't extract those pesky express types. interface AppRequest extends Request {
interface Request {
method: string;
path: string;
headers: { headers: {
authorization?: string; authorization?: string;
"trilium-cred"?: string; "trilium-cred"?: string;
@ -30,7 +28,7 @@ interface Response {
type Callback = () => void; type Callback = () => void;
function checkAuth(req: Request, res: Response, next: Callback) { function checkAuth(req: AppRequest, res: Response, next: Callback) {
if (!sqlInit.isDbInitialized()) { if (!sqlInit.isDbInitialized()) {
res.redirect("setup"); res.redirect("setup");
} }
@ -44,7 +42,7 @@ function checkAuth(req: Request, res: Response, next: Callback) {
// for electron things which need network stuff // for electron things which need network stuff
// currently, we're doing that for file upload because handling form data seems to be difficult // currently, we're doing that for file upload because handling form data seems to be difficult
function checkApiAuthOrElectron(req: Request, res: Response, next: Callback) { function checkApiAuthOrElectron(req: AppRequest, res: Response, next: Callback) {
if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) { if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
reject(req, res, "Logged in session not found"); reject(req, res, "Logged in session not found");
} }
@ -53,7 +51,7 @@ function checkApiAuthOrElectron(req: Request, res: Response, next: Callback) {
} }
} }
function checkApiAuth(req: Request, res: Response, next: Callback) { function checkApiAuth(req: AppRequest, res: Response, next: Callback) {
if (!req.session.loggedIn && !noAuthentication) { if (!req.session.loggedIn && !noAuthentication) {
reject(req, res, "Logged in session not found"); reject(req, res, "Logged in session not found");
} }
@ -62,7 +60,7 @@ function checkApiAuth(req: Request, res: Response, next: Callback) {
} }
} }
function checkAppInitialized(req: Request, res: Response, next: Callback) { function checkAppInitialized(req: AppRequest, res: Response, next: Callback) {
if (!sqlInit.isDbInitialized()) { if (!sqlInit.isDbInitialized()) {
res.redirect("setup"); res.redirect("setup");
} }
@ -71,7 +69,7 @@ function checkAppInitialized(req: Request, res: Response, next: Callback) {
} }
} }
function checkPasswordSet(req: Request, res: Response, next: Callback) { function checkPasswordSet(req: AppRequest, res: Response, next: Callback) {
if (!utils.isElectron() && !passwordService.isPasswordSet()) { if (!utils.isElectron() && !passwordService.isPasswordSet()) {
res.redirect("set-password"); res.redirect("set-password");
} else { } else {
@ -79,7 +77,7 @@ function checkPasswordSet(req: Request, res: Response, next: Callback) {
} }
} }
function checkPasswordNotSet(req: Request, res: Response, next: Callback) { function checkPasswordNotSet(req: AppRequest, res: Response, next: Callback) {
if (!utils.isElectron() && passwordService.isPasswordSet()) { if (!utils.isElectron() && passwordService.isPasswordSet()) {
res.redirect("login"); res.redirect("login");
} else { } else {
@ -87,7 +85,7 @@ function checkPasswordNotSet(req: Request, res: Response, next: Callback) {
} }
} }
function checkAppNotInitialized(req: Request, res: Response, next: Callback) { function checkAppNotInitialized(req: AppRequest, res: Response, next: Callback) {
if (sqlInit.isDbInitialized()) { if (sqlInit.isDbInitialized()) {
reject(req, res, "App already initialized."); reject(req, res, "App already initialized.");
} }
@ -96,7 +94,7 @@ function checkAppNotInitialized(req: Request, res: Response, next: Callback) {
} }
} }
function checkEtapiToken(req: Request, res: Response, next: Callback) { function checkEtapiToken(req: AppRequest, res: Response, next: Callback) {
if (etapiTokenService.isValidAuthHeader(req.headers.authorization)) { if (etapiTokenService.isValidAuthHeader(req.headers.authorization)) {
next(); next();
} }
@ -105,7 +103,7 @@ function checkEtapiToken(req: Request, res: Response, next: Callback) {
} }
} }
function reject(req: Request, res: Response, message: string) { function reject(req: AppRequest, res: Response, message: string) {
log.info(`${req.method} ${req.path} rejected with 401 ${message}`); log.info(`${req.method} ${req.path} rejected with 401 ${message}`);
res.setHeader("Content-Type", "text/plain") res.setHeader("Content-Type", "text/plain")
@ -113,7 +111,7 @@ function reject(req: Request, res: Response, message: string) {
.send(message); .send(message);
} }
function checkCredentials(req: Request, res: Response, next: Callback) { function checkCredentials(req: AppRequest, res: Response, next: Callback) {
if (!sqlInit.isDbInitialized()) { if (!sqlInit.isDbInitialized()) {
res.setHeader("Content-Type", "text/plain") res.setHeader("Content-Type", "text/plain")
.status(400) .status(400)

View File

@ -80,7 +80,7 @@ function validateLocalDateTime(str: string | null | undefined) {
} }
} }
function validateUtcDateTime(str?: string) { function validateUtcDateTime(str: string | undefined) {
if (!str) { if (!str) {
return; return;
} }

View File

@ -25,7 +25,7 @@ function createToken(tokenName: string) {
}; };
} }
function parseAuthToken(auth?: string | null) { function parseAuthToken(auth: string | undefined) {
if (!auth) { if (!auth) {
return null; return null;
} }
@ -64,7 +64,7 @@ function parseAuthToken(auth?: string | null) {
} }
} }
function isValidAuthHeader(auth?: string | null) { function isValidAuthHeader(auth: string | undefined) {
const parsed = parseAuthToken(auth); const parsed = parseAuthToken(auth);
if (!parsed) { if (!parsed) {

View File

@ -623,7 +623,7 @@ function getKeyboardActions() {
for (const option of optionService.getOptions()) { for (const option of optionService.getOptions()) {
if (option.name.startsWith('keyboardShortcuts')) { if (option.name.startsWith('keyboardShortcuts')) {
let actionName = option.name.substr(17); let actionName = option.name.substring(17);
actionName = actionName.charAt(0).toLowerCase() + actionName.slice(1); actionName = actionName.charAt(0).toLowerCase() + actionName.slice(1);
const action = actions.find(ea => ea.actionName === actionName); const action = actions.find(ea => ea.actionName === actionName);

View File

@ -873,7 +873,7 @@ function getUndeletedParentBranchIds(noteId: string, deleteId: string) {
AND parentNote.isDeleted = 0`, [noteId, deleteId]); AND parentNote.isDeleted = 0`, [noteId, deleteId]);
} }
function scanForLinks(note: BNote | null, content: string) { function scanForLinks(note: BNote, content: string) {
if (!note || !['text', 'relationMap'].includes(note.type)) { if (!note || !['text', 'relationMap'].includes(note.type)) {
return; return;
} }

View File

@ -144,7 +144,7 @@ function getRows<T>(query: string, params: Params = []): T[] {
} }
function getRawRows<T extends {} | unknown[]>(query: string, params: Params = []): T[] { function getRawRows<T extends {} | unknown[]>(query: string, params: Params = []): T[] {
return (wrap(query, s => s.raw().all(params)) as T[] | null) || []; return (wrap(query, s => s.raw().all(params)) as T[]) || [];
} }
function iterateRows(query: string, params: Params = []) { function iterateRows(query: string, params: Params = []) {