Compare commits

..

31 Commits

Author SHA1 Message Date
bd7525efb0 添加心跳机制以保持 WebSocket 连接,优化连接关闭时的资源清理 2026-02-05 04:58:28 +08:00
f616045625 优化用户代理逻辑,移除非管理员用户的敏感数据 2026-02-05 04:50:54 +08:00
a51d04341e 添加 @kevisual/api 依赖,更新 WebSocket 消息发送逻辑,支持上下文参数 2026-02-05 04:06:34 +08:00
7bbefd8a4a 添加自动检测最新版本功能,更新应用信息时支持检测参数 2026-02-05 01:07:44 +08:00
db5c5a89b3 clear 2026-02-04 19:48:02 +08:00
86d4c7f75b 移除不再支持的文件扩展名 '.mjs' 从文本内容类型列表中 2026-02-04 19:45:01 +08:00
cbc9b54284 update 2026-02-04 03:08:53 +08:00
b1d3ca241c 优化 token 处理逻辑,统一过期时间字段命名 2026-02-03 17:09:09 +08:00
158dd9e85c Refactor AI proxy error handling and remove deprecated upload and event routes
- Updated `getAiProxy` function to return a JSON response for missing objects when the user is the owner.
- Removed the `upload.ts`, `event.ts`, and related middleware files to streamline the codebase.
- Cleaned up `handle-request.ts` and `index.ts` by removing unused imports and routes.
- Deleted chunk upload handling and related utility functions to simplify resource management.
- Enhanced app manager list functionality to support app creation if not found.
2026-02-02 18:06:31 +08:00
82e3392b36 优化 token 处理逻辑,统一 createCookie 调用格式,返回 token 过期时间 2026-02-02 17:02:20 +08:00
a0f0f65d20 update 2026-02-01 19:26:44 +08:00
337abd2bc3 temp 2026-02-01 16:04:03 +08:00
32167faf67 重构 postProxy 函数,移动到新文件 ai-proxy-chunk/post-proxy.ts,优化文件上传处理逻辑 2026-02-01 14:33:26 +08:00
82c9b834e9 更新依赖项,添加 flowme 插入触发器和监听器;重构数据库连接管理;优化用户路由和 SSE 处理 2026-02-01 03:58:40 +08:00
7c61bd3ac5 添加 flowme 和 flowme-channel 路由,支持增删改查功能;更新 schema 以包含相关字段 2026-01-31 18:27:21 +08:00
eab14b9fe3 更新依赖项 commander 到版本 14.0.3;调整导入路径以使用 S3 模块;添加成功移动和备份用户的日志输出 2026-01-31 14:16:54 +08:00
296286bdaf Refactor code structure for improved readability and maintainability 2026-01-31 05:15:25 +08:00
6100e9833d Refactor storage integration from MinIO to S3
- Removed MinIO client and related imports from various modules.
- Introduced S3 client and OSS integration for object storage.
- Updated all references to MinIO methods with corresponding S3 methods.
- Added new flowme table schema to the database.
- Adjusted upload and download routes to utilize S3 for file operations.
- Removed obsolete MinIO-related files and routes.
- Ensured compatibility with existing application logic while transitioning to S3.
2026-01-31 05:12:56 +08:00
08023d6878 优化 postProxy 函数以支持从请求头获取文件大小;添加 renameProxy 函数以处理文件重命名;修复 isLocalhost 函数的格式问题 2026-01-30 17:26:35 +08:00
c3624a59de 优化 auth 中间件的日志输出;注释掉未使用的路由定义;更新 page-proxy-app 路由的描述和中间件 2026-01-26 20:53:51 +08:00
c4e5668b29 update 2026-01-26 18:52:41 +08:00
27f170ae2b 更新 package.json 版本号至 0.0.12;优化 isLocalhost 函数以处理空主机名;修复 handleRequest 中对 dns.hostName 的访问;在轻代码列表中添加 code 字段;更新示例路由描述 2026-01-26 04:24:00 +08:00
3464bd240b 更新数据库配置,添加默认时间戳,优化轻代码路由处理,新增示例代码文件 2026-01-26 03:46:32 +08:00
0e350b1bca clear old code 2026-01-26 03:02:55 +08:00
4bc58460b4 remove old apps 2026-01-26 03:01:29 +08:00
a9d725eb29 add convex 2026-01-26 02:44:17 +08:00
9ff4057166 update 2026-01-25 03:23:42 +08:00
bafd02452b update 2026-01-22 23:29:20 +08:00
3618d18915 fix: remove unused external dependency '@kevisual/router' 2026-01-18 20:57:22 +08:00
7b0b00feff test 2026-01-17 23:11:08 +08:00
d6da542989 temp 2026-01-17 21:17:50 +08:00
89 changed files with 4564 additions and 3304 deletions

View File

@@ -0,0 +1,150 @@
---
name: create-routes
description: 创建路由例子模板代码
---
# 创建路由例子模板代码
app是自定义@kevisual/router的一个APP
1. 一般来说修改path和对应的schema表就可以快速创建对应的增删改查接口。
2. 根据需要,每一个功能需要添加对应的描述
3. 根据需要对应schema表的字段进行修改代码
示例:
```ts
import { desc, eq, count, or, like, and } from 'drizzle-orm';
import { schema, app, db } from '@/app.ts'
app.route({
path: 'prompts',
key: 'list',
middleware: ['auth'],
description: '获取提示词列表',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const uid = tokenUser.id;
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query || {};
const offset = (page - 1) * pageSize;
const orderByField = sort === 'ASC' ? schema.prompts.updatedAt : desc(schema.prompts.updatedAt);
let whereCondition = eq(schema.prompts.uid, uid);
if (search) {
whereCondition = and(
eq(schema.prompts.uid, uid),
or(
like(schema.prompts.title, `%${search}%`),
like(schema.prompts.summary, `%${search}%`)
)
);
}
const [list, totalCount] = await Promise.all([
db.select()
.from(schema.prompts)
.where(whereCondition)
.limit(pageSize)
.offset(offset)
.orderBy(orderByField),
db.select({ count: count() })
.from(schema.prompts)
.where(whereCondition)
]);
ctx.body = {
list,
pagination: {
page,
current: page,
pageSize,
total: totalCount[0]?.count || 0,
},
};
return ctx;
}).addTo(app);
const promptUpdate = `创建或更新一个提示词, 参数定义:
title: 提示词标题, 必填
description: 描述, 选填
summary: 摘要, 选填
tags: 标签, 数组, 选填
link: 链接, 选填
data: 数据, 对象, 选填
parents: 父级ID数组, 选填
`;
app.route({
path: 'prompts',
key: 'update',
middleware: ['auth'],
description: promptUpdate,
}).define(async (ctx) => {
const { id, uid, updatedAt, ...rest } = ctx.query.data || {};
const tokenUser = ctx.state.tokenUser;
let prompt;
if (!id) {
prompt = await db.insert(schema.prompts).values({
title: rest.title,
description: rest.description,
...rest,
uid: tokenUser.id,
}).returning();
} else {
const existing = await db.select().from(schema.prompts).where(eq(schema.prompts.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的提示词');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限更新该提示词');
}
prompt = await db.update(schema.prompts).set({
...rest,
}).where(eq(schema.prompts.id, id)).returning();
}
ctx.body = prompt;
}).addTo(app);
app.route({
path: 'prompts',
key: 'delete',
middleware: ['auth'],
description: '删除提示词, 参数: id 提示词ID',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id 参数缺失');
}
const existing = await db.select().from(schema.prompts).where(eq(schema.prompts.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的提示词');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限删除该提示词');
}
await db.delete(schema.prompts).where(eq(schema.prompts.id, id));
ctx.body = { success: true };
}).addTo(app);
app.route({
path: 'prompts',
key: 'get',
middleware: ['auth'],
description: '获取单个提示词, 参数: id 提示词ID',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id 参数缺失');
}
const existing = await db.select().from(schema.prompts).where(eq(schema.prompts.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的提示词');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限查看该提示词');
}
ctx.body = existing[0];
}).addTo(app);
```

View File

@@ -3,4 +3,11 @@ code center
```
unzip -x app.config.json5
```
```
"@kevisual/oss": "file:/home/ubuntu/kevisual/dev3/kevisual-oss",
```
```
TODO: /home/ubuntu/kevisual/code-center/src/routes/file/module/get-minio-list.ts
```

49
convex/_generated/api.d.ts vendored Normal file
View File

@@ -0,0 +1,49 @@
/* eslint-disable */
/**
* Generated `api` utility.
*
* THIS CODE IS AUTOMATICALLY GENERATED.
*
* To regenerate, run `npx convex dev`.
* @module
*/
import type * as http from "../http.js";
import type {
ApiFromModules,
FilterApi,
FunctionReference,
} from "convex/server";
declare const fullApi: ApiFromModules<{
http: typeof http;
}>;
/**
* A utility for referencing Convex functions in your app's public API.
*
* Usage:
* ```js
* const myFunctionReference = api.myModule.myFunction;
* ```
*/
export declare const api: FilterApi<
typeof fullApi,
FunctionReference<any, "public">
>;
/**
* A utility for referencing Convex functions in your app's internal API.
*
* Usage:
* ```js
* const myFunctionReference = internal.myModule.myFunction;
* ```
*/
export declare const internal: FilterApi<
typeof fullApi,
FunctionReference<any, "internal">
>;
export declare const components: {};

23
convex/_generated/api.js Normal file
View File

@@ -0,0 +1,23 @@
/* eslint-disable */
/**
* Generated `api` utility.
*
* THIS CODE IS AUTOMATICALLY GENERATED.
*
* To regenerate, run `npx convex dev`.
* @module
*/
import { anyApi, componentsGeneric } from "convex/server";
/**
* A utility for referencing Convex functions in your app's API.
*
* Usage:
* ```js
* const myFunctionReference = api.myModule.myFunction;
* ```
*/
export const api = anyApi;
export const internal = anyApi;
export const components = componentsGeneric();

60
convex/_generated/dataModel.d.ts vendored Normal file
View File

@@ -0,0 +1,60 @@
/* eslint-disable */
/**
* Generated data model types.
*
* THIS CODE IS AUTOMATICALLY GENERATED.
*
* To regenerate, run `npx convex dev`.
* @module
*/
import type {
DataModelFromSchemaDefinition,
DocumentByName,
TableNamesInDataModel,
SystemTableNames,
} from "convex/server";
import type { GenericId } from "convex/values";
import schema from "../schema.js";
/**
* The names of all of your Convex tables.
*/
export type TableNames = TableNamesInDataModel<DataModel>;
/**
* The type of a document stored in Convex.
*
* @typeParam TableName - A string literal type of the table name (like "users").
*/
export type Doc<TableName extends TableNames> = DocumentByName<
DataModel,
TableName
>;
/**
* An identifier for a document in Convex.
*
* Convex documents are uniquely identified by their `Id`, which is accessible
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
*
* Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
*
* IDs are just strings at runtime, but this type can be used to distinguish them from other
* strings when type checking.
*
* @typeParam TableName - A string literal type of the table name (like "users").
*/
export type Id<TableName extends TableNames | SystemTableNames> =
GenericId<TableName>;
/**
* A type describing your Convex data model.
*
* This type includes information about what tables you have, the type of
* documents stored in those tables, and the indexes defined on them.
*
* This type is used to parameterize methods like `queryGeneric` and
* `mutationGeneric` to make them type-safe.
*/
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;

143
convex/_generated/server.d.ts vendored Normal file
View File

@@ -0,0 +1,143 @@
/* eslint-disable */
/**
* Generated utilities for implementing server-side Convex query and mutation functions.
*
* THIS CODE IS AUTOMATICALLY GENERATED.
*
* To regenerate, run `npx convex dev`.
* @module
*/
import {
ActionBuilder,
HttpActionBuilder,
MutationBuilder,
QueryBuilder,
GenericActionCtx,
GenericMutationCtx,
GenericQueryCtx,
GenericDatabaseReader,
GenericDatabaseWriter,
} from "convex/server";
import type { DataModel } from "./dataModel.js";
/**
* Define a query in this Convex app's public API.
*
* This function will be allowed to read your Convex database and will be accessible from the client.
*
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
*/
export declare const query: QueryBuilder<DataModel, "public">;
/**
* Define a query that is only accessible from other Convex functions (but not from the client).
*
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
*
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
*/
export declare const internalQuery: QueryBuilder<DataModel, "internal">;
/**
* Define a mutation in this Convex app's public API.
*
* This function will be allowed to modify your Convex database and will be accessible from the client.
*
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
*/
export declare const mutation: MutationBuilder<DataModel, "public">;
/**
* Define a mutation that is only accessible from other Convex functions (but not from the client).
*
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
*
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
*/
export declare const internalMutation: MutationBuilder<DataModel, "internal">;
/**
* Define an action in this Convex app's public API.
*
* An action is a function which can execute any JavaScript code, including non-deterministic
* code and code with side-effects, like calling third-party services.
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
*
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
*/
export declare const action: ActionBuilder<DataModel, "public">;
/**
* Define an action that is only accessible from other Convex functions (but not from the client).
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
*/
export declare const internalAction: ActionBuilder<DataModel, "internal">;
/**
* Define an HTTP action.
*
* The wrapped function will be used to respond to HTTP requests received
* by a Convex deployment if the requests matches the path and method where
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument
* and a Fetch API `Request` object as its second.
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
*/
export declare const httpAction: HttpActionBuilder;
/**
* A set of services for use within Convex query functions.
*
* The query context is passed as the first argument to any Convex query
* function run on the server.
*
* This differs from the {@link MutationCtx} because all of the services are
* read-only.
*/
export type QueryCtx = GenericQueryCtx<DataModel>;
/**
* A set of services for use within Convex mutation functions.
*
* The mutation context is passed as the first argument to any Convex mutation
* function run on the server.
*/
export type MutationCtx = GenericMutationCtx<DataModel>;
/**
* A set of services for use within Convex action functions.
*
* The action context is passed as the first argument to any Convex action
* function run on the server.
*/
export type ActionCtx = GenericActionCtx<DataModel>;
/**
* An interface to read from the database within Convex query functions.
*
* The two entry points are {@link DatabaseReader.get}, which fetches a single
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
* building a query.
*/
export type DatabaseReader = GenericDatabaseReader<DataModel>;
/**
* An interface to read from and write to the database within Convex mutation
* functions.
*
* Convex guarantees that all writes within a single mutation are
* executed atomically, so you never have to worry about partial writes leaving
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
* for the guarantees Convex provides your functions.
*/
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;

View File

@@ -0,0 +1,93 @@
/* eslint-disable */
/**
* Generated utilities for implementing server-side Convex query and mutation functions.
*
* THIS CODE IS AUTOMATICALLY GENERATED.
*
* To regenerate, run `npx convex dev`.
* @module
*/
import {
actionGeneric,
httpActionGeneric,
queryGeneric,
mutationGeneric,
internalActionGeneric,
internalMutationGeneric,
internalQueryGeneric,
} from "convex/server";
/**
* Define a query in this Convex app's public API.
*
* This function will be allowed to read your Convex database and will be accessible from the client.
*
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
*/
export const query = queryGeneric;
/**
* Define a query that is only accessible from other Convex functions (but not from the client).
*
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
*
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
*/
export const internalQuery = internalQueryGeneric;
/**
* Define a mutation in this Convex app's public API.
*
* This function will be allowed to modify your Convex database and will be accessible from the client.
*
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
*/
export const mutation = mutationGeneric;
/**
* Define a mutation that is only accessible from other Convex functions (but not from the client).
*
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
*
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
*/
export const internalMutation = internalMutationGeneric;
/**
* Define an action in this Convex app's public API.
*
* An action is a function which can execute any JavaScript code, including non-deterministic
* code and code with side-effects, like calling third-party services.
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
*
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
*/
export const action = actionGeneric;
/**
* Define an action that is only accessible from other Convex functions (but not from the client).
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
*/
export const internalAction = internalActionGeneric;
/**
* Define an HTTP action.
*
* The wrapped function will be used to respond to HTTP requests received
* by a Convex deployment if the requests matches the path and method where
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument
* and a Fetch API `Request` object as its second.
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
*/
export const httpAction = httpActionGeneric;

13
convex/auth.config.ts Normal file
View File

@@ -0,0 +1,13 @@
import { AuthConfig } from "convex/server";
export default {
providers: [
{
type: "customJwt",
applicationID: "convex-app",
issuer: "https://convex.kevisual.cn",
jwks: "https://api-convex.kevisual.cn/root/convex/jwks.json",
algorithm: "RS256",
},
],
};

7
convex/convex.config.ts Normal file
View File

@@ -0,0 +1,7 @@
import { defineApp } from "convex/server";
const app = defineApp();
// import myComponent from "../component/convex.config.ts";
// app.use(myComponent);
export default app;

39
convex/http.ts Normal file
View File

@@ -0,0 +1,39 @@
import { httpRouter } from "convex/server";
import { httpAction } from "./_generated/server";
const http = httpRouter();
http.route({
path: "/auth",
method: "POST",
handler: httpAction(async (ctx, request) => {
// 处理请求并返回响应
return new Response(JSON.stringify({ message: "Hello from custom endpoint!" }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}),
})
// https://api-convex.kevisual.cn/root/convex/jwks.json
http.route({
path: '/root/convex/jwks.json',
method: 'GET',
handler: httpAction(async (ctx, request) => {
// 返回 JWKS 数据
const jwks = {
"keys": [
{
"kty": "RSA",
"n": "km4cjJJOMFkl2G5qWMuFmWwF7rmeqRYzYdR8SddKeeMW0e9yIf5pv2Mfwv0aMJUpb-_j3j9M7whx_SEGc_2jx1vxCu1AlYURhnnLTWdsR-ZRPr2LK9UstMrgpWV425R2RliqXTDTYlSxUUlD9nPue_tqbfwN2aM9MCarm67xK_ZCcKRlW9o9L2-9UMfzRA7uiy4VQtOemP0PTXp-E9RxNiMPOQXIRls9wTW_EkDT3dGy7JCZhj7_qib3T8k9m84SwU7wI2R_3IH4DcHSMAn1BRRMXZ1_wPhbP39laNtdJgbDjGCqUccGhLUaoo2WGkZ52eb7NPqamp0K1Dh2jwTIJQ",
"e": "AQAB",
"kid": "kid-key-1"
}
]
};
return new Response(JSON.stringify(jwks), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}),
})
export default http;

30
convex/schema.ts Normal file
View File

@@ -0,0 +1,30 @@
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export default defineSchema({
// Other tables here...
github_starred: defineTable({
author: v.string(),
auto: v.string(),
description: v.string(),
last_updated: v.string(),
link: v.string(),
repo_id: v.float64(),
stars: v.float64(),
summary: v.string(),
title: v.string(),
}),
users: defineTable({
userId: v.string(), // 外部系统的用户 ID
name: v.string(),
createdAt: v.string(),
lastLoginAt: v.optional(v.string()),
}).index("userId", ["userId"]),
sessions: defineTable({
userId: v.id("users"),
createdAt: v.string(),
expiresAt: v.optional(v.string()),
token: v.optional(v.string()),
}).index("token", ["token"]),
});

View File

@@ -1,6 +1,8 @@
import { useConfig } from '@kevisual/use-config/context';
import type { Config } from 'drizzle-kit';
import 'dotenv/config';
const url = process.env.DATABASE_URL!;
const config = useConfig();
const url = config.DATABASE_URL!;
console.log('Drizzle config using database url:', url);
export default {
schema: './src/db/schema.ts',

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/code-center",
"version": "0.0.11",
"version": "0.0.12",
"description": "code center",
"type": "module",
"main": "index.js",
@@ -23,12 +23,12 @@
"build": "NODE_ENV=production bun bun.config.mjs",
"postbuild": "ev pack",
"deploy": "rsync -avz --delete ./pack-dist/ light:/root/kevisual/assistant-app/apps/root/code-center",
"deploy:envision": "rsync -avz --delete ./pack-dist/ envision:~/kevisual/assistant-app/apps/root/code-center",
"deploy:kevisual": "rsync -avz --delete ./pack-dist/ kevisual:~/kevisual/assistant-app/apps/root/code-center",
"clean": "rm -rf dist",
"reload": "ssh light pm2 restart root/code-center",
"reload:envision": "ssh envision pm2 restart root/code-center",
"reload:kevisual": "ssh kevisual pm2 restart root/code-center",
"pub:me": "npm run build && npm run deploy && npm run reload",
"pub:envision": "npm run build && npm run deploy:envision && npm run reload:envision",
"pub:kevisual": "npm run build && npm run deploy:kevisual && npm run reload:kevisual",
"start": "pm2 start dist/app.js --name code-center",
"client:start": "pm2 start apps/code-center/dist/app.js --name code-center",
"ssl": "ssh -L 5432:localhost:5432 light",
@@ -37,6 +37,7 @@
"studio": "npx drizzle-kit studio",
"drizzle:migrate": "npx drizzle-kit migrate",
"drizzle:push": "npx drizzle-kit push",
"convex": "bunx convex dev",
"pub": "envision pack -p -u -c"
},
"keywords": [],
@@ -46,21 +47,23 @@
],
"license": "UNLICENSED",
"dependencies": {
"@kevisual/ai": "^0.0.20",
"@kevisual/query": "^0.0.35",
"@kevisual/ai": "^0.0.24",
"@kevisual/auth": "^2.0.3",
"@kevisual/js-filter": "^0.0.5",
"@kevisual/query": "^0.0.39",
"@types/busboy": "^1.5.4",
"@types/send": "^1.2.1",
"@types/ws": "^8.18.1",
"bullmq": "^5.66.5",
"bullmq": "^5.67.2",
"busboy": "^1.6.0",
"commander": "^14.0.2",
"commander": "^14.0.3",
"drizzle-kit": "^0.31.8",
"drizzle-orm": "^0.45.1",
"drizzle-zod": "^0.8.3",
"eventemitter3": "^5.0.1",
"ioredis": "^5.9.1",
"eventemitter3": "^5.0.4",
"ioredis": "^5.9.2",
"minio": "^8.0.6",
"pg": "^8.17.1",
"pg": "^8.18.0",
"pm2": "^6.0.14",
"send": "^1.2.1",
"sequelize": "^6.37.7",
@@ -69,42 +72,44 @@
"zod-to-json-schema": "^3.25.1"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.981.0",
"@kevisual/api": "^0.0.44",
"@kevisual/code-center-module": "0.0.24",
"@kevisual/context": "^0.0.4",
"@kevisual/file-listener": "^0.0.2",
"@kevisual/local-app-manager": "0.1.32",
"@kevisual/logger": "^0.0.4",
"@kevisual/oss": "0.0.16",
"@kevisual/permission": "^0.0.3",
"@kevisual/router": "0.0.53",
"@kevisual/types": "^0.0.11",
"@kevisual/use-config": "^1.0.28",
"@kevisual/oss": "0.0.19",
"@kevisual/permission": "^0.0.4",
"@kevisual/router": "0.0.70",
"@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.30",
"@types/archiver": "^7.0.0",
"@types/bun": "^1.3.6",
"@types/bun": "^1.3.8",
"@types/crypto-js": "^4.2.2",
"@types/jsonwebtoken": "^9.0.10",
"@types/lodash-es": "^4.17.12",
"@types/node": "^25.0.8",
"@types/node": "^25.2.0",
"@types/pg": "^8.16.0",
"@types/semver": "^7.7.1",
"@types/xml2js": "^0.4.14",
"archiver": "^7.0.1",
"better-sqlite3": "^12.6.0",
"better-sqlite3": "^12.6.2",
"convex": "^1.31.7",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.19",
"dotenv": "^17.2.3",
"es-toolkit": "^1.43.0",
"ioredis": "^5.9.1",
"es-toolkit": "^1.44.0",
"ioredis": "^5.9.2",
"jsonwebtoken": "^9.0.3",
"lodash-es": "^4.17.22",
"minio": "^8.0.6",
"nanoid": "^5.1.6",
"nodemon": "^3.1.11",
"p-queue": "^9.1.0",
"pg": "^8.17.1",
"pg": "^8.18.0",
"pm2": "^6.0.14",
"semver": "^7.7.3",
"sequelize": "^6.37.7",
"zod": "^4.3.5"
"zod": "^4.3.6"
},
"resolutions": {
"inflight": "latest",
@@ -116,5 +121,5 @@
"better-sqlite3"
]
},
"packageManager": "pnpm@10.28.0"
"packageManager": "pnpm@10.28.2"
}

