feat: 更新依赖项版本,增强代理请求处理逻辑,调整 WebSocket 路由和 API 代理配置

This commit is contained in:
2025-12-21 06:40:16 +08:00
parent 8007315c66
commit f12fea7246
8 changed files with 49 additions and 47 deletions

View File

@@ -48,7 +48,7 @@
"@kevisual/logger": "^0.0.4",
"@kevisual/query": "0.0.33",
"@kevisual/query-login": "0.0.7",
"@kevisual/router": "^0.0.47",
"@kevisual/router": "^0.0.48",
"@kevisual/types": "^0.0.10",
"@kevisual/use-config": "^1.0.21",
"@types/bun": "^1.3.5",

View File

@@ -1,4 +1,5 @@
import { App } from '@kevisual/router';
// import { App } from '@kevisual/router/src/app.ts';
import { HttpsPem } from '@/module/assistant/https/sign.ts';
// import { AssistantConfig } from '@/module/assistant/index.ts';
import { AssistantInit, parseHomeArg } from '@/services/init/index.ts';

View File

@@ -8,6 +8,10 @@ export const defaultApiProxy = [
path: '/api/router',
target: 'https://kevisual.cn',
},
{
path: '/api/s1',
target: 'https://kevisual.cn',
},
{
path: '/v1',
target: 'https://kevisual.cn',
@@ -111,5 +115,5 @@ export const httpProxy = (req: http.IncomingMessage, res: http.ServerResponse, p
});
// 处理 POST 请求的请求体(传递数据到目标服务器),end:true 表示当请求体结束时,关闭请求
// req.pipe(proxyReq, { end: true });
pipeProxyReq(req, proxyReq);
pipeProxyReq(req, proxyReq, res);
}

View File

@@ -46,12 +46,13 @@ export const pipeIncoming = (proxyRes: http.IncomingMessage, res: http.ServerRes
if (isBun) {
// Bun环境下需要手动收集数据并end因为Bun的pipe机制与Node.js不同
const chunks: Buffer[] = [];
// 监听数据到达事件,收集所有数据块
proxyRes.on('data', (chunk: Buffer) => {
chunks.push(chunk);
});
if (proxyRes.url === '/api/router') {
console.log(proxyRes.url, proxyRes.statusCode);
}
// 监听数据结束事件,将收集的数据合并并发送给客户端
proxyRes.on('end', () => {
const result = Buffer.concat(chunks).toString();
@@ -77,43 +78,39 @@ export const pipeIncoming = (proxyRes: http.IncomingMessage, res: http.ServerRes
* @param req 客户端的请求对象
* @param proxyReq 代理服务器的请求对象
*/
export const pipeProxyReq = (req: http.IncomingMessage, proxyReq: http.ClientRequest) => {
export const pipeProxyReq = async (req: http.IncomingMessage, proxyReq: http.ClientRequest, res: any) => {
if (isBun) {
// 检查 req 对象是否具有必要的事件监听方法,如果没有,说明可能已经被处理过
if (typeof req.on !== 'function') {
// 如果 req.on 不存在,直接结束代理请求
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'] || '';
if (contentType.includes('multipart/form-data')) {
console.log('Processing multipart/form-data');
const arrayBuffer = await bunRequest.arrayBuffer();
// 设置请求头(在写入数据之前)
proxyReq.setHeader('content-type', contentType);
proxyReq.setHeader('content-length', arrayBuffer.byteLength.toString());
// 写入数据并结束请求
if (arrayBuffer.byteLength > 0) {
proxyReq.write(Buffer.from(arrayBuffer));
}
proxyReq.end();
return;
}
console.log('Bun pipeProxyReq content-type', contentType);
// @ts-ignore
const bodyString = req.body;
bodyString && proxyReq.write(bodyString);
proxyReq.end();
return;
}
// Bun环境下需要手动收集请求数据并发送
const chunks: Buffer[] = [];
// 监听请求数据到达事件,收集所有数据块
req.on('data', (chunk: Buffer) => {
chunks.push(chunk);
});
// 监听请求数据结束事件,将收集的数据合并后发送给目标服务器
req.on('end', () => {
const result = Buffer.concat(chunks);
proxyReq.end(result);
});
// 监听请求错误事件,处理请求传输过程中的错误
req.on('error', (error) => {
// ClientRequest没有writeHead方法直接销毁请求以触发错误处理
} catch (error) {
proxyReq.destroy(error);
});
} else {
// 非 Bun 环境下使用标准的pipe方法进行流传输
// 同样检查 pipe 方法是否存在以确保兼容性
if (typeof req.pipe === 'function') {
req.pipe(proxyReq, { end: true });
} else {
// 如果 pipe 方法不存在,直接结束代理请求
proxyReq.end();
}
} else {
// Node.js标准环境下直接使用pipe进行流传输
req.pipe(proxyReq, { end: true });
}
}

View File

@@ -39,8 +39,8 @@ export const runServer = async (port: number = 51015, listenPath = '127.0.0.1')
});
}
app.server.on([{
id: 'all',
func: proxyRoute as any
id: 'handle-all',
func: proxyRoute as any,
},
...proxyWs()
]);