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';
18import helmet from 'helmet';18import helmet from 'helmet';
19import bodyParser from 'body-parser';19import bodyParser from 'body-parser';
20import open from 'open';20import open from 'open';
21import fetch from 'node-fetch';
2221
23// local library imports22// local library imports
24import { CommandLineParser } from './src/command-line.js';23import { CommandLineParser } from './src/command-line.js';
@@ -36,7 +35,6 @@ import {
36 setUserDataMiddleware,35 setUserDataMiddleware,
37 shouldRedirectToLogin,36 shouldRedirectToLogin,
38 tryAutoLogin,37 tryAutoLogin,
39 router as userDataRouter,
40 cleanUploads,38 cleanUploads,
41 getSessionCookieAge,39 getSessionCookieAge,
42} from './src/users.js';40} from './src/users.js';
@@ -64,8 +62,6 @@ import { ensureThumbnailCache } from './src/endpoints/thumbnails.js';
6462
65// Routers63// Routers
66import { router as usersPublicRouter } from './src/endpoints/users-public.js';64import { router as usersPublicRouter } from './src/endpoints/users-public.js';
67import { router as usersPrivateRouter } from './src/endpoints/users-private.js';
68import { router as usersAdminRouter } from './src/endpoints/users-admin.js';
69import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js';65import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js';
70import { checkForNewContent } from './src/endpoints/content-manager.js';66import { checkForNewContent } from './src/endpoints/content-manager.js';
71import { init as settingsInit } from './src/endpoints/settings.js';67import { init as settingsInit } from './src/endpoints/settings.js';
@@ -255,17 +251,10 @@ app.get('/api/ping', (request, response) => {
255});251});
256252
257// File uploads253// File uploads
258const uploadsPath = path.join(globalThis.DATA_ROOT, UPLOADS_DIRECTORY);254const uploadsPath = path.join(cliArgs.dataRoot, UPLOADS_DIRECTORY);
259app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));255app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar'));
260app.use(multerMonkeyPatch);256app.use(multerMonkeyPatch);
261257
262// User data mount
263app.use('/', userDataRouter);
264// Private endpoints
265app.use('/api/users', usersPrivateRouter);
266// Admin endpoints
267app.use('/api/users', usersAdminRouter);
268
269app.get('/version', async function (_, response) {258app.get('/version', async function (_, response) {
270 const data = await getVersion();259 const data = await getVersion();
271 response.send(data);260 response.send(data);
@@ -335,8 +324,8 @@ async function preSetupTasks() {
335 * @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup324 * @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup
336 * @returns {Promise<void>}325 * @returns {Promise<void>}
337 */326 */
338async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) {327async function postSetupTasks(result) {
339 const autorunHostname = await cliArgs.getAutorunHostname(useIPv6, useIPv4);328 const autorunHostname = await cliArgs.getAutorunHostname(result);
340 const autorunUrl = cliArgs.getAutorunUrl(autorunHostname);329 const autorunUrl = cliArgs.getAutorunUrl(autorunHostname);
341 console.log('Launching...');330 console.log('Launching...');
342331
@@ -348,13 +337,13 @@ async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
348337
349 let logListen = 'SillyTavern is listening on';338 let logListen = 'SillyTavern is listening on';
350339
351 if (useIPv6 && !v6Failed) {340 if (result.useIPv6 && !result.v6Failed) {
352 logListen += color.green(341 logListen += color.green(
353 ' IPv6: ' + cliArgs.getIPv6ListenUrl().host,342 ' IPv6: ' + cliArgs.getIPv6ListenUrl().host,
354 );343 );
355 }344 }
356345
357 if (useIPv4 && !v4Failed) {346 if (result.useIPv4 && !result.v4Failed) {
358 logListen += color.green(347 logListen += color.green(
359 ' IPv4: ' + cliArgs.getIPv4ListenUrl().host,348 ' IPv4: ' + cliArgs.getIPv4ListenUrl().host,
360 );349 );
src/command-line.js+28 -28
@@ -4,33 +4,33 @@ import ipRegex from 'ip-regex';
4import { canResolve, color, getConfigValue, stringToBool } from './util.js';4import { canResolve, color, getConfigValue, stringToBool } from './util.js';
55
6/**6/**
7 * @typedef {object} CommandLineArguments7 * @typedef {object} CommandLineArguments Parsed command line arguments
8 * @property {string} dataRoot8 * @property {string} dataRoot Data root directory
9 * @property {number} port9 * @property {number} port Port number
10 * @property {boolean} listen10 * @property {boolean} listen If SillyTavern is listening on all network interfaces
11 * @property {string} listenAddressIPv611 * @property {string} listenAddressIPv6 IPv6 address to listen to
12 * @property {string} listenAddressIPv412 * @property {string} listenAddressIPv4 IPv4 address to listen to
13 * @property {boolean|string} enableIPv413 * @property {boolean|string} enableIPv4 If enable IPv4 protocol ("auto" is also allowed)
14 * @property {boolean|string} enableIPv614 * @property {boolean|string} enableIPv6 If enable IPv6 protocol ("auto" is also allowed)
15 * @property {boolean} dnsPreferIPv615 * @property {boolean} dnsPreferIPv6 If prefer IPv6 for DNS
16 * @property {boolean} autorun16 * @property {boolean} autorun If automatically launch SillyTavern in the browser
17 * @property {string} autorunHostname17 * @property {string} autorunHostname Autorun hostname
18 * @property {number} autorunPortOverride18 * @property {number} autorunPortOverride Autorun port override (-1 is use server port)
19 * @property {boolean} enableCorsProxy19 * @property {boolean} enableCorsProxy If enable CORS proxy
20 * @property {boolean} disableCsrf20 * @property {boolean} disableCsrf If disable CSRF protection
21 * @property {boolean} ssl21 * @property {boolean} ssl If enable SSL
22 * @property {string} certPath22 * @property {string} certPath Path to certificate
23 * @property {string} keyPath23 * @property {string} keyPath Path to private key
24 * @property {boolean} whitelistMode24 * @property {boolean} whitelistMode If enable whitelist mode
25 * @property {boolean} avoidLocalhost25 * @property {boolean} avoidLocalhost If avoid using 'localhost' for autorun in auto mode
26 * @property {boolean} basicAuthMode26 * @property {boolean} basicAuthMode If enable basic authentication
27 * @property {boolean} requestProxyEnabled27 * @property {boolean} requestProxyEnabled If enable outgoing request proxy
28 * @property {string} requestProxyUrl28 * @property {string} requestProxyUrl Request proxy URL
29 * @property {string[]} requestProxyBypass29 * @property {string[]} requestProxyBypass Request proxy bypass list
30 * @property {function(): URL} getIPv4ListenUrl30 * @property {function(): URL} getIPv4ListenUrl Get IPv4 listen URL
31 * @property {function(): URL} getIPv6ListenUrl31 * @property {function(): URL} getIPv6ListenUrl Get IPv6 listen URL
32 * @property {function(boolean, boolean): Promise<string>} getAutorunHostname32 * @property {function(import('./server-startup.js').ServerStartupResult): Promise<string>} getAutorunHostname Get autorun hostname
33 * @property {function(string): URL} getAutorunUrl33 * @property {function(string): URL} getAutorunUrl Get autorun URL
34 */34 */
3535
36/**36/**
@@ -220,7 +220,7 @@ export class CommandLineParser {
220 (':' + this.port),220 (':' + this.port),
221 );221 );
222 },222 },
223 getAutorunHostname: async function (useIPv6, useIPv4) {223 getAutorunHostname: async function ({ useIPv6, useIPv4 }) {
224 if (this.autorunHostname === 'auto') {224 if (this.autorunHostname === 'auto') {
225 let localhostResolve = await canResolve('localhost', useIPv6, useIPv4);225 let localhostResolve = await canResolve('localhost', useIPv6, useIPv4);
226226
src/server-startup.js+11 -7
@@ -4,6 +4,9 @@ import fs from 'node:fs';
4import { color, urlHostnameToIPv6, getHasIP } from './util.js';4import { color, urlHostnameToIPv6, getHasIP } from './util.js';
55
6// Express routers6// Express routers
7import { router as userDataRouter } from './users.js';
8import { router as usersPrivateRouter } from './endpoints/users-private.js';
9import { router as usersAdminRouter } from './endpoints/users-admin.js';
7import { router as movingUIRouter } from './endpoints/moving-ui.js';10import { router as movingUIRouter } from './endpoints/moving-ui.js';
8import { router as imagesRouter } from './endpoints/images.js';11import { router as imagesRouter } from './endpoints/images.js';
9import { router as quickRepliesRouter } from './endpoints/quick-replies.js';12import { router as quickRepliesRouter } from './endpoints/quick-replies.js';
@@ -128,6 +131,9 @@ export function redirectDeprecatedEndpoints(app) {
128 * @param {import('express').Express} app The Express app to use131 * @param {import('express').Express} app The Express app to use
129 */132 */
130export function setupPrivateEndpoints(app) {133export function setupPrivateEndpoints(app) {
134 app.use('/', userDataRouter);
135 app.use('/api/users', usersPrivateRouter);
136 app.use('/api/users', usersAdminRouter);
131 app.use('/api/moving-ui', movingUIRouter);137 app.use('/api/moving-ui', movingUIRouter);
132 app.use('/api/images', imagesRouter);138 app.use('/api/images', imagesRouter);
133 app.use('/api/quick-replies', quickRepliesRouter);139 app.use('/api/quick-replies', quickRepliesRouter);
@@ -274,13 +280,10 @@ export class ServerStartup {
274280
275 /**281 /**
276 * Handles the case where the server failed to start on one or both protocols.282 * Handles the case where the server failed to start on one or both protocols.
277 * @param {boolean} v6Failed If the server failed to start on IPv6283 * @param {ServerStartupResult} result The results of the server startup
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
281 * @returns {void}284 * @returns {void}
282 */285 */
283 #handleServerListenFail(v6Failed, v4Failed, useIPv6, useIPv4) {286 #handleServerListenFail({ v6Failed, v4Failed, useIPv6, useIPv4 }) {
284 if (v6Failed && !useIPv4) {287 if (v6Failed && !useIPv4) {
285 console.error(color.red('fatal error: Failed to start server on IPv6 and IPv4 disabled'));288 console.error(color.red('fatal error: Failed to start server on IPv6 and IPv4 disabled'));
286 process.exit(1);289 process.exit(1);
@@ -353,7 +356,8 @@ export class ServerStartup {
353 }356 }
354357
355 const [v6Failed, v4Failed] = await this.#startHTTPorHTTPS(useIPv6, useIPv4);358 const [v6Failed, v4Failed] = await this.#startHTTPorHTTPS(useIPv6, useIPv4);
356 this.#handleServerListenFail(v6Failed, v4Failed, useIPv6, useIPv4);359 const result = { v6Failed, v4Failed, useIPv6, useIPv4 };
357 return { v6Failed, v4Failed, useIPv6, useIPv4 };360 this.#handleServerListenFail(result);
361 return result;
358 }362 }
359}363}