2244
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,14 +1,13 @@
import { App } from '@kevisual/router';
import * as redisLib from './modules/redis.ts';
import * as minioLib from './modules/minio.ts';
import * as sequelizeLib from './modules/sequelize.ts';
import { useContextKey } from '@kevisual/context';
import { SimpleRouter } from '@kevisual/router/simple';
import { OssBase } from '@kevisual/oss/services';
import { s3Client, oss as s3Oss } from './modules/s3.ts';
import { BailianProvider } from '@kevisual/ai';
import * as schema from './db/schema.ts';
import { drizzle } from 'drizzle-orm/node-postgres';
import { config } from './modules/config.ts'
import { db } from './modules/db.ts'
export const router = useContextKey('router', () => new SimpleRouter());
export const runtime = useContextKey('runtime', () => {
return {
@@ -18,21 +17,13 @@ export const runtime = useContextKey('runtime', () => {
});
export const oss = useContextKey(
'oss',
() =>
new OssBase({
client: minioLib.minioClient,
bucketName: minioLib.bucketName,
prefix: '',
}),
() => s3Oss,
);
export { s3Client }
export const redis = useContextKey('redis', () => redisLib.redis);
export const subscriber = useContextKey('subscriber', () => redisLib.subscriber);
export const minioClient = useContextKey('minioClient', () => minioLib.minioClient);
export const sequelize = useContextKey('sequelize', () => sequelizeLib.sequelize);
export const db = useContextKey('db', () => {
const db = drizzle(config.DATABASE_URL || '');
return db;
})
export { db };
const init = () => {
return new App({
serverOptions: {

View File

@@ -54,7 +54,6 @@ export class UserSecret extends Model {
}
const secretToken = await oauth.verifyToken(token);
if (secretToken) {
console.log('verifyToken: verified as normal token');
return secretToken;
}
console.log('verifyToken: try to verify as secret key');

View File

@@ -62,7 +62,13 @@ export class User extends Model {
oauthUser.orgId = id;
}
const token = await oauth.generateToken(oauthUser, { type: loginType, hasRefreshToken: true, ...expand });
return { accessToken: token.accessToken, refreshToken: token.refreshToken, token: token.accessToken };
return {
accessToken: token.accessToken,
refreshToken: token.refreshToken,
token: token.accessToken,
refreshTokenExpiresIn: token.refreshTokenExpiresIn,
accessTokenExpiresIn: token.accessTokenExpiresIn,
};
}
/**
* 验证token
@@ -183,7 +189,7 @@ export class User extends Model {
avatar: this.avatar,
orgs,
};
if(this.data?.canChangeUsername) {
if (this.data?.canChangeUsername) {
info.canChangeUsername = this.data.canChangeUsername;
}
const tokenUser = this.tokenUser;
@@ -232,6 +238,17 @@ export class User extends Model {
async expireOrgs() {
await redis.del(`user:${this.id}:orgs`);
}
static async getUserNameById(id: string) {
const redisName = await redis.get(`user:id:${id}:name`);
if (redisName) {
return redisName;
}
const user = await User.findByPk(id);
if (user?.username) {
await redis.set(`user:id:${id}:name`, user.username, 'EX', 60 * 60); // 1 hour
}
return user?.username;
}
}
export type SyncOpts = {
alter?: boolean;

View File

@@ -70,9 +70,16 @@ interface Store<T> {
expire: (key: string, ttl?: number) => Promise<void>;
delObject: (value?: T) => Promise<void>;
keys: (key?: string) => Promise<string[]>;
setToken: (value: { accessToken: string; refreshToken: string; value?: T }, opts?: StoreSetOpts) => Promise<void>;
setToken: (value: { accessToken: string; refreshToken: string; value?: T }, opts?: StoreSetOpts) => Promise<TokenData>;
delKeys: (keys: string[]) => Promise<number>;
}
type TokenData = {
accessToken: string;
accessTokenExpiresIn?: number;
refreshToken?: string;
refreshTokenExpiresIn?: number;
}
export class RedisTokenStore implements Store<OauthUser> {
redis: Redis;
private prefix: string = 'oauth:';
@@ -131,7 +138,7 @@ export class RedisTokenStore implements Store<OauthUser> {
await this.del(userPrefix + ':token:' + accessToken);
}
}
async setToken(data: { accessToken: string; refreshToken: string; value?: OauthUser }, opts?: StoreSetOpts) {
async setToken(data: { accessToken: string; refreshToken: string; value?: OauthUser }, opts?: StoreSetOpts): Promise<TokenData> {
const { accessToken, refreshToken, value } = data;
let userPrefix = 'user:' + value?.id;
if (value?.orgId) {
@@ -163,14 +170,20 @@ export class RedisTokenStore implements Store<OauthUser> {
await this.set(accessToken, JSON.stringify(value), expire);
await this.set(userPrefix + ':token:' + accessToken, accessToken, expire);
let refreshTokenExpiresIn = Math.min(expire * 7, 60 * 60 * 24 * 30, 60 * 60 * 24 * 365); // 最大为一年
if (refreshToken) {
let refreshTokenExpire = Math.min(expire * 7, 60 * 60 * 24 * 30, 60 * 60 * 24 * 365); // 最大为一年
// 小于7天, 则设置为7天
if (refreshTokenExpire < 60 * 60 * 24 * 7) {
refreshTokenExpire = 60 * 60 * 24 * 7;
if (refreshTokenExpiresIn < 60 * 60 * 24 * 7) {
refreshTokenExpiresIn = 60 * 60 * 24 * 7;
}
await this.set(refreshToken, JSON.stringify(value), refreshTokenExpire);
await this.set(userPrefix + ':refreshToken:' + refreshToken, refreshToken, refreshTokenExpire);
await this.set(refreshToken, JSON.stringify(value), refreshTokenExpiresIn);
await this.set(userPrefix + ':refreshToken:' + refreshToken, refreshToken, refreshTokenExpiresIn);
}
return {
accessToken,
accessTokenExpiresIn: expire,
refreshToken,
refreshTokenExpiresIn: refreshTokenExpiresIn,
}
}
async delKeys(keys: string[]) {
@@ -206,10 +219,7 @@ export class OAuth<T extends OauthUser> {
async generateToken(
user: T,
expandOpts?: StoreSetOpts,
): Promise<{
accessToken: string;
refreshToken?: string;
}> {
): Promise<TokenData> {
// 拥有refreshToken 为 true 时accessToken 为 st_ 开头refreshToken 为 rk_开头
// 意思是secretToken 和 secretKey的缩写
const accessToken = expandOpts?.hasRefreshToken ? 'st_' + randomId32() : 'sk_' + randomId64();
@@ -227,9 +237,9 @@ export class OAuth<T extends OauthUser> {
user.oauthExpand.refreshToken = refreshToken;
}
}
await this.store.setToken({ accessToken, refreshToken, value: user }, expandOpts);
const tokenData = await this.store.setToken({ accessToken, refreshToken, value: user }, expandOpts);
return { accessToken, refreshToken };
return tokenData;
}
async saveSecretKey(oauthUser: T, secretKey: string, opts?: StoreSetOpts) {
// 生成一个secretKey

View File

@@ -10,8 +10,8 @@ export const testPromptTools = pgTable("TestPromptTools", {
args: jsonb().notNull(),
process: jsonb().notNull(),
type: varchar({ length: 255 }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
});
export const aiAgent = pgTable("ai_agent", {
@@ -22,8 +22,8 @@ export const aiAgent = pgTable("ai_agent", {
temperature: doublePrecision(),
cache: varchar({ length: 255 }),
cacheName: varchar({ length: 255 }),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
model: varchar({ length: 255 }).notNull(),
data: json().default({}),
status: varchar({ length: 255 }).default('open'),
@@ -43,8 +43,8 @@ export const appsTrades = pgTable("apps_trades", {
type: varchar({ length: 255 }).default('alipay').notNull(),
data: jsonb().default({ "list": [] }),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
}, (table) => [
unique("apps_trades_out_trade_no_key").on(table.outTradeNo),
@@ -54,8 +54,8 @@ export const cfOrgs = pgTable("cf_orgs", {
id: uuid().primaryKey().notNull(),
username: varchar({ length: 255 }).notNull(),
users: jsonb().default([]),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
description: varchar({ length: 255 }),
}, (table) => [
@@ -70,8 +70,8 @@ export const cfRouterCode = pgTable("cf_router_code", {
project: varchar({ length: 255 }).default('default'),
code: text().default(''),
type: enumCfRouterCodeType().default('route'),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
middleware: varchar({ length: 255 }).array().default(["RRAY[]::character varying[])::character varying(25"]),
next: varchar({ length: 255 }).default(''),
exec: text().default(''),
@@ -86,8 +86,8 @@ export const cfUser = pgTable("cf_user", {
password: varchar({ length: 255 }),
salt: varchar({ length: 255 }),
needChangePassword: boolean().default(false),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
description: text(),
data: jsonb().default({}),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
@@ -111,8 +111,8 @@ export const cfUserSecrets = pgTable("cf_user_secrets", {
userId: uuid(),
data: jsonb().default({}),
orgId: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
});
export const chatHistories = pgTable("chat_histories", {
@@ -123,8 +123,8 @@ export const chatHistories = pgTable("chat_histories", {
root: boolean().default(false),
show: boolean().default(true),
uid: varchar({ length: 255 }),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
role: varchar({ length: 255 }).default('user'),
});
@@ -135,8 +135,8 @@ export const chatPrompts = pgTable("chat_prompts", {
data: json(),
key: varchar({ length: 255 }).default('').notNull(),
uid: varchar({ length: 255 }),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
});
@@ -146,8 +146,8 @@ export const chatSessions = pgTable("chat_sessions", {
chatPromptId: uuid(),
type: varchar({ length: 255 }).default('production'),
uid: varchar({ length: 255 }),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
title: varchar({ length: 255 }).default(''),
key: varchar({ length: 255 }),
});
@@ -159,8 +159,8 @@ export const fileSync = pgTable("file_sync", {
stat: jsonb().default({}),
data: jsonb().default({}),
checkedAt: timestamp({ withTimezone: true, mode: 'string' }),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
}, (table) => [
index("file_sync_name_idx").using("btree", table.name.asc().nullsLast()),
]);
@@ -177,8 +177,8 @@ export const kvAiChatHistory = pgTable("kv_ai_chat_history", {
completionTokens: integer("completion_tokens").default(0),
data: jsonb().default({}),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
version: integer().default(0),
type: varchar({ length: 255 }).default('keep').notNull(),
});
@@ -189,8 +189,8 @@ export const kvApp = pgTable("kv_app", {
version: varchar({ length: 255 }).default(''),
key: varchar({ length: 255 }),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
title: varchar({ length: 255 }).default(''),
description: varchar({ length: 255 }).default(''),
@@ -208,8 +208,8 @@ export const kvAppDomain = pgTable("kv_app_domain", {
domain: varchar({ length: 255 }).notNull(),
appId: varchar({ length: 255 }),
uid: varchar({ length: 255 }),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
data: jsonb(),
status: varchar({ length: 255 }).default('running').notNull(),
@@ -222,8 +222,8 @@ export const kvAppList = pgTable("kv_app_list", {
data: json().default({}),
version: varchar({ length: 255 }).default(''),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
key: varchar({ length: 255 }),
status: varchar({ length: 255 }).default('running'),
@@ -237,25 +237,23 @@ export const kvConfig = pgTable("kv_config", {
tags: jsonb().default([]),
data: jsonb().default({}),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
hash: text().default(''),
});
export const kvContainer = pgTable("kv_container", {
id: uuid().primaryKey().notNull(),
export const kvContainer = pgTable("kv_light_code", {
id: uuid().primaryKey().notNull().defaultRandom(),
title: text().default(''),
description: text().default(''),
type: varchar({ length: 255 }).default('render-js'),
type: text().default('render-js'),
code: text().default(''),
data: json().default({}),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
data: jsonb().default({}),
createdAt: timestamp('createdAt').notNull().defaultNow(),
updatedAt: timestamp('updatedAt').notNull().defaultNow(),
uid: uuid(),
publish: json().default({}),
tags: json().default([]),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
tags: jsonb().default([]),
hash: text().default(''),
});
@@ -264,8 +262,8 @@ export const kvGithub = pgTable("kv_github", {
title: varchar({ length: 255 }).default(''),
githubToken: varchar({ length: 255 }).default(''),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
});
@@ -278,8 +276,8 @@ export const kvPackages = pgTable("kv_packages", {
publish: jsonb().default({}),
expand: jsonb().default({}),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
});
@@ -290,8 +288,8 @@ export const kvPage = pgTable("kv_page", {
type: varchar({ length: 255 }).default(''),
data: json().default({}),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
publish: json().default({}),
});
@@ -305,8 +303,8 @@ export const kvResource = pgTable("kv_resource", {
version: varchar({ length: 255 }).default('0.0.0'),
data: json().default({}),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
});
@@ -318,8 +316,8 @@ export const kvVip = pgTable("kv_vip", {
startDate: timestamp({ withTimezone: true, mode: 'string' }),
endDate: timestamp({ withTimezone: true, mode: 'string' }),
data: jsonb().default({}),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
title: text().default('').notNull(),
description: text().default('').notNull(),
@@ -336,8 +334,8 @@ export const microAppsUpload = pgTable("micro_apps_upload", {
share: boolean().default(false),
uname: varchar({ length: 255 }).default(''),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
});
export const microMark = pgTable("micro_mark", {
@@ -348,8 +346,8 @@ export const microMark = pgTable("micro_mark", {
data: jsonb().default({}),
uname: varchar({ length: 255 }).default(''),
uid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
cover: text().default(''),
thumbnail: text().default(''),
link: text().default(''),
@@ -381,8 +379,8 @@ export const workShareMark = pgTable("work_share_mark", {
markedAt: timestamp({ withTimezone: true, mode: 'string' }),
uid: uuid(),
puid: uuid(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull(),
createdAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
updatedAt: timestamp({ withTimezone: true, mode: 'string' }).notNull().defaultNow(),
deletedAt: timestamp({ withTimezone: true, mode: 'string' }),
});
@@ -494,4 +492,47 @@ export const queryViews = pgTable("query_views", {
}, (table) => [
index('query_views_uid_idx').using('btree', table.uid.asc().nullsLast()),
index('query_title_idx').using('btree', table.title.asc().nullsLast()),
]);
export const flowme = pgTable("flowme", {
id: uuid().primaryKey().notNull().defaultRandom(),
uid: uuid(),
title: text('title').default(''),
description: text('description').default(''),
tags: jsonb().default([]),
link: text('link').default(''),
data: jsonb().default({}),
channelId: uuid().references(() => flowmeChannels.id, { onDelete: 'set null' }),
type: text('type').default(''),
source: text('source').default(''),
importance: integer('importance').default(0), // 重要性等级
isArchived: boolean('isArchived').default(false), // 是否归档
createdAt: timestamp('createdAt').notNull().defaultNow(),
updatedAt: timestamp('updatedAt').notNull().defaultNow(),
}, (table) => [
index('flowme_uid_idx').using('btree', table.uid.asc().nullsLast()),
index('flowme_title_idx').using('btree', table.title.asc().nullsLast()),
index('flowme_channel_id_idx').using('btree', table.channelId.asc().nullsLast()),
]);
export const flowmeChannels = pgTable("flowme_channels", {
id: uuid().primaryKey().notNull().defaultRandom(),
uid: uuid(),
title: text('title').default(''),
key: text('key').default(''),
description: text('description').default(''),
tags: jsonb().default([]),
link: text('link').default(''),
data: jsonb().default({}),
color: text('color').default('#007bff'),
createdAt: timestamp('createdAt').notNull().defaultNow(),
updatedAt: timestamp('updatedAt').notNull().defaultNow(),
}, (table) => [
index('flowme_channels_uid_idx').using('btree', table.uid.asc().nullsLast()),
index('flowme_channels_key_idx').using('btree', table.key.asc().nullsLast()),
index('flowme_channels_title_idx').using('btree', table.title.asc().nullsLast()),
]);

View File

@@ -5,14 +5,9 @@ import { logger } from './logger.ts';
export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
};
export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
export const getTokenFromRequest = (req: http.IncomingMessage) => {
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
const url = new URL(req.url || '', 'http://localhost');
const resNoPermission = () => {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
};
if (!token) {
token = url.searchParams.get('token') || '';
}
@@ -20,23 +15,32 @@ export const checkAuth = async (req: http.IncomingMessage, res: http.ServerRespo
const parsedCookies = cookie.parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
if (!token) {
return resNoPermission();
}
if (token) {
token = token.replace('Bearer ', '');
}
let tokenUser;
try {
tokenUser = await User.verifyToken(token);
} catch (e) {
console.log('checkAuth error', e);
res.statusCode = 401;
res.end(error('Invalid token'));
return { tokenUser: null, token: null };
}
return { tokenUser, token };
};
return token;
}
// export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
// const token = getTokenFromRequest(req);
// const resNoPermission = () => {
// res.statusCode = 401;
// res.end(error('Invalid authorization'));
// return { tokenUser: null, token: null };
// };
// if (!token) {
// return resNoPermission();
// }
// let tokenUser;
// try {
// tokenUser = await User.verifyToken(token);
// } catch (e) {
// console.log('checkAuth error', e);
// res.statusCode = 401;
// res.end(error('Invalid token'));
// return { tokenUser: null, token: null };
// }
// return { tokenUser, token };
// };
export const getLoginUserByToken = async (token: string) => {
if (token) {
@@ -54,19 +58,7 @@ export const getLoginUserByToken = async (token: string) => {
}
}
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');
if (!token) {
token = url.searchParams.get('token') || '';
}
if (!token) {
const parsedCookies = cookie.parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
if (token) {
token = token.replace('Bearer ', '');
}
let token = getTokenFromRequest(req);
if (!token) {
return null;
}

View File

@@ -1,6 +1,6 @@
import { useConfig } from '@kevisual/use-config';
import { useFileStore } from '@kevisual/use-config';
import { minioResources } from './minio.ts';
import { minioResources } from './s3.ts';
export const config = useConfig() as any;
export const port = config.PORT ? Number(config.PORT) : 4005;

8
src/modules/db.ts Normal file
View File

@@ -0,0 +1,8 @@
import { useContextKey } from '@kevisual/context';
import { drizzle } from 'drizzle-orm/node-postgres';
import { config } from './config.ts'
export const db = useContextKey('db', () => {
const db = drizzle(config.DATABASE_URL || '');
return db;
})

View File

@@ -11,7 +11,6 @@ export const getTextContentType = (filePath: string, isFilePath = false) => {
'.env',
'.example',
'.log',
'.mjs',
'.map',
'.json5',
'.pem',

View File

@@ -0,0 +1,239 @@
import { IncomingMessage, ServerResponse } from 'http';
import busboy from 'busboy';
import fs from 'fs';
import path from 'path';
import { createWriteStream } from 'fs';
import { parseSearchValue } from '@kevisual/router/src/server/parse-body.ts';
import { pipeBusboy } from '../../pipe-busboy.ts';
import { ProxyOptions, getMetadata, getObjectName } from '../ai-proxy.ts';
import { fileIsExist, useFileStore } from '@kevisual/use-config';
import { getContentType } from '../../get-content-type.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const _u = new URL(req.url, 'http://localhost');
const pathname = _u.pathname;
const oss = opts.oss;
const params = _u.searchParams;
const force = !!params.get('force');
const hash = params.get('hash');
const _fileSize: string = params.get('size');
let fileSize: number | undefined = undefined;
if (_fileSize) {
fileSize = parseInt(_fileSize, 10);
}
console.log('postProxy', { hash, force, fileSize });
let meta = parseSearchValue(params.get('meta'), { decode: true });
if (!hash && !force) {
return opts?.createNotFoundPage?.('no hash');
}
const { objectName, isOwner } = await getObjectName(req);
if (!isOwner) {
return opts?.createNotFoundPage?.('no permission');
}
const end = (data: any, message?: string, code = 200) => {
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
let statMeta: any = {};
if (!force) {
const check = await oss.checkObjectHash(objectName, hash, meta);
statMeta = check?.metaData || {};
let isNewMeta = false;
if (check.success && JSON.stringify(meta) !== '{}' && !check.equalMeta) {
meta = { ...statMeta, ...getMetadata(pathname), ...meta };
isNewMeta = true;
await oss.replaceObject(objectName, { ...meta });
}
if (check.success) {
return end({ success: true, hash, meta, isNewMeta, equalMeta: check.equalMeta }, '文件已存在');
}
}
const bb = busboy({
headers: req.headers,
limits: {
fileSize: 100 * 1024 * 1024, // 100MB
files: 1,
},
defCharset: 'utf-8',
});
let fileProcessed = false;
bb.on('file', async (name, file, info) => {
fileProcessed = true;
try {
await oss.putObject(
objectName,
file,
{
...statMeta,
...getMetadata(pathname),
...meta,
},
{ check: false, isStream: true },
);
end({ success: true, name, info, isNew: true, hash, meta: meta?.metaData, statMeta }, '上传成功', 200);
} catch (error) {
console.log('postProxy upload error', error);
end({ error: error }, '上传失败', 500);
}
});
bb.on('finish', () => {
// 只有当没有文件被处理时才执行end
if (!fileProcessed) {
end({ success: false }, '没有接收到文件', 400);
}
});
bb.on('error', (err) => {
console.error('Busboy 错误:', err);
end({ error: err }, '文件解析失败', 500);
});
pipeBusboy(req, res, bb);
};
export const postChunkProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const oss = opts.oss;
const { objectName, isOwner } = await getObjectName(req);
if (!isOwner) {
return opts?.createNotFoundPage?.('no permission');
}
const _u = new URL(req.url, 'http://localhost');
const params = _u.searchParams;
const meta = parseSearchValue(params.get('meta'), { decode: true });
const end = (data: any, message?: string, code = 200) => {
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
const bb = busboy({
headers: req.headers,
limits: {
fileSize: 100 * 1024 * 1024, // 100MB
files: 1,
},
defCharset: 'utf-8',
});
const fields: any = {};
let filePromise: Promise<void> | null = null;
let tempPath = '';
let file: any = null;
bb.on('field', (fieldname, value) => {
fields[fieldname] = value;
});
bb.on('file', (_fieldname, fileStream, info) => {
const { filename, mimeType } = info;
const decodedFilename = typeof filename === 'string' ? Buffer.from(filename, 'latin1').toString('utf8') : filename;
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: decodedFilename,
mimetype: mimeType,
};
resolve();
});
writeStream.on('error', reject);
});
});
bb.on('finish', async () => {
if (filePromise) {
try {
await filePromise;
} catch (err) {
console.error(`File write error: ${err.message}`);
return end({ error: err }, '文件写入失败', 500);
}
}
const clearFiles = () => {
if (tempPath && fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath);
}
};
if (!file) {
clearFiles();
return end({ success: false }, '没有接收到文件', 400);
}
let { chunkIndex, totalChunks } = fields;
chunkIndex = parseInt(chunkIndex, 10);
totalChunks = parseInt(totalChunks, 10);
if (isNaN(chunkIndex) || isNaN(totalChunks) || totalChunks <= 0) {
clearFiles();
return end({ success: false }, 'chunkIndex 和 totalChunks 参数无效', 400);
}
const finalFilePath = path.join(cacheFilePath, `chunk-${objectName.replace(/[\/\.]/g, '-')}`);
const relativePath = file.originalFilename;
const writeStream = fs.createWriteStream(finalFilePath, { flags: 'a' });
const readStream = fs.createReadStream(tempPath);
readStream.pipe(writeStream);
writeStream.on('finish', async () => {
fs.unlinkSync(tempPath);
if (chunkIndex + 1 === totalChunks) {
try {
const metadata = {
...getMetadata(relativePath),
...meta,
'app-source': 'user-app',
};
if (fileIsExist(finalFilePath)) {
console.log('上传到对象存储', { objectName, metadata });
const content = fs.readFileSync(finalFilePath);
await oss.putObject(objectName, content, metadata);
console.log('上传到对象存储完成', { objectName });
fs.unlinkSync(finalFilePath);
}
return end({
success: true,
name: relativePath,
chunkIndex,
totalChunks,
objectName,
}, '分块上传完成', 200);
} catch (error) {
console.error('postChunkProxy upload error', error);
clearFiles();
if (fs.existsSync(finalFilePath)) {
fs.unlinkSync(finalFilePath);
}
return end({ error }, '上传失败', 500);
}
} else {
return end({
success: true,
chunkIndex,
totalChunks,
progress: ((chunkIndex + 1) / totalChunks) * 100,
}, '分块上传成功', 200);
}
});
writeStream.on('error', (err) => {
console.error('Write stream error', err);
clearFiles();
return end({ error: err }, '文件写入失败', 500);
});
});
bb.on('error', (err) => {
console.error('Busboy 错误:', err);
end({ error: err }, '文件解析失败', 500);
});
pipeBusboy(req, res, bb);
};

View File

@@ -1,4 +1,4 @@
import { bucketName, minioClient } from '@/modules/minio.ts';
import { oss } from '@/app.ts';
import { IncomingMessage, ServerResponse } from 'http';
import { filterKeys } from './http-proxy.ts';
import { getUserFromRequest } from '../utils.ts';
@@ -7,11 +7,12 @@ import { getLoginUser } from '@/modules/auth.ts';
import busboy from 'busboy';
import { getContentType, getTextContentType } from '../get-content-type.ts';
import { OssBase } from '@kevisual/oss';
import { parseSearchValue } from '@kevisual/router/browser';
import { parseSearchValue } from '@kevisual/router/src/server/parse-body.ts';
import { logger } from '@/modules/logger.ts';
import { pipeBusboy } from '../pipe-busboy.ts';
import { pipeMinioStream } from '../pipe.ts';
import { Readable } from 'stream';
import { postChunkProxy, postProxy } from './ai-proxy-chunk/post-proxy.ts'
type FileList = {
name: string;
prefix?: string;
@@ -53,6 +54,14 @@ export const getFileList = async (list: any, opts?: { objectName: string; app: s
});
};
// import { logger } from '@/module/logger.ts';
/**
* GET 处理 AI 代理请求
* 1. 如果是目录请求,返回目录列表
* 2. 如果是文件请求,返回文件流
*
* 如果是 stat
* 只返回对应的 stat 信息
*/
const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const { createNotFoundPage } = opts;
const _u = new URL(req.url, 'http://localhost');
@@ -62,6 +71,7 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
const hash = params.get('hash');
let dir = !!params.get('dir');
const recursive = !!params.get('recursive');
const showStat = !!params.get('stat');
const { objectName, app, owner, loginUser, isOwner } = await getObjectName(req);
if (!dir && _u.pathname.endsWith('/')) {
dir = true; // 如果是目录请求强制设置为true
@@ -88,10 +98,19 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
return true;
}
const stat = await oss.statObject(objectName);
if (!stat) {
createNotFoundPage('Invalid proxy url');
if (!stat && isOwner) {
// createNotFoundPage('文件不存在');
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
code: 404,
message: 'object not found',
}),
);
logger.debug('no stat', objectName, owner, req.url);
return true;
} else if (!stat && !isOwner) {
return createNotFoundPage('Invalid ai proxy url');
}
const permissionInstance = new UserPermission({ permission: stat.metaData as Permission, owner: owner });
const checkPermission = permissionInstance.checkPermissionSuccess({
@@ -102,6 +121,20 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
logger.info('no permission', checkPermission, loginUser, owner);
return createNotFoundPage('no permission');
}
if (showStat) {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
code: 200,
data: {
...stat,
metaData: filterKeys(stat.metaData, ['etag', 'size']),
},
}),
);
return true;
}
if (hash && stat.etag === hash) {
res.writeHead(304); // not modified
res.end('not modified');
@@ -112,7 +145,8 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
const etag = stat.etag;
const lastModified = stat.lastModified.toISOString();
const objectStream = await minioClient.getObject(bucketName, objectName);
const objectStream = await oss.getObject(objectName);
// const objectStream = await minioClient.getObject(bucketName, objectName);
const headers = {
'Content-Length': contentLength,
etag,
@@ -124,8 +158,7 @@ const getAiProxy = async (req: IncomingMessage, res: ServerResponse, opts: Proxy
...headers,
});
// objectStream.pipe(res, { end: true });
// @ts-ignore
pipeMinioStream(objectStream, res);
pipeMinioStream(objectStream.Body as Readable, res);
return true;
} catch (error) {
console.error(`Proxy request error: ${error.message}`);
@@ -152,86 +185,24 @@ export const getMetadata = (pathname: string) => {
return meta;
};
export const postProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const _u = new URL(req.url, 'http://localhost');
const pathname = _u.pathname;
const oss = opts.oss;
const params = _u.searchParams;
const force = !!params.get('force');
const hash = params.get('hash');
const _fileSize: string = params.get('size');
let fileSize: number | undefined = undefined;
if (_fileSize) {
fileSize = parseInt(_fileSize, 10)
export const getObjectByPathname = (opts: {
pathname: string,
version?: string,
}) => {
const [_, user, app] = opts.pathname.split('/');
let prefix = '';
let replaceKey = '';
if (app === 'ai') {
const version = opts?.version || '1.0.0';
replaceKey = `/${user}/${app}/`;
prefix = `${user}/${app}/${version}/`;
} else {
replaceKey = `/${user}/${app}/`;
prefix = `${user}/`; // root/resources
}
let meta = parseSearchValue(params.get('meta'), { decode: true });
if (!hash && !force) {
return opts?.createNotFoundPage?.('no hash');
}
const { objectName, isOwner } = await getObjectName(req);
if (!isOwner) {
return opts?.createNotFoundPage?.('no permission');
}
const end = (data: any, message?: string, code = 200) => {
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
let statMeta: any = {};
if (!force) {
const check = await oss.checkObjectHash(objectName, hash, meta);
statMeta = check?.metaData || {};
let isNewMeta = false;
if (check.success && JSON.stringify(meta) !== '{}' && !check.equalMeta) {
meta = { ...statMeta, ...getMetadata(pathname), ...meta };
isNewMeta = true;
await oss.replaceObject(objectName, { ...meta });
}
if (check.success) {
return end({ success: true, hash, meta, isNewMeta, equalMeta: check.equalMeta }, '文件已存在');
}
}
const bb = busboy({
headers: req.headers,
limits: {
fileSize: 100 * 1024 * 1024, // 100MB
files: 1,
},
});
let fileProcessed = false;
bb.on('file', async (name, file, info) => {
fileProcessed = true;
try {
await oss.putObject(
objectName,
file,
{
...statMeta,
...getMetadata(pathname),
...meta,
},
{ check: false, isStream: true, size: fileSize },
);
end({ success: true, name, info, isNew: true, hash, meta: meta?.metaData, statMeta }, '上传成功', 200);
} catch (error) {
end({ error: error }, '上传失败', 500);
}
});
bb.on('finish', () => {
// 只有当没有文件被处理时才执行end
if (!fileProcessed) {
end({ success: false }, '没有接收到文件', 400);
}
});
bb.on('error', (err) => {
console.error('Busboy 错误:', err);
end({ error: err }, '文件解析失败', 500);
});
pipeBusboy(req, res, bb);
};
let objectName = opts.pathname.replace(replaceKey, prefix);
return { prefix, replaceKey, objectName, user, app };
}
export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?: boolean }) => {
const _u = new URL(req.url, 'http://localhost');
const pathname = decodeURIComponent(_u.pathname);
@@ -251,7 +222,7 @@ export const getObjectName = async (req: IncomingMessage, opts?: { checkOwner?:
let loginUser: Awaited<ReturnType<typeof getLoginUser>> = null;
if (checkOwner) {
loginUser = await getLoginUser(req);
logger.debug('getObjectName', loginUser, user, app);
logger.debug('getObjectName', user, app);
isOwner = loginUser?.tokenUser?.username === owner;
}
return {
@@ -269,32 +240,162 @@ export const deleteProxy = async (req: IncomingMessage, res: ServerResponse, opt
if (!isOwner) {
return opts?.createNotFoundPage?.('no permission');
}
const end = (data: any, message?: string, code = 200) => {
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
try {
await oss.deleteObject(objectName);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, message: 'delete success', objectName }));
// 如果以 / 结尾,删除该前缀下的所有对象(文件夹)
if (objectName.endsWith('/')) {
const objects = await oss.listObjects<true>(objectName, { recursive: true });
if (objects.length > 0) {
for (const obj of objects) {
await oss.deleteObject(obj.name);
}
}
end({ success: true, objectName, deletedCount: objects.length }, 'delete success', 200);
} else {
await oss.deleteObject(objectName);
end({ success: true, objectName }, 'delete success', 200);
}
} catch (error) {
logger.error('deleteProxy error', error);
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: false, error: error }));
end({ success: false, error }, 'delete failed', 500);
}
};
type ProxyOptions = {
export const renameProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const { objectName, isOwner, user } = await getObjectName(req);
let oss = opts.oss;
if (!isOwner) {
return opts?.createNotFoundPage?.('no permission');
}
const end = (data: any, message?: string, code = 200) => {
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
const _u = new URL(req.url, 'http://localhost');
let newName = _u.searchParams.get('newName');
if (!newName) {
return end({ success: false }, 'newName parameter required', 400);
}
// 解码 URL 编码的路径
newName = decodeURIComponent(newName);
if (!newName.startsWith('/')) {
newName = '/' + newName;
}
const newUrl = new URL(newName, 'http://localhost');
const version = _u.searchParams.get('version') || '1.0.0';
const newNamePath = newUrl.pathname;
// 确保 newName 有正确的前缀路径
const newObject = getObjectByPathname({ pathname: newNamePath, version });
const { user: newUser, objectName: newObjectName } = newObject;
if (newUser !== user) {
return end({ success: false }, '文件重命名只能在同一用户下进行', 400);
}
try {
const isDir = objectName.endsWith('/');
let copiedCount = 0;
if (isDir) {
// 重命名文件夹:复制所有对象到新前缀,然后删除原对象
const objects = await oss.listObjects<true>(objectName, { recursive: true });
for (const obj of objects) {
const oldKey = obj.name;
const newKey = oldKey.replace(objectName, newObjectName);
console.log('rename dir object', oldKey, newKey);
await oss.copyObject(oldKey, newKey);
copiedCount++;
}
for (const obj of objects) {
console.log('deleted object', obj.name);
await oss.deleteObject(obj.name);
}
} else {
// 重命名文件
await oss.copyObject(objectName, newObjectName);
await oss.deleteObject(objectName);
copiedCount = 1;
}
end({ success: true, objectName, newObjectName, copiedCount }, 'rename success', 200);
} catch (error) {
logger.error('renameProxy error', error);
end({ success: false, error }, 'rename failed', 500);
}
};
export const updateMetadataProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const { objectName, isOwner } = await getObjectName(req);
let oss = opts.oss;
if (!isOwner) {
return opts?.createNotFoundPage?.('no permission');
}
const end = (data: any, message?: string, code = 200) => {
res.writeHead(code, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ code: code, data: data, message: message || 'success' }));
};
const _u = new URL(req.url, 'http://localhost');
const params = _u.searchParams;
const metaParam = params.get('meta');
if (!metaParam) {
return end({ success: false }, 'meta parameter required', 400);
}
const meta = parseSearchValue(metaParam, { decode: true });
try {
const stat = await oss.statObject(objectName);
if (!stat) {
return end({ success: false }, 'object not found', 404);
}
const newMeta = {
"app-source": "user-app",
...meta,
};
console.log('update metadata', objectName, newMeta);
// 过滤掉包含无效字符的 keyS3 元数据头不支持某些字符)
const filteredMeta: Record<string, string> = {};
for (const [key, value] of Object.entries(newMeta)) {
if (/^[\w\-]+$/.test(key)) {
filteredMeta[key] = String(value);
}
}
await oss.replaceObject(objectName, filteredMeta);
end({ success: true, objectName, meta }, 'update metadata success', 200);
} catch (error) {
logger.error('updateMetadataProxy error', error);
end({ success: false, error }, 'update metadata failed', 500);
}
};
export type ProxyOptions = {
createNotFoundPage: (msg?: string) => any;
oss?: OssBase;
};
export const aiProxy = async (req: IncomingMessage, res: ServerResponse, opts: ProxyOptions) => {
const oss = new OssBase({ bucketName, client: minioClient });
if (!opts.oss) {
opts.oss = oss;
}
const searchParams = new URL(req.url || '', 'http://localhost').searchParams;
if (req.method === 'POST') {
const chunk = searchParams.get('chunk');
const chunked = searchParams.get('chunked');
if (chunk !== null || chunked !== null) {
return postChunkProxy(req, res, opts);
}
return postProxy(req, res, opts);
}
if (req.method === 'DELETE') {
return deleteProxy(req, res, opts);
}
if (req.method === 'PUT') {
const meta = searchParams.get('meta');
if (meta) {
return updateMetadataProxy(req, res, opts);
}
return renameProxy(req, res, opts);
}
return getAiProxy(req, res, opts);
};

