新版界面挂载:/next/ 提供 web/dist 构建,单页回退并限制在目录内

试用、演示模式默认开启,云端需设置 WEB_NEXT=1;其余地址行为不变。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
黄健铭andClaude Opus 5.5 committed 2026-12-04 18:36:57 -08:00
1 parent 33fedc2e68
commit 40bff0be94
5 files changed
+273 -2

No files matched your search

+3 -1
View File
@@ -22,6 +22,7 @@ const { securityHeaders } = require('./middleware/security-headers');
const { checkRequest } = require('./middleware/request-guard');
const { requireActor } = require('./middleware/session');
const { serveStatic } = require('./middleware/static');
const { serveWebNext, webNextEnabled, isNextPath } = require('./middleware/web-next');
const { sendError } = require('./middleware/errors');
const UNMATCHED = {
@@ -63,7 +64,7 @@ async function runRoute(route, ctx) {
}
// 处理顺序:安全响应头 → Host/405/来源校验 → 登录前路由 → 解析当前账号 → /api/、/print/ 的 401/409
// → 路由(含未匹配)→ 静态文件 → 统一错误响应。
// → 路由(含未匹配)→ 新版界面 /next/(启用时)→ 静态文件 → 统一错误响应。
function createHandler(app, routes) {
const router = createRouter(routes);
const enabled = route => !route.when || route.when(app);
@@ -91,6 +92,7 @@ function createHandler(app, routes) {
requireActor(req, ctx.actor);
return await runRoute(hit?.route ?? UNMATCHED[req.method], ctx);
}
if (isNextPath(pathname) && webNextEnabled(app)) return serveWebNext(res, pathname, isPost);
serveStatic(res, pathname, isPost);
} catch (e) {
sendError(res, e);
@@ -0,0 +1,62 @@
'use strict';
// 新版界面(web/dist 的生产构建)挂在 /next/ 下:试用、演示模式默认开启;云端仅在 WEB_NEXT=1 时开启。
// 带扩展名的路径按资源文件查找,找不到就 404;其余 /next、/next/* 一律返回 index.html(单页应用回退)。
// 路径解码后必须仍落在 web/dist 之内,否则按未知地址 404。
const fs = require('node:fs');
const path = require('node:path');
const { fail } = require('../../core/ledger');
const DIST_DIR = path.join(__dirname, '..', '..', 'web', 'dist');
const TYPES = {
'.html': 'text/html; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.mjs': 'application/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.ico': 'image/x-icon',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
'.txt': 'text/plain; charset=utf-8',
'.map': 'application/json; charset=utf-8',
};
const isNextPath = pathname => pathname === '/next' || pathname.startsWith('/next/');
function webNextEnabled(app) {
return app.mode === 'demo' || app.mode === 'trial' || process.env.WEB_NEXT === '1';
}
function readFile(file) {
try {
const stat = fs.statSync(file);
return stat.isFile() ? fs.readFileSync(file) : null;
} catch {
return null;
}
}
function serveWebNext(res, pathname, isPost, { distDir = DIST_DIR } = {}) {
if (isPost) fail('地址不存在', 404);
let relative;
try {
relative = decodeURIComponent(pathname.slice('/next'.length));
} catch {
fail('地址不存在', 404);
}
if (relative.includes('\0') || relative.includes('\\')) fail('地址不存在', 404);
const root = path.resolve(distDir);
const target = path.resolve(root, '.' + path.posix.normalize('/' + relative));
if (target !== root && !target.startsWith(root + path.sep)) fail('地址不存在', 404);
const last = relative.split('/').pop();
const asset = last.includes('.');
const file = asset ? target : path.join(root, 'index.html');
const type = TYPES[path.extname(file).toLowerCase()];
const body = type ? readFile(file) : null;
if (!body) fail('地址不存在', 404);
// 直接写出原始字节:通用 send() 会把 JSON 类型的内容再序列化一次。
res.writeHead(200, { 'Content-Type': type });
res.end(body);
}
module.exports = { serveWebNext, webNextEnabled, isNextPath, DIST_DIR };