dsh-modpack-kit 0.1.1 → 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/lib/pack.mjs CHANGED
@@ -11,20 +11,26 @@
11
11
  import fs from 'node:fs'
12
12
  import path from 'node:path'
13
13
  import { writeZip } from './zip.mjs'
14
- import { scanEntries, renderScanReport } from './scan.mjs'
14
+ import { scanEntries, renderScanReport, groupFindings } from './scan.mjs'
15
15
  import { MANIFEST_NAME, SCHEMA, KIND, normalizeManifest, buildReadme, sha256, shortHash } from './manifest.mjs'
16
16
  import { scanFileName } from './scan.mjs'
17
17
  import { slugify } from './store.mjs'
18
18
 
19
+ const STUB_DIR = new URL('../assets/', import.meta.url)
20
+ const stub = (name) => fs.readFileSync(new URL(name, STUB_DIR))
21
+
19
22
  const SKIP_DIRS = new Set(['node_modules', '.git', '.svn', '__pycache__', '.cache', '.turbo', '.next', 'coverage'])
20
23
  const SKIP_FILES = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini'])
21
24
  const DATA_EXT = new Set(['.json', '.md', '.txt', '.yml', '.yaml', '.csv', '.toml', '.ini'])
22
25
 
23
26
  export class PackRefused extends Error {
24
- constructor(message, scan) {
27
+ constructor(message, scan, extra = {}) {
25
28
  super(message)
26
29
  this.name = 'PackRefused'
27
30
  this.scan = scan
31
+ this.needsAck = extra.needsAck === true
32
+ this.trustedOnly = extra.trustedOnly === true
33
+ this.checkpoint = extra.checkpoint || null
28
34
  }
29
35
  }
30
36
 
@@ -180,12 +186,30 @@ export async function buildPack(input) {
180
186
  const { selection, options, context } = input
181
187
  const config = context.config
182
188
  const plugins = []
183
- const payload = []
184
- const notes = []
185
- const manifestPlugins = []
189
+ let payload = []
190
+ let notes = []
191
+ let manifestPlugins = []
192
+ let capturedFiles = []
193
+ let scan = null
194
+ let trustedOnly = false
186
195
  const scanRules = context.extraScanRules || []
187
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 {} }
188
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, '读取插件清单')
189
213
  for (const pick of selection) {
190
214
  if (!pick || typeof pick.name !== 'string') continue
191
215
  let files = []
@@ -278,10 +302,11 @@ export async function buildPack(input) {
278
302
  }
279
303
  manifestPlugins.push(entry)
280
304
  payload.push(...files.map((f) => ({ ...f, plugin: name })))
305
+ tick('collect', 6 + 34 * (manifestPlugins.length / Math.max(1, selection.length)), '收集 ' + name + ' 的文件')
281
306
  }
282
307
 
283
308
  // 配置与数据快照
284
- const capturedFiles = []
309
+ capturedFiles = []
285
310
  if (options.includeConfig) {
286
311
  const { captured, notes: configNotes } = captureConfigs(context.patchText || '', manifestPlugins, context.parsePatch)
287
312
  notes.push(...configNotes)
@@ -307,13 +332,55 @@ export async function buildPack(input) {
307
332
 
308
333
  // 先扫描,再决定要不要写盘。扫描是强制的:没有「关掉扫描」这条路,
309
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, '抓取配置与数据快照')
310
346
  const scanTargets = [...payload, ...capturedFiles]
311
- const scan = scanEntries(scanTargets.map((f) => ({ name: f.name, data: f.data })), { extraRules: scanRules })
347
+ tick('scan', 55, '扫描敏感信息与风险代码')
348
+ scan = scanEntries(scanTargets.map((f) => ({ name: f.name, data: f.data })), { extraRules: scanRules })
312
349
 
313
- if (config?.blockHighRisk !== false && scan.counts.high > 0 && !options.force) {
314
- throw new PackRefused('安全检测发现 ' + scan.counts.high + ' 个高风险项,已停止打包', scan)
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
+ }
315
380
  }
316
381
 