View File

@@ -1,6 +1,6 @@
import { pipeline, Readable } from 'node:stream';
import { promisify } from 'node:util';
import { bucketName, minioClient, minioResources } from '@/modules/minio.ts';
import { Readable } from 'node:stream';
import { minioResources } from '@/modules/s3.ts';
import { oss } from '@/app.ts';
import fs from 'node:fs';
import { IncomingMessage, ServerResponse } from 'node:http';
import http from 'node:http';
@@ -11,14 +11,18 @@ 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);
import { GetObjectCommandOutput } from '@aws-sdk/client-s3';
export async function downloadFileFromMinio(fileUrl: string, destFile: string) {
const objectName = fileUrl.replace(minioResources + '/', '');
const objectStream = await minioClient.getObject(bucketName, objectName);
const destStream = fs.createWriteStream(destFile);
await pipelineAsync(objectStream, destStream);
console.log(`minio File downloaded to ${minioResources}/${objectName} \n ${destFile}`);
const objectStream = await oss.getObject(objectName) as GetObjectCommandOutput;
const body = objectStream.Body as Readable;
const chunks: Uint8Array[] = [];
for await (const chunk of body) {
chunks.push(chunk);
}
fs.writeFileSync(destFile, Buffer.concat(chunks));
}
export const filterKeys = (metaData: Record<string, string>, clearKeys: string[] = []) => {
const keys = Object.keys(metaData);
@@ -43,8 +47,8 @@ export async function minioProxy(
const { createNotFoundPage, isDownload = false } = opts;
const objectName = fileUrl.replace(minioResources + '/', '');
try {
const stat = await minioClient.statObject(bucketName, objectName);
if (stat.size === 0) {
const stat = await oss.statObject(objectName);
if (stat?.size === 0) {
createNotFoundPage('Invalid proxy url');
return true;
}
@@ -54,7 +58,8 @@ export async function minioProxy(
const lastModified = stat.lastModified.toISOString();
const fileName = objectName.split('/').pop();
const ext = path.extname(fileName || '');
const objectStream = await minioClient.getObject(bucketName, objectName);
const objectStreamOutput = await oss.getObject(objectName);
const objectStream = objectStreamOutput.Body as Readable;
const headers = {
'Content-Length': contentLength,
etag,
@@ -151,6 +156,7 @@ export const httpProxy = async (
return createNotFoundPage('Invalid proxy url:' + error.message);
}
} else {
console.log('Proxying file: headers', headers);
res.writeHead(proxyRes.statusCode, {
...headers,
});

View File

@@ -1,6 +1,7 @@
import http from 'node:http';
import { minioClient } from '@/modules/minio.ts';
import { pipeMinioStream } from '../pipe.ts';
import { oss } from '@/app.ts';
import { Readable } from 'node:stream';
type ProxyInfo = {
path?: string;
target: string;
@@ -15,9 +16,8 @@ export const minioProxyOrigin = async (req: http.IncomingMessage, res: http.Serv
if (objectName.startsWith(bucketName)) {
objectName = objectName.slice(bucketName.length);
}
const objectStream = await minioClient.getObject(bucketName, objectName);
// objectStream.pipe(res);
pipeMinioStream(objectStream, res);
const objectStream = await oss.getObject(objectName);
pipeMinioStream(objectStream.Body as Readable, res);
} catch (error) {
console.error('Error fetching object from MinIO:', error);
res.statusCode = 500;

View File

@@ -27,6 +27,9 @@ export const getDNS = (req: http.IncomingMessage) => {
};
export const isLocalhost = (hostName: string) => {
if (!hostName) {
return false;
}
return hostName.includes('localhost') || hostName.includes('192.168');
};

View File

@@ -0,0 +1,344 @@
type StudioOpts = { user: string, userAppKey?: string; appIds: string[] }
export const createStudioAppListHtml = (opts: StudioOpts) => {
const user = opts.user!;
const userAppKey = opts?.userAppKey;
let showUserAppKey = userAppKey;
if (showUserAppKey && showUserAppKey.startsWith(user + '-')) {
showUserAppKey = showUserAppKey.replace(user + '-', '');
}
const pathApps = opts?.appIds?.map(appId => {
const shortAppId = appId.replace(opts!.user + '-', '')
return {
appId,
shortAppId,
pathname: `/${user}/v1/${shortAppId}`
};
}) || []
// 应用列表内容
const appListContent = `
<div class="header">
<h1><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="12" x="2" y="6" rx="2"/><path d="M12 12h.01"/><path d="M17 12h.01"/><path d="M7 12h.01"/></svg> Studio 应用列表</h1>
<p class="user-info">用户: <strong>${user}</strong></p>
</div>
<div class="app-grid">
${pathApps.map((app, index) => `
<a href="${app.pathname}" class="app-card" style="animation-delay: ${index * 0.1}s">
<div class="app-icon"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="16" x="2" y="4" rx="2"/><path d="M6 16h12"/><path d="M2 8h20"/></svg></div>
<div class="app-info">
<h3>${app.shortAppId}</h3>
<p class="app-path">${app.pathname}</p>
</div>
<div class="app-arrow">→</div>
</a>
`).join('')}
</div>
${pathApps.length === 0 ? `
<div class="empty-state">
<div class="empty-icon">📭</div>
<p>暂无应用</p>
</div>
` : ''}
`
return `
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Studio - ${user} 的应用</title>
<style>
:root {
--primary-color: #000000;
--primary-hover: #333333;
--text-color: #111111;
--text-secondary: #666666;
--bg-color: #ffffff;
--card-bg: #ffffff;
--border-color: #e0e0e0;
--shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);
--shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
* {
box-sizing: border-box;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
margin: 0;
padding: 0;
min-height: 100vh;
line-height: 1.6;
}
.container {
max-width: 900px;
margin: 0 auto;
padding: 2rem;
}
/* Not Found Styles */
.not-found {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 60vh;
text-align: center;
padding: 3rem;
background-color: var(--card-bg);
border-radius: 16px;
border: 1px solid var(--border-color);
box-shadow: var(--shadow);
}
.not-found-icon {
width: 80px;
height: 80px;
margin-bottom: 1.5rem;
color: var(--text-secondary);
}
.not-found h1 {
font-size: 2.5rem;
color: #000000;
margin-bottom: 1rem;
}
.not-found p {
color: var(--text-secondary);
margin-bottom: 0.5rem;
max-width: 400px;
}
.not-found code {
background-color: #f5f5f5;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-family: 'Fira Code', 'Monaco', monospace;
color: #000000;
border: 1px solid var(--border-color);
}
.back-link {
display: inline-block;
margin-top: 1.5rem;
padding: 0.75rem 1.5rem;
background-color: var(--primary-color);
color: white;
text-decoration: none;
border-radius: 8px;
font-weight: 500;
transition: all 0.2s;
}
.back-link:hover {
background-color: var(--primary-hover);
transform: translateY(-2px);
}
/* App List Styles */
.header {
text-align: center;
margin-bottom: 3rem;
padding-bottom: 2rem;
border-bottom: 1px solid var(--border-color);
}
.header h1 {
font-size: 2rem;
font-weight: 700;
color: var(--text-color);
margin: 0 0 0.5rem 0;
}
.user-info {
color: var(--text-secondary);
margin: 0;
}
.user-info strong {
color: var(--primary-color);
}
.app-grid {
display: flex;
flex-direction: column;
gap: 1rem;
}
.app-card {
display: flex;
align-items: center;
padding: 1.25rem 1.5rem;
background-color: var(--card-bg);
border-radius: 12px;
text-decoration: none;
color: inherit;
border: 1px solid var(--border-color);
box-shadow: var(--shadow);
transition: all 0.3s ease;
animation: slideIn 0.5s ease-out backwards;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.app-card:hover {
transform: translateY(-3px);
box-shadow: var(--shadow-hover);
border-color: var(--primary-color);
}
.app-icon {
font-size: 2rem;
margin-right: 1rem;
flex-shrink: 0;
}
.app-info {
flex: 1;
min-width: 0;
}
.app-info h3 {
margin: 0;
font-size: 1.1rem;
font-weight: 600;
color: var(--text-color);
}
.app-path {
margin: 0.25rem 0 0 0;
font-size: 0.85rem;
color: var(--text-secondary);
font-family: 'Fira Code', 'Monaco', monospace;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.app-arrow {
font-size: 1.25rem;
color: var(--text-secondary);
transition: all 0.3s ease;
flex-shrink: 0;
}
.app-card:hover .app-arrow {
color: var(--primary-color);
transform: translateX(5px);
}
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 4rem 2rem;
text-align: center;
color: var(--text-secondary);
}
.empty-icon {
font-size: 4rem;
margin-bottom: 1rem;
opacity: 0.6;
}
.empty-state p {
font-size: 1.1rem;
margin: 0;
}
/* Footer */
.footer {
margin-top: 3rem;
padding-top: 2rem;
border-top: 1px solid var(--border-color);
text-align: center;
font-size: 0.85rem;
color: var(--text-secondary);
}
/* Dark mode support */
@media (prefers-color-scheme: dark) {
:root {
--primary-color: #ffffff;
--primary-hover: #cccccc;
--text-color: #ffffff;
--text-secondary: #999999;
--bg-color: #000000;
--card-bg: #1a1a1a;
--border-color: #333333;
}
.not-found code {
background-color: #333333;
}
}
/* Responsive */
@media (max-width: 600px) {
.container {
padding: 1rem;
}
.header h1 {
font-size: 1.5rem;
}
.app-card {
padding: 1rem;
}
.app-icon {
font-size: 1.5rem;
margin-right: 0.75rem;
}
.app-info h3 {
font-size: 1rem;
}
.app-path {
font-size: 0.75rem;
}
}
</style>
</head>
<body>
<div class="container">
${showUserAppKey ? `
<div class="not-found">
<svg class="not-found-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21 21-4.34-4.34"/><circle cx="11" cy="11" r="8"/></svg>
<h1>应用不存在</h1>
<p>抱歉,您访问的应用 <code>${showUserAppKey || ''}</code> 不存在。</p>
<p>请检查应用 Key 是否正确,或联系管理员。</p>
<a href="/${user}/v1/" class="back-link">← 返回应用列表</a>
</div>
` : ''}
${appListContent}
<div class="footer">
© ${new Date().getFullYear()} Studio - 应用管理
</div>
</div>
</body>
</html>
`;
};

View File

@@ -1,38 +1,13 @@
import { Client, ClientOptions } from 'minio';
import { Client, } from 'minio';
import { useConfig } from '@kevisual/use-config';
const config = useConfig();
import { OssBase } from '@kevisual/oss/services';
const minioConfig = {
endPoint: config.MINIO_ENDPOINT || 'localhost',
// @ts-ignore
port: parseInt(config.MINIO_PORT || '9000'),
useSSL: config.MINIO_USE_SSL === 'true',
accessKey: config.MINIO_ACCESS_KEY,
secretKey: config.MINIO_SECRET_KEY,
};
const { port, endPoint, useSSL } = minioConfig;
// console.log('minioConfig', minioConfig);
export const minioClient = new Client(minioConfig);
export const bucketName = config.MINIO_BUCKET_NAME || 'resources';
export const minioUrl = `http${useSSL ? 's' : ''}://${endPoint}:${port || 9000}`;
export const minioResources = `${minioUrl}/resources`;
if (!minioClient) {
throw new Error('Minio client not initialized');
}
// 验证权限
(async () => {
const bucketExists = await minioClient.bucketExists(bucketName);
if (!bucketExists) {
await minioClient.makeBucket(bucketName);
}
console.log('bucketExists', bucketExists);
// const res = await minioClient.putObject(bucketName, 'root/test/0.0.1/a.txt', 'test');
// console.log('minio putObject', res);
})();
export const oss = new OssBase({
client: minioClient,
bucketName: bucketName,
prefix: '',
});

44
src/modules/s3.ts Normal file
View File

@@ -0,0 +1,44 @@
import { CreateBucketCommand, HeadObjectCommand, S3Client, } from '@aws-sdk/client-s3';
import { OssBase } from '@kevisual/oss/s3.ts';
import { useConfig } from '@kevisual/use-config';
const config = useConfig();
export const bucketName = config.S3_BUCKET_NAME || 'resources';
export const s3Client = new S3Client({
credentials: {
accessKeyId: config.S3_ACCESS_KEY_ID || '',
secretAccessKey: config.S3_SECRET_ACCESS_KEY || '',
},
region: config.S3_REGION,
endpoint: config.S3_ENDPOINT,
// minio配置
forcePathStyle: true,
});
// 判断 bucketName 是否存在,不存在则创建
(async () => {
try {
await s3Client.send(new HeadObjectCommand({ Bucket: bucketName, Key: '' }));
console.log(`Bucket ${bucketName} exists.`);
} catch (error) {
console.log(`Bucket ${bucketName} does not exist. Creating...`);
if (config.S3_ENDPOINT?.includes?.('9000')) {
// 创建 bucket
await s3Client.send(new CreateBucketCommand({ Bucket: bucketName }));
console.log(`Bucket ${bucketName} created.`);
}
}
})();
if (!s3Client) {
throw new Error('S3 client not initialized');
}
export const oss = new OssBase({
client: s3Client,
bucketName: bucketName,
prefix: '',
})
export const minioResources = `${config.S3_ENDPOINT}/${bucketName}`;

View File

@@ -7,7 +7,7 @@ import { nanoid } from 'nanoid';
import { pipeline } from 'stream';
import { promisify } from 'util';
import { getAppLoadStatus, setAppLoadStatus } from './get-app-status.ts';
import { minioResources } from '../minio.ts';
import { minioResources } from '../s3.ts';
import { downloadFileFromMinio, fetchApp, fetchDomain, fetchTest } from '@/modules/fm-manager/index.ts';
import { logger } from '../logger.ts';
export * from './get-app-status.ts';

97
src/modules/v3/index.ts Normal file
View File

@@ -0,0 +1,97 @@
import { IncomingMessage, ServerResponse } from 'http';
import { App } from '@kevisual/router';
import { logger } from '../logger.ts';
// import { getLoginUser } from '@/modules/auth.ts';
import { SSEManager } from './sse/sse-manager.ts';
import { getLoginUser } from '../auth.ts';
import { emitter, flowme_insert } from '../../realtime/flowme/index.ts';
export const sseManager = new SSEManager();
emitter.on(flowme_insert, (data) => {
console.log('flowme_insert event received:', data);
const uid = data.uid;
if (uid) {
sseManager.broadcast({ type: 'flowme_insert', data }, { userId: uid });
}
});
type ProxyOptions = {
createNotFoundPage: (msg?: string) => any;
};
export const UserV3Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
const { url } = req;
const _url = new URL(url || '', `http://localhost`);
const { pathname, searchParams } = _url;
let [user, app, ...rest] = pathname.split('/').slice(1);
if (!user || !app) {
opts?.createNotFoundPage?.('应用未找到');
return false;
}
const last = rest.slice(-1)[0] || '';
const method = req.method || 'GET';
console.log('UserV3Proxy request: last', last, rest);
if (method === 'GET' && last === 'event') {
const info = await getLoginUser(req);
if (!info) {
opts?.createNotFoundPage?.('没有登录');
return false;
}
console.log('建立 SSE 连接, user=', info.tokenUser.uid);
addEventStream(req, res, info);
return true;
}
res.end(`UserV3Proxy: user=${user}, app=${app}, rest=${rest.join('/')}`);
console.log('UserV3Proxy:', { user, app, });
return true;
};
type Opts = {
tokenUser: any;
token: string;
}
const addEventStream = (req: IncomingMessage, res: ServerResponse, opts?: Opts) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
'Access-Control-Allow-Origin': '*'
});
console.log('Client connected for SSE', opts?.tokenUser?.username || 'unknown');
const uid = opts?.tokenUser?.id || 'guest';
console.log('SSE for userId=', opts?.tokenUser);
const connectionInfo = sseManager.createConnection({ userId: uid });
const { stream, id: connectionId } = connectionInfo;
// 设置心跳
connectionInfo.heartbeatInterval = setInterval(() => {
sseManager.sendToConnection(connectionId, { type: "heartbeat", timestamp: Date.now() })
.catch(() => {
// 心跳失败时清理连接
sseManager.closeConnection(connectionId);
});
}, 30000); // 30秒心跳
const timer = setInterval(async () => {
sseManager.broadcast({ type: "time", timestamp: Date.now() });
const hasId = sseManager.getConnection(connectionId);
if (!hasId) {
clearInterval(timer);
console.log('清理广播定时器,连接已关闭');
}
}, 1000);
res.pipe(stream as any);
const bun = (req as any).bun
const request = bun?.request as Bun.BunRequest<string>
if (request) {
if (request.signal) {
// 当客户端断开时清理连接
request.signal.addEventListener("abort", () => {
console.log(`Client ${connectionId} disconnected`);
sseManager.closeConnection(connectionId);
});
}
}
// console.log('res', req)
// res.end('123');
}
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

View File

@@ -0,0 +1,134 @@
import { nanoid } from "nanoid";
type ConnectionInfo = {
id: string;
writer: WritableStreamDefaultWriter;
stream: ReadableStream<any>;
connectedAt: Date;
heartbeatInterval: NodeJS.Timeout | null;
userId?: string;
};
export class SSEManager {
private connections: Map<string, ConnectionInfo> = new Map();
private userConnections: Map<string, Set<string>> = new Map(); // userId -> connectionIds
constructor() {
// 初始化逻辑
}
createConnection(info?: { userId?: string }): ConnectionInfo {
const connectionId = nanoid(16);
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
// 存储连接信息
const connectionInfo = {
id: connectionId,
writer,
stream: readable,
connectedAt: new Date(),
heartbeatInterval: null,
userId: info?.userId
};
this.connections.set(connectionId, connectionInfo);
// 添加到用户索引
if (info?.userId) {
const userSet = this.userConnections.get(info.userId) || new Set();
userSet.add(connectionId);
this.userConnections.set(info.userId, userSet);
}
return connectionInfo;
}
sendToConnection(connectionId: string, data: any) {
const connection = this.connections.get(connectionId);
if (connection) {
const message = `data: ${JSON.stringify(data)}\n\n`;
return connection.writer.write(new TextEncoder().encode(message));
}
throw new Error(`Connection ${connectionId} not found`);
}
getConnection(connectionId: string) {
return this.connections.get(connectionId);
}
broadcast(data: any, opts?: { userId?: string }) {
const message = `data: ${JSON.stringify(data)}\n\n`;
const promises = [];
// 指定 userId只发送给目标用户通过索引快速查找
if (opts?.userId) {
const userConnIds = this.userConnections.get(opts.userId);
if (userConnIds) {
for (const connId of userConnIds) {
const conn = this.connections.get(connId);
if (conn) {
promises.push(
conn.writer.write(new TextEncoder().encode(message))
.catch(() => {
this.closeConnection(connId);
})
);
}
}
}
return Promise.all(promises);
}
// 未指定 userId广播给所有人
for (const [id, connection] of this.connections) {
promises.push(
connection.writer.write(new TextEncoder().encode(message))
.catch(() => {
this.closeConnection(id);
})
);
}
return Promise.all(promises);
}
closeConnection(connectionId: string) {
const connection = this.connections.get(connectionId);
if (connection) {
// 清理心跳定时器
if (connection.heartbeatInterval) {
clearInterval(connection.heartbeatInterval);
}
// 从用户索引中移除
if (connection.userId) {
const userSet = this.userConnections.get(connection.userId);
if (userSet) {
userSet.delete(connectionId);
if (userSet.size === 0) {
this.userConnections.delete(connection.userId);
}
}
}
// 关闭写入器
connection.writer.close().catch(console.error);
// 从管理器中移除
this.connections.delete(connectionId);
console.log(`Connection ${connectionId} closed`);
return true;
}
return false;
}
closeAllConnections() {
for (const [connectionId, connection] of this.connections) {
this.closeConnection(connectionId);
}
}
getActiveConnections() {
return Array.from(this.connections.keys());
}
}

View File

@@ -2,21 +2,50 @@ import { nanoid } from 'nanoid';
import { WebSocket } from 'ws';
import { logger } from '../logger.ts';
import { EventEmitter } from 'eventemitter3';
class WsMessage {
ws: WebSocket;
user?: string;
emitter: EventEmitter;;
emitter: EventEmitter;
private pingTimer?: NodeJS.Timeout;
private readonly PING_INTERVAL = 30000; // 30 秒发送一次 ping
constructor({ ws, user }: WssMessageOptions) {
this.ws = ws;
this.user = user;
this.emitter = new EventEmitter();
this.startPing();
}
private startPing() {
this.stopPing();
this.pingTimer = setInterval(() => {
if (this.ws.readyState === WebSocket.OPEN) {
this.ws.ping();
} else {
this.stopPing();
}
}, this.PING_INTERVAL);
}
private stopPing() {
if (this.pingTimer) {
clearInterval(this.pingTimer);
this.pingTimer = undefined;
}
}
destroy() {
this.stopPing();
this.emitter.removeAllListeners();
}
async sendResponse(data: any) {
if (data.id) {
this.emitter.emit(data.id, data?.data);
}
}
async sendData(data: any, opts?: { timeout?: number }) {
async sendData(data: any, context?: any, opts?: { timeout?: number }) {
if (this.ws.readyState !== WebSocket.OPEN) {
return { code: 500, message: 'WebSocket is not open' };
}
@@ -25,7 +54,10 @@ class WsMessage {
const message = JSON.stringify({
id,
type: 'proxy',
data,
data: {
message: data,
context: context || {},
},
});
logger.info('ws-proxy sendData', message);
this.ws.send(message);
@@ -56,9 +88,12 @@ export class WsProxyManager {
const value = this.wssMap.get(id);
if (value) {
value.ws.close();
value.destroy();
}
}
console.log('WsProxyManager register', id);
const [username, appId] = id.split('-');
const url = new URL(`/${username}/v1/${appId}`, 'https://kevisual.cn/');
console.log('WsProxyManager register', id, '访问地址', url.toString());
const value = new WsMessage({ ws: opts?.ws, user: opts?.user });
this.wssMap.set(id, value);
}
@@ -66,10 +101,14 @@ export class WsProxyManager {
const value = this.wssMap.get(id);
if (value) {
value.ws.close();
value.destroy();
}
this.wssMap.delete(id);
}
getIds() {
getIds(beginWith?: string) {
if (beginWith) {
return Array.from(this.wssMap.keys()).filter(key => key.startsWith(beginWith));
}
return Array.from(this.wssMap.keys());
}
get(id: string) {

View File

@@ -4,15 +4,18 @@ import { wsProxyManager } from './index.ts';
import { App } from '@kevisual/router';
import { logger } from '../logger.ts';
import { getLoginUser } from '@/modules/auth.ts';
import { createStudioAppListHtml } from '../html/studio-app-list/index.ts';
import { omit } from 'es-toolkit';
type ProxyOptions = {
createNotFoundPage: (msg?: string) => any;
};
export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opts?: ProxyOptions) => {
const { url } = req;
const { pathname } = new URL(url || '', `http://localhost`);
const _url = new URL(url || '', `http://localhost`);
const { pathname, searchParams } = _url;
let [user, app, userAppKey] = pathname.split('/').slice(1);
if (!user || !app || !userAppKey) {
if (!user || !app) {
opts?.createNotFoundPage?.('应用未找到');
return false;
}
@@ -23,7 +26,26 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt
opts?.createNotFoundPage?.('没有登录');
return false;
}
const isAdmin = loginUser.tokenUser?.username === user
if (!userAppKey) {
if (isAdmin) {
// 获取所有的管理员的应用列表
const ids = wsProxyManager.getIds(user + '-');
const html = createStudioAppListHtml({ user, appIds: ids, userAppKey });
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
return;
} else {
opts?.createNotFoundPage?.('没有访问应用权限');
return false;
}
}
if (!userAppKey.includes('-')) {
userAppKey = user + '-' + userAppKey;
}
// TODO: 如果不是管理员,是否需要添加其他人可以访问的逻辑?
if (!isAdmin) {
opts?.createNotFoundPage?.('没有访问应用权限');
@@ -34,16 +56,32 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt
}
logger.debug('data', data);
const client = wsProxyManager.get(userAppKey);
const ids = wsProxyManager.getIds();
const ids = wsProxyManager.getIds(user + '-');
if (!client) {
if (isAdmin) {
opts?.createNotFoundPage?.(`未找到应用 [${userAppKey}], 当前应用列表: ${ids.join(',')}`);
const html = createStudioAppListHtml({ user, appIds: ids, userAppKey });
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
} else {
opts?.createNotFoundPage?.('应用访问失败');
}
return false;
}
const value = await client.sendData(data);
const path = searchParams.get('path');
if (!path) {
// 显示前端页面
const html = fetch('https://kevisual.cn/root/router-studio/index.html').then(res => res.text());
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(await html);
return true;
}
let message: any = data;
if (!isAdmin) {
message = omit(data, ['token', 'cookies']);
}
const value = await client.sendData(message, {
state: { tokenUser: omit(loginUser.tokenUser, ['oauthExpand']) },
});
if (value) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(value));

View File

@@ -1 +0,0 @@
import './list.ts';

View File

@@ -1,108 +0,0 @@
import { CustomError } from '@kevisual/router';
import { app } from '../../app.ts';
import { ContainerModel, ContainerData, Container } from './models/index.ts';
app
.route({
path: 'container',
key: 'list',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const list = await ContainerModel.findAll({
order: [['updatedAt', 'DESC']],
where: {
uid: tokenUser.id,
},
attributes: { exclude: ['code'] },
});
ctx.body = list;
return ctx;
})
.addTo(app);
app
.route({
path: 'container',
key: 'get',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
const container = await ContainerModel.findByPk(id);
if (!container) {
throw new CustomError('container not found');
}
if (container.uid !== tokenUser.id) {
throw new CustomError('container not found');
}
ctx.body = container;
return ctx;
})
.addTo(app);
app
.route({
path: 'container',
key: 'update',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const data = ctx.query.data;
const { id, ...container } = data;
let containerModel: ContainerModel | null = null;
if (id) {
containerModel = await ContainerModel.findByPk(id);
if (containerModel) {
containerModel.update({
...container,
publish: {
...containerModel.publish,
...container.publish,
},
});
await containerModel.save();
}
} else {
try {
containerModel = await ContainerModel.create({
...container,
uid: tokenUser.id,
});
} catch (e) {
console.log('error', e);
}
console.log('containerModel', container);
}
ctx.body = containerModel;
return ctx;
})
.addTo(app);
app
.route({
path: 'container',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
const container = await ContainerModel.findByPk(id);
if (!container) {
throw new CustomError('container not found');
}
if (container.uid !== tokenUser.id) {
throw new CustomError('container not found');
}
await container.destroy();
ctx.body = container;
return ctx;
})
.addTo(app);

View File

@@ -1,101 +0,0 @@
import { sequelize } from '../../../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
import crypto from 'crypto';
export interface ContainerData {}
export type ContainerPublish = {
key: string;
title?: string;
description?: string;
fileName?: string;
version?: string;
};
export type Container = Partial<InstanceType<typeof ContainerModel>>;
/**
* 用户代码容器
*/
export class ContainerModel extends Model {
declare id: string;
// 标题
declare title: string;
// 描述
declare description: string;
// 类型
declare type: string;
// 标签
declare tags: string[];
// 代码
declare code: string;
// hash 值
declare hash: string;
// 数据
declare data: ContainerData;
// 发布
declare publish: ContainerPublish;
// 用户 id
declare uid: string;
declare updatedAt: Date;
declare createdAt: Date;
createHash() {
const { code } = this;
const hash = crypto.createHash('md5');
hash.update(code);
this.hash = hash.digest('hex');
}
}
ContainerModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
comment: 'id',
},
title: {
type: DataTypes.TEXT,
defaultValue: '',
},
description: {
type: DataTypes.TEXT,
defaultValue: '',
},
tags: {
type: DataTypes.JSON,
defaultValue: [],
},
type: {
type: DataTypes.STRING, // 代码类型, html, js, render-js
defaultValue: 'render-js',
},
code: {
type: DataTypes.TEXT,
defaultValue: '',
},
hash: {
type: DataTypes.TEXT,
defaultValue: '',
},
data: {
type: DataTypes.JSON,
defaultValue: {},
},
publish: {
type: DataTypes.JSON,
defaultValue: {},
},
uid: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
sequelize,
tableName: 'kv_container',
paranoid: true,
},
);
// ContainerModel.sync({ alter: true, logging: false }).catch((e) => {
// console.error('ContainerModel sync', e);
// });

View File

@@ -1,11 +0,0 @@
import { ContainerModel } from '../models/index.ts';
export const getContainerById = async (id: string) => {
const container = await ContainerModel.findByPk(id);
const code = container?.code;
return {
code,
id: container?.id,
updatedAt: new Date(container?.updatedAt).getTime(),
};
};

View File

@@ -1,3 +0,0 @@
import { ContainerData } from './models/index.ts';
export { ContainerData };

View File

@@ -1,3 +0,0 @@
import './list.ts'
import './publish.ts'

View File

@@ -1,312 +0,0 @@
import { CustomError } from '@kevisual/router';
import { app } from '../../app.ts';
import { PageModel } from './models/index.ts';
import { nanoid, customAlphabet } from 'nanoid'
import { ContainerModel } from '../container/models/index.ts';
import { Op } from 'sequelize';
import { getDeck } from './module/cache-file.ts';
export const clearBlank = (newStyle: any) => {
for (let key in newStyle) {
if (newStyle[key] === '' || newStyle[key] === undefined || newStyle[key] === null) {
delete newStyle[key];
}
}
};
const uuidv4 = () => {
const alphabet = '0123456789abcdef';
const nanoidCustom = customAlphabet(alphabet, 36);
return nanoidCustom();
}
app
.route({
path: 'page',
key: 'get',
})
.define(async (ctx) => {
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
try {
const page = await PageModel.findByPk(id);
ctx.body = page;
} catch (e) {
console.log('error', e);
throw new CustomError(e.message || 'get error');
}
})
.addTo(app);
app
.route({
path: 'page',
key: 'list',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
ctx.body = await PageModel.findAll({
order: [['updatedAt', 'DESC']],
where: {
uid: tokenUser.id,
},
});
return ctx;
})
.addTo(app);
app
.route({
path: 'page',
key: 'update',
middleware: ['auth'],
})
.define(async (ctx) => {
const { data, id, publish, ...rest } = ctx.query.data;
const tokenUser = ctx.state.tokenUser;
let needUpdate = { ...rest };
if (data) {
needUpdate = { ...needUpdate, data };
}
if (publish) {
needUpdate = { ...needUpdate, publish };
}
if (id) {
const page = await PageModel.findByPk(id);
// if (!page?.publish && publish) {
// needUpdate = { ...needUpdate, publish };
// }
if (page) {
const newPage = await page.update({ ...needUpdate });
ctx.body = newPage;
} else {
throw new CustomError('page not found');
}
} else if (data) {
const page = await PageModel.create({ ...needUpdate, uid: tokenUser.id });
ctx.body = page;
}
})
.addTo(app);
app
.route('page', 'updateNode')
.define(async (ctx) => {
const { id, nodeData } = ctx.query.data;
const force = ctx.query.force;
if (!id) {
throw new CustomError('id is required');
}
const page = await PageModel.findByPk(id);
if (!page) {
throw new CustomError('page not found');
}
const { data } = page;
const { nodes = [] } = data;
let flag = false;
const newNodes = nodes.map((item) => {
const nodeItem = nodeData;
if (item.id === nodeItem.id) {
flag = true;
const { data, ...rest } = nodeItem;
const { style, ...restData } = data || {};
let newStyle = force ? { ...style } : { ...item?.data?.style, ...style };
clearBlank(newStyle);
console.log('newStyle', newStyle);
const newNodeItem = {
...item,
...rest,
data: {
...item?.data,
...restData,
style: newStyle,
},
};
console.log('newNodeItem', newNodeItem);
return newNodeItem;
}
return item;
});
if (!flag) {
newNodes.push(nodeData);
}
const newData = { ...data, nodes: newNodes };
const newPage = await page.update({ data: newData });
ctx.body = newPage;
return ctx;
})
.addTo(app);
app
.route({
path: 'page',
key: 'delete',
})
.define(async (ctx) => {
const id = ctx.query.id;
const page = await PageModel.findByPk(id);
if (page) {
await page.destroy();
}
ctx.body = page;
return ctx;
})
.addTo(app);
app
.route({
path: 'page',
key: 'addDemo',
})
.define(async (ctx) => {
const id = uuidv4();
const data = {
// id: 'container-1',
id,
title: 'demo',
description: 'demo',
type: 'conainer',
data: {
edges: [
{
id: 'e1',
// source: 'container-1',
source: id,
target: 'container-2',
},
{
id: 'e2',
// source: 'container-1',
source: id,
target: 'container-3',
},
{
id: 'e3',
source: 'container-2',
target: 'container-4',
},
],
nodes: [
{
// id: 'container-1',
id,
type: 'container',
data: {
label: '开始',
title: 'demo-hello-world',
cid: 'a6652ce0-82fb-432a-a6b0-2033a655b02c',
root: true,
style: {
border: '1px solid black',
},
},
position: {
x: 50,
y: 125,
},
},
{
id: 'container-2',
type: 'container',
data: {
label: '容器',
title: 'demo-child-01',
cid: '67e5b2ff-98dc-43ab-8ad9-9b062096f8eb',
style: {
color: 'green',
position: 'absolute',
border: '1px solid black',
top: '100px',
left: '100px',
width: '200px',
height: '200px',
},
shadowRoot: true,
},
position: {
x: 350,
y: 125,
},
},
{
id: 'container-3',
type: 'container',
data: {
label: '容器',
title: 'demo-child-03',
cid: '208c3e36-dc7d-46af-b2f0-81d5f43c974d',
style: {
color: 'green',
position: 'absolute',
border: '1px solid green',
top: '100px',
left: '100px',
width: '200px',
height: '200px',
},
},
position: {
x: 350,
y: 325,
},
},
{
id: 'container-4',
type: 'container',
data: {
label: '容器',
title: 'demo-child-04',
cid: '170c0b55-8c13-4d6e-bf35-3f935d979a0d',
style: {
color: 'green',
position: 'absolute',
border: '1px solid green',
top: '100px',
left: '400px',
width: '200px',
height: '200px',
},
},
position: {
x: 650,
y: 125,
},
},
],
viewport: {},
},
};
try {
const page = await PageModel.create(data);
ctx.body = page;
} catch (e) {
console.log('error', e);
throw new CustomError('addDemo error');
}
})
.addTo(app);
app
.route({
path: 'page',
key: 'getDeck',
})
.define<any>(async (ctx) => {
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
try {
const page = await PageModel.findByPk(id);
if (!page) {
throw new CustomError(404, 'panel not found');
}
const pageData = await getDeck(page);
ctx.body = pageData;
} catch (e) {
console.log('error', e);
throw new CustomError(e.message || 'get error');
}
})
.addTo(app);

View File

@@ -1,88 +0,0 @@
import { sequelize } from '../../../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
type PageNodeData = {
id: string;
type: string;
data: {
label?: string; // 容器 开始 结束
root?: boolean; // 是否是根节点
// 容器上的属性
cid?: string; // 容器id
style?: { [key: string]: string };
className?: string;
showChild?: boolean;
shadowRoot?: boolean;
};
[key: string]: any;
};
export interface PageData {
edges: any[];
nodes: PageNodeData[];
viewport: any;
[key: string]: any;
}
export type Publish = {
id?: string; // resource id
description?: string;
key?: string;
version?: string;
};
/**
* 页面数据
*/
export class PageModel extends Model {
declare id: string;
declare title: string;
declare description: string;
declare type: string;
declare data: PageData;
declare publish: Publish;
declare uid: string;
}
PageModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
comment: 'id',
},
title: {
type: DataTypes.STRING,
defaultValue: '',
},
description: {
type: DataTypes.TEXT,
defaultValue: '',
},
type: {
type: DataTypes.STRING,
defaultValue: '',
},
data: {
type: DataTypes.JSON,
defaultValue: {},
},
publish: {
type: DataTypes.JSON,
defaultValue: {},
},
uid: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
sequelize,
tableName: 'kv_page',
paranoid: true,
},
);
// PageModel.sync({ alter: true, logging: false }).catch((e) => {
// console.error('PageModel sync', e);
// });

View File

@@ -1,193 +0,0 @@
import { useFileStore } from '@kevisual/use-config';
import { PageModel } from '../models/index.ts';
import { ContainerModel } from '@/old-apps/container/models/index.ts';
import { Op } from 'sequelize';
import { getContainerData } from './get-container.ts';
import path from 'node:path';
import fs from 'node:fs';
import { getHTML, getDataJs, getOneHTML } from './file-template.ts';
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { getContentType } from '@/utils/get-content-type.ts';
import archiver from 'archiver';
import { CustomError } from '@kevisual/router';
import { nanoid } from 'nanoid';
export const cacheFile = useFileStore('cache-file', {
needExists: true,
});
export const getDeck = async (page: PageModel) => {
const { data } = page;
const { nodes = [], edges } = data;
const containerList = nodes
.map((item) => {
const { data } = item;
return data?.cid;
})
.filter((item) => item);
const quchong = Array.from(new Set(containerList));
const containers = await ContainerModel.findAll({
where: {
id: {
[Op.in]: quchong,
},
},
});
const pageData = {
page,
containerList: containers,
};
return pageData;
};
export const cachePage = async (page: PageModel, opts: { tokenUser: any; key; version }) => {
const _result = await getDeck(page);
const result = getContainerData(_result);
const key = 'data-' + nanoid(6);
const html = getHTML({ rootId: page.id, title: page?.publish?.key, dataKey: key });
const dataJs = getDataJs(result);
const htmlPath = path.resolve(cacheFile, `${page.id}.html`);
const dataJsPath = path.resolve(cacheFile, `${page.id}.js`);
fs.writeFileSync(htmlPath, html);
fs.writeFileSync(dataJsPath, dataJs);
const minioHTML = await uploadMinio({ ...opts, path: `index.html`, filePath: htmlPath });
const minioData = await uploadMinio({ ...opts, path: `${key || 'data'}.js`, filePath: dataJsPath });
return [
{
name: 'index.html',
path: minioHTML,
},
{
name: `${key || 'data'}.js`,
path: minioData,
},
];
};
export const uploadMinioContainer = async ({ tokenUser, key, version, code, filePath, saveHTML }) => {
if ((filePath as string).includes('..')) {
throw new CustomError('file path is invalid');
}
const uploadFiles = [];
const minioKeyVersion = `${tokenUser.username}/${key}/${version}`;
const minioPath = path.join(minioKeyVersion, filePath);
const minioFileName = path.basename(minioPath);
if (!minioFileName.endsWith('.js')) {
saveHTML = false;
}
console.log('minioPath', minioPath);
// const isHTML = filePath.endsWith('.html');
const name = minioPath.replace(minioKeyVersion + '/', '');
await minioClient.putObject(bucketName, minioPath, code, code.length, {
'Content-Type': getContentType(filePath),
'app-source': 'user-app',
'Cache-Control': 'no-cache', // no-cache
});
uploadFiles.push({
name,
path: minioPath,
});
if (saveHTML) {
const htmlPath = minioPath.replace('.js', '.html');
const code = getOneHTML({ title: 'Kevisual', file: minioFileName.replace('.js', '') });
await minioClient.putObject(bucketName, htmlPath, code, code.length, {
'Content-Type': 'text/html',
'app-source': 'user-app',
'Cache-Control': 'max-age=31536000, immutable',
});
uploadFiles.push({
name: 'index.html',
path: htmlPath,
});
}
return uploadFiles;
};
export const uploadMinio = async ({ tokenUser, key, version, path, filePath }) => {
const minioPath = `${tokenUser.username}/${key}/${version}/${path}`;
const isHTML = filePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, filePath, {
'Content-Type': getContentType(filePath),
'app-source': 'user-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
});
fs.unlinkSync(filePath); // 删除临时文件
return minioPath;
};
export const uploadMinioTemp = async ({ tokenUser, filePath, path }) => {
const minioPath = `${tokenUser.username}/temp/${path}`;
const isHTML = filePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, filePath, {
'Content-Type': getContentType(filePath),
'app-source': 'user-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
});
fs.unlinkSync(filePath); // 删除临时文件
return minioPath;
};
export const getZip = async (page: PageModel, opts: { tokenUser: any }) => {
const _result = await getDeck(page);
const result = getContainerData(_result);
const html = getHTML({ rootId: page.id, title: page?.publish?.key });
const dataJs = getDataJs(result);
const zip = archiver('zip', {
zlib: { level: 9 },
});
// 创建 zip 文件的输出流
const zipCache = path.join(cacheFile, `${page.id}.zip`);
if (checkFileExistsSync(zipCache)) {
throw new CustomError('page is on uploading');
}
return await new Promise((resolve, reject) => {
const output = fs.createWriteStream(zipCache);
// 监听事件
output.on('close', async () => {
console.log(`Zip file has been created successfully. Total size: ${zip.pointer()} bytes.`);
let time = (new Date().getTime() / 1000).toFixed(0);
const name = page.title || page.id;
const minioPath = await uploadMinioTemp({ ...opts, filePath: zipCache, path: `${name + '-' + time}.zip` });
resolve(minioPath);
});
output.on('end', () => {
console.log('Data has been drained.'); // 数据已被耗尽
throw new CustomError('Data has been drained.');
});
zip.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.warn('File not found:', err);
} else {
throw err;
}
});
zip.on('error', (err) => {
throw err;
});
// 通过管道将 zip 数据流输出到指定文件
zip.pipe(output);
// 添加 HTML 字符串作为文件到 zip 中
zip.append(html, { name: 'index.html' });
// 添加 JavaScript 字符串作为文件到 zip 中
zip.append(dataJs, { name: 'data.js' });
zip.append(JSON.stringify(page), { name: 'app.config.json' });
// 可以继续添加更多内容,文件或目录等
// zip.append('Another content', { name: 'other.txt' });
// 结束归档(必须调用,否则 zip 文件无法完成)
zip.finalize();
});
};
export const checkFileExistsSync = (filePath: string) => {
try {
// 使用 F_OK 检查文件或目录是否存在
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
};

View File

@@ -1,102 +0,0 @@
type HTMLOptions = {
title?: string;
rootId: string;
dataKey?: string;
};
/**
* data list
* @param opts
* @returns
*/
export const getHTML = (opts: HTMLOptions) => {
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${opts.title || 'Kevisual'}</title>
<style>
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
font-size: 16px;
}
</style>
<script src="https://kevisual.xiongxiao.me/system/lib/app.js"></script>
</head>
<body>
<div id="root"></div>
<script type="module">
import { Container } from 'https://kevisual.xiongxiao.me/root/container/index.js'
import { data } from './${opts.dataKey || 'data'}.js'
const container = new Container({
root: 'root',
data: data
});
container.render('${opts.rootId}');
</script>
</body>
</html>`;
};
export const getDataJs = (result: any) => {
return 'export const data=' + JSON.stringify(result);
};
type OneHTMLOptions = {
title?: string;
file: string;
}
/**
* one data
* @param opts
* @returns
*/
export const getOneHTML = (opts: OneHTMLOptions) => {
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="https://envision.xiongxiao.me/resources/root/avatar.png"/>
<title>${opts.title || 'Kevisual'}</title>
<style>
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
font-size: 16px;
}
</style>
<script src="https://kevisual.xiongxiao.me/system/lib/app.js"></script>
</head>
<body>
<div id="root"></div>
<script type="module">
import { ContainerOne } from 'https://kevisual.xiongxiao.me/system/lib/container.js'
import { render, unmount } from './${opts.file}.js'
const container = new ContainerOne({
root: '#root',
});
container.renderOne({
code: {render, unmount}
});
</script>
</body>
</html>`;
};

