UNPKG

rexuws

Version:

An express-like framework built on top of uWebsocket.js aims at simple codebase and high performance

96 lines (95 loc) 3.2 kB
import { AppOptions, RecognizedString, ListenOptions } from 'uWebSockets.js'; import Request from './Request'; import Response from './Response'; import { TMiddlewareErrorHandler, NextFunction } from './middlewares'; import { HttpMethod } from './utils/types'; import { ILogger } from './Logger'; import { AbstractRoutingParser, IRouteHandler, IUWSRouting, TUWSHandlers, IGetRouteHandlers, TDefaultRoutingFn, IUWSPublish } from './router'; export interface CoreApplicationOptions { /** * Enable default body parser includes json, raw, text, x-www-form-urlencoded, and multipart parser on POST/PATCH/PUT method * * If `true` enable both bodyParser and multipartParser * * @default true */ useDefaultParser?: boolean | { /** * Parse json, raw, text, x-www-form-urlencoded */ bodyParser?: boolean; /** * Parse multipart/ */ multipartParser?: true /** IMultipartParserOptions */; /** * Add method to parse cookie from req.headers.cookie into getter method req.cookies() */ cookieParser?: boolean; }; /** * Set default uWS app options */ uWSConfigurations?: AppOptions; /** * Set logging options */ logger: ILogger; /** * Attach abort handler to all router if forceAsync is true * * Should be `true` when using with NestJS * @default false */ forceAsync?: boolean; /** * Return appication/json on default middleware * * @default false */ preferJSON?: boolean; } export default class App extends AbstractRoutingParser<IRouteHandler, TDefaultRoutingFn> implements IUWSRouting, IUWSPublish { #private; constructor(options: CoreApplicationOptions); /** * @override * @param method * @param path * @param middlewares * @param baseUrl */ protected add(method: HttpMethod, args: unknown[]): this; useNativeHandlers(fn: TUWSHandlers): void; publish(topic: RecognizedString, message: RecognizedString, isBinary?: boolean, compress?: boolean): void; use(path: string, router: IGetRouteHandlers): this; use(middleware: (req: Request, res: Response, next: NextFunction) => void): this; use(middleware: TMiddlewareErrorHandler): this; listen(host: RecognizedString, port: number, cb?: () => void): void; listen(port: number, cb?: () => void): void; listen(port: number, options: ListenOptions, cb?: () => void): void; /** * Save server token to perform graceful shutdown by `app.close()` */ private setToken; private initUWS; private initRoutes; private nextHandler; private render; /** * Set View folder and render method from engine * @param viewPath * @param renderMethod */ setView(viewPath: string, engine: { renderMethod: (...args: any) => any; async?: boolean; extName: string; }): this; setView(viewPath: string, engine: { compileMethod: (...args: any) => any; extName: string; }): this; config(options: CoreApplicationOptions): this; close(cb?: () => void): void; }