Fix comments, update function interfaces

This commit is contained in:
Cohee
2025-02-26 15:36:01 +00:00
parent e7fcd0072b
commit b64273ab94
3 changed files with 44 additions and 51 deletions

View File

@ -18,7 +18,6 @@ import responseTime from 'response-time';
import helmet from 'helmet'; import helmet from 'helmet';
import bodyParser from 'body-parser'; import bodyParser from 'body-parser';
import open from 'open'; import open from 'open';
import fetch from 'node-fetch';
// local library imports // local library imports
import { CommandLineParser } from './src/command-line.js'; import { CommandLineParser } from './src/command-line.js';
@ -36,7 +35,6 @@ import {
setUserDataMiddleware, setUserDataMiddleware,
shouldRedirectToLogin, shouldRedirectToLogin,
tryAutoLogin, tryAutoLogin,
router as userDataRouter,
cleanUploads, cleanUploads,
getSessionCookieAge, getSessionCookieAge,
} from './src/users.js'; } from './src/users.js';
@ -64,8 +62,6 @@ import { ensureThumbnailCache } from './src/endpoints/thumbnails.js';
// Routers // Routers
import { router as usersPublicRouter } from './src/endpoints/users-public.js'; import { router as usersPublicRouter } from './src/endpoints/users-public.js';
import { router as usersPrivateRouter } from './src/endpoints/users-private.js';
import { router as usersAdminRouter } from './src/endpoints/users-admin.js';
import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js'; import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js';
import { checkForNewContent } from './src/endpoints/content-manager.js'; import { checkForNewContent } from './src/endpoints/content-manager.js';
import { init as settingsInit } from './src/endpoints/settings.js'; import { init as settingsInit } from './src/endpoints/settings.js';
@ -255,17 +251,10 @@ app.get('/api/ping', (request, response) => {
}); });
// File uploads // File uploads
const uploadsPath = path.join(globalThis.DATA_ROOT, UPLOADS_DIRECTORY); const uploadsPath = path.join(cliArgs.dataRoot, UPLOADS_DIRECTORY);
app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar')); app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));
app.use(multerMonkeyPatch); app.use(multerMonkeyPatch);
// User data mount
app.use('/', userDataRouter);
// Private endpoints
app.use('/api/users', usersPrivateRouter);
// Admin endpoints
app.use('/api/users', usersAdminRouter);
app.get('/version', async function (_, response) { app.get('/version', async function (_, response) {
const data = await getVersion(); const data = await getVersion();
response.send(data); response.send(data);
@ -335,8 +324,8 @@ async function preSetupTasks() {
* @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup * @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) { async function postSetupTasks(result) {
const autorunHostname = await cliArgs.getAutorunHostname(useIPv6, useIPv4); const autorunHostname = await cliArgs.getAutorunHostname(result);
const autorunUrl = cliArgs.getAutorunUrl(autorunHostname); const autorunUrl = cliArgs.getAutorunUrl(autorunHostname);
console.log('Launching...'); console.log('Launching...');
@ -348,13 +337,13 @@ async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
let logListen = 'SillyTavern is listening on'; let logListen = 'SillyTavern is listening on';
if (useIPv6 && !v6Failed) { if (result.useIPv6 && !result.v6Failed) {
logListen += color.green( logListen += color.green(
' IPv6: ' + cliArgs.getIPv6ListenUrl().host, ' IPv6: ' + cliArgs.getIPv6ListenUrl().host,
); );
} }
if (useIPv4 && !v4Failed) { if (result.useIPv4 && !result.v4Failed) {
logListen += color.green( logListen += color.green(
' IPv4: ' + cliArgs.getIPv4ListenUrl().host, ' IPv4: ' + cliArgs.getIPv4ListenUrl().host,
); );

View File

@ -4,33 +4,33 @@ import ipRegex from 'ip-regex';
import { canResolve, color, getConfigValue, stringToBool } from './util.js'; import { canResolve, color, getConfigValue, stringToBool } from './util.js';
/** /**
* @typedef {object} CommandLineArguments * @typedef {object} CommandLineArguments Parsed command line arguments
* @property {string} dataRoot * @property {string} dataRoot Data root directory
* @property {number} port * @property {number} port Port number
* @property {boolean} listen * @property {boolean} listen If SillyTavern is listening on all network interfaces
* @property {string} listenAddressIPv6 * @property {string} listenAddressIPv6 IPv6 address to listen to
* @property {string} listenAddressIPv4 * @property {string} listenAddressIPv4 IPv4 address to listen to
* @property {boolean|string} enableIPv4 * @property {boolean|string} enableIPv4 If enable IPv4 protocol ("auto" is also allowed)
* @property {boolean|string} enableIPv6 * @property {boolean|string} enableIPv6 If enable IPv6 protocol ("auto" is also allowed)
* @property {boolean} dnsPreferIPv6 * @property {boolean} dnsPreferIPv6 If prefer IPv6 for DNS
* @property {boolean} autorun * @property {boolean} autorun If automatically launch SillyTavern in the browser
* @property {string} autorunHostname * @property {string} autorunHostname Autorun hostname
* @property {number} autorunPortOverride * @property {number} autorunPortOverride Autorun port override (-1 is use server port)
* @property {boolean} enableCorsProxy * @property {boolean} enableCorsProxy If enable CORS proxy
* @property {boolean} disableCsrf * @property {boolean} disableCsrf If disable CSRF protection
* @property {boolean} ssl * @property {boolean} ssl If enable SSL
* @property {string} certPath * @property {string} certPath Path to certificate
* @property {string} keyPath * @property {string} keyPath Path to private key
* @property {boolean} whitelistMode * @property {boolean} whitelistMode If enable whitelist mode
* @property {boolean} avoidLocalhost * @property {boolean} avoidLocalhost If avoid using 'localhost' for autorun in auto mode
* @property {boolean} basicAuthMode * @property {boolean} basicAuthMode If enable basic authentication
* @property {boolean} requestProxyEnabled * @property {boolean} requestProxyEnabled If enable outgoing request proxy
* @property {string} requestProxyUrl * @property {string} requestProxyUrl Request proxy URL
* @property {string[]} requestProxyBypass * @property {string[]} requestProxyBypass Request proxy bypass list
* @property {function(): URL} getIPv4ListenUrl * @property {function(): URL} getIPv4ListenUrl Get IPv4 listen URL
* @property {function(): URL} getIPv6ListenUrl * @property {function(): URL} getIPv6ListenUrl Get IPv6 listen URL
* @property {function(boolean, boolean): Promise<string>} getAutorunHostname * @property {function(import('./server-startup.js').ServerStartupResult): Promise<string>} getAutorunHostname Get autorun hostname
* @property {function(string): URL} getAutorunUrl * @property {function(string): URL} getAutorunUrl Get autorun URL
*/ */
/** /**
@ -220,7 +220,7 @@ export class CommandLineParser {
(':' + this.port), (':' + this.port),
); );
}, },
getAutorunHostname: async function (useIPv6, useIPv4) { getAutorunHostname: async function ({ useIPv6, useIPv4 }) {
if (this.autorunHostname === 'auto') { if (this.autorunHostname === 'auto') {
let localhostResolve = await canResolve('localhost', useIPv6, useIPv4); let localhostResolve = await canResolve('localhost', useIPv6, useIPv4);

View File

@ -4,6 +4,9 @@ import fs from 'node:fs';
import { color, urlHostnameToIPv6, getHasIP } from './util.js'; import { color, urlHostnameToIPv6, getHasIP } from './util.js';
// Express routers // Express routers
import { router as userDataRouter } from './users.js';
import { router as usersPrivateRouter } from './endpoints/users-private.js';
import { router as usersAdminRouter } from './endpoints/users-admin.js';
import { router as movingUIRouter } from './endpoints/moving-ui.js'; import { router as movingUIRouter } from './endpoints/moving-ui.js';
import { router as imagesRouter } from './endpoints/images.js'; import { router as imagesRouter } from './endpoints/images.js';
import { router as quickRepliesRouter } from './endpoints/quick-replies.js'; import { router as quickRepliesRouter } from './endpoints/quick-replies.js';
@ -128,6 +131,9 @@ export function redirectDeprecatedEndpoints(app) {
* @param {import('express').Express} app The Express app to use * @param {import('express').Express} app The Express app to use
*/ */
export function setupPrivateEndpoints(app) { export function setupPrivateEndpoints(app) {
app.use('/', userDataRouter);
app.use('/api/users', usersPrivateRouter);
app.use('/api/users', usersAdminRouter);
app.use('/api/moving-ui', movingUIRouter); app.use('/api/moving-ui', movingUIRouter);
app.use('/api/images', imagesRouter); app.use('/api/images', imagesRouter);
app.use('/api/quick-replies', quickRepliesRouter); app.use('/api/quick-replies', quickRepliesRouter);
@ -274,13 +280,10 @@ export class ServerStartup {
/** /**
* Handles the case where the server failed to start on one or both protocols. * Handles the case where the server failed to start on one or both protocols.
* @param {boolean} v6Failed If the server failed to start on IPv6 * @param {ServerStartupResult} result The results of the server startup
* @param {boolean} v4Failed If the server failed to start on IPv4
* @param {boolean} useIPv6 If use IPv6
* @param {boolean} useIPv4 If use IPv4
* @returns {void} * @returns {void}
*/ */
#handleServerListenFail(v6Failed, v4Failed, useIPv6, useIPv4) { #handleServerListenFail({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
if (v6Failed && !useIPv4) { if (v6Failed && !useIPv4) {
console.error(color.red('fatal error: Failed to start server on IPv6 and IPv4 disabled')); console.error(color.red('fatal error: Failed to start server on IPv6 and IPv4 disabled'));
process.exit(1); process.exit(1);
@ -353,7 +356,8 @@ export class ServerStartup {
} }
const [v6Failed, v4Failed] = await this.#startHTTPorHTTPS(useIPv6, useIPv4); const [v6Failed, v4Failed] = await this.#startHTTPorHTTPS(useIPv6, useIPv4);
this.#handleServerListenFail(v6Failed, v4Failed, useIPv6, useIPv4); const result = { v6Failed, v4Failed, useIPv6, useIPv4 };
return { v6Failed, v4Failed, useIPv6, useIPv4 }; this.#handleServerListenFail(result);
return result;
} }
} }