View File

@@ -1,143 +0,0 @@
// import { RenderData } from '@abearxiong/container';
type RenderData = any;
type Page = {
data: {
edges: { id: string; source: string; target: string }[];
nodes: { id: string; type: string; position: { x: number; y: number }; data: any }[];
};
id: string;
type: string;
[key: string]: any;
};
type Container = {
code: string;
id: string;
[key: string]: any;
};
type PageEditData = {
page: Page;
containerList: Container[];
};
export const getContainerData = (pageEditData: any) => {
const { page, containerList } = pageEditData;
const containerObj = containerList.reduce((acc, container) => {
acc[container.id] = container;
return acc;
}, {});
const { edges, nodes } = page.data;
const nodesObj = nodes.reduce((acc, node) => {
acc[node.id] = node;
return acc;
}, {});
const treeArray = getTreeFromEdges(edges);
const floatNodes = nodes.filter((node) => !treeArray.find((item) => item.id === node.id));
const treeNodes = nodes.filter((node) => treeArray.find((item) => item.id === node.id));
const renderData: RenderData[] = [];
for (let tree of treeArray) {
const node = nodesObj[tree.id];
const container = containerObj[node.data?.cid];
const style = node.data?.style ?? {
position: 'absolute',
width: 100,
height: 100,
};
const data = {
node: { ...node },
container: { ...container },
};
renderData.push({
id: node.id,
children: tree.children,
parents: tree.parents,
code: container?.code || '',
codeId: container?.id,
data: data || {},
className: node.data?.className,
shadowRoot: node.data?.shadowRoot,
showChild: node.data?.showChild,
style,
});
}
for (let node of floatNodes) {
const container = containerObj[node.data?.cid];
const style = node.data?.style ?? {
position: 'absolute',
width: 100,
height: 100,
};
const data = {
node: { ...node },
container: { ...container },
};
renderData.push({
id: node.id,
children: [],
parents: [],
code: container?.code || '',
codeId: container?.id,
data: data || {},
className: node.data?.className,
shadowRoot: node.data?.shadowRoot,
showChild: node.data?.showChild,
style,
});
}
return renderData;
};
const getTreeFromEdges = (
edges: { id: string; source: string; target: string }[],
): {
id: string;
parents: string[];
children: string[];
}[] => {
// 构建树形结构
function buildNodeTree(edges) {
const nodeMap = {};
// 初始化每个节点的子节点列表和父节点列表
edges.forEach((edge) => {
if (!nodeMap[edge.source]) {
nodeMap[edge.source] = { id: edge.source, parents: [], children: [] };
}
if (!nodeMap[edge.target]) {
nodeMap[edge.target] = { id: edge.target, parents: [], children: [] };
}
// 连接父节点和子节点
nodeMap[edge.source].children.push(nodeMap[edge.target]);
nodeMap[edge.target].parents.push(nodeMap[edge.source]);
});
return nodeMap;
}
const nodeTree = buildNodeTree(edges);
// 递归获取所有父节点,按顺序
function getAllParents(node) {
const parents: string[] = [];
function traverseParents(currentNode) {
if (currentNode.parents.length > 0) {
currentNode.parents.forEach((parent: any) => {
parents.push(parent.id);
traverseParents(parent);
});
}
}
traverseParents(node);
return parents.reverse(); // 确保顺序从最顶层到直接父节点
}
function getNodeInfo(nodeMap) {
return Object.values(nodeMap).map((node: any) => ({
id: node.id,
parents: getAllParents(node),
children: node.children.map((child) => child.id),
}));
}
const result = getNodeInfo(nodeTree);
return result;
};

