UNPKG

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

130 lines (129 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.configSchema = exports.serverInstanceSchema = exports.routesSchema = void 0; const zod_1 = require("zod"); /** * Schema for validating hostnames, including: * - `localhost` with optional port. * - URLs starting with `http://` or `https://` followed by a valid domain and optional port. * * The hostname can also include an optional path and query string. * * @example * hostnameSchema.parse("http://localhost:8080"); */ const hostnameSchema = zod_1.z .string() .regex(/^(localhost(:\d{1,5})?|https?:\/\/([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+(:\d{1,5})?)(\/[a-zA-Z0-9-._~:/?#[\]@!$&'()*+,;%=]*)?$/, "Invalid hostname format"); /** * Schema for validating the `routes` configuration. * * The `routes` array consists of objects, each with: * - `path`: The URL path (e.g., `/api`). * - `destination`: The destination hostname, validated using `hostnameSchema`. * * @example * routesSchema.parse([{ path: "/api", destination: "http://localhost:8080" }]); */ exports.routesSchema = zod_1.z.array(zod_1.z.object({ path: zod_1.z.string(), destination: hostnameSchema, })); /** * Schema for validating a server instance configuration. * * Each `serverInstance` object includes: * - `port`: The port the server should listen on, within the valid range (0-65535). * - `sslConfig`: Optional SSL configuration (including `cert` and `key` paths). * - `public`: Optional path to serve static files from. * - `routes`: Optional routes configuration (validated using `routesSchema`). * * @example * serverInstanceSchema.parse({ * port: 80, * sslConfig: { cert: "cert.pem", key: "key.pem" }, * public: "/path/to/static", * routes: [{ path: "/api", destination: "http://localhost:8080" }] * }); */ exports.serverInstanceSchema = zod_1.z.object({ /** * The port the server should listen on (0-65535). * * @type {number} */ port: zod_1.z.number().min(0).max(65535), /** * The SSL configuration for the server (optional). * * The `sslConfig` object includes: * - `cert`: Path to the SSL certificate. * - `key`: Path to the SSL key. * * @type {{ cert: string, key: string } | undefined} */ sslConfig: zod_1.z .object({ cert: zod_1.z.string(), key: zod_1.z.string(), }) .optional(), /** * The path to the public directory to serve static files (optional). * * @type {string | undefined} */ public: zod_1.z.string().optional(), /** * The routes configuration (optional). * * The `routes` array is validated using `routesSchema`. * * @type {Array<{ path: string, destination: string }> | undefined} */ routes: exports.routesSchema.optional(), }); /** * Schema for validating the main configuration object (`configSchema`). * * The `configSchema` object includes: * - `workers`: The number of workers, or "auto" for automatic worker count. * - `server`: The server configuration, including the list of `server instances`. * - `upstream`: Optional list of upstream hostnames. * - `initUpstream`: Optional index to select the initial upstream server. * * @example * configSchema.parse({ * workers: "auto", * server: { instances: [{ port: 80, routes: [{ path: "/api", destination: "http://localhost:8080" }] }] }, * upstream: ["http://localhost:8081"], * }); */ exports.configSchema = zod_1.z.object({ /** * The number of workers or "auto" for automatic worker count. * * @type {number | "auto"} */ workers: zod_1.z.union([zod_1.z.number(), zod_1.z.literal("auto")]), /** * The server configuration, including instances. * * @type {{ instances: Array<typeof serverInstanceSchema> }} */ server: zod_1.z.object({ instances: zod_1.z.array(exports.serverInstanceSchema), }), /** * The upstream hostnames for load balancing (optional). * * @type {string[] | undefined} */ upstream: zod_1.z.array(hostnameSchema).optional(), /** * The index of the initial upstream server to use (optional). * * @type {number | undefined} */ initUpstream: zod_1.z.number().optional(), });