dsh-crate 0.0.0-stage → 0.5.0
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 +27 -0
- package/README.md +288 -2
- package/README.zh.md +263 -0
- package/UPDATE-API-V1.md +207 -0
- package/assets/logo.svg +37 -0
- package/client/client.js +18964 -0
- package/cordis.patch.yml +4 -0
- package/lib/accelerate.js +194 -0
- package/lib/agents.js +67 -0
- package/lib/backup.js +688 -0
- package/lib/body-progress.js +173 -0
- package/lib/catalog-local-match.js +160 -0
- package/lib/catalog-npm.js +180 -0
- package/lib/changelog.js +462 -0
- package/lib/channels.js +64 -0
- package/lib/check.js +1557 -0
- package/lib/compatibility.js +243 -0
- package/lib/crate-catalog.js +179 -0
- package/lib/diagnostics.js +49 -0
- package/lib/discovery-compatibility.js +447 -0
- package/lib/drain-journal.js +164 -0
- package/lib/drain.js +1043 -0
- package/lib/dsh-cli.js +1373 -0
- package/lib/dsh-install.js +259 -0
- package/lib/entry-identity.js +85 -0
- package/lib/gist.js +369 -0
- package/lib/groups.js +108 -0
- package/lib/home-paths.js +29 -0
- package/lib/host-recovery.js +187 -0
- package/lib/hot.js +871 -0
- package/lib/http.js +252 -0
- package/lib/index.js +394 -0
- package/lib/install.js +1147 -0
- package/lib/lifecycle.js +37 -0
- package/lib/log-redaction.js +24 -0
- package/lib/log.js +204 -0
- package/lib/ndjson.js +154 -0
- package/lib/net.js +174 -0
- package/lib/official-desktop.js +619 -0
- package/lib/order.js +284 -0
- package/lib/patch.js +943 -0
- package/lib/pending-rows.js +142 -0
- package/lib/pending-updates.js +346 -0
- package/lib/pnpm-compat.js +972 -0
- package/lib/presets.js +282 -0
- package/lib/probe-context.js +20 -0
- package/lib/proc-text.js +69 -0
- package/lib/profile.js +1978 -0
- package/lib/recovery.js +1059 -0
- package/lib/region-probe.js +93 -0
- package/lib/regions.js +283 -0
- package/lib/registry.js +308 -0
- package/lib/release-candidates.js +115 -0
- package/lib/release-updates.js +70 -0
- package/lib/restart.js +688 -0
- package/lib/routes.js +7960 -0
- package/lib/settings.js +164 -0
- package/lib/shelfry-catalog.js +179 -0
- package/lib/snapshot.js +506 -0
- package/lib/source-migration.js +164 -0
- package/lib/sources.js +1005 -0
- package/lib/state.js +38 -0
- package/lib/store.js +163 -0
- package/lib/themes.js +350 -0
- package/lib/trial.js +125 -0
- package/lib/types/accelerate.d.ts +85 -0
- package/lib/types/agents.d.ts +52 -0
- package/lib/types/backup.d.ts +171 -0
- package/lib/types/body-progress.d.ts +94 -0
- package/lib/types/catalog-local-match.d.ts +39 -0
- package/lib/types/catalog-npm.d.ts +77 -0
- package/lib/types/changelog.d.ts +131 -0
- package/lib/types/channels.d.ts +56 -0
- package/lib/types/check.d.ts +353 -0
- package/lib/types/compatibility.d.ts +83 -0
- package/lib/types/crate-catalog.d.ts +48 -0
- package/lib/types/diagnostics.d.ts +29 -0
- package/lib/types/discovery-compatibility.d.ts +145 -0
- package/lib/types/drain-journal.d.ts +25 -0
- package/lib/types/drain.d.ts +306 -0
- package/lib/types/dsh-cli.d.ts +481 -0
- package/lib/types/dsh-install.d.ts +106 -0
- package/lib/types/entry-identity.d.ts +68 -0
- package/lib/types/gist.d.ts +79 -0
- package/lib/types/groups.d.ts +34 -0
- package/lib/types/home-paths.d.ts +13 -0
- package/lib/types/host-recovery.d.ts +95 -0
- package/lib/types/hot.d.ts +405 -0
- package/lib/types/http.d.ts +114 -0
- package/lib/types/index.d.ts +52 -0
- package/lib/types/install.d.ts +463 -0
- package/lib/types/lifecycle.d.ts +14 -0
- package/lib/types/log-redaction.d.ts +2 -0
- package/lib/types/log.d.ts +43 -0
- package/lib/types/ndjson.d.ts +52 -0
- package/lib/types/net.d.ts +94 -0
- package/lib/types/official-desktop.d.ts +59 -0
- package/lib/types/order.d.ts +100 -0
- package/lib/types/patch.d.ts +218 -0
- package/lib/types/pending-rows.d.ts +81 -0
- package/lib/types/pending-updates.d.ts +161 -0
- package/lib/types/pnpm-compat.d.ts +102 -0
- package/lib/types/presets.d.ts +94 -0
- package/lib/types/probe-context.d.ts +14 -0
- package/lib/types/proc-text.d.ts +29 -0
- package/lib/types/profile.d.ts +551 -0
- package/lib/types/recovery.d.ts +342 -0
- package/lib/types/region-probe.d.ts +50 -0
- package/lib/types/regions.d.ts +134 -0
- package/lib/types/registry.d.ts +160 -0
- package/lib/types/release-candidates.d.ts +42 -0
- package/lib/types/release-updates.d.ts +15 -0
- package/lib/types/restart.d.ts +279 -0
- package/lib/types/routes.d.ts +130 -0
- package/lib/types/settings.d.ts +112 -0
- package/lib/types/shelfry-catalog.d.ts +48 -0
- package/lib/types/snapshot.d.ts +93 -0
- package/lib/types/source-migration.d.ts +100 -0
- package/lib/types/sources.d.ts +391 -0
- package/lib/types/state.d.ts +20 -0
- package/lib/types/store.d.ts +47 -0
- package/lib/types/themes.d.ts +46 -0
- package/lib/types/trial.d.ts +61 -0
- package/lib/types/update-api-v1.d.ts +83 -0
- package/lib/types/updates.d.ts +273 -0
- package/lib/types/verify.d.ts +277 -0
- package/lib/update-api-v1.js +273 -0
- package/lib/updates.js +981 -0
- package/lib/verify.js +904 -0
- package/locale/en.json +5 -0
- package/locale/zh.json +6 -0
- package/package.json +113 -4
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-contract compatibility preflight for #195.
|
|
3
|
+
*
|
|
4
|
+
* Pure evaluation of what `analyzeProfile()` already reports: a confirmed
|
|
5
|
+
* peer mismatch (`satisfied === false`) is translated into a directional
|
|
6
|
+
* verdict:
|
|
7
|
+
*
|
|
8
|
+
* - `belowMin`: the resolved version is older than every alternative's lower
|
|
9
|
+
* bound — the environment is too old for the plugin's declared contract.
|
|
10
|
+
* - `aboveMax`: the resolved version is newer than every alternative's upper
|
|
11
|
+
* bound (or the exact pin). This is only a risk when the author expressed
|
|
12
|
+
* an explicit upper bound or exact pin; otherwise it is a warning, because
|
|
13
|
+
* the ecosystem currently has many sloppy `^0.0.1`-style declarations that
|
|
14
|
+
* work in practice.
|
|
15
|
+
*
|
|
16
|
+
* Everything else stays informational: `*`, prerelease-vs-`*` artifacts,
|
|
17
|
+
* unparseable ranges, and optional peers never produce a risk here.
|
|
18
|
+
*/
|
|
19
|
+
import { analyzeProfile, compareSemver } from './check.js';
|
|
20
|
+
import { profileDir, readInstalledManifest } from './profile.js';
|
|
21
|
+
const SEMVER = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
22
|
+
function parseComparator(part) {
|
|
23
|
+
const p = part.trim();
|
|
24
|
+
const m = /^(\^|~|>=|<=|>|<|=)?(.*)$/.exec(p);
|
|
25
|
+
if (m === null)
|
|
26
|
+
return null;
|
|
27
|
+
const target = (m[2] ?? '').trim();
|
|
28
|
+
if (!SEMVER.test(target))
|
|
29
|
+
return null;
|
|
30
|
+
const raw = m[1] ?? '';
|
|
31
|
+
return { op: raw === '' || raw === '=' ? 'exact' : raw, target };
|
|
32
|
+
}
|
|
33
|
+
/** Next breaking bump for `^` / `~`, using `-0` so prereleases compare consistently. */
|
|
34
|
+
function nextBound(target, kind) {
|
|
35
|
+
const m = /^(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?$/.exec(target);
|
|
36
|
+
if (m === null)
|
|
37
|
+
return null;
|
|
38
|
+
const major = Number(m[1]);
|
|
39
|
+
const minor = Number(m[2]);
|
|
40
|
+
const patch = Number(m[3]);
|
|
41
|
+
if (kind === '^') {
|
|
42
|
+
if (major > 0)
|
|
43
|
+
return `${major + 1}.0.0-0`;
|
|
44
|
+
if (minor > 0)
|
|
45
|
+
return `0.${minor + 1}.0-0`;
|
|
46
|
+
return `0.0.${patch + 1}-0`;
|
|
47
|
+
}
|
|
48
|
+
return `${major}.${minor + 1}.0-0`;
|
|
49
|
+
}
|
|
50
|
+
function boundsFor(range) {
|
|
51
|
+
const alternatives = [];
|
|
52
|
+
for (const rawAlternative of range.split('||')) {
|
|
53
|
+
const parts = rawAlternative.trim().split(/\s+/).filter(Boolean);
|
|
54
|
+
const parsed = parts.map(parseComparator);
|
|
55
|
+
if (parsed.some(part => part === null))
|
|
56
|
+
return null;
|
|
57
|
+
let lower = null;
|
|
58
|
+
let upper = null;
|
|
59
|
+
let explicitUpper = false;
|
|
60
|
+
let exact = null;
|
|
61
|
+
for (const part of parsed) {
|
|
62
|
+
const p = part;
|
|
63
|
+
if (p.op === 'exact') {
|
|
64
|
+
exact = p.target;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (p.op === '^' || p.op === '~') {
|
|
68
|
+
const bound = nextBound(p.target, p.op);
|
|
69
|
+
if (bound === null)
|
|
70
|
+
return null;
|
|
71
|
+
if (lower === null || compareSemver(p.target, lower.target) > 0)
|
|
72
|
+
lower = p;
|
|
73
|
+
if (upper === null || compareSemver(bound, upper.target) < 0)
|
|
74
|
+
upper = { op: '<=', target: bound };
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (p.op === '>=' || p.op === '>') {
|
|
78
|
+
if (lower === null || compareSemver(p.target, lower.target) > 0)
|
|
79
|
+
lower = p;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
if (upper === null || compareSemver(p.target, upper.target) < 0)
|
|
83
|
+
upper = p;
|
|
84
|
+
explicitUpper = true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
alternatives.push({ lower, upper, explicitUpper, exact });
|
|
88
|
+
}
|
|
89
|
+
return alternatives;
|
|
90
|
+
}
|
|
91
|
+
function belowAllMins(resolved, bounds) {
|
|
92
|
+
return bounds.every(alternative => {
|
|
93
|
+
if (alternative.exact !== null)
|
|
94
|
+
return compareSemver(resolved, alternative.exact) < 0;
|
|
95
|
+
if (alternative.lower === null)
|
|
96
|
+
return false;
|
|
97
|
+
const lower = alternative.lower;
|
|
98
|
+
return lower.op === '>' ? compareSemver(resolved, lower.target) <= 0 : compareSemver(resolved, lower.target) < 0;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function aboveAllMaxes(resolved, bounds) {
|
|
102
|
+
return bounds.every(alternative => {
|
|
103
|
+
if (alternative.exact !== null)
|
|
104
|
+
return compareSemver(resolved, alternative.exact) > 0;
|
|
105
|
+
if (alternative.upper === null)
|
|
106
|
+
return false;
|
|
107
|
+
const upper = alternative.upper;
|
|
108
|
+
return upper.op === '<' ? compareSemver(resolved, upper.target) >= 0 : compareSemver(resolved, upper.target) > 0;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function hasExplicitUpperOrExact(bounds) {
|
|
112
|
+
return bounds.every(alternative => alternative.exact !== null || alternative.explicitUpper);
|
|
113
|
+
}
|
|
114
|
+
/** Major.minor lines a `^` / `~` comparator declared, as `major.minor`. */
|
|
115
|
+
function declaredLines(range) {
|
|
116
|
+
const lines = [];
|
|
117
|
+
for (const rawAlternative of range.split('||')) {
|
|
118
|
+
for (const raw of rawAlternative.trim().split(/\s+/).filter(Boolean)) {
|
|
119
|
+
const parsed = parseComparator(raw);
|
|
120
|
+
if (parsed === null)
|
|
121
|
+
return null;
|
|
122
|
+
if (parsed.op !== '^' && parsed.op !== '~')
|
|
123
|
+
continue;
|
|
124
|
+
const m = /^(\d+)\.(\d+)\./.exec(parsed.target);
|
|
125
|
+
if (m !== null)
|
|
126
|
+
lines.push(`${m[1]}.${m[2]}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return lines;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Whether a resolved version has crossed a `^` / `~` ceiling the author never
|
|
133
|
+
* wrote — the release-line break (`^0.1.x` judged by a 0.2 host).
|
|
134
|
+
*
|
|
135
|
+
* `hasExplicitUpperOrExact` says an implicit caret/tilde ceiling is too sloppy
|
|
136
|
+
* to call a risk, because authors write `^0.0.1` without meaning `<0.0.2-0`.
|
|
137
|
+
* That leniency earns its keep only while the host is still on a line the
|
|
138
|
+
* range named: the leftover failure is then the prerelease gate, which says
|
|
139
|
+
* nothing about the host's ability to load the plugin. `^0.1.x` under a 0.2
|
|
140
|
+
* host is a different claim — the declared line is behind us, and dsh's own
|
|
141
|
+
* gate (node-semver, the same bounds) refuses that release for exactly this
|
|
142
|
+
* reason. Both are `aboveMax` warnings to {@link classifyPeer}; the difference
|
|
143
|
+
* is whether a declared line is still shared.
|
|
144
|
+
*
|
|
145
|
+
* A `^0.0.x` range is excluded by design: it names no minor line to fall
|
|
146
|
+
* behind, and its ceiling has always been treated as noise rather than a
|
|
147
|
+
* declaration (see the sloppy-ceiling case in discovery-compatibility).
|
|
148
|
+
*/
|
|
149
|
+
export function crossesImplicitCeiling(resolved, range) {
|
|
150
|
+
const lines = declaredLines(range);
|
|
151
|
+
if (lines === null || lines.includes(resolved.split('.').slice(0, 2).join('.')))
|
|
152
|
+
return false;
|
|
153
|
+
// Only a range that named a real minor line can be left behind.
|
|
154
|
+
const minorLines = lines.filter(line => !line.endsWith('.0'));
|
|
155
|
+
if (minorLines.length === 0)
|
|
156
|
+
return false;
|
|
157
|
+
return boundsFor(range)?.some(alternative => alternative.exact === null && alternative.upper !== null && aboveAllMaxes(resolved, [alternative])) === true;
|
|
158
|
+
}
|
|
159
|
+
/** Translate one confirmed peer mismatch into a directional verdict. */
|
|
160
|
+
export function classifyPeer(plugin, peer, range, resolved, optional) {
|
|
161
|
+
if (resolved === null)
|
|
162
|
+
return { kind: 'none' };
|
|
163
|
+
if (optional) {
|
|
164
|
+
return {
|
|
165
|
+
kind: 'warning',
|
|
166
|
+
warning: { plugin, peer, range, resolved, reason: 'optional' },
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const bounds = boundsFor(range);
|
|
170
|
+
if (bounds === null)
|
|
171
|
+
return { kind: 'none' };
|
|
172
|
+
if (belowAllMins(resolved, bounds)) {
|
|
173
|
+
return {
|
|
174
|
+
kind: 'risk',
|
|
175
|
+
risk: { plugin, peer, range, resolved, direction: 'belowMin' },
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (aboveAllMaxes(resolved, bounds)) {
|
|
179
|
+
return hasExplicitUpperOrExact(bounds)
|
|
180
|
+
? {
|
|
181
|
+
kind: 'risk',
|
|
182
|
+
risk: { plugin, peer, range, resolved, direction: 'aboveMax' },
|
|
183
|
+
}
|
|
184
|
+
: {
|
|
185
|
+
kind: 'warning',
|
|
186
|
+
warning: { plugin, peer, range, resolved, reason: 'aboveMax' },
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return { kind: 'none' };
|
|
190
|
+
}
|
|
191
|
+
/** Whether a peer is declared optional in the installed plugin manifest. */
|
|
192
|
+
export function isOptionalPeer(profileDirectory, plugin, peer) {
|
|
193
|
+
const manifest = readInstalledManifest('web', plugin, profileDirectory);
|
|
194
|
+
return manifest?.peerDependenciesMeta?.[peer]?.optional === true;
|
|
195
|
+
}
|
|
196
|
+
/** Evaluate the current profile with the same machinery `/dsh-crate/check` uses. */
|
|
197
|
+
export function assessCompatibility(profileDirectory, options) {
|
|
198
|
+
const report = analyzeProfile(profileDirectory, options);
|
|
199
|
+
const risks = [];
|
|
200
|
+
const warnings = [];
|
|
201
|
+
for (const mismatch of report.peerMismatches) {
|
|
202
|
+
if (mismatch.satisfied !== false)
|
|
203
|
+
continue;
|
|
204
|
+
const optional = isOptionalPeer(profileDirectory, mismatch.plugin, mismatch.name);
|
|
205
|
+
const verdict = classifyPeer(mismatch.plugin, mismatch.name, mismatch.range, mismatch.resolved, optional);
|
|
206
|
+
if (verdict.kind === 'risk')
|
|
207
|
+
risks.push(verdict.risk);
|
|
208
|
+
else if (verdict.kind === 'warning')
|
|
209
|
+
warnings.push(verdict.warning);
|
|
210
|
+
}
|
|
211
|
+
return { risks, warnings, duplicateNames: report.duplicateNames };
|
|
212
|
+
}
|
|
213
|
+
function riskId(risk) {
|
|
214
|
+
return `${risk.plugin}\u0000${risk.peer}\u0000${risk.direction}`;
|
|
215
|
+
}
|
|
216
|
+
/** Risks present after a mutation but absent before it. */
|
|
217
|
+
export function introducedRisks(before, after) {
|
|
218
|
+
const seen = new Set(before.risks.map(riskId));
|
|
219
|
+
return after.risks.filter(risk => !seen.has(riskId(risk)));
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Cross-layer name collisions present after a mutation but absent before it
|
|
223
|
+
* (#230 by @dxc-dxc).
|
|
224
|
+
*
|
|
225
|
+
* Keyed by NAME alone, not by the layer set: a collision the operation made
|
|
226
|
+
* worse — same name, now shadowing across one more layer — is still the same
|
|
227
|
+
* collision the profile already had, and re-reporting it would put the
|
|
228
|
+
* operator back in front of a problem they did not just cause.
|
|
229
|
+
*
|
|
230
|
+
* This is what makes surfacing these safe at all. The underlying
|
|
231
|
+
* `duplicateNames` is informational precisely because a healthy-but-messy
|
|
232
|
+
* profile can carry collisions indefinitely; only the newly introduced ones
|
|
233
|
+
* are attributable to the install that just ran, and therefore undoable by
|
|
234
|
+
* rolling it back.
|
|
235
|
+
*/
|
|
236
|
+
export function introducedDuplicateNames(before, after) {
|
|
237
|
+
const seen = new Set(before.duplicateNames.map(entry => entry.name));
|
|
238
|
+
return after.duplicateNames.filter(entry => !seen.has(entry.name));
|
|
239
|
+
}
|
|
240
|
+
/** Convenience wrapper matching the profile helper signature. */
|
|
241
|
+
export function assessProfile(profile, explicitDir) {
|
|
242
|
+
return assessCompatibility(profileDir(profile, explicitDir));
|
|
243
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* crate 目录适配层。
|
|
3
|
+
*
|
|
4
|
+
* crate 的 dsh-crate.json 与市场内部使用的 Registry 是两份不同的契约:
|
|
5
|
+
* 前者用单字母字段(i/n/d/s/t/vr/cmd)描述条目,后者要求 category、owner、
|
|
6
|
+
* description 等结构化字段。本模块只做一次单向映射,并把外部输入的校验收在
|
|
7
|
+
* 这里;抓取、重试与缓存仍留在 registry.ts。
|
|
8
|
+
*
|
|
9
|
+
* 字段对应关系:
|
|
10
|
+
* - `i` owner/repo,同时决定展示页与兜底安装源
|
|
11
|
+
* - `n` 展示名(市场内部作为 name 使用)
|
|
12
|
+
* - `d` 描述(crate 只提供一份文本,中英两栏共用)
|
|
13
|
+
* - `s` star 数
|
|
14
|
+
* - `vr` 最新版本号
|
|
15
|
+
* - `t` 条目类型,映射成市场分类 id
|
|
16
|
+
* - `cmd` 源站给出的安装命令;其中若是普通 npm 包名,同时作为 npm 字段
|
|
17
|
+
*/
|
|
18
|
+
import { NPM_NAME_RE } from './sources.js';
|
|
19
|
+
/** crate 条目类型到市场分类 id 的映射;未知类型一律归入 other。 */
|
|
20
|
+
const TYPE_TO_CATEGORY = {
|
|
21
|
+
'cordis-plugin': 'plugin',
|
|
22
|
+
skill: 'skill',
|
|
23
|
+
theme: 'theme',
|
|
24
|
+
};
|
|
25
|
+
/** 分类的本地化标签;主题分类让插件端的主题页有内容可筛。 */
|
|
26
|
+
const CATEGORY_LABELS = {
|
|
27
|
+
plugin: { zh: '插件', en: 'Plugins' },
|
|
28
|
+
skill: { zh: '技能', en: 'Skills' },
|
|
29
|
+
theme: { zh: '主题', en: 'Themes' },
|
|
30
|
+
other: { zh: '其他', en: 'Other' },
|
|
31
|
+
};
|
|
32
|
+
/** 分类字典的输出顺序:插件、技能、主题、其他。 */
|
|
33
|
+
const CATEGORY_ORDER = ['plugin', 'skill', 'theme', 'other'];
|
|
34
|
+
/** `i` 字段的唯一合法形态:owner/repo。 */
|
|
35
|
+
const REPO_PATH_RE = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u;
|
|
36
|
+
/** 安装命令的前缀,源站与本地补全都按它拼接。 */
|
|
37
|
+
const INSTALL_PREFIX = 'dsh plugin --profile web add ';
|
|
38
|
+
/** 安装命令里分隔「参数」与「安装源」的词。 */
|
|
39
|
+
const ADD_TOKEN = ' add ';
|
|
40
|
+
/**
|
|
41
|
+
* 判断一份 JSON 是否为 crate 目录。
|
|
42
|
+
*
|
|
43
|
+
* 只用结构特征,不依赖某个版本号:带 `entries` 数组且没有 `plugins` 数组的
|
|
44
|
+
* 就是 crate 形态,旧目录照常走 `asRegistry`,两套夹具互不干扰。
|
|
45
|
+
*/
|
|
46
|
+
export function isCrateCatalog(value) {
|
|
47
|
+
if (typeof value !== 'object' || value === null)
|
|
48
|
+
return false;
|
|
49
|
+
const data = value;
|
|
50
|
+
return Array.isArray(data.entries) && !Array.isArray(data.plugins);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 把 crate 目录映射成市场内部的 Registry。
|
|
54
|
+
*
|
|
55
|
+
* @param value - 已经 `JSON.parse` 过的载荷。
|
|
56
|
+
* @returns 条目已校验、分类已归一化的目录。
|
|
57
|
+
* @throws 载荷不是对象、缺少 entries、或没有一条可用条目时抛出。
|
|
58
|
+
*/
|
|
59
|
+
export function asCrateRegistry(value) {
|
|
60
|
+
if (typeof value !== 'object' || value === null)
|
|
61
|
+
throw new Error('crate 目录不是对象');
|
|
62
|
+
const data = value;
|
|
63
|
+
if (!Array.isArray(data.entries))
|
|
64
|
+
throw new Error('crate 目录缺少 entries 数组');
|
|
65
|
+
const plugins = [];
|
|
66
|
+
for (const entry of data.entries) {
|
|
67
|
+
const plugin = asCratePlugin(entry);
|
|
68
|
+
if (plugin !== null)
|
|
69
|
+
plugins.push(plugin);
|
|
70
|
+
}
|
|
71
|
+
if (plugins.length === 0)
|
|
72
|
+
throw new Error('crate 目录里没有一条可用条目');
|
|
73
|
+
return {
|
|
74
|
+
updated: typeof data.generatedAt === 'string' ? data.generatedAt : '',
|
|
75
|
+
count: plugins.length,
|
|
76
|
+
categories: categoriesOf(plugins),
|
|
77
|
+
plugins,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 单条 crate 条目转成 RegistryPlugin。
|
|
82
|
+
*
|
|
83
|
+
* @param entry - entries 数组里的一项,来源不可信,逐字段校验。
|
|
84
|
+
* @returns 映射结果;缺少仓库路径或展示名时返回 null,由调用方跳过。
|
|
85
|
+
*/
|
|
86
|
+
export function asCratePlugin(entry) {
|
|
87
|
+
if (typeof entry !== 'object' || entry === null)
|
|
88
|
+
return null;
|
|
89
|
+
const row = entry;
|
|
90
|
+
const repo = typeof row.i === 'string' ? row.i.trim() : '';
|
|
91
|
+
if (!REPO_PATH_RE.test(repo))
|
|
92
|
+
return null;
|
|
93
|
+
const name = typeof row.n === 'string' ? row.n.trim() : '';
|
|
94
|
+
if (name === '')
|
|
95
|
+
return null;
|
|
96
|
+
const npm = npmFromCommand(row.cmd);
|
|
97
|
+
const text = typeof row.d === 'string' ? row.d.trim() : '';
|
|
98
|
+
return {
|
|
99
|
+
name,
|
|
100
|
+
owner: repo.slice(0, repo.indexOf('/')),
|
|
101
|
+
url: `https://github.com/${repo}`,
|
|
102
|
+
category: categoryOf(row.t),
|
|
103
|
+
description: { zh: text, en: text },
|
|
104
|
+
npm,
|
|
105
|
+
stars: starCountOf(row.s),
|
|
106
|
+
version: typeof row.vr === 'string' && row.vr.trim() !== '' ? row.vr.trim() : null,
|
|
107
|
+
icon: null,
|
|
108
|
+
install: installCommandOf(row.cmd, npm, repo),
|
|
109
|
+
added: '',
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 条目类型转分类 id。
|
|
114
|
+
*
|
|
115
|
+
* @param type - crate 的 `t` 字段,可能是任何类型。
|
|
116
|
+
*/
|
|
117
|
+
export function categoryOf(type) {
|
|
118
|
+
if (typeof type !== 'string')
|
|
119
|
+
return 'other';
|
|
120
|
+
return TYPE_TO_CATEGORY[type] ?? 'other';
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 从安装命令里取出 npm 包名。
|
|
124
|
+
*
|
|
125
|
+
* 命令形如 `dsh plugin --profile web add @scope/name@1.2.3`;版本号与 tag 都要
|
|
126
|
+
* 去掉,带变量的地址(`github:...#${DSH_PLUGIN_REF}`)和本地绝对路径都不是
|
|
127
|
+
* npm 包名,返回 null 让安装回落到 GitHub 源。
|
|
128
|
+
*
|
|
129
|
+
* @param command - crate 的 `cmd` 字段,可能是任何类型。
|
|
130
|
+
* @returns 合法 npm 包名,或 null。
|
|
131
|
+
*/
|
|
132
|
+
export function npmFromCommand(command) {
|
|
133
|
+
if (typeof command !== 'string')
|
|
134
|
+
return null;
|
|
135
|
+
const index = command.indexOf(ADD_TOKEN);
|
|
136
|
+
if (index < 0)
|
|
137
|
+
return null;
|
|
138
|
+
const spec = stripSpecVersion(command.slice(index + ADD_TOKEN.length).trim());
|
|
139
|
+
return NPM_NAME_RE.test(spec) ? spec : null;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 去掉安装源末尾的 `@版本` 或 `@tag`。
|
|
143
|
+
*
|
|
144
|
+
* 从下标 1 开始查找,避免把作用域包名开头的 `@` 当成版本分隔符。
|
|
145
|
+
*
|
|
146
|
+
* @param spec - 安装源,例如 `@scope/name@1.2.3`。
|
|
147
|
+
*/
|
|
148
|
+
export function stripSpecVersion(spec) {
|
|
149
|
+
const at = spec.indexOf('@', 1);
|
|
150
|
+
return at === -1 ? spec : spec.slice(0, at);
|
|
151
|
+
}
|
|
152
|
+
/** 卡片上展示的安装命令:优先用源站给的,缺省时按 npm / GitHub 源补全。 */
|
|
153
|
+
function installCommandOf(command, npm, repo) {
|
|
154
|
+
if (typeof command === 'string' && command.trim() !== '')
|
|
155
|
+
return command.trim();
|
|
156
|
+
return `${INSTALL_PREFIX}${npm ?? `github:${repo}`}`;
|
|
157
|
+
}
|
|
158
|
+
/** star 数只接受有限数字,其余一律当作「未知」而不是 0。 */
|
|
159
|
+
function starCountOf(value) {
|
|
160
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
|
161
|
+
}
|
|
162
|
+
/** 按固定顺序收集实际出现过的分类,供筛选栏渲染。 */
|
|
163
|
+
function categoriesOf(plugins) {
|
|
164
|
+
const used = new Set();
|
|
165
|
+
for (const plugin of plugins) {
|
|
166
|
+
const category = plugin.category;
|
|
167
|
+
if (typeof category === 'string')
|
|
168
|
+
used.add(category);
|
|
169
|
+
else
|
|
170
|
+
for (const id of category)
|
|
171
|
+
used.add(id);
|
|
172
|
+
}
|
|
173
|
+
const categories = {};
|
|
174
|
+
for (const id of CATEGORY_ORDER) {
|
|
175
|
+
if (used.has(id))
|
|
176
|
+
categories[id] = CATEGORY_LABELS[id];
|
|
177
|
+
}
|
|
178
|
+
return categories;
|
|
179
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Versioned, read-only diagnostics shared by the host route and client. */
|
|
2
|
+
export const DIAGNOSTIC_SCHEMA = 'dsh-crate/diagnostics/v1';
|
|
3
|
+
/** Conservative first set of known, identity-sensitive host contracts. */
|
|
4
|
+
export const KNOWN_SHARED_HOST_PACKAGES = [
|
|
5
|
+
'@deepseek-ai/cordis',
|
|
6
|
+
'@deepseek-ai/dsh-attachment',
|
|
7
|
+
'@deepseek-ai/dsh-llm',
|
|
8
|
+
'@deepseek-ai/dsh-system-prompt',
|
|
9
|
+
'@deepseek-ai/dsh-tools',
|
|
10
|
+
];
|
|
11
|
+
const knownSharedHostPackages = new Set(KNOWN_SHARED_HOST_PACKAGES);
|
|
12
|
+
function isRecord(value) {
|
|
13
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
/** Report exact manifest declarations; the resolved dependency tree is not inspected. */
|
|
16
|
+
function inspectKnownHostDependencyDeclarations(packageName, manifest) {
|
|
17
|
+
if (!isRecord(manifest) || !isRecord(manifest.dependencies))
|
|
18
|
+
return [];
|
|
19
|
+
const findings = [];
|
|
20
|
+
for (const dependency of Object.keys(manifest.dependencies).sort()) {
|
|
21
|
+
const declaredRange = manifest.dependencies[dependency];
|
|
22
|
+
if (!knownSharedHostPackages.has(dependency) || typeof declaredRange !== 'string')
|
|
23
|
+
continue;
|
|
24
|
+
findings.push({
|
|
25
|
+
code: 'shared-host-package-dependency',
|
|
26
|
+
severity: 'warning',
|
|
27
|
+
subject: { kind: 'package', name: packageName },
|
|
28
|
+
evidence: {
|
|
29
|
+
basis: 'manifest-declaration',
|
|
30
|
+
dependency,
|
|
31
|
+
declaredRange,
|
|
32
|
+
declaredIn: 'dependencies',
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return findings;
|
|
37
|
+
}
|
|
38
|
+
/** Build a stable diagnostic envelope from installed package manifests. */
|
|
39
|
+
export function diagnosePackageManifests(packages) {
|
|
40
|
+
const sortedPackages = [...packages].sort((a, b) => a.packageName < b.packageName ? -1 : a.packageName > b.packageName ? 1 : 0);
|
|
41
|
+
return {
|
|
42
|
+
schema: DIAGNOSTIC_SCHEMA,
|
|
43
|
+
findings: sortedPackages.flatMap(({ packageName, manifest }) => {
|
|
44
|
+
if (!isRecord(manifest) || manifest.dsh === undefined)
|
|
45
|
+
return [];
|
|
46
|
+
return inspectKnownHostDependencyDeclarations(packageName, manifest);
|
|
47
|
+
}),
|
|
48
|
+
};
|
|
49
|
+
}
|