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.
Files changed (132) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +288 -2
  3. package/README.zh.md +263 -0
  4. package/UPDATE-API-V1.md +207 -0
  5. package/assets/logo.svg +37 -0
  6. package/client/client.js +18964 -0
  7. package/cordis.patch.yml +4 -0
  8. package/lib/accelerate.js +194 -0
  9. package/lib/agents.js +67 -0
  10. package/lib/backup.js +688 -0
  11. package/lib/body-progress.js +173 -0
  12. package/lib/catalog-local-match.js +160 -0
  13. package/lib/catalog-npm.js +180 -0
  14. package/lib/changelog.js +462 -0
  15. package/lib/channels.js +64 -0
  16. package/lib/check.js +1557 -0
  17. package/lib/compatibility.js +243 -0
  18. package/lib/crate-catalog.js +179 -0
  19. package/lib/diagnostics.js +49 -0
  20. package/lib/discovery-compatibility.js +447 -0
  21. package/lib/drain-journal.js +164 -0
  22. package/lib/drain.js +1043 -0
  23. package/lib/dsh-cli.js +1373 -0
  24. package/lib/dsh-install.js +259 -0
  25. package/lib/entry-identity.js +85 -0
  26. package/lib/gist.js +369 -0
  27. package/lib/groups.js +108 -0
  28. package/lib/home-paths.js +29 -0
  29. package/lib/host-recovery.js +187 -0
  30. package/lib/hot.js +871 -0
  31. package/lib/http.js +252 -0
  32. package/lib/index.js +394 -0
  33. package/lib/install.js +1147 -0
  34. package/lib/lifecycle.js +37 -0
  35. package/lib/log-redaction.js +24 -0
  36. package/lib/log.js +204 -0
  37. package/lib/ndjson.js +154 -0
  38. package/lib/net.js +174 -0
  39. package/lib/official-desktop.js +619 -0
  40. package/lib/order.js +284 -0
  41. package/lib/patch.js +943 -0
  42. package/lib/pending-rows.js +142 -0
  43. package/lib/pending-updates.js +346 -0
  44. package/lib/pnpm-compat.js +972 -0
  45. package/lib/presets.js +282 -0
  46. package/lib/probe-context.js +20 -0
  47. package/lib/proc-text.js +69 -0
  48. package/lib/profile.js +1978 -0
  49. package/lib/recovery.js +1059 -0
  50. package/lib/region-probe.js +93 -0
  51. package/lib/regions.js +283 -0
  52. package/lib/registry.js +308 -0
  53. package/lib/release-candidates.js +115 -0
  54. package/lib/release-updates.js +70 -0
  55. package/lib/restart.js +688 -0
  56. package/lib/routes.js +7960 -0
  57. package/lib/settings.js +164 -0
  58. package/lib/shelfry-catalog.js +179 -0
  59. package/lib/snapshot.js +506 -0
  60. package/lib/source-migration.js +164 -0
  61. package/lib/sources.js +1005 -0
  62. package/lib/state.js +38 -0
  63. package/lib/store.js +163 -0
  64. package/lib/themes.js +350 -0
  65. package/lib/trial.js +125 -0
  66. package/lib/types/accelerate.d.ts +85 -0
  67. package/lib/types/agents.d.ts +52 -0
  68. package/lib/types/backup.d.ts +171 -0
  69. package/lib/types/body-progress.d.ts +94 -0
  70. package/lib/types/catalog-local-match.d.ts +39 -0
  71. package/lib/types/catalog-npm.d.ts +77 -0
  72. package/lib/types/changelog.d.ts +131 -0
  73. package/lib/types/channels.d.ts +56 -0
  74. package/lib/types/check.d.ts +353 -0
  75. package/lib/types/compatibility.d.ts +83 -0
  76. package/lib/types/crate-catalog.d.ts +48 -0
  77. package/lib/types/diagnostics.d.ts +29 -0
  78. package/lib/types/discovery-compatibility.d.ts +145 -0
  79. package/lib/types/drain-journal.d.ts +25 -0
  80. package/lib/types/drain.d.ts +306 -0
  81. package/lib/types/dsh-cli.d.ts +481 -0
  82. package/lib/types/dsh-install.d.ts +106 -0
  83. package/lib/types/entry-identity.d.ts +68 -0
  84. package/lib/types/gist.d.ts +79 -0
  85. package/lib/types/groups.d.ts +34 -0
  86. package/lib/types/home-paths.d.ts +13 -0
  87. package/lib/types/host-recovery.d.ts +95 -0
  88. package/lib/types/hot.d.ts +405 -0
  89. package/lib/types/http.d.ts +114 -0
  90. package/lib/types/index.d.ts +52 -0
  91. package/lib/types/install.d.ts +463 -0
  92. package/lib/types/lifecycle.d.ts +14 -0
  93. package/lib/types/log-redaction.d.ts +2 -0
  94. package/lib/types/log.d.ts +43 -0
  95. package/lib/types/ndjson.d.ts +52 -0
  96. package/lib/types/net.d.ts +94 -0
  97. package/lib/types/official-desktop.d.ts +59 -0
  98. package/lib/types/order.d.ts +100 -0
  99. package/lib/types/patch.d.ts +218 -0
  100. package/lib/types/pending-rows.d.ts +81 -0
  101. package/lib/types/pending-updates.d.ts +161 -0
  102. package/lib/types/pnpm-compat.d.ts +102 -0
  103. package/lib/types/presets.d.ts +94 -0
  104. package/lib/types/probe-context.d.ts +14 -0
  105. package/lib/types/proc-text.d.ts +29 -0
  106. package/lib/types/profile.d.ts +551 -0
  107. package/lib/types/recovery.d.ts +342 -0
  108. package/lib/types/region-probe.d.ts +50 -0
  109. package/lib/types/regions.d.ts +134 -0
  110. package/lib/types/registry.d.ts +160 -0
  111. package/lib/types/release-candidates.d.ts +42 -0
  112. package/lib/types/release-updates.d.ts +15 -0
  113. package/lib/types/restart.d.ts +279 -0
  114. package/lib/types/routes.d.ts +130 -0
  115. package/lib/types/settings.d.ts +112 -0
  116. package/lib/types/shelfry-catalog.d.ts +48 -0
  117. package/lib/types/snapshot.d.ts +93 -0
  118. package/lib/types/source-migration.d.ts +100 -0
  119. package/lib/types/sources.d.ts +391 -0
  120. package/lib/types/state.d.ts +20 -0
  121. package/lib/types/store.d.ts +47 -0
  122. package/lib/types/themes.d.ts +46 -0
  123. package/lib/types/trial.d.ts +61 -0
  124. package/lib/types/update-api-v1.d.ts +83 -0
  125. package/lib/types/updates.d.ts +273 -0
  126. package/lib/types/verify.d.ts +277 -0
  127. package/lib/update-api-v1.js +273 -0
  128. package/lib/updates.js +981 -0
  129. package/lib/verify.js +904 -0
  130. package/locale/en.json +5 -0
  131. package/locale/zh.json +6 -0
  132. package/package.json +113 -4
