dsh-fishpal 0.9.28 → 0.9.29
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 +90 -0
- package/lib/ui.js +15 -0
- package/package.json +1 -1
- package/tests/ui-endpoints.mjs +70 -0
- package/tests/ui-globals.mjs +234 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,96 @@
|
|
|
4
4
|
|
|
5
5
|
> 0.9.0 没有条目(那版的改动散在 git 历史里,不回填)。从 0.9.1 起逐版记录。
|
|
6
6
|
|
|
7
|
+
## [0.9.29] — 2026-12-02
|
|
8
|
+
|
|
9
|
+
**配对面板上那行红字「连不上本机服务:esc is not defined」,修了。顺手把这类型 bug 变成能机器扫的东西。**
|
|
10
|
+
|
|
11
|
+
> 用户报的原话:截图里配对按钮下面一行红字,`连不上本机服务:esc is not defined`。
|
|
12
|
+
|
|
13
|
+
### 现象:红字说的是「连不上」,其实压根没连
|
|
14
|
+
|
|
15
|
+
点「配对」以后底下出这么一行红字。第一反应是本地服务没起、或者端口不通 —— 都不是。
|
|
16
|
+
|
|
17
|
+
真相是:**那一段脚本执行到一半崩了**,`fetch` 的 `.catch` 把异常接住,把 `e.message` 打上去,于是「ReferenceError」被显示成了「连不上本机服务」。
|
|
18
|
+
|
|
19
|
+
### 根因:同一个名字,两份,不共享作用域
|
|
20
|
+
|
|
21
|
+
`lib/ui.js` 里 `esc` 这个转义函数有两个:
|
|
22
|
+
|
|
23
|
+
| 位置 | 属于谁 | 谁用 |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| 94 行 `const esc = (s) => …` | **Node 模块顶层** | 服务端拼 HTML 字符串时用 |
|
|
26
|
+
| 4673 / 4676 行 | 落在 `<script>` 块里 | **浏览器** —— 配对回执那块 |
|
|
27
|
+
|
|
28
|
+
`pairSubmit()` 的 `.then` 里拼回执时调了 `esc(String(j.sas))`。这段代码会原样输出到浏览器执行,**而浏览器里没有 `esc`** —— Node 那边那个顶层函数不会跟 HTML 一起送到前端。
|
|
29
|
+
|
|
30
|
+
于是执行到那一行抛 `ReferenceError: esc is not defined`,整个 `.then` 后半段(写入 `out.innerHTML`)一个字节都没跑。
|
|
31
|
+
|
|
32
|
+
**为什么 `node --check` 查不出来**:语法完全正确。这个错只在真浏览器里、只在真的点到那一步时发生。
|
|
33
|
+
|
|
34
|
+
### 改法
|
|
35
|
+
|
|
36
|
+
在 `<script>` 块开头(现在 2920 行)补一份浏览器端的 `esc`,函数体跟 Node 那份一致:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
function esc(s) {
|
|
40
|
+
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
|
|
41
|
+
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
名字一样、作用域两套 —— 这不是将就,是这两处本来就该各有一份。注释里写清了为什么,以及当初是怎么撞上的。
|
|
47
|
+
|
|
48
|
+
### 新增:两个能机器跑的审计器
|
|
49
|
+
|
|
50
|
+
这一类 bug(**在浏览器里引用了浏览器没有的名字**)的共同形状是:点了没反应、或者显示一个不相关的错误、整块脚本静默死掉。它不报语法错,只在你真点的时候死。靠肉眼翻 5000 行不现实,所以做成两个扫描器。
|
|
51
|
+
|
|
52
|
+
**`tests/ui-globals.mjs`** —— 拿**真实渲染出来的 HTML**(`renderUi({})`)来扫,不是猜模板结构:
|
|
53
|
+
|
|
54
|
+
- `A 类`:`<script>` 块里被**调用**、但块内没定义、也不是浏览器内置的名字 —— `esc` 就是这一类
|
|
55
|
+
- `B 类`:HTML 内联处理器 `on*=“fn(...)”` 引用的函数,`<script>` 里没有 —— 函数改名/删掉之后按钮点了永远没反应,同样是静默坏
|
|
56
|
+
- 另外每个 `<script>` 块单独过一遍语法(语法错整块都不执行)
|
|
57
|
+
- 最后单点 `esc` 这一对:Node 那份和浏览器那份都得在,少一份就是这个 bug 复发
|
|
58
|
+
|
|
59
|
+
实测:script 块 1 个 · 定义名 218 个 · 被调用名 93 个 · 内联处理器 21 处 · **7 条全过**。
|
|
60
|
+
|
|
61
|
+
**`tests/ui-endpoints.mjs`** —— 同一类 bug 的另一面:前端 `fetch('/api/xxx')` 打到后端没定义的路由,返回 404,页面上照样显示失败。
|
|
62
|
+
|
|
63
|
+
扫 `ui.js` 里出现的每个 `/api/名字`,对 `localserver.js` 里每条 `req.url === '/api/名字'`:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
前端用到 16 个:avatar, block, bottle, bottlehistory, budget, config,
|
|
67
|
+
daily, favorite, friend, global, pair, ping, privacy,
|
|
68
|
+
recall, review, wordfilter
|
|
69
|
+
后端定义 16 个:同上,完全一致
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**3 条全过**,零缺口。以后哪边改了名,这个测试会红。
|
|
73
|
+
|
|
74
|
+
### 修了两个验证器自己的 bug
|
|
75
|
+
|
|
76
|
+
审计器也会骗人,这一轮撞到两个:
|
|
77
|
+
|
|
78
|
+
1. **`_audit-css.mjs` 的 data 属性对账**:原来拿一个**手写的 7 项清单**(`data-id/at/mi/text/from/to/cmd`)去比 `getAttribute` 读到的名字。实际代码有 29 个 `data-*`,所以凡是清单外的全报 `✗` —— 屏幕上二十多个红叉,其实一个 bug 都没有。**验证器自己在骗人。** 改成真的去扫代码里印出了哪些 `data-*`(含 `setAttribute` 运行时设的),并对账时只比 `data-*` 开头的读名(`getAttribute('href')` 跟这件事无关,原来也被拉进来比,是类别错误)。
|
|
79
|
+
|
|
80
|
+
2. 修完之后剩下两个 ✗,查了代码:`data-open` 是 `bqToggle` 自己 `setAttribute('data-open','1')` 设的运行时状态,扫 `setAttribute` 之后就 ✓ 了;`href` 是上面说的类别错误。**两个都是假警报,不是 bug。**
|
|
81
|
+
|
|
82
|
+
### 验证
|
|
83
|
+
|
|
84
|
+
| 项目 | 结果 |
|
|
85
|
+
|---|---|
|
|
86
|
+
| `node --check lib/ui.js` | OK |
|
|
87
|
+
| `tests/ui-globals.mjs` | 7 条全过 |
|
|
88
|
+
| `tests/ui-endpoints.mjs` | 3 条全过 |
|
|
89
|
+
| `_audit-css.mjs` | CSS 全定义、data 属性全部对上 |
|
|
90
|
+
| `_audit-tools.mjs` | 21 个工具入口,缺入口 0 |
|
|
91
|
+
| `tests/run.mjs` 全量 | **798 通过,0 失败** |
|
|
92
|
+
|
|
93
|
+
### 用户要做的
|
|
94
|
+
|
|
95
|
+
**重启一次 DSH。** `ui.js` 是 Node 模块,启动时才加载,改完不重启不生效。重启后点「配对」,红字应该没了。
|
|
96
|
+
|
|
7
97
|
## [0.9.28] — 2026-12-01
|
|
8
98
|
|
|
9
99
|
**自己发出去的漂流瓶,被循环广播绕回来当成了别人的瓶子收下。这一版把它按住。**
|
package/lib/ui.js
CHANGED
|
@@ -2908,6 +2908,21 @@ var FPSET = ${JSON.stringify(settings || null)};
|
|
|
2908
2908
|
/* 这一页渲染时的数据指纹。前端拿它比对,变了才刷新(不整页重载就不会闪)。 */
|
|
2909
2909
|
var FP_REV = ${JSON.stringify(String(rev || ''))};
|
|
2910
2910
|
|
|
2911
|
+
/**
|
|
2912
|
+
* 浏览器端的转义。
|
|
2913
|
+
* ★ 2026-12-02 修的(用户截图里的红字「连不上本机服务:esc is not defined」):
|
|
2914
|
+
* 配对回执那块(pairSubmit 的 .then 里)调了 esc(),可 esc 定义在 Node 层
|
|
2915
|
+
* (服务端渲染用),浏览器 <script> 里根本没有这个名字 ——
|
|
2916
|
+
* 执行到那儿直接抛 ReferenceError,fetch 的 .catch 把它接住,
|
|
2917
|
+
* 显示成「连不上本机服务」。不是连不上,是脚本炸了。
|
|
2918
|
+
* 这个模块里 esc 有两份(Node 的 + 这里的),名字一样但不共享作用域。
|
|
2919
|
+
*/
|
|
2920
|
+
function esc(s) {
|
|
2921
|
+
return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) {
|
|
2922
|
+
return { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c];
|
|
2923
|
+
});
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2911
2926
|
function fpsetTarget() {
|
|
2912
2927
|
return FPSET && FPSET.port ? 'http://127.0.0.1:' + FPSET.port + '/api/friend' : null;
|
|
2913
2928
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 前端 fetch 的路由 vs 后端定义的路由(2026-12-02 建)
|
|
3
|
+
*
|
|
4
|
+
* 起因跟 ui-globals.mjs 同一类:**两边约定对不上,表现成「连不上本机服务」**。
|
|
5
|
+
* 前端 fetch('/api/xxx') 打到后端没定义的路由,返回 404,
|
|
6
|
+
* 页面上显示的就是失败 —— 但真实原因不是网络,是名字对不上。
|
|
7
|
+
*
|
|
8
|
+
* 判据:ui.js 里出现过的 `/api/名字`,localserver.js 里必须有一条
|
|
9
|
+
* `req.url === '/api/名字'`;反过来后端有、前端没用是正常的(给别的入口用)。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
|
|
14
|
+
let pass = 0;
|
|
15
|
+
const fails = [];
|
|
16
|
+
|
|
17
|
+
function ok(cond, label, detail) {
|
|
18
|
+
if (cond) pass += 1;
|
|
19
|
+
else fails.push(detail ? `${label} —— ${detail}` : label);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const uiSrc = fs.readFileSync(new URL('../lib/ui.js', import.meta.url), 'utf8');
|
|
23
|
+
const srvSrc = fs.readFileSync(new URL('../lib/localserver.js', import.meta.url), 'utf8');
|
|
24
|
+
|
|
25
|
+
// ── 前端用到的路由 ────────────────────────────────────────────
|
|
26
|
+
const front = new Set();
|
|
27
|
+
{
|
|
28
|
+
const re = /\/api\/([a-zA-Z][\w-]*)/g;
|
|
29
|
+
let m;
|
|
30
|
+
while ((m = re.exec(uiSrc)) !== null) front.add(m[1]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ── 后端定义的路由 ────────────────────────────────────────────
|
|
34
|
+
const back = new Set();
|
|
35
|
+
{
|
|
36
|
+
const re = /req\.url\s*===\s*['"]\/api\/([a-zA-Z][\w-]*)['"]/g;
|
|
37
|
+
let m;
|
|
38
|
+
while ((m = re.exec(srvSrc)) !== null) back.add(m[1]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
ok(front.size >= 10, '前端至少用了 10 个 /api 路由', `实际 ${front.size} 个`);
|
|
42
|
+
ok(back.size >= 10, '后端至少定义了 10 个 /api 路由', `实际 ${back.size} 个`);
|
|
43
|
+
|
|
44
|
+
// ── 核心判据:前端用到的每个路由,后端都得有 ───────────────────
|
|
45
|
+
const missing = [...front].filter((n) => !back.has(n)).sort();
|
|
46
|
+
ok(
|
|
47
|
+
missing.length === 0,
|
|
48
|
+
'前端 fetch 的每个路由后端都定义了',
|
|
49
|
+
missing.length ? `后端没有:${missing.join(', ')}` : '',
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// ── 反向:后端有但前端没用的,只报告(可能是别的入口在用)──────
|
|
53
|
+
const unused = [...back].filter((n) => !front.has(n)).sort();
|
|
54
|
+
|
|
55
|
+
console.log('');
|
|
56
|
+
console.log('=== 前后端路由对账 ===');
|
|
57
|
+
console.log(`前端用到 ${front.size} 个:${[...front].sort().join(', ')}`);
|
|
58
|
+
console.log(`后端定义 ${back.size} 个:${[...back].sort().join(', ')}`);
|
|
59
|
+
if (unused.length) {
|
|
60
|
+
console.log(`ℹ️ 后端有、前端没引用(可能是工具/别处用):${unused.join(', ')}`);
|
|
61
|
+
}
|
|
62
|
+
console.log('');
|
|
63
|
+
|
|
64
|
+
if (fails.length) {
|
|
65
|
+
console.log(`❌ ${fails.length} 条没过,${pass} 条通过`);
|
|
66
|
+
for (const f of fails) console.log(' ✗ ' + f);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
} else {
|
|
69
|
+
console.log(`✅ 全部通过(${pass} 条)`);
|
|
70
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI 全局引用审计(2026-12-02 建)
|
|
3
|
+
*
|
|
4
|
+
* 起因:用户截图里那行红字「连不上本机服务:esc is not defined」。
|
|
5
|
+
* 根因不是网络 —— 是浏览器 <script> 块里调了只存在于 Node 层的 esc(),
|
|
6
|
+
* 执行到就抛 ReferenceError,被 fetch 的 .catch 接住,显示成「连不上」。
|
|
7
|
+
*
|
|
8
|
+
* 这一类 bug 的共同形状:**在浏览器里引用了浏览器没有的名字**,
|
|
9
|
+
* 表现为「点了没反应 / 显示一个不相关的错误 / 整块脚本静默死掉」。
|
|
10
|
+
* 它不会让 node --check 报错(语法是对的),只在真浏览器里炸。
|
|
11
|
+
*
|
|
12
|
+
* 这个脚本抓两类:
|
|
13
|
+
* A. `<script>` 块里被**调用**、但块内没定义、也不是浏览器内置的名字
|
|
14
|
+
* (esc 就是这一类)
|
|
15
|
+
* B. HTML 内联处理器 `on*=“fn(...)”` 里引用的函数,script 块里没有
|
|
16
|
+
* (函数改名/删掉之后,按钮点了永远没反应 —— 也是静默坏)
|
|
17
|
+
*
|
|
18
|
+
* 判据来源:真实渲染出来的 HTML(renderUi),不是猜模板结构。
|
|
19
|
+
* 这样连服务端拼进 HTML 的东西一起验。
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { renderUi } from '../lib/ui.js';
|
|
23
|
+
|
|
24
|
+
let pass = 0;
|
|
25
|
+
const fails = [];
|
|
26
|
+
|
|
27
|
+
function ok(cond, label, detail) {
|
|
28
|
+
if (cond) {
|
|
29
|
+
pass += 1;
|
|
30
|
+
} else {
|
|
31
|
+
fails.push(detail ? `${label} —— ${detail}` : label);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ── 渲染真实 HTML ─────────────────────────────────────────────
|
|
36
|
+
const html = renderUi({});
|
|
37
|
+
ok(typeof html === 'string' && html.length > 5000, 'renderUi 输出非空 HTML', `长度 ${html?.length}`);
|
|
38
|
+
|
|
39
|
+
// ── 1) 取出所有 <script> 块 ───────────────────────────────────
|
|
40
|
+
const scriptBlocks = [];
|
|
41
|
+
{
|
|
42
|
+
const re = /<script(?:\s[^>]*)?>([\s\S]*?)<\/script>/gi;
|
|
43
|
+
let m;
|
|
44
|
+
while ((m = re.exec(html)) !== null) {
|
|
45
|
+
scriptBlocks.push({ code: m[1], at: m.index });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
ok(scriptBlocks.length >= 1, 'HTML 里至少有一个 <script> 块', `找到 ${scriptBlocks.length} 个`);
|
|
49
|
+
|
|
50
|
+
const allCode = scriptBlocks.map((b) => b.code).join('\n;\n');
|
|
51
|
+
|
|
52
|
+
// ── 2) 每个块先过一遍语法(语法错整块都不执行)────────────────
|
|
53
|
+
for (let i = 0; i < scriptBlocks.length; i += 1) {
|
|
54
|
+
let syntaxErr = '';
|
|
55
|
+
try {
|
|
56
|
+
// 只解析不执行。new Function 会对函数体做完整的语法检查。
|
|
57
|
+
// eslint-disable-next-line no-new-func
|
|
58
|
+
new Function(scriptBlocks[i].code);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
syntaxErr = e.message;
|
|
61
|
+
}
|
|
62
|
+
ok(!syntaxErr, `<script> 块 #${i + 1} 语法能解析`, syntaxErr);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── 3) 浏览器里本来就有的名字(白名单)────────────────────────
|
|
66
|
+
const BROWSER_GLOBALS = new Set([
|
|
67
|
+
// 语言内建
|
|
68
|
+
'Object', 'Array', 'String', 'Number', 'Boolean', 'Symbol', 'BigInt', 'Date',
|
|
69
|
+
'RegExp', 'Error', 'TypeError', 'RangeError', 'SyntaxError', 'Promise', 'Map',
|
|
70
|
+
'Set', 'WeakMap', 'WeakSet', 'JSON', 'Math', 'Intl', 'Proxy', 'Reflect',
|
|
71
|
+
'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'encodeURIComponent',
|
|
72
|
+
'decodeURIComponent', 'encodeURI', 'decodeURI', 'escape', 'unescape',
|
|
73
|
+
'Function', 'eval', 'structuredClone', 'queueMicrotask',
|
|
74
|
+
// 浏览器环境
|
|
75
|
+
'window', 'document', 'navigator', 'location', 'history', 'screen', 'console',
|
|
76
|
+
'fetch', 'XMLHttpRequest', 'FormData', 'URL', 'URLSearchParams', 'Blob', 'File',
|
|
77
|
+
'FileReader', 'Headers', 'Request', 'Response', 'AbortController', 'AbortSignal',
|
|
78
|
+
'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval',
|
|
79
|
+
'requestAnimationFrame', 'cancelAnimationFrame', 'requestIdleCallback',
|
|
80
|
+
'alert', 'confirm', 'prompt', 'localStorage', 'sessionStorage', 'indexedDB',
|
|
81
|
+
'addEventListener', 'removeEventListener', 'getComputedStyle', 'matchMedia',
|
|
82
|
+
'CustomEvent', 'Event', 'EventTarget', 'Node', 'Element', 'HTMLElement',
|
|
83
|
+
'Image', 'Audio', 'Worker', 'WebSocket', 'Notification', 'CSS', 'TextDecoder',
|
|
84
|
+
'TextEncoder', 'atob', 'btoa', 'crypto', 'performance', 'innerWidth',
|
|
85
|
+
'innerHeight', 'scrollTo', 'scrollBy', 'open', 'close', 'postMessage',
|
|
86
|
+
'MutationObserver', 'IntersectionObserver', 'ResizeObserver', 'DOMParser',
|
|
87
|
+
'ClipboardItem', 'AudioContext', 'webkitAudioContext', 'ReadableStream',
|
|
88
|
+
'Uint8Array', 'Uint16Array', 'Uint32Array', 'Int8Array', 'Int16Array',
|
|
89
|
+
'Int32Array', 'Float32Array', 'Float64Array', 'ArrayBuffer', 'DataView',
|
|
90
|
+
'isArray', 'isInteger', 'isSafeInteger',
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
// ── 4) script 块里定义了哪些名字 ──────────────────────────────
|
|
94
|
+
function collectDefined(code) {
|
|
95
|
+
const names = new Set();
|
|
96
|
+
const patterns = [
|
|
97
|
+
/\bfunction\s+([A-Za-z_$][\w$]*)\s*\(/g,
|
|
98
|
+
/\b(?:var|let|const)\s+([A-Za-z_$][\w$]*)\s*=/g,
|
|
99
|
+
// window.xxx = ... / window[“xxx”] = ...
|
|
100
|
+
/\bwindow\s*\.\s*([A-Za-z_$][\w$]*)\s*=/g,
|
|
101
|
+
// xxx = function / xxx = (a) => (隐式全局,脚本非严格模式下成立)
|
|
102
|
+
/^\s*([A-Za-z_$][\w$]*)\s*=\s*(?:function|\()/gm,
|
|
103
|
+
];
|
|
104
|
+
for (const re of patterns) {
|
|
105
|
+
let m;
|
|
106
|
+
while ((m = re.exec(code)) !== null) names.add(m[1]);
|
|
107
|
+
}
|
|
108
|
+
return names;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const defined = collectDefined(allCode);
|
|
112
|
+
|
|
113
|
+
// ── 5) script 块里“调用了”哪些名字 ────────────────────────────
|
|
114
|
+
// 排除:属性访问 obj.fn( 、字符串里的、注释里的、函数定义本身、new X(
|
|
115
|
+
function collectCalled(code) {
|
|
116
|
+
// 去掉注释和字符串,减少误报(粗粒度,够用)
|
|
117
|
+
const clean = code
|
|
118
|
+
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
119
|
+
.replace(/(^|[^:\\])\/\/[^\n]*/g, '$1 ')
|
|
120
|
+
.replace(/'(?:\\.|[^'\\])*'/g, "''")
|
|
121
|
+
.replace(/"(?:\\.|[^"\\])*"/g, '""')
|
|
122
|
+
.replace(/`(?:\\.|[^`\\])*`/g, '``');
|
|
123
|
+
|
|
124
|
+
const names = new Set();
|
|
125
|
+
// 前面不是 . 也不是标识符字符 —— 即不是 obj.fn(
|
|
126
|
+
const re = /(^|[^.\w$])([A-Za-z_$][\w$]*)\s*\(/g;
|
|
127
|
+
let m;
|
|
128
|
+
while ((m = re.exec(clean)) !== null) {
|
|
129
|
+
const name = m[2];
|
|
130
|
+
// 关键字排除
|
|
131
|
+
if (['if', 'for', 'while', 'switch', 'catch', 'function', 'return', 'typeof',
|
|
132
|
+
'new', 'delete', 'void', 'in', 'of', 'do', 'else', 'try', 'finally',
|
|
133
|
+
'throw', 'case', 'with', 'yield', 'await', 'async'].includes(name)) continue;
|
|
134
|
+
names.add(name);
|
|
135
|
+
}
|
|
136
|
+
return names;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const called = collectCalled(allCode);
|
|
140
|
+
|
|
141
|
+
// ── 6) A 类:调用了但没定义、也不是浏览器内置 ─────────────────
|
|
142
|
+
const undefinedCalls = [];
|
|
143
|
+
for (const name of called) {
|
|
144
|
+
if (defined.has(name)) continue;
|
|
145
|
+
if (BROWSER_GLOBALS.has(name)) continue;
|
|
146
|
+
// 大写开头多半是构造器/类(可能来自库全局),也放过 —— 但记录单独列
|
|
147
|
+
if (/^[A-Z]/.test(name)) continue;
|
|
148
|
+
undefinedCalls.push(name);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
ok(
|
|
152
|
+
undefinedCalls.length === 0,
|
|
153
|
+
'A 类:script 块里没有「调用了不存在的小写名字」',
|
|
154
|
+
undefinedCalls.length ? `未定义:${undefinedCalls.join(', ')}` : '',
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
// 大写开头也查一遍,但只报告不判失败(可能是合法的库全局)
|
|
158
|
+
const upperUndefined = [];
|
|
159
|
+
for (const name of called) {
|
|
160
|
+
if (defined.has(name) || BROWSER_GLOBALS.has(name)) continue;
|
|
161
|
+
if (!/^[A-Z]/.test(name)) continue;
|
|
162
|
+
upperUndefined.push(name);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── 7) B 类:内联 on*= 处理器引用的函数是否存在 ───────────────
|
|
166
|
+
// HTML 里的 on*="foo(...)" 只认全局函数;script 里没有就是点了没反应。
|
|
167
|
+
const JS_KEYWORDS = new Set([
|
|
168
|
+
'if', 'else', 'return', 'for', 'while', 'switch', 'catch', 'function', 'typeof',
|
|
169
|
+
'new', 'delete', 'void', 'in', 'of', 'do', 'try', 'finally', 'throw', 'case',
|
|
170
|
+
'with', 'yield', 'await', 'async', 'let', 'const', 'var',
|
|
171
|
+
]);
|
|
172
|
+
|
|
173
|
+
const inlineHandlers = [];
|
|
174
|
+
{
|
|
175
|
+
const re = /\son([a-z]+)\s*=\s*"([^"]*)"/gi;
|
|
176
|
+
let m;
|
|
177
|
+
while ((m = re.exec(html)) !== null) {
|
|
178
|
+
const body = m[2];
|
|
179
|
+
// 只认“独立的”调用:前面不是 . 也不是标识符字符。
|
|
180
|
+
// 这样 e.preventDefault() 里的 preventDefault 不算缺函数(它是方法),
|
|
181
|
+
// if ( / for ( 这类关键字也不算。
|
|
182
|
+
const callRe = /(^|[^.\w$])([A-Za-z_$][\w$]*)\s*\(/g;
|
|
183
|
+
let c;
|
|
184
|
+
while ((c = callRe.exec(body)) !== null) {
|
|
185
|
+
const name = c[2];
|
|
186
|
+
if (JS_KEYWORDS.has(name)) continue;
|
|
187
|
+
inlineHandlers.push({ event: m[1], fn: name });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const missingHandlers = [];
|
|
193
|
+
for (const h of inlineHandlers) {
|
|
194
|
+
if (BROWSER_GLOBALS.has(h.fn)) continue;
|
|
195
|
+
if (/^[A-Z]/.test(h.fn)) continue;
|
|
196
|
+
if (!defined.has(h.fn)) missingHandlers.push(`${h.fn}(on${h.event})`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
ok(
|
|
200
|
+
missingHandlers.length === 0,
|
|
201
|
+
'B 类:内联 on*= 引用的函数都在 script 块里定义过',
|
|
202
|
+
missingHandlers.length ? `缺失:${missingHandlers.join(', ')}` : '',
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
// ── 8) 兜底:esc 这类「Node 与浏览器同名」的坑单独点名 ─────────
|
|
206
|
+
// esc 现在两份都该有;少一份就是同类 bug 复发。
|
|
207
|
+
{
|
|
208
|
+
const nodeEsc = /^const esc = \(s\) =>/m.test(
|
|
209
|
+
// 源码层面才知道 Node 那份;这里用模块源码读一遍
|
|
210
|
+
(await import('node:fs')).readFileSync(new URL('../lib/ui.js', import.meta.url), 'utf8'),
|
|
211
|
+
);
|
|
212
|
+
ok(nodeEsc, 'Node 层 esc 还在(服务端渲染用)', '没了说明模板拼装会出错');
|
|
213
|
+
|
|
214
|
+
const browserEsc = /\bfunction\s+esc\s*\(\s*s\s*\)/.test(allCode);
|
|
215
|
+
ok(browserEsc, '浏览器层 esc 也在(2026-12-02 补的)', '缺了就是「esc is not defined」复发');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ── 输出 ─────────────────────────────────────────────────────
|
|
219
|
+
console.log('');
|
|
220
|
+
console.log('=== UI 全局引用审计 ===');
|
|
221
|
+
console.log(`script 块: ${scriptBlocks.length} 个 · 定义名 ${defined.size} 个 · 被调用名 ${called.size} 个`);
|
|
222
|
+
console.log(`内联处理器: ${inlineHandlers.length} 处`);
|
|
223
|
+
if (upperUndefined.length) {
|
|
224
|
+
console.log(`⚠️ 大写开头的未定义调用(可能来自库,未判失败): ${upperUndefined.join(', ')}`);
|
|
225
|
+
}
|
|
226
|
+
console.log('');
|
|
227
|
+
|
|
228
|
+
if (fails.length) {
|
|
229
|
+
console.log(`❌ ${fails.length} 条没过,${pass} 条通过`);
|
|
230
|
+
for (const f of fails) console.log(' ✗ ' + f);
|
|
231
|
+
process.exit(1);
|
|
232
|
+
} else {
|
|
233
|
+
console.log(`✅ 全部通过(${pass} 条)`);
|
|
234
|
+
}
|