pdd-skills 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1478 -0
- package/bin/pdd.js +354 -0
- package/config/bpmn-rules.yaml +166 -0
- package/config/checkstyle.xml +105 -0
- package/config/eslint.config.js +48 -0
- package/config/pmd.xml +91 -0
- package/config/prd-rules.yaml +113 -0
- package/config/ruff.toml +45 -0
- package/config/sqlfluff.cfg +82 -0
- package/hooks/hook-executor.js +332 -0
- package/index.js +43 -0
- package/lib/api-routes.js +750 -0
- package/lib/api-server.js +408 -0
- package/lib/cache/cache-config.js +209 -0
- package/lib/cache/system-cache.js +852 -0
- package/lib/config-manager.js +373 -0
- package/lib/generate.js +528 -0
- package/lib/grpc/grpc-routes.js +1134 -0
- package/lib/grpc/grpc-server.js +912 -0
- package/lib/grpc/proto-definitions.js +1033 -0
- package/lib/init.js +172 -0
- package/lib/iteration/auto-fixer.js +1025 -0
- package/lib/iteration/auto-reviewer.js +923 -0
- package/lib/iteration/controller.js +577 -0
- package/lib/list.js +130 -0
- package/lib/mcp-server.js +548 -0
- package/lib/openclaw/api-integration.js +535 -0
- package/lib/openclaw/cli-integration.js +567 -0
- package/lib/openclaw/data-sync.js +845 -0
- package/lib/openclaw/openclaw-adapter.js +783 -0
- package/lib/plugin/example-plugins/code-stats/index.js +332 -0
- package/lib/plugin/example-plugins/code-stats/plugin.json +1 -0
- package/lib/plugin/example-plugins/custom-linter/index.js +472 -0
- package/lib/plugin/example-plugins/custom-linter/plugin.json +1 -0
- package/lib/plugin/example-plugins/hello-world/index.js +86 -0
- package/lib/plugin/example-plugins/hello-world/plugin.json +1 -0
- package/lib/plugin/plugin-manager.js +655 -0
- package/lib/plugin/plugin-sdk.js +565 -0
- package/lib/plugin/sandbox.js +627 -0
- package/lib/quality/rules/maintainability.js +418 -0
- package/lib/quality/rules/performance.js +498 -0
- package/lib/quality/rules/readability.js +441 -0
- package/lib/quality/rules/robustness.js +504 -0
- package/lib/quality/rules/security.js +444 -0
- package/lib/quality/scorer.js +576 -0
- package/lib/report.js +669 -0
- package/lib/sdk-base.js +301 -0
- package/lib/sdk-js.js +446 -0
- package/lib/sdk-python/README.md +546 -0
- package/lib/sdk-python/examples/basic_usage.py +450 -0
- package/lib/sdk-python/pdd_sdk/__init__.py +180 -0
- package/lib/sdk-python/pdd_sdk/client.py +1170 -0
- package/lib/sdk-python/pdd_sdk/events.py +423 -0
- package/lib/sdk-python/pdd_sdk/exceptions.py +158 -0
- package/lib/sdk-python/pdd_sdk/models.py +518 -0
- package/lib/sdk-python/pdd_sdk/utils.py +759 -0
- package/lib/token/budget-alert.js +367 -0
- package/lib/token/budget-manager.js +485 -0
- package/lib/update.js +54 -0
- package/lib/utils/logger.js +88 -0
- package/lib/verify.js +741 -0
- package/lib/version.js +52 -0
- package/lib/vm/README.md +102 -0
- package/lib/vm/dashboard/api-routes.js +669 -0
- package/lib/vm/dashboard/server.js +391 -0
- package/lib/vm/dashboard/sse.js +358 -0
- package/lib/vm/dashboard/static/css/dashboard.css +1378 -0
- package/lib/vm/dashboard/static/index.html +118 -0
- package/lib/vm/dashboard/static/js/app.js +949 -0
- package/lib/vm/dashboard/static/js/charts.js +913 -0
- package/lib/vm/dashboard/static/js/kanban-view.js +1053 -0
- package/lib/vm/dashboard/static/js/pipeline-view.js +463 -0
- package/lib/vm/dashboard/static/js/quality-view.js +598 -0
- package/lib/vm/dashboard/static/js/system-view.js +1021 -0
- package/lib/vm/data-provider.js +1191 -0
- package/lib/vm/event-bus.js +402 -0
- package/lib/vm/hooks/extract-hook.js +307 -0
- package/lib/vm/hooks/generate-hook.js +374 -0
- package/lib/vm/hooks/hook-interface.js +458 -0
- package/lib/vm/hooks/report-hook.js +331 -0
- package/lib/vm/hooks/verify-hook.js +454 -0
- package/lib/vm/models.js +1003 -0
- package/lib/vm/reconciler.js +855 -0
- package/lib/vm/scanner.js +988 -0
- package/lib/vm/state-schema.js +955 -0
- package/lib/vm/state-store.js +733 -0
- package/lib/vm/tui/components/card.js +339 -0
- package/lib/vm/tui/components/progress-bar.js +368 -0
- package/lib/vm/tui/components/sparkline.js +327 -0
- package/lib/vm/tui/components/status-light.js +294 -0
- package/lib/vm/tui/components/table.js +370 -0
- package/lib/vm/tui/input.js +335 -0
- package/lib/vm/tui/renderer.js +548 -0
- package/lib/vm/tui/screens/kanban-screen.js +397 -0
- package/lib/vm/tui/screens/overview-screen.js +357 -0
- package/lib/vm/tui/screens/quality-screen.js +336 -0
- package/lib/vm/tui/screens/system-screen.js +379 -0
- package/lib/vm/tui/tui.js +805 -0
- package/package.json +1 -0
- package/scripts/cso-analyzer.js +198 -0
- package/scripts/eval-runner.js +359 -0
- package/scripts/i18n-checker.js +109 -0
- package/scripts/linter/activiti-linter.js +272 -0
- package/scripts/linter/prd-linter.js +162 -0
- package/scripts/linter/report-generator.js +207 -0
- package/scripts/linter/run-linters.js +285 -0
- package/scripts/linter/sql-linter.js +166 -0
- package/scripts/token-analyzer.js +162 -0
- package/scripts/vm-test.js +180 -0
- package/skills/core/official-doc-writer/LICENSE +21 -0
- package/skills/core/official-doc-writer/README.md +232 -0
- package/skills/core/official-doc-writer/SKILL.md +475 -0
- package/skills/core/official-doc-writer/_meta.json +1 -0
- package/skills/core/official-doc-writer/document_generator.py +580 -0
- package/skills/core/official-doc-writer/evals/default-evals.json +1 -0
- package/skills/core/official-doc-writer/examples.md +150 -0
- package/skills/core/official-doc-writer/fonts/FONTS_LIST.md +45 -0
- package/skills/core/official-doc-writer/fonts/README.md +141 -0
- package/skills/core/official-doc-writer/fonts/SIMFANG.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMHEI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMKAI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMSUN.TTC +0 -0
- package/skills/core/official-doc-writer/fonts//346/226/271/346/255/243/345/260/217/346/240/207/345/256/213GBK.TTF +0 -0
- package/skills/core/official-doc-writer/references/GBT_9704-2012_/345/205/232/346/224/277/346/234/272/345/205/263/345/205/254/346/226/207/346/240/274/345/274/217.md +422 -0
- package/skills/core/official-doc-writer/scripts/__pycache__/generate_official_doc.cpython-313.pyc +0 -0
- package/skills/core/official-doc-writer/scripts/dialog_manager.py +564 -0
- package/skills/core/official-doc-writer/scripts/generate_official_doc.py +252 -0
- package/skills/core/official-doc-writer/scripts/install_fonts.py +390 -0
- package/skills/core/official-doc-writer/scripts/smart_prompts.py +363 -0
- package/skills/core/pdd-ba/SKILL.md +305 -0
- package/skills/core/pdd-ba/_meta.json +1 -0
- package/skills/core/pdd-ba/evals/default-evals.json +1 -0
- package/skills/core/pdd-code-reviewer/SKILL.md +378 -0
- package/skills/core/pdd-code-reviewer/_meta.json +1 -0
- package/skills/core/pdd-code-reviewer/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-change/SKILL.md +350 -0
- package/skills/core/pdd-doc-change/_meta.json +1 -0
- package/skills/core/pdd-doc-change/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-gardener/SKILL.md +248 -0
- package/skills/core/pdd-doc-gardener/_meta.json +1 -0
- package/skills/core/pdd-doc-gardener/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/SKILL.md +360 -0
- package/skills/core/pdd-entropy-reduction/_meta.json +1 -0
- package/skills/core/pdd-entropy-reduction/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/references/entropy-report-template.md +287 -0
- package/skills/core/pdd-entropy-reduction/references/golden-principles.md +573 -0
- package/skills/core/pdd-entropy-reduction/scripts/entropy_scan.py +712 -0
- package/skills/core/pdd-extract-features/SKILL.md +320 -0
- package/skills/core/pdd-extract-features/_meta.json +1 -0
- package/skills/core/pdd-extract-features/evals/default-evals.json +1 -0
- package/skills/core/pdd-generate-spec/SKILL.md +418 -0
- package/skills/core/pdd-generate-spec/_meta.json +1 -0
- package/skills/core/pdd-generate-spec/evals/default-evals.json +1 -0
- package/skills/core/pdd-implement-feature/SKILL.md +332 -0
- package/skills/core/pdd-implement-feature/_meta.json +1 -0
- package/skills/core/pdd-implement-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/SKILL.md +540 -0
- package/skills/core/pdd-main/_meta.json +1 -0
- package/skills/core/pdd-main/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/evals/evals.json +215 -0
- package/skills/core/pdd-verify-feature/SKILL.md +474 -0
- package/skills/core/pdd-verify-feature/_meta.json +1 -0
- package/skills/core/pdd-verify-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-vm/evals/default-evals.json +1 -0
- package/skills/core/traffic-accident-assessor/LICENSE +29 -0
- package/skills/core/traffic-accident-assessor/SKILL.md +439 -0
- package/skills/core/traffic-accident-assessor/evals/evals.json +1 -0
- package/skills/core/traffic-accident-assessor/references/accident-types.md +369 -0
- package/skills/core/traffic-accident-assessor/references/liability-rules.md +287 -0
- package/skills/core/traffic-accident-assessor/references/traffic-laws.md +226 -0
- package/skills/core/traffic-accident-assessor/references//351/253/230/345/260/224/345/244/253/350/257/264/346/230/216/344/271/246.pdf +32576 -106
- package/skills/core/traffic-accident-assessor/scripts/generate_official_statement.py +588 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_report.py +495 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_statement.py +528 -0
- package/skills/core/traffic-accident-assessor.zip +0 -0
- package/skills/entropy/expert-arch-enforcer/SKILL.md +292 -0
- package/skills/entropy/expert-arch-enforcer/_meta.json +1 -0
- package/skills/entropy/expert-arch-enforcer/evals/default-evals.json +1 -0
- package/skills/entropy/expert-auto-refactor/SKILL.md +327 -0
- package/skills/entropy/expert-auto-refactor/_meta.json +1 -0
- package/skills/entropy/expert-auto-refactor/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/SKILL.md +468 -0
- package/skills/entropy/expert-code-quality/_meta.json +1 -0
- package/skills/entropy/expert-code-quality/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/evals/evals.json +109 -0
- package/skills/entropy/expert-code-quality/references/code-smells.md +605 -0
- package/skills/entropy/expert-code-quality/references/design-patterns.md +1111 -0
- package/skills/entropy/expert-code-quality/references/refactoring-catalog.md +1281 -0
- package/skills/entropy/expert-code-quality/references/solid-principles.md +524 -0
- package/skills/entropy/expert-entropy-auditor/SKILL.md +276 -0
- package/skills/entropy/expert-entropy-auditor/_meta.json +1 -0
- package/skills/entropy/expert-entropy-auditor/evals/default-evals.json +1 -0
- package/skills/expert/expert-activiti/SKILL.md +497 -0
- package/skills/expert/expert-activiti/_meta.json +1 -0
- package/skills/expert/expert-mysql/SKILL.md +832 -0
- package/skills/expert/expert-mysql/_meta.json +1 -0
- package/skills/expert/expert-performance/SKILL.md +379 -0
- package/skills/expert/expert-performance/_meta.json +1 -0
- package/skills/expert/expert-performance/evals/default-evals.json +1 -0
- package/skills/expert/expert-ruoyi/SKILL.md +472 -0
- package/skills/expert/expert-ruoyi/_meta.json +1 -0
- package/skills/expert/expert-security/SKILL.md +1341 -0
- package/skills/expert/expert-security/_meta.json +1 -0
- package/skills/expert/expert-security/evals/default-evals.json +1 -0
- package/skills/expert/software-architect/SKILL.md +350 -0
- package/skills/expert/software-architect/_meta.json +1 -0
- package/skills/expert/software-engineer/SKILL.md +437 -0
- package/skills/expert/software-engineer/_meta.json +1 -0
- package/skills/expert/software-engineer/architecture.md +130 -0
- package/skills/expert/software-engineer/patterns.md +151 -0
- package/skills/expert/software-engineer/testing.md +135 -0
- package/skills/expert/system-architect/SKILL.md +628 -0
- package/skills/expert/system-architect/_meta.json +1 -0
- package/skills/expert/system-architect/assets/templates/ARCHITECTURE.md +25 -0
- package/skills/expert/system-architect/assets/templates/README.md +44 -0
- package/skills/expert/system-architect/references/js-ts-standards.md +18 -0
- package/skills/expert/system-architect/references/python-standards.md +19 -0
- package/skills/expert/system-architect/references/scaffolding.md +61 -0
- package/skills/expert/system-architect/references/security-checklist.md +21 -0
- package/skills/openspec/openspec-apply-change/SKILL.md +156 -0
- package/skills/openspec/openspec-apply-change/_meta.json +1 -0
- package/skills/openspec/openspec-archive-change/SKILL.md +114 -0
- package/skills/openspec/openspec-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-bulk-archive-change/SKILL.md +246 -0
- package/skills/openspec/openspec-bulk-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-continue-change/SKILL.md +118 -0
- package/skills/openspec/openspec-continue-change/_meta.json +1 -0
- package/skills/openspec/openspec-explore/SKILL.md +288 -0
- package/skills/openspec/openspec-explore/_meta.json +1 -0
- package/skills/openspec/openspec-ff-change/SKILL.md +101 -0
- package/skills/openspec/openspec-ff-change/_meta.json +1 -0
- package/skills/openspec/openspec-new-change/SKILL.md +74 -0
- package/skills/openspec/openspec-new-change/_meta.json +1 -0
- package/skills/openspec/openspec-onboard/SKILL.md +554 -0
- package/skills/openspec/openspec-onboard/_meta.json +1 -0
- package/skills/openspec/openspec-sync-specs/SKILL.md +138 -0
- package/skills/openspec/openspec-sync-specs/_meta.json +1 -0
- package/skills/openspec/openspec-verify-change/SKILL.md +168 -0
- package/skills/openspec/openspec-verify-change/_meta.json +1 -0
- package/skills/pr/pdd-multi-review/SKILL.md +534 -0
- package/skills/pr/pdd-multi-review/_meta.json +1 -0
- package/skills/pr/pdd-pr-batch/SKILL.md +303 -0
- package/skills/pr/pdd-pr-batch/_meta.json +1 -0
- package/skills/pr/pdd-pr-create/SKILL.md +344 -0
- package/skills/pr/pdd-pr-create/_meta.json +1 -0
- package/skills/pr/pdd-pr-merge/SKILL.md +286 -0
- package/skills/pr/pdd-pr-merge/_meta.json +1 -0
- package/skills/pr/pdd-pr-review/SKILL.md +217 -0
- package/skills/pr/pdd-pr-review/_meta.json +1 -0
- package/skills/pr/pdd-task-manager/SKILL.md +636 -0
- package/skills/pr/pdd-task-manager/_meta.json +1 -0
- package/skills/pr/pdd-template-engine/SKILL.md +306 -0
- package/skills/pr/pdd-template-engine/_meta.json +1 -0
- package/templates/behavior-shaping/iron-law-template.md +87 -0
- package/templates/behavior-shaping/rationalization-template.md +62 -0
- package/templates/behavior-shaping/red-flags-template.md +70 -0
- package/templates/bilingual-template.md +139 -0
- package/templates/config/default.yaml +47 -0
- package/templates/project/default/README.md +31 -0
- package/templates/project/frontend/README.md +46 -0
- package/templates/project/java/README.md +48 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PDD Visual Manager - Dashboard HTTP Server (VM-B001)
|
|
3
|
+
*
|
|
4
|
+
* 基于 Node.js 内置 http 模块的 Dashboard 服务器
|
|
5
|
+
* 提供静态文件托管、REST API 路由和 SSE 端点
|
|
6
|
+
*
|
|
7
|
+
* @module vm/dashboard/server
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import http from 'http';
|
|
11
|
+
import { fileURLToPath } from 'url';
|
|
12
|
+
import path from 'path';
|
|
13
|
+
import fs from 'fs';
|
|
14
|
+
import { exec } from 'child_process';
|
|
15
|
+
|
|
16
|
+
import { routeMatcher, handleAPIRequest } from './api-routes.js';
|
|
17
|
+
import { SSEManager } from './sse.js';
|
|
18
|
+
|
|
19
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
20
|
+
const __dirname = path.dirname(__filename);
|
|
21
|
+
|
|
22
|
+
// 可选的 chalk 彩色输出支持
|
|
23
|
+
let chalk;
|
|
24
|
+
try {
|
|
25
|
+
const chalkModule = await import('chalk');
|
|
26
|
+
chalk = chalkModule.default;
|
|
27
|
+
} catch {
|
|
28
|
+
chalk = {
|
|
29
|
+
cyan: (s) => s,
|
|
30
|
+
green: (s) => s,
|
|
31
|
+
yellow: (s) => s,
|
|
32
|
+
red: (s) => s,
|
|
33
|
+
blue: (s) => s,
|
|
34
|
+
magenta: (s) => s,
|
|
35
|
+
bold: (s) => s
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* MIME 类型映射表
|
|
41
|
+
* @type {Object<string, string>}
|
|
42
|
+
*/
|
|
43
|
+
const MIME_TYPES = {
|
|
44
|
+
'.html': 'text/html; charset=utf-8',
|
|
45
|
+
'.css': 'text/css; charset=utf-8',
|
|
46
|
+
'.js': 'application/javascript; charset=utf-8',
|
|
47
|
+
'.json': 'application/json; charset=utf-8',
|
|
48
|
+
'.png': 'image/png',
|
|
49
|
+
'.jpg': 'image/jpeg',
|
|
50
|
+
'.jpeg': 'image/jpeg',
|
|
51
|
+
'.gif': 'image/gif',
|
|
52
|
+
'.svg': 'image/svg+xml',
|
|
53
|
+
'.ico': 'image/x-icon',
|
|
54
|
+
'.woff': 'font/woff',
|
|
55
|
+
'.woff2': 'font/woff2',
|
|
56
|
+
'.ttf': 'font/ttf'
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 默认配置选项
|
|
61
|
+
* @type {Object}
|
|
62
|
+
*/
|
|
63
|
+
const DEFAULT_OPTIONS = {
|
|
64
|
+
open: false,
|
|
65
|
+
host: '127.0.0.1',
|
|
66
|
+
refreshInterval: 30000
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* DashboardServer 类
|
|
71
|
+
* 管理 Dashboard 的 HTTP 服务器生命周期
|
|
72
|
+
*/
|
|
73
|
+
class DashboardServer {
|
|
74
|
+
/**
|
|
75
|
+
* 创建 DashboardServer 实例
|
|
76
|
+
* @param {Object} dataProvider - PDDDataProvider 实例
|
|
77
|
+
*/
|
|
78
|
+
constructor(dataProvider) {
|
|
79
|
+
this.dataProvider = dataProvider;
|
|
80
|
+
this.server = null;
|
|
81
|
+
this.sseManager = new SSEManager();
|
|
82
|
+
this.port = null;
|
|
83
|
+
this.options = { ...DEFAULT_OPTIONS };
|
|
84
|
+
this._isRunning = false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 启动 HTTP 服务器
|
|
89
|
+
* @param {number} port - 监听端口号
|
|
90
|
+
* @param {Object} [options={}] - 配置选项
|
|
91
|
+
* @param {boolean} [options.open=false] - 是否自动打开浏览器
|
|
92
|
+
* @param {string} [options.host='127.0.0.1'] - 绑定地址
|
|
93
|
+
* @param {number} [options.refreshInterval=30000] - 刷新间隔(毫秒)
|
|
94
|
+
* @returns {Promise<number>} 实际监听的端口号
|
|
95
|
+
*/
|
|
96
|
+
async start(port, options = {}) {
|
|
97
|
+
if (this._isRunning) {
|
|
98
|
+
console.log(chalk.yellow('[Dashboard] 服务器已在运行中'));
|
|
99
|
+
return this.port;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 合并配置选项
|
|
103
|
+
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
104
|
+
|
|
105
|
+
// 尝试绑定端口(自动处理端口冲突)
|
|
106
|
+
const actualPort = await this._findAvailablePort(port, this.options.host);
|
|
107
|
+
this.port = actualPort;
|
|
108
|
+
|
|
109
|
+
// 创建 HTTP 服务器
|
|
110
|
+
this.server = http.createServer((req, res) => this._handleRequest(req, res));
|
|
111
|
+
|
|
112
|
+
// 启动 SSE 心跳
|
|
113
|
+
this.sseManager.startHeartbeat();
|
|
114
|
+
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
this.server.listen(actualPort, this.options.host, () => {
|
|
117
|
+
this._isRunning = true;
|
|
118
|
+
const url = `http://${this.options.host}:${actualPort}`;
|
|
119
|
+
console.log(chalk.green(`[Dashboard] 服务器已启动: ${chalk.cyan(url)}`));
|
|
120
|
+
console.log(chalk.gray(`[Dashboard] 按 Ctrl+C 停止服务器`));
|
|
121
|
+
|
|
122
|
+
// 自动打开浏览器
|
|
123
|
+
if (this.options.open) {
|
|
124
|
+
this._openBrowser(url);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
resolve(actualPort);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
this.server.on('error', (err) => {
|
|
131
|
+
console.error(chalk.red(`[Dashboard] 服务器启动失败:`), err.message);
|
|
132
|
+
reject(err);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 优雅关闭服务器
|
|
139
|
+
* @returns {Promise<void>}
|
|
140
|
+
*/
|
|
141
|
+
async stop() {
|
|
142
|
+
if (!this._isRunning) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
console.log(chalk.yellow('[Dashboard] 正在关闭服务器...'));
|
|
147
|
+
|
|
148
|
+
// 关闭所有 SSE 连接
|
|
149
|
+
this.sseManager.closeAll();
|
|
150
|
+
|
|
151
|
+
// 停止心跳定时器
|
|
152
|
+
this.sseManager.stopHeartbeat();
|
|
153
|
+
|
|
154
|
+
// 关闭 HTTP 服务器
|
|
155
|
+
return new Promise((resolve) => {
|
|
156
|
+
this.server.close(() => {
|
|
157
|
+
this._isRunning = false;
|
|
158
|
+
this.server = null;
|
|
159
|
+
this.port = null;
|
|
160
|
+
console.log(chalk.green('[Dashboard] 服务器已关闭'));
|
|
161
|
+
resolve();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// 强制关闭超时连接(5秒后强制关闭)
|
|
165
|
+
setTimeout(() => {
|
|
166
|
+
if (this._isRunning) {
|
|
167
|
+
// 对于仍在运行的连接,直接销毁
|
|
168
|
+
resolve();
|
|
169
|
+
}
|
|
170
|
+
}, 5000);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 获取服务器状态
|
|
176
|
+
* @returns {boolean} 服务器是否运行中
|
|
177
|
+
*/
|
|
178
|
+
isRunning() {
|
|
179
|
+
return this._isRunning;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 获取当前监听 URL
|
|
184
|
+
* @returns {string|null}
|
|
185
|
+
*/
|
|
186
|
+
getUrl() {
|
|
187
|
+
if (!this._isRunning || !this.port) return null;
|
|
188
|
+
return `http://${this.options.host}:${this.port}`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 处理 HTTP 请求(内部方法)
|
|
193
|
+
* @param {http.IncomingMessage} req
|
|
194
|
+
* @param {http.ServerResponse} res
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
_handleRequest(req, res) {
|
|
198
|
+
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
|
|
199
|
+
const pathname = decodedURI(parsedUrl.pathname);
|
|
200
|
+
|
|
201
|
+
// 设置 CORS 头
|
|
202
|
+
this._setCORSHeaders(res);
|
|
203
|
+
|
|
204
|
+
// 处理 OPTIONS 预检请求
|
|
205
|
+
if (req.method === 'OPTIONS') {
|
|
206
|
+
res.writeHead(204);
|
|
207
|
+
res.end();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
// 路由分发
|
|
213
|
+
if (pathname === '/' || pathname === '/index.html') {
|
|
214
|
+
this._serveStaticFile(res, 'index.html', 'text/html; charset=utf-8');
|
|
215
|
+
} else if (pathname.startsWith('/api/')) {
|
|
216
|
+
// API 路由
|
|
217
|
+
handleAPIRequest(req, res, pathname, this.dataProvider, this.sseManager);
|
|
218
|
+
} else if (pathname === '/sse') {
|
|
219
|
+
// SSE 端点
|
|
220
|
+
this.sseManager.handleConnection(req, res);
|
|
221
|
+
} else if (pathname.startsWith('/css/') || pathname.startsWith('/js/') || pathname.startsWith('/assets/')) {
|
|
222
|
+
// 静态文件
|
|
223
|
+
this._serveStaticFile(res, pathname.substring(1));
|
|
224
|
+
} else {
|
|
225
|
+
// 404
|
|
226
|
+
this._sendJSON(res, 404, { error: 'Not Found', path: pathname });
|
|
227
|
+
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
console.error(chalk.red(`[Dashboard] 请求处理错误:`), err);
|
|
230
|
+
this._sendJSON(res, 500, { error: 'Internal Server Error', message: err.message });
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 设置 CORS 响应头
|
|
236
|
+
* @param {http.ServerResponse} res
|
|
237
|
+
* @private
|
|
238
|
+
*/
|
|
239
|
+
_setCORSHeaders(res) {
|
|
240
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
241
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
242
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept');
|
|
243
|
+
res.setHeader('Access-Control-Max-Age', '86400');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 提供静态文件服务
|
|
248
|
+
* @param {http.ServerResponse} res
|
|
249
|
+
* @param {string} filePath - 相对于 static 目录的文件路径
|
|
250
|
+
* @param {string} [contentType] - 显式指定的 Content-Type
|
|
251
|
+
* @private
|
|
252
|
+
*/
|
|
253
|
+
_serveStaticFile(res, filePath, contentType) {
|
|
254
|
+
const staticDir = path.join(__dirname, 'static');
|
|
255
|
+
const fullPath = path.join(staticDir, filePath);
|
|
256
|
+
|
|
257
|
+
// 安全检查:防止路径遍历攻击
|
|
258
|
+
if (!fullPath.startsWith(staticDir)) {
|
|
259
|
+
this._sendJSON(res, 403, { error: 'Forbidden' });
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
fs.readFile(fullPath, (err, data) => {
|
|
264
|
+
if (err) {
|
|
265
|
+
if (err.code === 'ENOENT') {
|
|
266
|
+
this._sendJSON(res, 404, { error: 'File Not Found', path: filePath });
|
|
267
|
+
} else {
|
|
268
|
+
this._sendJSON(res, 500, { error: 'Internal Server Error' });
|
|
269
|
+
}
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// 确定 Content-Type
|
|
274
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
275
|
+
const type = contentType || MIME_TYPES[ext] || 'application/octet-stream';
|
|
276
|
+
|
|
277
|
+
res.writeHead(200, {
|
|
278
|
+
'Content-Type': type,
|
|
279
|
+
'Content-Length': data.length,
|
|
280
|
+
'Cache-Control': 'public, max-age=3600'
|
|
281
|
+
});
|
|
282
|
+
res.end(data);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 发送 JSON 响应
|
|
288
|
+
* @param {http.ServerResponse} res
|
|
289
|
+
* @param {number} statusCode
|
|
290
|
+
* @param {Object} data
|
|
291
|
+
* @private
|
|
292
|
+
*/
|
|
293
|
+
_sendJSON(res, statusCode, data) {
|
|
294
|
+
const body = JSON.stringify(data);
|
|
295
|
+
res.writeHead(statusCode, {
|
|
296
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
297
|
+
'Content-Length': Buffer.byteLength(body)
|
|
298
|
+
});
|
|
299
|
+
res.end(body);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 查找可用端口(带冲突检测)
|
|
304
|
+
* @param {number} startPort - 起始端口
|
|
305
|
+
* @param {string} host - 绑定地址
|
|
306
|
+
* @returns {Promise<number>} 可用端口
|
|
307
|
+
* @private
|
|
308
|
+
*/
|
|
309
|
+
async _findAvailablePort(startPort, host) {
|
|
310
|
+
let port = startPort;
|
|
311
|
+
const maxAttempts = 100;
|
|
312
|
+
|
|
313
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
314
|
+
const isAvailable = await this._checkPortAvailable(port, host);
|
|
315
|
+
if (isAvailable) {
|
|
316
|
+
return port;
|
|
317
|
+
}
|
|
318
|
+
console.log(chalk.yellow(`[Dashboard] 端口 ${port} 已占用,尝试 ${port + 1}`));
|
|
319
|
+
port++;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
throw new Error(`无法在 ${startPort}-${port} 范围内找到可用端口`);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 检查端口是否可用
|
|
327
|
+
* @param {number} port
|
|
328
|
+
* @param {string} host
|
|
329
|
+
* @returns {Promise<boolean>}
|
|
330
|
+
* @private
|
|
331
|
+
*/
|
|
332
|
+
_checkPortAvailable(port, host) {
|
|
333
|
+
return new Promise((resolve) => {
|
|
334
|
+
const tester = http.createServer();
|
|
335
|
+
|
|
336
|
+
tester.once('error', () => resolve(false));
|
|
337
|
+
tester.once('listening', () => {
|
|
338
|
+
tester.once('close', () => resolve(true));
|
|
339
|
+
tester.close();
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
tester.listen(port, host);
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* 自动打开浏览器
|
|
348
|
+
* @param {string} url - 要打开的 URL
|
|
349
|
+
* @private
|
|
350
|
+
*/
|
|
351
|
+
_openBrowser(url) {
|
|
352
|
+
const platform = process.platform;
|
|
353
|
+
let command;
|
|
354
|
+
|
|
355
|
+
switch (platform) {
|
|
356
|
+
case 'darwin':
|
|
357
|
+
command = `open "${url}"`;
|
|
358
|
+
break;
|
|
359
|
+
case 'win32':
|
|
360
|
+
command = `start "" "${url}"`;
|
|
361
|
+
break;
|
|
362
|
+
default:
|
|
363
|
+
command = `xdg-open "${url}"`;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
exec(command, (err) => {
|
|
368
|
+
if (err) {
|
|
369
|
+
console.log(chalk.yellow(`[Dashboard] 无法自动打开浏览器,请手动访问: ${url}`));
|
|
370
|
+
} else {
|
|
371
|
+
console.log(chalk.green('[Dashboard] 已在默认浏览器中打开'));
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* 解码 URI(处理中文路径)
|
|
379
|
+
* @param {string} uri
|
|
380
|
+
* @returns {string}
|
|
381
|
+
*/
|
|
382
|
+
function decodedURI(uri) {
|
|
383
|
+
try {
|
|
384
|
+
return decodeURIComponent(uri);
|
|
385
|
+
} catch {
|
|
386
|
+
return uri;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export { DashboardServer };
|
|
391
|
+
export default DashboardServer;
|