UNPKG

@sigiljs/sigil

Version:

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

69 lines (68 loc) 3.18 kB
import { Handler } from '@sigiljs/pathfinder'; import { default as http, IncomingMessage } from 'node:http'; import { IncomingRequestProcessorResponse } from '../requests/containers'; import { SigilResponse } from '../responses'; import { Exception } from '../responses/exceptions'; import { MiddlewareModificationRequestOptions } from '../responses/middleware-modification-request'; import { default as SigilPluginSystem } from './sigil-plugin-system'; import { SigilOptions } from './types'; import { Internal } from '../types'; /** * Core request processor for the Sigil framework. * Extends plugin system to handle incoming HTTP messages, * execute route handlers, format responses, and send them over HTTP. * * @template T type of SigilOptions for runtime configuration. */ export default class SigilRequestProcessor<T extends Partial<SigilOptions>> extends SigilPluginSystem<T> { /** * Binds the incoming message handler implementation. */ constructor(options?: T); /** * Entry point for handling HTTP requests. * Delegates to the internal handler reference. * * @param req incoming HTTP message. * @param res HTTP server response object. */ incomingMessageHandler(req: IncomingMessage, res: http.ServerResponse): any; /** * Sends a formatted response back to the client. * Applies the response template, triggers plugin hooks, * logs the request, and writes the HTTP response. * * @param request inbound HTTP message for context. * @param processedRequest processed request * @param response sigilResponse or Exception to send. * @param res HTTP server response object. * @param modification Modified options * @param at timestamp when request processing started. */ protected $sendResponse(request: IncomingMessage, processedRequest: IncomingRequestProcessorResponse | null, response: SigilResponse | Exception, res: http.ServerResponse, modification: MiddlewareModificationRequestOptions, at: number): Promise<void>; /** * Executes a registered route handler safely, converting errors to Exception. * * @param handler pathfinder handler function. * @param req client request object. * @returns handler's response or an Exception on error. */ protected $executeHandler(handler: Handler, req: Internal.Requests.ClientRequest<any>): Promise<Internal.Requests.HandlerResponse>; /** * Formats a raw handler response into a SigilResponse or Exception. * Handles file reading, redirections, and exception wrapping. * * @param response raw handler response. * @returns SigilResponse or Exception ready for sending. */ protected $formatResponse(response: Internal.Requests.HandlerResponse): Promise<SigilResponse | Exception>; /** * Internal implementation for handling incoming HTTP messages. * Parses the request, invokes middleware, routes lookup, and * delegates to handler execution and response sending. * * @param req incoming HTTP message. * @param res HTTP server response object. */ private $incomingMessageHandlerImpl; }