View File

@@ -1,89 +0,0 @@
import { CustomError } from '@kevisual/router';
import { app } from '../../app.ts';
import { PageModel } from './models/index.ts';
import { AppListModel, AppModel } from '../../routes/app-manager/index.ts';
import { cachePage, getZip } from './module/cache-file.ts';
import { uniqBy } from 'lodash-es';
import semver from 'semver';
app
.route({
path: 'page',
key: 'publish',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { data, id, publish, ...rest } = ctx.query.data;
let needUpdate = { ...rest };
if (data) {
needUpdate = { ...needUpdate, data };
}
if (publish) {
needUpdate = { ...needUpdate, publish };
}
if (!id) {
throw new CustomError('id is required');
}
const page = await PageModel.findByPk(id);
if (!page) {
throw new CustomError('page not found');
}
await page.update(needUpdate);
const _publish = page.publish || {};
if (!_publish.key) {
throw new CustomError('publish key is required');
}
try {
const { key, description } = _publish;
const version = _publish.version || '0.0.0';
let app = await AppModel.findOne({ where: { key, uid: tokenUser.id } });
if (!app) {
app = await AppModel.create({ title: key, key, uid: tokenUser.id, description, user: tokenUser.username });
}
const _version = semver.inc(version, 'patch');
let appList = await AppListModel.findOne({ where: { key, version: _version, uid: tokenUser.id } });
if (!appList) {
appList = await AppListModel.create({ key, version: _version, uid: tokenUser.id, data: {} });
}
// 上传文件
const res = await cachePage(page, { tokenUser, key, version: _version });
const appFiles = appList?.data?.files || [];
const newFiles = uniqBy([...appFiles, ...res], 'name');
appList.data = {
...appList?.data,
files: newFiles,
};
await appList.save();
await page.update({ publish: { ..._publish, id: app.id, version: _version } });
ctx.body = page;
} catch (e) {
console.log('error', e);
throw new CustomError(e.message || 'publish error');
}
})
.addTo(app);
app
.route({
path: 'page',
key: 'download',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query;
const page = await PageModel.findByPk(id);
if (!page) {
throw new CustomError('page not found');
}
try {
const files = await getZip(page, { tokenUser });
ctx.body = files;
} catch (e) {
console.log('error', e);
throw new CustomError(e.message || 'download error');
}
})
.addTo(app);