382
+ tick('scan', 66, '完成安全检测')
383
+ const scanGroups = groupFindings(scan.findings, manifestPlugins)
317
384
  const manifestBase = {
318
385
  schema: SCHEMA,
319
386
  kind: KIND,
@@ -330,7 +397,7 @@ export async function buildPack(input) {
330
397
  options: {
331
398
  includePayload: format === 'full',
332
399
  includeConfig: Boolean(options.includeConfig),
333
- includeLocalEdits: Boolean(options.includeLocalEdits),
400
+ includeLocalEdits: manifestPlugins.some((plugin) => plugin.mode === 'local'),
334
401
  keepLocalOnInstall: options.keepLocal !== false,
335
402
  },
336
403
  plugins: manifestPlugins,
@@ -340,7 +407,9 @@ export async function buildPack(input) {
340
407
  high: scan.counts.high,
341
408
  medium: scan.counts.medium,
342
409
  low: scan.counts.low,
343
- approvedRisk: scan.counts.high > 0,
410
+ approvedRisk: scan.findings.some((f) => f.severity === 'high'),
411
+ acknowledged: options.acknowledge === true,
412
+ trustedOnly,
344
413
  note: scan.note,
345
414
  },
346
415
  install: {
@@ -357,10 +426,17 @@ export async function buildPack(input) {
357
426
  const report = renderScanReport(manifest.name, scan, { approvedRisk: manifest.security.approvedRisk })
358
427
  const readme = buildReadme(manifest)
359
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
+ ]
360
435
  const entries = [
361
436
  { name: MANIFEST_NAME, data: manifestBuf },
362
437
  { name: 'README.txt', data: Buffer.from(readme, 'utf8') },
363
438
  { name: 'security/report.txt', data: Buffer.from(report, 'utf8') },
439
+ ...installScripts,
364
440
  ...(options.includePayload === false ? [] : payload),
365
441
  ...capturedFiles,
366
442
  ]
@@ -372,11 +448,13 @@ export async function buildPack(input) {
372
448
  }
373
449
  entries.push({ name: 'integrity.json', data: Buffer.from(JSON.stringify(integrity, null, 2), 'utf8') })
374
450
 
451
+ tick('write', 90, '压缩打包')
375
452
  const buffer = writeZip(entries)
376
453
  if (config?.maxPackBytes && buffer.length > config.maxPackBytes) {
377
454
  throw new Error('整合包体积 ' + buffer.length + ' 字节,超过上限 ' + config.maxPackBytes)
378
455
  }
379
456
  const stamp = new Date().toISOString().slice(0, 10)
380
457
  const fileName = slugify(manifest.name) + '-' + stamp + '-' + format + '.dshpack'
381
- return { buffer, fileName, manifest, scan, report, notes, size: buffer.length }
458
+ tick('done', 100, '完成')
459
+ return { buffer, fileName, manifest, scan, scanGroups, report, notes, size: buffer.length }
382
460
  }
package/lib/scan.mjs CHANGED
@@ -184,6 +184,53 @@ export function scanFile(name, data) {
184
184
 
185
185
  const SEVERITY_ORDER = { high: 0, medium: 1, low: 2 }
186
186
 
187
+ /**
188
+ * 把命中按来源分组:插件本体 / 该插件的配置快照 / 数据快照 / 整合包自身文件。
189
+ * @param {object[]} findings
190
+ * @param {{name: string, dirName?: string, source?: string}[]} plugins
191
+ */
192
+ export function groupFindings(findings, plugins = []) {
193
+ const byDir = new Map()
194
+ for (const p of plugins) {
195
+ if (!p || !p.name) continue
196
+ byDir.set(p.dirName || p.name, p)
197
+ }
198
+ const groups = new Map()
199
+ const originOf = (p) => (p.source === 'npm' ? 'market' : 'local')
200
+ const slot = (key, label, origin, pluginName) => {
201
+ if (!groups.has(key)) groups.set(key, { key, label, origin, plugin: pluginName || '', counts: { high: 0, medium: 0, low: 0 }, findings: [] })
202
+ return groups.get(key)
203
+ }
204
+ for (const finding of findings) {
205
+ const file = String(finding.file || '')
206
+ let hit
207
+ if ((hit = /^plugins\/([^/]+)\//u.exec(file))) {
208
+ const plugin = byDir.get(hit[1])
209
+ const group = slot('plugin:' + hit[1], (plugin && plugin.name) || hit[1], plugin ? originOf(plugin) : 'local', (plugin && plugin.name) || hit[1])
210
+ group.findings.push(finding)
211
+ group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
212
+ } else if ((hit = /^configs\/([^/]+)\.json$/u.exec(file))) {
213
+ const plugin = byDir.get(hit[1])
214
+ const group = slot('config:' + hit[1], ((plugin && plugin.name) || hit[1]) + ' 的配置快照', 'config', (plugin && plugin.name) || hit[1])
215
+ group.findings.push(finding)
216
+ group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
217
+ } else if ((hit = /^data\/([^/]+)\//u.exec(file))) {
218
+ const plugin = byDir.get(hit[1])
219
+ const group = slot('data:' + hit[1], ((plugin && plugin.name) || hit[1]) + ' 的数据快照', 'data', (plugin && plugin.name) || hit[1])
220
+ group.findings.push(finding)
221
+ group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
222
+ } else {
223
+ const group = slot('pack', '整合包自身的清单与安装器', 'pack', '')
224
+ group.findings.push(finding)
225
+ group.counts[finding.severity] = (group.counts[finding.severity] || 0) + 1
226
+ }
227
+ }
228
+ const weight = (g) => (g.counts.high > 0 ? 0 : g.counts.medium > 0 ? 1 : 2)
229
+ return [...groups.values()]
230
+ .map((g) => ({ ...g, findings: g.findings.sort((a, b) => severityRank(a.severity) - severityRank(b.severity) || a.line - b.line) }))
231
+ .sort((a, b) => weight(a) - weight(b) || b.findings.length - a.findings.length || a.label.localeCompare(b.label))
232
+ }
233
+
187
234
  export function severityRank(severity) {
188
235
  return SEVERITY_ORDER[severity] ?? 3
189
236
  }
package/lib/store.mjs CHANGED
@@ -53,8 +53,8 @@ export const DEFAULT_CONFIG = {
53
53
  'cdn.jsdelivr.net', 'fastly.jsdelivr.net', 'gh-proxy.com',
54
54
  'github.com', 'api.github.com', 'codeload.github.com', 'objects.githubusercontent.com', 'raw.githubusercontent.com',
55
55
  ],
56
- maxDownloadBytes: 128 * 1024 * 1024,
57
- maxPackBytes: 256 * 1024 * 1024,
56
+ maxDownloadBytes: 256 * 1024 * 1024,
57
+ maxPackBytes: 512 * 1024 * 1024,
58
58
  maxFileBytes: 16 * 1024 * 1024,
59
59
  // 二次开发用:额外扫描规则(形状同 lib/scan.mjs 的 SECRET_RULES)
60
60
  extraScanRules: [],
@@ -105,6 +105,7 @@ function clampConfig(raw) {
105
105
  // 分页大小与刷新周期:给了边界,避免有人把 10000 写进设置把页面拖死
106
106
  cfg.catalogPageSize = Math.min(100, Math.max(5, Number(cfg.catalogPageSize) || DEFAULT_CONFIG.catalogPageSize))
107
107
  cfg.catalogRefreshHours = Math.min(168, Math.max(1, Number(cfg.catalogRefreshHours) || DEFAULT_CONFIG.catalogRefreshHours))
108
+ // 进度窗位置不在这里管:它由界面自己拖动并存在浏览器本地,服务端不需要知道
108
109
  // 打包前安全检测是强制的:设置里怎么写都不认,打包一律扫。
109
110
  cfg.scanBeforePack = true
110
111
  return cfg
package/locale/en.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Modpack",
4
- "description": "Bundle several plugins into one .dshpack and install them in a single pass: full packs carry the payloads, lite packs record names and versions and fetch them from the registry. Each plugin can be packed as upstream-basic or as your local copy. Packing always runs an in-memory scan for secrets and risky code (masked, never written to disk); installing re-checks integrity, compatibility and licences, then re-applies the packed config, with an option to install without enabling. Categories, search, sorting and entries come from the same plugin-market catalog (first screen lists only what you have), and the UI can be shown in settings, the sidebar or the plugins tab, each switchable. Installing the plugin market (dshmarket) first is recommended but not required: packing, installing and scanning all work without it, you just lose its UI and the market-version comparison.",
4
+ "description": "Bundle several plugins into one .dshpack and install them in a single pass: full packs carry the payloads, lite packs record names and versions and fetch them from the registry, and both ship a standalone installer so a machine with nothing but a bare DSH can still install. Each plugin can be packed as upstream-basic or as your local copy. Packing always runs an in-memory scan for secrets and risky code (masked, never written to disk); high-risk hits pause the job until you confirm, and the confirmation is recorded in the manifest. Installing re-checks integrity, compatibility and licences, then re-applies the packed config, with options to install without enabling or to enable right away. Categories, search, sorting and entries come from the same plugin-market catalog (first screen lists only what you have, paged on the server), and the UI can be shown in settings, the sidebar or the plugins tab, each switchable. Installing the plugin market (dshmarket) first is recommended but not required.",
5
5
  "detail": "docs/DETAIL.md"
6
6
  }
7
7
  }
package/locale/zh.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "整合包",
4
- "description": "把多个插件打成一个 .dshpack,一次装好:完整版自带插件本体,轻量版只记名字与版本、安装时按记录取包。每个插件可单独选「基础配置」或「本地改动」。打包前强制做敏感信息与风险代码检测(命中只留掩码、原文不落盘),安装前再查完整性、兼容性与许可证;装完按条目回填配置,也可选先不启用。分类、搜索、排序与条目都跟插件市场同源(首屏只列已装),三处入口(设置 / 侧边栏 / 插件页)可各自开关。建议先装插件市场(dshmarket)再一起用——不装也能打包、装包与检测,只是少了它的界面与市场版对照。",
4
+ "description": "把多个插件打成一个 .dshpack,一次装好:完整版自带插件本体,轻量版只记名字与版本、安装时按记录取包;两种包都附独立安装器,对方只剩一个 DSH 本体也能装。每个插件可单独选「基础配置」或「本地改动」。打包前强制做敏感信息与风险代码检测(命中只留掩码、原文不落盘),检出高危会先停下来,确认后才继续,确认状态写进清单;安装前再查完整性、兼容性与许可证,装完按条目回填配置,也可选先不启用或装完直接启用。分类、搜索、排序与条目都跟插件市场同源(首屏只列已装、服务端分页),三处入口(设置 / 侧边栏 / 插件页)可各自开关。建议先装插件市场(dshmarket)再一起用——不装也能打包、装包与检测,只是少了它的界面与市场版对照。",
5
5
  "detail": "docs/DETAIL.md"
6
6
  }
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-modpack-kit",
3
- "version": "0.1.1",
4
- "description": "Bundle several plugins into one .dshpack and install them in a single pass: full packs carry the payloads, lite packs record names and versions and fetch them from the registry. Each plugin can be packed as upstream-basic or as your local copy. Packing always runs an in-memory scan for secrets and risky code (masked, never written to disk); installing re-checks integrity, compatibility and licences, then re-applies the packed config, with an option to install without enabling. Categories, search, sorting and entries come from the same plugin-market catalog (first screen lists only what you have), and the UI can be shown in settings, the sidebar or the plugins tab, each switchable.",
3
+ "version": "0.1.3",
4
+ "description": "Bundle several plugins into one .dshpack and install them in a single pass: full packs carry the payloads, lite packs record names and versions and fetch them from the registry, and both ship a standalone installer so a machine with nothing but a bare DSH can still install. Each plugin can be packed as upstream-basic or as your local copy. Packing always runs an in-memory scan for secrets and risky code (masked, never written to disk); high-risk hits pause the job until you confirm, and the confirmation is recorded in the manifest. Installing re-checks integrity, compatibility and licences, then re-applies the packed config, with options to install without enabling or to enable right away. Categories, search, sorting and entries come from the same plugin-market catalog (first screen lists only what you have, paged on the server), and the UI can be shown in settings, the sidebar or the plugins tab, each switchable. Installing the plugin market (dshmarket) first is recommended but not required.",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "exports": {
@@ -20,7 +20,8 @@
20
20
  "docs",
21
21
  "icon.svg",
22
22
  "README.md",
23
- "LICENSE"
23
+ "LICENSE",
24
+ "assets"
24
25
  ],
25
26
  "keywords": [
26
27
  "dsh-plugin",