import http from 'node:http'; import { URL } from 'node:url'; import net from 'node:net'; type MaybeRewrite = T | ((original: T) => T); type LocationStrategy = "same" | "rewrite" | "redirect"; /** NodeJS http server middleware type */ type Middleware = http.RequestListener; /** NodeJS http server request type */ type MiddlewareRequest = Parameters[0]; /** NodeJS http server response type */ type MiddlewareResponse = Parameters[1]; /** * Creates a CORS middleware function for handling CORS requests. * @param allowedOrigins - List of allowed origins for CORS. If not provided, all origins are allowed. */ declare function createCORSMiddleware(allowedOrigins?: string[]): Middleware; /** * Sets CORS headers for the response based on the request and allowed origin. * @param allowedOrigin - The allowed origin string for CORS. If not provided, no CORS headers are set. */ declare function setCORSHeaders(req: MiddlewareRequest, headers: http.IncomingHttpHeaders, allowedOrigin?: string): void; /** * Removes restriction headers from the response headers. */ declare function removeRestrictionHeaders(headers: http.IncomingHttpHeaders): http.IncomingHttpHeaders; interface ProxyOptions { /** * The base pathname, must start with '/', recommended to end with '/'. * Middleware only handles when req.path starts with the mount path. * @default "/" */ base?: string; /** * The target URL, for example `https://example.net/subpath/`. */ target: string; /** * Note that this is ONLY for the proxy request and piping. * You should handle errors for req and res on your own. */ onError?: (e: unknown) => void; /** * same: Do nothing (default) * * rewrite: Rewrite (when not an external site) based on the `mount` * * redirect: Redirect to the original url. */ location?: LocationStrategy; removeRestrictionHeaders?: boolean; } /** * @param proxyOptions Highly recommend that both `mount` and `target` should end with '/' * @param requestOptions For rewrite (not merge) the request options * @param responseOptions For rewrite (not merge) the response object */ declare function createProxyMiddleware(proxyOptions: string | ProxyOptions, rewriteRequestOptions?: MaybeRewrite, rewriteResponseOptions?: MaybeRewrite>): Middleware; type HTTPRequestOptions = string | http.RequestOptions | URL; /** * isomorphic http/https.request, whether it's https will be automatically determined by the options * * http2 is not supported */ declare function request(options: HTTPRequestOptions, callback?: (res: http.IncomingMessage) => void): http.ClientRequest; /** * Create a WebSocket proxy upgrade handler. * * Attach it to your `http.Server`'s `upgrade` event: * * ```ts * const wsProxy = createWebSocketProxy("https://example.com/ws/"); * server.on("upgrade", wsProxy); * ``` */ declare function createWebSocketProxy(target: string, options?: { /** * The base pathname. Only upgrade requests whose URL starts with this * path are handled. Defaults to "/". */ base?: string; onError?: (e: unknown) => void; }): (req: http.IncomingMessage, socket: net.Socket, head: Buffer) => void; export { createCORSMiddleware, createProxyMiddleware, createWebSocketProxy, removeRestrictionHeaders, request, setCORSHeaders }; export type { HTTPRequestOptions, Middleware, MiddlewareRequest, MiddlewareResponse, ProxyOptions };