feat: 重命名代理响应流传输函数,增强代码可读性
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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'] || '';
|
||||
|
||||
Reference in New Issue
Block a user