f2e-server3
Version:
f2e-server 3.0
56 lines (52 loc) • 2.54 kB
text/typescript
import { IncomingMessage } from "node:http";
import { HttpHeaders } from "../../utils/resp";
import { APIContext } from "../../interface";
import { RequestOptions } from "node:https";
export interface ProxyItem {
/** 需要代理的路径
* 当类型为string时,用于匹配路径前缀, pathname 默认 = location
* 当类型为RegExp时,用于匹配路径, pathname 默认 = ""
*/
location: string | RegExp;
/** 优先匹配规则,忽略location匹配,但是仍然通过location和pathname进行路径修改,可以用于相同路径不同的query、headers等分别处理 */
match?: (url: string, ctx: APIContext) => boolean;
/** 代理服务的origin,形如: http://127.0.0.1:3000 */
origin: string | [string, ...string[]] | { (url: string, req: APIContext): string };
/** 代理服务的pathname,形如: /api,用于原始请求pathname的replacer */
pathname?: string | {
(s: string, ...args: any[]): string;
};
/** 超时时间,超时后返回504, stream=true时,当前配置无效 */
timeout?: number;
/** 是否以流的方式返回,默认false
* 注意: uWebSockets.js不支持stream, timeout配置失效,如果仍然需要设置,可使用requestOptions.timeout设置
*/
stream?: boolean;
/**
* 代理请求发起前执行: 可用于设置请求头等操作
*/
requestHeaders?: { (headers: HttpHeaders, ctx: APIContext): HttpHeaders };
/**
* 响应结束前执行: 可用于设置响应头等操作
*/
responseHeaders?: { (headers: HttpHeaders, resp: IncomingMessage, ctx: APIContext): HttpHeaders };
/** 代理请求参数统一设置 */
requestOptions?: RequestOptions;
/** 代理响应结果修改后输出, stream=true时,当前配置无效 */
responseRender?: (buffer: Buffer, resp: IncomingMessage, ctx: APIContext) => string | Buffer;
/** 存储代理请求结果, stream=true时,当前配置无效 */
saver?: {
/** 矫正存储请求路径 */
pathFixer?: { (pathname: string, ctx: APIContext): string };
/** 存储请求体目录 */
pathBodyDir: string;
/** 存储响应头文件 */
pathHeaders: string;
};
}
export interface ProxyItemRendered extends ProxyItem {
/** 处理后的 match */
match: (url: string, ctx: APIContext) => boolean;
/** url 均以 / 开头 */
getOrigin: (url: string, ctx: APIContext) => string;
}