Support weak If-None-Match ETag comparison

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 03:11:52 +00:00
1 parent 55faded027
commit adac335c3f
3 files changed
+26 -1

No files matched your search

+10
View File
@@ -273,6 +273,16 @@ async function main() {
if ((conditionalAsset.headers.get("cache-control") ?? "").toLowerCase() !== assetCacheControl) {
throw new Error("Smoke test failed: expected cache-control preserved on conditional asset response.");
}
const weakConditionalAsset = await fetch(assetUrl, {
headers: {
"If-None-Match": `W/${assetEtag}`,
},
});
if (weakConditionalAsset.status !== 304) {
throw new Error(
`Smoke test failed: expected weak conditional asset status 304, received ${weakConditionalAsset.status}.`,
);
}
const conditionalHeadAsset = await fetch(assetUrl, {
method: "HEAD",
headers: {
+5
View File
@@ -951,6 +951,11 @@ describe("ifNoneMatchSatisfied", () => {
expect(ifNoneMatchSatisfied("\"foo\", \"bar\", \"abc\"", "\"abc\"")).toBe(true);
});
it("matches weak and strong forms using weak comparison semantics", () => {
expect(ifNoneMatchSatisfied("W/\"abc\"", "\"abc\"")).toBe(true);
expect(ifNoneMatchSatisfied("\"abc\"", "W/\"abc\"")).toBe(true);
});
it("does not match non-identical etags", () => {
expect(ifNoneMatchSatisfied("\"foo\", \"bar\"", "\"abc\"")).toBe(false);
});
+11 -1
View File
@@ -516,6 +516,11 @@ function staticEtagFor(stats: fs.Stats): string {
return `"${stats.size.toString(16)}-${Math.trunc(stats.mtimeMs).toString(16)}"`;
}
function normalizeWeakEtag(etag: string): string {
const trimmed = etag.trim();
return trimmed.startsWith("W/") ? trimmed.slice(2).trim() : trimmed;
}
export function ifNoneMatchSatisfied(ifNoneMatchHeader: string | undefined, etag: string): boolean {
if (!ifNoneMatchHeader) {
return false;
@@ -529,7 +534,12 @@ export function ifNoneMatchSatisfied(ifNoneMatchHeader: string | undefined, etag
return false;
}
return candidates.includes("*") || candidates.includes(etag);
if (candidates.includes("*")) {
return true;
}
const normalizedEtag = normalizeWeakEtag(etag);
return candidates.some((candidate) => normalizeWeakEtag(candidate) === normalizedEtag);
}
export function parseByteRangeHeader(