Compare commits

..

2 Commits

22 changed files with 482 additions and 405 deletions

View File

@@ -69,12 +69,12 @@
"@kevisual/logger": "^0.0.4", "@kevisual/logger": "^0.0.4",
"@kevisual/oss": "0.0.13", "@kevisual/oss": "0.0.13",
"@kevisual/permission": "^0.0.3", "@kevisual/permission": "^0.0.3",
"@kevisual/router": "0.0.42", "@kevisual/router": "0.0.48",
"@kevisual/types": "^0.0.10", "@kevisual/types": "^0.0.10",
"@kevisual/use-config": "^1.0.21", "@kevisual/use-config": "^1.0.21",
"@types/archiver": "^7.0.0", "@types/archiver": "^7.0.0",
"@types/bun": "^1.3.5",
"@types/crypto-js": "^4.2.2", "@types/crypto-js": "^4.2.2",
"@types/formidable": "^3.4.6",
"@types/jsonwebtoken": "^9.0.10", "@types/jsonwebtoken": "^9.0.10",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"@types/node": "^25.0.3", "@types/node": "^25.0.3",
@@ -83,7 +83,6 @@
"crypto-js": "^4.2.0", "crypto-js": "^4.2.0",
"dayjs": "^1.11.19", "dayjs": "^1.11.19",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"formidable": "3.5.4",
"ioredis": "^5.8.2", "ioredis": "^5.8.2",
"jsonwebtoken": "^9.0.3", "jsonwebtoken": "^9.0.3",
"lodash-es": "^4.17.22", "lodash-es": "^4.17.22",

32
pnpm-lock.yaml generated
View File

@@ -41,7 +41,7 @@ importers:
version: 1.1.1 version: 1.1.1
drizzle-orm: drizzle-orm:
specifier: ^0.45.1 specifier: ^0.45.1
version: 0.45.1(pg@8.16.3) version: 0.45.1(bun-types@1.3.5)(pg@8.16.3)
eventemitter3: eventemitter3:
specifier: ^5.0.1 specifier: ^5.0.1
version: 5.0.1 version: 5.0.1
@@ -86,8 +86,8 @@ importers:
specifier: ^0.0.3 specifier: ^0.0.3
version: 0.0.3 version: 0.0.3
'@kevisual/router': '@kevisual/router':
specifier: 0.0.42 specifier: 0.0.48
version: 0.0.42 version: 0.0.48
'@kevisual/types': '@kevisual/types':
specifier: ^0.0.10 specifier: ^0.0.10
version: 0.0.10 version: 0.0.10
@@ -97,6 +97,9 @@ importers:
'@types/archiver': '@types/archiver':
specifier: ^7.0.0 specifier: ^7.0.0
version: 7.0.0 version: 7.0.0
'@types/bun':
specifier: ^1.3.5
version: 1.3.5
'@types/crypto-js': '@types/crypto-js':
specifier: ^4.2.2 specifier: ^4.2.2
version: 4.2.2 version: 4.2.2
@@ -252,8 +255,8 @@ packages:
'@kevisual/router@0.0.33': '@kevisual/router@0.0.33':
resolution: {integrity: sha512-9z7TkSzCIGbXn9SuHPBdZpGwHlAuwA8iN5jNAZBUvbEvBRkBxlrbdCSe9fBYiAHueLm2AceFNrW74uulOiAkqA==} resolution: {integrity: sha512-9z7TkSzCIGbXn9SuHPBdZpGwHlAuwA8iN5jNAZBUvbEvBRkBxlrbdCSe9fBYiAHueLm2AceFNrW74uulOiAkqA==}
'@kevisual/router@0.0.42': '@kevisual/router@0.0.48':
resolution: {integrity: sha512-6j254Hl1Q9uM4qKD4v6pcNSXVs7zwHZlyfSxUrNTWrgD7OCt/mrgBpzcNo0TM25/CsdrZCDs21kamienfYQ+lw==} resolution: {integrity: sha512-WsSvT+NpfC/bZbaAzE3WSKD2DRZP0JuPQJGr4YucSdO/lOLB4cEpOZRbPlV3l7G064ow8QJRAN2DUW+bRjrp1A==}
'@kevisual/types@0.0.10': '@kevisual/types@0.0.10':
resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==} resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==}
@@ -386,6 +389,9 @@ packages:
'@types/archiver@7.0.0': '@types/archiver@7.0.0':
resolution: {integrity: sha512-/3vwGwx9n+mCQdYZ2IKGGHEFL30I96UgBlk8EtRDDFQ9uxM1l4O5Ci6r00EMAkiDaTqD9DQ6nVrWRICnBPtzzg==} resolution: {integrity: sha512-/3vwGwx9n+mCQdYZ2IKGGHEFL30I96UgBlk8EtRDDFQ9uxM1l4O5Ci6r00EMAkiDaTqD9DQ6nVrWRICnBPtzzg==}
'@types/bun@1.3.5':
resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==}
'@types/busboy@1.5.4': '@types/busboy@1.5.4':
resolution: {integrity: sha512-kG7WrUuAKK0NoyxfQHsVE6j1m01s6kMma64E+OZenQABMQyTJop1DumUWcLwAQ2JzpefU7PDYoRDKl8uZosFjw==} resolution: {integrity: sha512-kG7WrUuAKK0NoyxfQHsVE6j1m01s6kMma64E+OZenQABMQyTJop1DumUWcLwAQ2JzpefU7PDYoRDKl8uZosFjw==}
@@ -583,6 +589,9 @@ packages:
bullmq@5.66.2: bullmq@5.66.2:
resolution: {integrity: sha512-0PrkpIakIntkBcPLltPIRWdLC1FTLUa/VhJkmEfobb5YUQjoUwJdmmf7HX+o/vMonS5048JpP+abf9lVRUFEjA==} resolution: {integrity: sha512-0PrkpIakIntkBcPLltPIRWdLC1FTLUa/VhJkmEfobb5YUQjoUwJdmmf7HX+o/vMonS5048JpP+abf9lVRUFEjA==}
bun-types@1.3.5:
resolution: {integrity: sha512-inmAYe2PFLs0SUbFOWSVD24sg1jFlMPxOjOSSCYqUgn4Hsc3rDc7dFvfVYjFPNHtov6kgUeulV4SxbuIV/stPw==}
busboy@1.6.0: busboy@1.6.0:
resolution: {integrity: sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==} resolution: {integrity: sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==}
engines: {node: '>=10.16.0'} engines: {node: '>=10.16.0'}
@@ -2145,7 +2154,7 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
'@kevisual/router@0.0.42': '@kevisual/router@0.0.48':
dependencies: dependencies:
path-to-regexp: 8.3.0 path-to-regexp: 8.3.0
selfsigned: 5.2.0 selfsigned: 5.2.0
@@ -2353,6 +2362,10 @@ snapshots:
dependencies: dependencies:
'@types/readdir-glob': 1.1.5 '@types/readdir-glob': 1.1.5
'@types/bun@1.3.5':
dependencies:
bun-types: 1.3.5
'@types/busboy@1.5.4': '@types/busboy@1.5.4':
dependencies: dependencies:
'@types/node': 25.0.3 '@types/node': 25.0.3
@@ -2562,6 +2575,10 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
bun-types@1.3.5:
dependencies:
'@types/node': 25.0.3
busboy@1.6.0: busboy@1.6.0:
dependencies: dependencies:
streamsearch: 1.1.0 streamsearch: 1.1.0
@@ -2713,8 +2730,9 @@ snapshots:
dottie@2.0.6: {} dottie@2.0.6: {}
drizzle-orm@0.45.1(pg@8.16.3): drizzle-orm@0.45.1(bun-types@1.3.5)(pg@8.16.3):
optionalDependencies: optionalDependencies:
bun-types: 1.3.5
pg: 8.16.3 pg: 8.16.3
eastasianwidth@0.2.0: {} eastasianwidth@0.2.0: {}

