dsh-cookie-cleaner 0.6.4 → 0.7.1

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.
@@ -0,0 +1,137 @@
1
+ /**
2
+ * dsh-cookie-cleaner — 历史注入残留的识别与清理(纯函数面,可单测)。
3
+ *
4
+ * 为什么需要这个模块:
5
+ * 0.2.1 之前,本插件曾在面板/命令面上提供过「应用端口」动作,把 `webserver` 行的
6
+ * `port` 改写成表达式 `ctx.webStartup.port ?? +<用户填的数>`(见 556e37b 及更早)。
7
+ * 2026-11-05 的裁决把端口写入能力整体删除(端口属启动行为面,不在本工具职责内),
8
+ * 但**已经写进用户 profile 的那一行不会自己消失**——它留在 `cordis.patch.yml` 里继续生效。
9
+ *
10
+ * 这一行的危害不是"插件又写端口了",而是:
11
+ * 1. 用户以为本工具已经不碰端口,实际 profile 里还钉着当年填的数;
12
+ * 2. 该表达式与 bundle 层原值可能已经不同(用户后来改过启动参数/升级过宿主),
13
+ * 表现为「启动层没传 --port 时落在旧数字上」这种难以归因的行为;
14
+ * 3. 卸载插件后残留仍在,用户找不到是谁写的。
15
+ *
16
+ * 清理原则(为什么只在 0.2.2-rc.2+ 执行):
17
+ * - 0.2.2-rc.2 起桌面外壳自带 `removeLegacyHostCookies`,cookie 侧的历史包袱由官方接管,
18
+ * 插件不再需要用"钉端口"这条侧路去压 cookie 增长;此时回收旧注入是纯收益。
19
+ * - 更早的宿主上,钉端口仍是上游 discussions 给的可选缓解之一,贸然回收可能让用户
20
+ * 重新落回"每次启动随机端口 → cookie 累积"的老路,所以那一段只**报告不清理**。
21
+ *
22
+ * 安全边界(这条最重要):**只删本插件自己写过的那一种形态**。
23
+ * 判定必须同时满足:值是指向 `webStartup.port` 的 `!!js` 表达式。用户自己手写的
24
+ * 字面量端口(P8 报的那类)、用户自己写的别的表达式,一律不碰——"看到但不替你改"。
25
+ */
26
+ import { isExpression } from './patch-model.js'
27
+
28
+ /** 本插件历史版本写过的端口表达式形态:`ctx.webStartup.port ?? <fallback>`。 */
29
+ const LEGACY_PORT_EXPRESSION = /^\s*ctx\.webStartup\.port\s*\?\?\s*(\+?\s*\d+)\s*$/
30
+
31
+ /**
32
+ * 判断一个 `!!js` 表达式值是否为本插件历史注入的端口表达式。
33
+ * @param {unknown} value 运行态配置里的值(表达式已被内核转成 `{__jsExpr}`)
34
+ * @returns {{ legacy: boolean, fallback?: number, expression?: string }}
35
+ */
36
+ export function classifyLegacyPort(value) {
37
+ if (!isExpression(value)) return { legacy: false }
38
+ const expression = value.__jsExpr
39
+ const hit = LEGACY_PORT_EXPRESSION.exec(expression)
40
+ if (hit === null) return { legacy: false, expression }
41
+ const fallback = Number(hit[1].replace(/[+\s]/g, ''))
42
+ return {
43
+ legacy: true,
44
+ fallback: Number.isInteger(fallback) ? fallback : undefined,
45
+ expression,
46
+ }
47
+ }
48
+
49
+ /**
50
+ * 扫描一行 config,找出本插件历史注入的残留。
51
+ *
52
+ * @param {{rowId?: string, current?: Record<string, unknown>, inherited?: Record<string, unknown>}} input
53
+ * `current` = 运行时组合后的配置;`inherited` = bundle 层原值(用于给出「还原成什么」)
54
+ * @returns {{ found: boolean, rowId: string, key: string, expression?: string, fallback?: number, restoreTo?: unknown }}
55
+ */
56
+ export function findLegacyInjection(input = {}) {
57
+ const rowId = String(input.rowId ?? 'webserver')
58
+ const current = input.current ?? {}
59
+ const inherited = input.inherited ?? {}
60
+ const classified = classifyLegacyPort(current.port)
61
+ if (!classified.legacy) {
62
+ return { found: false, rowId, key: 'port' }
63
+ }
64
+ const bundleValue = inherited.port
65
+ return {
66
+ found: true,
67
+ rowId,
68
+ key: 'port',
69
+ expression: classified.expression,
70
+ fallback: classified.fallback,
71
+ // 还原目标:bundle 层原值(通常是另一个表达式,也可能是字面量)。
72
+ // 拿不到 bundle 值时给 undefined,调用方据此只删覆盖行、不猜值。
73
+ restoreTo: isExpression(bundleValue) || typeof bundleValue === 'number' ? bundleValue : undefined,
74
+ }
75
+ }
76
+
77
+ /**
78
+ * 产出去掉历史注入后的 config。
79
+ *
80
+ * 语义选择:**删除该键**而不是写回 bundle 原值。
81
+ * 理由:覆盖行里少一个键 = 该键回落到 bundle 层默认,这正是"清理掉插件写的东西"该有的结果;
82
+ * 把 bundle 表达式抄进 profile 反而把上游实现细节固化进了用户配置(上游一改就脱节)。
83
+ *
84
+ * @param {{current: Record<string, unknown>, key?: string}} input
85
+ * @returns {{ next: Record<string, unknown>, changed: string[] }}
86
+ */
87
+ export function withoutLegacyInjection(input) {
88
+ const current = input.current ?? {}
89
+ const key = String(input.key ?? 'port')
90
+ if (!(key in current)) return { next: { ...current }, changed: [] }
91
+ const next = { ...current }
92
+ delete next[key]
93
+ return { next, changed: [key] }
94
+ }
95
+
96
+ /**
97
+ * 清理是否可执行 —— **以官方清理能力为准**,而不是版本号字符串。
98
+ *
99
+ * 为什么不判版本号:宿主 `ctx` 并不暴露版本(已核对 cordis/core 的 ctx 面),
100
+ * 插件能拿到的只有"外壳里有没有 `removeLegacyHostCookies`"这个**事实**。
101
+ * 而它恰好就是 0.2.2-rc.2 那条线的分界——官方接管了 cookie 清理,
102
+ * 插件才不需要再用钉端口去压 cookie 增长,回收旧注入才是纯收益。
103
+ *
104
+ * @param {{nativeCleanup?: boolean}} input
105
+ * @returns {boolean}
106
+ */
107
+ export function canCleanLegacy(input = {}) {
108
+ return input.nativeCleanup === true
109
+ }
110
+
111
+ /**
112
+ * 宿主版本是否达到「可以安全回收旧注入」的线(0.2.2-rc.2+)。
113
+ *
114
+ * 不引入 semver 依赖:本插件只判这一条线,手写比较足够,且规则显式可测。
115
+ * 版本串形态(上游实际发布):`0.2.2-rc.2` / `0.2.2` / `0.2.1-rc.5`。
116
+ *
117
+ * @param {string} version 宿主版本(如 `0.2.2-rc.3`)
118
+ * @returns {boolean}
119
+ */
120
+ export function supportsLegacyCleanup(version) {
121
+ const parsed = parseVersion(version)
122
+ if (parsed === undefined) return false
123
+ const [major, minor, patch, rc] = parsed
124
+ if (major !== 0) return major > 0
125
+ if (minor !== 2) return minor > 2
126
+ if (patch !== 2) return patch > 2
127
+ // 0.2.2 线:正式版(rc === null)或 rc >= 2 都可以;rc.0/rc.1 不行。
128
+ return rc === null || rc >= 2
129
+ }
130
+
131
+ /** 解析 `x.y.z[-rc.n]`;解析不了返回 undefined(调用方按「不支持」处理,fail-safe)。 */
132
+ function parseVersion(version) {
133
+ if (typeof version !== 'string') return undefined
134
+ const m = /^(\d+)\.(\d+)\.(\d+)(?:-rc\.(\d+))?$/.exec(version.trim())
135
+ if (m === null) return undefined
136
+ return [Number(m[1]), Number(m[2]), Number(m[3]), m[4] === undefined ? null : Number(m[4])]
137
+ }