@dsh-plugin/dsh-pack-plugin 0.0.0-stage → 0.0.1
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 +21 -0
- package/README.md +178 -3
- package/cordis.patch.yml +9 -0
- package/icons/folder-zip-line.svg +1 -0
- package/lib/ball.js +597 -0
- package/lib/client.js +2934 -0
- package/lib/host.js +51745 -0
- package/lib/migrate-helper.js +35346 -0
- package/lib/progress-main.cjs +79 -0
- package/lib/progress-window.ps1 +114 -0
- package/lib/progress.html +126 -0
- package/lib/publish-to-github/SKILL.md +132 -0
- package/package.json +77 -4
- package/src/ball-entry.js +5 -0
- package/src/ball-host.js +99 -0
- package/src/ball-settings.js +352 -0
- package/src/ball.js +418 -0
- package/src/channel.js +12 -0
- package/src/client-plugin.js +58 -0
- package/src/client-rpc.js +14 -0
- package/src/client.js +5 -0
- package/src/core/discovery.js +192 -0
- package/src/core/dspack.js +93 -0
- package/src/core/extension-files.js +102 -0
- package/src/core/extension-install.js +295 -0
- package/src/core/extension-patch.js +369 -0
- package/src/core/extension-skills.js +164 -0
- package/src/core/extension-validate.js +127 -0
- package/src/core/extensions.js +207 -0
- package/src/core/home-store.js +32 -0
- package/src/core/host.js +149 -0
- package/src/core/index.js +59 -0
- package/src/core/inspect.js +162 -0
- package/src/core/install.js +775 -0
- package/src/core/managed-install.js +84 -0
- package/src/core/manifest.js +717 -0
- package/src/core/market.js +268 -0
- package/src/core/mcp-composition.js +189 -0
- package/src/core/mcp-install.js +112 -0
- package/src/core/mcp-transport.js +116 -0
- package/src/core/pack.js +371 -0
- package/src/core/plugin-metadata.js +355 -0
- package/src/core/registry.js +316 -0
- package/src/core/repo.js +339 -0
- package/src/core/scan.js +78 -0
- package/src/core/security.js +101 -0
- package/src/core/special.js +50 -0
- package/src/core/tar.js +103 -0
- package/src/core/update-config.js +234 -0
- package/src/core/update.js +792 -0
- package/src/core/vendored.js +715 -0
- package/src/core/workspace.js +95 -0
- package/src/endpoints.js +455 -0
- package/src/ensure-manager.js +186 -0
- package/src/host-node.js +625 -0
- package/src/host.js +17 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +39 -0
- package/src/junction.js +142 -0
- package/src/migrate-helper.js +225 -0
- package/src/migrate.js +56 -0
- package/src/packaged.js +9 -0
- package/src/profiles.js +106 -0
- package/src/progress-main.cjs +79 -0
- package/src/progress-window.ps1 +114 -0
- package/src/progress.html +126 -0
- package/src/progress.js +132 -0
- package/src/rpc.js +157 -0
- package/src/runtime.js +43 -0
- package/src/section-gate.js +50 -0
- package/src/settings-nav-icon.js +188 -0
- package/src/settings.js +1611 -0
- package/src/skills/index.js +90 -0
- package/src/skills/publish-to-github/SKILL.md +132 -0
- package/src/tasks.js +145 -0
- package/src/v6-dialogs.js +312 -0
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
import { validatePluginMetadata } from './plugin-metadata.js';
|
|
2
|
+
import { installedPluginSource } from './registry.js';
|
|
3
|
+
// 新导出使用 manifest v6,已有 v4/v5 清单按各自版本解析。
|
|
4
|
+
// v5 = v4 全部硬约束(依赖坐标钉死精确版本/commit sha、dshVersion 精确、多语言元数据、files[])
|
|
5
|
+
// + type 区分形态:"profile"(单包)/ "dshhome"(整机,含 profiles/presets/skills/instructions)。
|
|
6
|
+
//
|
|
7
|
+
// 注意:与 vendored.js 互相 import(本模块需要 lockfilePackageNames 做闭包条目对账放行)。
|
|
8
|
+
// 循环依赖是安全的:双方都只在**运行时函数调用**里使用对方绑定,顶层求值互不依赖。
|
|
9
|
+
import { lockfilePackageNames } from './vendored.js';
|
|
10
|
+
|
|
11
|
+
const ICON_PATTERN = /^icons?\/.+\.(png|jpe?g|webp|ico|svg)$/i;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 从 Profile 生成 manifest v6(type: "profile")。
|
|
15
|
+
* @param {Host} host 用于读取 package.json 与 cordis.patch.yml
|
|
16
|
+
* @param {{name: string, dir: string}} profile
|
|
17
|
+
* @param {object} opts { name, displayName, version, description, author, icon, dshVersion, profileName, files }
|
|
18
|
+
* @param {{files: Array}} scan 扫描结果(用于图标探测)
|
|
19
|
+
*/
|
|
20
|
+
export async function buildManifest(host, profile, opts = {}, scan = { files: [] }) {
|
|
21
|
+
const pkg = parseJson(await host.readTextFile(host.joinPath(profile.dir, 'package.json')));
|
|
22
|
+
const name = sanitizeSlug(opts.name || profile.name);
|
|
23
|
+
const version = opts.version || pkg?.version || '1.0.0';
|
|
24
|
+
const displayName = opts.displayName || niceName(pkg?.name) || name;
|
|
25
|
+
const description = opts.description ?? pkg?.description ?? '';
|
|
26
|
+
const author = opts.author || (typeof pkg?.author === 'string' ? pkg.author : '') || '';
|
|
27
|
+
// v5:dshVersion 必须是精确版本号(如 0.1.1-rc.2)。导出侧由扫描器注入;缺省置空可被导入端兜底。
|
|
28
|
+
const dshVersion = opts.dshVersion || '';
|
|
29
|
+
const icon = opts.icon || findIcon(scan.files) || '';
|
|
30
|
+
const patch = (await host.readTextFile(host.joinPath(profile.dir, 'cordis.patch.yml'))) ?? '';
|
|
31
|
+
|
|
32
|
+
return applyR2CompatFields({
|
|
33
|
+
manifestVersion: 6,
|
|
34
|
+
type: 'profile',
|
|
35
|
+
name,
|
|
36
|
+
version,
|
|
37
|
+
displayName,
|
|
38
|
+
description,
|
|
39
|
+
author,
|
|
40
|
+
icon,
|
|
41
|
+
dshVersion,
|
|
42
|
+
profileName: opts.profileName || profile.name,
|
|
43
|
+
bundles: extractBundles(pkg),
|
|
44
|
+
dependencies: await coordinatesFromProfileDeps(host, profile.dir, pkg?.dependencies, pkg),
|
|
45
|
+
patch,
|
|
46
|
+
files: opts.files ?? [],
|
|
47
|
+
}, opts);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 从单个 profile 目录生成 ProfileUnit(manifest v5 `profiles` 的值:复用 v4 单 profile 字段,去掉 profileName)。 */
|
|
51
|
+
async function buildProfileUnit(host, profileDir) {
|
|
52
|
+
const pkg = parseJson(await host.readTextFile(host.joinPath(profileDir, 'package.json')));
|
|
53
|
+
const patch = (await host.readTextFile(host.joinPath(profileDir, 'cordis.patch.yml'))) ?? '';
|
|
54
|
+
return {
|
|
55
|
+
bundles: extractBundles(pkg),
|
|
56
|
+
dependencies: await coordinatesFromProfileDeps(host, profileDir, pkg?.dependencies, pkg),
|
|
57
|
+
patch,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 从 home 根与选中的 profile 构建 manifest v6(type: "dshhome")。
|
|
63
|
+
* 四类单元:profiles(有依赖坐标)+ presets / skills / instructions(纯文件索引)。
|
|
64
|
+
* @param {Host} host
|
|
65
|
+
* @param {{name:string, dir:string}} home home 根目录
|
|
66
|
+
* @param {object} opts { name, displayName, version, description, author, icon, dshVersion,
|
|
67
|
+
* defaultProfile, profiles:[{name,dir}], presets, skills, instructions, files }
|
|
68
|
+
*/
|
|
69
|
+
export async function buildHomeManifest(host, home, opts = {}) {
|
|
70
|
+
const profiles = {};
|
|
71
|
+
for (const p of opts.profiles ?? []) {
|
|
72
|
+
if (!p?.name || !p?.dir) continue;
|
|
73
|
+
profiles[p.name] = await buildProfileUnit(host, p.dir);
|
|
74
|
+
}
|
|
75
|
+
const names = Object.keys(profiles);
|
|
76
|
+
return applyR2CompatFields({
|
|
77
|
+
manifestVersion: 6,
|
|
78
|
+
type: 'dshhome',
|
|
79
|
+
name: sanitizeSlug(opts.name || home.name),
|
|
80
|
+
version: opts.version || '1.0.0',
|
|
81
|
+
displayName: opts.displayName || home.name,
|
|
82
|
+
description: opts.description ?? '',
|
|
83
|
+
author: opts.author || '',
|
|
84
|
+
icon: opts.icon || '',
|
|
85
|
+
dshVersion: opts.dshVersion || '',
|
|
86
|
+
defaultProfile: opts.defaultProfile || names[0] || '',
|
|
87
|
+
profiles,
|
|
88
|
+
presets: opts.presets ?? {},
|
|
89
|
+
skills: opts.skills ?? [],
|
|
90
|
+
instructions: opts.instructions || 'AGENTS.md',
|
|
91
|
+
files: opts.files ?? [],
|
|
92
|
+
}, opts);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* v5 r2 兼容性可选字段(dshVersions §13 / launchers §14):仅当 opts 提供有效非空值时写入。
|
|
97
|
+
* dshVersions 去重去空;launchers 过滤 null/undefined 条目。结构合法性由 validateManifest 把关
|
|
98
|
+
* (导出侧 packProfile/packHome 在打包前强校验,dshVersion ∉ dshVersions 等错误在导出时报出)。
|
|
99
|
+
*/
|
|
100
|
+
export function applyR2CompatFields(manifest, opts) {
|
|
101
|
+
if (Array.isArray(opts?.dshVersions)) {
|
|
102
|
+
const set = [...new Set(opts.dshVersions.map((v) => String(v).trim()).filter(Boolean))];
|
|
103
|
+
if (set.length) manifest.dshVersions = set;
|
|
104
|
+
}
|
|
105
|
+
const launchers = opts?.launchers;
|
|
106
|
+
if (launchers && typeof launchers === 'object' && !Array.isArray(launchers)) {
|
|
107
|
+
const entries = Object.entries(launchers).filter(([, v]) => v !== undefined && v !== null);
|
|
108
|
+
if (entries.length) manifest.launchers = Object.fromEntries(entries);
|
|
109
|
+
}
|
|
110
|
+
return manifest;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 有序层栈:dsh.profile.bundles 原文顺序,去重,只留字符串。 */
|
|
114
|
+
export function extractBundles(pkg) {
|
|
115
|
+
const bundles = pkg?.dsh?.profile?.bundles;
|
|
116
|
+
if (!Array.isArray(bundles)) return [];
|
|
117
|
+
const seen = new Set();
|
|
118
|
+
const out = [];
|
|
119
|
+
for (const b of bundles) {
|
|
120
|
+
if (typeof b === 'string' && b.trim() && !seen.has(b)) {
|
|
121
|
+
seen.add(b);
|
|
122
|
+
out.push(b);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 版本化依赖:package.json.dependencies 原文拷贝。 */
|
|
129
|
+
export function extractDependencies(pkg) {
|
|
130
|
+
const deps = pkg?.dependencies;
|
|
131
|
+
if (!deps || typeof deps !== 'object' || Array.isArray(deps)) return {};
|
|
132
|
+
return { ...deps };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 规范化 slug:小写、非 [a-z0-9-] 转 -、合并连续 -。 */
|
|
136
|
+
export function sanitizeSlug(input) {
|
|
137
|
+
return String(input)
|
|
138
|
+
.trim()
|
|
139
|
+
.toLowerCase()
|
|
140
|
+
.replace(/[^a-z0-9-]+/g, '-')
|
|
141
|
+
.replace(/-+/g, '-')
|
|
142
|
+
.replace(/^-|-$/g, '');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 展示名美化:去掉 DSH Profile 包名前缀。 */
|
|
146
|
+
function niceName(raw) {
|
|
147
|
+
if (!raw) return '';
|
|
148
|
+
return String(raw).replace(/^dsh-profile-/, '').replace(/^dsh-/, '');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function findIcon(files) {
|
|
152
|
+
for (const f of files) {
|
|
153
|
+
const rel = (f.rel || '').replace(/\\/g, '/');
|
|
154
|
+
if (ICON_PATTERN.test(rel)) return rel;
|
|
155
|
+
}
|
|
156
|
+
for (const f of files) {
|
|
157
|
+
const rel = (f.rel || '').replace(/\\/g, '/');
|
|
158
|
+
if (/^logo\.[a-z0-9]+$/i.test(rel)) return rel;
|
|
159
|
+
}
|
|
160
|
+
return '';
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function parseJson(raw) {
|
|
164
|
+
if (raw == null) return null;
|
|
165
|
+
try {
|
|
166
|
+
return JSON.parse(raw);
|
|
167
|
+
} catch {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* ---------------------------------------------------------------------------
|
|
173
|
+
* 坐标转换(v3 §5,v4 原样继承)
|
|
174
|
+
* manifest 形式「坐标 → 固定版本」 ⇄ package.json 形式「包名 → pnpm spec」
|
|
175
|
+
* ------------------------------------------------------------------------- */
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 正向:坐标 → package.json 依赖(导入/安装侧使用)。
|
|
179
|
+
* "dsh-pet": "0.2.0" → "dsh-pet": "0.2.0"
|
|
180
|
+
* "github:owner/repo": "<sha>" → "repo": "github:owner/repo#<sha>"
|
|
181
|
+
* "github:owner/repo#path:/pkg": "<sha>" → "pkg": "github:owner/repo#<sha>&path:pkg"
|
|
182
|
+
*/
|
|
183
|
+
export function coordsToPkgDeps(dependencies) {
|
|
184
|
+
const out = {};
|
|
185
|
+
for (const [coord, version] of Object.entries(dependencies ?? {})) {
|
|
186
|
+
const git = parseGitCoord(coord);
|
|
187
|
+
if (git) {
|
|
188
|
+
const ref = version === 'latest' ? '' : `#${version}`;
|
|
189
|
+
out[git.name] = `github:${git.owner}/${git.repo}${ref}` + (git.subpath ? `&path:${git.subpath}` : '');
|
|
190
|
+
} else {
|
|
191
|
+
out[coord] = version; // npm 精确版本,原样保留
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** 解析 manifest 侧的 git 坐标:'github:owner/repo' 或 'github:owner/repo#path:/子目录'。 */
|
|
198
|
+
export function parseGitCoord(coord) {
|
|
199
|
+
if (typeof coord !== 'string' || !coord.startsWith('github:')) return null;
|
|
200
|
+
const rest = coord.slice('github:'.length); // 'owner/repo' 或 'owner/repo#path:/pkg'
|
|
201
|
+
const hash = rest.indexOf('#path:/');
|
|
202
|
+
const repoPart = hash >= 0 ? rest.slice(0, hash) : rest;
|
|
203
|
+
const subpath = hash >= 0 ? rest.slice(hash + '#path:/'.length) : '';
|
|
204
|
+
const slash = repoPart.indexOf('/');
|
|
205
|
+
if (slash <= 0) return null;
|
|
206
|
+
return { owner: repoPart.slice(0, slash), repo: repoPart.slice(slash + 1), subpath, name: subpath || repoPart.slice(slash + 1) };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* 反向:package.json 依赖 → 坐标(导出侧使用)。
|
|
211
|
+
* "dsh-pet": "0.2.0" → "dsh-pet": "0.2.0"
|
|
212
|
+
* "repo": "github:owner/repo#<sha>" → "github:owner/repo": "<sha>"
|
|
213
|
+
* "pkg": "github:owner/repo#<sha>&path:pkg" → "github:owner/repo#path:/pkg": "<sha>"
|
|
214
|
+
*/
|
|
215
|
+
export function pkgDepsToCoords(dependencies) {
|
|
216
|
+
const out = {};
|
|
217
|
+
for (const [pkgName, spec] of Object.entries(dependencies ?? {})) {
|
|
218
|
+
const git = parsePkgGitSpec(spec);
|
|
219
|
+
if (git) {
|
|
220
|
+
out[`github:${git.owner}/${git.repo}${git.subpath ? `#path:/${git.subpath}` : ''}`] = git.sha;
|
|
221
|
+
} else {
|
|
222
|
+
out[pkgName] = spec; // 暂不订死范围(导出侧 M1 用 node_modules 实测版本钉精确)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return out;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const EXACT_SEMVER = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
|
|
229
|
+
|
|
230
|
+
/** 是否已是「精确版本」(无 ^ ~ > = 等前缀)。 */
|
|
231
|
+
export function isExactSemver(v) {
|
|
232
|
+
return typeof v === 'string' && EXACT_SEMVER.test(v.trim());
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 生成 v5 dependencies(坐标→固定版本):
|
|
237
|
+
* - git 依赖:commit sha 优先取 package.json 的 `#sha`,缺则从 pnpm-lock.yaml 的 resolution.commit 补齐;
|
|
238
|
+
* 都没有则标记为 `latest`(安装时跟随默认分支最新,不强制钉 sha);
|
|
239
|
+
* - npm 依赖若仍是范围(^/~ 等),读 node_modules/<name>/package.json 的实测版本钉精确。
|
|
240
|
+
*/
|
|
241
|
+
export async function coordinatesFromProfileDeps(host, dir, deps, profile = {}) {
|
|
242
|
+
if (!deps || typeof deps !== 'object' || Array.isArray(deps)) return {};
|
|
243
|
+
const lockText = await host.readTextFile(host.joinPath(dir, 'pnpm-lock.yaml'));
|
|
244
|
+
const out = {};
|
|
245
|
+
for (const [pkgName, spec] of Object.entries(deps)) {
|
|
246
|
+
const source = installedPluginSource(profile, pkgName);
|
|
247
|
+
if (source) {
|
|
248
|
+
out[pkgName] = source.version;
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const git = parsePkgGitSpec(spec);
|
|
252
|
+
if (git) {
|
|
253
|
+
// 不强制钉 sha:spec 里的 #sha 优先,其次 pnpm-lock 的 resolution.commit;都没有则标记 latest(跟随默认分支最新)。
|
|
254
|
+
const sha = git.sha || gitCommitFromLock(lockText, pkgName);
|
|
255
|
+
out[`github:${git.owner}/${git.repo}${git.subpath ? `#path:/${git.subpath}` : ''}`] = sha || 'latest';
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
// 只把「semver 范围」钉精确(^/~/>/ 等)。带 `:` 的是协议型 spec(file:/link:/workspace:/URL/别名),
|
|
259
|
+
// 必须原样保留——否则导入侧会把它当成 npm registry 包去 404(例如 file: 本地依赖被压平成 0.2.0)。
|
|
260
|
+
if (typeof spec === 'string' && spec && !isExactSemver(spec) && !spec.includes(':')) {
|
|
261
|
+
const v = await readInstalledVersion(host, host.joinPath(dir, 'node_modules', pkgName, 'package.json'));
|
|
262
|
+
out[pkgName] = v ?? spec;
|
|
263
|
+
} else {
|
|
264
|
+
out[pkgName] = spec;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 从 pnpm-lock.yaml 解析某 git 依赖的 commit sha(40 位十六进制)。
|
|
272
|
+
* 扫描 `packages:` 区块内 `pkgName@…` 条目,取其 resolution.commit;找不到返回 null。
|
|
273
|
+
*/
|
|
274
|
+
export function gitCommitFromLock(text, pkgName) {
|
|
275
|
+
if (!text || !pkgName) return null;
|
|
276
|
+
const lines = String(text).split(/\r?\n/);
|
|
277
|
+
let inPackages = false;
|
|
278
|
+
let active = -1; // 目标条目的 key 缩进;-1 = 不在目标条目内
|
|
279
|
+
for (const line of lines) {
|
|
280
|
+
const trimmed = line.trim();
|
|
281
|
+
if (!trimmed) continue;
|
|
282
|
+
if (!inPackages) {
|
|
283
|
+
if (/^packages:\s*$/.test(trimmed)) inPackages = true;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const indent = line.length - line.trimStart().length;
|
|
287
|
+
if (trimmed.endsWith(':') && indent === 0) {
|
|
288
|
+
if (/^packages:\s*$/.test(trimmed)) { active = -1; continue; }
|
|
289
|
+
break; // 顶层区块(snapshots: 等)→ packages 结束
|
|
290
|
+
}
|
|
291
|
+
const key = line.match(/^\s*([^:]+):\s*$/);
|
|
292
|
+
if (key) {
|
|
293
|
+
const k = key[1].replace(/^["']|["']$/g, '');
|
|
294
|
+
if (k === pkgName || k.startsWith(`${pkgName}@`)) {
|
|
295
|
+
active = indent;
|
|
296
|
+
} else if (active >= 0 && indent <= active) {
|
|
297
|
+
active = -1;
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (active >= 0) {
|
|
302
|
+
const cm = line.match(/\bcommit:\s*['"]?([0-9a-f]{40})['"]?/);
|
|
303
|
+
if (cm) return cm[1];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async function readInstalledVersion(host, pkgPath) {
|
|
310
|
+
const pkg = parseJson(await host.readTextFile(pkgPath));
|
|
311
|
+
return typeof pkg?.version === 'string' ? pkg.version : null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** 解析 package.json 侧的 git spec:github:owner/repo#sha[&path:pkg] 或 git+https://github.com/... */
|
|
315
|
+
export function parsePkgGitSpec(spec) {
|
|
316
|
+
if (typeof spec !== 'string') return null;
|
|
317
|
+
|
|
318
|
+
let body = spec;
|
|
319
|
+
if (body.startsWith('github:')) {
|
|
320
|
+
body = body.slice('github:'.length); // 'owner/repo#sha[&path:pkg]'
|
|
321
|
+
let subpath = '';
|
|
322
|
+
const amp = body.indexOf('&path:');
|
|
323
|
+
if (amp >= 0) {
|
|
324
|
+
subpath = body.slice(amp + '&path:'.length);
|
|
325
|
+
body = body.slice(0, amp);
|
|
326
|
+
}
|
|
327
|
+
const hash = body.indexOf('#');
|
|
328
|
+
const repoPart = hash >= 0 ? body.slice(0, hash) : body;
|
|
329
|
+
const sha = hash >= 0 ? body.slice(hash + 1) : '';
|
|
330
|
+
const slash = repoPart.indexOf('/');
|
|
331
|
+
if (slash <= 0) return null;
|
|
332
|
+
return { owner: repoPart.slice(0, slash), repo: repoPart.slice(slash + 1), subpath, sha };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const m = spec.match(
|
|
336
|
+
/^(?:git\+)?https?:\/\/(?:www\.)?github\.com\/([^/#]+)\/([^/#]+?)(?:\.git)?(?:#([^&]+))?(?:&path:([^#]+))?$/,
|
|
337
|
+
);
|
|
338
|
+
if (m) {
|
|
339
|
+
return { owner: m[1], repo: m[2], subpath: m[4] || '', sha: m[3] || '' };
|
|
340
|
+
}
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/* ---------------------------------------------------------------------------
|
|
345
|
+
* 校验
|
|
346
|
+
* ------------------------------------------------------------------------- */
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* manifest 结构校验(v5:profile 与 dshhome;v4:兼容单 profile),返回错误信息数组(空数组 = 合法)。
|
|
350
|
+
* - v4:type 仅接受 'profile'('collection' 预留报「暂未支持」);
|
|
351
|
+
* - v5:type 接受 'profile' 或 'dshhome'(dshhome 校验 profiles / presets / skills / instructions / defaultProfile)。
|
|
352
|
+
* @param {object} opts { lockText? } 可选的随包 pnpm-lock.yaml 文本——提供时 vendored 闭包条目
|
|
353
|
+
* (key = npm 包名,不在 dependencies 中)按 §12 约束 1 的闭包规则放行。
|
|
354
|
+
*/
|
|
355
|
+
export function validateManifest(m, opts = {}) {
|
|
356
|
+
const errors = [];
|
|
357
|
+
if (!m || typeof m !== 'object' || Array.isArray(m)) return ['manifest.json 缺失或不是对象'];
|
|
358
|
+
|
|
359
|
+
if (![4, 5, 6].includes(m.manifestVersion)) {
|
|
360
|
+
if (m.manifestVersion === 3 || m.manifestVersion === 2) {
|
|
361
|
+
errors.push(`manifestVersion 为 ${m.manifestVersion}(旧版 .tgz 格式),本工具仅安装 v4/v5/v6(.dspack) 整合包`);
|
|
362
|
+
} else {
|
|
363
|
+
errors.push('manifestVersion 必须为 4、5 或 6');
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// 公共字段(v4/v5 一致)
|
|
368
|
+
if (typeof m.name !== 'string' || !m.name.trim()) errors.push('manifest.name 缺失或为空');
|
|
369
|
+
if (typeof m.version !== 'string' || !m.version.trim()) errors.push('manifest.version 缺失或为空');
|
|
370
|
+
if (m.patch !== undefined && typeof m.patch !== 'string') errors.push('manifest.patch 必须是字符串');
|
|
371
|
+
if (m.dshVersion !== undefined && typeof m.dshVersion !== 'string') errors.push('manifest.dshVersion 必须是字符串');
|
|
372
|
+
for (const f of ['displayName', 'description']) {
|
|
373
|
+
if (m[f] !== undefined && !isLocaleString(m[f])) errors.push(`manifest.${f} 必须是字符串或多语言对象`);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (m.manifestVersion === 6) {
|
|
377
|
+
for (const key of ['name','version']) {
|
|
378
|
+
if (typeof m[key] === 'string' && (/[\\/:*?"<>|\x00-\x1f]|[. ]$/u.test(m[key]) || /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(m[key]))) {
|
|
379
|
+
errors.push('manifest.' + key + ' 包含不能用于发布文件名的字符');
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (m.type === 'dshhome') {
|
|
383
|
+
if (m.plugins !== undefined) {
|
|
384
|
+
errors.push('dshhome 的 plugins 必须位于对应的 profiles 条目内');
|
|
385
|
+
}
|
|
386
|
+
for (const [id, unit] of Object.entries(m.profiles || {})) {
|
|
387
|
+
for (const error of validatePluginMetadata(unit)) {
|
|
388
|
+
errors.push('profiles[' + id + '].' + error);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
errors.push(...validatePluginMetadata(m));
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (m.manifestVersion === 5 || m.manifestVersion === 6) {
|
|
396
|
+
if (m.type === 'dshhome') errors.push(...validateDshHome(m));
|
|
397
|
+
else if ((m.manifestVersion === 5 && m.type === undefined) || m.type === 'profile') errors.push(...validateProfile(m));
|
|
398
|
+
else errors.push('type 仅支持 "profile" 或 "dshhome"');
|
|
399
|
+
// v5 r2 可选字段(vendored / dshVersions / launchers):未声明时全部跳过(前向兼容,
|
|
400
|
+
// r1 包行为不变);声明了则做结构校验(未知字段不拒绝——消费者必须忽略不认识的字段)。
|
|
401
|
+
errors.push(...validateVendored(m, opts.lockText));
|
|
402
|
+
errors.push(...validateDshVersions(m));
|
|
403
|
+
errors.push(...validateLaunchers(m));
|
|
404
|
+
} else {
|
|
405
|
+
errors.push(...validateProfile(m));
|
|
406
|
+
}
|
|
407
|
+
return errors;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/* ---------------------------------------------------------------------------
|
|
411
|
+
* v5 r2 可选字段校验(vendored §12 / dshVersions §13 / launchers §14)
|
|
412
|
+
* ------------------------------------------------------------------------- */
|
|
413
|
+
|
|
414
|
+
/** 所有形态的依赖坐标合集(profile:dependencies;dshhome:各 profile 依赖并集)。 */
|
|
415
|
+
function dependencyCoords(m) {
|
|
416
|
+
if (m.type === 'dshhome') {
|
|
417
|
+
const set = new Map(); // coord → 版本(同名冲突时先到先得,仅用于一致性检查)
|
|
418
|
+
for (const u of Object.values(m.profiles ?? {})) {
|
|
419
|
+
for (const [k, v] of Object.entries(u?.dependencies ?? {})) if (!set.has(k)) set.set(k, v);
|
|
420
|
+
}
|
|
421
|
+
return set;
|
|
422
|
+
}
|
|
423
|
+
return new Map(Object.entries(m.dependencies ?? {}));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const VENDORED_REASONS = new Set(['upstream-missing', 'unpublished', 'local-modified', 'explicit']);
|
|
427
|
+
export { VENDORED_REASONS };
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* vendored{}(§12):条目五字段 + key ∈ dependencies ∪ lockfile packages 对账 + version 一致性。
|
|
431
|
+
* 闭包条目(key ∈ 随包 pnpm-lock.yaml 的 packages 名集,vendor=full 档传递依赖):不在
|
|
432
|
+
* dependencies 中,无钉死版本 → 跳过版本一致性检查(版本基准是 lockfile,见 v5 §12 约束 1)。
|
|
433
|
+
*/
|
|
434
|
+
function validateVendored(m, lockText) {
|
|
435
|
+
const errors = [];
|
|
436
|
+
if (m.vendored === undefined) return errors;
|
|
437
|
+
if (typeof m.vendored !== 'object' || m.vendored === null || Array.isArray(m.vendored)) {
|
|
438
|
+
return ['manifest.vendored 必须是对象(坐标 → VendoredEntry)'];
|
|
439
|
+
}
|
|
440
|
+
const deps = dependencyCoords(m);
|
|
441
|
+
const lockNames = lockText ? lockfilePackageNames(lockText) : null;
|
|
442
|
+
for (const [coord, e] of Object.entries(m.vendored)) {
|
|
443
|
+
const at = `vendored[${coord}]`;
|
|
444
|
+
if (!e || typeof e !== 'object' || Array.isArray(e)) {
|
|
445
|
+
errors.push(`${at} 必须是对象`);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (typeof e.version !== 'string' || !e.version) errors.push(`${at}.version 必须是非空字符串`);
|
|
449
|
+
if (typeof e.sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(e.sha256)) errors.push(`${at}.sha256 必须是 64 位十六进制`);
|
|
450
|
+
if (typeof e.size !== 'number' || !Number.isInteger(e.size) || e.size <= 0) errors.push(`${at}.size 必须是正整数`);
|
|
451
|
+
if (typeof e.path !== 'string' || !e.path) {
|
|
452
|
+
errors.push(`${at}.path 必须是非空字符串`);
|
|
453
|
+
} else {
|
|
454
|
+
const rel = e.path.replace(/\\/g, '/');
|
|
455
|
+
if (!rel.startsWith('vendor/') || !rel.endsWith('.tgz')) errors.push(`${at}.path 必须位于 vendor/ 下且以 .tgz 结尾`);
|
|
456
|
+
if (rel.split('/').includes('..')) errors.push(`${at}.path 含危险段 '..'`);
|
|
457
|
+
}
|
|
458
|
+
if (e.reason !== undefined && !VENDORED_REASONS.has(e.reason)) {
|
|
459
|
+
errors.push(`${at}.reason 仅支持 ${[...VENDORED_REASONS].join(' / ')}`);
|
|
460
|
+
}
|
|
461
|
+
if (!deps.has(coord)) {
|
|
462
|
+
if (!lockNames?.has(coord)) {
|
|
463
|
+
errors.push(`${at} 不在 dependencies ∪ lockfile packages 中(vendored key 必须 ∈ dependencies,闭包条目以 pnpm-lock.yaml 为准放行,见 v5 §12 约束 1)`);
|
|
464
|
+
}
|
|
465
|
+
// 闭包条目:无 dependencies 钉死版本 → 跳过版本一致性检查
|
|
466
|
+
} else if (typeof e.version === 'string' && e.version && deps.get(coord) !== e.version) {
|
|
467
|
+
errors.push(`${at}.version(${e.version})与 dependencies 钉死的版本(${deps.get(coord)})不一致`);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return errors;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/** dshVersions(§13):非空、字符串、去重;dshVersion 同时出现时必须 ∈ 集合。 */
|
|
474
|
+
function validateDshVersions(m) {
|
|
475
|
+
const errors = [];
|
|
476
|
+
if (m.dshVersions === undefined) return errors;
|
|
477
|
+
if (!Array.isArray(m.dshVersions) || m.dshVersions.length === 0) {
|
|
478
|
+
return ['manifest.dshVersions 必须是非空数组(缺省请整个省略该字段)'];
|
|
479
|
+
}
|
|
480
|
+
const seen = new Set();
|
|
481
|
+
for (const v of m.dshVersions) {
|
|
482
|
+
if (typeof v !== 'string' || !v.trim()) {
|
|
483
|
+
errors.push('manifest.dshVersions 每项必须是合法版本字符串');
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
if (seen.has(v)) errors.push(`manifest.dshVersions 存在重复项:${v}`);
|
|
487
|
+
seen.add(v);
|
|
488
|
+
}
|
|
489
|
+
if (typeof m.dshVersion === 'string' && m.dshVersion && !m.dshVersions.includes(m.dshVersion)) {
|
|
490
|
+
errors.push(`manifest.dshVersion(${m.dshVersion})必须 ∈ dshVersions`);
|
|
491
|
+
}
|
|
492
|
+
return errors;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const LAUNCHER_KEYS = new Set(['supported', 'minVersion', 'reason']);
|
|
496
|
+
|
|
497
|
+
/** launchers(§14):简式(boolean | string)或全式(supported/minVersion/reason,三字段锁定)。 */
|
|
498
|
+
function validateLaunchers(m) {
|
|
499
|
+
const errors = [];
|
|
500
|
+
if (m.launchers === undefined) return errors;
|
|
501
|
+
if (typeof m.launchers !== 'object' || m.launchers === null || Array.isArray(m.launchers)) {
|
|
502
|
+
return ['manifest.launchers 必须是对象(启动器 ID → 兼容声明)'];
|
|
503
|
+
}
|
|
504
|
+
for (const [id, v] of Object.entries(m.launchers)) {
|
|
505
|
+
const at = `launchers[${id}]`;
|
|
506
|
+
if (typeof v === 'boolean' || typeof v === 'string') continue; // 简式糖
|
|
507
|
+
if (!v || typeof v !== 'object' || Array.isArray(v)) {
|
|
508
|
+
errors.push(`${at} 必须是 boolean / 字符串(简式)或对象(全式)`);
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
for (const k of Object.keys(v)) {
|
|
512
|
+
if (!LAUNCHER_KEYS.has(k)) errors.push(`${at} 含未知字段「${k}」(全式仅锁定 supported / minVersion / reason)`);
|
|
513
|
+
}
|
|
514
|
+
if (v.supported !== undefined && typeof v.supported !== 'boolean') errors.push(`${at}.supported 必须是 boolean`);
|
|
515
|
+
if (v.minVersion !== undefined && (typeof v.minVersion !== 'string' || !v.minVersion.trim())) errors.push(`${at}.minVersion 必须是非空字符串`);
|
|
516
|
+
if (v.reason !== undefined && typeof v.reason !== 'string') errors.push(`${at}.reason 必须是字符串`);
|
|
517
|
+
}
|
|
518
|
+
// 未注册的启动器 ID:校验器警告不拒绝(launcher-registry §2,防拦截新生态)——结构合法即放行。
|
|
519
|
+
return errors;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/* ---------------------------------------------------------------------------
|
|
523
|
+
* launchers 归一化与版本比较(v5 §14 / launcher-registry §3,供安装端判定用)
|
|
524
|
+
* ------------------------------------------------------------------------- */
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* launchers 简式糖 → 全式归一(v5 §14:语义上只有全式,标量是糖):
|
|
528
|
+
* true → { supported: true }
|
|
529
|
+
* "<version>" → { supported: true, minVersion: "<version>" }
|
|
530
|
+
* false → { supported: false }
|
|
531
|
+
* 全式对象仅保留 supported / minVersion / reason 三字段(supported 缺省视为 true)。
|
|
532
|
+
* 非法条目静默跳过(结构合法性由 validateLaunchers 把关,本纯函数只做容错归一)。
|
|
533
|
+
* @param {object} launchers manifest.launchers 原文
|
|
534
|
+
* @returns {{ [id: string]: { supported: boolean, minVersion?: string, reason?: string } }}
|
|
535
|
+
*/
|
|
536
|
+
export function normalizeLaunchers(launchers) {
|
|
537
|
+
const out = {};
|
|
538
|
+
if (!launchers || typeof launchers !== 'object' || Array.isArray(launchers)) return out;
|
|
539
|
+
for (const [id, v] of Object.entries(launchers)) {
|
|
540
|
+
if (v === true) {
|
|
541
|
+
out[id] = { supported: true };
|
|
542
|
+
} else if (v === false) {
|
|
543
|
+
out[id] = { supported: false };
|
|
544
|
+
} else if (typeof v === 'string' && v.trim()) {
|
|
545
|
+
out[id] = { supported: true, minVersion: v.trim() };
|
|
546
|
+
} else if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
547
|
+
const e = { supported: v.supported !== false };
|
|
548
|
+
if (typeof v.minVersion === 'string' && v.minVersion.trim()) e.minVersion = v.minVersion.trim();
|
|
549
|
+
if (typeof v.reason === 'string') e.reason = v.reason;
|
|
550
|
+
out[id] = e;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return out;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* 启动器版本比较(launcher-registry §3):按 `.` 分段、逐段数值比较、缺段视为 0;
|
|
558
|
+
* 不做 rc 等预发布语义(启动器场景用不到,避免过度设计)。
|
|
559
|
+
* @returns {number} >0 表示 a 更新;<0 表示 b 更新;0 表示相等
|
|
560
|
+
*/
|
|
561
|
+
export function compareLauncherVersions(a, b) {
|
|
562
|
+
const seg = (v) => String(v ?? '').split('.').map((s) => parseInt(s, 10) || 0);
|
|
563
|
+
const A = seg(a);
|
|
564
|
+
const B = seg(b);
|
|
565
|
+
for (let i = 0; i < Math.max(A.length, B.length); i += 1) {
|
|
566
|
+
const d = (A[i] || 0) - (B[i] || 0);
|
|
567
|
+
if (d !== 0) return d > 0 ? 1 : -1;
|
|
568
|
+
}
|
|
569
|
+
return 0;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** 单 profile 校验(v4/v5 共用)。 */
|
|
573
|
+
function validateProfile(m) {
|
|
574
|
+
const errors = [];
|
|
575
|
+
if (m.type !== undefined && m.type !== 'profile') {
|
|
576
|
+
errors.push('type 仅支持 "profile"(collection 为预留值,暂未支持)');
|
|
577
|
+
}
|
|
578
|
+
if (!Array.isArray(m.bundles) || m.bundles.some((b) => typeof b !== 'string')) {
|
|
579
|
+
errors.push('manifest.bundles 必须是字符串数组');
|
|
580
|
+
}
|
|
581
|
+
if (typeof m.dependencies !== 'object' || m.dependencies === null || Array.isArray(m.dependencies)) {
|
|
582
|
+
errors.push('manifest.dependencies 必须是对象');
|
|
583
|
+
} else {
|
|
584
|
+
for (const [k, v] of Object.entries(m.dependencies)) {
|
|
585
|
+
if (typeof v !== 'string' || !v) errors.push(`dependencies[${k}] 必须是「坐标 → 固定版本」字符串`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (m.files !== undefined) errors.push(...validateFiles(m.files));
|
|
589
|
+
return errors;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** v5 dshhome 校验。 */
|
|
593
|
+
function validateDshHome(m) {
|
|
594
|
+
const errors = [];
|
|
595
|
+
if (m.type !== undefined && m.type !== 'dshhome') {
|
|
596
|
+
errors.push('type 仅支持 "dshhome"(单 profile 请用 type:"profile")');
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (typeof m.profiles !== 'object' || m.profiles === null || Array.isArray(m.profiles)) {
|
|
600
|
+
errors.push('manifest.profiles 必须是对象(name → ProfileUnit)');
|
|
601
|
+
} else {
|
|
602
|
+
const names = Object.keys(m.profiles);
|
|
603
|
+
if (names.length === 0) errors.push('manifest.profiles 至少含 1 个 profile');
|
|
604
|
+
for (const reserved of ['web', 'headless']) {
|
|
605
|
+
if (names.includes(reserved)) errors.push(`profiles 不得含安装基线模板「${reserved}」`);
|
|
606
|
+
}
|
|
607
|
+
for (const [name, u] of Object.entries(m.profiles)) {
|
|
608
|
+
if (!u || typeof u !== 'object' || Array.isArray(u)) {
|
|
609
|
+
errors.push(`profiles[${name}] 必须是对象`);
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
if (!Array.isArray(u.bundles) || u.bundles.some((b) => typeof b !== 'string')) {
|
|
613
|
+
errors.push(`profiles[${name}].bundles 必须是字符串数组`);
|
|
614
|
+
}
|
|
615
|
+
if (typeof u.dependencies !== 'object' || u.dependencies === null || Array.isArray(u.dependencies)) {
|
|
616
|
+
errors.push(`profiles[${name}].dependencies 必须是对象`);
|
|
617
|
+
} else {
|
|
618
|
+
for (const [k, v] of Object.entries(u.dependencies)) {
|
|
619
|
+
if (typeof v !== 'string' || !v) errors.push(`profiles[${name}].dependencies[${k}] 必须是「坐标 → 固定版本」字符串`);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
if (u.patch !== undefined && typeof u.patch !== 'string') errors.push(`profiles[${name}].patch 必须是字符串`);
|
|
623
|
+
}
|
|
624
|
+
if (typeof m.defaultProfile !== 'string' || !m.defaultProfile) {
|
|
625
|
+
errors.push('manifest.defaultProfile 缺失或为空');
|
|
626
|
+
} else if (!names.includes(m.defaultProfile)) {
|
|
627
|
+
errors.push(`defaultProfile「${m.defaultProfile}」不在 profiles 中`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
if (m.presets !== undefined) {
|
|
632
|
+
if (typeof m.presets !== 'object' || m.presets === null || Array.isArray(m.presets)) {
|
|
633
|
+
errors.push('manifest.presets 必须是对象(name → PresetUnit)');
|
|
634
|
+
} else {
|
|
635
|
+
for (const [name, u] of Object.entries(m.presets)) {
|
|
636
|
+
if (!u || typeof u !== 'object' || Array.isArray(u) || typeof u.path !== 'string' || !u.path) {
|
|
637
|
+
errors.push(`presets[${name}] 必须是含 path 的对象`);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (m.skills !== undefined) {
|
|
644
|
+
if (!Array.isArray(m.skills)) {
|
|
645
|
+
errors.push('manifest.skills 必须是数组');
|
|
646
|
+
} else {
|
|
647
|
+
m.skills.forEach((s, i) => {
|
|
648
|
+
if (!s || typeof s !== 'object' || Array.isArray(s) || typeof s.path !== 'string' || !s.path) {
|
|
649
|
+
errors.push(`skills[${i}] 必须是含 path 的对象`);
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (m.instructions !== undefined && (typeof m.instructions !== 'string' || !m.instructions)) {
|
|
656
|
+
errors.push('manifest.instructions 必须是非空字符串');
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (m.files !== undefined) errors.push(...validateFiles(m.files));
|
|
660
|
+
return errors;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/** files[] 数组校验(v4/v5 共用)。 */
|
|
664
|
+
function validateFiles(files) {
|
|
665
|
+
const errors = [];
|
|
666
|
+
if (!Array.isArray(files)) {
|
|
667
|
+
errors.push('manifest.files 必须是数组');
|
|
668
|
+
} else {
|
|
669
|
+
files.forEach((f, i) => {
|
|
670
|
+
for (const e of validateFileEntry(f)) errors.push(`files[${i}] ${e}`);
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
return errors;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** files[] 单条校验(v4 §3:path/sha256/size/urls[])。 */
|
|
677
|
+
export function validateFileEntry(f) {
|
|
678
|
+
const errors = [];
|
|
679
|
+
if (!f || typeof f !== 'object' || Array.isArray(f)) return ['不是对象'];
|
|
680
|
+
if (typeof f.path !== 'string' || !f.path || f.path.startsWith('/') || /^[a-zA-Z]:/.test(f.path)) {
|
|
681
|
+
errors.push('path 必须是相对路径("+"分隔,不以盘符/斜杠开头)');
|
|
682
|
+
}
|
|
683
|
+
if (typeof f.sha256 !== 'string' || !/^[0-9a-f]{64}$/.test(f.sha256)) {
|
|
684
|
+
errors.push('sha256 必须是 64 位十六进制');
|
|
685
|
+
}
|
|
686
|
+
if (typeof f.size !== 'number' || !Number.isInteger(f.size) || f.size <= 0) {
|
|
687
|
+
errors.push('size 必须是正整数');
|
|
688
|
+
}
|
|
689
|
+
if (!Array.isArray(f.urls) || f.urls.length === 0 || f.urls.some((u) => typeof u !== 'string' || !/^https?:\/\//i.test(u))) {
|
|
690
|
+
errors.push('urls 必须是非空数组,且每项是 http(s) 地址');
|
|
691
|
+
}
|
|
692
|
+
return errors;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** displayName/description 的 string|map 判定。 */
|
|
696
|
+
export function isLocaleString(v) {
|
|
697
|
+
if (typeof v === 'string') return true;
|
|
698
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
699
|
+
const keys = Object.keys(v);
|
|
700
|
+
return keys.length > 0 && keys.every((k) => typeof k === 'string' && k !== '' && typeof v[k] === 'string');
|
|
701
|
+
}
|
|
702
|
+
return false;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** 按界面语言解析多语言元数据:字符串原样,map 按 locale → en-US → zh-CN → 首项回退。 */
|
|
706
|
+
export function resolveLocale(value, locale) {
|
|
707
|
+
if (typeof value === 'string') return value;
|
|
708
|
+
if (value && typeof value === 'object') {
|
|
709
|
+
if (locale && typeof value[locale] === 'string') return value[locale];
|
|
710
|
+
if (typeof value['en-US'] === 'string') return value['en-US'];
|
|
711
|
+
if (typeof value['zh-CN'] === 'string') return value['zh-CN'];
|
|
712
|
+
for (const k of Object.keys(value)) {
|
|
713
|
+
if (typeof value[k] === 'string') return value[k];
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return '';
|
|
717
|
+
}
|