mirror of
https://github.com/morluto/rea.git
synced 2026-11-12 09:52:27 +00:00
* fix(website): keep the HTML validator when the Markdown route is absent A caller that prefers Markdown gets the HTML probe with its conditional headers deleted, so the Markdown route can be evaluated on its own. When the Markdown sidecar is missing the worker answers with that probe, which never saw the caller's `If-None-Match`, so a matching HTML ETag returned 200 instead of 304. Ask the asset service again with the caller's own request before answering with HTML. The decision moves into `htmlFallback` so the request handler keeps its complexity below the repository bound. Verified by executing `node --test website/worker.test.mjs`: without the change the new case fails with 200 instead of 304, and all four cases pass with it. `oxlint` reports no findings and `oxfmt --check` passes. The website worker test is not part of a CI workflow, so this suite runs locally only. * fix(scripts): resolve package-map imports in the module-boundary guard The guard resolved only relative, absolute and `file:` specifiers, so every `#`-prefixed package-map import returned no target and was skipped. A pure-layer file importing `#javascript-analysis-worker` passed, while the same import written relatively failed with a `pure-layer` diagnostic: the gate failed open on the spelling the package map exists to encourage. Resolve a package-map specifier to the source file that produces its built output, then apply the same ownership rules. The package map is read once per repository root. Verified with the new `scripts/lib/module-boundaries.test.mjs` (2 pass; the package-map case failed before the change) and by running the complete guard on this checkout, which still reports 4138 files with no findings, so the new rule adds no false positive. `oxfmt --check` passes; `oxlint` reports the same single pre-existing `failedBoundary` complexity warning it reported before this change. The script tests are not part of a CI workflow, so this suite runs locally only. * fix(scripts): normalize package targets as file URLs --------- Co-authored-by: 吴佳浩 <wujiahao@supremind.com>
119 lines
3.8 KiB
JavaScript
119 lines
3.8 KiB
JavaScript
// Negotiate prebuilt Markdown while keeping the static asset service's routing.
|
|
function prefersMarkdown(accept) {
|
|
const ranges = (accept ?? "")
|
|
.toLowerCase()
|
|
.split(",")
|
|
.map((range) => {
|
|
const [type, ...parameters] = range.trim().split(";");
|
|
const quality = parameters
|
|
.map((value) => value.trim())
|
|
.find((value) => value.startsWith("q="));
|
|
const q = quality === undefined ? 1 : Number(quality.slice(2));
|
|
return {
|
|
type: type.trim(),
|
|
q: Number.isFinite(q) && q >= 0 && q <= 1 ? q : 0,
|
|
};
|
|
});
|
|
const quality = (type) => {
|
|
for (const match of [type, "text/*", "*/*"]) {
|
|
const matches = ranges.filter((range) => range.type === match);
|
|
if (matches.length) return Math.max(...matches.map((range) => range.q));
|
|
}
|
|
return 0;
|
|
};
|
|
// Wildcards alone never opt a browser into Markdown; HTML wins ties.
|
|
return (
|
|
ranges.some((range) => range.type === "text/markdown") &&
|
|
quality("text/markdown") > 0 &&
|
|
quality("text/markdown") > quality("text/html")
|
|
);
|
|
}
|
|
|
|
function advertiseMarkdown(response, path) {
|
|
const vary = new Set(
|
|
(response.headers.get("Vary") ?? "")
|
|
.split(",")
|
|
.map((s) => s.trim())
|
|
.filter(Boolean),
|
|
);
|
|
if (![...vary].some((s) => s.toLowerCase() === "accept" || s === "*"))
|
|
vary.add("Accept");
|
|
response.headers.set("Vary", [...vary].join(", "));
|
|
response.headers.append(
|
|
"Link",
|
|
`<${path}>; rel="alternate"; type="text/markdown"`,
|
|
);
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* Answer with HTML when the Markdown route is absent.
|
|
*
|
|
* Reevaluate HTML only when probing removed conditional or range headers.
|
|
* Otherwise the existing response already answers the caller's request.
|
|
*/
|
|
const htmlFallback = async (original, request, probeChanged, env) => {
|
|
if (!probeChanged) return original;
|
|
await original.body?.cancel();
|
|
return env.ASSETS.fetch(request);
|
|
};
|
|
|
|
export default {
|
|
async fetch(request, env) {
|
|
const useMarkdown = prefersMarkdown(request.headers.get("Accept"));
|
|
// HTML validators describe a different representation. Probe the route first,
|
|
// then let the asset service evaluate validators/ranges against the Markdown.
|
|
const probe = new Request(request);
|
|
let probeChanged = false;
|
|
if (useMarkdown) {
|
|
for (const name of [
|
|
"If-None-Match",
|
|
"If-Modified-Since",
|
|
"If-Match",
|
|
"If-Unmodified-Since",
|
|
"If-Range",
|
|
"Range",
|
|
]) {
|
|
probeChanged ||= probe.headers.has(name);
|
|
probe.headers.delete(name);
|
|
}
|
|
}
|
|
const original = await env.ASSETS.fetch(probe);
|
|
if (
|
|
![200, 206, 304].includes(original.status) ||
|
|
!["GET", "HEAD"].includes(request.method) ||
|
|
!original.headers.get("Content-Type")?.startsWith("text/html")
|
|
)
|
|
return original;
|
|
|
|
const url = new URL(request.url);
|
|
if (!url.pathname.endsWith("/") && !url.pathname.endsWith(".html"))
|
|
return original;
|
|
const markdown = new URL(url);
|
|
markdown.pathname = url.pathname.endsWith("/")
|
|
? `${url.pathname}index.md`
|
|
: url.pathname.slice(0, -5) + ".md";
|
|
const candidate = await env.ASSETS.fetch(
|
|
useMarkdown
|
|
? new Request(markdown, request)
|
|
: new Request(markdown, { method: "HEAD" }),
|
|
);
|
|
if (!candidate.ok && ![304, 412, 416].includes(candidate.status)) {
|
|
await candidate.body?.cancel();
|
|
return htmlFallback(original, request, probeChanged, env);
|
|
}
|
|
const response = new Response(
|
|
useMarkdown ? candidate.body : original.body,
|
|
useMarkdown ? candidate : original,
|
|
);
|
|
if (useMarkdown) {
|
|
await original.body?.cancel();
|
|
if (candidate.status < 400)
|
|
response.headers.set("Content-Type", "text/markdown; charset=utf-8");
|
|
} else {
|
|
await candidate.body?.cancel();
|
|
}
|
|
return advertiseMarkdown(response, markdown.pathname);
|
|
},
|
|
};
|