UNPKG

@sigiljs/sigil

Version:

TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating

74 lines (73 loc) 2.79 kB
import { default as http, IncomingMessage } from 'node:http'; import { Route } from '../route'; import { SigilPlugin } from './misc'; import { SigilMiddlewareCallback } from './misc/sigil-middleware'; import { ServerDefinition, SigilOptions } from './types'; import { ILogOptions } from '../utils/make-log'; /** * Core class for the Sigil framework that initializes an HTTP/S server, * manages middleware, plugins, routes, and response templating. * * @template T type of Sigil runtime options. */ export default abstract class SigilCore<T extends Partial<SigilOptions>> { /** * Bound logger function configured with Sigil debug options. */ readonly logger: (options: ILogOptions) => void; /** * Underlying HTTP or HTTPS server instance, undefined in serverless mode. */ protected $server: ServerDefinition<T>; /** * Reference to the handler function for incoming HTTP messages. */ protected $incomingMessageHandlerRef?: (req: IncomingMessage, res: http.ServerResponse) => any; /** * Registered middleware callbacks, keyed by identifier. */ protected readonly $middlewares: Map<string, SigilMiddlewareCallback>; /** * Registered plugins for extending framework behavior. */ protected $plugins: Map<string, SigilPlugin>; /** * Template function for generating HTTP responses. */ protected readonly $responseTemplate: SigilOptions["responseTemplate"]; /** * Set of registered routes with their mount paths. */ protected readonly $routes: Set<[string, Route<any>]>; /** * Sigil runtime configuration options. */ protected readonly $options: Partial<SigilOptions>; /** * Root router instance for mounting application routes. */ protected readonly $root: Route<undefined, undefined, Record<string, string | undefined>, Record<string, string | undefined>>; /** * Flag indicating whether the framework has completed initialization. */ protected $initialized: boolean; /** * Internal flag to prevent multiple plugin initializations. */ protected $pluginsInitialized: boolean; /** * Constructs the Sigil core framework instance. * Initializes logger, HTTP/S server (unless serverless), response template, * and triggers initial plugin updates in serverless mode. * * @param options configuration options for Sigil (server, debug, etc.). */ protected constructor(options?: T); /** * Callback to initialize and update all registered plugins. * Called once when the server is ready and on subsequent route updates. * * @returns promise resolving after plugin hooks are executed. */ protected $updateCallback(): Promise<void>; }