UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

182 lines (181 loc) 5.47 kB
/// <reference types="node" /> import Cookie from "../../../classes/Cookie"; import Middleware, { UsableMiddleware } from "../../../classes/Middleware"; import Route from "../../../classes/Route"; import RuntimeError from "../../../classes/RuntimeError"; import Server from "../../../classes/Server"; import URLObject from "../../../classes/URLObject"; import ValueCollection from "../../../classes/ValueCollection"; import HttpRequestContext from "../../../classes/request/HttpRequestContext"; import { Content, ParsedBody } from "../../global"; import { HttpContext } from "../../implementation/contexts/http"; import GlobalContext from "./GlobalContext"; export default class RequestContext<MiddlewareData extends Record<any, any> = any> { private context; private middlewares; private server; protected executeSelf: () => Promise<boolean> | boolean; private middlewareData; private started; private aborted; constructor(context: HttpContext, middlewares: UsableMiddleware[], server: Server<any>, global: GlobalContext); /** * The URL Object * @since 9.0.0 */ url: URLObject; /** * The Headers of the URL Path * @since 9.0.0 */ headers: ValueCollection<string, string>; /** * The Params of the URL Path * @since 9.0.0 */ params: ValueCollection<string, string, string>; /** * The Cookies of the Request * @since 9.0.0 */ cookies: ValueCollection<string, string, Cookie> | null; /** * The Queries of the Request * @since 9.0.0 */ queries: ValueCollection<string, string> | null; /** * The Fragments of the Request * @since 9.0.0 */ fragments: ValueCollection<string, string> | null; /** * The Error this request encountered * @since 9.0.0 */ error: RuntimeError | null; /** * The Type of the Request * @since 9.0.0 */ type: 'http' | 'ws'; /** * Whether the EndFn was called * @since 9.0.0 */ endFn: boolean; /** * Whether the Request is chunked * @since 9.0.0 */ chunked: boolean; /** * The File to print at the end of the request * @warn Only used in static routes, do not use in other routes * @since 9.0.0 */ file: string | null; /** * Set Code to execute at the end end of the request, * return a boolean describing if you want the normal request end to proceed too * @since 9.0.0 */ setExecuteSelf(callback: () => Promise<boolean> | boolean): this; /** * Handle an Error, automatically sets status and calls callback * @since 9.0.0 */ handleError(error: unknown, cause: string): RuntimeError; /** * Initiate Request Abortion * @since 9.0.0 */ abort(ctr?: HttpRequestContext<any>): Promise<boolean>; /** * Retrieve Middleware Data * @since 9.0.0 */ data(middleware: Middleware | UsableMiddleware<any, any, any, any, any> | { use(...args: any[]): UsableMiddleware<any, any, any, any, any>; }): MiddlewareData; /** * Await the Body * @since 9.0.0 */ awaitBody(ctr: HttpRequestContext<any>, concat?: boolean): Promise<Buffer>; /** * Get the ms elapsed since the request started * @since 9.0.0 */ elapsed(reset?: boolean): number; /** * Body / Ws Message Data * @since 9.0.0 */ body: { /** * The Chunks of the Body * @since 9.0.0 */ chunks: Buffer[]; /** * Callbacks that get called when full body is resolved * @since 9.0.0 */ callbacks: ((raw: Buffer) => void)[]; /** * The Raw Data of the Body * @since 9.0.0 */ raw: Buffer | null; /** * The Parsed Data of the Body * @since 9.0.0 */ parsed: ParsedBody; /** * Whether the Body is being awaited * @since 9.0.0 */ awaiting: boolean; /** * The Type of the parsed Body * @since 9.0.0 */ type: 'json' | 'url-encoded' | 'raw'; }; /** * IP Information * @since 9.0.0 */ ip: { /** * Whether the IP is Proxied * @since 9.0.0 */ isProxied: boolean; /** * The Value of the IP * @since 9.0.0 */ value: string; /** * The Port that the IP is using * @since 9.0.0 */ port: number; }; /** * The Global Context * @since 9.0.0 */ global: GlobalContext; /** * The Route, if found * @since 9.0.0 */ route: Route<'http'> | Route<'ws'> | Route<'static'> | null; /** * The Response Data * @since 9.0.0 */ response: { /** * The Status Code to send * @default 200 * @since 9.0.0 */ status: number; /** * The Status Text to send * @default null * @since 9.0.0 */ statusText: string | null; /** * The Headers to send * @since 9.0.0 */ headers: ValueCollection<string, string>; /** * The Cookies to add * @since 9.0.0 */ cookies: ValueCollection<string, Cookie>; /** * The Content to send * @default [] * @since 9.0.0 */ content: Content; /** * Whether to prettify the content * @default false * @since 9.0.0 */ prettify: boolean; }; }