mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 05:10:22 +08:00
fix: allowBuilds URL 键截断与污染自愈,健康文件零写入(#180)
- parseAllowBuildLine 未引号键只在冒号+空白处切分,URL 键不再截断; 引号键照旧,行尾无值也能解析 - 含 :// 的键写回加引号,避免 YAML 冒号歧义 - 重写只保留精确等于期望键的 dsh-purge@*,畸形/过期键自动清掉 - 期望键优先读 profile 的 pnpm-lock.yaml 实际 tarball(固定 commit 安装 不再有双 URL 键),退回 package.json,再退裸包名;profileDir 参数可选 - 重写结果先过最小校验(键不重复、round-trip 无损),不过不写盘; 健康文件 next === current 零写入 - 新增 test/profile-insert.test.js(13 条,全量 36 条全过),版本 1.1.82
This commit is contained in:
1 parent
a6cc01c7bd
commit
6345f22cd5
4 files changed
+405
-18
No files matched your search
+152
-17
@@ -105,7 +105,7 @@ export function ensureProfileBundles(profileDir) {
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
}
|
||||
|
||||
function readPurgeTarball() {
|
||||
function readPkgTarball() {
|
||||
try {
|
||||
const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
||||
return String(pkg?.dsh?.install?.tarball || "").trim();
|
||||
@@ -114,10 +114,62 @@ function readPurgeTarball() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 profile 的 pnpm-lock.yaml 读 dsh-purge 实际解析到的 tarball URL(#180)。
|
||||
* 用户用 `github:user/repo#<commit>` 固定 commit 安装时,lock 里记录的是
|
||||
* `.../tar.gz/<commit>`,写死 master URL 会和实际键并存成两个 URL 键。
|
||||
* 简单文本/正则解析即可,不引 yaml 依赖;读不到返回空串。
|
||||
*/
|
||||
function readPurgeTarballFromLock(profileDir) {
|
||||
if (!profileDir) return "";
|
||||
let text = "";
|
||||
try {
|
||||
text = fs.readFileSync(path.join(profileDir, "pnpm-lock.yaml"), "utf8");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
const lines = String(text || "").replace(/\r\n/g, "\n").split("\n");
|
||||
let inImporters = false;
|
||||
for (let i = 0; i < lines.length; i += 1) {
|
||||
const line = lines[i];
|
||||
if (/^\S/.test(line)) {
|
||||
if (inImporters) break; // 已扫过 importers,遇到下一个顶层键就结束
|
||||
inImporters = /^importers[ \t]*:/.test(line);
|
||||
continue;
|
||||
}
|
||||
if (!inImporters) continue;
|
||||
const entry = line.match(/^([ \t]+)dsh-purge[ \t]*:[ \t]*$/);
|
||||
if (!entry) continue;
|
||||
const indent = entry[1].length;
|
||||
// 收集该条目下的 specifier / version 行,直到缩进回到条目本身
|
||||
let specifier = "";
|
||||
let version = "";
|
||||
for (let j = i + 1; j < lines.length; j += 1) {
|
||||
const sub = lines[j];
|
||||
if (sub.trim() && (sub.match(/^[ \t]*/) || [""])[0].length <= indent) break;
|
||||
const sm = sub.match(/^[ \t]+(specifier|version)[ \t]*:[ \t]*(.*)$/);
|
||||
if (!sm) continue;
|
||||
let value = String(sm[2] || "").trim();
|
||||
// 掉引号:pnpm-lock 里带引号与否取决于内容
|
||||
if (/^(["']).*\1[ \t]*$/.test(value)) value = value.slice(1, -1).trim();
|
||||
if (sm[1] === "specifier") specifier = value;
|
||||
else version = value;
|
||||
}
|
||||
// version 是实际解析结果,优先;specifier 只是没有 URL 化版本时兜底
|
||||
for (const candidate of [version, specifier]) {
|
||||
const url = String(candidate || "").match(/https?:\/\/[^\s'"]+/);
|
||||
if (url) return url[0];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/** pnpm 12 会给 tarball 单独写一行 `dsh-purge@https://...: set this to true or false`。只写包名不够。 */
|
||||
export function purgePnpmBuildSpecs() {
|
||||
export function purgePnpmBuildSpecs(profileDir) {
|
||||
const specs = ["dsh-purge"];
|
||||
const tarball = readPurgeTarball();
|
||||
// 期望键和实际安装形态一致:lock 里解析到的 URL > package.json 的 tarball > 只用裸包名
|
||||
const tarball = readPurgeTarballFromLock(profileDir) || readPkgTarball();
|
||||
if (tarball) specs.push(`${PURGE_NAME}@${tarball}`);
|
||||
return specs;
|
||||
}
|
||||
@@ -127,22 +179,90 @@ function isPurgeAllowBuildKey(key) {
|
||||
return name === PURGE_NAME || name.startsWith(`${PURGE_NAME}@`);
|
||||
}
|
||||
|
||||
function parseAllowBuildLine(line) {
|
||||
const m = String(line || "").match(/^[ \t]+(?:"([^"]+)"|'([^']+)'|(\S+?))[ \t]*:[ \t]*(.*?)\s*$/);
|
||||
if (!m) return null;
|
||||
return { key: m[1] || m[2] || m[3], value: String(m[4] || "").trim() };
|
||||
/** 导出仅供单测 round-trip(#180)。 */
|
||||
export function parseAllowBuildLine(line) {
|
||||
const text = String(line || "").replace(/\r$/, "");
|
||||
// 引号包裹:键整段取出,URL 里的冒号不会切碎键
|
||||
const quoted = text.match(/^[ \t]+(?:"([^"]+)"|'([^']+)')[ \t]*:[ \t]*(.*?)\s*$/);
|
||||
if (quoted) return { key: quoted[1] !== undefined ? quoted[1] : quoted[2], value: String(quoted[3] || "").trim() };
|
||||
// 未加引号的键:要求有缩进(顶层键不算),只在「冒号+空白(或行尾)」处切分,且取最后一次出现的位置——
|
||||
// URL 里的 `:` 后面跟的是 `/`,不会断键;`dsh-purge@https: true`(畸形历史键)也能正常切成键+值(#180)
|
||||
if (!/^[ \t]+/.test(text)) return null;
|
||||
let split = -1;
|
||||
for (let i = 0; i < text.length; i += 1) {
|
||||
if (text[i] !== ":") continue;
|
||||
if (i + 1 >= text.length || /[ \t]/.test(text[i + 1])) split = i;
|
||||
}
|
||||
if (split < 0) return null;
|
||||
const key = text.slice(0, split).trim();
|
||||
if (!key) return null;
|
||||
return { key, value: text.slice(split + 1).trim() };
|
||||
}
|
||||
|
||||
function formatAllowBuildLine(key) {
|
||||
return ` ${key}: true`;
|
||||
/** 导出仅供单测 round-trip(#180)。 */
|
||||
export function formatAllowBuildLine(key) {
|
||||
const name = String(key || "");
|
||||
// 含 `://` 的 URL 键加引号,避免 YAML 把 URL 里的冒号当分隔符(#180)
|
||||
if (name.includes("://")) return ` ${JSON.stringify(name)}: true`;
|
||||
return ` ${name}: true`;
|
||||
}
|
||||
|
||||
/** 把 dsh-purge 以及 `dsh-purge@tarball` 都写成 true,避免装别的插件还报 ERR_PNPM_IGNORED_BUILDS(#168)。 */
|
||||
export function rewritePnpmAllowBuilds(raw) {
|
||||
const wanted = purgePnpmBuildSpecs();
|
||||
/**
|
||||
* 重写前做最小校验:顶层键、allowBuilds 段内键都不重复,每个键写回格式都能被
|
||||
* parseAllowBuildLine 无损还原。校验不过就不写盘,宁可不动用户的文件(#180)。
|
||||
* 导出仅供单测(#180)。
|
||||
*/
|
||||
export function validateAllowBuildRewrite(text) {
|
||||
const lines = String(text || "").split("\n");
|
||||
let inAllow = false;
|
||||
let allowIndent = -1;
|
||||
const topKeys = new Set();
|
||||
const allowKeys = new Set();
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.startsWith("#")) continue; // 注释行原样保留,不参与校验
|
||||
if (/^\S/.test(line)) {
|
||||
inAllow = /^allowBuilds[ \t]*:/.test(line);
|
||||
allowIndent = -1;
|
||||
const m = line.match(/^(\S+?)[ \t]*:/);
|
||||
if (!m) return false;
|
||||
if (topKeys.has(m[1])) return false;
|
||||
topKeys.add(m[1]);
|
||||
continue;
|
||||
}
|
||||
if (!inAllow) continue;
|
||||
const indent = (line.match(/^[ \t]*/) || [""])[0].length;
|
||||
if (allowIndent < 0) allowIndent = indent;
|
||||
if (indent < allowIndent) {
|
||||
// 缩进回到 allowBuilds 的子键层级以上,说明段已结束
|
||||
inAllow = false;
|
||||
continue;
|
||||
}
|
||||
const parsed = parseAllowBuildLine(line);
|
||||
if (!parsed || !parsed.key) return false;
|
||||
const name = parsed.key;
|
||||
if (allowKeys.has(name)) return false;
|
||||
// 写回后的行必须能无损解析出同一个键
|
||||
if (parseAllowBuildLine(formatAllowBuildLine(name))?.key !== name) return false;
|
||||
allowKeys.add(name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 dsh-purge 以及 `dsh-purge@tarball` 都写成 true,避免装别的插件还报 ERR_PNPM_IGNORED_BUILDS(#168)。
|
||||
* profileDir 可选:给了就从该 profile 的 pnpm-lock.yaml 读实际 tarball URL(#180)。
|
||||
* 期望键精确命中的才保留;其它一切 `dsh-purge@*` 历史键(畸形 `dsh-purge@https`、过期版本/commit URL)一律丢弃,
|
||||
* 让旧的污染文件在下一次 ensure 时自动清干净。
|
||||
*/
|
||||
export function rewritePnpmAllowBuilds(raw, profileDir) {
|
||||
const wanted = purgePnpmBuildSpecs(profileDir);
|
||||
const wantedSet = new Set(wanted);
|
||||
const text = String(raw || "").replace(/\r\n/g, "\n");
|
||||
const wantedBlock = wanted.map(formatAllowBuildLine);
|
||||
if (!text.trim()) {
|
||||
return `allowBuilds:\n${wanted.map(formatAllowBuildLine).join("\n")}\n`;
|
||||
return `allowBuilds:\n${wantedBlock.join("\n")}\n`;
|
||||
}
|
||||
const lines = text.split(/\r?\n/);
|
||||
let start = -1;
|
||||
@@ -154,7 +274,7 @@ export function rewritePnpmAllowBuilds(raw) {
|
||||
}
|
||||
if (start < 0) {
|
||||
const prefix = text.endsWith("\n") ? "" : "\n";
|
||||
return `${text}${prefix}\nallowBuilds:\n${wanted.map(formatAllowBuildLine).join("\n")}\n`;
|
||||
return `${text}${prefix}\nallowBuilds:\n${wantedBlock.join("\n")}\n`;
|
||||
}
|
||||
let end = lines.length;
|
||||
const kept = [];
|
||||
@@ -169,20 +289,33 @@ export function rewritePnpmAllowBuilds(raw) {
|
||||
end = i;
|
||||
break;
|
||||
}
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.startsWith("#")) {
|
||||
kept.push(line);
|
||||
continue;
|
||||
}
|
||||
const parsed = parseAllowBuildLine(line);
|
||||
if (!parsed) {
|
||||
kept.push(line);
|
||||
continue;
|
||||
}
|
||||
if (isPurgeAllowBuildKey(parsed.key)) {
|
||||
// 只保留精确等于期望键的行,且去重;其余历史 purge 键丢弃(#180 自愈)
|
||||
if (!wantedSet.has(parsed.key) || seen.has(parsed.key)) continue;
|
||||
kept.push(formatAllowBuildLine(parsed.key));
|
||||
seen.add(parsed.key);
|
||||
continue;
|
||||
}
|
||||
// 非 purge 键原样保留;同键重复(会让 pnpm 报 duplicated mapping key)只留第一份
|
||||
if (seen.has(parsed.key)) continue;
|
||||
seen.add(parsed.key);
|
||||
kept.push(line);
|
||||
}
|
||||
for (const key of wanted) {
|
||||
if (!seen.has(key)) kept.push(formatAllowBuildLine(key));
|
||||
for (let i = 0; i < wanted.length; i += 1) {
|
||||
if (!seen.has(wanted[i])) {
|
||||
kept.push(wantedBlock[i]);
|
||||
seen.add(wanted[i]);
|
||||
}
|
||||
}
|
||||
const next = [...lines.slice(0, start + 1), ...kept, ...lines.slice(end)].join("\n");
|
||||
return next.endsWith("\n") ? next : `${next}\n`;
|
||||
@@ -194,8 +327,10 @@ export function ensurePnpmAllowBuild(profileDir) {
|
||||
let text = "";
|
||||
try { text = fs.readFileSync(fp, "utf8"); } catch { text = ""; }
|
||||
const current = String(text || "").replace(/\r\n/g, "\n");
|
||||
const next = rewritePnpmAllowBuilds(current);
|
||||
const next = rewritePnpmAllowBuilds(current, profileDir);
|
||||
if (next === current) return { ok: true, wrote: false, path: fp };
|
||||
// 写前校验不过就不写盘,宁可保持现状(#180)
|
||||
if (!validateAllowBuildRewrite(next)) return { ok: false, wrote: false, path: fp, reason: "allowBuilds-invalid" };
|
||||
fs.writeFileSync(fp, next, "utf8");
|
||||
return { ok: true, wrote: true, path: fp };
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dsh-purge",
|
||||
"version": "1.1.81",
|
||||
"version": "1.1.82",
|
||||
"description": "DeepSeek Harness 破甲:让所有模型都能破甲,不同模型可换不同提示词;默认提示词面向国模「小码酱」。Jailbreak for every model — swap prompts per model. 求 Star 收藏 ⭐ 如要二开,请查看署名要求部分",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
|
||||
@@ -1,3 +1,23 @@
|
||||
# 1.1.82
|
||||
|
||||
## 中文
|
||||
|
||||
- 版本 **1.1.82**。修复 bug,修复稳定性。
|
||||
- **#180**:修复 profile 的 `pnpm-workspace.yaml` `allowBuilds` 被反复写坏(`dsh-purge@https: true` 畸形键 → `ERR_PNPM_INVALID_VERSION_UNION`,重复键 → `duplicated mapping key`)。
|
||||
- URL 键解析不再被冒号截断;写回含 `://` 的键自动加引号。
|
||||
- 重写只保留精确等于期望键的 `dsh-purge@*`,旧污染键(畸形的、过期的版本/commit URL)自动清掉,不用手工修。
|
||||
- 期望键优先从 profile 的 `pnpm-lock.yaml` 读实际解析的 tarball URL(固定 commit 安装也不再有双 URL 键),读不到才退回 `package.json`。
|
||||
- 重写结果先过最小 YAML 校验(键不重复、round-trip 无损),不过就不写盘;健康文件零写入,启动不再反复重写。
|
||||
|
||||
## English
|
||||
|
||||
- **1.1.82**. Bug fixes and stability.
|
||||
- **#180**: stop rewriting the profile `pnpm-workspace.yaml` `allowBuilds` into garbage (`dsh-purge@https: true` malformed key → `ERR_PNPM_INVALID_VERSION_UNION`; duplicated keys → `duplicated mapping key`).
|
||||
- URL keys no longer truncate at the first colon; keys containing `://` are written back quoted.
|
||||
- Rewrites keep only `dsh-purge@*` keys exactly equal to the wanted set; stale polluted keys (malformed, old version/commit URLs) self-heal away on the next ensure.
|
||||
- The wanted key prefers the tarball URL actually resolved in the profile `pnpm-lock.yaml` (fixed-commit installs no longer get a duplicate URL key); falls back to `package.json`.
|
||||
- Rewritten output passes a minimal YAML check first (no duplicate keys, lossless round-trip); healthy files are never written again, so boot no longer rewrites each run.
|
||||
|
||||
# 1.1.81
|
||||
|
||||
## 中文
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
parseAllowBuildLine,
|
||||
formatAllowBuildLine,
|
||||
validateAllowBuildRewrite,
|
||||
purgePnpmBuildSpecs,
|
||||
rewritePnpmAllowBuilds,
|
||||
ensurePnpmAllowBuild,
|
||||
} from "../lib/profile-insert.js";
|
||||
|
||||
const MASTER_URL = "https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master";
|
||||
const COMMIT_URL = "https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/abc1234567890";
|
||||
|
||||
function withTempProfile(lockText) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-test-"));
|
||||
if (lockText !== undefined) {
|
||||
fs.writeFileSync(path.join(dir, "pnpm-lock.yaml"), lockText, "utf8");
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
test("URL 键解析不被冒号截断", () => {
|
||||
const parsed = parseAllowBuildLine(` dsh-purge@${MASTER_URL}: true`);
|
||||
assert.equal(parsed.key, `dsh-purge@${MASTER_URL}`);
|
||||
assert.equal(parsed.value, "true");
|
||||
});
|
||||
|
||||
test("畸形键也解析成整键(自愈时统一丢弃)", () => {
|
||||
assert.deepEqual(parseAllowBuildLine(" dsh-purge@https: true"), {
|
||||
key: "dsh-purge@https",
|
||||
value: "true",
|
||||
});
|
||||
assert.deepEqual(parseAllowBuildLine(" dsh-purge@1.1.81: true"), {
|
||||
key: "dsh-purge@1.1.81",
|
||||
value: "true",
|
||||
});
|
||||
});
|
||||
|
||||
test("行尾没有值的 `key:` 解析为空值", () => {
|
||||
assert.deepEqual(parseAllowBuildLine(" dsh-purge@https:"), {
|
||||
key: "dsh-purge@https",
|
||||
value: "",
|
||||
});
|
||||
assert.deepEqual(parseAllowBuildLine(" dsh-purge:"), {
|
||||
key: "dsh-purge",
|
||||
value: "",
|
||||
});
|
||||
});
|
||||
|
||||
test("引号包裹的键照旧解析", () => {
|
||||
const spec = `dsh-purge@${MASTER_URL}`;
|
||||
for (const line of [` "${spec}": true`, ` '${spec}': true`]) {
|
||||
assert.deepEqual(parseAllowBuildLine(line), { key: spec, value: "true" });
|
||||
}
|
||||
});
|
||||
|
||||
test("URL 键写回带引号且 round-trip 无损", () => {
|
||||
const spec = `dsh-purge@${MASTER_URL}`;
|
||||
const line = formatAllowBuildLine(spec);
|
||||
assert.equal(line, ` "${spec}": true`);
|
||||
assert.deepEqual(parseAllowBuildLine(line), { key: spec, value: "true" });
|
||||
// 不含 URL 的键保持无引号,round-trip 同样无损
|
||||
const plain = formatAllowBuildLine("dsh-purge");
|
||||
assert.equal(plain, " dsh-purge: true");
|
||||
assert.deepEqual(parseAllowBuildLine(plain), { key: "dsh-purge", value: "true" });
|
||||
});
|
||||
|
||||
test("污染的 allowBuilds 重写后只剩期望键", () => {
|
||||
const polluted = [
|
||||
"packages:",
|
||||
" - '.'",
|
||||
"",
|
||||
"allowBuilds:",
|
||||
" cpu-features: true",
|
||||
" ssh2: true",
|
||||
" dsh-purge: true",
|
||||
" dsh-purge@1.1.81: true",
|
||||
" dsh-purge@https: true",
|
||||
" dsh-purge@https: true",
|
||||
` dsh-purge@${MASTER_URL}: true`,
|
||||
"",
|
||||
].join("\n");
|
||||
const next = rewritePnpmAllowBuilds(polluted);
|
||||
const keys = next.split("\n").filter((line) => parseAllowBuildLine(line)).map((line) => parseAllowBuildLine(line).key);
|
||||
// 期望键:裸包名 + package.json 的 tarball(本仓库装的是 master tarball,无 profileDir 时)
|
||||
const wanted = ["cpu-features", "ssh2", ...purgePnpmBuildSpecs()];
|
||||
assert.deepEqual([...new Set(keys)], wanted);
|
||||
// 畸形历史键一个都不留
|
||||
for (const bad of [`dsh-purge@1.1.81`, `dsh-purge@https`]) {
|
||||
assert.equal(keys.includes(bad), false, `should drop ${bad}`);
|
||||
}
|
||||
// 重复出现过的 `dsh-purge@https` 只允许来自 wanted 精确键,且不重复
|
||||
assert.equal(keys.filter((key) => key.startsWith("dsh-purge")).length, purgePnpmBuildSpecs().length);
|
||||
// 保留段的结构仍然合法
|
||||
assert.equal(validateAllowBuildRewrite(next), true);
|
||||
});
|
||||
|
||||
test("非 purge 键原样保留,无 allowBuilds 段时追加", () => {
|
||||
const base = "packages:\n - '.'\n";
|
||||
const next = rewritePnpmAllowBuilds(base);
|
||||
assert.equal(next.startsWith("packages:\n - '.'\n\nallowBuilds:\n"), true);
|
||||
// 追加的是期望键(裸包名 + package.json 的 tarball)
|
||||
for (const spec of purgePnpmBuildSpecs()) {
|
||||
const escaped = formatAllowBuildLine(spec).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
assert.match(next, new RegExp(`^${escaped}$`, "m"));
|
||||
}
|
||||
const withSection = "allowBuilds:\n cpu-features: true\n ssh2: true\n";
|
||||
const next2 = rewritePnpmAllowBuilds(withSection);
|
||||
assert.match(next2, /^ {2}cpu-features: true$/m);
|
||||
assert.match(next2, /^ {2}ssh2: true$/m);
|
||||
});
|
||||
|
||||
test("写前校验:重复顶层键 / 重复 allowBuilds 键 / 解析不了的行判无效", () => {
|
||||
assert.equal(validateAllowBuildRewrite("packages:\n - '.'\n\npackages:\n - \"..\"\n"), false);
|
||||
assert.equal(validateAllowBuildRewrite("allowBuilds:\n cpu-features: true\n cpu-features: true\n"), false);
|
||||
// 没有键值分隔的裸行解析不出来,校验必须挡下
|
||||
assert.equal(validateAllowBuildRewrite("allowBuilds:\n 不是键值对\n"), false);
|
||||
assert.equal(validateAllowBuildRewrite("allowBuilds:\n# 注释行放行\n ssh2: true\n"), true);
|
||||
});
|
||||
|
||||
test("pnpm-lock.yaml 里的 commit URL 优先于 master", () => {
|
||||
const dir = withTempProfile([
|
||||
"lockfileVersion: '9.0'",
|
||||
"",
|
||||
"importers:",
|
||||
"",
|
||||
" .:",
|
||||
" dependencies:",
|
||||
" dsh-purge:",
|
||||
` specifier: ${COMMIT_URL}`,
|
||||
` version: dsh-purge@${COMMIT_URL}`,
|
||||
"",
|
||||
].join("\n"));
|
||||
const specs = purgePnpmBuildSpecs(dir);
|
||||
assert.deepEqual(specs, ["dsh-purge", `dsh-purge@${COMMIT_URL}`]);
|
||||
});
|
||||
|
||||
test("lock 读不到(旧 master 记录 / 无文件)退回 package.json tarball", () => {
|
||||
// 无 pnpm-lock.yaml 的临时目录 → 只用裸包名兜底逻辑应落到 package.json 的 master URL
|
||||
const dir = withTempProfile();
|
||||
assert.deepEqual(purgePnpmBuildSpecs(dir), ["dsh-purge", `dsh-purge@${MASTER_URL}`]);
|
||||
// lock 里 specifier 写死 master,也如实保留
|
||||
const dir2 = withTempProfile([
|
||||
"importers:",
|
||||
"",
|
||||
" .:",
|
||||
" dependencies:",
|
||||
" dsh-purge:",
|
||||
" specifier: https://codeload.github.com/YuJunZhiXue/dsh-purge/tar.gz/refs/heads/master",
|
||||
" version: '1.1.81'",
|
||||
"",
|
||||
].join("\n"));
|
||||
assert.deepEqual(purgePnpmBuildSpecs(dir2), ["dsh-purge", `dsh-purge@${MASTER_URL}`]);
|
||||
});
|
||||
|
||||
test("固定 commit 安装:重写后过期 master 键被换成 commit 键", () => {
|
||||
const dir = withTempProfile([
|
||||
"lockfileVersion: '9.0'",
|
||||
"",
|
||||
"importers:",
|
||||
"",
|
||||
" .:",
|
||||
" dependencies:",
|
||||
" dsh-purge:",
|
||||
` specifier: ${COMMIT_URL}`,
|
||||
` version: dsh-purge@${COMMIT_URL}`,
|
||||
"",
|
||||
].join("\n"));
|
||||
const polluted = [
|
||||
"packages:",
|
||||
" - '.'",
|
||||
"",
|
||||
"allowBuilds:",
|
||||
" ssh2: true",
|
||||
` dsh-purge@${MASTER_URL}: true`,
|
||||
" dsh-purge@https: true",
|
||||
"",
|
||||
].join("\n");
|
||||
const next = rewritePnpmAllowBuilds(polluted, dir);
|
||||
const keys = next.split("\n").filter((line) => parseAllowBuildLine(line)).map((line) => parseAllowBuildLine(line).key);
|
||||
assert.equal(keys.includes(`dsh-purge@${COMMIT_URL}`), true);
|
||||
assert.equal(keys.includes(`dsh-purge@${MASTER_URL}`), false);
|
||||
assert.equal(keys.includes("dsh-purge@https"), false);
|
||||
assert.equal(keys.includes("ssh2"), true);
|
||||
// URL 键写回带引号,可被 yaml 解析方安全读取
|
||||
assert.equal(next.split("\n").some((line) => line === ` "dsh-purge@${COMMIT_URL}": true`), true);
|
||||
});
|
||||
|
||||
test("幂等:健康文件重写输出等于输入,零写入", () => {
|
||||
const dir = withTempProfile();
|
||||
const fp = path.join(dir, "pnpm-workspace.yaml");
|
||||
const healthy = [
|
||||
"packages:",
|
||||
" - '.'",
|
||||
"",
|
||||
"allowBuilds:",
|
||||
" cpu-features: true",
|
||||
" ssh2: true",
|
||||
' dsh-purge: true',
|
||||
` "dsh-purge@${MASTER_URL}": true`,
|
||||
"",
|
||||
].join("\n");
|
||||
fs.writeFileSync(fp, healthy, "utf8");
|
||||
const before = fs.readFileSync(fp, "utf8");
|
||||
assert.equal(rewritePnpmAllowBuilds(before, dir), before);
|
||||
const statBefore = fs.statSync(fp);
|
||||
const result = ensurePnpmAllowBuild(dir);
|
||||
assert.equal(result.wrote, false);
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), before);
|
||||
assert.equal(fs.statSync(fp).mtimeMs, statBefore.mtimeMs);
|
||||
});
|
||||
|
||||
test("幂等:第二次 ensure 不再写盘(自愈后收敛)", () => {
|
||||
const dir = withTempProfile();
|
||||
const fp = path.join(dir, "pnpm-workspace.yaml");
|
||||
const polluted = `allowBuilds:\n ssh2: true\n dsh-purge@https: true\n dsh-purge@1.1.81: true\n`;
|
||||
fs.writeFileSync(fp, polluted, "utf8");
|
||||
const first = ensurePnpmAllowBuild(dir);
|
||||
assert.equal(first.wrote, true);
|
||||
const healed = fs.readFileSync(fp, "utf8");
|
||||
// 畸形历史键被清掉(URL 键带引号重写,不会留下这些裸键)
|
||||
assert.equal(healed.includes("dsh-purge@https: true"), false);
|
||||
assert.equal(healed.includes("dsh-purge@1.1.81"), false);
|
||||
const second = ensurePnpmAllowBuild(dir);
|
||||
assert.equal(second.wrote, false);
|
||||
assert.equal(fs.readFileSync(fp, "utf8"), healed);
|
||||
});
|
||||
Reference in new issue
Block a user