dsh-zdiary-kit 0.7.6 → 0.9.0
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/README.md +96 -16
- package/dist/app/plugin.js +54 -20
- package/dist/cli.js +170 -25
- package/dist/config/store.js +37 -0
- package/dist/features/readonly/feature.js +563 -0
- package/dist/features/readonly/html.js +255 -0
- package/dist/features/settings/client.js +32 -9
- package/dist/features/settings/feature.js +71 -17
- package/dist/features/zk-tools/feature.js +228 -42
- package/dist/lib/agents-template.js +49 -11
- package/dist/lib/dream.js +213 -16
- package/dist/lib/http.js +26 -0
- package/dist/lib/lint.js +329 -90
- package/dist/lib/memory-format.js +100 -0
- package/dist/lib/memory.js +201 -24
- package/dist/lib/notebook-config.js +188 -0
- package/dist/lib/notebook.js +357 -21
- package/dist/lib/wikilink.js +77 -0
- package/dist/lib/zk.js +108 -17
- package/docs/features/memory.md +76 -9
- package/docs/features/provisioning.md +47 -9
- package/docs/features/readonly.md +73 -0
- package/docs/features/settings.md +20 -6
- package/docs/features/zk-tools.md +64 -6
- package/package.json +1 -1
package/dist/lib/dream.js
CHANGED
|
@@ -6,15 +6,28 @@
|
|
|
6
6
|
* · daily 里 `## Loose ends` 小节还没划掉的项;
|
|
7
7
|
* · 被引用最多的笔记(一次 `zk graph` 数入链)。
|
|
8
8
|
* 「rejected」是机械规则,不是相关性判断:authority=informal 与无标签两条,别再加。
|
|
9
|
+
*
|
|
10
|
+
* 晋升是一道**门**,门得知道门里已经有什么(P0-2):读 MEMORY.md 判「这条已经晋升过」,
|
|
11
|
+
* 已经进去的移出候选、单列一栏 —— 否则同一批笔记每周原样重复,仪式几次之后就没人再看了。
|
|
12
|
+
* 窗口默认也不再固定 7 天,而是跟着 DREAMS.md 的「上次做梦」走:漏做一周不会把笔记永久甩出窗口。
|
|
9
13
|
*/
|
|
10
14
|
import fs from 'node:fs';
|
|
11
15
|
import path from 'node:path';
|
|
12
16
|
import { allNotes, graphEdges, localDate, MAX_SCAN } from './zk.js';
|
|
13
17
|
import { parseFrontmatter } from './frontmatter.js';
|
|
18
|
+
// memory.ts 也 import 本模块(DREAM_INTERVAL_DAYS / daysSinceDream):这里是运行期才取用,
|
|
19
|
+
// 循环引用在 ESM 下安全 —— 不要在模块顶层调用 memoryUsage。
|
|
20
|
+
import { memoryUsage } from './memory.js';
|
|
21
|
+
import { daysSinceDate, entryDate, isRetiredEntry, splitMemoryEntries, stripRetiredEntries } from './memory-format.js';
|
|
22
|
+
import { normalizeTitle, resolveWikiTarget, WIKI_LINK, wikiIndex } from './wikilink.js';
|
|
14
23
|
/** 每栏最多回多少条(计划 §4 M3 的 50 条上限) */
|
|
15
24
|
export const MAX_ITEMS = 50;
|
|
16
25
|
/** topCited 只取前几名:给 agent 一个「本周被反复引用」的提示,不是排行榜 */
|
|
17
26
|
export const TOP_CITED = 10;
|
|
27
|
+
/** 窗口之外、还没晋升的笔记最多回多少条:漏做一周不该让它们永久掉队 */
|
|
28
|
+
export const MAX_BACKLOG = 10;
|
|
29
|
+
/** 标题包含匹配的最短长度:短标题(「RAG」「额度」)容易在任意句子里撞上 */
|
|
30
|
+
export const MIN_TITLE_MATCH = 6;
|
|
18
31
|
/** `## Loose ends` / `## 未完成` / `## 待办` 都算(大小写不敏感,单复数都认) */
|
|
19
32
|
const LOOSE_HEADING = /^##\s+(loose ends?|未完成|待办(?:事项)?)\s*$/i;
|
|
20
33
|
/** 周节奏:AGENTS.md 的「每周」就按这个天数算。没有定时器,只在记忆注入里提醒到期 */
|
|
@@ -47,6 +60,10 @@ export function daysSinceDream(last, now = new Date()) {
|
|
|
47
60
|
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
48
61
|
return Math.round((today.getTime() - at.getTime()) / 86_400_000);
|
|
49
62
|
}
|
|
63
|
+
/** 超过多少天没复核算「可考虑退役」(只做机械年龄判断) */
|
|
64
|
+
export const STALE_DAYS = 90;
|
|
65
|
+
/** 退役候选最多回几条:给材料,不是给清理清单 */
|
|
66
|
+
export const MAX_RETIRE = 10;
|
|
50
67
|
/**
|
|
51
68
|
* 从日记正文里取 Loose ends 小节。
|
|
52
69
|
* `- [x]` 是已完成,不收;`- [ ]` 与普通列表项都收(划掉的状态比格式重要)。
|
|
@@ -73,13 +90,14 @@ export function looseEndsOf(content) {
|
|
|
73
90
|
/**
|
|
74
91
|
* 机械拒绝规则(只两条,别加语义判断):
|
|
75
92
|
* · authority=informal —— 口头/未验证来源,进长期记忆会污染结论;
|
|
76
|
-
* · 无标签 ——
|
|
93
|
+
* · 无标签 —— 标签是唯一能确定性筛选的维度;没标签的笔记晋升后只能靠全文检索找回来
|
|
94
|
+
* (P2-7 起中文检索不再漏,但「按主题找」还是得靠标签)。
|
|
77
95
|
*/
|
|
78
96
|
export function rejectionReason(note) {
|
|
79
97
|
if (note.authority === 'informal')
|
|
80
98
|
return 'authority: informal(口头/未验证来源)';
|
|
81
99
|
if (note.tags.length === 0)
|
|
82
|
-
return '
|
|
100
|
+
return '无标签(没有可筛选的标签,晋升后只能靠全文检索找回来)';
|
|
83
101
|
return null;
|
|
84
102
|
}
|
|
85
103
|
function isUnder(rel, dir) {
|
|
@@ -119,9 +137,124 @@ function excerptOf(cfg, note) {
|
|
|
119
137
|
}
|
|
120
138
|
return parts.join(' ').replace(/\s+/g, ' ').trim().slice(0, 200) || note.lead;
|
|
121
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* 窗口默认跟着 DREAMS.md 走:距上次做梦多少天,窗口就是多少天。
|
|
142
|
+
* 固定 7 天的话,漏做一周、或隔十天再整理,窗口外的笔记就再也不会出现在任何一栏 ——
|
|
143
|
+
* 那不是「这次没选上」,是「永远消失」。没有 DREAMS.md / 日期不合法 → 退回 7 天(旧行为)。
|
|
144
|
+
* 显式 `--days` 仍然优先:调用方说要看几天就看几天。
|
|
145
|
+
*/
|
|
146
|
+
function windowDaysOf(cfg, now, explicit) {
|
|
147
|
+
const clamp = (n) => Math.min(365, Math.max(1, Math.trunc(n)));
|
|
148
|
+
if (explicit !== undefined)
|
|
149
|
+
return clamp(explicit);
|
|
150
|
+
let body = '';
|
|
151
|
+
try {
|
|
152
|
+
body = fs.readFileSync(path.join(cfg.notebook, 'DREAMS.md'), 'utf8');
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return DREAM_INTERVAL_DAYS;
|
|
156
|
+
}
|
|
157
|
+
const last = lastDreamDate(body);
|
|
158
|
+
const days = last ? daysSinceDream(last, now) : null;
|
|
159
|
+
return days === null ? DREAM_INTERVAL_DAYS : clamp(days);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 已晋升集合:MEMORY.md 里的 `[[链接]]` + 纯文字条目里出现的标题。
|
|
163
|
+
*
|
|
164
|
+
* 为什么要有第二条:真机 MEMORY.md 7 条里 6 条是纯文字(没有链接),只按链接判会漏掉它们,
|
|
165
|
+
* 而它们正是每周重复出现的候选来源。标题包含只做字符串比较、且要求标题 ≥ 6 字 ——
|
|
166
|
+
* 短标题(「RAG」「额度」)在任意句子里都可能撞上,宁可重复报,不猜。
|
|
167
|
+
*
|
|
168
|
+
* 退役条目要分开处理(D3.3,顺序不能反):
|
|
169
|
+
* · 链接匹配用**原文** —— 退役行里的 `→ [[新结论]]` 是合法的「已晋升」指针;
|
|
170
|
+
* · 标题匹配用**去掉退役行之后**的正文 —— 退役行里的旧结论不该再算已晋升,
|
|
171
|
+
* 否则归档后复用同一标题的笔记会永远进不了候选。
|
|
172
|
+
*/
|
|
173
|
+
function promotionsOf(cfg, notes) {
|
|
174
|
+
const out = new Map();
|
|
175
|
+
let body = '';
|
|
176
|
+
try {
|
|
177
|
+
body = fs.readFileSync(path.join(cfg.notebook, 'MEMORY.md'), 'utf8');
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
if (!body.trim())
|
|
183
|
+
return out;
|
|
184
|
+
const index = wikiIndex(notes);
|
|
185
|
+
for (const match of body.matchAll(WIKI_LINK)) {
|
|
186
|
+
const hit = resolveWikiTarget(cfg, String(match[1] ?? '').trim(), index);
|
|
187
|
+
if (hit.status === 'live' && hit.path)
|
|
188
|
+
out.set(hit.path, 'link');
|
|
189
|
+
}
|
|
190
|
+
const lines = stripRetiredEntries(body).body.split('\n').map((line) => normalizeTitle(line)).filter(Boolean);
|
|
191
|
+
for (const note of notes) {
|
|
192
|
+
if (out.has(note.path))
|
|
193
|
+
continue;
|
|
194
|
+
const key = normalizeTitle(note.title);
|
|
195
|
+
if ([...key].length < MIN_TITLE_MATCH)
|
|
196
|
+
continue;
|
|
197
|
+
if (lines.some((line) => line.includes(key)))
|
|
198
|
+
out.set(note.path, 'title');
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 退役候选(D4.3):只做机械规则,不判断「这条还有没有用」。
|
|
204
|
+
* · 有日期且超过 STALE_DAYS → stale;
|
|
205
|
+
* · 条目里的 `[[来源]]` 指向归档 / 不存在 → source-superseded / source-missing;
|
|
206
|
+
* · 显式写 `archive/` 的链接是有意引用旧结论(与 lint 同口径),不算来源断了;
|
|
207
|
+
* · 无日期、无链接的不算候选(它们已经由 lint 的 undated / unlinked 报告)。
|
|
208
|
+
*/
|
|
209
|
+
function retireCandidatesOf(cfg, notes, now) {
|
|
210
|
+
let body = '';
|
|
211
|
+
try {
|
|
212
|
+
body = fs.readFileSync(path.join(cfg.notebook, 'MEMORY.md'), 'utf8');
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
const index = wikiIndex(notes);
|
|
218
|
+
const out = [];
|
|
219
|
+
const seen = new Set();
|
|
220
|
+
for (const entry of splitMemoryEntries(body)) {
|
|
221
|
+
if (isRetiredEntry(entry))
|
|
222
|
+
continue;
|
|
223
|
+
const date = entryDate(entry) ?? '';
|
|
224
|
+
const age = date ? (daysSinceDate(date, now) ?? -1) : -1;
|
|
225
|
+
let reason = null;
|
|
226
|
+
for (const match of entry.matchAll(WIKI_LINK)) {
|
|
227
|
+
const target = String(match[1] ?? '').trim();
|
|
228
|
+
if (!target)
|
|
229
|
+
continue;
|
|
230
|
+
const hit = resolveWikiTarget(cfg, target, index);
|
|
231
|
+
// 显式写 archive/ 的链接是有意引用(supersede 就是这样重写入链的):与 lint 一个口径,不算来源断了
|
|
232
|
+
if (hit.status === 'live' || hit.explicit)
|
|
233
|
+
continue;
|
|
234
|
+
reason = hit.status === 'archived' ? 'source-superseded' : 'source-missing';
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
if (!reason && age > STALE_DAYS)
|
|
238
|
+
reason = 'stale';
|
|
239
|
+
if (!reason)
|
|
240
|
+
continue;
|
|
241
|
+
const summary = (entry.split('\n', 1)[0] ?? '')
|
|
242
|
+
.replace(/^\s*(?:[-*+]|\d+[.)])\s+/, '')
|
|
243
|
+
.replace(/\s+/g, ' ')
|
|
244
|
+
.trim();
|
|
245
|
+
const one = summary.length > 100 ? summary.slice(0, 100) + '…' : summary;
|
|
246
|
+
if (!one || seen.has(one))
|
|
247
|
+
continue;
|
|
248
|
+
seen.add(one);
|
|
249
|
+
out.push({ entry: one, date, age, reason });
|
|
250
|
+
}
|
|
251
|
+
// 越旧越靠前;无日期(-1)的排在最后(不假装它们更旧)
|
|
252
|
+
out.sort((a, b) => b.age - a.age || a.entry.localeCompare(b.entry));
|
|
253
|
+
return out.slice(0, MAX_RETIRE);
|
|
254
|
+
}
|
|
122
255
|
export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
123
|
-
const windowDays = Math.min(365, Math.max(1, Math.trunc(opts.days ?? 7)));
|
|
124
256
|
const now = opts.now ?? new Date();
|
|
257
|
+
const windowDays = windowDaysOf(cfg, now, opts.days);
|
|
125
258
|
const sinceMs = now.getTime() - windowDays * 86_400_000;
|
|
126
259
|
// 顺序跑:两个 zk 进程同时补索引会撞 SQLite 写锁(lint 里同样处理)
|
|
127
260
|
const notes = await allNotes(cfg, {}, signal);
|
|
@@ -137,6 +270,48 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
137
270
|
inbound.set(edge.targetPath, sources);
|
|
138
271
|
}
|
|
139
272
|
const citedOf = (n) => inbound.get(n.path)?.length ?? 0;
|
|
273
|
+
// 使用信号(P2-12):日记是本插件里唯一「按时间记录真实工作」的层,正好当召回日志用。
|
|
274
|
+
// 零新增状态:来源路径与日期都在已载入的 notes / edges 里。
|
|
275
|
+
const byPath = new Map(notes.map((n) => [n.path, n]));
|
|
276
|
+
const usageCache = new Map();
|
|
277
|
+
const usageOf = (rel) => {
|
|
278
|
+
const cached = usageCache.get(rel);
|
|
279
|
+
if (cached)
|
|
280
|
+
return cached;
|
|
281
|
+
const sources = (inbound.get(rel) ?? []).filter((p) => isUnder(p, cfg.dailyDir));
|
|
282
|
+
const dates = new Set();
|
|
283
|
+
for (const source of sources) {
|
|
284
|
+
// 日记的文件名就是日期(zk 的 created 是文件创建时间,补写旧日记时会全是今天);
|
|
285
|
+
// 文件名不是日期形状(自定义模板)才退回 created。
|
|
286
|
+
const stem = path.basename(source, path.extname(source));
|
|
287
|
+
const day = /^\d{4}-\d{2}-\d{2}$/.test(stem) ? stem : localDate(byPath.get(source)?.created ?? '');
|
|
288
|
+
if (day)
|
|
289
|
+
dates.add(day);
|
|
290
|
+
}
|
|
291
|
+
const sorted = [...dates].sort();
|
|
292
|
+
const value = {
|
|
293
|
+
dailyMentions: sources.length,
|
|
294
|
+
daysMentioned: dates.size,
|
|
295
|
+
lastMentioned: sorted.length ? sorted[sorted.length - 1] : '',
|
|
296
|
+
};
|
|
297
|
+
usageCache.set(rel, value);
|
|
298
|
+
return value;
|
|
299
|
+
};
|
|
300
|
+
const candidateOf = (note, promotedBy) => ({
|
|
301
|
+
title: note.title,
|
|
302
|
+
path: note.path,
|
|
303
|
+
type: note.type,
|
|
304
|
+
authority: note.authority,
|
|
305
|
+
tags: note.tags,
|
|
306
|
+
created: localDate(note.created),
|
|
307
|
+
lead: excerptOf(cfg, note),
|
|
308
|
+
cited: citedOf(note),
|
|
309
|
+
citedBy: (inbound.get(note.path) ?? []).slice(0, 5),
|
|
310
|
+
promoted: promotedBy !== '',
|
|
311
|
+
promotedBy,
|
|
312
|
+
...usageOf(note.path),
|
|
313
|
+
});
|
|
314
|
+
const promoted = promotionsOf(cfg, notes);
|
|
140
315
|
const windowNotes = notes
|
|
141
316
|
.filter((n) => {
|
|
142
317
|
const at = Date.parse(n.created);
|
|
@@ -144,28 +319,38 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
144
319
|
})
|
|
145
320
|
.sort((a, b) => Date.parse(b.created) - Date.parse(a.created));
|
|
146
321
|
const newNotes = [];
|
|
322
|
+
const alreadyPromoted = [];
|
|
147
323
|
const rejected = [];
|
|
148
324
|
for (const note of windowNotes) {
|
|
149
325
|
// 日记不是晋升候选:它的「未完成项」走 dailyLooseEnds,报成「无标签」只会制造噪音
|
|
150
326
|
if (isUnder(note.path, cfg.dailyDir))
|
|
151
327
|
continue;
|
|
328
|
+
// 已经进 MEMORY 的先说:否则每周原样重复,仪式几次之后就没人看了
|
|
329
|
+
const by = promoted.get(note.path);
|
|
330
|
+
if (by) {
|
|
331
|
+
alreadyPromoted.push(candidateOf(note, by));
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
152
334
|
const reason = rejectionReason(note);
|
|
153
335
|
if (reason) {
|
|
154
336
|
rejected.push({ title: note.title, path: note.path, reason });
|
|
155
337
|
continue;
|
|
156
338
|
}
|
|
157
|
-
newNotes.push(
|
|
158
|
-
title: note.title,
|
|
159
|
-
path: note.path,
|
|
160
|
-
type: note.type,
|
|
161
|
-
authority: note.authority,
|
|
162
|
-
tags: note.tags,
|
|
163
|
-
created: localDate(note.created),
|
|
164
|
-
lead: excerptOf(cfg, note),
|
|
165
|
-
cited: citedOf(note),
|
|
166
|
-
citedBy: (inbound.get(note.path) ?? []).slice(0, 5),
|
|
167
|
-
});
|
|
339
|
+
newNotes.push(candidateOf(note, ''));
|
|
168
340
|
}
|
|
341
|
+
// 窗口外仍未晋升、且过机械门的:按被引数排,别让「漏做一次」变成永久掉队
|
|
342
|
+
const backlogNotes = notes
|
|
343
|
+
.filter((n) => {
|
|
344
|
+
if (isUnder(n.path, cfg.dailyDir))
|
|
345
|
+
return false;
|
|
346
|
+
const at = Date.parse(n.created);
|
|
347
|
+
if (Number.isFinite(at) && at >= sinceMs)
|
|
348
|
+
return false;
|
|
349
|
+
if (promoted.has(n.path))
|
|
350
|
+
return false;
|
|
351
|
+
return rejectionReason(n) === null;
|
|
352
|
+
})
|
|
353
|
+
.sort((a, b) => citedOf(b) - citedOf(a) || a.title.localeCompare(b.title));
|
|
169
354
|
const dailyLooseEnds = [];
|
|
170
355
|
let looseEndCount = 0;
|
|
171
356
|
for (const note of windowNotes) {
|
|
@@ -186,10 +371,16 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
186
371
|
dailyLooseEnds.push({ path: note.path, date: localDate(note.created), items });
|
|
187
372
|
}
|
|
188
373
|
}
|
|
374
|
+
// 排序说明(P2-12):先看「被日记提过几天」—— 跨天复现比同一天多次更强;同一天多篇日记
|
|
375
|
+
// 再按 dailyMentions 排,入链数只是最后的次序。没有日记的库两项都是 0,
|
|
376
|
+
// 排序自动退回原来的 cited 降序(行为兼容)。
|
|
189
377
|
const topCited = notes
|
|
190
|
-
.map((n) => ({ title: n.title, path: n.path, cited: citedOf(n) }))
|
|
378
|
+
.map((n) => ({ title: n.title, path: n.path, cited: citedOf(n), ...usageOf(n.path) }))
|
|
191
379
|
.filter((x) => x.cited > 0)
|
|
192
|
-
.sort((a, b) => b.
|
|
380
|
+
.sort((a, b) => b.daysMentioned - a.daysMentioned ||
|
|
381
|
+
b.dailyMentions - a.dailyMentions ||
|
|
382
|
+
b.cited - a.cited ||
|
|
383
|
+
a.title.localeCompare(b.title))
|
|
193
384
|
.slice(0, TOP_CITED);
|
|
194
385
|
return {
|
|
195
386
|
notebook: cfg.notebook,
|
|
@@ -202,10 +393,16 @@ export async function dreamNotebook(cfg, opts = {}, signal) {
|
|
|
202
393
|
candidates: newNotes.length,
|
|
203
394
|
rejected: rejected.length,
|
|
204
395
|
looseEnds: looseEndCount,
|
|
396
|
+
promoted: alreadyPromoted.length,
|
|
397
|
+
backlog: backlogNotes.length,
|
|
205
398
|
},
|
|
206
399
|
newNotes: newNotes.slice(0, MAX_ITEMS),
|
|
400
|
+
alreadyPromoted: alreadyPromoted.slice(0, MAX_ITEMS),
|
|
401
|
+
backlog: backlogNotes.slice(0, MAX_BACKLOG).map((n) => candidateOf(n, '')),
|
|
207
402
|
rejected: rejected.slice(0, MAX_ITEMS),
|
|
208
403
|
topCited,
|
|
209
404
|
dailyLooseEnds,
|
|
405
|
+
memory: memoryUsage(cfg),
|
|
406
|
+
retireCandidates: retireCandidatesOf(cfg, notes, now),
|
|
210
407
|
};
|
|
211
408
|
}
|
package/dist/lib/http.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本插件所有服务端路由的前缀(设置页与只读视图共用同一个真相)。
|
|
3
|
+
* client.ts 因「单文件零 import」的宿主约束另存了一份字面量 —— 改这里记得同步那边。
|
|
4
|
+
*/
|
|
5
|
+
export const ROUTE_BASE = '/dsh-zdiary-kit';
|
|
1
6
|
export function json(res, status, body) {
|
|
2
7
|
const payload = JSON.stringify(body);
|
|
3
8
|
res.statusCode = status;
|
|
@@ -18,3 +23,24 @@ export async function readBody(req, limit = 1024 * 1024) {
|
|
|
18
23
|
}
|
|
19
24
|
return Buffer.concat(chunks).toString('utf8');
|
|
20
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Host 头是不是回环。只证明「地址栏里写的是 127.0.0.1」——
|
|
28
|
+
* 单独用它会被 DNS rebinding 骗(恶意域名解析到 127.0.0.1,Host 还是域名,这里挡得住,
|
|
29
|
+
* 但反过来把 Host 伪造成 127.0.0.1 的请求挡不住)。要收紧就跟 isLoopbackPeer 一起判。
|
|
30
|
+
*/
|
|
31
|
+
export function isLoopbackHost(req) {
|
|
32
|
+
const host = String(req.headers?.host ?? '').toLowerCase();
|
|
33
|
+
const h = host.startsWith('[') ? host.slice(0, host.indexOf(']') + 1) : host.split(':')[0];
|
|
34
|
+
return h === '127.0.0.1' || h === 'localhost' || h === '[::1]' || h === '::1';
|
|
35
|
+
}
|
|
36
|
+
/** 对端 socket 地址是不是回环;没有对端地址(unix socket / 测试替身)不算反证 */
|
|
37
|
+
export function isLoopbackPeer(req) {
|
|
38
|
+
const addr = req.socket?.remoteAddress;
|
|
39
|
+
if (!addr)
|
|
40
|
+
return true;
|
|
41
|
+
return addr === '127.0.0.1' || addr === '::1' || addr === '::ffff:127.0.0.1';
|
|
42
|
+
}
|
|
43
|
+
/** 两边都在回环才算「本机打开」—— 只读视图用它,比只看 Host 严格 */
|
|
44
|
+
export function isLoopback(req) {
|
|
45
|
+
return isLoopbackHost(req) && isLoopbackPeer(req);
|
|
46
|
+
}
|