feat: 重命名代理响应流传输函数,增强代码可读性
This commit is contained in:
@@ -2,7 +2,7 @@ import http from 'node:http';
|
|||||||
import https from 'node:https';
|
import https from 'node:https';
|
||||||
import { rewriteCookieDomain } from '../https/cookie-rewrite.ts';
|
import { rewriteCookieDomain } from '../https/cookie-rewrite.ts';
|
||||||
import { ProxyInfo } from './proxy.ts';
|
import { ProxyInfo } from './proxy.ts';
|
||||||
import { pipeProxyReq, pipeIncoming, pipeStream } from './pipe.ts';
|
import { pipeProxyReq, pipeProxyRes, pipeStream } from './pipe.ts';
|
||||||
export const defaultApiProxy = [
|
export const defaultApiProxy = [
|
||||||
{
|
{
|
||||||
path: '/api/router',
|
path: '/api/router',
|
||||||
@@ -105,7 +105,7 @@ export const httpProxy = (req: http.IncomingMessage, res: http.ServerResponse, p
|
|||||||
res.writeHead(proxyRes.statusCode, responseHeaders);
|
res.writeHead(proxyRes.statusCode, responseHeaders);
|
||||||
// 将代理响应流写入客户端响应
|
// 将代理响应流写入客户端响应
|
||||||
// proxyRes.pipe(res, { end: true });
|
// proxyRes.pipe(res, { end: true });
|
||||||
pipeIncoming(proxyRes, res);
|
pipeProxyRes(proxyRes, res);
|
||||||
});
|
});
|
||||||
// 处理代理请求的错误事件
|
// 处理代理请求的错误事件
|
||||||
proxyReq.on('error', (err) => {
|
proxyReq.on('error', (err) => {
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export const pipeStream = (readStream: fs.ReadStream, res: http.ServerResponse)
|
|||||||
* @param proxyRes 代理服务器的响应对象
|
* @param proxyRes 代理服务器的响应对象
|
||||||
* @param res HTTP服务器响应对象
|
* @param res HTTP服务器响应对象
|
||||||
*/
|
*/
|
||||||
export const pipeIncoming = (proxyRes: http.IncomingMessage, res: http.ServerResponse) => {
|
export const pipeProxyRes = (proxyRes: http.IncomingMessage, res: http.ServerResponse) => {
|
||||||
if (isBun) {
|
if (isBun) {
|
||||||
// Bun环境下需要手动收集数据并end,因为Bun的pipe机制与Node.js不同
|
// Bun环境下需要手动收集数据并end,因为Bun的pipe机制与Node.js不同
|
||||||
const chunks: Buffer[] = [];
|
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) => {
|
export const pipeProxyReq = async (req: http.IncomingMessage, proxyReq: http.ClientRequest, res: any) => {
|
||||||
if (isBun) {
|
if (isBun) {
|
||||||
try {
|
try {
|
||||||
const method = req.method?.toUpperCase();
|
|
||||||
console.log('Bun pipeProxyReq method', method, req.body);
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const bunRequest = req.bun.request;
|
const bunRequest = req.bun.request;
|
||||||
const contentType = req.headers['content-type'] || '';
|
const contentType = req.headers['content-type'] || '';
|
||||||
|
|||||||
Reference in New Issue
Block a user