mirror of
https://github.com/ZeroCatDev/ClassworksKV.git
synced 2025-12-09 07:33:10 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e3b3df1ae | ||
|
|
21d6ddf164 | ||
|
|
e65f84aa22 | ||
|
|
ab8904b549 | ||
|
|
da633ca5b6 | ||
|
|
1f68aea39f | ||
|
|
b782945674 | ||
|
|
1e1b99a070 | ||
|
|
63716e0429 | ||
|
|
b582521fee | ||
|
|
f985b6a11a | ||
|
|
f0de2cd59b | ||
|
|
d52ed81a29 | ||
|
|
e73ff53f58 | ||
|
|
79ec5b94a4 |
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# 默认忽略的文件
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# 基于编辑器的 HTTP 客户端请求
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
12
.idea/FixClassworksKV.iml
generated
Normal file
12
.idea/FixClassworksKV.iml
generated
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/FixClassworksKV.iml" filepath="$PROJECT_DIR$/.idea/FixClassworksKV.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
4
app.js
4
app.js
@ -25,6 +25,10 @@ app.use(
|
|||||||
cors({
|
cors({
|
||||||
exposedHeaders: ["ratelimit-policy", "retry-after", "ratelimit"], // 告诉浏览器这些响应头可以暴露
|
exposedHeaders: ["ratelimit-policy", "retry-after", "ratelimit"], // 告诉浏览器这些响应头可以暴露
|
||||||
maxAge: 86400, // 设置OPTIONS请求的结果缓存24小时(86400秒),减少预检请求
|
maxAge: 86400, // 设置OPTIONS请求的结果缓存24小时(86400秒),减少预检请求
|
||||||
|
credentials: true, // 允许跨域请求携带凭证
|
||||||
|
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With", "Accept"], // 允许的请求头
|
||||||
|
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"], // 允许的HTTP方法
|
||||||
|
withCredentials: true, // 允许携带cookie等凭证信息
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
app.disable("x-powered-by");
|
app.disable("x-powered-by");
|
||||||
|
|||||||
@ -67,6 +67,24 @@ export const oauthProviders = {
|
|||||||
website: "https://houlang.cloud",
|
website: "https://houlang.cloud",
|
||||||
pkce: true, // 启用PKCE支持
|
pkce: true, // 启用PKCE支持
|
||||||
},
|
},
|
||||||
|
dlass: {
|
||||||
|
// Dlass(Casdoor)- 标准 OIDC Provider
|
||||||
|
clientId: process.env.DLASS_CLIENT_ID,
|
||||||
|
clientSecret: process.env.DLASS_CLIENT_SECRET,
|
||||||
|
// Casdoor 标准端点
|
||||||
|
authorizationURL: "https://auth.wiki.forum/login/oauth/authorize",
|
||||||
|
tokenURL: "https://auth.wiki.forum/api/login/oauth/access_token",
|
||||||
|
userInfoURL: "https://auth.wiki.forum/api/userinfo",
|
||||||
|
scope: "openid profile email offline_access",
|
||||||
|
// 展示相关
|
||||||
|
name: "dlass",
|
||||||
|
displayName: "Dlass 账户",
|
||||||
|
icon: "casdoor",
|
||||||
|
color: "#3498db",
|
||||||
|
description: "使用Dlass账户登录",
|
||||||
|
website: "https://dlass.tech",
|
||||||
|
tokenRequestFormat: "json", // Casdoor 推荐 JSON 提交
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// 获取OAuth回调URL
|
// 获取OAuth回调URL
|
||||||
|
|||||||
9032
package-lock.json
generated
Normal file
9032
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ClassworksKV",
|
"name": "ClassworksKV",
|
||||||
"version": "1.3.5",
|
"version": "1.3.8",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node ./bin/www",
|
"start": "node ./bin/www",
|
||||||
|
|||||||
@ -282,6 +282,14 @@ router.get("/oauth/:provider/callback", async (req, res) => {
|
|||||||
name: userData.name || userData.preferred_username || userData.nickname,
|
name: userData.name || userData.preferred_username || userData.nickname,
|
||||||
avatarUrl: userData.picture,
|
avatarUrl: userData.picture,
|
||||||
};
|
};
|
||||||
|
} else if (provider === "dlass") {
|
||||||
|
// Dlass(Casdoor)标准OIDC用户信息
|
||||||
|
normalizedUser = {
|
||||||
|
providerId: userData.sub,
|
||||||
|
email: userData.email_verified ? userData.email : userData.email || null,
|
||||||
|
name: userData.name || userData.preferred_username || userData.nickname,
|
||||||
|
avatarUrl: userData.picture,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 名称为空时,用邮箱@前部分回填(若邮箱可用)
|
// 名称为空时,用邮箱@前部分回填(若邮箱可用)
|
||||||
|
|||||||
@ -298,43 +298,25 @@ router.post(
|
|||||||
req.connection.socket?.remoteAddress ||
|
req.connection.socket?.remoteAddress ||
|
||||||
"";
|
"";
|
||||||
|
|
||||||
const results = [];
|
// 使用优化的批量upsert方法
|
||||||
const errorList = [];
|
const { results, errors: errorList } = await kvStore.batchUpsert(deviceId, data, creatorIp);
|
||||||
|
|
||||||
// 批量处理所有键值对
|
|
||||||
for (const [key, value] of Object.entries(data)) {
|
|
||||||
try {
|
|
||||||
const result = await kvStore.upsert(deviceId, key, value, creatorIp);
|
|
||||||
results.push({
|
|
||||||
key: result.key,
|
|
||||||
created: result.createdAt.getTime() === result.updatedAt.getTime(),
|
|
||||||
});
|
|
||||||
// 广播每个键的变更
|
|
||||||
const uuid = res.locals.device?.uuid;
|
|
||||||
if (uuid) {
|
|
||||||
broadcastKeyChanged(uuid, {
|
|
||||||
key: result.key,
|
|
||||||
action: "upsert",
|
|
||||||
created: result.createdAt.getTime() === result.updatedAt.getTime(),
|
|
||||||
updatedAt: result.updatedAt,
|
|
||||||
batch: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
errorList.push({
|
|
||||||
key,
|
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
|
code: 200,
|
||||||
|
message: "批量导入成功",
|
||||||
|
data: {
|
||||||
deviceId,
|
deviceId,
|
||||||
|
summary: {
|
||||||
total: Object.keys(data).length,
|
total: Object.keys(data).length,
|
||||||
successful: results.length,
|
successful: results.length,
|
||||||
failed: errorList.length,
|
failed: errorList.length,
|
||||||
results,
|
},
|
||||||
errors: errorList.length > 0 ? errorList : undefined,
|
results: results.map(r => ({
|
||||||
|
key: r.key,
|
||||||
|
isNew: r.created,
|
||||||
|
})),
|
||||||
|
...(errorList.length > 0 && { errors: errorList }),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@ -354,10 +336,20 @@ router.post(
|
|||||||
|
|
||||||
const deviceId = res.locals.deviceId;
|
const deviceId = res.locals.deviceId;
|
||||||
const { key } = req.params;
|
const { key } = req.params;
|
||||||
const value = req.body;
|
let value = req.body;
|
||||||
|
|
||||||
if (!value || Object.keys(value).length === 0) {
|
// 处理空值,转换为空对象
|
||||||
return next(errors.createError(400, "请提供有效的JSON值"));
|
if (value === null || value === undefined || value === '') {
|
||||||
|
value = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证是否能被 JSON 序列化
|
||||||
|
try {
|
||||||
|
JSON.stringify(value);
|
||||||
|
} catch (error) {
|
||||||
|
return next(
|
||||||
|
errors.createError(400, "无效的数据格式")
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取客户端IP
|
// 获取客户端IP
|
||||||
|
|||||||
@ -102,6 +102,62 @@ class KVStore {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量创建或更新键值对(优化性能)
|
||||||
|
* @param {number} deviceId - 设备ID
|
||||||
|
* @param {object} data - 键值对数据 {key1: value1, key2: value2, ...}
|
||||||
|
* @param {string} creatorIp - 创建者IP,可选
|
||||||
|
* @returns {object} {results: Array, errors: Array}
|
||||||
|
*/
|
||||||
|
async batchUpsert(deviceId, data, creatorIp = "") {
|
||||||
|
const results = [];
|
||||||
|
const errors = [];
|
||||||
|
|
||||||
|
// 使用事务处理所有操作
|
||||||
|
await prisma.$transaction(async (tx) => {
|
||||||
|
for (const [key, value] of Object.entries(data)) {
|
||||||
|
try {
|
||||||
|
const item = await tx.kVStore.upsert({
|
||||||
|
where: {
|
||||||
|
deviceId_key: {
|
||||||
|
deviceId: deviceId,
|
||||||
|
key: key,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
value,
|
||||||
|
...(creatorIp && {creatorIp}),
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
deviceId: deviceId,
|
||||||
|
key: key,
|
||||||
|
value,
|
||||||
|
creatorIp,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
results.push({
|
||||||
|
key: item.key,
|
||||||
|
created: item.createdAt.getTime() === item.updatedAt.getTime(),
|
||||||
|
createdAt: item.createdAt,
|
||||||
|
updatedAt: item.updatedAt,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
errors.push({
|
||||||
|
key,
|
||||||
|
error: error.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 在事务完成后,一次性更新指标
|
||||||
|
const totalKeys = await prisma.kVStore.count();
|
||||||
|
keysTotal.set(totalKeys);
|
||||||
|
|
||||||
|
return { results, errors };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过设备ID和键名删除
|
* 通过设备ID和键名删除
|
||||||
* @param {number} deviceId - 设备ID
|
* @param {number} deviceId - 设备ID
|
||||||
@ -219,6 +275,37 @@ class KVStore {
|
|||||||
});
|
});
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定设备的统计信息
|
||||||
|
* @param {number} deviceId - 设备ID
|
||||||
|
* @returns {object} 统计信息
|
||||||
|
*/
|
||||||
|
async getStats(deviceId) {
|
||||||
|
const [totalKeys, oldestKey, newestKey] = await Promise.all([
|
||||||
|
prisma.kVStore.count({
|
||||||
|
where: { deviceId },
|
||||||
|
}),
|
||||||
|
prisma.kVStore.findFirst({
|
||||||
|
where: { deviceId },
|
||||||
|
orderBy: { createdAt: "asc" },
|
||||||
|
select: { createdAt: true, key: true },
|
||||||
|
}),
|
||||||
|
prisma.kVStore.findFirst({
|
||||||
|
where: { deviceId },
|
||||||
|
orderBy: { updatedAt: "desc" },
|
||||||
|
select: { updatedAt: true, key: true },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalKeys,
|
||||||
|
oldestKey: oldestKey?.key,
|
||||||
|
oldestCreatedAt: oldestKey?.createdAt,
|
||||||
|
newestKey: newestKey?.key,
|
||||||
|
newestUpdatedAt: newestKey?.updatedAt,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new KVStore();
|
export default new KVStore();
|
||||||
|
|||||||
@ -96,14 +96,16 @@ function detectDeviceName(userAgent, headers = {}) {
|
|||||||
export function initSocket(server) {
|
export function initSocket(server) {
|
||||||
if (io) return io;
|
if (io) return io;
|
||||||
|
|
||||||
const allowOrigin = process.env.FRONTEND_URL || "*";
|
|
||||||
|
|
||||||
io = new Server(server, {
|
io = new Server(server, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: allowOrigin,
|
origin: "*",
|
||||||
methods: ["GET", "POST"],
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
||||||
credentials: true,
|
allowedHeaders: ["*"],
|
||||||
|
credentials: false
|
||||||
},
|
},
|
||||||
|
// 传输方式回退策略:优先使用WebSocket,回退到轮询
|
||||||
|
transports: ["polling", "websocket"],
|
||||||
});
|
});
|
||||||
|
|
||||||
io.on("connection", (socket) => {
|
io.on("connection", (socket) => {
|
||||||
@ -288,10 +290,10 @@ export function initSocket(server) {
|
|||||||
|
|
||||||
// 清理socket相关缓存
|
// 清理socket相关缓存
|
||||||
if (socket.data.currentToken) {
|
if (socket.data.currentToken) {
|
||||||
// 如果这是该token的最后一个连接,考虑清理缓存
|
// 如果这是该token的最后一个连接,考虑清理缓存
|
||||||
const tokenSet = onlineTokens.get(socket.data.currentToken);
|
const tokenSet = onlineTokens.get(socket.data.currentToken);
|
||||||
if (!tokenSet || tokenSet.size === 0) {
|
if (!tokenSet || tokenSet.size === 0) {
|
||||||
// 可以选择保留缓存一段时间,这里暂时保留
|
// 可以选择保留缓存一段时间,这里暂时保留
|
||||||
// tokenInfoCache.delete(socket.data.currentToken);
|
// tokenInfoCache.delete(socket.data.currentToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user