View File

@@ -0,0 +1 @@
export const flowme_insert = 'flowme_insert'

View File

@@ -0,0 +1,26 @@
import { db } from '@/modules/db.ts'
// 创建触发器函数和触发器,用于在 flowme 表插入新记录时发送通知
const sql = `CREATE OR REPLACE FUNCTION notify_flowme_insert()
RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('flowme_insert', row_to_json(NEW)::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER flowme_after_insert
AFTER INSERT ON flowme
FOR EACH ROW
EXECUTE FUNCTION notify_flowme_insert();
`;
if (import.meta.main) {
const result = await db.execute(sql)
console.log('✅ flowme 插入触发器已创建或更新:', result)
}
// const listFunSql = `SELECT proname FROM pg_proc WHERE proname = 'flowme_after_insert';`
// const funExists = await db.execute(listFunSql)
// console.log('函数是否存在:', funExists)

View File

@@ -0,0 +1,3 @@
export * from './listener.ts'
export * from './common.ts'

View File

@@ -0,0 +1,33 @@
import { Client } from 'pg'
import { useConfig } from '@kevisual/use-config'
import { EventEmitter } from 'eventemitter3'
const config = useConfig()
let pgClient: Client | null = null
export const emitter = new EventEmitter()
async function startFlowmeListener() {
// 使用独立的数据库连接来监听
pgClient = new Client({
connectionString: config.DATABASE_URL || 'postgresql://postgres:postgres@localhost:5432/code_center',
})
console.log('config.DATABASE_URL =', config.DATABASE_URL)
await pgClient.connect()
console.log('🔌 已连接到 PostgreSQL 监听器')
// 订阅通知事件
pgClient.on('notification', (data) => {
if (!data.payload) return
try {
const parsed = JSON.parse(data.payload)
emitter.emit('flowme_insert', parsed)
} catch (err) {
console.error('❌ 解析 flowme 通知失败:', err)
}
})
// 执行 LISTEN 命令订阅通道
await pgClient.query('LISTEN flowme_insert')
console.log('👂 开始监听 flowme_insert 通道...')
}
startFlowmeListener();

View File

@@ -26,12 +26,14 @@ export const addAuth = (app: App) => {
return;
}
if (!token) {
app.throw(401, 'Token is required');
ctx.throw(401, 'Token is required');
}
const user = await User.getOauthUser(token);
if (!user) {
app.throw(401, 'Token is invalid');
ctx.throw(401, 'Token is invalid');
return;
}
// console.log(`auth user: ${user.username} (${user.id})`);
const someInfo = getSomeInfoFromReq(ctx);
if (someInfo.isBrowser && !ctx.req?.cookies?.['token']) {
createCookie({ accessToken: token }, ctx);
@@ -84,6 +86,7 @@ app
if (!tokenUser) {
ctx.throw(401, 'No User For authorized');
}
console.log('auth-admin tokenUser', ctx.state);
if (typeof ctx.state.isAdmin !== 'undefined' && ctx.state.isAdmin === true) {
return;
}
@@ -111,6 +114,7 @@ app
}
})
.addTo(app);
app
.route({
path: 'auth-check',

View File

@@ -1,170 +0,0 @@
import Busboy from 'busboy';
import { checkAuth } from '../middleware/auth.ts';
import { router, clients, writeEvents } from '../router.ts';
import { error } from '../middleware/auth.ts';
import fs from 'fs';
import { useFileStore } from '@kevisual/use-config';
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; // 如果响应已发送,不再处理
res.writeHead(200, { 'Content-Type': 'application/json' });
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return;
// 使用 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;
});
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 = {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
writeEvents(req, data);
}
});
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 = () => {
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)) {
appKey = _appKey?.[0];
} else {
appKey = _appKey;
}
if (Array.isArray(_collecion)) {
collection = _collecion?.[0];
} else {
collection = _collecion;
}
collection = parseIfJson(collection);
appKey = appKey || 'micro-app';
console.log('Appkey', appKey);
console.log('collection', collection);
// 处理上传的文件
const uploadResults = [];
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const minioPath = `private/${tokenUser.username}/${appKey}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
'Content-Type': getContentType(relativePath),
'app-source': 'user-micro-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
});
uploadResults.push({
name: relativePath,
path: minioPath,
hash: file.hash,
size: file.size,
});
fs.unlinkSync(tempPath); // 删除临时文件
// 受控
const r = await app.call({
path: 'micro-app',
key: 'upload',
payload: {
token: token,
data: {
appKey,
collection,
files: uploadResults,
},
},
});
const data: any = {
code: r.code,
data: r.body,
};
if (r.message) {
data.message = r.message;
}
res.end(JSON.stringify(data));
});
pipeBusboy(req, res, busboy);
});
function parseIfJson(collection: any): any {
try {
return JSON.parse(collection);
} catch (e) {
return collection;
}
}

View File

@@ -1,49 +0,0 @@
import { router, error, checkAuth, clients, getTaskId, writeEvents, deleteOldClients } from './router.ts';
router.get('/api/events', async (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
const taskId = getTaskId(req);
if (!taskId) {
res.end(error('task-id is required'));
return;
}
// 将客户端连接推送到 clients 数组
clients.set(taskId, { client: res, createTime: Date.now() });
// 移除客户端连接
req.on('close', () => {
clients.delete(taskId);
});
});
router.get('/api/s1/events', async (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
const taskId = getTaskId(req);
if (!taskId) {
res.end(error('task-id is required'));
return;
}
// 将客户端连接推送到 clients 数组
clients.set(taskId, { client: res, createTime: Date.now() });
writeEvents(req, { progress: 0, message: 'start' });
// 不自动关闭连接
// res.end('ok');
});
router.get('/api/s1/events/close', async (req, res) => {
const taskId = getTaskId(req);
if (!taskId) {
res.end(error('task-id is required'));
return;
}
deleteOldClients();
clients.delete(taskId);
res.end('ok');
});

View File

@@ -1,237 +1,10 @@
import { useFileStore } from '@kevisual/use-config';
import http from 'node:http';
import fs from 'fs';
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 { getContainerById } from '@/old-apps/container/module/get-container-file.ts';
import { router, error, checkAuth, writeEvents } from './router.ts';
import { router } 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) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Upload API is ready');
});
router.post('/api/app/upload', async (req, res) => {
if (res.headersSent) return; // 如果响应已发送,不再处理
res.writeHead(200, { 'Content-Type': 'application/json' });
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return;
// 使用 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;
});
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 = {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
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;
}
const clearFiles = () => {
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 = '';
const { appKey: _appKey, version: _version, username: _username } = fields;
if (Array.isArray(_appKey)) {
appKey = _appKey?.[0];
} else {
appKey = _appKey;
}
if (Array.isArray(_version)) {
version = _version?.[0];
} else {
version = _version;
}
if (Array.isArray(_username)) {
username = _username?.[0];
} else if (_username) {
username = _username;
}
if (username) {
const user = await User.getUserByToken(token);
const has = await user.hasUser(username, true);
if (!has) {
res.end(error('username is not found'));
clearFiles();
return;
}
}
if (!appKey) {
res.end(error('appKey is required'));
clearFiles();
return;
}
if (!version) {
res.end(error('version is required'));
clearFiles();
return;
}
console.log('Appkey', appKey, version);
// 逐个处理每个上传的文件
const uploadResults = [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const minioPath = `${username || tokenUser.username}/${appKey}/${version}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
'Content-Type': getContentType(relativePath),
'app-source': 'user-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
});
uploadResults.push({
name: relativePath,
path: minioPath,
});
fs.unlinkSync(tempPath); // 删除临时文件
}
// 受控
const r = await app.call({
path: 'app',
key: 'uploadFiles',
payload: {
token: token,
data: {
appKey,
version,
username,
files: uploadResults,
},
},
});
const data: any = {
code: r.code,
data: r.body,
};
if (r.message) {
data.message = r.message;
}
res.end(JSON.stringify(data));
});
pipeBusboy(req, res, busboy);
});
router.get('/api/container/file/:id', async (req, res) => {
const id = req.params.id;
if (!id) {
res.end(error('id is required'));
return;
}
const container = await getContainerById(id);
if (container.id) {
const code = container.code;
res.writeHead(200, {
'Content-Type': 'application/javascript; charset=utf-8',
'container-id': container.id,
});
res.end(code);
} else {
res.end(error('Container not found'));
}
res.writeHead(200, {
'Content-Type': 'application/json',
});
res.end(JSON.stringify(container));
});
router.all('/api/nocodb-test/router', async (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
const param = await router.getSearch(req);
const body = await router.getBody(req);
const contentType = req.headers['content-type'] || '';
console.log('Content-Type:', contentType);
console.log('NocoDB test router called.', req.method, param, JSON.stringify(body, null));
res.end(JSON.stringify({ message: 'NocoDB test router is working' }));
});
const simpleAppsPrefixs = [
"/api/app/",
"/api/micro-app/",
"/api/events",
"/api/s1/",
"/api/container/",
"/api/resource/",
"/api/wxmsg",
"/api/nocodb-test/"
"/api/wxmsg"
];

View File

@@ -1,6 +0,0 @@
// import './code/upload.ts';
import './event.ts';
import './resources/upload.ts';
import './resources/chunk.ts';
// import './resources/get-resources.ts';

View File

@@ -1,61 +0,0 @@
import { User } from '@/models/user.ts';
import http from 'http';
import { parse } from '@kevisual/router/src/server/cookie.ts';
export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
};
export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
const url = new URL(req.url || '', 'http://localhost');
const resNoPermission = () => {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
};
if (!token) {
token = url.searchParams.get('token') || '';
}
if (!token) {
const parsedCookies = parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
if (!token) {
return resNoPermission();
}
if (token) {
token = token.replace('Bearer ', '');
}
let tokenUser;
try {
tokenUser = await User.verifyToken(token);
} catch (e) {
console.log('checkAuth error', e);
res.statusCode = 401;
res.end(error('Invalid token'));
return { tokenUser: null, token: null };
}
return { tokenUser, token };
};
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');
if (!token) {
token = url.searchParams.get('token') || '';
}
if (!token) {
const parsedCookies = parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
if (token) {
token = token.replace('Bearer ', '');
}
let tokenUser;
try {
tokenUser = await User.verifyToken(token);
return { tokenUser, token };
} catch (e) {
return null;
}
};

View File

@@ -1 +0,0 @@
export * from './auth.ts'

View File

@@ -1,18 +0,0 @@
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { router } from '../router.ts';
router.post('/api/minio', async (ctx) => {
let { username, appKey } = { username: '', appKey: '' };
const path = `${username}/${appKey}`;
const res = await minioClient.listObjectsV2(bucketName, path, true);
const file = res.filter((item) => item.isFile);
const fileList = file.map((item) => {
return {
name: item.name,
size: item.size,
};
});
// ctx.body = fileList;
});

View File

@@ -1,107 +0,0 @@
/**
* 更新时间2025-03-17
* 第二次更新2025-03-22
*/
import { minioClient } from '@/app.ts';
import { IncomingMessage, ServerResponse } from 'http';
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
* 并返回过滤后的 metaData
* @param metaData
* @returns
*/
const filterKeys = (metaData: Record<string, string>, clearKeys: string[] = []) => {
const keys = Object.keys(metaData);
// remove X-Amz- meta data
const removeKeys = ['password', 'accesskey', 'secretkey', ...clearKeys];
const filteredKeys = keys.filter((key) => !removeKeys.includes(key));
return filteredKeys.reduce((acc, key) => {
acc[key] = metaData[key];
return acc;
}, {} as Record<string, string>);
};
export const NotFoundFile = (res: ServerResponse, msg?: string, code = 404) => {
res.writeHead(code, { 'Content-Type': 'text/plain' });
res.end(msg || 'Not Found File');
return;
};
export const shareType = ['public', 'private', 'protected'] as const;
export type ShareType = (typeof shareType)[number];
export const authMinio = async (req: IncomingMessage, res: ServerResponse, objectName: string) => {
let stat: BucketItemStat;
try {
stat = await minioClient.statObject(bucketName, objectName);
} catch (e) {
return NotFoundFile(res);
}
const [userKey, ...rest] = objectName.split('/');
const _url = new URL(req.url || '', 'http://localhost');
const password = _url.searchParams.get('p') || '';
const isDownload = !!_url.searchParams.get('download');
const metaData = stat.metaData || {};
const filteredMetaData = filterKeys(metaData, ['size', 'etag', 'last-modified']);
if (stat.size === 0) {
return NotFoundFile(res);
}
const { tokenUser } = await getLoginUser(req);
const username = tokenUser?.username;
const owner = userKey;
const permission = new UserPermission({
permission: metaData as Permission,
owner,
});
const checkPermissionResult = permission.checkPermissionSuccess({
username,
password,
});
if (!checkPermissionResult.success) {
return NotFoundFile(res, checkPermissionResult.message, checkPermissionResult.code);
}
const contentLength = stat.size;
const etag = stat.etag;
const lastModified = stat.lastModified.toISOString();
const filename = objectName.split('/').pop() || 'no-file-name-download'; // Extract filename from objectName
const fileExtension = filename.split('.').pop()?.toLowerCase() || '';
const viewableExtensions = [
'jpg',
'jpeg',
'png',
'gif',
'svg',
'webp',
'mp4',
'webm',
'mp3',
'wav',
'ogg',
'pdf',
'doc',
'docx',
'xls',
'xlsx',
'ppt',
'pptx',
];
const contentDisposition = viewableExtensions.includes(fileExtension) && !isDownload ? 'inline' : `attachment; filename="${filename}"`;
res.writeHead(200, {
'Content-Length': contentLength,
etag,
'last-modified': lastModified,
'Content-Disposition': contentDisposition,
...filteredMetaData,
});
const objectStream = await minioClient.getObject(bucketName, objectName);
// objectStream.pipe(res, { end: true });
pipeMinioStream(objectStream, res);
};

View File

@@ -13,6 +13,7 @@ import { getLoginUser } from '../modules/auth.ts';
import { rediretHome } from '../modules/user-app/index.ts';
import { logger } from '../modules/logger.ts';
import { UserV1Proxy } from '../modules/ws-proxy/proxy.ts';
import { UserV3Proxy } from '@/modules/v3/index.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';
@@ -145,7 +146,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
let user, app;
let domainApp = false;
const isDev = isLocalhost(dns.hostName);
const isDev = isLocalhost(dns?.hostName);
if (isDev) {
console.debug('开发环境访问:', req.url, 'Host:', dns.hostName);
} else {
@@ -194,8 +195,8 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
if (!domainApp) {
// 原始url地址
const urls = url.split('/');
const [_, _user, _app] = urls;
if (urls.length < 3) {
const [_, _user] = urls;
if (_user === 'robots.txt') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(robotsTxt);
@@ -212,8 +213,12 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
forBadUser(req, res);
}
return res.end();
} else {
if (userPathIsBanned(_user) || userPathIsBanned(_app)) {
logger.warn(`Bad user access from IP: ${dns.ip}, Host: ${dns.hostName}, URL: ${req.url}`);
return forBadUser(req, res);
}
}
const [_, _user, _app] = urls;
if (_app && urls.length === 3) {
// 重定向到
res.writeHead(302, { Location: `${url}/` });
@@ -250,6 +255,11 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
createNotFoundPage,
});
}
if (user !== 'api' && app === 'v3') {
return UserV3Proxy(req, res, {
createNotFoundPage,
});
}
const userApp = new UserApp({ user, app });
let isExist = await userApp.getExist();
@@ -288,6 +298,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
username: loginUser?.tokenUser?.username || '',
password: password,
});
console.log('checkPermission', checkPermission, 'loginUser:', loginUser, password)
if (!checkPermission.success) {
return createNotFoundPage('no permission');
}

View File

