From 26b4ffa3a2f2dffa2e8e905b3f395bfcd6c02775 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 19 Jan 2026 03:52:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4fileProxy=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=9B=B4=E6=96=B0ProxyInfo=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/module/assistant/proxy/file-proxy.ts | 45 ------------------- assistant/src/module/assistant/proxy/proxy.ts | 10 ----- .../src/services/proxy/proxy-page-index.ts | 2 +- package.json | 2 +- 4 files changed, 2 insertions(+), 57 deletions(-) diff --git a/assistant/src/module/assistant/proxy/file-proxy.ts b/assistant/src/module/assistant/proxy/file-proxy.ts index 51168a1..92e0d00 100644 --- a/assistant/src/module/assistant/proxy/file-proxy.ts +++ b/assistant/src/module/assistant/proxy/file-proxy.ts @@ -7,51 +7,6 @@ import { checkFileExists } from '../file/index.ts'; import { log } from '@/module/logger.ts'; import { pipeFileStream } from './pipe.ts'; import { getContentType } from './module/mime.ts'; -export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => { - // url开头的文件 - const url = new URL(req.url, 'http://localhost'); - const [user, key, _info] = url.pathname.split('/'); - const pathname = url.pathname.slice(1); - const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi; - if (!indexPath) { - return res.end('Not Found indexPath'); - } - try { - // 检测文件是否存在,如果文件不存在,则返回404 - let filePath = ''; - let exist = false; - if (_info) { - filePath = path.join(rootPath, target, pathname); - exist = checkFileExists(filePath, true); - } - if (!exist) { - filePath = path.join(rootPath, target, indexPath); - exist = checkFileExists(filePath, true); - } - log.debug('filePath', { filePath, exist }); - - if (!exist) { - res.statusCode = 404; - res.end('Not Found File'); - return; - } - const ext = path.extname(filePath); - let maxAge = 24 * 60 * 60 * 1000; // 24小时 - if (ext === '.html') { - maxAge = 0; - } - let sendFilePath = path.relative(rootPath, filePath); - const file = send(req, sendFilePath, { - root: rootPath, - maxAge, - }); - file.pipe(res); - } catch (error) { - res.statusCode = 404; - res.end('Error:Not Found File'); - return; - } -}; export const fileProxy2 = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => { res.statusCode = 501; diff --git a/assistant/src/module/assistant/proxy/proxy.ts b/assistant/src/module/assistant/proxy/proxy.ts index 3180424..7a4354c 100644 --- a/assistant/src/module/assistant/proxy/proxy.ts +++ b/assistant/src/module/assistant/proxy/proxy.ts @@ -26,16 +26,6 @@ export type ProxyInfo = { * @default false */ ws?: boolean; - /** - * type为file时有效 - * 索引文件,比如index.html, type为fileProxy代理有用 设置了索引文件,如果文件不存在,则访问索引文件 - */ - indexPath?: string; - /** - * type为file时有效 - * 根路径, 默认是process.cwd(), type为fileProxy代理有用,必须为绝对路径 - */ - rootPath?: string; s3?: { /** * 如何id存在,使用assistan-config的storage配置 diff --git a/assistant/src/services/proxy/proxy-page-index.ts b/assistant/src/services/proxy/proxy-page-index.ts index c7e4587..b1b73fc 100644 --- a/assistant/src/services/proxy/proxy-page-index.ts +++ b/assistant/src/services/proxy/proxy-page-index.ts @@ -1,4 +1,4 @@ -import { fileProxy, httpProxy, createApiProxy, ProxyInfo, proxy, fileProxy2 } from '@/module/assistant/index.ts'; +import { createApiProxy, ProxyInfo, proxy } from '@/module/assistant/index.ts'; import http from 'node:http'; import { LocalProxy } from './local-proxy.ts'; import { assistantConfig, app, simpleRouter } from '@/app.ts'; diff --git a/package.json b/package.json index e2d95a8..44b8613 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/cli", - "version": "0.0.87", + "version": "0.0.88", "description": "envision 命令行工具", "type": "module", "basename": "/root/cli",