Fix comments, update function interfaces
| @@ -18,7 +18,6 @@ import responseTime from 'response-time'; | |||
| 18 | import helmet from 'helmet'; | 18 | import helmet from 'helmet'; |
| 19 | import bodyParser from 'body-parser'; | 19 | import bodyParser from 'body-parser'; |
| 20 | import open from 'open'; | 20 | import open from 'open'; |
| 21 | import fetch from 'node-fetch'; | ||
| 22 | 21 | ||
| 23 | // local library imports | 22 | // local library imports |
| 24 | import { CommandLineParser } from './src/command-line.js'; | 23 | import { 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'; | |||
| 64 | 62 | ||
| 65 | // Routers | 63 | // Routers |
| 66 | import { router as usersPublicRouter } from './src/endpoints/users-public.js'; | 64 | import { 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'; | ||
| 69 | import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js'; | 65 | import { init as statsInit, onExit as statsOnExit } from './src/endpoints/stats.js'; |
| 70 | import { checkForNewContent } from './src/endpoints/content-manager.js'; | 66 | import { checkForNewContent } from './src/endpoints/content-manager.js'; |
| 71 | import { init as settingsInit } from './src/endpoints/settings.js'; | 67 | import { init as settingsInit } from './src/endpoints/settings.js'; |
| @@ -255,17 +251,10 @@ app.get('/api/ping', (request, response) => { | |||
| 255 | }); | 251 | }); |
| 256 | 252 | ||
| 257 | // File uploads | 253 | // File uploads |
| 258 | const uploadsPath = path.join(globalThis.DATA_ROOT, UPLOADS_DIRECTORY); | 254 | const uploadsPath = path.join(cliArgs.dataRoot, UPLOADS_DIRECTORY); |
| 259 | app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar')); | 255 | app.use(multer({ dest: uploadsPath, limits: { fieldSize: 10 * 1024 * 1024 } }).single('avatar')); |
| 260 | app.use(multerMonkeyPatch); | 256 | app.use(multerMonkeyPatch); |
| 261 | 257 | ||
| 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 | |||
| 269 | app.get('/version', async function (_, response) { | 258 | app.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 startup | 324 | * @param {import('./src/server-startup.js').ServerStartupResult} result The result of the server startup |
| 336 | * @returns {Promise<void>} | 325 | * @returns {Promise<void>} |
| 337 | */ | 326 | */ |
| 338 | async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) { | 327 | async 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...'); |
| 342 | 331 | ||
| @@ -348,13 +337,13 @@ async function postSetupTasks({ v6Failed, v4Failed, useIPv6, useIPv4 }) { | |||
| 348 | 337 | ||
| 349 | let logListen = 'SillyTavern is listening on'; | 338 | let logListen = 'SillyTavern is listening on'; |
| 350 | 339 | ||
| 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 | } |
| 356 | 345 | ||
| 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 | ); |
| @@ -4,33 +4,33 @@ import ipRegex from 'ip-regex'; | |||
| 4 | import { canResolve, color, getConfigValue, stringToBool } from './util.js'; | 4 | import { canResolve, color, getConfigValue, stringToBool } from './util.js'; |
| 5 | 5 | ||
| 6 | /** | 6 | /** |
| 7 | * @typedef {object} CommandLineArguments | 7 | * @typedef {object} CommandLineArguments Parsed command line arguments |
| 8 | * @property {string} dataRoot | 8 | * @property {string} dataRoot Data root directory |
| 9 | * @property {number} port | 9 | * @property {number} port Port number |
| 10 | * @property {boolean} listen | 10 | * @property {boolean} listen If SillyTavern is listening on all network interfaces |
| 11 | * @property {string} listenAddressIPv6 | 11 | * @property {string} listenAddressIPv6 IPv6 address to listen to |
| 12 | * @property {string} listenAddressIPv4 | 12 | * @property {string} listenAddressIPv4 IPv4 address to listen to |
| 13 | * @property {boolean|string} enableIPv4 | 13 | * @property {boolean|string} enableIPv4 If enable IPv4 protocol ("auto" is also allowed) |
| 14 | * @property {boolean|string} enableIPv6 | 14 | * @property {boolean|string} enableIPv6 If enable IPv6 protocol ("auto" is also allowed) |
| 15 | * @property {boolean} dnsPreferIPv6 | 15 | * @property {boolean} dnsPreferIPv6 If prefer IPv6 for DNS |
| 16 | * @property {boolean} autorun | 16 | * @property {boolean} autorun If automatically launch SillyTavern in the browser |
| 17 | * @property {string} autorunHostname | 17 | * @property {string} autorunHostname Autorun hostname |
| 18 | * @property {number} autorunPortOverride | 18 | * @property {number} autorunPortOverride Autorun port override (-1 is use server port) |
| 19 | * @property {boolean} enableCorsProxy | 19 | * @property {boolean} enableCorsProxy If enable CORS proxy |
| 20 | * @property {boolean} disableCsrf | 20 | * @property {boolean} disableCsrf If disable CSRF protection |
| 21 | * @property {boolean} ssl | 21 | * @property {boolean} ssl If enable SSL |
| 22 | * @property {string} certPath | 22 | * @property {string} certPath Path to certificate |
| 23 | * @property {string} keyPath | 23 | * @property {string} keyPath Path to private key |
| 24 | * @property {boolean} whitelistMode | 24 | * @property {boolean} whitelistMode If enable whitelist mode |
| 25 | * @property {boolean} avoidLocalhost | 25 | * @property {boolean} avoidLocalhost If avoid using 'localhost' for autorun in auto mode |
| 26 | * @property {boolean} basicAuthMode | 26 | * @property {boolean} basicAuthMode If enable basic authentication |
| 27 | * @property {boolean} requestProxyEnabled | 27 | * @property {boolean} requestProxyEnabled If enable outgoing request proxy |
| 28 | * @property {string} requestProxyUrl | 28 | * @property {string} requestProxyUrl Request proxy URL |
| 29 | * @property {string[]} requestProxyBypass | 29 | * @property {string[]} requestProxyBypass Request proxy bypass list |
| 30 | * @property {function(): URL} getIPv4ListenUrl | 30 | * @property {function(): URL} getIPv4ListenUrl Get IPv4 listen URL |
| 31 | * @property {function(): URL} getIPv6ListenUrl | 31 | * @property {function(): URL} getIPv6ListenUrl Get IPv6 listen URL |
| 32 | * @property {function(boolean, boolean): Promise<string>} getAutorunHostname | 32 | * @property {function(import('./server-startup.js').ServerStartupResult): Promise<string>} getAutorunHostname Get autorun hostname |
| 33 | * @property {function(string): URL} getAutorunUrl | 33 | * @property {function(string): URL} getAutorunUrl Get autorun URL |
| 34 | */ | 34 | */ |
| 35 | 35 | ||
| 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); |
| 226 | 226 | ||
| @@ -4,6 +4,9 @@ import fs from 'node:fs'; | |||
| 4 | import { color, urlHostnameToIPv6, getHasIP } from './util.js'; | 4 | import { color, urlHostnameToIPv6, getHasIP } from './util.js'; |
| 5 | 5 | ||
| 6 | // Express routers | 6 | // 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'; | ||
| 7 | import { router as movingUIRouter } from './endpoints/moving-ui.js'; | 10 | import { router as movingUIRouter } from './endpoints/moving-ui.js'; |
| 8 | import { router as imagesRouter } from './endpoints/images.js'; | 11 | import { router as imagesRouter } from './endpoints/images.js'; |
| 9 | import { router as quickRepliesRouter } from './endpoints/quick-replies.js'; | 12 | import { 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 use | 131 | * @param {import('express').Express} app The Express app to use |
| 129 | */ | 132 | */ |
| 130 | export function setupPrivateEndpoints(app) { | 133 | export 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 { | |||
| 274 | 280 | ||
| 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 IPv6 | 283 | * @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 | } |
| 354 | 357 | ||
| 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 | } |