[chore] Fix grammar, add JSDocs

cb7185fa125468716f656f31d637c0e282ee2b6c

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

3 files changed, +61 -54Showing whitespace changes
default/config.yaml+8 -4
@@ -4,17 +4,21 @@ dataRoot: ./data
44# -- SERVER CONFIGURATION --
55# Listen for incoming connections
66listen: false
77# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
88protocol:
99 ipv4: true
1010 ipv6: false
1111# Prefers IPv6 for dns, you should probablyDNS. enableEnable this on ISPs that don't have issues with IPv6
1212dnsPreferIPv6: false
13-# the hostname that autorun opens probably best left on auto. use options like 'localhost', 'st.example.com'
13+# The hostname that autorun opens.
14+# - Use "auto" to let the server decide
15+# - Use options like 'localhost', 'st.example.com'
1416autorunHostname: "auto"
1517# Server port
1618port: 8000
17-# overrides the port for autorun with open your browser with this port and ignore what port the server is running on. -1 is use server port
19+# Overrides the port for autorun in browser.
20+# - Use -1 to use the server port.
21+# - Specify a port to override the default.
1822autorunPortOverride: -1
1923# -- SECURITY CONFIGURATION --
2024# Toggle whitelist mode
server.js+32 -50
@@ -43,6 +43,8 @@ const {
4343 getConfigValue,
4444 color,
4545 forwardFetchResponse,
46+ removeColorFormatting,
47+ getSeparator,
4648} = require('./src/util');
4749const { ensureThumbnailCache } = require('./src/endpoints/thumbnails');
4850
@@ -54,10 +56,6 @@ if (process.versions && process.versions.node && process.versions.node.match(/20
5456 if (net.setDefaultAutoSelectFamily) net.setDefaultAutoSelectFamily(false);
5557}
5658
57-
58-
59-
60-
6159const DEFAULT_PORT = 8000;
6260const DEFAULT_AUTORUN = false;
6361const DEFAULT_LISTEN = false;
@@ -618,7 +616,6 @@ const tavernUrl = new URL(
618616 (':' + server_port),
619617);
620618
621-
622619/**
623620 * Tasks that need to be run before the server starts listening.
624621 */
@@ -667,19 +664,11 @@ const preSetupTasks = async function () {
667664 });
668665};
669666
670-function removeColorFormatting(text) {
667+/**
671- // ANSI escape codes for colors are usually in the format \x1b[<codes>m
668+ * Gets the hostname to use for autorun in the browser.
672- return text.replace(/\x1b\[\d{1,2}(;\d{1,2})*m/g, '');
669+ * @returns {string} The hostname to use for autorun
673-}
670+ */
674-
675-function getSeparator(n) {
676- return '='.repeat(n);
677-}
678-
679-
680-
681671function getAutorunHostname() {
682-
683672 if (autorunHostname === 'auto') {
684673 if (enableIPv6 && enableIPv4) {
685674 if (avoidLocalhost) return '[::1]';
@@ -698,13 +687,12 @@ function getAutorunHostname() {
698687 return autorunHostname;
699688}
700689
701-
702690/**
703691 * Tasks that need to be run after the server starts listening.
692+ * @param {boolean} v6Failed If the server failed to start on IPv6
693+ * @param {boolean} v4Failed If the server failed to start on IPv4
704694 */
705695const postSetupTasks = async function (v6Failed, v4Failed) {
706-
707-
708696 const autorunUrl = new URL(
709697 (cliArguments.ssl ? 'https://' : 'http://') +
710698 (getAutorunHostname()) +
@@ -712,30 +700,24 @@ const postSetupTasks = async function (v6Failed, v4Failed) {
712700 ((autorunPortOverride >= 0) ? autorunPortOverride : server_port),
713701 );
714702
715-
716703 console.log('Launching...');
717704
718705 if (autorun) open(autorunUrl.toString());
719706
720707 setWindowTitle('SillyTavern WebServer');
721708
722-
723- let ipv6Color = color.green;
724- let ipv4Color = color.green;
725- let autorunColor = color.blue;
726-
727709 let logListen = 'SillyTavern is listening on';
728710
729711 if (enableIPv6 && !v6Failed) {
730712 logListen += ipv6Colorcolor.green(' IPv6: ' + tavernUrlV6.host);
731713 }
732714
733715 if (enableIPv4 && !v4Failed) {
734716 logListen += ipv4Colorcolor.green(' IPv4: ' + tavernUrl.host);
735717 }
736718
737719 letconst goToLog = 'Go to: ' + autorunColorcolor.blue(autorunUrl) + ' to open SillyTavern';
738720 letconst plainGoToLog = removeColorFormatting(goToLog);
739721
740722 console.log(logListen);
741723 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
@@ -798,8 +780,11 @@ function logSecurityAlert(message) {
798780 process.exit(1);
799781}
800782
801-
783+/**
802-
784+ * Handles the case where the server failed to start on one or both protocols.
785+ * @param {boolean} v6Failed If the server failed to start on IPv6
786+ * @param {boolean} v4Failed If the server failed to start on IPv4
787+ */
803788function handleServerListenFail(v6Failed, v4Failed) {
804789 if (v6Failed && !enableIPv4) {
805790 console.error('fatal error: Failed to start server on IPv6 and IPv4 disabled');
@@ -817,7 +802,12 @@ function handleServerListenFail(v6Failed, v4Failed) {
817802 }
818803}
819804
820-
805+/**
806+ * Creates an HTTPS server.
807+ * @param {URL} url The URL to listen on
808+ * @returns {Promise<void>} A promise that resolves when the server is listening
809+ * @throws {Error} If the server fails to start
810+ */
821811function createHttpsServer(url) {
822812 return new Promise((resolve, reject) => {
823813 const server = https.createServer(
@@ -831,6 +821,12 @@ function createHttpsServer(url) {
831821 });
832822}
833823
824+/**
825+ * Creates an HTTP server.
826+ * @param {URL} url The URL to listen on
827+ * @returns {Promise<void>} A promise that resolves when the server is listening
828+ * @throws {Error} If the server fails to start
829+ */
834830function createHttpServer(url) {
835831 return new Promise((resolve, reject) => {
836832 const server = http.createServer(app);
@@ -840,17 +836,11 @@ function createHttpServer(url) {
840836 });
841837}
842838
843-
844-
845-
846839async function startHTTPorHTTPS() {
847840 let v6Failed = false;
848841 let v4Failed = false;
849842
850843 letconst createFunc = cliArguments.ssl ? createHttpsServer : createHttpServer;
851- if (cliArguments.ssl) {
852- createFunc = createHttpsServer;
853- }
854844
855845 if (enableIPv6) {
856846 try {
@@ -875,25 +865,17 @@ async function startHTTPorHTTPS() {
875865 v4Failed = true;
876866 }
877867 }
868+
878869 return [v6Failed, v4Failed];
879870}
880871
881-
882-
883-
884872async function startServer() {
885873 letconst [v6Failed, v4Failed] = falseawait startHTTPorHTTPS();
886- let v4Failed = false;
887-
888-
889- [v6Failed, v4Failed] = await startHTTPorHTTPS();
890874
891875 handleServerListenFail(v6Failed, v4Failed);
892876 postSetupTasks(v6Failed, v4Failed);
893877}
894878
895-
896-
897879async function verifySecuritySettings() {
898880 // Skip all security checks as listen is set to false
899881 if (!listen) {
src/util.js+21 -0
@@ -627,6 +627,25 @@ class Cache {
627627 }
628628}
629629
630+/**
631+ * Removes color formatting from a text string.
632+ * @param {string} text Text with color formatting
633+ * @returns {string} Text without color formatting
634+ */
635+function removeColorFormatting(text) {
636+ // ANSI escape codes for colors are usually in the format \x1b[<codes>m
637+ return text.replace(/\x1b\[\d{1,2}(;\d{1,2})*m/g, '');
638+}
639+
640+/**
641+ * Gets a separator string repeated n times.
642+ * @param {number} n Number of times to repeat the separator
643+ * @returns {string} Separator string
644+ */
645+function getSeparator(n) {
646+ return '='.repeat(n);
647+}
648+
630649module.exports = {
631650 getConfig,
632651 getConfigValue,
@@ -654,4 +673,6 @@ module.exports = {
654673 trimV1,
655674 Cache,
656675 makeHttp2Request,
676+ removeColorFormatting,
677+ getSeparator,
657678};