dsh-modpack-kit 0.0.0-stage → 0.1.3
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/LICENSE +75 -0
- package/README.md +224 -2
- package/assets/icon-180.png +0 -0
- package/assets/install-stub.cmd +15 -0
- package/assets/install-stub.mjs +259 -0
- package/assets/install-stub.sh +9 -0
- package/client.js +1275 -0
- package/cordis.patch.yml +3 -0
- package/docs/DETAIL.md +97 -0
- package/icon.svg +14 -0
- package/index.js +820 -0
- package/lib/classify.mjs +105 -0
- package/lib/compat.mjs +112 -0
- package/lib/install.mjs +349 -0
- package/lib/manifest.mjs +197 -0
- package/lib/market-catalog.mjs +419 -0
- package/lib/pack.mjs +460 -0
- package/lib/profile.mjs +274 -0
- package/lib/registry.mjs +223 -0
- package/lib/scan.mjs +325 -0
- package/lib/semver.mjs +143 -0
- package/lib/store.mjs +183 -0
- package/lib/tar.mjs +107 -0
- package/lib/yamlish.mjs +385 -0
- package/lib/zip.mjs +232 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +54 -3
package/lib/classify.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 分类:插件市场有分区,本插件照同一套分类给「本机已装」和「市场目录」打标签,
|
|
3
|
+
* 让两边在同一张列表里能按类筛。纯字符串匹配,无网络、无副作用。
|
|
4
|
+
*
|
|
5
|
+
* 想加分类:往 CATEGORIES 加一条,再把关键词塞进 RULES;客户端 client.js 顶部的
|
|
6
|
+
* CATEGORY_LABELS 只用来显示名字,两边 id 必须一致。
|
|
7
|
+
*
|
|
8
|
+
* 匹配规则(2026-11-15 修):英文词按词边界匹配,避免 'ai' 命中 'detail'、'ui' 命中
|
|
9
|
+
* 'build' 这类误判;中文词直接子串匹配。市场数据里带了 category/分区字段时优先采信。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export const CATEGORIES = [
|
|
13
|
+
{ id: 'ui', label: '界面与外观' },
|
|
14
|
+
{ id: 'tool', label: '效率与工具' },
|
|
15
|
+
{ id: 'ai', label: 'AI 与模型' },
|
|
16
|
+
{ id: 'dev', label: '开发与调试' },
|
|
17
|
+
{ id: 'media', label: '媒体与内容' },
|
|
18
|
+
{ id: 'net', label: '网络与数据' },
|
|
19
|
+
{ id: 'pack', label: '打包与分发' },
|
|
20
|
+
{ id: 'other', label: '其它' },
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
const RULES = [
|
|
24
|
+
{ id: 'pack', words: ['modpack', 'installer', '插件市场', 'plugin market', 'marketplace', 'catalog', '整合包', '打包', '安装器'] },
|
|
25
|
+
{ id: 'ui', words: ['ui', 'theme', 'themes', 'skin', 'wallpaper', 'icon', 'sidebar', 'panel', 'layout', 'font', 'mascot', 'widget', '界面', '外观', '主题', '图标', '侧栏', '面板', '字体', '配色', '壁纸', '桌宠', '桌面', '挂件', 'locale', 'i18n', '语言'] },
|
|
26
|
+
{ id: 'ai', words: ['llm', 'model', 'gpt', 'claude', 'prompt', 'embedding', 'agent skills', 'skill deck', '技能', '技能包', '模型', '智能体', '推理', '记忆', '上下文'] },
|
|
27
|
+
{ id: 'dev', words: ['dev', 'debug', 'log', 'logs', 'lint', 'test', 'tests', 'code', 'git', 'trace', 'profiler', 'sandbox', '调试', '开发', '日志', '测试', '代码', '编译', '性能', '检查'] },
|
|
28
|
+
{ id: 'media', words: ['image', 'images', 'video', 'music', 'audio', 'media', 'bili', 'bilibili', 'subtitle', 'danmaku', 'screenshot', '图片', '视频', '音乐', '音频', '媒体', '字幕', '弹幕', '截图', 'B站'] },
|
|
29
|
+
{ id: 'net', words: ['http', 'api', 'proxy', 'sync', 'storage', 'database', 'webhook', 'sse', 'socket', 'websocket', 'mcp', 'network', '网络', '代理', '同步', '存储', '数据库', '连接'] },
|
|
30
|
+
{ id: 'tool', words: ['tool', 'tools', 'command', 'search', 'todo', 'note', 'notes', 'schedule', 'clipboard', 'calculator', 'shortcut', '工具', '效率', '快捷', '命令', '搜索', '待办', '笔记', '日程', '剪贴板', '计算'] },
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
const ALIASES = {
|
|
34
|
+
ui: 'ui', theme: 'ui', themes: 'ui', appearance: 'ui', style: 'ui', wallpaper: 'ui',
|
|
35
|
+
tool: 'tool', tools: 'tool', utility: 'tool', utilities: 'tool', productivity: 'tool', efficiency: 'tool',
|
|
36
|
+
ai: 'ai', ai_agent: 'ai', model: 'ai', models: 'ai', llm: 'ai', agent: 'ai', memory: 'ai', context: 'ai',
|
|
37
|
+
dev: 'dev', developer: 'dev', development: 'dev', debug: 'dev', devtools: 'dev', testing: 'dev',
|
|
38
|
+
media: 'media', content: 'media', image: 'media', images: 'media', video: 'media', audio: 'media',
|
|
39
|
+
net: 'net', network: 'net', networking: 'net', data: 'net', sync: 'net', storage: 'net', api: 'net',
|
|
40
|
+
pack: 'pack', bundle: 'pack', bundles: 'pack', modpack: 'pack', market: 'pack', install: 'pack', distribution: 'pack',
|
|
41
|
+
other: 'other', misc: 'other',
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 同分时的偏好:越具体的主题越优先,避免 'agent' 这类泛词把什么都吃成 AI
|
|
45
|
+
const TIE_PRIORITY = { media: 7, dev: 6, ui: 5, net: 4, tool: 3, ai: 2, pack: 1, other: 0 }
|
|
46
|
+
|
|
47
|
+
const ASCII_WORD = /^[\x20-\x7e]+$/
|
|
48
|
+
const escapeRe = (w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
49
|
+
|
|
50
|
+
/** 英文按词边界(允许复数 s/es),中文按子串 */
|
|
51
|
+
function hit(hay, word) {
|
|
52
|
+
const w = String(word || '').toLowerCase()
|
|
53
|
+
if (!w) return false
|
|
54
|
+
if (!ASCII_WORD.test(w)) return hay.includes(w)
|
|
55
|
+
const pattern = w.length <= 2
|
|
56
|
+
? `(^|[^a-z0-9])${escapeRe(w)}([^a-z0-9]|$)`
|
|
57
|
+
: `(^|[^a-z0-9])${escapeRe(w)}(s|es)?([^a-z0-9]|$)`
|
|
58
|
+
return new RegExp(pattern, 'i').test(hay)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const asList = (value) => (Array.isArray(value) ? value.map((x) => String(x).toLowerCase()) : [])
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @param {{name?: string, title?: string, description?: string, tags?: string[], category?: string}} meta
|
|
65
|
+
* @returns {{id: string, label: string, reason: string}}
|
|
66
|
+
*/
|
|
67
|
+
export function classifyPlugin(meta) {
|
|
68
|
+
const name = String(meta?.name || '').toLowerCase()
|
|
69
|
+
const title = String(meta?.title || '').toLowerCase()
|
|
70
|
+
const rest = [String(meta?.description || '').toLowerCase(), ...asList(meta?.tags), String(meta?.category || '').toLowerCase()].join(' ')
|
|
71
|
+
|
|
72
|
+
let best = null
|
|
73
|
+
for (const rule of RULES) {
|
|
74
|
+
// 每个字段只计一次,避免 'tool' 与 'tools' 同时命中把分数翻倍
|
|
75
|
+
let score = 0
|
|
76
|
+
const hits = []
|
|
77
|
+
for (const [field, weight] of [[name, 5], [title, 3], [rest, 1]]) {
|
|
78
|
+
const matched = rule.words.filter((word) => hit(field, word))
|
|
79
|
+
if (!matched.length) continue
|
|
80
|
+
matched.sort((a, b) => b.length - a.length)
|
|
81
|
+
hits.push(...matched)
|
|
82
|
+
score += weight
|
|
83
|
+
}
|
|
84
|
+
const better = !best
|
|
85
|
+
|| score > best.score
|
|
86
|
+
|| (score === best.score && hits.length > best.hits.length)
|
|
87
|
+
|| (score === best.score && hits.length === best.hits.length && (TIE_PRIORITY[rule.id] || 0) > (TIE_PRIORITY[best.id] || 0))
|
|
88
|
+
if (score > 0 && better) best = { id: rule.id, score, hits }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 市场数据里明确给了分区:优先采信,但仍要求上面没有更硬的证据(名字命中)
|
|
92
|
+
const byAlias = ALIASES[String(meta?.category || '').toLowerCase().trim()]
|
|
93
|
+
if (byAlias && (!best || best.score < 5)) {
|
|
94
|
+
const label = (CATEGORIES.find((c) => c.id === byAlias) || { label: '其它' }).label
|
|
95
|
+
return { id: byAlias, label, reason: '市场分区:' + meta.category }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!best) return { id: 'other', label: '其它', reason: '没有命中分类关键词' }
|
|
99
|
+
const label = (CATEGORIES.find((c) => c.id === best.id) || { label: '其它' }).label
|
|
100
|
+
return { id: best.id, label, reason: '命中:' + best.hits.slice(0, 3).join('、') }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function categoryLabel(id) {
|
|
104
|
+
return (CATEGORIES.find((c) => c.id === id) || { label: '其它' }).label
|
|
105
|
+
}
|
package/lib/compat.mjs
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-modpack —— 兼容性检查。
|
|
3
|
+
*
|
|
4
|
+
* 只报「确定能判断的」问题:peer 依赖在不在、版本满不满足、engines 对不对、
|
|
5
|
+
* license 允不允许、同 id 的配置会不会被覆盖。判断不了的一律说「无法判断」,
|
|
6
|
+
* 不装作通过——安装包最怕的就是假绿灯。
|
|
7
|
+
*/
|
|
8
|
+
import { satisfies, parseVersion, compareVersions } from './semver.mjs'
|
|
9
|
+
|
|
10
|
+
const HOST_SCOPE = /^@deepseek-ai\//u
|
|
11
|
+
|
|
12
|
+
function versionOf(map, name) {
|
|
13
|
+
const hit = map.get(name)
|
|
14
|
+
return hit ? hit.version : null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {object[]} plugins 清单里的插件条目
|
|
19
|
+
* @param {object} context {installed: object[], dshVersion, nodeVersion, config}
|
|
20
|
+
*/
|
|
21
|
+
export function checkCompatibility(plugins, context) {
|
|
22
|
+
const installedMap = new Map((context.installed || []).map((p) => [p.name, p]))
|
|
23
|
+
const packMap = new Map((plugins || []).map((p) => [p.name, p]))
|
|
24
|
+
const findings = []
|
|
25
|
+
const add = (level, plugin, code, message, suggestion) => findings.push({ level, plugin, code, message, suggestion })
|
|
26
|
+
const dshVersion = context.dshVersion || ''
|
|
27
|
+
const nodeVersion = context.nodeVersion || process.versions.node
|
|
28
|
+
const allow = new Set((context.config?.licenseAllow || []).map((x) => String(x).toLowerCase()))
|
|
29
|
+
|
|
30
|
+
for (const plugin of plugins || []) {
|
|
31
|
+
const local = installedMap.get(plugin.name)
|
|
32
|
+
if (local) {
|
|
33
|
+
if (local.version !== plugin.version) {
|
|
34
|
+
const cmp = compareVersions(plugin.version, local.version)
|
|
35
|
+
add('info', plugin.name, 'local-version',
|
|
36
|
+
'本地已装 ' + local.version + ',整合包里是 ' + plugin.version + (cmp === null ? '' : cmp > 0 ? '(较新)' : cmp < 0 ? '(较旧,可能降级)' : ''),
|
|
37
|
+
cmp !== null && cmp < 0 ? '确认要降级再装;降级可能丢掉新版本的配置项。' : '安装会按整合包里的版本处理。')
|
|
38
|
+
}
|
|
39
|
+
if (!local.isBundle && plugin.isBundle) {
|
|
40
|
+
add('warn', plugin.name, 'local-not-bundle', '本地同名包不是 DSH 插件(没有 dsh 段)', '确认整合包来源;装完可能不会出现在侧栏。')
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
add('info', plugin.name, 'not-installed', '本地未安装,将新增', '')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const peers = plugin.peerDependencies || {}
|
|
47
|
+
for (const [peer, range] of Object.entries(peers)) {
|
|
48
|
+
if (HOST_SCOPE.test(peer)) continue
|
|
49
|
+
const inPack = packMap.get(peer)
|
|
50
|
+
const inLocal = installedMap.get(peer)
|
|
51
|
+
const found = inPack ? inPack.version : inLocal ? inLocal.version : null
|
|
52
|
+
if (!found) {
|
|
53
|
+
add('warn', plugin.name, 'peer-missing-' + peer,
|
|
54
|
+
'需要 ' + peer + ' ' + range + ',整合包和本机都没有',
|
|
55
|
+
'安装后这个插件可能报错或功能缺失;把它一起打包进来。')
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
const ok = satisfies(found, range)
|
|
59
|
+
if (ok === false) {
|
|
60
|
+
add('error', plugin.name, 'peer-conflict-' + peer,
|
|
61
|
+
'需要 ' + peer + ' ' + range + ',实际是 ' + found + '(版本冲突)',
|
|
62
|
+
'换成满足范围的 ' + peer + ' 版本,或把整合包里那个插件换版本。')
|
|
63
|
+
} else if (ok === null) {
|
|
64
|
+
add('info', plugin.name, 'peer-unknown-' + peer, 'peer 范围看不懂:' + peer + ' ' + range, '请人工确认。')
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const engines = plugin.engines || {}
|
|
69
|
+
if (engines.dsh && dshVersion) {
|
|
70
|
+
const ok = satisfies(dshVersion, String(engines.dsh))
|
|
71
|
+
if (ok === false) {
|
|
72
|
+
add('error', plugin.name, 'engines-dsh', '要求 DSH ' + engines.dsh + ',本机是 ' + dshVersion,
|
|
73
|
+
'先升级 DSH 再装,或换一个兼容的插件版本。')
|
|
74
|
+
} else if (ok === null) {
|
|
75
|
+
add('info', plugin.name, 'engines-dsh-unknown', 'engines.dsh 看不懂:' + engines.dsh, '')
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (engines.node && nodeVersion) {
|
|
79
|
+
const ok = satisfies(String(nodeVersion), String(engines.node))
|
|
80
|
+
if (ok === false) {
|
|
81
|
+
add('error', plugin.name, 'engines-node', '要求 Node ' + engines.node + ',本机是 ' + nodeVersion,
|
|
82
|
+
'DSH 自带 Node 版本不可换;请找兼容的插件版本。')
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!plugin.license) {
|
|
87
|
+
add('warn', plugin.name, 'license-missing', '插件没写 license', '再分发前请联系作者确认授权,否则不要传给别人。')
|
|
88
|
+
} else if (!allow.has(String(plugin.license).toLowerCase())) {
|
|
89
|
+
add('warn', plugin.name, 'license-unlisted', 'license 不在允许列表:' + plugin.license,
|
|
90
|
+
'确认许可允许再分发;不确定就只在自己机器上装。')
|
|
91
|
+
}
|
|
92
|
+
if (plugin.localEdits) {
|
|
93
|
+
add('info', plugin.name, 'local-edits', '打包时带的是打包者改过的本地文件', '和上游发布的版本可能不一致。')
|
|
94
|
+
}
|
|
95
|
+
if (!plugin.hasClient && !plugin.hasPatch && plugin.isBundle === false) {
|
|
96
|
+
add('warn', plugin.name, 'not-bundle', '这个包没有 dsh 段,可能不是 DSH 插件', '确认是不是装错了东西。')
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const errors = findings.filter((f) => f.level === 'error')
|
|
101
|
+
const warns = findings.filter((f) => f.level === 'warn')
|
|
102
|
+
return {
|
|
103
|
+
findings,
|
|
104
|
+
blocking: errors.length > 0,
|
|
105
|
+
counts: { error: errors.length, warn: warns.length, info: findings.length - errors.length - warns.length },
|
|
106
|
+
summary: errors.length > 0
|
|
107
|
+
? '有 ' + errors.length + ' 个必须处理的问题,' + warns.length + ' 个提醒'
|
|
108
|
+
: warns.length > 0
|
|
109
|
+
? '可以装,但有 ' + warns.length + ' 个提醒'
|
|
110
|
+
: '没发现兼容问题',
|
|
111
|
+
}
|
|
112
|
+
}
|
package/lib/install.mjs
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-modpack —— 读包 / 检测 / 安装。
|
|
3
|
+
*
|
|
4
|
+
* 安装只有一条腿:调用官方 `dsh plugin add file:<目录>`。整合包不自己写 node_modules,
|
|
5
|
+
* 也不自己改 pnpm 锁——那样迟早和插件市场打架。本模块负责的是它前面那一步:
|
|
6
|
+
* 解出文件、校验完整性、扫一遍风险、判断兼容、再把配置补回去。
|
|
7
|
+
*/
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { readZip, readZipText, safeEntryName } from './zip.mjs'
|
|
11
|
+
import { scanEntries, renderScanReport } from './scan.mjs'
|
|
12
|
+
import { MANIFEST_NAME, normalizeManifest, sha256 } from './manifest.mjs'
|
|
13
|
+
import { checkCompatibility } from './compat.mjs'
|
|
14
|
+
import { captureConfigs, collectFiles } from './pack.mjs'
|
|
15
|
+
import { upsertPatchItem, parseYaml } from './yamlish.mjs'
|
|
16
|
+
import {
|
|
17
|
+
ensureDir,
|
|
18
|
+
writeJsonAtomic,
|
|
19
|
+
appendLog,
|
|
20
|
+
pathsFor,
|
|
21
|
+
slugify,
|
|
22
|
+
} from './store.mjs'
|
|
23
|
+
import {
|
|
24
|
+
ensureBundles,
|
|
25
|
+
setBundles,
|
|
26
|
+
readBundles,
|
|
27
|
+
readProfilePackage,
|
|
28
|
+
listInstalled,
|
|
29
|
+
dshVersion,
|
|
30
|
+
patchPath,
|
|
31
|
+
readPatch,
|
|
32
|
+
writePatch,
|
|
33
|
+
runDshPlugin,
|
|
34
|
+
} from './profile.mjs'
|
|
35
|
+
|
|
36
|
+
export class InstallRefused extends Error {
|
|
37
|
+
constructor(message, scan) {
|
|
38
|
+
super(message)
|
|
39
|
+
this.name = 'InstallRefused'
|
|
40
|
+
this.scan = scan
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const META_ENTRIES = new Set([MANIFEST_NAME, 'README.txt', 'integrity.json'])
|
|
45
|
+
const isMeta = (name) => META_ENTRIES.has(name) || name.startsWith('security/')
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 在内存里检查一个包:解 zip → 找清单 → 校验完整性 → 扫描。
|
|
49
|
+
* 不写任何东西;调用方拿到结果后再决定要不要装。
|
|
50
|
+
*/
|
|
51
|
+
export function inspectPackBuffer(buffer, options = {}) {
|
|
52
|
+
const config = options.config || {}
|
|
53
|
+
const notes = []
|
|
54
|
+
const entries = readZip(buffer, {
|
|
55
|
+
maxEntryBytes: config.maxFileBytes ?? 16 * 1024 * 1024,
|
|
56
|
+
maxTotalBytes: config.maxPackBytes ?? 256 * 1024 * 1024,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const manifestText = readZipText(entries, MANIFEST_NAME)
|
|
60
|
+
if (!manifestText) throw new Error('这个文件不是 DSH 整合包:缺少 ' + MANIFEST_NAME)
|
|
61
|
+
let raw
|
|
62
|
+
try {
|
|
63
|
+
raw = JSON.parse(manifestText)
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new Error('清单无法解析:' + String(error?.message || error))
|
|
66
|
+
}
|
|
67
|
+
const manifest = normalizeManifest(raw)
|
|
68
|
+
|
|
69
|
+
const integrity = { present: false, ok: true, mismatches: [], missing: [] }
|
|
70
|
+
const integrityText = readZipText(entries, 'integrity.json')
|
|
71
|
+
if (integrityText) {
|
|
72
|
+
integrity.present = true
|
|
73
|
+
try {
|
|
74
|
+
const record = JSON.parse(integrityText)
|
|
75
|
+
const files = record && typeof record.files === 'object' ? record.files : {}
|
|
76
|
+
const present = new Map(entries.map((e) => [e.name, e]))
|
|
77
|
+
for (const [name, expected] of Object.entries(files)) {
|
|
78
|
+
const entry = present.get(name)
|
|
79
|
+
if (!entry) { integrity.missing.push(name); continue }
|
|
80
|
+
const actual = sha256(entry.data)
|
|
81
|
+
if (actual !== expected) integrity.mismatches.push(name)
|
|
82
|
+
}
|
|
83
|
+
integrity.ok = integrity.mismatches.length === 0 && integrity.missing.length === 0
|
|
84
|
+
} catch (error) {
|
|
85
|
+
integrity.present = true
|
|
86
|
+
integrity.ok = false
|
|
87
|
+
notes.push('integrity.json 解析失败:' + String(error?.message || error))
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
integrity.present = false
|
|
91
|
+
integrity.ok = false
|
|
92
|
+
notes.push('包内没有 integrity.json,无法确认文件没被改过(已阻止安装;来源可信时可用「强制」跳过)')
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const payload = entries.filter((e) => !isMeta(e.name))
|
|
96
|
+
const scan = options.scanBeforeInstall === false
|
|
97
|
+
? { findings: [], counts: { high: 0, medium: 0, low: 0 }, scannedFiles: 0, scannedBytes: 0, skipped: [], redacted: true, note: '未执行扫描(调用方关闭)' }
|
|
98
|
+
: scanEntries(payload.map((e) => ({ name: e.name, data: e.data })), { extraRules: options.extraScanRules })
|
|
99
|
+
const report = renderScanReport(manifest.name, scan, { approvedRisk: Boolean(options.force) })
|
|
100
|
+
|
|
101
|
+
const blockingReasons = []
|
|
102
|
+
if (!integrity.present && !options.force) {
|
|
103
|
+
blockingReasons.push('包里没有 integrity.json,无法确认文件没被改过')
|
|
104
|
+
}
|
|
105
|
+
if (integrity.present && !integrity.ok) {
|
|
106
|
+
blockingReasons.push('完整性校验失败(' + (integrity.mismatches.length + integrity.missing.length) + ' 个文件对不上)')
|
|
107
|
+
}
|
|
108
|
+
// 打包方已确认(市场收录插件的确认放行)时,不再用高风险拦人;报告照常列明细。
|
|
109
|
+
const acknowledged = Boolean(manifest.security && (manifest.security.acknowledged === true || manifest.security.trustedOnly === true))
|
|
110
|
+
if (scan.counts.high > 0 && !options.force && !acknowledged) {
|
|
111
|
+
blockingReasons.push('安全检测发现 ' + scan.counts.high + ' 个高风险项')
|
|
112
|
+
}
|
|
113
|
+
if (scan.counts.high > 0 && acknowledged) {
|
|
114
|
+
notes.push('打包方已确认 ' + scan.counts.high + ' 个高风险项(市场收录插件),明细见扫描报告')
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return { manifest, entries, integrity, scan, report, blockingReasons, notes }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 从链接读取整合包(只允许 https 与白名单域名)。 */
|
|
121
|
+
export async function readPackFromUrl(url, config, fetchLimitedFn) {
|
|
122
|
+
const buffer = await fetchLimitedFn(url, config, {
|
|
123
|
+
maxBytes: config.maxDownloadBytes ?? 128 * 1024 * 1024,
|
|
124
|
+
accept: 'application/octet-stream, application/zip, */*',
|
|
125
|
+
})
|
|
126
|
+
return buffer
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function pluginPayload(entries, dirName) {
|
|
130
|
+
const prefix = 'plugins/' + dirName + '/'
|
|
131
|
+
return entries.filter((e) => e.name.startsWith(prefix))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 把某个插件的文件释放到 vendor 目录,返回可交给 `dsh plugin add file:` 的目录。
|
|
136
|
+
* 目录名带包 id,避免不同整合包互相覆盖。
|
|
137
|
+
*/
|
|
138
|
+
export function materializePayload(entries, manifest, plugin, config) {
|
|
139
|
+
const base = pathsFor(config).vendor
|
|
140
|
+
const packDir = path.join(base, slugify(manifest.id || manifest.name || 'pack', 'pack'))
|
|
141
|
+
const target = path.join(packDir, 'plugins', plugin.dirName || plugin.name)
|
|
142
|
+
const files = pluginPayload(entries, plugin.dirName || plugin.name)
|
|
143
|
+
if (files.length === 0) throw new Error(plugin.name + ':包里没有它的文件(轻量版需要用网络取)')
|
|
144
|
+
// 重装时旧文件必须清掉:pnpm 会把目录里的一切都装进去,残留文件会让新版本带上旧代码
|
|
145
|
+
try { fs.rmSync(target, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }) } catch {}
|
|
146
|
+
ensureDir(target)
|
|
147
|
+
for (const entry of files) {
|
|
148
|
+
const rel = entry.name.slice(('plugins/' + (plugin.dirName || plugin.name) + '/').length)
|
|
149
|
+
const safeRel = safeEntryName(rel)
|
|
150
|
+
const dest = path.join(target, safeRel)
|
|
151
|
+
const resolved = path.resolve(dest)
|
|
152
|
+
if (!resolved.startsWith(path.resolve(target) + path.sep) && resolved !== path.resolve(target)) {
|
|
153
|
+
throw new Error('包内路径逃逸:' + entry.name)
|
|
154
|
+
}
|
|
155
|
+
ensureDir(path.dirname(resolved))
|
|
156
|
+
fs.writeFileSync(resolved, entry.data)
|
|
157
|
+
}
|
|
158
|
+
return target
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function packDirOf(config, manifest) {
|
|
162
|
+
return path.join(pathsFor(config).vendor, slugify(manifest.id || manifest.name || 'pack', 'pack'))
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 安装整合包。
|
|
167
|
+
* @param {object} input
|
|
168
|
+
* @param {object} input.manifest
|
|
169
|
+
* @param {object[]} input.entries
|
|
170
|
+
* @param {object} input.config
|
|
171
|
+
* @param {object} input.options {force, keepLocal, applyConfig, dryRun}
|
|
172
|
+
* @param {object} input.context {profileDir, onLine}
|
|
173
|
+
*/
|
|
174
|
+
export async function installPack(input) {
|
|
175
|
+
const { manifest, entries, config, options = {}, context = {} } = input
|
|
176
|
+
const profileDir = context.profileDir || pathsFor(config).profile
|
|
177
|
+
const profile = config.profile || 'desktop'
|
|
178
|
+
const installed = listInstalled(profileDir)
|
|
179
|
+
const compat = checkCompatibility(manifest.plugins, {
|
|
180
|
+
installed,
|
|
181
|
+
dshVersion: dshVersion(profileDir),
|
|
182
|
+
nodeVersion: process.versions.node,
|
|
183
|
+
config,
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
const actions = []
|
|
187
|
+
const lines = []
|
|
188
|
+
const onLine = typeof context.onLine === 'function' ? context.onLine : () => {}
|
|
189
|
+
|
|
190
|
+
if (compat.blocking && !options.force) {
|
|
191
|
+
return {
|
|
192
|
+
ok: false,
|
|
193
|
+
refused: true,
|
|
194
|
+
reason: '兼容性检测有 ' + compat.counts.error + ' 个阻断问题',
|
|
195
|
+
compat,
|
|
196
|
+
actions,
|
|
197
|
+
lines,
|
|
198
|
+
needsRestart: false,
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const touchedBundles = []
|
|
203
|
+
const configItems = []
|
|
204
|
+
const packRoot = packDirOf(config, manifest)
|
|
205
|
+
// CLI 自己会把 bundle 登记进 profile;「装好先不启用」靠装完撤回列表实现,所以先记一份原始清单。
|
|
206
|
+
const bundlesBefore = readBundles(readProfilePackage(profileDir))
|
|
207
|
+
|
|
208
|
+
for (const plugin of manifest.plugins) {
|
|
209
|
+
const existing = installed.find((p) => p.name === plugin.name)
|
|
210
|
+
const keepLocal = options.keepLocal !== false && config.keepLocal !== false && manifest.options.keepLocalOnInstall !== false
|
|
211
|
+
if (existing && keepLocal) {
|
|
212
|
+
actions.push({ plugin: plugin.name, action: 'kept', ok: true, message: '本地已装 ' + existing.version + ',按“保留本地修改”跳过覆盖' })
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let dir = null
|
|
217
|
+
if (manifest.format === 'full' && pluginPayload(entries, plugin.dirName || plugin.name).length > 0) {
|
|
218
|
+
try {
|
|
219
|
+
dir = materializePayload(entries, manifest, plugin, config)
|
|
220
|
+
} catch (error) {
|
|
221
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: false, message: String(error?.message || error) })
|
|
222
|
+
continue
|
|
223
|
+
}
|
|
224
|
+
} else if (typeof context.resolveSource === 'function') {
|
|
225
|
+
try {
|
|
226
|
+
dir = await context.resolveSource(plugin, packRoot)
|
|
227
|
+
} catch (error) {
|
|
228
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: false, message: '取包失败:' + String(error?.message || error) })
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: false, message: '轻量包需要在联网环境下解析来源' })
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (manifest.format !== 'full') {
|
|
237
|
+
// 轻量包本体不夹带源码,真正的代码是刚下载回来的:装之前必须补扫一遍,否则「安装前检测」是空转
|
|
238
|
+
try {
|
|
239
|
+
const brought = collectFiles(dir, {
|
|
240
|
+
prefix: '',
|
|
241
|
+
maxFileBytes: config.maxFileBytes ?? 16 * 1024 * 1024,
|
|
242
|
+
maxTotalBytes: 64 * 1024 * 1024,
|
|
243
|
+
})
|
|
244
|
+
const deep = scanEntries(brought.files, { extraRules: Array.isArray(config.extraScanRules) ? config.extraScanRules : [] })
|
|
245
|
+
if (deep.counts.high > 0 && !options.force && !acknowledged) {
|
|
246
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: false, message: '从市场取回的代码里有 ' + deep.counts.high + ' 个高风险项,已拦下' })
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
lines.push('轻量包:已补扫从市场取回的 ' + deep.scannedFiles + ' 个文件(高 ' + deep.counts.high + ')')
|
|
250
|
+
} catch (error) {
|
|
251
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: false, message: '补充扫描失败:' + String(error?.message || error) })
|
|
252
|
+
continue
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (options.dryRun) {
|
|
257
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: true, message: '演练:将添加 ' + dir })
|
|
258
|
+
continue
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const runCli = typeof context.runCli === 'function' ? context.runCli : runDshPlugin
|
|
262
|
+
const result = await runCli(profile, ['plugin', '--profile', profile, 'add', 'file:' + dir], {
|
|
263
|
+
cwd: profileDir,
|
|
264
|
+
onLine: (line) => { lines.push(line); onLine(line) },
|
|
265
|
+
})
|
|
266
|
+
appendLog('install ' + plugin.name + ' exit=' + result.code)
|
|
267
|
+
if (result.code !== 0) {
|
|
268
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: false, message: 'dsh plugin add 失败(退出码 ' + result.code + '),详见日志' })
|
|
269
|
+
continue
|
|
270
|
+
}
|
|
271
|
+
if (plugin.isBundle && plugin.hasPatch) touchedBundles.push(plugin.name)
|
|
272
|
+
actions.push({ plugin: plugin.name, action: 'install', ok: true, message: '已装 ' + (plugin.version || '') })
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (!options.dryRun) {
|
|
276
|
+
const before = bundlesBefore
|
|
277
|
+
const after = readBundles(readProfilePackage(profileDir))
|
|
278
|
+
if (options.enableBundles === false) {
|
|
279
|
+
// 沙箱/试验场景:文件与依赖都装好,但不登记 bundle —— 重启也不会加载。
|
|
280
|
+
if (after.length !== before.length || after.some((x) => !before.includes(x))) {
|
|
281
|
+
const restored = setBundles(profileDir, before)
|
|
282
|
+
if (restored) lines.push('按设置未启用:已把 bundle 清单撤回原样(' + touchedBundles.join(', ') + ' 装了但不加载)')
|
|
283
|
+
} else {
|
|
284
|
+
lines.push('按设置未启用:' + (touchedBundles.join(', ') || '本次插件') + ' 没有登记 bundle,重启也不会加载')
|
|
285
|
+
}
|
|
286
|
+
} else if (touchedBundles.length > 0) {
|
|
287
|
+
const ensured = ensureBundles(profileDir, touchedBundles)
|
|
288
|
+
if (ensured.changed) lines.push('已登记 bundle:' + ensured.added.join(', ') + '(原清单备份 ' + ensured.backup + ')')
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 配置回填:只改目标条目,其它字节不动
|
|
293
|
+
if (options.applyConfig !== false && manifest.options.includeConfig) {
|
|
294
|
+
const patch = readPatch(profileDir)
|
|
295
|
+
let text = patch
|
|
296
|
+
for (const plugin of manifest.plugins) {
|
|
297
|
+
const snap = manifest.plugins.find((p) => p.name === plugin.name && p.configIncluded)
|
|
298
|
+
if (!snap) continue
|
|
299
|
+
if (options.keepLocal !== false && config.keepLocal !== false) {
|
|
300
|
+
const captured = captureConfigs(text, [snap], (t) => parseYaml(t)).captured
|
|
301
|
+
if (captured.size > 0) {
|
|
302
|
+
actions.push({ plugin: plugin.name, action: 'config', ok: true, message: '本地已有配置,按“保留本地”不覆盖' })
|
|
303
|
+
continue
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const entry = entries.find((e) => e.name === 'configs/' + plugin.name + '.json')
|
|
307
|
+
if (!entry) continue
|
|
308
|
+
let snapConfig
|
|
309
|
+
try { snapConfig = JSON.parse(entry.data.toString('utf8')) } catch { continue }
|
|
310
|
+
const result = upsertPatchItem(text, {
|
|
311
|
+
id: snapConfig.id || plugin.name,
|
|
312
|
+
name: snapConfig.name || plugin.name,
|
|
313
|
+
config: snapConfig.config ?? null,
|
|
314
|
+
disabled: snapConfig.disabled,
|
|
315
|
+
})
|
|
316
|
+
text = result.text
|
|
317
|
+
configItems.push({ plugin: plugin.name, action: result.action })
|
|
318
|
+
actions.push({ plugin: plugin.name, action: 'config', ok: true, message: (result.action === 'replaced' ? '已对齐配置' : '已补上配置条目') })
|
|
319
|
+
}
|
|
320
|
+
if (text !== patch && !options.dryRun) {
|
|
321
|
+
const backup = writePatch(profileDir, text)
|
|
322
|
+
lines.push('配置层已更新,备份:' + backup)
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const ok = actions.every((a) => a.ok)
|
|
327
|
+
const record = {
|
|
328
|
+
at: new Date().toISOString(),
|
|
329
|
+
kind: 'install',
|
|
330
|
+
pack: manifest.name,
|
|
331
|
+
id: manifest.id,
|
|
332
|
+
format: manifest.format,
|
|
333
|
+
ok,
|
|
334
|
+
actions,
|
|
335
|
+
configItems,
|
|
336
|
+
}
|
|
337
|
+
if (!options.dryRun) writeJsonAtomic(path.join(packRoot, 'install-result.json'), record)
|
|
338
|
+
|
|
339
|
+
return {
|
|
340
|
+
ok,
|
|
341
|
+
compat,
|
|
342
|
+
actions,
|
|
343
|
+
configItems,
|
|
344
|
+
lines,
|
|
345
|
+
needsRestart: options.enableBundles === false ? false : actions.some((a) => a.action === 'install' && a.ok),
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export { patchPath }
|