dsh-cookie-cleaner 0.2.0 → 0.2.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.
@@ -18,7 +18,7 @@
18
18
 
19
19
  /** @typedef {'ok'|'warn'|'fail'|'skipped'} Level */
20
20
 
21
- /** 受管行:`writable` = 「修复」按钮可动(只给 cookie 寿命);端口走另一个显式动作(PORT_WRITE)。 */
21
+ /** 受管行:`writable` = 「修复」按钮可动(**只给 cookie 寿命**);webserver 行只参与只读判定,本插件不写它。 */
22
22
  export const MANAGED_ROWS = Object.freeze({
23
23
  connection: Object.freeze({
24
24
  id: 'connection',
@@ -37,20 +37,10 @@ export const MANAGED_ROWS = Object.freeze({
37
37
  })
38
38
 
39
39
  /**
40
- * 端口写入门面(面板的「应用端口」专用,不并进「修复」)。
41
- * 本仓代码**不携带任何具体端口常量**:目标值只能来自用户输入,回落值只从宿主/用户已有的
42
- * 表达式里拿(见 portExpressionFor)。
40
+ * 本插件**不写端口**(用户裁决 2026-11-05):防 431 真正有效的是 cookie 寿命注入,
41
+ * 端口只是上游给的另一条可选缓解。原先的 `PORT_WRITE` / `isValidPort` /
42
+ * `portExpressionFor` / `nextPortConfig` 已全部删除;`webserver` 行仍参与只读判定(P8)。
43
43
  */
44
- export const PORT_WRITE = Object.freeze({
45
- rowId: 'webserver',
46
- key: 'port',
47
- minimum: 1,
48
- maximum: 65535,
49
- })
50
-
51
- export function isValidPort(value) {
52
- return Number.isInteger(value) && value >= PORT_WRITE.minimum && value <= PORT_WRITE.maximum
53
- }
54
44
 
55
45
  /** 档位优先级:聚合时取最高;`skipped` 中性,永不冒充异常。 */
56
46
  const LEVEL_RANK = Object.freeze({ fail: 3, warn: 2, ok: 1, skipped: 0 })
@@ -229,37 +219,6 @@ export function aggregateAll(rows) {
229
219
  return { level, rows }
230
220
  }
231
221
 
232
- /**
233
- * 端口表达式的写法:**绝不写裸字面量**(那会夺走 `--port` 旗标权),统一用
234
- * `ctx.webStartup.port ?? +<数字>`:`??` 只在启动层未传端口时兜底,`+` 把回落值显式转成数字
235
- * (防运行期 `webStartup.port` 以字符串形态进来时 `z.natural()` 报 expected number)。范围校验在代码里做。
236
- * 优先复用已有表达式只换末尾数字(保留宿主写法);本仓不假设默认端口是哪个数。
237
- */
238
- export function portExpressionFor(existing, port) {
239
- const expr = isExpression(existing) ? String(existing.__jsExpr) : ''
240
- const withPort = expr.replace(/\+?\d+\s*$/, `+${String(port)}`)
241
- // 末尾有数字且不是目标值 → 只换它
242
- if (withPort !== expr) return withPort
243
- // 尾数已经是目标值 → 原样返回(幂等:不得把自己模板化)
244
- if (/\+?\d+\s*$/.test(expr)) return expr
245
- // 没有可复用的表达式 → 用规范形式
246
- return `ctx.webStartup.port ?? +${String(port)}`
247
- }
248
-
249
- /**
250
- * 端口写入意图:只动 `port` 一键(其余键按运行态原样保留,表达式形态也原样保留)。
251
- * @param {{current:Record<string,unknown>, inherited?:Record<string,unknown>, port:number}} input
252
- */
253
- export function nextPortConfig(input) {
254
- const current = input.current ?? {}
255
- const target = input.port
256
- if (!isValidPort(target)) return { next: current, changed: [], invalid: true }
257
- const expr = portExpressionFor(isExpression(current[PORT_WRITE.key]) ? current[PORT_WRITE.key] : input.inherited?.[PORT_WRITE.key], target)
258
- const next = { ...current, [PORT_WRITE.key]: { __jsExpr: expr } }
259
- const already = isExpression(current[PORT_WRITE.key]) && current[PORT_WRITE.key].__jsExpr === expr
260
- return { next, changed: already ? [] : [PORT_WRITE.key], expr }
261
- }
262
-
263
222
  /**
264
223
  * 产出写意图。**关键性质(VERIFIED@0.2.1-rc.5)**:`current` 来自运行时组合配置,
265
224
  * 其中表达式键已是 `{__jsExpr}` 形态,所以展开即保留——用户改过的 port、既有的表达式
package/src/report.js CHANGED
@@ -27,6 +27,35 @@ export function useDetectedLang(explicit = '') {
27
27
  return setLang(lang)
28
28
  }
29
29
 
30
+ /**
31
+ * cookie 实时读数的一句(面板/命令面/CLI 共用)。
32
+ * 没折算结果就返回 undefined —— 绝不能拿“没读到”编一个 0%。
33
+ * 借句柄快照走 `cookieLiveSnapshot`:数字是真的,但不是实时,说成实时就是谎报。
34
+ * @param {{authCookies?:number,totalCookies?:number,header?:object,snapshot?:boolean}} [stat]
35
+ * @param {string} [lang]
36
+ */
37
+ export function cookieLiveText(stat, lang) {
38
+ if (stat === undefined || stat.header === undefined) return undefined
39
+ return patchText('text', stat.snapshot === true ? 'cookieLiveSnapshot' : 'cookieLive', lang ?? activeLang(), {
40
+ count: String(stat.authCookies ?? 0),
41
+ total: String(stat.totalCookies ?? 0),
42
+ bytes: String(stat.header.bytes),
43
+ max: String(stat.header.maxHeaderBytes),
44
+ pct: String(stat.header.pct),
45
+ remaining: String(stat.header.remaining),
46
+ })
47
+ }
48
+
49
+ /**
50
+ * 读不到的分档文案。三者不能共用一句:把“路径没找到/权限不够”说成“被 Chromium 占”是误引导。
51
+ * @param {string} [reason] statCookies 的 reason
52
+ * @param {string} [lang]
53
+ */
54
+ export function cookieUnreadableText(reason, lang) {
55
+ const key = reason === 'missing' ? 'cookieMissing' : reason === 'permission' ? 'cookiePermission' : 'cookieBusy'
56
+ return patchText('text', key, lang ?? activeLang())
57
+ }
58
+
30
59
  /**
31
60
  * 组装 `/cookie-cleaner` 的输出。
32
61
  * @param {object} stat statCookies 结果(可能是 busy/permission/skipped 降级形态)
@@ -69,13 +98,17 @@ export function cookieCleanerCommandText(stat, cfg = {}) {
69
98
  }
70
99
 
71
100
  lines.push(t('dbLine', { path: stat.path, bytes: stat.bytes, total: stat.totalCookies }))
101
+ // 占用折算与面板/命令面共用同一个 `cookieLive`(同一张 9 语表),两边不得各自拼一套句式。
102
+ const liveLine = cookieLiveText(stat, lang)
72
103
  if (stat.verdict === 'ok') {
73
104
  lines.push(t('ok'))
105
+ if (liveLine !== undefined) lines.push(liveLine)
74
106
  return lines.join('\n')
75
107
  }
76
108
  lines.push(t('found', { count: stat.authCookies, expired: stat.authExpired, live: stat.authLive }))
77
109
  lines.push(t('span', { oldest: stat.oldestExpiry, newest: stat.newestExpiry }))
78
110
  lines.push(t('hostLine') + stat.byHost.map((row) => `${row.hostKey}×${row.count}`).join(', '))
111
+ if (liveLine !== undefined) lines.push(liveLine)
79
112
  lines.push(t('note'))
80
113
 
81
114
  const flag = cfg.expiredOnly === true ? ' --expired-only' : ''
@@ -149,10 +182,28 @@ export function patchReportText(result, cfg = {}) {
149
182
  lines.push(L('webserverNote'))
150
183
  }
151
184
  const conn = (result.rows ?? []).find((row) => row.rowId === 'connection')
185
+ // 真正有效的那一项先说明自已是哪个值:面板隐藏了端口控件后,用户得看得到
186
+ // 「修复」到底写什么。行上没带时退到顶层 targetDays(两侧任一侧有就能说清)。
187
+ const targetDays = result.targetDays ?? conn?.targetDays
188
+ if (Number.isInteger(targetDays) && targetDays >= 1) {
189
+ lines.push('')
190
+ lines.push(patchText('text', 'targetDaysLine', lang ?? activeLang(), { days: String(targetDays) }))
191
+ }
152
192
  if (conn?.actionable === true && cfg.outcome === undefined) {
153
193
  lines.push('')
154
194
  lines.push(L('nextFix'))
155
195
  }
196
+ // cookie 读数与面板同构:同一个折算函数出同一句话。
197
+ if (result.cookie !== undefined) {
198
+ const c = result.cookie
199
+ const readable = c.verdict !== 'skipped' && c.reason !== 'busy' && c.reason !== 'permission'
200
+ lines.push('')
201
+ if (!readable) lines.push(cookieUnreadableText(c.reason, lang))
202
+ else {
203
+ const live = cookieLiveText(c, lang)
204
+ lines.push(live ?? `${String(c.authCookies ?? '?')} dsh-auth-* / ${String(c.totalCookies ?? '?')} total`)
205
+ }
206
+ }
156
207
  if (cfg.outcome !== undefined) lines.push('', patchOutcomeText(cfg.outcome, { lang }))
157
208
  return lines.join('\n')
158
209
  }
@@ -171,18 +222,6 @@ export function patchOutcomeText(outcome, cfg = {}) {
171
222
  case 'reverted': return L('reverted', { changed: (outcome.changed ?? []).join(', ') })
172
223
  + ((outcome.rows ?? []).length > 1 ? ` (${(outcome.rows ?? []).join(', ')})` : '')
173
224
  + ((outcome.failed ?? []).length > 0 ? ` (${L('failed', { message: (outcome.failed ?? []).map((f) => f.rowId).join(', ') })})` : '')
174
- // 端口:写的是什么表达式、监听现在到底在哪个端口,全部回传真实数据(不拿“点了”当“改好了”)。
175
- case 'port-applied': {
176
- const lines = [L('portApplied', { expr: outcome.expr ?? String(outcome.port), port: outcome.port })]
177
- if (outcome.hotApplied === true) lines.push(L('portHot'))
178
- else if (outcome.hotApplied === false) lines.push(L('portRestart'))
179
- else lines.push(L('portUnknown'))
180
- // 无论哪档,端口一旦生效旧地址就不可用:补一句同步提醒(热生效时尤其关键)。
181
- lines.push(L('portSwitchWarn'))
182
- return lines.join('\n') + (outcome.retried === true ? ` (${L('vWarn')}: P12)` : '')
183
- }
184
- case 'port-busy': return L('portBusy', { port: outcome.port })
185
- case 'invalid-port': return L('portInvalid', { minimum: outcome.minimum, maximum: outcome.maximum, got: String(outcome.got) })
186
225
  case 'no-snapshot': return L('noSnapshot')
187
226
  case 'no-editor': return L('noService')
188
227
  default: return L('failed', { message: outcome.kind ?? 'unknown' })