@@ -0,0 +1,164 @@
1
+ /**
2
+ * The market's own settings namespace: the half that makes `allowRestart`
3
+ * a switch on the plugin configuration page instead of a line the user has
4
+ * to hand-write into cordis.yml.
5
+ *
6
+ * `allowRestart: false` is the documented answer for a host owned by
7
+ * systemd, launchd or pm2 — a supervisor restarts it, so the market's
8
+ * one-click restart must not launch a second one. Until now the only way to
9
+ * say that was editing YAML in the right place with the right indentation,
10
+ * where a stray space stops the profile booting.
11
+ *
12
+ * Only `allowRestart` is exposed. `profile` names which profile this
13
+ * instance manages: it is decided at mount from the composition or the
14
+ * command line, and a running instance cannot switch to another one, so
15
+ * offering it as a field would promise something the write cannot deliver.
16
+ *
17
+ * `buildEnv` (issue #336) is deliberately NOT here. It is a map, so it
18
+ * wants a form, and the market's own settings card is the form that renders
19
+ * one — editing it there persists through the market's state.json and
20
+ * reaches children through spawnEnv. Registering it here as well would make
21
+ * this namespace a SECOND writer for a value that already has an editor:
22
+ * the settings layer and the card would overwrite each other on their next
23
+ * events, the exact shape of the channel bug documented below.
24
+ *
25
+ * The release channel is NOT here either, and that is a correction rather
26
+ * than an omission. It was, briefly, and it made this namespace a second
27
+ * writer for a value the market already stores in its own state.json: the
28
+ * mount read the user's saved channel off disk, then `onChange` assigned
29
+ * `source().channel` — which knows nothing about that file — straight back
30
+ * over it. The choice survived exactly until the next settings event.
31
+ *
32
+ * Only a real host could show that; the unit lane mounts the routes without
33
+ * this layer at all. `allowRestart` needs this door because its only other
34
+ * one is hand-edited YAML. The channel has a control of its own on the
35
+ * plugin configuration page, so a second door bought nothing and cost the
36
+ * setting its memory.
37
+ *
38
+ * The wiring rides the scoped fiber, so a host with no settings service —
39
+ * every dsh before 0.1.0-rc.7 — simply never runs any of this and the entry
40
+ * configuration stands as composed. That is why this needs no version check
41
+ * of its own.
42
+ *
43
+ * It depends on the SERVICE and nothing else, which is the whole point of
44
+ * the shape below. This module used to import two convenience helpers,
45
+ * `installSettingsSection` and `settingsNamespace`, from
46
+ * `@deepseek-ai/dsh-settings`. dsh 0.1.2-alpha.1 deleted both — and a
47
+ * missing NAMED EXPORT is not a missing service. `ctx.inject` degrades
48
+ * quietly; an ESM named import that resolves to nothing is a SyntaxError at
49
+ * module evaluation, which cordis's loader reports as a failed entry and the
50
+ * host exits 1. Installing the market stopped the host from booting at all:
51
+ *
52
+ * SyntaxError: The requested module '@deepseek-ai/dsh-settings' does not
53
+ * provide an export named 'installSettingsSection'
54
+ *
55
+ * The service itself did not change then — `sctx.settings.register(ns,
56
+ * schema, { base })` is identical in 0.1.0-rc.7 and 0.1.2-alpha.2. Only the
57
+ * two wrappers went away. So this inlines what the wrapper did (verified
58
+ * against its source: an inject, a register, a watch, and an unload effect)
59
+ * and validates the namespace here.
60
+ *
61
+ * It DID change in 0.1.7 (#677): `SettingsService` there has `describe` and
62
+ * `update` and no `register` — namespaces are derived from a plugin's Config
63
+ * schema instead. The `settings` service still exists, so the inject callback
64
+ * runs and `register` threw a TypeError that cordis swallowed. Both entry
65
+ * points now check for the method and, without it, leave the composed entry
66
+ * standing and say so once in the host log. That is a stop-gap, not the
67
+ * migration: on 0.1.7 the allowRestart switch is absent until the market
68
+ * moves to the new model.
69
+ */
70
+ import z from '@deepseek-ai/schemastery';
71
+ import { restartAllowed } from './restart.js';
72
+ /**
73
+ * The namespace pattern `settingsNamespace` enforced before it was removed.
74
+ * Kept as a literal check rather than an import for the reason above.
75
+ */
76
+ const NAMESPACE_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
77
+ /** Namespace the card on the browser side keys itself to. */
78
+ export const MARKET_SETTINGS_NS = 'dsh-crate';
79
+ let namespaceState = 'pending';
80
+ /** The state as of the last attempt to register the namespace. */
81
+ export function settingsNamespaceState() {
82
+ return namespaceState;
83
+ }
84
+ if (!NAMESPACE_PATTERN.test(MARKET_SETTINGS_NS)) {
85
+ throw new TypeError(`settings namespace "${MARKET_SETTINGS_NS}" must match ${String(NAMESPACE_PATTERN)}`);
86
+ }
87
+ export const MarketSettings = z.object({
88
+ allowRestart: z.boolean().default(true),
89
+ });
90
+ /**
91
+ * Whether this host's settings service still has the pre-0.1.7 `register`.
92
+ *
93
+ * @param service - the injected `settings` service.
94
+ * @param ctx - for the one-line explanation when it does not.
95
+ * @returns true when `register` can be called.
96
+ */
97
+ function canRegister(service, ctx) {
98
+ if (typeof service.register === 'function')
99
+ return true;
100
+ namespaceState = 'unsupported-by-host';
101
+ const logger = ctx.logger;
102
+ try {
103
+ logger?.('dsh-crate').warn('this host\'s settings service has no register() (dsh 0.1.7 derives settings from a plugin Config schema); '
104
+ + 'the market keeps its composed configuration and shows no allowRestart switch on this host yet (#677)');
105
+ }
106
+ catch { /* a logger is a nicety here, not a dependency */ }
107
+ return false;
108
+ }
109
+ /** Serve the Desktop card without claiming settings-controlled restart. */
110
+ export function installDesktopMarketSettings(ctx) {
111
+ ctx.inject(['settings'], (scopedCtx) => {
112
+ const scoped = scopedCtx;
113
+ if (!canRegister(scoped.settings, scoped))
114
+ return;
115
+ // The host dispatches cards only for registered namespaces. An empty
116
+ // schema offers no fields; old stored allowRestart values stay untouched
117
+ // and are never read or watched into the shell-owned runtime config.
118
+ scoped.settings.register(MARKET_SETTINGS_NS, z.object({}), { base: {} });
119
+ namespaceState = 'registered';
120
+ });
121
+ }
122
+ /**
123
+ * Wire the namespace so a saved change reaches the routes immediately.
124
+ *
125
+ * The routes read `allowRestart` off this object on every request (the
126
+ * status route reports the capability, the restart route enforces it), so
127
+ * updating it in place is what makes a toggle take effect without a
128
+ * restart — which would be a poor thing to require of a setting whose whole
129
+ * subject is restarting.
130
+ *
131
+ * @param ctx - the plugin context owning the wiring.
132
+ * @param resolved - the live config object the routes read.
133
+ */
134
+ export function installMarketSettings(ctx, resolved) {
135
+ // The switch must show what the routes will actually DO, which since #229
136
+ // is not simply "unset means on": under a detected supervisor an unset
137
+ // value means off, because the supervisor owns restarts. Asking
138
+ // restartAllowed() rather than re-deriving it here is what keeps the two
139
+ // from drifting — a switch showing On beside a hidden button is the same
140
+ // class of confusion the detection exists to end.
141
+ const entry = { allowRestart: restartAllowed(resolved) };
142
+ let source = () => entry;
143
+ // Assigns ONLY what this namespace owns. Writing back a field the market
144
+ // stores elsewhere is how the channel lost its memory.
145
+ const apply = () => { resolved.allowRestart = source().allowRestart; };
146
+ // `inject` is the graceful-degradation boundary: on a host with no
147
+ // settings service the callback never runs and the composed entry stands.
148
+ ctx.inject(['settings'], (scopedCtx) => {
149
+ const scoped = scopedCtx;
150
+ if (!canRegister(scoped.settings, scoped))
151
+ return;
152
+ const scope = scoped.settings.register(MARKET_SETTINGS_NS, MarketSettings, { base: entry });
153
+ namespaceState = 'registered';
154
+ source = () => scope.get();
155
+ // Unload restores the composed entry, so a disabled section cannot leave
156
+ // the routes reading a value nobody can see or change any more.
157
+ scoped.effect(() => () => {
158
+ source = () => entry;
159
+ apply();
160
+ });
161
+ apply();
162
+ scope.watch(apply);
163
+ });
164
+ }
@@ -0,0 +1,179 @@
1
+ /**
2
+ * shelfry 目录适配层。
3
+ *
4
+ * shelfry 的 dsh-shelfry.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` 描述(shelfry 只提供一份文本,中英两栏共用)
13
+ * - `s` star 数
14
+ * - `vr` 最新版本号
15
+ * - `t` 条目类型,映射成市场分类 id
16
+ * - `cmd` 源站给出的安装命令;其中若是普通 npm 包名,同时作为 npm 字段
17
+ */
18
+ import { NPM_NAME_RE } from './sources.js';
19
+ /** shelfry 条目类型到市场分类 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 是否为 shelfry 目录。
42
+ *
43
+ * 只用结构特征,不依赖某个版本号:带 `entries` 数组且没有 `plugins` 数组的
44
+ * 就是 shelfry 形态,旧目录照常走 `asRegistry`,两套夹具互不干扰。
45
+ */
46
+ export function isShelfryCatalog(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
+ * 把 shelfry 目录映射成市场内部的 Registry。
54
+ *
55
+ * @param value - 已经 `JSON.parse` 过的载荷。
56
+ * @returns 条目已校验、分类已归一化的目录。
57
+ * @throws 载荷不是对象、缺少 entries、或没有一条可用条目时抛出。
58
+ */
59
+ export function asShelfryRegistry(value) {
60
+ if (typeof value !== 'object' || value === null)
61
+ throw new Error('shelfry 目录不是对象');
62
+ const data = value;
63
+ if (!Array.isArray(data.entries))
64
+ throw new Error('shelfry 目录缺少 entries 数组');
65
+ const plugins = [];
66
+ for (const entry of data.entries) {
67
+ const plugin = asShelfryPlugin(entry);
68
+ if (plugin !== null)
69
+ plugins.push(plugin);
70
+ }
71
+ if (plugins.length === 0)
72
+ throw new Error('shelfry 目录里没有一条可用条目');
73
+ return {
74
+ updated: typeof data.generatedAt === 'string' ? data.generatedAt : '',
75
+ count: plugins.length,
76
+ categories: categoriesOf(plugins),
77
+ plugins,
78
+ };
79
+ }
80
+ /**
81
+ * 单条 shelfry 条目转成 RegistryPlugin。
82
+ *
83
+ * @param entry - entries 数组里的一项,来源不可信,逐字段校验。
84
+ * @returns 映射结果;缺少仓库路径或展示名时返回 null,由调用方跳过。
85
+ */
86
+ export function asShelfryPlugin(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 - shelfry 的 `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 - shelfry 的 `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
+ }