///
import { Filter, With } from "./filter";
import { ServerOptions as HttpOptions, Server as HttpServer, IncomingMessage, OutgoingHttpHeaders, ServerResponse } from "http";
import { ServerOptions as HttpsOptions } from "https";
/**
* Represents an incoming HTTP request
*/
export interface Request extends IncomingMessage {
pathSegments: string[];
}
/**
* Represents an outgoing HTTP reply
*/
export interface Reply {
/**
* Status code
*/
status: number;
/**
* Headers
*/
headers: OutgoingHttpHeaders;
/**
* Response body
*/
body: Buffer | string | undefined;
}
/**
* Writes a [[Reply]] to Node's `http.ServerResponse`
*
* @param reply - The reply to write
* @param response - The response to write to
* @param body - Whether to write the provided body
*/
export declare function writeReply(reply: Reply, response: ServerResponse, body?: boolean): void;
/** Abstraction over Node's `http.Server` to handle filters */
export declare class Server {
/**
* Inner `http.Server`
*/
readonly inner: HttpServer;
/**
* Runs the server
*
* If an error occurs while running, the server will automatically be closed.
*
* @param port - Port to listen on
* @param hostname - Address to listen on (defaults to localhost)
*
* @returns Will resolve when the server closes, will reject if an error occurs while running
*/
run(port: number, hostname?: string): Promise;
/**
* Starts the server
*
* The main difference between this method and [[`run`]] is that
* the promise returned by [[`run`]] only resolves once the server closes, while
* the promise return by this method will resolve as soon as the server starts litening.
*
* This method never throws, you are responsible for handling server errors when using it.
*
* @param port - Port to listen on
* @param hostname - Address to listen on (defaults to localhost)
*
* @returns Will resolve when the server starts listening
*/
listen(port: number, hostname?: string): Promise;
/**
* Closes the server
*
* @returns Will resolve when the server closes, will reject if an error occurs while closing
*/
close(): Promise;
}
/**
* Creates a server serving the provided filter
*
* @param filter - Filter to serve
*/
export declare function serve(filter: Filter<[Reply]>): Server;
/**
* Creates a server with the provided options serving the provided filter
*
* @param filter - Filter to serve
* @param options - Options for the inner `http.Server` (will automatically use HTTPS if both `cert` and `key` are provided)
*/
export declare function serve(filter: Filter<[Reply]>, options: HttpOptions | HttpsOptions): Server;
/**
* Creates a server serving the provided filter
* using the provided [[`With`]] wrapper globally
*
* @param filter - Filter to serve
* @param w - Global [[`With`]] wrapper
*/
export declare function serve(filter: Filter<[Reply]>, w: With<[Reply]>): Server;
/**
* Creates a server with the provided options serving the provided filter
* using the provided [[`With`]] wrapper globally
*
* @param filter - Filter to serve
* @param options - Options for the inner `http.Server` (will automatically use HTTPS if both `cert` and `key` are provided)
* @param w - Global [[`With`]] wrapper
*/
export declare function serve(filter: Filter<[Reply]>, options: HttpOptions | HttpsOptions, w: With<[Reply]>): Server;
/**
* A filter that matches any request
*/
export declare const any: Filter<[]>;