dsh-plugin 1.6.5 → 1.6.7
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.en.md +11 -11
- package/README.md +11 -11
- package/client/client.js +421 -390
- package/client/client.js.map +1 -1
- package/lib/http/catalog.js +132 -0
- package/lib/http/diagnostics.js +201 -0
- package/lib/http/install.js +399 -0
- package/lib/http/logs.js +164 -0
- package/lib/http/routes.js +19 -1263
- package/lib/http/settings.js +77 -0
- package/lib/http/shared.js +169 -0
- package/lib/http/task.js +208 -0
- package/lib/services/install/install.js +1 -1
- package/lib/services/install/package-entry.js +59 -8
- package/lib/services/install/preflight.js +47 -17
- package/lib/services/install/task-queue.js +75 -31
- package/lib/services/probe.js +1 -1
- package/lib/types/http/catalog.d.ts +10 -0
- package/lib/types/http/diagnostics.d.ts +4 -0
- package/lib/types/http/install.d.ts +11 -0
- package/lib/types/http/logs.d.ts +2 -0
- package/lib/types/http/routes.d.ts +15 -20
- package/lib/types/http/settings.d.ts +2 -0
- package/lib/types/http/shared.d.ts +44 -0
- package/lib/types/http/task.d.ts +3 -0
- package/lib/types/services/install/install.d.ts +1 -1
- package/lib/types/services/install/package-entry.d.ts +42 -10
- package/lib/types/services/install/preflight.d.ts +9 -1
- package/lib/types/services/install/task-queue.d.ts +6 -4
- package/package.json +1 -1
- package/src/client/components/PluginHubSection.tsx +3 -0
- package/src/client/components/modals/NotificationsModal.tsx +34 -18
- package/src/client/components/modals/modals.tsx +38 -28
- package/src/client/components/views/InstalledView.tsx +23 -11
- package/src/client/hooks/useCatalog.ts +1 -1
- package/src/client/locales.ts +28 -12
- package/src/client/logic/failures.ts +36 -157
- package/src/client/logic/urls.ts +75 -4
- package/src/client/types.ts +1 -1
- package/src/server/http/catalog.ts +143 -0
- package/src/server/http/diagnostics.ts +193 -0
- package/src/server/http/install.ts +391 -0
- package/src/server/http/logs.ts +149 -0
- package/src/server/http/routes.ts +22 -1222
- package/src/server/http/settings.ts +73 -0
- package/src/server/http/shared.ts +191 -0
- package/src/server/http/task.ts +194 -0
- package/src/server/services/install/install-types.ts +9 -1
- package/src/server/services/install/install.ts +11 -1
- package/src/server/services/install/package-entry.ts +72 -9
- package/src/server/services/install/preflight.ts +47 -18
- package/src/server/services/install/task-queue.ts +73 -34
- package/src/server/services/probe.ts +1 -1
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH Plugin Hub — the community plugin marketplace for DeepSeek Harness.
|
|
3
|
+
* Website: https://dsh-plugin.org
|
|
4
|
+
* GitHub: https://github.com/dshplugin/dsh-plugin-hub
|
|
5
|
+
*
|
|
6
|
+
* Install/uninstall routes: validate the target and spawn the official dsh CLI
|
|
7
|
+
* (see services/install/install.ts), reporting the captured result back.
|
|
8
|
+
*/
|
|
9
|
+
import { githubRefOf, githubRepoOf, githubTarget, globalNpmPackagesOf, hasQueuedTarget, installTargetOf, startPluginMutation, validPackageName } from '../services/install/install.js';
|
|
10
|
+
import { githubReleaseTarget } from '../services/install/release-target.js';
|
|
11
|
+
import { recordResolvedNpmPackage } from '../services/profile/installed-versions.js';
|
|
12
|
+
import { isDshNpmPackageForRepo, resolveNpmPackage } from '../services/install/npm-resolve.js';
|
|
13
|
+
import { preflightTarget } from '../services/install/preflight.js';
|
|
14
|
+
import { gitLsRemote, probeUrl, systemProxy } from '../services/probe.js';
|
|
15
|
+
import { loadSettings } from '../services/settings.js';
|
|
16
|
+
import { installedRepoOf, readInstalled } from './catalog.js';
|
|
17
|
+
import { COMMAND_TIMEOUT_MS, readJsonBody, requireTrustedPost, sendJson } from './shared.js';
|
|
18
|
+
/** 安装/卸载 CLI 的子进程环境:透传宿主环境,并按设置注入 HTTP(S) 代理。
|
|
19
|
+
* 不注入 npm registry —— 安装完全沿用用户本机 npm 配置(~/.npmrc / 全局配置)。
|
|
20
|
+
* 代理优先级:设置里的代理 → 系统代理(macOS scutil / Windows 注册表)→ 宿主 env 原有值。
|
|
21
|
+
* 使安装通道与诊断使用相同的代理来源。 */
|
|
22
|
+
function mutationEnv(settings) {
|
|
23
|
+
const env = { ...process.env, CI: 'true' };
|
|
24
|
+
const proxy = settings.proxy !== '' ? settings.proxy : (systemProxy() ?? '');
|
|
25
|
+
if (proxy !== '') {
|
|
26
|
+
env.HTTP_PROXY = proxy;
|
|
27
|
+
env.HTTPS_PROXY = proxy;
|
|
28
|
+
env.http_proxy = proxy;
|
|
29
|
+
env.https_proxy = proxy;
|
|
30
|
+
}
|
|
31
|
+
return env;
|
|
32
|
+
}
|
|
33
|
+
/** Match a GitHub install target against the installed table; returns the package name when already installed. */
|
|
34
|
+
function installedByRepo(installed, target) {
|
|
35
|
+
const repo = githubRepoOf(target);
|
|
36
|
+
if (repo === null)
|
|
37
|
+
return null;
|
|
38
|
+
const needle = repo.toLowerCase();
|
|
39
|
+
for (const [name, spec] of Object.entries(installed)) {
|
|
40
|
+
if (githubRepoOf(spec)?.toLowerCase() === needle)
|
|
41
|
+
return name;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
export function registerInstallRoutes(webServer, profile, loader) {
|
|
46
|
+
return [
|
|
47
|
+
{
|
|
48
|
+
kind: 'exact',
|
|
49
|
+
path: '/dsh-plugin-hub/install',
|
|
50
|
+
handler: async (request, response) => {
|
|
51
|
+
if (!requireTrustedPost(request, response))
|
|
52
|
+
return;
|
|
53
|
+
// 任务自动入队:即使已有插件操作在跑也接受请求(FIFO 串行执行),不做 409 拒绝
|
|
54
|
+
try {
|
|
55
|
+
const body = await readJsonBody(request);
|
|
56
|
+
// mode: 'update' = 已安装目标的覆盖更新(放行 add,pnpm 对已存在依赖原位覆盖重装)。
|
|
57
|
+
// 后续「同仓库残留」检测可能把 install 就地升级为 update(见包名冲突检测),故用 let。
|
|
58
|
+
let mode = body !== null && typeof body === 'object' && body.mode === 'update'
|
|
59
|
+
? 'update'
|
|
60
|
+
: 'install';
|
|
61
|
+
// installTargetOf 先把完整 DSH 命令(`dsh plugin [--profile <p>] add <target>`)剥成裸目标,
|
|
62
|
+
// 兼容用户直接粘贴命令行(防输错),真正语法判定交给下面 githubRepoOf / validPackageName。
|
|
63
|
+
const rawRepo = typeof body === 'object' && body !== null && typeof body.repo === 'string'
|
|
64
|
+
? installTargetOf(body.repo)
|
|
65
|
+
: '';
|
|
66
|
+
// 前端决策的展示用仓库名(owner/repo):npm 安装时 body.repo 是包名,display 用于
|
|
67
|
+
// 待重启行 / 任务恢复时展示仓库名,保证用户无感知(安装通道变化不改变所见目标)
|
|
68
|
+
const displayRepo = typeof body === 'object' && body !== null && typeof body.display === 'string'
|
|
69
|
+
? body.display.trim()
|
|
70
|
+
: rawRepo;
|
|
71
|
+
// 请求来源:目录插件安装(catalog)走目录白名单;命令行安装(custom)按通道受安全开关控制
|
|
72
|
+
const source = typeof body === 'object' && body !== null && typeof body.source === 'string'
|
|
73
|
+
? body.source
|
|
74
|
+
: '';
|
|
75
|
+
// 自定义安装入口渠道(客户端三张卡片:NPM 包 / GitHub 源码 / DSH 命令):
|
|
76
|
+
// 客户端显式上报,服务端日志/报错据此精准溯源「从哪个入口发起」;目录插件安装不传。
|
|
77
|
+
const installChannel = (() => {
|
|
78
|
+
const raw = body !== null && typeof body === 'object' ? body.installChannel : undefined;
|
|
79
|
+
return raw === 'git' || raw === 'dsh' || raw === 'npm' ? raw : undefined;
|
|
80
|
+
})();
|
|
81
|
+
const settings = loadSettings(profile);
|
|
82
|
+
// 界面语言(客户端随请求携带):错误消息按用户当前语言提示(中文界面给中文,英文界面给英文)
|
|
83
|
+
const lang = typeof body === 'object' && body !== null && body.lang === 'en'
|
|
84
|
+
? 'en'
|
|
85
|
+
: 'zh';
|
|
86
|
+
// 全局 npm 安装(官方 README 的 `npm install -g <pkgs>`,如 dsh-tui 的安装命令):
|
|
87
|
+
// 直接从命令框输入格式执行全局安装,不进任何 profile、无需宿主重启。
|
|
88
|
+
// 包列表优先取 body.globalNpm(客户端命令框解析后显式携带);兜底再从 rawRepo 解析,
|
|
89
|
+
// 保证直接调 API 传完整命令也能识别。
|
|
90
|
+
const bodyGlobalNpm = Array.isArray(body.globalNpm)
|
|
91
|
+
? body.globalNpm.filter((x) => typeof x === 'string' && x !== '')
|
|
92
|
+
: [];
|
|
93
|
+
let globalNpm = bodyGlobalNpm.length > 0 ? bodyGlobalNpm : (globalNpmPackagesOf(rawRepo) ?? []);
|
|
94
|
+
if (globalNpm.length > 0) {
|
|
95
|
+
// 包名逐个按 npm 包名语法校验 + 拒绝以 - 开头的 token(npm 会把 --xxx 当参数而非包名),数量封顶防滥用
|
|
96
|
+
if (globalNpm.length > 20 || globalNpm.some((p) => !validPackageName(p) || p.startsWith('-'))) {
|
|
97
|
+
sendJson(response, 400, { error: 'unsupported install target' });
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
// 通道门禁与安装通道同构:全局安装本质是 npm 操作,吃「启用 NPM 安装」开关
|
|
101
|
+
if (source !== 'catalog' && !settings.enableNpmInstall) {
|
|
102
|
+
sendJson(response, 403, { error: 'npm installs are disabled by the security settings' });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
// 队列级去重:同一包列表已在排队/执行中时拒绝
|
|
106
|
+
const identity = globalNpm.join(' ');
|
|
107
|
+
if (hasQueuedTarget(identity)) {
|
|
108
|
+
sendJson(response, 409, { error: `already queued: ${identity}` });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const task = startPluginMutation({
|
|
112
|
+
action: 'add',
|
|
113
|
+
profile,
|
|
114
|
+
target: identity,
|
|
115
|
+
displayTarget: identity,
|
|
116
|
+
globalNpm,
|
|
117
|
+
timeoutMs: COMMAND_TIMEOUT_MS,
|
|
118
|
+
env: mutationEnv(settings),
|
|
119
|
+
// 全局 npm 安装由 NPM 包卡片/官方命令发起:日志体现入口渠道
|
|
120
|
+
installChannel: installChannel ?? 'npm',
|
|
121
|
+
});
|
|
122
|
+
sendJson(response, 200, { ok: true, task: task.id });
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// 目标语法三态:固定 GitHub release .tgz(保留其包根目录,不转成仓库 HEAD);
|
|
126
|
+
// GitHub 地址(owner/repo、github:、https/ssh 链接等任意写法)→ 显式 HTTPS Git 源
|
|
127
|
+
// (走装前预检);npm 包名(@scope/name 或 name)→ 信任 registry 直接安装。
|
|
128
|
+
// githubRepoOf 先把各种 GitHub 地址归一成 owner/repo,让「输入地址即装」兼容粘贴完整链接。
|
|
129
|
+
const gitRepo = githubRepoOf(rawRepo);
|
|
130
|
+
const release = githubReleaseTarget(rawRepo);
|
|
131
|
+
// 显式 pin 的 Git ref(`#tag` / `#commit`)是用户意图:保留它,且不让 npm 反查
|
|
132
|
+
// 把这个精确修订换成 registry 上的默认版本。
|
|
133
|
+
const pinnedRef = githubRefOf(rawRepo);
|
|
134
|
+
const repoTarget = gitRepo !== null && release === null ? githubTarget(gitRepo, pinnedRef) : null;
|
|
135
|
+
let target = release?.target ?? repoTarget ?? (validPackageName(rawRepo) ? rawRepo : null);
|
|
136
|
+
if (target === null) {
|
|
137
|
+
sendJson(response, 400, { error: 'unsupported install target' });
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const attempts = [];
|
|
141
|
+
// 代理口径与诊断/安装通道一致:设置里的代理 → 系统代理 → 环境变量 → 直连。
|
|
142
|
+
// 供 npm 反查时跟随 GitHub 改名跳转用(Node https 读不到系统代理,交给 curl)。
|
|
143
|
+
const effectiveProxy = settings.proxy !== ''
|
|
144
|
+
? settings.proxy
|
|
145
|
+
: (systemProxy() ?? process.env.HTTPS_PROXY ?? process.env.https_proxy ?? '');
|
|
146
|
+
// Catalog metadata may outlive a packaging split: one repository can publish both a
|
|
147
|
+
// CLI/helper package and a dedicated DSH plugin package. Keep the catalog target when
|
|
148
|
+
// registry metadata confirms it is a DSH package for this repo. Otherwise inspect the
|
|
149
|
+
// repository root package.json and use its package name only when that exact registry
|
|
150
|
+
// package also points back to the repo and identifies as a DSH plugin. This avoids
|
|
151
|
+
// relying on npm search ranking, which is not a repository index and can omit the
|
|
152
|
+
// correct package even with a large result window.
|
|
153
|
+
const catalogRepo = source === 'catalog' ? githubRepoOf(displayRepo) : null;
|
|
154
|
+
if (catalogRepo !== null && release === null && repoTarget === null && validPackageName(rawRepo)) {
|
|
155
|
+
const catalogTargetIsDsh = await isDshNpmPackageForRepo(rawRepo, catalogRepo, settings.npmRegistry, effectiveProxy);
|
|
156
|
+
if (catalogTargetIsDsh) {
|
|
157
|
+
recordResolvedNpmPackage(profile, catalogRepo, rawRepo);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
const catalogGitTarget = githubTarget(catalogRepo);
|
|
161
|
+
if (catalogGitTarget !== null) {
|
|
162
|
+
const root = await preflightTarget(catalogGitTarget);
|
|
163
|
+
const rootName = root.ok && root.name !== null && validPackageName(root.name) ? root.name : null;
|
|
164
|
+
if (rootName !== null && rootName !== rawRepo
|
|
165
|
+
&& await isDshNpmPackageForRepo(rootName, catalogRepo, settings.npmRegistry, effectiveProxy)) {
|
|
166
|
+
attempts.push(`catalog npm target: \`${target}\` → repository root DSH package \`${rootName}\``);
|
|
167
|
+
target = rootName;
|
|
168
|
+
recordResolvedNpmPackage(profile, catalogRepo, rootName);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// npm 优先:git 目标先反查该仓库的官方 npm 包,命中则改走 npm 通道。
|
|
174
|
+
// git 分发常缺构建产物/子模块,npm 包是作者发布的完整产物;未命中或
|
|
175
|
+
// 查询失败保留 github 直装(不阻塞安装,预检仍会拦截缺入口文件的情况)。
|
|
176
|
+
// 通用机制,不针对具体插件。
|
|
177
|
+
// 反查本身计入「已尝试的安装方式」:组织 scope 与 GitHub 用户名不一致时仅凭
|
|
178
|
+
// 仓库名猜不到包名,失败提 Issue 时作者看到我们查过的命令就能直接指认正确包名。
|
|
179
|
+
// 显式 pin 了 Git ref 时跳过 npm 反查:用户要的就是那个精确修订,
|
|
180
|
+
// 不能用 registry 上的默认版本覆盖它。
|
|
181
|
+
if (repoTarget !== null && pinnedRef === null) {
|
|
182
|
+
// 反查/记录统一用归一化后的 owner/repo(gitRepo),保证「输入完整链接」也走同一套 npm 反查
|
|
183
|
+
const repoIdentity = gitRepo ?? rawRepo;
|
|
184
|
+
const npmName = await resolveNpmPackage(repoIdentity, settings.npmRegistry, effectiveProxy);
|
|
185
|
+
if (npmName !== null) {
|
|
186
|
+
target = npmName;
|
|
187
|
+
attempts.push(`npm registry search: \`npm search repository:${repoIdentity}\` → found \`${npmName}\``);
|
|
188
|
+
// 持久化 repo → npm 包名映射:目录数据未下发 npmPackage(组织 scope 与 GitHub
|
|
189
|
+
// 用户名不一致)时,客户端仍能通过 /installed 的 versions 把依赖 key 匹配回仓库,
|
|
190
|
+
// 列表立即显示「已安装」,避免安装成功后仍显示可安装
|
|
191
|
+
recordResolvedNpmPackage(profile, repoIdentity, npmName);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
attempts.push(`npm registry search: \`npm search repository:${repoIdentity}\` → no matching package found (falling back to git install)`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// 命令行安装(目录外)按通道门禁:GitHub 源码通道受「启用 GitHub 源码安装」控制,
|
|
198
|
+
// npm 通道受「启用 NPM 安装」控制 —— 目录插件安装走目录白名单,不受开关影响。
|
|
199
|
+
// 通道判定看反查后的最终 target:release 包与 git 目标同属 GitHub 通道,否则走 npm。
|
|
200
|
+
if (source !== 'catalog') {
|
|
201
|
+
const isGitChannel = release !== null || (repoTarget !== null && target === repoTarget);
|
|
202
|
+
if (isGitChannel && !settings.enableGitInstall) {
|
|
203
|
+
sendJson(response, 403, { error: 'git installs are disabled by the security settings' });
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!isGitChannel && !settings.enableNpmInstall) {
|
|
207
|
+
sendJson(response, 403, { error: 'npm installs are disabled by the security settings' });
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// 重复安装防护:非更新请求命中已安装目标时直接拒绝,不重复跑 CLI
|
|
212
|
+
// (否则 CLI 会因「已存在」失败,且用户看到的是莫名其妙的报错);
|
|
213
|
+
// 更新请求(mode: 'update')放行,CLI 的 add 对已存在依赖是 pnpm 原位覆盖重装。
|
|
214
|
+
// 命中判定双通道:npm 包名按 dependencies 键直接命中,GitHub 目标按仓库身份匹配。
|
|
215
|
+
const installed = readInstalled(profile);
|
|
216
|
+
const already = installedByRepo(installed, target) ?? (installed[target] !== undefined ? target : null);
|
|
217
|
+
if (already !== null && mode !== 'update') {
|
|
218
|
+
sendJson(response, 409, { error: `already installed: ${already}` });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
// 队列级去重:同一目标已在排队/执行中时拒绝,避免客户端防重竞态导致重复入队
|
|
222
|
+
if (hasQueuedTarget(target)) {
|
|
223
|
+
sendJson(response, 409, { error: `already queued: ${target}` });
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
// 安装前网络连通性预检:npm 通道探 registry、git 通道探 github.com 连通性。
|
|
227
|
+
// 探测目标固定为通道的稳定入口(registry 根 / github.com 主页),不探具体包或仓库
|
|
228
|
+
// 页 —— 404 表示「目标不存在」而非网络不通,不该被当成网络故障拦截。
|
|
229
|
+
// 仅新安装预检(更新是已信任目标的覆盖重装,跳过);不通直接 400 拦下并打
|
|
230
|
+
// [network] 标记,客户端据此提示「你的网络不通」,而不是把网络失败当成
|
|
231
|
+
// 插件侧问题引导去作者仓库提 Issue。
|
|
232
|
+
// 代理口径与诊断一致:设置里的代理 → 系统代理 → 环境变量 → 直连。
|
|
233
|
+
if (already === null) {
|
|
234
|
+
const isGitChannel = release !== null || (repoTarget !== null && target === repoTarget);
|
|
235
|
+
// release 包是 https://github.com/ 直链下载(非 git 克隆),探针按 HTTP 打 github.com 主页;
|
|
236
|
+
// npm 通道探 registry 根。两者都是通道的稳定入口,不探具体包/仓库(404 会被误判成网络故障)。
|
|
237
|
+
const httpProbeTarget = release !== null
|
|
238
|
+
? 'https://github.com/'
|
|
239
|
+
: `${(settings.npmRegistry.replace(/\/+$/, '') || 'https://registry.npmjs.org')}/`;
|
|
240
|
+
// git 通道的探针换成克隆握手本身:git ls-remote 走的就是 pnpm 克隆前的同一套
|
|
241
|
+
// https 传输。curl 打 github.com 主页会被按协议/端口区分的防火墙与 TLS 复检
|
|
242
|
+
// 拦截(能 git 克隆、打不开网页),把可装的仓库误判成 [network] 而中止。
|
|
243
|
+
// 握手失败时再用 curl 复核一次主页连通性:只有两者都不通
|
|
244
|
+
// 才判「网络不通」;仅握手失败(仓库不存在、私有仓库、需要凭据)放行,
|
|
245
|
+
// 交给后续预检与真实安装给出准确原因。
|
|
246
|
+
// 探针只握手仓库本身(去掉 git+ 前缀与 #ref 片段):ls-remote 认的是仓库地址,
|
|
247
|
+
// 带 ref 片段会干扰 URL 解析
|
|
248
|
+
const gitProbeTarget = repoTarget === null ? null : repoTarget.replace(/^git\+/, '').replace(/#.*$/, '');
|
|
249
|
+
let net;
|
|
250
|
+
if (gitProbeTarget !== null) {
|
|
251
|
+
net = await gitLsRemote(gitProbeTarget, effectiveProxy, 6000);
|
|
252
|
+
if (!net.ok) {
|
|
253
|
+
const reachable = await probeUrl('https://github.com/', effectiveProxy, 6000);
|
|
254
|
+
if (reachable.ok)
|
|
255
|
+
net = reachable;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
net = await probeUrl(httpProbeTarget, effectiveProxy, 6000);
|
|
260
|
+
}
|
|
261
|
+
const probeTarget = gitProbeTarget ?? httpProbeTarget;
|
|
262
|
+
if (!net.ok) {
|
|
263
|
+
// 错误消息按客户端界面语言提示:中文界面给中文、英文界面给英文,
|
|
264
|
+
// 让用户一眼看懂是网络问题而非插件问题
|
|
265
|
+
const channelName = isGitChannel
|
|
266
|
+
? (lang === 'zh' ? 'GitHub' : 'github.com')
|
|
267
|
+
: (lang === 'zh' ? 'npm 源' : 'the npm registry');
|
|
268
|
+
const error = lang === 'zh'
|
|
269
|
+
? `[network] 安装已中止:安装前无法连接到 ${channelName}(${probeTarget})—— 您的网络似乎不通或被拦截(断网 / DNS 解析失败 / 防火墙 / 代理配置问题)。这不是插件本身的问题,请检查网络连接或代理设置,运行「系统诊断」检测各通道连通性后重试。`
|
|
270
|
+
: `[network] install aborted: cannot reach ${channelName} (${probeTarget}) before install — your network connection appears to be down or blocked (DNS / proxy / firewall). This is not a problem with the plugin itself. Check your connection or proxy settings, run the connectivity diagnostic, then retry.`;
|
|
271
|
+
sendJson(response, 400, { error, attempts });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
// 安装前预检:仅新安装走预检(github 源分发缺 package.json 声明的文件时直接拦截)。
|
|
276
|
+
// 更新是已信任目标的覆盖重装,跳过预检避免重复下载 tarball。
|
|
277
|
+
// npm 包信任 registry 直接放行;github 源拦截时标记 [packaging] 前缀,
|
|
278
|
+
// 供客户端归类为「插件分发不完整」并引导去仓库提 Issue
|
|
279
|
+
if (already === null) {
|
|
280
|
+
const preflight = await preflightTarget(target);
|
|
281
|
+
if (!preflight.ok) {
|
|
282
|
+
// 预检拦截(git 分发缺作者声明的文件):同步 400 返回,附上已尝试的安装方式,
|
|
283
|
+
// 客户端失败弹窗提 Issue 时一并贴给作者
|
|
284
|
+
sendJson(response, 400, {
|
|
285
|
+
error: `[packaging] ${target}: plugin distribution is incomplete — the file ${preflight.missing ?? '?'} the package needs (its declared entry, an entry a bundled patch mounts, or the patch itself) is not present in the git distribution (build output not committed). Report to the author.`,
|
|
286
|
+
attempts,
|
|
287
|
+
});
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
// 包名冲突检测(仅 git 通道):git 目标包内声明的 name 若与 profile 已装依赖同名,
|
|
291
|
+
// pnpm 会以该 name 做依赖键撞车,抛出的 CLI 报错晦涩难懂 —— 且该包名在 registry 上
|
|
292
|
+
// 可能被别的仓库占用,npm 反查搜不到,git 直装就会撞上已装的同名依赖。
|
|
293
|
+
// 到这里已装依赖要么来源不同仓库、要么 spec 是版本号解析不出仓库身份。
|
|
294
|
+
// 读 node_modules 里已装包的真实 repository 区分两种情况:
|
|
295
|
+
// - 指向同一仓库 → 是「同仓库残留」(spec 丢了仓库身份),就地转 update 覆盖重装(幂等);
|
|
296
|
+
// - 指向别的仓库 / 读不到 → 跨仓库包名冲突,入队前转成明确 409,指导用户先卸载或改装。
|
|
297
|
+
if (preflight.name !== null && repoTarget !== null && target === repoTarget) {
|
|
298
|
+
const declared = preflight.name;
|
|
299
|
+
if (installed[declared] !== undefined) {
|
|
300
|
+
const installedRepo = installedRepoOf(profile, declared);
|
|
301
|
+
if (installedRepo !== null && gitRepo !== null && installedRepo.toLowerCase() === gitRepo.toLowerCase()) {
|
|
302
|
+
// 同名且同源:已装 spec 丢了仓库身份导致的残留,直接按 update 覆盖重装,不打扰用户
|
|
303
|
+
attempts.push(`same-repo residual: \`${declared}\` resolves to \`${installedRepo}\` → reinstall as update`);
|
|
304
|
+
mode = 'update';
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
attempts.push(`package name conflict: \`${gitRepo}\` declares npm name \`${declared}\`, which is already present in the profile and cannot be matched to this repo (that npm name is likely registered by another repository)`);
|
|
308
|
+
const error = lang === 'zh'
|
|
309
|
+
? `already installed: ${declared} —— ${gitRepo} 声明的 npm 包名「${declared}」已存在于当前 profile,且无法匹配到本仓库(该包名在 npm 上可能被其他仓库占用)。请先移除现有的 ${declared}(dsh plugin remove ${declared}),再重新安装;或改装该包名对应的官方 npm 包。`
|
|
310
|
+
: `already installed: ${declared} — ${gitRepo} declares the npm package name "${declared}", which is already present in the profile and cannot be matched to this repo (that npm name is likely registered by another repository). Remove the existing ${declared} dependency (dsh plugin remove ${declared}) first, then retry; or install the official npm package under that name.`;
|
|
311
|
+
sendJson(response, 409, { error, attempts });
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// 入队前复检(与上方检查同语义,但必须在同步块内):preflight 的 await 期间并发请求
|
|
318
|
+
// 可能已通过上方检查、任务却尚未入队——此处检查与 startPluginMutation 之间无 await,
|
|
319
|
+
// JS 单线程保证同一目标至多一个任务,杜绝双击/并发产生双任务
|
|
320
|
+
if (hasQueuedTarget(target)) {
|
|
321
|
+
sendJson(response, 409, { error: `already queued: ${target}` });
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
// Kick off the CLI in the background; the client polls /status for progress
|
|
325
|
+
const task = startPluginMutation({
|
|
326
|
+
// mode=update(已安装目标的覆盖更新)以 update 动作入队:
|
|
327
|
+
// 日志按 update 类别落盘(update.start/done/fail),实际 CLI 执行仍是 add <target>@latest
|
|
328
|
+
action: mode === 'update' ? 'update' : 'add',
|
|
329
|
+
profile,
|
|
330
|
+
target,
|
|
331
|
+
// npm 安装时 target 是包名:displayTarget 固定展示仓库名,待重启行/前端恢复不受通道变化影响
|
|
332
|
+
displayTarget: githubRepoOf(displayRepo) ?? undefined,
|
|
333
|
+
// 已尝试的安装方式(npm 反查):实际执行命令由 spawn 时追加,失败 issue 一并展示
|
|
334
|
+
attempts,
|
|
335
|
+
timeoutMs: COMMAND_TIMEOUT_MS,
|
|
336
|
+
env: mutationEnv(settings),
|
|
337
|
+
// 覆盖更新:npm 通道命令显式 @latest(pnpm add 无版本对已存在依赖是幂等的,不加版本不会真更新)
|
|
338
|
+
updateNpm: mode === 'update',
|
|
339
|
+
// 运行中 loader:安装成功后把新插件热挂进 loader,非桌面端 + 纯服务端插件无需重启即生效
|
|
340
|
+
loader,
|
|
341
|
+
// 自定义安装入口渠道(NPM 包 / GitHub 源码 / DSH 命令卡片):日志/报错据此溯源
|
|
342
|
+
installChannel,
|
|
343
|
+
});
|
|
344
|
+
sendJson(response, 200, { ok: true, task: task.id });
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
sendJson(response, 500, { error: error instanceof Error ? error.message : String(error) });
|
|
348
|
+
}
|
|
349
|
+
},
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
kind: 'exact',
|
|
353
|
+
path: '/dsh-plugin-hub/uninstall',
|
|
354
|
+
handler: async (request, response) => {
|
|
355
|
+
if (!requireTrustedPost(request, response))
|
|
356
|
+
return;
|
|
357
|
+
// 与安装一致:卸载也进入同一队列,FIFO 串行执行
|
|
358
|
+
try {
|
|
359
|
+
const body = await readJsonBody(request);
|
|
360
|
+
const name = typeof body === 'object' && body !== null && typeof body.name === 'string'
|
|
361
|
+
? body.name
|
|
362
|
+
: '';
|
|
363
|
+
// 展示用的 owner/repo:卸载命令只需要 npm 包名,但待重启行要与安装行一致地显示仓库名
|
|
364
|
+
const repo = typeof body === 'object' && body !== null && typeof body.repo === 'string'
|
|
365
|
+
? body.repo
|
|
366
|
+
: '';
|
|
367
|
+
if (!validPackageName(name) || name === 'dsh-plugin') {
|
|
368
|
+
sendJson(response, 400, { error: 'plugin cannot be uninstalled here' });
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (readInstalled(profile)[name] === undefined) {
|
|
372
|
+
sendJson(response, 400, { error: 'plugin is not installed' });
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
// 队列级去重:同一包已在排队/执行中卸载时拒绝
|
|
376
|
+
if (hasQueuedTarget(name)) {
|
|
377
|
+
sendJson(response, 409, { error: `already queued: ${name}` });
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const task = startPluginMutation({
|
|
381
|
+
action: 'remove',
|
|
382
|
+
profile,
|
|
383
|
+
target: name,
|
|
384
|
+
// 卸载的 mutation 目标是 npm 包名;待重启行要显示 owner/repo(与安装行一致),非法/缺失时回退包名
|
|
385
|
+
displayTarget: githubRepoOf(repo) ?? undefined,
|
|
386
|
+
// 运行中 loader:卸载成功后主动移除条目,立即生效、无需重启
|
|
387
|
+
loader,
|
|
388
|
+
timeoutMs: COMMAND_TIMEOUT_MS,
|
|
389
|
+
env: mutationEnv(loadSettings(profile)),
|
|
390
|
+
});
|
|
391
|
+
sendJson(response, 200, { ok: true, task: task.id });
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
sendJson(response, 500, { error: error instanceof Error ? error.message : String(error) });
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
];
|
|
399
|
+
}
|
package/lib/http/logs.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH Plugin Hub — the community plugin marketplace for DeepSeek Harness.
|
|
3
|
+
* Website: https://dsh-plugin.org
|
|
4
|
+
* GitHub: https://github.com/dshplugin/dsh-plugin-hub
|
|
5
|
+
*
|
|
6
|
+
* Log routes: paginated log viewer, clearing, reveal-in-file-manager and the
|
|
7
|
+
* native folder picker for choosing a log directory.
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { dirname } from 'node:path';
|
|
11
|
+
import { appendLog, clearLog, readLog, logFilePath, defaultLogFilePath } from '../services/log.js';
|
|
12
|
+
import { requireMethod, requireTrustedPost, sendJson } from './shared.js';
|
|
13
|
+
export function registerLogsRoutes(webServer, profile) {
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
kind: 'exact',
|
|
17
|
+
path: '/dsh-plugin-hub/logs',
|
|
18
|
+
handler: (request, response) => {
|
|
19
|
+
if (!requireMethod(request, response, 'GET'))
|
|
20
|
+
return;
|
|
21
|
+
// 日志查看器分页接口:?offset=&limit=&category=&level=&query=,返回过滤后的倒序分页
|
|
22
|
+
try {
|
|
23
|
+
const url = new URL(request.url ?? '/', 'http://localhost');
|
|
24
|
+
const num = (name) => {
|
|
25
|
+
const v = url.searchParams.get(name);
|
|
26
|
+
if (v === null || v === '')
|
|
27
|
+
return undefined;
|
|
28
|
+
const n = Number(v);
|
|
29
|
+
return Number.isFinite(n) && n >= 0 ? Math.floor(n) : undefined;
|
|
30
|
+
};
|
|
31
|
+
const limit = Math.min(num('limit') ?? 200, 500);
|
|
32
|
+
const category = url.searchParams.get('category') ?? 'all';
|
|
33
|
+
const level = url.searchParams.get('level') ?? 'all';
|
|
34
|
+
const query = url.searchParams.get('query') ?? '';
|
|
35
|
+
const result = readLog(profile, {
|
|
36
|
+
offset: num('offset') ?? 0,
|
|
37
|
+
limit,
|
|
38
|
+
category: category,
|
|
39
|
+
level: level,
|
|
40
|
+
query,
|
|
41
|
+
});
|
|
42
|
+
sendJson(response, 200, {
|
|
43
|
+
entries: result.entries,
|
|
44
|
+
total: result.total,
|
|
45
|
+
offset: num('offset') ?? 0,
|
|
46
|
+
limit,
|
|
47
|
+
hasMore: result.total > (num('offset') ?? 0) + result.entries.length,
|
|
48
|
+
path: logFilePath(profile),
|
|
49
|
+
defaultPath: defaultLogFilePath(profile),
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
sendJson(response, 400, { error: 'invalid log query' });
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
kind: 'exact',
|
|
59
|
+
path: '/dsh-plugin-hub/clear-log',
|
|
60
|
+
handler: (request, response) => {
|
|
61
|
+
if (!requireTrustedPost(request, response))
|
|
62
|
+
return;
|
|
63
|
+
// 清空所有日志:截断当前日志文件为空
|
|
64
|
+
try {
|
|
65
|
+
const ok = clearLog(profile);
|
|
66
|
+
if (!ok) {
|
|
67
|
+
sendJson(response, 500, { error: 'failed to clear log' });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
appendLog(profile, { at: Date.now(), level: 'info', category: 'system', event: 'system.clear', message: '日志已由用户清空' });
|
|
71
|
+
sendJson(response, 200, { ok: true });
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
sendJson(response, 500, { error: 'failed to clear log' });
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
kind: 'exact',
|
|
80
|
+
path: '/dsh-plugin-hub/open-log',
|
|
81
|
+
handler: (request, response) => {
|
|
82
|
+
if (!requireTrustedPost(request, response))
|
|
83
|
+
return;
|
|
84
|
+
// 在系统文件管理器里定位日志文件(macOS Finder -R / Windows explorer /select, / Linux xdg-open)
|
|
85
|
+
try {
|
|
86
|
+
const file = logFilePath(profile);
|
|
87
|
+
const bin = process.platform === 'darwin' ? 'open'
|
|
88
|
+
: process.platform === 'win32' ? 'explorer'
|
|
89
|
+
: 'xdg-open';
|
|
90
|
+
const args = process.platform === 'darwin' ? ['-R', file]
|
|
91
|
+
: process.platform === 'win32' ? ['/select,', file]
|
|
92
|
+
: [file];
|
|
93
|
+
const child = spawn(bin, args, { stdio: 'ignore', detached: true });
|
|
94
|
+
child.once('error', () => sendJson(response, 500, { error: 'failed to open log file' }));
|
|
95
|
+
child.once('spawn', () => sendJson(response, 200, { ok: true }));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
sendJson(response, 500, { error: 'failed to open log file' });
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
// 系统目录选择器:弹原生文件夹对话框让用户挑日志存放目录(macOS osascript /
|
|
103
|
+
// Windows FolderBrowserDialog / Linux zenity),选中返回绝对路径,取消返回 cancelled。
|
|
104
|
+
// 与 open-log 不同,这里必须等对话框关闭才能拿结果,所以阻塞收集 stdout 再应答。
|
|
105
|
+
// 对话框默认定位到当前日志文件所在目录 —— 每台机器/平台的默认位置都不同,
|
|
106
|
+
// 这里动态取 logFilePath() 的目录,用户打开就在「它原来的位置」附近。
|
|
107
|
+
{
|
|
108
|
+
kind: 'exact',
|
|
109
|
+
path: '/dsh-plugin-hub/choose-log-dir',
|
|
110
|
+
handler: (request, response) => {
|
|
111
|
+
if (!requireTrustedPost(request, response))
|
|
112
|
+
return;
|
|
113
|
+
try {
|
|
114
|
+
const title = 'DSH Plugin Hub — choose log folder';
|
|
115
|
+
const startDir = dirname(logFilePath(profile));
|
|
116
|
+
let bin;
|
|
117
|
+
let args;
|
|
118
|
+
if (process.platform === 'darwin') {
|
|
119
|
+
bin = 'osascript';
|
|
120
|
+
args = ['-e', `POSIX path of (choose folder with prompt "${title}" default location (POSIX file "${startDir}"))`];
|
|
121
|
+
}
|
|
122
|
+
else if (process.platform === 'win32') {
|
|
123
|
+
bin = 'powershell.exe';
|
|
124
|
+
args = ['-NoProfile', '-STA', '-Command', [
|
|
125
|
+
'Add-Type -AssemblyName System.Windows.Forms',
|
|
126
|
+
'$f = New-Object System.Windows.Forms.FolderBrowserDialog',
|
|
127
|
+
`$f.Description = '${title}'`,
|
|
128
|
+
// 初始定位到当前日志目录(单引号字符串内反斜杠不转义,Windows 路径原样传入)
|
|
129
|
+
`$f.SelectedPath = '${startDir}'`,
|
|
130
|
+
'if ($f.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $f.SelectedPath } else { "" }',
|
|
131
|
+
].join('; ')];
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
bin = 'zenity';
|
|
135
|
+
args = ['--file-selection', '--directory', `--title=${title}`, `--filename=${startDir}`];
|
|
136
|
+
}
|
|
137
|
+
const child = spawn(bin, args);
|
|
138
|
+
let out = '';
|
|
139
|
+
child.stdout.on('data', (d) => { out += String(d); });
|
|
140
|
+
// spawn 失败(如 Linux 无 zenity)会先触发 error 再触发 close(code=-2):
|
|
141
|
+
// 只允许第一个事件应答,否则第二次 sendJson 对已结束的响应再 writeHead,
|
|
142
|
+
// 抛 ERR_HTTP_HEADERS_SENT 成为宿主进程内的未捕获异常。
|
|
143
|
+
let done = false;
|
|
144
|
+
const finish = (reply) => { if (!done) {
|
|
145
|
+
done = true;
|
|
146
|
+
reply();
|
|
147
|
+
} };
|
|
148
|
+
child.on('error', () => finish(() => sendJson(response, 500, { error: 'directory picker unavailable' })));
|
|
149
|
+
child.on('close', (code) => finish(() => {
|
|
150
|
+
// macOS osascript 输出带引号(如 "/Users/x"),统一剥掉首尾空白与引号
|
|
151
|
+
const picked = out.trim().replace(/^"|"$/g, '');
|
|
152
|
+
if (code === 0 && picked !== '')
|
|
153
|
+
sendJson(response, 200, { path: picked });
|
|
154
|
+
else
|
|
155
|
+
sendJson(response, 200, { cancelled: true });
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
sendJson(response, 500, { error: 'directory picker unavailable' });
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
];
|
|
164
|
+
}
|