diff --git a/src/web/blog-refresh.ts b/src/web/blog-refresh.ts index 8d47ad7661..a1c33b70ff 100644 --- a/src/web/blog-refresh.ts +++ b/src/web/blog-refresh.ts @@ -129,7 +129,13 @@ export function kickBlogRefresh(opts?: { force?: boolean }): void { const due = (opts?.force ?? false) || Date.now() - lastRoundStartedAt >= ROUND_TTL_MS; if (!due) return; lastRoundStartedAt = Date.now(); - inflight = runRound().finally(() => { inflight = undefined; }); + const base = runRound(); + // gate is the rejection-immune twin of base (the same guard + // runBlogRefreshRound carries): kickBlogRefresh is fire-and-forget, so + // an unexpected runRound reject must never surface as an + // unhandledRejection (a process crash on Node ≥ 15). + const gate = settleRoundImmune(base, () => { if (inflight === gate) inflight = undefined; }); + inflight = gate; // assigned synchronously — the settle callback can only run later } /** One full GitHub pull round, awaitable (tests); production callers use @@ -146,8 +152,7 @@ export async function runBlogRefreshRound(opts?: { force?: boolean }): Promise<{ const base = runRound(); // gate is the rejection-immune twin of base: it only tracks settle- // ment so an unexpected reject can never leak as an unhandled one. - const gate = base.then(() => undefined, () => undefined) - .finally(() => { if (inflight === gate) inflight = undefined; }); + const gate = settleRoundImmune(base, () => { if (inflight === gate) inflight = undefined; }); inflight = gate; // assigned synchronously — the settle callback can only run later try { await base; @@ -278,6 +283,10 @@ async function getText(url: string, account?: (bytes: number) => void): Promise< const result = await fetchWithTimeout(url, ghHeaders(), FETCH_TIMEOUT_MS); try { if (!result.response.ok) throw new Error(`raw GET ${url} → HTTP ${result.response.status}`); + const declared = contentLengthOf(result.response.headers); + if (declared !== undefined && declared > MAX_TEXT_BYTES) { + throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes (content-length ${declared}): ${url}`); + } const buf = Buffer.from(await result.response.arrayBuffer()); if (buf.byteLength > MAX_TEXT_BYTES) throw new Error(`text document exceeds ${MAX_TEXT_BYTES} bytes: ${url}`); account?.(buf.byteLength); @@ -300,6 +309,8 @@ async function getImage(url: string, account?: (bytes: number) => void): Promise try { if (result.response.status === 404) return undefined; if (!result.response.ok) throw new Error(`image GET ${url} → HTTP ${result.response.status}`); + const declared = contentLengthOf(result.response.headers); + if (declared !== undefined && declared > MAX_IMAGE_BYTES) return undefined; // never download the body const buf = Buffer.from(await result.response.arrayBuffer()); if (buf.byteLength === 0 || buf.byteLength > MAX_IMAGE_BYTES) return undefined; account?.(buf.byteLength); @@ -315,6 +326,27 @@ function ghHeaders(accept?: string): Record { return h; } +/** Rejection-immune settle twin of a round promise — the guard BOTH + * fire-and-forget launchers (kickBlogRefresh, runBlogRefreshRound's own + * round start) route through: the returned promise only tracks settlement, + * so an unexpected runRound reject can never leak as an unhandledRejection + * (Node ≥ 15 crashes the process on one); onSettle clears `inflight` + * (caller-guarded so an older round cannot clear a newer one). */ +export function settleRoundImmune(base: Promise, onSettle: () => void): Promise { + return base.then(() => undefined, () => undefined).finally(onSettle); +} + +/** Numeric `content-length` of a response — undefined when the header is + * absent or not a plain digit run. An over-limit declaration lets + * getText/getImage reject/miss BEFORE reading the body, so a huge response + * never enters RAM; the post-read byte checks stay the authority when the + * header is absent or lies. */ +function contentLengthOf(headers: Headers): number | undefined { + const v = headers.get("content-length"); + if (v === null || !/^\d+$/.test(v)) return undefined; + return Number(v); +} + /** Map with bounded parallelism (CONCURRENCY), preserving input order; the * first rejection rejects immediately (round fails). */ async function mapPool(items: T[], fn: (t: T) => Promise): Promise { diff --git a/src/web/client.ts b/src/web/client.ts index f15f5ac4bc..17e02a348a 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -125,12 +125,22 @@ export function adminRelayActiveInDraft(text: string): boolean { return (sec as Record).adminRelay === true; } +// #4906 P3: i18n bl.sub promises the feed turns over "约 1 小时一次", but the +// panel kept its ONE fetch for the whole tab lifetime — pure staleness gate +// for loadBlog's cache, inlined into the browser IIFE below via toString() +// (same single-implementation discipline as the JSONC helpers above). The 1h +// matches the server's hourly pull cadence (web/blog-refresh.ts ROUND_TTL_MS). +export function blogCacheStale(cachedAt: number, now: number): boolean { + return now - cachedAt >= 60 * 60 * 1000; +} + export const WEB_CLIENT = `(function () { "use strict"; const stripJsoncComments = ${stripJsoncComments.toString()}; const stripJsoncTrailingCommas = ${stripJsoncTrailingCommas.toString()}; const parseConfigDraft = ${parseConfigDraft.toString()}; const adminRelayActiveInDraft = ${adminRelayActiveInDraft.toString()}; + const blogCacheStale = ${blogCacheStale.toString()}; const MESSAGES=${JSON.stringify(MESSAGES)}; let locale = "zh-CN"; try { @@ -889,19 +899,41 @@ function changelogCatOf(rawLine) { // markdown with web/markdown.ts (escaped by construction), so this side only // lays out cards and expands bodies. Language rides the page-level locale; // switching languages reloads the page, so no incremental re-render hook. - let blogCache = null; + // #4906 P3: the cache carries a timestamp and goes stale after ~1h (the + // cadence bl.sub promises), so a long-lived tab re-pulls instead of pinning + // one fetch forever. A stale hit still paints instantly from cache; the + // re-pull runs silently in the background and re-renders only if the blog + // view is still on screen — the overview poll's discipline (no blocked + // first paint, no toasts from background refreshes). + let blogCache = null; // { data, at } + let blogSub = null; + let blogRefreshBusy = false; + function refreshBlogInBackground() { + if (blogRefreshBusy) return; + blogRefreshBusy = true; + json(API_ROOT + "/blog").then(function (feed) { + blogCache = { data: feed, at: Date.now() }; + if (current === "blog") loadBlog(blogSub); // cache is fresh now — no refresh loop + }, function () { + // keep the stale paint; the cache keeps its old timestamp, so the next visit retries + }).then(function () { blogRefreshBusy = false; }); + } async function loadBlog(sub) { const host = $("bl-list"); if (!host) return; - let data = blogCache; + blogSub = sub || null; + let data = blogCache ? blogCache.data : null; if (!data) { try { - data = blogCache = await json(API_ROOT + "/blog"); + data = await json(API_ROOT + "/blog"); + blogCache = { data: data, at: Date.now() }; } catch (e) { host.innerHTML = ""; toast(t("toast.failed", { msg: e.message }), "err"); return; } + } else if (blogCacheStale(blogCache.at, Date.now())) { + refreshBlogInBackground(); } const items = (data && Array.isArray(data.items)) ? data.items : []; const zhOn = locale === "zh-CN"; diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 0a76902450..30c6213503 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -39,7 +39,7 @@ export const MESSAGES: Record> = { "bl.empty": "这个版本还没内置文章。", "bl.back": "← 全部文章", "bl.nf": "文章不存在或已被移除。", - "bl.offline": "GitHub 暂时不可达——显示的是上次成功拉取或包内内容。", + "bl.offline": "上次拉取未完成,显示的是缓存内容(上次成功拉取或包内文章)。", "nav.connect": "接入", "nav.logs": "日志", "logs.title": "运行日志", @@ -702,7 +702,7 @@ export const MESSAGES: Record> = { "bl.empty": "No bundled articles in this build.", "bl.back": "\u2190 All articles", "bl.nf": "Article not found or removed from this build.", - "bl.offline": "GitHub unreachable right now — showing the last good pull or bundled content.", + "bl.offline": "Last refresh did not finish — showing cached content (the last good pull or bundled articles).", "nav.connect": "Connect", "nav.logs": "Logs", "logs.title": "Run log", diff --git a/tests/web-blog-refresh.test.ts b/tests/web-blog-refresh.test.ts index c21b574727..79ca4f5243 100644 --- a/tests/web-blog-refresh.test.ts +++ b/tests/web-blog-refresh.test.ts @@ -29,7 +29,10 @@ import { warmBlogRefreshOnce, _resetBlogRefreshForTest, _setBlogRefreshDisabledForTest, + settleRoundImmune, } from "../src/web/blog-refresh.ts"; +import { blogCacheStale } from "../src/web/client.ts"; +import { MESSAGES } from "../src/web/i18n.ts"; const CONTENTS_URL = "https://api.github.com/repos/ranxianglei/billion-context/contents/website/blog/posts?ref=master"; const rawUrl = (rel: string): string => `https://raw.githubusercontent.com/ranxianglei/billion-context/master/${rel}`; @@ -338,3 +341,76 @@ test("collectImageTargets: dedupes, skips scheme targets, resolves against the f "website/blog/shared/q.gif", ]); }); + +/** Response whose body is never supposed to be read: the precheck must trip + * on the declared content-length BEFORE arrayBuffer() is touched (#4906 P3). */ +function spyBody(body: ConstructorParameters[0], init?: ConstructorParameters[1]): { res: Response; bodyRead(): boolean } { + let read = false; + const res = new Response(body, init); + const orig = res.arrayBuffer.bind(res); + Object.defineProperty(res, "arrayBuffer", { value: async () => { read = true; return orig(); } }); + return { res, bodyRead: () => read }; +} + +test("#4906 P3 content-length precheck: oversized declarations refuse before the body is read", async () => { + installGithubRouter(); + assert.equal((await runBlogRefreshRound({ force: true })).ok, true); // good baseline + // TEXT: declared 600KB > MAX_TEXT_BYTES with a tiny real body — the + // precheck must fail the round without downloading a byte. + const textSpy = spyBody("tiny", { status: 200, headers: { "content-length": String(600 * 1024) } }); + router = (url) => { + if (url === CONTENTS_URL) return new Response(JSON.stringify([{ name: "2026-10-09_fresh.md", type: "file" }]), { status: 200 }); + if (url === rawUrl("website/blog/posts/2026-10-09_fresh.md")) return textSpy.res; + return new Response("not here", { status: 404 }); + }; + assert.equal((await runBlogRefreshRound({ force: true })).ok, true); // fail-soft, marker rides the feed + assert.equal(getBlogFeed().error, "offline"); + assert.equal(textSpy.bodyRead(), false, "oversize text: body never downloaded"); + // IMAGE: declared 3MB > MAX_IMAGE_BYTES — a silent miss (404-churn + // discipline), round still succeeds, body never downloaded. + installGithubRouter(); + assert.equal((await runBlogRefreshRound({ force: true })).ok, true); + const imgSpy = spyBody("IMG", { status: 200, headers: { "content-length": String(3 * 1024 * 1024) } }); + const base = router; // the default GitHub router installed above + router = (url) => (url === rawUrl("website/blog/posts/figures/good.png") ? imgSpy.res : base(url)); + assert.equal((await runBlogRefreshRound({ force: true })).ok, true, "oversize image is churn, not a failed round"); + assert.equal(getBlogFeed().error, undefined, "no marker: the round itself succeeded"); + assert.equal(imgSpy.bodyRead(), false, "oversize image: body never downloaded"); +}); + +test("#4906 P3 lying-small content-length: the post-read byte check stays the authority", async () => { + installGithubRouter(); + assert.equal((await runBlogRefreshRound({ force: true })).ok, true); + const big = "x".repeat(512 * 1024 + 1); + router = (url) => { + if (url === CONTENTS_URL) return new Response(JSON.stringify([{ name: "2026-10-09_fresh.md", type: "file" }]), { status: 200 }); + if (url === rawUrl("website/blog/posts/2026-10-09_fresh.md")) return new Response(big, { status: 200, headers: { "content-length": "10" } }); + return new Response("not here", { status: 404 }); + }; + assert.equal((await runBlogRefreshRound({ force: true })).ok, true); // fail-soft + assert.equal(getBlogFeed().error, "offline", "header said 10 bytes; the real 512KB+1 body still blew the limit"); +}); + +test("#4906 P3 settleRoundImmune: a rejecting round never leaks an unhandledRejection", async () => { + let unhandled: unknown = undefined; + const onUn = (e: unknown) => { unhandled = e; }; + process.on("unhandledRejection", onUn); + let settled = 0; + const gate = settleRoundImmune(Promise.reject(new Error("unexpected")), () => { settled++; }); + await gate; // resolves despite the rejecting base — fire-and-forget safe + await new Promise((r) => setTimeout(r, 20)); + process.off("unhandledRejection", onUn); + assert.equal(settled, 1, "onSettle ran exactly once"); + assert.equal(unhandled, undefined, "no unhandledRejection escaped (Node >= 15 would crash)"); + // Resolving bases settle too — the gate is direction-agnostic. + let settled2 = 0; + await settleRoundImmune(Promise.resolve(), () => { settled2++; }); + assert.equal(settled2, 1); +}); + +test("#4906 P3 blogCacheStale boundary + neutral offline wording pins", () => { + assert.equal(blogCacheStale(0, 60 * 60 * 1000 - 1), false, "just under 1h: cached feed still served"); + assert.equal(blogCacheStale(0, 60 * 60 * 1000), true, "at 1h: stale -> background re-pull (bl.sub cadence)"); + assert.ok(MESSAGES["zh-CN"]["bl.offline"].includes("上次拉取未完成"), "zh wording no longer blames GitHub reachability"); + assert.ok(MESSAGES["en"]["bl.offline"].startsWith("Last refresh did not finish"), "en wording no longer blames GitHub reachability"); +});