dsh-cookie-cleaner 0.7.1 → 0.7.2
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/CHANGELOG.md +10 -0
- package/LICENSE +21 -21
- package/README.md +432 -432
- package/cordis.patch.yml +5 -5
- package/dsh.plugin.json +1 -1
- package/lib/client.js +213 -1
- package/lib/client.js.map +1 -1
- package/package.json +1 -1
- package/scripts/gen-locales.mjs +82 -82
- package/scripts/hosts.mjs +57 -57
- package/scripts/sync-hosts.mjs +80 -80
- package/src/client/card.tsx +5 -0
- package/src/client/index.ts +129 -129
- package/src/client/star-row-copy.ts +256 -0
- package/src/i18n.js +140 -140
- package/src/jar-live.js +365 -365
- package/src/locales-advise-owner.js +39 -39
- package/src/locales-dict.js +220 -220
- package/src/locales-dict2.js +214 -214
- package/src/locales-patch.js +207 -207
- package/src/patch-model.js +271 -271
- package/src/report.js +243 -243
- package/src/settings-waist.js +37 -37
package/src/patch-model.js
CHANGED
|
@@ -1,271 +1,271 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-cookie-cleaner — patch 结构判定内核(纯函数,零 IO)。
|
|
3
|
-
*
|
|
4
|
-
* 存在的理由:profile 的 `cordis.patch.yml` 是**补丁层**而不是条目列表,它的语义有三个
|
|
5
|
-
* 反直觉的坑(全部有源码依据,见 `.agents/plans/cookie-patch-guard/findings.md`):
|
|
6
|
-
*
|
|
7
|
-
* 1. patch 行按**顶层键浅赋值**(`cordis-plugin-include/src/index.ts:120-123`),所以
|
|
8
|
-
* `config:` 是整块替换而非深合并——漏写的键落回该插件的 **schema 默认**,不是落回
|
|
9
|
-
* bundle 层的值(`webserver.compression` 默认就是 `none`);
|
|
10
|
-
* 2. `name` 与宿主 bundle 行不一致时**整行静默跳过**,只在启动日志留一行 warn;
|
|
11
|
-
* 3. 层顺序是 bundle layers → profile → **home** → `--patch` overlay,home 里同 id 行会把
|
|
12
|
-
* profile 的写盖掉(官方 `configEditor` 写前检查的正是这一条)。
|
|
13
|
-
*
|
|
14
|
-
* 本模块只吃数据、只出结构化判定(code/level/params),**不碰文件系统、不取宿主服务、
|
|
15
|
-
* 不出文案**:文案由 `report.js` 经 i18n 字典渲染,IO 由 `patch-guard.js`(宿主半)与
|
|
16
|
-
* `cli.js`(只读行解析)各自负责。这样宿主半与 CLI 共用一份判定,不可能各说一套。
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/** @typedef {'ok'|'warn'|'fail'|'skipped'} Level */
|
|
20
|
-
|
|
21
|
-
/** 受管行:`writable` = 「修复」按钮可动(**只给 cookie 寿命**);webserver 行只参与只读判定,本插件不写它。 */
|
|
22
|
-
export const MANAGED_ROWS = Object.freeze({
|
|
23
|
-
connection: Object.freeze({
|
|
24
|
-
id: 'connection',
|
|
25
|
-
name: '@deepseek-ai/dsh-client-connection',
|
|
26
|
-
writable: true,
|
|
27
|
-
/** cookie 寿命的唯一杠杆(`connection/src/index.ts:113` 为 `natural().min(1).default(30)`)。 */
|
|
28
|
-
key: 'cookieMaxAgeDays',
|
|
29
|
-
minimum: 1,
|
|
30
|
-
}),
|
|
31
|
-
webserver: Object.freeze({
|
|
32
|
-
id: 'webserver',
|
|
33
|
-
name: '@deepseek-ai/dsh-host-webserver',
|
|
34
|
-
writable: false,
|
|
35
|
-
key: 'port',
|
|
36
|
-
}),
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 本插件**不写端口**(用户裁决 2026-11-05):防 431 真正有效的是 cookie 寿命注入,
|
|
41
|
-
* 端口只是上游给的另一条可选缓解。原先的 `PORT_WRITE` / `isValidPort` /
|
|
42
|
-
* `portExpressionFor` / `nextPortConfig` 已全部删除;`webserver` 行仍参与只读判定(P8)。
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
/** 档位优先级:聚合时取最高;`skipped` 中性,永不冒充异常。 */
|
|
46
|
-
const LEVEL_RANK = Object.freeze({ fail: 3, warn: 2, ok: 1, skipped: 0 })
|
|
47
|
-
|
|
48
|
-
/** `!!js` 表达式在 configEditor 往返里的表示形态(VERIFIED@0.2.1-rc.5)。 */
|
|
49
|
-
export function isExpression(value) {
|
|
50
|
-
return value !== null && typeof value === 'object' && typeof value.__jsExpr === 'string' && Object.keys(value).length === 1
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function nonNegativeInt(value) {
|
|
54
|
-
return typeof value === 'number' && Number.isInteger(value)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** 结构比较(值都是 JSON 安全形态:标量/数组/对象/`{__jsExpr}`)。 */
|
|
58
|
-
function sameValue(a, b) {
|
|
59
|
-
if (a === b) return true
|
|
60
|
-
try { return JSON.stringify(a) === JSON.stringify(b) } catch { return false }
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 值是否“有料”:空对象/空数组/空串/false/0 都视为默认形态(拿不出行为差异)。
|
|
65
|
-
* 真机教训:`webserver.tls` 在 bundle 层与运行态之间只是 `{}` 与缺省形态的区别,
|
|
66
|
-
* 却被告知「有偏差」——两边都空就没什么可报的。
|
|
67
|
-
*/
|
|
68
|
-
function isMaterial(value) {
|
|
69
|
-
if (value === null || value === undefined) return false
|
|
70
|
-
if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string') return value !== false && value !== 0 && value !== ''
|
|
71
|
-
if (Array.isArray(value)) return value.length > 0 && value.some(isMaterial)
|
|
72
|
-
if (typeof value === 'object') return Object.values(value).some(isMaterial)
|
|
73
|
-
return true
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 非表达式键:只有「运行态与 bundle 层不同值、且至少一边真有内容」才算**可证明的丢**。
|
|
78
|
-
* 真机噪声源:`webserver.tls` 这类 schema 默认键——浅赋值下它没写进覆盖行,
|
|
79
|
-
* 但两边都只有默认形态(`{}` vs 缺省),行为上什么都没变,却被报成「有偏差」。
|
|
80
|
-
* 运行态根本没有这个键 → 无从判定,中性,不编造结论。
|
|
81
|
-
*/
|
|
82
|
-
function isProvenLoss(key, inheritedValue, effective) {
|
|
83
|
-
if (!(key in effective)) return false
|
|
84
|
-
if (sameValue(inheritedValue, effective[key])) return false
|
|
85
|
-
return isMaterial(inheritedValue) || isMaterial(effective[key])
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 判定单行。参数全部是**已经读好的数据**:
|
|
90
|
-
* @param {{
|
|
91
|
-
* rowId: 'connection'|'webserver',
|
|
92
|
-
* entryFound: boolean, // 宿主树里是否存在该 id 的可寻址条目
|
|
93
|
-
* overrideRow?: {name?:string, config?:Record<string,unknown>}, // profile 层的那一行
|
|
94
|
-
* inherited?: Record<string, unknown>, // bundle 层配置(可能含 {__jsExpr})
|
|
95
|
-
* effective?: Record<string, unknown>, // 运行时组合后的配置
|
|
96
|
-
* targetDays?: number, // connection 的目标寿命;webserver 忽略
|
|
97
|
-
* shadowedBy?: 'home'|'overlay'|null,
|
|
98
|
-
* fileParse?: {ok: boolean, reason?: string},
|
|
99
|
-
* }} input
|
|
100
|
-
* @returns {{rowId:string, writable:boolean, level:Level, findings:Array<{code:string, level:Level, params:object}>}}
|
|
101
|
-
*/
|
|
102
|
-
export function assessRow(input) {
|
|
103
|
-
const spec = MANAGED_ROWS[input.rowId]
|
|
104
|
-
const findings = []
|
|
105
|
-
const push = (code, level, params = {}) => findings.push({ code, level, params })
|
|
106
|
-
const out = { rowId: input.rowId, writable: spec !== undefined && spec.writable === true, level: 'ok', findings, exprFrozen: [] }
|
|
107
|
-
|
|
108
|
-
if (spec === undefined) {
|
|
109
|
-
push('P00', 'skipped', { rowId: String(input.rowId) })
|
|
110
|
-
return aggregate(out)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// P11:整层 YAML 非法(典型成因是模板根 `[]` 后面又追了分块行)——所有覆盖一起失效
|
|
114
|
-
if (input.fileParse !== undefined && input.fileParse.ok === false) {
|
|
115
|
-
push('P11', 'fail', { reason: input.fileParse.reason ?? 'invalid yaml' })
|
|
116
|
-
return aggregate(out)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const { overrideRow, inherited = {}, effective = {} } = input
|
|
120
|
-
const shadow = input.shadowedBy ?? null
|
|
121
|
-
|
|
122
|
-
// P6:home 层或命令行 overlay 含同 id 行 → profile 的写会被盖掉(configEditor 也会拒写)
|
|
123
|
-
if (shadow !== null) {
|
|
124
|
-
push('P6', 'fail', { shadow, rowId: spec.id })
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// P10:该 id 在当前宿主树里不存在 → 裸覆盖行静默跳过(`patch: entry "x" not found`)
|
|
128
|
-
if (input.entryFound === false) {
|
|
129
|
-
push('P10', 'fail', { rowId: spec.id, name: String(overrideRow?.name ?? spec.name) })
|
|
130
|
-
return aggregate(out)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (overrideRow === undefined) {
|
|
134
|
-
// P1:没有覆盖行,且运行值确实不在目标档
|
|
135
|
-
if (spec.id === 'connection') {
|
|
136
|
-
const live = effective[spec.key]
|
|
137
|
-
if (live !== input.targetDays) push('P1', 'warn', { current: live ?? '默认 30', target: input.targetDays })
|
|
138
|
-
}
|
|
139
|
-
return aggregate(out)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// P2:name 不一致 → 整行被静默跳过,配置实际未生效(最阴的一类:用户以为配上了)
|
|
143
|
-
if (overrideRow.name !== undefined && overrideRow.name !== spec.name) {
|
|
144
|
-
push('P2', 'fail', { got: String(overrideRow.name), expected: spec.name })
|
|
145
|
-
return aggregate(out)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const config = overrideRow.config ?? {}
|
|
149
|
-
|
|
150
|
-
if (spec.id === 'connection') {
|
|
151
|
-
const value = config[spec.key]
|
|
152
|
-
// P4:非法值(<1 / 非整数)。schema 会在落盘前挡住,所以这里是「别去试写」的硬闸
|
|
153
|
-
if (value !== undefined && (!nonNegativeInt(value) || value < spec.minimum)) {
|
|
154
|
-
push('P4', 'fail', { key: spec.key, got: String(value), minimum: spec.minimum })
|
|
155
|
-
}
|
|
156
|
-
// P3:合法但偏档
|
|
157
|
-
else if (typeof value === 'number' && input.targetDays !== undefined && value > input.targetDays) {
|
|
158
|
-
push('P3', 'warn', { key: spec.key, current: value, target: input.targetDays })
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// P5:只报**可证明的丢**(运行态与 bundle 层不同值且至少一边有内容)。键名清单不是证据。
|
|
163
|
-
const dropped = Object.keys(inherited).filter((key) => !(key in config))
|
|
164
|
-
const lostExpr = dropped.filter((key) => isExpression(inherited[key]))
|
|
165
|
-
const provenLost = dropped.filter((key) => !isExpression(inherited[key]) && isProvenLoss(key, inherited[key], effective))
|
|
166
|
-
if (provenLost.length > 0) {
|
|
167
|
-
push('P5', 'warn', { rowId: spec.id, dropped: provenLost })
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// P7:文件里的值与运行时组合值不一致 → 上层覆盖或热应用未跟上
|
|
171
|
-
if (spec.id === 'connection' && config[spec.key] !== undefined && effective[spec.key] !== undefined
|
|
172
|
-
&& config[spec.key] !== effective[spec.key] && shadow === null) {
|
|
173
|
-
push('P7', 'fail', { file: config[spec.key], effective: effective[spec.key] })
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (spec.id === 'webserver') {
|
|
177
|
-
// P8:port 写成字面量会夺走 `--port` 旗标权(cmdline 文档原话「a flag beats the value written beside it」)
|
|
178
|
-
const port = config[spec.key]
|
|
179
|
-
if (port !== undefined && !isExpression(port)) {
|
|
180
|
-
push('P8', 'warn', { port })
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// P9(表达式没保住形态)**不再进体检结论**:我们无权求值那个表达式,拿不出“现状有变化”的证据。
|
|
185
|
-
// 两种形态都在这里:`frozen`(覆盖层抄成了字面量)与 `lostExpr`(根本没写,表达式整块被打掉)。
|
|
186
|
-
// 数据留在 row.exprFrozen,由 plan/粘贴块那里说(那里同时能给回跟随形态的写法)。
|
|
187
|
-
const frozen = Object.keys(inherited).filter((key) => isExpression(inherited[key]) && key in config && !isExpression(config[key]))
|
|
188
|
-
const exprGone = [...frozen, ...lostExpr].sort()
|
|
189
|
-
if (exprGone.length > 0) out.exprFrozen = exprGone
|
|
190
|
-
|
|
191
|
-
return aggregate(out)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/** 聚合档位(fail > warn > ok;全为 skipped 才是 skipped,中性结论不得被当成 ok 或异常)。 */
|
|
195
|
-
export function aggregate(result) {
|
|
196
|
-
const meaningful = result.findings.filter((f) => f.level !== 'skipped')
|
|
197
|
-
if (result.findings.length === 0) result.level = 'ok'
|
|
198
|
-
else if (meaningful.length === 0) result.level = 'skipped'
|
|
199
|
-
else {
|
|
200
|
-
const levels = meaningful.map((f) => f.level)
|
|
201
|
-
result.level = levels.reduce((acc, level) => (LEVEL_RANK[level] > LEVEL_RANK[acc] ? level : acc), levels[0])
|
|
202
|
-
}
|
|
203
|
-
// 可动作 = writable 且有非中性结论,且没有「写了也没用」的硬因:
|
|
204
|
-
// P2 name 不对(要改的是文件里的字)、P4 非法值(schema 会拒)、P6 上层影子(盖不住)、
|
|
205
|
-
// P10 id 不存在(写了也白写)、P11 整层非法(先修 YAML)。
|
|
206
|
-
const blocked = ['P2', 'P4', 'P6', 'P10', 'P11']
|
|
207
|
-
result.blocked = meaningful.some((f) => blocked.includes(f.code))
|
|
208
|
-
result.actionable = result.writable === true
|
|
209
|
-
&& (result.level === 'warn' || result.level === 'fail')
|
|
210
|
-
&& !result.blocked
|
|
211
|
-
return result
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** 整体聚合:任一 fail 即 fail;全 ok 才 ok。 */
|
|
215
|
-
export function aggregateAll(rows) {
|
|
216
|
-
const usable = rows.filter((row) => row.level !== 'skipped')
|
|
217
|
-
if (usable.length === 0) return { level: 'skipped', rows }
|
|
218
|
-
const level = usable.some((row) => row.level === 'fail') ? 'fail' : usable.some((row) => row.level === 'warn') ? 'warn' : 'ok'
|
|
219
|
-
return { level, rows }
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* 产出写意图。**关键性质(VERIFIED@0.2.1-rc.5)**:`current` 来自运行时组合配置,
|
|
224
|
-
* 其中表达式键已是 `{__jsExpr}` 形态,所以展开即保留——用户改过的 port、既有的表达式
|
|
225
|
-
* 都不会被刷掉;我们只动要求的那一键。
|
|
226
|
-
* @param {{current:Record<string,unknown>, targetDays?:number, guardMode?:'literal'|'expression', inherited?:Record<string,unknown>}} input
|
|
227
|
-
* @returns {{next:Record<string,unknown>, changed:string[]}}
|
|
228
|
-
*/
|
|
229
|
-
export function nextConfigFor(input) {
|
|
230
|
-
const current = input.current ?? {}
|
|
231
|
-
const next = { ...current }
|
|
232
|
-
const changed = []
|
|
233
|
-
|
|
234
|
-
if (input.targetDays !== undefined && next.cookieMaxAgeDays !== input.targetDays) {
|
|
235
|
-
next.cookieMaxAgeDays = input.targetDays
|
|
236
|
-
changed.push('cookieMaxAgeDays')
|
|
237
|
-
}
|
|
238
|
-
// 表达式档:把 bundle 层本来就是表达式的键还原成 `{__jsExpr}`(表达式串取自 inherited,绝不硬编码)
|
|
239
|
-
if (input.guardMode === 'expression' && input.inherited !== undefined) {
|
|
240
|
-
for (const [key, value] of Object.entries(input.inherited)) {
|
|
241
|
-
if (isExpression(value) && !isExpression(next[key])) {
|
|
242
|
-
next[key] = value
|
|
243
|
-
changed.push(key)
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
return { next, changed }
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* 生成可直接粘贴的 patch 块(用于只读面:webserver 行、以及 CLI 无法写时的处方)。
|
|
252
|
-
* 输出块序列形态,并提示 flow `[]` 根的坑(P11)。
|
|
253
|
-
* @param {{rowId:'connection'|'webserver', inherited:Record<string,unknown>, current?:Record<string,unknown>, targetDays?:number, guardMode?:'literal'|'expression'}} input
|
|
254
|
-
*/
|
|
255
|
-
export function renderPlanBlock(input) {
|
|
256
|
-
const spec = MANAGED_ROWS[input.rowId]
|
|
257
|
-
const source = { ...(input.inherited ?? {}), ...(input.current ?? {}) }
|
|
258
|
-
// 目标值在合成时就写进表里,避开「生成后再正则回改」这条脆路
|
|
259
|
-
if (spec.id === 'connection' && input.targetDays !== undefined) source[spec.key] = input.targetDays
|
|
260
|
-
const lines = [`- id: ${spec.id}`, ` name: '${spec.name}'`, ' config:']
|
|
261
|
-
for (const [key, value] of Object.entries(source)) {
|
|
262
|
-
if (isExpression(value)) {
|
|
263
|
-
lines.push(` ${key}: !!js ${value.__jsExpr}`)
|
|
264
|
-
}
|
|
265
|
-
else if (value === undefined) continue
|
|
266
|
-
else if (Array.isArray(value)) lines.push(` ${key}: ${value.length === 0 ? '[]' : JSON.stringify(value)}`)
|
|
267
|
-
else if (typeof value === 'string') lines.push(` ${key}: '${value}'`)
|
|
268
|
-
else lines.push(` ${key}: ${value}`)
|
|
269
|
-
}
|
|
270
|
-
return lines.join('\n')
|
|
271
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-cookie-cleaner — patch 结构判定内核(纯函数,零 IO)。
|
|
3
|
+
*
|
|
4
|
+
* 存在的理由:profile 的 `cordis.patch.yml` 是**补丁层**而不是条目列表,它的语义有三个
|
|
5
|
+
* 反直觉的坑(全部有源码依据,见 `.agents/plans/cookie-patch-guard/findings.md`):
|
|
6
|
+
*
|
|
7
|
+
* 1. patch 行按**顶层键浅赋值**(`cordis-plugin-include/src/index.ts:120-123`),所以
|
|
8
|
+
* `config:` 是整块替换而非深合并——漏写的键落回该插件的 **schema 默认**,不是落回
|
|
9
|
+
* bundle 层的值(`webserver.compression` 默认就是 `none`);
|
|
10
|
+
* 2. `name` 与宿主 bundle 行不一致时**整行静默跳过**,只在启动日志留一行 warn;
|
|
11
|
+
* 3. 层顺序是 bundle layers → profile → **home** → `--patch` overlay,home 里同 id 行会把
|
|
12
|
+
* profile 的写盖掉(官方 `configEditor` 写前检查的正是这一条)。
|
|
13
|
+
*
|
|
14
|
+
* 本模块只吃数据、只出结构化判定(code/level/params),**不碰文件系统、不取宿主服务、
|
|
15
|
+
* 不出文案**:文案由 `report.js` 经 i18n 字典渲染,IO 由 `patch-guard.js`(宿主半)与
|
|
16
|
+
* `cli.js`(只读行解析)各自负责。这样宿主半与 CLI 共用一份判定,不可能各说一套。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** @typedef {'ok'|'warn'|'fail'|'skipped'} Level */
|
|
20
|
+
|
|
21
|
+
/** 受管行:`writable` = 「修复」按钮可动(**只给 cookie 寿命**);webserver 行只参与只读判定,本插件不写它。 */
|
|
22
|
+
export const MANAGED_ROWS = Object.freeze({
|
|
23
|
+
connection: Object.freeze({
|
|
24
|
+
id: 'connection',
|
|
25
|
+
name: '@deepseek-ai/dsh-client-connection',
|
|
26
|
+
writable: true,
|
|
27
|
+
/** cookie 寿命的唯一杠杆(`connection/src/index.ts:113` 为 `natural().min(1).default(30)`)。 */
|
|
28
|
+
key: 'cookieMaxAgeDays',
|
|
29
|
+
minimum: 1,
|
|
30
|
+
}),
|
|
31
|
+
webserver: Object.freeze({
|
|
32
|
+
id: 'webserver',
|
|
33
|
+
name: '@deepseek-ai/dsh-host-webserver',
|
|
34
|
+
writable: false,
|
|
35
|
+
key: 'port',
|
|
36
|
+
}),
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 本插件**不写端口**(用户裁决 2026-11-05):防 431 真正有效的是 cookie 寿命注入,
|
|
41
|
+
* 端口只是上游给的另一条可选缓解。原先的 `PORT_WRITE` / `isValidPort` /
|
|
42
|
+
* `portExpressionFor` / `nextPortConfig` 已全部删除;`webserver` 行仍参与只读判定(P8)。
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** 档位优先级:聚合时取最高;`skipped` 中性,永不冒充异常。 */
|
|
46
|
+
const LEVEL_RANK = Object.freeze({ fail: 3, warn: 2, ok: 1, skipped: 0 })
|
|
47
|
+
|
|
48
|
+
/** `!!js` 表达式在 configEditor 往返里的表示形态(VERIFIED@0.2.1-rc.5)。 */
|
|
49
|
+
export function isExpression(value) {
|
|
50
|
+
return value !== null && typeof value === 'object' && typeof value.__jsExpr === 'string' && Object.keys(value).length === 1
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function nonNegativeInt(value) {
|
|
54
|
+
return typeof value === 'number' && Number.isInteger(value)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 结构比较(值都是 JSON 安全形态:标量/数组/对象/`{__jsExpr}`)。 */
|
|
58
|
+
function sameValue(a, b) {
|
|
59
|
+
if (a === b) return true
|
|
60
|
+
try { return JSON.stringify(a) === JSON.stringify(b) } catch { return false }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 值是否“有料”:空对象/空数组/空串/false/0 都视为默认形态(拿不出行为差异)。
|
|
65
|
+
* 真机教训:`webserver.tls` 在 bundle 层与运行态之间只是 `{}` 与缺省形态的区别,
|
|
66
|
+
* 却被告知「有偏差」——两边都空就没什么可报的。
|
|
67
|
+
*/
|
|
68
|
+
function isMaterial(value) {
|
|
69
|
+
if (value === null || value === undefined) return false
|
|
70
|
+
if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string') return value !== false && value !== 0 && value !== ''
|
|
71
|
+
if (Array.isArray(value)) return value.length > 0 && value.some(isMaterial)
|
|
72
|
+
if (typeof value === 'object') return Object.values(value).some(isMaterial)
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 非表达式键:只有「运行态与 bundle 层不同值、且至少一边真有内容」才算**可证明的丢**。
|
|
78
|
+
* 真机噪声源:`webserver.tls` 这类 schema 默认键——浅赋值下它没写进覆盖行,
|
|
79
|
+
* 但两边都只有默认形态(`{}` vs 缺省),行为上什么都没变,却被报成「有偏差」。
|
|
80
|
+
* 运行态根本没有这个键 → 无从判定,中性,不编造结论。
|
|
81
|
+
*/
|
|
82
|
+
function isProvenLoss(key, inheritedValue, effective) {
|
|
83
|
+
if (!(key in effective)) return false
|
|
84
|
+
if (sameValue(inheritedValue, effective[key])) return false
|
|
85
|
+
return isMaterial(inheritedValue) || isMaterial(effective[key])
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 判定单行。参数全部是**已经读好的数据**:
|
|
90
|
+
* @param {{
|
|
91
|
+
* rowId: 'connection'|'webserver',
|
|
92
|
+
* entryFound: boolean, // 宿主树里是否存在该 id 的可寻址条目
|
|
93
|
+
* overrideRow?: {name?:string, config?:Record<string,unknown>}, // profile 层的那一行
|
|
94
|
+
* inherited?: Record<string, unknown>, // bundle 层配置(可能含 {__jsExpr})
|
|
95
|
+
* effective?: Record<string, unknown>, // 运行时组合后的配置
|
|
96
|
+
* targetDays?: number, // connection 的目标寿命;webserver 忽略
|
|
97
|
+
* shadowedBy?: 'home'|'overlay'|null,
|
|
98
|
+
* fileParse?: {ok: boolean, reason?: string},
|
|
99
|
+
* }} input
|
|
100
|
+
* @returns {{rowId:string, writable:boolean, level:Level, findings:Array<{code:string, level:Level, params:object}>}}
|
|
101
|
+
*/
|
|
102
|
+
export function assessRow(input) {
|
|
103
|
+
const spec = MANAGED_ROWS[input.rowId]
|
|
104
|
+
const findings = []
|
|
105
|
+
const push = (code, level, params = {}) => findings.push({ code, level, params })
|
|
106
|
+
const out = { rowId: input.rowId, writable: spec !== undefined && spec.writable === true, level: 'ok', findings, exprFrozen: [] }
|
|
107
|
+
|
|
108
|
+
if (spec === undefined) {
|
|
109
|
+
push('P00', 'skipped', { rowId: String(input.rowId) })
|
|
110
|
+
return aggregate(out)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// P11:整层 YAML 非法(典型成因是模板根 `[]` 后面又追了分块行)——所有覆盖一起失效
|
|
114
|
+
if (input.fileParse !== undefined && input.fileParse.ok === false) {
|
|
115
|
+
push('P11', 'fail', { reason: input.fileParse.reason ?? 'invalid yaml' })
|
|
116
|
+
return aggregate(out)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const { overrideRow, inherited = {}, effective = {} } = input
|
|
120
|
+
const shadow = input.shadowedBy ?? null
|
|
121
|
+
|
|
122
|
+
// P6:home 层或命令行 overlay 含同 id 行 → profile 的写会被盖掉(configEditor 也会拒写)
|
|
123
|
+
if (shadow !== null) {
|
|
124
|
+
push('P6', 'fail', { shadow, rowId: spec.id })
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// P10:该 id 在当前宿主树里不存在 → 裸覆盖行静默跳过(`patch: entry "x" not found`)
|
|
128
|
+
if (input.entryFound === false) {
|
|
129
|
+
push('P10', 'fail', { rowId: spec.id, name: String(overrideRow?.name ?? spec.name) })
|
|
130
|
+
return aggregate(out)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (overrideRow === undefined) {
|
|
134
|
+
// P1:没有覆盖行,且运行值确实不在目标档
|
|
135
|
+
if (spec.id === 'connection') {
|
|
136
|
+
const live = effective[spec.key]
|
|
137
|
+
if (live !== input.targetDays) push('P1', 'warn', { current: live ?? '默认 30', target: input.targetDays })
|
|
138
|
+
}
|
|
139
|
+
return aggregate(out)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// P2:name 不一致 → 整行被静默跳过,配置实际未生效(最阴的一类:用户以为配上了)
|
|
143
|
+
if (overrideRow.name !== undefined && overrideRow.name !== spec.name) {
|
|
144
|
+
push('P2', 'fail', { got: String(overrideRow.name), expected: spec.name })
|
|
145
|
+
return aggregate(out)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const config = overrideRow.config ?? {}
|
|
149
|
+
|
|
150
|
+
if (spec.id === 'connection') {
|
|
151
|
+
const value = config[spec.key]
|
|
152
|
+
// P4:非法值(<1 / 非整数)。schema 会在落盘前挡住,所以这里是「别去试写」的硬闸
|
|
153
|
+
if (value !== undefined && (!nonNegativeInt(value) || value < spec.minimum)) {
|
|
154
|
+
push('P4', 'fail', { key: spec.key, got: String(value), minimum: spec.minimum })
|
|
155
|
+
}
|
|
156
|
+
// P3:合法但偏档
|
|
157
|
+
else if (typeof value === 'number' && input.targetDays !== undefined && value > input.targetDays) {
|
|
158
|
+
push('P3', 'warn', { key: spec.key, current: value, target: input.targetDays })
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// P5:只报**可证明的丢**(运行态与 bundle 层不同值且至少一边有内容)。键名清单不是证据。
|
|
163
|
+
const dropped = Object.keys(inherited).filter((key) => !(key in config))
|
|
164
|
+
const lostExpr = dropped.filter((key) => isExpression(inherited[key]))
|
|
165
|
+
const provenLost = dropped.filter((key) => !isExpression(inherited[key]) && isProvenLoss(key, inherited[key], effective))
|
|
166
|
+
if (provenLost.length > 0) {
|
|
167
|
+
push('P5', 'warn', { rowId: spec.id, dropped: provenLost })
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// P7:文件里的值与运行时组合值不一致 → 上层覆盖或热应用未跟上
|
|
171
|
+
if (spec.id === 'connection' && config[spec.key] !== undefined && effective[spec.key] !== undefined
|
|
172
|
+
&& config[spec.key] !== effective[spec.key] && shadow === null) {
|
|
173
|
+
push('P7', 'fail', { file: config[spec.key], effective: effective[spec.key] })
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (spec.id === 'webserver') {
|
|
177
|
+
// P8:port 写成字面量会夺走 `--port` 旗标权(cmdline 文档原话「a flag beats the value written beside it」)
|
|
178
|
+
const port = config[spec.key]
|
|
179
|
+
if (port !== undefined && !isExpression(port)) {
|
|
180
|
+
push('P8', 'warn', { port })
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// P9(表达式没保住形态)**不再进体检结论**:我们无权求值那个表达式,拿不出“现状有变化”的证据。
|
|
185
|
+
// 两种形态都在这里:`frozen`(覆盖层抄成了字面量)与 `lostExpr`(根本没写,表达式整块被打掉)。
|
|
186
|
+
// 数据留在 row.exprFrozen,由 plan/粘贴块那里说(那里同时能给回跟随形态的写法)。
|
|
187
|
+
const frozen = Object.keys(inherited).filter((key) => isExpression(inherited[key]) && key in config && !isExpression(config[key]))
|
|
188
|
+
const exprGone = [...frozen, ...lostExpr].sort()
|
|
189
|
+
if (exprGone.length > 0) out.exprFrozen = exprGone
|
|
190
|
+
|
|
191
|
+
return aggregate(out)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** 聚合档位(fail > warn > ok;全为 skipped 才是 skipped,中性结论不得被当成 ok 或异常)。 */
|
|
195
|
+
export function aggregate(result) {
|
|
196
|
+
const meaningful = result.findings.filter((f) => f.level !== 'skipped')
|
|
197
|
+
if (result.findings.length === 0) result.level = 'ok'
|
|
198
|
+
else if (meaningful.length === 0) result.level = 'skipped'
|
|
199
|
+
else {
|
|
200
|
+
const levels = meaningful.map((f) => f.level)
|
|
201
|
+
result.level = levels.reduce((acc, level) => (LEVEL_RANK[level] > LEVEL_RANK[acc] ? level : acc), levels[0])
|
|
202
|
+
}
|
|
203
|
+
// 可动作 = writable 且有非中性结论,且没有「写了也没用」的硬因:
|
|
204
|
+
// P2 name 不对(要改的是文件里的字)、P4 非法值(schema 会拒)、P6 上层影子(盖不住)、
|
|
205
|
+
// P10 id 不存在(写了也白写)、P11 整层非法(先修 YAML)。
|
|
206
|
+
const blocked = ['P2', 'P4', 'P6', 'P10', 'P11']
|
|
207
|
+
result.blocked = meaningful.some((f) => blocked.includes(f.code))
|
|
208
|
+
result.actionable = result.writable === true
|
|
209
|
+
&& (result.level === 'warn' || result.level === 'fail')
|
|
210
|
+
&& !result.blocked
|
|
211
|
+
return result
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 整体聚合:任一 fail 即 fail;全 ok 才 ok。 */
|
|
215
|
+
export function aggregateAll(rows) {
|
|
216
|
+
const usable = rows.filter((row) => row.level !== 'skipped')
|
|
217
|
+
if (usable.length === 0) return { level: 'skipped', rows }
|
|
218
|
+
const level = usable.some((row) => row.level === 'fail') ? 'fail' : usable.some((row) => row.level === 'warn') ? 'warn' : 'ok'
|
|
219
|
+
return { level, rows }
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 产出写意图。**关键性质(VERIFIED@0.2.1-rc.5)**:`current` 来自运行时组合配置,
|
|
224
|
+
* 其中表达式键已是 `{__jsExpr}` 形态,所以展开即保留——用户改过的 port、既有的表达式
|
|
225
|
+
* 都不会被刷掉;我们只动要求的那一键。
|
|
226
|
+
* @param {{current:Record<string,unknown>, targetDays?:number, guardMode?:'literal'|'expression', inherited?:Record<string,unknown>}} input
|
|
227
|
+
* @returns {{next:Record<string,unknown>, changed:string[]}}
|
|
228
|
+
*/
|
|
229
|
+
export function nextConfigFor(input) {
|
|
230
|
+
const current = input.current ?? {}
|
|
231
|
+
const next = { ...current }
|
|
232
|
+
const changed = []
|
|
233
|
+
|
|
234
|
+
if (input.targetDays !== undefined && next.cookieMaxAgeDays !== input.targetDays) {
|
|
235
|
+
next.cookieMaxAgeDays = input.targetDays
|
|
236
|
+
changed.push('cookieMaxAgeDays')
|
|
237
|
+
}
|
|
238
|
+
// 表达式档:把 bundle 层本来就是表达式的键还原成 `{__jsExpr}`(表达式串取自 inherited,绝不硬编码)
|
|
239
|
+
if (input.guardMode === 'expression' && input.inherited !== undefined) {
|
|
240
|
+
for (const [key, value] of Object.entries(input.inherited)) {
|
|
241
|
+
if (isExpression(value) && !isExpression(next[key])) {
|
|
242
|
+
next[key] = value
|
|
243
|
+
changed.push(key)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return { next, changed }
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 生成可直接粘贴的 patch 块(用于只读面:webserver 行、以及 CLI 无法写时的处方)。
|
|
252
|
+
* 输出块序列形态,并提示 flow `[]` 根的坑(P11)。
|
|
253
|
+
* @param {{rowId:'connection'|'webserver', inherited:Record<string,unknown>, current?:Record<string,unknown>, targetDays?:number, guardMode?:'literal'|'expression'}} input
|
|
254
|
+
*/
|
|
255
|
+
export function renderPlanBlock(input) {
|
|
256
|
+
const spec = MANAGED_ROWS[input.rowId]
|
|
257
|
+
const source = { ...(input.inherited ?? {}), ...(input.current ?? {}) }
|
|
258
|
+
// 目标值在合成时就写进表里,避开「生成后再正则回改」这条脆路
|
|
259
|
+
if (spec.id === 'connection' && input.targetDays !== undefined) source[spec.key] = input.targetDays
|
|
260
|
+
const lines = [`- id: ${spec.id}`, ` name: '${spec.name}'`, ' config:']
|
|
261
|
+
for (const [key, value] of Object.entries(source)) {
|
|
262
|
+
if (isExpression(value)) {
|
|
263
|
+
lines.push(` ${key}: !!js ${value.__jsExpr}`)
|
|
264
|
+
}
|
|
265
|
+
else if (value === undefined) continue
|
|
266
|
+
else if (Array.isArray(value)) lines.push(` ${key}: ${value.length === 0 ? '[]' : JSON.stringify(value)}`)
|
|
267
|
+
else if (typeof value === 'string') lines.push(` ${key}: '${value}'`)
|
|
268
|
+
else lines.push(` ${key}: ${value}`)
|
|
269
|
+
}
|
|
270
|
+
return lines.join('\n')
|
|
271
|
+
}
|