@@ -1,236 +0,0 @@
import { useFileStore } from '@kevisual/use-config';
import { checkAuth, error, router, writeEvents, getKey, getTaskId } from '../router.ts';
import Busboy from 'busboy';
import { app, oss } from '@/app.ts';
import { getContentType } from '@/utils/get-content-type.ts';
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 });
router.get('/api/s1/resources/upload/chunk', async (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Upload API is ready');
});
// /api/s1/resources/upload
router.post('/api/s1/resources/upload/chunk', async (req, res) => {
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) return;
const url = new URL(req.url || '', 'http://localhost');
const share = !!url.searchParams.get('public');
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
const taskId = getTaskId(req);
const finalFilePath = `${cacheFilePath}/${taskId}`;
if (!taskId) {
res.end(error('taskId is required'));
return;
}
// 使用 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 (tempPath && fs.existsSync(tempPath)) {
fs.unlinkSync(tempPath);
}
if (fs.existsSync(finalFilePath)) {
fs.unlinkSync(finalFilePath);
}
};
if (!file) {
res.end(error('No file uploaded'));
return;
}
// Handle chunked upload logic here
let { chunkIndex, totalChunks, appKey, version, username, directory } = getKey(fields, [
'chunkIndex',
'totalChunks',
'appKey',
'version',
'username',
'directory',
]);
if (!chunkIndex || !totalChunks) {
res.end(error('chunkIndex, totalChunks is required'));
clearFiles();
return;
}
const relativePath = file.originalFilename;
const writeStream = fs.createWriteStream(finalFilePath, { flags: 'a' });
const readStream = fs.createReadStream(tempPath);
readStream.pipe(writeStream);
writeStream.on('finish', async () => {
fs.unlinkSync(tempPath); // 删除临时文件
// Write event for progress tracking
const progress = ((parseInt(chunkIndex) + 1) / parseInt(totalChunks)) * 100;
writeEvents(req, {
progress,
message: `Upload progress: ${progress}%`,
});
if (parseInt(chunkIndex) + 1 === parseInt(totalChunks)) {
let uid = tokenUser.id;
if (username) {
const user = await User.getUserByToken(token);
const has = await user.hasUser(username, true);
if (!has) {
res.end(error('username is not found'));
clearFiles();
return;
}
const _user = await User.findOne({ where: { username } });
uid = _user?.id || '';
}
if (!appKey || !version) {
const config = await ConfigModel.getUploadConfig({ uid });
if (config) {
appKey = config.config?.data?.key || '';
version = config.config?.data?.version || '';
}
}
if (!appKey || !version) {
res.end(error('appKey or version is not found, please check the upload config.'));
clearFiles();
return;
}
const { code, message } = validateDirectory(directory);
if (code !== 200) {
res.end(error(message));
clearFiles();
return;
}
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
const metadata: any = {};
if (share) {
metadata.share = 'public';
}
const bucketName = oss.bucketName;
// All chunks uploaded, now upload to MinIO
await oss.client.fPutObject(bucketName, minioPath, finalFilePath, {
'Content-Type': getContentType(relativePath),
'app-source': 'user-app',
'Cache-Control': relativePath.endsWith('.html') ? 'no-cache' : 'max-age=31536000, immutable',
...metadata,
});
// Clean up the final file
fs.unlinkSync(finalFilePath);
const downloadBase = '/api/s1/share';
const uploadResult = {
name: relativePath,
path: `${downloadBase}/${minioPath}`,
appKey,
version,
username,
};
if (!noCheckAppFiles) {
// Notify the app
const r = await app.call({
path: 'app',
key: 'detectVersionList',
payload: {
token: token,
data: {
appKey,
version,
username,
},
},
});
const data: any = {
code: r.code,
data: {
app: r.body,
upload: [uploadResult],
},
};
if (r.message) {
data.message = r.message;
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
code: 200,
message: 'Chunk uploaded successfully',
data: { chunkIndex, totalChunks, upload: [uploadResult] },
}),
);
}
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
code: 200,
message: 'Chunk uploaded successfully',
data: {
chunkIndex,
totalChunks,
},
}),
);
}
});
});
pipeBusboy(req, res, busboy);
});

View File

@@ -1,19 +0,0 @@
import { router } from '@/app.ts';
import { authMinio } from '../minio/get-minio-resource.ts';
// 功能可以抽离为某一个dns请求的服务
router.all('/api/s1/share/*splat', async (req, res) => {
try {
const url = req.url;
const _url = new URL(url || '', 'http://localhost');
let objectName = _url.pathname.replace('/api/s1/share/', '');
objectName = decodeURIComponent(objectName);
await authMinio(req, res, objectName);
} catch (e) {
console.log('get share resource error url', req.url);
console.error('get share resource is error.', e.message);
res.end('get share resource is error.');
}
});

View File

@@ -1,288 +0,0 @@
import { useFileStore } from '@kevisual/use-config';
import { checkAuth, error, router, writeEvents, getKey } from '../router.ts';
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';
import { getFileStat } from '@/routes/file/index.ts';
import { logger } from '@/modules/logger.ts';
const cacheFilePath = useFileStore('cache-file', { needExists: true });
router.get('/api/s1/resources/upload', async (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Upload API is ready');
});
export const parseIfJson = (data = '{}') => {
try {
const _data = JSON.parse(data);
if (typeof _data === 'object') return _data;
return {};
} catch (error) {
return {};
}
};
router.post('/api/s1/resources/upload/check', async (req, res) => {
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) {
res.end(error('Token is invalid.'));
return;
}
console.log('data', req.url);
res.writeHead(200, { 'Content-Type': 'application/json' });
const data = await router.getBody(req);
type Data = {
appKey: string;
version: string;
username: string;
directory: string;
files: { path: string; hash: string }[];
};
let { appKey, version, username, directory, files } = pick(data, ['appKey', 'version', 'username', 'directory', 'files']) as Data;
let uid = tokenUser.id;
if (username) {
const user = await User.getUserByToken(token);
const has = await user.hasUser(username, true);
if (!has) {
res.end(error('username is not found'));
return;
}
const _user = await User.findOne({ where: { username } });
uid = _user?.id || '';
}
if (!appKey || !version) {
res.end(error('appKey and version is required'));
}
const { code, message } = validateDirectory(directory);
if (code !== 200) {
res.end(error(message));
return;
}
type CheckResult = {
path: string;
stat: any;
resourcePath: string;
hash: string;
uploadHash: string;
isUpload?: boolean;
};
const checkResult: CheckResult[] = [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
const relativePath = file.path;
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
let stat = await getFileStat(minioPath, true);
const statHash = stat?.etag || '';
checkResult.push({
path: relativePath,
uploadHash: file.hash,
resourcePath: minioPath,
isUpload: statHash === file.hash,
stat,
hash: statHash,
});
}
res.end(JSON.stringify({ code: 200, data: checkResult }));
});
// /api/s1/resources/upload
router.post('/api/s1/resources/upload', async (req, res) => {
const { tokenUser, token } = await checkAuth(req, res);
if (!tokenUser) {
res.end(error('Token is invalid.'));
return;
}
const url = new URL(req.url || '', 'http://localhost');
const share = !!url.searchParams.get('public');
const meta = parseIfJson(url.searchParams.get('meta'));
const noCheckAppFiles = !!url.searchParams.get('noCheckAppFiles');
// 使用 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;
});
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),
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);
});
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 = () => {
files.forEach((file) => {
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, directory, description } = getKey(fields, ['appKey', 'version', 'username', 'directory', 'description']);
let uid = tokenUser.id;
if (username) {
const user = await User.getUserByToken(token);
const has = await user.hasUser(username, true);
if (!has) {
res.end(error('username is not found'));
clearFiles();
return;
}
const _user = await User.findOne({ where: { username } });
uid = _user?.id || '';
}
if (!appKey || !version) {
const config = await ConfigModel.getUploadConfig({ uid });
if (config) {
appKey = config.config?.data?.key || '';
version = config.config?.data?.version || '';
}
}
if (!appKey || !version) {
res.end(error('appKey or version is not found, please check the upload config.'));
clearFiles();
return;
}
const { code, message } = validateDirectory(directory);
if (code !== 200) {
res.end(error(message));
clearFiles();
return;
}
// 逐个处理每个上传的文件
const uploadedFiles = files;
logger.info(
'upload files',
uploadedFiles.map((item) => {
return pick(item, ['filepath', 'originalFilename']);
}),
);
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
const minioPath = `${username || tokenUser.username}/${appKey}/${version}${directory ? `/${directory}` : ''}/${relativePath}`;
// 上传到 MinIO 并保留文件夹结构
const isHTML = relativePath.endsWith('.html');
const metadata: any = {};
if (share) {
metadata.share = 'public';
}
Object.assign(metadata, meta);
await minioClient.fPutObject(bucketName, minioPath, tempPath, {
'Content-Type': getContentType(relativePath),
'app-source': 'user-app',
'Cache-Control': isHTML ? 'no-cache' : 'max-age=31536000, immutable', // 缓存一年
...metadata,
});
uploadResults.push({
name: relativePath,
path: minioPath,
});
fs.unlinkSync(tempPath); // 删除临时文件
}
if (!noCheckAppFiles) {
const _data = { appKey, version, username, files: uploadResults, description, }
if (_data.description) {
delete _data.description;
}
// 受控
const r = await app.call({
path: 'app',
key: 'uploadFiles',
payload: {
token: token,
data: _data,
},
});
const data: any = {
code: r.code,
data: {
app: r.body,
upload: uploadResults,
},
};
if (r.message) {
data.message = r.message;
}
console.log('upload data', data);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
} else {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
code: 200,
data: {
detect: [],
upload: uploadResults,
},
}),
);
}
});
pipeBusboy(req, res, busboy);
});

View File

