Defer middleware HTML file reads

9aac5a22f174644504883e84abb6ba356c26032b

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +10 -9Ignore whitespace
src/middleware/basicAuth.js+6 -6
@@ -10,13 +10,13 @@ import { getConfig, getConfigValue, safeReadFileSync } from '../util.js';
1010const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
1111const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
1212
13-const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? '';
14-const unauthorizedResponse = (res) => {
15- res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
16- return res.status(401).send(unauthorizedWebpage);
17-};
18-
1913const basicAuthMiddleware = async function (request, response, callback) {
14+ const unauthorizedWebpage = safeReadFileSync('./public/error/unauthorized.html') ?? '';
15+ const unauthorizedResponse = (res) => {
16+ res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');
17+ return res.status(401).send(unauthorizedWebpage);
18+ };
19+
2020 const config = getConfig();
2121 const authHeader = request.headers.authorization;
2222
src/middleware/whitelist.js+4 -3
@@ -11,9 +11,6 @@ const whitelistPath = path.join(process.cwd(), './whitelist.txt');
1111const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);
1212let whitelist = getConfigValue('whitelist', []);
1313let knownIPs = new Set();
14-const forbiddenWebpage = Handlebars.compile(
15- safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
16-);
1714
1815if (fs.existsSync(whitelistPath)) {
1916 try {
@@ -56,6 +53,10 @@ function getForwardedIp(req) {
5653 * @returns {import('express').RequestHandler} The middleware function
5754 */
5855export default function whitelistMiddleware(whitelistMode, listen) {
56+ const forbiddenWebpage = Handlebars.compile(
57+ safeReadFileSync('./public/error/forbidden-by-whitelist.html') ?? '',
58+ );
59+
5960 return function (req, res, next) {
6061 const clientIp = getIpFromRequest(req);
6162 const forwardedIp = getForwardedIp(req);