dsh-crate 0.5.5 → 0.5.6
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/client/client.js +1832 -617
- package/lib/changelog.js +71 -28
- package/lib/gist.js +1 -1
- package/lib/hot.js +18 -6
- package/lib/index.js +10 -3
- package/lib/install.js +200 -9
- package/lib/official-desktop.js +80 -16
- package/lib/patch.js +223 -25
- package/lib/pending-checkout-links.js +123 -0
- package/lib/pending-updates.js +7 -4
- package/lib/profile.js +36 -0
- package/lib/registry.js +38 -20
- package/lib/release-blockers.js +177 -0
- package/lib/release-history.js +149 -0
- package/lib/release-updates.js +1 -34
- package/lib/routes.js +843 -166
- package/lib/sources.js +42 -2
- package/lib/startup-repair-report.js +46 -0
- package/lib/themes.js +7 -5
- package/lib/types/changelog.d.ts +4 -1
- package/lib/types/dsh-cli.d.ts +8 -0
- package/lib/types/gist.d.ts +1 -1
- package/lib/types/hot.d.ts +2 -1
- package/lib/types/install.d.ts +22 -4
- package/lib/types/official-desktop.d.ts +4 -0
- package/lib/types/patch.d.ts +36 -0
- package/lib/types/pending-checkout-links.d.ts +35 -0
- package/lib/types/pending-updates.d.ts +2 -0
- package/lib/types/profile.d.ts +3 -0
- package/lib/types/registry.d.ts +6 -8
- package/lib/types/release-blockers.d.ts +34 -0
- package/lib/types/release-history.d.ts +34 -0
- package/lib/types/release-updates.d.ts +2 -11
- package/lib/types/routes.d.ts +3 -0
- package/lib/types/sources.d.ts +11 -0
- package/lib/types/startup-repair-report.d.ts +20 -0
- package/lib/types/themes.d.ts +2 -2
- package/lib/types/update-notifications.d.ts +6 -0
- package/lib/types/updates.d.ts +19 -0
- package/lib/update-notifications.js +6 -4
- package/lib/updates.js +16 -0
- package/locale/en.json +1 -1
- package/locale/ja.json +5 -0
- package/package.json +2 -2
package/lib/official-desktop.js
CHANGED
|
@@ -24,9 +24,24 @@ import { activeRegion, DEFAULT_NPM_REGISTRY, routesFor } from './regions.js';
|
|
|
24
24
|
* snapshot two minutes later. A deadline is here to end an infinite wait, not
|
|
25
25
|
* to race pnpm's retry budget — a transient that resolves by itself must not
|
|
26
26
|
* turn into a permanent failure.
|
|
27
|
+
*
|
|
28
|
+
* Their length is also not ours to pick: every call bounded here has the
|
|
29
|
+
* host's own silence watchdog inside it (#1250). The plugin manager kills a
|
|
30
|
+
* pnpm child that prints nothing for ten minutes (`idleTimeoutMs` of
|
|
31
|
+
* `@deepseek-ai/dsh-plugin-manager`, the default #817 was reported against)
|
|
32
|
+
* and answers `dsh: pnpm printed nothing for 600000ms and was terminated` —
|
|
33
|
+
* the sentence `classifyPnpmFailure` turns into "the network stalled, nothing
|
|
34
|
+
* was installed" (`./pnpm-compat.ts`). A market bound at or under that
|
|
35
|
+
* instant is not a backstop for a silent host; it is the thing that decides
|
|
36
|
+
* the answer, and the host's own diagnosis is replaced by
|
|
37
|
+
* `operation-incomplete` — "the change may already be on disk", said about a
|
|
38
|
+
* profile that a killed pnpm had not touched.
|
|
27
39
|
*/
|
|
28
|
-
const
|
|
29
|
-
|
|
40
|
+
const HOST_IDLE_WATCHDOG_MS = 600_000;
|
|
41
|
+
/** Long enough for the host to turn a killed child into its answer. */
|
|
42
|
+
const DEADLINE_MARGIN_MS = 60_000;
|
|
43
|
+
const REMOVE_DEADLINE_MS = HOST_IDLE_WATCHDOG_MS + DEADLINE_MARGIN_MS;
|
|
44
|
+
const INSTALL_DEADLINE_MS = HOST_IDLE_WATCHDOG_MS + DEADLINE_MARGIN_MS;
|
|
30
45
|
/**
|
|
31
46
|
* The bound around a whole `add`, which may be an update and therefore holds
|
|
32
47
|
* two bounded phases in a row. It has to stay strictly longer than their sum:
|
|
@@ -35,7 +50,7 @@ const INSTALL_DEADLINE_MS = 300_000;
|
|
|
35
50
|
* "the update did not finish" and started being reported as the plain
|
|
36
51
|
* operation refusal (#928's spec caught it).
|
|
37
52
|
*/
|
|
38
|
-
const OPERATION_DEADLINE_MS = REMOVE_DEADLINE_MS + INSTALL_DEADLINE_MS;
|
|
53
|
+
const OPERATION_DEADLINE_MS = REMOVE_DEADLINE_MS + INSTALL_DEADLINE_MS + DEADLINE_MARGIN_MS;
|
|
39
54
|
/**
|
|
40
55
|
* What a user can actually do when the market cannot perform an operation on
|
|
41
56
|
* the official Desktop profile: the app's own Plugins page runs the same
|
|
@@ -222,6 +237,16 @@ function targetVersionOf(spec) {
|
|
|
222
237
|
function interrupted() {
|
|
223
238
|
return { application: 'failed', error: { code: 'update-incomplete' }, repairOnRestart: true };
|
|
224
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* A manager call the market stopped waiting for.
|
|
242
|
+
*
|
|
243
|
+
* Neither "nothing happened" nor "undo it": the manager may have written the
|
|
244
|
+
* change already, so the next start reconciles against disk instead of the
|
|
245
|
+
* market inventing an answer here (#1066).
|
|
246
|
+
*/
|
|
247
|
+
function unanswered() {
|
|
248
|
+
return { application: 'failed', error: { code: 'operation-incomplete' }, repairOnRestart: true };
|
|
249
|
+
}
|
|
225
250
|
/**
|
|
226
251
|
* Overlay the market's own version without unloading it (#982, #900).
|
|
227
252
|
*
|
|
@@ -317,12 +342,17 @@ async function updateMarketInPlace(spec, name, options, context) {
|
|
|
317
342
|
* undoes the removal from that record instead of leaving the plugin gone.
|
|
318
343
|
*/
|
|
319
344
|
async function installOrReinstall(manager, spec, options, context) {
|
|
320
|
-
const install = () => manager.installBundle(spec, options);
|
|
345
|
+
const install = () => trackManagerWrite(manager.installBundle(spec, options));
|
|
321
346
|
let first;
|
|
322
347
|
try {
|
|
323
|
-
|
|
348
|
+
// Bounded like every other manager call: a plain install the host never
|
|
349
|
+
// answers used to be bounded only by the whole-operation backstop, which
|
|
350
|
+
// has to cover remove-then-install and is therefore twice as long (#1250).
|
|
351
|
+
first = await withDeadline(install(), INSTALL_DEADLINE_MS, `install ${spec}`);
|
|
324
352
|
}
|
|
325
353
|
catch (error) {
|
|
354
|
+
if (error instanceof DeadlineExceeded)
|
|
355
|
+
return unanswered();
|
|
326
356
|
if (managementCode(error) !== 'already-installed')
|
|
327
357
|
throw error;
|
|
328
358
|
first = { application: 'failed', error };
|
|
@@ -350,7 +380,7 @@ async function installOrReinstall(manager, spec, options, context) {
|
|
|
350
380
|
progress.lastLine = '先移除旧版本,桌面端才能装新版本 / Removing the old version so the desktop app can install the new one';
|
|
351
381
|
let removed;
|
|
352
382
|
try {
|
|
353
|
-
removed = await withDeadline(manager.removeBundle(name), REMOVE_DEADLINE_MS, `remove ${name}`);
|
|
383
|
+
removed = await withDeadline(trackManagerWrite(manager.removeBundle(name)), REMOVE_DEADLINE_MS, `remove ${name}`);
|
|
354
384
|
}
|
|
355
385
|
catch (error) {
|
|
356
386
|
if (error instanceof DeadlineExceeded)
|
|
@@ -363,6 +393,12 @@ async function installOrReinstall(manager, spec, options, context) {
|
|
|
363
393
|
// Nothing was unloaded, so there is nothing for a later start to repair.
|
|
364
394
|
if (record !== undefined)
|
|
365
395
|
context?.journal.finish(record.id);
|
|
396
|
+
// An update already landed behind a restart in this process, and the
|
|
397
|
+
// host now says `bundle-in-use` for a plugin it put back itself (#1253).
|
|
398
|
+
// It is the same wait for a restart, not a plugin that is really in use.
|
|
399
|
+
if (removed.application === 'failed' && managementCode(removed.error) === 'bundle-in-use' && restartPending()) {
|
|
400
|
+
return { ...removed, needsRestart: true };
|
|
401
|
+
}
|
|
366
402
|
return removed;
|
|
367
403
|
}
|
|
368
404
|
if (record !== undefined)
|
|
@@ -377,6 +413,8 @@ async function installOrReinstall(manager, spec, options, context) {
|
|
|
377
413
|
}
|
|
378
414
|
if (record !== undefined)
|
|
379
415
|
context?.journal.finish(record.id);
|
|
416
|
+
if (installed.application === 'restart-required')
|
|
417
|
+
markRestartPending();
|
|
380
418
|
return installed;
|
|
381
419
|
}
|
|
382
420
|
catch (error) {
|
|
@@ -397,7 +435,38 @@ async function installOrReinstall(manager, spec, options, context) {
|
|
|
397
435
|
* The slot lives on `globalThis` because a reload may evaluate this module
|
|
398
436
|
* again, which a module-level variable would not survive.
|
|
399
437
|
*/
|
|
438
|
+
const RESTART_SLOT = Symbol.for('dsh-crate.officialDesktop.restartPending');
|
|
439
|
+
/**
|
|
440
|
+
* Whether an update in THIS host process already settled behind a restart.
|
|
441
|
+
* On the global like the slots below: a market reload keeps the process, and
|
|
442
|
+
* the process is what has to restart.
|
|
443
|
+
*/
|
|
444
|
+
function restartPending() {
|
|
445
|
+
return globalThis[RESTART_SLOT] === true;
|
|
446
|
+
}
|
|
447
|
+
function markRestartPending() {
|
|
448
|
+
globalThis[RESTART_SLOT] = true;
|
|
449
|
+
}
|
|
450
|
+
/** Test seam: a new host process starts with nothing pending. */
|
|
451
|
+
export function forgetRestartPending() {
|
|
452
|
+
delete globalThis[RESTART_SLOT];
|
|
453
|
+
}
|
|
454
|
+
/** What the user reads when the host asks for a restart before the next update (#1253). */
|
|
455
|
+
const RESTART_FIRST_MESSAGE = '这一轮只能更新到这里。重启后再点「全部更新」继续。 / This run can only go this far. Restart, then click Update all to continue.';
|
|
400
456
|
const OPERATION_SLOT = Symbol.for('dsh-crate.officialDesktop.operation');
|
|
457
|
+
const WRITE_SLOT = Symbol.for('dsh-crate.officialDesktop.packageWrites');
|
|
458
|
+
/** Keep the native promises across a market reload, including after a route deadline. */
|
|
459
|
+
function pendingManagerWrites() {
|
|
460
|
+
const processState = globalThis;
|
|
461
|
+
return processState[WRITE_SLOT] ??= new Set();
|
|
462
|
+
}
|
|
463
|
+
function trackManagerWrite(work) {
|
|
464
|
+
const pending = pendingManagerWrites();
|
|
465
|
+
pending.add(work);
|
|
466
|
+
const remove = () => pending.delete(work);
|
|
467
|
+
void work.then(remove, remove);
|
|
468
|
+
return work;
|
|
469
|
+
}
|
|
401
470
|
function runningOperation() {
|
|
402
471
|
return globalThis[OPERATION_SLOT];
|
|
403
472
|
}
|
|
@@ -522,18 +591,11 @@ packageManager) {
|
|
|
522
591
|
// ordinary install or uninstall needs the same bound.
|
|
523
592
|
const deadline = command === 'remove' ? REMOVE_DEADLINE_MS : OPERATION_DEADLINE_MS;
|
|
524
593
|
operation = withDeadline(command === 'remove'
|
|
525
|
-
? manager.removeBundle(target)
|
|
594
|
+
? trackManagerWrite(manager.removeBundle(target))
|
|
526
595
|
: installOrReinstall(manager, spec, options, updateContext), deadline, `${command} ${target}`).catch((error) => {
|
|
527
596
|
if (!(error instanceof DeadlineExceeded))
|
|
528
597
|
throw error;
|
|
529
|
-
return
|
|
530
|
-
application: 'failed',
|
|
531
|
-
// Neither "nothing happened" nor "undo it": the manager may have
|
|
532
|
-
// written the change already. The next start reconciles against
|
|
533
|
-
// disk instead of the market inventing an answer here.
|
|
534
|
-
repairOnRestart: true,
|
|
535
|
-
error: { code: 'operation-incomplete' },
|
|
536
|
-
};
|
|
598
|
+
return unanswered();
|
|
537
599
|
});
|
|
538
600
|
}
|
|
539
601
|
catch (error) {
|
|
@@ -556,7 +618,7 @@ packageManager) {
|
|
|
556
618
|
result.packageResult?.output ?? '',
|
|
557
619
|
result.application === 'overridden' ? 'the plugin manager kept this change, but another layer overrides whether it is enabled' : '',
|
|
558
620
|
].filter(Boolean).join('\n');
|
|
559
|
-
const message = ok ? '' : detail(result.error);
|
|
621
|
+
const message = ok ? '' : result.needsRestart === true ? RESTART_FIRST_MESSAGE : detail(result.error);
|
|
560
622
|
if (!ok)
|
|
561
623
|
progress.error = message;
|
|
562
624
|
// The manager never settled: the profile may be half-updated, so this
|
|
@@ -581,6 +643,7 @@ packageManager) {
|
|
|
581
643
|
// The manager's own code, kept machine-readable (#909): a client can
|
|
582
644
|
// tell a self-update refusal from an ordinary failure.
|
|
583
645
|
...(ok ? {} : { refusalCode: managementCode(result.error) }),
|
|
646
|
+
...(result.needsRestart === true ? { needsRestart: true } : {}),
|
|
584
647
|
};
|
|
585
648
|
}, (error) => {
|
|
586
649
|
const message = detail(error);
|
|
@@ -631,6 +694,7 @@ packageManager) {
|
|
|
631
694
|
runPlugin,
|
|
632
695
|
repairPendingUpdates,
|
|
633
696
|
busy,
|
|
697
|
+
packageWritesSettled: () => pendingManagerWrites().size === 0,
|
|
634
698
|
probePnpm: async () => managerLookup() !== undefined,
|
|
635
699
|
provisionPnpm: async () => ({ ok: managerLookup() !== undefined, hint: 'Use the official desktop plugin manager' }),
|
|
636
700
|
cancelActive: () => {
|
package/lib/patch.js
CHANGED
|
@@ -24,10 +24,12 @@
|
|
|
24
24
|
* - host infrastructure rows (transport / hot-reload / storage / settings
|
|
25
25
|
* chains) are protected and refuse to toggle.
|
|
26
26
|
*/
|
|
27
|
-
import {
|
|
27
|
+
import { randomUUID } from 'node:crypto';
|
|
28
|
+
import { accessSync, constants, lstatSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
28
29
|
import { join } from 'node:path';
|
|
29
30
|
import { fileURLToPath } from 'node:url';
|
|
30
31
|
import { logEvent } from './log.js';
|
|
32
|
+
import { packageOfEntryName } from './entry-identity.js';
|
|
31
33
|
import { parsePatchFile, parsePatchText } from './check.js';
|
|
32
34
|
import { bundleOptOutRowIds, bundlePatchInsertedIds, declaredBundlePatchFiles, parsePatchRows } from './profile.js';
|
|
33
35
|
import { NPM_NAME_RE } from './sources.js';
|
|
@@ -394,7 +396,9 @@ export function userPatchPackageReferences(patchPath, packageName) {
|
|
|
394
396
|
* @returns the id as a string, '' when absent or not a string/number.
|
|
395
397
|
*/
|
|
396
398
|
export function loaderEntryId(entry) {
|
|
397
|
-
|
|
399
|
+
return normalizeRowId(entry?.options?.id);
|
|
400
|
+
}
|
|
401
|
+
function normalizeRowId(id) {
|
|
398
402
|
if (typeof id === 'string')
|
|
399
403
|
return id;
|
|
400
404
|
if (typeof id === 'number' && Number.isFinite(id))
|
|
@@ -635,6 +639,15 @@ function escapeRegExp(value) {
|
|
|
635
639
|
return value.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
|
|
636
640
|
}
|
|
637
641
|
const MARKET_ROW_MARKER = '# dsh-crate';
|
|
642
|
+
const COMPONENT_MODULE_RE = /^@?[A-Za-z0-9_.~-]+(?:\/[A-Za-z0-9_.~-]+)*$/u;
|
|
643
|
+
/** Named switch rows can point at an export inside their owning package
|
|
644
|
+
* (#1242). Keep the full module identity for the host's exact-name match;
|
|
645
|
+
* cleanup may own the package, but not a similarly prefixed package. */
|
|
646
|
+
function isNamedSwitchModule(name, owner) {
|
|
647
|
+
const packageName = NPM_NAME_RE.test(name) ? name : packageOfEntryName(name);
|
|
648
|
+
return COMPONENT_MODULE_RE.test(name) && packageName !== null && NPM_NAME_RE.test(packageName)
|
|
649
|
+
&& (owner === null || name === owner || packageName === owner);
|
|
650
|
+
}
|
|
638
651
|
function moduleNameLine(name) {
|
|
639
652
|
return ` name: '${name}' ${MARKET_ROW_MARKER}`;
|
|
640
653
|
}
|
|
@@ -647,7 +660,7 @@ function rowBlock(rowId, disabled, name = null) {
|
|
|
647
660
|
* Also accept the module name market adds to its own rows (#1196). Any
|
|
648
661
|
* other key makes the entry a user configuration to preserve (#821).
|
|
649
662
|
*/
|
|
650
|
-
function rewriteRowBlocks(text, rowId, disabled, replace, expected = null, owner = expected) {
|
|
663
|
+
function rewriteRowBlocks(text, rowId, disabled, replace, expected = null, owner = expected, componentsOnly = false) {
|
|
651
664
|
const id = escapeRegExp(rowId);
|
|
652
665
|
const idLine = new RegExp(`^- id: (?:${id}|'${id}'|"${id}")[ \t]*$`, 'u');
|
|
653
666
|
const lines = text.match(/[^\n]*(?:\n|$)/gu)?.filter(Boolean) ?? [];
|
|
@@ -659,16 +672,23 @@ function rewriteRowBlocks(text, rowId, disabled, replace, expected = null, owner
|
|
|
659
672
|
if (!idLine.test(lines[index].replace(/\r?\n$/u, '')))
|
|
660
673
|
continue;
|
|
661
674
|
const flagIndex = nextPatchContentLine(lines, index + 1);
|
|
662
|
-
const flag = /^ {2}disabled: (true|false)[ \t]*(?:\r?\n)?$/u.exec(lines[flagIndex] ?? '');
|
|
675
|
+
const flag = /^ {2}disabled: (true|false)(?:[ \t]+# dsh-crate package-switch (\S+))?[ \t]*(?:\r?\n)?$/u.exec(lines[flagIndex] ?? '');
|
|
663
676
|
if (flag === null || (disabled !== 'any' && flag[1] !== disabled))
|
|
664
677
|
continue;
|
|
678
|
+
const componentOwner = flag[2];
|
|
679
|
+
if (componentsOnly && componentOwner === undefined)
|
|
680
|
+
continue;
|
|
681
|
+
if (componentOwner !== undefined && componentOwner !== owner)
|
|
682
|
+
continue;
|
|
665
683
|
let lastKey = flagIndex;
|
|
666
684
|
let name = null;
|
|
667
685
|
let following = nextPatchContentLine(lines, lastKey + 1);
|
|
668
686
|
const marker = /^ {2}name: (['"])([^'"]+)\1[ \t]+# dsh-crate[ \t]*(?:\r?\n)?$/u.exec(lines[following] ?? '');
|
|
669
687
|
if (marker !== null) {
|
|
670
688
|
name = marker[2];
|
|
671
|
-
|
|
689
|
+
const validName = componentOwner === undefined
|
|
690
|
+
? isNamedSwitchModule(name, owner) : COMPONENT_MODULE_RE.test(name);
|
|
691
|
+
if (!validName || !rowApplies(name, expected))
|
|
672
692
|
continue;
|
|
673
693
|
lastKey = following;
|
|
674
694
|
following = nextPatchContentLine(lines, lastKey + 1);
|
|
@@ -682,6 +702,177 @@ function rewriteRowBlocks(text, rowId, disabled, replace, expected = null, owner
|
|
|
682
702
|
}
|
|
683
703
|
return result + text.slice(copied);
|
|
684
704
|
}
|
|
705
|
+
function componentChoices(text, rows) {
|
|
706
|
+
const parsed = parsePatchText(withPlaceholderRestored(text));
|
|
707
|
+
if (parsed === null)
|
|
708
|
+
return null;
|
|
709
|
+
const choices = new Map();
|
|
710
|
+
for (const item of parsed) {
|
|
711
|
+
if (item === null || typeof item !== 'object')
|
|
712
|
+
continue;
|
|
713
|
+
const insert = item.insert;
|
|
714
|
+
for (const row of Array.isArray(insert) ? insert : [item]) {
|
|
715
|
+
if (row === null || typeof row !== 'object')
|
|
716
|
+
continue;
|
|
717
|
+
const { id, name, disabled } = row;
|
|
718
|
+
const rowId = normalizeRowId(id);
|
|
719
|
+
if (!rows.has(rowId) || !rowApplies(name, rows.get(rowId) ?? null))
|
|
720
|
+
continue;
|
|
721
|
+
if (typeof disabled === 'boolean')
|
|
722
|
+
choices.set(rowId, disabled);
|
|
723
|
+
else if (disabled !== null && typeof disabled === 'object' && '__jsExpr' in disabled)
|
|
724
|
+
choices.set(rowId, 'expression');
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return choices;
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* View the user's component choices without the temporary package-wide
|
|
731
|
+
* disable overrides. A named override the official page changed to false
|
|
732
|
+
* is a new choice, so only the still-disabled market overrides are removed.
|
|
733
|
+
*/
|
|
734
|
+
export function readComponentSwitchPatch(patchPath, packageName, rows, bundleDecides) {
|
|
735
|
+
let before = '';
|
|
736
|
+
try {
|
|
737
|
+
before = readPatchText(patchPath);
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
if (error.code !== 'ENOENT')
|
|
741
|
+
return null;
|
|
742
|
+
}
|
|
743
|
+
let resumed = before;
|
|
744
|
+
for (const [id, expected] of rows) {
|
|
745
|
+
resumed = rewriteRowBlocks(resumed, id, 'true', () => '', expected, packageName, true);
|
|
746
|
+
}
|
|
747
|
+
const choices = componentChoices(resumed, rows);
|
|
748
|
+
if (choices === null)
|
|
749
|
+
return null;
|
|
750
|
+
const keepOff = new Set();
|
|
751
|
+
const ownOff = new Set();
|
|
752
|
+
for (const id of rows.keys()) {
|
|
753
|
+
const choice = choices.get(id);
|
|
754
|
+
if (choice === true)
|
|
755
|
+
ownOff.add(id);
|
|
756
|
+
if (choice === true || choice === 'expression' || (choice !== false && bundleDecides.has(id)))
|
|
757
|
+
keepOff.add(id);
|
|
758
|
+
}
|
|
759
|
+
const customized = [...choices].some(([id, choice]) => choice !== true || !bundleDecides.has(id));
|
|
760
|
+
const suspended = before !== resumed;
|
|
761
|
+
// Only the old switch's standalone flags are ambiguous. If any exact
|
|
762
|
+
// user row still holds a component off without them, preserve that choice.
|
|
763
|
+
let legacyAllOff = false;
|
|
764
|
+
if (!suspended && ownOff.size === rows.size) {
|
|
765
|
+
let legacyCleared = resumed;
|
|
766
|
+
for (const [id, expected] of rows) {
|
|
767
|
+
legacyCleared = rewriteRowBlocks(legacyCleared, id, 'true', () => '', expected, packageName);
|
|
768
|
+
}
|
|
769
|
+
const remaining = componentChoices(legacyCleared, rows);
|
|
770
|
+
legacyAllOff = remaining !== null && ![...remaining.values()].includes(true);
|
|
771
|
+
}
|
|
772
|
+
return { before, resumed, rows, suspended, customized, keepOff, ownOff, legacyAllOff };
|
|
773
|
+
}
|
|
774
|
+
const PATCH_CHANGED = { ok: false, reason: '组件配置刚刚发生变化。请检查设置后重试 / The component settings changed. Check them and retry.' };
|
|
775
|
+
function readComponentPatchFile(patchPath) {
|
|
776
|
+
try {
|
|
777
|
+
return readFileSync(patchPath, 'utf8');
|
|
778
|
+
}
|
|
779
|
+
catch (error) {
|
|
780
|
+
if (error.code !== 'ENOENT')
|
|
781
|
+
throw error;
|
|
782
|
+
return null;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
/** Preserve the original YAML even when the write fails partway through. */
|
|
786
|
+
function replaceComponentPatch(patchPath, expected, contents) {
|
|
787
|
+
let temporary;
|
|
788
|
+
try {
|
|
789
|
+
if (readComponentPatchFile(patchPath) !== expected)
|
|
790
|
+
return PATCH_CHANGED;
|
|
791
|
+
const existing = lstatSync(patchPath, { throwIfNoEntry: false });
|
|
792
|
+
const target = existing === undefined ? patchPath : realpathSync(patchPath);
|
|
793
|
+
if (existing !== undefined)
|
|
794
|
+
accessSync(target, constants.W_OK);
|
|
795
|
+
if (contents === null) {
|
|
796
|
+
// Only a regular file created by this switch can be removed on rollback.
|
|
797
|
+
if (existing?.isFile() !== true)
|
|
798
|
+
return PATCH_CHANGED;
|
|
799
|
+
unlinkSync(patchPath);
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
temporary = target + '.dsh-crate-' + randomUUID();
|
|
803
|
+
const mode = existing === undefined ? 0o666 : statSync(target).mode & 0o777;
|
|
804
|
+
writeFileSync(temporary, contents, { flag: 'wx', mode });
|
|
805
|
+
if (readComponentPatchFile(patchPath) !== expected
|
|
806
|
+
|| (existing !== undefined && realpathSync(patchPath) !== target))
|
|
807
|
+
return PATCH_CHANGED;
|
|
808
|
+
renameSync(temporary, target);
|
|
809
|
+
temporary = undefined;
|
|
810
|
+
}
|
|
811
|
+
return { ok: true, reason: null };
|
|
812
|
+
}
|
|
813
|
+
catch (error) {
|
|
814
|
+
return patchWriteError(error);
|
|
815
|
+
}
|
|
816
|
+
finally {
|
|
817
|
+
if (temporary !== undefined) {
|
|
818
|
+
try {
|
|
819
|
+
unlinkSync(temporary);
|
|
820
|
+
}
|
|
821
|
+
catch { /* retain the original write result */ }
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/** Roll back only an unchanged file this switch wrote. */
|
|
826
|
+
export function rollbackComponentSwitchPatch(patchPath, change) {
|
|
827
|
+
return replaceComponentPatch(patchPath, change.after, change.before);
|
|
828
|
+
}
|
|
829
|
+
/** Only the switch's own overrides are written or removed. Keep the user's
|
|
830
|
+
* component flags and accompanying configuration in the file verbatim. */
|
|
831
|
+
export function writeComponentSwitchPatch(patchPath, packageName, patch, enabled) {
|
|
832
|
+
return queuedWrite(async () => commitComponentSwitchPatch(patchPath, packageName, patch, enabled));
|
|
833
|
+
}
|
|
834
|
+
/** The OFF transaction calls this while holding the host profile lock. */
|
|
835
|
+
export function commitComponentSwitchPatch(patchPath, packageName, patch, enabled) {
|
|
836
|
+
let latest;
|
|
837
|
+
try {
|
|
838
|
+
latest = readComponentPatchFile(patchPath);
|
|
839
|
+
}
|
|
840
|
+
catch {
|
|
841
|
+
return { ok: false, reason: '无法读取组件配置 / could not read the component settings' };
|
|
842
|
+
}
|
|
843
|
+
if (withoutPatchBom(latest ?? '') !== patch.before)
|
|
844
|
+
return PATCH_CHANGED;
|
|
845
|
+
let change;
|
|
846
|
+
const publish = (file, content) => {
|
|
847
|
+
if (latest === content)
|
|
848
|
+
return { ok: true, reason: null };
|
|
849
|
+
const result = replaceComponentPatch(file, latest, content);
|
|
850
|
+
if (result.ok)
|
|
851
|
+
change = { before: latest, after: content };
|
|
852
|
+
return result;
|
|
853
|
+
};
|
|
854
|
+
if (enabled) {
|
|
855
|
+
return patch.suspended
|
|
856
|
+
? publish(patchPath, withPlaceholderRestored(patch.resumed))
|
|
857
|
+
: { ok: true, reason: null };
|
|
858
|
+
}
|
|
859
|
+
const overrides = [];
|
|
860
|
+
const alreadyOff = patch.ownOff.size === patch.rows.size;
|
|
861
|
+
for (const [id, name] of patch.rows) {
|
|
862
|
+
// A carrier can still affect other packages when all its entries are
|
|
863
|
+
// off. Record the package-level switch without changing that flag.
|
|
864
|
+
if (patch.ownOff.has(id) && (!alreadyOff || overrides.length > 0))
|
|
865
|
+
continue;
|
|
866
|
+
if (!ROW_ID_RE.test(id)) {
|
|
867
|
+
return { ok: false, reason: '组件编号无法写入。请在官方插件页停用 / A component ID could not be saved. Turn it off on the DSH Plugins page.' };
|
|
868
|
+
}
|
|
869
|
+
const marker = MARKET_ROW_MARKER + ' package-switch ' + packageName;
|
|
870
|
+
const qualifiedName = name !== null && COMPONENT_MODULE_RE.test(name) ? moduleNameLine(name) + '\n' : '';
|
|
871
|
+
overrides.push('- id: ' + id + '\n disabled: true ' + marker + '\n' + qualifiedName);
|
|
872
|
+
}
|
|
873
|
+
const result = appendPatchEntry(patchPath, overrides.join(''), patch.resumed, publish);
|
|
874
|
+
return change === undefined ? result : { ...result, change };
|
|
875
|
+
}
|
|
685
876
|
/**
|
|
686
877
|
* What the patch file finally says about one row: the flag of its last
|
|
687
878
|
* `- id: X` entry that carries one, rows with more keys included. Later
|
|
@@ -715,7 +906,7 @@ function composedRowFlag(text, rowId, expected = null) {
|
|
|
715
906
|
if (row === null || typeof row !== 'object' || Array.isArray(row))
|
|
716
907
|
continue;
|
|
717
908
|
const entry = row;
|
|
718
|
-
if (entry.id !== rowId)
|
|
909
|
+
if (normalizeRowId(entry.id) !== rowId)
|
|
719
910
|
continue;
|
|
720
911
|
// A row naming another module never reaches this id's entry (#1113).
|
|
721
912
|
if (!rowApplies(entry.name, expected))
|
|
@@ -769,9 +960,11 @@ function lastRowFlag(text, rowId, expected = null) {
|
|
|
769
960
|
* after it and wrote a second top-level node. The market's own writes drop
|
|
770
961
|
* the BOM from then on. Throws like `readFileSync` when the file is missing.
|
|
771
962
|
*/
|
|
963
|
+
function withoutPatchBom(text) {
|
|
964
|
+
return text.startsWith('\uFEFF') ? text.slice(1) : text;
|
|
965
|
+
}
|
|
772
966
|
function readPatchText(patchPath) {
|
|
773
|
-
|
|
774
|
-
return text.startsWith('') ? text.slice(1) : text;
|
|
967
|
+
return withoutPatchBom(readFileSync(patchPath, 'utf8'));
|
|
775
968
|
}
|
|
776
969
|
function writePatchFile(patchPath, contents) {
|
|
777
970
|
try {
|
|
@@ -779,22 +972,26 @@ function writePatchFile(patchPath, contents) {
|
|
|
779
972
|
return { ok: true, reason: null };
|
|
780
973
|
}
|
|
781
974
|
catch (error) {
|
|
782
|
-
|
|
783
|
-
const detail = code === 'EACCES' || code === 'EPERM' || code === 'EROFS'
|
|
784
|
-
? '文件不可写(只读或被系统保护)'
|
|
785
|
-
: code === 'ENOSPC'
|
|
786
|
-
? '磁盘空间不足'
|
|
787
|
-
: error instanceof Error ? error.message : String(error);
|
|
788
|
-
return {
|
|
789
|
-
ok: false,
|
|
790
|
-
reason: `补丁层文件写不进去:${detail} / the patch file could not be written: ${code === 'EACCES' || code === 'EPERM' || code === 'EROFS' ? 'it is read-only' : detail}`,
|
|
791
|
-
};
|
|
975
|
+
return patchWriteError(error);
|
|
792
976
|
}
|
|
793
977
|
}
|
|
794
|
-
function
|
|
978
|
+
function patchWriteError(error) {
|
|
979
|
+
const code = error !== null && typeof error === 'object' && 'code' in error ? String(error.code) : '';
|
|
980
|
+
const readOnly = code === 'EACCES' || code === 'EPERM' || code === 'EROFS';
|
|
981
|
+
const detail = readOnly
|
|
982
|
+
? '文件不可写。解除只读或检查权限后重试'
|
|
983
|
+
: code === 'ENOSPC'
|
|
984
|
+
? '磁盘空间不足。腾出空间后重试'
|
|
985
|
+
: error instanceof Error ? error.message : String(error);
|
|
986
|
+
return {
|
|
987
|
+
ok: false,
|
|
988
|
+
reason: '补丁层文件写不进去:' + detail + ' / the patch file could not be written: ' + (readOnly ? 'it is read-only. Check permissions and retry.' : code === 'ENOSPC' ? 'free up disk space and retry.' : detail),
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
function appendPatchEntry(patchPath, block, text, publish = writePatchFile) {
|
|
795
992
|
const core = text.trim();
|
|
796
993
|
if (core === '') {
|
|
797
|
-
return
|
|
994
|
+
return publish(patchPath, block);
|
|
798
995
|
}
|
|
799
996
|
// The dsh profile template ships an empty `[]` placeholder (a valid
|
|
800
997
|
// empty list). Appending after it would create TWO top-level elements in
|
|
@@ -804,12 +1001,12 @@ function appendPatchEntry(patchPath, block, text) {
|
|
|
804
1001
|
// Comments only (the user dropped the placeholder): append after them.
|
|
805
1002
|
if (withoutComments === '') {
|
|
806
1003
|
const next = text.endsWith('\n') ? text : `${text}\n`;
|
|
807
|
-
return
|
|
1004
|
+
return publish(patchPath, `${next}${block}`);
|
|
808
1005
|
}
|
|
809
1006
|
if (withoutComments === '[]' || withoutComments === '[ ]') {
|
|
810
1007
|
const commented = text.replace(/^[ \t]*\[[ \t]*\][ \t]*(?:#.*)?(?:\r?\n|$)/mu, '# []\n');
|
|
811
1008
|
const next = commented.endsWith('\n') ? commented : `${commented}\n`;
|
|
812
|
-
return
|
|
1009
|
+
return publish(patchPath, `${next}${block}`);
|
|
813
1010
|
}
|
|
814
1011
|
// A top-level flow LIST (other than the placeholder above) cannot be
|
|
815
1012
|
// appended to: `- id: X` after `[1, 2]` would create two top-level
|
|
@@ -834,7 +1031,7 @@ function appendPatchEntry(patchPath, block, text) {
|
|
|
834
1031
|
};
|
|
835
1032
|
}
|
|
836
1033
|
const next = text.endsWith('\n') ? text : `${text}\n`;
|
|
837
|
-
return
|
|
1034
|
+
return publish(patchPath, `${next}${block}`);
|
|
838
1035
|
}
|
|
839
1036
|
/**
|
|
840
1037
|
* Disable one row: `- id: X` + `disabled: true` (idempotent).
|
|
@@ -847,7 +1044,8 @@ function appendPatchEntry(patchPath, block, text) {
|
|
|
847
1044
|
*/
|
|
848
1045
|
export function disableRow(patchPath, rowId, options = {}) {
|
|
849
1046
|
const expected = options.expectedName ?? null;
|
|
850
|
-
const name = options.recordModuleName === true && expected !== null
|
|
1047
|
+
const name = options.recordModuleName === true && expected !== null
|
|
1048
|
+
&& isNamedSwitchModule(expected, options.packageName ?? expected) ? expected : null;
|
|
851
1049
|
return queuedWrite(async () => {
|
|
852
1050
|
if (!ROW_ID_RE.test(rowId)) {
|
|
853
1051
|
return { ok: false, reason: `行 id 含特殊字符,不支持写入补丁层 / row id ${rowId} cannot be written to the patch layer` };
|
|
@@ -983,7 +1181,7 @@ export function removeRowBlocks(patchPath, rowIds, packageName = null) {
|
|
|
983
1181
|
} // no file, nothing to remove
|
|
984
1182
|
let next = text;
|
|
985
1183
|
for (const rowId of rowIds)
|
|
986
|
-
next = rewriteRowBlocks(next, rowId, 'any', () => '', packageName);
|
|
1184
|
+
next = rewriteRowBlocks(next, rowId, 'any', () => '', null, packageName);
|
|
987
1185
|
if (next === text)
|
|
988
1186
|
return { ok: true, reason: null };
|
|
989
1187
|
const written = writePatchFile(patchPath, withPlaceholderRestored(next));
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/** Checkout links disconnected before replacing a local build. */
|
|
2
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
3
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
4
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
5
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
return path;
|
|
9
|
+
};
|
|
10
|
+
import { randomUUID } from 'node:crypto';
|
|
11
|
+
import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { dirname, isAbsolute, join, relative, sep } from 'node:path';
|
|
14
|
+
import { pathToFileURL } from 'node:url';
|
|
15
|
+
import { hostNodeModulesRoot } from './install.js';
|
|
16
|
+
import { NPM_NAME_RE } from './sources.js';
|
|
17
|
+
export class PendingCheckoutLinks {
|
|
18
|
+
file;
|
|
19
|
+
constructor(profileDirectory) {
|
|
20
|
+
this.file = join(profileDirectory, '.dsh-crate', 'pending-checkout-links.json');
|
|
21
|
+
}
|
|
22
|
+
list() {
|
|
23
|
+
let data;
|
|
24
|
+
try {
|
|
25
|
+
data = JSON.parse(readFileSync(this.file, 'utf8'));
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error.code === 'ENOENT')
|
|
29
|
+
return [];
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
return Array.isArray(data?.operations) ? data.operations.filter((item) => item !== null && typeof item === 'object'
|
|
33
|
+
&& typeof item.name === 'string' && NPM_NAME_RE.test(item.name)
|
|
34
|
+
&& typeof item.spec === 'string' && /^link:.+/i.test(item.spec)
|
|
35
|
+
&& typeof item.rawTarget === 'string' && item.rawTarget !== '') : [];
|
|
36
|
+
}
|
|
37
|
+
record(item) {
|
|
38
|
+
this.save([...this.list().filter(itemBefore => itemBefore.name !== item.name), item]);
|
|
39
|
+
}
|
|
40
|
+
save(operations) {
|
|
41
|
+
mkdirSync(dirname(this.file), { recursive: true });
|
|
42
|
+
const temporary = `${this.file}.tmp-${randomUUID()}`;
|
|
43
|
+
writeFileSync(temporary, JSON.stringify({ operations }), { flag: 'wx' });
|
|
44
|
+
try {
|
|
45
|
+
renameSync(temporary, this.file);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
try {
|
|
49
|
+
unlinkSync(temporary);
|
|
50
|
+
}
|
|
51
|
+
catch { /* preserve the write failure */ }
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Claim the same exclusive profile writer lock as rc.3's plugin manager.
|
|
58
|
+
* An occupied lock is left for the host to release or take over.
|
|
59
|
+
*/
|
|
60
|
+
export function withIdleProfile(profileDirectory, action) {
|
|
61
|
+
const lock = join(profileDirectory, 'package.json.lock');
|
|
62
|
+
let descriptor;
|
|
63
|
+
try {
|
|
64
|
+
descriptor = openSync(lock, 'wx', 0o600);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
const code = error.code;
|
|
68
|
+
if (code === 'EEXIST' || (code === 'EPERM' && lstatSync(lock, { throwIfNoEntry: false }) !== undefined))
|
|
69
|
+
return null;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
writeFileSync(descriptor, `${process.pid}\n`);
|
|
74
|
+
return action();
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
closeSync(descriptor);
|
|
78
|
+
unlinkSync(lock);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Resolve the lock implementation from the host's own plugin manager. */
|
|
82
|
+
export async function loadHostProfileLock(installDirectory) {
|
|
83
|
+
if (installDirectory === null)
|
|
84
|
+
return null;
|
|
85
|
+
const nodeModules = hostNodeModulesRoot(installDirectory);
|
|
86
|
+
const belongsToHost = (path) => {
|
|
87
|
+
const inside = relative(nodeModules, path);
|
|
88
|
+
return inside !== '' && !isAbsolute(inside) && inside !== '..' && !inside.startsWith(`..${sep}`);
|
|
89
|
+
};
|
|
90
|
+
try {
|
|
91
|
+
const manager = createRequire(join(installDirectory, 'package.json')).resolve('@deepseek-ai/dsh-plugin-manager');
|
|
92
|
+
if (!belongsToHost(manager))
|
|
93
|
+
return null;
|
|
94
|
+
const atomic = createRequire(manager).resolve('@deepseek-ai/dsh-atomic-write');
|
|
95
|
+
if (!belongsToHost(atomic))
|
|
96
|
+
return null;
|
|
97
|
+
const module = await import(__rewriteRelativeImportExtension(pathToFileURL(atomic).href));
|
|
98
|
+
return typeof module.withFileLock === 'function' ? module.withFileLock : null;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** A pnpm child may survive the process that owned the writer lock. */
|
|
105
|
+
export function hasUnsettledPackageRun(profileDirectory) {
|
|
106
|
+
try {
|
|
107
|
+
const value = JSON.parse(readFileSync(join(profileDirectory, '.plugin-manager', 'run.json'), 'utf8'));
|
|
108
|
+
const pid = value?.pid;
|
|
109
|
+
const grouped = value?.grouped;
|
|
110
|
+
if (typeof pid !== 'number' || !Number.isSafeInteger(pid) || pid <= 0 || typeof grouped !== 'boolean')
|
|
111
|
+
return true;
|
|
112
|
+
try {
|
|
113
|
+
process.kill(grouped && process.platform !== 'win32' ? -pid : pid, 0);
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return error.code !== 'ESRCH';
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
return error.code !== 'ENOENT';
|
|
122
|
+
}
|
|
123
|
+
}
|