mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 12: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 };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user