Fix comments, update function interfaces

b64273ab948c1c7318ce6985c071e576301c38ab

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

3 files changed, +44 -51Ignore whitespace
server.js+5 -16
@@ -18,7 +18,6 @@ import responseTime from 'response-time';
1818import helmet from 'helmet';
1919import bodyParser from 'body-parser';
2020import open from 'open';
21-import fetch from 'node-fetch';
2221
2322// local library imports
2423import { CommandLineParser } from './src/command-line.js';
@@ -36,7 +35,6 @@ import {
3635 setUserDataMiddleware,
3736 shouldRedirectToLogin,
3837 tryAutoLogin,
39- router as userDataRouter,
4038 cleanUploads,
4139 getSessionCookieAge,
4240} from './src/users.js';
@@ -64,8 +62,6 @@ import { ensureThumbnailCache } from './src/endpoints/thumbnails.js';
6462
6563// Routers
6664import { router as usersPublicRouter } from './src/endpoints/users-public.js';
67-import { router as usersPrivateRouter } from './src/endpoints/users-private.js';
68-import { router as usersAdminRouter } from './src/endpoints/users-admin.js';
6965import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js';
7066import { checkForNewContent } from './src/endpoints/content-manager.js';
7167import { init as settingsInit } from './src/endpoints/settings.js';
@@ -255,17 +251,10 @@ app.get('/api/ping', (request, response) => {
255251});
256252
257253// File uploads
258254const uploadsPath = path.join(globalThiscliArgs.DATA_ROOTdataRoot, UPLOADS_DIRECTORY);
259255app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));
260256app.use(multerMonkeyPatch);
261257
262-// User data mount
263-app.use('/', userDataRouter);
264-// Private endpoints
265-app.use('/api/users', usersPrivateRouter);
266-// Admin endpoints
267-app.use('/api/users', usersAdminRouter);
268-
269258app.get('/version', async function (_, response) {
270259 const data = await getVersion();
271260 response.send(data);
@@ -335,8 +324,8 @@ async function preSetupTasks() {
335324 * @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup
336325 * @returns {Promise<void>}
337326 */
338327async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }result) {
339328 const autorunHostname = await cliArgs.getAutorunHostname(useIPv6, useIPv4result);
340329 const autorunUrl = cliArgs.getAutorunUrl(autorunHostname);
341330 console.log('Launching...');
342331
@@ -348,13 +337,13 @@ async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
348337
349338 let logListen = 'SillyTavern is listening on';
350339
351340 if (result.useIPv6 && !result.v6Failed) {
352341 logListen += color.green(
353342 ' IPv6: ' + cliArgs.getIPv6ListenUrl().host,
354343 );
355344 }
356345
357346 if (result.useIPv4 && !result.v4Failed) {
358347 logListen += color.green(
359348 ' IPv4: ' + cliArgs.getIPv4ListenUrl().host,
360349 );
src/command-line.js+28 -28
@@ -4,33 +4,33 @@ import ipRegex from 'ip-regex';
44import { canResolve, color, getConfigValue, stringToBool } from './util.js';
55
66/**
77 * @typedef {object} CommandLineArguments Parsed command line arguments
88 * @property {string} dataRoot Data root directory
99 * @property {number} port Port number
1010 * @property {boolean} listen If SillyTavern is listening on all network interfaces
1111 * @property {string} listenAddressIPv6 IPv6 address to listen to
1212 * @property {string} listenAddressIPv4 IPv4 address to listen to
1313 * @property {boolean|string} enableIPv4 If enable IPv4 protocol ("auto" is also allowed)
1414 * @property {boolean|string} enableIPv6 If enable IPv6 protocol ("auto" is also allowed)
1515 * @property {boolean} dnsPreferIPv6 If prefer IPv6 for DNS
1616 * @property {boolean} autorun If automatically launch SillyTavern in the browser
1717 * @property {string} autorunHostname Autorun hostname
1818 * @property {number} autorunPortOverride Autorun port override (-1 is use server port)
1919 * @property {boolean} enableCorsProxy If enable CORS proxy
2020 * @property {boolean} disableCsrf If disable CSRF protection
2121 * @property {boolean} ssl If enable SSL
2222 * @property {string} certPath Path to certificate
2323 * @property {string} keyPath Path to private key
2424 * @property {boolean} whitelistMode If enable whitelist mode
2525 * @property {boolean} avoidLocalhost If avoid using 'localhost' for autorun in auto mode
2626 * @property {boolean} basicAuthMode If enable basic authentication
2727 * @property {boolean} requestProxyEnabled If enable outgoing request proxy
2828 * @property {string} requestProxyUrl Request proxy URL
2929 * @property {string[]} requestProxyBypass Request proxy bypass list
3030 * @property {function(): URL} getIPv4ListenUrl Get IPv4 listen URL
3131 * @property {function(): URL} getIPv6ListenUrl Get IPv6 listen URL
3232 * @property {function(boolean, booleanimport('./server-startup.js').ServerStartupResult): Promise<string>} getAutorunHostname Get autorun hostname
3333 * @property {function(string): URL} getAutorunUrl Get autorun URL
3434 */
3535
3636/**
@@ -220,7 +220,7 @@ export class CommandLineParser {
220220 (':' + this.port),
221221 );
222222 },
223223 getAutorunHostname: async function ({ useIPv6, useIPv4 }) {
224224 if (this.autorunHostname === 'auto') {
225225 let localhostResolve = await canResolve('localhost', useIPv6, useIPv4);
226226
src/server-startup.js+11 -7
@@ -4,6 +4,9 @@ import fs from 'node:fs';
44import { color, urlHostnameToIPv6, getHasIP } from './util.js';
55
66// Express routers
7+import { router as userDataRouter } from './users.js';
8+import { router as usersPrivateRouter } from './endpoints/users-private.js';
9+import { router as usersAdminRouter } from './endpoints/users-admin.js';
710import { router as movingUIRouter } from './endpoints/moving-ui.js';
811import { router as imagesRouter } from './endpoints/images.js';
912import { router as quickRepliesRouter } from './endpoints/quick-replies.js';
@@ -128,6 +131,9 @@ export function redirectDeprecatedEndpoints(app) {
128131 * @param {import('express').Express} app The Express app to use
129132 */
130133export function setupPrivateEndpoints(app) {
134+ app.use('/', userDataRouter);
135+ app.use('/api/users', usersPrivateRouter);
136+ app.use('/api/users', usersAdminRouter);
131137 app.use('/api/moving-ui', movingUIRouter);
132138 app.use('/api/images', imagesRouter);
133139 app.use('/api/quick-replies', quickRepliesRouter);
@@ -274,13 +280,10 @@ export class ServerStartup {
274280
275281 /**
276282 * Handles the case where the server failed to start on one or both protocols.
277283 * @param {booleanServerStartupResult} v6Failed If theresult serverThe failedresults toof startthe onserver IPv6startup
278- * @param {boolean} v4Failed If the server failed to start on IPv4
279- * @param {boolean} useIPv6 If use IPv6
280- * @param {boolean} useIPv4 If use IPv4
281284 * @returns {void}
282285 */
283286 #handleServerListenFail({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
284287 if (v6Failed && !useIPv4) {
285288 console.error(color.red('fatal error: Failed to start server on IPv6 and IPv4 disabled'));
286289 process.exit(1);
@@ -353,7 +356,8 @@ export class ServerStartup {
353356 }
354357
355358 const [v6Failed, v4Failed] = await this.#startHTTPorHTTPS(useIPv6, useIPv4);
356359 this.#handleServerListenFail(const result = { v6Failed, v4Failed, useIPv6, useIPv4) };
357- return { v6Failed, v4Failed, useIPv6, useIPv4 };
360+ this.#handleServerListenFail(result);
361+ return result;
358362 }
359363}