/// import * as http from "http"; import { TwirpContext } from "./context"; import { ServerHooks } from "./hooks"; import { Interceptor } from "./interceptors"; import { TwirpError } from "./errors"; /** * Twirp Server options */ interface TwirpServerOptions { service: T; packageName: string; serviceName: string; methodList: keys; matchRoute: (method: string, events: RouterEvents) => TwirpHandler; } /** * httpHandler options */ export interface HttpHandlerOptions { prefix?: string | false; } /** * Handles a twirp request */ export declare type TwirpHandler = (ctx: S, service: T, data: Buffer, interceptors?: Interceptor[]) => Promise; /** * Callback events for route matching */ export interface RouterEvents { onMatch: (ctx: T) => Promise | void; onNotFound: () => Promise | void; } declare type keys = Array; /** * Runtime server implementation of a TwirpServer */ export declare class TwirpServer { readonly packageName: string; readonly serviceName: string; readonly methodList: keys; private service; private pathPrefix; private hooks; private interceptors; private matchRoute; constructor(options: TwirpServerOptions); /** * Returns the prefix for this server */ get prefix(): string; /** * The http handler for twirp complaint endpoints * @param options */ httpHandler(options?: HttpHandlerOptions): (req: http.IncomingMessage, resp: http.ServerResponse) => Promise; /** * Adds interceptors or hooks to the request stack * @param middlewares */ use(...middlewares: (ServerHooks | Interceptor)[]): this; /** * Adds a prefix to the service url path * @param prefix */ withPrefix(prefix: string | false): this; /** * Returns the regex matching path for this twirp server */ matchingPath(): RegExp; /** * Returns the base URI for this twirp server */ baseURI(): string; /** * Create a twirp context * @param req * @param res * @private */ protected createContext(req: http.IncomingMessage, res: http.ServerResponse): S; /** * Twrip server http handler implementation * @param req * @param resp * @private */ private _httpHandler; /** * Invoke a hook * @param hookName * @param ctx * @param err * @protected */ protected invokeHook(hookName: keyof ServerHooks, ctx: S, err?: TwirpError): Promise; } /** * Write http error response * @param res * @param error */ export declare function writeError(res: http.ServerResponse, error: Error | TwirpError): void; export {};