dshmarket 1.2.2 → 1.2.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/routes.js CHANGED
@@ -1,426 +1,24 @@
1
1
  /**
2
- * HTTP routes bridging the browser market UI to the host: registry fallback,
3
- * installed-plugin listing, and the install executor.
2
+ * HTTP routes bridging the browser market UI to the host. This layer only
3
+ * parses requests, calls the service modules, and serializes responses —
4
+ * process spawning lives in dsh-cli.ts, filesystem reads in profile.ts,
5
+ * orchestration in install.ts / themes.ts / updates.ts.
4
6
  *
5
7
  * Security: the install route executes a shell command, so it accepts only
6
8
  * same-origin POSTs and only sources present in the curated registry.
7
9
  */
8
- import { spawn } from 'node:child_process';
9
- import { existsSync, readdirSync, readFileSync } from 'node:fs';
10
- import { homedir } from 'node:os';
11
- import { dirname, join, resolve } from 'node:path';
10
+ import { readFileSync } from 'node:fs';
12
11
  import { loadRegistry } from './registry.js';
13
- import { cleanHotDir, hotMount, hotUnmount, listHotMounts, mountClientOnlyDeps, readDisabledThemes, writeDisabledThemes, } from './hot.js';
12
+ import { cleanHotDir, hotMount, hotUnmount, listHotMounts, mountClientOnlyDeps, readDisabledThemes, } from './hot.js';
14
13
  import { exportLogs, logEvent } from './log.js';
14
+ import { BOOT_ID, probePnpm, progress, provisionPnpm, runDshPlugin } from './dsh-cli.js';
15
+ import { profileDir, readInstalled, readInstalledVersion, readLockCommits } from './profile.js';
16
+ import { findInstalledAlias, installTargetFor } from './sources.js';
17
+ import { isStaleUpdate, retargetCollections, validateAddedPlugins, withHoistRecovery } from './install.js';
18
+ import { checkUpdates, invalidateUpdates } from './updates.js';
19
+ import { createThemeManager } from './themes.js';
20
+ import { readJsonBody, sameOrigin, sendJson } from './http.js';
15
21
  const PROFILE_RE = /^[A-Za-z0-9_-]+$/;
