outers
Version:
outers - a all in one package for your day to day use
84 lines (83 loc) • 4.97 kB
TypeScript
import { Express } from "express";
import { ResponseObject } from "../../Config/Interfaces/Cluster/CreateClusterByFunction.interfaces";
export default class CreateClusterByClass {
#private;
/**
* Creates an instance of the server configuration class.
* This constructor initializes the server with the specified Express server, port,
* and optional configurations for workers, middleware, and pre/post listen functions.
*
* @param {Express} ExpressServer - The Express server instance. If not provided, a new Express instance is created.
* @param {number} PORT - The port number on which the Express server will listen.
* @param {number} [NumberOfWorkers=cpus().length] - Optional. The number of worker processes to spawn in cluster mode.
* Defaults to the number of CPU cores available.
* @param {Function[]} [BeforeListenFunctions=[]] - Optional. An array of functions to execute before the server starts listening.
* @param {Function[]} [AfterListenFunctions=[]] - Optional. An array of functions to execute after the server starts listening.
* @param {Function[]} [FunctionMiddlewares=[]] - Optional. An array of middleware functions to apply to the Express server.
*/
constructor(ExpressServer: Express, PORT: number, NumberOfWorkers?: number, BeforeListenFunctions?: any[], AfterListenFunctions?: any[], FunctionMiddlewares?: any[], EnableTrustProxy?: boolean);
/**
* Starts the server using the configured Express instance and port. It initializes
* workers based on the number of workers specified if running in a cluster mode.
* This method also applies any configured middlewares and executes before and after
* listen functions as part of the server startup process.
*
* @returns {Promise<ResponseObject | undefined>} A promise that resolves to a ResponseObject
* containing the server and active worker information if the server starts successfully, or
* undefined if an error occurs.
* @throws {Error} Throws an error if the Express server instance or port is not provided.
*
* The method leverages clustering if `isPrimary` is true, creating workers and managing
* their lifecycle. For the worker processes, or when `isPrimary` is false, it applies
* middlewares, runs before listen functions, starts the Express server, and then runs
* after listen functions.
*
* Note: `Console` is used for logging, and `ClusterConfig`, `platform`, `arch`, `freemem`,
* `cpus` are assumed to be available in the scope for cluster and system information logging.
*/
StartServer(): Promise<ResponseObject | undefined>;
/**
* Sets the number of worker processes to be used in cluster mode.
* This method updates the instance's number of workers with the provided value,
* ensuring that it is a positive number.
*
* @param {number} NumberOfWorkers - The new number of worker processes to be set.
* Must be a positive integer.
* @throws {Error} Throws an error if `NumberOfWorkers` is not provided,
* is not a number, or is less than or equal to zero.
*/
SetNumberOfWorkers(NumberOfWorkers: number): void;
/**
* Adds a function to be executed before the server starts listening.
* This method appends the provided function to the list of functions to run
* before the server starts listening for incoming requests.
*
* @param {Function} FunctionToRun - The function to be added to the list of functions to run.
* @throws {Error} Throws an error if `FunctionToRun` is not provided or is not a function.
*/
AddBeforeListenFunction(FunctionToRun: Function): void;
/**
* Adds a function to be executed after the server starts listening.
* This method appends the provided function to the list of functions to run
* after the server starts listening for incoming requests.
*
* @param {Function} FunctionToRun - The function to be added to the list of functions to run.
* @throws {Error} Throws an error if `FunctionToRun` is not provided or is not a function.
*/
AddAfterListenFunction(FunctionToRun: Function): void;
/**
* Adds a middleware function to the list of middleware functions to be applied
* to the Express server instance.
*
* @param {Function} FunctionToRun - The function to be added to the list of middleware functions to run.
* @throws {Error} Throws an error if `FunctionToRun` is not provided or is not a function.
*/
AddFunctionMiddleware(FunctionToRun: Function): void;
/**
* Controls the Trust Proxy setting.
* @param {boolean} Status - The value indicating whether to enable or disable Trust Proxy.
* @throws {Error} If Trust Proxy is already enabled.
* @throws {Error} If the provided value is not a boolean.
*/
ControlTrustProxy(Status: boolean): void;
}