@sparkelf/dsh-plus 0.2.9-alpha.53 → 0.2.9-alpha.55

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.
@@ -156,6 +156,32 @@ function requireSourceBase(root, baseRevision) {
156
156
  if (checkoutRevision !== baseRevision) throw new Error("DSH checkout must be exact official base " + baseRevision + ", found " + checkoutRevision);
157
157
  return checkoutRevision;
158
158
  }
159
+ /**
160
+ * Names of the packages that carry `dshPatch` variants.
161
+ *
162
+ * The distribution moved from a flat `dshPlus.patchPackages` list to `dshPlus.patchSet`, whose
163
+ * entries name their target packages per patch. Older tarballs still carry the flat list, so it
164
+ * wins when present; otherwise the union of every entry's `target.packages` is used.
165
+ *
166
+ * @param plus - Parsed `dshPlus` block of the distribution manifest.
167
+ */
168
+ function resolvePatchPackageNames(plus) {
169
+ const declared = plus.patchPackages;
170
+ if (declared !== void 0) {
171
+ const names = requireStringArray(declared, "dshPlus.patchPackages");
172
+ if (names.length > 0) return names;
173
+ }
174
+ const entries = requireObject(plus.patchSet, "dshPlus.patchSet").entries;
175
+ if (!Array.isArray(entries) || entries.length === 0) throw new Error("dshPlus.patchSet.entries must be a non-empty array");
176
+ const names = /* @__PURE__ */ new Set();
177
+ for (const [index, value] of entries.entries()) {
178
+ const target = requireObject(requireObject(value, "dshPlus.patchSet.entries[" + String(index) + "]").target, "dshPlus.patchSet.entries[" + String(index) + "].target");
179
+ if (!Array.isArray(target.packages)) continue;
180
+ for (const name of requireStringArray(target.packages, "dshPlus.patchSet.entries[" + String(index) + "].target.packages")) names.add(name);
181
+ }
182
+ if (names.size === 0) throw new Error("dshPlus.patchSet must name at least one patch package");
183
+ return [...names].sort();
184
+ }
159
185
  function patchPackageVariants(distributionDirectory, names) {
160
186
  const variants = [];
161
187
  for (const name of names) {
@@ -186,7 +212,7 @@ function prepareSelection(distributionDirectory, profileDirectory, dshRoot) {
186
212
  if (typeof allowed !== "boolean") throw new Error("dshPlus.profile.allowBuilds." + name + " must be a boolean");
187
213
  allowBuilds[name] = allowed;
188
214
  }
189
- const patchPackageNames = requireStringArray(plus.patchPackages, "dshPlus.patchPackages");
215
+ const patchPackageNames = resolvePatchPackageNames(plus);
190
216
  const dependencies = requireObject(distributionManifest.dependencies, "Plus distribution dependencies");
191
217
  const dshVersion = requireVersion(readJsonObject(resolve(dshRoot, "package.json"), "DSH source manifest").version, "DSH source version");
192
218
  if (!satisfies(dshVersion, dshRange, { includePrerelease: true })) throw new Error(distributionName + "@" + distributionVersion + " requires DSH " + dshRange + ", found " + dshVersion);
@@ -363,6 +389,46 @@ function profilePatchConfiguration(profileDirectory, npmPatchFiles) {
363
389
  changed
364
390
  };
365
391
  }
392
+ /**
393
+ * Profile-local pnpm hook that keeps the CDN-only xlsx dependency installable.
394
+ *
395
+ * `@deepseek-ai/dsh-client-ui-sidebar-documentpreview` declares `xlsx` as
396
+ * `https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz`. SheetJS stopped publishing to npm
397
+ * after 0.18.5, so no registry mirror carries 0.20.3, and the build network cannot reach that
398
+ * CDN (measured 2026-11-06: every request times out). The image vendors the tarball at
399
+ * /opt/dsh-vendor; this hook rewrites the spec wherever it appears so a cold profile install
400
+ * never requests the CDN.
401
+ *
402
+ * Written into the profile root rather than a template: the Plus bundle has no profile
403
+ * template directory, the profile manifest and pnpm-workspace.yaml are generated here.
404
+ */
405
+ const PROFILE_PNPMFILE = `// Generated by @sparkelf/dsh-plus -- do not edit; rewritten on every apply.
406
+ const VENDORED_XLSX = '/opt/dsh-vendor/xlsx-0.20.3.tgz'
407
+ const CDN_XLSX = /^https?:\\/\\/cdn\\.sheetjs\\.com\\//
408
+ const FIELDS = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']
409
+
410
+ module.exports = {
411
+ hooks: {
412
+ readPackage(pkg) {
413
+ for (const field of FIELDS) {
414
+ const deps = pkg && pkg[field]
415
+ if (deps && typeof deps.xlsx === 'string' && CDN_XLSX.test(deps.xlsx)) {
416
+ deps.xlsx = 'file:' + VENDORED_XLSX
417
+ }
418
+ }
419
+ return pkg
420
+ },
421
+ },
422
+ }
423
+ `;
424
+ /** Write the profile pnpmfile when its content changed; returns whether it wrote. */
425
+ function writeProfilePnpmfile(profileDirectory) {
426
+ const path = resolve(profileDirectory, ".pnpmfile.cjs");
427
+ if ((existsSync(path) ? readFileSync(path, "utf8") : void 0) === PROFILE_PNPMFILE) return false;
428
+ writeFileSync(path, PROFILE_PNPMFILE);
429
+ console.log("profile pnpmfile: xlsx -> /opt/dsh-vendor/xlsx-0.20.3.tgz");
430
+ return true;
431
+ }
366
432
  function writeProfileRequirements(profileDirectory, dependencySpecs, allowBuilds) {
367
433
  const manifestPath = resolve(profileDirectory, "package.json");
368
434
  const manifest = readJsonObject(manifestPath, "DSH profile manifest");
@@ -413,7 +479,8 @@ function writeProfileRequirements(profileDirectory, dependencySpecs, allowBuilds
413
479
  writeFileSync(manifestPath, JSON.stringify(manifest, void 0, 2) + "\n");
414
480
  }
415
481
  if (workspaceChanged) writeFileSync(workspacePath, String(document));
416
- return manifestChanged || workspaceChanged;
482
+ const pnpmfileChanged = writeProfilePnpmfile(profileDirectory);
483
+ return manifestChanged || workspaceChanged || pnpmfileChanged;
417
484
  }
418
485
  function writeProfileComposition(profileDirectory, bundles, runtimePackages) {
419
486
  const path = resolve(profileDirectory, "package.json");
@@ -598,4 +665,4 @@ function runApply(argv) {
598
665
  process.stdout.write("[dsh-plus] applied " + String(selection.patches.length) + " patches; lock " + lock + "\n");
599
666
  }
600
667
  //#endregion
601
- export { parseOfficialWorkspacePackages as n, runApply as r, materializeOfficialPackageScope as t };
668
+ export { runApply as i, parseOfficialWorkspacePackages as n, resolvePatchPackageNames as r, materializeOfficialPackageScope as t };
package/lib/apply.js CHANGED
@@ -1,2 +1,2 @@
1
- import { n as parseOfficialWorkspacePackages, r as runApply, t as materializeOfficialPackageScope } from "./apply-08huHfLG.js";
2
- export { materializeOfficialPackageScope, parseOfficialWorkspacePackages, runApply };
1
+ import { i as runApply, n as parseOfficialWorkspacePackages, r as resolvePatchPackageNames, t as materializeOfficialPackageScope } from "./apply-DpmD0ljp.js";
2
+ export { materializeOfficialPackageScope, parseOfficialWorkspacePackages, resolvePatchPackageNames, runApply };
package/lib/bin.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
- import { i as readPatchSet, r as runApply } from "./apply-08huHfLG.js";
2
+ import { i as runApply } from "./apply-DpmD0ljp.js";
3
3
  import { createRequire } from "node:module";
4
4
  import { execFileSync, spawn, spawnSync } from "node:child_process";
5
5
  import { createWriteStream, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
6
- import { dirname, join, posix, resolve, win32 } from "node:path";
6
+ import { dirname, isAbsolute, join, posix, relative, resolve, win32 } from "node:path";
7
7
  import { homedir } from "node:os";
8
8
  import semver, { valid, validRange } from "semver";
9
9
  import { parseDocument } from "yaml";
@@ -1084,6 +1084,125 @@ async function waitForAuthenticatedUrl(logPath, timeoutMilliseconds) {
1084
1084
  }
1085
1085
  }
1086
1086
  //#endregion
1087
+ //#region lib/types/patch-set.js
1088
+ /**
1089
+ * Read the Plus distribution's embedded patch set.
1090
+ *
1091
+ * The distribution owns one patch set for official DSH and, separately, the patches it
1092
+ * carries for third-party packages. Both live inside the distribution package: the
1093
+ * manifest declares entries, and each entry's payload is a file under `patches/`. A
1094
+ * consumer resolves no independently published patch package.
1095
+ *
1096
+ * The two lists are governed differently. A Plus entry must target an official package
1097
+ * that the profile republishes and overrides; a third-party entry must name its upstream
1098
+ * and a retirement condition, and never becomes part of the official patch set.
1099
+ */
1100
+ function object(value, label) {
1101
+ if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error(label + " must be an object");
1102
+ return value;
1103
+ }
1104
+ function string(value, label) {
1105
+ if (typeof value !== "string" || value === "") throw new Error(label + " must be a non-empty string");
1106
+ return value;
1107
+ }
1108
+ function range(value, label) {
1109
+ const text = string(value, label);
1110
+ if (validRange(text) === null) throw new Error(label + " is not a semantic-version range: " + text);
1111
+ return text;
1112
+ }
1113
+ /** A payload stays inside the distribution package it is declared by. */
1114
+ function patchFile(distributionDirectory, declared, label) {
1115
+ const absolute = resolve(distributionDirectory, declared);
1116
+ const local = relative(distributionDirectory, absolute);
1117
+ if (local === "" || isAbsolute(local) || local === ".." || local.startsWith("../")) throw new Error(label + " must stay inside the distribution package: " + declared);
1118
+ if (!existsSync(absolute)) throw new Error(label + " does not exist: " + absolute);
1119
+ return local.replaceAll("\\", "/");
1120
+ }
1121
+ function sourceTarget(value, sourceBaseRevision, label) {
1122
+ const target = object(value, label);
1123
+ if (target.kind !== "dsh-source") throw new Error(label + ".kind must be dsh-source");
1124
+ if (!Array.isArray(target.paths) || target.paths.length === 0) throw new Error(label + ".paths must be a non-empty array");
1125
+ const paths = target.paths.map((entry, index) => {
1126
+ const path = string(entry, label + ".paths[" + String(index) + "]");
1127
+ if (!path.endsWith("/")) throw new Error(label + ".paths entries must be repository directory prefixes");
1128
+ return path;
1129
+ });
1130
+ if (!Array.isArray(target.packages) || target.packages.length === 0) throw new Error(label + ".packages must be a non-empty array");
1131
+ return {
1132
+ kind: "dsh-source",
1133
+ baseRevision: sourceBaseRevision,
1134
+ packages: target.packages.map((entry, index) => {
1135
+ const name = string(entry, label + ".packages[" + String(index) + "]");
1136
+ if (!name.startsWith("@deepseek-ai/")) throw new Error(label + ".packages entries must be official package names");
1137
+ return name;
1138
+ }),
1139
+ paths
1140
+ };
1141
+ }
1142
+ function npmTarget(value, label) {
1143
+ const target = object(value, label);
1144
+ if (target.kind !== "npm") throw new Error(label + ".kind must be npm");
1145
+ return {
1146
+ kind: "npm",
1147
+ name: string(target.name, label + ".name"),
1148
+ range: range(target.range, label + ".range")
1149
+ };
1150
+ }
1151
+ function entry(value, scope, sourceBaseRevision, distributionDirectory, label) {
1152
+ const record = object(value, label);
1153
+ const id = string(record.id, label + ".id");
1154
+ const dsh = range(record.dsh, label + ".dsh");
1155
+ const file = patchFile(distributionDirectory, string(record.file, label + ".file"), label + ".file");
1156
+ const retireWhen = string(record.retireWhen, label + ".retireWhen");
1157
+ const targetLabel = label + ".target";
1158
+ const target = scope === "plus" ? object(record.target, targetLabel).kind === "dsh-source" ? sourceTarget(record.target, sourceBaseRevision, targetLabel) : npmTarget(record.target, targetLabel) : npmTarget(record.target, targetLabel);
1159
+ if (scope === "third-party") return {
1160
+ id,
1161
+ scope,
1162
+ dsh,
1163
+ file,
1164
+ retireWhen,
1165
+ upstreamUrl: string(record.upstreamUrl, label + ".upstreamUrl"),
1166
+ target
1167
+ };
1168
+ return {
1169
+ id,
1170
+ scope,
1171
+ dsh,
1172
+ file,
1173
+ retireWhen,
1174
+ target
1175
+ };
1176
+ }
1177
+ /**
1178
+ * Read and validate the embedded patch set from an installed distribution.
1179
+ * @param distributionDirectory - installed `@sparkelf/dsh-plus` directory.
1180
+ * @param manifest - its parsed package manifest.
1181
+ * @returns the ordered Plus entries, third-party entries, and the exact source base.
1182
+ */
1183
+ function readPatchSet(distributionDirectory, manifest) {
1184
+ const plus = object(manifest.dshPlus, "dshPlus");
1185
+ const sourceBaseRevision = string(object(plus.sourceBase, "dshPlus.sourceBase").revision, "dshPlus.sourceBase.revision");
1186
+ const declaration = object(plus.patchSet, "dshPlus.patchSet");
1187
+ if (declaration.formatVersion !== 1) throw new Error("dshPlus.patchSet.formatVersion must be 1");
1188
+ if (!Array.isArray(declaration.entries) || declaration.entries.length === 0) throw new Error("dshPlus.patchSet.entries must be a non-empty array");
1189
+ const entries = declaration.entries.map((value, index) => entry(value, "plus", sourceBaseRevision, distributionDirectory, "dshPlus.patchSet.entries[" + String(index) + "]"));
1190
+ const thirdPartyRaw = plus.thirdPartyPatches;
1191
+ const thirdParty = thirdPartyRaw === void 0 ? [] : Array.isArray(thirdPartyRaw) ? thirdPartyRaw.map((value, index) => entry(value, "third-party", sourceBaseRevision, distributionDirectory, "dshPlus.thirdPartyPatches[" + String(index) + "]")) : (() => {
1192
+ throw new Error("dshPlus.thirdPartyPatches must be an array");
1193
+ })();
1194
+ const ids = /* @__PURE__ */ new Set();
1195
+ for (const item of [...entries, ...thirdParty]) {
1196
+ if (ids.has(item.id)) throw new Error("duplicate patch entry id: " + item.id);
1197
+ ids.add(item.id);
1198
+ }
1199
+ return {
1200
+ entries,
1201
+ thirdPartyPatches: thirdParty,
1202
+ sourceBaseRevision
1203
+ };
1204
+ }
1205
+ //#endregion
1087
1206
  //#region lib/types/profile.js
1088
1207
  /**
1089
1208
  * Runtime paths and profile composition for a standalone Plus installation.
@@ -1,9 +1,19 @@
1
- /** Materialize the Plus distribution's embedded patch set without Desktop. */
1
+ /** Materialize the Plus distribution's independent patch packages without Desktop. */
2
2
  /** One official workspace package available as a profile peer dependency. */
3
3
  export interface OfficialWorkspacePackage {
4
4
  name: string;
5
5
  directory: string;
6
6
  }
7
+ /**
8
+ * Names of the packages that carry `dshPatch` variants.
9
+ *
10
+ * The distribution moved from a flat `dshPlus.patchPackages` list to `dshPlus.patchSet`, whose
11
+ * entries name their target packages per patch. Older tarballs still carry the flat list, so it
12
+ * wins when present; otherwise the union of every entry's `target.packages` is used.
13
+ *
14
+ * @param plus - Parsed `dshPlus` block of the distribution manifest.
15
+ */
16
+ export declare function resolvePatchPackageNames(plus: Record<string, unknown>): string[];
7
17
  /**
8
18
  * Parse pnpm's workspace roster into exact official package directories.
9
19
  * @param dshRoot - exact official DSH checkout root.
@@ -1,12 +1,11 @@
1
- /** Materialize the Plus distribution's embedded patch set without Desktop. */
1
+ /** Materialize the Plus distribution's independent patch packages without Desktop. */
2
2
  import { spawnSync } from 'node:child_process';
3
3
  import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, symlinkSync, writeFileSync, } from 'node:fs';
4
4
  import { createRequire } from 'node:module';
5
- import { basename, dirname, isAbsolute, relative, resolve } from 'node:path';
5
+ import { dirname, isAbsolute, relative, resolve } from 'node:path';
6
6
  import { tmpdir } from 'node:os';
7
7
  import { satisfies, valid, validRange } from 'semver';
8
8
  import { parseDocument } from 'yaml';
9
- import { readPatchSet } from "./patch-set.js";
10
9
  const FORMAT_VERSION = 1;
11
10
  function isSelectedNpmPatch(patch) {
12
11
  return patch.target.kind === 'npm';
@@ -51,22 +50,7 @@ function requireStringArray(value, label) {
51
50
  }
52
51
  function resolveInstalledPackage(distributionDirectory, packageName) {
53
52
  const requireFromDistribution = createRequire(resolve(distributionDirectory, 'package.json'));
54
- let manifestPath;
55
- try {
56
- manifestPath = requireFromDistribution.resolve(packageName + '/package.json');
57
- }
58
- catch {
59
- // Some packages do not export `./package.json`; resolve the entry module instead and walk
60
- // up to the directory that holds the manifest.
61
- let directory = dirname(requireFromDistribution.resolve(packageName));
62
- while (!existsSync(resolve(directory, 'package.json'))) {
63
- const parent = dirname(directory);
64
- if (parent === directory)
65
- throw new Error(packageName + ' has no resolvable package manifest');
66
- directory = parent;
67
- }
68
- manifestPath = resolve(directory, 'package.json');
69
- }
53
+ const manifestPath = requireFromDistribution.resolve(packageName + '/package.json');
70
54
  const manifest = readJsonObject(manifestPath, packageName + ' manifest');
71
55
  return {
72
56
  name: packageName,
@@ -75,6 +59,48 @@ function resolveInstalledPackage(distributionDirectory, packageName) {
75
59
  manifest,
76
60
  };
77
61
  }
62
+ function resolvePatchFile(packageDirectory, declared) {
63
+ const absoluteFile = resolve(packageDirectory, declared);
64
+ const local = relative(packageDirectory, absoluteFile);
65
+ if (isAbsolute(local) || local === '..' || local.startsWith('../')) {
66
+ throw new Error('patch file must stay inside its patch package: ' + declared);
67
+ }
68
+ if (!existsSync(absoluteFile))
69
+ throw new Error('patch file does not exist: ' + absoluteFile);
70
+ return { relativeFile: local.replaceAll('\\', '/'), absoluteFile };
71
+ }
72
+ function parseVariant(value, index, patchPackage) {
73
+ const label = patchPackage.name + ' dshPatch.variants[' + String(index) + ']';
74
+ const variant = requireObject(value, label);
75
+ const target = requireObject(variant.target, label + '.target');
76
+ const kind = requireString(target.kind, label + '.target.kind');
77
+ const common = {
78
+ id: requireString(variant.id, label + '.id'),
79
+ dsh: requireRange(variant.dsh, label + '.dsh'),
80
+ file: requireString(variant.file, label + '.file'),
81
+ patchPackage,
82
+ };
83
+ if (kind === 'npm') {
84
+ return {
85
+ ...common,
86
+ target: {
87
+ kind,
88
+ name: requireString(target.name, label + '.target.name'),
89
+ range: requireRange(target.range, label + '.target.range'),
90
+ },
91
+ };
92
+ }
93
+ if (kind === 'dsh-source') {
94
+ return {
95
+ ...common,
96
+ target: {
97
+ kind,
98
+ baseRevision: requireString(target.baseRevision, label + '.target.baseRevision'),
99
+ },
100
+ };
101
+ }
102
+ throw new Error('unsupported patch target kind: ' + kind);
103
+ }
78
104
  function git(root, args, acceptFailure = false) {
79
105
  const result = spawnSync('git', args, { cwd: root, encoding: 'utf8' });
80
106
  if (result.status === 0)
@@ -122,6 +148,58 @@ function requireSourceBase(root, baseRevision) {
122
148
  }
123
149
  return checkoutRevision;
124
150
  }
151
+ /**
152
+ * Names of the packages that carry `dshPatch` variants.
153
+ *
154
+ * The distribution moved from a flat `dshPlus.patchPackages` list to `dshPlus.patchSet`, whose
155
+ * entries name their target packages per patch. Older tarballs still carry the flat list, so it
156
+ * wins when present; otherwise the union of every entry's `target.packages` is used.
157
+ *
158
+ * @param plus - Parsed `dshPlus` block of the distribution manifest.
159
+ */
160
+ export function resolvePatchPackageNames(plus) {
161
+ const declared = plus.patchPackages;
162
+ if (declared !== undefined) {
163
+ const names = requireStringArray(declared, 'dshPlus.patchPackages');
164
+ if (names.length > 0)
165
+ return names;
166
+ }
167
+ const patchSet = requireObject(plus.patchSet, 'dshPlus.patchSet');
168
+ const entries = patchSet.entries;
169
+ if (!Array.isArray(entries) || entries.length === 0) {
170
+ throw new Error('dshPlus.patchSet.entries must be a non-empty array');
171
+ }
172
+ const names = new Set();
173
+ for (const [index, value] of entries.entries()) {
174
+ const entry = requireObject(value, 'dshPlus.patchSet.entries[' + String(index) + ']');
175
+ const target = requireObject(entry.target, 'dshPlus.patchSet.entries[' + String(index) + '].target');
176
+ if (!Array.isArray(target.packages))
177
+ continue;
178
+ for (const name of requireStringArray(target.packages, 'dshPlus.patchSet.entries[' + String(index) + '].target.packages')) {
179
+ names.add(name);
180
+ }
181
+ }
182
+ if (names.size === 0)
183
+ throw new Error('dshPlus.patchSet must name at least one patch package');
184
+ return [...names].sort();
185
+ }
186
+ function patchPackageVariants(distributionDirectory, names) {
187
+ const variants = [];
188
+ for (const name of names) {
189
+ const patchPackage = resolveInstalledPackage(distributionDirectory, name);
190
+ const declaration = requireObject(patchPackage.manifest.dshPatch, name + ' dshPatch');
191
+ if (declaration.formatVersion !== FORMAT_VERSION) {
192
+ throw new Error('unsupported dshPatch formatVersion in ' + name + ': ' + String(declaration.formatVersion));
193
+ }
194
+ if (!Array.isArray(declaration.variants) || declaration.variants.length === 0) {
195
+ throw new Error(name + ' dshPatch.variants must be a non-empty array');
196
+ }
197
+ for (const [index, value] of declaration.variants.entries()) {
198
+ variants.push(parseVariant(value, index, patchPackage));
199
+ }
200
+ }
201
+ return variants;
202
+ }
125
203
  // 先验证official source,再物化Plus声明的profile依赖,避免source不匹配时改动用户profile。
126
204
  function prepareSelection(distributionDirectory, profileDirectory, dshRoot) {
127
205
  const distributionManifest = readJsonObject(resolve(distributionDirectory, 'package.json'), 'Plus distribution manifest');
@@ -149,24 +227,31 @@ function prepareSelection(distributionDirectory, profileDirectory, dshRoot) {
149
227
  throw new Error('dshPlus.profile.allowBuilds.' + name + ' must be a boolean');
150
228
  allowBuilds[name] = allowed;
151
229
  }
152
- const patchSet = readPatchSet(distributionDirectory, distributionManifest);
230
+ const patchPackageNames = resolvePatchPackageNames(plus);
153
231
  const dependencies = requireObject(distributionManifest.dependencies, 'Plus distribution dependencies');
154
232
  const dshManifest = readJsonObject(resolve(dshRoot, 'package.json'), 'DSH source manifest');
155
233
  const dshVersion = requireVersion(dshManifest.version, 'DSH source version');
156
234
  if (!satisfies(dshVersion, dshRange, { includePrerelease: true })) {
157
235
  throw new Error(distributionName + '@' + distributionVersion + ' requires DSH ' + dshRange + ', found ' + dshVersion);
158
236
  }
159
- const checkoutRevision = requireSourceBase(dshRoot, patchSet.sourceBaseRevision);
160
- const patchEntries = [...patchSet.entries, ...patchSet.thirdPartyPatches];
237
+ const checkoutRevision = requireSourceBase(dshRoot, baseRevision);
238
+ const groups = new Map();
239
+ for (const variant of patchPackageVariants(distributionDirectory, patchPackageNames)) {
240
+ const entries = groups.get(variant.id) ?? [];
241
+ entries.push(variant);
242
+ groups.set(variant.id, entries);
243
+ }
161
244
  const sourceStates = new Map();
162
- for (const item of patchEntries) {
163
- if (!satisfies(dshVersion, item.dsh, { includePrerelease: true })) {
164
- throw new Error('patch ' + item.id + ' declares DSH ' + item.dsh + ', found ' + dshVersion);
245
+ const orderedGroups = [...groups].sort(([left], [right]) => left.localeCompare(right));
246
+ for (const [, variants] of orderedGroups) {
247
+ for (const variant of variants) {
248
+ if (variant.target.kind !== 'dsh-source'
249
+ || variant.target.baseRevision !== baseRevision
250
+ || !satisfies(dshVersion, variant.dsh, { includePrerelease: true }))
251
+ continue;
252
+ const file = resolvePatchFile(variant.patchPackage.directory, variant.file);
253
+ sourceStates.set(file.absoluteFile, sourcePatchState(dshRoot, file.absoluteFile));
165
254
  }
166
- if (item.target.kind !== 'dsh-source')
167
- continue;
168
- const absoluteFile = resolve(distributionDirectory, item.file);
169
- sourceStates.set(absoluteFile, sourcePatchState(dshRoot, absoluteFile));
170
255
  }
171
256
  const pendingSourceFiles = [...sourceStates].flatMap(([file, state]) => state === 'pending' ? [file] : []);
172
257
  preflightSourcePatches(dshRoot, pendingSourceFiles);
@@ -177,38 +262,49 @@ function prepareSelection(distributionDirectory, profileDirectory, dshRoot) {
177
262
  return { name, version: installed.version };
178
263
  });
179
264
  const runtimePackages = [
180
- ...Object.keys(dependencies).sort().map((name) => {
265
+ ...Object.keys(dependencies).filter(name => !patchPackageNames.includes(name)).sort().map((name) => {
181
266
  const installed = resolveInstalledPackage(distributionDirectory, name);
182
267
  return { name, version: installed.version };
183
268
  }),
184
269
  ...profileDependencies,
185
270
  ].sort((left, right) => left.name.localeCompare(right.name));
186
271
  const selected = [];
187
- for (const item of patchEntries) {
188
- const absoluteFile = resolve(distributionDirectory, item.file);
189
- if (item.target.kind === 'dsh-source') {
190
- selected.push({
191
- id: item.id,
192
- scope: item.scope,
193
- file: item.file,
194
- absoluteFile,
195
- target: { kind: 'dsh-source', baseRevision: patchSet.sourceBaseRevision },
196
- alreadyApplied: sourceStates.get(absoluteFile) === 'applied',
197
- });
198
- continue;
199
- }
200
- const targetDirectory = item.target.name in profileDependencySpecs ? profileDirectory : distributionDirectory;
201
- const targetVersion = resolveInstalledPackage(targetDirectory, item.target.name).version;
202
- if (!satisfies(targetVersion, item.target.range, { includePrerelease: true })) {
203
- throw new Error('patch ' + item.id + ' targets ' + item.target.name + ' ' + item.target.range + ', found ' + targetVersion);
204
- }
205
- selected.push({
206
- id: item.id,
207
- scope: item.scope,
208
- file: item.file,
209
- absoluteFile,
210
- target: { kind: 'npm', name: item.target.name, version: targetVersion },
272
+ // 每个patch id必须在当前DSH、official base与installed npm graph上唯一命中,不能fallback到第二variant。
273
+ for (const [id, variants] of orderedGroups) {
274
+ const candidates = variants.flatMap((variant) => {
275
+ if (!satisfies(dshVersion, variant.dsh, { includePrerelease: true }))
276
+ return [];
277
+ const file = resolvePatchFile(variant.patchPackage.directory, variant.file);
278
+ if (variant.target.kind === 'dsh-source') {
279
+ if (variant.target.baseRevision !== baseRevision)
280
+ return [];
281
+ return [{
282
+ id,
283
+ patchPackage: { name: variant.patchPackage.name, version: variant.patchPackage.version },
284
+ file: file.relativeFile,
285
+ absoluteFile: file.absoluteFile,
286
+ target: { kind: 'dsh-source', baseRevision },
287
+ alreadyApplied: sourceStates.get(file.absoluteFile) === 'applied',
288
+ }];
289
+ }
290
+ const targetDirectory = variant.target.name in profileDependencySpecs ? profileDirectory : distributionDirectory;
291
+ const targetVersion = resolveInstalledPackage(targetDirectory, variant.target.name).version;
292
+ if (!satisfies(targetVersion, variant.target.range, { includePrerelease: true }))
293
+ return [];
294
+ return [{
295
+ id,
296
+ patchPackage: { name: variant.patchPackage.name, version: variant.patchPackage.version },
297
+ file: file.relativeFile,
298
+ absoluteFile: file.absoluteFile,
299
+ target: { kind: 'npm', name: variant.target.name, version: targetVersion },
300
+ }];
211
301
  });
302
+ const [candidate] = candidates;
303
+ if (candidate === undefined)
304
+ throw new Error('no compatible patch variant for ' + id + ' on DSH ' + dshVersion);
305
+ if (candidates.length > 1)
306
+ throw new Error('multiple compatible patch variants for ' + id + ' on DSH ' + dshVersion);
307
+ selected.push(candidate);
212
308
  }
213
309
  const lockPatches = selected.map((patch) => {
214
310
  if ('alreadyApplied' in patch) {
@@ -238,7 +334,7 @@ function materializePatchFiles(profileDirectory, patches) {
238
334
  const root = resolve(profileDirectory, '.dsh-plus', 'patches');
239
335
  rmSync(root, { recursive: true, force: true });
240
336
  const stablePatches = patches.map((patch) => {
241
- const destination = resolve(root, patch.scope, patch.id, basename(patch.file));
337
+ const destination = resolve(root, patch.patchPackage.name, patch.patchPackage.version, patch.file);
242
338
  mkdirSync(dirname(destination), { recursive: true });
243
339
  copyFileSync(patch.absoluteFile, destination);
244
340
  return { ...patch, absoluteFile: destination };
@@ -262,7 +358,7 @@ function materializePatchFiles(profileDirectory, patches) {
262
358
  continue;
263
359
  }
264
360
  // 成员或版本变化必须改变workspace路径,使pnpm重新应用新的组合payload。
265
- const members = group.map(patch => encodeURIComponent(patch.id)).join('+');
361
+ const members = group.map(patch => encodeURIComponent(patch.id + '@' + patch.patchPackage.version)).join('+');
266
362
  const destination = resolve(root, 'combined', encodeURIComponent(key), members + '.patch');
267
363
  mkdirSync(dirname(destination), { recursive: true });
268
364
  const contents = group.map((patch) => {
@@ -307,6 +403,48 @@ function profilePatchConfiguration(profileDirectory, npmPatchFiles) {
307
403
  return { text: String(document), changed };
308
404
  }
309
405
  // 用户显式执行apply后,这里一次性写入profile root依赖与唯一native build许可;冲突直接失败。
406
+ /**
407
+ * Profile-local pnpm hook that keeps the CDN-only xlsx dependency installable.
408
+ *
409
+ * `@deepseek-ai/dsh-client-ui-sidebar-documentpreview` declares `xlsx` as
410
+ * `https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz`. SheetJS stopped publishing to npm
411
+ * after 0.18.5, so no registry mirror carries 0.20.3, and the build network cannot reach that
412
+ * CDN (measured 2026-11-06: every request times out). The image vendors the tarball at
413
+ * /opt/dsh-vendor; this hook rewrites the spec wherever it appears so a cold profile install
414
+ * never requests the CDN.
415
+ *
416
+ * Written into the profile root rather than a template: the Plus bundle has no profile
417
+ * template directory, the profile manifest and pnpm-workspace.yaml are generated here.
418
+ */
419
+ const PROFILE_PNPMFILE = `// Generated by @sparkelf/dsh-plus -- do not edit; rewritten on every apply.
420
+ const VENDORED_XLSX = '/opt/dsh-vendor/xlsx-0.20.3.tgz'
421
+ const CDN_XLSX = /^https?:\\/\\/cdn\\.sheetjs\\.com\\//
422
+ const FIELDS = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']
423
+
424
+ module.exports = {
425
+ hooks: {
426
+ readPackage(pkg) {
427
+ for (const field of FIELDS) {
428
+ const deps = pkg && pkg[field]
429
+ if (deps && typeof deps.xlsx === 'string' && CDN_XLSX.test(deps.xlsx)) {
430
+ deps.xlsx = 'file:' + VENDORED_XLSX
431
+ }
432
+ }
433
+ return pkg
434
+ },
435
+ },
436
+ }
437
+ `;
438
+ /** Write the profile pnpmfile when its content changed; returns whether it wrote. */
439
+ function writeProfilePnpmfile(profileDirectory) {
440
+ const path = resolve(profileDirectory, '.pnpmfile.cjs');
441
+ const current = existsSync(path) ? readFileSync(path, 'utf8') : undefined;
442
+ if (current === PROFILE_PNPMFILE)
443
+ return false;
444
+ writeFileSync(path, PROFILE_PNPMFILE);
445
+ console.log('profile pnpmfile: xlsx -> /opt/dsh-vendor/xlsx-0.20.3.tgz');
446
+ return true;
447
+ }
310
448
  function writeProfileRequirements(profileDirectory, dependencySpecs, allowBuilds) {
311
449
  const manifestPath = resolve(profileDirectory, 'package.json');
312
450
  const manifest = readJsonObject(manifestPath, 'DSH profile manifest');
@@ -374,7 +512,8 @@ function writeProfileRequirements(profileDirectory, dependencySpecs, allowBuilds
374
512
  }
375
513
  if (workspaceChanged)
376
514
  writeFileSync(workspacePath, String(document));
377
- return manifestChanged || workspaceChanged;
515
+ const pnpmfileChanged = writeProfilePnpmfile(profileDirectory);
516
+ return manifestChanged || workspaceChanged || pnpmfileChanged;
378
517
  }
379
518
  function writeProfileComposition(profileDirectory, bundles, runtimePackages) {
380
519
  const path = resolve(profileDirectory, 'package.json');
package/package.json CHANGED
@@ -790,5 +790,5 @@
790
790
  },
791
791
  "type": "module",
792
792
  "types": "lib/types/index.d.ts",
793
- "version": "0.2.9-alpha.53"
793
+ "version": "0.2.9-alpha.55"
794
794
  }