feat: 重命名代理响应流传输函数,增强代码可读性

This commit is contained in:
2025-12-21 14:19:57 +08:00
parent f12fea7246
commit 03be62cfe6
2 changed files with 3 additions and 5 deletions

View File

@@ -2,7 +2,7 @@ import http from 'node:http';
import https from 'node:https';
import { rewriteCookieDomain } from '../https/cookie-rewrite.ts';
import { ProxyInfo } from './proxy.ts';
import { pipeProxyReq, pipeIncoming, pipeStream } from './pipe.ts';
import { pipeProxyReq, pipeProxyRes, pipeStream } from './pipe.ts';
export const defaultApiProxy = [
{
path: '/api/router',
@@ -105,7 +105,7 @@ export const httpProxy = (req: http.IncomingMessage, res: http.ServerResponse, p
res.writeHead(proxyRes.statusCode, responseHeaders);
// 将代理响应流写入客户端响应
// proxyRes.pipe(res, { end: true });
pipeIncoming(proxyRes, res);
pipeProxyRes(proxyRes, res);
});
// 处理代理请求的错误事件
proxyReq.on('error', (err) => {

View File

@@ -42,7 +42,7 @@ export const pipeStream = (readStream: fs.ReadStream, res: http.ServerResponse)
* @param proxyRes 代理服务器的响应对象
* @param res HTTP服务器响应对象
*/
export const pipeIncoming = (proxyRes: http.IncomingMessage, res: http.ServerResponse) => {
export const pipeProxyRes = (proxyRes: http.IncomingMessage, res: http.ServerResponse) => {
if (isBun) {
// Bun环境下需要手动收集数据并end因为Bun的pipe机制与Node.js不同
const chunks: Buffer[] = [];
@@ -81,8 +81,6 @@ export const pipeIncoming = (proxyRes: http.IncomingMessage, res: http.ServerRes
export const pipeProxyReq = async (req: http.IncomingMessage, proxyReq: http.ClientRequest, res: any) => {
if (isBun) {
try {
const method = req.method?.toUpperCase();
console.log('Bun pipeProxyReq method', method, req.body);
// @ts-ignore
const bunRequest = req.bun.request;
const contentType = req.headers['content-type'] || '';