Weak And Strong ETag Comparison
Parse entity-tag lists and evaluate If-Match and If-None-Match with the strong or weak comparison rules required by HTTP caching.
Code
export function parseETagList(header) {
if (typeof header !== "string") throw new TypeError("header must be a string");
const tags = [];
let index = 0;
while (index < header.length) {
while (index < header.length && (header[index] === "," || /\s/.test(header[index]))) {
index += 1;
}
if (index >= header.length) break;
let weak = false;
if (header.startsWith("W/", index)) {
weak = true;
index += 2;
}
if (header[index] !== '"') throw new SyntaxError("invalid ETag at index " + index);
index += 1;
const start = index;
while (index < header.length && header[index] !== '"') index += 1;
if (index >= header.length) throw new SyntaxError("unterminated ETag");
const opaque = header.slice(start, index);
index += 1;
tags.push({ weak, opaque });
}
return tags;
}
export function strongMatch(left, right) {
return Boolean(left && right && !left.weak && !right.weak && left.opaque === right.opaque);
}
export function weakMatch(left, right) {
return Boolean(left && right && left.opaque === right.opaque);
}
export function matchesIfNoneMatch(header, current) {
if (typeof header !== "string") throw new TypeError("header must be a string");
if (header.trim() === "*") return current != null;
return parseETagList(header).some((tag) => weakMatch(tag, current));
}
export function matchesIfMatch(header, current) {
if (typeof header !== "string") throw new TypeError("header must be a string");
if (header.trim() === "*") return current != null;
return parseETagList(header).some((tag) => strongMatch(tag, current));
}How the snippet works
The working surface is the functions parseETagList, strongMatch, weakMatch and matchesIfNoneMatch — everything else is wiring.
while (index < header.length) and while (index < header.length && (header[index] === "," || /\s/.test(header[index]))) nest, one inside the other. When the guard condition holds, TypeError and SyntaxError is thrown rather than a bogus value returned.
What to watch for
- The guard
typeof header !== "string"short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
The main signature reads export function parseETagList(header)
parseETagList is what another module would consume; nothing else here is load-bearing for callers.
Statements that matter
return parseETagList(header).some((tag) => strongMatch(tag, current));
return parseETagList(header).some((tag) => weakMatch(tag, current));
return Boolean(left && right && !left.weak && !right.weak && left.opaque === right.opaque);
Interactive Tools for This Topic
Cache-Control BuilderCompose a valid Cache-Control header from directives such as no-store, private, max-age, and immutable.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.Set-Cookie ParserParse one or more Set-Cookie header values into the cookie name, value, and attributes like Path, Domain, Expires, Max-Age, Secure, HttpOnly, and SameSite.
FAQ
- Does the code validate its input?
- `TypeError` and `SyntaxError` is raised at the guard (`typeof header !== "string"`), so invalid state never reaches the main logic.
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. The file defines `parseETagList`, `strongMatch`, `weakMatch` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.