@@ -1,30 +0,0 @@
/**
* 校验directory是否合法, 合法返回200, 不合法返回500
*
* directory 不能以/开头,不能以/结尾。不能以.开头,不能以.结尾。
* 把directory的/替换掉后,只能包含数字、字母、下划线、中划线
* @param directory 目录
* @returns
*/
export const validateDirectory = (directory?: string) => {
// 对directory进行校验不能以/开头,不能以/结尾。不能以.开头,不能以.结尾。
if (directory && (directory.startsWith('/') || directory.endsWith('/') || directory.startsWith('..') || directory.endsWith('..'))) {
return {
code: 500,
message: 'directory is invalid',
};
}
// 把directory的/替换掉后,只能包含数字、字母、下划线、中划线
// 可以包含.
let _directory = directory?.replace(/\//g, '');
if (_directory && !/^[a-zA-Z0-9_.-]+$/.test(_directory)) {
return {
code: 500,
message: 'directory is invalid, only number, letter, underline and hyphen are allowed',
};
}
return {
code: 200,
message: 'directory is valid',
};
};

View File

@@ -1,14 +1,13 @@
import { router } from '@/app.ts';
import http from 'http';
import { useContextKey } from '@kevisual/context';
import { checkAuth, error } from './middleware/auth.ts';
export { router, checkAuth, error };
export { router, };
/**
* 事件客户端
*/
const eventClientsInit = () => {
const clients = new Map<string, { client?: http.ServerResponse; createTime?: number; [key: string]: any }>();
const clients = new Map<string, { client?: http.ServerResponse; createTime?: number;[key: string]: any }>();
return clients;
};
export const clients = useContextKey('event-clients', () => eventClientsInit());

View File

@@ -1,11 +1,12 @@
import { App, CustomError } from '@kevisual/router';
import { AppModel, AppListModel } from './module/index.ts';
import { app, redis } from '@/app.ts';
import { uniqBy } from 'lodash-es';
import { uniqBy } from 'es-toolkit';
import { getUidByUsername, prefixFix } from './util.ts';
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
import { setExpire } from './revoke.ts';
import { User } from '@/models/user.ts';
import { callDetectAppVersion } from './export.ts';
app
.route({
path: 'app',
@@ -40,9 +41,10 @@ app
description: '获取应用详情可以通过id或者key+version来获取',
})
.define(async (ctx) => {
console.log('get app manager called');
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
const { key, version } = ctx.query?.data || {};
const { key, version, create = false } = ctx.query?.data || {};
if (!id && (!key || !version)) {
throw new CustomError('id is required');
}
@@ -58,9 +60,29 @@ app
},
});
}
if (!am) {
throw new CustomError('app not found');
if (!am && create) {
am = await AppListModel.create({
key,
version,
uid: tokenUser.id,
data: {},
});
const res = await app.run({ path: 'app', key: "detectVersionList", payload: { data: { appKey: key, version, username: tokenUser.username }, token: ctx.query.token } });
if (res.code !== 200) {
ctx.throw(res.message || 'detect version list error');
}
am = await AppListModel.findOne({
where: {
key,
version,
uid: tokenUser.id,
},
});
}
if (!am) {
ctx.throw('app not found');
}
console.log('get app', am.id, am.key, am.version);
ctx.body = prefixFix(am, tokenUser.username);
})
.addTo(app);
@@ -214,7 +236,7 @@ app
});
}
const dataFiles = app.data.files || [];
const newFiles = uniqBy([...dataFiles, ...files], 'name');
const newFiles = uniqBy([...dataFiles, ...files], (item) => item.name);
const res = await app.update({ data: { ...app.data, files: newFiles } });
if (version === am.version && !appIsNew) {
await am.update({ data: { ...am.data, files: newFiles } });
@@ -237,7 +259,7 @@ app
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id, username, appKey, version } = ctx.query.data;
const { id, username, appKey, version, detect } = ctx.query.data;
if (!id && !appKey) {
throw new CustomError('id or appKey is required');
}
@@ -247,22 +269,33 @@ app
if (id) {
appList = await AppListModel.findByPk(id);
if (appList?.uid !== uid) {
throw new CustomError('no permission');
ctx.throw('no permission');
}
}
if (!appList && appKey) {
if (!version) {
throw new CustomError('version is required');
ctx.throw('version is required');
}
appList = await AppListModel.findOne({ where: { key: appKey, version, uid } });
}
if (!appList) {
throw new CustomError('app not found');
ctx.throw('app 未发现');
}
if (detect) {
const appKey = appList.key;
const version = appList.version;
// 自动检测最新版本
const res = await callDetectAppVersion({ appKey, version, username: username || tokenUser.username }, ctx.query.token);
if (res.code !== 200) {
ctx.throw(res.message || '检测版本列表失败');
}
appList = await AppListModel.findByPk(appList.id);
}
const files = appList.data.files || [];
const am = await AppModel.findOne({ where: { key: appList.key, uid: uid } });
if (!am) {
throw new CustomError('app not found');
ctx.throw('app 未发现');
}
await am.update({ data: { ...am.data, files }, version: appList.version });
setExpire(appList.key, am.user);
@@ -364,7 +397,7 @@ app
am = await AppModel.create({
title: appKey,
key: appKey,
version: version || '0.0.0',
version: version || '0.0.1',
user: checkUsername,
uid,
data: { files: needAddFiles },

View File

@@ -3,7 +3,6 @@ import { app, redis } from '@/app.ts';
import fs from 'fs';
import { fileStore } from '@/modules/config.ts';
import { getAppLoadStatus } from '@/modules/user-app/index.ts';
import { getLoginUser } from '@/modules/auth.ts';
export class CenterUserApp {
user: string;
@@ -55,25 +54,25 @@ export class CenterUserApp {
deleteUserAppFiles(user, app);
}
}
app
.route({
path: 'page-proxy-app',
key: 'auth-admin',
id: 'auth-admin',
})
.define(async (ctx) => {
const { user } = ctx.query;
const loginUser = await getLoginUser(ctx.req);
if (loginUser) {
const root = ['admin', 'root'];
if (root.includes(loginUser.tokenUser?.username)) {
return;
}
ctx.throw(401, 'No Proxy App Permission');
}
ctx.throw(401, 'No Login And No Proxy App Permission');
})
.addTo(app);
// app
// .route({
// path: 'page-proxy-app',
// key: 'auth-admin',
// id: 'auth-admin',
// })
// .define(async (ctx) => {
// const { user } = ctx.query;
// const loginUser = await getLoginUser(ctx.req);
// if (loginUser) {
// const root = ['admin', 'root'];
// if (root.includes(loginUser.tokenUser?.username)) {
// return;
// }
// ctx.throw(401, 'No Proxy App Permission');
// }
// ctx.throw(401, 'No Login And No Proxy App Permission');
// })
// .addTo(app);
app
.route({
@@ -81,7 +80,6 @@ app
key: 'list',
middleware: ['auth-admin'],
description: '获取应用列表',
isDebug: true,
})
.define(async (ctx) => {
const keys = await redis.keys('user:app:*');
@@ -101,6 +99,7 @@ app
path: 'page-proxy-app',
key: 'delete',
middleware: ['auth-admin'],
description: '删除应用缓存',
})
.define(async (ctx) => {
const { user, app } = ctx.query;
@@ -119,6 +118,8 @@ app
.route({
path: 'page-proxy-app',
key: 'deleteAll',
middleware: ['auth-admin'],
description: '删除所有应用缓存',
})
.define(async (ctx) => {
const keys = await redis.keys('user:app:*');
@@ -134,7 +135,9 @@ app
app
.route({
path: 'page-proxy-app',
description: '清理所有应用缓存',
key: 'clear',
middleware: ['auth-admin'],
})
.define(async (ctx) => {
const keys = await redis.keys('user:app:*');
@@ -153,6 +156,7 @@ app
.route({
path: 'page-proxy-app',
key: 'get',
description: '获取应用缓存信息',
middleware: ['auth-admin'],
})
.define(async (ctx) => {
@@ -178,6 +182,7 @@ app
.route({
path: 'page-proxy-app',
key: 'status',
description: '获取应用加载状态',
middleware: [],
})
.define(async (ctx) => {

View File

@@ -59,10 +59,7 @@ app
}
}
await config.update({
data: {
...config.data,
...data,
},
data: data,
...rest,
});
if (config.data?.permission?.share === 'public') {
@@ -80,7 +77,7 @@ app
});
if (config) {
await config.update({
data: { ...config.data, ...data },
data: data,
...rest,
});
ctx.body = config;
@@ -177,7 +174,7 @@ app
const search: any = id ? { id } : { key };
const config = await ConfigModel.findOne({
where: {
...search
...search
},
});
if (config && config.uid === tuid) {

View File

@@ -2,7 +2,6 @@ import { app } from '@/app.ts';
import { getFileStat, getMinioList, deleteFile, updateFileStat, deleteFiles } from './module/get-minio-list.ts';
import path from 'path';
import { CustomError } from '@kevisual/router';
import { get } from 'http';
import { callDetectAppVersion } from '../app-manager/export.ts';
/**

View File

@@ -1,7 +1,6 @@
import dayjs from 'dayjs';
import { minioClient } from '../../../modules/minio.ts';
import { bucketName } from '../../../modules/minio.ts';
import { BucketItemStat, CopyDestinationOptions, CopySourceOptions } from 'minio';
import { oss } from '@/modules/s3.ts';
import { StatObjectResult } from '@kevisual/oss';
type MinioListOpt = {
prefix: string;
recursive?: boolean;
@@ -20,33 +19,13 @@ export type MinioList = (MinioFile | MinioDirectory)[];
export const getMinioList = async <IS_FILE extends boolean>(opts: MinioListOpt): Promise<IS_FILE extends true ? MinioFile[] : MinioDirectory[]> => {
const prefix = opts.prefix;
const recursive = opts.recursive ?? false;
const res = await new Promise((resolve, reject) => {
let res: any[] = [];
let hasError = false;
minioClient
.listObjectsV2(bucketName, prefix, recursive)
.on('data', (data) => {
res.push(data);
})
.on('error', (err) => {
console.error('minio error', opts.prefix, err);
hasError = true;
})
.on('end', () => {
if (hasError) {
reject();
return;
} else {
resolve(res);
}
});
});
const res = await oss.listObjects(prefix, { recursive });
return res as IS_FILE extends true ? MinioFile[] : MinioDirectory[];
};
export const getFileStat = async (prefix: string, isFile?: boolean): Promise<BucketItemStat | null> => {
export const getFileStat = async (prefix: string, isFile?: boolean): Promise<StatObjectResult | null> => {
try {
const obj = await minioClient.statObject(bucketName, prefix);
if (isFile && obj.size === 0) {
const obj = await oss.statObject(prefix);
if (isFile && obj?.size === 0) {
return null;
}
return obj;
@@ -69,10 +48,7 @@ export const deleteFile = async (prefix: string): Promise<{ code: number; messag
message: 'file not found',
};
}
await minioClient.removeObject(bucketName, prefix, {
versionId: 'null',
forceDelete: true, // 强制删除
});
await oss.deleteObject(prefix);
return {
code: 200,
message: 'delete success',
@@ -89,7 +65,9 @@ export const deleteFile = async (prefix: string): Promise<{ code: number; messag
// 批量删除文件
export const deleteFiles = async (prefixs: string[]): Promise<any> => {
try {
await minioClient.removeObjects(bucketName, prefixs);
for (const prefix of prefixs) {
await oss.deleteObject(prefix);
}
return true;
} catch (e) {
console.error('delete Files Error not handle', e);
@@ -135,14 +113,9 @@ export const updateFileStat = async (
message?: string;
}> => {
try {
const source = new CopySourceOptions({ Bucket: bucketName, Object: prefix });
const destination = new CopyDestinationOptions({
Bucket: bucketName,
Object: prefix,
UserMetadata: newMetadata,
MetadataDirective: 'REPLACE',
const copyResult = await oss.replaceObject(prefix, {
...newMetadata
});
const copyResult = await minioClient.copyObject(source, destination);
console.log('copyResult', copyResult);
console.log(`Metadata for ${prefix} updated successfully.`);
return {
@@ -171,25 +144,16 @@ export const mvUserAToUserB = async (usernameA: string, usernameB: string, clear
const newPrefix = `${usernameB}/`;
const listSource = await getMinioList<true>({ prefix: oldPrefix, recursive: true });
for (const item of listSource) {
const source = new CopySourceOptions({ Bucket: bucketName, Object: item.name });
const stat = await getFileStat(item.name);
const newName = item.name.slice(oldPrefix.length);
// @ts-ignore
const metadata = stat?.userMetadata || stat.metaData;
const destination = new CopyDestinationOptions({
Bucket: bucketName,
Object: `${newPrefix}${newName}`,
UserMetadata: metadata,
MetadataDirective: 'COPY',
});
await minioClient.copyObject(source, destination);
await oss.copyObject(item.name, `${newPrefix}${newName}`);
}
if (clearOldUser) {
const files = await getMinioList<true>({ prefix: oldPrefix, recursive: true });
for (const file of files) {
await minioClient.removeObject(bucketName, file.name);
await oss.deleteObject(file.name);
}
}
console.log(`移动 ${usernameA} to ${usernameB} success`);
};
export const backupUserA = async (usernameA: string, id: string, backName?: string) => {
const today = backName || dayjs().format('YYYY-MM-DD-HH-mm');
@@ -202,11 +166,12 @@ export const backupUserA = async (usernameA: string, id: string, backName?: stri
for (const item of deleteBackup) {
const files = await getMinioList<true>({ prefix: item.prefix, recursive: true });
for (const file of files) {
await minioClient.removeObject(bucketName, file.name);
await oss.deleteObject(file.name);
}
}
}
await mvUserAToUserB(usernameA, backupPrefix, false);
console.log(`Backup user ${usernameA} to ${backupPrefix} success`);
};
/**
* 删除用户
@@ -215,6 +180,6 @@ export const backupUserA = async (usernameA: string, id: string, backName?: stri
export const deleteUser = async (username: string) => {
const list = await getMinioList<true>({ prefix: `${username}/`, recursive: true });
for (const item of list) {
await minioClient.removeObject(bucketName, item.name);
await oss.deleteObject(item.name);
}
};

View File

@@ -0,0 +1,144 @@
import { desc, eq, count, or, like, and } from 'drizzle-orm';
import { schema, app, db } from '@/app.ts'
// 获取 flowme-channel 列表
app.route({
path: 'flowme-channel',
key: 'list',
middleware: ['auth'],
description: '获取 flowme-channel 列表',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const uid = tokenUser.id;
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query || {};
const offset = (page - 1) * pageSize;
const orderByField = sort === 'ASC' ? schema.flowmeChannels.updatedAt : desc(schema.flowmeChannels.updatedAt);
let whereCondition = eq(schema.flowmeChannels.uid, uid);
if (search) {
whereCondition = and(
eq(schema.flowmeChannels.uid, uid),
or(
like(schema.flowmeChannels.title, `%${search}%`),
like(schema.flowmeChannels.description, `%${search}%`)
)
);
}
const [list, totalCount] = await Promise.all([
db.select()
.from(schema.flowmeChannels)
.where(whereCondition)
.limit(pageSize)
.offset(offset)
.orderBy(orderByField),
db.select({ count: count() })
.from(schema.flowmeChannels)
.where(whereCondition)
]);
ctx.body = {
list,
pagination: {
page,
current: page,
pageSize,
total: totalCount[0]?.count || 0,
},
};
return ctx;
}).addTo(app);
// 创建或更新 flowme-channel
const channelUpdate = `创建或更新一个 flowme-channel, 参数定义:
title: 标题, 必填
description: 描述, 选填
tags: 标签, 数组, 选填
link: 链接, 选填
data: 数据, 对象, 选填
color: 颜色, 选填, 默认 #007bff
`;
app.route({
path: 'flowme-channel',
key: 'update',
middleware: ['auth'],
description: channelUpdate,
}).define(async (ctx) => {
const { id, uid, updatedAt, createdAt, ...rest } = ctx.query.data || {};
const tokenUser = ctx.state.tokenUser;
let channel;
if (!id) {
channel = await db.insert(schema.flowmeChannels).values({
title: rest.title || '',
description: rest.description || '',
tags: rest.tags || [],
link: rest.link || '',
data: rest.data || {},
color: rest.color || '#007bff',
uid: tokenUser.id,
}).returning();
} else {
const existing = await db.select().from(schema.flowmeChannels).where(eq(schema.flowmeChannels.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的 channel');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限更新该 channel');
}
channel = await db.update(schema.flowmeChannels).set({
title: rest.title,
description: rest.description,
tags: rest.tags,
link: rest.link,
data: rest.data,
color: rest.color,
}).where(eq(schema.flowmeChannels.id, id)).returning();
}
ctx.body = channel;
}).addTo(app);
// 删除 flowme-channel
app.route({
path: 'flowme-channel',
key: 'delete',
middleware: ['auth'],
description: '删除 flowme-channel, 参数: data.id 必填',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id 参数缺失');
}
const existing = await db.select().from(schema.flowmeChannels).where(eq(schema.flowmeChannels.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的 channel');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限删除该 channel');
}
await db.delete(schema.flowmeChannels).where(eq(schema.flowmeChannels.id, id));
ctx.body = { success: true };
}).addTo(app);
// 获取单个 flowme-channel
app.route({
path: 'flowme-channel',
key: 'get',
middleware: ['auth'],
description: '获取单个 flowme-channel, 参数: data.id 必填',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id 参数缺失');
}
const existing = await db.select().from(schema.flowmeChannels).where(eq(schema.flowmeChannels.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的 channel');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限查看该 channel');
}
ctx.body = existing[0];
}).addTo(app);

View File

@@ -0,0 +1,5 @@
import './list.ts'
// flowme channel 相关路由
import './flowme-channel/list.ts'

160
src/routes/flowme/list.ts Normal file
View File

@@ -0,0 +1,160 @@
import { desc, eq, count, or, like, and } from 'drizzle-orm';
import { schema, app, db } from '@/app.ts'
// 获取 flowme 列表
app.route({
path: 'flowme',
key: 'list',
middleware: ['auth'],
description: '获取 flowme 列表',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const uid = tokenUser.id;
const { page = 1, pageSize = 20, search, channelId, sort = 'DESC' } = ctx.query || {};
const offset = (page - 1) * pageSize;
const orderByField = sort === 'ASC' ? schema.flowme.updatedAt : desc(schema.flowme.updatedAt);
let whereCondition = eq(schema.flowme.uid, uid);
if (search) {
whereCondition = and(
eq(schema.flowme.uid, uid),
or(
like(schema.flowme.title, `%${search}%`),
like(schema.flowme.description, `%${search}%`)
)
);
}
if (channelId) {
whereCondition = and(
whereCondition,
eq(schema.flowme.channelId, channelId)
);
}
const [list, totalCount] = await Promise.all([
db.select()
.from(schema.flowme)
.where(whereCondition)
.limit(pageSize)
.offset(offset)
.orderBy(orderByField),
db.select({ count: count() })
.from(schema.flowme)
.where(whereCondition)
]);
ctx.body = {
list,
pagination: {
page,
current: page,
pageSize,
total: totalCount[0]?.count || 0,
},
};
return ctx;
}).addTo(app);
// 创建或更新 flowme
const flowmeUpdate = `创建或更新一个 flowme, 参数定义:
title: 标题, 必填
description: 描述, 选填
tags: 标签, 数组, 选填
link: 链接, 选填
data: 数据, 对象, 选填
channelId: 频道ID, 选填
type: 类型, 选填
source: 来源, 选填
importance: 重要性等级, 数字, 选填
`;
app.route({
path: 'flowme',
key: 'update',
middleware: ['auth'],
description: flowmeUpdate,
}).define(async (ctx) => {
const { id, uid, updatedAt, createdAt, ...rest } = ctx.query.data || {};
const tokenUser = ctx.state.tokenUser;
let flowmeItem;
if (!id) {
flowmeItem = await db.insert(schema.flowme).values({
title: rest.title || '',
description: rest.description || '',
tags: rest.tags || [],
link: rest.link || '',
data: rest.data || {},
channelId: rest.channelId || null,
type: rest.type || '',
source: rest.source || '',
importance: rest.importance || 0,
uid: tokenUser.id,
}).returning();
} else {
const existing = await db.select().from(schema.flowme).where(eq(schema.flowme.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的 flowme');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限更新该 flowme');
}
flowmeItem = await db.update(schema.flowme).set({
title: rest.title,
description: rest.description,
tags: rest.tags,
link: rest.link,
data: rest.data,
channelId: rest.channelId,
type: rest.type,
source: rest.source,
importance: rest.importance,
isArchived: rest.isArchived,
}).where(eq(schema.flowme.id, id)).returning();
}
ctx.body = flowmeItem;
}).addTo(app);
// 删除 flowme
app.route({
path: 'flowme',
key: 'delete',
middleware: ['auth'],
description: '删除 flowme, 参数: data.id 必填',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id 参数缺失');
}
const existing = await db.select().from(schema.flowme).where(eq(schema.flowme.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的 flowme');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限删除该 flowme');
}
await db.delete(schema.flowme).where(eq(schema.flowme.id, id));
ctx.body = { success: true };
}).addTo(app);
// 获取单个 flowme
app.route({
path: 'flowme',
key: 'get',
middleware: ['auth'],
description: '获取单个 flowme, 参数: data.id 必填',
}).define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { id } = ctx.query.data || {};
if (!id) {
ctx.throw(400, 'id 参数缺失');
}
const existing = await db.select().from(schema.flowme).where(eq(schema.flowme.id, id)).limit(1);
if (existing.length === 0) {
ctx.throw(404, '没有找到对应的 flowme');
}
if (existing[0].uid !== tokenUser.id) {
ctx.throw(403, '没有权限查看该 flowme');
}
ctx.body = existing[0];
}).addTo(app);

View File

@@ -0,0 +1,47 @@
import { schema, app, db } from '@/app.ts'
import { Client } from 'pg'
let pgClient: Client | null = null
async function startFlowmeListener() {
// 使用独立的数据库连接来监听
pgClient = new Client({
connectionString: process.env.DATABASE_URL || 'postgresql://postgres:postgres@localhost:5432/code_center',
})
await pgClient.connect()
console.log('🔌 已连接到 PostgreSQL 监听器')
// 订阅通知事件
pgClient.on('notification', (data) => {
if (!data.payload) return
try {
const parsed = JSON.parse(data.payload)
console.log('📥 收到新 flowme 创建通知:', parsed)
// 在这里处理你的业务逻辑
handleNewFlowme(parsed)
} catch (err) {
console.error('❌ 解析 flowme 通知失败:', err)
}
})
// 执行 LISTEN 命令订阅通道
await pgClient.query('LISTEN flowme_insert')
console.log('👂 开始监听 flowme_insert 通道...')
}
function handleNewFlowme(data: any) {
// 根据新创建的 flowme 数据执行相应操作
console.log('处理新 flowme:', data.id, data.title)
// 示例:可以通过 WebSocket 推送给前端
// wsServer.emit('flowme:created', data)
}
// 启动监听器(只启动一次)
if (!global.__flowmeListenerStarted) {
global.__flowmeListenerStarted = true
startFlowmeListener().catch(console.error)
}

View File

@@ -10,6 +10,7 @@ import './config/index.ts';
// import './file-listener/index.ts';
import './light-code/index.ts';
import './ai/index.ts';
@@ -17,4 +18,6 @@ import './prompts/index.ts'
import './views/index.ts';
import './query-views/index.ts';
import './query-views/index.ts';
import './flowme/index.ts'

View File

@@ -0,0 +1 @@
import './list.ts'

View File

@@ -0,0 +1,183 @@
import { eq, desc, and, like, or } from 'drizzle-orm';
import { CustomError } from '@kevisual/router';
import { app, db, schema } from '../../app.ts';
import { filter } from '@kevisual/js-filter'
import { z } from 'zod';
app
.route({
path: 'light-code',
key: 'list',
description: `获取轻代码列表,参数
type: 代码类型light-code, ts`,
middleware: ['auth'],
metadata: {
args: {
type: z.string().optional().describe('代码类型light-code, ts'),
search: z.string().optional().describe('搜索关键词,匹配标题和描述'),
filter: z
.string()
.optional()
.describe(
'过滤条件SQL like格式字符串例如WHERE tags LIKE \'%tag1%\' AND tags LIKE \'%tag2%\'',
),
}
}
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { type, search, filter: filterQuery } = ctx.query || {};
const conditions = [eq(schema.kvContainer.uid, tokenUser.id)];
if (type) {
conditions.push(eq(schema.kvContainer.type, type as string));
}
if (search) {
const searchTerm = `%${search}%`;
conditions.push(
or(
like(schema.kvContainer.title, searchTerm),
like(schema.kvContainer.description, searchTerm),
),
);
}
const list = await db
.select({
id: schema.kvContainer.id,
title: schema.kvContainer.title,
description: schema.kvContainer.description,
type: schema.kvContainer.type,
tags: schema.kvContainer.tags,
data: schema.kvContainer.data,
code: schema.kvContainer.code,
uid: schema.kvContainer.uid,
createdAt: schema.kvContainer.createdAt,
updatedAt: schema.kvContainer.updatedAt,
hash: schema.kvContainer.hash,
})
.from(schema.kvContainer)
.where(and(...conditions))
.orderBy(desc(schema.kvContainer.updatedAt));
if (filterQuery) {
const filteredList = filter(list, filterQuery);
ctx.body = { list: filteredList }
} else {
ctx.body = { list };
}
return ctx;
})
.addTo(app);
app
.route({
path: 'light-code',
key: 'get',
description: '获取轻代码详情',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
const result = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
const container = result[0];
if (!container) {
ctx.throw('未发现该代码内容');
}
if (container.uid !== tokenUser.id) {
ctx.throw('没有权限访问该代码内容');
}
ctx.body = container;
})
.addTo(app);
app
.route({
path: 'light-code',
key: 'update',
middleware: ['auth'],
isDebug: true,
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const data = ctx.query.data;
const { id, ...container } = data;
if (id) {
const result = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
const existing = result[0];
if (existing) {
await db
.update(schema.kvContainer)
.set({
...container,
updatedAt: new Date().toISOString(),
})
.where(eq(schema.kvContainer.id, id));
const updated = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
ctx.body = updated[0];
} else {
ctx.body = null;
}
} else {
try {
console.log('created', container, 'userId', tokenUser.id);
const [created] = await db
.insert(schema.kvContainer)
.values({
title: container.title || '',
description: container.description || '',
type: container.type || 'light-code',
code: container.code || '',
data: container.data || {},
tags: container.tags || [],
hash: container.hash || '',
uid: tokenUser.id,
})
.returning();
ctx.body = created;
} catch (error) {
console.error('Error creating container:', error);
throw error;
}
}
})
.addTo(app);
app
.route({
path: 'light-code',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
const result = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
const container = result[0];
if (!container) {
ctx.throw('未发现该容器');
}
if (container.uid !== tokenUser.id) {
ctx.throw('没有权限访问该容器');
}
await db.delete(schema.kvContainer).where(eq(schema.kvContainer.id, id));
ctx.body = container;
})
.addTo(app);

View File

@@ -1,5 +1,4 @@
import { minioClient } from '@/app.ts';
import { bucketName } from '@/modules/minio.ts';
import { oss } from '@/app.ts';
import { fileIsExist } from '@kevisual/use-config';
import { spawn, spawnSync } from 'child_process';
import { getFileStat, getMinioList, MinioFile } from '@/routes/file/index.ts';
@@ -8,6 +7,7 @@ import fs from 'fs';
import path from 'path';
import { appsPath } from '../lib/index.ts';
import { installAppFromKey } from './manager.ts';
import { Readable } from 'stream';
export type InstallAppOpts = {
needInstallDeps?: boolean;
// minio中
@@ -46,7 +46,7 @@ export const installApp = async (opts: InstallAppOpts) => {
if (!fileIsExist(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const fileStream = await minioClient.getObject(bucketName, `${name}`);
const fileStream = (await oss.getObject(`${name}`)).Body as Readable;
const writeStream = fs.createWriteStream(outputPath);
fileStream.pipe(writeStream);

View File

@@ -153,7 +153,9 @@ app
browser: someInfo['user-agent'],
host: someInfo.host,
});
createCookie(token, ctx);
createCookie({
token: token.accessToken
}, ctx);
ctx.body = token;
})
.addTo(app);
@@ -259,7 +261,10 @@ app
const refreshToken = accessUser.oauthExpand?.refreshToken;
if (refreshToken) {
const result = await User.oauth.refreshToken(refreshToken);
createCookie(result, ctx);
createCookie({
token: result.accessToken
}, ctx);
ctx.body = result;
return;
} else if (accessUser) {
@@ -268,7 +273,9 @@ app
...accessUser.oauthExpand,
hasRefreshToken: true,
});
createCookie(result, ctx);
createCookie({
token: result.accessToken
}, ctx);
ctx.body = result;
return;
}
@@ -323,13 +330,17 @@ app
if (orgsList.includes(username)) {
if (tokenUsername === username) {
const result = await User.oauth.resetToken(token);
createCookie(result, ctx);
createCookie({
token: result.accessToken,
}, ctx);
await User.oauth.delToken(token);
ctx.body = result;
} else {
const user = await User.findOne({ where: { username } });
const result = await user.createToken(userId, 'default');
createCookie(result, ctx);
createCookie({
token: result.accessToken,
}, ctx);
ctx.body = result;
}
} else {
@@ -352,7 +363,9 @@ app
const result = await User.oauth.refreshToken(refreshToken);
if (result) {
console.log('refreshToken result', result);
createCookie(result, ctx);
createCookie({
token: result.accessToken,
}, ctx);
ctx.body = result;
} else {
ctx.throw(500, 'Refresh Token Failed, please login again');

View File

@@ -109,7 +109,9 @@ app
const token = JSON.parse(data);
if (token.accessToken) {
ctx.body = token;
createCookie(token, ctx);
createCookie({
token: token.accessToken,
}, ctx);
} else {
ctx.throw(500, 'Checked error Failed, login failed, please login again');
}

View File

@@ -0,0 +1,44 @@
import { app, showMore, showRes } from './common.ts';
import crypto from 'node:crypto';
export const getStringHash = (str: string) => {
return crypto.createHash('md5').update(str).digest('hex');
}
const code = `// 这是一个示例代码文件
import {App} from '@kevisual/router';
const app = new App();
app.route({
path: 'hello',
description: 'LightCode 示例路由 2323232323',
metadata: {
tags: ['light-code', 'example'],
},
}).define(async (ctx) => {
console.log('tokenUser:', ctx.query?.tokenUser);
ctx.body = 'Hello from LightCode!';
}).addTo(app);
app.wait();`
const code2 = `const a = 1`
const res = await app.run({
path: 'light-code',
key: 'update',
payload: {
data: {
title: 'Demo Light Code',
description: '这是一个演示用的轻代码项目,包含一个简单的路由示例。',
type: 'light-code',
tags: ['demo', 'light-code'],
data: {},
code: code,
hash: getStringHash(code),
},
token: "st_idht7xpffhgu2eeh94zd8ze1t7ew3amy",
},
})
console.log('showMore', showMore(res));

View File

@@ -3,7 +3,9 @@ import '@/route.ts';
import { useConfig, useContextKey } from '@kevisual/context';
import { Query } from '@kevisual/query';
import util from 'node:util';
import dotenv from 'dotenv';
dotenv.config();
export {
app,
useContextKey

View File

@@ -1,40 +0,0 @@
import { bucketName, minioClient } from '@/modules/minio.ts';
import { S3Error } from 'minio';
const main = async () => {
const res = await new Promise((resolve, reject) => {
let res: any[] = [];
let hasError = false;
minioClient
.listObjectsV2(bucketName, 'root/codeflow/0.0.1/')
.on('data', (data) => {
res.push(data);
})
.on('error', (err) => {
console.error('error', err);
hasError = true;
})
.on('end', () => {
if (hasError) {
reject();
return;
} else {
resolve(res);
}
});
});
console.log(res);
};
// main();
const main2 = async () => {
try {
const obj = await minioClient.statObject(bucketName, 'root/codeflow/0.0.1/README.md');
console.log(obj);
} catch (e) {
console.log('', e.message, '\n\r', e.code);
// console.error(e);
}
};
main2();

View File

@@ -1,10 +1,10 @@
process.env.NODE_ENV = 'development';
// import { mvUserAToUserB, backupUserA } from '../routes/file/module/get-minio-list.ts';
import { mvUserAToUserB, backupUserA } from '../routes/file/module/get-minio-list.ts';
// mvUserAToUserB('demo', 'demo2');
// backupUserA('demo', '123', '2025-04-02-16-00');
// backupUserA('demo', '123', '2026-01-31-16-00');
// backupUserA('demo', '123', '2025-04-02-16-01');
// backupUserA('demo', '123', '2025-04-02-16-02');
// backupUserA('demo', '123', '2025-04-02-16-03');

4
src/test/s3-stat.ts Normal file
View File

@@ -0,0 +1,4 @@
import { oss } from '@/modules/s3.ts'
const stat = await oss.statObject('root/codepod/0.0.3/index.html');
console.log('Object Stat:', stat);

View File

@@ -23,13 +23,13 @@
],
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
"license": "MIT",
"packageManager": "pnpm@10.26.2",
"packageManager": "pnpm@10.28.1",
"type": "module",
"dependencies": {
"@kevisual/context": "^0.0.4",
"@kevisual/query": "^0.0.33",
"@kevisual/router": "0.0.50",
"@types/node": "^25.0.3",
"@kevisual/query": "^0.0.38",
"@kevisual/router": "0.0.60",
"@types/node": "^25.0.10",
"crypto-js": "^4.2.0",
"xml2js": "^0.6.2"
},

View File

@@ -10,4 +10,4 @@ import { useConfig } from '@kevisual/use-config';
export const config = useConfig()
console.log('配置项:', config);
// console.log('配置项:', config);