From 076e93036909a295b56981ab17edc4760abc269c Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 3 Oct 2024 11:59:26 +0200 Subject: [PATCH] refactor: :rotating_light: Remove process.exit usage --- app.ts | 6 +----- build.ts | 2 +- drizzle/db.ts | 8 ++------ index.ts | 4 ++++ packages/config-manager/config.type.ts | 4 ++++ packages/config-manager/index.ts | 5 +---- setup.ts | 10 --------- utils/init.ts | 28 +++++++++----------------- 8 files changed, 22 insertions(+), 45 deletions(-) diff --git a/app.ts b/app.ts index e74c6ffd..79a2b552 100644 --- a/app.ts +++ b/app.ts @@ -133,11 +133,7 @@ export const appFactory = async () => { serverLogger.fatal`Put your configuration at ${chalk.blueBright( "plugins.", )}`; - serverLogger.fatal`Press Ctrl+C to exit`; - - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); - process.exit(); + throw new Error("Plugin configuration is invalid"); } // biome-ignore lint/complexity/useLiteralKeys: AddToApp is a private method diff --git a/build.ts b/build.ts index 24accba5..f00b07f8 100644 --- a/build.ts +++ b/build.ts @@ -29,7 +29,7 @@ await Bun.build({ }).then((output) => { if (!output.success) { console.error(output.logs); - process.exit(1); + throw new Error("Build failed"); } }); diff --git a/drizzle/db.ts b/drizzle/db.ts index 8c029a0b..19220952 100644 --- a/drizzle/db.ts +++ b/drizzle/db.ts @@ -52,7 +52,6 @@ export const setupDatabase = async (info = true) => { return; } - logger.fatal`${e}`; logger.fatal`Failed to connect to database ${chalk.bold( // Index of the database in the array replicas.indexOf(dbPool) === -1 @@ -60,8 +59,7 @@ export const setupDatabase = async (info = true) => { : `replica-${replicas.indexOf(dbPool)}`, )}. Please check your configuration.`; - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); + throw e; } } @@ -73,11 +71,9 @@ export const setupDatabase = async (info = true) => { migrationsFolder: "./drizzle/migrations", }); } catch (e) { - logger.fatal`${e}`; logger.fatal`Failed to migrate database. Please check your configuration.`; - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); + throw e; } info && logger.info`Database migrated`; diff --git a/index.ts b/index.ts index 11f887e5..62f49926 100644 --- a/index.ts +++ b/index.ts @@ -4,6 +4,10 @@ import { createServer } from "@/server"; import { appFactory } from "~/app"; import { config } from "~/packages/config-manager/index"; +process.on("SIGINT", () => { + process.exit(); +}); + if (cluster.isPrimary) { for (let i = 0; i < Number(process.env.NUM_CPUS ?? 1); i++) { cluster.fork(); diff --git a/packages/config-manager/config.type.ts b/packages/config-manager/config.type.ts index 0e4877e1..861927cc 100644 --- a/packages/config-manager/config.type.ts +++ b/packages/config-manager/config.type.ts @@ -150,6 +150,10 @@ export const configValidator = z.object({ enabled: false, address: "", }) + .refine( + (arg) => !arg.enabled || !!arg.address, + "When proxy is enabled, address must be set", + ) .transform((arg) => ({ ...arg, address: arg.enabled ? arg.address : undefined, diff --git a/packages/config-manager/index.ts b/packages/config-manager/index.ts index 3d423988..35e47925 100644 --- a/packages/config-manager/index.ts +++ b/packages/config-manager/index.ts @@ -23,10 +23,7 @@ const parsed = await configValidator.safeParseAsync(config); if (!parsed.success) { console.error("Invalid config file:"); - console.error(fromZodError(parsed.error).message); - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); - process.exit(); + throw fromZodError(parsed.error).message; } const exportedConfig = parsed.data; diff --git a/setup.ts b/setup.ts index a7792800..64f9a4e7 100644 --- a/setup.ts +++ b/setup.ts @@ -20,10 +20,6 @@ if (config.sonic.enabled) { await searchManager.connect(); } -process.on("SIGINT", () => { - process.exit(); -}); - // Check if database is reachable const postCount = await Note.getCount(); @@ -34,12 +30,6 @@ serverLogger.info`Versia Server started at ${config.http.bind}:${config.http.bin serverLogger.info`Database is online, now serving ${postCount} posts`; if (config.frontend.enabled) { - if (!URL.canParse(config.frontend.url)) { - serverLogger.error`Frontend URL is not a valid URL: ${config.frontend.url}`; - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); - } - // Check if frontend is reachable const response = await fetch(new URL("/", config.frontend.url)) .then((res) => res.ok) diff --git a/utils/init.ts b/utils/init.ts index 48cd78da..5ced90be 100644 --- a/utils/init.ts +++ b/utils/init.ts @@ -17,13 +17,6 @@ const checkHttpProxyConfig = async (config: Config) => { const logger = getLogger("server"); if (config.http.proxy.enabled) { - if (!config.http.proxy.address) { - logger.fatal`The HTTP proxy is enabled, but the proxy address is not set in the config`; - - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); - } - logger.info`HTTP proxy enabled at ${chalk.gray(config.http.proxy.address)}, testing...`; // Test the proxy @@ -37,10 +30,9 @@ const checkHttpProxyConfig = async (config: Config) => { logger.info`Your IPv4 address is ${chalk.gray(ip)}`; if (!response.ok) { - logger.fatal`The HTTP proxy is enabled, but the proxy address is not reachable`; - - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); + throw new Error( + "The HTTP proxy is enabled, but the proxy address is not reachable", + ); } } }; @@ -127,10 +119,9 @@ const checkOidcConfig = async (config: Config) => { .catch((e) => e as Error); if (privateKey instanceof Error || publicKey instanceof Error) { - logger.fatal`The OpenID keys could not be imported! You may generate a new one by removing the old ones from config and restarting the server (this will invalidate all current JWTs).`; - - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); + throw new Error( + "The OpenID keys could not be imported! You may generate a new one by removing the old ones from config and restarting the server (this will invalidate all current JWTs).", + ); } }; @@ -174,9 +165,8 @@ const checkFederationConfig = async (config: Config) => { .catch((e) => e as Error); if (privateKey instanceof Error || publicKey instanceof Error) { - logger.fatal`The federation keys could not be imported! You may generate new ones by removing the old ones from the config and restarting the server.`; - - // Hang until Ctrl+C is pressed - await Bun.sleep(Number.POSITIVE_INFINITY); + throw new Error( + "The federation keys could not be imported! You may generate new ones by removing the old ones from the config and restarting the server.", + ); } };