/** * Portions of this file are adapted from Elysia (MIT, Copyright 2022 saltyAom): * - the AOT compiled-handler body parser that switches on * `contentType.charCodeAt(12)` (Elysia: src/compose.ts) * - the arrow-function source separator using `charCodeAt(0) === 40` * and bracket walking (Elysia: src/sucrose.ts) * - the query-string parser's bit-flag layout and `charCodeAt` switches * for `&` (38), `=` (61), `+` (43), `%` (37) (Elysia: src/parse-query.ts) * * See `packages/tekir-core/NOTICE.md` for the full Elysia license text. */ import { Router } from '../router/router'; import { ExceptionHandler } from '../exceptions/exception_handler'; import { WsManager } from '../ws/index'; import type { MiddlewareFunction, ServerOptions } from '../http/types'; /** * Core HTTP server that compiles routes, manages middleware, WebSocket handlers, * and serves requests via Bun or the Tekir runtime adapter. * * @example * const server = new TekirServer() * server.use([cors(), session()]) * server.configure({ port: 3000, development: true }) * server.start() */ export declare class TekirServer { private router; private exceptionHandler; private wsManager; private server; private options; private _routeCount; private _fallback; private _buildHooks; private _stopHooks; private _staticRoutes; constructor(); /** @returns The server's Router instance */ getRouter(): Router; /** @returns The server's ExceptionHandler instance */ getExceptionHandler(): ExceptionHandler; /** * Get the WebSocket manager to register WS routes. * @example * server.ws().route('/ws/chat', { * open(ws) { ws.subscribe('chat') }, * message(ws, msg) { ws.publish('chat', msg) }, * close(ws) { ws.unsubscribe('chat') }, * }) */ ws(): WsManager; /** * Register server-level middleware that runs on ALL requests (even unmatched routes). * @example * server.use([ * cors(config('cors')), * session(config('session')), * ]) */ use(middleware: MiddlewareFunction | MiddlewareFunction[]): this; /** * Set a fallback handler for requests that do not match any registered route. * @param handler - Function that receives the unmatched request and returns a Response * @returns The server instance for chaining */ fallback(handler: (req: Request) => Response | Promise): this; /** * Register a static route (e.g. HTML page imports) that bypasses the router trie. * @param path - The URL path to serve * @param handler - The route handler (typically a pre-compiled Response or function) * @returns The server instance for chaining */ addStaticRoute(path: string, handler: any): this; /** * Register a hook that runs during the build phase, before the server starts. * @param fn - Async function to execute during build * @returns The server instance for chaining */ onBuild(fn: () => Promise): this; /** Register cleanup owned by server integrations (Vite, Next, etc.). */ onStop(fn: () => void | Promise): this; build(): Promise; /** * Set a custom error handler for uncaught exceptions. * @example * server.errorHandler((error, ctx) => { * return ctx.response.internalServerError({ message: error.message }) * }) */ errorHandler(handler: (error: Error, ctx: any) => any): this; buildRoutes(): Record; configure(options: ServerOptions): this; private _compiledRoutes; private ensureRoutes; handle(request: Request): Promise; /** * Probe the trie with every known method to find any route that matches * `pathname`, regardless of HTTP method. Lets `handle()` recover the * matched pattern (and its params) for a 405 / OPTIONS branch. */ private _matchAnyMethod; /** * List the HTTP methods registered for `pathname` by probing the trie with * each known method. Used to build the `Allow` header on a 405 and to * answer OPTIONS. Returns an empty array when the path matches no route. */ private _allowedMethods; start(): Promise; private _globalHandlersInstalled; /** * Install process-level guards so a single rejected Promise or thrown error * in a fire-and-forget hook / WebSocket handler cannot silently crash the * server. Logs the error and keeps the process alive. Installed in every * mode (not just development), and only once per process. */ private _installGlobalHandlers; /** * stop the server after draining active requests by default. * pass false to close active connections immediately. */ stop(graceful?: boolean): Promise; getServer(): any; }