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/pack.mjs
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-modpack —— 打包。
|
|
3
|
+
*
|
|
4
|
+
* 两个版本:
|
|
5
|
+
* full 完整版:插件本体一起装进 zip,没装 dsh-modpack 的人解压也能按 README 手工装;
|
|
6
|
+
* lite 轻量版:只有清单和来源,必须由 dsh-modpack 联网按版本取包。
|
|
7
|
+
*
|
|
8
|
+
* 打包顺序是刻意的:先收集 → 先扫描 → 再决定是否允许写盘。扫描在内存里做,
|
|
9
|
+
* 命中原文不落盘;高风险默认拒绝出包,除非调用方显式 force 并写进报告。
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { writeZip } from './zip.mjs'
|
|
14
|
+
import { scanEntries, renderScanReport, groupFindings } from './scan.mjs'
|
|
15
|
+
import { MANIFEST_NAME, SCHEMA, KIND, normalizeManifest, buildReadme, sha256, shortHash } from './manifest.mjs'
|
|
16
|
+
import { scanFileName } from './scan.mjs'
|
|
17
|
+
import { slugify } from './store.mjs'
|
|
18
|
+
|
|
19
|
+
const STUB_DIR = new URL('../assets/', import.meta.url)
|
|
20
|
+
const stub = (name) => fs.readFileSync(new URL(name, STUB_DIR))
|
|
21
|
+
|
|
22
|
+
const SKIP_DIRS = new Set(['node_modules', '.git', '.svn', '__pycache__', '.cache', '.turbo', '.next', 'coverage'])
|
|
23
|
+
const SKIP_FILES = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini'])
|
|
24
|
+
const DATA_EXT = new Set(['.json', '.md', '.txt', '.yml', '.yaml', '.csv', '.toml', '.ini'])
|
|
25
|
+
|
|
26
|
+
export class PackRefused extends Error {
|
|
27
|
+
constructor(message, scan, extra = {}) {
|
|
28
|
+
super(message)
|
|
29
|
+
this.name = 'PackRefused'
|
|
30
|
+
this.scan = scan
|
|
31
|
+
this.needsAck = extra.needsAck === true
|
|
32
|
+
this.trustedOnly = extra.trustedOnly === true
|
|
33
|
+
this.checkpoint = extra.checkpoint || null
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 递归收集目录里的普通文件;符号链接一律跳过(不跟随、不打包)。 */
|
|
38
|
+
export function collectFiles(root, options = {}) {
|
|
39
|
+
const maxFileBytes = options.maxFileBytes ?? 16 * 1024 * 1024
|
|
40
|
+
const maxTotalBytes = options.maxTotalBytes ?? 256 * 1024 * 1024
|
|
41
|
+
const prefix = options.prefix ?? ''
|
|
42
|
+
const out = []
|
|
43
|
+
const notes = []
|
|
44
|
+
let total = 0
|
|
45
|
+
|
|
46
|
+
const walk = (dir, rel) => {
|
|
47
|
+
let dirents
|
|
48
|
+
try {
|
|
49
|
+
dirents = fs.readdirSync(dir, { withFileTypes: true })
|
|
50
|
+
} catch (error) {
|
|
51
|
+
notes.push('读不到目录 ' + rel + ':' + String(error?.message || error))
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
for (const dirent of dirents) {
|
|
55
|
+
const abs = path.join(dir, dirent.name)
|
|
56
|
+
const relPath = rel ? rel + '/' + dirent.name : dirent.name
|
|
57
|
+
if (dirent.isSymbolicLink()) { notes.push('跳过符号链接 ' + prefix + relPath); continue }
|
|
58
|
+
if (dirent.isDirectory()) {
|
|
59
|
+
if (SKIP_DIRS.has(dirent.name)) continue
|
|
60
|
+
walk(abs, relPath)
|
|
61
|
+
continue
|
|
62
|
+
}
|
|
63
|
+
if (!dirent.isFile()) continue
|
|
64
|
+
if (SKIP_FILES.has(dirent.name)) continue
|
|
65
|
+
let stat
|
|
66
|
+
try { stat = fs.statSync(abs) } catch { continue }
|
|
67
|
+
if (stat.size > maxFileBytes) { notes.push('跳过超大文件 ' + prefix + relPath + '(' + stat.size + ' 字节)'); continue }
|
|
68
|
+
total += stat.size
|
|
69
|
+
if (total > maxTotalBytes) throw new Error('内容超过打包上限(' + maxTotalBytes + ' 字节),请减少插件')
|
|
70
|
+
let data
|
|
71
|
+
try { data = fs.readFileSync(abs) } catch (error) { notes.push('读不到 ' + prefix + relPath + ':' + String(error?.message || error)); continue }
|
|
72
|
+
out.push({ name: prefix + relPath, data, mtime: stat.mtime })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
walk(root, '')
|
|
76
|
+
return { files: out, notes }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 从 profile 读一个已装插件。 */
|
|
80
|
+
export function readInstalledPlugin(installed, options = {}) {
|
|
81
|
+
const { files, notes } = collectFiles(installed.dir, {
|
|
82
|
+
prefix: 'plugins/' + installed.dirName + '/',
|
|
83
|
+
maxFileBytes: options.maxFileBytes,
|
|
84
|
+
maxTotalBytes: options.maxTotalBytes,
|
|
85
|
+
})
|
|
86
|
+
return { files, notes, pkg: installed.pkg }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 抓 profile 补丁层里的配置(每个插件一条)。
|
|
91
|
+
* 解析失败就整体放弃自动抓取,只留一句 note——宁可少带,也不带错。
|
|
92
|
+
*/
|
|
93
|
+
export function captureConfigs(patchText, plugins, parsePatch) {
|
|
94
|
+
const captured = new Map()
|
|
95
|
+
const notes = []
|
|
96
|
+
let items = []
|
|
97
|
+
try {
|
|
98
|
+
items = parsePatch(patchText)
|
|
99
|
+
} catch (error) {
|
|
100
|
+
notes.push('配置层解析失败,未抓取任何配置:' + String(error?.message || error))
|
|
101
|
+
return { captured, notes }
|
|
102
|
+
}
|
|
103
|
+
if (!Array.isArray(items)) return { captured, notes }
|
|
104
|
+
const byName = new Map(plugins.map((p) => [p.name, p]))
|
|
105
|
+
for (const item of items) {
|
|
106
|
+
if (!item || typeof item !== 'object') continue
|
|
107
|
+
const name = typeof item.name === 'string' ? item.name : ''
|
|
108
|
+
const id = typeof item.id === 'string' ? item.id : ''
|
|
109
|
+
const hit = byName.get(name) || byName.get(id)
|
|
110
|
+
if (!hit) continue
|
|
111
|
+
captured.set(hit.name, { id: item.id, name: item.name, disabled: item.disabled === true ? true : undefined, config: item.config ?? null })
|
|
112
|
+
}
|
|
113
|
+
return { captured, notes }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function dataDirsFor(name) {
|
|
117
|
+
const home = process.env.DSH_HOME || path.join(process.env.USERPROFILE || '', '.dsh')
|
|
118
|
+
const candidates = [name, name.replace(/^dsh-/u, '')].filter(Boolean)
|
|
119
|
+
const out = []
|
|
120
|
+
for (const candidate of candidates) {
|
|
121
|
+
const dir = path.join(home, candidate)
|
|
122
|
+
try {
|
|
123
|
+
if (fs.statSync(dir).isDirectory()) out.push({ dir, prefix: 'data/' + name + '/' })
|
|
124
|
+
} catch {}
|
|
125
|
+
}
|
|
126
|
+
return out
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 插件数据快照:只收小体积文本类,凭证类文件名直接跳过。 */
|
|
130
|
+
export function captureData(name, options = {}) {
|
|
131
|
+
const files = []
|
|
132
|
+
const notes = []
|
|
133
|
+
let total = 0
|
|
134
|
+
const limit = options.maxBytes ?? 8 * 1024 * 1024
|
|
135
|
+
for (const { dir, prefix } of dataDirsFor(name)) {
|
|
136
|
+
const entries = []
|
|
137
|
+
const walk = (d, rel) => {
|
|
138
|
+
let dirents
|
|
139
|
+
try { dirents = fs.readdirSync(d, { withFileTypes: true }) } catch { return }
|
|
140
|
+
for (const dirent of dirents) {
|
|
141
|
+
if (dirent.isSymbolicLink()) continue
|
|
142
|
+
const abs = path.join(d, dirent.name)
|
|
143
|
+
const relPath = rel ? rel + '/' + dirent.name : dirent.name
|
|
144
|
+
if (dirent.isDirectory()) { if (!SKIP_DIRS.has(dirent.name)) walk(abs, relPath); continue }
|
|
145
|
+
if (!dirent.isFile()) continue
|
|
146
|
+
const ext = path.extname(dirent.name).toLowerCase()
|
|
147
|
+
if (!DATA_EXT.has(ext)) continue
|
|
148
|
+
if (scanFileName(prefix + relPath).some((f) => f.severity === 'high')) { notes.push('跳过疑似凭证文件 ' + prefix + relPath); continue }
|
|
149
|
+
let stat
|
|
150
|
+
try { stat = fs.statSync(abs) } catch { continue }
|
|
151
|
+
if (stat.size > 1024 * 1024) continue
|
|
152
|
+
total += stat.size
|
|
153
|
+
if (total > limit) { notes.push('数据快照超过上限,后面的没带'); return }
|
|
154
|
+
entries.push({ abs, name: prefix + relPath, size: stat.size })
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
walk(dir, '')
|
|
158
|
+
for (const entry of entries) {
|
|
159
|
+
try { files.push({ name: entry.name, data: fs.readFileSync(entry.abs), mtime: new Date() }) } catch {}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return { files, notes }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function licenseId(pkg) {
|
|
166
|
+
if (typeof pkg.license === 'string') return pkg.license
|
|
167
|
+
if (pkg.license && typeof pkg.license.type === 'string') return pkg.license.type
|
|
168
|
+
if (Array.isArray(pkg.licenses) && pkg.licenses[0]) return String(pkg.licenses[0].type || '')
|
|
169
|
+
return ''
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function authorOf(pkg) {
|
|
173
|
+
if (typeof pkg.author === 'string') return pkg.author
|
|
174
|
+
if (pkg.author && typeof pkg.author.name === 'string') return pkg.author.name
|
|
175
|
+
return ''
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 生成整合包。
|
|
180
|
+
* @param {object} input
|
|
181
|
+
* @param {{name: string, source: 'profile'|'npm'|'url', spec?: string, download?: Function}[]} input.selection
|
|
182
|
+
* @param {object} input.options {includePayload, includeConfig, includeLocalEdits, blockHighRisk, force, confirmLicense}
|
|
183
|
+
* @param {object} input.context {config, profileDir, dshVersion, installed, pluginVersion, extraScanRules, parsePatch}
|
|
184
|
+
*/
|
|
185
|
+
export async function buildPack(input) {
|
|
186
|
+
const { selection, options, context } = input
|
|
187
|
+
const config = context.config
|
|
188
|
+
const plugins = []
|
|
189
|
+
let payload = []
|
|
190
|
+
let notes = []
|
|
191
|
+
let manifestPlugins = []
|
|
192
|
+
let capturedFiles = []
|
|
193
|
+
let scan = null
|
|
194
|
+
let trustedOnly = false
|
|
195
|
+
const scanRules = context.extraScanRules || []
|
|
196
|
+
const format = options.includePayload === false ? 'lite' : 'full'
|
|
197
|
+
const progress = typeof options.onProgress === 'function' ? options.onProgress : () => {}
|
|
198
|
+
const tick = (stage, pct, note) => { try { progress({ stage, pct: Math.max(0, Math.min(100, Math.round(pct))), note }) } catch {} }
|
|
199
|
+
|
|
200
|
+
const resume = options && typeof options.resume === 'object' ? options.resume : null
|
|
201
|
+
if (resume) {
|
|
202
|
+
// 停在风险点之后点「我知道在干什么」:复用上次的结果,不重新收集、也不重新检测
|
|
203
|
+
payload = Array.isArray(resume.payload) ? resume.payload : []
|
|
204
|
+
capturedFiles = Array.isArray(resume.capturedFiles) ? resume.capturedFiles : []
|
|
205
|
+
manifestPlugins = Array.isArray(resume.manifestPlugins) ? resume.manifestPlugins : []
|
|
206
|
+
notes = Array.isArray(resume.notes) ? resume.notes.slice() : []
|
|
207
|
+
scan = resume.scan
|
|
208
|
+
trustedOnly = resume.trustedOnly === true
|
|
209
|
+
notes.push('已确认风险:检测结果沿用上一次,不重复扫描')
|
|
210
|
+
tick('capture', 62, '已确认风险,从检测点继续')
|
|
211
|
+
} else {
|
|
212
|
+
tick('collect', 4, '读取插件清单')
|
|
213
|
+
for (const pick of selection) {
|
|
214
|
+
if (!pick || typeof pick.name !== 'string') continue
|
|
215
|
+
let files = []
|
|
216
|
+
let pkg = null
|
|
217
|
+
let source = pick.source
|
|
218
|
+
let registry = ''
|
|
219
|
+
let tarball = ''
|
|
220
|
+
let integrity = ''
|
|
221
|
+
let requested = pick.spec || ''
|
|
222
|
+
let localEdits = false
|
|
223
|
+
let payloadDir = ''
|
|
224
|
+
// 每个插件可以单独选:base = 上游原版 + 插件基础配置;local = 连本机改动一起打
|
|
225
|
+
const wantLocal = pick.mode === 'local' ? true : pick.mode === 'base' ? false : options.includeLocalEdits === true
|
|
226
|
+
|
|
227
|
+
if (source === 'profile') {
|
|
228
|
+
const installed = context.installed.find((p) => p.name === pick.name)
|
|
229
|
+
if (!installed) throw new Error('本地找不到插件:' + pick.name)
|
|
230
|
+
const loaded = readInstalledPlugin(installed, { maxFileBytes: config?.maxFileBytes, maxTotalBytes: config?.maxPackBytes })
|
|
231
|
+
files = loaded.files
|
|
232
|
+
pkg = loaded.pkg
|
|
233
|
+
notes.push(...loaded.notes)
|
|
234
|
+
payloadDir = installed.dirName || installed.name
|
|
235
|
+
localEdits = true
|
|
236
|
+
if (!wantLocal && typeof pick.download === 'function') {
|
|
237
|
+
// 想打“上游原版”就从 registry 重下,失败则保留本地并标注
|
|
238
|
+
try {
|
|
239
|
+
const downloaded = await pick.download(installed.version)
|
|
240
|
+
files = downloaded.entries
|
|
241
|
+
.filter((e) => !SKIP_DIRS.has(e.name.split('/')[0]))
|
|
242
|
+
.map((e) => ({ name: 'plugins/' + installed.dirName + '/' + e.name, data: e.data }))
|
|
243
|
+
pkg = downloaded.pkg
|
|
244
|
+
registry = downloaded.registry
|
|
245
|
+
tarball = downloaded.tarball
|
|
246
|
+
integrity = downloaded.integrity
|
|
247
|
+
localEdits = false
|
|
248
|
+
} catch (error) {
|
|
249
|
+
notes.push(pick.name + ':取上游原版失败,改打本地文件(' + String(error?.message || error) + ')')
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
} else if (source === 'npm') {
|
|
253
|
+
if (typeof pick.download !== 'function') throw new Error('缺少下载器')
|
|
254
|
+
const downloaded = await pick.download(requested)
|
|
255
|
+
pkg = downloaded.pkg
|
|
256
|
+
registry = downloaded.registry
|
|
257
|
+
tarball = downloaded.tarball
|
|
258
|
+
integrity = downloaded.integrity
|
|
259
|
+
const dirName = pkg.name
|
|
260
|
+
payloadDir = dirName
|
|
261
|
+
files = downloaded.entries.map((e) => ({ name: 'plugins/' + dirName + '/' + e.name, data: e.data }))
|
|
262
|
+
localEdits = false
|
|
263
|
+
} else {
|
|
264
|
+
throw new Error('不支持的来源:' + source)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const name = (pkg && pkg.name) || pick.name
|
|
268
|
+
const dirName = payloadDir || name
|
|
269
|
+
if (!pkg || typeof pkg !== 'object') throw new Error('插件缺少 package.json:' + name)
|
|
270
|
+
const license = licenseId(pkg)
|
|
271
|
+
const allow = new Set((config?.licenseAllow || []).map((x) => String(x).toLowerCase()))
|
|
272
|
+
let licenseState = 'ok'
|
|
273
|
+
if (!license || /^unlicensed$/iu.test(license)) licenseState = 'missing'
|
|
274
|
+
else if (!allow.has(license.toLowerCase())) licenseState = 'unlisted'
|
|
275
|
+
if (licenseState !== 'ok' && !options.confirmLicense) {
|
|
276
|
+
throw new Error(pick.name + ':license 为「' + (license || '未标注') + '」,不在允许列表。确认你有权再分发后再打包(需要显式勾选)。')
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const entry = {
|
|
280
|
+
name,
|
|
281
|
+
version: typeof pkg.version === 'string' ? pkg.version : '0.0.0',
|
|
282
|
+
requested,
|
|
283
|
+
source,
|
|
284
|
+
registry,
|
|
285
|
+
tarball,
|
|
286
|
+
integrity,
|
|
287
|
+
license,
|
|
288
|
+
repository: typeof pkg.repository === 'string' ? pkg.repository : (pkg.repository && pkg.repository.url) || '',
|
|
289
|
+
author: authorOf(pkg),
|
|
290
|
+
files: files.length,
|
|
291
|
+
size: files.reduce((sum, f) => sum + f.data.length, 0),
|
|
292
|
+
hasClient: Boolean(pkg.dsh && pkg.dsh.client) || Boolean(pkg.exports && (pkg.exports['./client'] || pkg.exports['./client.js'])),
|
|
293
|
+
hasPatch: Boolean(pkg.dsh && pkg.dsh.bundle && pkg.dsh.bundle.patch),
|
|
294
|
+
isBundle: Boolean(pkg.dsh && (pkg.dsh.bundle || pkg.dsh.client)),
|
|
295
|
+
localEdits,
|
|
296
|
+
mode: localEdits ? 'local' : 'base',
|
|
297
|
+
configIncluded: false,
|
|
298
|
+
dataIncluded: false,
|
|
299
|
+
peerDependencies: pkg.peerDependencies && typeof pkg.peerDependencies === 'object' ? pkg.peerDependencies : {},
|
|
300
|
+
engines: pkg.engines && typeof pkg.engines === 'object' ? pkg.engines : {},
|
|
301
|
+
dirName,
|
|
302
|
+
}
|
|
303
|
+
manifestPlugins.push(entry)
|
|
304
|
+
payload.push(...files.map((f) => ({ ...f, plugin: name })))
|
|
305
|
+
tick('collect', 6 + 34 * (manifestPlugins.length / Math.max(1, selection.length)), '收集 ' + name + ' 的文件')
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// 配置与数据快照
|
|
309
|
+
capturedFiles = []
|
|
310
|
+
if (options.includeConfig) {
|
|
311
|
+
const { captured, notes: configNotes } = captureConfigs(context.patchText || '', manifestPlugins, context.parsePatch)
|
|
312
|
+
notes.push(...configNotes)
|
|
313
|
+
for (const plugin of manifestPlugins) {
|
|
314
|
+
// 选了「基础配置」就没有本地改动可打
|
|
315
|
+
if (plugin.mode === 'base') continue
|
|
316
|
+
const snap = captured.get(plugin.name)
|
|
317
|
+
if (snap && (snap.config !== null || snap.disabled !== undefined)) {
|
|
318
|
+
const body = JSON.stringify({ id: snap.id, name: snap.name, disabled: snap.disabled, config: snap.config, capturedAt: new Date().toISOString() }, null, 2)
|
|
319
|
+
capturedFiles.push({ name: 'configs/' + plugin.name + '.json', data: Buffer.from(body, 'utf8') })
|
|
320
|
+
plugin.configIncluded = true
|
|
321
|
+
plugin.config = snap.config
|
|
322
|
+
if (snap.disabled) plugin.note = '打包时该插件被禁用'
|
|
323
|
+
}
|
|
324
|
+
const data = captureData(plugin.name, { maxBytes: 8 * 1024 * 1024 })
|
|
325
|
+
if (data.files.length > 0) {
|
|
326
|
+
capturedFiles.push(...data.files)
|
|
327
|
+
plugin.dataIncluded = true
|
|
328
|
+
notes.push(...data.notes)
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// 先扫描,再决定要不要写盘。扫描是强制的:没有「关掉扫描」这条路,
|
|
334
|
+
// 只有高风险默认阻断、显式 force 才继续(决定会写进清单 security.approvedRisk)。
|
|
335
|
+
// 信任名单:插件市场目录里能查到的插件(options.trusted)视为市场发行版。
|
|
336
|
+
// 这类插件的高危项不直接阻断,但必须由调用方 acknowledge 之后才出包。
|
|
337
|
+
const trustedSet = new Set((Array.isArray(options.trusted) ? options.trusted : []).map((n) => String(n)))
|
|
338
|
+
const dirToName = new Map()
|
|
339
|
+
for (const plugin of manifestPlugins) if (plugin.dirName) dirToName.set(plugin.dirName, plugin.name)
|
|
340
|
+
const dirOf = (file) => {
|
|
341
|
+
const hit = /^plugins\/([^/]+)\//.exec(String(file || ''))
|
|
342
|
+
return hit ? hit[1] : ''
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
tick('capture', 42, '抓取配置与数据快照')
|
|
346
|
+
const scanTargets = [...payload, ...capturedFiles]
|
|
347
|
+
tick('scan', 55, '扫描敏感信息与风险代码')
|
|
348
|
+
scan = scanEntries(scanTargets.map((f) => ({ name: f.name, data: f.data })), { extraRules: scanRules })
|
|
349
|
+
|
|
350
|
+
const highFindings = scan.findings.filter((f) => f.severity === 'high')
|
|
351
|
+
const highFromUntrusted = highFindings.filter((f) => {
|
|
352
|
+
const name = dirToName.get(dirOf(f.file))
|
|
353
|
+
return !name || !trustedSet.has(name)
|
|
354
|
+
})
|
|
355
|
+
trustedOnly = highFindings.length > 0 && highFromUntrusted.length === 0
|
|
356
|
+
// 高风险一律停在「等你点一下」:acknowledge 之后才出包,决定记进清单。
|
|
357
|
+
// 市场目录能查到的(trustedOnly)与查不到的(含本机自己改的)走同一条路,
|
|
358
|
+
// 只是提示措辞不同——这样界面上永远有「我知道在干什么」这个出口。
|
|
359
|
+
if (config?.blockHighRisk !== false && highFindings.length > 0 && options.acknowledge !== true && options.force !== true) {
|
|
360
|
+
const where = trustedOnly
|
|
361
|
+
? '均来自插件市场已收录的插件'
|
|
362
|
+
: (highFromUntrusted.length === highFindings.length ? '来自本机插件,市场目录里查不到同名条目' : '有一部分来自本机插件(市场目录里查不到同名条目)')
|
|
363
|
+
throw new PackRefused(
|
|
364
|
+
'检出 ' + highFindings.length + ' 个高风险项(' + where + ');确认了解风险后可继续打包',
|
|
365
|
+
scan,
|
|
366
|
+
{
|
|
367
|
+
needsAck: true,
|
|
368
|
+
trustedOnly,
|
|
369
|
+
checkpoint: {
|
|
370
|
+
payload,
|
|
371
|
+
capturedFiles,
|
|
372
|
+
manifestPlugins: JSON.parse(JSON.stringify(manifestPlugins)),
|
|
373
|
+
notes: notes.slice(),
|
|
374
|
+
scan,
|
|
375
|
+
trustedOnly,
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
)
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
tick('scan', 66, '完成安全检测')
|
|
383
|
+
const scanGroups = groupFindings(scan.findings, manifestPlugins)
|
|
384
|
+
const manifestBase = {
|
|
385
|
+
schema: SCHEMA,
|
|
386
|
+
kind: KIND,
|
|
387
|
+
format,
|
|
388
|
+
id: 'mp_' + Date.now().toString(36) + '_' + shortHash(Buffer.from(String(Math.random()))),
|
|
389
|
+
name: options.name || 'DSH 整合包',
|
|
390
|
+
description: options.description || '',
|
|
391
|
+
author: options.author || '',
|
|
392
|
+
license: options.license || '',
|
|
393
|
+
homepage: options.homepage || '',
|
|
394
|
+
createdAt: new Date().toISOString(),
|
|
395
|
+
generator: { name: 'dsh-modpack', version: context.pluginVersion || '0.0.0' },
|
|
396
|
+
dshVersion: context.dshVersion || '',
|
|
397
|
+
options: {
|
|
398
|
+
includePayload: format === 'full',
|
|
399
|
+
includeConfig: Boolean(options.includeConfig),
|
|
400
|
+
includeLocalEdits: manifestPlugins.some((plugin) => plugin.mode === 'local'),
|
|
401
|
+
keepLocalOnInstall: options.keepLocal !== false,
|
|
402
|
+
},
|
|
403
|
+
plugins: manifestPlugins,
|
|
404
|
+
files: [...payload, ...capturedFiles].map((f) => ({ path: f.name, sha256: sha256(f.data), size: f.data.length })),
|
|
405
|
+
security: {
|
|
406
|
+
scanned: true,
|
|
407
|
+
high: scan.counts.high,
|
|
408
|
+
medium: scan.counts.medium,
|
|
409
|
+
low: scan.counts.low,
|
|
410
|
+
approvedRisk: scan.findings.some((f) => f.severity === 'high'),
|
|
411
|
+
acknowledged: options.acknowledge === true,
|
|
412
|
+
trustedOnly,
|
|
413
|
+
note: scan.note,
|
|
414
|
+
},
|
|
415
|
+
install: {
|
|
416
|
+
restartRequired: true,
|
|
417
|
+
notes: notes.slice(0, 30),
|
|
418
|
+
},
|
|
419
|
+
extensions: options.extensions || {},
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const manifest = normalizeManifest(manifestBase)
|
|
423
|
+
const manifestText = JSON.stringify(manifest, null, 2)
|
|
424
|
+
const manifestBuf = Buffer.from(manifestText, 'utf8')
|
|
425
|
+
|
|
426
|
+
const report = renderScanReport(manifest.name, scan, { approvedRisk: manifest.security.approvedRisk })
|
|
427
|
+
const readme = buildReadme(manifest)
|
|
428
|
+
|
|
429
|
+
tick('write', 78, '写入清单与安装器')
|
|
430
|
+
const installScripts = [
|
|
431
|
+
{ name: 'install/install.mjs', data: stub('install-stub.mjs') },
|
|
432
|
+
{ name: 'install/install.cmd', data: stub('install-stub.cmd') },
|
|
433
|
+
{ name: 'install/install.sh', data: stub('install-stub.sh') },
|
|
434
|
+
]
|
|
435
|
+
const entries = [
|
|
436
|
+
{ name: MANIFEST_NAME, data: manifestBuf },
|
|
437
|
+
{ name: 'README.txt', data: Buffer.from(readme, 'utf8') },
|
|
438
|
+
{ name: 'security/report.txt', data: Buffer.from(report, 'utf8') },
|
|
439
|
+
...installScripts,
|
|
440
|
+
...(options.includePayload === false ? [] : payload),
|
|
441
|
+
...capturedFiles,
|
|
442
|
+
]
|
|
443
|
+
|
|
444
|
+
const integrity = {
|
|
445
|
+
generatedAt: new Date().toISOString(),
|
|
446
|
+
algorithm: 'sha256',
|
|
447
|
+
files: Object.fromEntries(entries.map((e) => [e.name, sha256(e.data)])),
|
|
448
|
+
}
|
|
449
|
+
entries.push({ name: 'integrity.json', data: Buffer.from(JSON.stringify(integrity, null, 2), 'utf8') })
|
|
450
|
+
|
|
451
|
+
tick('write', 90, '压缩打包')
|
|
452
|
+
const buffer = writeZip(entries)
|
|
453
|
+
if (config?.maxPackBytes && buffer.length > config.maxPackBytes) {
|
|
454
|
+
throw new Error('整合包体积 ' + buffer.length + ' 字节,超过上限 ' + config.maxPackBytes)
|
|
455
|
+
}
|
|
456
|
+
const stamp = new Date().toISOString().slice(0, 10)
|
|
457
|
+
const fileName = slugify(manifest.name) + '-' + stamp + '-' + format + '.dshpack'
|
|
458
|
+
tick('done', 100, '完成')
|
|
459
|
+
return { buffer, fileName, manifest, scan, scanGroups, report, notes, size: buffer.length }
|
|
460
|
+
}
|