dsh-session-guard 4.4.1 → 4.4.3
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 +9 -0
- package/CHANGELOG.pt.md +196 -196
- package/INSTALL.pt.md +120 -120
- package/README.pt.md +377 -377
- package/dsh.plugin.json +1 -1
- package/lib/client.js +399 -182
- package/lib/client.js.map +1 -1
- package/locale/de.json +6 -6
- package/locale/en.json +6 -6
- package/locale/es.json +6 -6
- package/locale/fr.json +6 -6
- package/locale/it.json +6 -6
- package/locale/ja.json +6 -6
- package/locale/ko.json +6 -6
- package/locale/pt.json +6 -6
- package/locale/ru.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +129 -129
- package/src/bridge.js +190 -190
- package/src/client/badge-text.ts +49 -49
- package/src/client/family-section.tsx +79 -79
- package/src/client/guide.tsx +2 -1
- package/src/client/locales.ts +400 -400
- package/src/client/pause-button-text.ts +76 -76
- package/src/client/pause-button.tsx +129 -129
- package/src/client/scope-face.ts +20 -20
- package/src/client/settings-bridge.ts +202 -202
- package/src/client/settings-card.tsx +4 -0
- package/src/client/star-row-copy.ts +256 -0
- package/src/client/status-badge.tsx +76 -76
- package/src/client/styles.ts +30 -30
- package/src/deferrals.js +197 -197
- package/src/holiday-store.js +196 -196
- package/src/holidays.js +215 -215
- package/src/pause-gate.js +544 -544
- package/src/provider-directory.js +101 -101
- package/src/provider.js +139 -139
- package/src/retry.js +227 -227
- package/src/settings-bridge.js +141 -141
- package/src/settings.js +98 -98
- package/src/step-gate.js +411 -411
- package/src/targets.js +70 -70
- package/src/time.js +216 -216
- package/src/tool-call-id.js +48 -48
- package/src/wiring.js +308 -308
package/src/holiday-store.js
CHANGED
|
@@ -1,196 +1,196 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-session-guard — 修正表的 IO 层:按年缓存落盘 + 自动抓取 + 启动指纹校验。
|
|
3
|
-
*
|
|
4
|
-
* 数据链路(全部 fail-open,任何失败只降级不抛错):
|
|
5
|
-
* 1. 内置快照(holidays.js BUILTIN_TABLES,已定稿年份);
|
|
6
|
-
* 2. 磁盘缓存 `$DSH_HOME/.dsh/session-guard/holidays/<year>.json`
|
|
7
|
-
* (`DSH_SESSION_GUARD_HOLIDAY_DIR` 可覆盖根目录)——**启动时校验指纹**:
|
|
8
|
-
* 重算内容 sha256 与文件内记录比对,不一致视为损坏,弃用并触发重抓;
|
|
9
|
-
* 3. 远端 holiday-cn(jsDelivr 主源、raw.githubusercontent 备源)——
|
|
10
|
-
* 抓取成功且指纹有变化才原子写缓存(tmp + rename);指纹一致只刷新
|
|
11
|
-
* checkedAt,不写盘(2026 已定稿,日常核对几乎恒等)。
|
|
12
|
-
*
|
|
13
|
-
* 「按年自动尝试抓取」:启动核对一次,之后每小时 ensureFresh;相关年份 =
|
|
14
|
-
* 北京时间当年 + 12 月追加次年(国务院公告一般 11–12 月发布)。内置表
|
|
15
|
-
* 覆盖的年份仍会被远端覆盖更新(公告修正 / 补录),但抓取节奏不变。
|
|
16
|
-
*/
|
|
17
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
18
|
-
import { dirname, join } from 'node:path'
|
|
19
|
-
import { storeRoot } from './store.js'
|
|
20
|
-
import { wallClock, BILLING_TIMEZONE } from './time.js'
|
|
21
|
-
import { BUILTIN_TABLES, buildMap, fingerprint, parseHolidayCn } from './holidays.js'
|
|
22
|
-
|
|
23
|
-
/** 单轮远端抓取超时(ms)。 */
|
|
24
|
-
const FETCH_TIMEOUT_MS = 10_000
|
|
25
|
-
/** 已到手年份表的复核间隔:24h。 */
|
|
26
|
-
const REFRESH_MS = 24 * 3600 * 1000
|
|
27
|
-
|
|
28
|
-
/** 缓存根目录(独立于 pause / 队列锁子目录)。 */
|
|
29
|
-
export function holidaysRoot() {
|
|
30
|
-
return process.env.DSH_SESSION_GUARD_HOLIDAY_DIR || join(storeRoot(), 'holidays')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** 某年缓存文件路径。 */
|
|
34
|
-
export function holidayFilePath(year, root = holidaysRoot()) {
|
|
35
|
-
return join(root, `${Number(year)}.json`)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** 远端候选源:jsDelivr 主源(国内可达性好),GitHub raw 备源。 */
|
|
39
|
-
export function holidayUrls(year) {
|
|
40
|
-
const y = Number(year)
|
|
41
|
-
return [
|
|
42
|
-
`https://cdn.jsdelivr.net/gh/NateScarlet/holiday-cn@master/${y}.json`,
|
|
43
|
-
`https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/${y}.json`,
|
|
44
|
-
]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 创建修正表存储。
|
|
49
|
-
* @param {object} [deps] { logger, fetchImpl, now, builtinTables, root }
|
|
50
|
-
* root 供单测注入临时目录;fetchImpl 供 mock。
|
|
51
|
-
* @returns {{
|
|
52
|
-
* loadAll: () => void,
|
|
53
|
-
* ensureFresh: (opts?: { autoFetch?: boolean }) => Promise<void>,
|
|
54
|
-
* map: () => Map<string, boolean>,
|
|
55
|
-
* info: () => object[],
|
|
56
|
-
* }}
|
|
57
|
-
*/
|
|
58
|
-
export function createHolidayStore(deps = {}) {
|
|
59
|
-
const logger = deps.logger
|
|
60
|
-
const fetchImpl = deps.fetchImpl ?? globalThis.fetch
|
|
61
|
-
const now = deps.now ?? (() => Date.now())
|
|
62
|
-
const builtin = deps.builtinTables ?? BUILTIN_TABLES
|
|
63
|
-
const root = deps.root ?? holidaysRoot()
|
|
64
|
-
|
|
65
|
-
/** @type {Map<number, {days: {date:string,isOffDay:boolean}[], sha:string, source:'builtin'|'cache'|'live', fetchedAt:number|null, checkedAt:number|null}>} */
|
|
66
|
-
const tables = new Map()
|
|
67
|
-
/** map() 的惰性合并缓存:任何表更新时置 null。 */
|
|
68
|
-
let merged = null
|
|
69
|
-
|
|
70
|
-
const warn = (msg, e) => {
|
|
71
|
-
const line = `[session-guard] ${msg}`
|
|
72
|
-
if (logger?.warn) logger.warn(e ? `${line}: ${String(e && e.message || e)}` : line)
|
|
73
|
-
else console.warn(e ? `${line}: ${String(e && e.message || e)}` : line)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function putTable(year, days, source, fetchedAt) {
|
|
77
|
-
tables.set(year, { days, sha: fingerprint(days), source, fetchedAt, checkedAt: now() })
|
|
78
|
-
merged = null
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** 装载单年:内置打底,缓存指纹校验通过则覆盖内置。 */
|
|
82
|
-
function loadYear(year) {
|
|
83
|
-
const seed = builtin.find((t) => t.year === year)
|
|
84
|
-
if (seed) putTable(year, seed.days.slice(), 'builtin', null)
|
|
85
|
-
let cached
|
|
86
|
-
try {
|
|
87
|
-
const f = holidayFilePath(year, root)
|
|
88
|
-
if (!existsSync(f)) return
|
|
89
|
-
cached = JSON.parse(readFileSync(f, 'utf8'))
|
|
90
|
-
} catch (e) {
|
|
91
|
-
warn('holiday cache unreadable (ignored)', e)
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
const parsed = parseHolidayCn(cached)
|
|
96
|
-
if (parsed.year !== year) throw new Error(`cache year ${parsed.year} != file slot ${year}`)
|
|
97
|
-
const sha = fingerprint(parsed.days)
|
|
98
|
-
if (cached.sha !== sha) {
|
|
99
|
-
// 启动指纹校验失败:内容损坏/被改,弃用缓存(ensureFresh 会重抓)。
|
|
100
|
-
warn(`holiday cache fingerprint mismatch for ${year}, discarding`)
|
|
101
|
-
return
|
|
102
|
-
}
|
|
103
|
-
putTable(year, parsed.days, 'cache', Number(cached.fetchedAt) || null)
|
|
104
|
-
} catch (e) {
|
|
105
|
-
warn(`holiday cache invalid for ${year} (ignored)`, e)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/** 相关年份:北京当年 +(11/12 月)次年——国务院一般年底公布来年安排。 */
|
|
110
|
-
function relevantYears() {
|
|
111
|
-
const wc = wallClock(BILLING_TIMEZONE, new Date(now()))
|
|
112
|
-
const years = [wc.year]
|
|
113
|
-
if (wc.month >= 11) years.push(wc.year + 1)
|
|
114
|
-
return years
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async function fetchYear(year) {
|
|
118
|
-
if (typeof fetchImpl !== 'function') return false
|
|
119
|
-
for (const url of holidayUrls(year)) {
|
|
120
|
-
try {
|
|
121
|
-
const res = await fetchImpl(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) })
|
|
122
|
-
if (!res || res.ok !== true) continue
|
|
123
|
-
const parsed = parseHolidayCn(await res.json())
|
|
124
|
-
if (parsed.year !== year) throw new Error(`payload year ${parsed.year} != requested ${year}`)
|
|
125
|
-
const sha = fingerprint(parsed.days)
|
|
126
|
-
const cur = tables.get(year)
|
|
127
|
-
if (cur && cur.sha === sha) {
|
|
128
|
-
cur.checkedAt = now() // 指纹一致:不写盘,只记核对时间
|
|
129
|
-
return true
|
|
130
|
-
}
|
|
131
|
-
try {
|
|
132
|
-
mkdirSync(dirname(holidayFilePath(year, root)), { recursive: true })
|
|
133
|
-
const tmp = `${holidayFilePath(year, root)}.tmp`
|
|
134
|
-
writeFileSync(tmp, JSON.stringify({
|
|
135
|
-
year, sha, fetchedAt: now(), sourceUrl: url,
|
|
136
|
-
days: parsed.days,
|
|
137
|
-
}, null, 2))
|
|
138
|
-
renameSync(tmp, holidayFilePath(year, root))
|
|
139
|
-
} catch (e) {
|
|
140
|
-
warn(`holiday cache write failed for ${year} (memory table still updated)`, e)
|
|
141
|
-
}
|
|
142
|
-
putTable(year, parsed.days, 'live', now())
|
|
143
|
-
warn(`holiday table updated for ${year} (${cur ? 'fingerprint changed' : 'new'}, ${sha.slice(0, 17)}…)`)
|
|
144
|
-
return true
|
|
145
|
-
} catch (e) {
|
|
146
|
-
warn(`holiday fetch failed via ${url}`, e)
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return false
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return {
|
|
153
|
-
/** 启动装载(同步):内置 + 缓存 + 指纹校验。 */
|
|
154
|
-
loadAll() {
|
|
155
|
-
for (const year of relevantYears()) loadYear(year)
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* 按年自动抓取核对:缺表的年份立即抓;已有表超 24h 复核。
|
|
160
|
-
* autoFetch=false(设置关)时只补缺年(缓存/内置仍生效),不发请求。
|
|
161
|
-
*/
|
|
162
|
-
async ensureFresh(opts = {}) {
|
|
163
|
-
const autoFetch = opts.autoFetch !== false
|
|
164
|
-
for (const year of relevantYears()) {
|
|
165
|
-
const cur = tables.get(year)
|
|
166
|
-
const anchor = Math.max((cur && cur.fetchedAt) || 0, (cur && cur.checkedAt) || 0)
|
|
167
|
-
if (cur && now() - anchor <= REFRESH_MS) continue // 24h 内已抓/已核对
|
|
168
|
-
if (autoFetch !== true) continue // 关自动抓取时纯靠内置 + 缓存,不发请求
|
|
169
|
-
await fetchYear(year)
|
|
170
|
-
}
|
|
171
|
-
},
|
|
172
|
-
|
|
173
|
-
/** 合并所有已装载年份的查表 Map('YYYY-MM-DD' → 是否放假日)。 */
|
|
174
|
-
map() {
|
|
175
|
-
if (merged) return merged
|
|
176
|
-
const days = []
|
|
177
|
-
for (const t of tables.values()) days.push(...t.days)
|
|
178
|
-
merged = buildMap(days)
|
|
179
|
-
return merged
|
|
180
|
-
},
|
|
181
|
-
|
|
182
|
-
/** 诊断输出(/session-guard/status 透出用)。 */
|
|
183
|
-
info() {
|
|
184
|
-
return [...tables.entries()]
|
|
185
|
-
.sort((a, b) => a[0] - b[0])
|
|
186
|
-
.map(([year, t]) => ({
|
|
187
|
-
year,
|
|
188
|
-
source: t.source,
|
|
189
|
-
fingerprint: t.sha,
|
|
190
|
-
fetchedAt: t.fetchedAt,
|
|
191
|
-
checkedAt: t.checkedAt,
|
|
192
|
-
entries: t.days.length,
|
|
193
|
-
}))
|
|
194
|
-
},
|
|
195
|
-
}
|
|
196
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-session-guard — 修正表的 IO 层:按年缓存落盘 + 自动抓取 + 启动指纹校验。
|
|
3
|
+
*
|
|
4
|
+
* 数据链路(全部 fail-open,任何失败只降级不抛错):
|
|
5
|
+
* 1. 内置快照(holidays.js BUILTIN_TABLES,已定稿年份);
|
|
6
|
+
* 2. 磁盘缓存 `$DSH_HOME/.dsh/session-guard/holidays/<year>.json`
|
|
7
|
+
* (`DSH_SESSION_GUARD_HOLIDAY_DIR` 可覆盖根目录)——**启动时校验指纹**:
|
|
8
|
+
* 重算内容 sha256 与文件内记录比对,不一致视为损坏,弃用并触发重抓;
|
|
9
|
+
* 3. 远端 holiday-cn(jsDelivr 主源、raw.githubusercontent 备源)——
|
|
10
|
+
* 抓取成功且指纹有变化才原子写缓存(tmp + rename);指纹一致只刷新
|
|
11
|
+
* checkedAt,不写盘(2026 已定稿,日常核对几乎恒等)。
|
|
12
|
+
*
|
|
13
|
+
* 「按年自动尝试抓取」:启动核对一次,之后每小时 ensureFresh;相关年份 =
|
|
14
|
+
* 北京时间当年 + 12 月追加次年(国务院公告一般 11–12 月发布)。内置表
|
|
15
|
+
* 覆盖的年份仍会被远端覆盖更新(公告修正 / 补录),但抓取节奏不变。
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
18
|
+
import { dirname, join } from 'node:path'
|
|
19
|
+
import { storeRoot } from './store.js'
|
|
20
|
+
import { wallClock, BILLING_TIMEZONE } from './time.js'
|
|
21
|
+
import { BUILTIN_TABLES, buildMap, fingerprint, parseHolidayCn } from './holidays.js'
|
|
22
|
+
|
|
23
|
+
/** 单轮远端抓取超时(ms)。 */
|
|
24
|
+
const FETCH_TIMEOUT_MS = 10_000
|
|
25
|
+
/** 已到手年份表的复核间隔:24h。 */
|
|
26
|
+
const REFRESH_MS = 24 * 3600 * 1000
|
|
27
|
+
|
|
28
|
+
/** 缓存根目录(独立于 pause / 队列锁子目录)。 */
|
|
29
|
+
export function holidaysRoot() {
|
|
30
|
+
return process.env.DSH_SESSION_GUARD_HOLIDAY_DIR || join(storeRoot(), 'holidays')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 某年缓存文件路径。 */
|
|
34
|
+
export function holidayFilePath(year, root = holidaysRoot()) {
|
|
35
|
+
return join(root, `${Number(year)}.json`)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 远端候选源:jsDelivr 主源(国内可达性好),GitHub raw 备源。 */
|
|
39
|
+
export function holidayUrls(year) {
|
|
40
|
+
const y = Number(year)
|
|
41
|
+
return [
|
|
42
|
+
`https://cdn.jsdelivr.net/gh/NateScarlet/holiday-cn@master/${y}.json`,
|
|
43
|
+
`https://raw.githubusercontent.com/NateScarlet/holiday-cn/master/${y}.json`,
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 创建修正表存储。
|
|
49
|
+
* @param {object} [deps] { logger, fetchImpl, now, builtinTables, root }
|
|
50
|
+
* root 供单测注入临时目录;fetchImpl 供 mock。
|
|
51
|
+
* @returns {{
|
|
52
|
+
* loadAll: () => void,
|
|
53
|
+
* ensureFresh: (opts?: { autoFetch?: boolean }) => Promise<void>,
|
|
54
|
+
* map: () => Map<string, boolean>,
|
|
55
|
+
* info: () => object[],
|
|
56
|
+
* }}
|
|
57
|
+
*/
|
|
58
|
+
export function createHolidayStore(deps = {}) {
|
|
59
|
+
const logger = deps.logger
|
|
60
|
+
const fetchImpl = deps.fetchImpl ?? globalThis.fetch
|
|
61
|
+
const now = deps.now ?? (() => Date.now())
|
|
62
|
+
const builtin = deps.builtinTables ?? BUILTIN_TABLES
|
|
63
|
+
const root = deps.root ?? holidaysRoot()
|
|
64
|
+
|
|
65
|
+
/** @type {Map<number, {days: {date:string,isOffDay:boolean}[], sha:string, source:'builtin'|'cache'|'live', fetchedAt:number|null, checkedAt:number|null}>} */
|
|
66
|
+
const tables = new Map()
|
|
67
|
+
/** map() 的惰性合并缓存:任何表更新时置 null。 */
|
|
68
|
+
let merged = null
|
|
69
|
+
|
|
70
|
+
const warn = (msg, e) => {
|
|
71
|
+
const line = `[session-guard] ${msg}`
|
|
72
|
+
if (logger?.warn) logger.warn(e ? `${line}: ${String(e && e.message || e)}` : line)
|
|
73
|
+
else console.warn(e ? `${line}: ${String(e && e.message || e)}` : line)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function putTable(year, days, source, fetchedAt) {
|
|
77
|
+
tables.set(year, { days, sha: fingerprint(days), source, fetchedAt, checkedAt: now() })
|
|
78
|
+
merged = null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 装载单年:内置打底,缓存指纹校验通过则覆盖内置。 */
|
|
82
|
+
function loadYear(year) {
|
|
83
|
+
const seed = builtin.find((t) => t.year === year)
|
|
84
|
+
if (seed) putTable(year, seed.days.slice(), 'builtin', null)
|
|
85
|
+
let cached
|
|
86
|
+
try {
|
|
87
|
+
const f = holidayFilePath(year, root)
|
|
88
|
+
if (!existsSync(f)) return
|
|
89
|
+
cached = JSON.parse(readFileSync(f, 'utf8'))
|
|
90
|
+
} catch (e) {
|
|
91
|
+
warn('holiday cache unreadable (ignored)', e)
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const parsed = parseHolidayCn(cached)
|
|
96
|
+
if (parsed.year !== year) throw new Error(`cache year ${parsed.year} != file slot ${year}`)
|
|
97
|
+
const sha = fingerprint(parsed.days)
|
|
98
|
+
if (cached.sha !== sha) {
|
|
99
|
+
// 启动指纹校验失败:内容损坏/被改,弃用缓存(ensureFresh 会重抓)。
|
|
100
|
+
warn(`holiday cache fingerprint mismatch for ${year}, discarding`)
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
putTable(year, parsed.days, 'cache', Number(cached.fetchedAt) || null)
|
|
104
|
+
} catch (e) {
|
|
105
|
+
warn(`holiday cache invalid for ${year} (ignored)`, e)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 相关年份:北京当年 +(11/12 月)次年——国务院一般年底公布来年安排。 */
|
|
110
|
+
function relevantYears() {
|
|
111
|
+
const wc = wallClock(BILLING_TIMEZONE, new Date(now()))
|
|
112
|
+
const years = [wc.year]
|
|
113
|
+
if (wc.month >= 11) years.push(wc.year + 1)
|
|
114
|
+
return years
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function fetchYear(year) {
|
|
118
|
+
if (typeof fetchImpl !== 'function') return false
|
|
119
|
+
for (const url of holidayUrls(year)) {
|
|
120
|
+
try {
|
|
121
|
+
const res = await fetchImpl(url, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) })
|
|
122
|
+
if (!res || res.ok !== true) continue
|
|
123
|
+
const parsed = parseHolidayCn(await res.json())
|
|
124
|
+
if (parsed.year !== year) throw new Error(`payload year ${parsed.year} != requested ${year}`)
|
|
125
|
+
const sha = fingerprint(parsed.days)
|
|
126
|
+
const cur = tables.get(year)
|
|
127
|
+
if (cur && cur.sha === sha) {
|
|
128
|
+
cur.checkedAt = now() // 指纹一致:不写盘,只记核对时间
|
|
129
|
+
return true
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
mkdirSync(dirname(holidayFilePath(year, root)), { recursive: true })
|
|
133
|
+
const tmp = `${holidayFilePath(year, root)}.tmp`
|
|
134
|
+
writeFileSync(tmp, JSON.stringify({
|
|
135
|
+
year, sha, fetchedAt: now(), sourceUrl: url,
|
|
136
|
+
days: parsed.days,
|
|
137
|
+
}, null, 2))
|
|
138
|
+
renameSync(tmp, holidayFilePath(year, root))
|
|
139
|
+
} catch (e) {
|
|
140
|
+
warn(`holiday cache write failed for ${year} (memory table still updated)`, e)
|
|
141
|
+
}
|
|
142
|
+
putTable(year, parsed.days, 'live', now())
|
|
143
|
+
warn(`holiday table updated for ${year} (${cur ? 'fingerprint changed' : 'new'}, ${sha.slice(0, 17)}…)`)
|
|
144
|
+
return true
|
|
145
|
+
} catch (e) {
|
|
146
|
+
warn(`holiday fetch failed via ${url}`, e)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return false
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
/** 启动装载(同步):内置 + 缓存 + 指纹校验。 */
|
|
154
|
+
loadAll() {
|
|
155
|
+
for (const year of relevantYears()) loadYear(year)
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 按年自动抓取核对:缺表的年份立即抓;已有表超 24h 复核。
|
|
160
|
+
* autoFetch=false(设置关)时只补缺年(缓存/内置仍生效),不发请求。
|
|
161
|
+
*/
|
|
162
|
+
async ensureFresh(opts = {}) {
|
|
163
|
+
const autoFetch = opts.autoFetch !== false
|
|
164
|
+
for (const year of relevantYears()) {
|
|
165
|
+
const cur = tables.get(year)
|
|
166
|
+
const anchor = Math.max((cur && cur.fetchedAt) || 0, (cur && cur.checkedAt) || 0)
|
|
167
|
+
if (cur && now() - anchor <= REFRESH_MS) continue // 24h 内已抓/已核对
|
|
168
|
+
if (autoFetch !== true) continue // 关自动抓取时纯靠内置 + 缓存,不发请求
|
|
169
|
+
await fetchYear(year)
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
/** 合并所有已装载年份的查表 Map('YYYY-MM-DD' → 是否放假日)。 */
|
|
174
|
+
map() {
|
|
175
|
+
if (merged) return merged
|
|
176
|
+
const days = []
|
|
177
|
+
for (const t of tables.values()) days.push(...t.days)
|
|
178
|
+
merged = buildMap(days)
|
|
179
|
+
return merged
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
/** 诊断输出(/session-guard/status 透出用)。 */
|
|
183
|
+
info() {
|
|
184
|
+
return [...tables.entries()]
|
|
185
|
+
.sort((a, b) => a[0] - b[0])
|
|
186
|
+
.map(([year, t]) => ({
|
|
187
|
+
year,
|
|
188
|
+
source: t.source,
|
|
189
|
+
fingerprint: t.sha,
|
|
190
|
+
fetchedAt: t.fetchedAt,
|
|
191
|
+
checkedAt: t.checkedAt,
|
|
192
|
+
entries: t.days.length,
|
|
193
|
+
}))
|
|
194
|
+
},
|
|
195
|
+
}
|
|
196
|
+
}
|