rex-server
Version:
Rex Server is a Node.js-based reverse proxy server available as an npm package. It allows you to handle HTTP and HTTPS traffic, route requests to upstream servers, and manage worker processes efficiently. With its CLI interface, Rex makes it easy to confi
119 lines (118 loc) • 5.05 kB
TypeScript
import { REX_CONFIG } from "../types/index";
/**
* Parses the command-line arguments to extract the configuration.
*
* This function searches the command-line arguments for a specified key (e.g., `--config`) and returns the parsed configuration
* if found. If no matching key is found, it returns `false`.
*
* @param {string[]} args - The command-line arguments.
* @param {string} argKey - The key to search for in the arguments (e.g., `--config`).
*
* @returns {REX_CONFIG | false} - The parsed configuration if found, or `false` otherwise.
*
* @example
* const config = parseCliArgs(process.argv, '--config');
* if (config) {
* console.log(config);
* }
*/
export declare function parseCliArgs(args: string[], argKey: string): REX_CONFIG | false;
/**
* Checks if the current process has the necessary privileges to bind to privileged ports.
*
* On Linux systems, binding to ports below 1024 requires elevated privileges. This function checks if the process has the
* required privileges to bind to such ports.
*
* @returns {Promise<boolean>} A promise that resolves to `true` if the process has the necessary privileges, or `false` otherwise.
*
* @example
* checkPortPrivilege().then((hasPriv) => {
* if (hasPriv) {
* console.log("Process has port binding privileges.");
* } else {
* console.log("Process does not have port binding privileges.");
* }
* });
*/
export declare function checkPortPrivilege(): Promise<boolean>;
/**
* Provides instructions on how to obtain the required port binding privileges based on the OS platform.
*
* This function checks the platform of the system and provides instructions on how to obtain the necessary permissions
* to bind to privileged ports.
*
* @example
* provideInstructions();
*/
export declare function provideInstructions(): void;
/**
* Parses and validates a YAML configuration file for the Rex server.
*
* This function reads a YAML configuration file, validates it using the `zod` schema, and returns the parsed configuration.
* If the configuration is invalid or missing required fields (e.g., SSL configuration for port 443), an error is thrown.
*
* @param {string} configPath - The path to the YAML configuration file.
* @returns {Promise<REX_CONFIG>} A promise that resolves with the parsed and validated configuration.
*
* @throws {Error} Throws an error if the configuration is invalid or missing required fields.
*
* @example
* configParser('/path/to/config.yaml').then(config => {
* console.log('Config parsed:', config);
* }).catch(err => {
* console.error('Error parsing config:', err);
* });
*/
export declare function configParser(configPath?: string): Promise<void | {
workers: number | "auto";
server: {
instances: {
port: number;
sslConfig?: {
cert: string;
key: string;
} | undefined;
public?: string | undefined;
routes?: {
path: string;
destination: string;
}[] | undefined;
}[];
};
upstream?: string[] | undefined;
initUpstream?: number | undefined;
}>;
/**
* Checks if the file has the necessary read permissions.
*
* This function checks if the specified file path has read permissions. If not, it throws an error with the provided message.
*
* @param {string} path - The file path to check.
* @param {string} errMsg - The error message to throw if the file lacks read permissions.
*
* @returns {Promise<boolean>} A promise that resolves if the file has read permissions, or rejects with an error message.
*
* @example
* checkFilesAccessibilty('/path/to/file', 'File lacks necessary read permissions')
* .then(() => console.log('File is accessible'))
* .catch((err) => console.error(err));
*/
export declare function checkFilesAccessibilty(path: string, errMsg: string): Promise<unknown>;
/**
* Exports a file from a source to a destination.
*
* This function copies a file from the source path to the destination file, and provides callback hooks for when the operation
* is ready, successful, or encounters an error.
*
* @param {string} sourceFilePath - The path to the source file.
* @param {string} destinationFileName - The name of the destination file.
* @param {Function} [onClose] - A callback function to be invoked when the file has been successfully written.
* @param {Function} [onError] - A callback function to be invoked if an error occurs during the file operation.
* @param {Function} [onOpen] - A callback function to be invoked when the file operation is ready to begin.
*
* @example
* fileExporter('/path/to/source/file', 'destination-file.txt')
* .then(() => console.log('File exported successfully'))
* .catch((err) => console.error('Error exporting file:', err));
*/
export declare function fileExporter(sourceFilePath: string, destinationFileName: string, onClose?: (destinationFilePath: string) => void, onError?: (err: any) => void, onOpen?: () => void): void;