View File

@@ -29,17 +29,13 @@ export const minioClient = useContextKey('minioClient', () => minioLib.minioClie
export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize); export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize);
const init = () => { const init = () => {
return new App<{ sequelize: typeof sequelize }>({ return new App({
serverOptions: { serverOptions: {
cors: { cors: {
origin: '*', origin: '*',
}, },
// httpType: 'https', io: true,
}, }
io: true,
routerContext: {
sequelize,
},
}); });
}; };
export const app = useContextKey('app', init); export const app = useContextKey('app', init);

View File

@@ -1,62 +1,19 @@
import { myConfig as config } from './modules/config.ts';
import { app } from './app.ts'; import { app } from './app.ts';
import './route.ts'; import './route.ts';
import { handleRequest } from './routes-simple/handle-request.ts'; import { handleRequest } from './routes-simple/handle-request.ts';
import { port } from './modules/config.ts'; import { port } from './modules/config.ts';
import { WssApp } from './modules/ws-proxy/index.ts'; import { wssFun } from './modules/ws-proxy/index.ts';
import net from 'node:net'; import { WebSocketListenerFun, HttpListenerFun } from '@kevisual/router/src/server/server-type.js';
// if (import.meta.url === `file://${process.argv[1]}`) { console.log('Starting server...', port);
app.listen(port, () => { app.listen(port, '0.0.0.0', () => {
console.log(`server is running at http://localhost:${port}`); console.log(`server is running at http://localhost:${port}`);
}); });
app.server.on(handleRequest); app.server.on([{
id: 'handle-all',
const wssApp = new WssApp(); func: handleRequest as any,
const main = () => { }, {
console.log('Upgrade initialization started'); id: 'wss',
io: true,
app.server.server.on('upgrade', async (req, socket, head) => { path: '/ws/proxy',
const isUpgrade = wssApp.upgrade(req, socket, head); func: wssFun as WebSocketListenerFun,
if (isUpgrade) { }]);
console.log('WebSocket upgrade successful for path:', req.url);
return;
}
const proxyApiList = config?.apiList || [];
const proxyApi = proxyApiList.find((item) => req.url.startsWith(item.path));
if (proxyApi) {
const _u = new URL(req.url, `${proxyApi.target}`);
const options = {
hostname: _u.hostname,
port: Number(_u.port) || 80,
path: _u.pathname,
headers: req.headers,
};
const proxySocket = net.connect(options.port, options.hostname, () => {
proxySocket.write(
`GET ${options.path} HTTP/1.1\r\n` +
`Host: ${options.hostname}\r\n` +
`Connection: Upgrade\r\n` +
`Upgrade: websocket\r\n` +
`Sec-WebSocket-Key: ${req.headers['sec-websocket-key']}\r\n` +
`Sec-WebSocket-Version: ${req.headers['sec-websocket-version']}\r\n` +
`\r\n`,
);
proxySocket.pipe(socket);
socket.pipe(proxySocket);
});
proxySocket.on('error', (err) => {
console.error(`WebSocket proxy error: ${err.message}`);
socket.end();
});
} else {
socket.end();
}
});
};
// setTimeout(() => {
// main();
// }, 1200);

View File