16
- const REPO_RE = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
17
- const INSTALL_TIMEOUT_MS = 5 * 60 * 1000;
18
- /**
19
- * Argv re-invoking the CLI that launched this host process, so installs work
20
- * whether dsh runs from a global bin, a local install, or repo source
21
- * (`node --import tsx/esm .../bin.ts`). Falls back to a PATH `dsh`.
22
- *
23
- * Installs run through node:child_process, not ctx.shell: the shell service is
24
- * the agent's sandboxed executor and denies writes to the profile directory.
25
- */
26
- function dshArgv() {
27
- const entry = process.argv[1];
28
- if (entry !== undefined && /[\\/](?:bin\.(?:js|ts)|dsh)$/.test(entry)) {
29
- // Absolute paths are required: source launches (`pnpm dsh`) pass a
30
- // relative entry, which the child resolves against its OWN cwd and dies
31
- // with MODULE_NOT_FOUND (#13). cwd near the entry keeps execArgv imports
32
- // (tsx/esm) resolvable on source launches.
33
- const abs = resolve(entry);
34
- return { file: process.execPath, args: [...process.execArgv, abs], cwd: dirname(abs), viaShell: false };
35
- }
36
- // Bare `dsh` is a .cmd shim on Windows that only a shell can start (#13).
37
- return { file: 'dsh', args: [], cwd: undefined, viaShell: winCmdShim };
38
- }
39
- /** Whether `pnpm` resolves on PATH; success is cached, absence is re-probed. */
40
- let pnpmReady = false;
41
- /**
42
- * Windows npm/corepack/pnpm are `.cmd` shims. Node's `spawn` without a shell
43
- * cannot start them (ENOENT / EINVAL). Same pattern as dsh's `plugin` forwarder.
44
- */
45
- const winCmdShim = process.platform === 'win32';
46
- /**
47
- * Kill a spawned child and, on Windows, its whole process tree — `kill()`
48
- * there only terminates the wrapper, leaving pnpm children running.
49
- * (Contributed in #7 by @mraing.)
50
- */
51
- function killChild(child) {
52
- if (process.platform === 'win32' && child.pid !== undefined) {
53
- try {
54
- spawn('taskkill', ['/pid', String(child.pid), '/t', '/f'], { stdio: 'ignore' });
55
- return;
56
- }
57
- catch { /* fall through */ }
58
- }
59
- child.kill('SIGKILL');
60
- }
61
- function probePnpm() {
62
- if (pnpmReady)
63
- return Promise.resolve(true);
64
- return new Promise((resolvePromise) => {
65
- const child = spawn('pnpm', ['--version'], { stdio: 'ignore', shell: winCmdShim });
66
- child.on('error', () => resolvePromise(false));
67
- child.on('close', (code) => {
68
- pnpmReady = code === 0;
69
- resolvePromise(pnpmReady);
70
- });
71
- });
72
- }
73
- function runQuiet(file, args, timeoutMs) {
74
- return new Promise((resolvePromise) => {
75
- const child = spawn(file, args, {
76
- env: { ...process.env, CI: 'true' },
77
- stdio: ['ignore', 'pipe', 'pipe'],
78
- shell: winCmdShim,
79
- });
80
- let output = '';
81
- const timer = setTimeout(() => killChild(child), timeoutMs);
82
- const collect = (chunk) => { output = (output + chunk.toString()).slice(-8 * 1024); };
83
- child.stdout.on('data', collect);
84
- child.stderr.on('data', collect);
85
- child.on('error', (error) => { clearTimeout(timer); resolvePromise({ code: 127, output: error.message }); });
86
- child.on('close', (code) => { clearTimeout(timer); resolvePromise({ code, output }); });
87
- });
88
- }
89
- /**
90
- * Provision pnpm without user involvement: corepack (ships with Node) first,
91
- * a global npm install as fallback.
92
- * @returns true when `pnpm --version` succeeds afterwards.
93
- */
94
- async function provisionPnpm() {
95
- const corepack = await runQuiet('corepack', ['enable', 'pnpm'], 60 * 1000);
96
- logEvent(corepack.code === 0 ? 'info' : 'warn', 'setup-pnpm', `corepack enable: exit=${String(corepack.code)} ${corepack.output.slice(-200)}`);
97
- if (await probePnpm())
98
- return true;
99
- const npm = await runQuiet('npm', ['install', '-g', 'pnpm'], 3 * 60 * 1000);
100
- logEvent(npm.code === 0 ? 'info' : 'error', 'setup-pnpm', `npm -g: exit=${String(npm.code)} ${npm.output.slice(-200)}`);
101
- return probePnpm();
102
- }
103
- const progress = { active: false, target: '', startedAt: 0, lastLine: '' };
104
- /** Identifies this host process; the client scopes its pending-restart flags to it. */
105
- const BOOT_ID = `${String(process.pid)}-${String(Date.now())}`;
106
- function trackProgress(chunk) {
107
- const lines = chunk.split('\n').map(l => l.trim()).filter(l => l !== '');
108
- if (lines.length > 0)
109
- progress.lastLine = lines[lines.length - 1].slice(0, 200);
110
- }
111
- /**
112
- * Central allowlist for every spawn target, regardless of which route built
113
- * it (defense in depth on top of per-route validation — the win32 bare-dsh
114
- * fallback runs through a shell). Suggested in #16 by @anupamme.
115
- */
116
- const TARGET_RE = /^[A-Za-z0-9@:./_#+-]+$/;
117
- function runDshPlugin(profile, pluginArgs) {
118
- const { file, args, cwd, viaShell } = dshArgv();
119
- // pnpm 9 refuses to add at a workspace root without -w (#17); pnpm 10/11
120
- // accept the flag as a no-op there, so it is safe to pass always.
121
- if (pluginArgs[0] === 'add' || pluginArgs[0] === 'remove') {
122
- pluginArgs = [pluginArgs[0], '-w', ...pluginArgs.slice(1)];
123
- }
124
- const target = pluginArgs[pluginArgs.length - 1] ?? '';
125
- if (!TARGET_RE.test(target)) {
126
- logEvent('error', 'install', `unsafe plugin target rejected: ${JSON.stringify(target)}`);
127
- return Promise.resolve({ exitCode: 1, timedOut: false, stdout: '', stderr: `unsafe plugin target rejected: ${JSON.stringify(target)}` });
128
- }
129
- progress.active = true;
130
- progress.target = target;
131
- progress.startedAt = Date.now();
132
- progress.lastLine = '';
133
- return new Promise((resolvePromise) => {
134
- const child = spawn(file, [...args, 'plugin', '--profile', profile, ...pluginArgs], {
135
- cwd,
136
- // pnpm v10 blocks forever on a silent interactive prompt without a TTY
137
- // (observed on re-add over a pinned git spec); CI mode forces it to act
138
- // or fail instead of asking.
139
- env: { ...process.env, CI: 'true' },
140
- stdio: ['ignore', 'pipe', 'pipe'],
141
- shell: viaShell,
142
- });
143
- let stdout = '';
144
- let stderr = '';
145
- let timedOut = false;
146
- const timer = setTimeout(() => {
147
- timedOut = true;
148
- killChild(child);
149
- }, INSTALL_TIMEOUT_MS);
150
- child.stdout.on('data', (chunk) => {
151
- const text = chunk.toString();
152
- stdout = (stdout + text).slice(-256 * 1024);
153
- trackProgress(text);
154
- });
155
- child.stderr.on('data', (chunk) => {
156
- const text = chunk.toString();
157
- stderr = (stderr + text).slice(-64 * 1024);
158
- trackProgress(text);
159
- });
160
- child.on('error', (error) => {
161
- clearTimeout(timer);
162
- progress.active = false;
163
- resolvePromise({ exitCode: 127, timedOut: false, stdout, stderr: `${stderr}\n${error.message}` });
164
- });
165
- child.on('close', (code) => {
166
- clearTimeout(timer);
167
- progress.active = false;
168
- resolvePromise({ exitCode: code, timedOut, stdout, stderr });
169
- });
170
- });
171
- }
172
- function sendJson(response, status, payload) {
173
- response.writeHead(status, {
174
- 'cache-control': 'no-store',
175
- 'content-type': 'application/json; charset=utf-8',
176
- });
177
- response.end(JSON.stringify(payload));
178
- }
179
- function sameOrigin(request) {
180
- const origin = request.headers.origin;
181
- const host = request.headers.host;
182
- if (origin === undefined || host === undefined)
183
- return false;
184
- try {
185
- return new URL(origin).host === host;
186
- }
187
- catch {
188
- return false;
189
- }
190
- }
191
- async function readJsonBody(request) {
192
- const chunks = [];
193
- let size = 0;
194
- for await (const chunk of request) {
195
- const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
196
- size += buffer.length;
197
- if (size > 4096)
198
- throw new Error('request body too large');
199
- chunks.push(buffer);
200
- }
201
- return JSON.parse(Buffer.concat(chunks).toString('utf8'));
202
- }
203
- function profileDir(profile) {
204
- const home = process.env.DSH_HOME ?? join(homedir(), '.dsh');
205
- return join(home, 'profiles', profile);
206
- }
207
- /** Community dependencies of the profile (official in-box scope filtered out). */
208
- function readInstalled(profile) {
209
- try {
210
- const manifest = JSON.parse(readFileSync(join(profileDir(profile), 'package.json'), 'utf8'));
211
- const installed = {};
212
- for (const [name, spec] of Object.entries(manifest.dependencies ?? {})) {
213
- if (!name.startsWith('@deepseek-ai/'))
214
- installed[name] = spec;
215
- }
216
- return installed;
217
- }
218
- catch {
219
- return {};
220
- }
221
- }
222
- /** GitHub `owner/repo` for a registry URL, or null when it is not a GitHub repo URL. */
223
- /**
224
- * Parse a registry source url: a github repo, optionally with a
225
- * `/tree/<branch>/<subpath>` suffix (how the curated list links monorepo
226
- * subpackages, e.g. dsh-plugins#theme-gallery).
227
- */
228
- function parseSourceUrl(url) {
229
- const m = /^https:\/\/github\.com\/([^/]+\/[^/]+?)(?:\/tree\/[^/]+\/(.+?))?\/?$/.exec(url);
230
- if (m === null || !REPO_RE.test(m[1]))
231
- return null;
232
- const subpath = m[2] ?? null;
233
- if (subpath !== null && !/^[A-Za-z0-9_./-]+$/.test(subpath))
234
- return null;
235
- return { repo: m[1], subpath };
236
- }
237
- function repoOf(url) {
238
- return parseSourceUrl(url)?.repo ?? null;
239
- }
240
- /** Pinned commit per `owner/repo` from the profile lockfile's codeload tarball URLs. */
241
- function readLockCommits(profile) {
242
- const commits = new Map();
243
- try {
244
- const lock = readFileSync(join(profileDir(profile), 'pnpm-lock.yaml'), 'utf8');
245
- for (const m of lock.matchAll(/codeload\.github\.com\/([^/\s]+\/[^/\s]+)\/tar\.gz\/([0-9a-f]{40})/g)) {
246
- commits.set(m[1].toLowerCase(), m[2]);
247
- }
248
- }
249
- catch { /* no lockfile — no git installs to report */ }
250
- return commits;
251
- }
252
- /**
253
- * Some registry entries point at collection repos whose actual plugin lives
254
- * in a subdirectory — the root has no package.json, and pnpm installs the
255
- * bare fileset with exit 0. Detect that junk install, drop it, and re-add
256
- * each plugin subdirectory through pnpm's `#path:` selector.
257
- * @returns overall success (true when nothing needed retargeting).
258
- */
259
- /**
260
- * True when the package's declared entry artifact actually exists — github
261
- * source checkouts of build-required plugins ship no lib/, and promoting one
262
- * into the bundle layer bricks the next boot (ERR_MODULE_NOT_FOUND kills the
263
- * whole profile, #18).
264
- */
265
- function entryArtifactExists(dir) {
266
- try {
267
- const manifest = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
268
- const candidates = [];
269
- if (typeof manifest.main === 'string')
270
- candidates.push(manifest.main);
271
- const rootExport = typeof manifest.exports === 'string'
272
- ? manifest.exports
273
- : manifest.exports?.['.'];
274
- if (typeof rootExport === 'string')
275
- candidates.push(rootExport);
276
- else if (rootExport !== null && typeof rootExport === 'object') {
277
- for (const value of Object.values(rootExport))
278
- if (typeof value === 'string')
279
- candidates.push(value);
280
- }
281
- if (candidates.length === 0)
282
- candidates.push('index.js');
283
- return candidates.some(rel => existsSync(join(dir, rel)));
284
- }
285
- catch {
286
- return false;
287
- }
288
- }
289
- /** True when the installed package's manifest declares a dsh plugin surface. */
290
- function hasDshManifest(dir) {
291
- try {
292
- const manifest = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
293
- return manifest.dsh !== undefined;
294
- }
295
- catch {
296
- return false;
297
- }
298
- }
299
- /** Plugin subdirectories (depth 2) of a collection checkout, as relative paths. */
300
- function pluginSubdirs(root) {
301
- const found = [];
302
- let level1 = [];
303
- try {
304
- level1 = readdirSync(root, { withFileTypes: true })
305
- .filter(dirent => dirent.isDirectory() && /^[A-Za-z0-9_.-]+$/.test(dirent.name) && dirent.name !== 'node_modules')
306
- .map(dirent => dirent.name);
307
- }
308
- catch {
309
- return found;
310
- }
311
- for (const sub of level1) {
312
- if (hasDshManifest(join(root, sub))) {
313
- found.push(sub);
314
- continue;
315
- }
316
- try {
317
- for (const inner of readdirSync(join(root, sub), { withFileTypes: true })) {
318
- if (!inner.isDirectory() || !/^[A-Za-z0-9_.-]+$/.test(inner.name) || inner.name === 'node_modules')
319
- continue;
320
- if (hasDshManifest(join(root, sub, inner.name)))
321
- found.push(`${sub}/${inner.name}`);
322
- }
323
- }
324
- catch { /* unreadable level — skip */ }
325
- if (found.length >= 8)
326
- break;
327
- }
328
- return found.slice(0, 8);
329
- }
330
- async function retargetCollections(profile, before, target) {
331
- if (!target.startsWith('github:'))
332
- return true;
333
- // A collection checkout: no root package.json at all, or a root manifest
334
- // that declares no dsh surface (workspace roots are usually also private
335
- // with no entry point — #18).
336
- const junk = Object.keys(readInstalled(profile)).filter((name) => {
337
- if (before.has(name))
338
- return false;
339
- const root = join(profileDir(profile), 'node_modules', name);
340
- if (!existsSync(join(root, 'package.json')))
341
- return true;
342
- return !hasDshManifest(root);
343
- });
344
- let allOk = true;
345
- for (const name of junk) {
346
- const root = join(profileDir(profile), 'node_modules', name);
347
- const candidates = pluginSubdirs(root);
348
- logEvent('info', 'install', `${name}: collection repo (root declares no dsh manifest); plugins inside: ${candidates.join(', ') || 'none'}`);
349
- await runDshPlugin(profile, ['remove', name]);
350
- if (candidates.length === 0) {
351
- allOk = false;
352
- continue;
353
- }
354
- for (const sub of candidates) {
355
- const result = await runDshPlugin(profile, ['add', `${target}#path:/${sub}`]);
356
- if (result.exitCode !== 0 || result.timedOut) {
357
- allOk = false;
358
- logEvent('error', 'install', `${target}#path:/${sub}: exit=${String(result.exitCode)}${result.timedOut ? ' TIMEOUT' : ''} — ${(result.stderr || result.stdout).slice(-220)}`);
359
- }
360
- }
361
- }
362
- return allOk;
363
- }
364
- function readInstalledVersion(profile, name) {
365
- try {
366
- const manifest = JSON.parse(readFileSync(join(profileDir(profile), 'node_modules', name, 'package.json'), 'utf8'));
367
- return manifest.version ?? null;
368
- }
369
- catch {
370
- return null;
371
- }
372
- }
373
- const UPDATES_TTL_MS = 30 * 60 * 1000;
374
- let updatesCache = null;
375
- async function fetchJson(url) {
376
- const res = await fetch(url, {
377
- headers: { accept: 'application/json', 'user-agent': 'dsh-market' },
378
- signal: AbortSignal.timeout(4000),
379
- });
380
- if (!res.ok)
381
- throw new Error(`HTTP ${res.status}`);
382
- return res.json();
383
- }
384
- /** Per-plugin update checks; a failed check reports no update rather than failing the listing. */
385
- async function checkUpdates(profile, force = false) {
386
- if (!force && updatesCache && Date.now() - updatesCache.at < UPDATES_TTL_MS)
387
- return updatesCache.data;
388
- const installed = readInstalled(profile);
389
- const lockCommits = readLockCommits(profile);
390
- const result = {};
391
- await Promise.all(Object.entries(installed).map(async ([name, spec]) => {
392
- const version = readInstalledVersion(profile, name);
393
- if (spec.startsWith('link:') || spec.startsWith('file:')) {
394
- result[name] = { kind: 'linked', version, current: null, latest: null, updateAvailable: false };
395
- return;
396
- }
397
- const gh = /^(?:github:)?([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+?)(?:#.*)?$/.exec(spec);
398
- try {
399
- if (spec.startsWith('github:') && gh !== null) {
400
- const current = lockCommits.get(gh[1].toLowerCase()) ?? null;
401
- const head = (await fetchJson(`https://api.github.com/repos/${gh[1]}/commits/HEAD`));
402
- const latest = typeof head.sha === 'string' ? head.sha : null;
403
- result[name] = {
404
- kind: 'github', version, current, latest,
405
- updateAvailable: current !== null && latest !== null && current !== latest,
406
- };
407
- }
408
- else {
409
- const meta = (await fetchJson(`https://registry.npmjs.org/${encodeURIComponent(name)}/latest`));
410
- const latest = typeof meta.version === 'string' ? meta.version : null;
411
- result[name] = {
412
- kind: 'npm', version, current: version, latest,
413
- updateAvailable: version !== null && latest !== null && version !== latest,
414
- };
415
- }
416
- }
417
- catch {
418
- result[name] = { kind: spec.startsWith('github:') ? 'github' : 'npm', version, current: null, latest: null, updateAvailable: false };
419
- }
420
- }));
421
- updatesCache = { at: Date.now(), data: result };
422
- return result;
423
- }
424
22
  /**
425
23
  * Register the market's HTTP routes.
426
24
  * @param host - Acquired webServer + shell services.
@@ -434,11 +32,12 @@ export function mountMarketRoutes(host, config) {
434
32
  // Boot-time wipe: stale hot-mount inputs from a previous session must never
435
33
  // survive into a composition where the bundle layer already covers them.
436
34
  cleanHotDir(profileDir(config.profile));
35
+ // The user's persisted theme choice; activateTheme mutates and writes it.
36
+ const disabledThemes = readDisabledThemes(profileDir(config.profile));
37
+ const themes = createThemeManager(host, config.profile, disabledThemes);
437
38
  // Client-only packages (dsh.client without dsh.bundle) are invisible to the
438
39
  // bundle layer in every boot; the market shim-mounts them so their client
439
40
  // bundles are actually served.
440
- // The user's persisted theme choice; activateTheme mutates and writes it.
441
- const disabledThemes = readDisabledThemes(profileDir(config.profile));
442
41
  void mountClientOnlyDeps(host, profileDir(config.profile)).then(async (mounted) => {
443
42
  if (mounted.length > 0)
444
43
  logEvent('info', 'boot', `client-only shims mounted: ${mounted.join(', ')}`);
@@ -446,7 +45,7 @@ export function mountMarketRoutes(host, config) {
446
45
  // from get live-disabled again (bundle trees are in-memory, so the
447
46
  // disable never persists on its own).
448
47
  for (const name of disabledThemes) {
449
- if (await setEntryDisabled(name, true))
48
+ if (await themes.setEntryDisabled(name, true))
450
49
  logEvent('info', 'boot', `theme kept off: ${name}`);
451
50
  }
452
51
  });
@@ -456,92 +55,11 @@ export function mountMarketRoutes(host, config) {
456
55
  host.on?.('internal/plugin', (fiber) => {
457
56
  const name = fiber.entry?.options?.name;
458
57
  if (name !== undefined && disabledThemes.has(name))
459
- void setEntryDisabled(name, true);
58
+ void themes.setEntryDisabled(name, true);
460
59
  });
461
60
  let installing = false;
462
- /** Installed package names classified as themes by the registry's theme category. */
463
- async function installedThemeNames(profile) {
464
- const names = new Set();
465
- try {
466
- const { registry } = await loadRegistry();
467
- const themeEntries = registry.plugins.filter(p => p.category === 'theme');
468
- const themeNames = new Set(themeEntries.map(p => p.name));
469
- const themeRepos = new Set(themeEntries.map(p => repoOf(p.url)).filter((r) => r !== null).map(r => r.toLowerCase()));
470
- for (const [name, spec] of Object.entries(readInstalled(profile))) {
471
- if (themeNames.has(name)) {
472
- names.add(name);
473
- continue;
474
- }
475
- const match = /github:([^#\s]+)/.exec(String(spec).toLowerCase());
476
- if (match !== null && themeRepos.has(match[1]))
477
- names.add(name);
478
- }
479
- }
480
- catch { /* registry unavailable — nothing classifies as a theme */ }
481
- return names;
482
- }
483
- /**
484
- * Live-toggle a bundle-layer plugin through its loader entry. Bundle trees
485
- * are in-memory (write is a no-op), so this never touches any file — the
486
- * market persists the choice itself and replays it at boot.
487
- * @returns true when a matching live entry was found and updated.
488
- */
489
- async function setEntryDisabled(name, disabledFlag) {
490
- let found = false;
491
- for (const entry of host.loader.entries()) {
492
- if (entry.options.name !== name)
493
- continue;
494
- // A disable can land while the entry's init is still in flight: the
495
- // options flip but the finishing init brings the fiber up anyway, and a
496
- // plain re-update no-ops on the empty diff. Force the update and verify
497
- // the live state, retrying until reality matches the flag.
498
- for (let attempt = 0; attempt < 3; attempt++) {
499
- try {
500
- await entry.update({ disabled: disabledFlag ? true : null }, false, true);
501
- found = true;
502
- }
503
- catch (error) {
504
- logEvent('warn', 'toggle', `${name}: entry update failed — ${error instanceof Error ? error.message : String(error)}`);
505
- break;
506
- }
507
- const live = entry.fiber !== undefined;
508
- if (live !== disabledFlag)
509
- break;
510
- await new Promise(resolvePromise => setTimeout(resolvePromise, 200));
511
- }
512
- logEvent('info', 'toggle', `${name} -> ${disabledFlag ? 'off' : 'on'}: fiber=${String(entry.fiber !== undefined)}`);
513
- }
514
- if (!found)
515
- logEvent('info', 'toggle', `${name}: no loader entry matched`);
516
- return found;
517
- }
518
- /**
519
- * Make `name` the one active theme: deactivate every other installed theme
520
- * (market hot mounts unmount; bundle-layer entries live-disable) and bring
521
- * it up. The choice persists in state.json and is replayed at boot.
522
- */
523
- async function activateTheme(name) {
524
- const dir = profileDir(config.profile);
525
- const themes = await installedThemeNames(config.profile);
526
- for (const other of themes) {
527
- if (other === name)
528
- continue;
529
- if (listHotMounts().includes(other)) {
530
- await hotUnmount(other);
531
- disabledThemes.add(other);
532
- }
533
- else if (await setEntryDisabled(other, true)) {
534
- disabledThemes.add(other);
535
- }
536
- }
537
- disabledThemes.delete(name);
538
- writeDisabledThemes(dir, disabledThemes);
539
- if (listHotMounts().includes(name))
540
- return true;
541
- if (await setEntryDisabled(name, false))
542
- return true;
543
- return hotMount(host, dir, name);
544
- }
61
+ /** Every plugin command goes through the pnpm-drift recovery wrapper (#20). */
62
+ const runPlugin = (profile, args) => withHoistRecovery(runDshPlugin, profile, args);
545
63
  const disposers = [
546
64
  host.webServer.register({
547
65
  kind: 'exact',
@@ -594,12 +112,12 @@ export function mountMarketRoutes(host, config) {
594
112
  const body = (await readJsonBody(request));
595
113
  const name = typeof body.name === 'string' ? body.name : '';
596
114
  const installed = readInstalled(config.profile);
597
- const themes = await installedThemeNames(config.profile);
598
- if (installed[name] === undefined || !themes.has(name)) {
115
+ const themeNames = await themes.installedThemeNames();
116
+ if (installed[name] === undefined || !themeNames.has(name)) {
599
117
  sendJson(response, 400, { error: 'not an installed theme' });
600
118
  return;
601
119
  }
602
- const activated = await activateTheme(name);
120
+ const activated = await themes.activateTheme(name);
603
121
  logEvent(activated ? 'info' : 'error', 'use-skin', `${name}: ${activated ? 'active' : 'failed'}`);
604
122
  sendJson(response, activated ? 200 : 502, { ok: activated, live: listHotMounts() });
605
123
  }
@@ -695,6 +213,7 @@ export function mountMarketRoutes(host, config) {
695
213
  try {
696
214
  const body = (await readJsonBody(request));
697
215
  const name = typeof body.name === 'string' ? body.name : '';
216
+ const force = body.force === true;
698
217
  const spec = readInstalled(config.profile)[name];
699
218
  if (spec === undefined) {
700
219
  sendJson(response, 400, { error: 'plugin is not installed' });
@@ -713,29 +232,32 @@ export function mountMarketRoutes(host, config) {
713
232
  const beforeCommit = repoKey !== null ? readLockCommits(config.profile).get(repoKey) ?? null : null;
714
233
  installing = true;
715
234
  try {
716
- const result = await runDshPlugin(config.profile, ['add', target]);
235
+ // force: the user chose to install a fresh release without the
236
+ // default one-day safety wait; scoped to this single command.
237
+ const addArgs = force ? ['add', '--config.minimumReleaseAge=0', target] : ['add', target];
238
+ const result = await runPlugin(config.profile, addArgs);
717
239
  let ok = result.exitCode === 0 && !result.timedOut;
718
240
  let stale = false;
719
241
  if (ok) {
720
- // pnpm's minimumReleaseAge silently keeps the old version and
721
- // exits 0 when the new release is "too young" (#13) — a clean
722
- // exit alone does not mean the update happened.
723
- const afterVersion = readInstalledVersion(config.profile, name);
724
- const afterCommit = repoKey !== null ? readLockCommits(config.profile).get(repoKey) ?? null : null;
725
- stale = isGit
726
- ? beforeCommit !== null && afterCommit === beforeCommit
727
- : beforeVersion !== null && afterVersion === beforeVersion;
242
+ stale = isStaleUpdate({
243
+ isGit,
244
+ beforeVersion,
245
+ afterVersion: readInstalledVersion(config.profile, name),
246
+ beforeCommit,
247
+ afterCommit: repoKey !== null ? readLockCommits(config.profile).get(repoKey) ?? null : null,
248
+ });
728
249
  if (stale)
729
250
  ok = false;
730
251
  }
731
252
  if (ok)
732
- updatesCache = null;
253
+ invalidateUpdates();
733
254
  const staleError = stale
734
- ? `still v${beforeVersion ?? beforeCommit?.slice(0, 7) ?? '?'} after the update — pnpm 的 minimumReleaseAge 安全策略会暂时拦下发布不久的新版本(静默保留旧版且返回成功)。请稍后再试;着急可调整 profile pnpm-workspace.yaml 的 minimumReleaseAge / pnpm's minimumReleaseAge holds back very fresh releases; retry later or tune it in the profile's pnpm-workspace.yaml`
255
+ ? '这个新版本刚发布不久。为了安全,系统默认会等它发布满一天后再安装——刚发布的版本偶尔会被发现问题然后撤回。可以明天再试,或点「立即更新」不再等待。 / This version was just released; for safety, installs normally wait about a day after a release. Try again tomorrow, or click "Update now" to install it right away.'
735
256
  : null;
736
257
  logEvent(ok ? 'info' : 'error', 'update', `${name} -> ${target} exit=${String(result.exitCode)}${result.timedOut ? ' TIMEOUT' : ''}${stale ? ' STALE(minimumReleaseAge?)' : ''}${ok ? '' : ` stderr=${result.stderr.slice(-300)}`}`);
737
258
  sendJson(response, ok ? 200 : 502, {
738
259
  ok,
260
+ stale: stale || undefined,
739
261
  error: staleError ?? undefined,
740
262
  exitCode: result.exitCode,
741
263
  timedOut: result.timedOut,
@@ -807,11 +329,11 @@ export function mountMarketRoutes(host, config) {
807
329
  }
808
330
  installing = true;
809
331
  try {
810
- const result = await runDshPlugin(config.profile, ['remove', name]);
332
+ const result = await runPlugin(config.profile, ['remove', name]);
811
333
  const ok = result.exitCode === 0 && !result.timedOut;
812
334
  let hot = false;
813
335
  if (ok) {
814
- updatesCache = null;
336
+ invalidateUpdates();
815
337
  hot = await hotUnmount(name);
816
338
  }
817
339
  logEvent(ok ? 'info' : 'error', 'uninstall', `${name} exit=${String(result.exitCode)}${ok ? ` live-removed=${String(hot)}` : ` stderr=${result.stderr.slice(-300)}`}`);
@@ -863,59 +385,48 @@ export function mountMarketRoutes(host, config) {
863
385
  sendJson(response, 400, { error: 'plugin is not in the curated registry' });
864
386
  return;
865
387
  }
866
- const source = parseSourceUrl(entry.url);
867
- if (source === null) {
388
+ const target = installTargetFor(entry);
389
+ if (target === null) {
868
390
  sendJson(response, 400, { error: 'unsupported source url' });
869
391
  return;
870
392
  }
871
- const repo = source.repo;
872
- // Registry tarballs beat full-repo GitHub downloads: smaller,
873
- // prebuilt, and CDN/mirror served. The npm name comes from our
874
- // curated registry, which only maps repo-verified packages.
875
- const NPM_NAME_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
876
- const target = typeof entry.npm === 'string' && NPM_NAME_RE.test(entry.npm)
877
- ? entry.npm
878
- : source.subpath !== null
879
- ? `github:${repo}#path:/${source.subpath}`
880
- : `github:${repo}`;
393
+ // Duplicate guard (#27): the same plugin listed under another name
394
+ // (an alias entry pointing at the same repo) must never install
395
+ // twice — two loader entries with one id brick the next boot.
396
+ // Monorepo subpath entries (distinct plugins in one repo) pass:
397
+ // their entry urls differ by subpath and identity is name-based.
398
+ const aliasOf = findInstalledAlias(entry, readInstalled(config.profile));
399
+ if (aliasOf !== null) {
400
+ logEvent('warn', 'install-rejected', `${entry.name}: same plugin already installed as ${aliasOf}`);
401
+ sendJson(response, 400, { error: `已以「${aliasOf}」安装过同一个插件,无需重复安装 / this plugin is already installed as "${aliasOf}"` });
402
+ return;
403
+ }
881
404
  installing = true;
882
405
  try {
883
406
  const before = new Set(Object.keys(readInstalled(config.profile)));
884
- const result = await runDshPlugin(config.profile, ['add', target]);
407
+ const result = await runPlugin(config.profile, ['add', target]);
885
408
  let ok = result.exitCode === 0 && !result.timedOut;
886
409
  if (ok)
887
- updatesCache = null;
410
+ invalidateUpdates();
888
411
  if (ok) {
889
412
  // Collection repos (e.g. skin monorepos) install as a junk
890
413
  // fileset with no root package.json; retarget to the real
891
414
  // plugin subdirectories via pnpm's #path: selector.
892
- ok = await retargetCollections(config.profile, before, target);
415
+ ok = await retargetCollections(runPlugin, config.profile, before, target);
893
416
  }
894
417
  // Fake-success guard (#18): a clean exit that added nothing
895
- // installable must not read as success — and a plugin whose entry
896
- // artifact is missing (source-only checkout, build blocked) would
897
- // brick the next boot, so it is removed on the spot.
418
+ // installable must not read as success. Runs even when
419
+ // retargeting partially failed — a broken piece that slipped in
420
+ // must never survive to brick the next boot.
898
421
  let notAPlugin = false;
899
422
  let removedBroken = [];
900
- // Runs even when retargeting partially failed — a broken piece
901
- // that slipped in must never survive to brick the next boot.
902
423
  if (result.exitCode === 0 && !result.timedOut) {
903
- const addedNow = Object.keys(readInstalled(config.profile)).filter(n => !before.has(n));
904
- const keep = [];
905
- for (const n of addedNow) {
906
- const dir = join(profileDir(config.profile), 'node_modules', n);
907
- if (hasDshManifest(dir) && entryArtifactExists(dir)) {
908
- keep.push(n);
909
- }
910
- else {
911
- removedBroken.push(n);
912
- await runDshPlugin(config.profile, ['remove', n]);
913
- }
914
- }
424
+ const validated = await validateAddedPlugins(runPlugin, config.profile, before);
425
+ removedBroken = validated.removedBroken;
915
426
  if (removedBroken.length > 0) {
916
427
  logEvent('warn', 'install', `${target}: removed uninstallable pieces (no dsh manifest or missing build artifacts): ${removedBroken.join(', ')}`);
917
428
  }
918
- if (keep.length === 0) {
429
+ if (validated.keep.length === 0) {
919
430
  ok = false;
920
431
  notAPlugin = true;
921
432
  logEvent('error', 'install', `${target}: nothing installable survived validation`);
@@ -935,7 +446,7 @@ export function mountMarketRoutes(host, config) {
935
446
  hot = true;
936
447
  for (const name of added) {
937
448
  const live = entry.category === 'theme'
938
- ? await activateTheme(name)
449
+ ? await themes.activateTheme(name)
939
450
  : await hotMount(host, profileDir(config.profile), name);
940
451
  if (!live)
941
452
  hot = false;