Compare commits
5 Commits
d22be3a840
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 18a7c15a76 | |||
| d1e619d04c | |||
| 5e43c5fc4c | |||
| 8a633feb4f | |||
| 15fcfdad18 |
@@ -69,12 +69,12 @@
|
||||
"@kevisual/logger": "^0.0.4",
|
||||
"@kevisual/oss": "0.0.13",
|
||||
"@kevisual/permission": "^0.0.3",
|
||||
"@kevisual/router": "0.0.42",
|
||||
"@kevisual/router": "0.0.48",
|
||||
"@kevisual/types": "^0.0.10",
|
||||
"@kevisual/use-config": "^1.0.21",
|
||||
"@types/archiver": "^7.0.0",
|
||||
"@types/bun": "^1.3.5",
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/formidable": "^3.4.6",
|
||||
"@types/jsonwebtoken": "^9.0.10",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
"@types/node": "^25.0.3",
|
||||
@@ -83,7 +83,6 @@
|
||||
"crypto-js": "^4.2.0",
|
||||
"dayjs": "^1.11.19",
|
||||
"dotenv": "^17.2.3",
|
||||
"formidable": "3.5.4",
|
||||
"ioredis": "^5.8.2",
|
||||
"jsonwebtoken": "^9.0.3",
|
||||
"lodash-es": "^4.17.22",
|
||||
|
||||
32
pnpm-lock.yaml
generated
32
pnpm-lock.yaml
generated
@@ -41,7 +41,7 @@ importers:
|
||||
version: 1.1.1
|
||||
drizzle-orm:
|
||||
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:
|
||||
specifier: ^5.0.1
|
||||
version: 5.0.1
|
||||
@@ -86,8 +86,8 @@ importers:
|
||||
specifier: ^0.0.3
|
||||
version: 0.0.3
|
||||
'@kevisual/router':
|
||||
specifier: 0.0.42
|
||||
version: 0.0.42
|
||||
specifier: 0.0.48
|
||||
version: 0.0.48
|
||||
'@kevisual/types':
|
||||
specifier: ^0.0.10
|
||||
version: 0.0.10
|
||||
@@ -97,6 +97,9 @@ importers:
|
||||
'@types/archiver':
|
||||
specifier: ^7.0.0
|
||||
version: 7.0.0
|
||||
'@types/bun':
|
||||
specifier: ^1.3.5
|
||||
version: 1.3.5
|
||||
'@types/crypto-js':
|
||||
specifier: ^4.2.2
|
||||
version: 4.2.2
|
||||
@@ -252,8 +255,8 @@ packages:
|
||||
'@kevisual/router@0.0.33':
|
||||
resolution: {integrity: sha512-9z7TkSzCIGbXn9SuHPBdZpGwHlAuwA8iN5jNAZBUvbEvBRkBxlrbdCSe9fBYiAHueLm2AceFNrW74uulOiAkqA==}
|
||||
|
||||
'@kevisual/router@0.0.42':
|
||||
resolution: {integrity: sha512-6j254Hl1Q9uM4qKD4v6pcNSXVs7zwHZlyfSxUrNTWrgD7OCt/mrgBpzcNo0TM25/CsdrZCDs21kamienfYQ+lw==}
|
||||
'@kevisual/router@0.0.48':
|
||||
resolution: {integrity: sha512-WsSvT+NpfC/bZbaAzE3WSKD2DRZP0JuPQJGr4YucSdO/lOLB4cEpOZRbPlV3l7G064ow8QJRAN2DUW+bRjrp1A==}
|
||||
|
||||
'@kevisual/types@0.0.10':
|
||||
resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==}
|
||||
@@ -386,6 +389,9 @@ packages:
|
||||
'@types/archiver@7.0.0':
|
||||
resolution: {integrity: sha512-/3vwGwx9n+mCQdYZ2IKGGHEFL30I96UgBlk8EtRDDFQ9uxM1l4O5Ci6r00EMAkiDaTqD9DQ6nVrWRICnBPtzzg==}
|
||||
|
||||
'@types/bun@1.3.5':
|
||||
resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==}
|
||||
|
||||
'@types/busboy@1.5.4':
|
||||
resolution: {integrity: sha512-kG7WrUuAKK0NoyxfQHsVE6j1m01s6kMma64E+OZenQABMQyTJop1DumUWcLwAQ2JzpefU7PDYoRDKl8uZosFjw==}
|
||||
|
||||
@@ -583,6 +589,9 @@ packages:
|
||||
bullmq@5.66.2:
|
||||
resolution: {integrity: sha512-0PrkpIakIntkBcPLltPIRWdLC1FTLUa/VhJkmEfobb5YUQjoUwJdmmf7HX+o/vMonS5048JpP+abf9lVRUFEjA==}
|
||||
|
||||
bun-types@1.3.5:
|
||||
resolution: {integrity: sha512-inmAYe2PFLs0SUbFOWSVD24sg1jFlMPxOjOSSCYqUgn4Hsc3rDc7dFvfVYjFPNHtov6kgUeulV4SxbuIV/stPw==}
|
||||
|
||||
busboy@1.6.0:
|
||||
resolution: {integrity: sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==}
|
||||
engines: {node: '>=10.16.0'}
|
||||
@@ -2145,7 +2154,7 @@ snapshots:
|
||||
transitivePeerDependencies:
|
||||
- supports-color
|
||||
|
||||
'@kevisual/router@0.0.42':
|
||||
'@kevisual/router@0.0.48':
|
||||
dependencies:
|
||||
path-to-regexp: 8.3.0
|
||||
selfsigned: 5.2.0
|
||||
@@ -2353,6 +2362,10 @@ snapshots:
|
||||
dependencies:
|
||||
'@types/readdir-glob': 1.1.5
|
||||
|
||||
'@types/bun@1.3.5':
|
||||
dependencies:
|
||||
bun-types: 1.3.5
|
||||
|
||||
'@types/busboy@1.5.4':
|
||||
dependencies:
|
||||
'@types/node': 25.0.3
|
||||
@@ -2562,6 +2575,10 @@ snapshots:
|
||||
transitivePeerDependencies:
|
||||
- supports-color
|
||||
|
||||
bun-types@1.3.5:
|
||||
dependencies:
|
||||
'@types/node': 25.0.3
|
||||
|
||||
busboy@1.6.0:
|
||||
dependencies:
|
||||
streamsearch: 1.1.0
|
||||
@@ -2713,8 +2730,9 @@ snapshots:
|
||||
|
||||
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:
|
||||
bun-types: 1.3.5
|
||||
pg: 8.16.3
|
||||
|
||||
eastasianwidth@0.2.0: {}
|
||||
|
||||
@@ -29,17 +29,13 @@ export const minioClient = useContextKey('minioClient', () => minioLib.minioClie
|
||||
export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize);
|
||||
|
||||
const init = () => {
|
||||
return new App<{ sequelize: typeof sequelize }>({
|
||||
return new App({
|
||||
serverOptions: {
|
||||
cors: {
|
||||
origin: '*',
|
||||
},
|
||||
// httpType: 'https',
|
||||
},
|
||||
io: true,
|
||||
routerContext: {
|
||||
sequelize,
|
||||
},
|
||||
}
|
||||
});
|
||||
};
|
||||
export const app = useContextKey('app', init);
|
||||
|
||||
69
src/index.ts
69
src/index.ts
@@ -1,62 +1,19 @@
|
||||
import { myConfig as config } from './modules/config.ts';
|
||||
import { app } from './app.ts';
|
||||
import './route.ts';
|
||||
import { handleRequest } from './routes-simple/handle-request.ts';
|
||||
import { port } from './modules/config.ts';
|
||||
import { WssApp } from './modules/ws-proxy/index.ts';
|
||||
import net from 'node:net';
|
||||
// if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
app.listen(port, () => {
|
||||
import { wssFun } from './modules/ws-proxy/index.ts';
|
||||
import { WebSocketListenerFun, HttpListenerFun } from '@kevisual/router/src/server/server-type.js';
|
||||
console.log('Starting server...', port);
|
||||
app.listen(port, '0.0.0.0', () => {
|
||||
console.log(`server is running at http://localhost:${port}`);
|
||||
});
|
||||
app.server.on(handleRequest);
|
||||
|
||||
const wssApp = new WssApp();
|
||||
const main = () => {
|
||||
console.log('Upgrade initialization started');
|
||||
|
||||
app.server.server.on('upgrade', async (req, socket, head) => {
|
||||
const isUpgrade = wssApp.upgrade(req, socket, head);
|
||||
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);
|
||||
app.server.on([{
|
||||
id: 'handle-all',
|
||||
func: handleRequest as any,
|
||||
}, {
|
||||
id: 'wss',
|
||||
io: true,
|
||||
path: '/ws/proxy',
|
||||
func: wssFun as WebSocketListenerFun,
|
||||
}]);
|
||||
|
||||
@@ -38,6 +38,21 @@ export const checkAuth = async (req: http.IncomingMessage, res: http.ServerRespo
|
||||
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) => {
|
||||
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
|
||||
const url = new URL(req.url || '', 'http://localhost');
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useFileStore } from '@kevisual/use-config/file-store';
|
||||
import { minioResources } from './minio.ts';
|
||||
|
||||
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');
|
||||
type ConfigType = {
|
||||
api: {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import path from 'node:path';
|
||||
export const getTextContentType = (ext: string) => {
|
||||
export const getTextContentType = (filePath: string, isFilePath = false) => {
|
||||
let ext = filePath;
|
||||
if (isFilePath) {
|
||||
ext = path.extname(filePath).toLowerCase();
|
||||
}
|
||||
const textContentTypes = [
|
||||
'.tsx',
|
||||
'.jsx', //
|
||||
@@ -12,17 +16,21 @@ export const getTextContentType = (ext: string) => {
|
||||
'.json5',
|
||||
'.pem',
|
||||
'.crt',
|
||||
'.yaml',
|
||||
'.yml',
|
||||
];
|
||||
const include = textContentTypes.includes(ext);
|
||||
if (!include) {
|
||||
return {};
|
||||
if (include) {
|
||||
return {
|
||||
'content-type': 'text/plain; charset=utf-8',
|
||||
};
|
||||
}
|
||||
const contentType = getContentTypeCore(ext);
|
||||
if (!contentType) {
|
||||
return {};
|
||||
}
|
||||
return {
|
||||
'Content-Type': contentType,
|
||||
'content-type': contentType,
|
||||
};
|
||||
};
|
||||
// 获取文件的 content-type
|
||||
@@ -76,8 +84,8 @@ export const getContentTypeCore = (extname: string) => {
|
||||
'.wasm': 'application/wasm', // WebAssembly 文件
|
||||
'.pem': 'application/x-pem-file', // PEM 证书文件
|
||||
'.crt': 'application/x-x509-ca-cert', // CRT 证书文件
|
||||
'.yaml': 'application/x-yaml; charset=utf-8', // YAML 文件
|
||||
'.yml': 'application/x-yaml; charset=utf-8', // YAML 文件(别名)
|
||||
'.yaml': 'application/yaml; charset=utf-8', // YAML 文件
|
||||
'.yml': 'application/yaml; charset=utf-8', // YAML 文件(别名)
|
||||
'.zip': 'application/octet-stream',
|
||||
};
|
||||
return contentType[extname];
|
||||
|
||||
@@ -8,3 +8,7 @@ export * from './get-router.ts'
|
||||
export * from './get-content-type.ts'
|
||||
|
||||
export * from './utils.ts'
|
||||
|
||||
export * from './pipe.ts'
|
||||
|
||||
export { pipeBusboy } from './pipe-busboy.ts'
|
||||
13
src/modules/fm-manager/pipe-busboy.ts
Normal file
13
src/modules/fm-manager/pipe-busboy.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
138
src/modules/fm-manager/pipe.ts
Normal file
138
src/modules/fm-manager/pipe.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import * as http from 'http';
|
||||
import * as fs from 'fs';
|
||||
import { isBun } from './utils.ts';
|
||||
import Stream from 'stream';
|
||||
|
||||
/**
|
||||
* 文件流管道传输函数
|
||||
* 将指定文件的内容通过流的方式传输给客户端响应
|
||||
* @param filePath 要传输的文件路径
|
||||
* @param res HTTP服务器响应对象
|
||||
*/
|
||||
export const pipeFileStream = (filePath: string, res: http.ServerResponse) => {
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
if (isBun) {
|
||||
// Bun环境下的流处理方式
|
||||
res.pipe(readStream as any);
|
||||
} else {
|
||||
// Node.js标准环境下的流处理方式,end:true表示在流结束时自动关闭响应
|
||||
readStream.pipe(res, { end: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用流管道传输函数
|
||||
* 将可读流的数据传输给客户端响应
|
||||
* @param readStream 可读流对象
|
||||
* @param res HTTP服务器响应对象
|
||||
*/
|
||||
export const pipeStream = (readStream: fs.ReadStream, res: http.ServerResponse) => {
|
||||
if (isBun) {
|
||||
// Bun环境下的流处理方式
|
||||
res.pipe(readStream as any);
|
||||
} else {
|
||||
// Node.js标准环境下的流处理方式
|
||||
readStream.pipe(res, { end: true });
|
||||
}
|
||||
}
|
||||
|
||||
export const pipeMinioStream = (minioStream: Stream.Readable, res: http.ServerResponse) => {
|
||||
if (isBun) {
|
||||
const chunks: Buffer[] = [];
|
||||
// 监听数据到达事件,收集所有数据块
|
||||
minioStream.on('data', (chunk: Buffer) => {
|
||||
chunks.push(chunk);
|
||||
});
|
||||
// 监听数据结束事件,将收集的数据合并并发送给客户端
|
||||
minioStream.on('end', () => {
|
||||
const result = Buffer.concat(chunks);
|
||||
res.end(result);
|
||||
});
|
||||
|
||||
// 监听错误事件,处理代理响应过程中的错误
|
||||
minioStream.on('error', (error) => {
|
||||
res.writeHead(500);
|
||||
res.end(JSON.stringify({ error: error.message }));
|
||||
});
|
||||
} else {
|
||||
minioStream.pipe(res, { end: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理响应流传输函数
|
||||
* 将代理服务器返回的响应数据传输给客户端
|
||||
* 处理从目标服务器收到的响应流并转发给原始客户端
|
||||
* @param proxyRes 代理服务器的响应对象
|
||||
* @param res HTTP服务器响应对象
|
||||
*/
|
||||
export const pipeProxyRes = (proxyRes: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
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();
|
||||
res.end(result);
|
||||
});
|
||||
|
||||
// 监听错误事件,处理代理响应过程中的错误
|
||||
proxyRes.on('error', (error) => {
|
||||
res.writeHead(500);
|
||||
res.end(JSON.stringify({ error: error.message }));
|
||||
});
|
||||
|
||||
} else {
|
||||
// Node.js标准环境下直接使用pipe进行流传输
|
||||
proxyRes.pipe(res, { end: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理请求流传输函数
|
||||
* 将客户端的请求数据传输给代理服务器
|
||||
* 处理来自客户端的请求流并转发给目标服务器
|
||||
* @param req 客户端的请求对象
|
||||
* @param proxyReq 代理服务器的请求对象
|
||||
*/
|
||||
export const pipeProxyReq = async (req: http.IncomingMessage, proxyReq: http.ClientRequest, res: any) => {
|
||||
if (isBun) {
|
||||
try {
|
||||
// @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();
|
||||
} catch (error) {
|
||||
proxyReq.destroy(error);
|
||||
}
|
||||
} else {
|
||||
// Node.js标准环境下直接使用pipe进行流传输
|
||||
req.pipe(proxyReq, { end: true });
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,12 @@ import { getUserFromRequest } from '../utils.ts';
|
||||
import { UserPermission, Permission } from '@kevisual/permission';
|
||||
import { getLoginUser } from '@/modules/auth.ts';
|
||||
import busboy from 'busboy';
|
||||
import { getContentType } from '../get-content-type.ts';
|
||||
import { getContentType, getTextContentType } from '../get-content-type.ts';
|
||||
import { OssBase } from '@kevisual/oss';
|
||||
import { parseSearchValue } from '@kevisual/router/browser';
|
||||
import { logger } from '@/modules/logger.ts';
|
||||
import { pipeBusboy } from '../pipe-busboy.ts';
|
||||
import { pipeMinioStream } from '../pipe.ts';
|
||||
|
||||
type FileList = {
|
||||
name: string;
|
||||
@@ -116,12 +118,14 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
|
||||
etag,
|
||||
'last-modified': lastModified,
|
||||
...filterMetaData,
|
||||
...getTextContentType(objectName, true),
|
||||
};
|
||||
|
||||
res.writeHead(200, {
|
||||
...headers,
|
||||
});
|
||||
objectStream.pipe(res, { end: true });
|
||||
// objectStream.pipe(res, { end: true });
|
||||
// @ts-ignore
|
||||
pipeMinioStream(objectStream, res);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`Proxy request error: ${error.message}`);
|
||||
@@ -226,7 +230,7 @@ export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts:
|
||||
end({ error: err }, '文件解析失败', 500);
|
||||
});
|
||||
|
||||
req.pipe(bb);
|
||||
pipeBusboy(req, res, bb);
|
||||
};
|
||||
export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?: boolean }) => {
|
||||
const _u = new URL(req.url, 'http://localhost');
|
||||
|
||||
@@ -10,7 +10,7 @@ import { addStat } from '@/modules/html/stat/index.ts';
|
||||
import path from 'path';
|
||||
import { getTextContentType } from '@/modules/fm-manager/index.ts';
|
||||
import { logger } from '@/modules/logger.ts';
|
||||
|
||||
import { pipeStream } from '../pipe.ts';
|
||||
const pipelineAsync = promisify(pipeline);
|
||||
|
||||
export async function downloadFileFromMinio(fileUrl: string, destFile: string) {
|
||||
@@ -74,7 +74,7 @@ export async function minioProxy(
|
||||
res.writeHead(200, {
|
||||
...headers,
|
||||
});
|
||||
objectStream.pipe(res, { end: true });
|
||||
pipeStream(objectStream as any, res);
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
@@ -154,7 +154,7 @@ export const httpProxy = async (
|
||||
res.writeHead(proxyRes.statusCode, {
|
||||
...headers,
|
||||
});
|
||||
proxyRes.pipe(res, { end: true });
|
||||
pipeStream(proxyRes as any, res);
|
||||
}
|
||||
});
|
||||
proxyReq.on('error', (err) => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import http from 'http';
|
||||
import http from 'node:http';
|
||||
import { minioClient } from '@/modules/minio.ts';
|
||||
import { pipeMinioStream } from '../pipe.ts';
|
||||
type ProxyInfo = {
|
||||
path?: string;
|
||||
target: string;
|
||||
@@ -15,7 +16,8 @@ export const minioProxyOrigin = async (req: http.IncomingMessage, res: http.Serv
|
||||
objectName = objectName.slice(bucketName.length);
|
||||
}
|
||||
const objectStream = await minioClient.getObject(bucketName, objectName);
|
||||
objectStream.pipe(res);
|
||||
// objectStream.pipe(res);
|
||||
pipeMinioStream(objectStream, res);
|
||||
} catch (error) {
|
||||
console.error('Error fetching object from MinIO:', error);
|
||||
res.statusCode = 500;
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
import { IncomingMessage } from 'node:http';
|
||||
import http from 'node:http';
|
||||
import { logger } from '../logger.ts';
|
||||
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;
|
||||
|
||||
export const getUserFromRequest = (req: IncomingMessage) => {
|
||||
const url = new URL(req.url, `http://${req.headers.host}`);
|
||||
@@ -14,8 +21,8 @@ export const getUserFromRequest = (req: IncomingMessage) => {
|
||||
|
||||
|
||||
export const getDNS = (req: http.IncomingMessage) => {
|
||||
const hostName = req.headers.host;
|
||||
const ip = req.socket.remoteAddress;
|
||||
const hostName = req.headers?.host;
|
||||
const ip = req?.socket?.remoteAddress || '';
|
||||
return { hostName, ip };
|
||||
};
|
||||
|
||||
|
||||
@@ -283,6 +283,10 @@ const commonSensitiveFiles = [
|
||||
"brevo",
|
||||
];
|
||||
|
||||
const appSensitiveFiles = [
|
||||
"App_Handlers",
|
||||
"CMS",
|
||||
]
|
||||
// ========================
|
||||
// 🚨 汇总:所有敏感文件路径(供安全系统统一调用)
|
||||
// ========================
|
||||
@@ -297,6 +301,7 @@ export const ALL_SENSITIVE_FILES = [
|
||||
...wordpressFiles,
|
||||
...secretFiles,
|
||||
...gitFiles,
|
||||
...appSensitiveFiles,
|
||||
...commonSensitiveFiles
|
||||
];
|
||||
|
||||
|
||||
@@ -1,48 +1,28 @@
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { WsProxyManager } from './manager.ts';
|
||||
import { getLoginUser } from '@/modules/auth.ts';
|
||||
import { getLoginUserByToken } from '@/modules/auth.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
export const wsProxyManager = new WsProxyManager();
|
||||
|
||||
export const upgrade = (request: any, socket: any, head: any) => {
|
||||
const req = request as any;
|
||||
const url = new URL(req.url, 'http://localhost');
|
||||
const id = url.searchParams.get('id');
|
||||
if (url.pathname === '/ws/proxy') {
|
||||
console.log('upgrade', request.url, id);
|
||||
wss.handleUpgrade(req, socket, head, (ws) => {
|
||||
// 这里手动触发 connection 事件
|
||||
console.log('emitting connection event');
|
||||
// @ts-ignore
|
||||
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('未登录,断开连接');
|
||||
import { WebScoketListenerFun } from '@kevisual/router/src/server/server-type.ts'
|
||||
export const wssFun: WebScoketListenerFun = async (req, res) => {
|
||||
// do nothing, just to enable ws upgrade event
|
||||
const { id, ws, token, data, emitter } = req;
|
||||
logger.debug('ws proxy connected, id=', id, ' token=', token, ' data=', data);
|
||||
// console.log('req', req)
|
||||
const { type } = data || {};
|
||||
if (type === 'registryClient') {
|
||||
const loginUser = await getLoginUserByToken(token);
|
||||
if (!loginUser?.tokenUser) {
|
||||
logger.debug('未登录,断开连接');
|
||||
ws.send(JSON.stringify({ code: 401, message: '未登录' }));
|
||||
setTimeout(() => {
|
||||
ws.close();
|
||||
ws.close(401, 'Unauthorized');
|
||||
}, 1000);
|
||||
return;
|
||||
}
|
||||
const user = loginUser.tokenUser.username;
|
||||
const user = loginUser?.tokenUser?.username;
|
||||
const userApp = user + '-' + id;
|
||||
console.log('注册 ws 连接', userApp);
|
||||
logger.debug('注册 ws 连接', userApp);
|
||||
// @ts-ignore
|
||||
wsProxyManager.register(userApp, { user, ws });
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
@@ -51,113 +31,22 @@ wss.on('connection', async (ws, req) => {
|
||||
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');
|
||||
emitter.once('close--' + id, () => {
|
||||
logger.debug('ws emitter closed');
|
||||
wsProxyManager.unregister(userApp);
|
||||
});
|
||||
});
|
||||
|
||||
export class WssApp {
|
||||
wss: WebSocketServer;
|
||||
bunWSS = websocket;
|
||||
constructor() {
|
||||
this.wss = wss;
|
||||
// @ts-ignore
|
||||
ws.data.userApp = userApp;
|
||||
return;
|
||||
}
|
||||
upgrade(request: any, socket: any, head: any) {
|
||||
// return upgrade(request, socket, head);
|
||||
return bunUpgrade(request);
|
||||
// @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);
|
||||
}
|
||||
}
|
||||
|
||||
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.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const user = loginUser.tokenUser.username;
|
||||
const userApp = user + '-' + id;
|
||||
console.log('注册 ws 连接', userApp);
|
||||
|
||||
ws.data.userApp = userApp;
|
||||
ws.data.user = user;
|
||||
|
||||
wsProxyManager.register(userApp, { user, ws });
|
||||
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: 'connected',
|
||||
user: user,
|
||||
id,
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
||||
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);
|
||||
}
|
||||
},
|
||||
|
||||
error(ws: any, error: Error) {
|
||||
console.error('WebSocket error:', error);
|
||||
},
|
||||
};
|
||||
@@ -10,18 +10,11 @@ class WsMessage {
|
||||
this.ws = ws;
|
||||
this.user = user;
|
||||
this.emitter = new EventEmitter();
|
||||
this.listenMessage();
|
||||
}
|
||||
async listenMessage() {
|
||||
this.ws.on('message', (event: Buffer) => {
|
||||
const eventData = event.toString();
|
||||
if (!eventData) {
|
||||
return;
|
||||
async sendResponse(data: any) {
|
||||
if (data.id) {
|
||||
this.emitter.emit(data.id, data?.data);
|
||||
}
|
||||
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 }) {
|
||||
if (this.ws.readyState !== WebSocket.OPEN) {
|
||||
@@ -65,6 +58,7 @@ export class WsProxyManager {
|
||||
value.ws.close();
|
||||
}
|
||||
}
|
||||
console.log('WsProxyManager register', id);
|
||||
const value = new WsMessage({ ws: opts?.ws, user: opts?.user });
|
||||
this.wssMap.set(id, value);
|
||||
}
|
||||
|
||||
@@ -11,26 +11,36 @@ type ProxyOptions = {
|
||||
export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
|
||||
const { url } = req;
|
||||
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) {
|
||||
opts?.createNotFoundPage?.('应用未找到');
|
||||
return false;
|
||||
}
|
||||
|
||||
const data = await App.handleRequest(req, res);
|
||||
const loginUser = await getLoginUser(req);
|
||||
if (!loginUser) {
|
||||
opts?.createNotFoundPage?.('没有登录');
|
||||
return false;
|
||||
}
|
||||
if (loginUser.tokenUser?.username !== user) {
|
||||
const isAdmin = loginUser.tokenUser?.username === user
|
||||
// TODO: 如果不是管理员,是否需要添加其他人可以访问的逻辑?
|
||||
if (!isAdmin) {
|
||||
opts?.createNotFoundPage?.('没有访问应用权限');
|
||||
return false;
|
||||
}
|
||||
if (!userAppKey.startsWith(user + '-')) {
|
||||
userAppKey = user + '-' + userAppKey;
|
||||
}
|
||||
logger.debug('data', data);
|
||||
const client = wsProxyManager.get(userAppKey);
|
||||
const ids = wsProxyManager.getIds();
|
||||
if (!client) {
|
||||
opts?.createNotFoundPage?.(`未找到应用, ${userAppKey}, ${ids.join(',')}`);
|
||||
if (isAdmin) {
|
||||
opts?.createNotFoundPage?.(`未找到应用 [${userAppKey}], 当前应用列表: ${ids.join(',')}`);
|
||||
} else {
|
||||
opts?.createNotFoundPage?.('应用访问失败');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
const value = await client.sendData(data);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IncomingForm } from 'formidable';
|
||||
import Busboy from 'busboy';
|
||||
import { checkAuth } from '../middleware/auth.ts';
|
||||
import { router, clients, writeEvents } from '../router.ts';
|
||||
import { error } from '../middleware/auth.ts';
|
||||
@@ -7,26 +7,43 @@ import { useFileStore } from '@kevisual/use-config/file-store';
|
||||
import { app, minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.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 });
|
||||
|
||||
router.post('/api/micro-app/upload', async (req, res) => {
|
||||
if (res.headersSent) return; // 如果响应已发送,不再处理
|
||||
if (res.headersSent) return; // 如果响应已发送,不再处理
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
const { tokenUser, token } = await checkAuth(req, res);
|
||||
if (!tokenUser) return;
|
||||
//
|
||||
// 使用 formidable 解析 multipart/form-data
|
||||
const form = new IncomingForm({
|
||||
multiples: false, // 支持多文件上传
|
||||
uploadDir: cacheFilePath, // 上传文件存储目录
|
||||
allowEmptyFiles: true, // 允许空
|
||||
minFileSize: 0, // 最小文件大小
|
||||
maxFiles: 1, // 最大文件数量
|
||||
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
|
||||
keepExtensions: true, // 保留文件
|
||||
hashAlgorithm: 'md5', // 文件哈希算法
|
||||
|
||||
// 使用 busboy 解析 multipart/form-data
|
||||
const busboy = Busboy({ headers: req.headers, preservePath: true });
|
||||
const fields: any = {};
|
||||
let file: any = null;
|
||||
let filePromise: Promise<void> | null = null;
|
||||
let bytesReceived = 0;
|
||||
let bytesExpected = parseInt(req.headers['content-length'] || '0');
|
||||
|
||||
busboy.on('field', (fieldname, value) => {
|
||||
fields[fieldname] = value;
|
||||
});
|
||||
form.on('progress', (bytesReceived, bytesExpected) => {
|
||||
|
||||
busboy.on('file', (fieldname, fileStream, info) => {
|
||||
const { filename, encoding, mimeType } = info;
|
||||
const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`);
|
||||
const writeStream = createWriteStream(tempPath);
|
||||
const hash = crypto.createHash('md5');
|
||||
let size = 0;
|
||||
|
||||
filePromise = new Promise<void>((resolve, reject) => {
|
||||
fileStream.on('data', (chunk) => {
|
||||
bytesReceived += chunk.length;
|
||||
size += chunk.length;
|
||||
hash.update(chunk);
|
||||
if (bytesExpected > 0) {
|
||||
const progress = (bytesReceived / bytesExpected) * 100;
|
||||
console.log(`Upload progress: ${progress.toFixed(2)}%`);
|
||||
const data = {
|
||||
@@ -34,23 +51,50 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
message: `Upload progress: ${progress.toFixed(2)}%`,
|
||||
};
|
||||
writeEvents(req, data);
|
||||
}
|
||||
});
|
||||
// 解析上传的文件
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
if (err) {
|
||||
res.end(error(`Upload error: ${err.message}`));
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
fs.unlinkSync(file.filepath);
|
||||
|
||||
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 uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
if (file?.filepath && fs.existsSync(file.filepath)) {
|
||||
fs.unlinkSync(file.filepath);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (!file) {
|
||||
res.end(error('No file uploaded'));
|
||||
return;
|
||||
}
|
||||
|
||||
let appKey, collection;
|
||||
const { appKey: _appKey, collection: _collecion } = fields;
|
||||
if (Array.isArray(_appKey)) {
|
||||
@@ -68,12 +112,9 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
appKey = appKey || 'micro-app';
|
||||
console.log('Appkey', appKey);
|
||||
console.log('collection', collection);
|
||||
// 逐个处理每个上传的文件
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
|
||||
// 处理上传的文件
|
||||
const uploadResults = [];
|
||||
for (let i = 0; i < uploadedFiles.length; i++) {
|
||||
const file = uploadedFiles[i];
|
||||
// @ts-ignore
|
||||
const tempPath = file.filepath; // 文件上传时的临时路径
|
||||
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
|
||||
// 比如 child2/b.txt
|
||||
@@ -92,7 +133,7 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
size: file.size,
|
||||
});
|
||||
fs.unlinkSync(tempPath); // 删除临时文件
|
||||
}
|
||||
|
||||
// 受控
|
||||
const r = await app.call({
|
||||
path: 'micro-app',
|
||||
@@ -115,6 +156,8 @@ router.post('/api/micro-app/upload', async (req, res) => {
|
||||
}
|
||||
res.end(JSON.stringify(data));
|
||||
});
|
||||
|
||||
pipeBusboy(req, res, busboy);
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useFileStore } from '@kevisual/use-config/file-store';
|
||||
import http from 'node:http';
|
||||
import fs from 'fs';
|
||||
import { IncomingForm } from 'formidable';
|
||||
import Busboy from 'busboy';
|
||||
import { app, minioClient } from '@/app.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 './index.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 });
|
||||
|
||||
router.get('/api/app/upload', async (req, res) => {
|
||||
@@ -23,17 +26,28 @@ router.post('/api/app/upload', async (req, res) => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
const { tokenUser, token } = await checkAuth(req, res);
|
||||
if (!tokenUser) return;
|
||||
// 使用 formidable 解析 multipart/form-data
|
||||
const form = new IncomingForm({
|
||||
multiples: true, // 支持多文件上传
|
||||
uploadDir: cacheFilePath, // 上传文件存储目录
|
||||
allowEmptyFiles: true, // 允许空
|
||||
minFileSize: 0, // 最小文件大小
|
||||
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
|
||||
keepExtensions: true, // 保留文件
|
||||
hashAlgorithm: 'md5', // 文件哈希算法
|
||||
|
||||
// 使用 busboy 解析 multipart/form-data
|
||||
const busboy = Busboy({ headers: req.headers, preservePath: true });
|
||||
const fields: any = {};
|
||||
const files: any = [];
|
||||
const filePromises: Promise<void>[] = [];
|
||||
let bytesReceived = 0;
|
||||
let bytesExpected = parseInt(req.headers['content-length'] || '0');
|
||||
|
||||
busboy.on('field', (fieldname, value) => {
|
||||
fields[fieldname] = value;
|
||||
});
|
||||
form.on('progress', (bytesReceived, bytesExpected) => {
|
||||
|
||||
busboy.on('file', (fieldname, fileStream, info) => {
|
||||
const { filename, encoding, mimeType } = info;
|
||||
const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`);
|
||||
const writeStream = createWriteStream(tempPath);
|
||||
|
||||
const filePromise = new Promise<void>((resolve, reject) => {
|
||||
fileStream.on('data', (chunk) => {
|
||||
bytesReceived += chunk.length;
|
||||
if (bytesExpected > 0) {
|
||||
const progress = (bytesReceived / bytesExpected) * 100;
|
||||
console.log(`Upload progress: ${progress.toFixed(2)}%`);
|
||||
const data = {
|
||||
@@ -41,23 +55,51 @@ router.post('/api/app/upload', async (req, res) => {
|
||||
message: `Upload progress: ${progress.toFixed(2)}%`,
|
||||
};
|
||||
writeEvents(req, data);
|
||||
}
|
||||
});
|
||||
// 解析上传的文件
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
if (err) {
|
||||
res.end(error(`Upload error: ${err.message}`));
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
fs.unlinkSync(file.filepath);
|
||||
|
||||
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;
|
||||
}
|
||||
const clearFiles = () => {
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
files.forEach((file: any) => {
|
||||
if (file?.filepath && fs.existsSync(file.filepath)) {
|
||||
fs.unlinkSync(file.filepath);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查是否有文件上传
|
||||
if (files.length === 0) {
|
||||
res.end(error('files is required'));
|
||||
return;
|
||||
}
|
||||
|
||||
let appKey,
|
||||
version,
|
||||
username = '';
|
||||
@@ -99,11 +141,9 @@ router.post('/api/app/upload', async (req, res) => {
|
||||
console.log('Appkey', appKey, version);
|
||||
|
||||
// 逐个处理每个上传的文件
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
const uploadResults = [];
|
||||
for (let i = 0; i < uploadedFiles.length; i++) {
|
||||
const file = uploadedFiles[i];
|
||||
// @ts-ignore
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const tempPath = file.filepath; // 文件上传时的临时路径
|
||||
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
|
||||
// 比如 child2/b.txt
|
||||
@@ -144,6 +184,8 @@ router.post('/api/app/upload', async (req, res) => {
|
||||
}
|
||||
res.end(JSON.stringify(data));
|
||||
});
|
||||
|
||||
pipeBusboy(req, res, busboy);
|
||||
});
|
||||
|
||||
router.get('/api/container/file/:id', async (req, res) => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { bucketName } from '@/modules/minio.ts';
|
||||
import { getLoginUser } from '../middleware/auth.ts';
|
||||
import { BucketItemStat } from 'minio';
|
||||
import { UserPermission, Permission } from '@kevisual/permission';
|
||||
import { pipeMinioStream } from '@/modules/fm-manager/index.ts';
|
||||
|
||||
/**
|
||||
* 过滤 metaData 中的 key, 去除 password, accesskey, secretkey,
|
||||
@@ -101,5 +102,6 @@ export const authMinio = async (req: IncomingMessage, res: ServerResponse, objec
|
||||
});
|
||||
const objectStream = await minioClient.getObject(bucketName, objectName);
|
||||
|
||||
objectStream.pipe(res, { end: true });
|
||||
// objectStream.pipe(res, { end: true });
|
||||
pipeMinioStream(objectStream, res);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { getDNS, isIpv4OrIpv6, isLocalhost } from '../modules/fm-manager/index.ts';
|
||||
import { getDNS, isIpv4OrIpv6, isLocalhost, pipeFileStream, pipeProxyReq, pipeProxyRes } from '../modules/fm-manager/index.ts';
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
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 { hasBadUser, userIsBanned, appIsBanned, userPathIsBanned } from '@/modules/off/index.ts';
|
||||
import { robotsTxt } from '@/modules/html/index.ts';
|
||||
import { isBun } from '@/utils/get-engine.ts';
|
||||
const domain = config?.proxy?.domain;
|
||||
const allowedOrigins = config?.proxy?.allowedOrigin || [];
|
||||
|
||||
@@ -50,7 +51,7 @@ const checkNotAuthPath = (user, app) => {
|
||||
const forBadUser = (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
// TODO: 记录日志,封禁IP等操作
|
||||
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等
|
||||
}
|
||||
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
|
||||
@@ -109,7 +110,8 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
// 将代理服务器的响应头和状态码返回给客户端
|
||||
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
||||
// 将代理响应流写入客户端响应
|
||||
proxyRes.pipe(res, { end: true });
|
||||
// proxyRes.pipe(res, { end: true });
|
||||
pipeProxyRes(proxyRes, res);
|
||||
});
|
||||
// 处理代理请求的错误事件
|
||||
proxyReq.on('error', (err) => {
|
||||
@@ -118,7 +120,8 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
res.write(`Proxy request error: ${err.message}`);
|
||||
});
|
||||
// 处理 POST 请求的请求体(传递数据到目标服务器)
|
||||
req.pipe(proxyReq, { end: true });
|
||||
// req.pipe(proxyReq, { end: true });
|
||||
pipeProxyReq(req, proxyReq, res);
|
||||
return;
|
||||
}
|
||||
if (req.url.startsWith('/api') || req.url.startsWith('/v1')) {
|
||||
@@ -132,7 +135,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
const _orings = allowedOrigins || [];
|
||||
const host = dns.hostName;
|
||||
if (
|
||||
_orings.some((item) => {
|
||||
host && _orings.some((item) => {
|
||||
return host.includes(item);
|
||||
})
|
||||
) {
|
||||
@@ -142,11 +145,9 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
|
||||
let user, app;
|
||||
let domainApp = false;
|
||||
if (isLocalhost(dns.hostName)) {
|
||||
// 本地开发环境 测试
|
||||
// user = 'root';
|
||||
// app = 'codeflow';
|
||||
// domainApp = true;
|
||||
const isDev = isLocalhost(dns.hostName);
|
||||
if (isDev) {
|
||||
console.debug('开发环境访问:', req.url, 'Host:', dns.hostName);
|
||||
} else {
|
||||
if (isIpv4OrIpv6(dns.hostName)) {
|
||||
// 打印出 req.url 和错误信息
|
||||
@@ -184,14 +185,9 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
const url = pathname;
|
||||
if (!domainApp && noProxyUrl.includes(url)) {
|
||||
if (url === '/') {
|
||||
// TODO: 获取一下登陆用户,如果没有登陆用户,重定向到ai-chat页面
|
||||
// 重定向到
|
||||
// res.writeHead(302, { Location: home });
|
||||
// return res.end();
|
||||
rediretHome(req, res);
|
||||
return;
|
||||
}
|
||||
// 不是域名代理,且是在不代理的url当中
|
||||
res.write('No proxy for this URL\n');
|
||||
return res.end();
|
||||
}
|
||||
@@ -205,7 +201,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
res.end(robotsTxt);
|
||||
return;
|
||||
}
|
||||
if(userPathIsBanned(_user)) {
|
||||
if (userPathIsBanned(_user)) {
|
||||
logger.warn(`Bad user access from IP: ${dns.ip}, Host: ${dns.hostName}, URL: ${req.url}`);
|
||||
} else {
|
||||
console.log('urls error', urls, 'originUrl:', url);
|
||||
@@ -259,7 +255,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
let isExist = await userApp.getExist();
|
||||
logger.debug('userApp', userApp, isExist);
|
||||
if (userIsBanned(user) || appIsBanned(app)) {
|
||||
forBadUser(req, res);
|
||||
if (!isDev) forBadUser(req, res);
|
||||
return createErrorPage();
|
||||
}
|
||||
if (!isExist) {
|
||||
@@ -314,7 +310,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
if (!proxyUrl.startsWith('http')) {
|
||||
return createNotFoundPage('Invalid proxy url');
|
||||
}
|
||||
console.log('proxyUrl', appFileUrl, proxyUrl);
|
||||
console.log('proxyUrl indexFile', appFileUrl, proxyUrl);
|
||||
httpProxy(req, res, {
|
||||
proxyUrl,
|
||||
userApp,
|
||||
@@ -332,8 +328,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
const filePath = path.join(fileStore, indexFilePath);
|
||||
if (!userApp.fileCheck(filePath)) {
|
||||
res.writeHead(500, { 'Content-Type': 'text/html; charset=utf-8', tips: 'App Cache expired, Please refresh' });
|
||||
res.write(createRefreshHtml(user, app));
|
||||
res.end();
|
||||
res.end(createRefreshHtml(user, app));
|
||||
await userApp.clearCacheData();
|
||||
return;
|
||||
}
|
||||
@@ -384,17 +379,14 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
headers.set('Content-Type', contentType);
|
||||
headers.set('Cache-Control', isHTML ? 'no-cache' : 'public, max-age=3600'); // 设置缓存时间为 1 小时
|
||||
headers.set('ETag', eTag);
|
||||
res?.setHeaders?.(headers);
|
||||
res.writeHead(200, Object.fromEntries(headers));
|
||||
if (isHTML) {
|
||||
const newHtml = await getTextFromStreamAndAddStat(fs.createReadStream(filePath));
|
||||
resContent = newHtml.html;
|
||||
headers.set('Content-Length', newHtml.contentLength.toString());
|
||||
res.writeHead(200);
|
||||
res.end(resContent);
|
||||
} else {
|
||||
res.writeHead(200);
|
||||
const readStream = fs.createReadStream(filePath);
|
||||
readStream.pipe(res);
|
||||
pipeFileStream(filePath, res);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -402,3 +394,4 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
||||
console.error('getFile error', error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useFileStore } from '@kevisual/use-config/file-store';
|
||||
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 { getContentType } from '@/utils/get-content-type.ts';
|
||||
@@ -8,6 +8,9 @@ import { User } from '@/models/user.ts';
|
||||
import fs from 'fs';
|
||||
import { ConfigModel } from '@/routes/config/models/model.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 });
|
||||
|
||||
@@ -23,35 +26,70 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
|
||||
const url = new URL(req.url || '', 'http://localhost');
|
||||
const share = !!url.searchParams.get('public');
|
||||
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 finalFilePath = `${cacheFilePath}/${taskId}`;
|
||||
if (!taskId) {
|
||||
res.end(error('taskId is required'));
|
||||
return;
|
||||
}
|
||||
// 解析上传的文件
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
const file = Array.isArray(files.file) ? files.file[0] : files.file;
|
||||
|
||||
// 使用 busboy 解析 multipart/form-data
|
||||
const busboy = Busboy({ headers: req.headers, preservePath: true });
|
||||
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)}`);
|
||||
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 = () => {
|
||||
if (file) {
|
||||
fs.unlinkSync(file.filepath);
|
||||
if (tempPath && fs.existsSync(tempPath)) {
|
||||
fs.unlinkSync(tempPath);
|
||||
}
|
||||
if (fs.existsSync(finalFilePath)) {
|
||||
fs.unlinkSync(finalFilePath);
|
||||
}
|
||||
};
|
||||
|
||||
if (err) {
|
||||
res.end(error(`Upload error: ${err.message}`));
|
||||
clearFiles();
|
||||
if (!file) {
|
||||
res.end(error('No file uploaded'));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -69,9 +107,7 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
|
||||
clearFiles();
|
||||
return;
|
||||
}
|
||||
const tempPath = file.filepath;
|
||||
const relativePath = file.originalFilename;
|
||||
// Append chunk to the final file
|
||||
|
||||
const writeStream = fs.createWriteStream(finalFilePath, { flags: 'a' });
|
||||
const readStream = fs.createReadStream(tempPath);
|
||||
@@ -195,4 +231,6 @@ router.post('/api/s1/resources/upload/chunk', async (req, res) => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
pipeBusboy(req, res, busboy);
|
||||
});
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { useFileStore } from '@kevisual/use-config/file-store';
|
||||
import { checkAuth, error, router, writeEvents, getKey } from '../router.ts';
|
||||
import { IncomingForm } from 'formidable';
|
||||
import Busboy from 'busboy';
|
||||
import { app, minioClient } from '@/app.ts';
|
||||
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
import { getContentType } from '@/utils/get-content-type.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
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 { validateDirectory } from './util.ts';
|
||||
import { pick } from 'lodash-es';
|
||||
@@ -103,17 +106,25 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
||||
const share = !!url.searchParams.get('public');
|
||||
const meta = parseIfJson(url.searchParams.get('meta'));
|
||||
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
|
||||
// 使用 formi dable 解析 multipart/form-data
|
||||
const form = new IncomingForm({
|
||||
multiples: true, // 支持多文件上传
|
||||
uploadDir: cacheFilePath, // 上传文件存储目录
|
||||
allowEmptyFiles: true, // 允许空
|
||||
minFileSize: 0, // 最小文件大小
|
||||
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
|
||||
keepExtensions: true, // 保留文件拓展名
|
||||
hashAlgorithm: 'md5', // 文件哈希算法
|
||||
// 使用 busboy 解析 multipart/form-data
|
||||
const busboy = Busboy({ headers: req.headers, preservePath: true });
|
||||
const fields: any = {};
|
||||
const files: any[] = [];
|
||||
const filePromises: Promise<void>[] = [];
|
||||
let bytesReceived = 0;
|
||||
let bytesExpected = parseInt(req.headers['content-length'] || '0');
|
||||
busboy.on('field', (fieldname, value) => {
|
||||
fields[fieldname] = value;
|
||||
});
|
||||
form.on('progress', (bytesReceived, bytesExpected) => {
|
||||
|
||||
busboy.on('file', (fieldname, fileStream, info) => {
|
||||
const { filename, encoding, mimeType } = info;
|
||||
const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`);
|
||||
const writeStream = createWriteStream(tempPath);
|
||||
const filePromise = new Promise<void>((resolve, reject) => {
|
||||
fileStream.on('data', (chunk) => {
|
||||
bytesReceived += chunk.length;
|
||||
if (bytesExpected > 0) {
|
||||
const progress = (bytesReceived / bytesExpected) * 100;
|
||||
const data = {
|
||||
progress: progress.toFixed(2),
|
||||
@@ -121,23 +132,51 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
||||
};
|
||||
console.log('progress-upload', data);
|
||||
writeEvents(req, data);
|
||||
}
|
||||
});
|
||||
// 解析上传的文件
|
||||
form.parse(req, async (err, fields, files) => {
|
||||
|
||||
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) {
|
||||
logger.error(`File write error: ${err.message}`);
|
||||
res.end(error(`File write error: ${err.message}`));
|
||||
return;
|
||||
}
|
||||
const clearFiles = () => {
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
uploadedFiles.forEach((file) => {
|
||||
files.forEach((file) => {
|
||||
if (file?.filepath && fs.existsSync(file.filepath)) {
|
||||
fs.unlinkSync(file.filepath);
|
||||
}
|
||||
});
|
||||
};
|
||||
if (err) {
|
||||
logger.error(`Upload error: ${err.message}`);
|
||||
res.end(error(`Upload error: ${err.message}`));
|
||||
clearFiles();
|
||||
|
||||
// 检查是否有文件上传
|
||||
if (files.length === 0) {
|
||||
res.end(error('files is required'));
|
||||
return;
|
||||
}
|
||||
|
||||
let { appKey, version, username, directory, description } = getKey(fields, ['appKey', 'version', 'username', 'directory', 'description']);
|
||||
let uid = tokenUser.id;
|
||||
if (username) {
|
||||
@@ -170,7 +209,7 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
||||
return;
|
||||
}
|
||||
// 逐个处理每个上传的文件
|
||||
const uploadedFiles = Array.isArray(files.file) ? files.file : [files.file];
|
||||
const uploadedFiles = files;
|
||||
logger.info(
|
||||
'upload files',
|
||||
uploadedFiles.map((item) => {
|
||||
@@ -244,4 +283,6 @@ router.post('/api/s1/resources/upload', async (req, res) => {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
pipeBusboy(req, res, busboy);
|
||||
});
|
||||
|
||||
@@ -2,7 +2,6 @@ import { router } from '@/app.ts';
|
||||
import http from 'http';
|
||||
import { useContextKey } from '@kevisual/context';
|
||||
import { checkAuth, error } from './middleware/auth.ts';
|
||||
import formidable from 'formidable';
|
||||
export { router, checkAuth, error };
|
||||
|
||||
/**
|
||||
@@ -68,7 +67,7 @@ export const deleteOldClients = () => {
|
||||
* @param parseKeys 需要解析的键
|
||||
* @returns 解析后的数据
|
||||
*/
|
||||
export const getKey = (fields: formidable.Fields<string>, parseKeys: string[]) => {
|
||||
export const getKey = (fields: Record<string, any>, parseKeys: string[]) => {
|
||||
let value: Record<string, any> = {};
|
||||
for (const key of parseKeys) {
|
||||
const v = fields[key];
|
||||
|
||||
6
src/utils/get-engine.ts
Normal file
6
src/utils/get-engine.ts
Normal 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;
|
||||
3
src/utils/sleep.ts
Normal file
3
src/utils/sleep.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const sleep = (ms: number = 1000) => {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
Reference in New Issue
Block a user