@@ -38,6 +38,21 @@ export const checkAuth = async (req: http.IncomingMessage, res: http.ServerRespo
return { tokenUser, token }; return { tokenUser, token };
}; };
export const getLoginUserByToken = async (token: string) => {
if (token) {
token = token.replace('Bearer ', '');
}
if (!token) {
return null;
}
let tokenUser;
try {
tokenUser = await User.verifyToken(token);
return { tokenUser, token };
} catch (e) {
return null;
}
}
export const getLoginUser = async (req: http.IncomingMessage) => { export const getLoginUser = async (req: http.IncomingMessage) => {
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || ''; let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
const url = new URL(req.url || '', 'http://localhost'); const url = new URL(req.url || '', 'http://localhost');

View File

@@ -3,7 +3,7 @@ import { useFileStore } from '@kevisual/use-config/file-store';
import { minioResources } from './minio.ts'; import { minioResources } from './minio.ts';
export const config = useConfig() as any; export const config = useConfig() as any;
export const port = config.PORT || 4005; export const port = config.PORT ? Number(config.PORT) : 4005;
export const fileStore = useFileStore('pages'); export const fileStore = useFileStore('pages');
type ConfigType = { type ConfigType = {
api: { api: {

View File

@@ -8,3 +8,7 @@ export * from './get-router.ts'
export * from './get-content-type.ts' export * from './get-content-type.ts'
export * from './utils.ts' export * from './utils.ts'
export { pipeFileStream, pipeStream } from './pipe.ts'
export { pipeBusboy } from './pipe-busboy.ts'

View File

@@ -0,0 +1,13 @@
import { isBun } from '@/utils/get-engine.ts';
import http from 'node:http';
export const pipeBusboy = async (req: http.IncomingMessage, res: http.ServerResponse, busboy: any) => {
if (isBun) {
// @ts-ignore
const bunRequest = req.bun.request;
const arrayBuffer = await bunRequest.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
busboy.end(buffer);
} else {
req.pipe(busboy);
}
}

View File

@@ -0,0 +1,19 @@
import * as http from 'http';
import * as fs from 'fs';
import { isBun } from '../../utils/get-engine.ts';
export const pipeFileStream = (filePath: string, res: http.ServerResponse) => {
const readStream = fs.createReadStream(filePath);
if (isBun) {
res.pipe(readStream as any);
} else {
readStream.pipe(res, { end: true });
}
}
export const pipeStream = (readStream: fs.ReadStream, res: http.ServerResponse) => {
if (isBun) {
res.pipe(readStream as any);
} else {
readStream.pipe(res, { end: true });
}
}

View File

@@ -10,7 +10,7 @@ import { addStat } from '@/modules/html/stat/index.ts';
import path from 'path'; import path from 'path';
import { getTextContentType } from '@/modules/fm-manager/index.ts'; import { getTextContentType } from '@/modules/fm-manager/index.ts';
import { logger } from '@/modules/logger.ts'; import { logger } from '@/modules/logger.ts';
import { pipeStream } from '../pipe.ts';
const pipelineAsync = promisify(pipeline); const pipelineAsync = promisify(pipeline);
export async function downloadFileFromMinio(fileUrl: string, destFile: string) { export async function downloadFileFromMinio(fileUrl: string, destFile: string) {
@@ -74,7 +74,7 @@ export async function minioProxy(
res.writeHead(200, { res.writeHead(200, {
...headers, ...headers,
}); });
objectStream.pipe(res, { end: true }); pipeStream(objectStream as any, res);
} }
return true; return true;
} catch (error) { } catch (error) {
@@ -154,7 +154,7 @@ export const httpProxy = async (
res.writeHead(proxyRes.statusCode, { res.writeHead(proxyRes.statusCode, {
...headers, ...headers,
}); });
proxyRes.pipe(res, { end: true }); pipeStream(proxyRes as any, res);
} }
}); });
proxyReq.on('error', (err) => { proxyReq.on('error', (err) => {

View File

@@ -1,4 +1,4 @@
import http from 'http'; import http from 'node:http';
import { minioClient } from '@/modules/minio.ts'; import { minioClient } from '@/modules/minio.ts';
type ProxyInfo = { type ProxyInfo = {
path?: string; path?: string;

View File

@@ -1,5 +1,6 @@
import { IncomingMessage } from 'node:http'; import { IncomingMessage } from 'node:http';
import http from 'node:http'; import http from 'node:http';
import { logger } from '../logger.ts';
export const getUserFromRequest = (req: IncomingMessage) => { export const getUserFromRequest = (req: IncomingMessage) => {
const url = new URL(req.url, `http://${req.headers.host}`); const url = new URL(req.url, `http://${req.headers.host}`);
@@ -14,8 +15,8 @@ export const getUserFromRequest = (req: IncomingMessage) => {
export const getDNS = (req: http.IncomingMessage) => { export const getDNS = (req: http.IncomingMessage) => {
const hostName = req.headers.host; const hostName = req.headers?.host;
const ip = req.socket.remoteAddress; const ip = req?.socket?.remoteAddress || '';
return { hostName, ip }; return { hostName, ip };
}; };

View File

@@ -1,136 +1,29 @@
import { WebSocketServer } from 'ws';
import { nanoid } from 'nanoid';
import { WsProxyManager } from './manager.ts'; import { WsProxyManager } from './manager.ts';
import { getLoginUser } from '@/modules/auth.ts'; import { getLoginUserByToken } from '@/modules/auth.ts';
import { logger } from '../logger.ts'; import { logger } from '../logger.ts';
export const wsProxyManager = new WsProxyManager(); export const wsProxyManager = new WsProxyManager();
import { WebScoketListenerFun } from '@kevisual/router/src/server/server-type.ts'
export const upgrade = (request: any, socket: any, head: any) => { export const wssFun: WebScoketListenerFun = async (req, res) => {
const req = request as any; // do nothing, just to enable ws upgrade event
const url = new URL(req.url, 'http://localhost'); const { id, ws, token, data, emitter } = req;
const id = url.searchParams.get('id'); logger.debug('ws proxy connected, id=', id, ' token=', token, ' data=', data);
if (url.pathname === '/ws/proxy') { // console.log('req', req)
console.log('upgrade', request.url, id); const { type } = data || {};
wss.handleUpgrade(req, socket, head, (ws) => { if (type === 'registryClient') {
// 这里手动触发 connection 事件 const loginUser = await getLoginUserByToken(token);
console.log('emitting connection event'); if (!loginUser?.tokenUser) {
// @ts-ignore logger.debug('未登录,断开连接');
wss.emit('connection', ws, req);
});
return true;
}
return false;
};
export const wss = new WebSocketServer({
noServer: true,
path: '/ws/proxy',
});
wss.on('connection', async (ws, req) => {
console.log('connected', req.url);
const url = new URL(req.url, 'http://localhost');
const _id = url?.searchParams?.get('id');
const id = _id || nanoid();
const loginUser = await getLoginUser(req);
if (!loginUser) {
console.log('未登录,断开连接');
ws.send(JSON.stringify({ code: 401, message: '未登录' }));
setTimeout(() => {
ws.close();
}, 1000);
return;
}
const user = loginUser.tokenUser.username;
const userApp = user + '-' + id;
console.log('注册 ws 连接', userApp);
wsProxyManager.register(userApp, { user, ws });
ws.send(
JSON.stringify({
type: 'connected',
user: user,
id,
}),
);
ws.on('message', async (event: Buffer) => {
const eventData = event.toString();
if (!eventData) {
return;
}
const data = JSON.parse(eventData);
logger.debug('message', data);
});
ws.on('close', () => {
logger.debug('ws closed');
wsProxyManager.unregister(userApp);
});
});
export class WssApp {
wss: WebSocketServer;
bunWSS = websocket;
constructor() {
this.wss = wss;
}
upgrade(request: any, socket: any, head: any) {
// return upgrade(request, socket, head);
return bunUpgrade(request);
}
}
export const bunUpgrade = (request: Request) => {
const url = new URL(request.url, 'http://localhost');
const isUpgrade = url.pathname === '/ws/proxy';
if (isUpgrade) {
console.log('upgrade', request.url);
// 使用 Bun 原生 WebSocket
new Response(null, {
status: 101,
headers: {
'Upgrade': 'websocket',
},
});
return true;
}
return false;
};
// Bun WebSocket 处理器
export const websocket = {
async open(ws: any) {
console.log('WebSocket opened');
const { url, token } = ws.data;
const urlObj = new URL(url, 'http://localhost');
const _id = urlObj.searchParams.get('id');
const id = _id || nanoid();
// 创建一个模拟的 request 对象用于认证
const mockReq: any = {
url: url,
headers: {
authorization: token ? `Bearer ${token}` : undefined,
},
};
const loginUser = await getLoginUser(mockReq);
if (!loginUser) {
console.log('未登录,断开连接');
ws.send(JSON.stringify({ code: 401, message: '未登录' })); ws.send(JSON.stringify({ code: 401, message: '未登录' }));
ws.close(); setTimeout(() => {
ws.close(401, 'Unauthorized');
}, 1000);
return; return;
} }
const user = loginUser?.tokenUser?.username;
const user = loginUser.tokenUser.username;
const userApp = user + '-' + id; const userApp = user + '-' + id;
console.log('注册 ws 连接', userApp); logger.debug('注册 ws 连接', userApp);
// @ts-ignore
ws.data.userApp = userApp;
ws.data.user = user;
wsProxyManager.register(userApp, { user, ws }); wsProxyManager.register(userApp, { user, ws });
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
type: 'connected', type: 'connected',
@@ -138,26 +31,22 @@ export const websocket = {
id, id,
}), }),
); );
}, emitter.once('close--' + id, () => {
logger.debug('ws emitter closed');
async message(ws: any, message: string) {
try {
const data = JSON.parse(message);
logger.debug('message', data);
} catch (error) {
logger.error('Failed to parse message', error);
}
},
close(ws: any) {
const { userApp } = ws.data;
logger.debug('ws closed', userApp);
if (userApp) {
wsProxyManager.unregister(userApp); wsProxyManager.unregister(userApp);
} });
}, // @ts-ignore
ws.data.userApp = userApp;
error(ws: any, error: Error) { return;
console.error('WebSocket error:', error); }
}, // @ts-ignore
}; const userApp = ws.data.userApp;
logger.debug('message', data, ' userApp=', userApp);
const wsMessage = wsProxyManager.get(userApp);
if (wsMessage) {
wsMessage.sendResponse(data);
} else {
// @ts-ignore
logger.debug('账号应用未注册,无法处理消息。未授权?', ws.data);
}
}

View File

@@ -10,18 +10,11 @@ class WsMessage {
this.ws = ws; this.ws = ws;
this.user = user; this.user = user;
this.emitter = new EventEmitter(); this.emitter = new EventEmitter();
this.listenMessage();
} }
async listenMessage() { async sendResponse(data: any) {
this.ws.on('message', (event: Buffer) => { if (data.id) {
const eventData = event.toString(); this.emitter.emit(data.id, data?.data);
if (!eventData) { }
return;
}
const data = JSON.parse(eventData);
logger.debug('ws-proxy listenMessage', data);
this.emitter.emit(data.id, data.data);
});
} }
async sendData(data: any, opts?: { timeout?: number }) { async sendData(data: any, opts?: { timeout?: number }) {
if (this.ws.readyState !== WebSocket.OPEN) { if (this.ws.readyState !== WebSocket.OPEN) {
@@ -65,6 +58,7 @@ export class WsProxyManager {
value.ws.close(); value.ws.close();
} }
} }
console.log('WsProxyManager register', id);
const value = new WsMessage({ ws: opts?.ws, user: opts?.user }); const value = new WsMessage({ ws: opts?.ws, user: opts?.user });
this.wssMap.set(id, value); this.wssMap.set(id, value);
} }

View File

@@ -11,26 +11,36 @@ type ProxyOptions = {
export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => { export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
const { url } = req; const { url } = req;
const { pathname } = new URL(url || '', `http://localhost`); const { pathname } = new URL(url || '', `http://localhost`);
const [user, app, userAppKey] = pathname.split('/').slice(1); let [user, app, userAppKey] = pathname.split('/').slice(1);
if (!user || !app || !userAppKey) { if (!user || !app || !userAppKey) {
opts?.createNotFoundPage?.('应用未找到'); opts?.createNotFoundPage?.('应用未找到');
return false; return false;
} }
const data = await App.handleRequest(req, res); const data = await App.handleRequest(req, res);
const loginUser = await getLoginUser(req); const loginUser = await getLoginUser(req);
if (!loginUser) { if (!loginUser) {
opts?.createNotFoundPage?.('没有登录'); opts?.createNotFoundPage?.('没有登录');
return false; return false;
} }
if (loginUser.tokenUser?.username !== user) { const isAdmin = loginUser.tokenUser?.username === user
// TODO: 如果不是管理员,是否需要添加其他人可以访问的逻辑?
if (!isAdmin) {
opts?.createNotFoundPage?.('没有访问应用权限'); opts?.createNotFoundPage?.('没有访问应用权限');
return false; return false;
} }
if (!userAppKey.startsWith(user + '-')) {
userAppKey = user + '-' + userAppKey;
}
logger.debug('data', data); logger.debug('data', data);
const client = wsProxyManager.get(userAppKey); const client = wsProxyManager.get(userAppKey);
const ids = wsProxyManager.getIds(); const ids = wsProxyManager.getIds();
if (!client) { if (!client) {
opts?.createNotFoundPage?.(`未找到应用, ${userAppKey}, ${ids.join(',')}`); if (isAdmin) {
opts?.createNotFoundPage?.(`未找到应用 [${userAppKey}], 当前应用列表: ${ids.join(',')}`);
} else {
opts?.createNotFoundPage?.('应用访问失败');
}
return false; return false;
} }
const value = await client.sendData(data); const value = await client.sendData(data);

View File

@@ -1,4 +1,4 @@
import { IncomingForm } from 'formidable'; import Busboy from 'busboy';
import { checkAuth } from '../middleware/auth.ts'; import { checkAuth } from '../middleware/auth.ts';
import { router, clients, writeEvents } from '../router.ts'; import { router, clients, writeEvents } from '../router.ts';
import { error } from '../middleware/auth.ts'; import { error } from '../middleware/auth.ts';
@@ -7,50 +7,94 @@ import { useFileStore } from '@kevisual/use-config/file-store';
import { app, minioClient } from '@/app.ts'; import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts'; import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts'; import { getContentType } from '@/utils/get-content-type.ts';
import path from 'path';
import { createWriteStream } from 'fs';
import crypto from 'crypto';
import { pipeBusboy } from '@/modules/fm-manager/index.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true }); const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.post('/api/micro-app/upload', async (req, res) => { router.post('/api/micro-app/upload', async (req, res) => {
if (res.headersSent) return; // 如果响应已发送不再处理 if (res.headersSent) return; // 如果响应已发送,不再处理
res.writeHead(200, { 'Content-Type': 'application/json' }); res.writeHead(200, { 'Content-Type': 'application/json' });
const { tokenUser, token } = await checkAuth(req, res); const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return; if (!tokenUser) return;
//
// 使用 formidable 解析 multipart/form-data // 使用 busboy 解析 multipart/form-data
const form = new IncomingForm({ const busboy = Busboy({ headers: req.headers });
multiples: false, // 支持多文件上传 const fields: any = {};
uploadDir: cacheFilePath, // 上传文件存储目录 let file: any = null;
allowEmptyFiles: true, // 允许空 let filePromise: Promise<void> | null = null;
minFileSize: 0, // 最小文件大小 let bytesReceived = 0;
maxFiles: 1, // 最大文件数量 let bytesExpected = parseInt(req.headers['content-length'] || '0');
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
keepExtensions: true, // 保留文件 busboy.on('field', (fieldname, value) => {
hashAlgorithm: 'md5', // 文件哈希算法 fields[fieldname] = value;
}); });
form.on('progress', (bytesReceived, bytesExpected) => {
const progress = (bytesReceived / bytesExpected) * 100; busboy.on('file', (fieldname, fileStream, info) => {
console.log(`Upload progress: ${progress.toFixed(2)}%`); const { filename, encoding, mimeType } = info;
const data = { const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`);
progress: progress.toFixed(2), const writeStream = createWriteStream(tempPath);
message: `Upload progress: ${progress.toFixed(2)}%`, const hash = crypto.createHash('md5');
}; let size = 0;
writeEvents(req, data);
}); filePromise = new Promise<void>((resolve, reject) => {
// 解析上传的文件 fileStream.on('data', (chunk) => {
form.parse(req, async (err, fields, files) => { bytesReceived += chunk.length;
if (err) { size += chunk.length;
res.end(error(`Upload error: ${err.message}`)); hash.update(chunk);
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; if (bytesExpected > 0) {
uploadedFiles.forEach((file) => { const progress = (bytesReceived / bytesExpected) * 100;
fs.unlinkSync(file.filepath); console.log(`Upload progress: ${progress.toFixed(2)}%`);
const data = {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
writeEvents(req, data);
}
}); });
return;
fileStream.pipe(writeStream);
writeStream.on('finish', () => {
file = {
filepath: tempPath,
originalFilename: filename,
mimetype: mimeType,
hash: hash.digest('hex'),
size: size,
};
resolve();
});
writeStream.on('error', (err) => {
reject(err);
});
});
});
busboy.on('finish', async () => {
// 等待文件写入完成
if (filePromise) {
try {
await filePromise;
} catch (err) {
console.error(`File write error: ${err.message}`);
res.end(error(`File write error: ${err.message}`));
return;
}
} }
const clearFiles = () => { const clearFiles = () => {
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; if (file?.filepath && fs.existsSync(file.filepath)) {
uploadedFiles.forEach((file) => {
fs.unlinkSync(file.filepath); fs.unlinkSync(file.filepath);
}); }
}; };
if (!file) {
res.end(error('No file uploaded'));
return;
}
let appKey, collection; let appKey, collection;
const { appKey: _appKey, collection: _collecion } = fields; const { appKey: _appKey, collection: _collecion } = fields;
if (Array.isArray(_appKey)) { if (Array.isArray(_appKey)) {
@@ -68,31 +112,28 @@ router.post('/api/micro-app/upload', async (req, res) => {
appKey = appKey || 'micro-app'; appKey = appKey || 'micro-app';
console.log('Appkey', appKey); console.log('Appkey', appKey);
console.log('collection', collection); console.log('collection', collection);
// 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; // 处理上传的文件
const uploadResults = []; const uploadResults = [];
for (let i = 0; i < uploadedFiles.length; i++) { const tempPath = file.filepath; // 文件上传时的临时路径
const file = uploadedFiles[i]; const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// @ts-ignore // 比如 child2/b.txt
const tempPath = file.filepath; // 文件上传时的临时路径 const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`;
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构) // 上传到 MinIO 并保留文件夹结构
// 比如 child2/b.txt const isHTML = relativePath.endsWith('.html');
const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`; await minioClient.fPutObject(bucketName, minioPath, tempPath, {
// 上传到 MinIO 并保留文件夹结构 'Content-Type': getContentType(relativePath),
const isHTML = relativePath.endsWith('.html'); 'app-source': 'user-micro-app',
await minioClient.fPutObject(bucketName, minioPath, tempPath, { 'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
'Content-Type': getContentType(relativePath), });
'app-source': 'user-micro-app', uploadResults.push({
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年 name: relativePath,
}); path: minioPath,
uploadResults.push({ hash: file.hash,
name: relativePath, size: file.size,
path: minioPath, });
hash: file.hash, fs.unlinkSync(tempPath); // 删除临时文件
size: file.size,
});
fs.unlinkSync(tempPath); // 删除临时文件
}
// 受控 // 受控
const r = await app.call({ const r = await app.call({
path: 'micro-app', path: 'micro-app',
@@ -115,6 +156,8 @@ router.post('/api/micro-app/upload', async (req, res) => {
} }
res.end(JSON.stringify(data)); res.end(JSON.stringify(data));
}); });
pipeBusboy(req, res, busboy);
}); });

View File

@@ -1,7 +1,7 @@
import { useFileStore } from '@kevisual/use-config/file-store'; import { useFileStore } from '@kevisual/use-config/file-store';
import http from 'node:http'; import http from 'node:http';
import fs from 'fs'; import fs from 'fs';
import { IncomingForm } from 'formidable'; import Busboy from 'busboy';
import { app, minioClient } from '@/app.ts'; import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts'; import { bucketName } from '@/modules/minio.ts';
@@ -11,6 +11,9 @@ import { getContainerById } from '@/routes/container/module/get-container-file.t
import { router, error, checkAuth, writeEvents } from './router.ts'; import { router, error, checkAuth, writeEvents } from './router.ts';
import './index.ts'; import './index.ts';
import { handleRequest as PageProxy } from './page-proxy.ts'; import { handleRequest as PageProxy } from './page-proxy.ts';
import path from 'path';
import { createWriteStream } from 'fs';
import { pipeBusboy } from '@/modules/fm-manager/pipe-busboy.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true }); const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.get('/api/app/upload', async (req, res) => { router.get('/api/app/upload', async (req, res) => {
@@ -23,41 +26,80 @@ router.post('/api/app/upload', async (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' }); res.writeHead(200, { 'Content-Type': 'application/json' });
const { tokenUser, token } = await checkAuth(req, res); const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return; if (!tokenUser) return;
// 使用 formidable 解析 multipart/form-data
const form = new IncomingForm({ // 使用 busboy 解析 multipart/form-data
multiples: true, // 支持多文件上传 const busboy = Busboy({ headers: req.headers });
uploadDir: cacheFilePath, // 上传文件存储目录 const fields: any = {};
allowEmptyFiles: true, // 允许空 const files: any = [];
minFileSize: 0, // 最小文件大小 const filePromises: Promise<void>[] = [];
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录 let bytesReceived = 0;
keepExtensions: true, // 保留文件 let bytesExpected = parseInt(req.headers['content-length'] || '0');
hashAlgorithm: 'md5', // 文件哈希算法
busboy.on('field', (fieldname, value) => {
fields[fieldname] = value;
}); });
form.on('progress', (bytesReceived, bytesExpected) => {
const progress = (bytesReceived / bytesExpected) * 100; busboy.on('file', (fieldname, fileStream, info) => {
console.log(`Upload progress: ${progress.toFixed(2)}%`); const { filename, encoding, mimeType } = info;
const data = { const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`);
progress: progress.toFixed(2), const writeStream = createWriteStream(tempPath);
message: `Upload progress: ${progress.toFixed(2)}%`,
}; const filePromise = new Promise<void>((resolve, reject) => {
writeEvents(req, data); fileStream.on('data', (chunk) => {
}); bytesReceived += chunk.length;
// 解析上传的文件 if (bytesExpected > 0) {
form.parse(req, async (err, fields, files) => { const progress = (bytesReceived / bytesExpected) * 100;
if (err) { console.log(`Upload progress: ${progress.toFixed(2)}%`);
res.end(error(`Upload error: ${err.message}`)); const data = {
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; progress: progress.toFixed(2),
uploadedFiles.forEach((file) => { message: `Upload progress: ${progress.toFixed(2)}%`,
fs.unlinkSync(file.filepath); };
writeEvents(req, data);
}
}); });
fileStream.pipe(writeStream);
writeStream.on('finish', () => {
files.push({
filepath: tempPath,
originalFilename: filename,
mimetype: mimeType,
});
resolve();
});
writeStream.on('error', (err) => {
reject(err);
});
});
filePromises.push(filePromise);
});
busboy.on('finish', async () => {
// 等待所有文件写入完成
try {
await Promise.all(filePromises);
} catch (err) {
console.error(`File write error: ${err.message}`);
res.end(error(`File write error: ${err.message}`));
return; return;
} }
const clearFiles = () => { const clearFiles = () => {
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; files.forEach((file: any) => {
uploadedFiles.forEach((file) => { if (file?.filepath && fs.existsSync(file.filepath)) {
fs.unlinkSync(file.filepath); fs.unlinkSync(file.filepath);
}
}); });
}; };
// 检查是否有文件上传
if (files.length === 0) {
res.end(error('files is required'));
return;
}
let appKey, let appKey,
version, version,
username = ''; username = '';
@@ -99,11 +141,9 @@ router.post('/api/app/upload', async (req, res) => {
console.log('Appkey', appKey, version); console.log('Appkey', appKey, version);
// 逐个处理每个上传的文件 // 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
const uploadResults = []; const uploadResults = [];
for (let i = 0; i < uploadedFiles.length; i++) { for (let i = 0; i < files.length; i++) {
const file = uploadedFiles[i]; const file = files[i];
// @ts-ignore
const tempPath = file.filepath; // 文件上传时的临时路径 const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构) const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt // 比如 child2/b.txt
@@ -144,6 +184,8 @@ router.post('/api/app/upload', async (req, res) => {
} }
res.end(JSON.stringify(data)); res.end(JSON.stringify(data));
}); });
pipeBusboy(req, res, busboy);
}); });
router.get('/api/container/file/:id', async (req, res) => { router.get('/api/container/file/:id', async (req, res) => {

View File

@@ -1,4 +1,4 @@
import { getDNS, isIpv4OrIpv6, isLocalhost } from '../modules/fm-manager/index.ts'; import { getDNS, isIpv4OrIpv6, isLocalhost, pipeFileStream } from '../modules/fm-manager/index.ts';
import http from 'node:http'; import http from 'node:http';
import https from 'node:https'; import https from 'node:https';
import { UserApp } from '../modules/user-app/index.ts'; import { UserApp } from '../modules/user-app/index.ts';
@@ -15,6 +15,7 @@ import { logger } from '../modules/logger.ts';
import { UserV1Proxy } from '../modules/ws-proxy/proxy.ts'; import { UserV1Proxy } from '../modules/ws-proxy/proxy.ts';
import { hasBadUser, userIsBanned, appIsBanned, userPathIsBanned } from '@/modules/off/index.ts'; import { hasBadUser, userIsBanned, appIsBanned, userPathIsBanned } from '@/modules/off/index.ts';
import { robotsTxt } from '@/modules/html/index.ts'; import { robotsTxt } from '@/modules/html/index.ts';
import { isBun } from '@/utils/get-engine.ts';
const domain = config?.proxy?.domain; const domain = config?.proxy?.domain;
const allowedOrigins = config?.proxy?.allowedOrigin || []; const allowedOrigins = config?.proxy?.allowedOrigin || [];
@@ -50,7 +51,7 @@ const checkNotAuthPath = (user, app) => {
const forBadUser = (req: http.IncomingMessage, res: http.ServerResponse) => { const forBadUser = (req: http.IncomingMessage, res: http.ServerResponse) => {
// TODO: 记录日志封禁IP等操作 // TODO: 记录日志封禁IP等操作
const dns = getDNS(req); const dns = getDNS(req);
logger.warn(`Bad user access from IP: ${dns.ip}, Host: ${dns.hostName}, URL: ${req.url}`); logger.warn(`forBadUser: Bad user access from IP: ${dns.ip}, Host: ${dns.hostName}, URL: ${req.url}`);
// 这里可以添加更多的处理逻辑比如封禁IP等 // 这里可以添加更多的处理逻辑比如封禁IP等
} }
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => { export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
@@ -132,7 +133,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
const _orings = allowedOrigins || []; const _orings = allowedOrigins || [];
const host = dns.hostName; const host = dns.hostName;
if ( if (
_orings.some((item) => { host && _orings.some((item) => {
return host.includes(item); return host.includes(item);
}) })
) { ) {
@@ -142,11 +143,9 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
let user, app; let user, app;
let domainApp = false; let domainApp = false;
if (isLocalhost(dns.hostName)) { const isDev = isLocalhost(dns.hostName);
// 本地开发环境 测试 if (isDev) {
// user = 'root'; console.debug('开发环境访问:', req.url, 'Host:', dns.hostName);
// app = 'codeflow';
// domainApp = true;
} else { } else {
if (isIpv4OrIpv6(dns.hostName)) { if (isIpv4OrIpv6(dns.hostName)) {
// 打印出 req.url 和错误信息 // 打印出 req.url 和错误信息
@@ -184,14 +183,9 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
const url = pathname; const url = pathname;
if (!domainApp && noProxyUrl.includes(url)) { if (!domainApp && noProxyUrl.includes(url)) {
if (url === '/') { if (url === '/') {
// TODO: 获取一下登陆用户如果没有登陆用户重定向到ai-chat页面
// 重定向到
// res.writeHead(302, { Location: home });
// return res.end();
rediretHome(req, res); rediretHome(req, res);
return; return;
} }
// 不是域名代理且是在不代理的url当中
res.write('No proxy for this URL\n'); res.write('No proxy for this URL\n');
return res.end(); return res.end();
} }
@@ -205,7 +199,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
res.end(robotsTxt); res.end(robotsTxt);
return; return;
} }
if(userPathIsBanned(_user)) { if (userPathIsBanned(_user)) {
logger.warn(`Bad user access from IP: ${dns.ip}, Host: ${dns.hostName}, URL: ${req.url}`); logger.warn(`Bad user access from IP: ${dns.ip}, Host: ${dns.hostName}, URL: ${req.url}`);
} else { } else {
console.log('urls error', urls, 'originUrl:', url); console.log('urls error', urls, 'originUrl:', url);
@@ -259,7 +253,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
let isExist = await userApp.getExist(); let isExist = await userApp.getExist();
logger.debug('userApp', userApp, isExist); logger.debug('userApp', userApp, isExist);
if (userIsBanned(user) || appIsBanned(app)) { if (userIsBanned(user) || appIsBanned(app)) {
forBadUser(req, res); if (!isDev) forBadUser(req, res);
return createErrorPage(); return createErrorPage();
} }
if (!isExist) { if (!isExist) {
@@ -314,7 +308,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
if (!proxyUrl.startsWith('http')) { if (!proxyUrl.startsWith('http')) {
return createNotFoundPage('Invalid proxy url'); return createNotFoundPage('Invalid proxy url');
} }
console.log('proxyUrl', appFileUrl, proxyUrl); console.log('proxyUrl indexFile', appFileUrl, proxyUrl);
httpProxy(req, res, { httpProxy(req, res, {
proxyUrl, proxyUrl,
userApp, userApp,
@@ -332,8 +326,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
const filePath = path.join(fileStore, indexFilePath); const filePath = path.join(fileStore, indexFilePath);
if (!userApp.fileCheck(filePath)) { if (!userApp.fileCheck(filePath)) {
res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8', tips: 'App Cache expired, Please refresh' }); res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8', tips: 'App Cache expired, Please refresh' });
res.write(createRefreshHtml(user, app)); res.end(createRefreshHtml(user, app));
res.end();
await userApp.clearCacheData(); await userApp.clearCacheData();
return; return;
} }
@@ -384,17 +377,14 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
headers.set('Content-Type', contentType); headers.set('Content-Type', contentType);
headers.set('Cache-Control', isHTML ? 'no-cache' : 'public, max-age=3600'); // 设置缓存时间为 1 小时 headers.set('Cache-Control', isHTML ? 'no-cache' : 'public, max-age=3600'); // 设置缓存时间为 1 小时
headers.set('ETag', eTag); headers.set('ETag', eTag);
res?.setHeaders?.(headers); res.writeHead(200, Object.fromEntries(headers));
if (isHTML) { if (isHTML) {
const newHtml = await getTextFromStreamAndAddStat(fs.createReadStream(filePath)); const newHtml = await getTextFromStreamAndAddStat(fs.createReadStream(filePath));
resContent = newHtml.html; resContent = newHtml.html;
headers.set('Content-Length', newHtml.contentLength.toString()); headers.set('Content-Length', newHtml.contentLength.toString());
res.writeHead(200);
res.end(resContent); res.end(resContent);
} else { } else {
res.writeHead(200); pipeFileStream(filePath, res);
const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
} }
return; return;
} }
@@ -402,3 +392,4 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
console.error('getFile error', error); console.error('getFile error', error);
} }
}; };

View File

@@ -1,6 +1,6 @@
import { useFileStore } from '@kevisual/use-config/file-store'; import { useFileStore } from '@kevisual/use-config/file-store';
import { checkAuth, error, router, writeEvents, getKey, getTaskId } from '../router.ts'; import { checkAuth, error, router, writeEvents, getKey, getTaskId } from '../router.ts';
import { IncomingForm } from 'formidable'; import Busboy from 'busboy';
import { app, oss } from '@/app.ts'; import { app, oss } from '@/app.ts';
import { getContentType } from '@/utils/get-content-type.ts'; import { getContentType } from '@/utils/get-content-type.ts';
@@ -8,6 +8,9 @@ import { User } from '@/models/user.ts';
import fs from 'fs'; import fs from 'fs';
import { ConfigModel } from '@/routes/config/models/model.ts'; import { ConfigModel } from '@/routes/config/models/model.ts';
import { validateDirectory } from './util.ts'; import { validateDirectory } from './util.ts';
import path from 'path';
import { createWriteStream } from 'fs';
import { pipeBusboy } from '@/modules/fm-manager/index.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true }); const cacheFilePath = useFileStore('cache-file', { needExists: true });
@@ -23,35 +26,70 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
const url = new URL(req.url || '', 'http://localhost'); const url = new URL(req.url || '', 'http://localhost');
const share = !!url.searchParams.get('public'); const share = !!url.searchParams.get('public');
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles'); const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
// 使用 formidable 解析 multipart/form-data
const form = new IncomingForm({
multiples: false, // 改为单文件上传
uploadDir: cacheFilePath, // 上传文件存储目录
allowEmptyFiles: true, // 允许空
minFileSize: 0, // 最小文件大小
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
keepExtensions: true, // 保留文件拓展名
hashAlgorithm: 'md5', // 文件哈希算法
});
const taskId = getTaskId(req); const taskId = getTaskId(req);
const finalFilePath = `${cacheFilePath}/${taskId}`; const finalFilePath = `${cacheFilePath}/${taskId}`;
if (!taskId) { if (!taskId) {
res.end(error('taskId is required')); res.end(error('taskId is required'));
return; return;
} }
// 解析上传的文件
form.parse(req, async (err, fields, files) => { // 使用 busboy 解析 multipart/form-data
const file = Array.isArray(files.file) ? files.file[0] : files.file; const busboy = Busboy({ headers: req.headers });
const fields: any = {};
let file: any = null;
let tempPath = '';
let filePromise: Promise<void> | null = null;
busboy.on('field', (fieldname, value) => {
fields[fieldname] = value;
});
busboy.on('file', (fieldname, fileStream, info) => {
const { filename, encoding, mimeType } = info;
tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`);
const writeStream = createWriteStream(tempPath);
filePromise = new Promise<void>((resolve, reject) => {
fileStream.pipe(writeStream);
writeStream.on('finish', () => {
file = {
filepath: tempPath,
originalFilename: filename,
mimetype: mimeType,
};
resolve();
});
writeStream.on('error', (err) => {
reject(err);
});
});
});
busboy.on('finish', async () => {
// 等待文件写入完成
if (filePromise) {
try {
await filePromise;
} catch (err) {
console.error(`File write error: ${err.message}`);
res.end(error(`File write error: ${err.message}`));
return;
}
}
const clearFiles = () => { const clearFiles = () => {
if (file) { if (tempPath && fs.existsSync(tempPath)) {
fs.unlinkSync(file.filepath); fs.unlinkSync(tempPath);
}
if (fs.existsSync(finalFilePath)) {
fs.unlinkSync(finalFilePath); fs.unlinkSync(finalFilePath);
} }
}; };
if (err) { if (!file) {
res.end(error(`Upload error: ${err.message}`)); res.end(error('No file uploaded'));
clearFiles();
return; return;
} }
@@ -69,9 +107,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
clearFiles(); clearFiles();
return; return;
} }
const tempPath = file.filepath;
const relativePath = file.originalFilename; const relativePath = file.originalFilename;
// Append chunk to the final file
const writeStream = fs.createWriteStream(finalFilePath, { flags: 'a' }); const writeStream = fs.createWriteStream(finalFilePath, { flags: 'a' });
const readStream = fs.createReadStream(tempPath); const readStream = fs.createReadStream(tempPath);
@@ -195,4 +231,6 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
} }
}); });
}); });
pipeBusboy(req, res, busboy);
}); });

View File

@@ -1,12 +1,15 @@
import { useFileStore } from '@kevisual/use-config/file-store'; import { useFileStore } from '@kevisual/use-config/file-store';
import { checkAuth, error, router, writeEvents, getKey } from '../router.ts'; import { checkAuth, error, router, writeEvents, getKey } from '../router.ts';
import { IncomingForm } from 'formidable'; import Busboy from 'busboy';
import { app, minioClient } from '@/app.ts'; import { app, minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts'; import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts'; import { getContentType } from '@/utils/get-content-type.ts';
import { User } from '@/models/user.ts'; import { User } from '@/models/user.ts';
import fs from 'fs'; import fs from 'fs';
import path from 'path';
import { createWriteStream } from 'fs';
import { pipeBusboy } from '@/modules/fm-manager/pipe-busboy.ts';
import { ConfigModel } from '@/routes/config/models/model.ts'; import { ConfigModel } from '@/routes/config/models/model.ts';
import { validateDirectory } from './util.ts'; import { validateDirectory } from './util.ts';
import { pick } from 'lodash-es'; import { pick } from 'lodash-es';
@@ -103,41 +106,79 @@ router.post('/api/s1/resources/upload', async (req, res) => {
const share = !!url.searchParams.get('public'); const share = !!url.searchParams.get('public');
const meta = parseIfJson(url.searchParams.get('meta')); const meta = parseIfJson(url.searchParams.get('meta'));
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles'); const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
// 使用 formi dable 解析 multipart/form-data // 使用 busboy 解析 multipart/form-data
const form = new IncomingForm({ const busboy = Busboy({ headers: req.headers });
multiples: true, // 支持多文件上传 const fields: any = {};
uploadDir: cacheFilePath, // 上传文件存储目录 const files: any[] = [];
allowEmptyFiles: true, // 允许空 const filePromises: Promise<void>[] = [];
minFileSize: 0, // 最小文件大小 let bytesReceived = 0;
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录 let bytesExpected = parseInt(req.headers['content-length'] || '0');
keepExtensions: true, // 保留文件拓展名
hashAlgorithm: 'md5', // 文件哈希算法 busboy.on('field', (fieldname, value) => {
fields[fieldname] = value;
}); });
form.on('progress', (bytesReceived, bytesExpected) => {
const progress = (bytesReceived / bytesExpected) * 100; busboy.on('file', (fieldname, fileStream, info) => {
const data = { const { filename, encoding, mimeType } = info;
progress: progress.toFixed(2), const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}-${filename}`);
message: `Upload progress: ${progress.toFixed(2)}%`, const writeStream = createWriteStream(tempPath);
};
console.log('progress-upload', data); const filePromise = new Promise<void>((resolve, reject) => {
writeEvents(req, data); fileStream.on('data', (chunk) => {
bytesReceived += chunk.length;
if (bytesExpected > 0) {
const progress = (bytesReceived / bytesExpected) * 100;
const data = {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
console.log('progress-upload', data);
writeEvents(req, data);
}
});
fileStream.pipe(writeStream);
writeStream.on('finish', () => {
files.push({
filepath: tempPath,
originalFilename: filename,
mimetype: mimeType,
});
resolve();
});
writeStream.on('error', (err) => {
reject(err);
});
});
filePromises.push(filePromise);
}); });
// 解析上传的文件
form.parse(req, async (err, fields, files) => { busboy.on('finish', async () => {
// 等待所有文件写入完成
try {
await Promise.all(filePromises);
} catch (err) {
logger.error(`File write error: ${err.message}`);
res.end(error(`File write error: ${err.message}`));
return;
}
const clearFiles = () => { const clearFiles = () => {
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; files.forEach((file) => {
uploadedFiles.forEach((file) => {
if (file?.filepath && fs.existsSync(file.filepath)) { if (file?.filepath && fs.existsSync(file.filepath)) {
fs.unlinkSync(file.filepath); fs.unlinkSync(file.filepath);
} }
}); });
}; };
if (err) {
logger.error(`Upload error: ${err.message}`); // 检查是否有文件上传
res.end(error(`Upload error: ${err.message}`)); if (files.length === 0) {
clearFiles(); res.end(error('files is required'));
return; return;
} }
let { appKey, version, username, directory, description } = getKey(fields, ['appKey', 'version', 'username', 'directory', 'description']); let { appKey, version, username, directory, description } = getKey(fields, ['appKey', 'version', 'username', 'directory', 'description']);
let uid = tokenUser.id; let uid = tokenUser.id;
if (username) { if (username) {
@@ -170,7 +211,7 @@ router.post('/api/s1/resources/upload', async (req, res) => {
return; return;
} }
// 逐个处理每个上传的文件 // 逐个处理每个上传的文件
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file]; const uploadedFiles = files;
logger.info( logger.info(
'upload files', 'upload files',
uploadedFiles.map((item) => { uploadedFiles.map((item) => {
@@ -244,4 +285,6 @@ router.post('/api/s1/resources/upload', async (req, res) => {
); );
} }
}); });
pipeBusboy(req, res, busboy);
}); });

View File

@@ -2,7 +2,6 @@ import { router } from '@/app.ts';
import http from 'http'; import http from 'http';
import { useContextKey } from '@kevisual/context'; import { useContextKey } from '@kevisual/context';
import { checkAuth, error } from './middleware/auth.ts'; import { checkAuth, error } from './middleware/auth.ts';
import formidable from 'formidable';
export { router, checkAuth, error }; export { router, checkAuth, error };
/** /**
@@ -68,7 +67,7 @@ export const deleteOldClients = () => {
* @param parseKeys 需要解析的键 * @param parseKeys 需要解析的键
* @returns 解析后的数据 * @returns 解析后的数据
*/ */
export const getKey = (fields: formidable.Fields<string>, parseKeys: string[]) => { export const getKey = (fields: Record<string, any>, parseKeys: string[]) => {
let value: Record<string, any> = {}; let value: Record<string, any> = {};
for (const key of parseKeys) { for (const key of parseKeys) {
const v = fields[key]; const v = fields[key];

6
src/utils/get-engine.ts Normal file
View File

@@ -0,0 +1,6 @@
export const isBun = typeof Bun !== 'undefined' && Bun?.version != null;
export const isNode = typeof process !== 'undefined' && process?.versions != null && process.versions?.node != null;
// @ts-ignore
export const isDeno = typeof Deno !== 'undefined' && Deno?.version != null && Deno?.version?.deno != null;