@linxin666/dsh-client-ui-plugin-manager 0.2.6 → 0.2.8
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/README.i18n.yaml +2 -2
- package/README.md +5 -2
- package/README.zh.md +5 -2
- package/lib/client.js +41 -5
- package/lib/client.js.map +1 -1
- package/lib/index.js +308 -39
- package/lib/types/client/locales.d.ts +6 -0
- package/lib/types/core/protocol.d.ts +4 -0
- package/lib/types/core/version.d.ts +67 -0
- package/lib/types/host/gateway.d.ts +3 -1
- package/lib/types/host/profile.d.ts +4 -0
- package/lib/types/host/routes.d.ts +13 -2
- package/package.json +11 -8
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { copyFile, readFile, rename, writeFile } from "node:fs/promises";
|
|
4
|
-
import { isAbsolute, join, win32 } from "node:path";
|
|
4
|
+
import { isAbsolute, join, posix, win32 } from "node:path";
|
|
5
5
|
import { isMap, isScalar, isSeq, parseDocument } from "yaml";
|
|
6
6
|
import { homedir } from "node:os";
|
|
7
7
|
//#region src/mount-once.ts
|
|
@@ -411,6 +411,21 @@ function resolveDshHome(env = process.env, home = homedir()) {
|
|
|
411
411
|
* write goes through the official CLI or the patch-row editor.
|
|
412
412
|
* @module @linxin666/dsh-client-ui-plugin-manager/host
|
|
413
413
|
*/
|
|
414
|
+
/** Read the packaged desktop app's persisted active profile, when present. */
|
|
415
|
+
function desktopSelectedProfile(env = process.env) {
|
|
416
|
+
const explicit = env.DSH_DESKTOP_DEFAULT_PROFILE?.trim();
|
|
417
|
+
if (explicit) return explicit;
|
|
418
|
+
const appRoots = [
|
|
419
|
+
env.APPDATA && join(env.APPDATA, "DSH Desktop"),
|
|
420
|
+
env.XDG_CONFIG_HOME && join(env.XDG_CONFIG_HOME, "DSH Desktop"),
|
|
421
|
+
env.HOME && join(env.HOME, "Library", "Application Support", "DSH Desktop"),
|
|
422
|
+
env.HOME && join(env.HOME, ".config", "DSH Desktop")
|
|
423
|
+
].filter((value) => typeof value === "string");
|
|
424
|
+
for (const root of appRoots) try {
|
|
425
|
+
const parsed = JSON.parse(readFileSync(join(root, "profile-selection", "state.json"), "utf8"));
|
|
426
|
+
if (typeof parsed.active === "string" && parsed.active.trim() !== "") return parsed.active.trim();
|
|
427
|
+
} catch {}
|
|
428
|
+
}
|
|
414
429
|
/**
|
|
415
430
|
* Resolve the boot profile name from the host argv: an explicit `--profile`
|
|
416
431
|
* flag wins, then the DSH_PROFILE environment override, then the `web`
|
|
@@ -423,9 +438,16 @@ function resolveDshHome(env = process.env, home = homedir()) {
|
|
|
423
438
|
function resolveProfile(argv = process.argv, env = process.env) {
|
|
424
439
|
const flagIndex = argv.indexOf("--profile");
|
|
425
440
|
let name;
|
|
441
|
+
let desktop = false;
|
|
426
442
|
if (flagIndex !== -1 && argv[flagIndex + 1] !== void 0 && argv[flagIndex + 1] !== "") name = argv[flagIndex + 1];
|
|
427
|
-
else if (env.DSH_PROFILE !== void 0 && env.DSH_PROFILE.trim() !== "")
|
|
428
|
-
|
|
443
|
+
else if (env.DSH_PROFILE !== void 0 && env.DSH_PROFILE.trim() !== "") {
|
|
444
|
+
name = env.DSH_PROFILE.trim();
|
|
445
|
+
desktop = argv.length <= 1 && desktopSelectedProfile(env) === name;
|
|
446
|
+
} else if (argv.includes("web")) name = "web";
|
|
447
|
+
else {
|
|
448
|
+
name = desktopSelectedProfile(env);
|
|
449
|
+
desktop = name !== void 0;
|
|
450
|
+
}
|
|
429
451
|
if (name === void 0) throw new Error("plugin-manager: cannot determine the boot profile; pass --profile <name> or set DSH_PROFILE");
|
|
430
452
|
if (name.includes("/") || name.includes("\\") || name.includes("..")) throw new Error(`plugin-manager: invalid profile name ${JSON.stringify(name)}`);
|
|
431
453
|
const profileDir = join(resolveDshHome(env), "profiles", name);
|
|
@@ -433,7 +455,8 @@ function resolveProfile(argv = process.argv, env = process.env) {
|
|
|
433
455
|
profileName: name,
|
|
434
456
|
profileDir,
|
|
435
457
|
patchPath: join(profileDir, "cordis.patch.yml"),
|
|
436
|
-
packageJsonPath: join(profileDir, "package.json")
|
|
458
|
+
packageJsonPath: join(profileDir, "package.json"),
|
|
459
|
+
desktop
|
|
437
460
|
};
|
|
438
461
|
}
|
|
439
462
|
/**
|
|
@@ -634,24 +657,36 @@ const REMOVE_TIMEOUT_MS = 2 * 6e4;
|
|
|
634
657
|
* pnpm with a cmd.exe shell on Windows, so a spec carrying these can still be
|
|
635
658
|
* re-parsed one layer down (a failed install reported as success, or worse).
|
|
636
659
|
*/
|
|
637
|
-
const UNSAFE_SPEC_CHARS = /[&|<>"'
|
|
660
|
+
const UNSAFE_SPEC_CHARS = /[&|<>"'`%!\n\r\0]/;
|
|
638
661
|
/**
|
|
639
662
|
* Validate an install spec or package id against shell metacharacters.
|
|
640
663
|
* @param spec - the user-supplied spec or id.
|
|
641
664
|
* @returns the rejection message, or undefined when the spec is safe.
|
|
642
665
|
*/
|
|
643
666
|
function unsafeSpecReason(spec) {
|
|
644
|
-
return UNSAFE_SPEC_CHARS.test(spec) ? "plugin-manager: spec 含有危险的 shell
|
|
667
|
+
return UNSAFE_SPEC_CHARS.test(spec) ? "plugin-manager: spec 含有危险的 shell 元字符或控制字符,已拒绝" : void 0;
|
|
645
668
|
}
|
|
646
669
|
/** The binary search roots for the dsh CLI. */
|
|
647
|
-
function findDshBinary(env = process.env, platform = process.platform, exists = existsSync) {
|
|
670
|
+
function findDshBinary(env = process.env, platform = process.platform, exists = existsSync, hostEntryPath = process.argv[1]) {
|
|
648
671
|
const candidates = [];
|
|
649
672
|
const separator = platform === "win32" ? ";" : ":";
|
|
673
|
+
const pathApi = platform === "win32" ? win32 : posix;
|
|
650
674
|
for (const dir of (env.PATH ?? "").split(separator)) {
|
|
651
675
|
if (dir === "") continue;
|
|
652
676
|
if (platform === "win32") candidates.push(`${dir}\\dsh.cmd`, `${dir}\\dsh.exe`);
|
|
653
677
|
else candidates.push(`${dir}/dsh`);
|
|
654
678
|
}
|
|
679
|
+
if (hostEntryPath !== void 0 && hostEntryPath !== "") {
|
|
680
|
+
let current = pathApi.dirname(hostEntryPath);
|
|
681
|
+
for (let depth = 0; depth < 10; depth += 1) {
|
|
682
|
+
const binDir = pathApi.join(current, "node_modules", ".bin");
|
|
683
|
+
if (platform === "win32") candidates.push(pathApi.join(binDir, "dsh.cmd"), pathApi.join(binDir, "dsh.exe"));
|
|
684
|
+
else candidates.push(pathApi.join(binDir, "dsh"));
|
|
685
|
+
const parent = pathApi.dirname(current);
|
|
686
|
+
if (parent === current) break;
|
|
687
|
+
current = parent;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
655
690
|
if (platform === "darwin") candidates.push("/opt/homebrew/bin/dsh", "/usr/local/bin/dsh");
|
|
656
691
|
for (const candidate of candidates) if (exists(candidate)) return candidate;
|
|
657
692
|
return null;
|
|
@@ -678,18 +713,44 @@ function dshSpawnCommand(binary, platform = process.platform, localNodeExists =
|
|
|
678
713
|
};
|
|
679
714
|
const dir = win32.dirname(binary);
|
|
680
715
|
const localNode = win32.join(dir, "node.exe");
|
|
681
|
-
const
|
|
682
|
-
|
|
716
|
+
const binJs = [win32.join(dir, "node_modules", "@deepseek-ai", "dsh", "lib", "bin.js"), win32.join(dir, "..", "@deepseek-ai", "dsh", "lib", "bin.js")].find((candidate) => binJsExists(candidate));
|
|
717
|
+
if (binJs === void 0) return {
|
|
718
|
+
command: binary,
|
|
719
|
+
argsPrefix: []
|
|
720
|
+
};
|
|
683
721
|
return {
|
|
684
722
|
command: localNodeExists(localNode) ? localNode : process.execPath,
|
|
685
723
|
argsPrefix: [binJs]
|
|
686
724
|
};
|
|
687
725
|
}
|
|
726
|
+
/** Build the exact cmd.exe command line required to execute a trusted .cmd shim. */
|
|
727
|
+
function windowsCmdShimArgs(binary, args) {
|
|
728
|
+
const unsafe = /[&|<>"'`%!\n\r\0]/;
|
|
729
|
+
if (/["%\n\r\0]/.test(binary) || args.some((arg) => unsafe.test(arg))) throw new Error("plugin-manager: unsafe Windows command argument");
|
|
730
|
+
return [
|
|
731
|
+
"/d",
|
|
732
|
+
"/s",
|
|
733
|
+
"/c",
|
|
734
|
+
`""${binary}" ${args.map((arg) => `"${arg}"`).join(" ")}"`
|
|
735
|
+
];
|
|
736
|
+
}
|
|
688
737
|
/** Spawn the dsh CLI with piped stdio and no shell parsing (see {@link dshSpawnCommand}). */
|
|
689
738
|
function spawnDsh(binary, args, env) {
|
|
690
739
|
const { command, argsPrefix } = dshSpawnCommand(binary);
|
|
691
|
-
return spawn(
|
|
740
|
+
if (process.platform === "win32" && command.toLowerCase().endsWith(".cmd")) return spawn("cmd.exe", windowsCmdShimArgs(command, args), {
|
|
692
741
|
env,
|
|
742
|
+
windowsVerbatimArguments: true,
|
|
743
|
+
stdio: [
|
|
744
|
+
"ignore",
|
|
745
|
+
"pipe",
|
|
746
|
+
"pipe"
|
|
747
|
+
]
|
|
748
|
+
});
|
|
749
|
+
return spawn(command, [...argsPrefix, ...args], {
|
|
750
|
+
env: command === process.execPath ? {
|
|
751
|
+
...env,
|
|
752
|
+
ELECTRON_RUN_AS_NODE: "1"
|
|
753
|
+
} : env,
|
|
693
754
|
stdio: [
|
|
694
755
|
"ignore",
|
|
695
756
|
"pipe",
|
|
@@ -1228,6 +1289,96 @@ function isLoopbackRequest(request) {
|
|
|
1228
1289
|
}
|
|
1229
1290
|
}
|
|
1230
1291
|
//#endregion
|
|
1292
|
+
//#region src/core/version.ts
|
|
1293
|
+
const VERSION_PATTERN = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/;
|
|
1294
|
+
/**
|
|
1295
|
+
* Parse a DSH host or package version string.
|
|
1296
|
+
* @param value - e.g. `0.1.0-rc.8`, `v1.2.3`.
|
|
1297
|
+
* @returns the parsed version, or undefined when malformed.
|
|
1298
|
+
*/
|
|
1299
|
+
function parseDshVersion(value) {
|
|
1300
|
+
const match = VERSION_PATTERN.exec(value.trim());
|
|
1301
|
+
if (match === null) return void 0;
|
|
1302
|
+
const prereleaseParts = match[4] === void 0 ? [] : match[4].split(".");
|
|
1303
|
+
if (prereleaseParts.some((part) => part === "")) return void 0;
|
|
1304
|
+
return {
|
|
1305
|
+
major: Number(match[1]),
|
|
1306
|
+
minor: Number(match[2]),
|
|
1307
|
+
patch: Number(match[3]),
|
|
1308
|
+
prerelease: prereleaseParts.map((part) => /^\d+$/.test(part) ? Number(part) : part)
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Compare two version strings by semver order (a release is newer than any
|
|
1313
|
+
* prerelease of the same tuple).
|
|
1314
|
+
* @returns -1 / 0 / 1, or undefined when either side is malformed.
|
|
1315
|
+
*/
|
|
1316
|
+
function compareVersions(left, right) {
|
|
1317
|
+
const a = parseDshVersion(left);
|
|
1318
|
+
const b = parseDshVersion(right);
|
|
1319
|
+
if (a === void 0 || b === void 0) return void 0;
|
|
1320
|
+
for (const key of [
|
|
1321
|
+
"major",
|
|
1322
|
+
"minor",
|
|
1323
|
+
"patch"
|
|
1324
|
+
]) if (a[key] !== b[key]) return a[key] < b[key] ? -1 : 1;
|
|
1325
|
+
return comparePrerelease(a.prerelease, b.prerelease);
|
|
1326
|
+
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Semver prerelease ordering: numeric identifiers compare numerically,
|
|
1329
|
+
* numeric identifiers rank below alphanumeric ones, alphanumeric identifiers
|
|
1330
|
+
* compare lexically, and a longer list wins when the shared prefix is equal.
|
|
1331
|
+
*/
|
|
1332
|
+
function comparePrerelease(left, right) {
|
|
1333
|
+
if (left.length === 0 && right.length === 0) return 0;
|
|
1334
|
+
if (left.length === 0) return 1;
|
|
1335
|
+
if (right.length === 0) return -1;
|
|
1336
|
+
const length = Math.max(left.length, right.length);
|
|
1337
|
+
for (let index = 0; index < length; index += 1) {
|
|
1338
|
+
const a = left[index];
|
|
1339
|
+
const b = right[index];
|
|
1340
|
+
if (a === void 0) return -1;
|
|
1341
|
+
if (b === void 0) return 1;
|
|
1342
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
1343
|
+
if (a !== b) return a < b ? -1 : 1;
|
|
1344
|
+
} else if (typeof a === "number") return -1;
|
|
1345
|
+
else if (typeof b === "number") return 1;
|
|
1346
|
+
else if (a !== b) return a < b ? -1 : 1;
|
|
1347
|
+
}
|
|
1348
|
+
return 0;
|
|
1349
|
+
}
|
|
1350
|
+
const MINIMUM_RANGE_PATTERN = /^>=\s*(v?\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/;
|
|
1351
|
+
/**
|
|
1352
|
+
* Whether a host version satisfies a declared `>=X.Y.Z[-prerelease]` minimum.
|
|
1353
|
+
* @param host - the running DSH host version.
|
|
1354
|
+
* @param minimum - the declared minimum; any form other than `>=<semver>`
|
|
1355
|
+
* (plain semver, `^`, `~`, multi-comparator ranges, empty) returns undefined.
|
|
1356
|
+
* @returns true / false, or undefined when the host version is malformed or
|
|
1357
|
+
* the minimum uses an unsupported form — callers treat undefined as
|
|
1358
|
+
* "cannot verify" and fail closed for declared requirements.
|
|
1359
|
+
*/
|
|
1360
|
+
function meetsMinimumDsh(host, minimum) {
|
|
1361
|
+
const match = MINIMUM_RANGE_PATTERN.exec(minimum.trim());
|
|
1362
|
+
if (match === null) return void 0;
|
|
1363
|
+
const compared = compareVersions(host, match[1]);
|
|
1364
|
+
if (compared === void 0) return void 0;
|
|
1365
|
+
return compared >= 0;
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Read the declared DSH minimum from a published registry manifest:
|
|
1369
|
+
* `dsh.engines.dsh` first, top-level `engines.dsh` as the fallback.
|
|
1370
|
+
* Defensive against malformed or untrusted metadata: anything that is not a
|
|
1371
|
+
* non-empty string reads as absent.
|
|
1372
|
+
* @param manifest - the decoded registry version manifest.
|
|
1373
|
+
* @returns the declared minimum, or undefined when not declared.
|
|
1374
|
+
*/
|
|
1375
|
+
function dshRequirementOf(manifest) {
|
|
1376
|
+
const dshEngines = manifest.dsh?.engines;
|
|
1377
|
+
const engines = manifest.engines?.dsh;
|
|
1378
|
+
const value = dshEngines?.dsh ?? engines;
|
|
1379
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
|
|
1380
|
+
}
|
|
1381
|
+
//#endregion
|
|
1231
1382
|
//#region src/host/routes.ts
|
|
1232
1383
|
/** Route prefix the browser half mirrors. */
|
|
1233
1384
|
const GATEWAY_PREFIX = "/api/plugin-manager";
|
|
@@ -1235,6 +1386,14 @@ const GATEWAY_PREFIX = "/api/plugin-manager";
|
|
|
1235
1386
|
const MAX_JSON_BODY_BYTES = 64 * 1024;
|
|
1236
1387
|
/** Registry timeout for one update check. */
|
|
1237
1388
|
const REGISTRY_TIMEOUT_MS = 3e4;
|
|
1389
|
+
/** Deadline for one dsh --version probe. */
|
|
1390
|
+
const VERSION_TIMEOUT_MS = 1e4;
|
|
1391
|
+
/** Grace period after SIGTERM before a stuck probe child is SIGKILLed. */
|
|
1392
|
+
const VERSION_ESCALATION_TIMEOUT_MS = 5e3;
|
|
1393
|
+
/** Successful probe freshness window before the host version is re-read. */
|
|
1394
|
+
const VERSION_PROBE_TTL_MS = 5 * 6e4;
|
|
1395
|
+
/** Minimum gap between failed version probes (avoids a spawn per request). */
|
|
1396
|
+
const VERSION_PROBE_COOLDOWN_MS = 6e4;
|
|
1238
1397
|
/** Error text for a caught request or lifecycle failure. */
|
|
1239
1398
|
function messageOf(error) {
|
|
1240
1399
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -1261,13 +1420,67 @@ async function readJsonBody(req) {
|
|
|
1261
1420
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("plugin-manager: request body must be a JSON object");
|
|
1262
1421
|
return parsed;
|
|
1263
1422
|
}
|
|
1264
|
-
/**
|
|
1265
|
-
|
|
1423
|
+
/** Append bounded probe output. */
|
|
1424
|
+
function captureProbe(chunk, buffer) {
|
|
1425
|
+
buffer.value = (buffer.value + chunk.toString()).slice(-4096);
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
* Default registry manifest probe for npm packages: `/<name>/latest` returns
|
|
1429
|
+
* the full latest-version manifest, so the `dsh` / `engines` compat metadata
|
|
1430
|
+
* rides the same request as the version (no packument needed).
|
|
1431
|
+
*/
|
|
1432
|
+
async function fetchRegistryManifest(name) {
|
|
1266
1433
|
const encoded = name.startsWith("@") ? name.replace("/", "%2F") : name;
|
|
1267
1434
|
const response = await fetch(`https://registry.npmjs.org/${encoded}/latest`, { signal: AbortSignal.timeout(REGISTRY_TIMEOUT_MS) });
|
|
1268
1435
|
if (!response.ok) return void 0;
|
|
1269
1436
|
const body = await response.json();
|
|
1270
|
-
|
|
1437
|
+
if (typeof body.version !== "string") return void 0;
|
|
1438
|
+
return {
|
|
1439
|
+
version: body.version,
|
|
1440
|
+
dsh: body.dsh,
|
|
1441
|
+
engines: body.engines
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Read the running DSH host version through `dsh --version` (the CLI is the
|
|
1446
|
+
* gateway's write path already; this package has no in-process source).
|
|
1447
|
+
* Returns undefined when the binary is unavailable or the output is not a
|
|
1448
|
+
* plain semver; callers treat an unknown host version as a fail-closed
|
|
1449
|
+
* verdict for declared requirements (issue #754).
|
|
1450
|
+
*/
|
|
1451
|
+
async function probeDshVersion(cliAvailable) {
|
|
1452
|
+
if (!cliAvailable()) return void 0;
|
|
1453
|
+
const binary = findDshBinary();
|
|
1454
|
+
if (binary === null) return void 0;
|
|
1455
|
+
const output = { value: "" };
|
|
1456
|
+
const child = spawnDsh(binary, ["--version"], process.env);
|
|
1457
|
+
child.stdout?.on("data", (chunk) => {
|
|
1458
|
+
captureProbe(chunk, output);
|
|
1459
|
+
});
|
|
1460
|
+
child.stderr?.on("data", (chunk) => {
|
|
1461
|
+
captureProbe(chunk, output);
|
|
1462
|
+
});
|
|
1463
|
+
let escalated;
|
|
1464
|
+
const timer = setTimeout(() => {
|
|
1465
|
+
child.kill();
|
|
1466
|
+
escalated = setTimeout(() => {
|
|
1467
|
+
child.kill("SIGKILL");
|
|
1468
|
+
}, VERSION_ESCALATION_TIMEOUT_MS);
|
|
1469
|
+
}, VERSION_TIMEOUT_MS);
|
|
1470
|
+
if (await new Promise((resolve) => {
|
|
1471
|
+
let settled = false;
|
|
1472
|
+
const finish = (value) => {
|
|
1473
|
+
if (settled) return;
|
|
1474
|
+
settled = true;
|
|
1475
|
+
clearTimeout(timer);
|
|
1476
|
+
if (escalated !== void 0) clearTimeout(escalated);
|
|
1477
|
+
resolve(value);
|
|
1478
|
+
};
|
|
1479
|
+
child.once("error", () => finish(null));
|
|
1480
|
+
child.once("close", finish);
|
|
1481
|
+
}) !== 0) return void 0;
|
|
1482
|
+
const version = output.value.trim().split(/\r?\n/, 1)[0]?.trim() ?? "";
|
|
1483
|
+
return parseDshVersion(version) === void 0 ? void 0 : version;
|
|
1271
1484
|
}
|
|
1272
1485
|
/** Whether a dependency spec is a direct npm-registry selector, not an alias or external source. */
|
|
1273
1486
|
function isDirectRegistrySpec(spec) {
|
|
@@ -1280,7 +1493,48 @@ function isDirectRegistrySpec(spec) {
|
|
|
1280
1493
|
*/
|
|
1281
1494
|
function makeGatewayRoutes(deps) {
|
|
1282
1495
|
const { facts, gateway } = deps;
|
|
1283
|
-
const
|
|
1496
|
+
const fetchManifest = deps.fetchManifest ?? fetchRegistryManifest;
|
|
1497
|
+
/**
|
|
1498
|
+
* Cached `dsh --version`: successful verdicts refresh after a TTL (the CLI
|
|
1499
|
+
* update path is the gateway itself, so a stale success is wrong long-term),
|
|
1500
|
+
* failed probes are retried after a cooldown instead of being cached forever,
|
|
1501
|
+
* and concurrent requests share one in-flight probe.
|
|
1502
|
+
*/
|
|
1503
|
+
let dshVersion;
|
|
1504
|
+
let dshVersionAt = 0;
|
|
1505
|
+
let dshVersionPending;
|
|
1506
|
+
const resolveDshVersion = () => {
|
|
1507
|
+
if (deps.dshVersion !== void 0) return deps.dshVersion();
|
|
1508
|
+
const now = Date.now();
|
|
1509
|
+
if (dshVersion !== void 0 && now - dshVersionAt < VERSION_PROBE_TTL_MS) return Promise.resolve(dshVersion);
|
|
1510
|
+
if (dshVersionAt !== 0 && now - dshVersionAt < VERSION_PROBE_COOLDOWN_MS) return Promise.resolve(void 0);
|
|
1511
|
+
if (dshVersionPending === void 0) {
|
|
1512
|
+
dshVersionAt = now;
|
|
1513
|
+
dshVersionPending = probeDshVersion(deps.cliAvailable).catch(() => void 0).then((version) => {
|
|
1514
|
+
dshVersionPending = void 0;
|
|
1515
|
+
if (version !== void 0) {
|
|
1516
|
+
dshVersion = version;
|
|
1517
|
+
dshVersionAt = now;
|
|
1518
|
+
}
|
|
1519
|
+
return version;
|
|
1520
|
+
});
|
|
1521
|
+
}
|
|
1522
|
+
return dshVersionPending;
|
|
1523
|
+
};
|
|
1524
|
+
/**
|
|
1525
|
+
* Compat verdict for one declared requirement. Unverified (unknown host,
|
|
1526
|
+
* malformed host output, unsupported range) is incompatible so an update
|
|
1527
|
+
* can never run against a runtime we cannot prove compatible (issue #754);
|
|
1528
|
+
* only absent metadata keeps the update fail-open.
|
|
1529
|
+
*/
|
|
1530
|
+
const compatibleVerdict = async (requiresDsh) => {
|
|
1531
|
+
const hostVersion = await resolveDshVersion();
|
|
1532
|
+
if (hostVersion === void 0) return { compatible: false };
|
|
1533
|
+
return {
|
|
1534
|
+
hostVersion,
|
|
1535
|
+
compatible: meetsMinimumDsh(hostVersion, requiresDsh) === true
|
|
1536
|
+
};
|
|
1537
|
+
};
|
|
1284
1538
|
/** Wrap a handler with the loopback fence and JSON error reporting. */
|
|
1285
1539
|
const guard = (handler) => async (req, res) => {
|
|
1286
1540
|
if (!isLoopbackRequest(req)) {
|
|
@@ -1342,8 +1596,13 @@ function makeGatewayRoutes(deps) {
|
|
|
1342
1596
|
status: 400,
|
|
1343
1597
|
error: `plugin-manager: ${target} is not a direct npm registry plugin`
|
|
1344
1598
|
};
|
|
1345
|
-
const
|
|
1346
|
-
if (
|
|
1599
|
+
const manifest = await fetchManifest(target).catch(() => void 0);
|
|
1600
|
+
if (manifest === void 0) return {
|
|
1601
|
+
status: 502,
|
|
1602
|
+
error: `plugin-manager: cannot resolve the latest version for ${target}`
|
|
1603
|
+
};
|
|
1604
|
+
const latest = manifest.version;
|
|
1605
|
+
if (latest === "") return {
|
|
1347
1606
|
status: 502,
|
|
1348
1607
|
error: `plugin-manager: cannot resolve the latest version for ${target}`
|
|
1349
1608
|
};
|
|
@@ -1356,6 +1615,14 @@ function makeGatewayRoutes(deps) {
|
|
|
1356
1615
|
status: 409,
|
|
1357
1616
|
error: `plugin-manager: ${target} is already at ${latest}`
|
|
1358
1617
|
};
|
|
1618
|
+
const requiresDsh = dshRequirementOf(manifest);
|
|
1619
|
+
if (requiresDsh !== void 0) {
|
|
1620
|
+
const { hostVersion, compatible } = await compatibleVerdict(requiresDsh);
|
|
1621
|
+
if (!compatible) return {
|
|
1622
|
+
status: 412,
|
|
1623
|
+
error: hostVersion === void 0 ? `plugin-manager: cannot verify the DSH version for ${target} (dsh --version failed); upgrade DSH before updating` : `plugin-manager: ${target} requires DSH ${requiresDsh} (current DSH ${hostVersion}); upgrade DSH before updating`
|
|
1624
|
+
};
|
|
1625
|
+
}
|
|
1359
1626
|
return {
|
|
1360
1627
|
status: 200,
|
|
1361
1628
|
job: gateway.update(target, latest)
|
|
@@ -1411,27 +1678,21 @@ function makeGatewayRoutes(deps) {
|
|
|
1411
1678
|
sendJson(res, 400, { error: unsafeTarget });
|
|
1412
1679
|
return;
|
|
1413
1680
|
}
|
|
1414
|
-
|
|
1681
|
+
const outcome = await gateway.withMutationLock(async () => {
|
|
1415
1682
|
const patchText = await readPatchText(facts.patchPath);
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
name: target
|
|
1419
|
-
}];
|
|
1683
|
+
if ((await readProfileManifest(facts.packageJsonPath)).dependencies[target] === void 0) return { error: `plugin-manager: plugin ${target} is not installed` };
|
|
1684
|
+
const entries = await claimedEntryRowsOf(facts, target);
|
|
1420
1685
|
let next = patchText;
|
|
1421
1686
|
for (const entry of entries) next = setRowEnabled(next, facts.patchPath, entry.id, entry.name, enabled);
|
|
1422
1687
|
if (next !== patchText) await writePatchAtomic(facts.patchPath, next);
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
},
|
|
1432
|
-
installedAt: "",
|
|
1433
|
-
enabled
|
|
1434
|
-
} });
|
|
1688
|
+
const plugin = (await snapshotGateway(facts, next)).plugins.find((item) => item.id === target);
|
|
1689
|
+
return plugin === void 0 ? { error: `plugin-manager: plugin ${target} is not installed` } : { plugin };
|
|
1690
|
+
});
|
|
1691
|
+
if ("error" in outcome) {
|
|
1692
|
+
sendJson(res, 404, { error: outcome.error });
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
sendJson(res, 200, { plugin: outcome.plugin });
|
|
1435
1696
|
};
|
|
1436
1697
|
const failuresHandler = async (req, res) => {
|
|
1437
1698
|
sendJson(res, 200, {
|
|
@@ -1447,7 +1708,8 @@ function makeGatewayRoutes(deps) {
|
|
|
1447
1708
|
return detectOfficialChannels(binary, facts.profileName);
|
|
1448
1709
|
};
|
|
1449
1710
|
const modeHandler = async (req, res) => {
|
|
1450
|
-
if (modePromise === void 0)
|
|
1711
|
+
if (modePromise === void 0) if (facts.desktop) modePromise = Promise.resolve({ official: null });
|
|
1712
|
+
else modePromise = (deps.officialChannels ?? probeOfficialChannels)().then((official) => ({ official })).catch(() => ({ official: false }));
|
|
1451
1713
|
sendJson(res, 200, await modePromise);
|
|
1452
1714
|
};
|
|
1453
1715
|
const checkUpdatesHandler = async (req, res) => {
|
|
@@ -1456,12 +1718,19 @@ function makeGatewayRoutes(deps) {
|
|
|
1456
1718
|
const updates = [];
|
|
1457
1719
|
for (const plugin of snapshot.plugins) {
|
|
1458
1720
|
if (plugin.source.kind !== "npm" || !isDirectRegistrySpec(plugin.source.spec)) continue;
|
|
1459
|
-
const
|
|
1460
|
-
if (
|
|
1721
|
+
const manifest = await fetchManifest(plugin.id).catch(() => void 0);
|
|
1722
|
+
if (manifest === void 0 || manifest.version === plugin.version) continue;
|
|
1723
|
+
const update = {
|
|
1461
1724
|
id: plugin.id,
|
|
1462
1725
|
current: plugin.version,
|
|
1463
|
-
latest
|
|
1464
|
-
}
|
|
1726
|
+
latest: manifest.version
|
|
1727
|
+
};
|
|
1728
|
+
const requiresDsh = dshRequirementOf(manifest);
|
|
1729
|
+
if (requiresDsh !== void 0) {
|
|
1730
|
+
update.requiresDsh = requiresDsh;
|
|
1731
|
+
update.compatible = (await compatibleVerdict(requiresDsh)).compatible;
|
|
1732
|
+
}
|
|
1733
|
+
updates.push(update);
|
|
1465
1734
|
}
|
|
1466
1735
|
sendJson(res, 200, { updates });
|
|
1467
1736
|
};
|
|
@@ -27,6 +27,8 @@ declare const STATIC_ZH: {
|
|
|
27
27
|
checkUpdates: string;
|
|
28
28
|
checking: string;
|
|
29
29
|
noUpdates: string;
|
|
30
|
+
updateRequiresDsh: string;
|
|
31
|
+
updateBlockedDsh: string;
|
|
30
32
|
uninstall: string;
|
|
31
33
|
uninstallConfirmTitle: string;
|
|
32
34
|
uninstallConfirmBody: string;
|
|
@@ -106,6 +108,8 @@ export declare const zh: {
|
|
|
106
108
|
checkUpdates: string;
|
|
107
109
|
checking: string;
|
|
108
110
|
noUpdates: string;
|
|
111
|
+
updateRequiresDsh: string;
|
|
112
|
+
updateBlockedDsh: string;
|
|
109
113
|
uninstall: string;
|
|
110
114
|
uninstallConfirmTitle: string;
|
|
111
115
|
uninstallConfirmBody: string;
|
|
@@ -185,6 +189,8 @@ export declare const en: {
|
|
|
185
189
|
checkUpdates: string;
|
|
186
190
|
checking: string;
|
|
187
191
|
noUpdates: string;
|
|
192
|
+
updateRequiresDsh: string;
|
|
193
|
+
updateBlockedDsh: string;
|
|
188
194
|
uninstall: string;
|
|
189
195
|
uninstallConfirmTitle: string;
|
|
190
196
|
uninstallConfirmBody: string;
|
|
@@ -35,6 +35,10 @@ export interface PluginUpdateItem {
|
|
|
35
35
|
id: string;
|
|
36
36
|
current: string;
|
|
37
37
|
latest: string;
|
|
38
|
+
/** Declared DSH minimum the update needs (package manifest's `dsh.engines.dsh`). */
|
|
39
|
+
requiresDsh?: string;
|
|
40
|
+
/** Whether the running DSH host satisfies requiresDsh; absent when unknown. */
|
|
41
|
+
compatible?: boolean;
|
|
38
42
|
}
|
|
39
43
|
/** One recorded plugin boot failure served by the host. */
|
|
40
44
|
export interface PluginFailureItem {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH host version parsing and comparison for the update compatibility
|
|
3
|
+
* check. The declared minimum lives in the published package manifest under
|
|
4
|
+
* `dsh.engines.dsh`, falling back to the top-level `engines.dsh`; the only
|
|
5
|
+
* supported form is `>=X.Y.Z[-prerelease]`.
|
|
6
|
+
*
|
|
7
|
+
* Comparison deliberately uses plain semver ordering instead of
|
|
8
|
+
* `semver.satisfies`: npm's prerelease rule only allows a prerelease version
|
|
9
|
+
* to satisfy a comparator set carrying a prerelease on the same
|
|
10
|
+
* major.minor.patch tuple, which would reject a newer host line (0.1.1-rc.2)
|
|
11
|
+
* against a declared `>=0.1.0-rc.8` — exactly the cross-cohort upgrade path
|
|
12
|
+
* this check must allow. Everything here is pure and tolerant of untrusted
|
|
13
|
+
* input: a malformed value means "cannot verify", which the callers treat as
|
|
14
|
+
* a fail-closed verdict when a requirement was declared (issue #754).
|
|
15
|
+
* @module @linxin666/dsh-client-ui-plugin-manager/core
|
|
16
|
+
*/
|
|
17
|
+
/** One parsed DSH host or package version. */
|
|
18
|
+
export interface DshVersion {
|
|
19
|
+
major: number;
|
|
20
|
+
minor: number;
|
|
21
|
+
patch: number;
|
|
22
|
+
/** Prerelease identifiers, numeric identifiers kept as numbers (semver order). */
|
|
23
|
+
prerelease: readonly (number | string)[];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse a DSH host or package version string.
|
|
27
|
+
* @param value - e.g. `0.1.0-rc.8`, `v1.2.3`.
|
|
28
|
+
* @returns the parsed version, or undefined when malformed.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseDshVersion(value: string): DshVersion | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Compare two version strings by semver order (a release is newer than any
|
|
33
|
+
* prerelease of the same tuple).
|
|
34
|
+
* @returns -1 / 0 / 1, or undefined when either side is malformed.
|
|
35
|
+
*/
|
|
36
|
+
export declare function compareVersions(left: string, right: string): number | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Whether a host version satisfies a declared `>=X.Y.Z[-prerelease]` minimum.
|
|
39
|
+
* @param host - the running DSH host version.
|
|
40
|
+
* @param minimum - the declared minimum; any form other than `>=<semver>`
|
|
41
|
+
* (plain semver, `^`, `~`, multi-comparator ranges, empty) returns undefined.
|
|
42
|
+
* @returns true / false, or undefined when the host version is malformed or
|
|
43
|
+
* the minimum uses an unsupported form — callers treat undefined as
|
|
44
|
+
* "cannot verify" and fail closed for declared requirements.
|
|
45
|
+
*/
|
|
46
|
+
export declare function meetsMinimumDsh(host: string, minimum: string): boolean | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* The bare minimum version for display: strips the `>=` operator (and an
|
|
49
|
+
* optional leading `v`) from a declared requirement so UI copy that already
|
|
50
|
+
* contains the comparison operator renders `0.1.1-rc.1` instead of
|
|
51
|
+
* `>= >=0.1.1-rc.1`. Unsupported range forms render unchanged.
|
|
52
|
+
* @param minimum - the declared minimum range.
|
|
53
|
+
* @returns the version portion for display.
|
|
54
|
+
*/
|
|
55
|
+
export declare function displayMinimumVersion(minimum: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Read the declared DSH minimum from a published registry manifest:
|
|
58
|
+
* `dsh.engines.dsh` first, top-level `engines.dsh` as the fallback.
|
|
59
|
+
* Defensive against malformed or untrusted metadata: anything that is not a
|
|
60
|
+
* non-empty string reads as absent.
|
|
61
|
+
* @param manifest - the decoded registry version manifest.
|
|
62
|
+
* @returns the declared minimum, or undefined when not declared.
|
|
63
|
+
*/
|
|
64
|
+
export declare function dshRequirementOf(manifest: {
|
|
65
|
+
dsh?: unknown;
|
|
66
|
+
engines?: unknown;
|
|
67
|
+
}): string | undefined;
|
|
@@ -43,7 +43,7 @@ export interface GatewayJob {
|
|
|
43
43
|
error?: string;
|
|
44
44
|
}
|
|
45
45
|
/** The binary search roots for the dsh CLI. */
|
|
46
|
-
export declare function findDshBinary(env?: NodeJS.ProcessEnv, platform?: string, exists?: (path: string) => boolean): string | null;
|
|
46
|
+
export declare function findDshBinary(env?: NodeJS.ProcessEnv, platform?: string, exists?: (path: string) => boolean, hostEntryPath?: string | undefined): string | null;
|
|
47
47
|
/**
|
|
48
48
|
* The spawn command for the dsh CLI on this platform. Windows runs the
|
|
49
49
|
* npm-generated dsh.cmd wrapper by resolving its node binary and bin.js script
|
|
@@ -59,6 +59,8 @@ export declare function dshSpawnCommand(binary: string, platform?: string, local
|
|
|
59
59
|
command: string;
|
|
60
60
|
argsPrefix: string[];
|
|
61
61
|
};
|
|
62
|
+
/** Build the exact cmd.exe command line required to execute a trusted .cmd shim. */
|
|
63
|
+
export declare function windowsCmdShimArgs(binary: string, args: readonly string[]): string[];
|
|
62
64
|
/** Spawn the dsh CLI with piped stdio and no shell parsing (see {@link dshSpawnCommand}). */
|
|
63
65
|
export declare function spawnDsh(binary: string, args: string[], env: NodeJS.ProcessEnv): import("child_process").ChildProcessByStdio<null, import("stream").Readable, import("stream").Readable>;
|
|
64
66
|
/**
|
|
@@ -16,7 +16,11 @@ export interface ProfileFacts {
|
|
|
16
16
|
patchPath: string;
|
|
17
17
|
/** Absolute path of the profile's package.json. */
|
|
18
18
|
packageJsonPath: string;
|
|
19
|
+
/** True when the profile was inferred from the packaged desktop host. */
|
|
20
|
+
desktop?: boolean;
|
|
19
21
|
}
|
|
22
|
+
/** Read the packaged desktop app's persisted active profile, when present. */
|
|
23
|
+
export declare function desktopSelectedProfile(env?: NodeJS.ProcessEnv): string | undefined;
|
|
20
24
|
/**
|
|
21
25
|
* Resolve the boot profile name from the host argv: an explicit `--profile`
|
|
22
26
|
* flag wins, then the DSH_PROFILE environment override, then the `web`
|
|
@@ -19,11 +19,22 @@ export interface GatewayRouteDeps {
|
|
|
19
19
|
gateway: CliGateway;
|
|
20
20
|
/** Resolve the dsh binary presence (the CLI is the write path). */
|
|
21
21
|
cliAvailable: () => boolean;
|
|
22
|
-
/** Registry fetch seam for update checks (test seam)
|
|
23
|
-
|
|
22
|
+
/** Registry fetch seam for update checks (test seam); the default reads the
|
|
23
|
+
* `/<name>/latest` manifest including the `dsh` / `engines` metadata. */
|
|
24
|
+
fetchManifest?: (name: string) => Promise<RegistryVersionManifest | undefined>;
|
|
25
|
+
/** Running DSH host version seam (test seam); the default probes `dsh --version`. */
|
|
26
|
+
dshVersion?: () => Promise<string | undefined>;
|
|
24
27
|
/** Official-channel detection seam (test seam); defaults to the boot dump probe. */
|
|
25
28
|
officialChannels?: () => Promise<boolean>;
|
|
26
29
|
}
|
|
30
|
+
/** The published `/latest` manifest: version plus the compat metadata fields. */
|
|
31
|
+
export interface RegistryVersionManifest {
|
|
32
|
+
version: string;
|
|
33
|
+
/** Untrusted package manifest `dsh` object (bundle / client / engines). */
|
|
34
|
+
dsh?: unknown;
|
|
35
|
+
/** Untrusted package manifest `engines` object (node / dsh). */
|
|
36
|
+
engines?: unknown;
|
|
37
|
+
}
|
|
27
38
|
/**
|
|
28
39
|
* Build the gateway routes.
|
|
29
40
|
* @param deps - profile facts, the CLI gateway, and seams.
|