import * as http from "node:http"; import * as net from "node:net"; import { EventEmitter } from "node:events"; import type { Stream } from "node:stream"; export interface ProxyTargetDetailed { host: string; port: number; protocol?: string; hostname?: string; socketPath?: string; key?: string; passphrase?: string; pfx?: Buffer | string; cert?: string; ca?: string; ciphers?: string; secureProtocol?: string; } export type ProxyType = "ws" | "web"; export type ProxyTarget = ProxyTargetUrl | ProxyTargetDetailed; export type ProxyTargetUrl = URL | string | { port: number; host: string; protocol?: string; }; export type NormalizeProxyTarget = Exclude | URL; export interface ServerOptions { /** URL string to be parsed with the url module. */ target?: ProxyTarget; /** URL string to be parsed with the url module or a URL object. */ forward?: ProxyTargetUrl; /** Object to be passed to http(s).request. */ agent?: any; /** Object to be passed to https.createServer(). */ ssl?: any; /** If you want to proxy websockets. */ ws?: boolean; /** Adds x- forward headers. */ xfwd?: boolean; /** Verify SSL certificate. */ secure?: boolean; /** Explicitly specify if we are proxying to another proxy. */ toProxy?: boolean; /** Specify whether you want to prepend the target's path to the proxy path. */ prependPath?: boolean; /** Specify whether you want to ignore the proxy path of the incoming request. */ ignorePath?: boolean; /** Local interface string to bind for outgoing connections. */ localAddress?: string; /** Changes the origin of the host header to the target URL. */ changeOrigin?: boolean; /** specify whether you want to keep letter case of response header key */ preserveHeaderKeyCase?: boolean; /** Basic authentication i.e. 'user:password' to compute an Authorization header. */ auth?: string; /** Rewrites the location hostname on (301 / 302 / 307 / 308) redirects, Default: null. */ hostRewrite?: string; /** Rewrites the location host/ port on (301 / 302 / 307 / 308) redirects based on requested host/ port.Default: false. */ autoRewrite?: boolean; /** Rewrites the location protocol on (301 / 302 / 307 / 308) redirects to 'http' or 'https'.Default: null. */ protocolRewrite?: string; /** rewrites domain of set-cookie headers. */ cookieDomainRewrite?: false | string | { [oldDomain: string]: string; }; /** rewrites path of set-cookie headers. Default: false */ cookiePathRewrite?: false | string | { [oldPath: string]: string; }; /** object with extra headers to be added to target requests. */ headers?: { [header: string]: string | string[] | undefined; }; /** Timeout (in milliseconds) when proxy receives no response from target. Default: 120000 (2 minutes) */ proxyTimeout?: number; /** Timeout (in milliseconds) for incoming requests */ timeout?: number; /** Specify whether you want to follow redirects. Default: false */ followRedirects?: boolean; /** If set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes event */ selfHandleResponse?: boolean; /** Buffer */ buffer?: Stream; /** Explicitly set the method type of the ProxyReq */ method?: string; /** * Optionally override the trusted CA certificates. * This is passed to https.request. */ ca?: string; /** Optional fetch implementation to use instead of global fetch, use this to activate fetch-based proxying, * for example to proxy HTTP/2 requests */ fetch?: typeof fetch; /** Optional configuration object for fetch-based proxy requests. * Use this to customize fetch request and response handling. * For custom fetch implementations, use the `fetch` property.*/ fetchOptions?: FetchOptions; } export interface FetchOptions { /** Fetch request options */ requestOptions?: RequestInit; /** Called before making the fetch request */ onBeforeRequest?: (requestOptions: RequestInit, req: http.IncomingMessage, res: http.ServerResponse, options: NormalizedServerOptions) => void | Promise; /** Called after receiving the fetch response */ onAfterResponse?: (response: Response, req: http.IncomingMessage, res: http.ServerResponse, options: NormalizedServerOptions) => void | Promise; } export interface NormalizedServerOptions extends ServerOptions { target?: NormalizeProxyTarget; forward?: NormalizeProxyTarget; } export type ErrorCallback = (err: TError, req: InstanceType, res: InstanceType | net.Socket, target?: ProxyTargetUrl) => void; type ProxyServerEventMap = { error: Parameters>; start: [ req: InstanceType, res: InstanceType, target: ProxyTargetUrl ]; open: [socket: net.Socket]; proxyReq: [ proxyReq: http.ClientRequest, req: InstanceType, res: InstanceType, options: ServerOptions, socket: net.Socket ]; proxyRes: [ proxyRes: InstanceType, req: InstanceType, res: InstanceType ]; proxyReqWs: [ proxyReq: http.ClientRequest, req: InstanceType, socket: net.Socket, options: ServerOptions, head: any ]; econnreset: [ err: Error, req: InstanceType, res: InstanceType, target: ProxyTargetUrl ]; end: [ req: InstanceType, res: InstanceType, proxyRes: InstanceType ]; close: [ proxyRes: InstanceType, proxySocket: net.Socket, proxyHead: any ]; }; type ProxyMethodArgs = { ws: [ req: InstanceType, socket: any, head: any, ...args: [ options?: ServerOptions, callback?: ErrorCallback ] | [callback?: ErrorCallback] ]; web: [ req: InstanceType, res: InstanceType, ...args: [ options: ServerOptions, callback?: ErrorCallback ] | [callback?: ErrorCallback] ]; }; type PassFunctions = { ws: (req: InstanceType, socket: net.Socket, options: NormalizedServerOptions, head: Buffer | undefined, server: ProxyServer, cb?: ErrorCallback) => unknown; web: (req: InstanceType, res: InstanceType, options: NormalizedServerOptions, head: Buffer | undefined, server: ProxyServer, cb?: ErrorCallback) => unknown; }; export declare class ProxyServer extends EventEmitter> { /** * Used for proxying WS(S) requests * @param req - Client request. * @param socket - Client socket. * @param head - Client head. * @param options - Additional options. */ readonly ws: (...args: ProxyMethodArgs["ws"]) => void; /** * Used for proxying regular HTTP(S) requests * @param req - Client request. * @param res - Client response. * @param options - Additional options. */ readonly web: (...args: ProxyMethodArgs["web"]) => void; private options; private webPasses; private wsPasses; private _server?; /** * Creates the proxy server with specified options. * @param options - Config object passed to the proxy */ constructor(options?: ServerOptions); /** * Creates the proxy server with specified options. * @param options Config object passed to the proxy * @returns Proxy object with handlers for `ws` and `web` requests */ static createProxyServer(options?: ServerOptions): ProxyServer; /** * Creates the proxy server with specified options. * @param options Config object passed to the proxy * @returns Proxy object with handlers for `ws` and `web` requests */ static createServer(options?: ServerOptions): ProxyServer; /** * Creates the proxy server with specified options. * @param options Config object passed to the proxy * @returns Proxy object with handlers for `ws` and `web` requests */ static createProxy(options?: ServerOptions): ProxyServer; createRightProxy: (type: PT) => Function; onError: (err: TError) => void; /** * A function that wraps the object in a webserver, for your convenience * @param port - Port to listen on * @param hostname - The hostname to listen on */ listen: (port: number, hostname?: string) => this; address: () => string | net.AddressInfo | null | undefined; /** * A function that closes the inner webserver and stops listening on given port */ close: (cb?: Function) => void; before: (type: PT, passName: string, cb: PassFunctions[PT]) => void; after: (type: PT, passName: string, cb: PassFunctions[PT]) => void; } export {};