UNPKG

rexuws

Version:

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

75 lines (74 loc) 2.99 kB
/// <reference types="node" /> import { HttpResponse, RecognizedString } from 'uWebSockets.js'; import { ReadStream } from 'fs'; import { HAS_ASYNC, FROM_REQ, READ_STREAM, WRITE_STATUS, WRITE_HEADER, TRY_END, END, GET_WRITE_OFFSET, GET_PROXIED_ADDR, GET_REMOTE_ADDR, ON_ABORTED, ON_WRITABLE, CORK, FROM_APP, NEXT } from './utils/symbol'; import { ILogger } from './Logger'; import { TRequestExposedMethods, IResponseSendFileOption as IResponseSendFileOptions, IResponse, CookieOptions, TApplicationExposedMethods } from './utils/types'; import { NextFunction } from './middlewares'; export interface IResponseOptions { /** * Specify if this route is async */ hasAsync?: boolean; /** * Speficy the limit of file's size allowed to be directly read into buffer rather than a ReadStream * * @default 100kb */ maxReadFileSize?: number; } export interface IResponseHeader { name: string; value: string; } export default class Response implements IResponse { [x: string]: unknown; [NEXT]: NextFunction; [FROM_APP]: TApplicationExposedMethods; private _statusCode?; private _headers; locals: Record<string, any>; originalRes: HttpResponse; private [WRITE_STATUS]; private [WRITE_HEADER]; private [END]; private [TRY_END]; private [GET_WRITE_OFFSET]; private [ON_WRITABLE]; private [ON_ABORTED]; private [GET_REMOTE_ADDR]; private [GET_PROXIED_ADDR]; [CORK]: (cb: () => void) => void; [FROM_REQ]: TRequestExposedMethods; getHeader: (field: string) => string | undefined; set: (field: Record<string, string | string[]> | string, val?: string) => this; header: (field: Record<string, string | string[]> | string, val?: string) => this; contentType: (type: string) => this; type: (type: string) => this; private [HAS_ASYNC]; private debug; private maxReadFileSize; private [READ_STREAM]?; private _cookies; constructor(res: HttpResponse, opts?: IResponseOptions, logger?: ILogger); get preStatus(): number | undefined; get preHeader(): IResponseHeader[]; private attachAbortHandler; private setHeaderAndStatusByNativeMethod; private setHeader; private setType; get(field: string): string | undefined; getHeaders(): IResponseHeader[]; status(code: number): this; sendStatus(code: number): void; send(body: string | Record<string, unknown>): void; json(body: any): void; location(url: string): this; redirect(url: string): void; redirect(status: number, url: string): void; sendFile(path: string | Buffer | ReadStream, options?: IResponseSendFileOptions | ((err: any) => void), cb?: (err: any) => void): void; cookie(name: string, val: any, options?: CookieOptions): this; download(path: string, fileName?: string, options?: {}): void; render(view: string, options?: any, callback?: any): void; end(body?: RecognizedString, hasAsync?: boolean): void; }