ziron-server
Version:
104 lines (103 loc) • 3.84 kB
TypeScript
import { HttpResponse as RawHttpResponse } from "ziron-ws";
export declare const enum HttpResponseState {
Available = 0,
Reserved = 1,
Aborted = 2,
Ended = 3
}
export interface HttpResponse extends RawHttpResponse {
/**
* @description
* This object can be used to set the headers of the response.
* Notice that writeHeaders must be called afterwards to write the headers from the object.
*/
readonly headers: Record<string, string>;
readonly writeHeader: any;
/**
* @description
* Writes all headers from the headers object.
* Optionally you can pass additional headers as the first parameter.
*/
readonly writeHeaders: (extraHeaders?: Record<string, string>) => HttpResponse;
/**
* @description
* The current state of the response can be either available, reserved, aborted or ended.
* The response also provides the two getters: available and closed to check the state.
* You should check if the response is still available whenever you want to use the response.
* Especially after an async operation, the response might have been aborted in the meantime.
*/
readonly state: HttpResponseState;
/**
* @description
* Indicates if the response state is available.
* It is important to check that the response is available whenever you want to use the response.
* Especially after an async operation, the response might have been aborted in the meantime.
*/
readonly available: boolean;
/**
* @description
* Indicates if the response state is aborted or ended.
* This getter can be helpful if you have reserved the response and
* want to check if the response was not aborted or ended in
* the meantime before each async write.
* Notice that the available getter would return false because
* the response state is reserved by yourself.
*/
readonly closed: boolean;
/**
* @description
* Reserves the response.
* If it is ensured that the response will end async in this execution and
* you want to avoid others from using the response,
* the response can be reserved by using this function.
* Notice to use the closed getter to check if the response is not ended or aborted
* instead of the available getter because the available getter would return
* false when the response state is reserved by yourself.
*/
readonly reserve: () => void;
/**
* @description
* Sends back a file and finishes the response.
*/
readonly writeFile: (path: string, reqHeaders?: {
'if-modified-since'?: string;
range?: string;
'accept-encoding'?: string;
}, handleLastModified?: boolean) => Promise<void>;
/**
* @description
* Sends a redirect to another location and finishes the response.
*/
readonly redirect: (location: string) => void;
}
export default function enhanceHttpResponse(res: RawHttpResponse & Partial<HttpResponse>): HttpResponse;
/**
* @description
* Write response headers utility function.
* @param res
* @param headers
*/
export declare function writeResponseHeaders(res: RawHttpResponse, headers?: {
[name: string]: string;
}): void;
/**
* @description
* Redirects to another location.
* @param res
* @param location
*/
export declare function writeResponseRedirection(res: RawHttpResponse, location: string): void;
/**
* @description
* Streams a file in a response.
* Also handles last modified.
* @param res
* @param path
* @param reqHeaders
* @param handleLastModified
*/
export declare function sendFileToRes(res: HttpResponse, path: string, reqHeaders?: {
'if-modified-since'?: string;
range?: string;
'accept-encoding'?: string;
}, handleLastModified?: boolean): Promise<void>;