dsh-modpack-kit 0.1.3 → 0.1.4
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 +51 -1
- package/assets/install-stub.mjs +41 -1
- package/client.js +377 -43
- package/docs/DETAIL.md +33 -2
- package/index.js +334 -66
- package/lib/install.mjs +96 -39
- package/lib/manifest.mjs +3 -1
- package/lib/store.mjs +7 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +2 -2
package/docs/DETAIL.md
CHANGED
|
@@ -11,8 +11,13 @@
|
|
|
11
11
|
- **可选项**:是否带插件本体、是否带配置与数据快照、打包前是否扫描。
|
|
12
12
|
- **检测(打包前强制)**:打包一定会扫,没有关掉的路径——开关照常显示但点不动,点一下弹红字「你想干什么」五秒;API 里写 `scanBeforePack: false` 也会被拉回 `true`。安装前再扫一次(这一项可以关,默认开)。规则覆盖私钥、云厂商与代码托管 token、常见大模型 key、Cookie/会话字段、邮箱与手机号、内网地址、本机用户路径,以及 `curl|sh`、`powershell -enc`、`eval`/`new Function`、可疑长 base64、关闭证书校验、`postinstall` 脚本等。
|
|
13
13
|
- **兼容性**:与已装版本、peer 依赖、`engines.dsh`/`engines.node`、许可证允许列表比对,给出等级、原因与建议;有阻断项时默认不装。
|
|
14
|
+
- **安装**:读包后先校验完整性、扫描与兼容性,再逐插件走官方 `dsh plugin add` 装进 profile。和打包共用左下角那个进度窗(读包 → 校验 → 扫描 → 逐插件装 → 登记启用清单 → 回填配置,带百分比,可终止、可和别的任务并行)。
|
|
15
|
+
- **已装先跳过**:按插件名判定(同名、以及同一个插件不同版本都算「已有」),先跳过、其余照装;装完列出「本机版本 → 包内版本」,由你选「直接完成(保留本机)」或「替换为包内版本(只换列出来的这几个)」。
|
|
16
|
+
- **联网优先市场版**:开关默认开(设置项 `preferMarketOnInstall`)。能查到就用 `名字@版本` 从 registry 装,查不到/离线/关掉才用包内那份;每个插件的动作里写明走的是 `market` 还是 `pack`。
|
|
14
17
|
- **安装后同步**:包内配置按条目回填到 profile 的 `cordis.patch.yml`,只改目标插件那一条,其它字节原样保留;已装插件默认不被覆盖(「保留本地修改」)。
|
|
15
|
-
-
|
|
18
|
+
- **读取方式**:「选择文件…」(系统文件选择器,只列 `.dshpack`)、「浏览文件夹…」(系统目录选择器,选完自动列目录)、手写任意目录后「读这个目录」、插件自己的 `packs/` 目录,或一个 https 链接。用前三种读过的包会留在「已导入的整合包」列表里(最多 8 份,各自显示打包时的名字、作者、生成时间与描述,可切换或移除)。拖拽入口已去掉(外壳自身会接管文件拖放)。
|
|
19
|
+
- **独立安装器**:完整版与轻量版都带 `install/install.mjs|.cmd|.sh`,对方不装本插件、只有一个裸 DSH 也能装。它把插件复制到 `~/.dsh/dsh-modpack/vendor/<包>/` 再登记,所以删掉解压目录也不会断;默认跳过本机已装的插件并汇总提示,加 `--replace` 才换成包内版本;`--no-bundles` 装完不启用,`--dry-run` 只演练。
|
|
20
|
+
- **进度窗的收尾**:失败与取消的记录只留十几秒,完成的按设置 `jobKeepSeconds`(默认 60 秒,0 = 收尾即清)清掉,卡住的 running(十分钟没有新进度)先判失败再收走;只有正在跑的、停在风险点的会一直留着。「清空进程」是乐观更新(先在界面摘掉再通知服务端),单条「删除该进程」同样会收掉安装页那条结论。
|
|
16
21
|
- **与插件市场联动(建议先装市场,但非必须)**:目录直接读市场自己的目录包 `dsh-plugin-catalog`(CDN 的 `index.json` 优先,取不到退回 npm tarball),分区名与条目数跟市场一致;本机列表来自同一个 `node_modules`;安装走同一条 `dsh plugin add` 链路,所以两边装的插件互相可见。已装插件会从目录补齐描述与分区,并标出「市场新版 x.y.z」。
|
|
17
22
|
- **装了市场**:目录、分类、搜索、「切换市场版」、市场版版本对比都在;市场的 Discover 栏与本插件的「插件页内」栏并排,互不占位。
|
|
18
23
|
- **没装市场**:打包、装包、强制检测、完整性/兼容/许可证校验、配置回填、装好先不启用、三处入口**全部照常**(这些槽位由 DSH 核心声明,不依赖市场插件)。少的只有市场 Discover 界面,以及**尚未建过目录索引时**的分类胶囊、搜索、市场版对比与「切换市场版」。建一次索引(5~30 秒)即恢复。
|
|
@@ -57,13 +62,39 @@ data/<name>/** 插件数据快照(仅小体积文本)
|
|
|
57
62
|
|
|
58
63
|
文件名:`<包名>-<日期>-<full|lite>.dshpack`。
|
|
59
64
|
|
|
65
|
+
## 打包市场里没装的插件
|
|
66
|
+
|
|
67
|
+
`buildPack` 的 `selection[]` 支持三种来源:`profile`(读本机已装目录)、`npm`(按 `spec`/`version` 从 registry 取 tarball)、`url`(保留给后续扩展)。`/pack` 路由会自己纠偏:`item.source` 写了 npm、或这个插件在本机 `installed` 里找不到,一律按 npm 源走,`spec` 缺省用 `item.version`。npm 源的条目在清单里记 `source: "npm"`、`localEdits: false`、`mode: "base"`。
|
|
68
|
+
|
|
69
|
+
客户端:搜索结果里 `installed === false` 的行,勾选时按 `source: "npm"` 入队,版本框预填该条目的版本(清空 = 取最新)。
|
|
70
|
+
|
|
71
|
+
## 用「打开文件」挑包与自定义目录(安装入口)
|
|
72
|
+
|
|
73
|
+
- `POST /pack/drop`:两种请求体都收——默认是 `.dshpack` 原始字节(`Content-Type: application/octet-stream`,上限 `MAX_DROP_BODY = 256 MB`,走 `readRawBody`);当 `Content-Type` 含 `application/json` 时读 `{ name, data }`,`data` 是 base64(走 `readBody` + `readRawBody`,上限同上),`data` 缺失返回 400「没有收到整合包内容」。两条路都 `inspectPackBuffer` → `rememberPending` 拿 token,返回与 `/inspect` 同形(含 `scanGroups`、`compat`、`integrity`)。
|
|
74
|
+
- 客户端两条入口(`InstallPanel`):「选择文件…」(隐藏 `<input type="file" accept=".dshpack">`)、「浏览文件夹…」(`ctx.get('uiWorkspace').pickDirectory()`,宿主原生目录选择器;不可用时提示手写路径)。上传统一走 `uploadPackFile`:先试原始字节,失败自动退回 base64 的 JSON 再试一次,两条路的错误一起报出来。拖拽入口已移除(外壳自带文件拖放会抢事件)。
|
|
75
|
+
- 「已导入的整合包」列表:`importStore`(模块级,最多 8 份)保存读过的包——`importEntry(data, fileName)` 取清单里的 `name/description/author/format/createdAt` 与插件数;卡片上「选择」调 `POST /inspect {token}` 重新取预览,过期则自动从列表移除;「移除」/「全部移除」只删列表项,不动宿主的 pending。
|
|
76
|
+
- 裸 DSH 侧:`assets/install-stub.mjs` 首行打印包名与格式,随后打印「打包者」「打包时间」「包含插件:N 个」「说明:」逐行原文——不装插件也能知道这份包是谁做的、干什么用的。
|
|
77
|
+
- `POST /packs { dir }`:列任意目录(绝对路径)里的 `.dshpack`,条目带绝对 `path`;`GET /packs` 仍读插件自己的 `packs`。`inspectInput` 支持绝对路径的 `file`,所以选中即按路径读。
|
|
78
|
+
- 进程清理:`/jobs/clear` 只留 `running` 与 `paused`,其余(含 4% 就因许可证被拒的失败任务、卡住的假 running)都清掉;客户端「清空进程」在有任务时常驻,批量失败会退回逐条 `/jobs/dismiss`。`pruneJobs()` 由设置 `jobKeepSeconds`(默认 60 秒,0 = 收尾即清)驱动:完成/已确认的按它清,失败/取消的取 `min(它, 15 秒)`,仍在 `running` 但心跳(`updatedAt`)超过 10 分钟的先被判成失败再一起清;`paused`(等风险决定)永不自动清。安装任务在结果写进页面后由客户端自动 `/jobs/dismiss`。客户端的「清空进程」是乐观更新:先把收尾的行从界面摘掉并 `fireClear()`,再 `POST /jobs/clear`,失败时退回逐条 `/jobs/dismiss`。
|
|
79
|
+
|
|
80
|
+
## 安装顺序与「已装先跳过」
|
|
81
|
+
|
|
82
|
+
安装与打包共用任务队列(`GET /jobs` 里 `kind: "install"`):`POST /install { async: true }` 立即返回 job,进度由 `installPack` 的 `onProgress` 推进(8 检查已装 → 12~80 逐插件 → 84 登记启用清单 → 92 回填配置 → 100 完成)。
|
|
83
|
+
|
|
84
|
+
判定「已装」用插件名(`installed` 列表),不比对版本。默认 `deferExisting: true`:已装的不动,记进返回值的 `deferred`(`{ name, localVersion, packVersion, sameVersion }`),动作是 `deferred`。装完用户二选一:保留本机 → 什么都不做;替换 → `POST /install/replace { token, names }`,内部用同一次读取的 `inspected` 以 `replaceExisting: true, keepLocal: false, only: names` 再跑一遍,未选中的动作记为 `skipped`。
|
|
85
|
+
|
|
86
|
+
装的时候还会先问一句市场:`context.resolveMarket(plugin)` → `npmMetadata` + `pickVersion`(先按包内版本找,找不到取 latest),命中就用 `plugin.name@version` 交给 `dsh plugin add`,动作里 `source: "market"`;关掉 `preferMarket`(设置项 `preferMarketOnInstall`,请求里也能逐次关)或查询失败,才用包内那份(`source: "pack"`,`file:` 指向 vendor 里的副本)。
|
|
87
|
+
|
|
88
|
+
包内独立安装器(`install/install.mjs`)行为对齐:读 `profile/package.json` 的 `dependencies`,同名即跳过并收集到 `skipped`,收尾打印清单与 `--replace` 提示;`--replace`(别名 `--force-existing`)时照常覆盖。
|
|
89
|
+
|
|
60
90
|
## 风险列表与打包进度
|
|
61
91
|
|
|
62
92
|
- 命中按来源分栏:每个插件一组、`configs/` 一组、`data/` 一组,整合包自身(清单、README、安装器)单独一组;整体可折叠,每组也可折叠,组头带高/中/低计数,高危组排最前。
|
|
63
93
|
- 打包走后台任务(`POST /pack` 带 `async: true` → `GET /jobs` 轮询):悬浮进度窗显示概述 + 百分比 + 进度条,展开可看每个任务;`POST /jobs/cancel` 终止,包在内存里组装,取消不会写到磁盘。
|
|
64
94
|
- 检出高风险时任务一律停在 `paused`:`GET /jobs` 带 `risk`(高/中/低、`trustedOnly`、按插件分组的命中摘要 `risk.groups[].samples`)与 `canAck`,打包页会在选项下方按插件分别列出这些命中项。「我知道在干什么」= `POST /jobs/ack`:复用停在那一刻的中间结果(`job.checkpoint`:已收集的文件、配置快照、检测结果),从检测点继续,**不重新收集、也不重复检测**,清单记 `security.acknowledged`;确认成功的同时 `job.risk` 清空,界面上的红字警告随之消失(记录只留状态与进度)。「删除该进程」= `POST /jobs/dismiss`(只删记录,跑着的任务不给删)。
|
|
95
|
+
- 安装任务拿到结果后就自动 `/jobs/dismiss`(结果已在安装页),不需要手动清。
|
|
65
96
|
- 收尾的任务在进度窗里给「下载安装包」:点击会切到打包页、把该任务的 `data` 放回结果区并滚到下载部分;结果存在客户端模块级缓存里(`packStore`),切页签、卸载再挂载都不丢,程序退出或插件被关掉才清。进度窗的「清空进程」(`POST /jobs/clear`)清掉所有收尾记录——完成的、失败的、取消的,以及像「超过打包上限」这类中途报错卡住的失败进程都带走;`running` 与 `paused`(还在跑或等你决定)保留。界面侧如果发现宿主还是旧代码(`/jobs/clear` 404),会退回逐个 `POST /jobs/dismiss`;清空后打包页上残留的报错与失败结果也一并收掉。
|
|
66
|
-
- 悬浮进度窗可拖:按住标题拖动,位置存在浏览器 `localStorage`(键 `dsh-modpack-kit:dock-pos
|
|
97
|
+
- 悬浮进度窗可拖:按住标题拖动,位置存在浏览器 `localStorage`(键 `dsh-modpack-kit:dock-pos`),不经过服务端配置;没有任务时自动收起,只留一枚「进度」按钮用于唤出,任务开跑会自动冒出。标题栏里的按钮(「清空进程」「收起」)与拖动、点击展开不共享判定:`startDrag` 遇到 `event.target.closest("button")` 直接返回(否则 `setPointerCapture` 会把 `click` 抢走,按钮点不到,抬手还会顺手切换展开状态),两个按钮本身也在 `pointerdown` 上 `stopPropagation`,并带 `flexShrink: 0` 免得被标题挤扁。
|
|
67
98
|
- 体积上限默认 `maxPackBytes = 512MB`;单个下载另有 `maxDownloadBytes = 256MB`。
|
|
68
99
|
- 允许同时打多个包(每个任务独立 token、独立进度)。
|
|
69
100
|
|
package/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import path from 'node:path'
|
|
|
9
9
|
import { randomBytes } from 'node:crypto'
|
|
10
10
|
import { loadConfig, saveConfig, pathsFor, profileName, readHistory, appendHistory, appendLog, ensureDir, slugify, DEFAULT_CONFIG } from './lib/store.mjs'
|
|
11
11
|
import { listInstalled, dshVersion, readProfilePackage, readBundles } from './lib/profile.mjs'
|
|
12
|
-
import { downloadPackage, npmMetadata, fetchLimited, relativeTime } from './lib/registry.mjs'
|
|
12
|
+
import { downloadPackage, npmMetadata, pickVersion, fetchLimited, relativeTime } from './lib/registry.mjs'
|
|
13
13
|
import { buildPack, PackRefused } from './lib/pack.mjs'
|
|
14
14
|
import { inspectPackBuffer, installPack, readPackFromUrl, InstallRefused } from './lib/install.mjs'
|
|
15
15
|
import { checkCompatibility } from './lib/compat.mjs'
|
|
@@ -22,6 +22,8 @@ import { buildReadme, pluginSummary } from './lib/manifest.mjs'
|
|
|
22
22
|
|
|
23
23
|
const VERSION = '0.1.0'
|
|
24
24
|
const MAX_JSON_BODY = 1024 * 1024
|
|
25
|
+
// 直接拖进来的整合包走原始字节(不塞 base64),上限与单次下载一致,够装大包
|
|
26
|
+
const MAX_DROP_BODY = 256 * 1024 * 1024
|
|
25
27
|
const PENDING_TTL = 30 * 60 * 1000
|
|
26
28
|
const PENDING_MAX = 4
|
|
27
29
|
const PENDING_BYTES = 512 * 1024 * 1024
|
|
@@ -43,6 +45,22 @@ const readBody = (req, limit = MAX_JSON_BODY) => new Promise((resolve, reject) =
|
|
|
43
45
|
req.on('error', reject)
|
|
44
46
|
})
|
|
45
47
|
|
|
48
|
+
const readRawBody = (req, limit = MAX_DROP_BODY) => new Promise((resolve, reject) => {
|
|
49
|
+
let size = 0
|
|
50
|
+
const chunks = []
|
|
51
|
+
req.on('data', (chunk) => {
|
|
52
|
+
size += chunk.length
|
|
53
|
+
if (size > limit) {
|
|
54
|
+
reject(Object.assign(new Error('整合包太大:上限 ' + Math.round(limit / 1024 / 1024) + ' MB'), { statusCode: 413 }))
|
|
55
|
+
req.destroy()
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
chunks.push(chunk)
|
|
59
|
+
})
|
|
60
|
+
req.on('end', () => resolve(Buffer.concat(chunks)))
|
|
61
|
+
req.on('error', reject)
|
|
62
|
+
})
|
|
63
|
+
|
|
46
64
|
const asJson = (text, fallback) => {
|
|
47
65
|
try { return JSON.parse(text) } catch { return fallback }
|
|
48
66
|
}
|
|
@@ -313,18 +331,42 @@ export default {
|
|
|
313
331
|
// —— 打包(支持后台任务:左下角悬浮窗要进度,且允许同时打多个包) ——
|
|
314
332
|
const jobs = new Map()
|
|
315
333
|
let jobSeq = 0
|
|
316
|
-
|
|
334
|
+
// 收尾的记录按状态分别留:完成的多留一会儿方便回看,失败/取消的很快就清,别堆在眼前。
|
|
335
|
+
// 秒数来自设置 jobKeepSeconds(0 = 收尾就清),测试就靠它把时间压到零。
|
|
336
|
+
const FAILED_KEEP = 15 * 1000
|
|
337
|
+
const RUNNING_STALE = 10 * 60 * 1000
|
|
338
|
+
const keepMs = () => Math.max(0, Number(config.jobKeepSeconds) || 0) * 1000
|
|
339
|
+
const heartbeatAge = (job, now) => {
|
|
340
|
+
const stamp = Date.parse(job.updatedAt || job.finishedAt || job.startedAt || '') || 0
|
|
341
|
+
return stamp > 0 ? now - stamp : 0
|
|
342
|
+
}
|
|
343
|
+
// 卡住的 running(进程还在但心跳没了):标成失败,让它走失败那条清理路径
|
|
344
|
+
const reapStuck = (job, now) => {
|
|
345
|
+
if (job.status !== 'running') return
|
|
346
|
+
if (heartbeatAge(job, now) <= RUNNING_STALE) return
|
|
347
|
+
job.status = 'failed'
|
|
348
|
+
job.error = '卡住了:很久没有新进度,已自动收掉'
|
|
349
|
+
job.note = '已自动收掉'
|
|
350
|
+
job.finishedAt = new Date().toISOString()
|
|
351
|
+
job.updatedAt = job.finishedAt
|
|
352
|
+
job.updatedAt = job.finishedAt
|
|
353
|
+
}
|
|
317
354
|
const pruneJobs = () => {
|
|
318
|
-
const
|
|
355
|
+
const now = Date.now()
|
|
356
|
+
const keep = keepMs()
|
|
319
357
|
for (const [id, job] of jobs) {
|
|
358
|
+
reapStuck(job, now)
|
|
359
|
+
// 停在风险点等你决定的,一直留着
|
|
360
|
+
if (job.status === 'paused') continue
|
|
320
361
|
if (job.status === 'running') continue
|
|
321
|
-
const
|
|
322
|
-
|
|
362
|
+
const age = heartbeatAge(job, now)
|
|
363
|
+
const limit = job.status === 'done' || job.status === 'acked' ? keep : Math.min(keep, FAILED_KEEP)
|
|
364
|
+
if (limit <= 0 || age > limit) jobs.delete(id)
|
|
323
365
|
}
|
|
324
366
|
for (const id of [...jobs.keys()]) {
|
|
325
367
|
if (jobs.size <= 20) break
|
|
326
368
|
const job = jobs.get(id)
|
|
327
|
-
if (job.status === 'running') continue
|
|
369
|
+
if (job.status === 'running' || job.status === 'paused') continue
|
|
328
370
|
jobs.delete(id)
|
|
329
371
|
}
|
|
330
372
|
}
|
|
@@ -337,6 +379,8 @@ export default {
|
|
|
337
379
|
pct: 0,
|
|
338
380
|
note: '准备中',
|
|
339
381
|
startedAt: new Date().toISOString(),
|
|
382
|
+
// 最近一次有动静的时间:用来判断「还活着」还是「卡住了」
|
|
383
|
+
updatedAt: new Date().toISOString(),
|
|
340
384
|
finishedAt: '',
|
|
341
385
|
error: '',
|
|
342
386
|
data: null,
|
|
@@ -386,13 +430,19 @@ export default {
|
|
|
386
430
|
}
|
|
387
431
|
try {
|
|
388
432
|
const result = await buildPack({
|
|
389
|
-
selection: selection.map((item) =>
|
|
390
|
-
name
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
433
|
+
selection: selection.map((item) => {
|
|
434
|
+
const name = String(item.name || '')
|
|
435
|
+
const here = installed.some((p) => p.name === name)
|
|
436
|
+
// 市场里没装的插件也能打包:本机没有这份代码,就从 registry 按版本取包
|
|
437
|
+
const source = item.source === 'npm' || item.source === 'market' || !here ? 'npm' : 'profile'
|
|
438
|
+
return {
|
|
439
|
+
name,
|
|
440
|
+
source,
|
|
441
|
+
spec: item.spec ? String(item.spec) : (source === 'npm' ? String(item.version || '') : ''),
|
|
442
|
+
mode: item.mode === 'local' ? 'local' : item.mode === 'base' ? 'base' : undefined,
|
|
443
|
+
download: (spec) => downloadPackage(name, spec || item.spec || String(item.version || ''), config),
|
|
444
|
+
}
|
|
445
|
+
}),
|
|
396
446
|
options: {
|
|
397
447
|
includePayload: options.includePayload !== false,
|
|
398
448
|
includeConfig: options.includeConfig !== false,
|
|
@@ -484,6 +534,7 @@ export default {
|
|
|
484
534
|
: [],
|
|
485
535
|
}
|
|
486
536
|
job.finishedAt = new Date().toISOString()
|
|
537
|
+
job.updatedAt = job.finishedAt
|
|
487
538
|
if (payload.needsAck === true) {
|
|
488
539
|
job.status = 'paused'
|
|
489
540
|
job.note = '检测到风险,等你决定'
|
|
@@ -505,6 +556,7 @@ export default {
|
|
|
505
556
|
// 失败/取消也把原因放进 error,进度窗那一行能直接看到为什么卡住
|
|
506
557
|
if (!(payload && payload.ok)) job.error = (payload && payload.error) || job.note
|
|
507
558
|
job.finishedAt = new Date().toISOString()
|
|
559
|
+
job.updatedAt = job.finishedAt
|
|
508
560
|
}
|
|
509
561
|
const startPackJob = (body, label) => {
|
|
510
562
|
const job = createJob('pack', label || ('打包「' + String((body.options && body.options.name) || 'DSH 整合包') + '」'))
|
|
@@ -522,6 +574,7 @@ export default {
|
|
|
522
574
|
job.error = String((error && error.message) || error)
|
|
523
575
|
job.note = '失败'
|
|
524
576
|
job.finishedAt = new Date().toISOString()
|
|
577
|
+
job.updatedAt = job.finishedAt
|
|
525
578
|
})
|
|
526
579
|
return job
|
|
527
580
|
}
|
|
@@ -571,6 +624,7 @@ export default {
|
|
|
571
624
|
job.error = String((error && error.message) || error)
|
|
572
625
|
job.note = '失败'
|
|
573
626
|
job.finishedAt = new Date().toISOString()
|
|
627
|
+
job.updatedAt = job.finishedAt
|
|
574
628
|
})
|
|
575
629
|
json(res, 200, { ok: true, job: publicJob(job) })
|
|
576
630
|
})
|
|
@@ -586,17 +640,24 @@ export default {
|
|
|
586
640
|
json(res, 200, { ok: true, removed: id })
|
|
587
641
|
})
|
|
588
642
|
|
|
589
|
-
//
|
|
590
|
-
//
|
|
643
|
+
// 清空进程:除了「正在跑」与「停在风险点等决定」,其余一律删——
|
|
644
|
+
// 完成的、失败的、取消的、超上限卡住的、以及假 running(心跳停了)都带走。
|
|
591
645
|
route('/dsh-modpack-kit/api/jobs/clear', async (req, res) => {
|
|
592
646
|
if (!needPost(req, res)) return
|
|
593
|
-
const keep = new Set(['running', 'paused'])
|
|
594
647
|
const removed = []
|
|
648
|
+
const now = Date.now()
|
|
595
649
|
for (const [id, job] of [...jobs.entries()]) {
|
|
596
|
-
if (
|
|
650
|
+
if (job.status === 'paused') continue
|
|
651
|
+
if (job.status === 'running') {
|
|
652
|
+
// 还活着的不动;心跳停了的先标成失败,再按收尾清掉
|
|
653
|
+
reapStuck(job, now)
|
|
654
|
+
if (job.status === 'running') continue
|
|
655
|
+
}
|
|
656
|
+
// 卡住的 running:没有文件落盘(包在内存里组装),直接摘掉即可
|
|
597
657
|
jobs.delete(id)
|
|
598
658
|
removed.push(id)
|
|
599
659
|
}
|
|
660
|
+
pruneJobs()
|
|
600
661
|
json(res, 200, { ok: true, removed, jobs: [...jobs.values()].map(publicJob) })
|
|
601
662
|
})
|
|
602
663
|
|
|
@@ -659,19 +720,91 @@ export default {
|
|
|
659
720
|
json(res, 200, { ok: true, path: target, size: item.buffer.length })
|
|
660
721
|
})
|
|
661
722
|
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const dir = pathsFor(config).packs
|
|
723
|
+
// 列某个目录下的整合包:GET 读本插件自己的 packs 目录,POST { dir } 读你指定的目录
|
|
724
|
+
const listPacks = (dir) => {
|
|
665
725
|
const items = []
|
|
726
|
+
let error = ''
|
|
666
727
|
try {
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
728
|
+
const stat = fs.statSync(dir)
|
|
729
|
+
if (!stat.isDirectory()) error = '这个路径不是目录'
|
|
730
|
+
} catch (e) {
|
|
731
|
+
error = '这个目录读不到(不存在或没有权限)'
|
|
732
|
+
}
|
|
733
|
+
if (!error) {
|
|
734
|
+
try {
|
|
735
|
+
for (const dirent of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
736
|
+
if (!dirent.isFile() || !dirent.name.toLowerCase().endsWith('.dshpack')) continue
|
|
737
|
+
const stat = fs.statSync(path.join(dir, dirent.name))
|
|
738
|
+
items.push({ name: dirent.name, size: stat.size, mtime: stat.mtime.toISOString(), path: path.join(dir, dirent.name) })
|
|
739
|
+
}
|
|
740
|
+
} catch (e) {
|
|
741
|
+
error = String((e && e.message) || e)
|
|
671
742
|
}
|
|
672
|
-
}
|
|
743
|
+
}
|
|
673
744
|
items.sort((a, b) => (a.mtime < b.mtime ? 1 : -1))
|
|
674
|
-
|
|
745
|
+
return { error, items }
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
route('/dsh-modpack-kit/api/packs', async (req, res) => {
|
|
749
|
+
if (req.method === 'GET') {
|
|
750
|
+
const dir = pathsFor(config).packs
|
|
751
|
+
const listed = listPacks(dir)
|
|
752
|
+
json(res, 200, { ok: !listed.error, dir, items: listed.items, error: listed.error })
|
|
753
|
+
return
|
|
754
|
+
}
|
|
755
|
+
if (req.method !== 'POST') { json(res, 405, { ok: false }); return }
|
|
756
|
+
const body = asJson(await readBody(req), {})
|
|
757
|
+
const raw = String(body.dir || '').trim()
|
|
758
|
+
const dir = raw ? path.resolve(raw) : pathsFor(config).packs
|
|
759
|
+
const listed = listPacks(dir)
|
|
760
|
+
json(res, 200, { ok: !listed.error, dir, items: listed.items, error: listed.error })
|
|
761
|
+
})
|
|
762
|
+
|
|
763
|
+
// 拖到界面上的整合包:原始字节能走就走原始字节,走不通再走 base64 JSON
|
|
764
|
+
route('/dsh-modpack-kit/api/pack/drop', async (req, res) => {
|
|
765
|
+
if (!needPost(req, res)) return
|
|
766
|
+
let bytes = null
|
|
767
|
+
const contentType = String(req.headers['content-type'] || '').toLowerCase()
|
|
768
|
+
try {
|
|
769
|
+
if (contentType.includes('application/json')) {
|
|
770
|
+
// 有些环境不许 POST 二进制,界面会退回 base64:这里同样收
|
|
771
|
+
const body = asJson(await readBody(req, MAX_DROP_BODY), {}) || {}
|
|
772
|
+
const raw = typeof body.data === 'string' ? body.data : ''
|
|
773
|
+
if (!raw) { json(res, 400, { ok: false, error: '没有收到整合包内容' }); return }
|
|
774
|
+
bytes = Buffer.from(raw, 'base64')
|
|
775
|
+
} else {
|
|
776
|
+
bytes = await readRawBody(req)
|
|
777
|
+
}
|
|
778
|
+
} catch (error) {
|
|
779
|
+
json(res, 413, { ok: false, error: String((error && error.message) || error) })
|
|
780
|
+
return
|
|
781
|
+
}
|
|
782
|
+
if (!bytes || bytes.length < 32) { json(res, 400, { ok: false, error: '这个文件看起来不是整合包(太小了)' }); return }
|
|
783
|
+
try {
|
|
784
|
+
const inspected = inspectPackBuffer(bytes, {
|
|
785
|
+
config,
|
|
786
|
+
scanBeforeInstall: config.scanBeforeInstall !== false,
|
|
787
|
+
extraScanRules: Array.isArray(config.extraScanRules) ? config.extraScanRules : [],
|
|
788
|
+
})
|
|
789
|
+
const fileName = slugify(inspected.manifest.name || 'dropped', 'pack') + '.dshpack'
|
|
790
|
+
const token = rememberPending(fileName, { buffer: bytes, manifest: inspected.manifest, report: inspected.report, scan: inspected.scan })
|
|
791
|
+
json(res, 200, {
|
|
792
|
+
ok: true,
|
|
793
|
+
token,
|
|
794
|
+
fileName,
|
|
795
|
+
size: bytes.length,
|
|
796
|
+
manifest: inspected.manifest,
|
|
797
|
+
integrity: inspected.integrity,
|
|
798
|
+
scan: inspected.scan,
|
|
799
|
+
scanGroups: groupFindings(inspected.scan.findings, inspected.manifest.plugins),
|
|
800
|
+
report: inspected.report,
|
|
801
|
+
blockingReasons: inspected.blockingReasons,
|
|
802
|
+
notes: inspected.notes,
|
|
803
|
+
compat: compatFor(inspected.manifest),
|
|
804
|
+
})
|
|
805
|
+
} catch (error) {
|
|
806
|
+
json(res, 200, { ok: false, error: String((error && error.message) || error) })
|
|
807
|
+
}
|
|
675
808
|
})
|
|
676
809
|
|
|
677
810
|
async function inspectInput(body) {
|
|
@@ -717,51 +850,186 @@ export default {
|
|
|
717
850
|
})
|
|
718
851
|
|
|
719
852
|
// —— 安装 ——
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
const
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
853
|
+
// —— 安装:先跳过本机已装的,装完其余再问要不要替换 ——
|
|
854
|
+
const pendingInstalls = new Map()
|
|
855
|
+
const rememberInstall = (inspected) => {
|
|
856
|
+
const token = 'inst_' + Math.random().toString(36).slice(2, 10) + Date.now().toString(36)
|
|
857
|
+
pendingInstalls.set(token, { inspected, at: Date.now() })
|
|
858
|
+
// 上限 4 份、30 分钟过期:别让内存一直攒着包
|
|
859
|
+
for (const [key, item] of pendingInstalls) {
|
|
860
|
+
if (pendingInstalls.size <= 4) break
|
|
861
|
+
if (key === token) continue
|
|
862
|
+
pendingInstalls.delete(key)
|
|
730
863
|
}
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
864
|
+
for (const [key, item] of pendingInstalls) {
|
|
865
|
+
if (Date.now() - item.at > 30 * 60 * 1000) pendingInstalls.delete(key)
|
|
866
|
+
}
|
|
867
|
+
return token
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
const installOptionsOf = (body) => ({
|
|
871
|
+
force: body.force === true,
|
|
872
|
+
keepLocal: body.keepLocal !== false,
|
|
873
|
+
applyConfig: body.applyConfig !== false,
|
|
874
|
+
dryRun: body.dryRun === true,
|
|
875
|
+
// 是否登记 bundle:请求里给了就听请求的,没给就听设置里的 enableOnInstall(默认关)。
|
|
876
|
+
enableBundles: typeof body.enableBundles === 'boolean' ? body.enableBundles : config.enableOnInstall === true,
|
|
877
|
+
// 默认:本机已装(同名或同插件不同版本)先跳过,其余照装,最后再问
|
|
878
|
+
deferExisting: body.deferExisting !== false && body.replaceExisting !== true,
|
|
879
|
+
replaceExisting: body.replaceExisting === true,
|
|
880
|
+
only: Array.isArray(body.only) && body.only.length > 0 ? body.only.map(String) : null,
|
|
881
|
+
// 联网且有插件市场时优先按市场版本装(默认开;请求里能逐次关掉)
|
|
882
|
+
preferMarket: typeof body.preferMarket === 'boolean' ? body.preferMarket : config.preferMarketOnInstall !== false,
|
|
883
|
+
})
|
|
884
|
+
|
|
885
|
+
const runInstall = async (inspected, body, job) => {
|
|
886
|
+
const options = installOptionsOf(body)
|
|
887
|
+
const push = (pct, note) => {
|
|
888
|
+
if (!job) return
|
|
889
|
+
if (job.cancelled) throw new JobCancelled('已取消')
|
|
890
|
+
job.pct = pct
|
|
891
|
+
job.note = note || job.note
|
|
892
|
+
job.updatedAt = new Date().toISOString()
|
|
893
|
+
}
|
|
894
|
+
const scanGroups = groupFindings(inspected.scan.findings, inspected.manifest.plugins)
|
|
895
|
+
if (inspected.blockingReasons.length > 0 && !options.force) {
|
|
896
|
+
return {
|
|
897
|
+
ok: false,
|
|
898
|
+
refused: true,
|
|
899
|
+
error: inspected.blockingReasons.join(';'),
|
|
900
|
+
integrity: inspected.integrity,
|
|
901
|
+
scan: inspected.scan,
|
|
902
|
+
scanGroups,
|
|
903
|
+
report: inspected.report,
|
|
737
904
|
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
905
|
+
}
|
|
906
|
+
push(12, '开始安装')
|
|
907
|
+
const result = await installPack({
|
|
908
|
+
manifest: inspected.manifest,
|
|
909
|
+
entries: inspected.entries,
|
|
910
|
+
config,
|
|
911
|
+
options: { ...options, onProgress: (event) => push(event.pct, event.note) },
|
|
912
|
+
context: {
|
|
913
|
+
profileDir: profileDir(),
|
|
914
|
+
// 联网且市场可用时,优先按市场版本装(能用就返回 name@version,用不了返回 null 走包内)
|
|
915
|
+
resolveMarket: async (plugin) => {
|
|
916
|
+
try {
|
|
917
|
+
const { meta } = await npmMetadata(plugin.name, config)
|
|
918
|
+
const version = pickVersion(meta, plugin.version || plugin.spec || '') || pickVersion(meta, '')
|
|
919
|
+
if (!version) return null
|
|
920
|
+
appendLog('market ' + plugin.name + '@' + version + ' 优先于包内 ' + (plugin.version || '(未标版本)'))
|
|
921
|
+
return { spec: plugin.name + '@' + version, version }
|
|
922
|
+
} catch (error) {
|
|
923
|
+
appendLog('market miss ' + plugin.name + ': ' + String((error && error.message) || error))
|
|
924
|
+
return null
|
|
925
|
+
}
|
|
758
926
|
},
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
927
|
+
resolveSource: async (plugin, packRoot) => {
|
|
928
|
+
const downloaded = await downloadPackage(plugin.name, plugin.version || plugin.requested || '', config)
|
|
929
|
+
const target = path.join(packRoot, 'plugins', plugin.dirName || plugin.name)
|
|
930
|
+
ensureDir(target)
|
|
931
|
+
for (const entry of downloaded.entries) {
|
|
932
|
+
const dest = path.join(target, entry.name)
|
|
933
|
+
if (!path.resolve(dest).startsWith(path.resolve(target) + path.sep)) continue
|
|
934
|
+
ensureDir(path.dirname(dest))
|
|
935
|
+
fs.writeFileSync(dest, entry.data)
|
|
936
|
+
}
|
|
937
|
+
appendLog('resolve ' + plugin.name + '@' + downloaded.version + ' from ' + downloaded.registry)
|
|
938
|
+
return target
|
|
939
|
+
},
|
|
940
|
+
},
|
|
941
|
+
})
|
|
942
|
+
appendHistory({ at: new Date().toISOString(), kind: 'install', name: inspected.manifest.name, format: inspected.manifest.format, ok: result.ok, pack: inspected.manifest.name, actions: result.actions.length }, config.historyLimit)
|
|
943
|
+
const token = rememberInstall(inspected)
|
|
944
|
+
return {
|
|
945
|
+
ok: result.ok,
|
|
946
|
+
...result,
|
|
947
|
+
token,
|
|
948
|
+
manifest: inspected.manifest,
|
|
949
|
+
scan: inspected.scan,
|
|
950
|
+
scanGroups,
|
|
951
|
+
report: inspected.report,
|
|
952
|
+
integrity: inspected.integrity,
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
const performInstall = async (body, job) => {
|
|
957
|
+
try {
|
|
958
|
+
const push = (pct, note) => {
|
|
959
|
+
if (!job) return
|
|
960
|
+
if (job.cancelled) throw new JobCancelled('已取消')
|
|
961
|
+
job.pct = pct
|
|
962
|
+
job.note = note || job.note
|
|
963
|
+
}
|
|
964
|
+
push(4, '读取整合包')
|
|
965
|
+
const inspected = await inspectInput(body)
|
|
966
|
+
return await runInstall(inspected, body, job)
|
|
762
967
|
} catch (error) {
|
|
763
|
-
|
|
968
|
+
if (error instanceof JobCancelled) return { ok: false, cancelled: true, error: '已取消(没有改动 profile)' }
|
|
969
|
+
return { ok: false, error: String((error && error.message) || error) }
|
|
764
970
|
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
const finishInstallJob = (job, payload) => {
|
|
974
|
+
job.status = payload && payload.ok ? 'done' : payload && payload.cancelled === true ? 'cancelled' : 'failed'
|
|
975
|
+
job.data = payload
|
|
976
|
+
job.pct = payload && payload.ok ? 100 : job.pct
|
|
977
|
+
job.note = payload && payload.ok ? '完成' : ((payload && payload.error) || '已停止')
|
|
978
|
+
if (!(payload && payload.ok)) job.error = (payload && payload.error) || job.note
|
|
979
|
+
job.finishedAt = new Date().toISOString()
|
|
980
|
+
job.updatedAt = job.finishedAt
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
const startInstallJob = (body, label) => {
|
|
984
|
+
const job = createJob('install', label || '安装整合包')
|
|
985
|
+
performInstall(body, job)
|
|
986
|
+
.then((payload) => finishInstallJob(job, payload))
|
|
987
|
+
.catch((error) => {
|
|
988
|
+
job.status = 'failed'
|
|
989
|
+
job.error = String((error && error.message) || error)
|
|
990
|
+
job.note = '失败'
|
|
991
|
+
job.finishedAt = new Date().toISOString()
|
|
992
|
+
job.updatedAt = job.finishedAt
|
|
993
|
+
})
|
|
994
|
+
return job
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
route('/dsh-modpack-kit/api/install', async (req, res) => {
|
|
998
|
+
if (!needPost(req, res)) return
|
|
999
|
+
const body = asJson(await readBody(req), {})
|
|
1000
|
+
if (body.async === true || body.background === true) {
|
|
1001
|
+
const job = startInstallJob(body, body.label ? String(body.label) : undefined)
|
|
1002
|
+
json(res, 200, { ok: true, async: true, job: publicJob(job) })
|
|
1003
|
+
return
|
|
1004
|
+
}
|
|
1005
|
+
const payload = await performInstall(body, null)
|
|
1006
|
+
json(res, 200, payload)
|
|
1007
|
+
})
|
|
1008
|
+
|
|
1009
|
+
// 装完的追问:把刚才跳过的那些换成包内版本
|
|
1010
|
+
route('/dsh-modpack-kit/api/install/replace', async (req, res) => {
|
|
1011
|
+
if (!needPost(req, res)) return
|
|
1012
|
+
const body = asJson(await readBody(req), {})
|
|
1013
|
+
const token = String(body.token || '')
|
|
1014
|
+
const item = pendingInstalls.get(token)
|
|
1015
|
+
if (!item) { json(res, 400, { ok: false, error: '这次安装的上下文已过期,请重新读一遍整合包' }); return }
|
|
1016
|
+
const names = Array.isArray(body.names) && body.names.length > 0 ? body.names.map(String) : null
|
|
1017
|
+
if (body.async === true || body.background === true) {
|
|
1018
|
+
const job = createJob('install', '替换为包内版本' + (names ? '(' + names.length + ' 个)' : ''))
|
|
1019
|
+
runInstall(item.inspected, { ...body, replaceExisting: true, keepLocal: false, only: names }, job)
|
|
1020
|
+
.then((payload) => finishInstallJob(job, payload))
|
|
1021
|
+
.catch((error) => {
|
|
1022
|
+
job.status = 'failed'
|
|
1023
|
+
job.error = String((error && error.message) || error)
|
|
1024
|
+
job.note = '失败'
|
|
1025
|
+
job.finishedAt = new Date().toISOString()
|
|
1026
|
+
job.updatedAt = job.finishedAt
|
|
1027
|
+
})
|
|
1028
|
+
json(res, 200, { ok: true, async: true, job: publicJob(job) })
|
|
1029
|
+
return
|
|
1030
|
+
}
|
|
1031
|
+
const payload = await runInstall(item.inspected, { ...body, replaceExisting: true, keepLocal: false, only: names }, null)
|
|
1032
|
+
json(res, 200, payload)
|
|
765
1033
|
})
|
|
766
1034
|
|
|
767
1035
|
// —— 历史 ——
|