Defer middleware HTML file reads
| @@ -10,13 +10,13 @@ import { getConfig, getConfigValue, safeReadFileSync } from '../util.js'; | ||
| 10 | 10 | const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false); |
| 11 | 11 | const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false); |
| 12 | 12 | |
| 13 | +const basicAuthMiddleware = async function (request, response, callback) { | |
| 13 | 14 | const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? ''; |
| 14 | 15 | const unauthorizedResponse = (res) => { |
| 15 | 16 | res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"'); |
| 16 | 17 | return res.status(401).send(unauthorizedWebpage); |
| 17 | 18 | }; |
| 18 | 19 | |
| 19 | -const basicAuthMiddleware = async function (request, response, callback) { | |
| 20 | 20 | const config = getConfig(); |
| 21 | 21 | const authHeader = request.headers.authorization; |
| 22 | 22 | |
| @@ -11,9 +11,6 @@ const whitelistPath = path.join(process.cwd(), './whitelist.txt'); | ||
| 11 | 11 | const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false); |
| 12 | 12 | let whitelist = getConfigValue('whitelist', []); |
| 13 | 13 | let knownIPs = new Set(); |
| 14 | -const forbiddenWebpage = Handlebars.compile( | |
| 15 | - safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '', | |
| 16 | -); | |
| 17 | 14 | |
| 18 | 15 | if (fs.existsSync(whitelistPath)) { |
| 19 | 16 | try { |
| @@ -56,6 +53,10 @@ function getForwardedIp(req) { | ||
| 56 | 53 | * @returns {import('express').RequestHandler} The middleware function |
| 57 | 54 | */ |
| 58 | 55 | export default function whitelistMiddleware(whitelistMode, listen) { |
| 56 | + const forbiddenWebpage = Handlebars.compile( | |
| 57 | + safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '', | |
| 58 | + ); | |
| 59 | + | |
| 59 | 60 | return function (req, res, next) { |
| 60 | 61 | const clientIp = getIpFromRequest(req); |
| 61 | 62 | const forwardedIp = getForwardedIp(req); |