dsh-plugin-snapshot 0.3.7 → 0.3.9
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/lib/allow-builds.js +163 -0
- package/lib/auto-update.js +410 -146
- package/lib/doctor.js +98 -4
- package/lib/dsh-version.js +127 -0
- package/lib/exec.js +39 -0
- package/lib/health-gate.js +192 -0
- package/lib/index.js +49 -7
- package/lib/policy.js +245 -0
- package/lib/routes.js +25 -4
- package/lib/updates.js +97 -19
- package/package.json +2 -2
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 构建授权表:pnpm 10+ 默认不执行依赖的生命周期脚本(install / postinstall / prepare),
|
|
3
|
+
* 必须由 pnpm-workspace.yaml 的 allowBuilds(或 onlyBuiltDependencies)显式放行。
|
|
4
|
+
*
|
|
5
|
+
* 为什么这个文件决定升级成功率:
|
|
6
|
+
* `pnpm add --ignore-scripts` 的命令行开关优先级高于授权表。
|
|
7
|
+
* 旧实现对每一次安装都无差别加 --ignore-scripts,等于把用户已经授权好的
|
|
8
|
+
* cpu-features / ssh2 / dsh-purge 等包的构建全部作废 ——
|
|
9
|
+
* 版本号装上了,postinstall 没跑,插件要到下次加载时才炸。
|
|
10
|
+
*
|
|
11
|
+
* 这里只「读」授权表,不写:改用户的构建授权属于 DSH 官方管理器的职责,
|
|
12
|
+
* 一个快照工具不该替用户做安全决策。
|
|
13
|
+
*/
|
|
14
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
|
|
17
|
+
/** 去掉 YAML 标量两端的引号 */
|
|
18
|
+
function unquote(s) {
|
|
19
|
+
const t = String(s || '').trim();
|
|
20
|
+
if (t.length >= 2) {
|
|
21
|
+
const a = t[0], b = t[t.length - 1];
|
|
22
|
+
if ((a === '"' && b === '"') || (a === "'" && b === "'")) return t.slice(1, -1);
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 解析 allowBuilds 段:
|
|
29
|
+
* allowBuilds:
|
|
30
|
+
* cpu-features: true
|
|
31
|
+
* "dsh-boot-animation@file:../../local-pkgs/x.tgz": true
|
|
32
|
+
* node-pty: false
|
|
33
|
+
* @returns {{ allowed: Set<string>, denied: Set<string> }}
|
|
34
|
+
*/
|
|
35
|
+
export function parseAllowBuilds(text) {
|
|
36
|
+
const allowed = new Set();
|
|
37
|
+
const denied = new Set();
|
|
38
|
+
const lines = String(text || '').split(/\r?\n/);
|
|
39
|
+
let inBlock = false;
|
|
40
|
+
let baseIndent = -1;
|
|
41
|
+
for (const raw of lines) {
|
|
42
|
+
if (!raw.trim() || /^\s*#/.test(raw)) continue;
|
|
43
|
+
const indent = raw.length - raw.replace(/^[ \t]+/, '').length;
|
|
44
|
+
if (!inBlock) {
|
|
45
|
+
if (/^allowBuilds:\s*$/.test(raw)) { inBlock = true; baseIndent = -1; }
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
/* 出现同级或更外层的键 = 本段结束 */
|
|
49
|
+
if (indent === 0) { inBlock = false; continue; }
|
|
50
|
+
if (baseIndent < 0) baseIndent = indent;
|
|
51
|
+
if (indent < baseIndent) { inBlock = false; continue; }
|
|
52
|
+
/* key 可能自带 ':'(file:/github: 形式),只从末尾的 : true / : false 切 */
|
|
53
|
+
const m = /^[ \t]+(.+?):[ \t]*(true|false)[ \t]*$/.exec(raw);
|
|
54
|
+
if (!m) continue;
|
|
55
|
+
const key = unquote(m[1]);
|
|
56
|
+
if (!key) continue;
|
|
57
|
+
if (m[2] === 'true') allowed.add(key); else denied.add(key);
|
|
58
|
+
}
|
|
59
|
+
return { allowed, denied };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 解析列表形式的白名单键(pnpm 原生写法) */
|
|
63
|
+
export function parseBuiltList(text, key) {
|
|
64
|
+
const out = new Set();
|
|
65
|
+
const lines = String(text || '').split(/\r?\n/);
|
|
66
|
+
let inBlock = false;
|
|
67
|
+
for (const raw of lines) {
|
|
68
|
+
if (!raw.trim() || /^\s*#/.test(raw)) continue;
|
|
69
|
+
const indent = raw.length - raw.replace(/^[ \t]+/, '').length;
|
|
70
|
+
if (!inBlock) {
|
|
71
|
+
if (new RegExp('^' + key + ':\\s*$').test(raw)) inBlock = true;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (indent === 0) { inBlock = false; continue; }
|
|
75
|
+
const m = /^[ \t]+-[ \t]*(.+)$/.exec(raw);
|
|
76
|
+
if (!m) continue;
|
|
77
|
+
const v = unquote(m[1]);
|
|
78
|
+
if (v) out.add(v);
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 名字是否命中授权条目;返回命中的那条原文,未命中返回 null */
|
|
84
|
+
export function matchEntry(keys, name, version) {
|
|
85
|
+
const n = String(name || '');
|
|
86
|
+
const v = String(version || '').replace(/^[\^~>=<\s]+/, '');
|
|
87
|
+
for (const k of keys) {
|
|
88
|
+
if (k === n) return k;
|
|
89
|
+
if (k.startsWith(n + '@')) {
|
|
90
|
+
const tail = k.slice(n.length + 1);
|
|
91
|
+
if (v && tail === v) return k;
|
|
92
|
+
/* file: / github: / link: / workspace: 这类声明没有可比的 semver,按包名放行 */
|
|
93
|
+
if (!/^\d/.test(tail)) return k;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 读出这个 profile 的构建授权表。
|
|
101
|
+
* 三个来源合并(任一为 true 即放行):
|
|
102
|
+
* 1) pnpm-workspace.yaml 的 allowBuilds
|
|
103
|
+
* 2) pnpm-workspace.yaml 的 onlyBuiltDependencies(pnpm 原生)
|
|
104
|
+
* 3) package.json 的 pnpm.onlyBuiltDependencies
|
|
105
|
+
* denied 优先于 allowed。
|
|
106
|
+
*/
|
|
107
|
+
export function readAllowBuilds(profileDir) {
|
|
108
|
+
const allowed = new Set();
|
|
109
|
+
const denied = new Set();
|
|
110
|
+
const sources = [];
|
|
111
|
+
|
|
112
|
+
const wsPath = join(profileDir, 'pnpm-workspace.yaml');
|
|
113
|
+
if (existsSync(wsPath)) {
|
|
114
|
+
try {
|
|
115
|
+
const text = readFileSync(wsPath, 'utf8');
|
|
116
|
+
const a = parseAllowBuilds(text);
|
|
117
|
+
for (const k of a.allowed) allowed.add(k);
|
|
118
|
+
for (const k of a.denied) denied.add(k);
|
|
119
|
+
for (const k of parseBuiltList(text, 'onlyBuiltDependencies')) allowed.add(k);
|
|
120
|
+
if (a.allowed.size || a.denied.size) sources.push('pnpm-workspace.yaml');
|
|
121
|
+
} catch (e) { /* 读不到就当作没有授权,行为回落到 pnpm 默认 */ }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const pkgPath = join(profileDir, 'package.json');
|
|
125
|
+
if (existsSync(pkgPath)) {
|
|
126
|
+
try {
|
|
127
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
128
|
+
const pnpmCfg = (pkg && pkg.pnpm) || {};
|
|
129
|
+
const list = Array.isArray(pnpmCfg.onlyBuiltDependencies) ? pnpmCfg.onlyBuiltDependencies : [];
|
|
130
|
+
const never = Array.isArray(pnpmCfg.neverBuiltDependencies) ? pnpmCfg.neverBuiltDependencies : [];
|
|
131
|
+
for (const k of list) allowed.add(String(k));
|
|
132
|
+
for (const k of never) denied.add(String(k));
|
|
133
|
+
if (list.length || never.length) sources.push('package.json');
|
|
134
|
+
} catch (e) { /* package.json 坏掉不阻断安装 */ }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return { allowed, denied, sources };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 这个包能不能执行生命周期脚本。
|
|
142
|
+
* @returns {{ ok: boolean, reason: string, entry?: string }}
|
|
143
|
+
*/
|
|
144
|
+
export function scriptsAllowed(table, name, version) {
|
|
145
|
+
const t = table || { allowed: new Set(), denied: new Set(), sources: [] };
|
|
146
|
+
const blocked = matchEntry(t.denied, name, version);
|
|
147
|
+
if (blocked) return { ok: false, reason: '显式禁止构建(neverBuiltDependencies)', entry: blocked };
|
|
148
|
+
const hit = matchEntry(t.allowed, name, version);
|
|
149
|
+
if (hit) return { ok: true, reason: '授权表已放行', entry: hit };
|
|
150
|
+
return { ok: false, reason: '授权表未列该包,按 pnpm 默认不构建' };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 构造一次 pnpm 安装的参数。
|
|
155
|
+
* 只有「明确被授权」的包才不加 --ignore-scripts;其余保持跳过,
|
|
156
|
+
* 并在返回值里带上 scriptsSkipped,让「装上了但没构建」成为可见状态。
|
|
157
|
+
*/
|
|
158
|
+
export function installArgs(baseArgs, table, name, version) {
|
|
159
|
+
const v = scriptsAllowed(table, name, version);
|
|
160
|
+
const args = baseArgs.slice();
|
|
161
|
+
if (!v.ok && args.indexOf('--ignore-scripts') < 0) args.push('--ignore-scripts');
|
|
162
|
+
return { args, scriptsOk: v.ok, scriptsReason: v.reason, scriptsEntry: v.entry || '' };
|
|
163
|
+
}
|